aco: don't assume split_vector(create_vector) has the same number of elements when...
[mesa.git] / src / amd / compiler / aco_optimizer.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Daniel Schürmann (daniel.schuermann@campus.tu-berlin.de)
25 *
26 */
27
28 #include <algorithm>
29 #include <math.h>
30
31 #include "aco_ir.h"
32 #include "util/half_float.h"
33 #include "util/u_math.h"
34
35 namespace aco {
36
37 /**
38 * The optimizer works in 4 phases:
39 * (1) The first pass collects information for each ssa-def,
40 * propagates reg->reg operands of the same type, inline constants
41 * and neg/abs input modifiers.
42 * (2) The second pass combines instructions like mad, omod, clamp and
43 * propagates sgpr's on VALU instructions.
44 * This pass depends on information collected in the first pass.
45 * (3) The third pass goes backwards, and selects instructions,
46 * i.e. decides if a mad instruction is profitable and eliminates dead code.
47 * (4) The fourth pass cleans up the sequence: literals get applied and dead
48 * instructions are removed from the sequence.
49 */
50
51
52 struct mad_info {
53 aco_ptr<Instruction> add_instr;
54 uint32_t mul_temp_id;
55 uint32_t literal_idx;
56 bool check_literal;
57
58 mad_info(aco_ptr<Instruction> instr, uint32_t id)
59 : add_instr(std::move(instr)), mul_temp_id(id), check_literal(false) {}
60 };
61
62 enum Label {
63 label_vec = 1 << 0,
64 label_constant = 1 << 1,
65 label_abs = 1 << 2,
66 label_neg = 1 << 3,
67 label_mul = 1 << 4,
68 label_temp = 1 << 5,
69 label_literal = 1 << 6,
70 label_mad = 1 << 7,
71 label_omod2 = 1 << 8,
72 label_omod4 = 1 << 9,
73 label_omod5 = 1 << 10,
74 label_omod_success = 1 << 11,
75 label_clamp = 1 << 12,
76 label_clamp_success = 1 << 13,
77 label_undefined = 1 << 14,
78 label_vcc = 1 << 15,
79 label_b2f = 1 << 16,
80 label_add_sub = 1 << 17,
81 label_bitwise = 1 << 18,
82 label_minmax = 1 << 19,
83 label_fcmp = 1 << 20,
84 label_uniform_bool = 1 << 21,
85 label_constant_64bit = 1 << 22,
86 label_uniform_bitwise = 1 << 23,
87 label_scc_invert = 1 << 24,
88 label_vcc_hint = 1 << 25,
89 label_scc_needed = 1 << 26,
90 };
91
92 static constexpr uint32_t instr_labels = label_vec | label_mul | label_mad | label_omod_success | label_clamp_success |
93 label_add_sub | label_bitwise | label_uniform_bitwise | label_minmax | label_fcmp;
94 static constexpr uint32_t temp_labels = label_abs | label_neg | label_temp | label_vcc | label_b2f | label_uniform_bool |
95 label_omod2 | label_omod4 | label_omod5 | label_clamp | label_scc_invert;
96 static constexpr uint32_t val_labels = label_constant | label_constant_64bit | label_literal | label_mad;
97
98 struct ssa_info {
99 uint32_t val;
100 union {
101 Temp temp;
102 Instruction* instr;
103 };
104 uint32_t label;
105
106 void add_label(Label new_label)
107 {
108 /* Since all labels which use "instr" use it for the same thing
109 * (indicating the defining instruction), there is no need to clear
110 * any other instr labels. */
111 if (new_label & instr_labels)
112 label &= ~temp_labels; /* instr and temp alias */
113
114 if (new_label & temp_labels) {
115 label &= ~temp_labels;
116 label &= ~instr_labels; /* instr and temp alias */
117 }
118
119 if (new_label & val_labels)
120 label &= ~val_labels;
121
122 label |= new_label;
123 }
124
125 void set_vec(Instruction* vec)
126 {
127 add_label(label_vec);
128 instr = vec;
129 }
130
131 bool is_vec()
132 {
133 return label & label_vec;
134 }
135
136 void set_constant(uint32_t constant)
137 {
138 add_label(label_constant);
139 val = constant;
140 }
141
142 bool is_constant()
143 {
144 return label & label_constant;
145 }
146
147 void set_constant_64bit(uint32_t constant)
148 {
149 add_label(label_constant_64bit);
150 val = constant;
151 }
152
153 bool is_constant_64bit()
154 {
155 return label & label_constant_64bit;
156 }
157
158 void set_abs(Temp abs_temp)
159 {
160 add_label(label_abs);
161 temp = abs_temp;
162 }
163
164 bool is_abs()
165 {
166 return label & label_abs;
167 }
168
169 void set_neg(Temp neg_temp)
170 {
171 add_label(label_neg);
172 temp = neg_temp;
173 }
174
175 bool is_neg()
176 {
177 return label & label_neg;
178 }
179
180 void set_neg_abs(Temp neg_abs_temp)
181 {
182 add_label((Label)((uint32_t)label_abs | (uint32_t)label_neg));
183 temp = neg_abs_temp;
184 }
185
186 void set_mul(Instruction* mul)
187 {
188 add_label(label_mul);
189 instr = mul;
190 }
191
192 bool is_mul()
193 {
194 return label & label_mul;
195 }
196
197 void set_temp(Temp tmp)
198 {
199 add_label(label_temp);
200 temp = tmp;
201 }
202
203 bool is_temp()
204 {
205 return label & label_temp;
206 }
207
208 void set_literal(uint32_t lit)
209 {
210 add_label(label_literal);
211 val = lit;
212 }
213
214 bool is_literal()
215 {
216 return label & label_literal;
217 }
218
219 void set_mad(Instruction* mad, uint32_t mad_info_idx)
220 {
221 add_label(label_mad);
222 val = mad_info_idx;
223 instr = mad;
224 }
225
226 bool is_mad()
227 {
228 return label & label_mad;
229 }
230
231 void set_omod2(Temp def)
232 {
233 add_label(label_omod2);
234 temp = def;
235 }
236
237 bool is_omod2()
238 {
239 return label & label_omod2;
240 }
241
242 void set_omod4(Temp def)
243 {
244 add_label(label_omod4);
245 temp = def;
246 }
247
248 bool is_omod4()
249 {
250 return label & label_omod4;
251 }
252
253 void set_omod5(Temp def)
254 {
255 add_label(label_omod5);
256 temp = def;
257 }
258
259 bool is_omod5()
260 {
261 return label & label_omod5;
262 }
263
264 void set_omod_success(Instruction* omod_instr)
265 {
266 add_label(label_omod_success);
267 instr = omod_instr;
268 }
269
270 bool is_omod_success()
271 {
272 return label & label_omod_success;
273 }
274
275 void set_clamp(Temp def)
276 {
277 add_label(label_clamp);
278 temp = def;
279 }
280
281 bool is_clamp()
282 {
283 return label & label_clamp;
284 }
285
286 void set_clamp_success(Instruction* clamp_instr)
287 {
288 add_label(label_clamp_success);
289 instr = clamp_instr;
290 }
291
292 bool is_clamp_success()
293 {
294 return label & label_clamp_success;
295 }
296
297 void set_undefined()
298 {
299 add_label(label_undefined);
300 }
301
302 bool is_undefined()
303 {
304 return label & label_undefined;
305 }
306
307 void set_vcc(Temp vcc)
308 {
309 add_label(label_vcc);
310 temp = vcc;
311 }
312
313 bool is_vcc()
314 {
315 return label & label_vcc;
316 }
317
318 bool is_constant_or_literal()
319 {
320 return is_constant() || is_literal();
321 }
322
323 void set_b2f(Temp val)
324 {
325 add_label(label_b2f);
326 temp = val;
327 }
328
329 bool is_b2f()
330 {
331 return label & label_b2f;
332 }
333
334 void set_add_sub(Instruction *add_sub_instr)
335 {
336 add_label(label_add_sub);
337 instr = add_sub_instr;
338 }
339
340 bool is_add_sub()
341 {
342 return label & label_add_sub;
343 }
344
345 void set_bitwise(Instruction *bitwise_instr)
346 {
347 add_label(label_bitwise);
348 instr = bitwise_instr;
349 }
350
351 bool is_bitwise()
352 {
353 return label & label_bitwise;
354 }
355
356 void set_uniform_bitwise()
357 {
358 add_label(label_uniform_bitwise);
359 }
360
361 bool is_uniform_bitwise()
362 {
363 return label & label_uniform_bitwise;
364 }
365
366 void set_minmax(Instruction *minmax_instr)
367 {
368 add_label(label_minmax);
369 instr = minmax_instr;
370 }
371
372 bool is_minmax()
373 {
374 return label & label_minmax;
375 }
376
377 void set_fcmp(Instruction *fcmp_instr)
378 {
379 add_label(label_fcmp);
380 instr = fcmp_instr;
381 }
382
383 bool is_fcmp()
384 {
385 return label & label_fcmp;
386 }
387
388 void set_scc_needed()
389 {
390 add_label(label_scc_needed);
391 }
392
393 bool is_scc_needed()
394 {
395 return label & label_scc_needed;
396 }
397
398 void set_scc_invert(Temp scc_inv)
399 {
400 add_label(label_scc_invert);
401 temp = scc_inv;
402 }
403
404 bool is_scc_invert()
405 {
406 return label & label_scc_invert;
407 }
408
409 void set_uniform_bool(Temp uniform_bool)
410 {
411 add_label(label_uniform_bool);
412 temp = uniform_bool;
413 }
414
415 bool is_uniform_bool()
416 {
417 return label & label_uniform_bool;
418 }
419
420 void set_vcc_hint()
421 {
422 add_label(label_vcc_hint);
423 }
424
425 bool is_vcc_hint()
426 {
427 return label & label_vcc_hint;
428 }
429 };
430
431 struct opt_ctx {
432 Program* program;
433 std::vector<aco_ptr<Instruction>> instructions;
434 ssa_info* info;
435 std::pair<uint32_t,Temp> last_literal;
436 std::vector<mad_info> mad_infos;
437 std::vector<uint16_t> uses;
438 };
439
440 bool can_swap_operands(aco_ptr<Instruction>& instr)
441 {
442 if (instr->operands[0].isConstant() ||
443 (instr->operands[0].isTemp() && instr->operands[0].getTemp().type() == RegType::sgpr))
444 return false;
445
446 switch (instr->opcode) {
447 case aco_opcode::v_add_f32:
448 case aco_opcode::v_mul_f32:
449 case aco_opcode::v_or_b32:
450 case aco_opcode::v_and_b32:
451 case aco_opcode::v_xor_b32:
452 case aco_opcode::v_max_f32:
453 case aco_opcode::v_min_f32:
454 case aco_opcode::v_max_i32:
455 case aco_opcode::v_min_i32:
456 case aco_opcode::v_max_u32:
457 case aco_opcode::v_min_u32:
458 case aco_opcode::v_cmp_eq_f32:
459 case aco_opcode::v_cmp_lg_f32:
460 return true;
461 case aco_opcode::v_sub_f32:
462 instr->opcode = aco_opcode::v_subrev_f32;
463 return true;
464 case aco_opcode::v_cmp_lt_f32:
465 instr->opcode = aco_opcode::v_cmp_gt_f32;
466 return true;
467 case aco_opcode::v_cmp_ge_f32:
468 instr->opcode = aco_opcode::v_cmp_le_f32;
469 return true;
470 case aco_opcode::v_cmp_lt_i32:
471 instr->opcode = aco_opcode::v_cmp_gt_i32;
472 return true;
473 default:
474 return false;
475 }
476 }
477
478 bool can_use_VOP3(opt_ctx& ctx, aco_ptr<Instruction>& instr)
479 {
480 if (instr->isVOP3())
481 return true;
482
483 if (instr->operands.size() && instr->operands[0].isLiteral() && ctx.program->chip_class < GFX10)
484 return false;
485
486 if (instr->isDPP() || instr->isSDWA())
487 return false;
488
489 return instr->opcode != aco_opcode::v_madmk_f32 &&
490 instr->opcode != aco_opcode::v_madak_f32 &&
491 instr->opcode != aco_opcode::v_madmk_f16 &&
492 instr->opcode != aco_opcode::v_madak_f16 &&
493 instr->opcode != aco_opcode::v_fmamk_f32 &&
494 instr->opcode != aco_opcode::v_fmaak_f32 &&
495 instr->opcode != aco_opcode::v_fmamk_f16 &&
496 instr->opcode != aco_opcode::v_fmaak_f16 &&
497 instr->opcode != aco_opcode::v_readlane_b32 &&
498 instr->opcode != aco_opcode::v_writelane_b32 &&
499 instr->opcode != aco_opcode::v_readfirstlane_b32;
500 }
501
502 bool can_apply_sgprs(aco_ptr<Instruction>& instr)
503 {
504 return instr->opcode != aco_opcode::v_readfirstlane_b32 &&
505 instr->opcode != aco_opcode::v_readlane_b32 &&
506 instr->opcode != aco_opcode::v_readlane_b32_e64 &&
507 instr->opcode != aco_opcode::v_writelane_b32 &&
508 instr->opcode != aco_opcode::v_writelane_b32_e64;
509 }
510
511 void to_VOP3(opt_ctx& ctx, aco_ptr<Instruction>& instr)
512 {
513 if (instr->isVOP3())
514 return;
515
516 aco_ptr<Instruction> tmp = std::move(instr);
517 Format format = asVOP3(tmp->format);
518 instr.reset(create_instruction<VOP3A_instruction>(tmp->opcode, format, tmp->operands.size(), tmp->definitions.size()));
519 std::copy(tmp->operands.cbegin(), tmp->operands.cend(), instr->operands.begin());
520 for (unsigned i = 0; i < instr->definitions.size(); i++) {
521 instr->definitions[i] = tmp->definitions[i];
522 if (instr->definitions[i].isTemp()) {
523 ssa_info& info = ctx.info[instr->definitions[i].tempId()];
524 if (info.label & instr_labels && info.instr == tmp.get())
525 info.instr = instr.get();
526 }
527 }
528 }
529
530 /* only covers special cases */
531 bool alu_can_accept_constant(aco_opcode opcode, unsigned operand)
532 {
533 switch (opcode) {
534 case aco_opcode::v_interp_p2_f32:
535 case aco_opcode::v_mac_f32:
536 case aco_opcode::v_writelane_b32:
537 case aco_opcode::v_writelane_b32_e64:
538 case aco_opcode::v_cndmask_b32:
539 return operand != 2;
540 case aco_opcode::s_addk_i32:
541 case aco_opcode::s_mulk_i32:
542 case aco_opcode::p_wqm:
543 case aco_opcode::p_extract_vector:
544 case aco_opcode::p_split_vector:
545 case aco_opcode::v_readlane_b32:
546 case aco_opcode::v_readlane_b32_e64:
547 case aco_opcode::v_readfirstlane_b32:
548 return operand != 0;
549 default:
550 return true;
551 }
552 }
553
554 bool valu_can_accept_vgpr(aco_ptr<Instruction>& instr, unsigned operand)
555 {
556 if (instr->opcode == aco_opcode::v_readlane_b32 || instr->opcode == aco_opcode::v_readlane_b32_e64 ||
557 instr->opcode == aco_opcode::v_writelane_b32 || instr->opcode == aco_opcode::v_writelane_b32_e64)
558 return operand != 1;
559 return true;
560 }
561
562 /* check constant bus and literal limitations */
563 bool check_vop3_operands(opt_ctx& ctx, unsigned num_operands, Operand *operands)
564 {
565 int limit = ctx.program->chip_class >= GFX10 ? 2 : 1;
566 Operand literal32(s1);
567 Operand literal64(s2);
568 unsigned num_sgprs = 0;
569 unsigned sgpr[] = {0, 0};
570
571 for (unsigned i = 0; i < num_operands; i++) {
572 Operand op = operands[i];
573
574 if (op.hasRegClass() && op.regClass().type() == RegType::sgpr) {
575 /* two reads of the same SGPR count as 1 to the limit */
576 if (op.tempId() != sgpr[0] && op.tempId() != sgpr[1]) {
577 if (num_sgprs < 2)
578 sgpr[num_sgprs++] = op.tempId();
579 limit--;
580 if (limit < 0)
581 return false;
582 }
583 } else if (op.isLiteral()) {
584 if (ctx.program->chip_class < GFX10)
585 return false;
586
587 if (!literal32.isUndefined() && literal32.constantValue() != op.constantValue())
588 return false;
589 if (!literal64.isUndefined() && literal64.constantValue() != op.constantValue())
590 return false;
591
592 /* Any number of 32-bit literals counts as only 1 to the limit. Same
593 * (but separately) for 64-bit literals. */
594 if (op.size() == 1 && literal32.isUndefined()) {
595 limit--;
596 literal32 = op;
597 } else if (op.size() == 2 && literal64.isUndefined()) {
598 limit--;
599 literal64 = op;
600 }
601
602 if (limit < 0)
603 return false;
604 }
605 }
606
607 return true;
608 }
609
610 bool parse_base_offset(opt_ctx &ctx, Instruction* instr, unsigned op_index, Temp *base, uint32_t *offset)
611 {
612 Operand op = instr->operands[op_index];
613
614 if (!op.isTemp())
615 return false;
616 Temp tmp = op.getTemp();
617 if (!ctx.info[tmp.id()].is_add_sub())
618 return false;
619
620 Instruction *add_instr = ctx.info[tmp.id()].instr;
621
622 switch (add_instr->opcode) {
623 case aco_opcode::v_add_u32:
624 case aco_opcode::v_add_co_u32:
625 case aco_opcode::v_add_co_u32_e64:
626 case aco_opcode::s_add_i32:
627 case aco_opcode::s_add_u32:
628 break;
629 default:
630 return false;
631 }
632
633 if (add_instr->usesModifiers())
634 return false;
635
636 for (unsigned i = 0; i < 2; i++) {
637 if (add_instr->operands[i].isConstant()) {
638 *offset = add_instr->operands[i].constantValue();
639 } else if (add_instr->operands[i].isTemp() &&
640 ctx.info[add_instr->operands[i].tempId()].is_constant_or_literal()) {
641 *offset = ctx.info[add_instr->operands[i].tempId()].val;
642 } else {
643 continue;
644 }
645 if (!add_instr->operands[!i].isTemp())
646 continue;
647
648 uint32_t offset2 = 0;
649 if (parse_base_offset(ctx, add_instr, !i, base, &offset2)) {
650 *offset += offset2;
651 } else {
652 *base = add_instr->operands[!i].getTemp();
653 }
654 return true;
655 }
656
657 return false;
658 }
659
660 Operand get_constant_op(opt_ctx &ctx, uint32_t val, bool is64bit = false)
661 {
662 // TODO: this functions shouldn't be needed if we store Operand instead of value.
663 Operand op(val, is64bit);
664 if (val == 0x3e22f983 && ctx.program->chip_class >= GFX8)
665 op.setFixed(PhysReg{248}); /* 1/2 PI can be an inline constant on GFX8+ */
666 return op;
667 }
668
669 bool fixed_to_exec(Operand op)
670 {
671 return op.isFixed() && op.physReg() == exec;
672 }
673
674 void label_instruction(opt_ctx &ctx, Block& block, aco_ptr<Instruction>& instr)
675 {
676 if (instr->isSALU() || instr->isVALU() || instr->format == Format::PSEUDO) {
677 ASSERTED bool all_const = false;
678 for (Operand& op : instr->operands)
679 all_const = all_const && (!op.isTemp() || ctx.info[op.tempId()].is_constant_or_literal());
680 perfwarn(all_const, "All instruction operands are constant", instr.get());
681 }
682
683 for (unsigned i = 0; i < instr->operands.size(); i++)
684 {
685 if (!instr->operands[i].isTemp())
686 continue;
687
688 ssa_info info = ctx.info[instr->operands[i].tempId()];
689 /* propagate undef */
690 if (info.is_undefined() && is_phi(instr))
691 instr->operands[i] = Operand(instr->operands[i].regClass());
692 /* propagate reg->reg of same type */
693 if (info.is_temp() && info.temp.regClass() == instr->operands[i].getTemp().regClass()) {
694 instr->operands[i].setTemp(ctx.info[instr->operands[i].tempId()].temp);
695 info = ctx.info[info.temp.id()];
696 }
697
698 /* SALU / PSEUDO: propagate inline constants */
699 if (instr->isSALU() || instr->format == Format::PSEUDO) {
700 if (info.is_temp() && info.temp.type() == RegType::sgpr) {
701 const bool is_subdword = std::any_of(instr->definitions.begin(), instr->definitions.end(),
702 [] (const Definition& def) { return def.regClass().is_subdword();});
703 if (instr->isSALU() || !is_subdword) {
704 instr->operands[i].setTemp(info.temp);
705 info = ctx.info[info.temp.id()];
706 }
707 } else if (info.is_temp() && info.temp.type() == RegType::vgpr) {
708 /* propagate vgpr if it can take it */
709 switch (instr->opcode) {
710 case aco_opcode::p_create_vector:
711 case aco_opcode::p_split_vector:
712 case aco_opcode::p_extract_vector:
713 case aco_opcode::p_phi: {
714 const bool all_vgpr = std::none_of(instr->definitions.begin(), instr->definitions.end(),
715 [] (const Definition& def) { return def.getTemp().type() != RegType::vgpr;});
716 if (all_vgpr) {
717 instr->operands[i] = Operand(info.temp);
718 info = ctx.info[info.temp.id()];
719 }
720 break;
721 }
722 default:
723 break;
724 }
725 }
726 if ((info.is_constant() || info.is_constant_64bit() || (info.is_literal() && instr->format == Format::PSEUDO)) &&
727 !instr->operands[i].isFixed() && alu_can_accept_constant(instr->opcode, i)) {
728 instr->operands[i] = get_constant_op(ctx, info.val, info.is_constant_64bit());
729 continue;
730 }
731 }
732
733 /* VALU: propagate neg, abs & inline constants */
734 else if (instr->isVALU()) {
735 if (info.is_temp() && info.temp.type() == RegType::vgpr && valu_can_accept_vgpr(instr, i)) {
736 instr->operands[i].setTemp(info.temp);
737 info = ctx.info[info.temp.id()];
738 }
739 if (info.is_abs() && (can_use_VOP3(ctx, instr) || instr->isDPP()) && instr_info.can_use_input_modifiers[(int)instr->opcode]) {
740 if (!instr->isDPP())
741 to_VOP3(ctx, instr);
742 instr->operands[i] = Operand(info.temp);
743 if (instr->isDPP())
744 static_cast<DPP_instruction*>(instr.get())->abs[i] = true;
745 else
746 static_cast<VOP3A_instruction*>(instr.get())->abs[i] = true;
747 }
748 if (info.is_neg() && instr->opcode == aco_opcode::v_add_f32) {
749 instr->opcode = i ? aco_opcode::v_sub_f32 : aco_opcode::v_subrev_f32;
750 instr->operands[i].setTemp(info.temp);
751 continue;
752 } else if (info.is_neg() && (can_use_VOP3(ctx, instr) || instr->isDPP()) && instr_info.can_use_input_modifiers[(int)instr->opcode]) {
753 if (!instr->isDPP())
754 to_VOP3(ctx, instr);
755 instr->operands[i].setTemp(info.temp);
756 if (instr->isDPP())
757 static_cast<DPP_instruction*>(instr.get())->neg[i] = true;
758 else
759 static_cast<VOP3A_instruction*>(instr.get())->neg[i] = true;
760 continue;
761 }
762 if ((info.is_constant() || info.is_constant_64bit()) && alu_can_accept_constant(instr->opcode, i)) {
763 Operand op = get_constant_op(ctx, info.val, info.is_constant_64bit());
764 perfwarn(instr->opcode == aco_opcode::v_cndmask_b32 && i == 2, "v_cndmask_b32 with a constant selector", instr.get());
765 if (i == 0 || instr->opcode == aco_opcode::v_readlane_b32 || instr->opcode == aco_opcode::v_writelane_b32) {
766 instr->operands[i] = op;
767 continue;
768 } else if (!instr->isVOP3() && can_swap_operands(instr)) {
769 instr->operands[i] = instr->operands[0];
770 instr->operands[0] = op;
771 continue;
772 } else if (can_use_VOP3(ctx, instr)) {
773 to_VOP3(ctx, instr);
774 instr->operands[i] = op;
775 continue;
776 }
777 }
778 }
779
780 /* MUBUF: propagate constants and combine additions */
781 else if (instr->format == Format::MUBUF) {
782 MUBUF_instruction *mubuf = static_cast<MUBUF_instruction *>(instr.get());
783 Temp base;
784 uint32_t offset;
785 while (info.is_temp())
786 info = ctx.info[info.temp.id()];
787
788 if (mubuf->offen && i == 1 && info.is_constant_or_literal() && mubuf->offset + info.val < 4096) {
789 assert(!mubuf->idxen);
790 instr->operands[1] = Operand(v1);
791 mubuf->offset += info.val;
792 mubuf->offen = false;
793 continue;
794 } else if (i == 2 && info.is_constant_or_literal() && mubuf->offset + info.val < 4096) {
795 instr->operands[2] = Operand((uint32_t) 0);
796 mubuf->offset += info.val;
797 continue;
798 } else if (mubuf->offen && i == 1 && parse_base_offset(ctx, instr.get(), i, &base, &offset) && base.regClass() == v1 && mubuf->offset + offset < 4096) {
799 assert(!mubuf->idxen);
800 instr->operands[1].setTemp(base);
801 mubuf->offset += offset;
802 continue;
803 } else if (i == 2 && parse_base_offset(ctx, instr.get(), i, &base, &offset) && base.regClass() == s1 && mubuf->offset + offset < 4096) {
804 instr->operands[i].setTemp(base);
805 mubuf->offset += offset;
806 continue;
807 }
808 }
809
810 /* DS: combine additions */
811 else if (instr->format == Format::DS) {
812
813 DS_instruction *ds = static_cast<DS_instruction *>(instr.get());
814 Temp base;
815 uint32_t offset;
816 bool has_usable_ds_offset = ctx.program->chip_class >= GFX7;
817 if (has_usable_ds_offset &&
818 i == 0 && parse_base_offset(ctx, instr.get(), i, &base, &offset) &&
819 base.regClass() == instr->operands[i].regClass() &&
820 instr->opcode != aco_opcode::ds_swizzle_b32) {
821 if (instr->opcode == aco_opcode::ds_write2_b32 || instr->opcode == aco_opcode::ds_read2_b32 ||
822 instr->opcode == aco_opcode::ds_write2_b64 || instr->opcode == aco_opcode::ds_read2_b64) {
823 unsigned mask = (instr->opcode == aco_opcode::ds_write2_b64 || instr->opcode == aco_opcode::ds_read2_b64) ? 0x7 : 0x3;
824 unsigned shifts = (instr->opcode == aco_opcode::ds_write2_b64 || instr->opcode == aco_opcode::ds_read2_b64) ? 3 : 2;
825
826 if ((offset & mask) == 0 &&
827 ds->offset0 + (offset >> shifts) <= 255 &&
828 ds->offset1 + (offset >> shifts) <= 255) {
829 instr->operands[i].setTemp(base);
830 ds->offset0 += offset >> shifts;
831 ds->offset1 += offset >> shifts;
832 }
833 } else {
834 if (ds->offset0 + offset <= 65535) {
835 instr->operands[i].setTemp(base);
836 ds->offset0 += offset;
837 }
838 }
839 }
840 }
841
842 /* SMEM: propagate constants and combine additions */
843 else if (instr->format == Format::SMEM) {
844
845 SMEM_instruction *smem = static_cast<SMEM_instruction *>(instr.get());
846 Temp base;
847 uint32_t offset;
848 if (i == 1 && info.is_constant_or_literal() &&
849 ((ctx.program->chip_class == GFX6 && info.val <= 0x3FF) ||
850 (ctx.program->chip_class == GFX7 && info.val <= 0xFFFFFFFF) ||
851 (ctx.program->chip_class >= GFX8 && info.val <= 0xFFFFF))) {
852 instr->operands[i] = Operand(info.val);
853 continue;
854 } else if (i == 1 && parse_base_offset(ctx, instr.get(), i, &base, &offset) && base.regClass() == s1 && offset <= 0xFFFFF && ctx.program->chip_class >= GFX9) {
855 bool soe = smem->operands.size() >= (!smem->definitions.empty() ? 3 : 4);
856 if (soe &&
857 (!ctx.info[smem->operands.back().tempId()].is_constant_or_literal() ||
858 ctx.info[smem->operands.back().tempId()].val != 0)) {
859 continue;
860 }
861 if (soe) {
862 smem->operands[1] = Operand(offset);
863 smem->operands.back() = Operand(base);
864 } else {
865 SMEM_instruction *new_instr = create_instruction<SMEM_instruction>(smem->opcode, Format::SMEM, smem->operands.size() + 1, smem->definitions.size());
866 new_instr->operands[0] = smem->operands[0];
867 new_instr->operands[1] = Operand(offset);
868 if (smem->definitions.empty())
869 new_instr->operands[2] = smem->operands[2];
870 new_instr->operands.back() = Operand(base);
871 if (!smem->definitions.empty())
872 new_instr->definitions[0] = smem->definitions[0];
873 new_instr->can_reorder = smem->can_reorder;
874 new_instr->barrier = smem->barrier;
875 instr.reset(new_instr);
876 smem = static_cast<SMEM_instruction *>(instr.get());
877 }
878 continue;
879 }
880 }
881
882 else if (instr->format == Format::PSEUDO_BRANCH) {
883 if (ctx.info[instr->operands[0].tempId()].is_scc_invert()) {
884 /* Flip the branch instruction to get rid of the scc_invert instruction */
885 instr->opcode = instr->opcode == aco_opcode::p_cbranch_z ? aco_opcode::p_cbranch_nz : aco_opcode::p_cbranch_z;
886 instr->operands[0].setTemp(ctx.info[instr->operands[0].tempId()].temp);
887 }
888 }
889 }
890
891 /* if this instruction doesn't define anything, return */
892 if (instr->definitions.empty())
893 return;
894
895 switch (instr->opcode) {
896 case aco_opcode::p_create_vector: {
897 unsigned num_ops = instr->operands.size();
898 for (const Operand& op : instr->operands) {
899 if (op.isTemp() && ctx.info[op.tempId()].is_vec())
900 num_ops += ctx.info[op.tempId()].instr->operands.size() - 1;
901 }
902 if (num_ops != instr->operands.size()) {
903 aco_ptr<Instruction> old_vec = std::move(instr);
904 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_ops, 1));
905 instr->definitions[0] = old_vec->definitions[0];
906 unsigned k = 0;
907 for (Operand& old_op : old_vec->operands) {
908 if (old_op.isTemp() && ctx.info[old_op.tempId()].is_vec()) {
909 for (unsigned j = 0; j < ctx.info[old_op.tempId()].instr->operands.size(); j++) {
910 Operand op = ctx.info[old_op.tempId()].instr->operands[j];
911 if (op.isTemp() && ctx.info[op.tempId()].is_temp() &&
912 ctx.info[op.tempId()].temp.type() == instr->definitions[0].regClass().type())
913 op.setTemp(ctx.info[op.tempId()].temp);
914 instr->operands[k++] = op;
915 }
916 } else {
917 instr->operands[k++] = old_op;
918 }
919 }
920 assert(k == num_ops);
921 }
922 if (instr->operands.size() == 1 && instr->operands[0].isTemp())
923 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
924 else if (instr->definitions[0].getTemp().size() == instr->operands.size())
925 ctx.info[instr->definitions[0].tempId()].set_vec(instr.get());
926 break;
927 }
928 case aco_opcode::p_split_vector: {
929 if (!ctx.info[instr->operands[0].tempId()].is_vec())
930 break;
931 Instruction* vec = ctx.info[instr->operands[0].tempId()].instr;
932 if (instr->definitions.size() != vec->operands.size())
933 break;
934 for (unsigned i = 0; i < instr->definitions.size(); i++) {
935 Operand vec_op = vec->operands[i];
936 if (vec_op.isConstant()) {
937 if (vec_op.isLiteral())
938 ctx.info[instr->definitions[i].tempId()].set_literal(vec_op.constantValue());
939 else if (vec_op.size() == 1)
940 ctx.info[instr->definitions[i].tempId()].set_constant(vec_op.constantValue());
941 else if (vec_op.size() == 2)
942 ctx.info[instr->definitions[i].tempId()].set_constant_64bit(vec_op.constantValue());
943 } else {
944 assert(vec_op.isTemp());
945 ctx.info[instr->definitions[i].tempId()].set_temp(vec_op.getTemp());
946 }
947 }
948 break;
949 }
950 case aco_opcode::p_extract_vector: { /* mov */
951 if (!ctx.info[instr->operands[0].tempId()].is_vec())
952 break;
953 Instruction* vec = ctx.info[instr->operands[0].tempId()].instr;
954 if (vec->definitions[0].getTemp().size() == vec->operands.size() && /* TODO: what about 64bit or other combinations? */
955 vec->operands[0].size() == instr->definitions[0].size()) {
956
957 /* convert this extract into a mov instruction */
958 Operand vec_op = vec->operands[instr->operands[1].constantValue()];
959 bool is_vgpr = instr->definitions[0].getTemp().type() == RegType::vgpr;
960 aco_opcode opcode = is_vgpr ? aco_opcode::v_mov_b32 : aco_opcode::s_mov_b32;
961 Format format = is_vgpr ? Format::VOP1 : Format::SOP1;
962 instr->opcode = opcode;
963 instr->format = format;
964 while (instr->operands.size() > 1)
965 instr->operands.pop_back();
966 instr->operands[0] = vec_op;
967
968 if (vec_op.isConstant()) {
969 if (vec_op.isLiteral())
970 ctx.info[instr->definitions[0].tempId()].set_literal(vec_op.constantValue());
971 else if (vec_op.size() == 1)
972 ctx.info[instr->definitions[0].tempId()].set_constant(vec_op.constantValue());
973 else if (vec_op.size() == 2)
974 ctx.info[instr->definitions[0].tempId()].set_constant_64bit(vec_op.constantValue());
975
976 } else {
977 assert(vec_op.isTemp());
978 ctx.info[instr->definitions[0].tempId()].set_temp(vec_op.getTemp());
979 }
980 }
981 break;
982 }
983 case aco_opcode::s_mov_b32: /* propagate */
984 case aco_opcode::s_mov_b64:
985 case aco_opcode::v_mov_b32:
986 case aco_opcode::p_as_uniform:
987 if (instr->definitions[0].isFixed()) {
988 /* don't copy-propagate copies into fixed registers */
989 } else if (instr->usesModifiers()) {
990 // TODO
991 } else if (instr->operands[0].isConstant()) {
992 if (instr->operands[0].isLiteral())
993 ctx.info[instr->definitions[0].tempId()].set_literal(instr->operands[0].constantValue());
994 else if (instr->operands[0].size() == 1)
995 ctx.info[instr->definitions[0].tempId()].set_constant(instr->operands[0].constantValue());
996 else if (instr->operands[0].size() == 2)
997 ctx.info[instr->definitions[0].tempId()].set_constant_64bit(instr->operands[0].constantValue());
998 } else if (instr->operands[0].isTemp()) {
999 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
1000 } else {
1001 assert(instr->operands[0].isFixed());
1002 }
1003 break;
1004 case aco_opcode::p_is_helper:
1005 if (!ctx.program->needs_wqm)
1006 ctx.info[instr->definitions[0].tempId()].set_constant(0u);
1007 break;
1008 case aco_opcode::s_movk_i32: {
1009 uint32_t v = static_cast<SOPK_instruction*>(instr.get())->imm;
1010 v = v & 0x8000 ? (v | 0xffff0000) : v;
1011 if (v <= 64 || v >= 0xfffffff0)
1012 ctx.info[instr->definitions[0].tempId()].set_constant(v);
1013 else
1014 ctx.info[instr->definitions[0].tempId()].set_literal(v);
1015 break;
1016 }
1017 case aco_opcode::v_bfrev_b32:
1018 case aco_opcode::s_brev_b32: {
1019 if (instr->operands[0].isConstant()) {
1020 uint32_t v = util_bitreverse(instr->operands[0].constantValue());
1021 if (v <= 64 || v >= 0xfffffff0)
1022 ctx.info[instr->definitions[0].tempId()].set_constant(v);
1023 else
1024 ctx.info[instr->definitions[0].tempId()].set_literal(v);
1025 }
1026 break;
1027 }
1028 case aco_opcode::s_bfm_b32: {
1029 if (instr->operands[0].isConstant() && instr->operands[1].isConstant()) {
1030 unsigned size = instr->operands[0].constantValue() & 0x1f;
1031 unsigned start = instr->operands[1].constantValue() & 0x1f;
1032 uint32_t v = ((1u << size) - 1u) << start;
1033 if (v <= 64 || v >= 0xfffffff0)
1034 ctx.info[instr->definitions[0].tempId()].set_constant(v);
1035 else
1036 ctx.info[instr->definitions[0].tempId()].set_literal(v);
1037 }
1038 }
1039 case aco_opcode::v_mul_f32: { /* omod */
1040 /* TODO: try to move the negate/abs modifier to the consumer instead */
1041 if (instr->usesModifiers())
1042 break;
1043
1044 for (unsigned i = 0; i < 2; i++) {
1045 if (instr->operands[!i].isConstant() && instr->operands[i].isTemp()) {
1046 if (instr->operands[!i].constantValue() == 0x40000000) { /* 2.0 */
1047 ctx.info[instr->operands[i].tempId()].set_omod2(instr->definitions[0].getTemp());
1048 } else if (instr->operands[!i].constantValue() == 0x40800000) { /* 4.0 */
1049 ctx.info[instr->operands[i].tempId()].set_omod4(instr->definitions[0].getTemp());
1050 } else if (instr->operands[!i].constantValue() == 0x3f000000) { /* 0.5 */
1051 ctx.info[instr->operands[i].tempId()].set_omod5(instr->definitions[0].getTemp());
1052 } else if (instr->operands[!i].constantValue() == 0x3f800000 &&
1053 !block.fp_mode.must_flush_denorms32) { /* 1.0 */
1054 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[i].getTemp());
1055 } else {
1056 continue;
1057 }
1058 break;
1059 }
1060 }
1061 break;
1062 }
1063 case aco_opcode::v_and_b32: /* abs */
1064 if (!instr->usesModifiers() && instr->operands[0].constantEquals(0x7FFFFFFF) &&
1065 instr->operands[1].isTemp() && instr->operands[1].getTemp().type() == RegType::vgpr)
1066 ctx.info[instr->definitions[0].tempId()].set_abs(instr->operands[1].getTemp());
1067 else
1068 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1069 break;
1070 case aco_opcode::v_xor_b32: { /* neg */
1071 if (!instr->usesModifiers() && instr->operands[0].constantEquals(0x80000000u) && instr->operands[1].isTemp()) {
1072 if (ctx.info[instr->operands[1].tempId()].is_neg()) {
1073 ctx.info[instr->definitions[0].tempId()].set_temp(ctx.info[instr->operands[1].tempId()].temp);
1074 } else if (instr->operands[1].getTemp().type() == RegType::vgpr) {
1075 if (ctx.info[instr->operands[1].tempId()].is_abs()) { /* neg(abs(x)) */
1076 instr->operands[1].setTemp(ctx.info[instr->operands[1].tempId()].temp);
1077 instr->opcode = aco_opcode::v_or_b32;
1078 ctx.info[instr->definitions[0].tempId()].set_neg_abs(instr->operands[1].getTemp());
1079 } else {
1080 ctx.info[instr->definitions[0].tempId()].set_neg(instr->operands[1].getTemp());
1081 }
1082 }
1083 } else {
1084 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1085 }
1086 break;
1087 }
1088 case aco_opcode::v_med3_f32: { /* clamp */
1089 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(instr.get());
1090 if (vop3->abs[0] || vop3->abs[1] || vop3->abs[2] ||
1091 vop3->neg[0] || vop3->neg[1] || vop3->neg[2] ||
1092 vop3->omod != 0 || vop3->opsel != 0)
1093 break;
1094
1095 unsigned idx = 0;
1096 bool found_zero = false, found_one = false;
1097 for (unsigned i = 0; i < 3; i++)
1098 {
1099 if (instr->operands[i].constantEquals(0))
1100 found_zero = true;
1101 else if (instr->operands[i].constantEquals(0x3f800000)) /* 1.0 */
1102 found_one = true;
1103 else
1104 idx = i;
1105 }
1106 if (found_zero && found_one && instr->operands[idx].isTemp()) {
1107 ctx.info[instr->operands[idx].tempId()].set_clamp(instr->definitions[0].getTemp());
1108 }
1109 break;
1110 }
1111 case aco_opcode::v_cndmask_b32:
1112 if (instr->operands[0].constantEquals(0) &&
1113 instr->operands[1].constantEquals(0xFFFFFFFF) &&
1114 instr->operands[2].isTemp())
1115 ctx.info[instr->definitions[0].tempId()].set_vcc(instr->operands[2].getTemp());
1116 else if (instr->operands[0].constantEquals(0) &&
1117 instr->operands[1].constantEquals(0x3f800000u) &&
1118 instr->operands[2].isTemp())
1119 ctx.info[instr->definitions[0].tempId()].set_b2f(instr->operands[2].getTemp());
1120
1121 ctx.info[instr->operands[2].tempId()].set_vcc_hint();
1122 break;
1123 case aco_opcode::v_cmp_lg_u32:
1124 if (instr->format == Format::VOPC && /* don't optimize VOP3 / SDWA / DPP */
1125 instr->operands[0].constantEquals(0) &&
1126 instr->operands[1].isTemp() && ctx.info[instr->operands[1].tempId()].is_vcc())
1127 ctx.info[instr->definitions[0].tempId()].set_temp(ctx.info[instr->operands[1].tempId()].temp);
1128 break;
1129 case aco_opcode::p_phi:
1130 case aco_opcode::p_linear_phi: {
1131 /* lower_bool_phis() can create phis like this */
1132 bool all_same_temp = instr->operands[0].isTemp();
1133 /* this check is needed when moving uniform loop counters out of a divergent loop */
1134 if (all_same_temp)
1135 all_same_temp = instr->definitions[0].regClass() == instr->operands[0].regClass();
1136 for (unsigned i = 1; all_same_temp && (i < instr->operands.size()); i++) {
1137 if (!instr->operands[i].isTemp() || instr->operands[i].tempId() != instr->operands[0].tempId())
1138 all_same_temp = false;
1139 }
1140 if (all_same_temp) {
1141 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
1142 } else {
1143 bool all_undef = instr->operands[0].isUndefined();
1144 for (unsigned i = 1; all_undef && (i < instr->operands.size()); i++) {
1145 if (!instr->operands[i].isUndefined())
1146 all_undef = false;
1147 }
1148 if (all_undef)
1149 ctx.info[instr->definitions[0].tempId()].set_undefined();
1150 }
1151 break;
1152 }
1153 case aco_opcode::v_add_u32:
1154 case aco_opcode::v_add_co_u32:
1155 case aco_opcode::v_add_co_u32_e64:
1156 case aco_opcode::s_add_i32:
1157 case aco_opcode::s_add_u32:
1158 ctx.info[instr->definitions[0].tempId()].set_add_sub(instr.get());
1159 break;
1160 case aco_opcode::s_not_b32:
1161 case aco_opcode::s_not_b64:
1162 if (ctx.info[instr->operands[0].tempId()].is_uniform_bool()) {
1163 ctx.info[instr->definitions[0].tempId()].set_uniform_bitwise();
1164 ctx.info[instr->definitions[1].tempId()].set_scc_invert(ctx.info[instr->operands[0].tempId()].temp);
1165 } else if (ctx.info[instr->operands[0].tempId()].is_uniform_bitwise()) {
1166 ctx.info[instr->definitions[0].tempId()].set_uniform_bitwise();
1167 ctx.info[instr->definitions[1].tempId()].set_scc_invert(ctx.info[instr->operands[0].tempId()].instr->definitions[1].getTemp());
1168 }
1169 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1170 break;
1171 case aco_opcode::s_and_b32:
1172 case aco_opcode::s_and_b64:
1173 if (fixed_to_exec(instr->operands[1]) && instr->operands[0].isTemp()) {
1174 if (ctx.info[instr->operands[0].tempId()].is_uniform_bool()) {
1175 /* Try to get rid of the superfluous s_cselect + s_and_b64 that comes from turning a uniform bool into divergent */
1176 ctx.info[instr->definitions[1].tempId()].set_temp(ctx.info[instr->operands[0].tempId()].temp);
1177 ctx.info[instr->definitions[0].tempId()].set_uniform_bool(ctx.info[instr->operands[0].tempId()].temp);
1178 break;
1179 } else if (ctx.info[instr->operands[0].tempId()].is_uniform_bitwise()) {
1180 /* Try to get rid of the superfluous s_and_b64, since the uniform bitwise instruction already produces the same SCC */
1181 ctx.info[instr->definitions[1].tempId()].set_temp(ctx.info[instr->operands[0].tempId()].instr->definitions[1].getTemp());
1182 ctx.info[instr->definitions[0].tempId()].set_uniform_bool(ctx.info[instr->operands[0].tempId()].instr->definitions[1].getTemp());
1183 break;
1184 }
1185 }
1186 /* fallthrough */
1187 case aco_opcode::s_or_b32:
1188 case aco_opcode::s_or_b64:
1189 case aco_opcode::s_xor_b32:
1190 case aco_opcode::s_xor_b64:
1191 if (std::all_of(instr->operands.begin(), instr->operands.end(), [&ctx](const Operand& op) {
1192 return op.isTemp() && (ctx.info[op.tempId()].is_uniform_bool() || ctx.info[op.tempId()].is_uniform_bitwise());
1193 })) {
1194 ctx.info[instr->definitions[0].tempId()].set_uniform_bitwise();
1195 }
1196 /* fallthrough */
1197 case aco_opcode::s_lshl_b32:
1198 case aco_opcode::v_or_b32:
1199 case aco_opcode::v_lshlrev_b32:
1200 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1201 break;
1202 case aco_opcode::v_min_f32:
1203 case aco_opcode::v_min_f16:
1204 case aco_opcode::v_min_u32:
1205 case aco_opcode::v_min_i32:
1206 case aco_opcode::v_min_u16:
1207 case aco_opcode::v_min_i16:
1208 case aco_opcode::v_max_f32:
1209 case aco_opcode::v_max_f16:
1210 case aco_opcode::v_max_u32:
1211 case aco_opcode::v_max_i32:
1212 case aco_opcode::v_max_u16:
1213 case aco_opcode::v_max_i16:
1214 ctx.info[instr->definitions[0].tempId()].set_minmax(instr.get());
1215 break;
1216 case aco_opcode::v_cmp_lt_f32:
1217 case aco_opcode::v_cmp_eq_f32:
1218 case aco_opcode::v_cmp_le_f32:
1219 case aco_opcode::v_cmp_gt_f32:
1220 case aco_opcode::v_cmp_lg_f32:
1221 case aco_opcode::v_cmp_ge_f32:
1222 case aco_opcode::v_cmp_o_f32:
1223 case aco_opcode::v_cmp_u_f32:
1224 case aco_opcode::v_cmp_nge_f32:
1225 case aco_opcode::v_cmp_nlg_f32:
1226 case aco_opcode::v_cmp_ngt_f32:
1227 case aco_opcode::v_cmp_nle_f32:
1228 case aco_opcode::v_cmp_neq_f32:
1229 case aco_opcode::v_cmp_nlt_f32:
1230 ctx.info[instr->definitions[0].tempId()].set_fcmp(instr.get());
1231 break;
1232 case aco_opcode::s_cselect_b64:
1233 case aco_opcode::s_cselect_b32:
1234 if (instr->operands[0].constantEquals((unsigned) -1) &&
1235 instr->operands[1].constantEquals(0)) {
1236 /* Found a cselect that operates on a uniform bool that comes from eg. s_cmp */
1237 ctx.info[instr->definitions[0].tempId()].set_uniform_bool(instr->operands[2].getTemp());
1238 }
1239 if (instr->operands[2].isTemp() && ctx.info[instr->operands[2].tempId()].is_scc_invert()) {
1240 /* Flip the operands to get rid of the scc_invert instruction */
1241 std::swap(instr->operands[0], instr->operands[1]);
1242 instr->operands[2].setTemp(ctx.info[instr->operands[2].tempId()].temp);
1243 }
1244 break;
1245 case aco_opcode::p_wqm:
1246 if (instr->operands[0].isTemp() &&
1247 ctx.info[instr->operands[0].tempId()].is_scc_invert()) {
1248 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
1249 }
1250 break;
1251 default:
1252 break;
1253 }
1254 }
1255
1256 ALWAYS_INLINE bool get_cmp_info(aco_opcode op, aco_opcode *ordered, aco_opcode *unordered, aco_opcode *inverse)
1257 {
1258 *ordered = *unordered = op;
1259 switch (op) {
1260 #define CMP(ord, unord) \
1261 case aco_opcode::v_cmp_##ord##_f32:\
1262 case aco_opcode::v_cmp_n##unord##_f32:\
1263 *ordered = aco_opcode::v_cmp_##ord##_f32;\
1264 *unordered = aco_opcode::v_cmp_n##unord##_f32;\
1265 *inverse = op == aco_opcode::v_cmp_n##unord##_f32 ? aco_opcode::v_cmp_##unord##_f32 : aco_opcode::v_cmp_n##ord##_f32;\
1266 return true;
1267 CMP(lt, /*n*/ge)
1268 CMP(eq, /*n*/lg)
1269 CMP(le, /*n*/gt)
1270 CMP(gt, /*n*/le)
1271 CMP(lg, /*n*/eq)
1272 CMP(ge, /*n*/lt)
1273 #undef CMP
1274 default:
1275 return false;
1276 }
1277 }
1278
1279 aco_opcode get_ordered(aco_opcode op)
1280 {
1281 aco_opcode ordered, unordered, inverse;
1282 return get_cmp_info(op, &ordered, &unordered, &inverse) ? ordered : aco_opcode::last_opcode;
1283 }
1284
1285 aco_opcode get_unordered(aco_opcode op)
1286 {
1287 aco_opcode ordered, unordered, inverse;
1288 return get_cmp_info(op, &ordered, &unordered, &inverse) ? unordered : aco_opcode::last_opcode;
1289 }
1290
1291 aco_opcode get_inverse(aco_opcode op)
1292 {
1293 aco_opcode ordered, unordered, inverse;
1294 return get_cmp_info(op, &ordered, &unordered, &inverse) ? inverse : aco_opcode::last_opcode;
1295 }
1296
1297 bool is_cmp(aco_opcode op)
1298 {
1299 aco_opcode ordered, unordered, inverse;
1300 return get_cmp_info(op, &ordered, &unordered, &inverse);
1301 }
1302
1303 unsigned original_temp_id(opt_ctx &ctx, Temp tmp)
1304 {
1305 if (ctx.info[tmp.id()].is_temp())
1306 return ctx.info[tmp.id()].temp.id();
1307 else
1308 return tmp.id();
1309 }
1310
1311 void decrease_uses(opt_ctx &ctx, Instruction* instr)
1312 {
1313 if (!--ctx.uses[instr->definitions[0].tempId()]) {
1314 for (const Operand& op : instr->operands) {
1315 if (op.isTemp())
1316 ctx.uses[op.tempId()]--;
1317 }
1318 }
1319 }
1320
1321 Instruction *follow_operand(opt_ctx &ctx, Operand op, bool ignore_uses=false)
1322 {
1323 if (!op.isTemp() || !(ctx.info[op.tempId()].label & instr_labels))
1324 return nullptr;
1325 if (!ignore_uses && ctx.uses[op.tempId()] > 1)
1326 return nullptr;
1327
1328 Instruction *instr = ctx.info[op.tempId()].instr;
1329
1330 if (instr->definitions.size() == 2) {
1331 assert(instr->definitions[0].isTemp() && instr->definitions[0].tempId() == op.tempId());
1332 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1333 return nullptr;
1334 }
1335
1336 return instr;
1337 }
1338
1339 /* s_or_b64(neq(a, a), neq(b, b)) -> v_cmp_u_f32(a, b)
1340 * s_and_b64(eq(a, a), eq(b, b)) -> v_cmp_o_f32(a, b) */
1341 bool combine_ordering_test(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1342 {
1343 if (instr->definitions[0].regClass() != ctx.program->lane_mask)
1344 return false;
1345 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1346 return false;
1347
1348 bool is_or = instr->opcode == aco_opcode::s_or_b64 || instr->opcode == aco_opcode::s_or_b32;
1349
1350 bool neg[2] = {false, false};
1351 bool abs[2] = {false, false};
1352 uint8_t opsel = 0;
1353 Instruction *op_instr[2];
1354 Temp op[2];
1355
1356 for (unsigned i = 0; i < 2; i++) {
1357 op_instr[i] = follow_operand(ctx, instr->operands[i], true);
1358 if (!op_instr[i])
1359 return false;
1360
1361 aco_opcode expected_cmp = is_or ? aco_opcode::v_cmp_neq_f32 : aco_opcode::v_cmp_eq_f32;
1362
1363 if (op_instr[i]->opcode != expected_cmp)
1364 return false;
1365 if (!op_instr[i]->operands[0].isTemp() || !op_instr[i]->operands[1].isTemp())
1366 return false;
1367
1368 if (op_instr[i]->isVOP3()) {
1369 VOP3A_instruction *vop3 = static_cast<VOP3A_instruction*>(op_instr[i]);
1370 if (vop3->neg[0] != vop3->neg[1] || vop3->abs[0] != vop3->abs[1] || vop3->opsel == 1 || vop3->opsel == 2)
1371 return false;
1372 neg[i] = vop3->neg[0];
1373 abs[i] = vop3->abs[0];
1374 opsel |= (vop3->opsel & 1) << i;
1375 }
1376
1377 Temp op0 = op_instr[i]->operands[0].getTemp();
1378 Temp op1 = op_instr[i]->operands[1].getTemp();
1379 if (original_temp_id(ctx, op0) != original_temp_id(ctx, op1))
1380 return false;
1381
1382 op[i] = op1;
1383 }
1384
1385 if (op[1].type() == RegType::sgpr)
1386 std::swap(op[0], op[1]);
1387 unsigned num_sgprs = (op[0].type() == RegType::sgpr) + (op[1].type() == RegType::sgpr);
1388 if (num_sgprs > (ctx.program->chip_class >= GFX10 ? 2 : 1))
1389 return false;
1390
1391 ctx.uses[op[0].id()]++;
1392 ctx.uses[op[1].id()]++;
1393 decrease_uses(ctx, op_instr[0]);
1394 decrease_uses(ctx, op_instr[1]);
1395
1396 aco_opcode new_op = is_or ? aco_opcode::v_cmp_u_f32 : aco_opcode::v_cmp_o_f32;
1397 Instruction *new_instr;
1398 if (neg[0] || neg[1] || abs[0] || abs[1] || opsel || num_sgprs > 1) {
1399 VOP3A_instruction *vop3 = create_instruction<VOP3A_instruction>(new_op, asVOP3(Format::VOPC), 2, 1);
1400 for (unsigned i = 0; i < 2; i++) {
1401 vop3->neg[i] = neg[i];
1402 vop3->abs[i] = abs[i];
1403 }
1404 vop3->opsel = opsel;
1405 new_instr = static_cast<Instruction *>(vop3);
1406 } else {
1407 new_instr = create_instruction<VOPC_instruction>(new_op, Format::VOPC, 2, 1);
1408 }
1409 new_instr->operands[0] = Operand(op[0]);
1410 new_instr->operands[1] = Operand(op[1]);
1411 new_instr->definitions[0] = instr->definitions[0];
1412
1413 ctx.info[instr->definitions[0].tempId()].label = 0;
1414 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1415
1416 instr.reset(new_instr);
1417
1418 return true;
1419 }
1420
1421 /* s_or_b64(v_cmp_u_f32(a, b), cmp(a, b)) -> get_unordered(cmp)(a, b)
1422 * s_and_b64(v_cmp_o_f32(a, b), cmp(a, b)) -> get_ordered(cmp)(a, b) */
1423 bool combine_comparison_ordering(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1424 {
1425 if (instr->definitions[0].regClass() != ctx.program->lane_mask)
1426 return false;
1427 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1428 return false;
1429
1430 bool is_or = instr->opcode == aco_opcode::s_or_b64 || instr->opcode == aco_opcode::s_or_b32;
1431 aco_opcode expected_nan_test = is_or ? aco_opcode::v_cmp_u_f32 : aco_opcode::v_cmp_o_f32;
1432
1433 Instruction *nan_test = follow_operand(ctx, instr->operands[0], true);
1434 Instruction *cmp = follow_operand(ctx, instr->operands[1], true);
1435 if (!nan_test || !cmp)
1436 return false;
1437
1438 if (cmp->opcode == expected_nan_test)
1439 std::swap(nan_test, cmp);
1440 else if (nan_test->opcode != expected_nan_test)
1441 return false;
1442
1443 if (!is_cmp(cmp->opcode))
1444 return false;
1445
1446 if (!nan_test->operands[0].isTemp() || !nan_test->operands[1].isTemp())
1447 return false;
1448 if (!cmp->operands[0].isTemp() || !cmp->operands[1].isTemp())
1449 return false;
1450
1451 unsigned prop_cmp0 = original_temp_id(ctx, cmp->operands[0].getTemp());
1452 unsigned prop_cmp1 = original_temp_id(ctx, cmp->operands[1].getTemp());
1453 unsigned prop_nan0 = original_temp_id(ctx, nan_test->operands[0].getTemp());
1454 unsigned prop_nan1 = original_temp_id(ctx, nan_test->operands[1].getTemp());
1455 if (prop_cmp0 != prop_nan0 && prop_cmp0 != prop_nan1)
1456 return false;
1457 if (prop_cmp1 != prop_nan0 && prop_cmp1 != prop_nan1)
1458 return false;
1459
1460 ctx.uses[cmp->operands[0].tempId()]++;
1461 ctx.uses[cmp->operands[1].tempId()]++;
1462 decrease_uses(ctx, nan_test);
1463 decrease_uses(ctx, cmp);
1464
1465 aco_opcode new_op = is_or ? get_unordered(cmp->opcode) : get_ordered(cmp->opcode);
1466 Instruction *new_instr;
1467 if (cmp->isVOP3()) {
1468 VOP3A_instruction *new_vop3 = create_instruction<VOP3A_instruction>(new_op, asVOP3(Format::VOPC), 2, 1);
1469 VOP3A_instruction *cmp_vop3 = static_cast<VOP3A_instruction*>(cmp);
1470 memcpy(new_vop3->abs, cmp_vop3->abs, sizeof(new_vop3->abs));
1471 memcpy(new_vop3->neg, cmp_vop3->neg, sizeof(new_vop3->neg));
1472 new_vop3->clamp = cmp_vop3->clamp;
1473 new_vop3->omod = cmp_vop3->omod;
1474 new_vop3->opsel = cmp_vop3->opsel;
1475 new_instr = new_vop3;
1476 } else {
1477 new_instr = create_instruction<VOPC_instruction>(new_op, Format::VOPC, 2, 1);
1478 }
1479 new_instr->operands[0] = cmp->operands[0];
1480 new_instr->operands[1] = cmp->operands[1];
1481 new_instr->definitions[0] = instr->definitions[0];
1482
1483 ctx.info[instr->definitions[0].tempId()].label = 0;
1484 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1485
1486 instr.reset(new_instr);
1487
1488 return true;
1489 }
1490
1491 /* s_or_b64(v_cmp_neq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_unordered(cmp)(a, b)
1492 * s_and_b64(v_cmp_eq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_ordered(cmp)(a, b) */
1493 bool combine_constant_comparison_ordering(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1494 {
1495 if (instr->definitions[0].regClass() != ctx.program->lane_mask)
1496 return false;
1497 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1498 return false;
1499
1500 bool is_or = instr->opcode == aco_opcode::s_or_b64 || instr->opcode == aco_opcode::s_or_b32;
1501
1502 Instruction *nan_test = follow_operand(ctx, instr->operands[0], true);
1503 Instruction *cmp = follow_operand(ctx, instr->operands[1], true);
1504
1505 if (!nan_test || !cmp)
1506 return false;
1507
1508 aco_opcode expected_nan_test = is_or ? aco_opcode::v_cmp_neq_f32 : aco_opcode::v_cmp_eq_f32;
1509 if (cmp->opcode == expected_nan_test)
1510 std::swap(nan_test, cmp);
1511 else if (nan_test->opcode != expected_nan_test)
1512 return false;
1513
1514 if (!is_cmp(cmp->opcode))
1515 return false;
1516
1517 if (!nan_test->operands[0].isTemp() || !nan_test->operands[1].isTemp())
1518 return false;
1519 if (!cmp->operands[0].isTemp() && !cmp->operands[1].isTemp())
1520 return false;
1521
1522 unsigned prop_nan0 = original_temp_id(ctx, nan_test->operands[0].getTemp());
1523 unsigned prop_nan1 = original_temp_id(ctx, nan_test->operands[1].getTemp());
1524 if (prop_nan0 != prop_nan1)
1525 return false;
1526
1527 if (nan_test->isVOP3()) {
1528 VOP3A_instruction *vop3 = static_cast<VOP3A_instruction*>(nan_test);
1529 if (vop3->neg[0] != vop3->neg[1] || vop3->abs[0] != vop3->abs[1] || vop3->opsel == 1 || vop3->opsel == 2)
1530 return false;
1531 }
1532
1533 int constant_operand = -1;
1534 for (unsigned i = 0; i < 2; i++) {
1535 if (cmp->operands[i].isTemp() && original_temp_id(ctx, cmp->operands[i].getTemp()) == prop_nan0) {
1536 constant_operand = !i;
1537 break;
1538 }
1539 }
1540 if (constant_operand == -1)
1541 return false;
1542
1543 uint32_t constant;
1544 if (cmp->operands[constant_operand].isConstant()) {
1545 constant = cmp->operands[constant_operand].constantValue();
1546 } else if (cmp->operands[constant_operand].isTemp()) {
1547 Temp tmp = cmp->operands[constant_operand].getTemp();
1548 unsigned id = original_temp_id(ctx, tmp);
1549 if (!ctx.info[id].is_constant() && !ctx.info[id].is_literal())
1550 return false;
1551 constant = ctx.info[id].val;
1552 } else {
1553 return false;
1554 }
1555
1556 float constantf;
1557 memcpy(&constantf, &constant, 4);
1558 if (isnan(constantf))
1559 return false;
1560
1561 if (cmp->operands[0].isTemp())
1562 ctx.uses[cmp->operands[0].tempId()]++;
1563 if (cmp->operands[1].isTemp())
1564 ctx.uses[cmp->operands[1].tempId()]++;
1565 decrease_uses(ctx, nan_test);
1566 decrease_uses(ctx, cmp);
1567
1568 aco_opcode new_op = is_or ? get_unordered(cmp->opcode) : get_ordered(cmp->opcode);
1569 Instruction *new_instr;
1570 if (cmp->isVOP3()) {
1571 VOP3A_instruction *new_vop3 = create_instruction<VOP3A_instruction>(new_op, asVOP3(Format::VOPC), 2, 1);
1572 VOP3A_instruction *cmp_vop3 = static_cast<VOP3A_instruction*>(cmp);
1573 memcpy(new_vop3->abs, cmp_vop3->abs, sizeof(new_vop3->abs));
1574 memcpy(new_vop3->neg, cmp_vop3->neg, sizeof(new_vop3->neg));
1575 new_vop3->clamp = cmp_vop3->clamp;
1576 new_vop3->omod = cmp_vop3->omod;
1577 new_vop3->opsel = cmp_vop3->opsel;
1578 new_instr = new_vop3;
1579 } else {
1580 new_instr = create_instruction<VOPC_instruction>(new_op, Format::VOPC, 2, 1);
1581 }
1582 new_instr->operands[0] = cmp->operands[0];
1583 new_instr->operands[1] = cmp->operands[1];
1584 new_instr->definitions[0] = instr->definitions[0];
1585
1586 ctx.info[instr->definitions[0].tempId()].label = 0;
1587 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1588
1589 instr.reset(new_instr);
1590
1591 return true;
1592 }
1593
1594 /* s_not_b64(cmp(a, b) -> get_inverse(cmp)(a, b) */
1595 bool combine_inverse_comparison(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1596 {
1597 if (instr->opcode != aco_opcode::s_not_b64)
1598 return false;
1599 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1600 return false;
1601 if (!instr->operands[0].isTemp())
1602 return false;
1603
1604 Instruction *cmp = follow_operand(ctx, instr->operands[0]);
1605 if (!cmp)
1606 return false;
1607
1608 aco_opcode new_opcode = get_inverse(cmp->opcode);
1609 if (new_opcode == aco_opcode::last_opcode)
1610 return false;
1611
1612 if (cmp->operands[0].isTemp())
1613 ctx.uses[cmp->operands[0].tempId()]++;
1614 if (cmp->operands[1].isTemp())
1615 ctx.uses[cmp->operands[1].tempId()]++;
1616 decrease_uses(ctx, cmp);
1617
1618 Instruction *new_instr;
1619 if (cmp->isVOP3()) {
1620 VOP3A_instruction *new_vop3 = create_instruction<VOP3A_instruction>(new_opcode, asVOP3(Format::VOPC), 2, 1);
1621 VOP3A_instruction *cmp_vop3 = static_cast<VOP3A_instruction*>(cmp);
1622 memcpy(new_vop3->abs, cmp_vop3->abs, sizeof(new_vop3->abs));
1623 memcpy(new_vop3->neg, cmp_vop3->neg, sizeof(new_vop3->neg));
1624 new_vop3->clamp = cmp_vop3->clamp;
1625 new_vop3->omod = cmp_vop3->omod;
1626 new_vop3->opsel = cmp_vop3->opsel;
1627 new_instr = new_vop3;
1628 } else {
1629 new_instr = create_instruction<VOPC_instruction>(new_opcode, Format::VOPC, 2, 1);
1630 }
1631 new_instr->operands[0] = cmp->operands[0];
1632 new_instr->operands[1] = cmp->operands[1];
1633 new_instr->definitions[0] = instr->definitions[0];
1634
1635 ctx.info[instr->definitions[0].tempId()].label = 0;
1636 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1637
1638 instr.reset(new_instr);
1639
1640 return true;
1641 }
1642
1643 /* op1(op2(1, 2), 0) if swap = false
1644 * op1(0, op2(1, 2)) if swap = true */
1645 bool match_op3_for_vop3(opt_ctx &ctx, aco_opcode op1, aco_opcode op2,
1646 Instruction* op1_instr, bool swap, const char *shuffle_str,
1647 Operand operands[3], bool neg[3], bool abs[3], uint8_t *opsel,
1648 bool *op1_clamp, uint8_t *op1_omod,
1649 bool *inbetween_neg, bool *inbetween_abs, bool *inbetween_opsel)
1650 {
1651 /* checks */
1652 if (op1_instr->opcode != op1)
1653 return false;
1654
1655 Instruction *op2_instr = follow_operand(ctx, op1_instr->operands[swap]);
1656 if (!op2_instr || op2_instr->opcode != op2)
1657 return false;
1658 if (fixed_to_exec(op2_instr->operands[0]) || fixed_to_exec(op2_instr->operands[1]))
1659 return false;
1660
1661 VOP3A_instruction *op1_vop3 = op1_instr->isVOP3() ? static_cast<VOP3A_instruction *>(op1_instr) : NULL;
1662 VOP3A_instruction *op2_vop3 = op2_instr->isVOP3() ? static_cast<VOP3A_instruction *>(op2_instr) : NULL;
1663
1664 /* don't support inbetween clamp/omod */
1665 if (op2_vop3 && (op2_vop3->clamp || op2_vop3->omod))
1666 return false;
1667
1668 /* get operands and modifiers and check inbetween modifiers */
1669 *op1_clamp = op1_vop3 ? op1_vop3->clamp : false;
1670 *op1_omod = op1_vop3 ? op1_vop3->omod : 0u;
1671
1672 if (inbetween_neg)
1673 *inbetween_neg = op1_vop3 ? op1_vop3->neg[swap] : false;
1674 else if (op1_vop3 && op1_vop3->neg[swap])
1675 return false;
1676
1677 if (inbetween_abs)
1678 *inbetween_abs = op1_vop3 ? op1_vop3->abs[swap] : false;
1679 else if (op1_vop3 && op1_vop3->abs[swap])
1680 return false;
1681
1682 if (inbetween_opsel)
1683 *inbetween_opsel = op1_vop3 ? op1_vop3->opsel & (1 << swap) : false;
1684 else if (op1_vop3 && op1_vop3->opsel & (1 << swap))
1685 return false;
1686
1687 int shuffle[3];
1688 shuffle[shuffle_str[0] - '0'] = 0;
1689 shuffle[shuffle_str[1] - '0'] = 1;
1690 shuffle[shuffle_str[2] - '0'] = 2;
1691
1692 operands[shuffle[0]] = op1_instr->operands[!swap];
1693 neg[shuffle[0]] = op1_vop3 ? op1_vop3->neg[!swap] : false;
1694 abs[shuffle[0]] = op1_vop3 ? op1_vop3->abs[!swap] : false;
1695 if (op1_vop3 && op1_vop3->opsel & (1 << !swap))
1696 *opsel |= 1 << shuffle[0];
1697
1698 for (unsigned i = 0; i < 2; i++) {
1699 operands[shuffle[i + 1]] = op2_instr->operands[i];
1700 neg[shuffle[i + 1]] = op2_vop3 ? op2_vop3->neg[i] : false;
1701 abs[shuffle[i + 1]] = op2_vop3 ? op2_vop3->abs[i] : false;
1702 if (op2_vop3 && op2_vop3->opsel & (1 << i))
1703 *opsel |= 1 << shuffle[i + 1];
1704 }
1705
1706 /* check operands */
1707 if (!check_vop3_operands(ctx, 3, operands))
1708 return false;
1709
1710 return true;
1711 }
1712
1713 void create_vop3_for_op3(opt_ctx& ctx, aco_opcode opcode, aco_ptr<Instruction>& instr,
1714 Operand operands[3], bool neg[3], bool abs[3], uint8_t opsel,
1715 bool clamp, unsigned omod)
1716 {
1717 VOP3A_instruction *new_instr = create_instruction<VOP3A_instruction>(opcode, Format::VOP3A, 3, 1);
1718 memcpy(new_instr->abs, abs, sizeof(bool[3]));
1719 memcpy(new_instr->neg, neg, sizeof(bool[3]));
1720 new_instr->clamp = clamp;
1721 new_instr->omod = omod;
1722 new_instr->opsel = opsel;
1723 new_instr->operands[0] = operands[0];
1724 new_instr->operands[1] = operands[1];
1725 new_instr->operands[2] = operands[2];
1726 new_instr->definitions[0] = instr->definitions[0];
1727 ctx.info[instr->definitions[0].tempId()].label = 0;
1728
1729 instr.reset(new_instr);
1730 }
1731
1732 bool combine_three_valu_op(opt_ctx& ctx, aco_ptr<Instruction>& instr, aco_opcode op2, aco_opcode new_op, const char *shuffle, uint8_t ops)
1733 {
1734 uint32_t omod_clamp = ctx.info[instr->definitions[0].tempId()].label &
1735 (label_omod_success | label_clamp_success);
1736
1737 for (unsigned swap = 0; swap < 2; swap++) {
1738 if (!((1 << swap) & ops))
1739 continue;
1740
1741 Operand operands[3];
1742 bool neg[3], abs[3], clamp;
1743 uint8_t opsel = 0, omod = 0;
1744 if (match_op3_for_vop3(ctx, instr->opcode, op2,
1745 instr.get(), swap, shuffle,
1746 operands, neg, abs, &opsel,
1747 &clamp, &omod, NULL, NULL, NULL)) {
1748 ctx.uses[instr->operands[swap].tempId()]--;
1749 create_vop3_for_op3(ctx, new_op, instr, operands, neg, abs, opsel, clamp, omod);
1750 if (omod_clamp & label_omod_success)
1751 ctx.info[instr->definitions[0].tempId()].set_omod_success(instr.get());
1752 if (omod_clamp & label_clamp_success)
1753 ctx.info[instr->definitions[0].tempId()].set_clamp_success(instr.get());
1754 return true;
1755 }
1756 }
1757 return false;
1758 }
1759
1760 bool combine_minmax(opt_ctx& ctx, aco_ptr<Instruction>& instr, aco_opcode opposite, aco_opcode minmax3)
1761 {
1762 if (combine_three_valu_op(ctx, instr, instr->opcode, minmax3, "012", 1 | 2))
1763 return true;
1764
1765 uint32_t omod_clamp = ctx.info[instr->definitions[0].tempId()].label &
1766 (label_omod_success | label_clamp_success);
1767
1768 /* min(-max(a, b), c) -> min3(-a, -b, c) *
1769 * max(-min(a, b), c) -> max3(-a, -b, c) */
1770 for (unsigned swap = 0; swap < 2; swap++) {
1771 Operand operands[3];
1772 bool neg[3], abs[3], clamp;
1773 uint8_t opsel = 0, omod = 0;
1774 bool inbetween_neg;
1775 if (match_op3_for_vop3(ctx, instr->opcode, opposite,
1776 instr.get(), swap, "012",
1777 operands, neg, abs, &opsel,
1778 &clamp, &omod, &inbetween_neg, NULL, NULL) &&
1779 inbetween_neg) {
1780 ctx.uses[instr->operands[swap].tempId()]--;
1781 neg[1] = true;
1782 neg[2] = true;
1783 create_vop3_for_op3(ctx, minmax3, instr, operands, neg, abs, opsel, clamp, omod);
1784 if (omod_clamp & label_omod_success)
1785 ctx.info[instr->definitions[0].tempId()].set_omod_success(instr.get());
1786 if (omod_clamp & label_clamp_success)
1787 ctx.info[instr->definitions[0].tempId()].set_clamp_success(instr.get());
1788 return true;
1789 }
1790 }
1791 return false;
1792 }
1793
1794 /* s_not_b32(s_and_b32(a, b)) -> s_nand_b32(a, b)
1795 * s_not_b32(s_or_b32(a, b)) -> s_nor_b32(a, b)
1796 * s_not_b32(s_xor_b32(a, b)) -> s_xnor_b32(a, b)
1797 * s_not_b64(s_and_b64(a, b)) -> s_nand_b64(a, b)
1798 * s_not_b64(s_or_b64(a, b)) -> s_nor_b64(a, b)
1799 * s_not_b64(s_xor_b64(a, b)) -> s_xnor_b64(a, b) */
1800 bool combine_salu_not_bitwise(opt_ctx& ctx, aco_ptr<Instruction>& instr)
1801 {
1802 /* checks */
1803 if (!instr->operands[0].isTemp())
1804 return false;
1805 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1806 return false;
1807
1808 Instruction *op2_instr = follow_operand(ctx, instr->operands[0]);
1809 if (!op2_instr)
1810 return false;
1811 switch (op2_instr->opcode) {
1812 case aco_opcode::s_and_b32:
1813 case aco_opcode::s_or_b32:
1814 case aco_opcode::s_xor_b32:
1815 case aco_opcode::s_and_b64:
1816 case aco_opcode::s_or_b64:
1817 case aco_opcode::s_xor_b64:
1818 break;
1819 default:
1820 return false;
1821 }
1822
1823 /* create instruction */
1824 std::swap(instr->definitions[0], op2_instr->definitions[0]);
1825 std::swap(instr->definitions[1], op2_instr->definitions[1]);
1826 ctx.uses[instr->operands[0].tempId()]--;
1827 ctx.info[op2_instr->definitions[0].tempId()].label = 0;
1828
1829 switch (op2_instr->opcode) {
1830 case aco_opcode::s_and_b32:
1831 op2_instr->opcode = aco_opcode::s_nand_b32;
1832 break;
1833 case aco_opcode::s_or_b32:
1834 op2_instr->opcode = aco_opcode::s_nor_b32;
1835 break;
1836 case aco_opcode::s_xor_b32:
1837 op2_instr->opcode = aco_opcode::s_xnor_b32;
1838 break;
1839 case aco_opcode::s_and_b64:
1840 op2_instr->opcode = aco_opcode::s_nand_b64;
1841 break;
1842 case aco_opcode::s_or_b64:
1843 op2_instr->opcode = aco_opcode::s_nor_b64;
1844 break;
1845 case aco_opcode::s_xor_b64:
1846 op2_instr->opcode = aco_opcode::s_xnor_b64;
1847 break;
1848 default:
1849 break;
1850 }
1851
1852 return true;
1853 }
1854
1855 /* s_and_b32(a, s_not_b32(b)) -> s_andn2_b32(a, b)
1856 * s_or_b32(a, s_not_b32(b)) -> s_orn2_b32(a, b)
1857 * s_and_b64(a, s_not_b64(b)) -> s_andn2_b64(a, b)
1858 * s_or_b64(a, s_not_b64(b)) -> s_orn2_b64(a, b) */
1859 bool combine_salu_n2(opt_ctx& ctx, aco_ptr<Instruction>& instr)
1860 {
1861 if (instr->definitions[0].isTemp() && ctx.info[instr->definitions[0].tempId()].is_uniform_bool())
1862 return false;
1863
1864 for (unsigned i = 0; i < 2; i++) {
1865 Instruction *op2_instr = follow_operand(ctx, instr->operands[i]);
1866 if (!op2_instr || (op2_instr->opcode != aco_opcode::s_not_b32 && op2_instr->opcode != aco_opcode::s_not_b64))
1867 continue;
1868 if (ctx.uses[op2_instr->definitions[1].tempId()] || fixed_to_exec(op2_instr->operands[0]))
1869 continue;
1870
1871 if (instr->operands[!i].isLiteral() && op2_instr->operands[0].isLiteral() &&
1872 instr->operands[!i].constantValue() != op2_instr->operands[0].constantValue())
1873 continue;
1874
1875 ctx.uses[instr->operands[i].tempId()]--;
1876 instr->operands[0] = instr->operands[!i];
1877 instr->operands[1] = op2_instr->operands[0];
1878 ctx.info[instr->definitions[0].tempId()].label = 0;
1879
1880 switch (instr->opcode) {
1881 case aco_opcode::s_and_b32:
1882 instr->opcode = aco_opcode::s_andn2_b32;
1883 break;
1884 case aco_opcode::s_or_b32:
1885 instr->opcode = aco_opcode::s_orn2_b32;
1886 break;
1887 case aco_opcode::s_and_b64:
1888 instr->opcode = aco_opcode::s_andn2_b64;
1889 break;
1890 case aco_opcode::s_or_b64:
1891 instr->opcode = aco_opcode::s_orn2_b64;
1892 break;
1893 default:
1894 break;
1895 }
1896
1897 return true;
1898 }
1899 return false;
1900 }
1901
1902 /* s_add_{i32,u32}(a, s_lshl_b32(b, <n>)) -> s_lshl<n>_add_u32(a, b) */
1903 bool combine_salu_lshl_add(opt_ctx& ctx, aco_ptr<Instruction>& instr)
1904 {
1905 if (instr->opcode == aco_opcode::s_add_i32 && ctx.uses[instr->definitions[1].tempId()])
1906 return false;
1907
1908 for (unsigned i = 0; i < 2; i++) {
1909 Instruction *op2_instr = follow_operand(ctx, instr->operands[i]);
1910 if (!op2_instr || op2_instr->opcode != aco_opcode::s_lshl_b32 ||
1911 ctx.uses[op2_instr->definitions[1].tempId()])
1912 continue;
1913 if (!op2_instr->operands[1].isConstant() || fixed_to_exec(op2_instr->operands[0]))
1914 continue;
1915
1916 uint32_t shift = op2_instr->operands[1].constantValue();
1917 if (shift < 1 || shift > 4)
1918 continue;
1919
1920 if (instr->operands[!i].isLiteral() && op2_instr->operands[0].isLiteral() &&
1921 instr->operands[!i].constantValue() != op2_instr->operands[0].constantValue())
1922 continue;
1923
1924 ctx.uses[instr->operands[i].tempId()]--;
1925 instr->operands[1] = instr->operands[!i];
1926 instr->operands[0] = op2_instr->operands[0];
1927 ctx.info[instr->definitions[0].tempId()].label = 0;
1928
1929 instr->opcode = ((aco_opcode[]){aco_opcode::s_lshl1_add_u32,
1930 aco_opcode::s_lshl2_add_u32,
1931 aco_opcode::s_lshl3_add_u32,
1932 aco_opcode::s_lshl4_add_u32})[shift - 1];
1933
1934 return true;
1935 }
1936 return false;
1937 }
1938
1939 bool get_minmax_info(aco_opcode op, aco_opcode *min, aco_opcode *max, aco_opcode *min3, aco_opcode *max3, aco_opcode *med3, bool *some_gfx9_only)
1940 {
1941 switch (op) {
1942 #define MINMAX(type, gfx9) \
1943 case aco_opcode::v_min_##type:\
1944 case aco_opcode::v_max_##type:\
1945 case aco_opcode::v_med3_##type:\
1946 *min = aco_opcode::v_min_##type;\
1947 *max = aco_opcode::v_max_##type;\
1948 *med3 = aco_opcode::v_med3_##type;\
1949 *min3 = aco_opcode::v_min3_##type;\
1950 *max3 = aco_opcode::v_max3_##type;\
1951 *some_gfx9_only = gfx9;\
1952 return true;
1953 MINMAX(f32, false)
1954 MINMAX(u32, false)
1955 MINMAX(i32, false)
1956 MINMAX(f16, true)
1957 MINMAX(u16, true)
1958 MINMAX(i16, true)
1959 #undef MINMAX
1960 default:
1961 return false;
1962 }
1963 }
1964
1965 /* v_min_{f,u,i}{16,32}(v_max_{f,u,i}{16,32}(a, lb), ub) -> v_med3_{f,u,i}{16,32}(a, lb, ub) when ub > lb
1966 * v_max_{f,u,i}{16,32}(v_min_{f,u,i}{16,32}(a, ub), lb) -> v_med3_{f,u,i}{16,32}(a, lb, ub) when ub > lb */
1967 bool combine_clamp(opt_ctx& ctx, aco_ptr<Instruction>& instr,
1968 aco_opcode min, aco_opcode max, aco_opcode med)
1969 {
1970 /* TODO: GLSL's clamp(x, minVal, maxVal) and SPIR-V's
1971 * FClamp(x, minVal, maxVal)/NClamp(x, minVal, maxVal) are undefined if
1972 * minVal > maxVal, which means we can always select it to a v_med3_f32 */
1973 aco_opcode other_op;
1974 if (instr->opcode == min)
1975 other_op = max;
1976 else if (instr->opcode == max)
1977 other_op = min;
1978 else
1979 return false;
1980
1981 uint32_t omod_clamp = ctx.info[instr->definitions[0].tempId()].label &
1982 (label_omod_success | label_clamp_success);
1983
1984 for (unsigned swap = 0; swap < 2; swap++) {
1985 Operand operands[3];
1986 bool neg[3], abs[3], clamp;
1987 uint8_t opsel = 0, omod = 0;
1988 if (match_op3_for_vop3(ctx, instr->opcode, other_op, instr.get(), swap,
1989 "012", operands, neg, abs, &opsel,
1990 &clamp, &omod, NULL, NULL, NULL)) {
1991 int const0_idx = -1, const1_idx = -1;
1992 uint32_t const0 = 0, const1 = 0;
1993 for (int i = 0; i < 3; i++) {
1994 uint32_t val;
1995 if (operands[i].isConstant()) {
1996 val = operands[i].constantValue();
1997 } else if (operands[i].isTemp() && ctx.info[operands[i].tempId()].is_constant_or_literal()) {
1998 val = ctx.info[operands[i].tempId()].val;
1999 } else {
2000 continue;
2001 }
2002 if (const0_idx >= 0) {
2003 const1_idx = i;
2004 const1 = val;
2005 } else {
2006 const0_idx = i;
2007 const0 = val;
2008 }
2009 }
2010 if (const0_idx < 0 || const1_idx < 0)
2011 continue;
2012
2013 if (opsel & (1 << const0_idx))
2014 const0 >>= 16;
2015 if (opsel & (1 << const1_idx))
2016 const1 >>= 16;
2017
2018 int lower_idx = const0_idx;
2019 switch (min) {
2020 case aco_opcode::v_min_f32:
2021 case aco_opcode::v_min_f16: {
2022 float const0_f, const1_f;
2023 if (min == aco_opcode::v_min_f32) {
2024 memcpy(&const0_f, &const0, 4);
2025 memcpy(&const1_f, &const1, 4);
2026 } else {
2027 const0_f = _mesa_half_to_float(const0);
2028 const1_f = _mesa_half_to_float(const1);
2029 }
2030 if (abs[const0_idx]) const0_f = fabsf(const0_f);
2031 if (abs[const1_idx]) const1_f = fabsf(const1_f);
2032 if (neg[const0_idx]) const0_f = -const0_f;
2033 if (neg[const1_idx]) const1_f = -const1_f;
2034 lower_idx = const0_f < const1_f ? const0_idx : const1_idx;
2035 break;
2036 }
2037 case aco_opcode::v_min_u32: {
2038 lower_idx = const0 < const1 ? const0_idx : const1_idx;
2039 break;
2040 }
2041 case aco_opcode::v_min_u16: {
2042 lower_idx = (uint16_t)const0 < (uint16_t)const1 ? const0_idx : const1_idx;
2043 break;
2044 }
2045 case aco_opcode::v_min_i32: {
2046 int32_t const0_i = const0 & 0x80000000u ? -2147483648 + (int32_t)(const0 & 0x7fffffffu) : const0;
2047 int32_t const1_i = const1 & 0x80000000u ? -2147483648 + (int32_t)(const1 & 0x7fffffffu) : const1;
2048 lower_idx = const0_i < const1_i ? const0_idx : const1_idx;
2049 break;
2050 }
2051 case aco_opcode::v_min_i16: {
2052 int16_t const0_i = const0 & 0x8000u ? -32768 + (int16_t)(const0 & 0x7fffu) : const0;
2053 int16_t const1_i = const1 & 0x8000u ? -32768 + (int16_t)(const1 & 0x7fffu) : const1;
2054 lower_idx = const0_i < const1_i ? const0_idx : const1_idx;
2055 break;
2056 }
2057 default:
2058 break;
2059 }
2060 int upper_idx = lower_idx == const0_idx ? const1_idx : const0_idx;
2061
2062 if (instr->opcode == min) {
2063 if (upper_idx != 0 || lower_idx == 0)
2064 return false;
2065 } else {
2066 if (upper_idx == 0 || lower_idx != 0)
2067 return false;
2068 }
2069
2070 ctx.uses[instr->operands[swap].tempId()]--;
2071 create_vop3_for_op3(ctx, med, instr, operands, neg, abs, opsel, clamp, omod);
2072 if (omod_clamp & label_omod_success)
2073 ctx.info[instr->definitions[0].tempId()].set_omod_success(instr.get());
2074 if (omod_clamp & label_clamp_success)
2075 ctx.info[instr->definitions[0].tempId()].set_clamp_success(instr.get());
2076
2077 return true;
2078 }
2079 }
2080
2081 return false;
2082 }
2083
2084
2085 void apply_sgprs(opt_ctx &ctx, aco_ptr<Instruction>& instr)
2086 {
2087 bool is_shift64 = instr->opcode == aco_opcode::v_lshlrev_b64 ||
2088 instr->opcode == aco_opcode::v_lshrrev_b64 ||
2089 instr->opcode == aco_opcode::v_ashrrev_i64;
2090
2091 /* find candidates and create the set of sgprs already read */
2092 unsigned sgpr_ids[2] = {0, 0};
2093 uint32_t operand_mask = 0;
2094 bool has_literal = false;
2095 for (unsigned i = 0; i < instr->operands.size(); i++) {
2096 if (instr->operands[i].isLiteral())
2097 has_literal = true;
2098 if (!instr->operands[i].isTemp())
2099 continue;
2100 if (instr->operands[i].getTemp().type() == RegType::sgpr) {
2101 if (instr->operands[i].tempId() != sgpr_ids[0])
2102 sgpr_ids[!!sgpr_ids[0]] = instr->operands[i].tempId();
2103 }
2104 ssa_info& info = ctx.info[instr->operands[i].tempId()];
2105 if (info.is_temp() && info.temp.type() == RegType::sgpr)
2106 operand_mask |= 1u << i;
2107 }
2108 unsigned max_sgprs = 1;
2109 if (ctx.program->chip_class >= GFX10 && !is_shift64)
2110 max_sgprs = 2;
2111 if (has_literal)
2112 max_sgprs--;
2113
2114 unsigned num_sgprs = !!sgpr_ids[0] + !!sgpr_ids[1];
2115
2116 /* keep on applying sgprs until there is nothing left to be done */
2117 while (operand_mask) {
2118 uint32_t sgpr_idx = 0;
2119 uint32_t sgpr_info_id = 0;
2120 uint32_t mask = operand_mask;
2121 /* choose a sgpr */
2122 while (mask) {
2123 unsigned i = u_bit_scan(&mask);
2124 uint16_t uses = ctx.uses[instr->operands[i].tempId()];
2125 if (sgpr_info_id == 0 || uses < ctx.uses[sgpr_info_id]) {
2126 sgpr_idx = i;
2127 sgpr_info_id = instr->operands[i].tempId();
2128 }
2129 }
2130 operand_mask &= ~(1u << sgpr_idx);
2131
2132 /* Applying two sgprs require making it VOP3, so don't do it unless it's
2133 * definitively beneficial.
2134 * TODO: this is too conservative because later the use count could be reduced to 1 */
2135 if (num_sgprs && ctx.uses[sgpr_info_id] > 1 && !instr->isVOP3())
2136 break;
2137
2138 Temp sgpr = ctx.info[sgpr_info_id].temp;
2139 bool new_sgpr = sgpr.id() != sgpr_ids[0] && sgpr.id() != sgpr_ids[1];
2140 if (new_sgpr && num_sgprs >= max_sgprs)
2141 continue;
2142
2143 if (sgpr_idx == 0 || instr->isVOP3()) {
2144 instr->operands[sgpr_idx] = Operand(sgpr);
2145 } else if (can_swap_operands(instr)) {
2146 instr->operands[sgpr_idx] = instr->operands[0];
2147 instr->operands[0] = Operand(sgpr);
2148 /* swap bits using a 4-entry LUT */
2149 uint32_t swapped = (0x3120 >> (operand_mask & 0x3)) & 0xf;
2150 operand_mask = (operand_mask & ~0x3) | swapped;
2151 } else if (can_use_VOP3(ctx, instr)) {
2152 to_VOP3(ctx, instr);
2153 instr->operands[sgpr_idx] = Operand(sgpr);
2154 } else {
2155 continue;
2156 }
2157
2158 if (new_sgpr)
2159 sgpr_ids[num_sgprs++] = sgpr.id();
2160 ctx.uses[sgpr_info_id]--;
2161 ctx.uses[sgpr.id()]++;
2162 }
2163 }
2164
2165 bool apply_omod_clamp(opt_ctx &ctx, Block& block, aco_ptr<Instruction>& instr)
2166 {
2167 /* check if we could apply omod on predecessor */
2168 if (instr->opcode == aco_opcode::v_mul_f32) {
2169 bool op0 = instr->operands[0].isTemp() && ctx.info[instr->operands[0].tempId()].is_omod_success();
2170 bool op1 = instr->operands[1].isTemp() && ctx.info[instr->operands[1].tempId()].is_omod_success();
2171 if (op0 || op1) {
2172 unsigned idx = op0 ? 0 : 1;
2173 /* omod was successfully applied */
2174 /* if the omod instruction is v_mad, we also have to change the original add */
2175 if (ctx.info[instr->operands[idx].tempId()].is_mad()) {
2176 Instruction* add_instr = ctx.mad_infos[ctx.info[instr->operands[idx].tempId()].val].add_instr.get();
2177 if (ctx.info[instr->definitions[0].tempId()].is_clamp())
2178 static_cast<VOP3A_instruction*>(add_instr)->clamp = true;
2179 add_instr->definitions[0] = instr->definitions[0];
2180 }
2181
2182 Instruction* omod_instr = ctx.info[instr->operands[idx].tempId()].instr;
2183 /* check if we have an additional clamp modifier */
2184 if (ctx.info[instr->definitions[0].tempId()].is_clamp() && ctx.uses[instr->definitions[0].tempId()] == 1 &&
2185 ctx.uses[ctx.info[instr->definitions[0].tempId()].temp.id()]) {
2186 static_cast<VOP3A_instruction*>(omod_instr)->clamp = true;
2187 ctx.info[instr->definitions[0].tempId()].set_clamp_success(omod_instr);
2188 }
2189 /* change definition ssa-id of modified instruction */
2190 omod_instr->definitions[0] = instr->definitions[0];
2191
2192 /* change the definition of instr to something unused, e.g. the original omod def */
2193 instr->definitions[0] = Definition(instr->operands[idx].getTemp());
2194 ctx.uses[instr->definitions[0].tempId()] = 0;
2195 return true;
2196 }
2197 if (!ctx.info[instr->definitions[0].tempId()].label) {
2198 /* in all other cases, label this instruction as option for multiply-add */
2199 ctx.info[instr->definitions[0].tempId()].set_mul(instr.get());
2200 }
2201 }
2202
2203 /* check if we could apply clamp on predecessor */
2204 if (instr->opcode == aco_opcode::v_med3_f32) {
2205 unsigned idx = 0;
2206 bool found_zero = false, found_one = false;
2207 for (unsigned i = 0; i < 3; i++)
2208 {
2209 if (instr->operands[i].constantEquals(0))
2210 found_zero = true;
2211 else if (instr->operands[i].constantEquals(0x3f800000)) /* 1.0 */
2212 found_one = true;
2213 else
2214 idx = i;
2215 }
2216 if (found_zero && found_one && instr->operands[idx].isTemp() &&
2217 ctx.info[instr->operands[idx].tempId()].is_clamp_success()) {
2218 /* clamp was successfully applied */
2219 /* if the clamp instruction is v_mad, we also have to change the original add */
2220 if (ctx.info[instr->operands[idx].tempId()].is_mad()) {
2221 Instruction* add_instr = ctx.mad_infos[ctx.info[instr->operands[idx].tempId()].val].add_instr.get();
2222 add_instr->definitions[0] = instr->definitions[0];
2223 }
2224 Instruction* clamp_instr = ctx.info[instr->operands[idx].tempId()].instr;
2225 /* change definition ssa-id of modified instruction */
2226 clamp_instr->definitions[0] = instr->definitions[0];
2227
2228 /* change the definition of instr to something unused, e.g. the original omod def */
2229 instr->definitions[0] = Definition(instr->operands[idx].getTemp());
2230 ctx.uses[instr->definitions[0].tempId()] = 0;
2231 return true;
2232 }
2233 }
2234
2235 /* omod has no effect if denormals are enabled */
2236 bool can_use_omod = block.fp_mode.denorm32 == 0;
2237
2238 /* apply omod / clamp modifiers if the def is used only once and the instruction can have modifiers */
2239 if (!instr->definitions.empty() && ctx.uses[instr->definitions[0].tempId()] == 1 &&
2240 can_use_VOP3(ctx, instr) && instr_info.can_use_output_modifiers[(int)instr->opcode]) {
2241 ssa_info& def_info = ctx.info[instr->definitions[0].tempId()];
2242 if (can_use_omod && def_info.is_omod2() && ctx.uses[def_info.temp.id()]) {
2243 to_VOP3(ctx, instr);
2244 static_cast<VOP3A_instruction*>(instr.get())->omod = 1;
2245 def_info.set_omod_success(instr.get());
2246 } else if (can_use_omod && def_info.is_omod4() && ctx.uses[def_info.temp.id()]) {
2247 to_VOP3(ctx, instr);
2248 static_cast<VOP3A_instruction*>(instr.get())->omod = 2;
2249 def_info.set_omod_success(instr.get());
2250 } else if (can_use_omod && def_info.is_omod5() && ctx.uses[def_info.temp.id()]) {
2251 to_VOP3(ctx, instr);
2252 static_cast<VOP3A_instruction*>(instr.get())->omod = 3;
2253 def_info.set_omod_success(instr.get());
2254 } else if (def_info.is_clamp() && ctx.uses[def_info.temp.id()]) {
2255 to_VOP3(ctx, instr);
2256 static_cast<VOP3A_instruction*>(instr.get())->clamp = true;
2257 def_info.set_clamp_success(instr.get());
2258 }
2259 }
2260
2261 return false;
2262 }
2263
2264 // TODO: we could possibly move the whole label_instruction pass to combine_instruction:
2265 // this would mean that we'd have to fix the instruction uses while value propagation
2266
2267 void combine_instruction(opt_ctx &ctx, Block& block, aco_ptr<Instruction>& instr)
2268 {
2269 if (instr->definitions.empty() || is_dead(ctx.uses, instr.get()))
2270 return;
2271
2272 if (instr->isVALU()) {
2273 if (can_apply_sgprs(instr))
2274 apply_sgprs(ctx, instr);
2275 if (apply_omod_clamp(ctx, block, instr))
2276 return;
2277 }
2278
2279 if (ctx.info[instr->definitions[0].tempId()].is_vcc_hint()) {
2280 instr->definitions[0].setHint(vcc);
2281 }
2282
2283 /* TODO: There are still some peephole optimizations that could be done:
2284 * - abs(a - b) -> s_absdiff_i32
2285 * - various patterns for s_bitcmp{0,1}_b32 and s_bitset{0,1}_b32
2286 * - patterns for v_alignbit_b32 and v_alignbyte_b32
2287 * These aren't probably too interesting though.
2288 * There are also patterns for v_cmp_class_f{16,32,64}. This is difficult but
2289 * probably more useful than the previously mentioned optimizations.
2290 * The various comparison optimizations also currently only work with 32-bit
2291 * floats. */
2292
2293 /* neg(mul(a, b)) -> mul(neg(a), b) */
2294 if (ctx.info[instr->definitions[0].tempId()].is_neg() && ctx.uses[instr->operands[1].tempId()] == 1) {
2295 Temp val = ctx.info[instr->definitions[0].tempId()].temp;
2296
2297 if (!ctx.info[val.id()].is_mul())
2298 return;
2299
2300 Instruction* mul_instr = ctx.info[val.id()].instr;
2301
2302 if (mul_instr->operands[0].isLiteral())
2303 return;
2304 if (mul_instr->isVOP3() && static_cast<VOP3A_instruction*>(mul_instr)->clamp)
2305 return;
2306
2307 /* convert to mul(neg(a), b) */
2308 ctx.uses[mul_instr->definitions[0].tempId()]--;
2309 Definition def = instr->definitions[0];
2310 /* neg(abs(mul(a, b))) -> mul(neg(abs(a)), abs(b)) */
2311 bool is_abs = ctx.info[instr->definitions[0].tempId()].is_abs();
2312 instr.reset(create_instruction<VOP3A_instruction>(aco_opcode::v_mul_f32, asVOP3(Format::VOP2), 2, 1));
2313 instr->operands[0] = mul_instr->operands[0];
2314 instr->operands[1] = mul_instr->operands[1];
2315 instr->definitions[0] = def;
2316 VOP3A_instruction* new_mul = static_cast<VOP3A_instruction*>(instr.get());
2317 if (mul_instr->isVOP3()) {
2318 VOP3A_instruction* mul = static_cast<VOP3A_instruction*>(mul_instr);
2319 new_mul->neg[0] = mul->neg[0] && !is_abs;
2320 new_mul->neg[1] = mul->neg[1] && !is_abs;
2321 new_mul->abs[0] = mul->abs[0] || is_abs;
2322 new_mul->abs[1] = mul->abs[1] || is_abs;
2323 new_mul->omod = mul->omod;
2324 }
2325 new_mul->neg[0] ^= true;
2326 new_mul->clamp = false;
2327
2328 ctx.info[instr->definitions[0].tempId()].set_mul(instr.get());
2329 return;
2330 }
2331 /* combine mul+add -> mad */
2332 else if ((instr->opcode == aco_opcode::v_add_f32 ||
2333 instr->opcode == aco_opcode::v_sub_f32 ||
2334 instr->opcode == aco_opcode::v_subrev_f32) &&
2335 block.fp_mode.denorm32 == 0 && !block.fp_mode.preserve_signed_zero_inf_nan32) {
2336 //TODO: we could use fma instead when denormals are enabled if the NIR isn't marked as precise
2337
2338 uint32_t uses_src0 = UINT32_MAX;
2339 uint32_t uses_src1 = UINT32_MAX;
2340 Instruction* mul_instr = nullptr;
2341 unsigned add_op_idx;
2342 /* check if any of the operands is a multiplication */
2343 if (instr->operands[0].isTemp() && ctx.info[instr->operands[0].tempId()].is_mul())
2344 uses_src0 = ctx.uses[instr->operands[0].tempId()];
2345 if (instr->operands[1].isTemp() && ctx.info[instr->operands[1].tempId()].is_mul())
2346 uses_src1 = ctx.uses[instr->operands[1].tempId()];
2347
2348 /* find the 'best' mul instruction to combine with the add */
2349 if (uses_src0 < uses_src1) {
2350 mul_instr = ctx.info[instr->operands[0].tempId()].instr;
2351 add_op_idx = 1;
2352 } else if (uses_src1 < uses_src0) {
2353 mul_instr = ctx.info[instr->operands[1].tempId()].instr;
2354 add_op_idx = 0;
2355 } else if (uses_src0 != UINT32_MAX) {
2356 /* tiebreaker: quite random what to pick */
2357 if (ctx.info[instr->operands[0].tempId()].instr->operands[0].isLiteral()) {
2358 mul_instr = ctx.info[instr->operands[1].tempId()].instr;
2359 add_op_idx = 0;
2360 } else {
2361 mul_instr = ctx.info[instr->operands[0].tempId()].instr;
2362 add_op_idx = 1;
2363 }
2364 }
2365 if (mul_instr) {
2366 Operand op[3] = {Operand(v1), Operand(v1), Operand(v1)};
2367 bool neg[3] = {false, false, false};
2368 bool abs[3] = {false, false, false};
2369 unsigned omod = 0;
2370 bool clamp = false;
2371 op[0] = mul_instr->operands[0];
2372 op[1] = mul_instr->operands[1];
2373 op[2] = instr->operands[add_op_idx];
2374 // TODO: would be better to check this before selecting a mul instr?
2375 if (!check_vop3_operands(ctx, 3, op))
2376 return;
2377
2378 if (mul_instr->isVOP3()) {
2379 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*> (mul_instr);
2380 neg[0] = vop3->neg[0];
2381 neg[1] = vop3->neg[1];
2382 abs[0] = vop3->abs[0];
2383 abs[1] = vop3->abs[1];
2384 /* we cannot use these modifiers between mul and add */
2385 if (vop3->clamp || vop3->omod)
2386 return;
2387 }
2388
2389 /* convert to mad */
2390 ctx.uses[mul_instr->definitions[0].tempId()]--;
2391 if (ctx.uses[mul_instr->definitions[0].tempId()]) {
2392 if (op[0].isTemp())
2393 ctx.uses[op[0].tempId()]++;
2394 if (op[1].isTemp())
2395 ctx.uses[op[1].tempId()]++;
2396 }
2397
2398 if (instr->isVOP3()) {
2399 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*> (instr.get());
2400 neg[2] = vop3->neg[add_op_idx];
2401 abs[2] = vop3->abs[add_op_idx];
2402 omod = vop3->omod;
2403 clamp = vop3->clamp;
2404 /* abs of the multiplication result */
2405 if (vop3->abs[1 - add_op_idx]) {
2406 neg[0] = false;
2407 neg[1] = false;
2408 abs[0] = true;
2409 abs[1] = true;
2410 }
2411 /* neg of the multiplication result */
2412 neg[1] = neg[1] ^ vop3->neg[1 - add_op_idx];
2413 }
2414 if (instr->opcode == aco_opcode::v_sub_f32)
2415 neg[1 + add_op_idx] = neg[1 + add_op_idx] ^ true;
2416 else if (instr->opcode == aco_opcode::v_subrev_f32)
2417 neg[2 - add_op_idx] = neg[2 - add_op_idx] ^ true;
2418
2419 aco_ptr<VOP3A_instruction> mad{create_instruction<VOP3A_instruction>(aco_opcode::v_mad_f32, Format::VOP3A, 3, 1)};
2420 for (unsigned i = 0; i < 3; i++)
2421 {
2422 mad->operands[i] = op[i];
2423 mad->neg[i] = neg[i];
2424 mad->abs[i] = abs[i];
2425 }
2426 mad->omod = omod;
2427 mad->clamp = clamp;
2428 mad->definitions[0] = instr->definitions[0];
2429
2430 /* mark this ssa_def to be re-checked for profitability and literals */
2431 ctx.mad_infos.emplace_back(std::move(instr), mul_instr->definitions[0].tempId());
2432 ctx.info[mad->definitions[0].tempId()].set_mad(mad.get(), ctx.mad_infos.size() - 1);
2433 instr.reset(mad.release());
2434 return;
2435 }
2436 }
2437 /* v_mul_f32(v_cndmask_b32(0, 1.0, cond), a) -> v_cndmask_b32(0, a, cond) */
2438 else if (instr->opcode == aco_opcode::v_mul_f32 && !instr->isVOP3()) {
2439 for (unsigned i = 0; i < 2; i++) {
2440 if (instr->operands[i].isTemp() && ctx.info[instr->operands[i].tempId()].is_b2f() &&
2441 ctx.uses[instr->operands[i].tempId()] == 1 &&
2442 instr->operands[!i].isTemp() && instr->operands[!i].getTemp().type() == RegType::vgpr) {
2443 ctx.uses[instr->operands[i].tempId()]--;
2444 ctx.uses[ctx.info[instr->operands[i].tempId()].temp.id()]++;
2445
2446 aco_ptr<VOP2_instruction> new_instr{create_instruction<VOP2_instruction>(aco_opcode::v_cndmask_b32, Format::VOP2, 3, 1)};
2447 new_instr->operands[0] = Operand(0u);
2448 new_instr->operands[1] = instr->operands[!i];
2449 new_instr->operands[2] = Operand(ctx.info[instr->operands[i].tempId()].temp);
2450 new_instr->definitions[0] = instr->definitions[0];
2451 instr.reset(new_instr.release());
2452 ctx.info[instr->definitions[0].tempId()].label = 0;
2453 return;
2454 }
2455 }
2456 } else if (instr->opcode == aco_opcode::v_or_b32 && ctx.program->chip_class >= GFX9) {
2457 if (combine_three_valu_op(ctx, instr, aco_opcode::v_or_b32, aco_opcode::v_or3_b32, "012", 1 | 2)) ;
2458 else if (combine_three_valu_op(ctx, instr, aco_opcode::v_and_b32, aco_opcode::v_and_or_b32, "120", 1 | 2)) ;
2459 else combine_three_valu_op(ctx, instr, aco_opcode::v_lshlrev_b32, aco_opcode::v_lshl_or_b32, "210", 1 | 2);
2460 } else if (instr->opcode == aco_opcode::v_add_u32 && ctx.program->chip_class >= GFX9) {
2461 if (combine_three_valu_op(ctx, instr, aco_opcode::v_xor_b32, aco_opcode::v_xad_u32, "120", 1 | 2)) ;
2462 else if (combine_three_valu_op(ctx, instr, aco_opcode::v_add_u32, aco_opcode::v_add3_u32, "012", 1 | 2)) ;
2463 else combine_three_valu_op(ctx, instr, aco_opcode::v_lshlrev_b32, aco_opcode::v_lshl_add_u32, "210", 1 | 2);
2464 } else if (instr->opcode == aco_opcode::v_lshlrev_b32 && ctx.program->chip_class >= GFX9) {
2465 combine_three_valu_op(ctx, instr, aco_opcode::v_add_u32, aco_opcode::v_add_lshl_u32, "120", 2);
2466 } else if ((instr->opcode == aco_opcode::s_add_u32 || instr->opcode == aco_opcode::s_add_i32) && ctx.program->chip_class >= GFX9) {
2467 combine_salu_lshl_add(ctx, instr);
2468 } else if (instr->opcode == aco_opcode::s_not_b32) {
2469 combine_salu_not_bitwise(ctx, instr);
2470 } else if (instr->opcode == aco_opcode::s_not_b64) {
2471 if (combine_inverse_comparison(ctx, instr)) ;
2472 else combine_salu_not_bitwise(ctx, instr);
2473 } else if (instr->opcode == aco_opcode::s_and_b32 || instr->opcode == aco_opcode::s_or_b32 ||
2474 instr->opcode == aco_opcode::s_and_b64 || instr->opcode == aco_opcode::s_or_b64) {
2475 if (combine_ordering_test(ctx, instr)) ;
2476 else if (combine_comparison_ordering(ctx, instr)) ;
2477 else if (combine_constant_comparison_ordering(ctx, instr)) ;
2478 else combine_salu_n2(ctx, instr);
2479 } else {
2480 aco_opcode min, max, min3, max3, med3;
2481 bool some_gfx9_only;
2482 if (get_minmax_info(instr->opcode, &min, &max, &min3, &max3, &med3, &some_gfx9_only) &&
2483 (!some_gfx9_only || ctx.program->chip_class >= GFX9)) {
2484 if (combine_minmax(ctx, instr, instr->opcode == min ? max : min, instr->opcode == min ? min3 : max3)) ;
2485 else combine_clamp(ctx, instr, min, max, med3);
2486 }
2487 }
2488 }
2489
2490 bool to_uniform_bool_instr(opt_ctx &ctx, aco_ptr<Instruction> &instr)
2491 {
2492 switch (instr->opcode) {
2493 case aco_opcode::s_and_b32:
2494 case aco_opcode::s_and_b64:
2495 instr->opcode = aco_opcode::s_and_b32;
2496 break;
2497 case aco_opcode::s_or_b32:
2498 case aco_opcode::s_or_b64:
2499 instr->opcode = aco_opcode::s_or_b32;
2500 break;
2501 case aco_opcode::s_xor_b32:
2502 case aco_opcode::s_xor_b64:
2503 instr->opcode = aco_opcode::s_absdiff_i32;
2504 break;
2505 default:
2506 /* Don't transform other instructions. They are very unlikely to appear here. */
2507 return false;
2508 }
2509
2510 for (Operand &op : instr->operands) {
2511 ctx.uses[op.tempId()]--;
2512
2513 if (ctx.info[op.tempId()].is_uniform_bool()) {
2514 /* Just use the uniform boolean temp. */
2515 op.setTemp(ctx.info[op.tempId()].temp);
2516 } else if (ctx.info[op.tempId()].is_uniform_bitwise()) {
2517 /* Use the SCC definition of the predecessor instruction.
2518 * This allows the predecessor to get picked up by the same optimization (if it has no divergent users),
2519 * and it also makes sure that the current instruction will keep working even if the predecessor won't be transformed.
2520 */
2521 Instruction *pred_instr = ctx.info[op.tempId()].instr;
2522 assert(pred_instr->definitions.size() >= 2);
2523 assert(pred_instr->definitions[1].isFixed() && pred_instr->definitions[1].physReg() == scc);
2524 op.setTemp(pred_instr->definitions[1].getTemp());
2525 } else {
2526 unreachable("Invalid operand on uniform bitwise instruction.");
2527 }
2528
2529 ctx.uses[op.tempId()]++;
2530 }
2531
2532 instr->definitions[0].setTemp(Temp(instr->definitions[0].tempId(), s1));
2533 assert(instr->operands[0].regClass() == s1);
2534 assert(instr->operands[1].regClass() == s1);
2535 return true;
2536 }
2537
2538 void select_instruction(opt_ctx &ctx, aco_ptr<Instruction>& instr)
2539 {
2540 const uint32_t threshold = 4;
2541
2542 if (is_dead(ctx.uses, instr.get())) {
2543 instr.reset();
2544 return;
2545 }
2546
2547 /* convert split_vector into a copy or extract_vector if only one definition is ever used */
2548 if (instr->opcode == aco_opcode::p_split_vector) {
2549 unsigned num_used = 0;
2550 unsigned idx = 0;
2551 for (unsigned i = 0; i < instr->definitions.size(); i++) {
2552 if (ctx.uses[instr->definitions[i].tempId()]) {
2553 num_used++;
2554 idx = i;
2555 }
2556 }
2557 bool done = false;
2558 if (num_used == 1 && ctx.info[instr->operands[0].tempId()].is_vec() &&
2559 ctx.uses[instr->operands[0].tempId()] == 1) {
2560 Instruction *vec = ctx.info[instr->operands[0].tempId()].instr;
2561
2562 unsigned off = 0;
2563 Operand op;
2564 for (Operand& vec_op : vec->operands) {
2565 if (off == idx * instr->definitions[0].size()) {
2566 op = vec_op;
2567 break;
2568 }
2569 off += vec_op.size();
2570 }
2571 if (off != instr->operands[0].size()) {
2572 ctx.uses[instr->operands[0].tempId()]--;
2573 for (Operand& vec_op : vec->operands) {
2574 if (vec_op.isTemp())
2575 ctx.uses[vec_op.tempId()]--;
2576 }
2577 if (op.isTemp())
2578 ctx.uses[op.tempId()]++;
2579
2580 aco_ptr<Pseudo_instruction> extract{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 1, 1)};
2581 extract->operands[0] = op;
2582 extract->definitions[0] = instr->definitions[idx];
2583 instr.reset(extract.release());
2584
2585 done = true;
2586 }
2587 }
2588
2589 if (!done && num_used == 1) {
2590 aco_ptr<Pseudo_instruction> extract{create_instruction<Pseudo_instruction>(aco_opcode::p_extract_vector, Format::PSEUDO, 2, 1)};
2591 extract->operands[0] = instr->operands[0];
2592 extract->operands[1] = Operand((uint32_t) idx);
2593 extract->definitions[0] = instr->definitions[idx];
2594 instr.reset(extract.release());
2595 }
2596 }
2597
2598 mad_info* mad_info = NULL;
2599 if (instr->opcode == aco_opcode::v_mad_f32 && ctx.info[instr->definitions[0].tempId()].is_mad()) {
2600 mad_info = &ctx.mad_infos[ctx.info[instr->definitions[0].tempId()].val];
2601 /* re-check mad instructions */
2602 if (ctx.uses[mad_info->mul_temp_id]) {
2603 ctx.uses[mad_info->mul_temp_id]++;
2604 if (instr->operands[0].isTemp())
2605 ctx.uses[instr->operands[0].tempId()]--;
2606 if (instr->operands[1].isTemp())
2607 ctx.uses[instr->operands[1].tempId()]--;
2608 instr.swap(mad_info->add_instr);
2609 mad_info = NULL;
2610 }
2611 /* check literals */
2612 else if (!instr->usesModifiers()) {
2613 bool sgpr_used = false;
2614 uint32_t literal_idx = 0;
2615 uint32_t literal_uses = UINT32_MAX;
2616 for (unsigned i = 0; i < instr->operands.size(); i++)
2617 {
2618 if (instr->operands[i].isConstant() && i > 0) {
2619 literal_uses = UINT32_MAX;
2620 break;
2621 }
2622 if (!instr->operands[i].isTemp())
2623 continue;
2624 /* if one of the operands is sgpr, we cannot add a literal somewhere else on pre-GFX10 or operands other than the 1st */
2625 if (instr->operands[i].getTemp().type() == RegType::sgpr && (i > 0 || ctx.program->chip_class < GFX10)) {
2626 if (!sgpr_used && ctx.info[instr->operands[i].tempId()].is_literal()) {
2627 literal_uses = ctx.uses[instr->operands[i].tempId()];
2628 literal_idx = i;
2629 } else {
2630 literal_uses = UINT32_MAX;
2631 }
2632 sgpr_used = true;
2633 /* don't break because we still need to check constants */
2634 } else if (!sgpr_used &&
2635 ctx.info[instr->operands[i].tempId()].is_literal() &&
2636 ctx.uses[instr->operands[i].tempId()] < literal_uses) {
2637 literal_uses = ctx.uses[instr->operands[i].tempId()];
2638 literal_idx = i;
2639 }
2640 }
2641
2642 /* Limit the number of literals to apply to not increase the code
2643 * size too much, but always apply literals for v_mad->v_madak
2644 * because both instructions are 64-bit and this doesn't increase
2645 * code size.
2646 * TODO: try to apply the literals earlier to lower the number of
2647 * uses below threshold
2648 */
2649 if (literal_uses < threshold || literal_idx == 2) {
2650 ctx.uses[instr->operands[literal_idx].tempId()]--;
2651 mad_info->check_literal = true;
2652 mad_info->literal_idx = literal_idx;
2653 return;
2654 }
2655 }
2656 }
2657
2658 /* Mark SCC needed, so the uniform boolean transformation won't swap the definitions when it isn't beneficial */
2659 if (instr->format == Format::PSEUDO_BRANCH &&
2660 instr->operands.size() &&
2661 instr->operands[0].isTemp()) {
2662 ctx.info[instr->operands[0].tempId()].set_scc_needed();
2663 return;
2664 } else if ((instr->opcode == aco_opcode::s_cselect_b64 ||
2665 instr->opcode == aco_opcode::s_cselect_b32) &&
2666 instr->operands[2].isTemp()) {
2667 ctx.info[instr->operands[2].tempId()].set_scc_needed();
2668 }
2669
2670 /* check for literals */
2671 if (!instr->isSALU() && !instr->isVALU())
2672 return;
2673
2674 /* Transform uniform bitwise boolean operations to 32-bit when there are no divergent uses. */
2675 if (instr->definitions.size() &&
2676 ctx.uses[instr->definitions[0].tempId()] == 0 &&
2677 ctx.info[instr->definitions[0].tempId()].is_uniform_bitwise()) {
2678 bool transform_done = to_uniform_bool_instr(ctx, instr);
2679
2680 if (transform_done && !ctx.info[instr->definitions[1].tempId()].is_scc_needed()) {
2681 /* Swap the two definition IDs in order to avoid overusing the SCC. This reduces extra moves generated by RA. */
2682 uint32_t def0_id = instr->definitions[0].getTemp().id();
2683 uint32_t def1_id = instr->definitions[1].getTemp().id();
2684 instr->definitions[0].setTemp(Temp(def1_id, s1));
2685 instr->definitions[1].setTemp(Temp(def0_id, s1));
2686 }
2687
2688 return;
2689 }
2690
2691 if (instr->isSDWA() || instr->isDPP() || (instr->isVOP3() && ctx.program->chip_class < GFX10))
2692 return; /* some encodings can't ever take literals */
2693
2694 /* we do not apply the literals yet as we don't know if it is profitable */
2695 Operand current_literal(s1);
2696
2697 unsigned literal_id = 0;
2698 unsigned literal_uses = UINT32_MAX;
2699 Operand literal(s1);
2700 unsigned num_operands = 1;
2701 if (instr->isSALU() || (ctx.program->chip_class >= GFX10 && can_use_VOP3(ctx, instr)))
2702 num_operands = instr->operands.size();
2703 /* catch VOP2 with a 3rd SGPR operand (e.g. v_cndmask_b32, v_addc_co_u32) */
2704 else if (instr->isVALU() && instr->operands.size() >= 3)
2705 return;
2706
2707 unsigned sgpr_ids[2] = {0, 0};
2708 bool is_literal_sgpr = false;
2709 uint32_t mask = 0;
2710
2711 /* choose a literal to apply */
2712 for (unsigned i = 0; i < num_operands; i++) {
2713 Operand op = instr->operands[i];
2714
2715 if (instr->isVALU() && op.isTemp() && op.getTemp().type() == RegType::sgpr &&
2716 op.tempId() != sgpr_ids[0])
2717 sgpr_ids[!!sgpr_ids[0]] = op.tempId();
2718
2719 if (op.isLiteral()) {
2720 current_literal = op;
2721 continue;
2722 } else if (!op.isTemp() || !ctx.info[op.tempId()].is_literal()) {
2723 continue;
2724 }
2725
2726 if (!alu_can_accept_constant(instr->opcode, i))
2727 continue;
2728
2729 if (ctx.uses[op.tempId()] < literal_uses) {
2730 is_literal_sgpr = op.getTemp().type() == RegType::sgpr;
2731 mask = 0;
2732 literal = Operand(ctx.info[op.tempId()].val);
2733 literal_uses = ctx.uses[op.tempId()];
2734 literal_id = op.tempId();
2735 }
2736
2737 mask |= (op.tempId() == literal_id) << i;
2738 }
2739
2740
2741 /* don't go over the constant bus limit */
2742 bool is_shift64 = instr->opcode == aco_opcode::v_lshlrev_b64 ||
2743 instr->opcode == aco_opcode::v_lshrrev_b64 ||
2744 instr->opcode == aco_opcode::v_ashrrev_i64;
2745 unsigned const_bus_limit = instr->isVALU() ? 1 : UINT32_MAX;
2746 if (ctx.program->chip_class >= GFX10 && !is_shift64)
2747 const_bus_limit = 2;
2748
2749 unsigned num_sgprs = !!sgpr_ids[0] + !!sgpr_ids[1];
2750 if (num_sgprs == const_bus_limit && !is_literal_sgpr)
2751 return;
2752
2753 if (literal_id && literal_uses < threshold &&
2754 (current_literal.isUndefined() ||
2755 (current_literal.size() == literal.size() &&
2756 current_literal.constantValue() == literal.constantValue()))) {
2757 /* mark the literal to be applied */
2758 while (mask) {
2759 unsigned i = u_bit_scan(&mask);
2760 if (instr->operands[i].isTemp() && instr->operands[i].tempId() == literal_id)
2761 ctx.uses[instr->operands[i].tempId()]--;
2762 }
2763 }
2764 }
2765
2766
2767 void apply_literals(opt_ctx &ctx, aco_ptr<Instruction>& instr)
2768 {
2769 /* Cleanup Dead Instructions */
2770 if (!instr)
2771 return;
2772
2773 /* apply literals on MAD */
2774 if (instr->opcode == aco_opcode::v_mad_f32 && ctx.info[instr->definitions[0].tempId()].is_mad()) {
2775 mad_info* info = &ctx.mad_infos[ctx.info[instr->definitions[0].tempId()].val];
2776 if (info->check_literal &&
2777 (ctx.uses[instr->operands[info->literal_idx].tempId()] == 0 || info->literal_idx == 2)) {
2778 aco_ptr<Instruction> new_mad;
2779 if (info->literal_idx == 2) { /* add literal -> madak */
2780 new_mad.reset(create_instruction<VOP2_instruction>(aco_opcode::v_madak_f32, Format::VOP2, 3, 1));
2781 new_mad->operands[0] = instr->operands[0];
2782 new_mad->operands[1] = instr->operands[1];
2783 } else { /* mul literal -> madmk */
2784 new_mad.reset(create_instruction<VOP2_instruction>(aco_opcode::v_madmk_f32, Format::VOP2, 3, 1));
2785 new_mad->operands[0] = instr->operands[1 - info->literal_idx];
2786 new_mad->operands[1] = instr->operands[2];
2787 }
2788 new_mad->operands[2] = Operand(ctx.info[instr->operands[info->literal_idx].tempId()].val);
2789 new_mad->definitions[0] = instr->definitions[0];
2790 ctx.instructions.emplace_back(std::move(new_mad));
2791 return;
2792 }
2793 }
2794
2795 /* apply literals on other SALU/VALU */
2796 if (instr->isSALU() || instr->isVALU()) {
2797 for (unsigned i = 0; i < instr->operands.size(); i++) {
2798 Operand op = instr->operands[i];
2799 if (op.isTemp() && ctx.info[op.tempId()].is_literal() && ctx.uses[op.tempId()] == 0) {
2800 Operand literal(ctx.info[op.tempId()].val);
2801 if (instr->isVALU() && i > 0)
2802 to_VOP3(ctx, instr);
2803 instr->operands[i] = literal;
2804 }
2805 }
2806 }
2807
2808 ctx.instructions.emplace_back(std::move(instr));
2809 }
2810
2811
2812 void optimize(Program* program)
2813 {
2814 opt_ctx ctx;
2815 ctx.program = program;
2816 std::vector<ssa_info> info(program->peekAllocationId());
2817 ctx.info = info.data();
2818
2819 /* 1. Bottom-Up DAG pass (forward) to label all ssa-defs */
2820 for (Block& block : program->blocks) {
2821 for (aco_ptr<Instruction>& instr : block.instructions)
2822 label_instruction(ctx, block, instr);
2823 }
2824
2825 ctx.uses = std::move(dead_code_analysis(program));
2826
2827 /* 2. Combine v_mad, omod, clamp and propagate sgpr on VALU instructions */
2828 for (Block& block : program->blocks) {
2829 for (aco_ptr<Instruction>& instr : block.instructions)
2830 combine_instruction(ctx, block, instr);
2831 }
2832
2833 /* 3. Top-Down DAG pass (backward) to select instructions (includes DCE) */
2834 for (std::vector<Block>::reverse_iterator it = program->blocks.rbegin(); it != program->blocks.rend(); ++it) {
2835 Block* block = &(*it);
2836 for (std::vector<aco_ptr<Instruction>>::reverse_iterator it = block->instructions.rbegin(); it != block->instructions.rend(); ++it)
2837 select_instruction(ctx, *it);
2838 }
2839
2840 /* 4. Add literals to instructions */
2841 for (Block& block : program->blocks) {
2842 ctx.instructions.clear();
2843 for (aco_ptr<Instruction>& instr : block.instructions)
2844 apply_literals(ctx, instr);
2845 block.instructions.swap(ctx.instructions);
2846 }
2847
2848 }
2849
2850 }