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