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