aco: handle undef p_create_vector operands in the optimizer
[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 bool copy_prop = instr->operands.size() == 1 && instr->operands[0].isTemp();
902 if (copy_prop)
903 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
904
905 unsigned num_ops = instr->operands.size();
906 for (const Operand& op : instr->operands) {
907 if (op.isTemp() && ctx.info[op.tempId()].is_vec())
908 num_ops += ctx.info[op.tempId()].instr->operands.size() - 1;
909 }
910 if (num_ops != instr->operands.size()) {
911 aco_ptr<Instruction> old_vec = std::move(instr);
912 instr.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, num_ops, 1));
913 instr->definitions[0] = old_vec->definitions[0];
914 unsigned k = 0;
915 for (Operand& old_op : old_vec->operands) {
916 if (old_op.isTemp() && ctx.info[old_op.tempId()].is_vec()) {
917 for (unsigned j = 0; j < ctx.info[old_op.tempId()].instr->operands.size(); j++) {
918 Operand op = ctx.info[old_op.tempId()].instr->operands[j];
919 if (op.isTemp() && ctx.info[op.tempId()].is_temp() &&
920 ctx.info[op.tempId()].temp.type() == instr->definitions[0].regClass().type())
921 op.setTemp(ctx.info[op.tempId()].temp);
922 instr->operands[k++] = op;
923 }
924 } else {
925 instr->operands[k++] = old_op;
926 }
927 }
928 assert(k == num_ops);
929 }
930
931 if (!copy_prop)
932 ctx.info[instr->definitions[0].tempId()].set_vec(instr.get());
933 break;
934 }
935 case aco_opcode::p_split_vector: {
936 if (!ctx.info[instr->operands[0].tempId()].is_vec())
937 break;
938 Instruction* vec = ctx.info[instr->operands[0].tempId()].instr;
939 unsigned split_offset = 0;
940 unsigned vec_offset = 0;
941 unsigned vec_index = 0;
942 for (unsigned i = 0; i < instr->definitions.size(); split_offset += instr->definitions[i++].bytes()) {
943 while (vec_offset < split_offset && vec_index < vec->operands.size())
944 vec_offset += vec->operands[vec_index++].bytes();
945
946 if (vec_offset != split_offset || vec->operands[vec_index].bytes() != instr->definitions[i].bytes())
947 continue;
948
949 Operand vec_op = vec->operands[vec_index];
950 if (vec_op.isConstant()) {
951 if (vec_op.isLiteral())
952 ctx.info[instr->definitions[i].tempId()].set_literal(vec_op.constantValue());
953 else if (vec_op.size() == 1)
954 ctx.info[instr->definitions[i].tempId()].set_constant(vec_op.constantValue());
955 else if (vec_op.size() == 2)
956 ctx.info[instr->definitions[i].tempId()].set_constant_64bit(vec_op.constantValue());
957 } else if (vec_op.isUndefined()) {
958 ctx.info[instr->definitions[i].tempId()].set_undefined();
959 } else {
960 assert(vec_op.isTemp());
961 ctx.info[instr->definitions[i].tempId()].set_temp(vec_op.getTemp());
962 }
963 }
964 break;
965 }
966 case aco_opcode::p_extract_vector: { /* mov */
967 if (!ctx.info[instr->operands[0].tempId()].is_vec())
968 break;
969
970 /* check if we index directly into a vector element */
971 Instruction* vec = ctx.info[instr->operands[0].tempId()].instr;
972 const unsigned index = instr->operands[1].constantValue();
973 const unsigned dst_offset = index * instr->definitions[0].bytes();
974 unsigned offset = 0;
975
976 for (const Operand& op : vec->operands) {
977 if (offset < dst_offset) {
978 offset += op.bytes();
979 continue;
980 } else if (offset != dst_offset || op.bytes() != instr->definitions[0].bytes()) {
981 break;
982 }
983
984 /* convert this extract into a copy instruction */
985 instr->opcode = aco_opcode::p_parallelcopy;
986 instr->operands.pop_back();
987 instr->operands[0] = op;
988
989 if (op.isConstant()) {
990 if (op.isLiteral())
991 ctx.info[instr->definitions[0].tempId()].set_literal(op.constantValue());
992 else if (op.size() == 1)
993 ctx.info[instr->definitions[0].tempId()].set_constant(op.constantValue());
994 else if (op.size() == 2)
995 ctx.info[instr->definitions[0].tempId()].set_constant_64bit(op.constantValue());
996 } else if (op.isUndefined()) {
997 ctx.info[instr->definitions[0].tempId()].set_undefined();
998 } else {
999 assert(op.isTemp());
1000 ctx.info[instr->definitions[0].tempId()].set_temp(op.getTemp());
1001 }
1002 break;
1003 }
1004 break;
1005 }
1006 case aco_opcode::s_mov_b32: /* propagate */
1007 case aco_opcode::s_mov_b64:
1008 case aco_opcode::v_mov_b32:
1009 case aco_opcode::p_as_uniform:
1010 if (instr->definitions[0].isFixed()) {
1011 /* don't copy-propagate copies into fixed registers */
1012 } else if (instr->usesModifiers()) {
1013 // TODO
1014 } else if (instr->operands[0].isConstant()) {
1015 if (instr->operands[0].isLiteral())
1016 ctx.info[instr->definitions[0].tempId()].set_literal(instr->operands[0].constantValue());
1017 else if (instr->operands[0].size() == 1)
1018 ctx.info[instr->definitions[0].tempId()].set_constant(instr->operands[0].constantValue());
1019 else if (instr->operands[0].size() == 2)
1020 ctx.info[instr->definitions[0].tempId()].set_constant_64bit(instr->operands[0].constantValue());
1021 } else if (instr->operands[0].isTemp()) {
1022 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
1023 } else {
1024 assert(instr->operands[0].isFixed());
1025 }
1026 break;
1027 case aco_opcode::p_is_helper:
1028 if (!ctx.program->needs_wqm)
1029 ctx.info[instr->definitions[0].tempId()].set_constant(0u);
1030 break;
1031 case aco_opcode::s_movk_i32: {
1032 uint32_t v = static_cast<SOPK_instruction*>(instr.get())->imm;
1033 v = v & 0x8000 ? (v | 0xffff0000) : v;
1034 if (v <= 64 || v >= 0xfffffff0)
1035 ctx.info[instr->definitions[0].tempId()].set_constant(v);
1036 else
1037 ctx.info[instr->definitions[0].tempId()].set_literal(v);
1038 break;
1039 }
1040 case aco_opcode::v_bfrev_b32:
1041 case aco_opcode::s_brev_b32: {
1042 if (instr->operands[0].isConstant()) {
1043 uint32_t v = util_bitreverse(instr->operands[0].constantValue());
1044 if (v <= 64 || v >= 0xfffffff0)
1045 ctx.info[instr->definitions[0].tempId()].set_constant(v);
1046 else
1047 ctx.info[instr->definitions[0].tempId()].set_literal(v);
1048 }
1049 break;
1050 }
1051 case aco_opcode::s_bfm_b32: {
1052 if (instr->operands[0].isConstant() && instr->operands[1].isConstant()) {
1053 unsigned size = instr->operands[0].constantValue() & 0x1f;
1054 unsigned start = instr->operands[1].constantValue() & 0x1f;
1055 uint32_t v = ((1u << size) - 1u) << start;
1056 if (v <= 64 || v >= 0xfffffff0)
1057 ctx.info[instr->definitions[0].tempId()].set_constant(v);
1058 else
1059 ctx.info[instr->definitions[0].tempId()].set_literal(v);
1060 }
1061 }
1062 case aco_opcode::v_mul_f32: { /* omod */
1063 /* TODO: try to move the negate/abs modifier to the consumer instead */
1064 if (instr->usesModifiers())
1065 break;
1066
1067 for (unsigned i = 0; i < 2; i++) {
1068 if (instr->operands[!i].isConstant() && instr->operands[i].isTemp()) {
1069 if (instr->operands[!i].constantValue() == 0x40000000) { /* 2.0 */
1070 ctx.info[instr->operands[i].tempId()].set_omod2(instr->definitions[0].getTemp());
1071 } else if (instr->operands[!i].constantValue() == 0x40800000) { /* 4.0 */
1072 ctx.info[instr->operands[i].tempId()].set_omod4(instr->definitions[0].getTemp());
1073 } else if (instr->operands[!i].constantValue() == 0x3f000000) { /* 0.5 */
1074 ctx.info[instr->operands[i].tempId()].set_omod5(instr->definitions[0].getTemp());
1075 } else if (instr->operands[!i].constantValue() == 0x3f800000 &&
1076 !block.fp_mode.must_flush_denorms32) { /* 1.0 */
1077 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[i].getTemp());
1078 } else {
1079 continue;
1080 }
1081 break;
1082 }
1083 }
1084 break;
1085 }
1086 case aco_opcode::v_and_b32: /* abs */
1087 if (!instr->usesModifiers() && instr->operands[0].constantEquals(0x7FFFFFFF) &&
1088 instr->operands[1].isTemp() && instr->operands[1].getTemp().type() == RegType::vgpr)
1089 ctx.info[instr->definitions[0].tempId()].set_abs(instr->operands[1].getTemp());
1090 else
1091 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1092 break;
1093 case aco_opcode::v_xor_b32: { /* neg */
1094 if (!instr->usesModifiers() && instr->operands[0].constantEquals(0x80000000u) && instr->operands[1].isTemp()) {
1095 if (ctx.info[instr->operands[1].tempId()].is_neg()) {
1096 ctx.info[instr->definitions[0].tempId()].set_temp(ctx.info[instr->operands[1].tempId()].temp);
1097 } else if (instr->operands[1].getTemp().type() == RegType::vgpr) {
1098 if (ctx.info[instr->operands[1].tempId()].is_abs()) { /* neg(abs(x)) */
1099 instr->operands[1].setTemp(ctx.info[instr->operands[1].tempId()].temp);
1100 instr->opcode = aco_opcode::v_or_b32;
1101 ctx.info[instr->definitions[0].tempId()].set_neg_abs(instr->operands[1].getTemp());
1102 } else {
1103 ctx.info[instr->definitions[0].tempId()].set_neg(instr->operands[1].getTemp());
1104 }
1105 }
1106 } else {
1107 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1108 }
1109 break;
1110 }
1111 case aco_opcode::v_med3_f32: { /* clamp */
1112 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(instr.get());
1113 if (vop3->abs[0] || vop3->abs[1] || vop3->abs[2] ||
1114 vop3->neg[0] || vop3->neg[1] || vop3->neg[2] ||
1115 vop3->omod != 0 || vop3->opsel != 0)
1116 break;
1117
1118 unsigned idx = 0;
1119 bool found_zero = false, found_one = false;
1120 for (unsigned i = 0; i < 3; i++)
1121 {
1122 if (instr->operands[i].constantEquals(0))
1123 found_zero = true;
1124 else if (instr->operands[i].constantEquals(0x3f800000)) /* 1.0 */
1125 found_one = true;
1126 else
1127 idx = i;
1128 }
1129 if (found_zero && found_one && instr->operands[idx].isTemp()) {
1130 ctx.info[instr->operands[idx].tempId()].set_clamp(instr->definitions[0].getTemp());
1131 }
1132 break;
1133 }
1134 case aco_opcode::v_cndmask_b32:
1135 if (instr->operands[0].constantEquals(0) &&
1136 instr->operands[1].constantEquals(0xFFFFFFFF) &&
1137 instr->operands[2].isTemp())
1138 ctx.info[instr->definitions[0].tempId()].set_vcc(instr->operands[2].getTemp());
1139 else if (instr->operands[0].constantEquals(0) &&
1140 instr->operands[1].constantEquals(0x3f800000u) &&
1141 instr->operands[2].isTemp())
1142 ctx.info[instr->definitions[0].tempId()].set_b2f(instr->operands[2].getTemp());
1143
1144 ctx.info[instr->operands[2].tempId()].set_vcc_hint();
1145 break;
1146 case aco_opcode::v_cmp_lg_u32:
1147 if (instr->format == Format::VOPC && /* don't optimize VOP3 / SDWA / DPP */
1148 instr->operands[0].constantEquals(0) &&
1149 instr->operands[1].isTemp() && ctx.info[instr->operands[1].tempId()].is_vcc())
1150 ctx.info[instr->definitions[0].tempId()].set_temp(ctx.info[instr->operands[1].tempId()].temp);
1151 break;
1152 case aco_opcode::p_phi:
1153 case aco_opcode::p_linear_phi: {
1154 /* lower_bool_phis() can create phis like this */
1155 bool all_same_temp = instr->operands[0].isTemp();
1156 /* this check is needed when moving uniform loop counters out of a divergent loop */
1157 if (all_same_temp)
1158 all_same_temp = instr->definitions[0].regClass() == instr->operands[0].regClass();
1159 for (unsigned i = 1; all_same_temp && (i < instr->operands.size()); i++) {
1160 if (!instr->operands[i].isTemp() || instr->operands[i].tempId() != instr->operands[0].tempId())
1161 all_same_temp = false;
1162 }
1163 if (all_same_temp) {
1164 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
1165 } else {
1166 bool all_undef = instr->operands[0].isUndefined();
1167 for (unsigned i = 1; all_undef && (i < instr->operands.size()); i++) {
1168 if (!instr->operands[i].isUndefined())
1169 all_undef = false;
1170 }
1171 if (all_undef)
1172 ctx.info[instr->definitions[0].tempId()].set_undefined();
1173 }
1174 break;
1175 }
1176 case aco_opcode::v_add_u32:
1177 case aco_opcode::v_add_co_u32:
1178 case aco_opcode::v_add_co_u32_e64:
1179 case aco_opcode::s_add_i32:
1180 case aco_opcode::s_add_u32:
1181 ctx.info[instr->definitions[0].tempId()].set_add_sub(instr.get());
1182 break;
1183 case aco_opcode::s_not_b32:
1184 case aco_opcode::s_not_b64:
1185 if (ctx.info[instr->operands[0].tempId()].is_uniform_bool()) {
1186 ctx.info[instr->definitions[0].tempId()].set_uniform_bitwise();
1187 ctx.info[instr->definitions[1].tempId()].set_scc_invert(ctx.info[instr->operands[0].tempId()].temp);
1188 } else if (ctx.info[instr->operands[0].tempId()].is_uniform_bitwise()) {
1189 ctx.info[instr->definitions[0].tempId()].set_uniform_bitwise();
1190 ctx.info[instr->definitions[1].tempId()].set_scc_invert(ctx.info[instr->operands[0].tempId()].instr->definitions[1].getTemp());
1191 }
1192 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1193 break;
1194 case aco_opcode::s_and_b32:
1195 case aco_opcode::s_and_b64:
1196 if (fixed_to_exec(instr->operands[1]) && instr->operands[0].isTemp()) {
1197 if (ctx.info[instr->operands[0].tempId()].is_uniform_bool()) {
1198 /* Try to get rid of the superfluous s_cselect + s_and_b64 that comes from turning a uniform bool into divergent */
1199 ctx.info[instr->definitions[1].tempId()].set_temp(ctx.info[instr->operands[0].tempId()].temp);
1200 ctx.info[instr->definitions[0].tempId()].set_uniform_bool(ctx.info[instr->operands[0].tempId()].temp);
1201 break;
1202 } else if (ctx.info[instr->operands[0].tempId()].is_uniform_bitwise()) {
1203 /* Try to get rid of the superfluous s_and_b64, since the uniform bitwise instruction already produces the same SCC */
1204 ctx.info[instr->definitions[1].tempId()].set_temp(ctx.info[instr->operands[0].tempId()].instr->definitions[1].getTemp());
1205 ctx.info[instr->definitions[0].tempId()].set_uniform_bool(ctx.info[instr->operands[0].tempId()].instr->definitions[1].getTemp());
1206 break;
1207 }
1208 }
1209 /* fallthrough */
1210 case aco_opcode::s_or_b32:
1211 case aco_opcode::s_or_b64:
1212 case aco_opcode::s_xor_b32:
1213 case aco_opcode::s_xor_b64:
1214 if (std::all_of(instr->operands.begin(), instr->operands.end(), [&ctx](const Operand& op) {
1215 return op.isTemp() && (ctx.info[op.tempId()].is_uniform_bool() || ctx.info[op.tempId()].is_uniform_bitwise());
1216 })) {
1217 ctx.info[instr->definitions[0].tempId()].set_uniform_bitwise();
1218 }
1219 /* fallthrough */
1220 case aco_opcode::s_lshl_b32:
1221 case aco_opcode::v_or_b32:
1222 case aco_opcode::v_lshlrev_b32:
1223 ctx.info[instr->definitions[0].tempId()].set_bitwise(instr.get());
1224 break;
1225 case aco_opcode::v_min_f32:
1226 case aco_opcode::v_min_f16:
1227 case aco_opcode::v_min_u32:
1228 case aco_opcode::v_min_i32:
1229 case aco_opcode::v_min_u16:
1230 case aco_opcode::v_min_i16:
1231 case aco_opcode::v_max_f32:
1232 case aco_opcode::v_max_f16:
1233 case aco_opcode::v_max_u32:
1234 case aco_opcode::v_max_i32:
1235 case aco_opcode::v_max_u16:
1236 case aco_opcode::v_max_i16:
1237 ctx.info[instr->definitions[0].tempId()].set_minmax(instr.get());
1238 break;
1239 case aco_opcode::v_cmp_lt_f32:
1240 case aco_opcode::v_cmp_eq_f32:
1241 case aco_opcode::v_cmp_le_f32:
1242 case aco_opcode::v_cmp_gt_f32:
1243 case aco_opcode::v_cmp_lg_f32:
1244 case aco_opcode::v_cmp_ge_f32:
1245 case aco_opcode::v_cmp_o_f32:
1246 case aco_opcode::v_cmp_u_f32:
1247 case aco_opcode::v_cmp_nge_f32:
1248 case aco_opcode::v_cmp_nlg_f32:
1249 case aco_opcode::v_cmp_ngt_f32:
1250 case aco_opcode::v_cmp_nle_f32:
1251 case aco_opcode::v_cmp_neq_f32:
1252 case aco_opcode::v_cmp_nlt_f32:
1253 ctx.info[instr->definitions[0].tempId()].set_fcmp(instr.get());
1254 break;
1255 case aco_opcode::s_cselect_b64:
1256 case aco_opcode::s_cselect_b32:
1257 if (instr->operands[0].constantEquals((unsigned) -1) &&
1258 instr->operands[1].constantEquals(0)) {
1259 /* Found a cselect that operates on a uniform bool that comes from eg. s_cmp */
1260 ctx.info[instr->definitions[0].tempId()].set_uniform_bool(instr->operands[2].getTemp());
1261 }
1262 if (instr->operands[2].isTemp() && ctx.info[instr->operands[2].tempId()].is_scc_invert()) {
1263 /* Flip the operands to get rid of the scc_invert instruction */
1264 std::swap(instr->operands[0], instr->operands[1]);
1265 instr->operands[2].setTemp(ctx.info[instr->operands[2].tempId()].temp);
1266 }
1267 break;
1268 case aco_opcode::p_wqm:
1269 if (instr->operands[0].isTemp() &&
1270 ctx.info[instr->operands[0].tempId()].is_scc_invert()) {
1271 ctx.info[instr->definitions[0].tempId()].set_temp(instr->operands[0].getTemp());
1272 }
1273 break;
1274 default:
1275 break;
1276 }
1277 }
1278
1279 ALWAYS_INLINE bool get_cmp_info(aco_opcode op, aco_opcode *ordered, aco_opcode *unordered, aco_opcode *inverse)
1280 {
1281 *ordered = *unordered = op;
1282 switch (op) {
1283 #define CMP(ord, unord) \
1284 case aco_opcode::v_cmp_##ord##_f32:\
1285 case aco_opcode::v_cmp_n##unord##_f32:\
1286 *ordered = aco_opcode::v_cmp_##ord##_f32;\
1287 *unordered = aco_opcode::v_cmp_n##unord##_f32;\
1288 *inverse = op == aco_opcode::v_cmp_n##unord##_f32 ? aco_opcode::v_cmp_##unord##_f32 : aco_opcode::v_cmp_n##ord##_f32;\
1289 return true;
1290 CMP(lt, /*n*/ge)
1291 CMP(eq, /*n*/lg)
1292 CMP(le, /*n*/gt)
1293 CMP(gt, /*n*/le)
1294 CMP(lg, /*n*/eq)
1295 CMP(ge, /*n*/lt)
1296 #undef CMP
1297 default:
1298 return false;
1299 }
1300 }
1301
1302 aco_opcode get_ordered(aco_opcode op)
1303 {
1304 aco_opcode ordered, unordered, inverse;
1305 return get_cmp_info(op, &ordered, &unordered, &inverse) ? ordered : aco_opcode::last_opcode;
1306 }
1307
1308 aco_opcode get_unordered(aco_opcode op)
1309 {
1310 aco_opcode ordered, unordered, inverse;
1311 return get_cmp_info(op, &ordered, &unordered, &inverse) ? unordered : aco_opcode::last_opcode;
1312 }
1313
1314 aco_opcode get_inverse(aco_opcode op)
1315 {
1316 aco_opcode ordered, unordered, inverse;
1317 return get_cmp_info(op, &ordered, &unordered, &inverse) ? inverse : aco_opcode::last_opcode;
1318 }
1319
1320 bool is_cmp(aco_opcode op)
1321 {
1322 aco_opcode ordered, unordered, inverse;
1323 return get_cmp_info(op, &ordered, &unordered, &inverse);
1324 }
1325
1326 unsigned original_temp_id(opt_ctx &ctx, Temp tmp)
1327 {
1328 if (ctx.info[tmp.id()].is_temp())
1329 return ctx.info[tmp.id()].temp.id();
1330 else
1331 return tmp.id();
1332 }
1333
1334 void decrease_uses(opt_ctx &ctx, Instruction* instr)
1335 {
1336 if (!--ctx.uses[instr->definitions[0].tempId()]) {
1337 for (const Operand& op : instr->operands) {
1338 if (op.isTemp())
1339 ctx.uses[op.tempId()]--;
1340 }
1341 }
1342 }
1343
1344 Instruction *follow_operand(opt_ctx &ctx, Operand op, bool ignore_uses=false)
1345 {
1346 if (!op.isTemp() || !(ctx.info[op.tempId()].label & instr_labels))
1347 return nullptr;
1348 if (!ignore_uses && ctx.uses[op.tempId()] > 1)
1349 return nullptr;
1350
1351 Instruction *instr = ctx.info[op.tempId()].instr;
1352
1353 if (instr->definitions.size() == 2) {
1354 assert(instr->definitions[0].isTemp() && instr->definitions[0].tempId() == op.tempId());
1355 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1356 return nullptr;
1357 }
1358
1359 return instr;
1360 }
1361
1362 /* s_or_b64(neq(a, a), neq(b, b)) -> v_cmp_u_f32(a, b)
1363 * s_and_b64(eq(a, a), eq(b, b)) -> v_cmp_o_f32(a, b) */
1364 bool combine_ordering_test(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1365 {
1366 if (instr->definitions[0].regClass() != ctx.program->lane_mask)
1367 return false;
1368 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1369 return false;
1370
1371 bool is_or = instr->opcode == aco_opcode::s_or_b64 || instr->opcode == aco_opcode::s_or_b32;
1372
1373 bool neg[2] = {false, false};
1374 bool abs[2] = {false, false};
1375 uint8_t opsel = 0;
1376 Instruction *op_instr[2];
1377 Temp op[2];
1378
1379 for (unsigned i = 0; i < 2; i++) {
1380 op_instr[i] = follow_operand(ctx, instr->operands[i], true);
1381 if (!op_instr[i])
1382 return false;
1383
1384 aco_opcode expected_cmp = is_or ? aco_opcode::v_cmp_neq_f32 : aco_opcode::v_cmp_eq_f32;
1385
1386 if (op_instr[i]->opcode != expected_cmp)
1387 return false;
1388 if (!op_instr[i]->operands[0].isTemp() || !op_instr[i]->operands[1].isTemp())
1389 return false;
1390
1391 if (op_instr[i]->isVOP3()) {
1392 VOP3A_instruction *vop3 = static_cast<VOP3A_instruction*>(op_instr[i]);
1393 if (vop3->neg[0] != vop3->neg[1] || vop3->abs[0] != vop3->abs[1] || vop3->opsel == 1 || vop3->opsel == 2)
1394 return false;
1395 neg[i] = vop3->neg[0];
1396 abs[i] = vop3->abs[0];
1397 opsel |= (vop3->opsel & 1) << i;
1398 }
1399
1400 Temp op0 = op_instr[i]->operands[0].getTemp();
1401 Temp op1 = op_instr[i]->operands[1].getTemp();
1402 if (original_temp_id(ctx, op0) != original_temp_id(ctx, op1))
1403 return false;
1404
1405 op[i] = op1;
1406 }
1407
1408 if (op[1].type() == RegType::sgpr)
1409 std::swap(op[0], op[1]);
1410 unsigned num_sgprs = (op[0].type() == RegType::sgpr) + (op[1].type() == RegType::sgpr);
1411 if (num_sgprs > (ctx.program->chip_class >= GFX10 ? 2 : 1))
1412 return false;
1413
1414 ctx.uses[op[0].id()]++;
1415 ctx.uses[op[1].id()]++;
1416 decrease_uses(ctx, op_instr[0]);
1417 decrease_uses(ctx, op_instr[1]);
1418
1419 aco_opcode new_op = is_or ? aco_opcode::v_cmp_u_f32 : aco_opcode::v_cmp_o_f32;
1420 Instruction *new_instr;
1421 if (neg[0] || neg[1] || abs[0] || abs[1] || opsel || num_sgprs > 1) {
1422 VOP3A_instruction *vop3 = create_instruction<VOP3A_instruction>(new_op, asVOP3(Format::VOPC), 2, 1);
1423 for (unsigned i = 0; i < 2; i++) {
1424 vop3->neg[i] = neg[i];
1425 vop3->abs[i] = abs[i];
1426 }
1427 vop3->opsel = opsel;
1428 new_instr = static_cast<Instruction *>(vop3);
1429 } else {
1430 new_instr = create_instruction<VOPC_instruction>(new_op, Format::VOPC, 2, 1);
1431 }
1432 new_instr->operands[0] = Operand(op[0]);
1433 new_instr->operands[1] = Operand(op[1]);
1434 new_instr->definitions[0] = instr->definitions[0];
1435
1436 ctx.info[instr->definitions[0].tempId()].label = 0;
1437 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1438
1439 instr.reset(new_instr);
1440
1441 return true;
1442 }
1443
1444 /* s_or_b64(v_cmp_u_f32(a, b), cmp(a, b)) -> get_unordered(cmp)(a, b)
1445 * s_and_b64(v_cmp_o_f32(a, b), cmp(a, b)) -> get_ordered(cmp)(a, b) */
1446 bool combine_comparison_ordering(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1447 {
1448 if (instr->definitions[0].regClass() != ctx.program->lane_mask)
1449 return false;
1450 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1451 return false;
1452
1453 bool is_or = instr->opcode == aco_opcode::s_or_b64 || instr->opcode == aco_opcode::s_or_b32;
1454 aco_opcode expected_nan_test = is_or ? aco_opcode::v_cmp_u_f32 : aco_opcode::v_cmp_o_f32;
1455
1456 Instruction *nan_test = follow_operand(ctx, instr->operands[0], true);
1457 Instruction *cmp = follow_operand(ctx, instr->operands[1], true);
1458 if (!nan_test || !cmp)
1459 return false;
1460
1461 if (cmp->opcode == expected_nan_test)
1462 std::swap(nan_test, cmp);
1463 else if (nan_test->opcode != expected_nan_test)
1464 return false;
1465
1466 if (!is_cmp(cmp->opcode))
1467 return false;
1468
1469 if (!nan_test->operands[0].isTemp() || !nan_test->operands[1].isTemp())
1470 return false;
1471 if (!cmp->operands[0].isTemp() || !cmp->operands[1].isTemp())
1472 return false;
1473
1474 unsigned prop_cmp0 = original_temp_id(ctx, cmp->operands[0].getTemp());
1475 unsigned prop_cmp1 = original_temp_id(ctx, cmp->operands[1].getTemp());
1476 unsigned prop_nan0 = original_temp_id(ctx, nan_test->operands[0].getTemp());
1477 unsigned prop_nan1 = original_temp_id(ctx, nan_test->operands[1].getTemp());
1478 if (prop_cmp0 != prop_nan0 && prop_cmp0 != prop_nan1)
1479 return false;
1480 if (prop_cmp1 != prop_nan0 && prop_cmp1 != prop_nan1)
1481 return false;
1482
1483 ctx.uses[cmp->operands[0].tempId()]++;
1484 ctx.uses[cmp->operands[1].tempId()]++;
1485 decrease_uses(ctx, nan_test);
1486 decrease_uses(ctx, cmp);
1487
1488 aco_opcode new_op = is_or ? get_unordered(cmp->opcode) : get_ordered(cmp->opcode);
1489 Instruction *new_instr;
1490 if (cmp->isVOP3()) {
1491 VOP3A_instruction *new_vop3 = create_instruction<VOP3A_instruction>(new_op, asVOP3(Format::VOPC), 2, 1);
1492 VOP3A_instruction *cmp_vop3 = static_cast<VOP3A_instruction*>(cmp);
1493 memcpy(new_vop3->abs, cmp_vop3->abs, sizeof(new_vop3->abs));
1494 memcpy(new_vop3->neg, cmp_vop3->neg, sizeof(new_vop3->neg));
1495 new_vop3->clamp = cmp_vop3->clamp;
1496 new_vop3->omod = cmp_vop3->omod;
1497 new_vop3->opsel = cmp_vop3->opsel;
1498 new_instr = new_vop3;
1499 } else {
1500 new_instr = create_instruction<VOPC_instruction>(new_op, Format::VOPC, 2, 1);
1501 }
1502 new_instr->operands[0] = cmp->operands[0];
1503 new_instr->operands[1] = cmp->operands[1];
1504 new_instr->definitions[0] = instr->definitions[0];
1505
1506 ctx.info[instr->definitions[0].tempId()].label = 0;
1507 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1508
1509 instr.reset(new_instr);
1510
1511 return true;
1512 }
1513
1514 /* s_or_b64(v_cmp_neq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_unordered(cmp)(a, b)
1515 * s_and_b64(v_cmp_eq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_ordered(cmp)(a, b) */
1516 bool combine_constant_comparison_ordering(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1517 {
1518 if (instr->definitions[0].regClass() != ctx.program->lane_mask)
1519 return false;
1520 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1521 return false;
1522
1523 bool is_or = instr->opcode == aco_opcode::s_or_b64 || instr->opcode == aco_opcode::s_or_b32;
1524
1525 Instruction *nan_test = follow_operand(ctx, instr->operands[0], true);
1526 Instruction *cmp = follow_operand(ctx, instr->operands[1], true);
1527
1528 if (!nan_test || !cmp)
1529 return false;
1530
1531 aco_opcode expected_nan_test = is_or ? aco_opcode::v_cmp_neq_f32 : aco_opcode::v_cmp_eq_f32;
1532 if (cmp->opcode == expected_nan_test)
1533 std::swap(nan_test, cmp);
1534 else if (nan_test->opcode != expected_nan_test)
1535 return false;
1536
1537 if (!is_cmp(cmp->opcode))
1538 return false;
1539
1540 if (!nan_test->operands[0].isTemp() || !nan_test->operands[1].isTemp())
1541 return false;
1542 if (!cmp->operands[0].isTemp() && !cmp->operands[1].isTemp())
1543 return false;
1544
1545 unsigned prop_nan0 = original_temp_id(ctx, nan_test->operands[0].getTemp());
1546 unsigned prop_nan1 = original_temp_id(ctx, nan_test->operands[1].getTemp());
1547 if (prop_nan0 != prop_nan1)
1548 return false;
1549
1550 if (nan_test->isVOP3()) {
1551 VOP3A_instruction *vop3 = static_cast<VOP3A_instruction*>(nan_test);
1552 if (vop3->neg[0] != vop3->neg[1] || vop3->abs[0] != vop3->abs[1] || vop3->opsel == 1 || vop3->opsel == 2)
1553 return false;
1554 }
1555
1556 int constant_operand = -1;
1557 for (unsigned i = 0; i < 2; i++) {
1558 if (cmp->operands[i].isTemp() && original_temp_id(ctx, cmp->operands[i].getTemp()) == prop_nan0) {
1559 constant_operand = !i;
1560 break;
1561 }
1562 }
1563 if (constant_operand == -1)
1564 return false;
1565
1566 uint32_t constant;
1567 if (cmp->operands[constant_operand].isConstant()) {
1568 constant = cmp->operands[constant_operand].constantValue();
1569 } else if (cmp->operands[constant_operand].isTemp()) {
1570 Temp tmp = cmp->operands[constant_operand].getTemp();
1571 unsigned id = original_temp_id(ctx, tmp);
1572 if (!ctx.info[id].is_constant() && !ctx.info[id].is_literal())
1573 return false;
1574 constant = ctx.info[id].val;
1575 } else {
1576 return false;
1577 }
1578
1579 float constantf;
1580 memcpy(&constantf, &constant, 4);
1581 if (isnan(constantf))
1582 return false;
1583
1584 if (cmp->operands[0].isTemp())
1585 ctx.uses[cmp->operands[0].tempId()]++;
1586 if (cmp->operands[1].isTemp())
1587 ctx.uses[cmp->operands[1].tempId()]++;
1588 decrease_uses(ctx, nan_test);
1589 decrease_uses(ctx, cmp);
1590
1591 aco_opcode new_op = is_or ? get_unordered(cmp->opcode) : get_ordered(cmp->opcode);
1592 Instruction *new_instr;
1593 if (cmp->isVOP3()) {
1594 VOP3A_instruction *new_vop3 = create_instruction<VOP3A_instruction>(new_op, asVOP3(Format::VOPC), 2, 1);
1595 VOP3A_instruction *cmp_vop3 = static_cast<VOP3A_instruction*>(cmp);
1596 memcpy(new_vop3->abs, cmp_vop3->abs, sizeof(new_vop3->abs));
1597 memcpy(new_vop3->neg, cmp_vop3->neg, sizeof(new_vop3->neg));
1598 new_vop3->clamp = cmp_vop3->clamp;
1599 new_vop3->omod = cmp_vop3->omod;
1600 new_vop3->opsel = cmp_vop3->opsel;
1601 new_instr = new_vop3;
1602 } else {
1603 new_instr = create_instruction<VOPC_instruction>(new_op, Format::VOPC, 2, 1);
1604 }
1605 new_instr->operands[0] = cmp->operands[0];
1606 new_instr->operands[1] = cmp->operands[1];
1607 new_instr->definitions[0] = instr->definitions[0];
1608
1609 ctx.info[instr->definitions[0].tempId()].label = 0;
1610 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1611
1612 instr.reset(new_instr);
1613
1614 return true;
1615 }
1616
1617 /* s_not_b64(cmp(a, b) -> get_inverse(cmp)(a, b) */
1618 bool combine_inverse_comparison(opt_ctx &ctx, aco_ptr<Instruction>& instr)
1619 {
1620 if (instr->opcode != aco_opcode::s_not_b64)
1621 return false;
1622 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1623 return false;
1624 if (!instr->operands[0].isTemp())
1625 return false;
1626
1627 Instruction *cmp = follow_operand(ctx, instr->operands[0]);
1628 if (!cmp)
1629 return false;
1630
1631 aco_opcode new_opcode = get_inverse(cmp->opcode);
1632 if (new_opcode == aco_opcode::last_opcode)
1633 return false;
1634
1635 if (cmp->operands[0].isTemp())
1636 ctx.uses[cmp->operands[0].tempId()]++;
1637 if (cmp->operands[1].isTemp())
1638 ctx.uses[cmp->operands[1].tempId()]++;
1639 decrease_uses(ctx, cmp);
1640
1641 Instruction *new_instr;
1642 if (cmp->isVOP3()) {
1643 VOP3A_instruction *new_vop3 = create_instruction<VOP3A_instruction>(new_opcode, asVOP3(Format::VOPC), 2, 1);
1644 VOP3A_instruction *cmp_vop3 = static_cast<VOP3A_instruction*>(cmp);
1645 memcpy(new_vop3->abs, cmp_vop3->abs, sizeof(new_vop3->abs));
1646 memcpy(new_vop3->neg, cmp_vop3->neg, sizeof(new_vop3->neg));
1647 new_vop3->clamp = cmp_vop3->clamp;
1648 new_vop3->omod = cmp_vop3->omod;
1649 new_vop3->opsel = cmp_vop3->opsel;
1650 new_instr = new_vop3;
1651 } else {
1652 new_instr = create_instruction<VOPC_instruction>(new_opcode, Format::VOPC, 2, 1);
1653 }
1654 new_instr->operands[0] = cmp->operands[0];
1655 new_instr->operands[1] = cmp->operands[1];
1656 new_instr->definitions[0] = instr->definitions[0];
1657
1658 ctx.info[instr->definitions[0].tempId()].label = 0;
1659 ctx.info[instr->definitions[0].tempId()].set_fcmp(new_instr);
1660
1661 instr.reset(new_instr);
1662
1663 return true;
1664 }
1665
1666 /* op1(op2(1, 2), 0) if swap = false
1667 * op1(0, op2(1, 2)) if swap = true */
1668 bool match_op3_for_vop3(opt_ctx &ctx, aco_opcode op1, aco_opcode op2,
1669 Instruction* op1_instr, bool swap, const char *shuffle_str,
1670 Operand operands[3], bool neg[3], bool abs[3], uint8_t *opsel,
1671 bool *op1_clamp, uint8_t *op1_omod,
1672 bool *inbetween_neg, bool *inbetween_abs, bool *inbetween_opsel)
1673 {
1674 /* checks */
1675 if (op1_instr->opcode != op1)
1676 return false;
1677
1678 Instruction *op2_instr = follow_operand(ctx, op1_instr->operands[swap]);
1679 if (!op2_instr || op2_instr->opcode != op2)
1680 return false;
1681 if (fixed_to_exec(op2_instr->operands[0]) || fixed_to_exec(op2_instr->operands[1]))
1682 return false;
1683
1684 VOP3A_instruction *op1_vop3 = op1_instr->isVOP3() ? static_cast<VOP3A_instruction *>(op1_instr) : NULL;
1685 VOP3A_instruction *op2_vop3 = op2_instr->isVOP3() ? static_cast<VOP3A_instruction *>(op2_instr) : NULL;
1686
1687 /* don't support inbetween clamp/omod */
1688 if (op2_vop3 && (op2_vop3->clamp || op2_vop3->omod))
1689 return false;
1690
1691 /* get operands and modifiers and check inbetween modifiers */
1692 *op1_clamp = op1_vop3 ? op1_vop3->clamp : false;
1693 *op1_omod = op1_vop3 ? op1_vop3->omod : 0u;
1694
1695 if (inbetween_neg)
1696 *inbetween_neg = op1_vop3 ? op1_vop3->neg[swap] : false;
1697 else if (op1_vop3 && op1_vop3->neg[swap])
1698 return false;
1699
1700 if (inbetween_abs)
1701 *inbetween_abs = op1_vop3 ? op1_vop3->abs[swap] : false;
1702 else if (op1_vop3 && op1_vop3->abs[swap])
1703 return false;
1704
1705 if (inbetween_opsel)
1706 *inbetween_opsel = op1_vop3 ? op1_vop3->opsel & (1 << swap) : false;
1707 else if (op1_vop3 && op1_vop3->opsel & (1 << swap))
1708 return false;
1709
1710 int shuffle[3];
1711 shuffle[shuffle_str[0] - '0'] = 0;
1712 shuffle[shuffle_str[1] - '0'] = 1;
1713 shuffle[shuffle_str[2] - '0'] = 2;
1714
1715 operands[shuffle[0]] = op1_instr->operands[!swap];
1716 neg[shuffle[0]] = op1_vop3 ? op1_vop3->neg[!swap] : false;
1717 abs[shuffle[0]] = op1_vop3 ? op1_vop3->abs[!swap] : false;
1718 if (op1_vop3 && op1_vop3->opsel & (1 << !swap))
1719 *opsel |= 1 << shuffle[0];
1720
1721 for (unsigned i = 0; i < 2; i++) {
1722 operands[shuffle[i + 1]] = op2_instr->operands[i];
1723 neg[shuffle[i + 1]] = op2_vop3 ? op2_vop3->neg[i] : false;
1724 abs[shuffle[i + 1]] = op2_vop3 ? op2_vop3->abs[i] : false;
1725 if (op2_vop3 && op2_vop3->opsel & (1 << i))
1726 *opsel |= 1 << shuffle[i + 1];
1727 }
1728
1729 /* check operands */
1730 if (!check_vop3_operands(ctx, 3, operands))
1731 return false;
1732
1733 return true;
1734 }
1735
1736 void create_vop3_for_op3(opt_ctx& ctx, aco_opcode opcode, aco_ptr<Instruction>& instr,
1737 Operand operands[3], bool neg[3], bool abs[3], uint8_t opsel,
1738 bool clamp, unsigned omod)
1739 {
1740 VOP3A_instruction *new_instr = create_instruction<VOP3A_instruction>(opcode, Format::VOP3A, 3, 1);
1741 memcpy(new_instr->abs, abs, sizeof(bool[3]));
1742 memcpy(new_instr->neg, neg, sizeof(bool[3]));
1743 new_instr->clamp = clamp;
1744 new_instr->omod = omod;
1745 new_instr->opsel = opsel;
1746 new_instr->operands[0] = operands[0];
1747 new_instr->operands[1] = operands[1];
1748 new_instr->operands[2] = operands[2];
1749 new_instr->definitions[0] = instr->definitions[0];
1750 ctx.info[instr->definitions[0].tempId()].label = 0;
1751
1752 instr.reset(new_instr);
1753 }
1754
1755 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)
1756 {
1757 uint32_t omod_clamp = ctx.info[instr->definitions[0].tempId()].label &
1758 (label_omod_success | label_clamp_success);
1759
1760 for (unsigned swap = 0; swap < 2; swap++) {
1761 if (!((1 << swap) & ops))
1762 continue;
1763
1764 Operand operands[3];
1765 bool neg[3], abs[3], clamp;
1766 uint8_t opsel = 0, omod = 0;
1767 if (match_op3_for_vop3(ctx, instr->opcode, op2,
1768 instr.get(), swap, shuffle,
1769 operands, neg, abs, &opsel,
1770 &clamp, &omod, NULL, NULL, NULL)) {
1771 ctx.uses[instr->operands[swap].tempId()]--;
1772 create_vop3_for_op3(ctx, new_op, instr, operands, neg, abs, opsel, clamp, omod);
1773 if (omod_clamp & label_omod_success)
1774 ctx.info[instr->definitions[0].tempId()].set_omod_success(instr.get());
1775 if (omod_clamp & label_clamp_success)
1776 ctx.info[instr->definitions[0].tempId()].set_clamp_success(instr.get());
1777 return true;
1778 }
1779 }
1780 return false;
1781 }
1782
1783 bool combine_minmax(opt_ctx& ctx, aco_ptr<Instruction>& instr, aco_opcode opposite, aco_opcode minmax3)
1784 {
1785 if (combine_three_valu_op(ctx, instr, instr->opcode, minmax3, "012", 1 | 2))
1786 return true;
1787
1788 uint32_t omod_clamp = ctx.info[instr->definitions[0].tempId()].label &
1789 (label_omod_success | label_clamp_success);
1790
1791 /* min(-max(a, b), c) -> min3(-a, -b, c) *
1792 * max(-min(a, b), c) -> max3(-a, -b, c) */
1793 for (unsigned swap = 0; swap < 2; swap++) {
1794 Operand operands[3];
1795 bool neg[3], abs[3], clamp;
1796 uint8_t opsel = 0, omod = 0;
1797 bool inbetween_neg;
1798 if (match_op3_for_vop3(ctx, instr->opcode, opposite,
1799 instr.get(), swap, "012",
1800 operands, neg, abs, &opsel,
1801 &clamp, &omod, &inbetween_neg, NULL, NULL) &&
1802 inbetween_neg) {
1803 ctx.uses[instr->operands[swap].tempId()]--;
1804 neg[1] = true;
1805 neg[2] = true;
1806 create_vop3_for_op3(ctx, minmax3, instr, operands, neg, abs, opsel, clamp, omod);
1807 if (omod_clamp & label_omod_success)
1808 ctx.info[instr->definitions[0].tempId()].set_omod_success(instr.get());
1809 if (omod_clamp & label_clamp_success)
1810 ctx.info[instr->definitions[0].tempId()].set_clamp_success(instr.get());
1811 return true;
1812 }
1813 }
1814 return false;
1815 }
1816
1817 /* s_not_b32(s_and_b32(a, b)) -> s_nand_b32(a, b)
1818 * s_not_b32(s_or_b32(a, b)) -> s_nor_b32(a, b)
1819 * s_not_b32(s_xor_b32(a, b)) -> s_xnor_b32(a, b)
1820 * s_not_b64(s_and_b64(a, b)) -> s_nand_b64(a, b)
1821 * s_not_b64(s_or_b64(a, b)) -> s_nor_b64(a, b)
1822 * s_not_b64(s_xor_b64(a, b)) -> s_xnor_b64(a, b) */
1823 bool combine_salu_not_bitwise(opt_ctx& ctx, aco_ptr<Instruction>& instr)
1824 {
1825 /* checks */
1826 if (!instr->operands[0].isTemp())
1827 return false;
1828 if (instr->definitions[1].isTemp() && ctx.uses[instr->definitions[1].tempId()])
1829 return false;
1830
1831 Instruction *op2_instr = follow_operand(ctx, instr->operands[0]);
1832 if (!op2_instr)
1833 return false;
1834 switch (op2_instr->opcode) {
1835 case aco_opcode::s_and_b32:
1836 case aco_opcode::s_or_b32:
1837 case aco_opcode::s_xor_b32:
1838 case aco_opcode::s_and_b64:
1839 case aco_opcode::s_or_b64:
1840 case aco_opcode::s_xor_b64:
1841 break;
1842 default:
1843 return false;
1844 }
1845
1846 /* create instruction */
1847 std::swap(instr->definitions[0], op2_instr->definitions[0]);
1848 std::swap(instr->definitions[1], op2_instr->definitions[1]);
1849 ctx.uses[instr->operands[0].tempId()]--;
1850 ctx.info[op2_instr->definitions[0].tempId()].label = 0;
1851
1852 switch (op2_instr->opcode) {
1853 case aco_opcode::s_and_b32:
1854 op2_instr->opcode = aco_opcode::s_nand_b32;
1855 break;
1856 case aco_opcode::s_or_b32:
1857 op2_instr->opcode = aco_opcode::s_nor_b32;
1858 break;
1859 case aco_opcode::s_xor_b32:
1860 op2_instr->opcode = aco_opcode::s_xnor_b32;
1861 break;
1862 case aco_opcode::s_and_b64:
1863 op2_instr->opcode = aco_opcode::s_nand_b64;
1864 break;
1865 case aco_opcode::s_or_b64:
1866 op2_instr->opcode = aco_opcode::s_nor_b64;
1867 break;
1868 case aco_opcode::s_xor_b64:
1869 op2_instr->opcode = aco_opcode::s_xnor_b64;
1870 break;
1871 default:
1872 break;
1873 }
1874
1875 return true;
1876 }
1877
1878 /* s_and_b32(a, s_not_b32(b)) -> s_andn2_b32(a, b)
1879 * s_or_b32(a, s_not_b32(b)) -> s_orn2_b32(a, b)
1880 * s_and_b64(a, s_not_b64(b)) -> s_andn2_b64(a, b)
1881 * s_or_b64(a, s_not_b64(b)) -> s_orn2_b64(a, b) */
1882 bool combine_salu_n2(opt_ctx& ctx, aco_ptr<Instruction>& instr)
1883 {
1884 if (instr->definitions[0].isTemp() && ctx.info[instr->definitions[0].tempId()].is_uniform_bool())
1885 return false;
1886
1887 for (unsigned i = 0; i < 2; i++) {
1888 Instruction *op2_instr = follow_operand(ctx, instr->operands[i]);
1889 if (!op2_instr || (op2_instr->opcode != aco_opcode::s_not_b32 && op2_instr->opcode != aco_opcode::s_not_b64))
1890 continue;
1891 if (ctx.uses[op2_instr->definitions[1].tempId()] || fixed_to_exec(op2_instr->operands[0]))
1892 continue;
1893
1894 if (instr->operands[!i].isLiteral() && op2_instr->operands[0].isLiteral() &&
1895 instr->operands[!i].constantValue() != op2_instr->operands[0].constantValue())
1896 continue;
1897
1898 ctx.uses[instr->operands[i].tempId()]--;
1899 instr->operands[0] = instr->operands[!i];
1900 instr->operands[1] = op2_instr->operands[0];
1901 ctx.info[instr->definitions[0].tempId()].label = 0;
1902
1903 switch (instr->opcode) {
1904 case aco_opcode::s_and_b32:
1905 instr->opcode = aco_opcode::s_andn2_b32;
1906 break;
1907 case aco_opcode::s_or_b32:
1908 instr->opcode = aco_opcode::s_orn2_b32;
1909 break;
1910 case aco_opcode::s_and_b64:
1911 instr->opcode = aco_opcode::s_andn2_b64;
1912 break;
1913 case aco_opcode::s_or_b64:
1914 instr->opcode = aco_opcode::s_orn2_b64;
1915 break;
1916 default:
1917 break;
1918 }
1919
1920 return true;
1921 }
1922 return false;
1923 }
1924
1925 /* s_add_{i32,u32}(a, s_lshl_b32(b, <n>)) -> s_lshl<n>_add_u32(a, b) */
1926 bool combine_salu_lshl_add(opt_ctx& ctx, aco_ptr<Instruction>& instr)
1927 {
1928 if (instr->opcode == aco_opcode::s_add_i32 && ctx.uses[instr->definitions[1].tempId()])
1929 return false;
1930
1931 for (unsigned i = 0; i < 2; i++) {
1932 Instruction *op2_instr = follow_operand(ctx, instr->operands[i]);
1933 if (!op2_instr || op2_instr->opcode != aco_opcode::s_lshl_b32 ||
1934 ctx.uses[op2_instr->definitions[1].tempId()])
1935 continue;
1936 if (!op2_instr->operands[1].isConstant() || fixed_to_exec(op2_instr->operands[0]))
1937 continue;
1938
1939 uint32_t shift = op2_instr->operands[1].constantValue();
1940 if (shift < 1 || shift > 4)
1941 continue;
1942
1943 if (instr->operands[!i].isLiteral() && op2_instr->operands[0].isLiteral() &&
1944 instr->operands[!i].constantValue() != op2_instr->operands[0].constantValue())
1945 continue;
1946
1947 ctx.uses[instr->operands[i].tempId()]--;
1948 instr->operands[1] = instr->operands[!i];
1949 instr->operands[0] = op2_instr->operands[0];
1950 ctx.info[instr->definitions[0].tempId()].label = 0;
1951
1952 instr->opcode = ((aco_opcode[]){aco_opcode::s_lshl1_add_u32,
1953 aco_opcode::s_lshl2_add_u32,
1954 aco_opcode::s_lshl3_add_u32,
1955 aco_opcode::s_lshl4_add_u32})[shift - 1];
1956
1957 return true;
1958 }
1959 return false;
1960 }
1961
1962 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)
1963 {
1964 switch (op) {
1965 #define MINMAX(type, gfx9) \
1966 case aco_opcode::v_min_##type:\
1967 case aco_opcode::v_max_##type:\
1968 case aco_opcode::v_med3_##type:\
1969 *min = aco_opcode::v_min_##type;\
1970 *max = aco_opcode::v_max_##type;\
1971 *med3 = aco_opcode::v_med3_##type;\
1972 *min3 = aco_opcode::v_min3_##type;\
1973 *max3 = aco_opcode::v_max3_##type;\
1974 *some_gfx9_only = gfx9;\
1975 return true;
1976 MINMAX(f32, false)
1977 MINMAX(u32, false)
1978 MINMAX(i32, false)
1979 MINMAX(f16, true)
1980 MINMAX(u16, true)
1981 MINMAX(i16, true)
1982 #undef MINMAX
1983 default:
1984 return false;
1985 }
1986 }
1987
1988 /* 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
1989 * 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 */
1990 bool combine_clamp(opt_ctx& ctx, aco_ptr<Instruction>& instr,
1991 aco_opcode min, aco_opcode max, aco_opcode med)
1992 {
1993 /* TODO: GLSL's clamp(x, minVal, maxVal) and SPIR-V's
1994 * FClamp(x, minVal, maxVal)/NClamp(x, minVal, maxVal) are undefined if
1995 * minVal > maxVal, which means we can always select it to a v_med3_f32 */
1996 aco_opcode other_op;
1997 if (instr->opcode == min)
1998 other_op = max;
1999 else if (instr->opcode == max)
2000 other_op = min;
2001 else
2002 return false;
2003
2004 uint32_t omod_clamp = ctx.info[instr->definitions[0].tempId()].label &
2005 (label_omod_success | label_clamp_success);
2006
2007 for (unsigned swap = 0; swap < 2; swap++) {
2008 Operand operands[3];
2009 bool neg[3], abs[3], clamp;
2010 uint8_t opsel = 0, omod = 0;
2011 if (match_op3_for_vop3(ctx, instr->opcode, other_op, instr.get(), swap,
2012 "012", operands, neg, abs, &opsel,
2013 &clamp, &omod, NULL, NULL, NULL)) {
2014 int const0_idx = -1, const1_idx = -1;
2015 uint32_t const0 = 0, const1 = 0;
2016 for (int i = 0; i < 3; i++) {
2017 uint32_t val;
2018 if (operands[i].isConstant()) {
2019 val = operands[i].constantValue();
2020 } else if (operands[i].isTemp() && ctx.info[operands[i].tempId()].is_constant_or_literal()) {
2021 val = ctx.info[operands[i].tempId()].val;
2022 } else {
2023 continue;
2024 }
2025 if (const0_idx >= 0) {
2026 const1_idx = i;
2027 const1 = val;
2028 } else {
2029 const0_idx = i;
2030 const0 = val;
2031 }
2032 }
2033 if (const0_idx < 0 || const1_idx < 0)
2034 continue;
2035
2036 if (opsel & (1 << const0_idx))
2037 const0 >>= 16;
2038 if (opsel & (1 << const1_idx))
2039 const1 >>= 16;
2040
2041 int lower_idx = const0_idx;
2042 switch (min) {
2043 case aco_opcode::v_min_f32:
2044 case aco_opcode::v_min_f16: {
2045 float const0_f, const1_f;
2046 if (min == aco_opcode::v_min_f32) {
2047 memcpy(&const0_f, &const0, 4);
2048 memcpy(&const1_f, &const1, 4);
2049 } else {
2050 const0_f = _mesa_half_to_float(const0);
2051 const1_f = _mesa_half_to_float(const1);
2052 }
2053 if (abs[const0_idx]) const0_f = fabsf(const0_f);
2054 if (abs[const1_idx]) const1_f = fabsf(const1_f);
2055 if (neg[const0_idx]) const0_f = -const0_f;
2056 if (neg[const1_idx]) const1_f = -const1_f;
2057 lower_idx = const0_f < const1_f ? const0_idx : const1_idx;
2058 break;
2059 }
2060 case aco_opcode::v_min_u32: {
2061 lower_idx = const0 < const1 ? const0_idx : const1_idx;
2062 break;
2063 }
2064 case aco_opcode::v_min_u16: {
2065 lower_idx = (uint16_t)const0 < (uint16_t)const1 ? const0_idx : const1_idx;
2066 break;
2067 }
2068 case aco_opcode::v_min_i32: {
2069 int32_t const0_i = const0 & 0x80000000u ? -2147483648 + (int32_t)(const0 & 0x7fffffffu) : const0;
2070 int32_t const1_i = const1 & 0x80000000u ? -2147483648 + (int32_t)(const1 & 0x7fffffffu) : const1;
2071 lower_idx = const0_i < const1_i ? const0_idx : const1_idx;
2072 break;
2073 }
2074 case aco_opcode::v_min_i16: {
2075 int16_t const0_i = const0 & 0x8000u ? -32768 + (int16_t)(const0 & 0x7fffu) : const0;
2076 int16_t const1_i = const1 & 0x8000u ? -32768 + (int16_t)(const1 & 0x7fffu) : const1;
2077 lower_idx = const0_i < const1_i ? const0_idx : const1_idx;
2078 break;
2079 }
2080 default:
2081 break;
2082 }
2083 int upper_idx = lower_idx == const0_idx ? const1_idx : const0_idx;
2084
2085 if (instr->opcode == min) {
2086 if (upper_idx != 0 || lower_idx == 0)
2087 return false;
2088 } else {
2089 if (upper_idx == 0 || lower_idx != 0)
2090 return false;
2091 }
2092
2093 ctx.uses[instr->operands[swap].tempId()]--;
2094 create_vop3_for_op3(ctx, med, instr, operands, neg, abs, opsel, clamp, omod);
2095 if (omod_clamp & label_omod_success)
2096 ctx.info[instr->definitions[0].tempId()].set_omod_success(instr.get());
2097 if (omod_clamp & label_clamp_success)
2098 ctx.info[instr->definitions[0].tempId()].set_clamp_success(instr.get());
2099
2100 return true;
2101 }
2102 }
2103
2104 return false;
2105 }
2106
2107
2108 void apply_sgprs(opt_ctx &ctx, aco_ptr<Instruction>& instr)
2109 {
2110 bool is_shift64 = instr->opcode == aco_opcode::v_lshlrev_b64 ||
2111 instr->opcode == aco_opcode::v_lshrrev_b64 ||
2112 instr->opcode == aco_opcode::v_ashrrev_i64;
2113
2114 /* find candidates and create the set of sgprs already read */
2115 unsigned sgpr_ids[2] = {0, 0};
2116 uint32_t operand_mask = 0;
2117 bool has_literal = false;
2118 for (unsigned i = 0; i < instr->operands.size(); i++) {
2119 if (instr->operands[i].isLiteral())
2120 has_literal = true;
2121 if (!instr->operands[i].isTemp())
2122 continue;
2123 if (instr->operands[i].getTemp().type() == RegType::sgpr) {
2124 if (instr->operands[i].tempId() != sgpr_ids[0])
2125 sgpr_ids[!!sgpr_ids[0]] = instr->operands[i].tempId();
2126 }
2127 ssa_info& info = ctx.info[instr->operands[i].tempId()];
2128 if (info.is_temp() && info.temp.type() == RegType::sgpr)
2129 operand_mask |= 1u << i;
2130 }
2131 unsigned max_sgprs = 1;
2132 if (ctx.program->chip_class >= GFX10 && !is_shift64)
2133 max_sgprs = 2;
2134 if (has_literal)
2135 max_sgprs--;
2136
2137 unsigned num_sgprs = !!sgpr_ids[0] + !!sgpr_ids[1];
2138
2139 /* keep on applying sgprs until there is nothing left to be done */
2140 while (operand_mask) {
2141 uint32_t sgpr_idx = 0;
2142 uint32_t sgpr_info_id = 0;
2143 uint32_t mask = operand_mask;
2144 /* choose a sgpr */
2145 while (mask) {
2146 unsigned i = u_bit_scan(&mask);
2147 uint16_t uses = ctx.uses[instr->operands[i].tempId()];
2148 if (sgpr_info_id == 0 || uses < ctx.uses[sgpr_info_id]) {
2149 sgpr_idx = i;
2150 sgpr_info_id = instr->operands[i].tempId();
2151 }
2152 }
2153 operand_mask &= ~(1u << sgpr_idx);
2154
2155 /* Applying two sgprs require making it VOP3, so don't do it unless it's
2156 * definitively beneficial.
2157 * TODO: this is too conservative because later the use count could be reduced to 1 */
2158 if (num_sgprs && ctx.uses[sgpr_info_id] > 1 && !instr->isVOP3())
2159 break;
2160
2161 Temp sgpr = ctx.info[sgpr_info_id].temp;
2162 bool new_sgpr = sgpr.id() != sgpr_ids[0] && sgpr.id() != sgpr_ids[1];
2163 if (new_sgpr && num_sgprs >= max_sgprs)
2164 continue;
2165
2166 if (sgpr_idx == 0 || instr->isVOP3()) {
2167 instr->operands[sgpr_idx] = Operand(sgpr);
2168 } else if (can_swap_operands(instr)) {
2169 instr->operands[sgpr_idx] = instr->operands[0];
2170 instr->operands[0] = Operand(sgpr);
2171 /* swap bits using a 4-entry LUT */
2172 uint32_t swapped = (0x3120 >> (operand_mask & 0x3)) & 0xf;
2173 operand_mask = (operand_mask & ~0x3) | swapped;
2174 } else if (can_use_VOP3(ctx, instr)) {
2175 to_VOP3(ctx, instr);
2176 instr->operands[sgpr_idx] = Operand(sgpr);
2177 } else {
2178 continue;
2179 }
2180
2181 if (new_sgpr)
2182 sgpr_ids[num_sgprs++] = sgpr.id();
2183 ctx.uses[sgpr_info_id]--;
2184 ctx.uses[sgpr.id()]++;
2185 }
2186 }
2187
2188 bool apply_omod_clamp(opt_ctx &ctx, Block& block, aco_ptr<Instruction>& instr)
2189 {
2190 /* check if we could apply omod on predecessor */
2191 if (instr->opcode == aco_opcode::v_mul_f32) {
2192 bool op0 = instr->operands[0].isTemp() && ctx.info[instr->operands[0].tempId()].is_omod_success();
2193 bool op1 = instr->operands[1].isTemp() && ctx.info[instr->operands[1].tempId()].is_omod_success();
2194 if (op0 || op1) {
2195 unsigned idx = op0 ? 0 : 1;
2196 /* omod was successfully applied */
2197 /* if the omod instruction is v_mad, we also have to change the original add */
2198 if (ctx.info[instr->operands[idx].tempId()].is_mad()) {
2199 Instruction* add_instr = ctx.mad_infos[ctx.info[instr->operands[idx].tempId()].val].add_instr.get();
2200 if (ctx.info[instr->definitions[0].tempId()].is_clamp())
2201 static_cast<VOP3A_instruction*>(add_instr)->clamp = true;
2202 add_instr->definitions[0] = instr->definitions[0];
2203 }
2204
2205 Instruction* omod_instr = ctx.info[instr->operands[idx].tempId()].instr;
2206 /* check if we have an additional clamp modifier */
2207 if (ctx.info[instr->definitions[0].tempId()].is_clamp() && ctx.uses[instr->definitions[0].tempId()] == 1 &&
2208 ctx.uses[ctx.info[instr->definitions[0].tempId()].temp.id()]) {
2209 static_cast<VOP3A_instruction*>(omod_instr)->clamp = true;
2210 ctx.info[instr->definitions[0].tempId()].set_clamp_success(omod_instr);
2211 }
2212 /* change definition ssa-id of modified instruction */
2213 omod_instr->definitions[0] = instr->definitions[0];
2214
2215 /* change the definition of instr to something unused, e.g. the original omod def */
2216 instr->definitions[0] = Definition(instr->operands[idx].getTemp());
2217 ctx.uses[instr->definitions[0].tempId()] = 0;
2218 return true;
2219 }
2220 if (!ctx.info[instr->definitions[0].tempId()].label) {
2221 /* in all other cases, label this instruction as option for multiply-add */
2222 ctx.info[instr->definitions[0].tempId()].set_mul(instr.get());
2223 }
2224 }
2225
2226 /* check if we could apply clamp on predecessor */
2227 if (instr->opcode == aco_opcode::v_med3_f32) {
2228 unsigned idx = 0;
2229 bool found_zero = false, found_one = false;
2230 for (unsigned i = 0; i < 3; i++)
2231 {
2232 if (instr->operands[i].constantEquals(0))
2233 found_zero = true;
2234 else if (instr->operands[i].constantEquals(0x3f800000)) /* 1.0 */
2235 found_one = true;
2236 else
2237 idx = i;
2238 }
2239 if (found_zero && found_one && instr->operands[idx].isTemp() &&
2240 ctx.info[instr->operands[idx].tempId()].is_clamp_success()) {
2241 /* clamp was successfully applied */
2242 /* if the clamp instruction is v_mad, we also have to change the original add */
2243 if (ctx.info[instr->operands[idx].tempId()].is_mad()) {
2244 Instruction* add_instr = ctx.mad_infos[ctx.info[instr->operands[idx].tempId()].val].add_instr.get();
2245 add_instr->definitions[0] = instr->definitions[0];
2246 }
2247 Instruction* clamp_instr = ctx.info[instr->operands[idx].tempId()].instr;
2248 /* change definition ssa-id of modified instruction */
2249 clamp_instr->definitions[0] = instr->definitions[0];
2250
2251 /* change the definition of instr to something unused, e.g. the original omod def */
2252 instr->definitions[0] = Definition(instr->operands[idx].getTemp());
2253 ctx.uses[instr->definitions[0].tempId()] = 0;
2254 return true;
2255 }
2256 }
2257
2258 /* omod has no effect if denormals are enabled */
2259 bool can_use_omod = block.fp_mode.denorm32 == 0;
2260
2261 /* apply omod / clamp modifiers if the def is used only once and the instruction can have modifiers */
2262 if (!instr->definitions.empty() && ctx.uses[instr->definitions[0].tempId()] == 1 &&
2263 can_use_VOP3(ctx, instr) && instr_info.can_use_output_modifiers[(int)instr->opcode]) {
2264 ssa_info& def_info = ctx.info[instr->definitions[0].tempId()];
2265 if (can_use_omod && def_info.is_omod2() && ctx.uses[def_info.temp.id()]) {
2266 to_VOP3(ctx, instr);
2267 static_cast<VOP3A_instruction*>(instr.get())->omod = 1;
2268 def_info.set_omod_success(instr.get());
2269 } else if (can_use_omod && def_info.is_omod4() && ctx.uses[def_info.temp.id()]) {
2270 to_VOP3(ctx, instr);
2271 static_cast<VOP3A_instruction*>(instr.get())->omod = 2;
2272 def_info.set_omod_success(instr.get());
2273 } else if (can_use_omod && def_info.is_omod5() && ctx.uses[def_info.temp.id()]) {
2274 to_VOP3(ctx, instr);
2275 static_cast<VOP3A_instruction*>(instr.get())->omod = 3;
2276 def_info.set_omod_success(instr.get());
2277 } else if (def_info.is_clamp() && ctx.uses[def_info.temp.id()]) {
2278 to_VOP3(ctx, instr);
2279 static_cast<VOP3A_instruction*>(instr.get())->clamp = true;
2280 def_info.set_clamp_success(instr.get());
2281 }
2282 }
2283
2284 return false;
2285 }
2286
2287 // TODO: we could possibly move the whole label_instruction pass to combine_instruction:
2288 // this would mean that we'd have to fix the instruction uses while value propagation
2289
2290 void combine_instruction(opt_ctx &ctx, Block& block, aco_ptr<Instruction>& instr)
2291 {
2292 if (instr->definitions.empty() || is_dead(ctx.uses, instr.get()))
2293 return;
2294
2295 if (instr->isVALU()) {
2296 if (can_apply_sgprs(instr))
2297 apply_sgprs(ctx, instr);
2298 if (apply_omod_clamp(ctx, block, instr))
2299 return;
2300 }
2301
2302 if (ctx.info[instr->definitions[0].tempId()].is_vcc_hint()) {
2303 instr->definitions[0].setHint(vcc);
2304 }
2305
2306 /* TODO: There are still some peephole optimizations that could be done:
2307 * - abs(a - b) -> s_absdiff_i32
2308 * - various patterns for s_bitcmp{0,1}_b32 and s_bitset{0,1}_b32
2309 * - patterns for v_alignbit_b32 and v_alignbyte_b32
2310 * These aren't probably too interesting though.
2311 * There are also patterns for v_cmp_class_f{16,32,64}. This is difficult but
2312 * probably more useful than the previously mentioned optimizations.
2313 * The various comparison optimizations also currently only work with 32-bit
2314 * floats. */
2315
2316 /* neg(mul(a, b)) -> mul(neg(a), b) */
2317 if (ctx.info[instr->definitions[0].tempId()].is_neg() && ctx.uses[instr->operands[1].tempId()] == 1) {
2318 Temp val = ctx.info[instr->definitions[0].tempId()].temp;
2319
2320 if (!ctx.info[val.id()].is_mul())
2321 return;
2322
2323 Instruction* mul_instr = ctx.info[val.id()].instr;
2324
2325 if (mul_instr->operands[0].isLiteral())
2326 return;
2327 if (mul_instr->isVOP3() && static_cast<VOP3A_instruction*>(mul_instr)->clamp)
2328 return;
2329
2330 /* convert to mul(neg(a), b) */
2331 ctx.uses[mul_instr->definitions[0].tempId()]--;
2332 Definition def = instr->definitions[0];
2333 /* neg(abs(mul(a, b))) -> mul(neg(abs(a)), abs(b)) */
2334 bool is_abs = ctx.info[instr->definitions[0].tempId()].is_abs();
2335 instr.reset(create_instruction<VOP3A_instruction>(aco_opcode::v_mul_f32, asVOP3(Format::VOP2), 2, 1));
2336 instr->operands[0] = mul_instr->operands[0];
2337 instr->operands[1] = mul_instr->operands[1];
2338 instr->definitions[0] = def;
2339 VOP3A_instruction* new_mul = static_cast<VOP3A_instruction*>(instr.get());
2340 if (mul_instr->isVOP3()) {
2341 VOP3A_instruction* mul = static_cast<VOP3A_instruction*>(mul_instr);
2342 new_mul->neg[0] = mul->neg[0] && !is_abs;
2343 new_mul->neg[1] = mul->neg[1] && !is_abs;
2344 new_mul->abs[0] = mul->abs[0] || is_abs;
2345 new_mul->abs[1] = mul->abs[1] || is_abs;
2346 new_mul->omod = mul->omod;
2347 }
2348 new_mul->neg[0] ^= true;
2349 new_mul->clamp = false;
2350
2351 ctx.info[instr->definitions[0].tempId()].set_mul(instr.get());
2352 return;
2353 }
2354 /* combine mul+add -> mad */
2355 else if ((instr->opcode == aco_opcode::v_add_f32 ||
2356 instr->opcode == aco_opcode::v_sub_f32 ||
2357 instr->opcode == aco_opcode::v_subrev_f32) &&
2358 block.fp_mode.denorm32 == 0 && !block.fp_mode.preserve_signed_zero_inf_nan32) {
2359 //TODO: we could use fma instead when denormals are enabled if the NIR isn't marked as precise
2360
2361 uint32_t uses_src0 = UINT32_MAX;
2362 uint32_t uses_src1 = UINT32_MAX;
2363 Instruction* mul_instr = nullptr;
2364 unsigned add_op_idx;
2365 /* check if any of the operands is a multiplication */
2366 if (instr->operands[0].isTemp() && ctx.info[instr->operands[0].tempId()].is_mul())
2367 uses_src0 = ctx.uses[instr->operands[0].tempId()];
2368 if (instr->operands[1].isTemp() && ctx.info[instr->operands[1].tempId()].is_mul())
2369 uses_src1 = ctx.uses[instr->operands[1].tempId()];
2370
2371 /* find the 'best' mul instruction to combine with the add */
2372 if (uses_src0 < uses_src1) {
2373 mul_instr = ctx.info[instr->operands[0].tempId()].instr;
2374 add_op_idx = 1;
2375 } else if (uses_src1 < uses_src0) {
2376 mul_instr = ctx.info[instr->operands[1].tempId()].instr;
2377 add_op_idx = 0;
2378 } else if (uses_src0 != UINT32_MAX) {
2379 /* tiebreaker: quite random what to pick */
2380 if (ctx.info[instr->operands[0].tempId()].instr->operands[0].isLiteral()) {
2381 mul_instr = ctx.info[instr->operands[1].tempId()].instr;
2382 add_op_idx = 0;
2383 } else {
2384 mul_instr = ctx.info[instr->operands[0].tempId()].instr;
2385 add_op_idx = 1;
2386 }
2387 }
2388 if (mul_instr) {
2389 Operand op[3] = {Operand(v1), Operand(v1), Operand(v1)};
2390 bool neg[3] = {false, false, false};
2391 bool abs[3] = {false, false, false};
2392 unsigned omod = 0;
2393 bool clamp = false;
2394 op[0] = mul_instr->operands[0];
2395 op[1] = mul_instr->operands[1];
2396 op[2] = instr->operands[add_op_idx];
2397 // TODO: would be better to check this before selecting a mul instr?
2398 if (!check_vop3_operands(ctx, 3, op))
2399 return;
2400
2401 if (mul_instr->isVOP3()) {
2402 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*> (mul_instr);
2403 neg[0] = vop3->neg[0];
2404 neg[1] = vop3->neg[1];
2405 abs[0] = vop3->abs[0];
2406 abs[1] = vop3->abs[1];
2407 /* we cannot use these modifiers between mul and add */
2408 if (vop3->clamp || vop3->omod)
2409 return;
2410 }
2411
2412 /* convert to mad */
2413 ctx.uses[mul_instr->definitions[0].tempId()]--;
2414 if (ctx.uses[mul_instr->definitions[0].tempId()]) {
2415 if (op[0].isTemp())
2416 ctx.uses[op[0].tempId()]++;
2417 if (op[1].isTemp())
2418 ctx.uses[op[1].tempId()]++;
2419 }
2420
2421 if (instr->isVOP3()) {
2422 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*> (instr.get());
2423 neg[2] = vop3->neg[add_op_idx];
2424 abs[2] = vop3->abs[add_op_idx];
2425 omod = vop3->omod;
2426 clamp = vop3->clamp;
2427 /* abs of the multiplication result */
2428 if (vop3->abs[1 - add_op_idx]) {
2429 neg[0] = false;
2430 neg[1] = false;
2431 abs[0] = true;
2432 abs[1] = true;
2433 }
2434 /* neg of the multiplication result */
2435 neg[1] = neg[1] ^ vop3->neg[1 - add_op_idx];
2436 }
2437 if (instr->opcode == aco_opcode::v_sub_f32)
2438 neg[1 + add_op_idx] = neg[1 + add_op_idx] ^ true;
2439 else if (instr->opcode == aco_opcode::v_subrev_f32)
2440 neg[2 - add_op_idx] = neg[2 - add_op_idx] ^ true;
2441
2442 aco_ptr<VOP3A_instruction> mad{create_instruction<VOP3A_instruction>(aco_opcode::v_mad_f32, Format::VOP3A, 3, 1)};
2443 for (unsigned i = 0; i < 3; i++)
2444 {
2445 mad->operands[i] = op[i];
2446 mad->neg[i] = neg[i];
2447 mad->abs[i] = abs[i];
2448 }
2449 mad->omod = omod;
2450 mad->clamp = clamp;
2451 mad->definitions[0] = instr->definitions[0];
2452
2453 /* mark this ssa_def to be re-checked for profitability and literals */
2454 ctx.mad_infos.emplace_back(std::move(instr), mul_instr->definitions[0].tempId());
2455 ctx.info[mad->definitions[0].tempId()].set_mad(mad.get(), ctx.mad_infos.size() - 1);
2456 instr.reset(mad.release());
2457 return;
2458 }
2459 }
2460 /* v_mul_f32(v_cndmask_b32(0, 1.0, cond), a) -> v_cndmask_b32(0, a, cond) */
2461 else if (instr->opcode == aco_opcode::v_mul_f32 && !instr->isVOP3()) {
2462 for (unsigned i = 0; i < 2; i++) {
2463 if (instr->operands[i].isTemp() && ctx.info[instr->operands[i].tempId()].is_b2f() &&
2464 ctx.uses[instr->operands[i].tempId()] == 1 &&
2465 instr->operands[!i].isTemp() && instr->operands[!i].getTemp().type() == RegType::vgpr) {
2466 ctx.uses[instr->operands[i].tempId()]--;
2467 ctx.uses[ctx.info[instr->operands[i].tempId()].temp.id()]++;
2468
2469 aco_ptr<VOP2_instruction> new_instr{create_instruction<VOP2_instruction>(aco_opcode::v_cndmask_b32, Format::VOP2, 3, 1)};
2470 new_instr->operands[0] = Operand(0u);
2471 new_instr->operands[1] = instr->operands[!i];
2472 new_instr->operands[2] = Operand(ctx.info[instr->operands[i].tempId()].temp);
2473 new_instr->definitions[0] = instr->definitions[0];
2474 instr.reset(new_instr.release());
2475 ctx.info[instr->definitions[0].tempId()].label = 0;
2476 return;
2477 }
2478 }
2479 } else if (instr->opcode == aco_opcode::v_or_b32 && ctx.program->chip_class >= GFX9) {
2480 if (combine_three_valu_op(ctx, instr, aco_opcode::s_or_b32, aco_opcode::v_or3_b32, "012", 1 | 2)) ;
2481 else if (combine_three_valu_op(ctx, instr, aco_opcode::v_or_b32, aco_opcode::v_or3_b32, "012", 1 | 2)) ;
2482 else if (combine_three_valu_op(ctx, instr, aco_opcode::s_and_b32, aco_opcode::v_and_or_b32, "120", 1 | 2)) ;
2483 else if (combine_three_valu_op(ctx, instr, aco_opcode::v_and_b32, aco_opcode::v_and_or_b32, "120", 1 | 2)) ;
2484 else if (combine_three_valu_op(ctx, instr, aco_opcode::s_lshl_b32, aco_opcode::v_lshl_or_b32, "120", 1 | 2)) ;
2485 else combine_three_valu_op(ctx, instr, aco_opcode::v_lshlrev_b32, aco_opcode::v_lshl_or_b32, "210", 1 | 2);
2486 } else if (instr->opcode == aco_opcode::v_add_u32 && ctx.program->chip_class >= GFX9) {
2487 if (combine_three_valu_op(ctx, instr, aco_opcode::s_xor_b32, aco_opcode::v_xad_u32, "120", 1 | 2)) ;
2488 else if (combine_three_valu_op(ctx, instr, aco_opcode::v_xor_b32, aco_opcode::v_xad_u32, "120", 1 | 2)) ;
2489 else if (combine_three_valu_op(ctx, instr, aco_opcode::s_add_i32, aco_opcode::v_add3_u32, "012", 1 | 2)) ;
2490 else if (combine_three_valu_op(ctx, instr, aco_opcode::s_add_u32, aco_opcode::v_add3_u32, "012", 1 | 2)) ;
2491 else if (combine_three_valu_op(ctx, instr, aco_opcode::v_add_u32, aco_opcode::v_add3_u32, "012", 1 | 2)) ;
2492 else if (combine_three_valu_op(ctx, instr, aco_opcode::s_lshl_b32, aco_opcode::v_lshl_add_u32, "120", 1 | 2)) ;
2493 else combine_three_valu_op(ctx, instr, aco_opcode::v_lshlrev_b32, aco_opcode::v_lshl_add_u32, "210", 1 | 2);
2494 } else if (instr->opcode == aco_opcode::v_lshlrev_b32 && ctx.program->chip_class >= GFX9) {
2495 combine_three_valu_op(ctx, instr, aco_opcode::v_add_u32, aco_opcode::v_add_lshl_u32, "120", 2);
2496 } else if ((instr->opcode == aco_opcode::s_add_u32 || instr->opcode == aco_opcode::s_add_i32) && ctx.program->chip_class >= GFX9) {
2497 combine_salu_lshl_add(ctx, instr);
2498 } else if (instr->opcode == aco_opcode::s_not_b32) {
2499 combine_salu_not_bitwise(ctx, instr);
2500 } else if (instr->opcode == aco_opcode::s_not_b64) {
2501 if (combine_inverse_comparison(ctx, instr)) ;
2502 else combine_salu_not_bitwise(ctx, instr);
2503 } else if (instr->opcode == aco_opcode::s_and_b32 || instr->opcode == aco_opcode::s_or_b32 ||
2504 instr->opcode == aco_opcode::s_and_b64 || instr->opcode == aco_opcode::s_or_b64) {
2505 if (combine_ordering_test(ctx, instr)) ;
2506 else if (combine_comparison_ordering(ctx, instr)) ;
2507 else if (combine_constant_comparison_ordering(ctx, instr)) ;
2508 else combine_salu_n2(ctx, instr);
2509 } else {
2510 aco_opcode min, max, min3, max3, med3;
2511 bool some_gfx9_only;
2512 if (get_minmax_info(instr->opcode, &min, &max, &min3, &max3, &med3, &some_gfx9_only) &&
2513 (!some_gfx9_only || ctx.program->chip_class >= GFX9)) {
2514 if (combine_minmax(ctx, instr, instr->opcode == min ? max : min, instr->opcode == min ? min3 : max3)) ;
2515 else combine_clamp(ctx, instr, min, max, med3);
2516 }
2517 }
2518 }
2519
2520 bool to_uniform_bool_instr(opt_ctx &ctx, aco_ptr<Instruction> &instr)
2521 {
2522 switch (instr->opcode) {
2523 case aco_opcode::s_and_b32:
2524 case aco_opcode::s_and_b64:
2525 instr->opcode = aco_opcode::s_and_b32;
2526 break;
2527 case aco_opcode::s_or_b32:
2528 case aco_opcode::s_or_b64:
2529 instr->opcode = aco_opcode::s_or_b32;
2530 break;
2531 case aco_opcode::s_xor_b32:
2532 case aco_opcode::s_xor_b64:
2533 instr->opcode = aco_opcode::s_absdiff_i32;
2534 break;
2535 default:
2536 /* Don't transform other instructions. They are very unlikely to appear here. */
2537 return false;
2538 }
2539
2540 for (Operand &op : instr->operands) {
2541 ctx.uses[op.tempId()]--;
2542
2543 if (ctx.info[op.tempId()].is_uniform_bool()) {
2544 /* Just use the uniform boolean temp. */
2545 op.setTemp(ctx.info[op.tempId()].temp);
2546 } else if (ctx.info[op.tempId()].is_uniform_bitwise()) {
2547 /* Use the SCC definition of the predecessor instruction.
2548 * This allows the predecessor to get picked up by the same optimization (if it has no divergent users),
2549 * and it also makes sure that the current instruction will keep working even if the predecessor won't be transformed.
2550 */
2551 Instruction *pred_instr = ctx.info[op.tempId()].instr;
2552 assert(pred_instr->definitions.size() >= 2);
2553 assert(pred_instr->definitions[1].isFixed() && pred_instr->definitions[1].physReg() == scc);
2554 op.setTemp(pred_instr->definitions[1].getTemp());
2555 } else {
2556 unreachable("Invalid operand on uniform bitwise instruction.");
2557 }
2558
2559 ctx.uses[op.tempId()]++;
2560 }
2561
2562 instr->definitions[0].setTemp(Temp(instr->definitions[0].tempId(), s1));
2563 assert(instr->operands[0].regClass() == s1);
2564 assert(instr->operands[1].regClass() == s1);
2565 return true;
2566 }
2567
2568 void select_instruction(opt_ctx &ctx, aco_ptr<Instruction>& instr)
2569 {
2570 const uint32_t threshold = 4;
2571
2572 if (is_dead(ctx.uses, instr.get())) {
2573 instr.reset();
2574 return;
2575 }
2576
2577 /* convert split_vector into a copy or extract_vector if only one definition is ever used */
2578 if (instr->opcode == aco_opcode::p_split_vector) {
2579 unsigned num_used = 0;
2580 unsigned idx = 0;
2581 unsigned split_offset = 0;
2582 for (unsigned i = 0, offset = 0; i < instr->definitions.size(); offset += instr->definitions[i++].bytes()) {
2583 if (ctx.uses[instr->definitions[i].tempId()]) {
2584 num_used++;
2585 idx = i;
2586 split_offset = offset;
2587 }
2588 }
2589 bool done = false;
2590 if (num_used == 1 && ctx.info[instr->operands[0].tempId()].is_vec() &&
2591 ctx.uses[instr->operands[0].tempId()] == 1) {
2592 Instruction *vec = ctx.info[instr->operands[0].tempId()].instr;
2593
2594 unsigned off = 0;
2595 Operand op;
2596 for (Operand& vec_op : vec->operands) {
2597 if (off == split_offset) {
2598 op = vec_op;
2599 break;
2600 }
2601 off += vec_op.bytes();
2602 }
2603 if (off != instr->operands[0].bytes() && op.bytes() == instr->definitions[idx].bytes()) {
2604 ctx.uses[instr->operands[0].tempId()]--;
2605 for (Operand& vec_op : vec->operands) {
2606 if (vec_op.isTemp())
2607 ctx.uses[vec_op.tempId()]--;
2608 }
2609 if (op.isTemp())
2610 ctx.uses[op.tempId()]++;
2611
2612 aco_ptr<Pseudo_instruction> extract{create_instruction<Pseudo_instruction>(aco_opcode::p_create_vector, Format::PSEUDO, 1, 1)};
2613 extract->operands[0] = op;
2614 extract->definitions[0] = instr->definitions[idx];
2615 instr.reset(extract.release());
2616
2617 done = true;
2618 }
2619 }
2620
2621 if (!done && num_used == 1 &&
2622 instr->operands[0].bytes() % instr->definitions[idx].bytes() == 0 &&
2623 split_offset % instr->definitions[idx].bytes() == 0) {
2624 aco_ptr<Pseudo_instruction> extract{create_instruction<Pseudo_instruction>(aco_opcode::p_extract_vector, Format::PSEUDO, 2, 1)};
2625 extract->operands[0] = instr->operands[0];
2626 extract->operands[1] = Operand((uint32_t) split_offset / instr->definitions[idx].bytes());
2627 extract->definitions[0] = instr->definitions[idx];
2628 instr.reset(extract.release());
2629 }
2630 }
2631
2632 mad_info* mad_info = NULL;
2633 if (instr->opcode == aco_opcode::v_mad_f32 && ctx.info[instr->definitions[0].tempId()].is_mad()) {
2634 mad_info = &ctx.mad_infos[ctx.info[instr->definitions[0].tempId()].val];
2635 /* re-check mad instructions */
2636 if (ctx.uses[mad_info->mul_temp_id]) {
2637 ctx.uses[mad_info->mul_temp_id]++;
2638 if (instr->operands[0].isTemp())
2639 ctx.uses[instr->operands[0].tempId()]--;
2640 if (instr->operands[1].isTemp())
2641 ctx.uses[instr->operands[1].tempId()]--;
2642 instr.swap(mad_info->add_instr);
2643 mad_info = NULL;
2644 }
2645 /* check literals */
2646 else if (!instr->usesModifiers()) {
2647 bool sgpr_used = false;
2648 uint32_t literal_idx = 0;
2649 uint32_t literal_uses = UINT32_MAX;
2650 for (unsigned i = 0; i < instr->operands.size(); i++)
2651 {
2652 if (instr->operands[i].isConstant() && i > 0) {
2653 literal_uses = UINT32_MAX;
2654 break;
2655 }
2656 if (!instr->operands[i].isTemp())
2657 continue;
2658 /* if one of the operands is sgpr, we cannot add a literal somewhere else on pre-GFX10 or operands other than the 1st */
2659 if (instr->operands[i].getTemp().type() == RegType::sgpr && (i > 0 || ctx.program->chip_class < GFX10)) {
2660 if (!sgpr_used && ctx.info[instr->operands[i].tempId()].is_literal()) {
2661 literal_uses = ctx.uses[instr->operands[i].tempId()];
2662 literal_idx = i;
2663 } else {
2664 literal_uses = UINT32_MAX;
2665 }
2666 sgpr_used = true;
2667 /* don't break because we still need to check constants */
2668 } else if (!sgpr_used &&
2669 ctx.info[instr->operands[i].tempId()].is_literal() &&
2670 ctx.uses[instr->operands[i].tempId()] < literal_uses) {
2671 literal_uses = ctx.uses[instr->operands[i].tempId()];
2672 literal_idx = i;
2673 }
2674 }
2675
2676 /* Limit the number of literals to apply to not increase the code
2677 * size too much, but always apply literals for v_mad->v_madak
2678 * because both instructions are 64-bit and this doesn't increase
2679 * code size.
2680 * TODO: try to apply the literals earlier to lower the number of
2681 * uses below threshold
2682 */
2683 if (literal_uses < threshold || literal_idx == 2) {
2684 ctx.uses[instr->operands[literal_idx].tempId()]--;
2685 mad_info->check_literal = true;
2686 mad_info->literal_idx = literal_idx;
2687 return;
2688 }
2689 }
2690 }
2691
2692 /* Mark SCC needed, so the uniform boolean transformation won't swap the definitions when it isn't beneficial */
2693 if (instr->format == Format::PSEUDO_BRANCH &&
2694 instr->operands.size() &&
2695 instr->operands[0].isTemp()) {
2696 ctx.info[instr->operands[0].tempId()].set_scc_needed();
2697 return;
2698 } else if ((instr->opcode == aco_opcode::s_cselect_b64 ||
2699 instr->opcode == aco_opcode::s_cselect_b32) &&
2700 instr->operands[2].isTemp()) {
2701 ctx.info[instr->operands[2].tempId()].set_scc_needed();
2702 }
2703
2704 /* check for literals */
2705 if (!instr->isSALU() && !instr->isVALU())
2706 return;
2707
2708 /* Transform uniform bitwise boolean operations to 32-bit when there are no divergent uses. */
2709 if (instr->definitions.size() &&
2710 ctx.uses[instr->definitions[0].tempId()] == 0 &&
2711 ctx.info[instr->definitions[0].tempId()].is_uniform_bitwise()) {
2712 bool transform_done = to_uniform_bool_instr(ctx, instr);
2713
2714 if (transform_done && !ctx.info[instr->definitions[1].tempId()].is_scc_needed()) {
2715 /* Swap the two definition IDs in order to avoid overusing the SCC. This reduces extra moves generated by RA. */
2716 uint32_t def0_id = instr->definitions[0].getTemp().id();
2717 uint32_t def1_id = instr->definitions[1].getTemp().id();
2718 instr->definitions[0].setTemp(Temp(def1_id, s1));
2719 instr->definitions[1].setTemp(Temp(def0_id, s1));
2720 }
2721
2722 return;
2723 }
2724
2725 if (instr->isSDWA() || instr->isDPP() || (instr->isVOP3() && ctx.program->chip_class < GFX10))
2726 return; /* some encodings can't ever take literals */
2727
2728 /* we do not apply the literals yet as we don't know if it is profitable */
2729 Operand current_literal(s1);
2730
2731 unsigned literal_id = 0;
2732 unsigned literal_uses = UINT32_MAX;
2733 Operand literal(s1);
2734 unsigned num_operands = 1;
2735 if (instr->isSALU() || (ctx.program->chip_class >= GFX10 && can_use_VOP3(ctx, instr)))
2736 num_operands = instr->operands.size();
2737 /* catch VOP2 with a 3rd SGPR operand (e.g. v_cndmask_b32, v_addc_co_u32) */
2738 else if (instr->isVALU() && instr->operands.size() >= 3)
2739 return;
2740
2741 unsigned sgpr_ids[2] = {0, 0};
2742 bool is_literal_sgpr = false;
2743 uint32_t mask = 0;
2744
2745 /* choose a literal to apply */
2746 for (unsigned i = 0; i < num_operands; i++) {
2747 Operand op = instr->operands[i];
2748
2749 if (instr->isVALU() && op.isTemp() && op.getTemp().type() == RegType::sgpr &&
2750 op.tempId() != sgpr_ids[0])
2751 sgpr_ids[!!sgpr_ids[0]] = op.tempId();
2752
2753 if (op.isLiteral()) {
2754 current_literal = op;
2755 continue;
2756 } else if (!op.isTemp() || !ctx.info[op.tempId()].is_literal()) {
2757 continue;
2758 }
2759
2760 if (!alu_can_accept_constant(instr->opcode, i))
2761 continue;
2762
2763 if (ctx.uses[op.tempId()] < literal_uses) {
2764 is_literal_sgpr = op.getTemp().type() == RegType::sgpr;
2765 mask = 0;
2766 literal = Operand(ctx.info[op.tempId()].val);
2767 literal_uses = ctx.uses[op.tempId()];
2768 literal_id = op.tempId();
2769 }
2770
2771 mask |= (op.tempId() == literal_id) << i;
2772 }
2773
2774
2775 /* don't go over the constant bus limit */
2776 bool is_shift64 = instr->opcode == aco_opcode::v_lshlrev_b64 ||
2777 instr->opcode == aco_opcode::v_lshrrev_b64 ||
2778 instr->opcode == aco_opcode::v_ashrrev_i64;
2779 unsigned const_bus_limit = instr->isVALU() ? 1 : UINT32_MAX;
2780 if (ctx.program->chip_class >= GFX10 && !is_shift64)
2781 const_bus_limit = 2;
2782
2783 unsigned num_sgprs = !!sgpr_ids[0] + !!sgpr_ids[1];
2784 if (num_sgprs == const_bus_limit && !is_literal_sgpr)
2785 return;
2786
2787 if (literal_id && literal_uses < threshold &&
2788 (current_literal.isUndefined() ||
2789 (current_literal.size() == literal.size() &&
2790 current_literal.constantValue() == literal.constantValue()))) {
2791 /* mark the literal to be applied */
2792 while (mask) {
2793 unsigned i = u_bit_scan(&mask);
2794 if (instr->operands[i].isTemp() && instr->operands[i].tempId() == literal_id)
2795 ctx.uses[instr->operands[i].tempId()]--;
2796 }
2797 }
2798 }
2799
2800
2801 void apply_literals(opt_ctx &ctx, aco_ptr<Instruction>& instr)
2802 {
2803 /* Cleanup Dead Instructions */
2804 if (!instr)
2805 return;
2806
2807 /* apply literals on MAD */
2808 if (instr->opcode == aco_opcode::v_mad_f32 && ctx.info[instr->definitions[0].tempId()].is_mad()) {
2809 mad_info* info = &ctx.mad_infos[ctx.info[instr->definitions[0].tempId()].val];
2810 if (info->check_literal &&
2811 (ctx.uses[instr->operands[info->literal_idx].tempId()] == 0 || info->literal_idx == 2)) {
2812 aco_ptr<Instruction> new_mad;
2813 if (info->literal_idx == 2) { /* add literal -> madak */
2814 new_mad.reset(create_instruction<VOP2_instruction>(aco_opcode::v_madak_f32, Format::VOP2, 3, 1));
2815 new_mad->operands[0] = instr->operands[0];
2816 new_mad->operands[1] = instr->operands[1];
2817 } else { /* mul literal -> madmk */
2818 new_mad.reset(create_instruction<VOP2_instruction>(aco_opcode::v_madmk_f32, Format::VOP2, 3, 1));
2819 new_mad->operands[0] = instr->operands[1 - info->literal_idx];
2820 new_mad->operands[1] = instr->operands[2];
2821 }
2822 new_mad->operands[2] = Operand(ctx.info[instr->operands[info->literal_idx].tempId()].val);
2823 new_mad->definitions[0] = instr->definitions[0];
2824 ctx.instructions.emplace_back(std::move(new_mad));
2825 return;
2826 }
2827 }
2828
2829 /* apply literals on other SALU/VALU */
2830 if (instr->isSALU() || instr->isVALU()) {
2831 for (unsigned i = 0; i < instr->operands.size(); i++) {
2832 Operand op = instr->operands[i];
2833 if (op.isTemp() && ctx.info[op.tempId()].is_literal() && ctx.uses[op.tempId()] == 0) {
2834 Operand literal(ctx.info[op.tempId()].val);
2835 if (instr->isVALU() && i > 0)
2836 to_VOP3(ctx, instr);
2837 instr->operands[i] = literal;
2838 }
2839 }
2840 }
2841
2842 ctx.instructions.emplace_back(std::move(instr));
2843 }
2844
2845
2846 void optimize(Program* program)
2847 {
2848 opt_ctx ctx;
2849 ctx.program = program;
2850 std::vector<ssa_info> info(program->peekAllocationId());
2851 ctx.info = info.data();
2852
2853 /* 1. Bottom-Up DAG pass (forward) to label all ssa-defs */
2854 for (Block& block : program->blocks) {
2855 for (aco_ptr<Instruction>& instr : block.instructions)
2856 label_instruction(ctx, block, instr);
2857 }
2858
2859 ctx.uses = std::move(dead_code_analysis(program));
2860
2861 /* 2. Combine v_mad, omod, clamp and propagate sgpr on VALU instructions */
2862 for (Block& block : program->blocks) {
2863 for (aco_ptr<Instruction>& instr : block.instructions)
2864 combine_instruction(ctx, block, instr);
2865 }
2866
2867 /* 3. Top-Down DAG pass (backward) to select instructions (includes DCE) */
2868 for (std::vector<Block>::reverse_iterator it = program->blocks.rbegin(); it != program->blocks.rend(); ++it) {
2869 Block* block = &(*it);
2870 for (std::vector<aco_ptr<Instruction>>::reverse_iterator it = block->instructions.rbegin(); it != block->instructions.rend(); ++it)
2871 select_instruction(ctx, *it);
2872 }
2873
2874 /* 4. Add literals to instructions */
2875 for (Block& block : program->blocks) {
2876 ctx.instructions.clear();
2877 for (aco_ptr<Instruction>& instr : block.instructions)
2878 apply_literals(ctx, instr);
2879 block.instructions.swap(ctx.instructions);
2880 }
2881
2882 }
2883
2884 }