2 * Copyright © 2018 Valve Corporation
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:
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
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
24 * Daniel Schürmann (daniel.schuermann@campus.tu-berlin.de)
32 #include "util/half_float.h"
33 #include "util/u_math.h"
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.
53 aco_ptr
<Instruction
> add_instr
;
59 mad_info(aco_ptr
<Instruction
> instr
, uint32_t id
, bool vop3
)
60 : add_instr(std::move(instr
)), mul_temp_id(id
), needs_vop3(vop3
), check_literal(false) {}
65 label_constant
= 1 << 1,
70 label_literal
= 1 << 6,
74 label_omod5
= 1 << 10,
75 label_omod_success
= 1 << 11,
76 label_clamp
= 1 << 12,
77 label_clamp_success
= 1 << 13,
78 label_undefined
= 1 << 14,
81 label_add_sub
= 1 << 17,
82 label_bitwise
= 1 << 18,
83 label_minmax
= 1 << 19,
87 static constexpr uint32_t instr_labels
= label_vec
| label_mul
| label_mad
| label_omod_success
| label_clamp_success
| label_add_sub
| label_bitwise
| label_minmax
| label_fcmp
;
88 static constexpr uint32_t temp_labels
= label_abs
| label_neg
| label_temp
| label_vcc
| label_b2f
;
89 static constexpr uint32_t val_labels
= label_constant
| label_literal
| label_mad
;
99 void add_label(Label new_label
)
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 */
107 if (new_label
& temp_labels
) {
108 label
&= ~temp_labels
;
109 label
&= ~instr_labels
; /* instr and temp alias */
112 if (new_label
& val_labels
)
113 label
&= ~val_labels
;
118 void set_vec(Instruction
* vec
)
120 add_label(label_vec
);
126 return label
& label_vec
;
129 void set_constant(uint32_t constant
)
131 add_label(label_constant
);
137 return label
& label_constant
;
140 void set_abs(Temp abs_temp
)
142 add_label(label_abs
);
148 return label
& label_abs
;
151 void set_neg(Temp neg_temp
)
153 add_label(label_neg
);
159 return label
& label_neg
;
162 void set_neg_abs(Temp neg_abs_temp
)
164 add_label((Label
)((uint32_t)label_abs
| (uint32_t)label_neg
));
168 void set_mul(Instruction
* mul
)
170 add_label(label_mul
);
176 return label
& label_mul
;
179 void set_temp(Temp tmp
)
181 add_label(label_temp
);
187 return label
& label_temp
;
190 void set_literal(uint32_t lit
)
192 add_label(label_literal
);
198 return label
& label_literal
;
201 void set_mad(Instruction
* mad
, uint32_t mad_info_idx
)
203 add_label(label_mad
);
210 return label
& label_mad
;
215 add_label(label_omod2
);
220 return label
& label_omod2
;
225 add_label(label_omod4
);
230 return label
& label_omod4
;
235 add_label(label_omod5
);
240 return label
& label_omod5
;
243 void set_omod_success(Instruction
* omod_instr
)
245 add_label(label_omod_success
);
249 bool is_omod_success()
251 return label
& label_omod_success
;
256 add_label(label_clamp
);
261 return label
& label_clamp
;
264 void set_clamp_success(Instruction
* clamp_instr
)
266 add_label(label_clamp_success
);
270 bool is_clamp_success()
272 return label
& label_clamp_success
;
277 add_label(label_undefined
);
282 return label
& label_undefined
;
285 void set_vcc(Temp vcc
)
287 add_label(label_vcc
);
293 return label
& label_vcc
;
296 bool is_constant_or_literal()
298 return is_constant() || is_literal();
301 void set_b2f(Temp val
)
303 add_label(label_b2f
);
309 return label
& label_b2f
;
312 void set_add_sub(Instruction
*add_sub_instr
)
314 add_label(label_add_sub
);
315 instr
= add_sub_instr
;
320 return label
& label_add_sub
;
323 void set_bitwise(Instruction
*bitwise_instr
)
325 add_label(label_bitwise
);
326 instr
= bitwise_instr
;
331 return label
& label_bitwise
;
334 void set_minmax(Instruction
*minmax_instr
)
336 add_label(label_minmax
);
337 instr
= minmax_instr
;
342 return label
& label_minmax
;
345 void set_fcmp(Instruction
*fcmp_instr
)
347 add_label(label_fcmp
);
353 return label
& label_fcmp
;
360 std::vector
<aco_ptr
<Instruction
>> instructions
;
362 std::pair
<uint32_t,Temp
> last_literal
;
363 std::vector
<mad_info
> mad_infos
;
364 std::vector
<uint16_t> uses
;
367 bool can_swap_operands(aco_ptr
<Instruction
>& instr
)
369 if (instr
->operands
[0].isConstant() ||
370 (instr
->operands
[0].isTemp() && instr
->operands
[0].getTemp().type() == RegType::sgpr
))
373 switch (instr
->opcode
) {
374 case aco_opcode::v_add_f32
:
375 case aco_opcode::v_mul_f32
:
376 case aco_opcode::v_or_b32
:
377 case aco_opcode::v_and_b32
:
378 case aco_opcode::v_xor_b32
:
379 case aco_opcode::v_max_f32
:
380 case aco_opcode::v_min_f32
:
381 case aco_opcode::v_cmp_eq_f32
:
382 case aco_opcode::v_cmp_lg_f32
:
384 case aco_opcode::v_sub_f32
:
385 instr
->opcode
= aco_opcode::v_subrev_f32
;
387 case aco_opcode::v_cmp_lt_f32
:
388 instr
->opcode
= aco_opcode::v_cmp_gt_f32
;
390 case aco_opcode::v_cmp_ge_f32
:
391 instr
->opcode
= aco_opcode::v_cmp_le_f32
;
393 case aco_opcode::v_cmp_lt_i32
:
394 instr
->opcode
= aco_opcode::v_cmp_gt_i32
;
401 bool can_use_VOP3(aco_ptr
<Instruction
>& instr
)
403 if (instr
->operands
.size() && instr
->operands
[0].isLiteral())
406 if (instr
->isDPP() || instr
->isSDWA())
409 return instr
->opcode
!= aco_opcode::v_madmk_f32
&&
410 instr
->opcode
!= aco_opcode::v_madak_f32
&&
411 instr
->opcode
!= aco_opcode::v_madmk_f16
&&
412 instr
->opcode
!= aco_opcode::v_madak_f16
;
415 bool can_apply_sgprs(aco_ptr
<Instruction
>& instr
)
417 return instr
->opcode
!= aco_opcode::v_readfirstlane_b32
&&
418 instr
->opcode
!= aco_opcode::v_readlane_b32
&&
419 instr
->opcode
!= aco_opcode::v_writelane_b32
;
422 void to_VOP3(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
427 assert(!instr
->operands
[0].isLiteral());
428 aco_ptr
<Instruction
> tmp
= std::move(instr
);
429 Format format
= asVOP3(tmp
->format
);
430 instr
.reset(create_instruction
<VOP3A_instruction
>(tmp
->opcode
, format
, tmp
->operands
.size(), tmp
->definitions
.size()));
431 std::copy(tmp
->operands
.cbegin(), tmp
->operands
.cend(), instr
->operands
.begin());
432 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
433 instr
->definitions
[i
] = tmp
->definitions
[i
];
434 if (instr
->definitions
[i
].isTemp()) {
435 ssa_info
& info
= ctx
.info
[instr
->definitions
[i
].tempId()];
436 if (info
.label
& instr_labels
&& info
.instr
== tmp
.get())
437 info
.instr
= instr
.get();
442 /* only covers special cases */
443 bool can_accept_constant(aco_ptr
<Instruction
>& instr
, unsigned operand
)
445 switch (instr
->opcode
) {
446 case aco_opcode::v_interp_p2_f32
:
447 case aco_opcode::v_mac_f32
:
448 case aco_opcode::v_writelane_b32
:
449 case aco_opcode::v_cndmask_b32
:
451 case aco_opcode::s_addk_i32
:
452 case aco_opcode::s_mulk_i32
:
453 case aco_opcode::p_wqm
:
454 case aco_opcode::p_extract_vector
:
455 case aco_opcode::p_split_vector
:
456 case aco_opcode::v_readlane_b32
:
457 case aco_opcode::v_readfirstlane_b32
:
460 if ((instr
->format
== Format::MUBUF
||
461 instr
->format
== Format::MIMG
) &&
462 instr
->definitions
.size() == 1 &&
463 instr
->operands
.size() == 4) {
470 bool valu_can_accept_literal(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
, unsigned operand
)
472 // TODO: VOP3 can take a literal on GFX10
473 return !instr
->isSDWA() && !instr
->isDPP() && !instr
->isVOP3() &&
474 operand
== 0 && can_accept_constant(instr
, operand
);
477 bool parse_base_offset(opt_ctx
&ctx
, Instruction
* instr
, unsigned op_index
, Temp
*base
, uint32_t *offset
)
479 Operand op
= instr
->operands
[op_index
];
483 Temp tmp
= op
.getTemp();
484 if (!ctx
.info
[tmp
.id()].is_add_sub())
487 Instruction
*add_instr
= ctx
.info
[tmp
.id()].instr
;
489 switch (add_instr
->opcode
) {
490 case aco_opcode::v_add_u32
:
491 case aco_opcode::v_add_co_u32
:
492 case aco_opcode::s_add_i32
:
493 case aco_opcode::s_add_u32
:
499 for (unsigned i
= 0; i
< 2; i
++) {
500 if (add_instr
->operands
[i
].isConstant()) {
501 *offset
= add_instr
->operands
[i
].constantValue();
502 } else if (add_instr
->operands
[i
].isTemp() &&
503 ctx
.info
[add_instr
->operands
[i
].tempId()].is_constant_or_literal()) {
504 *offset
= ctx
.info
[add_instr
->operands
[i
].tempId()].val
;
508 if (!add_instr
->operands
[!i
].isTemp())
511 uint32_t offset2
= 0;
512 if (parse_base_offset(ctx
, add_instr
, !i
, base
, &offset2
)) {
515 *base
= add_instr
->operands
[!i
].getTemp();
523 void label_instruction(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
525 if (instr
->isSALU() || instr
->isVALU() || instr
->format
== Format::PSEUDO
) {
526 ASSERTED
bool all_const
= false;
527 for (Operand
& op
: instr
->operands
)
528 all_const
= all_const
&& (!op
.isTemp() || ctx
.info
[op
.tempId()].is_constant_or_literal());
529 perfwarn(all_const
, "All instruction operands are constant", instr
.get());
532 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
534 if (!instr
->operands
[i
].isTemp())
537 ssa_info info
= ctx
.info
[instr
->operands
[i
].tempId()];
538 /* propagate undef */
539 if (info
.is_undefined() && is_phi(instr
))
540 instr
->operands
[i
] = Operand(instr
->operands
[i
].regClass());
541 /* propagate reg->reg of same type */
542 if (info
.is_temp() && info
.temp
.regClass() == instr
->operands
[i
].getTemp().regClass()) {
543 instr
->operands
[i
].setTemp(ctx
.info
[instr
->operands
[i
].tempId()].temp
);
544 info
= ctx
.info
[info
.temp
.id()];
547 /* SALU / PSEUDO: propagate inline constants */
548 if (instr
->isSALU() || instr
->format
== Format::PSEUDO
) {
549 if (info
.is_temp() && info
.temp
.type() == RegType::sgpr
) {
550 instr
->operands
[i
].setTemp(info
.temp
);
551 info
= ctx
.info
[info
.temp
.id()];
552 } else if (info
.is_temp() && info
.temp
.type() == RegType::vgpr
) {
553 /* propagate vgpr if it can take it */
554 switch (instr
->opcode
) {
555 case aco_opcode::p_create_vector
:
556 case aco_opcode::p_split_vector
:
557 case aco_opcode::p_extract_vector
:
558 case aco_opcode::p_phi
: {
559 const bool all_vgpr
= std::none_of(instr
->definitions
.begin(), instr
->definitions
.end(),
560 [] (const Definition
& def
) { return def
.getTemp().type() != RegType::vgpr
;});
562 instr
->operands
[i
] = Operand(info
.temp
);
563 info
= ctx
.info
[info
.temp
.id()];
571 if ((info
.is_constant() || (info
.is_literal() && instr
->format
== Format::PSEUDO
)) && !instr
->operands
[i
].isFixed() && can_accept_constant(instr
, i
)) {
572 instr
->operands
[i
] = Operand(info
.val
);
577 /* VALU: propagate neg, abs & inline constants */
578 else if (instr
->isVALU()) {
579 if (info
.is_temp() && info
.temp
.type() == RegType::vgpr
) {
580 instr
->operands
[i
].setTemp(info
.temp
);
581 info
= ctx
.info
[info
.temp
.id()];
583 if (info
.is_abs() && (can_use_VOP3(instr
) || instr
->isDPP()) && instr_info
.can_use_input_modifiers
[(int)instr
->opcode
]) {
586 instr
->operands
[i
] = Operand(info
.temp
);
588 static_cast<DPP_instruction
*>(instr
.get())->abs
[i
] = true;
590 static_cast<VOP3A_instruction
*>(instr
.get())->abs
[i
] = true;
592 if (info
.is_neg() && instr
->opcode
== aco_opcode::v_add_f32
) {
593 instr
->opcode
= i
? aco_opcode::v_sub_f32
: aco_opcode::v_subrev_f32
;
594 instr
->operands
[i
].setTemp(info
.temp
);
596 } else if (info
.is_neg() && (can_use_VOP3(instr
) || instr
->isDPP()) && instr_info
.can_use_input_modifiers
[(int)instr
->opcode
]) {
599 instr
->operands
[i
].setTemp(info
.temp
);
601 static_cast<DPP_instruction
*>(instr
.get())->neg
[i
] = true;
603 static_cast<VOP3A_instruction
*>(instr
.get())->neg
[i
] = true;
606 if (info
.is_constant() && can_accept_constant(instr
, i
)) {
607 perfwarn(instr
->opcode
== aco_opcode::v_cndmask_b32
&& i
== 2, "v_cndmask_b32 with a constant selector", instr
.get());
609 instr
->operands
[i
] = Operand(info
.val
);
611 } else if (!instr
->isVOP3() && can_swap_operands(instr
)) {
612 instr
->operands
[i
] = instr
->operands
[0];
613 instr
->operands
[0] = Operand(info
.val
);
615 } else if (can_use_VOP3(instr
)) {
617 instr
->operands
[i
] = Operand(info
.val
);
623 /* MUBUF: propagate constants and combine additions */
624 else if (instr
->format
== Format::MUBUF
) {
625 MUBUF_instruction
*mubuf
= static_cast<MUBUF_instruction
*>(instr
.get());
628 while (info
.is_temp())
629 info
= ctx
.info
[info
.temp
.id()];
631 if (mubuf
->offen
&& i
== 0 && info
.is_constant_or_literal() && mubuf
->offset
+ info
.val
< 4096) {
632 assert(!mubuf
->idxen
);
633 instr
->operands
[i
] = Operand(v1
);
634 mubuf
->offset
+= info
.val
;
635 mubuf
->offen
= false;
637 } else if (i
== 2 && info
.is_constant_or_literal() && mubuf
->offset
+ info
.val
< 4096) {
638 instr
->operands
[2] = Operand((uint32_t) 0);
639 mubuf
->offset
+= info
.val
;
641 } else if (mubuf
->offen
&& i
== 0 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == v1
&& mubuf
->offset
+ offset
< 4096) {
642 assert(!mubuf
->idxen
);
643 instr
->operands
[i
].setTemp(base
);
644 mubuf
->offset
+= offset
;
646 } else if (i
== 2 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == s1
&& mubuf
->offset
+ offset
< 4096) {
647 instr
->operands
[i
].setTemp(base
);
648 mubuf
->offset
+= offset
;
653 /* DS: combine additions */
654 else if (instr
->format
== Format::DS
) {
656 DS_instruction
*ds
= static_cast<DS_instruction
*>(instr
.get());
659 if (i
== 0 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == instr
->operands
[i
].regClass()) {
660 if (instr
->opcode
== aco_opcode::ds_write2_b32
|| instr
->opcode
== aco_opcode::ds_read2_b32
) {
661 if (offset
% 4 == 0 &&
662 ds
->offset0
+ (offset
>> 2) <= 255 &&
663 ds
->offset1
+ (offset
>> 2) <= 255) {
664 instr
->operands
[i
].setTemp(base
);
665 ds
->offset0
+= offset
>> 2;
666 ds
->offset1
+= offset
>> 2;
669 if (ds
->offset0
+ offset
<= 65535) {
670 instr
->operands
[i
].setTemp(base
);
671 ds
->offset0
+= offset
;
677 /* SMEM: propagate constants and combine additions */
678 else if (instr
->format
== Format::SMEM
) {
680 SMEM_instruction
*smem
= static_cast<SMEM_instruction
*>(instr
.get());
683 if (i
== 1 && info
.is_constant_or_literal() && info
.val
<= 0xFFFFF) {
684 instr
->operands
[i
] = Operand(info
.val
);
686 } else if (i
== 1 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == s1
&& offset
<= 0xFFFFF && ctx
.program
->chip_class
>= GFX9
) {
687 bool soe
= smem
->operands
.size() >= (!smem
->definitions
.empty() ? 3 : 4);
689 (!ctx
.info
[smem
->operands
.back().tempId()].is_constant_or_literal() ||
690 ctx
.info
[smem
->operands
.back().tempId()].val
!= 0)) {
694 smem
->operands
[1] = Operand(offset
);
695 smem
->operands
.back() = Operand(base
);
697 SMEM_instruction
*new_instr
= create_instruction
<SMEM_instruction
>(smem
->opcode
, Format::SMEM
, smem
->operands
.size() + 1, smem
->definitions
.size());
698 new_instr
->operands
[0] = smem
->operands
[0];
699 new_instr
->operands
[1] = Operand(offset
);
700 if (smem
->definitions
.empty())
701 new_instr
->operands
[2] = smem
->operands
[2];
702 new_instr
->operands
.back() = Operand(base
);
703 if (!smem
->definitions
.empty())
704 new_instr
->definitions
[0] = smem
->definitions
[0];
705 instr
.reset(new_instr
);
706 smem
= static_cast<SMEM_instruction
*>(instr
.get());
713 /* if this instruction doesn't define anything, return */
714 if (instr
->definitions
.empty())
717 switch (instr
->opcode
) {
718 case aco_opcode::p_create_vector
: {
719 unsigned num_ops
= instr
->operands
.size();
720 for (const Operand
& op
: instr
->operands
) {
721 if (op
.isTemp() && ctx
.info
[op
.tempId()].is_vec())
722 num_ops
+= ctx
.info
[op
.tempId()].instr
->operands
.size() - 1;
724 if (num_ops
!= instr
->operands
.size()) {
725 aco_ptr
<Instruction
> old_vec
= std::move(instr
);
726 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_ops
, 1));
727 instr
->definitions
[0] = old_vec
->definitions
[0];
729 for (Operand
& old_op
: old_vec
->operands
) {
730 if (old_op
.isTemp() && ctx
.info
[old_op
.tempId()].is_vec()) {
731 for (unsigned j
= 0; j
< ctx
.info
[old_op
.tempId()].instr
->operands
.size(); j
++)
732 instr
->operands
[k
++] = ctx
.info
[old_op
.tempId()].instr
->operands
[j
];
734 instr
->operands
[k
++] = old_op
;
737 assert(k
== num_ops
);
739 if (instr
->operands
.size() == 1 && instr
->operands
[0].isTemp())
740 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
741 else if (instr
->definitions
[0].getTemp().size() == instr
->operands
.size())
742 ctx
.info
[instr
->definitions
[0].tempId()].set_vec(instr
.get());
745 case aco_opcode::p_split_vector
: {
746 if (!ctx
.info
[instr
->operands
[0].tempId()].is_vec())
748 Instruction
* vec
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
749 assert(instr
->definitions
.size() == vec
->operands
.size());
750 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
751 Operand vec_op
= vec
->operands
[i
];
752 if (vec_op
.isConstant()) {
753 if (vec_op
.isLiteral())
754 ctx
.info
[instr
->definitions
[i
].tempId()].set_literal(vec_op
.constantValue());
756 ctx
.info
[instr
->definitions
[i
].tempId()].set_constant(vec_op
.constantValue());
758 assert(vec_op
.isTemp());
759 ctx
.info
[instr
->definitions
[i
].tempId()].set_temp(vec_op
.getTemp());
764 case aco_opcode::p_extract_vector
: { /* mov */
765 if (!ctx
.info
[instr
->operands
[0].tempId()].is_vec())
767 Instruction
* vec
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
768 if (vec
->definitions
[0].getTemp().size() == vec
->operands
.size() && /* TODO: what about 64bit or other combinations? */
769 vec
->operands
[0].size() == instr
->definitions
[0].size()) {
771 /* convert this extract into a mov instruction */
772 Operand vec_op
= vec
->operands
[instr
->operands
[1].constantValue()];
773 bool is_vgpr
= instr
->definitions
[0].getTemp().type() == RegType::vgpr
;
774 aco_opcode opcode
= is_vgpr
? aco_opcode::v_mov_b32
: aco_opcode::s_mov_b32
;
775 Format format
= is_vgpr
? Format::VOP1
: Format::SOP1
;
776 instr
->opcode
= opcode
;
777 instr
->format
= format
;
778 instr
->operands
= {instr
->operands
.begin(), 1 };
779 instr
->operands
[0] = vec_op
;
781 if (vec_op
.isConstant()) {
782 if (vec_op
.isLiteral())
783 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(vec_op
.constantValue());
785 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(vec_op
.constantValue());
787 assert(vec_op
.isTemp());
788 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(vec_op
.getTemp());
793 case aco_opcode::s_mov_b32
: /* propagate */
794 case aco_opcode::s_mov_b64
:
795 case aco_opcode::v_mov_b32
:
796 case aco_opcode::p_as_uniform
:
797 if (instr
->definitions
[0].isFixed()) {
798 /* don't copy-propagate copies into fixed registers */
799 } else if (instr
->operands
[0].isConstant()) {
800 if (instr
->operands
[0].isLiteral())
801 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(instr
->operands
[0].constantValue());
803 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(instr
->operands
[0].constantValue());
804 } else if (instr
->isDPP()) {
806 } else if (instr
->operands
[0].isTemp()) {
807 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
809 assert(instr
->operands
[0].isFixed());
812 case aco_opcode::p_is_helper
:
813 if (!ctx
.program
->needs_wqm
)
814 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(0u);
816 case aco_opcode::s_movk_i32
: {
817 uint32_t v
= static_cast<SOPK_instruction
*>(instr
.get())->imm
;
818 v
= v
& 0x8000 ? (v
| 0xffff0000) : v
;
819 if (v
<= 64 || v
>= 0xfffffff0)
820 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
822 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
825 case aco_opcode::v_bfrev_b32
:
826 case aco_opcode::s_brev_b32
: {
827 if (instr
->operands
[0].isConstant()) {
828 uint32_t v
= util_bitreverse(instr
->operands
[0].constantValue());
829 if (v
<= 64 || v
>= 0xfffffff0)
830 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
832 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
836 case aco_opcode::s_bfm_b32
: {
837 if (instr
->operands
[0].isConstant() && instr
->operands
[1].isConstant()) {
838 unsigned size
= instr
->operands
[0].constantValue() & 0x1f;
839 unsigned start
= instr
->operands
[1].constantValue() & 0x1f;
840 uint32_t v
= ((1u << size
) - 1u) << start
;
841 if (v
<= 64 || v
>= 0xfffffff0)
842 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
844 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
847 case aco_opcode::v_mul_f32
: { /* omod */
848 /* TODO: try to move the negate/abs modifier to the consumer instead */
849 if (instr
->isVOP3()) {
850 VOP3A_instruction
*vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
851 if (vop3
->abs
[0] || vop3
->abs
[1] || vop3
->neg
[0] || vop3
->neg
[1] || vop3
->omod
|| vop3
->clamp
)
855 for (unsigned i
= 0; i
< 2; i
++) {
856 if (instr
->operands
[!i
].isConstant() && instr
->operands
[i
].isTemp()) {
857 if (instr
->operands
[!i
].constantValue() == 0x40000000) { /* 2.0 */
858 ctx
.info
[instr
->operands
[i
].tempId()].set_omod2();
859 } else if (instr
->operands
[!i
].constantValue() == 0x40800000) { /* 4.0 */
860 ctx
.info
[instr
->operands
[i
].tempId()].set_omod4();
861 } else if (instr
->operands
[!i
].constantValue() == 0x3f000000) { /* 0.5 */
862 ctx
.info
[instr
->operands
[i
].tempId()].set_omod5();
863 } else if (instr
->operands
[!i
].constantValue() == 0x3f800000) { /* 1.0 */
864 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[i
].getTemp());
873 case aco_opcode::v_and_b32
: /* abs */
874 if (instr
->operands
[0].constantEquals(0x7FFFFFFF) && instr
->operands
[1].isTemp())
875 ctx
.info
[instr
->definitions
[0].tempId()].set_abs(instr
->operands
[1].getTemp());
877 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
879 case aco_opcode::v_xor_b32
: { /* neg */
880 if (instr
->operands
[0].constantEquals(0x80000000u
) && instr
->operands
[1].isTemp()) {
881 if (ctx
.info
[instr
->operands
[1].tempId()].is_neg()) {
882 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
884 if (ctx
.info
[instr
->operands
[1].tempId()].is_abs()) { /* neg(abs(x)) */
885 instr
->operands
[1].setTemp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
886 instr
->opcode
= aco_opcode::v_or_b32
;
887 ctx
.info
[instr
->definitions
[0].tempId()].set_neg_abs(instr
->operands
[1].getTemp());
889 ctx
.info
[instr
->definitions
[0].tempId()].set_neg(instr
->operands
[1].getTemp());
893 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
897 case aco_opcode::v_med3_f32
: { /* clamp */
898 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
899 if (vop3
->abs
[0] || vop3
->neg
[0] || vop3
->opsel
[0] ||
900 vop3
->abs
[1] || vop3
->neg
[1] || vop3
->opsel
[1] ||
901 vop3
->abs
[2] || vop3
->neg
[2] || vop3
->opsel
[2] ||
906 bool found_zero
= false, found_one
= false;
907 for (unsigned i
= 0; i
< 3; i
++)
909 if (instr
->operands
[i
].constantEquals(0))
911 else if (instr
->operands
[i
].constantEquals(0x3f800000)) /* 1.0 */
916 if (found_zero
&& found_one
&& instr
->operands
[idx
].isTemp()) {
917 ctx
.info
[instr
->operands
[idx
].tempId()].set_clamp();
921 case aco_opcode::v_cndmask_b32
:
922 if (instr
->operands
[0].constantEquals(0) &&
923 instr
->operands
[1].constantEquals(0xFFFFFFFF) &&
924 instr
->operands
[2].isTemp())
925 ctx
.info
[instr
->definitions
[0].tempId()].set_vcc(instr
->operands
[2].getTemp());
926 else if (instr
->operands
[0].constantEquals(0) &&
927 instr
->operands
[1].constantEquals(0x3f800000u
) &&
928 instr
->operands
[2].isTemp())
929 ctx
.info
[instr
->definitions
[0].tempId()].set_b2f(instr
->operands
[2].getTemp());
931 case aco_opcode::v_cmp_lg_u32
:
932 if (instr
->format
== Format::VOPC
&& /* don't optimize VOP3 / SDWA / DPP */
933 instr
->operands
[0].constantEquals(0) &&
934 instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_vcc())
935 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
937 case aco_opcode::p_phi
:
938 case aco_opcode::p_linear_phi
: {
939 /* lower_bool_phis() can create phis like this */
940 bool all_same_temp
= instr
->operands
[0].isTemp();
941 /* this check is needed when moving uniform loop counters out of a divergent loop */
943 all_same_temp
= instr
->definitions
[0].regClass() == instr
->operands
[0].regClass();
944 for (unsigned i
= 1; all_same_temp
&& (i
< instr
->operands
.size()); i
++) {
945 if (!instr
->operands
[i
].isTemp() || instr
->operands
[i
].tempId() != instr
->operands
[0].tempId())
946 all_same_temp
= false;
949 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
951 bool all_undef
= instr
->operands
[0].isUndefined();
952 for (unsigned i
= 1; all_undef
&& (i
< instr
->operands
.size()); i
++) {
953 if (!instr
->operands
[i
].isUndefined())
957 ctx
.info
[instr
->definitions
[0].tempId()].set_undefined();
961 case aco_opcode::v_add_u32
:
962 case aco_opcode::v_add_co_u32
:
963 case aco_opcode::s_add_i32
:
964 case aco_opcode::s_add_u32
:
965 ctx
.info
[instr
->definitions
[0].tempId()].set_add_sub(instr
.get());
967 case aco_opcode::s_not_b32
:
968 case aco_opcode::s_not_b64
:
969 case aco_opcode::s_and_b32
:
970 case aco_opcode::s_and_b64
:
971 case aco_opcode::s_or_b32
:
972 case aco_opcode::s_or_b64
:
973 case aco_opcode::s_xor_b32
:
974 case aco_opcode::s_xor_b64
:
975 case aco_opcode::s_lshl_b32
:
976 case aco_opcode::v_or_b32
:
977 case aco_opcode::v_lshlrev_b32
:
978 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
980 case aco_opcode::v_min_f32
:
981 case aco_opcode::v_min_f16
:
982 case aco_opcode::v_min_u32
:
983 case aco_opcode::v_min_i32
:
984 case aco_opcode::v_min_u16
:
985 case aco_opcode::v_min_i16
:
986 case aco_opcode::v_max_f32
:
987 case aco_opcode::v_max_f16
:
988 case aco_opcode::v_max_u32
:
989 case aco_opcode::v_max_i32
:
990 case aco_opcode::v_max_u16
:
991 case aco_opcode::v_max_i16
:
992 ctx
.info
[instr
->definitions
[0].tempId()].set_minmax(instr
.get());
994 case aco_opcode::v_cmp_lt_f32
:
995 case aco_opcode::v_cmp_eq_f32
:
996 case aco_opcode::v_cmp_le_f32
:
997 case aco_opcode::v_cmp_gt_f32
:
998 case aco_opcode::v_cmp_lg_f32
:
999 case aco_opcode::v_cmp_ge_f32
:
1000 case aco_opcode::v_cmp_o_f32
:
1001 case aco_opcode::v_cmp_u_f32
:
1002 case aco_opcode::v_cmp_nge_f32
:
1003 case aco_opcode::v_cmp_nlg_f32
:
1004 case aco_opcode::v_cmp_ngt_f32
:
1005 case aco_opcode::v_cmp_nle_f32
:
1006 case aco_opcode::v_cmp_neq_f32
:
1007 case aco_opcode::v_cmp_nlt_f32
:
1008 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(instr
.get());
1015 ALWAYS_INLINE
bool get_cmp_info(aco_opcode op
, aco_opcode
*ordered
, aco_opcode
*unordered
, aco_opcode
*inverse
)
1017 *ordered
= *unordered
= op
;
1019 #define CMP(ord, unord) \
1020 case aco_opcode::v_cmp_##ord##_f32:\
1021 case aco_opcode::v_cmp_n##unord##_f32:\
1022 *ordered = aco_opcode::v_cmp_##ord##_f32;\
1023 *unordered = aco_opcode::v_cmp_n##unord##_f32;\
1024 *inverse = op == aco_opcode::v_cmp_n##unord##_f32 ? aco_opcode::v_cmp_##unord##_f32 : aco_opcode::v_cmp_n##ord##_f32;\
1038 aco_opcode
get_ordered(aco_opcode op
)
1040 aco_opcode ordered
, unordered
, inverse
;
1041 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? ordered
: aco_opcode::last_opcode
;
1044 aco_opcode
get_unordered(aco_opcode op
)
1046 aco_opcode ordered
, unordered
, inverse
;
1047 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? unordered
: aco_opcode::last_opcode
;
1050 aco_opcode
get_inverse(aco_opcode op
)
1052 aco_opcode ordered
, unordered
, inverse
;
1053 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? inverse
: aco_opcode::last_opcode
;
1056 bool is_cmp(aco_opcode op
)
1058 aco_opcode ordered
, unordered
, inverse
;
1059 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
);
1062 unsigned original_temp_id(opt_ctx
&ctx
, Temp tmp
)
1064 if (ctx
.info
[tmp
.id()].is_temp())
1065 return ctx
.info
[tmp
.id()].temp
.id();
1070 void decrease_uses(opt_ctx
&ctx
, Instruction
* instr
)
1072 if (!--ctx
.uses
[instr
->definitions
[0].tempId()]) {
1073 for (const Operand
& op
: instr
->operands
) {
1075 ctx
.uses
[op
.tempId()]--;
1080 Instruction
*follow_operand(opt_ctx
&ctx
, Operand op
, bool ignore_uses
=false)
1082 if (!op
.isTemp() || !(ctx
.info
[op
.tempId()].label
& instr_labels
))
1084 if (!ignore_uses
&& ctx
.uses
[op
.tempId()] > 1)
1087 Instruction
*instr
= ctx
.info
[op
.tempId()].instr
;
1089 if (instr
->definitions
.size() == 2) {
1090 assert(instr
->definitions
[0].isTemp() && instr
->definitions
[0].tempId() == op
.tempId());
1091 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1098 /* s_or_b64(neq(a, a), neq(b, b)) -> v_cmp_u_f32(a, b)
1099 * s_and_b64(eq(a, a), eq(b, b)) -> v_cmp_o_f32(a, b) */
1100 bool combine_ordering_test(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1102 if (instr
->opcode
!= aco_opcode::s_or_b64
&& instr
->opcode
!= aco_opcode::s_and_b64
)
1104 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1107 bool neg
[2] = {false, false};
1108 bool abs
[2] = {false, false};
1109 bool opsel
[2] = {false, false};
1110 Instruction
*op_instr
[2];
1113 for (unsigned i
= 0; i
< 2; i
++) {
1114 op_instr
[i
] = follow_operand(ctx
, instr
->operands
[i
], true);
1118 aco_opcode expected_cmp
= instr
->opcode
== aco_opcode::s_or_b64
?
1119 aco_opcode::v_cmp_neq_f32
: aco_opcode::v_cmp_eq_f32
;
1121 if (op_instr
[i
]->opcode
!= expected_cmp
)
1123 if (!op_instr
[i
]->operands
[0].isTemp() || !op_instr
[i
]->operands
[1].isTemp())
1126 if (op_instr
[i
]->isVOP3()) {
1127 VOP3A_instruction
*vop3
= static_cast<VOP3A_instruction
*>(op_instr
[i
]);
1128 if (vop3
->neg
[0] != vop3
->neg
[1] || vop3
->abs
[0] != vop3
->abs
[1] || vop3
->opsel
[0] != vop3
->opsel
[1])
1130 neg
[i
] = vop3
->neg
[0];
1131 abs
[i
] = vop3
->abs
[0];
1132 opsel
[i
] = vop3
->opsel
[0];
1135 Temp op0
= op_instr
[i
]->operands
[0].getTemp();
1136 Temp op1
= op_instr
[i
]->operands
[1].getTemp();
1137 if (original_temp_id(ctx
, op0
) != original_temp_id(ctx
, op1
))
1139 /* shouldn't happen yet, but best to be safe */
1140 if (op1
.type() != RegType::vgpr
)
1146 ctx
.uses
[op
[0].id()]++;
1147 ctx
.uses
[op
[1].id()]++;
1148 decrease_uses(ctx
, op_instr
[0]);
1149 decrease_uses(ctx
, op_instr
[1]);
1151 aco_opcode new_op
= instr
->opcode
== aco_opcode::s_or_b64
?
1152 aco_opcode::v_cmp_u_f32
: aco_opcode::v_cmp_o_f32
;
1153 Instruction
*new_instr
;
1154 if (neg
[0] || neg
[1] || abs
[0] || abs
[1] || opsel
[0] || opsel
[1]) {
1155 VOP3A_instruction
*vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1156 for (unsigned i
= 0; i
< 2; i
++) {
1157 vop3
->neg
[i
] = neg
[i
];
1158 vop3
->abs
[i
] = abs
[i
];
1159 vop3
->opsel
[i
] = opsel
[i
];
1161 new_instr
= static_cast<Instruction
*>(vop3
);
1163 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1165 new_instr
->operands
[0] = Operand(op
[0]);
1166 new_instr
->operands
[1] = Operand(op
[1]);
1167 new_instr
->definitions
[0] = instr
->definitions
[0];
1169 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1170 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1172 instr
.reset(new_instr
);
1177 /* s_or_b64(v_cmp_u_f32(a, b), cmp(a, b)) -> get_unordered(cmp)(a, b)
1178 * s_and_b64(v_cmp_o_f32(a, b), cmp(a, b)) -> get_ordered(cmp)(a, b) */
1179 bool combine_comparison_ordering(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1181 if (instr
->opcode
!= aco_opcode::s_or_b64
&& instr
->opcode
!= aco_opcode::s_and_b64
)
1183 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1186 aco_opcode expected_nan_test
= instr
->opcode
== aco_opcode::s_or_b64
?
1187 aco_opcode::v_cmp_u_f32
: aco_opcode::v_cmp_o_f32
;
1189 Instruction
*nan_test
= follow_operand(ctx
, instr
->operands
[0], true);
1190 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[1], true);
1191 if (!nan_test
|| !cmp
)
1194 if (cmp
->opcode
== expected_nan_test
)
1195 std::swap(nan_test
, cmp
);
1196 else if (nan_test
->opcode
!= expected_nan_test
)
1199 if (!is_cmp(cmp
->opcode
))
1202 if (!nan_test
->operands
[0].isTemp() || !nan_test
->operands
[1].isTemp())
1204 if (!cmp
->operands
[0].isTemp() || !cmp
->operands
[1].isTemp())
1207 unsigned prop_cmp0
= original_temp_id(ctx
, cmp
->operands
[0].getTemp());
1208 unsigned prop_cmp1
= original_temp_id(ctx
, cmp
->operands
[1].getTemp());
1209 unsigned prop_nan0
= original_temp_id(ctx
, nan_test
->operands
[0].getTemp());
1210 unsigned prop_nan1
= original_temp_id(ctx
, nan_test
->operands
[1].getTemp());
1211 if (prop_cmp0
!= prop_nan0
&& prop_cmp0
!= prop_nan1
)
1213 if (prop_cmp1
!= prop_nan0
&& prop_cmp1
!= prop_nan1
)
1216 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1217 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1218 decrease_uses(ctx
, nan_test
);
1219 decrease_uses(ctx
, cmp
);
1221 aco_opcode new_op
= instr
->opcode
== aco_opcode::s_or_b64
?
1222 get_unordered(cmp
->opcode
) : get_ordered(cmp
->opcode
);
1223 Instruction
*new_instr
;
1224 if (cmp
->isVOP3()) {
1225 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1226 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1227 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1228 memcpy(new_vop3
->opsel
, cmp_vop3
->opsel
, sizeof(new_vop3
->opsel
));
1229 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1230 new_vop3
->clamp
= cmp_vop3
->clamp
;
1231 new_vop3
->omod
= cmp_vop3
->omod
;
1232 new_instr
= new_vop3
;
1234 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1236 new_instr
->operands
[0] = cmp
->operands
[0];
1237 new_instr
->operands
[1] = cmp
->operands
[1];
1238 new_instr
->definitions
[0] = instr
->definitions
[0];
1240 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1241 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1243 instr
.reset(new_instr
);
1248 /* s_or_b64(v_cmp_neq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_unordered(cmp)(a, b)
1249 * s_and_b64(v_cmp_eq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_ordered(cmp)(a, b) */
1250 bool combine_constant_comparison_ordering(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1252 if (instr
->opcode
!= aco_opcode::s_or_b64
&& instr
->opcode
!= aco_opcode::s_and_b64
)
1254 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1257 Instruction
*nan_test
= follow_operand(ctx
, instr
->operands
[0], true);
1258 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[1], true);
1260 if (!nan_test
|| !cmp
)
1263 aco_opcode expected_nan_test
= instr
->opcode
== aco_opcode::s_or_b64
?
1264 aco_opcode::v_cmp_neq_f32
: aco_opcode::v_cmp_eq_f32
;
1265 if (cmp
->opcode
== expected_nan_test
)
1266 std::swap(nan_test
, cmp
);
1267 else if (nan_test
->opcode
!= expected_nan_test
)
1270 if (!is_cmp(cmp
->opcode
))
1273 if (!nan_test
->operands
[0].isTemp() || !nan_test
->operands
[1].isTemp())
1275 if (!cmp
->operands
[0].isTemp() && !cmp
->operands
[1].isTemp())
1278 unsigned prop_nan0
= original_temp_id(ctx
, nan_test
->operands
[0].getTemp());
1279 unsigned prop_nan1
= original_temp_id(ctx
, nan_test
->operands
[1].getTemp());
1280 if (prop_nan0
!= prop_nan1
)
1283 int constant_operand
= -1;
1284 for (unsigned i
= 0; i
< 2; i
++) {
1285 if (cmp
->operands
[i
].isTemp() && original_temp_id(ctx
, cmp
->operands
[i
].getTemp()) == prop_nan0
) {
1286 constant_operand
= !i
;
1290 if (constant_operand
== -1)
1294 if (cmp
->operands
[constant_operand
].isConstant()) {
1295 constant
= cmp
->operands
[constant_operand
].constantValue();
1296 } else if (cmp
->operands
[constant_operand
].isTemp()) {
1297 unsigned id
= cmp
->operands
[constant_operand
].tempId();
1298 if (!ctx
.info
[id
].is_constant() && !ctx
.info
[id
].is_literal())
1300 constant
= ctx
.info
[id
].val
;
1306 memcpy(&constantf
, &constant
, 4);
1307 if (isnan(constantf
))
1310 if (cmp
->operands
[0].isTemp())
1311 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1312 if (cmp
->operands
[1].isTemp())
1313 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1314 decrease_uses(ctx
, nan_test
);
1315 decrease_uses(ctx
, cmp
);
1317 aco_opcode new_op
= instr
->opcode
== aco_opcode::s_or_b64
?
1318 get_unordered(cmp
->opcode
) : get_ordered(cmp
->opcode
);
1319 Instruction
*new_instr
;
1320 if (cmp
->isVOP3()) {
1321 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1322 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1323 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1324 memcpy(new_vop3
->opsel
, cmp_vop3
->opsel
, sizeof(new_vop3
->opsel
));
1325 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1326 new_vop3
->clamp
= cmp_vop3
->clamp
;
1327 new_vop3
->omod
= cmp_vop3
->omod
;
1328 new_instr
= new_vop3
;
1330 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1332 new_instr
->operands
[0] = cmp
->operands
[0];
1333 new_instr
->operands
[1] = cmp
->operands
[1];
1334 new_instr
->definitions
[0] = instr
->definitions
[0];
1336 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1337 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1339 instr
.reset(new_instr
);
1344 /* s_not_b64(cmp(a, b) -> get_inverse(cmp)(a, b) */
1345 bool combine_inverse_comparison(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1347 if (instr
->opcode
!= aco_opcode::s_not_b64
)
1349 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1351 if (!instr
->operands
[0].isTemp())
1354 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[0]);
1358 aco_opcode new_opcode
= get_inverse(cmp
->opcode
);
1359 if (new_opcode
== aco_opcode::last_opcode
)
1362 if (cmp
->operands
[0].isTemp())
1363 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1364 if (cmp
->operands
[1].isTemp())
1365 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1366 decrease_uses(ctx
, cmp
);
1368 Instruction
*new_instr
;
1369 if (cmp
->isVOP3()) {
1370 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_opcode
, asVOP3(Format::VOPC
), 2, 1);
1371 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1372 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1373 memcpy(new_vop3
->opsel
, cmp_vop3
->opsel
, sizeof(new_vop3
->opsel
));
1374 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1375 new_vop3
->clamp
= cmp_vop3
->clamp
;
1376 new_vop3
->omod
= cmp_vop3
->omod
;
1377 new_instr
= new_vop3
;
1379 new_instr
= create_instruction
<VOPC_instruction
>(new_opcode
, Format::VOPC
, 2, 1);
1381 new_instr
->operands
[0] = cmp
->operands
[0];
1382 new_instr
->operands
[1] = cmp
->operands
[1];
1383 new_instr
->definitions
[0] = instr
->definitions
[0];
1385 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1386 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1388 instr
.reset(new_instr
);
1393 /* op1(op2(1, 2), 0) if swap = false
1394 * op1(0, op2(1, 2)) if swap = true */
1395 bool match_op3_for_vop3(opt_ctx
&ctx
, aco_opcode op1
, aco_opcode op2
,
1396 Instruction
* op1_instr
, bool swap
, const char *shuffle_str
,
1397 Operand operands
[3], bool neg
[3], bool abs
[3], bool opsel
[3],
1398 bool *op1_clamp
, unsigned *op1_omod
,
1399 bool *inbetween_neg
, bool *inbetween_abs
, bool *inbetween_opsel
)
1402 if (op1_instr
->opcode
!= op1
)
1405 Instruction
*op2_instr
= follow_operand(ctx
, op1_instr
->operands
[swap
]);
1406 if (!op2_instr
|| op2_instr
->opcode
!= op2
)
1409 VOP3A_instruction
*op1_vop3
= op1_instr
->isVOP3() ? static_cast<VOP3A_instruction
*>(op1_instr
) : NULL
;
1410 VOP3A_instruction
*op2_vop3
= op2_instr
->isVOP3() ? static_cast<VOP3A_instruction
*>(op2_instr
) : NULL
;
1412 /* don't support inbetween clamp/omod */
1413 if (op2_vop3
&& (op2_vop3
->clamp
|| op2_vop3
->omod
))
1416 /* get operands and modifiers and check inbetween modifiers */
1417 *op1_clamp
= op1_vop3
? op1_vop3
->clamp
: false;
1418 *op1_omod
= op1_vop3
? op1_vop3
->omod
: 0u;
1421 *inbetween_neg
= op1_vop3
? op1_vop3
->neg
[swap
] : false;
1422 else if (op1_vop3
&& op1_vop3
->neg
[swap
])
1426 *inbetween_abs
= op1_vop3
? op1_vop3
->abs
[swap
] : false;
1427 else if (op1_vop3
&& op1_vop3
->abs
[swap
])
1430 if (inbetween_opsel
)
1431 *inbetween_opsel
= op1_vop3
? op1_vop3
->opsel
[swap
] : false;
1432 else if (op1_vop3
&& op1_vop3
->opsel
[swap
])
1436 shuffle
[shuffle_str
[0] - '0'] = 0;
1437 shuffle
[shuffle_str
[1] - '0'] = 1;
1438 shuffle
[shuffle_str
[2] - '0'] = 2;
1440 operands
[shuffle
[0]] = op1_instr
->operands
[!swap
];
1441 neg
[shuffle
[0]] = op1_vop3
? op1_vop3
->neg
[!swap
] : false;
1442 abs
[shuffle
[0]] = op1_vop3
? op1_vop3
->abs
[!swap
] : false;
1443 opsel
[shuffle
[0]] = op1_vop3
? op1_vop3
->opsel
[!swap
] : false;
1445 for (unsigned i
= 0; i
< 2; i
++) {
1446 operands
[shuffle
[i
+ 1]] = op2_instr
->operands
[i
];
1447 neg
[shuffle
[i
+ 1]] = op2_vop3
? op2_vop3
->neg
[i
] : false;
1448 abs
[shuffle
[i
+ 1]] = op2_vop3
? op2_vop3
->abs
[i
] : false;
1449 opsel
[shuffle
[i
+ 1]] = op2_vop3
? op2_vop3
->opsel
[i
] : false;
1452 /* check operands */
1453 unsigned sgpr_id
= 0;
1454 for (unsigned i
= 0; i
< 3; i
++) {
1455 Operand op
= operands
[i
];
1456 if (op
.isLiteral()) {
1458 } else if (op
.isTemp() && op
.getTemp().type() == RegType::sgpr
) {
1459 if (sgpr_id
&& sgpr_id
!= op
.tempId())
1461 sgpr_id
= op
.tempId();
1468 void create_vop3_for_op3(opt_ctx
& ctx
, aco_opcode opcode
, aco_ptr
<Instruction
>& instr
,
1469 Operand operands
[3], bool neg
[3], bool abs
[3], bool opsel
[3],
1470 bool clamp
, unsigned omod
)
1472 VOP3A_instruction
*new_instr
= create_instruction
<VOP3A_instruction
>(opcode
, Format::VOP3A
, 3, 1);
1473 memcpy(new_instr
->abs
, abs
, sizeof(bool[3]));
1474 memcpy(new_instr
->opsel
, opsel
, sizeof(bool[3]));
1475 memcpy(new_instr
->neg
, neg
, sizeof(bool[3]));
1476 new_instr
->clamp
= clamp
;
1477 new_instr
->omod
= omod
;
1478 new_instr
->operands
[0] = operands
[0];
1479 new_instr
->operands
[1] = operands
[1];
1480 new_instr
->operands
[2] = operands
[2];
1481 new_instr
->definitions
[0] = instr
->definitions
[0];
1482 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1484 instr
.reset(new_instr
);
1487 bool combine_three_valu_op(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
, aco_opcode op2
, aco_opcode new_op
, const char *shuffle
, uint8_t ops
)
1489 uint32_t omod_clamp
= ctx
.info
[instr
->definitions
[0].tempId()].label
&
1490 (label_omod_success
| label_clamp_success
);
1492 for (unsigned swap
= 0; swap
< 2; swap
++) {
1493 if (!((1 << swap
) & ops
))
1496 Operand operands
[3];
1497 bool neg
[3], abs
[3], opsel
[3], clamp
;
1499 if (match_op3_for_vop3(ctx
, instr
->opcode
, op2
,
1500 instr
.get(), swap
, shuffle
,
1501 operands
, neg
, abs
, opsel
,
1502 &clamp
, &omod
, NULL
, NULL
, NULL
)) {
1503 ctx
.uses
[instr
->operands
[swap
].tempId()]--;
1504 create_vop3_for_op3(ctx
, new_op
, instr
, operands
, neg
, abs
, opsel
, clamp
, omod
);
1505 if (omod_clamp
& label_omod_success
)
1506 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1507 if (omod_clamp
& label_clamp_success
)
1508 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
1515 /* s_not_b32(s_and_b32(a, b)) -> s_nand_b32(a, b)
1516 * s_not_b32(s_or_b32(a, b)) -> s_nor_b32(a, b)
1517 * s_not_b32(s_xor_b32(a, b)) -> s_xnor_b32(a, b)
1518 * s_not_b64(s_and_b64(a, b)) -> s_nand_b64(a, b)
1519 * s_not_b64(s_or_b64(a, b)) -> s_nor_b64(a, b)
1520 * s_not_b64(s_xor_b64(a, b)) -> s_xnor_b64(a, b) */
1521 bool combine_salu_not_bitwise(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1524 if (!instr
->operands
[0].isTemp())
1526 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1529 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[0]);
1532 switch (op2_instr
->opcode
) {
1533 case aco_opcode::s_and_b32
:
1534 case aco_opcode::s_or_b32
:
1535 case aco_opcode::s_xor_b32
:
1536 case aco_opcode::s_and_b64
:
1537 case aco_opcode::s_or_b64
:
1538 case aco_opcode::s_xor_b64
:
1544 /* create instruction */
1545 std::swap(instr
->definitions
[0], op2_instr
->definitions
[0]);
1546 ctx
.uses
[instr
->operands
[0].tempId()]--;
1547 ctx
.info
[op2_instr
->definitions
[0].tempId()].label
= 0;
1549 switch (op2_instr
->opcode
) {
1550 case aco_opcode::s_and_b32
:
1551 op2_instr
->opcode
= aco_opcode::s_nand_b32
;
1553 case aco_opcode::s_or_b32
:
1554 op2_instr
->opcode
= aco_opcode::s_nor_b32
;
1556 case aco_opcode::s_xor_b32
:
1557 op2_instr
->opcode
= aco_opcode::s_xnor_b32
;
1559 case aco_opcode::s_and_b64
:
1560 op2_instr
->opcode
= aco_opcode::s_nand_b64
;
1562 case aco_opcode::s_or_b64
:
1563 op2_instr
->opcode
= aco_opcode::s_nor_b64
;
1565 case aco_opcode::s_xor_b64
:
1566 op2_instr
->opcode
= aco_opcode::s_xnor_b64
;
1575 /* s_and_b32(a, s_not_b32(b)) -> s_andn2_b32(a, b)
1576 * s_or_b32(a, s_not_b32(b)) -> s_orn2_b32(a, b)
1577 * s_and_b64(a, s_not_b64(b)) -> s_andn2_b64(a, b)
1578 * s_or_b64(a, s_not_b64(b)) -> s_orn2_b64(a, b) */
1579 bool combine_salu_n2(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1581 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1584 for (unsigned i
= 0; i
< 2; i
++) {
1585 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[i
]);
1586 if (!op2_instr
|| (op2_instr
->opcode
!= aco_opcode::s_not_b32
&& op2_instr
->opcode
!= aco_opcode::s_not_b64
))
1589 ctx
.uses
[instr
->operands
[i
].tempId()]--;
1590 instr
->operands
[0] = instr
->operands
[!i
];
1591 instr
->operands
[1] = op2_instr
->operands
[0];
1592 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1594 switch (instr
->opcode
) {
1595 case aco_opcode::s_and_b32
:
1596 instr
->opcode
= aco_opcode::s_andn2_b32
;
1598 case aco_opcode::s_or_b32
:
1599 instr
->opcode
= aco_opcode::s_orn2_b32
;
1601 case aco_opcode::s_and_b64
:
1602 instr
->opcode
= aco_opcode::s_andn2_b64
;
1604 case aco_opcode::s_or_b64
:
1605 instr
->opcode
= aco_opcode::s_orn2_b64
;
1616 /* s_add_{i32,u32}(a, s_lshl_b32(b, <n>)) -> s_lshl<n>_add_u32(a, b) */
1617 bool combine_salu_lshl_add(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1619 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1622 for (unsigned i
= 0; i
< 2; i
++) {
1623 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[i
]);
1624 if (!op2_instr
|| op2_instr
->opcode
!= aco_opcode::s_lshl_b32
|| !op2_instr
->operands
[1].isConstant())
1627 uint32_t shift
= op2_instr
->operands
[1].constantValue();
1628 if (shift
< 1 || shift
> 4)
1631 ctx
.uses
[instr
->operands
[i
].tempId()]--;
1632 instr
->operands
[1] = instr
->operands
[!i
];
1633 instr
->operands
[0] = op2_instr
->operands
[0];
1634 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1636 instr
->opcode
= ((aco_opcode
[]){aco_opcode::s_lshl1_add_u32
,
1637 aco_opcode::s_lshl2_add_u32
,
1638 aco_opcode::s_lshl3_add_u32
,
1639 aco_opcode::s_lshl4_add_u32
})[shift
- 1];
1646 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
)
1649 #define MINMAX(type, gfx9) \
1650 case aco_opcode::v_min_##type:\
1651 case aco_opcode::v_max_##type:\
1652 case aco_opcode::v_med3_##type:\
1653 *min = aco_opcode::v_min_##type;\
1654 *max = aco_opcode::v_max_##type;\
1655 *med3 = aco_opcode::v_med3_##type;\
1656 *min3 = aco_opcode::v_min3_##type;\
1657 *max3 = aco_opcode::v_max3_##type;\
1658 *some_gfx9_only = gfx9;\
1672 /* 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
1673 * 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 */
1674 bool combine_clamp(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
,
1675 aco_opcode min
, aco_opcode max
, aco_opcode med
)
1677 aco_opcode other_op
;
1678 if (instr
->opcode
== min
)
1680 else if (instr
->opcode
== max
)
1685 uint32_t omod_clamp
= ctx
.info
[instr
->definitions
[0].tempId()].label
&
1686 (label_omod_success
| label_clamp_success
);
1688 for (unsigned swap
= 0; swap
< 2; swap
++) {
1689 Operand operands
[3];
1690 bool neg
[3], abs
[3], opsel
[3], clamp
, inbetween_neg
, inbetween_abs
;
1692 if (match_op3_for_vop3(ctx
, instr
->opcode
, other_op
, instr
.get(), swap
,
1693 "012", operands
, neg
, abs
, opsel
,
1694 &clamp
, &omod
, &inbetween_neg
, &inbetween_abs
, NULL
)) {
1695 int const0_idx
= -1, const1_idx
= -1;
1696 uint32_t const0
= 0, const1
= 0;
1697 for (int i
= 0; i
< 3; i
++) {
1699 if (operands
[i
].isConstant()) {
1700 val
= operands
[i
].constantValue();
1701 } else if (operands
[i
].isTemp() && ctx
.uses
[operands
[i
].tempId()] == 1 &&
1702 ctx
.info
[operands
[i
].tempId()].is_constant_or_literal()) {
1703 val
= ctx
.info
[operands
[i
].tempId()].val
;
1707 if (const0_idx
>= 0) {
1715 if (const0_idx
< 0 || const1_idx
< 0)
1718 if (opsel
[const0_idx
])
1720 if (opsel
[const1_idx
])
1723 int lower_idx
= const0_idx
;
1725 case aco_opcode::v_min_f32
:
1726 case aco_opcode::v_min_f16
: {
1727 float const0_f
, const1_f
;
1728 if (min
== aco_opcode::v_min_f32
) {
1729 memcpy(&const0_f
, &const0
, 4);
1730 memcpy(&const1_f
, &const1
, 4);
1732 const0_f
= _mesa_half_to_float(const0
);
1733 const1_f
= _mesa_half_to_float(const1
);
1735 if (abs
[const0_idx
]) const0_f
= fabsf(const0_f
);
1736 if (abs
[const1_idx
]) const1_f
= fabsf(const1_f
);
1737 if (neg
[const0_idx
]) const0_f
= -const0_f
;
1738 if (neg
[const1_idx
]) const1_f
= -const1_f
;
1739 lower_idx
= const0_f
< const1_f
? const0_idx
: const1_idx
;
1742 case aco_opcode::v_min_u32
: {
1743 lower_idx
= const0
< const1
? const0_idx
: const1_idx
;
1746 case aco_opcode::v_min_u16
: {
1747 lower_idx
= (uint16_t)const0
< (uint16_t)const1
? const0_idx
: const1_idx
;
1750 case aco_opcode::v_min_i32
: {
1751 int32_t const0_i
= const0
& 0x80000000u
? -2147483648 + (int32_t)(const0
& 0x7fffffffu
) : const0
;
1752 int32_t const1_i
= const1
& 0x80000000u
? -2147483648 + (int32_t)(const1
& 0x7fffffffu
) : const1
;
1753 lower_idx
= const0_i
< const1_i
? const0_idx
: const1_idx
;
1756 case aco_opcode::v_min_i16
: {
1757 int16_t const0_i
= const0
& 0x8000u
? -32768 + (int16_t)(const0
& 0x7fffu
) : const0
;
1758 int16_t const1_i
= const1
& 0x8000u
? -32768 + (int16_t)(const1
& 0x7fffu
) : const1
;
1759 lower_idx
= const0_i
< const1_i
? const0_idx
: const1_idx
;
1765 int upper_idx
= lower_idx
== const0_idx
? const1_idx
: const0_idx
;
1767 if (instr
->opcode
== min
) {
1768 if (upper_idx
!= 0 || lower_idx
== 0)
1771 if (upper_idx
== 0 || lower_idx
!= 0)
1775 neg
[1] ^= inbetween_neg
;
1776 neg
[2] ^= inbetween_neg
;
1777 abs
[1] |= inbetween_abs
;
1778 abs
[2] |= inbetween_abs
;
1780 ctx
.uses
[instr
->operands
[swap
].tempId()]--;
1781 create_vop3_for_op3(ctx
, med
, instr
, operands
, neg
, abs
, opsel
, clamp
, omod
);
1782 if (omod_clamp
& label_omod_success
)
1783 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1784 if (omod_clamp
& label_clamp_success
)
1785 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
1795 void apply_sgprs(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1798 uint32_t sgpr_idx
= 0;
1799 uint32_t sgpr_info_id
= 0;
1800 bool has_sgpr
= false;
1801 uint32_t sgpr_ssa_id
= 0;
1802 /* find 'best' possible sgpr */
1803 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
1805 if (instr
->operands
[i
].isLiteral()) {
1809 if (!instr
->operands
[i
].isTemp())
1811 if (instr
->operands
[i
].getTemp().type() == RegType::sgpr
) {
1813 sgpr_ssa_id
= instr
->operands
[i
].tempId();
1816 ssa_info
& info
= ctx
.info
[instr
->operands
[i
].tempId()];
1817 if (info
.is_temp() && info
.temp
.type() == RegType::sgpr
) {
1818 uint16_t uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
1819 if (sgpr_info_id
== 0 || uses
< ctx
.uses
[sgpr_info_id
]) {
1821 sgpr_info_id
= instr
->operands
[i
].tempId();
1825 if (!has_sgpr
&& sgpr_info_id
!= 0) {
1826 ssa_info
& info
= ctx
.info
[sgpr_info_id
];
1827 if (sgpr_idx
== 0 || instr
->isVOP3()) {
1828 instr
->operands
[sgpr_idx
] = Operand(info
.temp
);
1829 ctx
.uses
[sgpr_info_id
]--;
1830 ctx
.uses
[info
.temp
.id()]++;
1831 } else if (can_swap_operands(instr
)) {
1832 instr
->operands
[sgpr_idx
] = instr
->operands
[0];
1833 instr
->operands
[0] = Operand(info
.temp
);
1834 ctx
.uses
[sgpr_info_id
]--;
1835 ctx
.uses
[info
.temp
.id()]++;
1836 } else if (can_use_VOP3(instr
)) {
1837 to_VOP3(ctx
, instr
);
1838 instr
->operands
[sgpr_idx
] = Operand(info
.temp
);
1839 ctx
.uses
[sgpr_info_id
]--;
1840 ctx
.uses
[info
.temp
.id()]++;
1843 /* we can have two sgprs on one instruction if it is the same sgpr! */
1844 } else if (sgpr_info_id
!= 0 &&
1845 sgpr_ssa_id
== sgpr_info_id
&&
1846 ctx
.uses
[sgpr_info_id
] == 1 &&
1847 can_use_VOP3(instr
)) {
1848 to_VOP3(ctx
, instr
);
1849 instr
->operands
[sgpr_idx
] = Operand(ctx
.info
[sgpr_info_id
].temp
);
1850 ctx
.uses
[sgpr_info_id
]--;
1851 ctx
.uses
[ctx
.info
[sgpr_info_id
].temp
.id()]++;
1855 bool apply_omod_clamp(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1857 /* check if we could apply omod on predecessor */
1858 if (instr
->opcode
== aco_opcode::v_mul_f32
) {
1859 if (instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_omod_success()) {
1861 /* omod was successfully applied */
1862 /* if the omod instruction is v_mad, we also have to change the original add */
1863 if (ctx
.info
[instr
->operands
[1].tempId()].is_mad()) {
1864 Instruction
* add_instr
= ctx
.mad_infos
[ctx
.info
[instr
->operands
[1].tempId()].val
].add_instr
.get();
1865 if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp())
1866 static_cast<VOP3A_instruction
*>(add_instr
)->clamp
= true;
1867 add_instr
->definitions
[0] = instr
->definitions
[0];
1870 Instruction
* omod_instr
= ctx
.info
[instr
->operands
[1].tempId()].instr
;
1871 /* check if we have an additional clamp modifier */
1872 if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp() && ctx
.uses
[instr
->definitions
[0].tempId()] == 1) {
1873 static_cast<VOP3A_instruction
*>(omod_instr
)->clamp
= true;
1874 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(omod_instr
);
1876 /* change definition ssa-id of modified instruction */
1877 omod_instr
->definitions
[0] = instr
->definitions
[0];
1879 /* change the definition of instr to something unused, e.g. the original omod def */
1880 instr
->definitions
[0] = Definition(instr
->operands
[1].getTemp());
1881 ctx
.uses
[instr
->definitions
[0].tempId()] = 0;
1884 if (!ctx
.info
[instr
->definitions
[0].tempId()].label
) {
1885 /* in all other cases, label this instruction as option for multiply-add */
1886 ctx
.info
[instr
->definitions
[0].tempId()].set_mul(instr
.get());
1890 /* check if we could apply clamp on predecessor */
1891 if (instr
->opcode
== aco_opcode::v_med3_f32
) {
1893 bool found_zero
= false, found_one
= false;
1894 for (unsigned i
= 0; i
< 3; i
++)
1896 if (instr
->operands
[i
].constantEquals(0))
1898 else if (instr
->operands
[i
].constantEquals(0x3f800000)) /* 1.0 */
1903 if (found_zero
&& found_one
&& instr
->operands
[idx
].isTemp() &&
1904 ctx
.info
[instr
->operands
[idx
].tempId()].is_clamp_success()) {
1905 /* clamp was successfully applied */
1906 /* if the clamp instruction is v_mad, we also have to change the original add */
1907 if (ctx
.info
[instr
->operands
[idx
].tempId()].is_mad()) {
1908 Instruction
* add_instr
= ctx
.mad_infos
[ctx
.info
[instr
->operands
[idx
].tempId()].val
].add_instr
.get();
1909 add_instr
->definitions
[0] = instr
->definitions
[0];
1911 Instruction
* clamp_instr
= ctx
.info
[instr
->operands
[idx
].tempId()].instr
;
1912 /* change definition ssa-id of modified instruction */
1913 clamp_instr
->definitions
[0] = instr
->definitions
[0];
1915 /* change the definition of instr to something unused, e.g. the original omod def */
1916 instr
->definitions
[0] = Definition(instr
->operands
[idx
].getTemp());
1917 ctx
.uses
[instr
->definitions
[0].tempId()] = 0;
1922 /* apply omod / clamp modifiers if the def is used only once and the instruction can have modifiers */
1923 if (!instr
->definitions
.empty() && ctx
.uses
[instr
->definitions
[0].tempId()] == 1 &&
1924 can_use_VOP3(instr
) && instr_info
.can_use_output_modifiers
[(int)instr
->opcode
]) {
1925 if(ctx
.info
[instr
->definitions
[0].tempId()].is_omod2()) {
1926 to_VOP3(ctx
, instr
);
1927 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 1;
1928 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1929 } else if (ctx
.info
[instr
->definitions
[0].tempId()].is_omod4()) {
1930 to_VOP3(ctx
, instr
);
1931 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 2;
1932 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1933 } else if (ctx
.info
[instr
->definitions
[0].tempId()].is_omod5()) {
1934 to_VOP3(ctx
, instr
);
1935 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 3;
1936 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1937 } else if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp()) {
1938 to_VOP3(ctx
, instr
);
1939 static_cast<VOP3A_instruction
*>(instr
.get())->clamp
= true;
1940 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
1947 // TODO: we could possibly move the whole label_instruction pass to combine_instruction:
1948 // this would mean that we'd have to fix the instruction uses while value propagation
1950 void combine_instruction(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1952 if (instr
->definitions
.empty() || !ctx
.uses
[instr
->definitions
[0].tempId()])
1955 if (instr
->isVALU()) {
1956 if (can_apply_sgprs(instr
))
1957 apply_sgprs(ctx
, instr
);
1958 if (apply_omod_clamp(ctx
, instr
))
1962 /* TODO: There are still some peephole optimizations that could be done:
1963 * - abs(a - b) -> s_absdiff_i32
1964 * - various patterns for s_bitcmp{0,1}_b32 and s_bitset{0,1}_b32
1965 * - patterns for v_alignbit_b32 and v_alignbyte_b32
1966 * These aren't probably too interesting though.
1967 * There are also patterns for v_cmp_class_f{16,32,64}. This is difficult but
1968 * probably more useful than the previously mentioned optimizations.
1969 * The various comparison optimizations also currently only work with 32-bit
1972 /* neg(mul(a, b)) -> mul(neg(a), b) */
1973 if (ctx
.info
[instr
->definitions
[0].tempId()].is_neg() && ctx
.uses
[instr
->operands
[1].tempId()] == 1) {
1974 Temp val
= ctx
.info
[instr
->definitions
[0].tempId()].temp
;
1976 if (!ctx
.info
[val
.id()].is_mul())
1979 Instruction
* mul_instr
= ctx
.info
[val
.id()].instr
;
1981 if (mul_instr
->operands
[0].isLiteral())
1983 if (mul_instr
->isVOP3() && static_cast<VOP3A_instruction
*>(mul_instr
)->clamp
)
1986 /* convert to mul(neg(a), b) */
1987 ctx
.uses
[mul_instr
->definitions
[0].tempId()]--;
1988 Definition def
= instr
->definitions
[0];
1989 /* neg(abs(mul(a, b))) -> mul(neg(abs(a)), abs(b)) */
1990 bool is_abs
= ctx
.info
[instr
->definitions
[0].tempId()].is_abs();
1991 instr
.reset(create_instruction
<VOP3A_instruction
>(aco_opcode::v_mul_f32
, asVOP3(Format::VOP2
), 2, 1));
1992 instr
->operands
[0] = mul_instr
->operands
[0];
1993 instr
->operands
[1] = mul_instr
->operands
[1];
1994 instr
->definitions
[0] = def
;
1995 VOP3A_instruction
* new_mul
= static_cast<VOP3A_instruction
*>(instr
.get());
1996 if (mul_instr
->isVOP3()) {
1997 VOP3A_instruction
* mul
= static_cast<VOP3A_instruction
*>(mul_instr
);
1998 new_mul
->neg
[0] = mul
->neg
[0] && !is_abs
;
1999 new_mul
->neg
[1] = mul
->neg
[1] && !is_abs
;
2000 new_mul
->abs
[0] = mul
->abs
[0] || is_abs
;
2001 new_mul
->abs
[1] = mul
->abs
[1] || is_abs
;
2002 new_mul
->omod
= mul
->omod
;
2004 new_mul
->neg
[0] ^= true;
2005 new_mul
->clamp
= false;
2007 ctx
.info
[instr
->definitions
[0].tempId()].set_mul(instr
.get());
2010 /* combine mul+add -> mad */
2011 else if (instr
->opcode
== aco_opcode::v_add_f32
||
2012 instr
->opcode
== aco_opcode::v_sub_f32
||
2013 instr
->opcode
== aco_opcode::v_subrev_f32
) {
2015 uint32_t uses_src0
= UINT32_MAX
;
2016 uint32_t uses_src1
= UINT32_MAX
;
2017 Instruction
* mul_instr
= nullptr;
2018 unsigned add_op_idx
;
2019 /* check if any of the operands is a multiplication */
2020 if (instr
->operands
[0].isTemp() && ctx
.info
[instr
->operands
[0].tempId()].is_mul())
2021 uses_src0
= ctx
.uses
[instr
->operands
[0].tempId()];
2022 if (instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_mul())
2023 uses_src1
= ctx
.uses
[instr
->operands
[1].tempId()];
2025 /* find the 'best' mul instruction to combine with the add */
2026 if (uses_src0
< uses_src1
) {
2027 mul_instr
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
2029 } else if (uses_src1
< uses_src0
) {
2030 mul_instr
= ctx
.info
[instr
->operands
[1].tempId()].instr
;
2032 } else if (uses_src0
!= UINT32_MAX
) {
2033 /* tiebreaker: quite random what to pick */
2034 if (ctx
.info
[instr
->operands
[0].tempId()].instr
->operands
[0].isLiteral()) {
2035 mul_instr
= ctx
.info
[instr
->operands
[1].tempId()].instr
;
2038 mul_instr
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
2043 Operand op
[3] = {Operand(v1
), Operand(v1
), Operand(v1
)};
2044 bool neg
[3] = {false, false, false};
2045 bool abs
[3] = {false, false, false};
2048 bool need_vop3
= false;
2050 op
[0] = mul_instr
->operands
[0];
2051 op
[1] = mul_instr
->operands
[1];
2052 op
[2] = instr
->operands
[add_op_idx
];
2053 for (unsigned i
= 0; i
< 3; i
++)
2055 if (op
[i
].isLiteral())
2057 if (op
[i
].isTemp() && op
[i
].getTemp().type() == RegType::sgpr
)
2059 if (!(i
== 0 || (op
[i
].isTemp() && op
[i
].getTemp().type() == RegType::vgpr
)))
2062 // TODO: would be better to check this before selecting a mul instr?
2066 if (mul_instr
->isVOP3()) {
2067 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*> (mul_instr
);
2068 neg
[0] = vop3
->neg
[0];
2069 neg
[1] = vop3
->neg
[1];
2070 abs
[0] = vop3
->abs
[0];
2071 abs
[1] = vop3
->abs
[1];
2073 /* we cannot use these modifiers between mul and add */
2074 if (vop3
->clamp
|| vop3
->omod
)
2078 /* convert to mad */
2079 ctx
.uses
[mul_instr
->definitions
[0].tempId()]--;
2080 if (ctx
.uses
[mul_instr
->definitions
[0].tempId()]) {
2082 ctx
.uses
[op
[0].tempId()]++;
2084 ctx
.uses
[op
[1].tempId()]++;
2087 if (instr
->isVOP3()) {
2088 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*> (instr
.get());
2089 neg
[2] = vop3
->neg
[add_op_idx
];
2090 abs
[2] = vop3
->abs
[add_op_idx
];
2092 clamp
= vop3
->clamp
;
2093 /* abs of the multiplication result */
2094 if (vop3
->abs
[1 - add_op_idx
]) {
2100 /* neg of the multiplication result */
2101 neg
[1] = neg
[1] ^ vop3
->neg
[1 - add_op_idx
];
2104 if (instr
->opcode
== aco_opcode::v_sub_f32
) {
2105 neg
[1 + add_op_idx
] = neg
[1 + add_op_idx
] ^ true;
2107 } else if (instr
->opcode
== aco_opcode::v_subrev_f32
) {
2108 neg
[2 - add_op_idx
] = neg
[2 - add_op_idx
] ^ true;
2112 aco_ptr
<VOP3A_instruction
> mad
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_mad_f32
, Format::VOP3A
, 3, 1)};
2113 for (unsigned i
= 0; i
< 3; i
++)
2115 mad
->operands
[i
] = op
[i
];
2116 mad
->neg
[i
] = neg
[i
];
2117 mad
->abs
[i
] = abs
[i
];
2121 mad
->definitions
[0] = instr
->definitions
[0];
2123 /* mark this ssa_def to be re-checked for profitability and literals */
2124 ctx
.mad_infos
.emplace_back(std::move(instr
), mul_instr
->definitions
[0].tempId(), need_vop3
);
2125 ctx
.info
[mad
->definitions
[0].tempId()].set_mad(mad
.get(), ctx
.mad_infos
.size() - 1);
2126 instr
.reset(mad
.release());
2130 /* v_mul_f32(v_cndmask_b32(0, 1.0, cond), a) -> v_cndmask_b32(0, a, cond) */
2131 else if (instr
->opcode
== aco_opcode::v_mul_f32
&& !instr
->isVOP3()) {
2132 for (unsigned i
= 0; i
< 2; i
++) {
2133 if (instr
->operands
[i
].isTemp() && ctx
.info
[instr
->operands
[i
].tempId()].is_b2f() &&
2134 ctx
.uses
[instr
->operands
[i
].tempId()] == 1 &&
2135 instr
->operands
[!i
].isTemp() && instr
->operands
[!i
].getTemp().type() == RegType::vgpr
) {
2136 ctx
.uses
[instr
->operands
[i
].tempId()]--;
2137 ctx
.uses
[ctx
.info
[instr
->operands
[i
].tempId()].temp
.id()]++;
2139 aco_ptr
<VOP2_instruction
> new_instr
{create_instruction
<VOP2_instruction
>(aco_opcode::v_cndmask_b32
, Format::VOP2
, 3, 1)};
2140 new_instr
->operands
[0] = Operand(0u);
2141 new_instr
->operands
[1] = instr
->operands
[!i
];
2142 new_instr
->operands
[2] = Operand(ctx
.info
[instr
->operands
[i
].tempId()].temp
);
2143 new_instr
->definitions
[0] = instr
->definitions
[0];
2144 instr
.reset(new_instr
.release());
2145 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
2149 } else if (instr
->opcode
== aco_opcode::v_or_b32
&& ctx
.program
->chip_class
>= GFX9
) {
2150 if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_or_b32
, aco_opcode::v_or3_b32
, "012", 1 | 2)) ;
2151 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_and_b32
, aco_opcode::v_and_or_b32
, "120", 1 | 2)) ;
2152 else combine_three_valu_op(ctx
, instr
, aco_opcode::v_lshlrev_b32
, aco_opcode::v_lshl_or_b32
, "210", 1 | 2);
2153 } else if (instr
->opcode
== aco_opcode::v_add_u32
&& ctx
.program
->chip_class
>= GFX9
) {
2154 if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_xor_b32
, aco_opcode::v_xad_u32
, "120", 1 | 2)) ;
2155 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_add_u32
, aco_opcode::v_add3_u32
, "012", 1 | 2)) ;
2156 else combine_three_valu_op(ctx
, instr
, aco_opcode::v_lshlrev_b32
, aco_opcode::v_lshl_add_u32
, "210", 1 | 2);
2157 } else if (instr
->opcode
== aco_opcode::v_lshlrev_b32
&& ctx
.program
->chip_class
>= GFX9
) {
2158 combine_three_valu_op(ctx
, instr
, aco_opcode::v_add_u32
, aco_opcode::v_add_lshl_u32
, "120", 2);
2159 } else if ((instr
->opcode
== aco_opcode::s_add_u32
|| instr
->opcode
== aco_opcode::s_add_i32
) && ctx
.program
->chip_class
>= GFX9
) {
2160 combine_salu_lshl_add(ctx
, instr
);
2161 } else if (instr
->opcode
== aco_opcode::s_not_b32
) {
2162 combine_salu_not_bitwise(ctx
, instr
);
2163 } else if (instr
->opcode
== aco_opcode::s_not_b64
) {
2164 if (combine_inverse_comparison(ctx
, instr
)) ;
2165 else combine_salu_not_bitwise(ctx
, instr
);
2166 } else if (instr
->opcode
== aco_opcode::s_and_b32
|| instr
->opcode
== aco_opcode::s_or_b32
) {
2167 combine_salu_n2(ctx
, instr
);
2168 } else if (instr
->opcode
== aco_opcode::s_and_b64
|| instr
->opcode
== aco_opcode::s_or_b64
) {
2169 if (combine_ordering_test(ctx
, instr
)) ;
2170 else if (combine_comparison_ordering(ctx
, instr
)) ;
2171 else if (combine_constant_comparison_ordering(ctx
, instr
)) ;
2172 else combine_salu_n2(ctx
, instr
);
2174 aco_opcode min
, max
, min3
, max3
, med3
;
2175 bool some_gfx9_only
;
2176 if (get_minmax_info(instr
->opcode
, &min
, &max
, &min3
, &max3
, &med3
, &some_gfx9_only
) &&
2177 (!some_gfx9_only
|| ctx
.program
->chip_class
>= GFX9
)) {
2178 if (combine_three_valu_op(ctx
, instr
, instr
->opcode
, instr
->opcode
== min
? min3
: max3
, "012", 1 | 2));
2179 else combine_clamp(ctx
, instr
, min
, max
, med3
);
2185 void select_instruction(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
2187 const uint32_t threshold
= 4;
2189 /* Dead Code Elimination:
2190 * We remove instructions if they define temporaries which all are unused */
2191 const bool is_used
= instr
->definitions
.empty() ||
2192 std::any_of(instr
->definitions
.begin(), instr
->definitions
.end(),
2193 [&ctx
](const Definition
& def
) { return ctx
.uses
[def
.tempId()]; });
2199 /* convert split_vector into extract_vector if only one definition is ever used */
2200 if (instr
->opcode
== aco_opcode::p_split_vector
) {
2201 unsigned num_used
= 0;
2203 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
2204 if (ctx
.uses
[instr
->definitions
[i
].tempId()]) {
2209 if (num_used
== 1) {
2210 aco_ptr
<Pseudo_instruction
> extract
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_extract_vector
, Format::PSEUDO
, 2, 1)};
2211 extract
->operands
[0] = instr
->operands
[0];
2212 extract
->operands
[1] = Operand((uint32_t) idx
);
2213 extract
->definitions
[0] = instr
->definitions
[idx
];
2214 instr
.reset(extract
.release());
2218 /* re-check mad instructions */
2219 if (instr
->opcode
== aco_opcode::v_mad_f32
&& ctx
.info
[instr
->definitions
[0].tempId()].is_mad()) {
2220 mad_info
* info
= &ctx
.mad_infos
[ctx
.info
[instr
->definitions
[0].tempId()].val
];
2221 /* first, check profitability */
2222 if (ctx
.uses
[info
->mul_temp_id
]) {
2223 ctx
.uses
[info
->mul_temp_id
]++;
2224 instr
.swap(info
->add_instr
);
2226 /* second, check possible literals */
2227 } else if (!info
->needs_vop3
) {
2228 uint32_t literal_idx
= 0;
2229 uint32_t literal_uses
= UINT32_MAX
;
2230 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
2232 if (!instr
->operands
[i
].isTemp())
2234 /* if one of the operands is sgpr, we cannot add a literal somewhere else */
2235 if (instr
->operands
[i
].getTemp().type() == RegType::sgpr
) {
2236 if (ctx
.info
[instr
->operands
[i
].tempId()].is_literal()) {
2237 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2240 literal_uses
= UINT32_MAX
;
2244 else if (ctx
.info
[instr
->operands
[i
].tempId()].is_literal() &&
2245 ctx
.uses
[instr
->operands
[i
].tempId()] < literal_uses
) {
2246 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2250 if (literal_uses
< threshold
) {
2251 ctx
.uses
[instr
->operands
[literal_idx
].tempId()]--;
2252 info
->check_literal
= true;
2253 info
->literal_idx
= literal_idx
;
2259 /* check for literals */
2260 /* we do not apply the literals yet as we don't know if it is profitable */
2261 if (instr
->isSALU()) {
2262 uint32_t literal_idx
= 0;
2263 uint32_t literal_uses
= UINT32_MAX
;
2264 bool has_literal
= false;
2265 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
2267 if (instr
->operands
[i
].isLiteral()) {
2271 if (!instr
->operands
[i
].isTemp())
2273 if (ctx
.info
[instr
->operands
[i
].tempId()].is_literal() &&
2274 ctx
.uses
[instr
->operands
[i
].tempId()] < literal_uses
) {
2275 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2279 if (!has_literal
&& literal_uses
< threshold
) {
2280 ctx
.uses
[instr
->operands
[literal_idx
].tempId()]--;
2281 if (ctx
.uses
[instr
->operands
[literal_idx
].tempId()] == 0)
2282 instr
->operands
[literal_idx
] = Operand(ctx
.info
[instr
->operands
[literal_idx
].tempId()].val
);
2284 } else if (instr
->isVALU() && valu_can_accept_literal(ctx
, instr
, 0) &&
2285 instr
->operands
[0].isTemp() &&
2286 ctx
.info
[instr
->operands
[0].tempId()].is_literal() &&
2287 ctx
.uses
[instr
->operands
[0].tempId()] < threshold
) {
2288 ctx
.uses
[instr
->operands
[0].tempId()]--;
2289 if (ctx
.uses
[instr
->operands
[0].tempId()] == 0)
2290 instr
->operands
[0] = Operand(ctx
.info
[instr
->operands
[0].tempId()].val
);
2296 void apply_literals(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
2298 /* Cleanup Dead Instructions */
2302 /* apply literals on SALU */
2303 if (instr
->isSALU()) {
2304 for (Operand
& op
: instr
->operands
) {
2309 if (ctx
.info
[op
.tempId()].is_literal() &&
2310 ctx
.uses
[op
.tempId()] == 0)
2311 op
= Operand(ctx
.info
[op
.tempId()].val
);
2315 /* apply literals on VALU */
2316 else if (instr
->isVALU() && !instr
->isVOP3() &&
2317 instr
->operands
[0].isTemp() &&
2318 ctx
.info
[instr
->operands
[0].tempId()].is_literal() &&
2319 ctx
.uses
[instr
->operands
[0].tempId()] == 0) {
2320 instr
->operands
[0] = Operand(ctx
.info
[instr
->operands
[0].tempId()].val
);
2323 /* apply literals on MAD */
2324 else if (instr
->opcode
== aco_opcode::v_mad_f32
&& ctx
.info
[instr
->definitions
[0].tempId()].is_mad()) {
2325 mad_info
* info
= &ctx
.mad_infos
[ctx
.info
[instr
->definitions
[0].tempId()].val
];
2326 aco_ptr
<Instruction
> new_mad
;
2327 if (info
->check_literal
&& ctx
.uses
[instr
->operands
[info
->literal_idx
].tempId()] == 0) {
2328 if (info
->literal_idx
== 2) { /* add literal -> madak */
2329 new_mad
.reset(create_instruction
<VOP2_instruction
>(aco_opcode::v_madak_f32
, Format::VOP2
, 3, 1));
2330 new_mad
->operands
[0] = instr
->operands
[0];
2331 new_mad
->operands
[1] = instr
->operands
[1];
2332 } else { /* mul literal -> madmk */
2333 new_mad
.reset(create_instruction
<VOP2_instruction
>(aco_opcode::v_madmk_f32
, Format::VOP2
, 3, 1));
2334 new_mad
->operands
[0] = instr
->operands
[1 - info
->literal_idx
];
2335 new_mad
->operands
[1] = instr
->operands
[2];
2337 new_mad
->operands
[2] = Operand(ctx
.info
[instr
->operands
[info
->literal_idx
].tempId()].val
);
2338 new_mad
->definitions
[0] = instr
->definitions
[0];
2339 instr
.swap(new_mad
);
2343 ctx
.instructions
.emplace_back(std::move(instr
));
2347 void optimize(Program
* program
)
2350 ctx
.program
= program
;
2351 std::vector
<ssa_info
> info(program
->peekAllocationId());
2352 ctx
.info
= info
.data();
2354 /* 1. Bottom-Up DAG pass (forward) to label all ssa-defs */
2355 for (Block
& block
: program
->blocks
) {
2356 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2357 label_instruction(ctx
, instr
);
2360 ctx
.uses
= std::move(dead_code_analysis(program
));
2362 /* 2. Combine v_mad, omod, clamp and propagate sgpr on VALU instructions */
2363 for (Block
& block
: program
->blocks
) {
2364 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2365 combine_instruction(ctx
, instr
);
2368 /* 3. Top-Down DAG pass (backward) to select instructions (includes DCE) */
2369 for (std::vector
<Block
>::reverse_iterator it
= program
->blocks
.rbegin(); it
!= program
->blocks
.rend(); ++it
) {
2370 Block
* block
= &(*it
);
2371 for (std::vector
<aco_ptr
<Instruction
>>::reverse_iterator it
= block
->instructions
.rbegin(); it
!= block
->instructions
.rend(); ++it
)
2372 select_instruction(ctx
, *it
);
2375 /* 4. Add literals to instructions */
2376 for (Block
& block
: program
->blocks
) {
2377 ctx
.instructions
.clear();
2378 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2379 apply_literals(ctx
, instr
);
2380 block
.instructions
.swap(ctx
.instructions
);