2017-06-02 19:23:20 +00:00
|
|
|
// Copyright (c) 2017 The Khronos Group Inc.
|
|
|
|
// Copyright (c) 2017 Valve Corporation
|
|
|
|
// Copyright (c) 2017 LunarG Inc.
|
2018-01-04 22:04:03 +00:00
|
|
|
// Copyright (c) 2018 Google Inc.
|
2017-06-02 19:23:20 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "dead_branch_elim_pass.h"
|
|
|
|
|
|
|
|
#include "cfa.h"
|
2017-11-13 20:31:43 +00:00
|
|
|
#include "ir_context.h"
|
2017-11-27 15:16:41 +00:00
|
|
|
#include "iterator.h"
|
2018-01-04 22:04:03 +00:00
|
|
|
#include "make_unique.h"
|
2017-06-02 19:23:20 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const uint32_t kBranchCondTrueLabIdInIdx = 1;
|
|
|
|
const uint32_t kBranchCondFalseLabIdInIdx = 2;
|
|
|
|
|
2017-11-08 17:40:02 +00:00
|
|
|
} // anonymous namespace
|
2017-06-02 19:23:20 +00:00
|
|
|
|
2017-10-06 02:37:00 +00:00
|
|
|
bool DeadBranchElimPass::GetConstCondition(uint32_t condId, bool* condVal) {
|
|
|
|
bool condIsConst;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* cInst = get_def_use_mgr()->GetDef(condId);
|
2017-06-02 19:23:20 +00:00
|
|
|
switch (cInst->opcode()) {
|
|
|
|
case SpvOpConstantFalse: {
|
|
|
|
*condVal = false;
|
2017-10-06 02:37:00 +00:00
|
|
|
condIsConst = true;
|
2017-06-02 19:23:20 +00:00
|
|
|
} break;
|
|
|
|
case SpvOpConstantTrue: {
|
|
|
|
*condVal = true;
|
2017-10-06 02:37:00 +00:00
|
|
|
condIsConst = true;
|
2017-06-02 19:23:20 +00:00
|
|
|
} break;
|
|
|
|
case SpvOpLogicalNot: {
|
|
|
|
bool negVal;
|
2017-11-08 17:40:02 +00:00
|
|
|
condIsConst =
|
|
|
|
GetConstCondition(cInst->GetSingleWordInOperand(0), &negVal);
|
|
|
|
if (condIsConst) *condVal = !negVal;
|
2017-06-02 19:23:20 +00:00
|
|
|
} break;
|
2017-11-08 17:40:02 +00:00
|
|
|
default: { condIsConst = false; } break;
|
2017-06-02 19:23:20 +00:00
|
|
|
}
|
2017-10-06 02:37:00 +00:00
|
|
|
return condIsConst;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeadBranchElimPass::GetConstInteger(uint32_t selId, uint32_t* selVal) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* sInst = get_def_use_mgr()->GetDef(selId);
|
2017-10-06 02:37:00 +00:00
|
|
|
uint32_t typeId = sInst->type_id();
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* typeInst = get_def_use_mgr()->GetDef(typeId);
|
2017-10-12 16:21:26 +00:00
|
|
|
if (!typeInst || (typeInst->opcode() != SpvOpTypeInt)) return false;
|
2017-10-06 02:37:00 +00:00
|
|
|
// TODO(greg-lunarg): Support non-32 bit ints
|
2017-11-08 17:40:02 +00:00
|
|
|
if (typeInst->GetSingleWordInOperand(0) != 32) return false;
|
2017-10-06 02:37:00 +00:00
|
|
|
if (sInst->opcode() == SpvOpConstant) {
|
|
|
|
*selVal = sInst->GetSingleWordInOperand(0);
|
|
|
|
return true;
|
2017-11-08 17:40:02 +00:00
|
|
|
} else if (sInst->opcode() == SpvOpConstantNull) {
|
2017-10-06 02:37:00 +00:00
|
|
|
*selVal = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-06-02 19:23:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void DeadBranchElimPass::AddBranch(uint32_t labelId, BasicBlock* bp) {
|
2018-01-04 22:04:03 +00:00
|
|
|
assert(get_def_use_mgr()->GetDef(labelId) != nullptr);
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> newBranch(
|
|
|
|
new Instruction(context(), SpvOpBranch, 0, 0,
|
|
|
|
{{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {labelId}}}));
|
2018-06-01 17:04:46 +00:00
|
|
|
context()->AnalyzeDefUse(&*newBranch);
|
|
|
|
context()->set_instr_block(&*newBranch, bp);
|
2017-06-02 19:23:20 +00:00
|
|
|
bp->AddInstruction(std::move(newBranch));
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* DeadBranchElimPass::GetParentBlock(uint32_t id) {
|
2018-01-04 22:04:03 +00:00
|
|
|
return context()->get_instr_block(get_def_use_mgr()->GetDef(id));
|
2017-06-02 19:23:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
bool DeadBranchElimPass::MarkLiveBlocks(
|
2018-07-12 19:14:43 +00:00
|
|
|
Function* func, std::unordered_set<BasicBlock*>* live_blocks) {
|
|
|
|
std::unordered_set<BasicBlock*> continues;
|
|
|
|
std::vector<BasicBlock*> stack;
|
2018-01-04 22:04:03 +00:00
|
|
|
stack.push_back(&*func->begin());
|
|
|
|
bool modified = false;
|
|
|
|
while (!stack.empty()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* block = stack.back();
|
2018-01-04 22:04:03 +00:00
|
|
|
stack.pop_back();
|
2017-06-02 19:23:20 +00:00
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
// Live blocks doubles as visited set.
|
|
|
|
if (!live_blocks->insert(block).second) continue;
|
2017-06-02 19:23:20 +00:00
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
uint32_t cont_id = block->ContinueBlockIdIfAny();
|
|
|
|
if (cont_id != 0) continues.insert(GetParentBlock(cont_id));
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* terminator = block->terminator();
|
2018-01-04 22:04:03 +00:00
|
|
|
uint32_t live_lab_id = 0;
|
|
|
|
// Check if the terminator has a single valid successor.
|
|
|
|
if (terminator->opcode() == SpvOpBranchConditional) {
|
|
|
|
bool condVal;
|
|
|
|
if (GetConstCondition(terminator->GetSingleWordInOperand(0u), &condVal)) {
|
|
|
|
live_lab_id = terminator->GetSingleWordInOperand(
|
|
|
|
condVal ? kBranchCondTrueLabIdInIdx : kBranchCondFalseLabIdInIdx);
|
|
|
|
}
|
|
|
|
} else if (terminator->opcode() == SpvOpSwitch) {
|
|
|
|
uint32_t sel_val;
|
|
|
|
if (GetConstInteger(terminator->GetSingleWordInOperand(0u), &sel_val)) {
|
|
|
|
// Search switch operands for selector value, set live_lab_id to
|
|
|
|
// corresponding label, use default if not found.
|
|
|
|
uint32_t icnt = 0;
|
|
|
|
uint32_t case_val;
|
2018-01-12 20:05:53 +00:00
|
|
|
terminator->WhileEachInOperand(
|
2018-01-04 22:04:03 +00:00
|
|
|
[&icnt, &case_val, &sel_val, &live_lab_id](const uint32_t* idp) {
|
|
|
|
if (icnt == 1) {
|
|
|
|
// Start with default label.
|
|
|
|
live_lab_id = *idp;
|
|
|
|
} else if (icnt > 1) {
|
|
|
|
if (icnt % 2 == 0) {
|
|
|
|
case_val = *idp;
|
|
|
|
} else {
|
2018-01-12 20:05:53 +00:00
|
|
|
if (case_val == sel_val) {
|
|
|
|
live_lab_id = *idp;
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-04 22:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++icnt;
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
2018-01-04 22:04:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't simplify branches of continue blocks. A path from the continue to
|
|
|
|
// the header is required.
|
|
|
|
// TODO(alan-baker): They can be simplified iff there remains a path to the
|
|
|
|
// backedge. Structured control flow should guarantee one path hits the
|
|
|
|
// backedge, but I've removed the requirement for structured control flow
|
|
|
|
// from this pass.
|
|
|
|
bool simplify = live_lab_id != 0 && !continues.count(block);
|
|
|
|
|
|
|
|
if (simplify) {
|
|
|
|
modified = true;
|
|
|
|
// Replace with unconditional branch.
|
|
|
|
// Remove the merge instruction if it is a selection merge.
|
|
|
|
AddBranch(live_lab_id, block);
|
|
|
|
context()->KillInst(terminator);
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* mergeInst = block->GetMergeInst();
|
2018-01-30 15:15:43 +00:00
|
|
|
if (mergeInst && mergeInst->opcode() == SpvOpSelectionMerge) {
|
2018-01-04 22:04:03 +00:00
|
|
|
context()->KillInst(mergeInst);
|
|
|
|
}
|
|
|
|
stack.push_back(GetParentBlock(live_lab_id));
|
|
|
|
} else {
|
|
|
|
// All successors are live.
|
2018-02-02 22:17:42 +00:00
|
|
|
const auto* const_block = block;
|
|
|
|
const_block->ForEachSuccessorLabel([&stack, this](const uint32_t label) {
|
2018-01-04 22:04:03 +00:00
|
|
|
stack.push_back(GetParentBlock(label));
|
2017-11-14 19:11:50 +00:00
|
|
|
});
|
2018-01-04 22:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
2017-06-02 19:23:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
void DeadBranchElimPass::MarkUnreachableStructuredTargets(
|
2018-07-12 19:14:43 +00:00
|
|
|
const std::unordered_set<BasicBlock*>& live_blocks,
|
|
|
|
std::unordered_set<BasicBlock*>* unreachable_merges,
|
|
|
|
std::unordered_map<BasicBlock*, BasicBlock*>* unreachable_continues) {
|
2018-01-04 22:04:03 +00:00
|
|
|
for (auto block : live_blocks) {
|
|
|
|
if (auto merge_id = block->MergeBlockIdIfAny()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* merge_block = GetParentBlock(merge_id);
|
2018-01-04 22:04:03 +00:00
|
|
|
if (!live_blocks.count(merge_block)) {
|
|
|
|
unreachable_merges->insert(merge_block);
|
|
|
|
}
|
|
|
|
if (auto cont_id = block->ContinueBlockIdIfAny()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* cont_block = GetParentBlock(cont_id);
|
2018-01-04 22:04:03 +00:00
|
|
|
if (!live_blocks.count(cont_block)) {
|
|
|
|
(*unreachable_continues)[cont_block] = block;
|
2017-10-16 23:30:52 +00:00
|
|
|
}
|
2018-01-04 22:04:03 +00:00
|
|
|
}
|
2017-10-16 23:30:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
bool DeadBranchElimPass::FixPhiNodesInLiveBlocks(
|
2018-07-12 19:14:43 +00:00
|
|
|
Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
|
|
|
|
const std::unordered_map<BasicBlock*, BasicBlock*>& unreachable_continues) {
|
2017-06-02 19:23:20 +00:00
|
|
|
bool modified = false;
|
2018-01-04 22:04:03 +00:00
|
|
|
for (auto& block : *func) {
|
|
|
|
if (live_blocks.count(&block)) {
|
|
|
|
for (auto iter = block.begin(); iter != block.end();) {
|
|
|
|
if (iter->opcode() != SpvOpPhi) {
|
|
|
|
break;
|
|
|
|
}
|
2017-10-06 02:37:00 +00:00
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
bool changed = false;
|
|
|
|
bool backedge_added = false;
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* inst = &*iter;
|
|
|
|
std::vector<Operand> operands;
|
2018-01-04 22:04:03 +00:00
|
|
|
// Build a complete set of operands (not just input operands). Start
|
|
|
|
// with type and result id operands.
|
|
|
|
operands.push_back(inst->GetOperand(0u));
|
|
|
|
operands.push_back(inst->GetOperand(1u));
|
|
|
|
// Iterate through the incoming labels and determine which to keep
|
2018-02-14 02:08:43 +00:00
|
|
|
// and/or modify. If there in an unreachable continue block, there will
|
|
|
|
// be an edge from that block to the header. We need to keep it to
|
|
|
|
// maintain the structured control flow. If the header has more that 2
|
|
|
|
// incoming edges, then the OpPhi must have an entry for that edge.
|
|
|
|
// However, if there is only one other incoming edge, the OpPhi can be
|
|
|
|
// eliminated.
|
2018-01-04 22:04:03 +00:00
|
|
|
for (uint32_t i = 1; i < inst->NumInOperands(); i += 2) {
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* inc = GetParentBlock(inst->GetSingleWordInOperand(i));
|
2018-01-04 22:04:03 +00:00
|
|
|
auto cont_iter = unreachable_continues.find(inc);
|
|
|
|
if (cont_iter != unreachable_continues.end() &&
|
2018-02-14 02:08:43 +00:00
|
|
|
cont_iter->second == &block && inst->NumInOperands() > 4) {
|
2018-01-30 20:00:27 +00:00
|
|
|
if (get_def_use_mgr()
|
|
|
|
->GetDef(inst->GetSingleWordInOperand(i - 1))
|
|
|
|
->opcode() == SpvOpUndef) {
|
|
|
|
// Already undef incoming value, no change necessary.
|
|
|
|
operands.push_back(inst->GetInOperand(i - 1));
|
|
|
|
operands.push_back(inst->GetInOperand(i));
|
|
|
|
backedge_added = true;
|
|
|
|
} else {
|
|
|
|
// Replace incoming value with undef if this phi exists in the
|
|
|
|
// loop header. Otherwise, this edge is not live since the
|
|
|
|
// unreachable continue block will be replaced with an
|
|
|
|
// unconditional branch to the header only.
|
|
|
|
operands.emplace_back(
|
|
|
|
SPV_OPERAND_TYPE_ID,
|
|
|
|
std::initializer_list<uint32_t>{Type2Undef(inst->type_id())});
|
|
|
|
operands.push_back(inst->GetInOperand(i));
|
|
|
|
changed = true;
|
|
|
|
backedge_added = true;
|
|
|
|
}
|
2018-01-04 22:04:03 +00:00
|
|
|
} else if (live_blocks.count(inc) && inc->IsSuccessor(&block)) {
|
|
|
|
// Keep live incoming edge.
|
|
|
|
operands.push_back(inst->GetInOperand(i - 1));
|
|
|
|
operands.push_back(inst->GetInOperand(i));
|
|
|
|
} else {
|
|
|
|
// Remove incoming edge.
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 19:23:20 +00:00
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
if (changed) {
|
2018-01-30 20:00:27 +00:00
|
|
|
modified = true;
|
2018-01-04 22:04:03 +00:00
|
|
|
uint32_t continue_id = block.ContinueBlockIdIfAny();
|
|
|
|
if (!backedge_added && continue_id != 0 &&
|
2018-02-14 02:08:43 +00:00
|
|
|
unreachable_continues.count(GetParentBlock(continue_id)) &&
|
|
|
|
operands.size() > 4) {
|
2018-01-04 22:04:03 +00:00
|
|
|
// Changed the backedge to branch from the continue block instead
|
|
|
|
// of a successor of the continue block. Add an entry to the phi to
|
|
|
|
// provide an undef for the continue block. Since the successor of
|
|
|
|
// the continue must also be unreachable (dominated by the continue
|
|
|
|
// block), any entry for the original backedge has been removed
|
|
|
|
// from the phi operands.
|
|
|
|
operands.emplace_back(
|
|
|
|
SPV_OPERAND_TYPE_ID,
|
|
|
|
std::initializer_list<uint32_t>{Type2Undef(inst->type_id())});
|
|
|
|
operands.emplace_back(SPV_OPERAND_TYPE_ID,
|
|
|
|
std::initializer_list<uint32_t>{continue_id});
|
|
|
|
}
|
2017-10-06 02:37:00 +00:00
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
// Either replace the phi with a single value or rebuild the phi out
|
|
|
|
// of |operands|.
|
|
|
|
//
|
|
|
|
// We always have type and result id operands. So this phi has a
|
|
|
|
// single source if there are two more operands beyond those.
|
|
|
|
if (operands.size() == 4) {
|
|
|
|
// First input data operands is at index 2.
|
|
|
|
uint32_t replId = operands[2u].words[0];
|
|
|
|
context()->ReplaceAllUsesWith(inst->result_id(), replId);
|
|
|
|
iter = context()->KillInst(&*inst);
|
|
|
|
} else {
|
|
|
|
// We've rewritten the operands, so first instruct the def/use
|
|
|
|
// manager to forget uses in the phi before we replace them. After
|
|
|
|
// replacing operands update the def/use manager by re-analyzing
|
|
|
|
// the used ids in this phi.
|
|
|
|
get_def_use_mgr()->EraseUseRecordsOfOperandIds(inst);
|
|
|
|
inst->ReplaceOperands(operands);
|
|
|
|
get_def_use_mgr()->AnalyzeInstUse(inst);
|
|
|
|
++iter;
|
2017-10-06 02:37:00 +00:00
|
|
|
}
|
2018-01-04 22:04:03 +00:00
|
|
|
} else {
|
|
|
|
++iter;
|
2017-10-06 02:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-02 19:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 22:04:03 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeadBranchElimPass::EraseDeadBlocks(
|
2018-07-12 19:14:43 +00:00
|
|
|
Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
|
|
|
|
const std::unordered_set<BasicBlock*>& unreachable_merges,
|
|
|
|
const std::unordered_map<BasicBlock*, BasicBlock*>& unreachable_continues) {
|
2018-01-04 22:04:03 +00:00
|
|
|
bool modified = false;
|
|
|
|
for (auto ebi = func->begin(); ebi != func->end();) {
|
|
|
|
if (unreachable_merges.count(&*ebi)) {
|
2018-01-30 20:00:27 +00:00
|
|
|
if (ebi->begin() != ebi->tail() ||
|
|
|
|
ebi->terminator()->opcode() != SpvOpUnreachable) {
|
|
|
|
// Make unreachable, but leave the label.
|
|
|
|
KillAllInsts(&*ebi, false);
|
|
|
|
// Add unreachable terminator.
|
2018-07-12 19:14:43 +00:00
|
|
|
ebi->AddInstruction(
|
|
|
|
MakeUnique<Instruction>(context(), SpvOpUnreachable, 0, 0,
|
|
|
|
std::initializer_list<Operand>{}));
|
2018-06-01 17:04:46 +00:00
|
|
|
context()->set_instr_block(&*ebi->tail(), &*ebi);
|
2018-01-30 20:00:27 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
2018-01-04 22:04:03 +00:00
|
|
|
++ebi;
|
|
|
|
} else if (unreachable_continues.count(&*ebi)) {
|
|
|
|
uint32_t cont_id = unreachable_continues.find(&*ebi)->second->id();
|
2018-01-30 20:00:27 +00:00
|
|
|
if (ebi->begin() != ebi->tail() ||
|
|
|
|
ebi->terminator()->opcode() != SpvOpBranch ||
|
|
|
|
ebi->terminator()->GetSingleWordInOperand(0u) != cont_id) {
|
|
|
|
// Make unreachable, but leave the label.
|
|
|
|
KillAllInsts(&*ebi, false);
|
|
|
|
// Add unconditional branch to header.
|
|
|
|
assert(unreachable_continues.count(&*ebi));
|
2018-07-12 19:14:43 +00:00
|
|
|
ebi->AddInstruction(MakeUnique<Instruction>(
|
2018-07-09 15:32:29 +00:00
|
|
|
context(), SpvOpBranch, 0, 0,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {cont_id}}}));
|
2018-01-30 20:00:27 +00:00
|
|
|
get_def_use_mgr()->AnalyzeInstUse(&*ebi->tail());
|
2018-06-01 17:04:46 +00:00
|
|
|
context()->set_instr_block(&*ebi->tail(), &*ebi);
|
2018-01-30 20:00:27 +00:00
|
|
|
modified = true;
|
|
|
|
}
|
2018-01-04 22:04:03 +00:00
|
|
|
++ebi;
|
|
|
|
} else if (!live_blocks.count(&*ebi)) {
|
|
|
|
// Kill this block.
|
|
|
|
KillAllInsts(&*ebi);
|
2017-06-02 19:23:20 +00:00
|
|
|
ebi = ebi.Erase();
|
2018-01-04 22:04:03 +00:00
|
|
|
modified = true;
|
|
|
|
} else {
|
2017-06-02 19:23:20 +00:00
|
|
|
++ebi;
|
2018-01-04 22:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool DeadBranchElimPass::EliminateDeadBranches(Function* func) {
|
2018-01-04 22:04:03 +00:00
|
|
|
bool modified = false;
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_set<BasicBlock*> live_blocks;
|
2018-01-04 22:04:03 +00:00
|
|
|
modified |= MarkLiveBlocks(func, &live_blocks);
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_set<BasicBlock*> unreachable_merges;
|
|
|
|
std::unordered_map<BasicBlock*, BasicBlock*> unreachable_continues;
|
2018-01-04 22:04:03 +00:00
|
|
|
MarkUnreachableStructuredTargets(live_blocks, &unreachable_merges,
|
|
|
|
&unreachable_continues);
|
|
|
|
modified |= FixPhiNodesInLiveBlocks(func, live_blocks, unreachable_continues);
|
|
|
|
modified |= EraseDeadBlocks(func, live_blocks, unreachable_merges,
|
|
|
|
unreachable_continues);
|
|
|
|
|
2017-06-02 19:23:20 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:50:32 +00:00
|
|
|
void DeadBranchElimPass::FixBlockOrder() {
|
|
|
|
context()->BuildInvalidAnalyses(IRContext::kAnalysisCFG |
|
|
|
|
IRContext::kAnalysisDominatorAnalysis);
|
|
|
|
// Reorders blocks according to DFS of dominator tree.
|
|
|
|
ProcessFunction reorder_dominators = [this](Function* function) {
|
|
|
|
DominatorAnalysis* dominators = context()->GetDominatorAnalysis(function);
|
|
|
|
std::vector<BasicBlock*> blocks;
|
|
|
|
for (auto iter = dominators->GetDomTree().begin();
|
|
|
|
iter != dominators->GetDomTree().end(); ++iter) {
|
|
|
|
if (iter->id() != 0) {
|
|
|
|
blocks.push_back(iter->bb_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (uint32_t i = 1; i < blocks.size(); ++i) {
|
|
|
|
function->MoveBasicBlockToAfter(blocks[i]->id(), blocks[i - 1]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reorders blocks according to structured order.
|
|
|
|
ProcessFunction reorder_structured = [this](Function* function) {
|
|
|
|
std::list<BasicBlock*> order;
|
|
|
|
context()->cfg()->ComputeStructuredOrder(function, &*function->begin(),
|
|
|
|
&order);
|
|
|
|
std::vector<BasicBlock*> blocks;
|
|
|
|
for (auto block : order) {
|
|
|
|
blocks.push_back(block);
|
|
|
|
}
|
|
|
|
for (uint32_t i = 1; i < blocks.size(); ++i) {
|
|
|
|
function->MoveBasicBlockToAfter(blocks[i]->id(), blocks[i - 1]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Structured order is more intuitive so use it where possible.
|
|
|
|
if (context()->get_feature_mgr()->HasCapability(SpvCapabilityShader)) {
|
|
|
|
ProcessReachableCallTree(reorder_structured, context());
|
|
|
|
} else {
|
|
|
|
ProcessReachableCallTree(reorder_dominators, context());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:08:45 +00:00
|
|
|
Pass::Status DeadBranchElimPass::Process() {
|
2017-07-26 22:34:41 +00:00
|
|
|
// Do not process if module contains OpGroupDecorate. Additional
|
|
|
|
// support required in KillNamesAndDecorates().
|
|
|
|
// TODO(greg-lunarg): Add support for OpGroupDecorate
|
2017-10-25 17:26:25 +00:00
|
|
|
for (auto& ai : get_module()->annotations())
|
2017-11-08 17:40:02 +00:00
|
|
|
if (ai.opcode() == SpvOpGroupDecorate) return Status::SuccessWithoutChange;
|
2017-07-19 00:57:26 +00:00
|
|
|
// Process all entry point functions
|
2018-07-12 19:14:43 +00:00
|
|
|
ProcessFunction pfn = [this](Function* fp) {
|
2017-08-10 22:42:16 +00:00
|
|
|
return EliminateDeadBranches(fp);
|
|
|
|
};
|
2018-01-04 22:04:03 +00:00
|
|
|
bool modified = ProcessReachableCallTree(pfn, context());
|
2018-07-19 12:50:32 +00:00
|
|
|
if (modified) FixBlockOrder();
|
2017-06-02 19:23:20 +00:00
|
|
|
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|