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
26 #include <unordered_map>
30 * Implements the algorithm for dominator-tree value numbering
31 * from "Value Numbering" by Briggs, Cooper, and Simpson.
38 std::size_t operator()(Instruction
* instr
) const
40 uint64_t hash
= (uint64_t) instr
->opcode
+ (uint64_t) instr
->format
;
41 for (unsigned i
= 0; i
< instr
->operands
.size(); i
++) {
42 Operand op
= instr
->operands
[i
];
43 uint64_t val
= op
.isTemp() ? op
.tempId() : op
.isFixed() ? op
.physReg() : op
.constantValue();
44 hash
|= val
<< (i
+1) * 8;
46 if (instr
->isVOP3()) {
47 VOP3A_instruction
* vop3
= static_cast<VOP3A_instruction
*>(instr
);
48 for (unsigned i
= 0; i
< 3; i
++) {
49 hash
^= vop3
->abs
[i
] << (i
*3 + 0);
50 hash
^= vop3
->opsel
[i
] << (i
*3 + 1);
51 hash
^= vop3
->neg
[i
] << (i
*3 + 2);
53 hash
^= (vop3
->clamp
<< 28) * 13;
54 hash
+= vop3
->omod
<< 19;
56 switch (instr
->format
) {
59 case Format::VINTRP
: {
60 Interp_instruction
* interp
= static_cast<Interp_instruction
*>(instr
);
61 hash
^= interp
->attribute
<< 13;
62 hash
^= interp
->component
<< 27;
76 bool operator()(Instruction
* a
, Instruction
* b
) const
78 if (a
->format
!= b
->format
)
80 if (a
->opcode
!= b
->opcode
)
82 if (a
->operands
.size() != b
->operands
.size() || a
->definitions
.size() != b
->definitions
.size())
83 return false; /* possible with pseudo-instructions */
84 for (unsigned i
= 0; i
< a
->operands
.size(); i
++) {
85 if (a
->operands
[i
].isConstant()) {
86 if (!b
->operands
[i
].isConstant())
88 if (a
->operands
[i
].constantValue() != b
->operands
[i
].constantValue())
91 else if (a
->operands
[i
].isTemp()) {
92 if (!b
->operands
[i
].isTemp())
94 if (a
->operands
[i
].tempId() != b
->operands
[i
].tempId())
97 else if (a
->operands
[i
].isUndefined() ^ b
->operands
[i
].isUndefined())
99 if (a
->operands
[i
].isFixed()) {
100 if (!b
->operands
[i
].isFixed())
102 if (a
->operands
[i
].physReg() != b
->operands
[i
].physReg())
104 if (a
->operands
[i
].physReg() == exec
&& a
->pass_flags
!= b
->pass_flags
)
108 for (unsigned i
= 0; i
< a
->definitions
.size(); i
++) {
109 if (a
->definitions
[i
].isTemp()) {
110 if (!b
->definitions
[i
].isTemp())
112 if (a
->definitions
[i
].regClass() != b
->definitions
[i
].regClass())
115 if (a
->definitions
[i
].isFixed()) {
116 if (!b
->definitions
[i
].isFixed())
118 if (a
->definitions
[i
].physReg() != b
->definitions
[i
].physReg())
120 if (a
->definitions
[i
].physReg() == exec
)
125 if (a
->opcode
== aco_opcode::v_readfirstlane_b32
)
126 return a
->pass_flags
== b
->pass_flags
;
128 /* The results of VOPC depend on the exec mask if used for subgroup operations. */
129 if ((uint32_t) a
->format
& (uint32_t) Format::VOPC
&& a
->pass_flags
!= b
->pass_flags
)
131 if (a
->format
== Format::PSEUDO_BRANCH
)
134 VOP3A_instruction
* a3
= static_cast<VOP3A_instruction
*>(a
);
135 VOP3A_instruction
* b3
= static_cast<VOP3A_instruction
*>(b
);
136 for (unsigned i
= 0; i
< 3; i
++) {
137 if (a3
->abs
[i
] != b3
->abs
[i
] ||
138 a3
->opsel
[i
] != b3
->opsel
[i
] ||
139 a3
->neg
[i
] != b3
->neg
[i
])
142 return a3
->clamp
== b3
->clamp
&&
143 a3
->omod
== b3
->omod
;
146 DPP_instruction
* aDPP
= static_cast<DPP_instruction
*>(a
);
147 DPP_instruction
* bDPP
= static_cast<DPP_instruction
*>(b
);
148 return aDPP
->dpp_ctrl
== bDPP
->dpp_ctrl
&&
149 aDPP
->bank_mask
== bDPP
->bank_mask
&&
150 aDPP
->row_mask
== bDPP
->row_mask
&&
151 aDPP
->bound_ctrl
== bDPP
->bound_ctrl
&&
152 aDPP
->abs
[0] == bDPP
->abs
[0] &&
153 aDPP
->abs
[1] == bDPP
->abs
[1] &&
154 aDPP
->neg
[0] == bDPP
->neg
[0] &&
155 aDPP
->neg
[1] == bDPP
->neg
[1];
159 SOPK_instruction
* aK
= static_cast<SOPK_instruction
*>(a
);
160 SOPK_instruction
* bK
= static_cast<SOPK_instruction
*>(b
);
161 return aK
->imm
== bK
->imm
;
164 SMEM_instruction
* aS
= static_cast<SMEM_instruction
*>(a
);
165 SMEM_instruction
* bS
= static_cast<SMEM_instruction
*>(b
);
166 return aS
->can_reorder
&& bS
->can_reorder
&&
167 aS
->glc
== bS
->glc
&& aS
->nv
== bS
->nv
;
169 case Format::VINTRP
: {
170 Interp_instruction
* aI
= static_cast<Interp_instruction
*>(a
);
171 Interp_instruction
* bI
= static_cast<Interp_instruction
*>(b
);
172 if (aI
->attribute
!= bI
->attribute
)
174 if (aI
->component
!= bI
->component
)
178 case Format::PSEUDO_REDUCTION
: {
179 Pseudo_reduction_instruction
*aR
= static_cast<Pseudo_reduction_instruction
*>(a
);
180 Pseudo_reduction_instruction
*bR
= static_cast<Pseudo_reduction_instruction
*>(b
);
181 return aR
->pass_flags
== bR
->pass_flags
&&
182 aR
->reduce_op
== bR
->reduce_op
&&
183 aR
->cluster_size
== bR
->cluster_size
;
185 case Format::MTBUF
: {
186 /* this is fine since they are only used for vertex input fetches */
187 MTBUF_instruction
* aM
= static_cast<MTBUF_instruction
*>(a
);
188 MTBUF_instruction
* bM
= static_cast<MTBUF_instruction
*>(b
);
189 return aM
->can_reorder
== bM
->can_reorder
&&
190 aM
->barrier
== bM
->barrier
&&
191 aM
->dfmt
== bM
->dfmt
&&
192 aM
->nfmt
== bM
->nfmt
&&
193 aM
->offset
== bM
->offset
&&
194 aM
->offen
== bM
->offen
&&
195 aM
->idxen
== bM
->idxen
&&
196 aM
->glc
== bM
->glc
&&
197 aM
->slc
== bM
->slc
&&
198 aM
->tfe
== bM
->tfe
&&
199 aM
->disable_wqm
== bM
->disable_wqm
;
201 /* we want to optimize these in NIR and don't hassle with load-store dependencies */
205 case Format::SCRATCH
:
208 if (a
->opcode
!= aco_opcode::ds_bpermute_b32
&&
209 a
->opcode
!= aco_opcode::ds_permute_b32
&&
210 a
->opcode
!= aco_opcode::ds_swizzle_b32
)
212 DS_instruction
* aD
= static_cast<DS_instruction
*>(a
);
213 DS_instruction
* bD
= static_cast<DS_instruction
*>(b
);
214 return aD
->pass_flags
== bD
->pass_flags
&&
215 aD
->gds
== bD
->gds
&&
216 aD
->offset0
== bD
->offset0
&&
217 aD
->offset1
== bD
->offset1
;
220 MIMG_instruction
* aM
= static_cast<MIMG_instruction
*>(a
);
221 MIMG_instruction
* bM
= static_cast<MIMG_instruction
*>(b
);
222 return aM
->can_reorder
&& bM
->can_reorder
&&
223 aM
->barrier
== bM
->barrier
&&
224 aM
->dmask
== bM
->dmask
&&
225 aM
->unrm
== bM
->unrm
&&
226 aM
->glc
== bM
->glc
&&
227 aM
->slc
== bM
->slc
&&
228 aM
->tfe
== bM
->tfe
&&
230 aM
->lwe
== bM
->lwe
&&
231 aM
->r128
== bM
->r128
&&
232 aM
->a16
== bM
->a16
&&
233 aM
->d16
== bM
->d16
&&
234 aM
->disable_wqm
== bM
->disable_wqm
;
242 using expr_set
= std::unordered_map
<Instruction
*, uint32_t, InstrHash
, InstrPred
>;
246 expr_set expr_values
;
247 std::map
<uint32_t, Temp
> renames
;
249 /* The exec id should be the same on the same level of control flow depth.
250 * Together with the check for dominator relations, it is safe to assume
251 * that the same exec_id also means the same execution mask.
252 * Discards increment the exec_id, so that it won't return to the previous value.
254 uint32_t exec_id
= 1;
256 vn_ctx(Program
* program
) : program(program
) {}
259 bool dominates(vn_ctx
& ctx
, uint32_t parent
, uint32_t child
)
261 while (parent
< child
)
262 child
= ctx
.program
->blocks
[child
].logical_idom
;
264 return parent
== child
;
267 void process_block(vn_ctx
& ctx
, Block
& block
)
269 std::vector
<aco_ptr
<Instruction
>> new_instructions
;
270 new_instructions
.reserve(block
.instructions
.size());
272 for (aco_ptr
<Instruction
>& instr
: block
.instructions
) {
273 /* first, rename operands */
274 for (Operand
& op
: instr
->operands
) {
277 auto it
= ctx
.renames
.find(op
.tempId());
278 if (it
!= ctx
.renames
.end())
279 op
.setTemp(it
->second
);
282 if (instr
->definitions
.empty() || instr
->opcode
== aco_opcode::p_phi
|| instr
->opcode
== aco_opcode::p_linear_phi
) {
283 new_instructions
.emplace_back(std::move(instr
));
287 /* simple copy-propagation through renaming */
288 if ((instr
->opcode
== aco_opcode::s_mov_b32
|| instr
->opcode
== aco_opcode::s_mov_b64
|| instr
->opcode
== aco_opcode::v_mov_b32
) &&
289 !instr
->definitions
[0].isFixed() && instr
->operands
[0].isTemp() && instr
->operands
[0].regClass() == instr
->definitions
[0].regClass() &&
290 !instr
->isDPP() && !((int)instr
->format
& (int)Format::SDWA
)) {
291 ctx
.renames
[instr
->definitions
[0].tempId()] = instr
->operands
[0].getTemp();
294 if (instr
->opcode
== aco_opcode::p_discard_if
||
295 instr
->opcode
== aco_opcode::p_demote_to_helper
)
298 instr
->pass_flags
= ctx
.exec_id
;
299 std::pair
<expr_set::iterator
, bool> res
= ctx
.expr_values
.emplace(instr
.get(), block
.index
);
301 /* if there was already an expression with the same value number */
303 Instruction
* orig_instr
= res
.first
->first
;
304 assert(instr
->definitions
.size() == orig_instr
->definitions
.size());
305 /* check if the original instruction dominates the current one */
306 if (dominates(ctx
, res
.first
->second
, block
.index
)) {
307 for (unsigned i
= 0; i
< instr
->definitions
.size(); i
++) {
308 assert(instr
->definitions
[i
].regClass() == orig_instr
->definitions
[i
].regClass());
309 ctx
.renames
[instr
->definitions
[i
].tempId()] = orig_instr
->definitions
[i
].getTemp();
312 ctx
.expr_values
.erase(res
.first
);
313 ctx
.expr_values
.emplace(instr
.get(), block
.index
);
314 new_instructions
.emplace_back(std::move(instr
));
317 new_instructions
.emplace_back(std::move(instr
));
321 block
.instructions
= std::move(new_instructions
);
324 void rename_phi_operands(Block
& block
, std::map
<uint32_t, Temp
>& renames
)
326 for (aco_ptr
<Instruction
>& phi
: block
.instructions
) {
327 if (phi
->opcode
!= aco_opcode::p_phi
&& phi
->opcode
!= aco_opcode::p_linear_phi
)
330 for (Operand
& op
: phi
->operands
) {
333 auto it
= renames
.find(op
.tempId());
334 if (it
!= renames
.end())
335 op
.setTemp(it
->second
);
339 } /* end namespace */
342 void value_numbering(Program
* program
)
345 std::vector
<unsigned> loop_headers
;
347 for (Block
& block
: program
->blocks
) {
348 assert(ctx
.exec_id
> 0);
349 /* decrement exec_id when leaving nested control flow */
350 if (block
.kind
& block_kind_loop_header
)
351 loop_headers
.push_back(block
.index
);
352 if (block
.kind
& block_kind_merge
) {
354 } else if (block
.kind
& block_kind_loop_exit
) {
355 ctx
.exec_id
-= program
->blocks
[loop_headers
.back()].logical_preds
.size();
356 ctx
.exec_id
-= block
.logical_preds
.size();
357 loop_headers
.pop_back();
360 if (block
.logical_idom
!= -1)
361 process_block(ctx
, block
);
363 rename_phi_operands(block
, ctx
.renames
);
365 /* increment exec_id when entering nested control flow */
366 if (block
.kind
& block_kind_branch
||
367 block
.kind
& block_kind_loop_preheader
||
368 block
.kind
& block_kind_break
||
369 block
.kind
& block_kind_continue
)
371 else if (block
.kind
& block_kind_continue_or_break
)
375 /* rename loop header phi operands */
376 for (Block
& block
: program
->blocks
) {
377 if (block
.kind
& block_kind_loop_header
)
378 rename_phi_operands(block
, ctx
.renames
);