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
;
58 mad_info(aco_ptr
<Instruction
> instr
, uint32_t id
)
59 : add_instr(std::move(instr
)), mul_temp_id(id
), check_literal(false) {}
64 label_constant
= 1 << 1,
69 label_literal
= 1 << 6,
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,
80 label_add_sub
= 1 << 17,
81 label_bitwise
= 1 << 18,
82 label_minmax
= 1 << 19,
84 label_uniform_bool
= 1 << 21,
85 label_constant_64bit
= 1 << 22,
86 label_uniform_bitwise
= 1 << 23,
87 label_scc_invert
= 1 << 24,
88 label_vcc_hint
= 1 << 25,
89 label_scc_needed
= 1 << 26,
92 static constexpr uint32_t instr_labels
= label_vec
| label_mul
| label_mad
| label_omod_success
| label_clamp_success
|
93 label_add_sub
| label_bitwise
| label_uniform_bitwise
| label_minmax
| label_fcmp
;
94 static constexpr uint32_t temp_labels
= label_abs
| label_neg
| label_temp
| label_vcc
| label_b2f
| label_uniform_bool
|
95 label_omod2
| label_omod4
| label_omod5
| label_clamp
| label_scc_invert
;
96 static constexpr uint32_t val_labels
= label_constant
| label_constant_64bit
| label_literal
| label_mad
;
106 ssa_info() : label(0) {}
108 void add_label(Label new_label
)
110 /* Since all labels which use "instr" use it for the same thing
111 * (indicating the defining instruction), there is no need to clear
112 * any other instr labels. */
113 if (new_label
& instr_labels
)
114 label
&= ~temp_labels
; /* instr and temp alias */
116 if (new_label
& temp_labels
) {
117 label
&= ~temp_labels
;
118 label
&= ~instr_labels
; /* instr and temp alias */
121 if (new_label
& val_labels
)
122 label
&= ~val_labels
;
127 void set_vec(Instruction
* vec
)
129 add_label(label_vec
);
135 return label
& label_vec
;
138 void set_constant(uint32_t constant
)
140 add_label(label_constant
);
146 return label
& label_constant
;
149 void set_constant_64bit(uint32_t constant
)
151 add_label(label_constant_64bit
);
155 bool is_constant_64bit()
157 return label
& label_constant_64bit
;
160 void set_abs(Temp abs_temp
)
162 add_label(label_abs
);
168 return label
& label_abs
;
171 void set_neg(Temp neg_temp
)
173 add_label(label_neg
);
179 return label
& label_neg
;
182 void set_neg_abs(Temp neg_abs_temp
)
184 add_label((Label
)((uint32_t)label_abs
| (uint32_t)label_neg
));
188 void set_mul(Instruction
* mul
)
190 add_label(label_mul
);
196 return label
& label_mul
;
199 void set_temp(Temp tmp
)
201 add_label(label_temp
);
207 return label
& label_temp
;
210 void set_literal(uint32_t lit
)
212 add_label(label_literal
);
218 return label
& label_literal
;
221 void set_mad(Instruction
* mad
, uint32_t mad_info_idx
)
223 add_label(label_mad
);
230 return label
& label_mad
;
233 void set_omod2(Temp def
)
235 add_label(label_omod2
);
241 return label
& label_omod2
;
244 void set_omod4(Temp def
)
246 add_label(label_omod4
);
252 return label
& label_omod4
;
255 void set_omod5(Temp def
)
257 add_label(label_omod5
);
263 return label
& label_omod5
;
266 void set_omod_success(Instruction
* omod_instr
)
268 add_label(label_omod_success
);
272 bool is_omod_success()
274 return label
& label_omod_success
;
277 void set_clamp(Temp def
)
279 add_label(label_clamp
);
285 return label
& label_clamp
;
288 void set_clamp_success(Instruction
* clamp_instr
)
290 add_label(label_clamp_success
);
294 bool is_clamp_success()
296 return label
& label_clamp_success
;
301 add_label(label_undefined
);
306 return label
& label_undefined
;
309 void set_vcc(Temp vcc
)
311 add_label(label_vcc
);
317 return label
& label_vcc
;
320 bool is_constant_or_literal()
322 return is_constant() || is_literal();
325 void set_b2f(Temp val
)
327 add_label(label_b2f
);
333 return label
& label_b2f
;
336 void set_add_sub(Instruction
*add_sub_instr
)
338 add_label(label_add_sub
);
339 instr
= add_sub_instr
;
344 return label
& label_add_sub
;
347 void set_bitwise(Instruction
*bitwise_instr
)
349 add_label(label_bitwise
);
350 instr
= bitwise_instr
;
355 return label
& label_bitwise
;
358 void set_uniform_bitwise()
360 add_label(label_uniform_bitwise
);
363 bool is_uniform_bitwise()
365 return label
& label_uniform_bitwise
;
368 void set_minmax(Instruction
*minmax_instr
)
370 add_label(label_minmax
);
371 instr
= minmax_instr
;
376 return label
& label_minmax
;
379 void set_fcmp(Instruction
*fcmp_instr
)
381 add_label(label_fcmp
);
387 return label
& label_fcmp
;
390 void set_scc_needed()
392 add_label(label_scc_needed
);
397 return label
& label_scc_needed
;
400 void set_scc_invert(Temp scc_inv
)
402 add_label(label_scc_invert
);
408 return label
& label_scc_invert
;
411 void set_uniform_bool(Temp uniform_bool
)
413 add_label(label_uniform_bool
);
417 bool is_uniform_bool()
419 return label
& label_uniform_bool
;
424 add_label(label_vcc_hint
);
429 return label
& label_vcc_hint
;
435 std::vector
<aco_ptr
<Instruction
>> instructions
;
437 std::pair
<uint32_t,Temp
> last_literal
;
438 std::vector
<mad_info
> mad_infos
;
439 std::vector
<uint16_t> uses
;
442 bool can_swap_operands(aco_ptr
<Instruction
>& instr
)
444 if (instr
->operands
[0].isConstant() ||
445 (instr
->operands
[0].isTemp() && instr
->operands
[0].getTemp().type() == RegType::sgpr
))
448 switch (instr
->opcode
) {
449 case aco_opcode::v_add_f32
:
450 case aco_opcode::v_mul_f32
:
451 case aco_opcode::v_or_b32
:
452 case aco_opcode::v_and_b32
:
453 case aco_opcode::v_xor_b32
:
454 case aco_opcode::v_max_f32
:
455 case aco_opcode::v_min_f32
:
456 case aco_opcode::v_max_i32
:
457 case aco_opcode::v_min_i32
:
458 case aco_opcode::v_max_u32
:
459 case aco_opcode::v_min_u32
:
460 case aco_opcode::v_cmp_eq_f32
:
461 case aco_opcode::v_cmp_lg_f32
:
463 case aco_opcode::v_sub_f32
:
464 instr
->opcode
= aco_opcode::v_subrev_f32
;
466 case aco_opcode::v_cmp_lt_f32
:
467 instr
->opcode
= aco_opcode::v_cmp_gt_f32
;
469 case aco_opcode::v_cmp_ge_f32
:
470 instr
->opcode
= aco_opcode::v_cmp_le_f32
;
472 case aco_opcode::v_cmp_lt_i32
:
473 instr
->opcode
= aco_opcode::v_cmp_gt_i32
;
480 bool can_use_VOP3(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
485 if (instr
->operands
.size() && instr
->operands
[0].isLiteral() && ctx
.program
->chip_class
< GFX10
)
488 if (instr
->isDPP() || instr
->isSDWA())
491 return instr
->opcode
!= aco_opcode::v_madmk_f32
&&
492 instr
->opcode
!= aco_opcode::v_madak_f32
&&
493 instr
->opcode
!= aco_opcode::v_madmk_f16
&&
494 instr
->opcode
!= aco_opcode::v_madak_f16
&&
495 instr
->opcode
!= aco_opcode::v_fmamk_f32
&&
496 instr
->opcode
!= aco_opcode::v_fmaak_f32
&&
497 instr
->opcode
!= aco_opcode::v_fmamk_f16
&&
498 instr
->opcode
!= aco_opcode::v_fmaak_f16
&&
499 instr
->opcode
!= aco_opcode::v_readlane_b32
&&
500 instr
->opcode
!= aco_opcode::v_writelane_b32
&&
501 instr
->opcode
!= aco_opcode::v_readfirstlane_b32
;
504 bool can_apply_sgprs(aco_ptr
<Instruction
>& instr
)
506 return instr
->opcode
!= aco_opcode::v_readfirstlane_b32
&&
507 instr
->opcode
!= aco_opcode::v_readlane_b32
&&
508 instr
->opcode
!= aco_opcode::v_readlane_b32_e64
&&
509 instr
->opcode
!= aco_opcode::v_writelane_b32
&&
510 instr
->opcode
!= aco_opcode::v_writelane_b32_e64
;
513 void to_VOP3(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
518 aco_ptr
<Instruction
> tmp
= std::move(instr
);
519 Format format
= asVOP3(tmp
->format
);
520 instr
.reset(create_instruction
<VOP3A_instruction
>(tmp
->opcode
, format
, tmp
->operands
.size(), tmp
->definitions
.size()));
521 std::copy(tmp
->operands
.cbegin(), tmp
->operands
.cend(), instr
->operands
.begin());
522 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
523 instr
->definitions
[i
] = tmp
->definitions
[i
];
524 if (instr
->definitions
[i
].isTemp()) {
525 ssa_info
& info
= ctx
.info
[instr
->definitions
[i
].tempId()];
526 if (info
.label
& instr_labels
&& info
.instr
== tmp
.get())
527 info
.instr
= instr
.get();
532 /* only covers special cases */
533 bool alu_can_accept_constant(aco_opcode opcode
, unsigned operand
)
536 case aco_opcode::v_interp_p2_f32
:
537 case aco_opcode::v_mac_f32
:
538 case aco_opcode::v_writelane_b32
:
539 case aco_opcode::v_writelane_b32_e64
:
540 case aco_opcode::v_cndmask_b32
:
542 case aco_opcode::s_addk_i32
:
543 case aco_opcode::s_mulk_i32
:
544 case aco_opcode::p_wqm
:
545 case aco_opcode::p_extract_vector
:
546 case aco_opcode::p_split_vector
:
547 case aco_opcode::v_readlane_b32
:
548 case aco_opcode::v_readlane_b32_e64
:
549 case aco_opcode::v_readfirstlane_b32
:
556 bool valu_can_accept_vgpr(aco_ptr
<Instruction
>& instr
, unsigned operand
)
558 if (instr
->opcode
== aco_opcode::v_readlane_b32
|| instr
->opcode
== aco_opcode::v_readlane_b32_e64
||
559 instr
->opcode
== aco_opcode::v_writelane_b32
|| instr
->opcode
== aco_opcode::v_writelane_b32_e64
)
564 /* check constant bus and literal limitations */
565 bool check_vop3_operands(opt_ctx
& ctx
, unsigned num_operands
, Operand
*operands
)
567 int limit
= ctx
.program
->chip_class
>= GFX10
? 2 : 1;
568 Operand
literal32(s1
);
569 Operand
literal64(s2
);
570 unsigned num_sgprs
= 0;
571 unsigned sgpr
[] = {0, 0};
573 for (unsigned i
= 0; i
< num_operands
; i
++) {
574 Operand op
= operands
[i
];
576 if (op
.hasRegClass() && op
.regClass().type() == RegType::sgpr
) {
577 /* two reads of the same SGPR count as 1 to the limit */
578 if (op
.tempId() != sgpr
[0] && op
.tempId() != sgpr
[1]) {
580 sgpr
[num_sgprs
++] = op
.tempId();
585 } else if (op
.isLiteral()) {
586 if (ctx
.program
->chip_class
< GFX10
)
589 if (!literal32
.isUndefined() && literal32
.constantValue() != op
.constantValue())
591 if (!literal64
.isUndefined() && literal64
.constantValue() != op
.constantValue())
594 /* Any number of 32-bit literals counts as only 1 to the limit. Same
595 * (but separately) for 64-bit literals. */
596 if (op
.size() == 1 && literal32
.isUndefined()) {
599 } else if (op
.size() == 2 && literal64
.isUndefined()) {
612 bool parse_base_offset(opt_ctx
&ctx
, Instruction
* instr
, unsigned op_index
, Temp
*base
, uint32_t *offset
)
614 Operand op
= instr
->operands
[op_index
];
618 Temp tmp
= op
.getTemp();
619 if (!ctx
.info
[tmp
.id()].is_add_sub())
622 Instruction
*add_instr
= ctx
.info
[tmp
.id()].instr
;
624 switch (add_instr
->opcode
) {
625 case aco_opcode::v_add_u32
:
626 case aco_opcode::v_add_co_u32
:
627 case aco_opcode::v_add_co_u32_e64
:
628 case aco_opcode::s_add_i32
:
629 case aco_opcode::s_add_u32
:
635 if (add_instr
->usesModifiers())
638 for (unsigned i
= 0; i
< 2; i
++) {
639 if (add_instr
->operands
[i
].isConstant()) {
640 *offset
= add_instr
->operands
[i
].constantValue();
641 } else if (add_instr
->operands
[i
].isTemp() &&
642 ctx
.info
[add_instr
->operands
[i
].tempId()].is_constant_or_literal()) {
643 *offset
= ctx
.info
[add_instr
->operands
[i
].tempId()].val
;
647 if (!add_instr
->operands
[!i
].isTemp())
650 uint32_t offset2
= 0;
651 if (parse_base_offset(ctx
, add_instr
, !i
, base
, &offset2
)) {
654 *base
= add_instr
->operands
[!i
].getTemp();
662 Operand
get_constant_op(opt_ctx
&ctx
, uint32_t val
, bool is64bit
= false)
664 // TODO: this functions shouldn't be needed if we store Operand instead of value.
665 Operand
op(val
, is64bit
);
666 if (val
== 0x3e22f983 && ctx
.program
->chip_class
>= GFX8
)
667 op
.setFixed(PhysReg
{248}); /* 1/2 PI can be an inline constant on GFX8+ */
671 bool fixed_to_exec(Operand op
)
673 return op
.isFixed() && op
.physReg() == exec
;
676 void label_instruction(opt_ctx
&ctx
, Block
& block
, aco_ptr
<Instruction
>& instr
)
678 if (instr
->isSALU() || instr
->isVALU() || instr
->format
== Format::PSEUDO
) {
679 ASSERTED
bool all_const
= false;
680 for (Operand
& op
: instr
->operands
)
681 all_const
= all_const
&& (!op
.isTemp() || ctx
.info
[op
.tempId()].is_constant_or_literal());
682 perfwarn(all_const
, "All instruction operands are constant", instr
.get());
685 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
687 if (!instr
->operands
[i
].isTemp())
690 ssa_info info
= ctx
.info
[instr
->operands
[i
].tempId()];
691 /* propagate undef */
692 if (info
.is_undefined() && is_phi(instr
))
693 instr
->operands
[i
] = Operand(instr
->operands
[i
].regClass());
694 /* propagate reg->reg of same type */
695 if (info
.is_temp() && info
.temp
.regClass() == instr
->operands
[i
].getTemp().regClass()) {
696 instr
->operands
[i
].setTemp(ctx
.info
[instr
->operands
[i
].tempId()].temp
);
697 info
= ctx
.info
[info
.temp
.id()];
700 /* SALU / PSEUDO: propagate inline constants */
701 if (instr
->isSALU() || instr
->format
== Format::PSEUDO
) {
702 const bool is_subdword
= std::any_of(instr
->definitions
.begin(), instr
->definitions
.end(),
703 [] (const Definition
& def
) { return def
.regClass().is_subdword();});
704 // TODO: optimize SGPR and constant propagation for subdword pseudo instructions on gfx9+
708 if (info
.is_temp() && info
.temp
.type() == RegType::sgpr
) {
709 instr
->operands
[i
].setTemp(info
.temp
);
710 info
= ctx
.info
[info
.temp
.id()];
711 } else if (info
.is_temp() && info
.temp
.type() == RegType::vgpr
) {
712 /* propagate vgpr if it can take it */
713 switch (instr
->opcode
) {
714 case aco_opcode::p_create_vector
:
715 case aco_opcode::p_split_vector
:
716 case aco_opcode::p_extract_vector
:
717 case aco_opcode::p_phi
: {
718 const bool all_vgpr
= std::none_of(instr
->definitions
.begin(), instr
->definitions
.end(),
719 [] (const Definition
& def
) { return def
.getTemp().type() != RegType::vgpr
;});
721 instr
->operands
[i
] = Operand(info
.temp
);
722 info
= ctx
.info
[info
.temp
.id()];
730 if ((info
.is_constant() || info
.is_constant_64bit() || (info
.is_literal() && instr
->format
== Format::PSEUDO
)) &&
731 !instr
->operands
[i
].isFixed() && alu_can_accept_constant(instr
->opcode
, i
)) {
732 instr
->operands
[i
] = get_constant_op(ctx
, info
.val
, info
.is_constant_64bit());
737 /* VALU: propagate neg, abs & inline constants */
738 else if (instr
->isVALU()) {
739 if (info
.is_temp() && info
.temp
.type() == RegType::vgpr
&& valu_can_accept_vgpr(instr
, i
)) {
740 instr
->operands
[i
].setTemp(info
.temp
);
741 info
= ctx
.info
[info
.temp
.id()];
743 if (info
.is_abs() && (can_use_VOP3(ctx
, instr
) || instr
->isDPP()) && instr_info
.can_use_input_modifiers
[(int)instr
->opcode
]) {
746 instr
->operands
[i
] = Operand(info
.temp
);
748 static_cast<DPP_instruction
*>(instr
.get())->abs
[i
] = true;
750 static_cast<VOP3A_instruction
*>(instr
.get())->abs
[i
] = true;
752 if (info
.is_neg() && instr
->opcode
== aco_opcode::v_add_f32
) {
753 instr
->opcode
= i
? aco_opcode::v_sub_f32
: aco_opcode::v_subrev_f32
;
754 instr
->operands
[i
].setTemp(info
.temp
);
756 } else if (info
.is_neg() && (can_use_VOP3(ctx
, instr
) || instr
->isDPP()) && instr_info
.can_use_input_modifiers
[(int)instr
->opcode
]) {
759 instr
->operands
[i
].setTemp(info
.temp
);
761 static_cast<DPP_instruction
*>(instr
.get())->neg
[i
] = true;
763 static_cast<VOP3A_instruction
*>(instr
.get())->neg
[i
] = true;
766 if ((info
.is_constant() || info
.is_constant_64bit()) && alu_can_accept_constant(instr
->opcode
, i
)) {
767 Operand op
= get_constant_op(ctx
, info
.val
, info
.is_constant_64bit());
768 perfwarn(instr
->opcode
== aco_opcode::v_cndmask_b32
&& i
== 2, "v_cndmask_b32 with a constant selector", instr
.get());
769 if (i
== 0 || instr
->opcode
== aco_opcode::v_readlane_b32
|| instr
->opcode
== aco_opcode::v_writelane_b32
) {
770 instr
->operands
[i
] = op
;
772 } else if (!instr
->isVOP3() && can_swap_operands(instr
)) {
773 instr
->operands
[i
] = instr
->operands
[0];
774 instr
->operands
[0] = op
;
776 } else if (can_use_VOP3(ctx
, instr
)) {
778 instr
->operands
[i
] = op
;
784 /* MUBUF: propagate constants and combine additions */
785 else if (instr
->format
== Format::MUBUF
) {
786 MUBUF_instruction
*mubuf
= static_cast<MUBUF_instruction
*>(instr
.get());
789 while (info
.is_temp())
790 info
= ctx
.info
[info
.temp
.id()];
792 if (mubuf
->offen
&& i
== 1 && info
.is_constant_or_literal() && mubuf
->offset
+ info
.val
< 4096) {
793 assert(!mubuf
->idxen
);
794 instr
->operands
[1] = Operand(v1
);
795 mubuf
->offset
+= info
.val
;
796 mubuf
->offen
= false;
798 } else if (i
== 2 && info
.is_constant_or_literal() && mubuf
->offset
+ info
.val
< 4096) {
799 instr
->operands
[2] = Operand((uint32_t) 0);
800 mubuf
->offset
+= info
.val
;
802 } else if (mubuf
->offen
&& i
== 1 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == v1
&& mubuf
->offset
+ offset
< 4096) {
803 assert(!mubuf
->idxen
);
804 instr
->operands
[1].setTemp(base
);
805 mubuf
->offset
+= offset
;
807 } else if (i
== 2 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == s1
&& mubuf
->offset
+ offset
< 4096) {
808 instr
->operands
[i
].setTemp(base
);
809 mubuf
->offset
+= offset
;
814 /* DS: combine additions */
815 else if (instr
->format
== Format::DS
) {
817 DS_instruction
*ds
= static_cast<DS_instruction
*>(instr
.get());
820 bool has_usable_ds_offset
= ctx
.program
->chip_class
>= GFX7
;
821 if (has_usable_ds_offset
&&
822 i
== 0 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) &&
823 base
.regClass() == instr
->operands
[i
].regClass() &&
824 instr
->opcode
!= aco_opcode::ds_swizzle_b32
) {
825 if (instr
->opcode
== aco_opcode::ds_write2_b32
|| instr
->opcode
== aco_opcode::ds_read2_b32
||
826 instr
->opcode
== aco_opcode::ds_write2_b64
|| instr
->opcode
== aco_opcode::ds_read2_b64
) {
827 unsigned mask
= (instr
->opcode
== aco_opcode::ds_write2_b64
|| instr
->opcode
== aco_opcode::ds_read2_b64
) ? 0x7 : 0x3;
828 unsigned shifts
= (instr
->opcode
== aco_opcode::ds_write2_b64
|| instr
->opcode
== aco_opcode::ds_read2_b64
) ? 3 : 2;
830 if ((offset
& mask
) == 0 &&
831 ds
->offset0
+ (offset
>> shifts
) <= 255 &&
832 ds
->offset1
+ (offset
>> shifts
) <= 255) {
833 instr
->operands
[i
].setTemp(base
);
834 ds
->offset0
+= offset
>> shifts
;
835 ds
->offset1
+= offset
>> shifts
;
838 if (ds
->offset0
+ offset
<= 65535) {
839 instr
->operands
[i
].setTemp(base
);
840 ds
->offset0
+= offset
;
846 /* SMEM: propagate constants and combine additions */
847 else if (instr
->format
== Format::SMEM
) {
849 SMEM_instruction
*smem
= static_cast<SMEM_instruction
*>(instr
.get());
852 if (i
== 1 && info
.is_constant_or_literal() &&
853 ((ctx
.program
->chip_class
== GFX6
&& info
.val
<= 0x3FF) ||
854 (ctx
.program
->chip_class
== GFX7
&& info
.val
<= 0xFFFFFFFF) ||
855 (ctx
.program
->chip_class
>= GFX8
&& info
.val
<= 0xFFFFF))) {
856 instr
->operands
[i
] = Operand(info
.val
);
858 } else if (i
== 1 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == s1
&& offset
<= 0xFFFFF && ctx
.program
->chip_class
>= GFX9
) {
859 bool soe
= smem
->operands
.size() >= (!smem
->definitions
.empty() ? 3 : 4);
861 (!ctx
.info
[smem
->operands
.back().tempId()].is_constant_or_literal() ||
862 ctx
.info
[smem
->operands
.back().tempId()].val
!= 0)) {
866 smem
->operands
[1] = Operand(offset
);
867 smem
->operands
.back() = Operand(base
);
869 SMEM_instruction
*new_instr
= create_instruction
<SMEM_instruction
>(smem
->opcode
, Format::SMEM
, smem
->operands
.size() + 1, smem
->definitions
.size());
870 new_instr
->operands
[0] = smem
->operands
[0];
871 new_instr
->operands
[1] = Operand(offset
);
872 if (smem
->definitions
.empty())
873 new_instr
->operands
[2] = smem
->operands
[2];
874 new_instr
->operands
.back() = Operand(base
);
875 if (!smem
->definitions
.empty())
876 new_instr
->definitions
[0] = smem
->definitions
[0];
877 new_instr
->can_reorder
= smem
->can_reorder
;
878 new_instr
->barrier
= smem
->barrier
;
879 instr
.reset(new_instr
);
880 smem
= static_cast<SMEM_instruction
*>(instr
.get());
886 else if (instr
->format
== Format::PSEUDO_BRANCH
) {
887 if (ctx
.info
[instr
->operands
[0].tempId()].is_scc_invert()) {
888 /* Flip the branch instruction to get rid of the scc_invert instruction */
889 instr
->opcode
= instr
->opcode
== aco_opcode::p_cbranch_z
? aco_opcode::p_cbranch_nz
: aco_opcode::p_cbranch_z
;
890 instr
->operands
[0].setTemp(ctx
.info
[instr
->operands
[0].tempId()].temp
);
895 /* if this instruction doesn't define anything, return */
896 if (instr
->definitions
.empty())
899 switch (instr
->opcode
) {
900 case aco_opcode::p_create_vector
: {
901 if (instr
->operands
.size() == 1 && instr
->operands
[0].isTemp())
902 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
904 ctx
.info
[instr
->definitions
[0].tempId()].set_vec(instr
.get());
906 unsigned num_ops
= instr
->operands
.size();
907 for (const Operand
& op
: instr
->operands
) {
908 if (op
.isTemp() && ctx
.info
[op
.tempId()].is_vec())
909 num_ops
+= ctx
.info
[op
.tempId()].instr
->operands
.size() - 1;
911 if (num_ops
!= instr
->operands
.size()) {
912 aco_ptr
<Instruction
> old_vec
= std::move(instr
);
913 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_ops
, 1));
914 instr
->definitions
[0] = old_vec
->definitions
[0];
916 for (Operand
& old_op
: old_vec
->operands
) {
917 if (old_op
.isTemp() && ctx
.info
[old_op
.tempId()].is_vec()) {
918 for (unsigned j
= 0; j
< ctx
.info
[old_op
.tempId()].instr
->operands
.size(); j
++) {
919 Operand op
= ctx
.info
[old_op
.tempId()].instr
->operands
[j
];
920 if (op
.isTemp() && ctx
.info
[op
.tempId()].is_temp() &&
921 ctx
.info
[op
.tempId()].temp
.type() == instr
->definitions
[0].regClass().type())
922 op
.setTemp(ctx
.info
[op
.tempId()].temp
);
923 instr
->operands
[k
++] = op
;
926 instr
->operands
[k
++] = old_op
;
929 assert(k
== num_ops
);
933 case aco_opcode::p_split_vector
: {
934 if (!ctx
.info
[instr
->operands
[0].tempId()].is_vec())
936 Instruction
* vec
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
937 unsigned split_offset
= 0;
938 unsigned vec_offset
= 0;
939 unsigned vec_index
= 0;
940 for (unsigned i
= 0; i
< instr
->definitions
.size(); split_offset
+= instr
->definitions
[i
++].bytes()) {
941 while (vec_offset
< split_offset
&& vec_index
< vec
->operands
.size())
942 vec_offset
+= vec
->operands
[vec_index
++].bytes();
944 if (vec_offset
!= split_offset
|| vec
->operands
[vec_index
].bytes() != instr
->definitions
[i
].bytes())
947 Operand vec_op
= vec
->operands
[vec_index
];
948 if (vec_op
.isConstant()) {
949 if (vec_op
.isLiteral())
950 ctx
.info
[instr
->definitions
[i
].tempId()].set_literal(vec_op
.constantValue());
951 else if (vec_op
.size() == 1)
952 ctx
.info
[instr
->definitions
[i
].tempId()].set_constant(vec_op
.constantValue());
953 else if (vec_op
.size() == 2)
954 ctx
.info
[instr
->definitions
[i
].tempId()].set_constant_64bit(vec_op
.constantValue());
956 assert(vec_op
.isTemp());
957 ctx
.info
[instr
->definitions
[i
].tempId()].set_temp(vec_op
.getTemp());
962 case aco_opcode::p_extract_vector
: { /* mov */
963 if (!ctx
.info
[instr
->operands
[0].tempId()].is_vec())
966 /* check if we index directly into a vector element */
967 Instruction
* vec
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
968 const unsigned index
= instr
->operands
[1].constantValue();
969 const unsigned dst_offset
= index
* instr
->definitions
[0].bytes();
972 for (const Operand
& op
: vec
->operands
) {
973 if (offset
< dst_offset
) {
974 offset
+= op
.bytes();
976 } else if (offset
!= dst_offset
|| op
.bytes() != instr
->definitions
[0].bytes()) {
980 /* convert this extract into a copy instruction */
981 instr
->opcode
= aco_opcode::p_parallelcopy
;
982 instr
->operands
.pop_back();
983 instr
->operands
[0] = op
;
985 if (op
.isConstant()) {
987 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(op
.constantValue());
988 else if (op
.size() == 1)
989 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(op
.constantValue());
990 else if (op
.size() == 2)
991 ctx
.info
[instr
->definitions
[0].tempId()].set_constant_64bit(op
.constantValue());
994 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(op
.getTemp());
1000 case aco_opcode::s_mov_b32
: /* propagate */
1001 case aco_opcode::s_mov_b64
:
1002 case aco_opcode::v_mov_b32
:
1003 case aco_opcode::p_as_uniform
:
1004 if (instr
->definitions
[0].isFixed()) {
1005 /* don't copy-propagate copies into fixed registers */
1006 } else if (instr
->usesModifiers()) {
1008 } else if (instr
->operands
[0].isConstant()) {
1009 if (instr
->operands
[0].isLiteral())
1010 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(instr
->operands
[0].constantValue());
1011 else if (instr
->operands
[0].size() == 1)
1012 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(instr
->operands
[0].constantValue());
1013 else if (instr
->operands
[0].size() == 2)
1014 ctx
.info
[instr
->definitions
[0].tempId()].set_constant_64bit(instr
->operands
[0].constantValue());
1015 } else if (instr
->operands
[0].isTemp()) {
1016 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
1018 assert(instr
->operands
[0].isFixed());
1021 case aco_opcode::p_is_helper
:
1022 if (!ctx
.program
->needs_wqm
)
1023 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(0u);
1025 case aco_opcode::s_movk_i32
: {
1026 uint32_t v
= static_cast<SOPK_instruction
*>(instr
.get())->imm
;
1027 v
= v
& 0x8000 ? (v
| 0xffff0000) : v
;
1028 if (v
<= 64 || v
>= 0xfffffff0)
1029 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
1031 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
1034 case aco_opcode::v_bfrev_b32
:
1035 case aco_opcode::s_brev_b32
: {
1036 if (instr
->operands
[0].isConstant()) {
1037 uint32_t v
= util_bitreverse(instr
->operands
[0].constantValue());
1038 if (v
<= 64 || v
>= 0xfffffff0)
1039 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
1041 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
1045 case aco_opcode::s_bfm_b32
: {
1046 if (instr
->operands
[0].isConstant() && instr
->operands
[1].isConstant()) {
1047 unsigned size
= instr
->operands
[0].constantValue() & 0x1f;
1048 unsigned start
= instr
->operands
[1].constantValue() & 0x1f;
1049 uint32_t v
= ((1u << size
) - 1u) << start
;
1050 if (v
<= 64 || v
>= 0xfffffff0)
1051 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
1053 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
1056 case aco_opcode::v_mul_f32
: { /* omod */
1057 /* TODO: try to move the negate/abs modifier to the consumer instead */
1058 if (instr
->usesModifiers())
1061 for (unsigned i
= 0; i
< 2; i
++) {
1062 if (instr
->operands
[!i
].isConstant() && instr
->operands
[i
].isTemp()) {
1063 if (instr
->operands
[!i
].constantValue() == 0x40000000) { /* 2.0 */
1064 ctx
.info
[instr
->operands
[i
].tempId()].set_omod2(instr
->definitions
[0].getTemp());
1065 } else if (instr
->operands
[!i
].constantValue() == 0x40800000) { /* 4.0 */
1066 ctx
.info
[instr
->operands
[i
].tempId()].set_omod4(instr
->definitions
[0].getTemp());
1067 } else if (instr
->operands
[!i
].constantValue() == 0x3f000000) { /* 0.5 */
1068 ctx
.info
[instr
->operands
[i
].tempId()].set_omod5(instr
->definitions
[0].getTemp());
1069 } else if (instr
->operands
[!i
].constantValue() == 0x3f800000 &&
1070 !block
.fp_mode
.must_flush_denorms32
) { /* 1.0 */
1071 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[i
].getTemp());
1080 case aco_opcode::v_and_b32
: /* abs */
1081 if (!instr
->usesModifiers() && instr
->operands
[0].constantEquals(0x7FFFFFFF) &&
1082 instr
->operands
[1].isTemp() && instr
->operands
[1].getTemp().type() == RegType::vgpr
)
1083 ctx
.info
[instr
->definitions
[0].tempId()].set_abs(instr
->operands
[1].getTemp());
1085 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
1087 case aco_opcode::v_xor_b32
: { /* neg */
1088 if (!instr
->usesModifiers() && instr
->operands
[0].constantEquals(0x80000000u
) && instr
->operands
[1].isTemp()) {
1089 if (ctx
.info
[instr
->operands
[1].tempId()].is_neg()) {
1090 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
1091 } else if (instr
->operands
[1].getTemp().type() == RegType::vgpr
) {
1092 if (ctx
.info
[instr
->operands
[1].tempId()].is_abs()) { /* neg(abs(x)) */
1093 instr
->operands
[1].setTemp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
1094 instr
->opcode
= aco_opcode::v_or_b32
;
1095 ctx
.info
[instr
->definitions
[0].tempId()].set_neg_abs(instr
->operands
[1].getTemp());
1097 ctx
.info
[instr
->definitions
[0].tempId()].set_neg(instr
->operands
[1].getTemp());
1101 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
1105 case aco_opcode::v_med3_f32
: { /* clamp */
1106 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
1107 if (vop3
->abs
[0] || vop3
->abs
[1] || vop3
->abs
[2] ||
1108 vop3
->neg
[0] || vop3
->neg
[1] || vop3
->neg
[2] ||
1109 vop3
->omod
!= 0 || vop3
->opsel
!= 0)
1113 bool found_zero
= false, found_one
= false;
1114 for (unsigned i
= 0; i
< 3; i
++)
1116 if (instr
->operands
[i
].constantEquals(0))
1118 else if (instr
->operands
[i
].constantEquals(0x3f800000)) /* 1.0 */
1123 if (found_zero
&& found_one
&& instr
->operands
[idx
].isTemp()) {
1124 ctx
.info
[instr
->operands
[idx
].tempId()].set_clamp(instr
->definitions
[0].getTemp());
1128 case aco_opcode::v_cndmask_b32
:
1129 if (instr
->operands
[0].constantEquals(0) &&
1130 instr
->operands
[1].constantEquals(0xFFFFFFFF) &&
1131 instr
->operands
[2].isTemp())
1132 ctx
.info
[instr
->definitions
[0].tempId()].set_vcc(instr
->operands
[2].getTemp());
1133 else if (instr
->operands
[0].constantEquals(0) &&
1134 instr
->operands
[1].constantEquals(0x3f800000u
) &&
1135 instr
->operands
[2].isTemp())
1136 ctx
.info
[instr
->definitions
[0].tempId()].set_b2f(instr
->operands
[2].getTemp());
1138 ctx
.info
[instr
->operands
[2].tempId()].set_vcc_hint();
1140 case aco_opcode::v_cmp_lg_u32
:
1141 if (instr
->format
== Format::VOPC
&& /* don't optimize VOP3 / SDWA / DPP */
1142 instr
->operands
[0].constantEquals(0) &&
1143 instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_vcc())
1144 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
1146 case aco_opcode::p_phi
:
1147 case aco_opcode::p_linear_phi
: {
1148 /* lower_bool_phis() can create phis like this */
1149 bool all_same_temp
= instr
->operands
[0].isTemp();
1150 /* this check is needed when moving uniform loop counters out of a divergent loop */
1152 all_same_temp
= instr
->definitions
[0].regClass() == instr
->operands
[0].regClass();
1153 for (unsigned i
= 1; all_same_temp
&& (i
< instr
->operands
.size()); i
++) {
1154 if (!instr
->operands
[i
].isTemp() || instr
->operands
[i
].tempId() != instr
->operands
[0].tempId())
1155 all_same_temp
= false;
1157 if (all_same_temp
) {
1158 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
1160 bool all_undef
= instr
->operands
[0].isUndefined();
1161 for (unsigned i
= 1; all_undef
&& (i
< instr
->operands
.size()); i
++) {
1162 if (!instr
->operands
[i
].isUndefined())
1166 ctx
.info
[instr
->definitions
[0].tempId()].set_undefined();
1170 case aco_opcode::v_add_u32
:
1171 case aco_opcode::v_add_co_u32
:
1172 case aco_opcode::v_add_co_u32_e64
:
1173 case aco_opcode::s_add_i32
:
1174 case aco_opcode::s_add_u32
:
1175 ctx
.info
[instr
->definitions
[0].tempId()].set_add_sub(instr
.get());
1177 case aco_opcode::s_not_b32
:
1178 case aco_opcode::s_not_b64
:
1179 if (ctx
.info
[instr
->operands
[0].tempId()].is_uniform_bool()) {
1180 ctx
.info
[instr
->definitions
[0].tempId()].set_uniform_bitwise();
1181 ctx
.info
[instr
->definitions
[1].tempId()].set_scc_invert(ctx
.info
[instr
->operands
[0].tempId()].temp
);
1182 } else if (ctx
.info
[instr
->operands
[0].tempId()].is_uniform_bitwise()) {
1183 ctx
.info
[instr
->definitions
[0].tempId()].set_uniform_bitwise();
1184 ctx
.info
[instr
->definitions
[1].tempId()].set_scc_invert(ctx
.info
[instr
->operands
[0].tempId()].instr
->definitions
[1].getTemp());
1186 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
1188 case aco_opcode::s_and_b32
:
1189 case aco_opcode::s_and_b64
:
1190 if (fixed_to_exec(instr
->operands
[1]) && instr
->operands
[0].isTemp()) {
1191 if (ctx
.info
[instr
->operands
[0].tempId()].is_uniform_bool()) {
1192 /* Try to get rid of the superfluous s_cselect + s_and_b64 that comes from turning a uniform bool into divergent */
1193 ctx
.info
[instr
->definitions
[1].tempId()].set_temp(ctx
.info
[instr
->operands
[0].tempId()].temp
);
1194 ctx
.info
[instr
->definitions
[0].tempId()].set_uniform_bool(ctx
.info
[instr
->operands
[0].tempId()].temp
);
1196 } else if (ctx
.info
[instr
->operands
[0].tempId()].is_uniform_bitwise()) {
1197 /* Try to get rid of the superfluous s_and_b64, since the uniform bitwise instruction already produces the same SCC */
1198 ctx
.info
[instr
->definitions
[1].tempId()].set_temp(ctx
.info
[instr
->operands
[0].tempId()].instr
->definitions
[1].getTemp());
1199 ctx
.info
[instr
->definitions
[0].tempId()].set_uniform_bool(ctx
.info
[instr
->operands
[0].tempId()].instr
->definitions
[1].getTemp());
1204 case aco_opcode::s_or_b32
:
1205 case aco_opcode::s_or_b64
:
1206 case aco_opcode::s_xor_b32
:
1207 case aco_opcode::s_xor_b64
:
1208 if (std::all_of(instr
->operands
.begin(), instr
->operands
.end(), [&ctx
](const Operand
& op
) {
1209 return op
.isTemp() && (ctx
.info
[op
.tempId()].is_uniform_bool() || ctx
.info
[op
.tempId()].is_uniform_bitwise());
1211 ctx
.info
[instr
->definitions
[0].tempId()].set_uniform_bitwise();
1214 case aco_opcode::s_lshl_b32
:
1215 case aco_opcode::v_or_b32
:
1216 case aco_opcode::v_lshlrev_b32
:
1217 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
1219 case aco_opcode::v_min_f32
:
1220 case aco_opcode::v_min_f16
:
1221 case aco_opcode::v_min_u32
:
1222 case aco_opcode::v_min_i32
:
1223 case aco_opcode::v_min_u16
:
1224 case aco_opcode::v_min_i16
:
1225 case aco_opcode::v_max_f32
:
1226 case aco_opcode::v_max_f16
:
1227 case aco_opcode::v_max_u32
:
1228 case aco_opcode::v_max_i32
:
1229 case aco_opcode::v_max_u16
:
1230 case aco_opcode::v_max_i16
:
1231 ctx
.info
[instr
->definitions
[0].tempId()].set_minmax(instr
.get());
1233 case aco_opcode::v_cmp_lt_f32
:
1234 case aco_opcode::v_cmp_eq_f32
:
1235 case aco_opcode::v_cmp_le_f32
:
1236 case aco_opcode::v_cmp_gt_f32
:
1237 case aco_opcode::v_cmp_lg_f32
:
1238 case aco_opcode::v_cmp_ge_f32
:
1239 case aco_opcode::v_cmp_o_f32
:
1240 case aco_opcode::v_cmp_u_f32
:
1241 case aco_opcode::v_cmp_nge_f32
:
1242 case aco_opcode::v_cmp_nlg_f32
:
1243 case aco_opcode::v_cmp_ngt_f32
:
1244 case aco_opcode::v_cmp_nle_f32
:
1245 case aco_opcode::v_cmp_neq_f32
:
1246 case aco_opcode::v_cmp_nlt_f32
:
1247 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(instr
.get());
1249 case aco_opcode::s_cselect_b64
:
1250 case aco_opcode::s_cselect_b32
:
1251 if (instr
->operands
[0].constantEquals((unsigned) -1) &&
1252 instr
->operands
[1].constantEquals(0)) {
1253 /* Found a cselect that operates on a uniform bool that comes from eg. s_cmp */
1254 ctx
.info
[instr
->definitions
[0].tempId()].set_uniform_bool(instr
->operands
[2].getTemp());
1256 if (instr
->operands
[2].isTemp() && ctx
.info
[instr
->operands
[2].tempId()].is_scc_invert()) {
1257 /* Flip the operands to get rid of the scc_invert instruction */
1258 std::swap(instr
->operands
[0], instr
->operands
[1]);
1259 instr
->operands
[2].setTemp(ctx
.info
[instr
->operands
[2].tempId()].temp
);
1262 case aco_opcode::p_wqm
:
1263 if (instr
->operands
[0].isTemp() &&
1264 ctx
.info
[instr
->operands
[0].tempId()].is_scc_invert()) {
1265 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
1273 ALWAYS_INLINE
bool get_cmp_info(aco_opcode op
, aco_opcode
*ordered
, aco_opcode
*unordered
, aco_opcode
*inverse
)
1275 *ordered
= *unordered
= op
;
1277 #define CMP(ord, unord) \
1278 case aco_opcode::v_cmp_##ord##_f32:\
1279 case aco_opcode::v_cmp_n##unord##_f32:\
1280 *ordered = aco_opcode::v_cmp_##ord##_f32;\
1281 *unordered = aco_opcode::v_cmp_n##unord##_f32;\
1282 *inverse = op == aco_opcode::v_cmp_n##unord##_f32 ? aco_opcode::v_cmp_##unord##_f32 : aco_opcode::v_cmp_n##ord##_f32;\
1296 aco_opcode
get_ordered(aco_opcode op
)
1298 aco_opcode ordered
, unordered
, inverse
;
1299 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? ordered
: aco_opcode::last_opcode
;
1302 aco_opcode
get_unordered(aco_opcode op
)
1304 aco_opcode ordered
, unordered
, inverse
;
1305 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? unordered
: aco_opcode::last_opcode
;
1308 aco_opcode
get_inverse(aco_opcode op
)
1310 aco_opcode ordered
, unordered
, inverse
;
1311 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? inverse
: aco_opcode::last_opcode
;
1314 bool is_cmp(aco_opcode op
)
1316 aco_opcode ordered
, unordered
, inverse
;
1317 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
);
1320 unsigned original_temp_id(opt_ctx
&ctx
, Temp tmp
)
1322 if (ctx
.info
[tmp
.id()].is_temp())
1323 return ctx
.info
[tmp
.id()].temp
.id();
1328 void decrease_uses(opt_ctx
&ctx
, Instruction
* instr
)
1330 if (!--ctx
.uses
[instr
->definitions
[0].tempId()]) {
1331 for (const Operand
& op
: instr
->operands
) {
1333 ctx
.uses
[op
.tempId()]--;
1338 Instruction
*follow_operand(opt_ctx
&ctx
, Operand op
, bool ignore_uses
=false)
1340 if (!op
.isTemp() || !(ctx
.info
[op
.tempId()].label
& instr_labels
))
1342 if (!ignore_uses
&& ctx
.uses
[op
.tempId()] > 1)
1345 Instruction
*instr
= ctx
.info
[op
.tempId()].instr
;
1347 if (instr
->definitions
.size() == 2) {
1348 assert(instr
->definitions
[0].isTemp() && instr
->definitions
[0].tempId() == op
.tempId());
1349 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1356 /* s_or_b64(neq(a, a), neq(b, b)) -> v_cmp_u_f32(a, b)
1357 * s_and_b64(eq(a, a), eq(b, b)) -> v_cmp_o_f32(a, b) */
1358 bool combine_ordering_test(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1360 if (instr
->definitions
[0].regClass() != ctx
.program
->lane_mask
)
1362 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1365 bool is_or
= instr
->opcode
== aco_opcode::s_or_b64
|| instr
->opcode
== aco_opcode::s_or_b32
;
1367 bool neg
[2] = {false, false};
1368 bool abs
[2] = {false, false};
1370 Instruction
*op_instr
[2];
1373 for (unsigned i
= 0; i
< 2; i
++) {
1374 op_instr
[i
] = follow_operand(ctx
, instr
->operands
[i
], true);
1378 aco_opcode expected_cmp
= is_or
? aco_opcode::v_cmp_neq_f32
: aco_opcode::v_cmp_eq_f32
;
1380 if (op_instr
[i
]->opcode
!= expected_cmp
)
1382 if (!op_instr
[i
]->operands
[0].isTemp() || !op_instr
[i
]->operands
[1].isTemp())
1385 if (op_instr
[i
]->isVOP3()) {
1386 VOP3A_instruction
*vop3
= static_cast<VOP3A_instruction
*>(op_instr
[i
]);
1387 if (vop3
->neg
[0] != vop3
->neg
[1] || vop3
->abs
[0] != vop3
->abs
[1] || vop3
->opsel
== 1 || vop3
->opsel
== 2)
1389 neg
[i
] = vop3
->neg
[0];
1390 abs
[i
] = vop3
->abs
[0];
1391 opsel
|= (vop3
->opsel
& 1) << i
;
1394 Temp op0
= op_instr
[i
]->operands
[0].getTemp();
1395 Temp op1
= op_instr
[i
]->operands
[1].getTemp();
1396 if (original_temp_id(ctx
, op0
) != original_temp_id(ctx
, op1
))
1402 if (op
[1].type() == RegType::sgpr
)
1403 std::swap(op
[0], op
[1]);
1404 unsigned num_sgprs
= (op
[0].type() == RegType::sgpr
) + (op
[1].type() == RegType::sgpr
);
1405 if (num_sgprs
> (ctx
.program
->chip_class
>= GFX10
? 2 : 1))
1408 ctx
.uses
[op
[0].id()]++;
1409 ctx
.uses
[op
[1].id()]++;
1410 decrease_uses(ctx
, op_instr
[0]);
1411 decrease_uses(ctx
, op_instr
[1]);
1413 aco_opcode new_op
= is_or
? aco_opcode::v_cmp_u_f32
: aco_opcode::v_cmp_o_f32
;
1414 Instruction
*new_instr
;
1415 if (neg
[0] || neg
[1] || abs
[0] || abs
[1] || opsel
|| num_sgprs
> 1) {
1416 VOP3A_instruction
*vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1417 for (unsigned i
= 0; i
< 2; i
++) {
1418 vop3
->neg
[i
] = neg
[i
];
1419 vop3
->abs
[i
] = abs
[i
];
1421 vop3
->opsel
= opsel
;
1422 new_instr
= static_cast<Instruction
*>(vop3
);
1424 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1426 new_instr
->operands
[0] = Operand(op
[0]);
1427 new_instr
->operands
[1] = Operand(op
[1]);
1428 new_instr
->definitions
[0] = instr
->definitions
[0];
1430 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1431 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1433 instr
.reset(new_instr
);
1438 /* s_or_b64(v_cmp_u_f32(a, b), cmp(a, b)) -> get_unordered(cmp)(a, b)
1439 * s_and_b64(v_cmp_o_f32(a, b), cmp(a, b)) -> get_ordered(cmp)(a, b) */
1440 bool combine_comparison_ordering(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1442 if (instr
->definitions
[0].regClass() != ctx
.program
->lane_mask
)
1444 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1447 bool is_or
= instr
->opcode
== aco_opcode::s_or_b64
|| instr
->opcode
== aco_opcode::s_or_b32
;
1448 aco_opcode expected_nan_test
= is_or
? aco_opcode::v_cmp_u_f32
: aco_opcode::v_cmp_o_f32
;
1450 Instruction
*nan_test
= follow_operand(ctx
, instr
->operands
[0], true);
1451 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[1], true);
1452 if (!nan_test
|| !cmp
)
1455 if (cmp
->opcode
== expected_nan_test
)
1456 std::swap(nan_test
, cmp
);
1457 else if (nan_test
->opcode
!= expected_nan_test
)
1460 if (!is_cmp(cmp
->opcode
))
1463 if (!nan_test
->operands
[0].isTemp() || !nan_test
->operands
[1].isTemp())
1465 if (!cmp
->operands
[0].isTemp() || !cmp
->operands
[1].isTemp())
1468 unsigned prop_cmp0
= original_temp_id(ctx
, cmp
->operands
[0].getTemp());
1469 unsigned prop_cmp1
= original_temp_id(ctx
, cmp
->operands
[1].getTemp());
1470 unsigned prop_nan0
= original_temp_id(ctx
, nan_test
->operands
[0].getTemp());
1471 unsigned prop_nan1
= original_temp_id(ctx
, nan_test
->operands
[1].getTemp());
1472 if (prop_cmp0
!= prop_nan0
&& prop_cmp0
!= prop_nan1
)
1474 if (prop_cmp1
!= prop_nan0
&& prop_cmp1
!= prop_nan1
)
1477 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1478 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1479 decrease_uses(ctx
, nan_test
);
1480 decrease_uses(ctx
, cmp
);
1482 aco_opcode new_op
= is_or
? get_unordered(cmp
->opcode
) : get_ordered(cmp
->opcode
);
1483 Instruction
*new_instr
;
1484 if (cmp
->isVOP3()) {
1485 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1486 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1487 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1488 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1489 new_vop3
->clamp
= cmp_vop3
->clamp
;
1490 new_vop3
->omod
= cmp_vop3
->omod
;
1491 new_vop3
->opsel
= cmp_vop3
->opsel
;
1492 new_instr
= new_vop3
;
1494 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1496 new_instr
->operands
[0] = cmp
->operands
[0];
1497 new_instr
->operands
[1] = cmp
->operands
[1];
1498 new_instr
->definitions
[0] = instr
->definitions
[0];
1500 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1501 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1503 instr
.reset(new_instr
);
1508 /* s_or_b64(v_cmp_neq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_unordered(cmp)(a, b)
1509 * s_and_b64(v_cmp_eq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_ordered(cmp)(a, b) */
1510 bool combine_constant_comparison_ordering(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1512 if (instr
->definitions
[0].regClass() != ctx
.program
->lane_mask
)
1514 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1517 bool is_or
= instr
->opcode
== aco_opcode::s_or_b64
|| instr
->opcode
== aco_opcode::s_or_b32
;
1519 Instruction
*nan_test
= follow_operand(ctx
, instr
->operands
[0], true);
1520 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[1], true);
1522 if (!nan_test
|| !cmp
)
1525 aco_opcode expected_nan_test
= is_or
? aco_opcode::v_cmp_neq_f32
: aco_opcode::v_cmp_eq_f32
;
1526 if (cmp
->opcode
== expected_nan_test
)
1527 std::swap(nan_test
, cmp
);
1528 else if (nan_test
->opcode
!= expected_nan_test
)
1531 if (!is_cmp(cmp
->opcode
))
1534 if (!nan_test
->operands
[0].isTemp() || !nan_test
->operands
[1].isTemp())
1536 if (!cmp
->operands
[0].isTemp() && !cmp
->operands
[1].isTemp())
1539 unsigned prop_nan0
= original_temp_id(ctx
, nan_test
->operands
[0].getTemp());
1540 unsigned prop_nan1
= original_temp_id(ctx
, nan_test
->operands
[1].getTemp());
1541 if (prop_nan0
!= prop_nan1
)
1544 if (nan_test
->isVOP3()) {
1545 VOP3A_instruction
*vop3
= static_cast<VOP3A_instruction
*>(nan_test
);
1546 if (vop3
->neg
[0] != vop3
->neg
[1] || vop3
->abs
[0] != vop3
->abs
[1] || vop3
->opsel
== 1 || vop3
->opsel
== 2)
1550 int constant_operand
= -1;
1551 for (unsigned i
= 0; i
< 2; i
++) {
1552 if (cmp
->operands
[i
].isTemp() && original_temp_id(ctx
, cmp
->operands
[i
].getTemp()) == prop_nan0
) {
1553 constant_operand
= !i
;
1557 if (constant_operand
== -1)
1561 if (cmp
->operands
[constant_operand
].isConstant()) {
1562 constant
= cmp
->operands
[constant_operand
].constantValue();
1563 } else if (cmp
->operands
[constant_operand
].isTemp()) {
1564 Temp tmp
= cmp
->operands
[constant_operand
].getTemp();
1565 unsigned id
= original_temp_id(ctx
, tmp
);
1566 if (!ctx
.info
[id
].is_constant() && !ctx
.info
[id
].is_literal())
1568 constant
= ctx
.info
[id
].val
;
1574 memcpy(&constantf
, &constant
, 4);
1575 if (isnan(constantf
))
1578 if (cmp
->operands
[0].isTemp())
1579 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1580 if (cmp
->operands
[1].isTemp())
1581 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1582 decrease_uses(ctx
, nan_test
);
1583 decrease_uses(ctx
, cmp
);
1585 aco_opcode new_op
= is_or
? get_unordered(cmp
->opcode
) : get_ordered(cmp
->opcode
);
1586 Instruction
*new_instr
;
1587 if (cmp
->isVOP3()) {
1588 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1589 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1590 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1591 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1592 new_vop3
->clamp
= cmp_vop3
->clamp
;
1593 new_vop3
->omod
= cmp_vop3
->omod
;
1594 new_vop3
->opsel
= cmp_vop3
->opsel
;
1595 new_instr
= new_vop3
;
1597 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1599 new_instr
->operands
[0] = cmp
->operands
[0];
1600 new_instr
->operands
[1] = cmp
->operands
[1];
1601 new_instr
->definitions
[0] = instr
->definitions
[0];
1603 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1604 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1606 instr
.reset(new_instr
);
1611 /* s_not_b64(cmp(a, b) -> get_inverse(cmp)(a, b) */
1612 bool combine_inverse_comparison(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1614 if (instr
->opcode
!= aco_opcode::s_not_b64
)
1616 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1618 if (!instr
->operands
[0].isTemp())
1621 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[0]);
1625 aco_opcode new_opcode
= get_inverse(cmp
->opcode
);
1626 if (new_opcode
== aco_opcode::last_opcode
)
1629 if (cmp
->operands
[0].isTemp())
1630 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1631 if (cmp
->operands
[1].isTemp())
1632 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1633 decrease_uses(ctx
, cmp
);
1635 Instruction
*new_instr
;
1636 if (cmp
->isVOP3()) {
1637 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_opcode
, asVOP3(Format::VOPC
), 2, 1);
1638 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1639 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1640 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1641 new_vop3
->clamp
= cmp_vop3
->clamp
;
1642 new_vop3
->omod
= cmp_vop3
->omod
;
1643 new_vop3
->opsel
= cmp_vop3
->opsel
;
1644 new_instr
= new_vop3
;
1646 new_instr
= create_instruction
<VOPC_instruction
>(new_opcode
, Format::VOPC
, 2, 1);
1648 new_instr
->operands
[0] = cmp
->operands
[0];
1649 new_instr
->operands
[1] = cmp
->operands
[1];
1650 new_instr
->definitions
[0] = instr
->definitions
[0];
1652 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1653 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1655 instr
.reset(new_instr
);
1660 /* op1(op2(1, 2), 0) if swap = false
1661 * op1(0, op2(1, 2)) if swap = true */
1662 bool match_op3_for_vop3(opt_ctx
&ctx
, aco_opcode op1
, aco_opcode op2
,
1663 Instruction
* op1_instr
, bool swap
, const char *shuffle_str
,
1664 Operand operands
[3], bool neg
[3], bool abs
[3], uint8_t *opsel
,
1665 bool *op1_clamp
, uint8_t *op1_omod
,
1666 bool *inbetween_neg
, bool *inbetween_abs
, bool *inbetween_opsel
)
1669 if (op1_instr
->opcode
!= op1
)
1672 Instruction
*op2_instr
= follow_operand(ctx
, op1_instr
->operands
[swap
]);
1673 if (!op2_instr
|| op2_instr
->opcode
!= op2
)
1675 if (fixed_to_exec(op2_instr
->operands
[0]) || fixed_to_exec(op2_instr
->operands
[1]))
1678 VOP3A_instruction
*op1_vop3
= op1_instr
->isVOP3() ? static_cast<VOP3A_instruction
*>(op1_instr
) : NULL
;
1679 VOP3A_instruction
*op2_vop3
= op2_instr
->isVOP3() ? static_cast<VOP3A_instruction
*>(op2_instr
) : NULL
;
1681 /* don't support inbetween clamp/omod */
1682 if (op2_vop3
&& (op2_vop3
->clamp
|| op2_vop3
->omod
))
1685 /* get operands and modifiers and check inbetween modifiers */
1686 *op1_clamp
= op1_vop3
? op1_vop3
->clamp
: false;
1687 *op1_omod
= op1_vop3
? op1_vop3
->omod
: 0u;
1690 *inbetween_neg
= op1_vop3
? op1_vop3
->neg
[swap
] : false;
1691 else if (op1_vop3
&& op1_vop3
->neg
[swap
])
1695 *inbetween_abs
= op1_vop3
? op1_vop3
->abs
[swap
] : false;
1696 else if (op1_vop3
&& op1_vop3
->abs
[swap
])
1699 if (inbetween_opsel
)
1700 *inbetween_opsel
= op1_vop3
? op1_vop3
->opsel
& (1 << swap
) : false;
1701 else if (op1_vop3
&& op1_vop3
->opsel
& (1 << swap
))
1705 shuffle
[shuffle_str
[0] - '0'] = 0;
1706 shuffle
[shuffle_str
[1] - '0'] = 1;
1707 shuffle
[shuffle_str
[2] - '0'] = 2;
1709 operands
[shuffle
[0]] = op1_instr
->operands
[!swap
];
1710 neg
[shuffle
[0]] = op1_vop3
? op1_vop3
->neg
[!swap
] : false;
1711 abs
[shuffle
[0]] = op1_vop3
? op1_vop3
->abs
[!swap
] : false;
1712 if (op1_vop3
&& op1_vop3
->opsel
& (1 << !swap
))
1713 *opsel
|= 1 << shuffle
[0];
1715 for (unsigned i
= 0; i
< 2; i
++) {
1716 operands
[shuffle
[i
+ 1]] = op2_instr
->operands
[i
];
1717 neg
[shuffle
[i
+ 1]] = op2_vop3
? op2_vop3
->neg
[i
] : false;
1718 abs
[shuffle
[i
+ 1]] = op2_vop3
? op2_vop3
->abs
[i
] : false;
1719 if (op2_vop3
&& op2_vop3
->opsel
& (1 << i
))
1720 *opsel
|= 1 << shuffle
[i
+ 1];
1723 /* check operands */
1724 if (!check_vop3_operands(ctx
, 3, operands
))
1730 void create_vop3_for_op3(opt_ctx
& ctx
, aco_opcode opcode
, aco_ptr
<Instruction
>& instr
,
1731 Operand operands
[3], bool neg
[3], bool abs
[3], uint8_t opsel
,
1732 bool clamp
, unsigned omod
)
1734 VOP3A_instruction
*new_instr
= create_instruction
<VOP3A_instruction
>(opcode
, Format::VOP3A
, 3, 1);
1735 memcpy(new_instr
->abs
, abs
, sizeof(bool[3]));
1736 memcpy(new_instr
->neg
, neg
, sizeof(bool[3]));
1737 new_instr
->clamp
= clamp
;
1738 new_instr
->omod
= omod
;
1739 new_instr
->opsel
= opsel
;
1740 new_instr
->operands
[0] = operands
[0];
1741 new_instr
->operands
[1] = operands
[1];
1742 new_instr
->operands
[2] = operands
[2];
1743 new_instr
->definitions
[0] = instr
->definitions
[0];
1744 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1746 instr
.reset(new_instr
);
1749 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
)
1751 uint32_t omod_clamp
= ctx
.info
[instr
->definitions
[0].tempId()].label
&
1752 (label_omod_success
| label_clamp_success
);
1754 for (unsigned swap
= 0; swap
< 2; swap
++) {
1755 if (!((1 << swap
) & ops
))
1758 Operand operands
[3];
1759 bool neg
[3], abs
[3], clamp
;
1760 uint8_t opsel
= 0, omod
= 0;
1761 if (match_op3_for_vop3(ctx
, instr
->opcode
, op2
,
1762 instr
.get(), swap
, shuffle
,
1763 operands
, neg
, abs
, &opsel
,
1764 &clamp
, &omod
, NULL
, NULL
, NULL
)) {
1765 ctx
.uses
[instr
->operands
[swap
].tempId()]--;
1766 create_vop3_for_op3(ctx
, new_op
, instr
, operands
, neg
, abs
, opsel
, clamp
, omod
);
1767 if (omod_clamp
& label_omod_success
)
1768 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1769 if (omod_clamp
& label_clamp_success
)
1770 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
1777 bool combine_minmax(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
, aco_opcode opposite
, aco_opcode minmax3
)
1779 if (combine_three_valu_op(ctx
, instr
, instr
->opcode
, minmax3
, "012", 1 | 2))
1782 uint32_t omod_clamp
= ctx
.info
[instr
->definitions
[0].tempId()].label
&
1783 (label_omod_success
| label_clamp_success
);
1785 /* min(-max(a, b), c) -> min3(-a, -b, c) *
1786 * max(-min(a, b), c) -> max3(-a, -b, c) */
1787 for (unsigned swap
= 0; swap
< 2; swap
++) {
1788 Operand operands
[3];
1789 bool neg
[3], abs
[3], clamp
;
1790 uint8_t opsel
= 0, omod
= 0;
1792 if (match_op3_for_vop3(ctx
, instr
->opcode
, opposite
,
1793 instr
.get(), swap
, "012",
1794 operands
, neg
, abs
, &opsel
,
1795 &clamp
, &omod
, &inbetween_neg
, NULL
, NULL
) &&
1797 ctx
.uses
[instr
->operands
[swap
].tempId()]--;
1800 create_vop3_for_op3(ctx
, minmax3
, instr
, operands
, neg
, abs
, opsel
, clamp
, omod
);
1801 if (omod_clamp
& label_omod_success
)
1802 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1803 if (omod_clamp
& label_clamp_success
)
1804 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
1811 /* s_not_b32(s_and_b32(a, b)) -> s_nand_b32(a, b)
1812 * s_not_b32(s_or_b32(a, b)) -> s_nor_b32(a, b)
1813 * s_not_b32(s_xor_b32(a, b)) -> s_xnor_b32(a, b)
1814 * s_not_b64(s_and_b64(a, b)) -> s_nand_b64(a, b)
1815 * s_not_b64(s_or_b64(a, b)) -> s_nor_b64(a, b)
1816 * s_not_b64(s_xor_b64(a, b)) -> s_xnor_b64(a, b) */
1817 bool combine_salu_not_bitwise(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1820 if (!instr
->operands
[0].isTemp())
1822 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1825 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[0]);
1828 switch (op2_instr
->opcode
) {
1829 case aco_opcode::s_and_b32
:
1830 case aco_opcode::s_or_b32
:
1831 case aco_opcode::s_xor_b32
:
1832 case aco_opcode::s_and_b64
:
1833 case aco_opcode::s_or_b64
:
1834 case aco_opcode::s_xor_b64
:
1840 /* create instruction */
1841 std::swap(instr
->definitions
[0], op2_instr
->definitions
[0]);
1842 std::swap(instr
->definitions
[1], op2_instr
->definitions
[1]);
1843 ctx
.uses
[instr
->operands
[0].tempId()]--;
1844 ctx
.info
[op2_instr
->definitions
[0].tempId()].label
= 0;
1846 switch (op2_instr
->opcode
) {
1847 case aco_opcode::s_and_b32
:
1848 op2_instr
->opcode
= aco_opcode::s_nand_b32
;
1850 case aco_opcode::s_or_b32
:
1851 op2_instr
->opcode
= aco_opcode::s_nor_b32
;
1853 case aco_opcode::s_xor_b32
:
1854 op2_instr
->opcode
= aco_opcode::s_xnor_b32
;
1856 case aco_opcode::s_and_b64
:
1857 op2_instr
->opcode
= aco_opcode::s_nand_b64
;
1859 case aco_opcode::s_or_b64
:
1860 op2_instr
->opcode
= aco_opcode::s_nor_b64
;
1862 case aco_opcode::s_xor_b64
:
1863 op2_instr
->opcode
= aco_opcode::s_xnor_b64
;
1872 /* s_and_b32(a, s_not_b32(b)) -> s_andn2_b32(a, b)
1873 * s_or_b32(a, s_not_b32(b)) -> s_orn2_b32(a, b)
1874 * s_and_b64(a, s_not_b64(b)) -> s_andn2_b64(a, b)
1875 * s_or_b64(a, s_not_b64(b)) -> s_orn2_b64(a, b) */
1876 bool combine_salu_n2(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1878 if (instr
->definitions
[0].isTemp() && ctx
.info
[instr
->definitions
[0].tempId()].is_uniform_bool())
1881 for (unsigned i
= 0; i
< 2; i
++) {
1882 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[i
]);
1883 if (!op2_instr
|| (op2_instr
->opcode
!= aco_opcode::s_not_b32
&& op2_instr
->opcode
!= aco_opcode::s_not_b64
))
1885 if (ctx
.uses
[op2_instr
->definitions
[1].tempId()] || fixed_to_exec(op2_instr
->operands
[0]))
1888 if (instr
->operands
[!i
].isLiteral() && op2_instr
->operands
[0].isLiteral() &&
1889 instr
->operands
[!i
].constantValue() != op2_instr
->operands
[0].constantValue())
1892 ctx
.uses
[instr
->operands
[i
].tempId()]--;
1893 instr
->operands
[0] = instr
->operands
[!i
];
1894 instr
->operands
[1] = op2_instr
->operands
[0];
1895 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1897 switch (instr
->opcode
) {
1898 case aco_opcode::s_and_b32
:
1899 instr
->opcode
= aco_opcode::s_andn2_b32
;
1901 case aco_opcode::s_or_b32
:
1902 instr
->opcode
= aco_opcode::s_orn2_b32
;
1904 case aco_opcode::s_and_b64
:
1905 instr
->opcode
= aco_opcode::s_andn2_b64
;
1907 case aco_opcode::s_or_b64
:
1908 instr
->opcode
= aco_opcode::s_orn2_b64
;
1919 /* s_add_{i32,u32}(a, s_lshl_b32(b, <n>)) -> s_lshl<n>_add_u32(a, b) */
1920 bool combine_salu_lshl_add(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1922 if (instr
->opcode
== aco_opcode::s_add_i32
&& ctx
.uses
[instr
->definitions
[1].tempId()])
1925 for (unsigned i
= 0; i
< 2; i
++) {
1926 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[i
]);
1927 if (!op2_instr
|| op2_instr
->opcode
!= aco_opcode::s_lshl_b32
||
1928 ctx
.uses
[op2_instr
->definitions
[1].tempId()])
1930 if (!op2_instr
->operands
[1].isConstant() || fixed_to_exec(op2_instr
->operands
[0]))
1933 uint32_t shift
= op2_instr
->operands
[1].constantValue();
1934 if (shift
< 1 || shift
> 4)
1937 if (instr
->operands
[!i
].isLiteral() && op2_instr
->operands
[0].isLiteral() &&
1938 instr
->operands
[!i
].constantValue() != op2_instr
->operands
[0].constantValue())
1941 ctx
.uses
[instr
->operands
[i
].tempId()]--;
1942 instr
->operands
[1] = instr
->operands
[!i
];
1943 instr
->operands
[0] = op2_instr
->operands
[0];
1944 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1946 instr
->opcode
= ((aco_opcode
[]){aco_opcode::s_lshl1_add_u32
,
1947 aco_opcode::s_lshl2_add_u32
,
1948 aco_opcode::s_lshl3_add_u32
,
1949 aco_opcode::s_lshl4_add_u32
})[shift
- 1];
1956 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
)
1959 #define MINMAX(type, gfx9) \
1960 case aco_opcode::v_min_##type:\
1961 case aco_opcode::v_max_##type:\
1962 case aco_opcode::v_med3_##type:\
1963 *min = aco_opcode::v_min_##type;\
1964 *max = aco_opcode::v_max_##type;\
1965 *med3 = aco_opcode::v_med3_##type;\
1966 *min3 = aco_opcode::v_min3_##type;\
1967 *max3 = aco_opcode::v_max3_##type;\
1968 *some_gfx9_only = gfx9;\
1982 /* 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
1983 * 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 */
1984 bool combine_clamp(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
,
1985 aco_opcode min
, aco_opcode max
, aco_opcode med
)
1987 /* TODO: GLSL's clamp(x, minVal, maxVal) and SPIR-V's
1988 * FClamp(x, minVal, maxVal)/NClamp(x, minVal, maxVal) are undefined if
1989 * minVal > maxVal, which means we can always select it to a v_med3_f32 */
1990 aco_opcode other_op
;
1991 if (instr
->opcode
== min
)
1993 else if (instr
->opcode
== max
)
1998 uint32_t omod_clamp
= ctx
.info
[instr
->definitions
[0].tempId()].label
&
1999 (label_omod_success
| label_clamp_success
);
2001 for (unsigned swap
= 0; swap
< 2; swap
++) {
2002 Operand operands
[3];
2003 bool neg
[3], abs
[3], clamp
;
2004 uint8_t opsel
= 0, omod
= 0;
2005 if (match_op3_for_vop3(ctx
, instr
->opcode
, other_op
, instr
.get(), swap
,
2006 "012", operands
, neg
, abs
, &opsel
,
2007 &clamp
, &omod
, NULL
, NULL
, NULL
)) {
2008 int const0_idx
= -1, const1_idx
= -1;
2009 uint32_t const0
= 0, const1
= 0;
2010 for (int i
= 0; i
< 3; i
++) {
2012 if (operands
[i
].isConstant()) {
2013 val
= operands
[i
].constantValue();
2014 } else if (operands
[i
].isTemp() && ctx
.info
[operands
[i
].tempId()].is_constant_or_literal()) {
2015 val
= ctx
.info
[operands
[i
].tempId()].val
;
2019 if (const0_idx
>= 0) {
2027 if (const0_idx
< 0 || const1_idx
< 0)
2030 if (opsel
& (1 << const0_idx
))
2032 if (opsel
& (1 << const1_idx
))
2035 int lower_idx
= const0_idx
;
2037 case aco_opcode::v_min_f32
:
2038 case aco_opcode::v_min_f16
: {
2039 float const0_f
, const1_f
;
2040 if (min
== aco_opcode::v_min_f32
) {
2041 memcpy(&const0_f
, &const0
, 4);
2042 memcpy(&const1_f
, &const1
, 4);
2044 const0_f
= _mesa_half_to_float(const0
);
2045 const1_f
= _mesa_half_to_float(const1
);
2047 if (abs
[const0_idx
]) const0_f
= fabsf(const0_f
);
2048 if (abs
[const1_idx
]) const1_f
= fabsf(const1_f
);
2049 if (neg
[const0_idx
]) const0_f
= -const0_f
;
2050 if (neg
[const1_idx
]) const1_f
= -const1_f
;
2051 lower_idx
= const0_f
< const1_f
? const0_idx
: const1_idx
;
2054 case aco_opcode::v_min_u32
: {
2055 lower_idx
= const0
< const1
? const0_idx
: const1_idx
;
2058 case aco_opcode::v_min_u16
: {
2059 lower_idx
= (uint16_t)const0
< (uint16_t)const1
? const0_idx
: const1_idx
;
2062 case aco_opcode::v_min_i32
: {
2063 int32_t const0_i
= const0
& 0x80000000u
? -2147483648 + (int32_t)(const0
& 0x7fffffffu
) : const0
;
2064 int32_t const1_i
= const1
& 0x80000000u
? -2147483648 + (int32_t)(const1
& 0x7fffffffu
) : const1
;
2065 lower_idx
= const0_i
< const1_i
? const0_idx
: const1_idx
;
2068 case aco_opcode::v_min_i16
: {
2069 int16_t const0_i
= const0
& 0x8000u
? -32768 + (int16_t)(const0
& 0x7fffu
) : const0
;
2070 int16_t const1_i
= const1
& 0x8000u
? -32768 + (int16_t)(const1
& 0x7fffu
) : const1
;
2071 lower_idx
= const0_i
< const1_i
? const0_idx
: const1_idx
;
2077 int upper_idx
= lower_idx
== const0_idx
? const1_idx
: const0_idx
;
2079 if (instr
->opcode
== min
) {
2080 if (upper_idx
!= 0 || lower_idx
== 0)
2083 if (upper_idx
== 0 || lower_idx
!= 0)
2087 ctx
.uses
[instr
->operands
[swap
].tempId()]--;
2088 create_vop3_for_op3(ctx
, med
, instr
, operands
, neg
, abs
, opsel
, clamp
, omod
);
2089 if (omod_clamp
& label_omod_success
)
2090 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
2091 if (omod_clamp
& label_clamp_success
)
2092 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
2102 void apply_sgprs(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
2104 bool is_shift64
= instr
->opcode
== aco_opcode::v_lshlrev_b64
||
2105 instr
->opcode
== aco_opcode::v_lshrrev_b64
||
2106 instr
->opcode
== aco_opcode::v_ashrrev_i64
;
2108 /* find candidates and create the set of sgprs already read */
2109 unsigned sgpr_ids
[2] = {0, 0};
2110 uint32_t operand_mask
= 0;
2111 bool has_literal
= false;
2112 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
2113 if (instr
->operands
[i
].isLiteral())
2115 if (!instr
->operands
[i
].isTemp())
2117 if (instr
->operands
[i
].getTemp().type() == RegType::sgpr
) {
2118 if (instr
->operands
[i
].tempId() != sgpr_ids
[0])
2119 sgpr_ids
[!!sgpr_ids
[0]] = instr
->operands
[i
].tempId();
2121 ssa_info
& info
= ctx
.info
[instr
->operands
[i
].tempId()];
2122 if (info
.is_temp() && info
.temp
.type() == RegType::sgpr
)
2123 operand_mask
|= 1u << i
;
2125 unsigned max_sgprs
= 1;
2126 if (ctx
.program
->chip_class
>= GFX10
&& !is_shift64
)
2131 unsigned num_sgprs
= !!sgpr_ids
[0] + !!sgpr_ids
[1];
2133 /* keep on applying sgprs until there is nothing left to be done */
2134 while (operand_mask
) {
2135 uint32_t sgpr_idx
= 0;
2136 uint32_t sgpr_info_id
= 0;
2137 uint32_t mask
= operand_mask
;
2140 unsigned i
= u_bit_scan(&mask
);
2141 uint16_t uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2142 if (sgpr_info_id
== 0 || uses
< ctx
.uses
[sgpr_info_id
]) {
2144 sgpr_info_id
= instr
->operands
[i
].tempId();
2147 operand_mask
&= ~(1u << sgpr_idx
);
2149 /* Applying two sgprs require making it VOP3, so don't do it unless it's
2150 * definitively beneficial.
2151 * TODO: this is too conservative because later the use count could be reduced to 1 */
2152 if (num_sgprs
&& ctx
.uses
[sgpr_info_id
] > 1 && !instr
->isVOP3())
2155 Temp sgpr
= ctx
.info
[sgpr_info_id
].temp
;
2156 bool new_sgpr
= sgpr
.id() != sgpr_ids
[0] && sgpr
.id() != sgpr_ids
[1];
2157 if (new_sgpr
&& num_sgprs
>= max_sgprs
)
2160 if (sgpr_idx
== 0 || instr
->isVOP3()) {
2161 instr
->operands
[sgpr_idx
] = Operand(sgpr
);
2162 } else if (can_swap_operands(instr
)) {
2163 instr
->operands
[sgpr_idx
] = instr
->operands
[0];
2164 instr
->operands
[0] = Operand(sgpr
);
2165 /* swap bits using a 4-entry LUT */
2166 uint32_t swapped
= (0x3120 >> (operand_mask
& 0x3)) & 0xf;
2167 operand_mask
= (operand_mask
& ~0x3) | swapped
;
2168 } else if (can_use_VOP3(ctx
, instr
)) {
2169 to_VOP3(ctx
, instr
);
2170 instr
->operands
[sgpr_idx
] = Operand(sgpr
);
2176 sgpr_ids
[num_sgprs
++] = sgpr
.id();
2177 ctx
.uses
[sgpr_info_id
]--;
2178 ctx
.uses
[sgpr
.id()]++;
2182 bool apply_omod_clamp(opt_ctx
&ctx
, Block
& block
, aco_ptr
<Instruction
>& instr
)
2184 /* check if we could apply omod on predecessor */
2185 if (instr
->opcode
== aco_opcode::v_mul_f32
) {
2186 bool op0
= instr
->operands
[0].isTemp() && ctx
.info
[instr
->operands
[0].tempId()].is_omod_success();
2187 bool op1
= instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_omod_success();
2189 unsigned idx
= op0
? 0 : 1;
2190 /* omod was successfully applied */
2191 /* if the omod instruction is v_mad, we also have to change the original add */
2192 if (ctx
.info
[instr
->operands
[idx
].tempId()].is_mad()) {
2193 Instruction
* add_instr
= ctx
.mad_infos
[ctx
.info
[instr
->operands
[idx
].tempId()].val
].add_instr
.get();
2194 if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp())
2195 static_cast<VOP3A_instruction
*>(add_instr
)->clamp
= true;
2196 add_instr
->definitions
[0] = instr
->definitions
[0];
2199 Instruction
* omod_instr
= ctx
.info
[instr
->operands
[idx
].tempId()].instr
;
2200 /* check if we have an additional clamp modifier */
2201 if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp() && ctx
.uses
[instr
->definitions
[0].tempId()] == 1 &&
2202 ctx
.uses
[ctx
.info
[instr
->definitions
[0].tempId()].temp
.id()]) {
2203 static_cast<VOP3A_instruction
*>(omod_instr
)->clamp
= true;
2204 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(omod_instr
);
2206 /* change definition ssa-id of modified instruction */
2207 omod_instr
->definitions
[0] = instr
->definitions
[0];
2209 /* change the definition of instr to something unused, e.g. the original omod def */
2210 instr
->definitions
[0] = Definition(instr
->operands
[idx
].getTemp());
2211 ctx
.uses
[instr
->definitions
[0].tempId()] = 0;
2214 if (!ctx
.info
[instr
->definitions
[0].tempId()].label
) {
2215 /* in all other cases, label this instruction as option for multiply-add */
2216 ctx
.info
[instr
->definitions
[0].tempId()].set_mul(instr
.get());
2220 /* check if we could apply clamp on predecessor */
2221 if (instr
->opcode
== aco_opcode::v_med3_f32
) {
2223 bool found_zero
= false, found_one
= false;
2224 for (unsigned i
= 0; i
< 3; i
++)
2226 if (instr
->operands
[i
].constantEquals(0))
2228 else if (instr
->operands
[i
].constantEquals(0x3f800000)) /* 1.0 */
2233 if (found_zero
&& found_one
&& instr
->operands
[idx
].isTemp() &&
2234 ctx
.info
[instr
->operands
[idx
].tempId()].is_clamp_success()) {
2235 /* clamp was successfully applied */
2236 /* if the clamp instruction is v_mad, we also have to change the original add */
2237 if (ctx
.info
[instr
->operands
[idx
].tempId()].is_mad()) {
2238 Instruction
* add_instr
= ctx
.mad_infos
[ctx
.info
[instr
->operands
[idx
].tempId()].val
].add_instr
.get();
2239 add_instr
->definitions
[0] = instr
->definitions
[0];
2241 Instruction
* clamp_instr
= ctx
.info
[instr
->operands
[idx
].tempId()].instr
;
2242 /* change definition ssa-id of modified instruction */
2243 clamp_instr
->definitions
[0] = instr
->definitions
[0];
2245 /* change the definition of instr to something unused, e.g. the original omod def */
2246 instr
->definitions
[0] = Definition(instr
->operands
[idx
].getTemp());
2247 ctx
.uses
[instr
->definitions
[0].tempId()] = 0;
2252 /* omod has no effect if denormals are enabled */
2253 bool can_use_omod
= block
.fp_mode
.denorm32
== 0;
2255 /* apply omod / clamp modifiers if the def is used only once and the instruction can have modifiers */
2256 if (!instr
->definitions
.empty() && ctx
.uses
[instr
->definitions
[0].tempId()] == 1 &&
2257 can_use_VOP3(ctx
, instr
) && instr_info
.can_use_output_modifiers
[(int)instr
->opcode
]) {
2258 ssa_info
& def_info
= ctx
.info
[instr
->definitions
[0].tempId()];
2259 if (can_use_omod
&& def_info
.is_omod2() && ctx
.uses
[def_info
.temp
.id()]) {
2260 to_VOP3(ctx
, instr
);
2261 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 1;
2262 def_info
.set_omod_success(instr
.get());
2263 } else if (can_use_omod
&& def_info
.is_omod4() && ctx
.uses
[def_info
.temp
.id()]) {
2264 to_VOP3(ctx
, instr
);
2265 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 2;
2266 def_info
.set_omod_success(instr
.get());
2267 } else if (can_use_omod
&& def_info
.is_omod5() && ctx
.uses
[def_info
.temp
.id()]) {
2268 to_VOP3(ctx
, instr
);
2269 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 3;
2270 def_info
.set_omod_success(instr
.get());
2271 } else if (def_info
.is_clamp() && ctx
.uses
[def_info
.temp
.id()]) {
2272 to_VOP3(ctx
, instr
);
2273 static_cast<VOP3A_instruction
*>(instr
.get())->clamp
= true;
2274 def_info
.set_clamp_success(instr
.get());
2281 // TODO: we could possibly move the whole label_instruction pass to combine_instruction:
2282 // this would mean that we'd have to fix the instruction uses while value propagation
2284 void combine_instruction(opt_ctx
&ctx
, Block
& block
, aco_ptr
<Instruction
>& instr
)
2286 if (instr
->definitions
.empty() || is_dead(ctx
.uses
, instr
.get()))
2289 if (instr
->isVALU()) {
2290 if (can_apply_sgprs(instr
))
2291 apply_sgprs(ctx
, instr
);
2292 if (apply_omod_clamp(ctx
, block
, instr
))
2296 if (ctx
.info
[instr
->definitions
[0].tempId()].is_vcc_hint()) {
2297 instr
->definitions
[0].setHint(vcc
);
2300 /* TODO: There are still some peephole optimizations that could be done:
2301 * - abs(a - b) -> s_absdiff_i32
2302 * - various patterns for s_bitcmp{0,1}_b32 and s_bitset{0,1}_b32
2303 * - patterns for v_alignbit_b32 and v_alignbyte_b32
2304 * These aren't probably too interesting though.
2305 * There are also patterns for v_cmp_class_f{16,32,64}. This is difficult but
2306 * probably more useful than the previously mentioned optimizations.
2307 * The various comparison optimizations also currently only work with 32-bit
2310 /* neg(mul(a, b)) -> mul(neg(a), b) */
2311 if (ctx
.info
[instr
->definitions
[0].tempId()].is_neg() && ctx
.uses
[instr
->operands
[1].tempId()] == 1) {
2312 Temp val
= ctx
.info
[instr
->definitions
[0].tempId()].temp
;
2314 if (!ctx
.info
[val
.id()].is_mul())
2317 Instruction
* mul_instr
= ctx
.info
[val
.id()].instr
;
2319 if (mul_instr
->operands
[0].isLiteral())
2321 if (mul_instr
->isVOP3() && static_cast<VOP3A_instruction
*>(mul_instr
)->clamp
)
2324 /* convert to mul(neg(a), b) */
2325 ctx
.uses
[mul_instr
->definitions
[0].tempId()]--;
2326 Definition def
= instr
->definitions
[0];
2327 /* neg(abs(mul(a, b))) -> mul(neg(abs(a)), abs(b)) */
2328 bool is_abs
= ctx
.info
[instr
->definitions
[0].tempId()].is_abs();
2329 instr
.reset(create_instruction
<VOP3A_instruction
>(aco_opcode::v_mul_f32
, asVOP3(Format::VOP2
), 2, 1));
2330 instr
->operands
[0] = mul_instr
->operands
[0];
2331 instr
->operands
[1] = mul_instr
->operands
[1];
2332 instr
->definitions
[0] = def
;
2333 VOP3A_instruction
* new_mul
= static_cast<VOP3A_instruction
*>(instr
.get());
2334 if (mul_instr
->isVOP3()) {
2335 VOP3A_instruction
* mul
= static_cast<VOP3A_instruction
*>(mul_instr
);
2336 new_mul
->neg
[0] = mul
->neg
[0] && !is_abs
;
2337 new_mul
->neg
[1] = mul
->neg
[1] && !is_abs
;
2338 new_mul
->abs
[0] = mul
->abs
[0] || is_abs
;
2339 new_mul
->abs
[1] = mul
->abs
[1] || is_abs
;
2340 new_mul
->omod
= mul
->omod
;
2342 new_mul
->neg
[0] ^= true;
2343 new_mul
->clamp
= false;
2345 ctx
.info
[instr
->definitions
[0].tempId()].set_mul(instr
.get());
2348 /* combine mul+add -> mad */
2349 else if ((instr
->opcode
== aco_opcode::v_add_f32
||
2350 instr
->opcode
== aco_opcode::v_sub_f32
||
2351 instr
->opcode
== aco_opcode::v_subrev_f32
) &&
2352 block
.fp_mode
.denorm32
== 0 && !block
.fp_mode
.preserve_signed_zero_inf_nan32
) {
2353 //TODO: we could use fma instead when denormals are enabled if the NIR isn't marked as precise
2355 uint32_t uses_src0
= UINT32_MAX
;
2356 uint32_t uses_src1
= UINT32_MAX
;
2357 Instruction
* mul_instr
= nullptr;
2358 unsigned add_op_idx
;
2359 /* check if any of the operands is a multiplication */
2360 if (instr
->operands
[0].isTemp() && ctx
.info
[instr
->operands
[0].tempId()].is_mul())
2361 uses_src0
= ctx
.uses
[instr
->operands
[0].tempId()];
2362 if (instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_mul())
2363 uses_src1
= ctx
.uses
[instr
->operands
[1].tempId()];
2365 /* find the 'best' mul instruction to combine with the add */
2366 if (uses_src0
< uses_src1
) {
2367 mul_instr
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
2369 } else if (uses_src1
< uses_src0
) {
2370 mul_instr
= ctx
.info
[instr
->operands
[1].tempId()].instr
;
2372 } else if (uses_src0
!= UINT32_MAX
) {
2373 /* tiebreaker: quite random what to pick */
2374 if (ctx
.info
[instr
->operands
[0].tempId()].instr
->operands
[0].isLiteral()) {
2375 mul_instr
= ctx
.info
[instr
->operands
[1].tempId()].instr
;
2378 mul_instr
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
2383 Operand op
[3] = {Operand(v1
), Operand(v1
), Operand(v1
)};
2384 bool neg
[3] = {false, false, false};
2385 bool abs
[3] = {false, false, false};
2388 op
[0] = mul_instr
->operands
[0];
2389 op
[1] = mul_instr
->operands
[1];
2390 op
[2] = instr
->operands
[add_op_idx
];
2391 // TODO: would be better to check this before selecting a mul instr?
2392 if (!check_vop3_operands(ctx
, 3, op
))
2395 if (mul_instr
->isVOP3()) {
2396 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*> (mul_instr
);
2397 neg
[0] = vop3
->neg
[0];
2398 neg
[1] = vop3
->neg
[1];
2399 abs
[0] = vop3
->abs
[0];
2400 abs
[1] = vop3
->abs
[1];
2401 /* we cannot use these modifiers between mul and add */
2402 if (vop3
->clamp
|| vop3
->omod
)
2406 /* convert to mad */
2407 ctx
.uses
[mul_instr
->definitions
[0].tempId()]--;
2408 if (ctx
.uses
[mul_instr
->definitions
[0].tempId()]) {
2410 ctx
.uses
[op
[0].tempId()]++;
2412 ctx
.uses
[op
[1].tempId()]++;
2415 if (instr
->isVOP3()) {
2416 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*> (instr
.get());
2417 neg
[2] = vop3
->neg
[add_op_idx
];
2418 abs
[2] = vop3
->abs
[add_op_idx
];
2420 clamp
= vop3
->clamp
;
2421 /* abs of the multiplication result */
2422 if (vop3
->abs
[1 - add_op_idx
]) {
2428 /* neg of the multiplication result */
2429 neg
[1] = neg
[1] ^ vop3
->neg
[1 - add_op_idx
];
2431 if (instr
->opcode
== aco_opcode::v_sub_f32
)
2432 neg
[1 + add_op_idx
] = neg
[1 + add_op_idx
] ^ true;
2433 else if (instr
->opcode
== aco_opcode::v_subrev_f32
)
2434 neg
[2 - add_op_idx
] = neg
[2 - add_op_idx
] ^ true;
2436 aco_ptr
<VOP3A_instruction
> mad
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_mad_f32
, Format::VOP3A
, 3, 1)};
2437 for (unsigned i
= 0; i
< 3; i
++)
2439 mad
->operands
[i
] = op
[i
];
2440 mad
->neg
[i
] = neg
[i
];
2441 mad
->abs
[i
] = abs
[i
];
2445 mad
->definitions
[0] = instr
->definitions
[0];
2447 /* mark this ssa_def to be re-checked for profitability and literals */
2448 ctx
.mad_infos
.emplace_back(std::move(instr
), mul_instr
->definitions
[0].tempId());
2449 ctx
.info
[mad
->definitions
[0].tempId()].set_mad(mad
.get(), ctx
.mad_infos
.size() - 1);
2450 instr
.reset(mad
.release());
2454 /* v_mul_f32(v_cndmask_b32(0, 1.0, cond), a) -> v_cndmask_b32(0, a, cond) */
2455 else if (instr
->opcode
== aco_opcode::v_mul_f32
&& !instr
->isVOP3()) {
2456 for (unsigned i
= 0; i
< 2; i
++) {
2457 if (instr
->operands
[i
].isTemp() && ctx
.info
[instr
->operands
[i
].tempId()].is_b2f() &&
2458 ctx
.uses
[instr
->operands
[i
].tempId()] == 1 &&
2459 instr
->operands
[!i
].isTemp() && instr
->operands
[!i
].getTemp().type() == RegType::vgpr
) {
2460 ctx
.uses
[instr
->operands
[i
].tempId()]--;
2461 ctx
.uses
[ctx
.info
[instr
->operands
[i
].tempId()].temp
.id()]++;
2463 aco_ptr
<VOP2_instruction
> new_instr
{create_instruction
<VOP2_instruction
>(aco_opcode::v_cndmask_b32
, Format::VOP2
, 3, 1)};
2464 new_instr
->operands
[0] = Operand(0u);
2465 new_instr
->operands
[1] = instr
->operands
[!i
];
2466 new_instr
->operands
[2] = Operand(ctx
.info
[instr
->operands
[i
].tempId()].temp
);
2467 new_instr
->definitions
[0] = instr
->definitions
[0];
2468 instr
.reset(new_instr
.release());
2469 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
2473 } else if (instr
->opcode
== aco_opcode::v_or_b32
&& ctx
.program
->chip_class
>= GFX9
) {
2474 if (combine_three_valu_op(ctx
, instr
, aco_opcode::s_or_b32
, aco_opcode::v_or3_b32
, "012", 1 | 2)) ;
2475 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_or_b32
, aco_opcode::v_or3_b32
, "012", 1 | 2)) ;
2476 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::s_and_b32
, aco_opcode::v_and_or_b32
, "120", 1 | 2)) ;
2477 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_and_b32
, aco_opcode::v_and_or_b32
, "120", 1 | 2)) ;
2478 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::s_lshl_b32
, aco_opcode::v_lshl_or_b32
, "201", 1 | 2)) ;
2479 else combine_three_valu_op(ctx
, instr
, aco_opcode::v_lshlrev_b32
, aco_opcode::v_lshl_or_b32
, "210", 1 | 2);
2480 } else if (instr
->opcode
== aco_opcode::v_add_u32
&& ctx
.program
->chip_class
>= GFX9
) {
2481 if (combine_three_valu_op(ctx
, instr
, aco_opcode::s_xor_b32
, aco_opcode::v_xad_u32
, "120", 1 | 2)) ;
2482 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_xor_b32
, aco_opcode::v_xad_u32
, "120", 1 | 2)) ;
2483 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::s_add_i32
, aco_opcode::v_add3_u32
, "012", 1 | 2)) ;
2484 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::s_add_u32
, aco_opcode::v_add3_u32
, "012", 1 | 2)) ;
2485 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_add_u32
, aco_opcode::v_add3_u32
, "012", 1 | 2)) ;
2486 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::s_lshl_b32
, aco_opcode::v_lshl_add_u32
, "201", 1 | 2)) ;
2487 else combine_three_valu_op(ctx
, instr
, aco_opcode::v_lshlrev_b32
, aco_opcode::v_lshl_add_u32
, "210", 1 | 2);
2488 } else if (instr
->opcode
== aco_opcode::v_lshlrev_b32
&& ctx
.program
->chip_class
>= GFX9
) {
2489 combine_three_valu_op(ctx
, instr
, aco_opcode::v_add_u32
, aco_opcode::v_add_lshl_u32
, "120", 2);
2490 } else if ((instr
->opcode
== aco_opcode::s_add_u32
|| instr
->opcode
== aco_opcode::s_add_i32
) && ctx
.program
->chip_class
>= GFX9
) {
2491 combine_salu_lshl_add(ctx
, instr
);
2492 } else if (instr
->opcode
== aco_opcode::s_not_b32
) {
2493 combine_salu_not_bitwise(ctx
, instr
);
2494 } else if (instr
->opcode
== aco_opcode::s_not_b64
) {
2495 if (combine_inverse_comparison(ctx
, instr
)) ;
2496 else combine_salu_not_bitwise(ctx
, instr
);
2497 } else if (instr
->opcode
== aco_opcode::s_and_b32
|| instr
->opcode
== aco_opcode::s_or_b32
||
2498 instr
->opcode
== aco_opcode::s_and_b64
|| instr
->opcode
== aco_opcode::s_or_b64
) {
2499 if (combine_ordering_test(ctx
, instr
)) ;
2500 else if (combine_comparison_ordering(ctx
, instr
)) ;
2501 else if (combine_constant_comparison_ordering(ctx
, instr
)) ;
2502 else combine_salu_n2(ctx
, instr
);
2504 aco_opcode min
, max
, min3
, max3
, med3
;
2505 bool some_gfx9_only
;
2506 if (get_minmax_info(instr
->opcode
, &min
, &max
, &min3
, &max3
, &med3
, &some_gfx9_only
) &&
2507 (!some_gfx9_only
|| ctx
.program
->chip_class
>= GFX9
)) {
2508 if (combine_minmax(ctx
, instr
, instr
->opcode
== min
? max
: min
, instr
->opcode
== min
? min3
: max3
)) ;
2509 else combine_clamp(ctx
, instr
, min
, max
, med3
);
2514 bool to_uniform_bool_instr(opt_ctx
&ctx
, aco_ptr
<Instruction
> &instr
)
2516 switch (instr
->opcode
) {
2517 case aco_opcode::s_and_b32
:
2518 case aco_opcode::s_and_b64
:
2519 instr
->opcode
= aco_opcode::s_and_b32
;
2521 case aco_opcode::s_or_b32
:
2522 case aco_opcode::s_or_b64
:
2523 instr
->opcode
= aco_opcode::s_or_b32
;
2525 case aco_opcode::s_xor_b32
:
2526 case aco_opcode::s_xor_b64
:
2527 instr
->opcode
= aco_opcode::s_absdiff_i32
;
2530 /* Don't transform other instructions. They are very unlikely to appear here. */
2534 for (Operand
&op
: instr
->operands
) {
2535 ctx
.uses
[op
.tempId()]--;
2537 if (ctx
.info
[op
.tempId()].is_uniform_bool()) {
2538 /* Just use the uniform boolean temp. */
2539 op
.setTemp(ctx
.info
[op
.tempId()].temp
);
2540 } else if (ctx
.info
[op
.tempId()].is_uniform_bitwise()) {
2541 /* Use the SCC definition of the predecessor instruction.
2542 * This allows the predecessor to get picked up by the same optimization (if it has no divergent users),
2543 * and it also makes sure that the current instruction will keep working even if the predecessor won't be transformed.
2545 Instruction
*pred_instr
= ctx
.info
[op
.tempId()].instr
;
2546 assert(pred_instr
->definitions
.size() >= 2);
2547 assert(pred_instr
->definitions
[1].isFixed() && pred_instr
->definitions
[1].physReg() == scc
);
2548 op
.setTemp(pred_instr
->definitions
[1].getTemp());
2550 unreachable("Invalid operand on uniform bitwise instruction.");
2553 ctx
.uses
[op
.tempId()]++;
2556 instr
->definitions
[0].setTemp(Temp(instr
->definitions
[0].tempId(), s1
));
2557 assert(instr
->operands
[0].regClass() == s1
);
2558 assert(instr
->operands
[1].regClass() == s1
);
2562 void select_instruction(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
2564 const uint32_t threshold
= 4;
2566 if (is_dead(ctx
.uses
, instr
.get())) {
2571 /* convert split_vector into a copy or extract_vector if only one definition is ever used */
2572 if (instr
->opcode
== aco_opcode::p_split_vector
) {
2573 unsigned num_used
= 0;
2575 unsigned split_offset
= 0;
2576 for (unsigned i
= 0, offset
= 0; i
< instr
->definitions
.size(); offset
+= instr
->definitions
[i
++].bytes()) {
2577 if (ctx
.uses
[instr
->definitions
[i
].tempId()]) {
2580 split_offset
= offset
;
2584 if (num_used
== 1 && ctx
.info
[instr
->operands
[0].tempId()].is_vec() &&
2585 ctx
.uses
[instr
->operands
[0].tempId()] == 1) {
2586 Instruction
*vec
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
2590 for (Operand
& vec_op
: vec
->operands
) {
2591 if (off
== split_offset
) {
2595 off
+= vec_op
.bytes();
2597 if (off
!= instr
->operands
[0].bytes() && op
.bytes() == instr
->definitions
[idx
].bytes()) {
2598 ctx
.uses
[instr
->operands
[0].tempId()]--;
2599 for (Operand
& vec_op
: vec
->operands
) {
2600 if (vec_op
.isTemp())
2601 ctx
.uses
[vec_op
.tempId()]--;
2604 ctx
.uses
[op
.tempId()]++;
2606 aco_ptr
<Pseudo_instruction
> extract
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, 1, 1)};
2607 extract
->operands
[0] = op
;
2608 extract
->definitions
[0] = instr
->definitions
[idx
];
2609 instr
.reset(extract
.release());
2615 if (!done
&& num_used
== 1 &&
2616 instr
->operands
[0].bytes() % instr
->definitions
[idx
].bytes() == 0 &&
2617 split_offset
% instr
->definitions
[idx
].bytes() == 0) {
2618 aco_ptr
<Pseudo_instruction
> extract
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_extract_vector
, Format::PSEUDO
, 2, 1)};
2619 extract
->operands
[0] = instr
->operands
[0];
2620 extract
->operands
[1] = Operand((uint32_t) split_offset
/ instr
->definitions
[idx
].bytes());
2621 extract
->definitions
[0] = instr
->definitions
[idx
];
2622 instr
.reset(extract
.release());
2626 mad_info
* mad_info
= NULL
;
2627 if (instr
->opcode
== aco_opcode::v_mad_f32
&& ctx
.info
[instr
->definitions
[0].tempId()].is_mad()) {
2628 mad_info
= &ctx
.mad_infos
[ctx
.info
[instr
->definitions
[0].tempId()].val
];
2629 /* re-check mad instructions */
2630 if (ctx
.uses
[mad_info
->mul_temp_id
]) {
2631 ctx
.uses
[mad_info
->mul_temp_id
]++;
2632 if (instr
->operands
[0].isTemp())
2633 ctx
.uses
[instr
->operands
[0].tempId()]--;
2634 if (instr
->operands
[1].isTemp())
2635 ctx
.uses
[instr
->operands
[1].tempId()]--;
2636 instr
.swap(mad_info
->add_instr
);
2639 /* check literals */
2640 else if (!instr
->usesModifiers()) {
2641 bool sgpr_used
= false;
2642 uint32_t literal_idx
= 0;
2643 uint32_t literal_uses
= UINT32_MAX
;
2644 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
2646 if (instr
->operands
[i
].isConstant() && i
> 0) {
2647 literal_uses
= UINT32_MAX
;
2650 if (!instr
->operands
[i
].isTemp())
2652 /* if one of the operands is sgpr, we cannot add a literal somewhere else on pre-GFX10 or operands other than the 1st */
2653 if (instr
->operands
[i
].getTemp().type() == RegType::sgpr
&& (i
> 0 || ctx
.program
->chip_class
< GFX10
)) {
2654 if (!sgpr_used
&& ctx
.info
[instr
->operands
[i
].tempId()].is_literal()) {
2655 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2658 literal_uses
= UINT32_MAX
;
2661 /* don't break because we still need to check constants */
2662 } else if (!sgpr_used
&&
2663 ctx
.info
[instr
->operands
[i
].tempId()].is_literal() &&
2664 ctx
.uses
[instr
->operands
[i
].tempId()] < literal_uses
) {
2665 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2670 /* Limit the number of literals to apply to not increase the code
2671 * size too much, but always apply literals for v_mad->v_madak
2672 * because both instructions are 64-bit and this doesn't increase
2674 * TODO: try to apply the literals earlier to lower the number of
2675 * uses below threshold
2677 if (literal_uses
< threshold
|| literal_idx
== 2) {
2678 ctx
.uses
[instr
->operands
[literal_idx
].tempId()]--;
2679 mad_info
->check_literal
= true;
2680 mad_info
->literal_idx
= literal_idx
;
2686 /* Mark SCC needed, so the uniform boolean transformation won't swap the definitions when it isn't beneficial */
2687 if (instr
->format
== Format::PSEUDO_BRANCH
&&
2688 instr
->operands
.size() &&
2689 instr
->operands
[0].isTemp()) {
2690 ctx
.info
[instr
->operands
[0].tempId()].set_scc_needed();
2692 } else if ((instr
->opcode
== aco_opcode::s_cselect_b64
||
2693 instr
->opcode
== aco_opcode::s_cselect_b32
) &&
2694 instr
->operands
[2].isTemp()) {
2695 ctx
.info
[instr
->operands
[2].tempId()].set_scc_needed();
2698 /* check for literals */
2699 if (!instr
->isSALU() && !instr
->isVALU())
2702 /* Transform uniform bitwise boolean operations to 32-bit when there are no divergent uses. */
2703 if (instr
->definitions
.size() &&
2704 ctx
.uses
[instr
->definitions
[0].tempId()] == 0 &&
2705 ctx
.info
[instr
->definitions
[0].tempId()].is_uniform_bitwise()) {
2706 bool transform_done
= to_uniform_bool_instr(ctx
, instr
);
2708 if (transform_done
&& !ctx
.info
[instr
->definitions
[1].tempId()].is_scc_needed()) {
2709 /* Swap the two definition IDs in order to avoid overusing the SCC. This reduces extra moves generated by RA. */
2710 uint32_t def0_id
= instr
->definitions
[0].getTemp().id();
2711 uint32_t def1_id
= instr
->definitions
[1].getTemp().id();
2712 instr
->definitions
[0].setTemp(Temp(def1_id
, s1
));
2713 instr
->definitions
[1].setTemp(Temp(def0_id
, s1
));
2719 if (instr
->isSDWA() || instr
->isDPP() || (instr
->isVOP3() && ctx
.program
->chip_class
< GFX10
))
2720 return; /* some encodings can't ever take literals */
2722 /* we do not apply the literals yet as we don't know if it is profitable */
2723 Operand
current_literal(s1
);
2725 unsigned literal_id
= 0;
2726 unsigned literal_uses
= UINT32_MAX
;
2727 Operand
literal(s1
);
2728 unsigned num_operands
= 1;
2729 if (instr
->isSALU() || (ctx
.program
->chip_class
>= GFX10
&& can_use_VOP3(ctx
, instr
)))
2730 num_operands
= instr
->operands
.size();
2731 /* catch VOP2 with a 3rd SGPR operand (e.g. v_cndmask_b32, v_addc_co_u32) */
2732 else if (instr
->isVALU() && instr
->operands
.size() >= 3)
2735 unsigned sgpr_ids
[2] = {0, 0};
2736 bool is_literal_sgpr
= false;
2739 /* choose a literal to apply */
2740 for (unsigned i
= 0; i
< num_operands
; i
++) {
2741 Operand op
= instr
->operands
[i
];
2743 if (instr
->isVALU() && op
.isTemp() && op
.getTemp().type() == RegType::sgpr
&&
2744 op
.tempId() != sgpr_ids
[0])
2745 sgpr_ids
[!!sgpr_ids
[0]] = op
.tempId();
2747 if (op
.isLiteral()) {
2748 current_literal
= op
;
2750 } else if (!op
.isTemp() || !ctx
.info
[op
.tempId()].is_literal()) {
2754 if (!alu_can_accept_constant(instr
->opcode
, i
))
2757 if (ctx
.uses
[op
.tempId()] < literal_uses
) {
2758 is_literal_sgpr
= op
.getTemp().type() == RegType::sgpr
;
2760 literal
= Operand(ctx
.info
[op
.tempId()].val
);
2761 literal_uses
= ctx
.uses
[op
.tempId()];
2762 literal_id
= op
.tempId();
2765 mask
|= (op
.tempId() == literal_id
) << i
;
2769 /* don't go over the constant bus limit */
2770 bool is_shift64
= instr
->opcode
== aco_opcode::v_lshlrev_b64
||
2771 instr
->opcode
== aco_opcode::v_lshrrev_b64
||
2772 instr
->opcode
== aco_opcode::v_ashrrev_i64
;
2773 unsigned const_bus_limit
= instr
->isVALU() ? 1 : UINT32_MAX
;
2774 if (ctx
.program
->chip_class
>= GFX10
&& !is_shift64
)
2775 const_bus_limit
= 2;
2777 unsigned num_sgprs
= !!sgpr_ids
[0] + !!sgpr_ids
[1];
2778 if (num_sgprs
== const_bus_limit
&& !is_literal_sgpr
)
2781 if (literal_id
&& literal_uses
< threshold
&&
2782 (current_literal
.isUndefined() ||
2783 (current_literal
.size() == literal
.size() &&
2784 current_literal
.constantValue() == literal
.constantValue()))) {
2785 /* mark the literal to be applied */
2787 unsigned i
= u_bit_scan(&mask
);
2788 if (instr
->operands
[i
].isTemp() && instr
->operands
[i
].tempId() == literal_id
)
2789 ctx
.uses
[instr
->operands
[i
].tempId()]--;
2795 void apply_literals(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
2797 /* Cleanup Dead Instructions */
2801 /* apply literals on MAD */
2802 if (instr
->opcode
== aco_opcode::v_mad_f32
&& ctx
.info
[instr
->definitions
[0].tempId()].is_mad()) {
2803 mad_info
* info
= &ctx
.mad_infos
[ctx
.info
[instr
->definitions
[0].tempId()].val
];
2804 if (info
->check_literal
&&
2805 (ctx
.uses
[instr
->operands
[info
->literal_idx
].tempId()] == 0 || info
->literal_idx
== 2)) {
2806 aco_ptr
<Instruction
> new_mad
;
2807 if (info
->literal_idx
== 2) { /* add literal -> madak */
2808 new_mad
.reset(create_instruction
<VOP2_instruction
>(aco_opcode::v_madak_f32
, Format::VOP2
, 3, 1));
2809 new_mad
->operands
[0] = instr
->operands
[0];
2810 new_mad
->operands
[1] = instr
->operands
[1];
2811 } else { /* mul literal -> madmk */
2812 new_mad
.reset(create_instruction
<VOP2_instruction
>(aco_opcode::v_madmk_f32
, Format::VOP2
, 3, 1));
2813 new_mad
->operands
[0] = instr
->operands
[1 - info
->literal_idx
];
2814 new_mad
->operands
[1] = instr
->operands
[2];
2816 new_mad
->operands
[2] = Operand(ctx
.info
[instr
->operands
[info
->literal_idx
].tempId()].val
);
2817 new_mad
->definitions
[0] = instr
->definitions
[0];
2818 ctx
.instructions
.emplace_back(std::move(new_mad
));
2823 /* apply literals on other SALU/VALU */
2824 if (instr
->isSALU() || instr
->isVALU()) {
2825 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
2826 Operand op
= instr
->operands
[i
];
2827 if (op
.isTemp() && ctx
.info
[op
.tempId()].is_literal() && ctx
.uses
[op
.tempId()] == 0) {
2828 Operand
literal(ctx
.info
[op
.tempId()].val
);
2829 if (instr
->isVALU() && i
> 0)
2830 to_VOP3(ctx
, instr
);
2831 instr
->operands
[i
] = literal
;
2836 ctx
.instructions
.emplace_back(std::move(instr
));
2840 void optimize(Program
* program
)
2843 ctx
.program
= program
;
2844 std::vector
<ssa_info
> info(program
->peekAllocationId());
2845 ctx
.info
= info
.data();
2847 /* 1. Bottom-Up DAG pass (forward) to label all ssa-defs */
2848 for (Block
& block
: program
->blocks
) {
2849 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2850 label_instruction(ctx
, block
, instr
);
2853 ctx
.uses
= std::move(dead_code_analysis(program
));
2855 /* 2. Combine v_mad, omod, clamp and propagate sgpr on VALU instructions */
2856 for (Block
& block
: program
->blocks
) {
2857 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2858 combine_instruction(ctx
, block
, instr
);
2861 /* 3. Top-Down DAG pass (backward) to select instructions (includes DCE) */
2862 for (std::vector
<Block
>::reverse_iterator it
= program
->blocks
.rbegin(); it
!= program
->blocks
.rend(); ++it
) {
2863 Block
* block
= &(*it
);
2864 for (std::vector
<aco_ptr
<Instruction
>>::reverse_iterator it
= block
->instructions
.rbegin(); it
!= block
->instructions
.rend(); ++it
)
2865 select_instruction(ctx
, *it
);
2868 /* 4. Add literals to instructions */
2869 for (Block
& block
: program
->blocks
) {
2870 ctx
.instructions
.clear();
2871 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2872 apply_literals(ctx
, instr
);
2873 block
.instructions
.swap(ctx
.instructions
);