SPIRV-Tools/source/opt/struct_cfg_analysis.cpp
Steven Perron 5f599e700e
Fix infinite loop in dead-branch-elimination (#1891)
* Create structed cfg analysis.

There are lots of optimization that have to traverse the CFG in a
structured order just because it wants to know which constructs a
basic block in contained in.  This adds extra complexity to these
optimizations, for causes too much refactoring of older optimizations.

To help with this problem, I have written an analysis that can give this
information.

* Identify branches breaking from loops.

Dead branch elimination does a search for a conditional branch to the
end of the current selection construct.  This search assumes that the
only way to leave the construct is through the merge node.  But that is
not true.  The code can jump to the merge node of a loop that contains
the construct.

The search needs to take this into consideration.
2018-09-17 13:00:24 -04:00

105 lines
3.0 KiB
C++

// Copyright (c) 2018 Google LLC.
//
// 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 "source/opt/struct_cfg_analysis.h"
namespace {
const uint32_t kMergeNodeIndex = 0;
}
namespace spvtools {
namespace opt {
StructuredCFGAnalysis::StructuredCFGAnalysis(IRContext* ctx) : context_(ctx) {
// If this is not a shader, there are no merge instructions, and not
// structured CFG to analyze.
if (!context_->get_feature_mgr()->HasCapability(SpvCapabilityShader)) {
return;
}
for (auto& func : *context_->module()) {
AddBlocksInFunction(&func);
}
}
void StructuredCFGAnalysis::AddBlocksInFunction(Function* func) {
std::list<BasicBlock*> order;
context_->cfg()->ComputeStructuredOrder(func, &*func->begin(), &order);
struct TraversalInfo {
ConstructInfo cinfo;
uint32_t merge_node;
};
// Set up a stack to keep track of currently active constructs.
std::vector<TraversalInfo> state;
state.emplace_back();
state[0].cinfo.containing_construct = 0;
state[0].cinfo.containing_loop = 0;
state[0].merge_node = 0;
for (BasicBlock* block : order) {
if (context_->cfg()->IsPseudoEntryBlock(block) ||
context_->cfg()->IsPseudoExitBlock(block)) {
continue;
}
if (block->id() == state.back().merge_node) {
state.pop_back();
}
bb_to_construct_.emplace(std::make_pair(block->id(), state.back().cinfo));
if (Instruction* merge_inst = block->GetMergeInst()) {
TraversalInfo new_state;
new_state.merge_node =
merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
new_state.cinfo.containing_construct = block->id();
if (merge_inst->opcode() == SpvOpLoopMerge) {
new_state.cinfo.containing_loop = block->id();
} else {
new_state.cinfo.containing_loop = state.back().cinfo.containing_loop;
}
state.emplace_back(new_state);
}
}
}
uint32_t StructuredCFGAnalysis::MergeBlock(uint32_t bb_id) {
uint32_t header_id = ContainingConstruct(bb_id);
if (header_id == 0) {
return 0;
}
BasicBlock* header = context_->cfg()->block(header_id);
Instruction* merge_inst = header->GetMergeInst();
return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
}
uint32_t StructuredCFGAnalysis::LoopMergeBlock(uint32_t bb_id) {
uint32_t header_id = ContainingLoop(bb_id);
if (header_id == 0) {
return 0;
}
BasicBlock* header = context_->cfg()->block(header_id);
Instruction* merge_inst = header->GetMergeInst();
return merge_inst->GetSingleWordInOperand(kMergeNodeIndex);
}
} // namespace opt
} // namespace spvtools