| 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 |
Pass::Status IfConversion::Process() { |
| 26 |
if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader)) { |
| 27 |
return Status::SuccessWithoutChange; |
| 28 |
} |
| 29 |
|
| 30 |
const ValueNumberTable& vn_table = *context()->GetValueNumberTable(); |
| 31 |
bool modified = false; |
| 32 |
std::vector<Instruction*> to_kill; |
| 33 |
for (auto& func : *get_module()) { |
| 34 |
DominatorAnalysis* dominators = context()->GetDominatorAnalysis(&func); |
| 35 |
for (auto& block : func) { |
| 36 |
// Check if it is possible for |block| to have phis that can be |
| 37 |
// transformed. |
| 38 |
BasicBlock* common = nullptr; |
| 39 |
if (!CheckBlock(&block, dominators, &common)) continue; |
| 40 |
|
| 41 |
// Get an insertion point. |
| 42 |
auto iter = block.begin(); |
| 43 |
while (iter != block.end() && iter->opcode() == SpvOpPhi) { |
| 44 |
++iter; |
| 45 |
} |
| 46 |
|
| 47 |
InstructionBuilder builder( |
| 48 |
context(), &*iter, |
| 49 |
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping); |
| 50 |
block.ForEachPhiInst([this, &builder, &modified, &common, &to_kill, |
| 51 |
dominators, &block, &vn_table](Instruction* phi) { |
| 52 |
// This phi is not compatible, but subsequent phis might be. |
| 53 |
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 |
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 |
BasicBlock* inc0 = GetIncomingBlock(phi, 0u); |
| 66 |
Instruction* branch = common->terminator(); |
| 67 |
uint32_t condition = branch->GetSingleWordInOperand(0u); |
| 68 |
BasicBlock* then_block = GetBlock(branch->GetSingleWordInOperand(1u)); |
| 69 |
Instruction* true_value = nullptr; |
| 70 |
Instruction* false_value = nullptr; |
| 71 |
if ((then_block == &block && inc0 == common) || |
| 72 |
dominators->Dominates(then_block, inc0)) { |
| 73 |
true_value = GetIncomingValue(phi, 0u); |
| 74 |
false_value = GetIncomingValue(phi, 1u); |
| 75 |
} else { |
| 76 |
true_value = GetIncomingValue(phi, 1u); |
| 77 |
false_value = GetIncomingValue(phi, 0u); |
| 78 |
} |
| 79 |
|
| 80 |
BasicBlock* true_def_block = context()->get_instr_block(true_value); |
| 81 |
BasicBlock* false_def_block = context()->get_instr_block(false_value); |
| 82 |
|
| 83 |
uint32_t true_vn = vn_table.GetValueNumber(true_value); |
| 84 |
uint32_t false_vn = vn_table.GetValueNumber(false_value); |
| 85 |
if (true_vn != 0 && true_vn == false_vn) { |
| 86 |
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 |
if (!true_def_block || |
| 92 |
dominators->Dominates(true_def_block, &block)) { |
| 93 |
inst_to_use = true_value; |
| 94 |
} else if (!false_def_block || |
| 95 |
dominators->Dominates(false_def_block, &block)) { |
| 96 |
inst_to_use = false_value; |
| 97 |
} else if (CanHoistInstruction(true_value, common, dominators)) { |
| 98 |
inst_to_use = true_value; |
| 99 |
} else if (CanHoistInstruction(false_value, common, dominators)) { |
| 100 |
inst_to_use = false_value; |
| 101 |
} |
| 102 |
|
| 103 |
if (inst_to_use != nullptr) { |
| 104 |
modified = true; |
| 105 |
HoistInstruction(inst_to_use, common, dominators); |
| 106 |
context()->KillNamesAndDecorates(phi); |
| 107 |
context()->ReplaceAllUsesWith(phi->result_id(), |
| 108 |
inst_to_use->result_id()); |
| 109 |
} |
| 110 |
return; |
| 111 |
} |
| 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 |
if (true_def_block && !dominators->Dominates(true_def_block, &block)) |
| 118 |
return; |
| 119 |
|
| 120 |
if (false_def_block && !dominators->Dominates(false_def_block, &block)) |
| 121 |
return; |
| 122 |
|
| 123 |
analysis::Type* data_ty = |
| 124 |
context()->get_type_mgr()->GetType(true_value->type_id()); |
| 125 |
if (analysis::Vector* vec_data_ty = data_ty->AsVector()) { |
| 126 |
condition = SplatCondition(vec_data_ty, condition, &builder); |
| 127 |
} |
| 128 |
|
| 129 |
Instruction* select = builder.AddSelect(phi->type_id(), condition, |
| 130 |
true_value->result_id(), |
| 131 |
false_value->result_id()); |
| 132 |
select->UpdateDebugInfoFrom(phi); |
| 133 |
context()->ReplaceAllUsesWith(phi->result_id(), select->result_id()); |
| 134 |
to_kill.push_back(phi); |
| 135 |
modified = true; |
| 136 |
|
| 137 |
return; |
| 138 |
}); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
for (auto inst : to_kill) { |
| 143 |
context()->KillInst(inst); |
| 144 |
} |
| 145 |
|
| 146 |
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange; |
| 147 |
} |
| 148 |
|
| 149 |
bool IfConversion::CheckBlock(BasicBlock* block, DominatorAnalysis* dominators, |
| 150 |
BasicBlock** common) { |
| 151 |
const std::vector<uint32_t>& preds = cfg()->preds(block->id()); |
| 152 |
|
| 153 |
// TODO(alan-baker): Extend to more than two predecessors |
| 154 |
if (preds.size() != 2) return false; |
| 155 |
|
| 156 |
BasicBlock* inc0 = context()->get_instr_block(preds[0]); |
| 157 |
if (dominators->Dominates(block, inc0)) return false; |
| 158 |
|
| 159 |
BasicBlock* inc1 = context()->get_instr_block(preds[1]); |
| 160 |
if (dominators->Dominates(block, inc1)) return false; |
| 161 |
|
| 162 |
// All phis will have the same common dominator, so cache the result |
| 163 |
// for this block. If there is no common dominator, then we cannot transform |
| 164 |
// any phi in this basic block. |
| 165 |
*common = dominators->CommonDominator(inc0, inc1); |
| 166 |
if (!*common || cfg()->IsPseudoEntryBlock(*common)) return false; |
| 167 |
Instruction* branch = (*common)->terminator(); |
| 168 |
if (branch->opcode() != SpvOpBranchConditional) return false; |
| 169 |
auto merge = (*common)->GetMergeInst(); |
| 170 |
if (!merge || merge->opcode() != SpvOpSelectionMerge) return false; |
| 171 |
if ((*common)->MergeBlockIdIfAny() != block->id()) return false; |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
bool IfConversion::CheckPhiUsers(Instruction* phi, BasicBlock* block) { |
| 177 |
return get_def_use_mgr()->WhileEachUser(phi, [block, |
| 178 |
this](Instruction* user) { |
| 179 |
if (user->opcode() == SpvOpPhi && context()->get_instr_block(user) == block) |
| 180 |
return false; |
| 181 |
return true; |
| 182 |
}); |
| 183 |
} |
| 184 |
|
| 185 |
uint32_t IfConversion::SplatCondition(analysis::Vector* vec_data_ty, |
| 186 |
uint32_t cond, |
| 187 |
InstructionBuilder* builder) { |
| 188 |
// If the data inputs to OpSelect are vectors, the condition for |
| 189 |
// OpSelect must be a boolean vector with the same number of |
| 190 |
// components. So splat the condition for the branch into a vector |
| 191 |
// type. |
| 192 |
analysis::Bool bool_ty; |
| 193 |
analysis::Vector bool_vec_ty(&bool_ty, vec_data_ty->element_count()); |
| 194 |
uint32_t bool_vec_id = |
| 195 |
context()->get_type_mgr()->GetTypeInstruction(&bool_vec_ty); |
| 196 |
std::vector<uint32_t> ids(vec_data_ty->element_count(), cond); |
| 197 |
return builder->AddCompositeConstruct(bool_vec_id, ids)->result_id(); |
| 198 |
} |
| 199 |
|
| 200 |
bool IfConversion::CheckType(uint32_t id) { |
| 201 |
Instruction* type = get_def_use_mgr()->GetDef(id); |
| 202 |
SpvOp op = type->opcode(); |
| 203 |
if (spvOpcodeIsScalarType(op) || op == SpvOpTypePointer || |
| 204 |
op == SpvOpTypeVector) |
| 205 |
return true; |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
BasicBlock* IfConversion::GetBlock(uint32_t id) { |
| 210 |
return context()->get_instr_block(get_def_use_mgr()->GetDef(id)); |
| 211 |
} |
| 212 |
|
| 213 |
BasicBlock* IfConversion::GetIncomingBlock(Instruction* phi, |
| 214 |
uint32_t predecessor) { |
| 215 |
uint32_t in_index = 2 * predecessor + 1; |
| 216 |
return GetBlock(phi->GetSingleWordInOperand(in_index)); |
| 217 |
} |
| 218 |
|
| 219 |
Instruction* IfConversion::GetIncomingValue(Instruction* phi, |
| 220 |
uint32_t predecessor) { |
| 221 |
uint32_t in_index = 2 * predecessor; |
| 222 |
return get_def_use_mgr()->GetDef(phi->GetSingleWordInOperand(in_index)); |
| 223 |
} |
| 224 |
|
| 225 |
void IfConversion::HoistInstruction(Instruction* inst, BasicBlock* target_block, |
| 226 |
DominatorAnalysis* dominators) { |
| 227 |
BasicBlock* inst_block = context()->get_instr_block(inst); |
| 228 |
if (!inst_block) { |
| 229 |
// This is in the header, and dominates everything. |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
if (dominators->Dominates(inst_block, target_block)) { |
| 234 |
// Already in position. No work to do. |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
assert(inst->IsOpcodeCodeMotionSafe() && |
| 239 |
"Trying to move an instruction that is not safe to move."); |
| 240 |
|
| 241 |
// First hoist all instructions it depends on. |
| 242 |
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr(); |
| 243 |
inst->ForEachInId( |
| 244 |
[this, target_block, def_use_mgr, dominators](uint32_t* id) { |
| 245 |
Instruction* operand_inst = def_use_mgr->GetDef(*id); |
| 246 |
HoistInstruction(operand_inst, target_block, dominators); |
| 247 |
}); |
| 248 |
|
| 249 |
Instruction* insertion_pos = target_block->terminator(); |
| 250 |
if ((insertion_pos)->PreviousNode()->opcode() == SpvOpSelectionMerge) { |
| 251 |
insertion_pos = insertion_pos->PreviousNode(); |
| 252 |
} |
| 253 |
inst->RemoveFromList(); |
| 254 |
insertion_pos->InsertBefore(std::unique_ptr<Instruction>(inst)); |
| 255 |
context()->set_instr_block(inst, target_block); |
| 256 |
} |
| 257 |
|
| 258 |
bool IfConversion::CanHoistInstruction(Instruction* inst, |
| 259 |
BasicBlock* target_block, |
| 260 |
DominatorAnalysis* dominators) { |
| 261 |
BasicBlock* inst_block = context()->get_instr_block(inst); |
| 262 |
if (!inst_block) { |
| 263 |
// This is in the header, and dominates everything. |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
if (dominators->Dominates(inst_block, target_block)) { |
| 268 |
// Already in position. No work to do. |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
if (!inst->IsOpcodeCodeMotionSafe()) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
// Check all instruction |inst| depends on. |
| 277 |
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr(); |
| 278 |
return inst->WhileEachInId( |
| 279 |
[this, target_block, def_use_mgr, dominators](uint32_t* id) { |
| 280 |
Instruction* operand_inst = def_use_mgr->GetDef(*id); |
| 281 |
return CanHoistInstruction(operand_inst, target_block, dominators); |
| 282 |
}); |
| 283 |
} |
| 284 |
|
| 285 |
} // namespace opt |
| 286 |
} // namespace spvtools |