/src/spirv-tools/source/opt/if_conversion.cpp
Line | Count | Source |
1 | | // Copyright (c) 2018 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "source/opt/if_conversion.h" |
16 | | |
17 | | #include <memory> |
18 | | #include <vector> |
19 | | |
20 | | #include "source/opt/value_number_table.h" |
21 | | |
22 | | namespace spvtools { |
23 | | namespace opt { |
24 | | |
25 | 7.95k | Pass::Status IfConversion::Process() { |
26 | 7.95k | if (!context()->get_feature_mgr()->HasCapability(spv::Capability::Shader)) { |
27 | 31 | return Status::SuccessWithoutChange; |
28 | 31 | } |
29 | | |
30 | 7.92k | const ValueNumberTable& vn_table = *context()->GetValueNumberTable(); |
31 | 7.92k | bool modified = false; |
32 | 7.92k | std::vector<Instruction*> to_kill; |
33 | 7.92k | for (auto& func : *get_module()) { |
34 | 7.84k | DominatorAnalysis* dominators = context()->GetDominatorAnalysis(&func); |
35 | 712k | for (auto& block : func) { |
36 | | // Check if it is possible for |block| to have phis that can be |
37 | | // transformed. |
38 | 712k | BasicBlock* common = nullptr; |
39 | 712k | if (!CheckBlock(&block, dominators, &common)) continue; |
40 | | |
41 | | // Get an insertion point. |
42 | 21.5k | auto iter = block.begin(); |
43 | 40.5k | while (iter != block.end() && iter->opcode() == spv::Op::OpPhi) { |
44 | 19.0k | ++iter; |
45 | 19.0k | } |
46 | | |
47 | 21.5k | InstructionBuilder builder( |
48 | 21.5k | context(), &*iter, |
49 | 21.5k | IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping); |
50 | 21.5k | block.ForEachPhiInst([this, &builder, &modified, &common, &to_kill, |
51 | 21.5k | dominators, &block, &vn_table](Instruction* phi) { |
52 | | // This phi is not compatible, but subsequent phis might be. |
53 | 19.0k | if (!CheckType(phi->type_id())) return; |
54 | | |
55 | | // We cannot transform cases where the phi is used by another phi in the |
56 | | // same block due to instruction ordering restrictions. |
57 | | // TODO(alan-baker): If all inappropriate uses could also be |
58 | | // transformed, we could still remove this phi. |
59 | 19.0k | if (!CheckPhiUsers(phi, &block)) return; |
60 | | |
61 | | // Identify the incoming values associated with the true and false |
62 | | // branches. If |then_block| dominates |inc0| or if the true edge |
63 | | // branches straight to this block and |common| is |inc0|, then |inc0| |
64 | | // is on the true branch. Otherwise the |inc1| is on the true branch. |
65 | 19.0k | BasicBlock* inc0 = GetIncomingBlock(phi, 0u); |
66 | 19.0k | Instruction* branch = common->terminator(); |
67 | 19.0k | uint32_t condition = branch->GetSingleWordInOperand(0u); |
68 | 19.0k | BasicBlock* then_block = GetBlock(branch->GetSingleWordInOperand(1u)); |
69 | 19.0k | Instruction* true_value = nullptr; |
70 | 19.0k | Instruction* false_value = nullptr; |
71 | 19.0k | if ((then_block == &block && inc0 == common) || |
72 | 18.8k | dominators->Dominates(then_block, inc0)) { |
73 | 4.71k | true_value = GetIncomingValue(phi, 0u); |
74 | 4.71k | false_value = GetIncomingValue(phi, 1u); |
75 | 14.3k | } else { |
76 | 14.3k | true_value = GetIncomingValue(phi, 1u); |
77 | 14.3k | false_value = GetIncomingValue(phi, 0u); |
78 | 14.3k | } |
79 | | |
80 | 19.0k | BasicBlock* true_def_block = context()->get_instr_block(true_value); |
81 | 19.0k | BasicBlock* false_def_block = context()->get_instr_block(false_value); |
82 | | |
83 | 19.0k | uint32_t true_vn = vn_table.GetValueNumber(true_value); |
84 | 19.0k | uint32_t false_vn = vn_table.GetValueNumber(false_value); |
85 | 19.0k | if (true_vn != 0 && true_vn == false_vn) { |
86 | 295 | Instruction* inst_to_use = nullptr; |
87 | | |
88 | | // Try to pick an instruction that is not in a side node. If we can't |
89 | | // pick either the true for false branch as long as they can be |
90 | | // legally moved. |
91 | 295 | if (!true_def_block || |
92 | 159 | dominators->Dominates(true_def_block, &block)) { |
93 | 159 | inst_to_use = true_value; |
94 | 159 | } else if (!false_def_block || |
95 | 136 | dominators->Dominates(false_def_block, &block)) { |
96 | 96 | inst_to_use = false_value; |
97 | 96 | } else if (CanHoistInstruction(true_value, common, dominators)) { |
98 | 32 | inst_to_use = true_value; |
99 | 32 | } else if (CanHoistInstruction(false_value, common, dominators)) { |
100 | 0 | inst_to_use = false_value; |
101 | 0 | } |
102 | | |
103 | 295 | if (inst_to_use != nullptr) { |
104 | 287 | modified = true; |
105 | 287 | HoistInstruction(inst_to_use, common, dominators); |
106 | 287 | context()->KillNamesAndDecorates(phi); |
107 | 287 | context()->ReplaceAllUsesWith(phi->result_id(), |
108 | 287 | inst_to_use->result_id()); |
109 | 287 | } |
110 | 295 | return; |
111 | 295 | } |
112 | | |
113 | | // If either incoming value is defined in a block that does not dominate |
114 | | // this phi, then we cannot eliminate the phi with a select. |
115 | | // TODO(alan-baker): Perform code motion where it makes sense to enable |
116 | | // the transform in this case. |
117 | 18.7k | if (true_def_block && !dominators->Dominates(true_def_block, &block)) |
118 | 10.2k | return; |
119 | | |
120 | 8.51k | if (false_def_block && !dominators->Dominates(false_def_block, &block)) |
121 | 888 | return; |
122 | | |
123 | 7.63k | analysis::Type* data_ty = |
124 | 7.63k | context()->get_type_mgr()->GetType(true_value->type_id()); |
125 | 7.63k | if (analysis::Vector* vec_data_ty = data_ty->AsVector()) { |
126 | 1.00k | condition = SplatCondition(vec_data_ty, condition, &builder); |
127 | 1.00k | } |
128 | | |
129 | | // TODO(1841): Handle id overflow. |
130 | 7.63k | Instruction* select = builder.AddSelect(phi->type_id(), condition, |
131 | 7.63k | true_value->result_id(), |
132 | 7.63k | false_value->result_id()); |
133 | 7.63k | context()->get_def_use_mgr()->AnalyzeInstDefUse(select); |
134 | 7.63k | select->UpdateDebugInfoFrom(phi); |
135 | 7.63k | context()->ReplaceAllUsesWith(phi->result_id(), select->result_id()); |
136 | 7.63k | to_kill.push_back(phi); |
137 | 7.63k | modified = true; |
138 | | |
139 | 7.63k | return; |
140 | 8.51k | }); |
141 | 21.5k | } |
142 | 7.84k | } |
143 | | |
144 | 7.92k | for (auto inst : to_kill) { |
145 | 7.63k | context()->KillInst(inst); |
146 | 7.63k | } |
147 | | |
148 | 7.92k | return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange; |
149 | 7.95k | } |
150 | | |
151 | | bool IfConversion::CheckBlock(BasicBlock* block, DominatorAnalysis* dominators, |
152 | 712k | BasicBlock** common) { |
153 | 712k | const std::vector<uint32_t>& preds = cfg()->preds(block->id()); |
154 | | |
155 | | // TODO(alan-baker): Extend to more than two predecessors |
156 | 712k | if (preds.size() != 2) return false; |
157 | | |
158 | 59.0k | BasicBlock* inc0 = context()->get_instr_block(preds[0]); |
159 | 59.0k | if (dominators->Dominates(block, inc0)) return false; |
160 | | |
161 | 59.0k | BasicBlock* inc1 = context()->get_instr_block(preds[1]); |
162 | 59.0k | if (dominators->Dominates(block, inc1)) return false; |
163 | | |
164 | 47.9k | if (inc0 == inc1) { |
165 | | // If the predecessor blocks are the same, then there is only 1 value for |
166 | | // the OpPhi. Other transformation should be able to simplify that. |
167 | 2.66k | return false; |
168 | 2.66k | } |
169 | | // All phis will have the same common dominator, so cache the result |
170 | | // for this block. If there is no common dominator, then we cannot transform |
171 | | // any phi in this basic block. |
172 | 45.2k | *common = dominators->CommonDominator(inc0, inc1); |
173 | 45.2k | if (!*common || cfg()->IsPseudoEntryBlock(*common)) return false; |
174 | 39.7k | Instruction* branch = (*common)->terminator(); |
175 | 39.7k | if (branch->opcode() != spv::Op::OpBranchConditional) return false; |
176 | 39.4k | auto merge = (*common)->GetMergeInst(); |
177 | 39.4k | if (!merge || merge->opcode() != spv::Op::OpSelectionMerge) return false; |
178 | 35.4k | if (spv::SelectionControlMask(merge->GetSingleWordInOperand(1)) == |
179 | 35.4k | spv::SelectionControlMask::DontFlatten) { |
180 | 4.34k | return false; |
181 | 4.34k | } |
182 | 31.1k | if ((*common)->MergeBlockIdIfAny() != block->id()) return false; |
183 | | |
184 | 21.5k | return true; |
185 | 31.1k | } |
186 | | |
187 | 19.0k | bool IfConversion::CheckPhiUsers(Instruction* phi, BasicBlock* block) { |
188 | 19.0k | return get_def_use_mgr()->WhileEachUser( |
189 | 40.1k | phi, [block, this](Instruction* user) { |
190 | 40.1k | if (user->opcode() == spv::Op::OpPhi && |
191 | 10.8k | context()->get_instr_block(user) == block) |
192 | 0 | return false; |
193 | 40.1k | return true; |
194 | 40.1k | }); |
195 | 19.0k | } |
196 | | |
197 | | uint32_t IfConversion::SplatCondition(analysis::Vector* vec_data_ty, |
198 | | uint32_t cond, |
199 | 1.00k | InstructionBuilder* builder) { |
200 | | // If the data inputs to OpSelect are vectors, the condition for |
201 | | // OpSelect must be a boolean vector with the same number of |
202 | | // components. So splat the condition for the branch into a vector |
203 | | // type. |
204 | 1.00k | analysis::Bool bool_ty; |
205 | 1.00k | analysis::Vector bool_vec_ty(&bool_ty, vec_data_ty->element_count()); |
206 | 1.00k | uint32_t bool_vec_id = |
207 | 1.00k | context()->get_type_mgr()->GetTypeInstruction(&bool_vec_ty); |
208 | 1.00k | std::vector<uint32_t> ids(vec_data_ty->element_count(), cond); |
209 | | // TODO(1841): Handle id overflow. |
210 | 1.00k | return builder->AddCompositeConstruct(bool_vec_id, ids)->result_id(); |
211 | 1.00k | } |
212 | | |
213 | 19.0k | bool IfConversion::CheckType(uint32_t id) { |
214 | 19.0k | Instruction* type = get_def_use_mgr()->GetDef(id); |
215 | 19.0k | spv::Op op = type->opcode(); |
216 | 19.0k | if (spvOpcodeIsScalarType(op) || op == spv::Op::OpTypePointer || |
217 | 6.85k | op == spv::Op::OpTypeVector) |
218 | 19.0k | return true; |
219 | 24 | return false; |
220 | 19.0k | } |
221 | | |
222 | 38.0k | BasicBlock* IfConversion::GetBlock(uint32_t id) { |
223 | 38.0k | return context()->get_instr_block(get_def_use_mgr()->GetDef(id)); |
224 | 38.0k | } |
225 | | |
226 | | BasicBlock* IfConversion::GetIncomingBlock(Instruction* phi, |
227 | 19.0k | uint32_t predecessor) { |
228 | 19.0k | uint32_t in_index = 2 * predecessor + 1; |
229 | 19.0k | return GetBlock(phi->GetSingleWordInOperand(in_index)); |
230 | 19.0k | } |
231 | | |
232 | | Instruction* IfConversion::GetIncomingValue(Instruction* phi, |
233 | 38.0k | uint32_t predecessor) { |
234 | 38.0k | uint32_t in_index = 2 * predecessor; |
235 | 38.0k | return get_def_use_mgr()->GetDef(phi->GetSingleWordInOperand(in_index)); |
236 | 38.0k | } |
237 | | |
238 | | void IfConversion::HoistInstruction(Instruction* inst, BasicBlock* target_block, |
239 | 351 | DominatorAnalysis* dominators) { |
240 | 351 | BasicBlock* inst_block = context()->get_instr_block(inst); |
241 | 351 | if (!inst_block) { |
242 | | // This is in the header, and dominates everything. |
243 | 187 | return; |
244 | 187 | } |
245 | | |
246 | 164 | if (dominators->Dominates(inst_block, target_block)) { |
247 | | // Already in position. No work to do. |
248 | 132 | return; |
249 | 132 | } |
250 | | |
251 | 164 | assert(inst->IsOpcodeCodeMotionSafe() && |
252 | 32 | "Trying to move an instruction that is not safe to move."); |
253 | | |
254 | | // First hoist all instructions it depends on. |
255 | 32 | analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr(); |
256 | 32 | inst->ForEachInId( |
257 | 64 | [this, target_block, def_use_mgr, dominators](uint32_t* id) { |
258 | 64 | Instruction* operand_inst = def_use_mgr->GetDef(*id); |
259 | 64 | HoistInstruction(operand_inst, target_block, dominators); |
260 | 64 | }); |
261 | | |
262 | 32 | Instruction* insertion_pos = target_block->terminator(); |
263 | 32 | if ((insertion_pos)->PreviousNode()->opcode() == spv::Op::OpSelectionMerge) { |
264 | 32 | insertion_pos = insertion_pos->PreviousNode(); |
265 | 32 | } |
266 | 32 | inst->RemoveFromList(); |
267 | 32 | insertion_pos->InsertBefore(std::unique_ptr<Instruction>(inst)); |
268 | 32 | context()->set_instr_block(inst, target_block); |
269 | 32 | } |
270 | | |
271 | | bool IfConversion::CanHoistInstruction(Instruction* inst, |
272 | | BasicBlock* target_block, |
273 | 112 | DominatorAnalysis* dominators) { |
274 | 112 | BasicBlock* inst_block = context()->get_instr_block(inst); |
275 | 112 | if (!inst_block) { |
276 | | // This is in the header, and dominates everything. |
277 | 28 | return true; |
278 | 28 | } |
279 | | |
280 | 84 | if (dominators->Dominates(inst_block, target_block)) { |
281 | | // Already in position. No work to do. |
282 | 36 | return true; |
283 | 36 | } |
284 | | |
285 | 48 | if (!inst->IsOpcodeCodeMotionSafe()) { |
286 | 16 | return false; |
287 | 16 | } |
288 | | |
289 | | // Check all instruction |inst| depends on. |
290 | 32 | analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr(); |
291 | 32 | return inst->WhileEachInId( |
292 | 64 | [this, target_block, def_use_mgr, dominators](uint32_t* id) { |
293 | 64 | Instruction* operand_inst = def_use_mgr->GetDef(*id); |
294 | 64 | return CanHoistInstruction(operand_inst, target_block, dominators); |
295 | 64 | }); |
296 | 48 | } |
297 | | |
298 | | } // namespace opt |
299 | | } // namespace spvtools |