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