2017-10-30 21:42:26 +00:00
|
|
|
// Copyright (c) 2017 Google Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/cfg.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "source/cfa.h"
|
|
|
|
#include "source/opt/ir_builder.h"
|
|
|
|
#include "source/opt/ir_context.h"
|
|
|
|
#include "source/opt/module.h"
|
2017-10-30 21:42:26 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2017-10-30 21:42:26 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-07-12 18:40:40 +00:00
|
|
|
using cbb_ptr = const opt::BasicBlock*;
|
|
|
|
|
2017-10-30 21:42:26 +00:00
|
|
|
// Universal Limit of ResultID + 1
|
2022-11-17 18:02:50 +00:00
|
|
|
constexpr int kMaxResultId = 0x400000;
|
2017-10-30 21:42:26 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
CFG::CFG(Module* module)
|
2017-10-30 21:42:26 +00:00
|
|
|
: module_(module),
|
2018-07-12 19:14:43 +00:00
|
|
|
pseudo_entry_block_(std::unique_ptr<Instruction>(
|
2022-11-04 21:27:10 +00:00
|
|
|
new Instruction(module->context(), spv::Op::OpLabel, 0, 0, {}))),
|
2018-07-12 19:14:43 +00:00
|
|
|
pseudo_exit_block_(std::unique_ptr<Instruction>(new Instruction(
|
2022-11-04 21:27:10 +00:00
|
|
|
module->context(), spv::Op::OpLabel, 0, kMaxResultId, {}))) {
|
2017-10-30 21:42:26 +00:00
|
|
|
for (auto& fn : *module) {
|
|
|
|
for (auto& blk : fn) {
|
2018-01-26 12:07:10 +00:00
|
|
|
RegisterBlock(&blk);
|
2017-10-30 21:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void CFG::AddEdges(BasicBlock* blk) {
|
2018-02-02 22:17:42 +00:00
|
|
|
uint32_t blk_id = blk->id();
|
|
|
|
// Force the creation of an entry, not all basic block have predecessors
|
|
|
|
// (such as the entry blocks and some unreachables).
|
|
|
|
label2preds_[blk_id];
|
|
|
|
const auto* const_blk = blk;
|
|
|
|
const_blk->ForEachSuccessorLabel(
|
|
|
|
[blk_id, this](const uint32_t succ_id) { AddEdge(blk_id, succ_id); });
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:07:10 +00:00
|
|
|
void CFG::RemoveNonExistingEdges(uint32_t blk_id) {
|
|
|
|
std::vector<uint32_t> updated_pred_list;
|
|
|
|
for (uint32_t id : preds(blk_id)) {
|
2018-07-12 19:14:43 +00:00
|
|
|
const BasicBlock* pred_blk = block(id);
|
2018-01-26 12:07:10 +00:00
|
|
|
bool has_branch = false;
|
|
|
|
pred_blk->ForEachSuccessorLabel([&has_branch, blk_id](uint32_t succ) {
|
2018-01-29 10:39:55 +00:00
|
|
|
if (succ == blk_id) {
|
|
|
|
has_branch = true;
|
|
|
|
}
|
2018-01-26 12:07:10 +00:00
|
|
|
});
|
|
|
|
if (has_branch) updated_pred_list.push_back(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
label2preds_.at(blk_id) = std::move(updated_pred_list);
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void CFG::ComputeStructuredOrder(Function* func, BasicBlock* root,
|
|
|
|
std::list<BasicBlock*>* order) {
|
2022-06-29 13:53:26 +00:00
|
|
|
ComputeStructuredOrder(func, root, nullptr, order);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFG::ComputeStructuredOrder(Function* func, BasicBlock* root,
|
|
|
|
BasicBlock* end,
|
|
|
|
std::list<BasicBlock*>* order) {
|
2017-12-19 19:18:13 +00:00
|
|
|
assert(module_->context()->get_feature_mgr()->HasCapability(
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::Capability::Shader) &&
|
2017-10-30 21:42:26 +00:00
|
|
|
"This only works on structured control flow");
|
|
|
|
|
|
|
|
// Compute structured successors and do DFS.
|
|
|
|
ComputeStructuredSuccessors(func);
|
|
|
|
auto ignore_block = [](cbb_ptr) {};
|
2022-06-29 13:53:26 +00:00
|
|
|
auto terminal = [end](cbb_ptr bb) { return bb == end; };
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
auto get_structured_successors = [this](const BasicBlock* b) {
|
2017-10-30 21:42:26 +00:00
|
|
|
return &(block2structured_succs_[b]);
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(greg-lunarg): Get rid of const_cast by making moving const
|
|
|
|
// out of the cfa.h prototypes and into the invoking code.
|
|
|
|
auto post_order = [&](cbb_ptr b) {
|
2018-07-12 19:14:43 +00:00
|
|
|
order->push_front(const_cast<BasicBlock*>(b));
|
2017-10-30 21:42:26 +00:00
|
|
|
};
|
2018-07-12 19:14:43 +00:00
|
|
|
CFA<BasicBlock>::DepthFirstTraversal(root, get_structured_successors,
|
2022-09-02 16:27:10 +00:00
|
|
|
ignore_block, post_order, terminal);
|
2017-10-30 21:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 14:31:09 +00:00
|
|
|
void CFG::ForEachBlockInPostOrder(BasicBlock* bb,
|
|
|
|
const std::function<void(BasicBlock*)>& f) {
|
|
|
|
std::vector<BasicBlock*> po;
|
|
|
|
std::unordered_set<BasicBlock*> seen;
|
|
|
|
ComputePostOrderTraversal(bb, &po, &seen);
|
|
|
|
|
|
|
|
for (BasicBlock* current_bb : po) {
|
|
|
|
if (!IsPseudoExitBlock(current_bb) && !IsPseudoEntryBlock(current_bb)) {
|
|
|
|
f(current_bb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:55:05 +00:00
|
|
|
void CFG::ForEachBlockInReversePostOrder(
|
|
|
|
BasicBlock* bb, const std::function<void(BasicBlock*)>& f) {
|
2019-09-10 13:38:23 +00:00
|
|
|
WhileEachBlockInReversePostOrder(bb, [f](BasicBlock* b) {
|
|
|
|
f(b);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFG::WhileEachBlockInReversePostOrder(
|
|
|
|
BasicBlock* bb, const std::function<bool(BasicBlock*)>& f) {
|
2018-02-02 16:55:05 +00:00
|
|
|
std::vector<BasicBlock*> po;
|
|
|
|
std::unordered_set<BasicBlock*> seen;
|
|
|
|
ComputePostOrderTraversal(bb, &po, &seen);
|
|
|
|
|
|
|
|
for (auto current_bb = po.rbegin(); current_bb != po.rend(); ++current_bb) {
|
|
|
|
if (!IsPseudoExitBlock(*current_bb) && !IsPseudoEntryBlock(*current_bb)) {
|
2019-09-10 13:38:23 +00:00
|
|
|
if (!f(*current_bb)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-02 16:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 13:38:23 +00:00
|
|
|
return true;
|
2018-02-02 16:55:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void CFG::ComputeStructuredSuccessors(Function* func) {
|
2017-10-30 21:42:26 +00:00
|
|
|
block2structured_succs_.clear();
|
|
|
|
for (auto& blk : *func) {
|
|
|
|
// If no predecessors in function, make successor to pseudo entry.
|
|
|
|
if (label2preds_[blk.id()].size() == 0)
|
|
|
|
block2structured_succs_[&pseudo_entry_block_].push_back(&blk);
|
|
|
|
|
|
|
|
// If header, make merge block first successor and continue block second
|
|
|
|
// successor if there is one.
|
|
|
|
uint32_t mbid = blk.MergeBlockIdIfAny();
|
|
|
|
if (mbid != 0) {
|
2018-03-06 16:20:28 +00:00
|
|
|
block2structured_succs_[&blk].push_back(block(mbid));
|
2017-10-30 21:42:26 +00:00
|
|
|
uint32_t cbid = blk.ContinueBlockIdIfAny();
|
2018-03-06 16:20:28 +00:00
|
|
|
if (cbid != 0) {
|
|
|
|
block2structured_succs_[&blk].push_back(block(cbid));
|
|
|
|
}
|
2017-10-30 21:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add true successors.
|
2018-02-02 22:17:42 +00:00
|
|
|
const auto& const_blk = blk;
|
|
|
|
const_blk.ForEachSuccessorLabel([&blk, this](const uint32_t sbid) {
|
2018-03-06 16:20:28 +00:00
|
|
|
block2structured_succs_[&blk].push_back(block(sbid));
|
2017-10-30 21:42:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:58:12 +00:00
|
|
|
void CFG::ComputePostOrderTraversal(BasicBlock* bb,
|
|
|
|
std::vector<BasicBlock*>* order,
|
|
|
|
std::unordered_set<BasicBlock*>* seen) {
|
2019-04-29 21:09:20 +00:00
|
|
|
std::vector<BasicBlock*> stack;
|
|
|
|
stack.push_back(bb);
|
|
|
|
while (!stack.empty()) {
|
|
|
|
bb = stack.back();
|
|
|
|
seen->insert(bb);
|
|
|
|
static_cast<const BasicBlock*>(bb)->WhileEachSuccessorLabel(
|
|
|
|
[&seen, &stack, this](const uint32_t sbid) {
|
|
|
|
BasicBlock* succ_bb = id2block_[sbid];
|
|
|
|
if (!seen->count(succ_bb)) {
|
|
|
|
stack.push_back(succ_bb);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (stack.back() == bb) {
|
|
|
|
order->push_back(bb);
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 16:55:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* CFG::SplitLoopHeader(BasicBlock* bb) {
|
2018-03-06 16:20:28 +00:00
|
|
|
assert(bb->GetLoopMergeInst() && "Expecting bb to be the header of a loop.");
|
|
|
|
|
|
|
|
Function* fn = bb->GetParent();
|
2018-07-12 14:16:15 +00:00
|
|
|
IRContext* context = module_->context();
|
2018-03-06 16:20:28 +00:00
|
|
|
|
2018-12-06 14:07:00 +00:00
|
|
|
// Get the new header id up front. If we are out of ids, then we cannot split
|
|
|
|
// the loop.
|
|
|
|
uint32_t new_header_id = context->TakeNextId();
|
|
|
|
if (new_header_id == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
// Find the insertion point for the new bb.
|
|
|
|
Function::iterator header_it = std::find_if(
|
|
|
|
fn->begin(), fn->end(),
|
|
|
|
[bb](BasicBlock& block_in_func) { return &block_in_func == bb; });
|
|
|
|
assert(header_it != fn->end());
|
|
|
|
|
2018-03-21 16:52:13 +00:00
|
|
|
const std::vector<uint32_t>& pred = preds(bb->id());
|
2018-03-06 16:20:28 +00:00
|
|
|
// Find the back edge
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* latch_block = nullptr;
|
2018-03-21 16:52:13 +00:00
|
|
|
Function::iterator latch_block_iter = header_it;
|
2022-06-24 16:33:45 +00:00
|
|
|
for (; latch_block_iter != fn->end(); ++latch_block_iter) {
|
2018-03-21 16:52:13 +00:00
|
|
|
// If blocks are in the proper order, then the only branch that appears
|
|
|
|
// after the header is the latch.
|
|
|
|
if (std::find(pred.begin(), pred.end(), latch_block_iter->id()) !=
|
|
|
|
pred.end()) {
|
|
|
|
break;
|
2018-03-06 16:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 16:52:13 +00:00
|
|
|
assert(latch_block_iter != fn->end() && "Could not find the latch.");
|
|
|
|
latch_block = &*latch_block_iter;
|
2018-03-06 16:20:28 +00:00
|
|
|
|
|
|
|
RemoveSuccessorEdges(bb);
|
|
|
|
|
|
|
|
// Create the new header bb basic bb.
|
|
|
|
// Leave the phi instructions behind.
|
|
|
|
auto iter = bb->begin();
|
2022-11-04 21:27:10 +00:00
|
|
|
while (iter->opcode() == spv::Op::OpPhi) {
|
2018-03-06 16:20:28 +00:00
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
2018-12-06 14:07:00 +00:00
|
|
|
BasicBlock* new_header = bb->SplitBasicBlock(context, new_header_id, iter);
|
2018-03-06 16:20:28 +00:00
|
|
|
context->AnalyzeDefUse(new_header->GetLabelInst());
|
|
|
|
|
|
|
|
// Update cfg
|
|
|
|
RegisterBlock(new_header);
|
|
|
|
|
|
|
|
// Update bb mappings.
|
|
|
|
context->set_instr_block(new_header->GetLabelInst(), new_header);
|
2018-07-12 19:14:43 +00:00
|
|
|
new_header->ForEachInst([new_header, context](Instruction* inst) {
|
2018-03-06 16:20:28 +00:00
|
|
|
context->set_instr_block(inst, new_header);
|
|
|
|
});
|
|
|
|
|
2022-06-24 16:33:45 +00:00
|
|
|
// If |bb| was the latch block, the branch back to the header is not in
|
|
|
|
// |new_header|.
|
|
|
|
if (latch_block == bb) {
|
|
|
|
if (new_header->ContinueBlockId() == bb->id()) {
|
|
|
|
new_header->GetLoopMergeInst()->SetInOperand(1, {new_header_id});
|
|
|
|
}
|
|
|
|
latch_block = new_header;
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
// Adjust the OpPhi instructions as needed.
|
|
|
|
bb->ForEachPhiInst([latch_block, bb, new_header, context](Instruction* phi) {
|
|
|
|
std::vector<uint32_t> preheader_phi_ops;
|
|
|
|
std::vector<Operand> header_phi_ops;
|
|
|
|
|
2018-03-21 16:52:13 +00:00
|
|
|
// Identify where the original inputs to original OpPhi belong: header or
|
2018-03-06 16:20:28 +00:00
|
|
|
// preheader.
|
|
|
|
for (uint32_t i = 0; i < phi->NumInOperands(); i += 2) {
|
|
|
|
uint32_t def_id = phi->GetSingleWordInOperand(i);
|
|
|
|
uint32_t branch_id = phi->GetSingleWordInOperand(i + 1);
|
|
|
|
if (branch_id == latch_block->id()) {
|
|
|
|
header_phi_ops.push_back({SPV_OPERAND_TYPE_ID, {def_id}});
|
|
|
|
header_phi_ops.push_back({SPV_OPERAND_TYPE_ID, {branch_id}});
|
|
|
|
} else {
|
|
|
|
preheader_phi_ops.push_back(def_id);
|
|
|
|
preheader_phi_ops.push_back(branch_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a phi instruction if and only if the preheader_phi_ops has more
|
|
|
|
// than one pair.
|
|
|
|
if (preheader_phi_ops.size() > 2) {
|
2018-07-12 19:14:43 +00:00
|
|
|
InstructionBuilder builder(
|
2018-03-06 16:20:28 +00:00
|
|
|
context, &*bb->begin(),
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
|
2018-03-06 16:20:28 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* new_phi = builder.AddPhi(phi->type_id(), preheader_phi_ops);
|
2018-03-06 16:20:28 +00:00
|
|
|
|
|
|
|
// Add the OpPhi to the header bb.
|
|
|
|
header_phi_ops.push_back({SPV_OPERAND_TYPE_ID, {new_phi->result_id()}});
|
|
|
|
header_phi_ops.push_back({SPV_OPERAND_TYPE_ID, {bb->id()}});
|
|
|
|
} else {
|
|
|
|
// An OpPhi with a single entry is just a copy. In this case use the same
|
|
|
|
// instruction in the new header.
|
|
|
|
header_phi_ops.push_back({SPV_OPERAND_TYPE_ID, {preheader_phi_ops[0]}});
|
|
|
|
header_phi_ops.push_back({SPV_OPERAND_TYPE_ID, {bb->id()}});
|
|
|
|
}
|
|
|
|
|
|
|
|
phi->RemoveFromList();
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<Instruction> phi_owner(phi);
|
2018-03-06 16:20:28 +00:00
|
|
|
phi->SetInOperands(std::move(header_phi_ops));
|
|
|
|
new_header->begin()->InsertBefore(std::move(phi_owner));
|
|
|
|
context->set_instr_block(phi, new_header);
|
|
|
|
context->AnalyzeUses(phi);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add a branch to the new header.
|
2018-07-12 19:14:43 +00:00
|
|
|
InstructionBuilder branch_builder(
|
2018-03-06 16:20:28 +00:00
|
|
|
context, bb,
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
|
|
|
|
bb->AddInstruction(
|
2022-11-04 21:27:10 +00:00
|
|
|
MakeUnique<Instruction>(context, spv::Op::OpBranch, 0, 0,
|
2018-07-12 19:14:43 +00:00
|
|
|
std::initializer_list<Operand>{
|
|
|
|
{SPV_OPERAND_TYPE_ID, {new_header->id()}}}));
|
2018-03-06 16:20:28 +00:00
|
|
|
context->AnalyzeUses(bb->terminator());
|
|
|
|
context->set_instr_block(bb->terminator(), bb);
|
|
|
|
label2preds_[new_header->id()].push_back(bb->id());
|
|
|
|
|
|
|
|
// Update the latch to branch to the new header.
|
|
|
|
latch_block->ForEachSuccessorLabel([bb, new_header_id](uint32_t* id) {
|
|
|
|
if (*id == bb->id()) {
|
|
|
|
*id = new_header_id;
|
|
|
|
}
|
|
|
|
});
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* latch_branch = latch_block->terminator();
|
2018-03-06 16:20:28 +00:00
|
|
|
context->AnalyzeUses(latch_branch);
|
|
|
|
label2preds_[new_header->id()].push_back(latch_block->id());
|
|
|
|
|
|
|
|
auto& block_preds = label2preds_[bb->id()];
|
|
|
|
auto latch_pos =
|
|
|
|
std::find(block_preds.begin(), block_preds.end(), latch_block->id());
|
|
|
|
assert(latch_pos != block_preds.end() && "The cfg was invalid.");
|
|
|
|
block_preds.erase(latch_pos);
|
|
|
|
|
|
|
|
// Update the loop descriptors
|
2018-07-12 19:14:43 +00:00
|
|
|
if (context->AreAnalysesValid(IRContext::kAnalysisLoopAnalysis)) {
|
2018-03-06 16:20:28 +00:00
|
|
|
LoopDescriptor* loop_desc = context->GetLoopDescriptor(bb->GetParent());
|
|
|
|
Loop* loop = (*loop_desc)[bb->id()];
|
|
|
|
|
|
|
|
loop->AddBasicBlock(new_header_id);
|
|
|
|
loop->SetHeaderBlock(new_header);
|
|
|
|
loop_desc->SetBasicBlockToLoop(new_header_id, loop);
|
|
|
|
|
|
|
|
loop->RemoveBasicBlock(bb->id());
|
|
|
|
loop->SetPreHeaderBlock(bb);
|
|
|
|
|
|
|
|
Loop* parent_loop = loop->GetParent();
|
|
|
|
if (parent_loop != nullptr) {
|
|
|
|
parent_loop->AddBasicBlock(bb->id());
|
|
|
|
loop_desc->SetBasicBlockToLoop(bb->id(), parent_loop);
|
|
|
|
} else {
|
|
|
|
loop_desc->SetBasicBlockToLoop(bb->id(), nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new_header;
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2017-10-30 21:42:26 +00:00
|
|
|
} // namespace spvtools
|