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