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