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