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