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