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