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