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