2 * Copyright © 2018 Valve Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Daniel Schürmann (daniel.schuermann@campus.tu-berlin.de)
32 #include "util/half_float.h"
33 #include "util/u_math.h"
38 * The optimizer works in 4 phases:
39 * (1) The first pass collects information for each ssa-def,
40 * propagates reg->reg operands of the same type, inline constants
41 * and neg/abs input modifiers.
42 * (2) The second pass combines instructions like mad, omod, clamp and
43 * propagates sgpr's on VALU instructions.
44 * This pass depends on information collected in the first pass.
45 * (3) The third pass goes backwards, and selects instructions,
46 * i.e. decides if a mad instruction is profitable and eliminates dead code.
47 * (4) The fourth pass cleans up the sequence: literals get applied and dead
48 * instructions are removed from the sequence.
53 aco_ptr
<Instruction
> add_instr
;
59 mad_info(aco_ptr
<Instruction
> instr
, uint32_t id
, bool vop3
)
60 : add_instr(std::move(instr
)), mul_temp_id(id
), needs_vop3(vop3
), check_literal(false) {}
65 label_constant
= 1 << 1,
70 label_literal
= 1 << 6,
74 label_omod5
= 1 << 10,
75 label_omod_success
= 1 << 11,
76 label_clamp
= 1 << 12,
77 label_clamp_success
= 1 << 13,
78 label_undefined
= 1 << 14,
81 label_add_sub
= 1 << 17,
82 label_bitwise
= 1 << 18,
83 label_minmax
= 1 << 19,
85 label_uniform_bool
= 1 << 21,
88 static constexpr uint32_t instr_labels
= label_vec
| label_mul
| label_mad
| label_omod_success
| label_clamp_success
| label_add_sub
| label_bitwise
| label_minmax
| label_fcmp
;
89 static constexpr uint32_t temp_labels
= label_abs
| label_neg
| label_temp
| label_vcc
| label_b2f
| label_uniform_bool
;
90 static constexpr uint32_t val_labels
= label_constant
| label_literal
| label_mad
;
100 void add_label(Label new_label
)
102 /* Since all labels which use "instr" use it for the same thing
103 * (indicating the defining instruction), there is no need to clear
104 * any other instr labels. */
105 if (new_label
& instr_labels
)
106 label
&= ~temp_labels
; /* instr and temp alias */
108 if (new_label
& temp_labels
) {
109 label
&= ~temp_labels
;
110 label
&= ~instr_labels
; /* instr and temp alias */
113 if (new_label
& val_labels
)
114 label
&= ~val_labels
;
119 void set_vec(Instruction
* vec
)
121 add_label(label_vec
);
127 return label
& label_vec
;
130 void set_constant(uint32_t constant
)
132 add_label(label_constant
);
138 return label
& label_constant
;
141 void set_abs(Temp abs_temp
)
143 add_label(label_abs
);
149 return label
& label_abs
;
152 void set_neg(Temp neg_temp
)
154 add_label(label_neg
);
160 return label
& label_neg
;
163 void set_neg_abs(Temp neg_abs_temp
)
165 add_label((Label
)((uint32_t)label_abs
| (uint32_t)label_neg
));
169 void set_mul(Instruction
* mul
)
171 add_label(label_mul
);
177 return label
& label_mul
;
180 void set_temp(Temp tmp
)
182 add_label(label_temp
);
188 return label
& label_temp
;
191 void set_literal(uint32_t lit
)
193 add_label(label_literal
);
199 return label
& label_literal
;
202 void set_mad(Instruction
* mad
, uint32_t mad_info_idx
)
204 add_label(label_mad
);
211 return label
& label_mad
;
216 add_label(label_omod2
);
221 return label
& label_omod2
;
226 add_label(label_omod4
);
231 return label
& label_omod4
;
236 add_label(label_omod5
);
241 return label
& label_omod5
;
244 void set_omod_success(Instruction
* omod_instr
)
246 add_label(label_omod_success
);
250 bool is_omod_success()
252 return label
& label_omod_success
;
257 add_label(label_clamp
);
262 return label
& label_clamp
;
265 void set_clamp_success(Instruction
* clamp_instr
)
267 add_label(label_clamp_success
);
271 bool is_clamp_success()
273 return label
& label_clamp_success
;
278 add_label(label_undefined
);
283 return label
& label_undefined
;
286 void set_vcc(Temp vcc
)
288 add_label(label_vcc
);
294 return label
& label_vcc
;
297 bool is_constant_or_literal()
299 return is_constant() || is_literal();
302 void set_b2f(Temp val
)
304 add_label(label_b2f
);
310 return label
& label_b2f
;
313 void set_add_sub(Instruction
*add_sub_instr
)
315 add_label(label_add_sub
);
316 instr
= add_sub_instr
;
321 return label
& label_add_sub
;
324 void set_bitwise(Instruction
*bitwise_instr
)
326 add_label(label_bitwise
);
327 instr
= bitwise_instr
;
332 return label
& label_bitwise
;
335 void set_minmax(Instruction
*minmax_instr
)
337 add_label(label_minmax
);
338 instr
= minmax_instr
;
343 return label
& label_minmax
;
346 void set_fcmp(Instruction
*fcmp_instr
)
348 add_label(label_fcmp
);
354 return label
& label_fcmp
;
357 void set_uniform_bool(Temp uniform_bool
)
359 add_label(label_uniform_bool
);
363 bool is_uniform_bool()
365 return label
& label_uniform_bool
;
372 std::vector
<aco_ptr
<Instruction
>> instructions
;
374 std::pair
<uint32_t,Temp
> last_literal
;
375 std::vector
<mad_info
> mad_infos
;
376 std::vector
<uint16_t> uses
;
379 bool can_swap_operands(aco_ptr
<Instruction
>& instr
)
381 if (instr
->operands
[0].isConstant() ||
382 (instr
->operands
[0].isTemp() && instr
->operands
[0].getTemp().type() == RegType::sgpr
))
385 switch (instr
->opcode
) {
386 case aco_opcode::v_add_f32
:
387 case aco_opcode::v_mul_f32
:
388 case aco_opcode::v_or_b32
:
389 case aco_opcode::v_and_b32
:
390 case aco_opcode::v_xor_b32
:
391 case aco_opcode::v_max_f32
:
392 case aco_opcode::v_min_f32
:
393 case aco_opcode::v_cmp_eq_f32
:
394 case aco_opcode::v_cmp_lg_f32
:
396 case aco_opcode::v_sub_f32
:
397 instr
->opcode
= aco_opcode::v_subrev_f32
;
399 case aco_opcode::v_cmp_lt_f32
:
400 instr
->opcode
= aco_opcode::v_cmp_gt_f32
;
402 case aco_opcode::v_cmp_ge_f32
:
403 instr
->opcode
= aco_opcode::v_cmp_le_f32
;
405 case aco_opcode::v_cmp_lt_i32
:
406 instr
->opcode
= aco_opcode::v_cmp_gt_i32
;
413 bool can_use_VOP3(aco_ptr
<Instruction
>& instr
)
415 if (instr
->operands
.size() && instr
->operands
[0].isLiteral())
418 if (instr
->isDPP() || instr
->isSDWA())
421 return instr
->opcode
!= aco_opcode::v_madmk_f32
&&
422 instr
->opcode
!= aco_opcode::v_madak_f32
&&
423 instr
->opcode
!= aco_opcode::v_madmk_f16
&&
424 instr
->opcode
!= aco_opcode::v_madak_f16
&&
425 instr
->opcode
!= aco_opcode::v_readlane_b32
&&
426 instr
->opcode
!= aco_opcode::v_writelane_b32
&&
427 instr
->opcode
!= aco_opcode::v_readfirstlane_b32
;
430 bool can_apply_sgprs(aco_ptr
<Instruction
>& instr
)
432 return instr
->opcode
!= aco_opcode::v_readfirstlane_b32
&&
433 instr
->opcode
!= aco_opcode::v_readlane_b32
&&
434 instr
->opcode
!= aco_opcode::v_readlane_b32_e64
&&
435 instr
->opcode
!= aco_opcode::v_writelane_b32
&&
436 instr
->opcode
!= aco_opcode::v_writelane_b32_e64
;
439 void to_VOP3(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
444 assert(!instr
->operands
[0].isLiteral());
445 aco_ptr
<Instruction
> tmp
= std::move(instr
);
446 Format format
= asVOP3(tmp
->format
);
447 instr
.reset(create_instruction
<VOP3A_instruction
>(tmp
->opcode
, format
, tmp
->operands
.size(), tmp
->definitions
.size()));
448 std::copy(tmp
->operands
.cbegin(), tmp
->operands
.cend(), instr
->operands
.begin());
449 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
450 instr
->definitions
[i
] = tmp
->definitions
[i
];
451 if (instr
->definitions
[i
].isTemp()) {
452 ssa_info
& info
= ctx
.info
[instr
->definitions
[i
].tempId()];
453 if (info
.label
& instr_labels
&& info
.instr
== tmp
.get())
454 info
.instr
= instr
.get();
459 /* only covers special cases */
460 bool can_accept_constant(aco_ptr
<Instruction
>& instr
, unsigned operand
)
462 switch (instr
->opcode
) {
463 case aco_opcode::v_interp_p2_f32
:
464 case aco_opcode::v_mac_f32
:
465 case aco_opcode::v_writelane_b32
:
466 case aco_opcode::v_writelane_b32_e64
:
467 case aco_opcode::v_cndmask_b32
:
469 case aco_opcode::s_addk_i32
:
470 case aco_opcode::s_mulk_i32
:
471 case aco_opcode::p_wqm
:
472 case aco_opcode::p_extract_vector
:
473 case aco_opcode::p_split_vector
:
474 case aco_opcode::v_readlane_b32
:
475 case aco_opcode::v_readlane_b32_e64
:
476 case aco_opcode::v_readfirstlane_b32
:
479 if ((instr
->format
== Format::MUBUF
||
480 instr
->format
== Format::MIMG
) &&
481 instr
->definitions
.size() == 1 &&
482 instr
->operands
.size() == 4) {
489 bool valu_can_accept_literal(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
, unsigned operand
)
491 /* instructions like v_cndmask_b32 can't take a literal because they always
493 if (instr
->operands
.size() >= 3 &&
494 instr
->operands
[2].isTemp() && instr
->operands
[2].regClass().type() == RegType::sgpr
)
497 // TODO: VOP3 can take a literal on GFX10
498 return !instr
->isSDWA() && !instr
->isDPP() && !instr
->isVOP3() &&
499 operand
== 0 && can_accept_constant(instr
, operand
);
502 bool valu_can_accept_vgpr(aco_ptr
<Instruction
>& instr
, unsigned operand
)
504 if (instr
->opcode
== aco_opcode::v_readlane_b32
|| instr
->opcode
== aco_opcode::v_readlane_b32_e64
||
505 instr
->opcode
== aco_opcode::v_writelane_b32
|| instr
->opcode
== aco_opcode::v_writelane_b32_e64
)
510 bool parse_base_offset(opt_ctx
&ctx
, Instruction
* instr
, unsigned op_index
, Temp
*base
, uint32_t *offset
)
512 Operand op
= instr
->operands
[op_index
];
516 Temp tmp
= op
.getTemp();
517 if (!ctx
.info
[tmp
.id()].is_add_sub())
520 Instruction
*add_instr
= ctx
.info
[tmp
.id()].instr
;
522 switch (add_instr
->opcode
) {
523 case aco_opcode::v_add_u32
:
524 case aco_opcode::v_add_co_u32
:
525 case aco_opcode::s_add_i32
:
526 case aco_opcode::s_add_u32
:
532 if (add_instr
->usesModifiers())
535 for (unsigned i
= 0; i
< 2; i
++) {
536 if (add_instr
->operands
[i
].isConstant()) {
537 *offset
= add_instr
->operands
[i
].constantValue();
538 } else if (add_instr
->operands
[i
].isTemp() &&
539 ctx
.info
[add_instr
->operands
[i
].tempId()].is_constant_or_literal()) {
540 *offset
= ctx
.info
[add_instr
->operands
[i
].tempId()].val
;
544 if (!add_instr
->operands
[!i
].isTemp())
547 uint32_t offset2
= 0;
548 if (parse_base_offset(ctx
, add_instr
, !i
, base
, &offset2
)) {
551 *base
= add_instr
->operands
[!i
].getTemp();
559 Operand
get_constant_op(opt_ctx
&ctx
, uint32_t val
)
561 // TODO: this functions shouldn't be needed if we store Operand instead of value.
563 if (val
== 0x3e22f983 && ctx
.program
->chip_class
>= GFX8
)
564 op
.setFixed(PhysReg
{248}); /* 1/2 PI can be an inline constant on GFX8+ */
568 void label_instruction(opt_ctx
&ctx
, Block
& block
, aco_ptr
<Instruction
>& instr
)
570 if (instr
->isSALU() || instr
->isVALU() || instr
->format
== Format::PSEUDO
) {
571 ASSERTED
bool all_const
= false;
572 for (Operand
& op
: instr
->operands
)
573 all_const
= all_const
&& (!op
.isTemp() || ctx
.info
[op
.tempId()].is_constant_or_literal());
574 perfwarn(all_const
, "All instruction operands are constant", instr
.get());
577 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
579 if (!instr
->operands
[i
].isTemp())
582 ssa_info info
= ctx
.info
[instr
->operands
[i
].tempId()];
583 /* propagate undef */
584 if (info
.is_undefined() && is_phi(instr
))
585 instr
->operands
[i
] = Operand(instr
->operands
[i
].regClass());
586 /* propagate reg->reg of same type */
587 if (info
.is_temp() && info
.temp
.regClass() == instr
->operands
[i
].getTemp().regClass()) {
588 instr
->operands
[i
].setTemp(ctx
.info
[instr
->operands
[i
].tempId()].temp
);
589 info
= ctx
.info
[info
.temp
.id()];
592 /* SALU / PSEUDO: propagate inline constants */
593 if (instr
->isSALU() || instr
->format
== Format::PSEUDO
) {
594 if (info
.is_temp() && info
.temp
.type() == RegType::sgpr
) {
595 instr
->operands
[i
].setTemp(info
.temp
);
596 info
= ctx
.info
[info
.temp
.id()];
597 } else if (info
.is_temp() && info
.temp
.type() == RegType::vgpr
) {
598 /* propagate vgpr if it can take it */
599 switch (instr
->opcode
) {
600 case aco_opcode::p_create_vector
:
601 case aco_opcode::p_split_vector
:
602 case aco_opcode::p_extract_vector
:
603 case aco_opcode::p_phi
: {
604 const bool all_vgpr
= std::none_of(instr
->definitions
.begin(), instr
->definitions
.end(),
605 [] (const Definition
& def
) { return def
.getTemp().type() != RegType::vgpr
;});
607 instr
->operands
[i
] = Operand(info
.temp
);
608 info
= ctx
.info
[info
.temp
.id()];
616 if ((info
.is_constant() || (info
.is_literal() && instr
->format
== Format::PSEUDO
)) && !instr
->operands
[i
].isFixed() && can_accept_constant(instr
, i
)) {
617 instr
->operands
[i
] = get_constant_op(ctx
, info
.val
);
622 /* VALU: propagate neg, abs & inline constants */
623 else if (instr
->isVALU()) {
624 if (info
.is_temp() && info
.temp
.type() == RegType::vgpr
&& valu_can_accept_vgpr(instr
, i
)) {
625 instr
->operands
[i
].setTemp(info
.temp
);
626 info
= ctx
.info
[info
.temp
.id()];
628 if (info
.is_abs() && (can_use_VOP3(instr
) || instr
->isDPP()) && instr_info
.can_use_input_modifiers
[(int)instr
->opcode
]) {
631 instr
->operands
[i
] = Operand(info
.temp
);
633 static_cast<DPP_instruction
*>(instr
.get())->abs
[i
] = true;
635 static_cast<VOP3A_instruction
*>(instr
.get())->abs
[i
] = true;
637 if (info
.is_neg() && instr
->opcode
== aco_opcode::v_add_f32
) {
638 instr
->opcode
= i
? aco_opcode::v_sub_f32
: aco_opcode::v_subrev_f32
;
639 instr
->operands
[i
].setTemp(info
.temp
);
641 } else if (info
.is_neg() && (can_use_VOP3(instr
) || instr
->isDPP()) && instr_info
.can_use_input_modifiers
[(int)instr
->opcode
]) {
644 instr
->operands
[i
].setTemp(info
.temp
);
646 static_cast<DPP_instruction
*>(instr
.get())->neg
[i
] = true;
648 static_cast<VOP3A_instruction
*>(instr
.get())->neg
[i
] = true;
651 if (info
.is_constant() && can_accept_constant(instr
, i
)) {
652 perfwarn(instr
->opcode
== aco_opcode::v_cndmask_b32
&& i
== 2, "v_cndmask_b32 with a constant selector", instr
.get());
653 if (i
== 0 || instr
->opcode
== aco_opcode::v_readlane_b32
|| instr
->opcode
== aco_opcode::v_writelane_b32
) {
654 instr
->operands
[i
] = get_constant_op(ctx
, info
.val
);
656 } else if (!instr
->isVOP3() && can_swap_operands(instr
)) {
657 instr
->operands
[i
] = instr
->operands
[0];
658 instr
->operands
[0] = get_constant_op(ctx
, info
.val
);
660 } else if (can_use_VOP3(instr
)) {
662 instr
->operands
[i
] = get_constant_op(ctx
, info
.val
);
668 /* MUBUF: propagate constants and combine additions */
669 else if (instr
->format
== Format::MUBUF
) {
670 MUBUF_instruction
*mubuf
= static_cast<MUBUF_instruction
*>(instr
.get());
673 while (info
.is_temp())
674 info
= ctx
.info
[info
.temp
.id()];
676 if (mubuf
->offen
&& i
== 0 && info
.is_constant_or_literal() && mubuf
->offset
+ info
.val
< 4096) {
677 assert(!mubuf
->idxen
);
678 instr
->operands
[i
] = Operand(v1
);
679 mubuf
->offset
+= info
.val
;
680 mubuf
->offen
= false;
682 } else if (i
== 2 && info
.is_constant_or_literal() && mubuf
->offset
+ info
.val
< 4096) {
683 instr
->operands
[2] = Operand((uint32_t) 0);
684 mubuf
->offset
+= info
.val
;
686 } else if (mubuf
->offen
&& i
== 0 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == v1
&& mubuf
->offset
+ offset
< 4096) {
687 assert(!mubuf
->idxen
);
688 instr
->operands
[i
].setTemp(base
);
689 mubuf
->offset
+= offset
;
691 } else if (i
== 2 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == s1
&& mubuf
->offset
+ offset
< 4096) {
692 instr
->operands
[i
].setTemp(base
);
693 mubuf
->offset
+= offset
;
698 /* DS: combine additions */
699 else if (instr
->format
== Format::DS
) {
701 DS_instruction
*ds
= static_cast<DS_instruction
*>(instr
.get());
704 if (i
== 0 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == instr
->operands
[i
].regClass() && instr
->opcode
!= aco_opcode::ds_swizzle_b32
) {
705 if (instr
->opcode
== aco_opcode::ds_write2_b32
|| instr
->opcode
== aco_opcode::ds_read2_b32
||
706 instr
->opcode
== aco_opcode::ds_write2_b64
|| instr
->opcode
== aco_opcode::ds_read2_b64
) {
707 if (offset
% 4 == 0 &&
708 ds
->offset0
+ (offset
>> 2) <= 255 &&
709 ds
->offset1
+ (offset
>> 2) <= 255) {
710 instr
->operands
[i
].setTemp(base
);
711 ds
->offset0
+= offset
>> 2;
712 ds
->offset1
+= offset
>> 2;
715 if (ds
->offset0
+ offset
<= 65535) {
716 instr
->operands
[i
].setTemp(base
);
717 ds
->offset0
+= offset
;
723 /* SMEM: propagate constants and combine additions */
724 else if (instr
->format
== Format::SMEM
) {
726 SMEM_instruction
*smem
= static_cast<SMEM_instruction
*>(instr
.get());
729 if (i
== 1 && info
.is_constant_or_literal() &&
730 (ctx
.program
->chip_class
< GFX8
|| info
.val
<= 0xFFFFF)) {
731 instr
->operands
[i
] = Operand(info
.val
);
733 } else if (i
== 1 && parse_base_offset(ctx
, instr
.get(), i
, &base
, &offset
) && base
.regClass() == s1
&& offset
<= 0xFFFFF && ctx
.program
->chip_class
>= GFX9
) {
734 bool soe
= smem
->operands
.size() >= (!smem
->definitions
.empty() ? 3 : 4);
736 (!ctx
.info
[smem
->operands
.back().tempId()].is_constant_or_literal() ||
737 ctx
.info
[smem
->operands
.back().tempId()].val
!= 0)) {
741 smem
->operands
[1] = Operand(offset
);
742 smem
->operands
.back() = Operand(base
);
744 SMEM_instruction
*new_instr
= create_instruction
<SMEM_instruction
>(smem
->opcode
, Format::SMEM
, smem
->operands
.size() + 1, smem
->definitions
.size());
745 new_instr
->operands
[0] = smem
->operands
[0];
746 new_instr
->operands
[1] = Operand(offset
);
747 if (smem
->definitions
.empty())
748 new_instr
->operands
[2] = smem
->operands
[2];
749 new_instr
->operands
.back() = Operand(base
);
750 if (!smem
->definitions
.empty())
751 new_instr
->definitions
[0] = smem
->definitions
[0];
752 new_instr
->can_reorder
= smem
->can_reorder
;
753 new_instr
->barrier
= smem
->barrier
;
754 instr
.reset(new_instr
);
755 smem
= static_cast<SMEM_instruction
*>(instr
.get());
762 /* if this instruction doesn't define anything, return */
763 if (instr
->definitions
.empty())
766 switch (instr
->opcode
) {
767 case aco_opcode::p_create_vector
: {
768 unsigned num_ops
= instr
->operands
.size();
769 for (const Operand
& op
: instr
->operands
) {
770 if (op
.isTemp() && ctx
.info
[op
.tempId()].is_vec())
771 num_ops
+= ctx
.info
[op
.tempId()].instr
->operands
.size() - 1;
773 if (num_ops
!= instr
->operands
.size()) {
774 aco_ptr
<Instruction
> old_vec
= std::move(instr
);
775 instr
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_create_vector
, Format::PSEUDO
, num_ops
, 1));
776 instr
->definitions
[0] = old_vec
->definitions
[0];
778 for (Operand
& old_op
: old_vec
->operands
) {
779 if (old_op
.isTemp() && ctx
.info
[old_op
.tempId()].is_vec()) {
780 for (unsigned j
= 0; j
< ctx
.info
[old_op
.tempId()].instr
->operands
.size(); j
++) {
781 Operand op
= ctx
.info
[old_op
.tempId()].instr
->operands
[j
];
782 if (op
.isTemp() && ctx
.info
[op
.tempId()].is_temp() &&
783 ctx
.info
[op
.tempId()].temp
.type() == instr
->definitions
[0].regClass().type())
784 op
.setTemp(ctx
.info
[op
.tempId()].temp
);
785 instr
->operands
[k
++] = op
;
788 instr
->operands
[k
++] = old_op
;
791 assert(k
== num_ops
);
793 if (instr
->operands
.size() == 1 && instr
->operands
[0].isTemp())
794 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
795 else if (instr
->definitions
[0].getTemp().size() == instr
->operands
.size())
796 ctx
.info
[instr
->definitions
[0].tempId()].set_vec(instr
.get());
799 case aco_opcode::p_split_vector
: {
800 if (!ctx
.info
[instr
->operands
[0].tempId()].is_vec())
802 Instruction
* vec
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
803 assert(instr
->definitions
.size() == vec
->operands
.size());
804 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
805 Operand vec_op
= vec
->operands
[i
];
806 if (vec_op
.isConstant()) {
807 if (vec_op
.isLiteral())
808 ctx
.info
[instr
->definitions
[i
].tempId()].set_literal(vec_op
.constantValue());
809 else if (vec_op
.size() == 1)
810 ctx
.info
[instr
->definitions
[i
].tempId()].set_constant(vec_op
.constantValue());
812 assert(vec_op
.isTemp());
813 ctx
.info
[instr
->definitions
[i
].tempId()].set_temp(vec_op
.getTemp());
818 case aco_opcode::p_extract_vector
: { /* mov */
819 if (!ctx
.info
[instr
->operands
[0].tempId()].is_vec())
821 Instruction
* vec
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
822 if (vec
->definitions
[0].getTemp().size() == vec
->operands
.size() && /* TODO: what about 64bit or other combinations? */
823 vec
->operands
[0].size() == instr
->definitions
[0].size()) {
825 /* convert this extract into a mov instruction */
826 Operand vec_op
= vec
->operands
[instr
->operands
[1].constantValue()];
827 bool is_vgpr
= instr
->definitions
[0].getTemp().type() == RegType::vgpr
;
828 aco_opcode opcode
= is_vgpr
? aco_opcode::v_mov_b32
: aco_opcode::s_mov_b32
;
829 Format format
= is_vgpr
? Format::VOP1
: Format::SOP1
;
830 instr
->opcode
= opcode
;
831 instr
->format
= format
;
832 while (instr
->operands
.size() > 1)
833 instr
->operands
.pop_back();
834 instr
->operands
[0] = vec_op
;
836 if (vec_op
.isConstant()) {
837 if (vec_op
.isLiteral())
838 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(vec_op
.constantValue());
839 else if (vec_op
.size() == 1)
840 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(vec_op
.constantValue());
842 assert(vec_op
.isTemp());
843 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(vec_op
.getTemp());
848 case aco_opcode::s_mov_b32
: /* propagate */
849 case aco_opcode::s_mov_b64
:
850 case aco_opcode::v_mov_b32
:
851 case aco_opcode::p_as_uniform
:
852 if (instr
->definitions
[0].isFixed()) {
853 /* don't copy-propagate copies into fixed registers */
854 } else if (instr
->usesModifiers()) {
856 } else if (instr
->operands
[0].isConstant()) {
857 if (instr
->operands
[0].isLiteral())
858 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(instr
->operands
[0].constantValue());
859 else if (instr
->operands
[0].size() == 1)
860 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(instr
->operands
[0].constantValue());
861 } else if (instr
->operands
[0].isTemp()) {
862 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
864 assert(instr
->operands
[0].isFixed());
867 case aco_opcode::p_is_helper
:
868 if (!ctx
.program
->needs_wqm
)
869 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(0u);
871 case aco_opcode::s_movk_i32
: {
872 uint32_t v
= static_cast<SOPK_instruction
*>(instr
.get())->imm
;
873 v
= v
& 0x8000 ? (v
| 0xffff0000) : v
;
874 if (v
<= 64 || v
>= 0xfffffff0)
875 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
877 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
880 case aco_opcode::v_bfrev_b32
:
881 case aco_opcode::s_brev_b32
: {
882 if (instr
->operands
[0].isConstant()) {
883 uint32_t v
= util_bitreverse(instr
->operands
[0].constantValue());
884 if (v
<= 64 || v
>= 0xfffffff0)
885 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
887 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
891 case aco_opcode::s_bfm_b32
: {
892 if (instr
->operands
[0].isConstant() && instr
->operands
[1].isConstant()) {
893 unsigned size
= instr
->operands
[0].constantValue() & 0x1f;
894 unsigned start
= instr
->operands
[1].constantValue() & 0x1f;
895 uint32_t v
= ((1u << size
) - 1u) << start
;
896 if (v
<= 64 || v
>= 0xfffffff0)
897 ctx
.info
[instr
->definitions
[0].tempId()].set_constant(v
);
899 ctx
.info
[instr
->definitions
[0].tempId()].set_literal(v
);
902 case aco_opcode::v_mul_f32
: { /* omod */
903 /* TODO: try to move the negate/abs modifier to the consumer instead */
904 if (instr
->usesModifiers())
907 for (unsigned i
= 0; i
< 2; i
++) {
908 if (instr
->operands
[!i
].isConstant() && instr
->operands
[i
].isTemp()) {
909 if (instr
->operands
[!i
].constantValue() == 0x40000000) { /* 2.0 */
910 ctx
.info
[instr
->operands
[i
].tempId()].set_omod2();
911 } else if (instr
->operands
[!i
].constantValue() == 0x40800000) { /* 4.0 */
912 ctx
.info
[instr
->operands
[i
].tempId()].set_omod4();
913 } else if (instr
->operands
[!i
].constantValue() == 0x3f000000) { /* 0.5 */
914 ctx
.info
[instr
->operands
[i
].tempId()].set_omod5();
915 } else if (instr
->operands
[!i
].constantValue() == 0x3f800000 &&
916 !block
.fp_mode
.must_flush_denorms32
) { /* 1.0 */
917 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[i
].getTemp());
926 case aco_opcode::v_and_b32
: /* abs */
927 if (!instr
->usesModifiers() && instr
->operands
[0].constantEquals(0x7FFFFFFF) &&
928 instr
->operands
[1].isTemp() && instr
->operands
[1].getTemp().type() == RegType::vgpr
)
929 ctx
.info
[instr
->definitions
[0].tempId()].set_abs(instr
->operands
[1].getTemp());
931 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
933 case aco_opcode::v_xor_b32
: { /* neg */
934 if (!instr
->usesModifiers() && instr
->operands
[0].constantEquals(0x80000000u
) && instr
->operands
[1].isTemp()) {
935 if (ctx
.info
[instr
->operands
[1].tempId()].is_neg()) {
936 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
937 } else if (instr
->operands
[1].getTemp().type() == RegType::vgpr
) {
938 if (ctx
.info
[instr
->operands
[1].tempId()].is_abs()) { /* neg(abs(x)) */
939 instr
->operands
[1].setTemp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
940 instr
->opcode
= aco_opcode::v_or_b32
;
941 ctx
.info
[instr
->definitions
[0].tempId()].set_neg_abs(instr
->operands
[1].getTemp());
943 ctx
.info
[instr
->definitions
[0].tempId()].set_neg(instr
->operands
[1].getTemp());
947 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
951 case aco_opcode::v_med3_f32
: { /* clamp */
952 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(instr
.get());
953 if (vop3
->abs
[0] || vop3
->abs
[1] || vop3
->abs
[2] ||
954 vop3
->neg
[0] || vop3
->neg
[1] || vop3
->neg
[2] ||
955 vop3
->omod
!= 0 || vop3
->opsel
!= 0)
959 bool found_zero
= false, found_one
= false;
960 for (unsigned i
= 0; i
< 3; i
++)
962 if (instr
->operands
[i
].constantEquals(0))
964 else if (instr
->operands
[i
].constantEquals(0x3f800000)) /* 1.0 */
969 if (found_zero
&& found_one
&& instr
->operands
[idx
].isTemp()) {
970 ctx
.info
[instr
->operands
[idx
].tempId()].set_clamp();
974 case aco_opcode::v_cndmask_b32
:
975 if (instr
->operands
[0].constantEquals(0) &&
976 instr
->operands
[1].constantEquals(0xFFFFFFFF) &&
977 instr
->operands
[2].isTemp())
978 ctx
.info
[instr
->definitions
[0].tempId()].set_vcc(instr
->operands
[2].getTemp());
979 else if (instr
->operands
[0].constantEquals(0) &&
980 instr
->operands
[1].constantEquals(0x3f800000u
) &&
981 instr
->operands
[2].isTemp())
982 ctx
.info
[instr
->definitions
[0].tempId()].set_b2f(instr
->operands
[2].getTemp());
984 case aco_opcode::v_cmp_lg_u32
:
985 if (instr
->format
== Format::VOPC
&& /* don't optimize VOP3 / SDWA / DPP */
986 instr
->operands
[0].constantEquals(0) &&
987 instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_vcc())
988 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(ctx
.info
[instr
->operands
[1].tempId()].temp
);
990 case aco_opcode::p_phi
:
991 case aco_opcode::p_linear_phi
: {
992 /* lower_bool_phis() can create phis like this */
993 bool all_same_temp
= instr
->operands
[0].isTemp();
994 /* this check is needed when moving uniform loop counters out of a divergent loop */
996 all_same_temp
= instr
->definitions
[0].regClass() == instr
->operands
[0].regClass();
997 for (unsigned i
= 1; all_same_temp
&& (i
< instr
->operands
.size()); i
++) {
998 if (!instr
->operands
[i
].isTemp() || instr
->operands
[i
].tempId() != instr
->operands
[0].tempId())
999 all_same_temp
= false;
1001 if (all_same_temp
) {
1002 ctx
.info
[instr
->definitions
[0].tempId()].set_temp(instr
->operands
[0].getTemp());
1004 bool all_undef
= instr
->operands
[0].isUndefined();
1005 for (unsigned i
= 1; all_undef
&& (i
< instr
->operands
.size()); i
++) {
1006 if (!instr
->operands
[i
].isUndefined())
1010 ctx
.info
[instr
->definitions
[0].tempId()].set_undefined();
1014 case aco_opcode::v_add_u32
:
1015 case aco_opcode::v_add_co_u32
:
1016 case aco_opcode::s_add_i32
:
1017 case aco_opcode::s_add_u32
:
1018 ctx
.info
[instr
->definitions
[0].tempId()].set_add_sub(instr
.get());
1020 case aco_opcode::s_and_b32
:
1021 case aco_opcode::s_and_b64
:
1022 if (instr
->operands
[1].isFixed() && instr
->operands
[1].physReg() == exec
&&
1023 instr
->operands
[0].isTemp() && ctx
.info
[instr
->operands
[0].tempId()].is_uniform_bool()) {
1024 ctx
.info
[instr
->definitions
[1].tempId()].set_temp(ctx
.info
[instr
->operands
[0].tempId()].temp
);
1027 case aco_opcode::s_not_b32
:
1028 case aco_opcode::s_not_b64
:
1029 case aco_opcode::s_or_b32
:
1030 case aco_opcode::s_or_b64
:
1031 case aco_opcode::s_xor_b32
:
1032 case aco_opcode::s_xor_b64
:
1033 case aco_opcode::s_lshl_b32
:
1034 case aco_opcode::v_or_b32
:
1035 case aco_opcode::v_lshlrev_b32
:
1036 ctx
.info
[instr
->definitions
[0].tempId()].set_bitwise(instr
.get());
1038 case aco_opcode::v_min_f32
:
1039 case aco_opcode::v_min_f16
:
1040 case aco_opcode::v_min_u32
:
1041 case aco_opcode::v_min_i32
:
1042 case aco_opcode::v_min_u16
:
1043 case aco_opcode::v_min_i16
:
1044 case aco_opcode::v_max_f32
:
1045 case aco_opcode::v_max_f16
:
1046 case aco_opcode::v_max_u32
:
1047 case aco_opcode::v_max_i32
:
1048 case aco_opcode::v_max_u16
:
1049 case aco_opcode::v_max_i16
:
1050 ctx
.info
[instr
->definitions
[0].tempId()].set_minmax(instr
.get());
1052 case aco_opcode::v_cmp_lt_f32
:
1053 case aco_opcode::v_cmp_eq_f32
:
1054 case aco_opcode::v_cmp_le_f32
:
1055 case aco_opcode::v_cmp_gt_f32
:
1056 case aco_opcode::v_cmp_lg_f32
:
1057 case aco_opcode::v_cmp_ge_f32
:
1058 case aco_opcode::v_cmp_o_f32
:
1059 case aco_opcode::v_cmp_u_f32
:
1060 case aco_opcode::v_cmp_nge_f32
:
1061 case aco_opcode::v_cmp_nlg_f32
:
1062 case aco_opcode::v_cmp_ngt_f32
:
1063 case aco_opcode::v_cmp_nle_f32
:
1064 case aco_opcode::v_cmp_neq_f32
:
1065 case aco_opcode::v_cmp_nlt_f32
:
1066 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(instr
.get());
1068 case aco_opcode::s_cselect_b64
:
1069 case aco_opcode::s_cselect_b32
:
1070 if (instr
->operands
[0].constantEquals((unsigned) -1) &&
1071 instr
->operands
[1].constantEquals(0)) {
1072 /* Found a cselect that operates on a uniform bool that comes from eg. s_cmp */
1073 ctx
.info
[instr
->definitions
[0].tempId()].set_uniform_bool(instr
->operands
[2].getTemp());
1081 ALWAYS_INLINE
bool get_cmp_info(aco_opcode op
, aco_opcode
*ordered
, aco_opcode
*unordered
, aco_opcode
*inverse
)
1083 *ordered
= *unordered
= op
;
1085 #define CMP(ord, unord) \
1086 case aco_opcode::v_cmp_##ord##_f32:\
1087 case aco_opcode::v_cmp_n##unord##_f32:\
1088 *ordered = aco_opcode::v_cmp_##ord##_f32;\
1089 *unordered = aco_opcode::v_cmp_n##unord##_f32;\
1090 *inverse = op == aco_opcode::v_cmp_n##unord##_f32 ? aco_opcode::v_cmp_##unord##_f32 : aco_opcode::v_cmp_n##ord##_f32;\
1104 aco_opcode
get_ordered(aco_opcode op
)
1106 aco_opcode ordered
, unordered
, inverse
;
1107 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? ordered
: aco_opcode::last_opcode
;
1110 aco_opcode
get_unordered(aco_opcode op
)
1112 aco_opcode ordered
, unordered
, inverse
;
1113 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? unordered
: aco_opcode::last_opcode
;
1116 aco_opcode
get_inverse(aco_opcode op
)
1118 aco_opcode ordered
, unordered
, inverse
;
1119 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
) ? inverse
: aco_opcode::last_opcode
;
1122 bool is_cmp(aco_opcode op
)
1124 aco_opcode ordered
, unordered
, inverse
;
1125 return get_cmp_info(op
, &ordered
, &unordered
, &inverse
);
1128 unsigned original_temp_id(opt_ctx
&ctx
, Temp tmp
)
1130 if (ctx
.info
[tmp
.id()].is_temp())
1131 return ctx
.info
[tmp
.id()].temp
.id();
1136 void decrease_uses(opt_ctx
&ctx
, Instruction
* instr
)
1138 if (!--ctx
.uses
[instr
->definitions
[0].tempId()]) {
1139 for (const Operand
& op
: instr
->operands
) {
1141 ctx
.uses
[op
.tempId()]--;
1146 Instruction
*follow_operand(opt_ctx
&ctx
, Operand op
, bool ignore_uses
=false)
1148 if (!op
.isTemp() || !(ctx
.info
[op
.tempId()].label
& instr_labels
))
1150 if (!ignore_uses
&& ctx
.uses
[op
.tempId()] > 1)
1153 Instruction
*instr
= ctx
.info
[op
.tempId()].instr
;
1155 if (instr
->definitions
.size() == 2) {
1156 assert(instr
->definitions
[0].isTemp() && instr
->definitions
[0].tempId() == op
.tempId());
1157 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1164 /* s_or_b64(neq(a, a), neq(b, b)) -> v_cmp_u_f32(a, b)
1165 * s_and_b64(eq(a, a), eq(b, b)) -> v_cmp_o_f32(a, b) */
1166 bool combine_ordering_test(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1168 if (instr
->definitions
[0].regClass() != ctx
.program
->lane_mask
)
1170 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1173 bool is_or
= instr
->opcode
== aco_opcode::s_or_b64
|| instr
->opcode
== aco_opcode::s_or_b32
;
1175 bool neg
[2] = {false, false};
1176 bool abs
[2] = {false, false};
1178 Instruction
*op_instr
[2];
1181 for (unsigned i
= 0; i
< 2; i
++) {
1182 op_instr
[i
] = follow_operand(ctx
, instr
->operands
[i
], true);
1186 aco_opcode expected_cmp
= is_or
? aco_opcode::v_cmp_neq_f32
: aco_opcode::v_cmp_eq_f32
;
1188 if (op_instr
[i
]->opcode
!= expected_cmp
)
1190 if (!op_instr
[i
]->operands
[0].isTemp() || !op_instr
[i
]->operands
[1].isTemp())
1193 if (op_instr
[i
]->isVOP3()) {
1194 VOP3A_instruction
*vop3
= static_cast<VOP3A_instruction
*>(op_instr
[i
]);
1195 if (vop3
->neg
[0] != vop3
->neg
[1] || vop3
->abs
[0] != vop3
->abs
[1] || vop3
->opsel
== 1 || vop3
->opsel
== 2)
1197 neg
[i
] = vop3
->neg
[0];
1198 abs
[i
] = vop3
->abs
[0];
1199 opsel
|= (vop3
->opsel
& 1) << i
;
1202 Temp op0
= op_instr
[i
]->operands
[0].getTemp();
1203 Temp op1
= op_instr
[i
]->operands
[1].getTemp();
1204 if (original_temp_id(ctx
, op0
) != original_temp_id(ctx
, op1
))
1206 /* shouldn't happen yet, but best to be safe */
1207 if (op1
.type() != RegType::vgpr
)
1213 ctx
.uses
[op
[0].id()]++;
1214 ctx
.uses
[op
[1].id()]++;
1215 decrease_uses(ctx
, op_instr
[0]);
1216 decrease_uses(ctx
, op_instr
[1]);
1218 aco_opcode new_op
= is_or
? aco_opcode::v_cmp_u_f32
: aco_opcode::v_cmp_o_f32
;
1219 Instruction
*new_instr
;
1220 if (neg
[0] || neg
[1] || abs
[0] || abs
[1] || opsel
) {
1221 VOP3A_instruction
*vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1222 for (unsigned i
= 0; i
< 2; i
++) {
1223 vop3
->neg
[i
] = neg
[i
];
1224 vop3
->abs
[i
] = abs
[i
];
1226 vop3
->opsel
= opsel
;
1227 new_instr
= static_cast<Instruction
*>(vop3
);
1229 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1231 new_instr
->operands
[0] = Operand(op
[0]);
1232 new_instr
->operands
[1] = Operand(op
[1]);
1233 new_instr
->definitions
[0] = instr
->definitions
[0];
1235 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1236 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1238 instr
.reset(new_instr
);
1243 /* s_or_b64(v_cmp_u_f32(a, b), cmp(a, b)) -> get_unordered(cmp)(a, b)
1244 * s_and_b64(v_cmp_o_f32(a, b), cmp(a, b)) -> get_ordered(cmp)(a, b) */
1245 bool combine_comparison_ordering(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1247 if (instr
->definitions
[0].regClass() != ctx
.program
->lane_mask
)
1249 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1252 bool is_or
= instr
->opcode
== aco_opcode::s_or_b64
|| instr
->opcode
== aco_opcode::s_or_b32
;
1253 aco_opcode expected_nan_test
= is_or
? aco_opcode::v_cmp_u_f32
: aco_opcode::v_cmp_o_f32
;
1255 Instruction
*nan_test
= follow_operand(ctx
, instr
->operands
[0], true);
1256 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[1], true);
1257 if (!nan_test
|| !cmp
)
1260 if (cmp
->opcode
== expected_nan_test
)
1261 std::swap(nan_test
, cmp
);
1262 else if (nan_test
->opcode
!= expected_nan_test
)
1265 if (!is_cmp(cmp
->opcode
))
1268 if (!nan_test
->operands
[0].isTemp() || !nan_test
->operands
[1].isTemp())
1270 if (!cmp
->operands
[0].isTemp() || !cmp
->operands
[1].isTemp())
1273 unsigned prop_cmp0
= original_temp_id(ctx
, cmp
->operands
[0].getTemp());
1274 unsigned prop_cmp1
= original_temp_id(ctx
, cmp
->operands
[1].getTemp());
1275 unsigned prop_nan0
= original_temp_id(ctx
, nan_test
->operands
[0].getTemp());
1276 unsigned prop_nan1
= original_temp_id(ctx
, nan_test
->operands
[1].getTemp());
1277 if (prop_cmp0
!= prop_nan0
&& prop_cmp0
!= prop_nan1
)
1279 if (prop_cmp1
!= prop_nan0
&& prop_cmp1
!= prop_nan1
)
1282 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1283 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1284 decrease_uses(ctx
, nan_test
);
1285 decrease_uses(ctx
, cmp
);
1287 aco_opcode new_op
= is_or
? get_unordered(cmp
->opcode
) : get_ordered(cmp
->opcode
);
1288 Instruction
*new_instr
;
1289 if (cmp
->isVOP3()) {
1290 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1291 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1292 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1293 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1294 new_vop3
->clamp
= cmp_vop3
->clamp
;
1295 new_vop3
->omod
= cmp_vop3
->omod
;
1296 new_vop3
->opsel
= cmp_vop3
->opsel
;
1297 new_instr
= new_vop3
;
1299 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1301 new_instr
->operands
[0] = cmp
->operands
[0];
1302 new_instr
->operands
[1] = cmp
->operands
[1];
1303 new_instr
->definitions
[0] = instr
->definitions
[0];
1305 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1306 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1308 instr
.reset(new_instr
);
1313 /* s_or_b64(v_cmp_neq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_unordered(cmp)(a, b)
1314 * s_and_b64(v_cmp_eq_f32(a, a), cmp(a, #b)) and b is not NaN -> get_ordered(cmp)(a, b) */
1315 bool combine_constant_comparison_ordering(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1317 if (instr
->definitions
[0].regClass() != ctx
.program
->lane_mask
)
1319 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1322 bool is_or
= instr
->opcode
== aco_opcode::s_or_b64
|| instr
->opcode
== aco_opcode::s_or_b32
;
1324 Instruction
*nan_test
= follow_operand(ctx
, instr
->operands
[0], true);
1325 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[1], true);
1327 if (!nan_test
|| !cmp
)
1330 aco_opcode expected_nan_test
= is_or
? aco_opcode::v_cmp_neq_f32
: aco_opcode::v_cmp_eq_f32
;
1331 if (cmp
->opcode
== expected_nan_test
)
1332 std::swap(nan_test
, cmp
);
1333 else if (nan_test
->opcode
!= expected_nan_test
)
1336 if (!is_cmp(cmp
->opcode
))
1339 if (!nan_test
->operands
[0].isTemp() || !nan_test
->operands
[1].isTemp())
1341 if (!cmp
->operands
[0].isTemp() && !cmp
->operands
[1].isTemp())
1344 unsigned prop_nan0
= original_temp_id(ctx
, nan_test
->operands
[0].getTemp());
1345 unsigned prop_nan1
= original_temp_id(ctx
, nan_test
->operands
[1].getTemp());
1346 if (prop_nan0
!= prop_nan1
)
1349 if (nan_test
->isVOP3()) {
1350 VOP3A_instruction
*vop3
= static_cast<VOP3A_instruction
*>(nan_test
);
1351 if (vop3
->neg
[0] != vop3
->neg
[1] || vop3
->abs
[0] != vop3
->abs
[1] || vop3
->opsel
== 1 || vop3
->opsel
== 2)
1355 int constant_operand
= -1;
1356 for (unsigned i
= 0; i
< 2; i
++) {
1357 if (cmp
->operands
[i
].isTemp() && original_temp_id(ctx
, cmp
->operands
[i
].getTemp()) == prop_nan0
) {
1358 constant_operand
= !i
;
1362 if (constant_operand
== -1)
1366 if (cmp
->operands
[constant_operand
].isConstant()) {
1367 constant
= cmp
->operands
[constant_operand
].constantValue();
1368 } else if (cmp
->operands
[constant_operand
].isTemp()) {
1369 unsigned id
= cmp
->operands
[constant_operand
].tempId();
1370 if (!ctx
.info
[id
].is_constant() && !ctx
.info
[id
].is_literal())
1372 constant
= ctx
.info
[id
].val
;
1378 memcpy(&constantf
, &constant
, 4);
1379 if (isnan(constantf
))
1382 if (cmp
->operands
[0].isTemp())
1383 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1384 if (cmp
->operands
[1].isTemp())
1385 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1386 decrease_uses(ctx
, nan_test
);
1387 decrease_uses(ctx
, cmp
);
1389 aco_opcode new_op
= is_or
? get_unordered(cmp
->opcode
) : get_ordered(cmp
->opcode
);
1390 Instruction
*new_instr
;
1391 if (cmp
->isVOP3()) {
1392 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_op
, asVOP3(Format::VOPC
), 2, 1);
1393 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1394 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1395 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1396 new_vop3
->clamp
= cmp_vop3
->clamp
;
1397 new_vop3
->omod
= cmp_vop3
->omod
;
1398 new_vop3
->opsel
= cmp_vop3
->opsel
;
1399 new_instr
= new_vop3
;
1401 new_instr
= create_instruction
<VOPC_instruction
>(new_op
, Format::VOPC
, 2, 1);
1403 new_instr
->operands
[0] = cmp
->operands
[0];
1404 new_instr
->operands
[1] = cmp
->operands
[1];
1405 new_instr
->definitions
[0] = instr
->definitions
[0];
1407 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1408 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1410 instr
.reset(new_instr
);
1415 /* s_not_b64(cmp(a, b) -> get_inverse(cmp)(a, b) */
1416 bool combine_inverse_comparison(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1418 if (instr
->opcode
!= aco_opcode::s_not_b64
)
1420 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1422 if (!instr
->operands
[0].isTemp())
1425 Instruction
*cmp
= follow_operand(ctx
, instr
->operands
[0]);
1429 aco_opcode new_opcode
= get_inverse(cmp
->opcode
);
1430 if (new_opcode
== aco_opcode::last_opcode
)
1433 if (cmp
->operands
[0].isTemp())
1434 ctx
.uses
[cmp
->operands
[0].tempId()]++;
1435 if (cmp
->operands
[1].isTemp())
1436 ctx
.uses
[cmp
->operands
[1].tempId()]++;
1437 decrease_uses(ctx
, cmp
);
1439 Instruction
*new_instr
;
1440 if (cmp
->isVOP3()) {
1441 VOP3A_instruction
*new_vop3
= create_instruction
<VOP3A_instruction
>(new_opcode
, asVOP3(Format::VOPC
), 2, 1);
1442 VOP3A_instruction
*cmp_vop3
= static_cast<VOP3A_instruction
*>(cmp
);
1443 memcpy(new_vop3
->abs
, cmp_vop3
->abs
, sizeof(new_vop3
->abs
));
1444 memcpy(new_vop3
->neg
, cmp_vop3
->neg
, sizeof(new_vop3
->neg
));
1445 new_vop3
->clamp
= cmp_vop3
->clamp
;
1446 new_vop3
->omod
= cmp_vop3
->omod
;
1447 new_vop3
->opsel
= cmp_vop3
->opsel
;
1448 new_instr
= new_vop3
;
1450 new_instr
= create_instruction
<VOPC_instruction
>(new_opcode
, Format::VOPC
, 2, 1);
1452 new_instr
->operands
[0] = cmp
->operands
[0];
1453 new_instr
->operands
[1] = cmp
->operands
[1];
1454 new_instr
->definitions
[0] = instr
->definitions
[0];
1456 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1457 ctx
.info
[instr
->definitions
[0].tempId()].set_fcmp(new_instr
);
1459 instr
.reset(new_instr
);
1464 /* op1(op2(1, 2), 0) if swap = false
1465 * op1(0, op2(1, 2)) if swap = true */
1466 bool match_op3_for_vop3(opt_ctx
&ctx
, aco_opcode op1
, aco_opcode op2
,
1467 Instruction
* op1_instr
, bool swap
, const char *shuffle_str
,
1468 Operand operands
[3], bool neg
[3], bool abs
[3], uint8_t *opsel
,
1469 bool *op1_clamp
, uint8_t *op1_omod
,
1470 bool *inbetween_neg
, bool *inbetween_abs
, bool *inbetween_opsel
)
1473 if (op1_instr
->opcode
!= op1
)
1476 Instruction
*op2_instr
= follow_operand(ctx
, op1_instr
->operands
[swap
]);
1477 if (!op2_instr
|| op2_instr
->opcode
!= op2
)
1480 VOP3A_instruction
*op1_vop3
= op1_instr
->isVOP3() ? static_cast<VOP3A_instruction
*>(op1_instr
) : NULL
;
1481 VOP3A_instruction
*op2_vop3
= op2_instr
->isVOP3() ? static_cast<VOP3A_instruction
*>(op2_instr
) : NULL
;
1483 /* don't support inbetween clamp/omod */
1484 if (op2_vop3
&& (op2_vop3
->clamp
|| op2_vop3
->omod
))
1487 /* get operands and modifiers and check inbetween modifiers */
1488 *op1_clamp
= op1_vop3
? op1_vop3
->clamp
: false;
1489 *op1_omod
= op1_vop3
? op1_vop3
->omod
: 0u;
1492 *inbetween_neg
= op1_vop3
? op1_vop3
->neg
[swap
] : false;
1493 else if (op1_vop3
&& op1_vop3
->neg
[swap
])
1497 *inbetween_abs
= op1_vop3
? op1_vop3
->abs
[swap
] : false;
1498 else if (op1_vop3
&& op1_vop3
->abs
[swap
])
1501 if (inbetween_opsel
)
1502 *inbetween_opsel
= op1_vop3
? op1_vop3
->opsel
& (1 << swap
) : false;
1503 else if (op1_vop3
&& op1_vop3
->opsel
& (1 << swap
))
1507 shuffle
[shuffle_str
[0] - '0'] = 0;
1508 shuffle
[shuffle_str
[1] - '0'] = 1;
1509 shuffle
[shuffle_str
[2] - '0'] = 2;
1511 operands
[shuffle
[0]] = op1_instr
->operands
[!swap
];
1512 neg
[shuffle
[0]] = op1_vop3
? op1_vop3
->neg
[!swap
] : false;
1513 abs
[shuffle
[0]] = op1_vop3
? op1_vop3
->abs
[!swap
] : false;
1514 if (op1_vop3
&& op1_vop3
->opsel
& (1 << !swap
))
1515 *opsel
|= 1 << shuffle
[0];
1517 for (unsigned i
= 0; i
< 2; i
++) {
1518 operands
[shuffle
[i
+ 1]] = op2_instr
->operands
[i
];
1519 neg
[shuffle
[i
+ 1]] = op2_vop3
? op2_vop3
->neg
[i
] : false;
1520 abs
[shuffle
[i
+ 1]] = op2_vop3
? op2_vop3
->abs
[i
] : false;
1521 if (op2_vop3
&& op2_vop3
->opsel
& (1 << i
))
1522 *opsel
|= 1 << shuffle
[i
+ 1];
1525 /* check operands */
1526 unsigned sgpr_id
= 0;
1527 for (unsigned i
= 0; i
< 3; i
++) {
1528 Operand op
= operands
[i
];
1529 if (op
.isLiteral()) {
1531 } else if (op
.isTemp() && op
.getTemp().type() == RegType::sgpr
) {
1532 if (sgpr_id
&& sgpr_id
!= op
.tempId())
1534 sgpr_id
= op
.tempId();
1541 void create_vop3_for_op3(opt_ctx
& ctx
, aco_opcode opcode
, aco_ptr
<Instruction
>& instr
,
1542 Operand operands
[3], bool neg
[3], bool abs
[3], uint8_t opsel
,
1543 bool clamp
, unsigned omod
)
1545 VOP3A_instruction
*new_instr
= create_instruction
<VOP3A_instruction
>(opcode
, Format::VOP3A
, 3, 1);
1546 memcpy(new_instr
->abs
, abs
, sizeof(bool[3]));
1547 memcpy(new_instr
->neg
, neg
, sizeof(bool[3]));
1548 new_instr
->clamp
= clamp
;
1549 new_instr
->omod
= omod
;
1550 new_instr
->opsel
= opsel
;
1551 new_instr
->operands
[0] = operands
[0];
1552 new_instr
->operands
[1] = operands
[1];
1553 new_instr
->operands
[2] = operands
[2];
1554 new_instr
->definitions
[0] = instr
->definitions
[0];
1555 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1557 instr
.reset(new_instr
);
1560 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
)
1562 uint32_t omod_clamp
= ctx
.info
[instr
->definitions
[0].tempId()].label
&
1563 (label_omod_success
| label_clamp_success
);
1565 for (unsigned swap
= 0; swap
< 2; swap
++) {
1566 if (!((1 << swap
) & ops
))
1569 Operand operands
[3];
1570 bool neg
[3], abs
[3], clamp
;
1571 uint8_t opsel
= 0, omod
= 0;
1572 if (match_op3_for_vop3(ctx
, instr
->opcode
, op2
,
1573 instr
.get(), swap
, shuffle
,
1574 operands
, neg
, abs
, &opsel
,
1575 &clamp
, &omod
, NULL
, NULL
, NULL
)) {
1576 ctx
.uses
[instr
->operands
[swap
].tempId()]--;
1577 create_vop3_for_op3(ctx
, new_op
, instr
, operands
, neg
, abs
, opsel
, clamp
, omod
);
1578 if (omod_clamp
& label_omod_success
)
1579 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1580 if (omod_clamp
& label_clamp_success
)
1581 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
1588 /* s_not_b32(s_and_b32(a, b)) -> s_nand_b32(a, b)
1589 * s_not_b32(s_or_b32(a, b)) -> s_nor_b32(a, b)
1590 * s_not_b32(s_xor_b32(a, b)) -> s_xnor_b32(a, b)
1591 * s_not_b64(s_and_b64(a, b)) -> s_nand_b64(a, b)
1592 * s_not_b64(s_or_b64(a, b)) -> s_nor_b64(a, b)
1593 * s_not_b64(s_xor_b64(a, b)) -> s_xnor_b64(a, b) */
1594 bool combine_salu_not_bitwise(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1597 if (!instr
->operands
[0].isTemp())
1599 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1602 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[0]);
1605 switch (op2_instr
->opcode
) {
1606 case aco_opcode::s_and_b32
:
1607 case aco_opcode::s_or_b32
:
1608 case aco_opcode::s_xor_b32
:
1609 case aco_opcode::s_and_b64
:
1610 case aco_opcode::s_or_b64
:
1611 case aco_opcode::s_xor_b64
:
1617 /* create instruction */
1618 std::swap(instr
->definitions
[0], op2_instr
->definitions
[0]);
1619 ctx
.uses
[instr
->operands
[0].tempId()]--;
1620 ctx
.info
[op2_instr
->definitions
[0].tempId()].label
= 0;
1622 switch (op2_instr
->opcode
) {
1623 case aco_opcode::s_and_b32
:
1624 op2_instr
->opcode
= aco_opcode::s_nand_b32
;
1626 case aco_opcode::s_or_b32
:
1627 op2_instr
->opcode
= aco_opcode::s_nor_b32
;
1629 case aco_opcode::s_xor_b32
:
1630 op2_instr
->opcode
= aco_opcode::s_xnor_b32
;
1632 case aco_opcode::s_and_b64
:
1633 op2_instr
->opcode
= aco_opcode::s_nand_b64
;
1635 case aco_opcode::s_or_b64
:
1636 op2_instr
->opcode
= aco_opcode::s_nor_b64
;
1638 case aco_opcode::s_xor_b64
:
1639 op2_instr
->opcode
= aco_opcode::s_xnor_b64
;
1648 /* s_and_b32(a, s_not_b32(b)) -> s_andn2_b32(a, b)
1649 * s_or_b32(a, s_not_b32(b)) -> s_orn2_b32(a, b)
1650 * s_and_b64(a, s_not_b64(b)) -> s_andn2_b64(a, b)
1651 * s_or_b64(a, s_not_b64(b)) -> s_orn2_b64(a, b) */
1652 bool combine_salu_n2(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1654 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1657 for (unsigned i
= 0; i
< 2; i
++) {
1658 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[i
]);
1659 if (!op2_instr
|| (op2_instr
->opcode
!= aco_opcode::s_not_b32
&& op2_instr
->opcode
!= aco_opcode::s_not_b64
))
1662 ctx
.uses
[instr
->operands
[i
].tempId()]--;
1663 instr
->operands
[0] = instr
->operands
[!i
];
1664 instr
->operands
[1] = op2_instr
->operands
[0];
1665 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1667 switch (instr
->opcode
) {
1668 case aco_opcode::s_and_b32
:
1669 instr
->opcode
= aco_opcode::s_andn2_b32
;
1671 case aco_opcode::s_or_b32
:
1672 instr
->opcode
= aco_opcode::s_orn2_b32
;
1674 case aco_opcode::s_and_b64
:
1675 instr
->opcode
= aco_opcode::s_andn2_b64
;
1677 case aco_opcode::s_or_b64
:
1678 instr
->opcode
= aco_opcode::s_orn2_b64
;
1689 /* s_add_{i32,u32}(a, s_lshl_b32(b, <n>)) -> s_lshl<n>_add_u32(a, b) */
1690 bool combine_salu_lshl_add(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
)
1692 if (instr
->definitions
[1].isTemp() && ctx
.uses
[instr
->definitions
[1].tempId()])
1695 for (unsigned i
= 0; i
< 2; i
++) {
1696 Instruction
*op2_instr
= follow_operand(ctx
, instr
->operands
[i
]);
1697 if (!op2_instr
|| op2_instr
->opcode
!= aco_opcode::s_lshl_b32
|| !op2_instr
->operands
[1].isConstant())
1700 uint32_t shift
= op2_instr
->operands
[1].constantValue();
1701 if (shift
< 1 || shift
> 4)
1704 ctx
.uses
[instr
->operands
[i
].tempId()]--;
1705 instr
->operands
[1] = instr
->operands
[!i
];
1706 instr
->operands
[0] = op2_instr
->operands
[0];
1707 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
1709 instr
->opcode
= ((aco_opcode
[]){aco_opcode::s_lshl1_add_u32
,
1710 aco_opcode::s_lshl2_add_u32
,
1711 aco_opcode::s_lshl3_add_u32
,
1712 aco_opcode::s_lshl4_add_u32
})[shift
- 1];
1719 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
)
1722 #define MINMAX(type, gfx9) \
1723 case aco_opcode::v_min_##type:\
1724 case aco_opcode::v_max_##type:\
1725 case aco_opcode::v_med3_##type:\
1726 *min = aco_opcode::v_min_##type;\
1727 *max = aco_opcode::v_max_##type;\
1728 *med3 = aco_opcode::v_med3_##type;\
1729 *min3 = aco_opcode::v_min3_##type;\
1730 *max3 = aco_opcode::v_max3_##type;\
1731 *some_gfx9_only = gfx9;\
1745 /* 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
1746 * 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 */
1747 bool combine_clamp(opt_ctx
& ctx
, aco_ptr
<Instruction
>& instr
,
1748 aco_opcode min
, aco_opcode max
, aco_opcode med
)
1750 aco_opcode other_op
;
1751 if (instr
->opcode
== min
)
1753 else if (instr
->opcode
== max
)
1758 uint32_t omod_clamp
= ctx
.info
[instr
->definitions
[0].tempId()].label
&
1759 (label_omod_success
| label_clamp_success
);
1761 for (unsigned swap
= 0; swap
< 2; swap
++) {
1762 Operand operands
[3];
1763 bool neg
[3], abs
[3], clamp
, inbetween_neg
, inbetween_abs
;
1764 uint8_t opsel
= 0, omod
= 0;
1765 if (match_op3_for_vop3(ctx
, instr
->opcode
, other_op
, instr
.get(), swap
,
1766 "012", operands
, neg
, abs
, &opsel
,
1767 &clamp
, &omod
, &inbetween_neg
, &inbetween_abs
, NULL
)) {
1768 int const0_idx
= -1, const1_idx
= -1;
1769 uint32_t const0
= 0, const1
= 0;
1770 for (int i
= 0; i
< 3; i
++) {
1772 if (operands
[i
].isConstant()) {
1773 val
= operands
[i
].constantValue();
1774 } else if (operands
[i
].isTemp() && ctx
.uses
[operands
[i
].tempId()] == 1 &&
1775 ctx
.info
[operands
[i
].tempId()].is_constant_or_literal()) {
1776 val
= ctx
.info
[operands
[i
].tempId()].val
;
1780 if (const0_idx
>= 0) {
1788 if (const0_idx
< 0 || const1_idx
< 0)
1791 if (opsel
& (1 << const0_idx
))
1793 if (opsel
& (1 << const1_idx
))
1796 int lower_idx
= const0_idx
;
1798 case aco_opcode::v_min_f32
:
1799 case aco_opcode::v_min_f16
: {
1800 float const0_f
, const1_f
;
1801 if (min
== aco_opcode::v_min_f32
) {
1802 memcpy(&const0_f
, &const0
, 4);
1803 memcpy(&const1_f
, &const1
, 4);
1805 const0_f
= _mesa_half_to_float(const0
);
1806 const1_f
= _mesa_half_to_float(const1
);
1808 if (abs
[const0_idx
]) const0_f
= fabsf(const0_f
);
1809 if (abs
[const1_idx
]) const1_f
= fabsf(const1_f
);
1810 if (neg
[const0_idx
]) const0_f
= -const0_f
;
1811 if (neg
[const1_idx
]) const1_f
= -const1_f
;
1812 lower_idx
= const0_f
< const1_f
? const0_idx
: const1_idx
;
1815 case aco_opcode::v_min_u32
: {
1816 lower_idx
= const0
< const1
? const0_idx
: const1_idx
;
1819 case aco_opcode::v_min_u16
: {
1820 lower_idx
= (uint16_t)const0
< (uint16_t)const1
? const0_idx
: const1_idx
;
1823 case aco_opcode::v_min_i32
: {
1824 int32_t const0_i
= const0
& 0x80000000u
? -2147483648 + (int32_t)(const0
& 0x7fffffffu
) : const0
;
1825 int32_t const1_i
= const1
& 0x80000000u
? -2147483648 + (int32_t)(const1
& 0x7fffffffu
) : const1
;
1826 lower_idx
= const0_i
< const1_i
? const0_idx
: const1_idx
;
1829 case aco_opcode::v_min_i16
: {
1830 int16_t const0_i
= const0
& 0x8000u
? -32768 + (int16_t)(const0
& 0x7fffu
) : const0
;
1831 int16_t const1_i
= const1
& 0x8000u
? -32768 + (int16_t)(const1
& 0x7fffu
) : const1
;
1832 lower_idx
= const0_i
< const1_i
? const0_idx
: const1_idx
;
1838 int upper_idx
= lower_idx
== const0_idx
? const1_idx
: const0_idx
;
1840 if (instr
->opcode
== min
) {
1841 if (upper_idx
!= 0 || lower_idx
== 0)
1844 if (upper_idx
== 0 || lower_idx
!= 0)
1848 neg
[1] ^= inbetween_neg
;
1849 neg
[2] ^= inbetween_neg
;
1850 abs
[1] |= inbetween_abs
;
1851 abs
[2] |= inbetween_abs
;
1853 ctx
.uses
[instr
->operands
[swap
].tempId()]--;
1854 create_vop3_for_op3(ctx
, med
, instr
, operands
, neg
, abs
, opsel
, clamp
, omod
);
1855 if (omod_clamp
& label_omod_success
)
1856 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
1857 if (omod_clamp
& label_clamp_success
)
1858 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
1868 void apply_sgprs(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
1871 uint32_t sgpr_idx
= 0;
1872 uint32_t sgpr_info_id
= 0;
1873 bool has_sgpr
= false;
1874 uint32_t sgpr_ssa_id
= 0;
1875 /* find 'best' possible sgpr */
1876 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
1878 if (instr
->operands
[i
].isLiteral()) {
1882 if (!instr
->operands
[i
].isTemp())
1884 if (instr
->operands
[i
].getTemp().type() == RegType::sgpr
) {
1886 sgpr_ssa_id
= instr
->operands
[i
].tempId();
1889 ssa_info
& info
= ctx
.info
[instr
->operands
[i
].tempId()];
1890 if (info
.is_temp() && info
.temp
.type() == RegType::sgpr
) {
1891 uint16_t uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
1892 if (sgpr_info_id
== 0 || uses
< ctx
.uses
[sgpr_info_id
]) {
1894 sgpr_info_id
= instr
->operands
[i
].tempId();
1898 if (!has_sgpr
&& sgpr_info_id
!= 0) {
1899 ssa_info
& info
= ctx
.info
[sgpr_info_id
];
1900 if (sgpr_idx
== 0 || instr
->isVOP3()) {
1901 instr
->operands
[sgpr_idx
] = Operand(info
.temp
);
1902 ctx
.uses
[sgpr_info_id
]--;
1903 ctx
.uses
[info
.temp
.id()]++;
1904 } else if (can_swap_operands(instr
)) {
1905 instr
->operands
[sgpr_idx
] = instr
->operands
[0];
1906 instr
->operands
[0] = Operand(info
.temp
);
1907 ctx
.uses
[sgpr_info_id
]--;
1908 ctx
.uses
[info
.temp
.id()]++;
1909 } else if (can_use_VOP3(instr
)) {
1910 to_VOP3(ctx
, instr
);
1911 instr
->operands
[sgpr_idx
] = Operand(info
.temp
);
1912 ctx
.uses
[sgpr_info_id
]--;
1913 ctx
.uses
[info
.temp
.id()]++;
1916 /* we can have two sgprs on one instruction if it is the same sgpr! */
1917 } else if (sgpr_info_id
!= 0 &&
1918 sgpr_ssa_id
== sgpr_info_id
&&
1919 ctx
.uses
[sgpr_info_id
] == 1 &&
1920 can_use_VOP3(instr
)) {
1921 to_VOP3(ctx
, instr
);
1922 instr
->operands
[sgpr_idx
] = Operand(ctx
.info
[sgpr_info_id
].temp
);
1923 ctx
.uses
[sgpr_info_id
]--;
1924 ctx
.uses
[ctx
.info
[sgpr_info_id
].temp
.id()]++;
1928 bool apply_omod_clamp(opt_ctx
&ctx
, Block
& block
, aco_ptr
<Instruction
>& instr
)
1930 /* check if we could apply omod on predecessor */
1931 if (instr
->opcode
== aco_opcode::v_mul_f32
) {
1932 bool op0
= instr
->operands
[0].isTemp() && ctx
.info
[instr
->operands
[0].tempId()].is_omod_success();
1933 bool op1
= instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_omod_success();
1935 unsigned idx
= op0
? 0 : 1;
1936 /* omod was successfully applied */
1937 /* if the omod instruction is v_mad, we also have to change the original add */
1938 if (ctx
.info
[instr
->operands
[idx
].tempId()].is_mad()) {
1939 Instruction
* add_instr
= ctx
.mad_infos
[ctx
.info
[instr
->operands
[idx
].tempId()].val
].add_instr
.get();
1940 if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp())
1941 static_cast<VOP3A_instruction
*>(add_instr
)->clamp
= true;
1942 add_instr
->definitions
[0] = instr
->definitions
[0];
1945 Instruction
* omod_instr
= ctx
.info
[instr
->operands
[idx
].tempId()].instr
;
1946 /* check if we have an additional clamp modifier */
1947 if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp() && ctx
.uses
[instr
->definitions
[0].tempId()] == 1) {
1948 static_cast<VOP3A_instruction
*>(omod_instr
)->clamp
= true;
1949 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(omod_instr
);
1951 /* change definition ssa-id of modified instruction */
1952 omod_instr
->definitions
[0] = instr
->definitions
[0];
1954 /* change the definition of instr to something unused, e.g. the original omod def */
1955 instr
->definitions
[0] = Definition(instr
->operands
[idx
].getTemp());
1956 ctx
.uses
[instr
->definitions
[0].tempId()] = 0;
1959 if (!ctx
.info
[instr
->definitions
[0].tempId()].label
) {
1960 /* in all other cases, label this instruction as option for multiply-add */
1961 ctx
.info
[instr
->definitions
[0].tempId()].set_mul(instr
.get());
1965 /* check if we could apply clamp on predecessor */
1966 if (instr
->opcode
== aco_opcode::v_med3_f32
) {
1968 bool found_zero
= false, found_one
= false;
1969 for (unsigned i
= 0; i
< 3; i
++)
1971 if (instr
->operands
[i
].constantEquals(0))
1973 else if (instr
->operands
[i
].constantEquals(0x3f800000)) /* 1.0 */
1978 if (found_zero
&& found_one
&& instr
->operands
[idx
].isTemp() &&
1979 ctx
.info
[instr
->operands
[idx
].tempId()].is_clamp_success()) {
1980 /* clamp was successfully applied */
1981 /* if the clamp instruction is v_mad, we also have to change the original add */
1982 if (ctx
.info
[instr
->operands
[idx
].tempId()].is_mad()) {
1983 Instruction
* add_instr
= ctx
.mad_infos
[ctx
.info
[instr
->operands
[idx
].tempId()].val
].add_instr
.get();
1984 add_instr
->definitions
[0] = instr
->definitions
[0];
1986 Instruction
* clamp_instr
= ctx
.info
[instr
->operands
[idx
].tempId()].instr
;
1987 /* change definition ssa-id of modified instruction */
1988 clamp_instr
->definitions
[0] = instr
->definitions
[0];
1990 /* change the definition of instr to something unused, e.g. the original omod def */
1991 instr
->definitions
[0] = Definition(instr
->operands
[idx
].getTemp());
1992 ctx
.uses
[instr
->definitions
[0].tempId()] = 0;
1997 /* omod has no effect if denormals are enabled */
1998 bool can_use_omod
= block
.fp_mode
.denorm32
== 0;
2000 /* apply omod / clamp modifiers if the def is used only once and the instruction can have modifiers */
2001 if (!instr
->definitions
.empty() && ctx
.uses
[instr
->definitions
[0].tempId()] == 1 &&
2002 can_use_VOP3(instr
) && instr_info
.can_use_output_modifiers
[(int)instr
->opcode
]) {
2003 if (can_use_omod
&& ctx
.info
[instr
->definitions
[0].tempId()].is_omod2()) {
2004 to_VOP3(ctx
, instr
);
2005 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 1;
2006 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
2007 } else if (can_use_omod
&& ctx
.info
[instr
->definitions
[0].tempId()].is_omod4()) {
2008 to_VOP3(ctx
, instr
);
2009 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 2;
2010 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
2011 } else if (can_use_omod
&& ctx
.info
[instr
->definitions
[0].tempId()].is_omod5()) {
2012 to_VOP3(ctx
, instr
);
2013 static_cast<VOP3A_instruction
*>(instr
.get())->omod
= 3;
2014 ctx
.info
[instr
->definitions
[0].tempId()].set_omod_success(instr
.get());
2015 } else if (ctx
.info
[instr
->definitions
[0].tempId()].is_clamp()) {
2016 to_VOP3(ctx
, instr
);
2017 static_cast<VOP3A_instruction
*>(instr
.get())->clamp
= true;
2018 ctx
.info
[instr
->definitions
[0].tempId()].set_clamp_success(instr
.get());
2025 // TODO: we could possibly move the whole label_instruction pass to combine_instruction:
2026 // this would mean that we'd have to fix the instruction uses while value propagation
2028 void combine_instruction(opt_ctx
&ctx
, Block
& block
, aco_ptr
<Instruction
>& instr
)
2030 if (instr
->definitions
.empty() || !ctx
.uses
[instr
->definitions
[0].tempId()])
2033 if (instr
->isVALU()) {
2034 if (can_apply_sgprs(instr
))
2035 apply_sgprs(ctx
, instr
);
2036 if (apply_omod_clamp(ctx
, block
, instr
))
2040 /* TODO: There are still some peephole optimizations that could be done:
2041 * - abs(a - b) -> s_absdiff_i32
2042 * - various patterns for s_bitcmp{0,1}_b32 and s_bitset{0,1}_b32
2043 * - patterns for v_alignbit_b32 and v_alignbyte_b32
2044 * These aren't probably too interesting though.
2045 * There are also patterns for v_cmp_class_f{16,32,64}. This is difficult but
2046 * probably more useful than the previously mentioned optimizations.
2047 * The various comparison optimizations also currently only work with 32-bit
2050 /* neg(mul(a, b)) -> mul(neg(a), b) */
2051 if (ctx
.info
[instr
->definitions
[0].tempId()].is_neg() && ctx
.uses
[instr
->operands
[1].tempId()] == 1) {
2052 Temp val
= ctx
.info
[instr
->definitions
[0].tempId()].temp
;
2054 if (!ctx
.info
[val
.id()].is_mul())
2057 Instruction
* mul_instr
= ctx
.info
[val
.id()].instr
;
2059 if (mul_instr
->operands
[0].isLiteral())
2061 if (mul_instr
->isVOP3() && static_cast<VOP3A_instruction
*>(mul_instr
)->clamp
)
2064 /* convert to mul(neg(a), b) */
2065 ctx
.uses
[mul_instr
->definitions
[0].tempId()]--;
2066 Definition def
= instr
->definitions
[0];
2067 /* neg(abs(mul(a, b))) -> mul(neg(abs(a)), abs(b)) */
2068 bool is_abs
= ctx
.info
[instr
->definitions
[0].tempId()].is_abs();
2069 instr
.reset(create_instruction
<VOP3A_instruction
>(aco_opcode::v_mul_f32
, asVOP3(Format::VOP2
), 2, 1));
2070 instr
->operands
[0] = mul_instr
->operands
[0];
2071 instr
->operands
[1] = mul_instr
->operands
[1];
2072 instr
->definitions
[0] = def
;
2073 VOP3A_instruction
* new_mul
= static_cast<VOP3A_instruction
*>(instr
.get());
2074 if (mul_instr
->isVOP3()) {
2075 VOP3A_instruction
* mul
= static_cast<VOP3A_instruction
*>(mul_instr
);
2076 new_mul
->neg
[0] = mul
->neg
[0] && !is_abs
;
2077 new_mul
->neg
[1] = mul
->neg
[1] && !is_abs
;
2078 new_mul
->abs
[0] = mul
->abs
[0] || is_abs
;
2079 new_mul
->abs
[1] = mul
->abs
[1] || is_abs
;
2080 new_mul
->omod
= mul
->omod
;
2082 new_mul
->neg
[0] ^= true;
2083 new_mul
->clamp
= false;
2085 ctx
.info
[instr
->definitions
[0].tempId()].set_mul(instr
.get());
2088 /* combine mul+add -> mad */
2089 else if ((instr
->opcode
== aco_opcode::v_add_f32
||
2090 instr
->opcode
== aco_opcode::v_sub_f32
||
2091 instr
->opcode
== aco_opcode::v_subrev_f32
) &&
2092 block
.fp_mode
.denorm32
== 0 && !block
.fp_mode
.preserve_signed_zero_inf_nan32
) {
2093 //TODO: we could use fma instead when denormals are enabled if the NIR isn't marked as precise
2095 uint32_t uses_src0
= UINT32_MAX
;
2096 uint32_t uses_src1
= UINT32_MAX
;
2097 Instruction
* mul_instr
= nullptr;
2098 unsigned add_op_idx
;
2099 /* check if any of the operands is a multiplication */
2100 if (instr
->operands
[0].isTemp() && ctx
.info
[instr
->operands
[0].tempId()].is_mul())
2101 uses_src0
= ctx
.uses
[instr
->operands
[0].tempId()];
2102 if (instr
->operands
[1].isTemp() && ctx
.info
[instr
->operands
[1].tempId()].is_mul())
2103 uses_src1
= ctx
.uses
[instr
->operands
[1].tempId()];
2105 /* find the 'best' mul instruction to combine with the add */
2106 if (uses_src0
< uses_src1
) {
2107 mul_instr
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
2109 } else if (uses_src1
< uses_src0
) {
2110 mul_instr
= ctx
.info
[instr
->operands
[1].tempId()].instr
;
2112 } else if (uses_src0
!= UINT32_MAX
) {
2113 /* tiebreaker: quite random what to pick */
2114 if (ctx
.info
[instr
->operands
[0].tempId()].instr
->operands
[0].isLiteral()) {
2115 mul_instr
= ctx
.info
[instr
->operands
[1].tempId()].instr
;
2118 mul_instr
= ctx
.info
[instr
->operands
[0].tempId()].instr
;
2123 Operand op
[3] = {Operand(v1
), Operand(v1
), Operand(v1
)};
2124 bool neg
[3] = {false, false, false};
2125 bool abs
[3] = {false, false, false};
2128 bool need_vop3
= false;
2130 op
[0] = mul_instr
->operands
[0];
2131 op
[1] = mul_instr
->operands
[1];
2132 op
[2] = instr
->operands
[add_op_idx
];
2133 for (unsigned i
= 0; i
< 3; i
++)
2135 if (op
[i
].isLiteral())
2137 if (op
[i
].isTemp() && op
[i
].getTemp().type() == RegType::sgpr
)
2139 if (!(i
== 0 || (op
[i
].isTemp() && op
[i
].getTemp().type() == RegType::vgpr
)))
2142 // TODO: would be better to check this before selecting a mul instr?
2146 if (mul_instr
->isVOP3()) {
2147 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*> (mul_instr
);
2148 neg
[0] = vop3
->neg
[0];
2149 neg
[1] = vop3
->neg
[1];
2150 abs
[0] = vop3
->abs
[0];
2151 abs
[1] = vop3
->abs
[1];
2153 /* we cannot use these modifiers between mul and add */
2154 if (vop3
->clamp
|| vop3
->omod
)
2158 /* convert to mad */
2159 ctx
.uses
[mul_instr
->definitions
[0].tempId()]--;
2160 if (ctx
.uses
[mul_instr
->definitions
[0].tempId()]) {
2162 ctx
.uses
[op
[0].tempId()]++;
2164 ctx
.uses
[op
[1].tempId()]++;
2167 if (instr
->isVOP3()) {
2168 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*> (instr
.get());
2169 neg
[2] = vop3
->neg
[add_op_idx
];
2170 abs
[2] = vop3
->abs
[add_op_idx
];
2172 clamp
= vop3
->clamp
;
2173 /* abs of the multiplication result */
2174 if (vop3
->abs
[1 - add_op_idx
]) {
2180 /* neg of the multiplication result */
2181 neg
[1] = neg
[1] ^ vop3
->neg
[1 - add_op_idx
];
2184 if (instr
->opcode
== aco_opcode::v_sub_f32
) {
2185 neg
[1 + add_op_idx
] = neg
[1 + add_op_idx
] ^ true;
2187 } else if (instr
->opcode
== aco_opcode::v_subrev_f32
) {
2188 neg
[2 - add_op_idx
] = neg
[2 - add_op_idx
] ^ true;
2192 aco_ptr
<VOP3A_instruction
> mad
{create_instruction
<VOP3A_instruction
>(aco_opcode::v_mad_f32
, Format::VOP3A
, 3, 1)};
2193 for (unsigned i
= 0; i
< 3; i
++)
2195 mad
->operands
[i
] = op
[i
];
2196 mad
->neg
[i
] = neg
[i
];
2197 mad
->abs
[i
] = abs
[i
];
2201 mad
->definitions
[0] = instr
->definitions
[0];
2203 /* mark this ssa_def to be re-checked for profitability and literals */
2204 ctx
.mad_infos
.emplace_back(std::move(instr
), mul_instr
->definitions
[0].tempId(), need_vop3
);
2205 ctx
.info
[mad
->definitions
[0].tempId()].set_mad(mad
.get(), ctx
.mad_infos
.size() - 1);
2206 instr
.reset(mad
.release());
2210 /* v_mul_f32(v_cndmask_b32(0, 1.0, cond), a) -> v_cndmask_b32(0, a, cond) */
2211 else if (instr
->opcode
== aco_opcode::v_mul_f32
&& !instr
->isVOP3()) {
2212 for (unsigned i
= 0; i
< 2; i
++) {
2213 if (instr
->operands
[i
].isTemp() && ctx
.info
[instr
->operands
[i
].tempId()].is_b2f() &&
2214 ctx
.uses
[instr
->operands
[i
].tempId()] == 1 &&
2215 instr
->operands
[!i
].isTemp() && instr
->operands
[!i
].getTemp().type() == RegType::vgpr
) {
2216 ctx
.uses
[instr
->operands
[i
].tempId()]--;
2217 ctx
.uses
[ctx
.info
[instr
->operands
[i
].tempId()].temp
.id()]++;
2219 aco_ptr
<VOP2_instruction
> new_instr
{create_instruction
<VOP2_instruction
>(aco_opcode::v_cndmask_b32
, Format::VOP2
, 3, 1)};
2220 new_instr
->operands
[0] = Operand(0u);
2221 new_instr
->operands
[1] = instr
->operands
[!i
];
2222 new_instr
->operands
[2] = Operand(ctx
.info
[instr
->operands
[i
].tempId()].temp
);
2223 new_instr
->definitions
[0] = instr
->definitions
[0];
2224 instr
.reset(new_instr
.release());
2225 ctx
.info
[instr
->definitions
[0].tempId()].label
= 0;
2229 } else if (instr
->opcode
== aco_opcode::v_or_b32
&& ctx
.program
->chip_class
>= GFX9
) {
2230 if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_or_b32
, aco_opcode::v_or3_b32
, "012", 1 | 2)) ;
2231 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_and_b32
, aco_opcode::v_and_or_b32
, "120", 1 | 2)) ;
2232 else combine_three_valu_op(ctx
, instr
, aco_opcode::v_lshlrev_b32
, aco_opcode::v_lshl_or_b32
, "210", 1 | 2);
2233 } else if (instr
->opcode
== aco_opcode::v_add_u32
&& ctx
.program
->chip_class
>= GFX9
) {
2234 if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_xor_b32
, aco_opcode::v_xad_u32
, "120", 1 | 2)) ;
2235 else if (combine_three_valu_op(ctx
, instr
, aco_opcode::v_add_u32
, aco_opcode::v_add3_u32
, "012", 1 | 2)) ;
2236 else combine_three_valu_op(ctx
, instr
, aco_opcode::v_lshlrev_b32
, aco_opcode::v_lshl_add_u32
, "210", 1 | 2);
2237 } else if (instr
->opcode
== aco_opcode::v_lshlrev_b32
&& ctx
.program
->chip_class
>= GFX9
) {
2238 combine_three_valu_op(ctx
, instr
, aco_opcode::v_add_u32
, aco_opcode::v_add_lshl_u32
, "120", 2);
2239 } else if ((instr
->opcode
== aco_opcode::s_add_u32
|| instr
->opcode
== aco_opcode::s_add_i32
) && ctx
.program
->chip_class
>= GFX9
) {
2240 combine_salu_lshl_add(ctx
, instr
);
2241 } else if (instr
->opcode
== aco_opcode::s_not_b32
) {
2242 combine_salu_not_bitwise(ctx
, instr
);
2243 } else if (instr
->opcode
== aco_opcode::s_not_b64
) {
2244 if (combine_inverse_comparison(ctx
, instr
)) ;
2245 else combine_salu_not_bitwise(ctx
, instr
);
2246 } else if (instr
->opcode
== aco_opcode::s_and_b32
|| instr
->opcode
== aco_opcode::s_or_b32
||
2247 instr
->opcode
== aco_opcode::s_and_b64
|| instr
->opcode
== aco_opcode::s_or_b64
) {
2248 if (combine_ordering_test(ctx
, instr
)) ;
2249 else if (combine_comparison_ordering(ctx
, instr
)) ;
2250 else if (combine_constant_comparison_ordering(ctx
, instr
)) ;
2251 else combine_salu_n2(ctx
, instr
);
2253 aco_opcode min
, max
, min3
, max3
, med3
;
2254 bool some_gfx9_only
;
2255 if (get_minmax_info(instr
->opcode
, &min
, &max
, &min3
, &max3
, &med3
, &some_gfx9_only
) &&
2256 (!some_gfx9_only
|| ctx
.program
->chip_class
>= GFX9
)) {
2257 if (combine_three_valu_op(ctx
, instr
, instr
->opcode
, instr
->opcode
== min
? min3
: max3
, "012", 1 | 2));
2258 else combine_clamp(ctx
, instr
, min
, max
, med3
);
2264 void select_instruction(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
2266 const uint32_t threshold
= 4;
2268 if (is_dead(ctx
.uses
, instr
.get())) {
2273 /* convert split_vector into extract_vector if only one definition is ever used */
2274 if (instr
->opcode
== aco_opcode::p_split_vector
) {
2275 unsigned num_used
= 0;
2277 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
2278 if (ctx
.uses
[instr
->definitions
[i
].tempId()]) {
2283 if (num_used
== 1) {
2284 aco_ptr
<Pseudo_instruction
> extract
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_extract_vector
, Format::PSEUDO
, 2, 1)};
2285 extract
->operands
[0] = instr
->operands
[0];
2286 extract
->operands
[1] = Operand((uint32_t) idx
);
2287 extract
->definitions
[0] = instr
->definitions
[idx
];
2288 instr
.reset(extract
.release());
2292 /* re-check mad instructions */
2293 if (instr
->opcode
== aco_opcode::v_mad_f32
&& ctx
.info
[instr
->definitions
[0].tempId()].is_mad()) {
2294 mad_info
* info
= &ctx
.mad_infos
[ctx
.info
[instr
->definitions
[0].tempId()].val
];
2295 /* first, check profitability */
2296 if (ctx
.uses
[info
->mul_temp_id
]) {
2297 ctx
.uses
[info
->mul_temp_id
]++;
2298 instr
.swap(info
->add_instr
);
2300 /* second, check possible literals */
2301 } else if (!info
->needs_vop3
) {
2302 uint32_t literal_idx
= 0;
2303 uint32_t literal_uses
= UINT32_MAX
;
2304 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
2306 if (!instr
->operands
[i
].isTemp())
2308 /* if one of the operands is sgpr, we cannot add a literal somewhere else */
2309 if (instr
->operands
[i
].getTemp().type() == RegType::sgpr
) {
2310 if (ctx
.info
[instr
->operands
[i
].tempId()].is_literal()) {
2311 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2314 literal_uses
= UINT32_MAX
;
2318 else if (ctx
.info
[instr
->operands
[i
].tempId()].is_literal() &&
2319 ctx
.uses
[instr
->operands
[i
].tempId()] < literal_uses
) {
2320 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2324 if (literal_uses
< threshold
) {
2325 ctx
.uses
[instr
->operands
[literal_idx
].tempId()]--;
2326 info
->check_literal
= true;
2327 info
->literal_idx
= literal_idx
;
2333 /* check for literals */
2334 /* we do not apply the literals yet as we don't know if it is profitable */
2335 if (instr
->isSALU()) {
2336 uint32_t literal_idx
= 0;
2337 uint32_t literal_uses
= UINT32_MAX
;
2338 bool has_literal
= false;
2339 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++)
2341 if (instr
->operands
[i
].isLiteral()) {
2345 if (!instr
->operands
[i
].isTemp())
2347 if (ctx
.info
[instr
->operands
[i
].tempId()].is_literal() &&
2348 ctx
.uses
[instr
->operands
[i
].tempId()] < literal_uses
) {
2349 literal_uses
= ctx
.uses
[instr
->operands
[i
].tempId()];
2353 if (!has_literal
&& literal_uses
< threshold
) {
2354 ctx
.uses
[instr
->operands
[literal_idx
].tempId()]--;
2355 if (ctx
.uses
[instr
->operands
[literal_idx
].tempId()] == 0)
2356 instr
->operands
[literal_idx
] = Operand(ctx
.info
[instr
->operands
[literal_idx
].tempId()].val
);
2358 } else if (instr
->isVALU() && valu_can_accept_literal(ctx
, instr
, 0) &&
2359 instr
->operands
[0].isTemp() &&
2360 ctx
.info
[instr
->operands
[0].tempId()].is_literal() &&
2361 ctx
.uses
[instr
->operands
[0].tempId()] < threshold
) {
2362 ctx
.uses
[instr
->operands
[0].tempId()]--;
2363 if (ctx
.uses
[instr
->operands
[0].tempId()] == 0)
2364 instr
->operands
[0] = Operand(ctx
.info
[instr
->operands
[0].tempId()].val
);
2370 void apply_literals(opt_ctx
&ctx
, aco_ptr
<Instruction
>& instr
)
2372 /* Cleanup Dead Instructions */
2376 /* apply literals on SALU */
2377 if (instr
->isSALU()) {
2378 for (Operand
& op
: instr
->operands
) {
2383 if (ctx
.info
[op
.tempId()].is_literal() &&
2384 ctx
.uses
[op
.tempId()] == 0)
2385 op
= Operand(ctx
.info
[op
.tempId()].val
);
2389 /* apply literals on VALU */
2390 else if (instr
->isVALU() && !instr
->isVOP3() &&
2391 instr
->operands
[0].isTemp() &&
2392 ctx
.info
[instr
->operands
[0].tempId()].is_literal() &&
2393 ctx
.uses
[instr
->operands
[0].tempId()] == 0) {
2394 instr
->operands
[0] = Operand(ctx
.info
[instr
->operands
[0].tempId()].val
);
2397 /* apply literals on MAD */
2398 else if (instr
->opcode
== aco_opcode::v_mad_f32
&& ctx
.info
[instr
->definitions
[0].tempId()].is_mad()) {
2399 mad_info
* info
= &ctx
.mad_infos
[ctx
.info
[instr
->definitions
[0].tempId()].val
];
2400 aco_ptr
<Instruction
> new_mad
;
2401 if (info
->check_literal
&& ctx
.uses
[instr
->operands
[info
->literal_idx
].tempId()] == 0) {
2402 if (info
->literal_idx
== 2) { /* add literal -> madak */
2403 new_mad
.reset(create_instruction
<VOP2_instruction
>(aco_opcode::v_madak_f32
, Format::VOP2
, 3, 1));
2404 new_mad
->operands
[0] = instr
->operands
[0];
2405 new_mad
->operands
[1] = instr
->operands
[1];
2406 } else { /* mul literal -> madmk */
2407 new_mad
.reset(create_instruction
<VOP2_instruction
>(aco_opcode::v_madmk_f32
, Format::VOP2
, 3, 1));
2408 new_mad
->operands
[0] = instr
->operands
[1 - info
->literal_idx
];
2409 new_mad
->operands
[1] = instr
->operands
[2];
2411 new_mad
->operands
[2] = Operand(ctx
.info
[instr
->operands
[info
->literal_idx
].tempId()].val
);
2412 new_mad
->definitions
[0] = instr
->definitions
[0];
2413 instr
.swap(new_mad
);
2417 ctx
.instructions
.emplace_back(std::move(instr
));
2421 void optimize(Program
* program
)
2424 ctx
.program
= program
;
2425 std::vector
<ssa_info
> info(program
->peekAllocationId());
2426 ctx
.info
= info
.data();
2428 /* 1. Bottom-Up DAG pass (forward) to label all ssa-defs */
2429 for (Block
& block
: program
->blocks
) {
2430 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2431 label_instruction(ctx
, block
, instr
);
2434 ctx
.uses
= std::move(dead_code_analysis(program
));
2436 /* 2. Combine v_mad, omod, clamp and propagate sgpr on VALU instructions */
2437 for (Block
& block
: program
->blocks
) {
2438 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2439 combine_instruction(ctx
, block
, instr
);
2442 /* 3. Top-Down DAG pass (backward) to select instructions (includes DCE) */
2443 for (std::vector
<Block
>::reverse_iterator it
= program
->blocks
.rbegin(); it
!= program
->blocks
.rend(); ++it
) {
2444 Block
* block
= &(*it
);
2445 for (std::vector
<aco_ptr
<Instruction
>>::reverse_iterator it
= block
->instructions
.rbegin(); it
!= block
->instructions
.rend(); ++it
)
2446 select_instruction(ctx
, *it
);
2449 /* 4. Add literals to instructions */
2450 for (Block
& block
: program
->blocks
) {
2451 ctx
.instructions
.clear();
2452 for (aco_ptr
<Instruction
>& instr
: block
.instructions
)
2453 apply_literals(ctx
, instr
);
2454 block
.instructions
.swap(ctx
.instructions
);