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