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