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