2016-11-10 17:11:50 +00:00
|
|
|
// Copyright (c) 2016 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/basic_block.h"
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2018-01-10 19:23:47 +00:00
|
|
|
#include <ostream>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/function.h"
|
|
|
|
#include "source/opt/ir_context.h"
|
|
|
|
#include "source/opt/module.h"
|
|
|
|
#include "source/opt/reflect.h"
|
2018-08-14 16:44:54 +00:00
|
|
|
#include "source/util/make_unique.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2017-10-30 21:42:26 +00:00
|
|
|
namespace {
|
2022-11-17 18:02:50 +00:00
|
|
|
constexpr uint32_t kLoopMergeContinueBlockIdInIdx = 1;
|
|
|
|
constexpr uint32_t kLoopMergeMergeBlockIdInIdx = 0;
|
|
|
|
constexpr uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
|
2017-10-30 21:42:26 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
BasicBlock* BasicBlock::Clone(IRContext* context) const {
|
2017-11-27 15:16:41 +00:00
|
|
|
BasicBlock* clone = new BasicBlock(
|
2018-01-03 00:54:55 +00:00
|
|
|
std::unique_ptr<Instruction>(GetLabelInst()->Clone(context)));
|
2018-09-26 21:36:27 +00:00
|
|
|
for (const auto& inst : insts_) {
|
2017-11-14 19:11:50 +00:00
|
|
|
// Use the incoming context
|
|
|
|
clone->AddInstruction(std::unique_ptr<Instruction>(inst.Clone(context)));
|
2018-09-26 21:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (context->AreAnalysesValid(
|
|
|
|
IRContext::Analysis::kAnalysisInstrToBlockMapping)) {
|
|
|
|
for (auto& inst : *clone) {
|
|
|
|
context->set_instr_block(&inst, clone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
return clone;
|
2017-07-13 00:16:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 15:37:17 +00:00
|
|
|
const Instruction* BasicBlock::GetMergeInst() const {
|
|
|
|
const Instruction* result = nullptr;
|
|
|
|
// If it exists, the merge instruction immediately precedes the
|
|
|
|
// terminator.
|
|
|
|
auto iter = ctail();
|
|
|
|
if (iter != cbegin()) {
|
|
|
|
--iter;
|
|
|
|
const auto opcode = iter->opcode();
|
2022-11-04 21:27:10 +00:00
|
|
|
if (opcode == spv::Op::OpLoopMerge || opcode == spv::Op::OpSelectionMerge) {
|
2017-08-31 15:37:17 +00:00
|
|
|
result = &*iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction* BasicBlock::GetMergeInst() {
|
|
|
|
Instruction* result = nullptr;
|
|
|
|
// If it exists, the merge instruction immediately precedes the
|
|
|
|
// terminator.
|
|
|
|
auto iter = tail();
|
|
|
|
if (iter != begin()) {
|
|
|
|
--iter;
|
|
|
|
const auto opcode = iter->opcode();
|
2022-11-04 21:27:10 +00:00
|
|
|
if (opcode == spv::Op::OpLoopMerge || opcode == spv::Op::OpSelectionMerge) {
|
2017-08-31 15:37:17 +00:00
|
|
|
result = &*iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Instruction* BasicBlock::GetLoopMergeInst() const {
|
|
|
|
if (auto* merge = GetMergeInst()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (merge->opcode() == spv::Op::OpLoopMerge) {
|
2017-08-31 15:37:17 +00:00
|
|
|
return merge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction* BasicBlock::GetLoopMergeInst() {
|
|
|
|
if (auto* merge = GetMergeInst()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (merge->opcode() == spv::Op::OpLoopMerge) {
|
2017-08-31 15:37:17 +00:00
|
|
|
return merge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
void BasicBlock::KillAllInsts(bool killLabel) {
|
2018-07-12 19:14:43 +00:00
|
|
|
ForEachInst([killLabel](Instruction* ip) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (killLabel || ip->opcode() != spv::Op::OpLabel) {
|
2018-02-12 21:42:15 +00:00
|
|
|
ip->context()->KillInst(ip);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
void BasicBlock::ForEachSuccessorLabel(
|
2018-01-04 22:04:03 +00:00
|
|
|
const std::function<void(const uint32_t)>& f) const {
|
2019-04-29 21:09:20 +00:00
|
|
|
WhileEachSuccessorLabel([f](const uint32_t l) {
|
|
|
|
f(l);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BasicBlock::WhileEachSuccessorLabel(
|
|
|
|
const std::function<bool(const uint32_t)>& f) const {
|
2017-10-13 18:25:21 +00:00
|
|
|
const auto br = &insts_.back();
|
2016-11-10 17:11:50 +00:00
|
|
|
switch (br->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBranch:
|
2019-04-29 21:09:20 +00:00
|
|
|
return f(br->GetOperand(0).words[0]);
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBranchConditional:
|
|
|
|
case spv::Op::OpSwitch: {
|
2016-11-10 17:11:50 +00:00
|
|
|
bool is_first = true;
|
2019-04-29 21:09:20 +00:00
|
|
|
return br->WhileEachInId([&is_first, &f](const uint32_t* idp) {
|
|
|
|
if (!is_first) return f(*idp);
|
2016-11-10 17:11:50 +00:00
|
|
|
is_first = false;
|
2019-04-29 21:09:20 +00:00
|
|
|
return true;
|
2016-11-10 17:11:50 +00:00
|
|
|
});
|
2019-04-29 21:09:20 +00:00
|
|
|
}
|
2016-11-10 17:11:50 +00:00
|
|
|
default:
|
2019-04-29 21:09:20 +00:00
|
|
|
return true;
|
2016-11-10 17:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:07:10 +00:00
|
|
|
void BasicBlock::ForEachSuccessorLabel(
|
|
|
|
const std::function<void(uint32_t*)>& f) {
|
|
|
|
auto br = &insts_.back();
|
|
|
|
switch (br->opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBranch: {
|
2018-01-26 12:07:10 +00:00
|
|
|
uint32_t tmp_id = br->GetOperand(0).words[0];
|
|
|
|
f(&tmp_id);
|
|
|
|
if (tmp_id != br->GetOperand(0).words[0]) br->SetOperand(0, {tmp_id});
|
|
|
|
} break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBranchConditional:
|
|
|
|
case spv::Op::OpSwitch: {
|
2018-01-26 12:07:10 +00:00
|
|
|
bool is_first = true;
|
|
|
|
br->ForEachInId([&is_first, &f](uint32_t* idp) {
|
|
|
|
if (!is_first) f(idp);
|
|
|
|
is_first = false;
|
|
|
|
});
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool BasicBlock::IsSuccessor(const BasicBlock* block) const {
|
2018-01-04 22:04:03 +00:00
|
|
|
uint32_t succId = block->id();
|
|
|
|
bool isSuccessor = false;
|
|
|
|
ForEachSuccessorLabel([&isSuccessor, succId](const uint32_t label) {
|
|
|
|
if (label == succId) isSuccessor = true;
|
|
|
|
});
|
|
|
|
return isSuccessor;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:05:38 +00:00
|
|
|
void BasicBlock::ForMergeAndContinueLabel(
|
|
|
|
const std::function<void(const uint32_t)>& f) {
|
|
|
|
auto ii = insts_.end();
|
|
|
|
--ii;
|
|
|
|
if (ii == insts_.begin()) return;
|
|
|
|
--ii;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (ii->opcode() == spv::Op::OpSelectionMerge ||
|
|
|
|
ii->opcode() == spv::Op::OpLoopMerge) {
|
2017-10-30 21:42:26 +00:00
|
|
|
ii->ForEachInId([&f](const uint32_t* idp) { f(*idp); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BasicBlock::MergeBlockIdIfAny() const {
|
|
|
|
auto merge_ii = cend();
|
|
|
|
--merge_ii;
|
|
|
|
uint32_t mbid = 0;
|
|
|
|
if (merge_ii != cbegin()) {
|
|
|
|
--merge_ii;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (merge_ii->opcode() == spv::Op::OpLoopMerge) {
|
2017-10-30 21:42:26 +00:00
|
|
|
mbid = merge_ii->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx);
|
2022-11-04 21:27:10 +00:00
|
|
|
} else if (merge_ii->opcode() == spv::Op::OpSelectionMerge) {
|
2017-10-30 21:42:26 +00:00
|
|
|
mbid = merge_ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mbid;
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:29:24 +00:00
|
|
|
uint32_t BasicBlock::MergeBlockId() const {
|
|
|
|
uint32_t mbid = MergeBlockIdIfAny();
|
|
|
|
assert(mbid && "Expected block to have a corresponding merge block");
|
|
|
|
return mbid;
|
|
|
|
}
|
|
|
|
|
2017-10-30 21:42:26 +00:00
|
|
|
uint32_t BasicBlock::ContinueBlockIdIfAny() const {
|
|
|
|
auto merge_ii = cend();
|
|
|
|
--merge_ii;
|
|
|
|
uint32_t cbid = 0;
|
|
|
|
if (merge_ii != cbegin()) {
|
|
|
|
--merge_ii;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (merge_ii->opcode() == spv::Op::OpLoopMerge) {
|
2017-10-30 21:42:26 +00:00
|
|
|
cbid = merge_ii->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cbid;
|
2017-08-23 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:29:24 +00:00
|
|
|
uint32_t BasicBlock::ContinueBlockId() const {
|
|
|
|
uint32_t cbid = ContinueBlockIdIfAny();
|
|
|
|
assert(cbid && "Expected block to have a corresponding continue target");
|
|
|
|
return cbid;
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:23:47 +00:00
|
|
|
std::ostream& operator<<(std::ostream& str, const BasicBlock& block) {
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
str << block.PrettyPrint();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:57:12 +00:00
|
|
|
void BasicBlock::Dump() const {
|
|
|
|
std::cerr << "Basic block #" << id() << "\n" << *this << "\n ";
|
|
|
|
}
|
|
|
|
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
std::string BasicBlock::PrettyPrint(uint32_t options) const {
|
|
|
|
std::ostringstream str;
|
2018-07-12 19:14:43 +00:00
|
|
|
ForEachInst([&str, options](const Instruction* inst) {
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
str << inst->PrettyPrint(options);
|
2020-12-07 15:26:05 +00:00
|
|
|
if (!spvOpcodeIsBlockTerminator(inst->opcode())) {
|
2018-01-10 19:23:47 +00:00
|
|
|
str << std::endl;
|
|
|
|
}
|
|
|
|
});
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
return str.str();
|
2018-01-10 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
BasicBlock* BasicBlock::SplitBasicBlock(IRContext* context, uint32_t label_id,
|
|
|
|
iterator iter) {
|
|
|
|
assert(!insts_.empty());
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
std::unique_ptr<BasicBlock> new_block_temp = MakeUnique<BasicBlock>(
|
|
|
|
MakeUnique<Instruction>(context, spv::Op::OpLabel, 0, label_id,
|
|
|
|
std::initializer_list<Operand>{}));
|
2018-09-18 12:52:47 +00:00
|
|
|
BasicBlock* new_block = new_block_temp.get();
|
|
|
|
function_->InsertBasicBlockAfter(std::move(new_block_temp), this);
|
2018-03-06 16:20:28 +00:00
|
|
|
|
|
|
|
new_block->insts_.Splice(new_block->end(), &insts_, iter, end());
|
2020-11-13 17:33:15 +00:00
|
|
|
assert(new_block->GetParent() == GetParent() &&
|
|
|
|
"The parent should already be set appropriately.");
|
2018-03-06 16:20:28 +00:00
|
|
|
|
2018-08-02 15:02:50 +00:00
|
|
|
context->AnalyzeDefUse(new_block->GetLabelInst());
|
|
|
|
|
|
|
|
// Update the phi nodes in the successor blocks to reference the new block id.
|
|
|
|
const_cast<const BasicBlock*>(new_block)->ForEachSuccessorLabel(
|
|
|
|
[new_block, this, context](const uint32_t label) {
|
|
|
|
BasicBlock* target_bb = context->get_instr_block(label);
|
|
|
|
target_bb->ForEachPhiInst(
|
|
|
|
[this, new_block, context](Instruction* phi_inst) {
|
|
|
|
bool changed = false;
|
|
|
|
for (uint32_t i = 1; i < phi_inst->NumInOperands(); i += 2) {
|
|
|
|
if (phi_inst->GetSingleWordInOperand(i) == this->id()) {
|
|
|
|
changed = true;
|
|
|
|
phi_inst->SetInOperand(i, {new_block->id()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
context->UpdateDefUse(phi_inst);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
if (context->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
|
2018-08-02 15:02:50 +00:00
|
|
|
context->set_instr_block(new_block->GetLabelInst(), new_block);
|
2018-07-12 19:14:43 +00:00
|
|
|
new_block->ForEachInst([new_block, context](Instruction* inst) {
|
2018-03-06 16:20:28 +00:00
|
|
|
context->set_instr_block(inst, new_block);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_block;
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2016-11-10 17:11:50 +00:00
|
|
|
} // namespace spvtools
|