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