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