2017-12-21 14:47:25 +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/loop_descriptor.h"
|
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
#include <algorithm>
|
2017-12-21 14:47:25 +00:00
|
|
|
#include <iostream>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <stack>
|
2017-12-21 14:47:25 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/cfg.h"
|
|
|
|
#include "source/opt/constants.h"
|
|
|
|
#include "source/opt/dominator_tree.h"
|
|
|
|
#include "source/opt/ir_builder.h"
|
|
|
|
#include "source/opt/ir_context.h"
|
|
|
|
#include "source/opt/iterator.h"
|
|
|
|
#include "source/opt/tree_iterator.h"
|
2018-08-14 16:44:54 +00:00
|
|
|
#include "source/util/make_unique.h"
|
2017-12-21 14:47:25 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2017-12-21 14:47:25 +00:00
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
// Takes in a phi instruction |induction| and the loop |header| and returns the
|
|
|
|
// step operation of the loop.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* Loop::GetInductionStepOperation(
|
|
|
|
const Instruction* induction) const {
|
2018-02-14 17:03:12 +00:00
|
|
|
// Induction must be a phi instruction.
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(induction->opcode() == spv::Op::OpPhi);
|
2018-02-14 17:03:12 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* step = nullptr;
|
2018-02-14 17:03:12 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Traverse the incoming operands of the phi instruction.
|
|
|
|
for (uint32_t operand_id = 1; operand_id < induction->NumInOperands();
|
|
|
|
operand_id += 2) {
|
|
|
|
// Incoming edge.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* incoming_block =
|
2018-02-14 17:03:12 +00:00
|
|
|
context_->cfg()->block(induction->GetSingleWordInOperand(operand_id));
|
|
|
|
|
|
|
|
// Check if the block is dominated by header, and thus coming from within
|
|
|
|
// the loop.
|
2018-02-27 11:50:08 +00:00
|
|
|
if (IsInsideLoop(incoming_block)) {
|
2018-02-14 17:03:12 +00:00
|
|
|
step = def_use_manager->GetDef(
|
|
|
|
induction->GetSingleWordInOperand(operand_id - 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!step || !IsSupportedStepOp(step->opcode())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:50:08 +00:00
|
|
|
// The induction variable which binds the loop must only be modified once.
|
|
|
|
uint32_t lhs = step->GetSingleWordInOperand(0);
|
|
|
|
uint32_t rhs = step->GetSingleWordInOperand(1);
|
|
|
|
|
|
|
|
// One of the left hand side or right hand side of the step instruction must
|
|
|
|
// be the induction phi and the other must be an OpConstant.
|
|
|
|
if (lhs != induction->result_id() && rhs != induction->result_id()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
if (def_use_manager->GetDef(lhs)->opcode() != spv::Op::OpConstant &&
|
|
|
|
def_use_manager->GetDef(rhs)->opcode() != spv::Op::OpConstant) {
|
2018-02-27 11:50:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
return step;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the |step| operation is an induction variable step operation
|
|
|
|
// which is currently handled.
|
2022-11-04 21:27:10 +00:00
|
|
|
bool Loop::IsSupportedStepOp(spv::Op step) const {
|
2018-02-14 17:03:12 +00:00
|
|
|
switch (step) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpISub:
|
|
|
|
case spv::Op::OpIAdd:
|
2018-02-14 17:03:12 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
bool Loop::IsSupportedCondition(spv::Op condition) const {
|
2018-02-14 17:03:12 +00:00
|
|
|
switch (condition) {
|
|
|
|
// <
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpULessThan:
|
|
|
|
case spv::Op::OpSLessThan:
|
2018-02-14 17:03:12 +00:00
|
|
|
// >
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpUGreaterThan:
|
|
|
|
case spv::Op::OpSGreaterThan:
|
2018-02-27 11:50:08 +00:00
|
|
|
|
|
|
|
// >=
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSGreaterThanEqual:
|
|
|
|
case spv::Op::OpUGreaterThanEqual:
|
2018-02-27 11:50:08 +00:00
|
|
|
// <=
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSLessThanEqual:
|
|
|
|
case spv::Op::OpULessThanEqual:
|
2018-02-27 11:50:08 +00:00
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
int64_t Loop::GetResidualConditionValue(spv::Op condition,
|
|
|
|
int64_t initial_value,
|
2018-02-27 11:50:08 +00:00
|
|
|
int64_t step_value,
|
|
|
|
size_t number_of_iterations,
|
|
|
|
size_t factor) {
|
|
|
|
int64_t remainder =
|
|
|
|
initial_value + (number_of_iterations % factor) * step_value;
|
|
|
|
|
|
|
|
// We subtract or add one as the above formula calculates the remainder if the
|
|
|
|
// loop where just less than or greater than. Adding or subtracting one should
|
|
|
|
// give a functionally equivalent value.
|
|
|
|
switch (condition) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSGreaterThanEqual:
|
|
|
|
case spv::Op::OpUGreaterThanEqual: {
|
2018-02-27 11:50:08 +00:00
|
|
|
remainder -= 1;
|
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSLessThanEqual:
|
|
|
|
case spv::Op::OpULessThanEqual: {
|
2018-02-27 11:50:08 +00:00
|
|
|
remainder += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return remainder;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* Loop::GetConditionInst() const {
|
|
|
|
BasicBlock* condition_block = FindConditionBlock();
|
2018-03-29 12:39:54 +00:00
|
|
|
if (!condition_block) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* branch_conditional = &*condition_block->tail();
|
2018-03-29 12:39:54 +00:00
|
|
|
if (!branch_conditional ||
|
2022-11-04 21:27:10 +00:00
|
|
|
branch_conditional->opcode() != spv::Op::OpBranchConditional) {
|
2018-03-29 12:39:54 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* condition_inst = context_->get_def_use_mgr()->GetDef(
|
2018-03-29 12:39:54 +00:00
|
|
|
branch_conditional->GetSingleWordInOperand(0));
|
|
|
|
if (IsSupportedCondition(condition_inst->opcode())) {
|
|
|
|
return condition_inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
// Extract the initial value from the |induction| OpPhi instruction and store it
|
|
|
|
// in |value|. If the function couldn't find the initial value of |induction|
|
|
|
|
// return false.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool Loop::GetInductionInitValue(const Instruction* induction,
|
2018-02-14 17:03:12 +00:00
|
|
|
int64_t* value) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* constant_instruction = nullptr;
|
|
|
|
analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
for (uint32_t operand_id = 0; operand_id < induction->NumInOperands();
|
|
|
|
operand_id += 2) {
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* bb = context_->cfg()->block(
|
2018-02-14 17:03:12 +00:00
|
|
|
induction->GetSingleWordInOperand(operand_id + 1));
|
|
|
|
|
2018-02-27 11:50:08 +00:00
|
|
|
if (!IsInsideLoop(bb)) {
|
2018-02-14 17:03:12 +00:00
|
|
|
constant_instruction = def_use_manager->GetDef(
|
|
|
|
induction->GetSingleWordInOperand(operand_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!constant_instruction) return false;
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
const analysis::Constant* constant =
|
2018-02-14 17:03:12 +00:00
|
|
|
context_->get_constant_mgr()->FindDeclaredConstant(
|
|
|
|
constant_instruction->result_id());
|
|
|
|
if (!constant) return false;
|
|
|
|
|
|
|
|
if (value) {
|
2020-08-12 17:00:41 +00:00
|
|
|
const analysis::Integer* type = constant->type()->AsInteger();
|
|
|
|
if (!type) {
|
|
|
|
return false;
|
2018-02-14 17:03:12 +00:00
|
|
|
}
|
2020-08-12 17:00:41 +00:00
|
|
|
|
|
|
|
*value = type->IsSigned() ? constant->GetSignExtendedValue()
|
|
|
|
: constant->GetZeroExtendedValue();
|
2018-02-14 17:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Loop::Loop(IRContext* context, DominatorAnalysis* dom_analysis,
|
2017-12-21 14:47:25 +00:00
|
|
|
BasicBlock* header, BasicBlock* continue_target,
|
|
|
|
BasicBlock* merge_target)
|
2018-01-29 10:39:55 +00:00
|
|
|
: context_(context),
|
|
|
|
loop_header_(header),
|
2017-12-21 14:47:25 +00:00
|
|
|
loop_continue_(continue_target),
|
|
|
|
loop_merge_(merge_target),
|
|
|
|
loop_preheader_(nullptr),
|
2018-02-14 17:03:12 +00:00
|
|
|
parent_(nullptr),
|
|
|
|
loop_is_marked_for_removal_(false) {
|
2017-12-21 14:47:25 +00:00
|
|
|
assert(context);
|
|
|
|
assert(dom_analysis);
|
2018-01-29 10:39:55 +00:00
|
|
|
loop_preheader_ = FindLoopPreheader(dom_analysis);
|
2018-05-02 17:01:24 +00:00
|
|
|
loop_latch_ = FindLatchBlock();
|
2017-12-21 14:47:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* Loop::FindLoopPreheader(DominatorAnalysis* dom_analysis) {
|
2018-01-29 10:39:55 +00:00
|
|
|
CFG* cfg = context_->cfg();
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorTree& dom_tree = dom_analysis->GetDomTree();
|
|
|
|
DominatorTreeNode* header_node = dom_tree.GetTreeNode(loop_header_);
|
2017-12-21 14:47:25 +00:00
|
|
|
|
|
|
|
// The loop predecessor.
|
|
|
|
BasicBlock* loop_pred = nullptr;
|
|
|
|
|
|
|
|
auto header_pred = cfg->preds(loop_header_->id());
|
|
|
|
for (uint32_t p_id : header_pred) {
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorTreeNode* node = dom_tree.GetTreeNode(p_id);
|
2017-12-21 14:47:25 +00:00
|
|
|
if (node && !dom_tree.Dominates(header_node, node)) {
|
|
|
|
// The predecessor is not part of the loop, so potential loop preheader.
|
|
|
|
if (loop_pred && node->bb_ != loop_pred) {
|
|
|
|
// If we saw 2 distinct predecessors that are outside the loop, we don't
|
|
|
|
// have a loop preheader.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
loop_pred = node->bb_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Safe guard against invalid code, SPIR-V spec forbids loop with the entry
|
|
|
|
// node as header.
|
|
|
|
assert(loop_pred && "The header node is the entry block ?");
|
|
|
|
|
|
|
|
// So we have a unique basic block that can enter this loop.
|
|
|
|
// If this loop is the unique successor of this block, then it is a loop
|
|
|
|
// preheader.
|
|
|
|
bool is_preheader = true;
|
|
|
|
uint32_t loop_header_id = loop_header_->id();
|
2018-02-02 22:17:42 +00:00
|
|
|
const auto* const_loop_pred = loop_pred;
|
|
|
|
const_loop_pred->ForEachSuccessorLabel(
|
2017-12-21 14:47:25 +00:00
|
|
|
[&is_preheader, loop_header_id](const uint32_t id) {
|
|
|
|
if (id != loop_header_id) is_preheader = false;
|
|
|
|
});
|
|
|
|
if (is_preheader) return loop_pred;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:33:06 +00:00
|
|
|
bool Loop::IsInsideLoop(Instruction* inst) const {
|
2018-01-29 10:39:55 +00:00
|
|
|
const BasicBlock* parent_block = context_->get_instr_block(inst);
|
2018-01-25 10:33:06 +00:00
|
|
|
if (!parent_block) return false;
|
|
|
|
return IsInsideLoop(parent_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Loop::IsBasicBlockInLoopSlow(const BasicBlock* bb) {
|
|
|
|
assert(bb->GetParent() && "The basic block does not belong to a function");
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorAnalysis* dom_analysis =
|
2018-04-20 16:28:40 +00:00
|
|
|
context_->GetDominatorAnalysis(bb->GetParent());
|
2018-02-12 21:42:15 +00:00
|
|
|
if (dom_analysis->IsReachable(bb) &&
|
|
|
|
!dom_analysis->Dominates(GetHeaderBlock(), bb))
|
|
|
|
return false;
|
2018-01-25 10:33:06 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-29 10:39:55 +00:00
|
|
|
BasicBlock* Loop::GetOrCreatePreHeaderBlock() {
|
2018-01-26 12:07:10 +00:00
|
|
|
if (loop_preheader_) return loop_preheader_;
|
|
|
|
|
2018-01-29 10:39:55 +00:00
|
|
|
CFG* cfg = context_->cfg();
|
2018-03-06 16:20:28 +00:00
|
|
|
loop_header_ = cfg->SplitLoopHeader(loop_header_);
|
2018-01-26 12:07:10 +00:00
|
|
|
return loop_preheader_;
|
|
|
|
}
|
|
|
|
|
2018-05-02 17:01:24 +00:00
|
|
|
void Loop::SetContinueBlock(BasicBlock* continue_block) {
|
|
|
|
assert(IsInsideLoop(continue_block));
|
|
|
|
loop_continue_ = continue_block;
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:07:10 +00:00
|
|
|
void Loop::SetLatchBlock(BasicBlock* latch) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(latch->GetParent() && "The basic block does not belong to a function");
|
|
|
|
|
2018-02-02 22:17:42 +00:00
|
|
|
const auto* const_latch = latch;
|
|
|
|
const_latch->ForEachSuccessorLabel([this](uint32_t id) {
|
2018-01-26 12:07:10 +00:00
|
|
|
assert((!IsInsideLoop(id) || id == GetHeaderBlock()->id()) &&
|
|
|
|
"A predecessor of the continue block does not belong to the loop");
|
|
|
|
});
|
|
|
|
#endif // NDEBUG
|
|
|
|
assert(IsInsideLoop(latch) && "The continue block is not in the loop");
|
|
|
|
|
|
|
|
SetLatchBlockImpl(latch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Loop::SetMergeBlock(BasicBlock* merge) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(merge->GetParent() && "The basic block does not belong to a function");
|
2018-01-29 10:39:55 +00:00
|
|
|
#endif // NDEBUG
|
2018-02-14 17:03:12 +00:00
|
|
|
assert(!IsInsideLoop(merge) && "The merge block is in the loop");
|
2018-01-26 12:07:10 +00:00
|
|
|
|
|
|
|
SetMergeBlockImpl(merge);
|
|
|
|
if (GetHeaderBlock()->GetLoopMergeInst()) {
|
|
|
|
UpdateLoopMergeInst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
void Loop::SetPreHeaderBlock(BasicBlock* preheader) {
|
2018-03-07 18:21:11 +00:00
|
|
|
if (preheader) {
|
|
|
|
assert(!IsInsideLoop(preheader) && "The preheader block is in the loop");
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(preheader->tail()->opcode() == spv::Op::OpBranch &&
|
2018-03-07 18:21:11 +00:00
|
|
|
"The preheader block does not unconditionally branch to the header "
|
|
|
|
"block");
|
|
|
|
assert(preheader->tail()->GetSingleWordOperand(0) ==
|
|
|
|
GetHeaderBlock()->id() &&
|
|
|
|
"The preheader block does not unconditionally branch to the header "
|
|
|
|
"block");
|
|
|
|
}
|
2018-02-12 21:42:15 +00:00
|
|
|
loop_preheader_ = preheader;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* Loop::FindLatchBlock() {
|
|
|
|
CFG* cfg = context_->cfg();
|
2018-05-02 17:01:24 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorAnalysis* dominator_analysis =
|
2018-05-02 17:01:24 +00:00
|
|
|
context_->GetDominatorAnalysis(loop_header_->GetParent());
|
|
|
|
|
|
|
|
// Look at the predecessors of the loop header to find a predecessor block
|
|
|
|
// which is dominated by the loop continue target. There should only be one
|
|
|
|
// block which meets this criteria and this is the latch block, as per the
|
|
|
|
// SPIR-V spec.
|
|
|
|
for (uint32_t block_id : cfg->preds(loop_header_->id())) {
|
|
|
|
if (dominator_analysis->Dominates(loop_continue_->id(), block_id)) {
|
|
|
|
return cfg->block(block_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(
|
|
|
|
false &&
|
|
|
|
"Every loop should have a latch block dominated by the continue target");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-29 10:39:55 +00:00
|
|
|
void Loop::GetExitBlocks(std::unordered_set<uint32_t>* exit_blocks) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
CFG* cfg = context_->cfg();
|
2018-01-29 10:39:55 +00:00
|
|
|
exit_blocks->clear();
|
2018-01-26 12:07:10 +00:00
|
|
|
|
|
|
|
for (uint32_t bb_id : GetBlocks()) {
|
2018-07-07 13:38:00 +00:00
|
|
|
const BasicBlock* bb = cfg->block(bb_id);
|
2018-01-26 12:07:10 +00:00
|
|
|
bb->ForEachSuccessorLabel([exit_blocks, this](uint32_t succ) {
|
|
|
|
if (!IsInsideLoop(succ)) {
|
|
|
|
exit_blocks->insert(succ);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Loop::GetMergingBlocks(
|
2018-01-29 10:39:55 +00:00
|
|
|
std::unordered_set<uint32_t>* merging_blocks) const {
|
2018-01-26 12:07:10 +00:00
|
|
|
assert(GetMergeBlock() && "This loop is not structured");
|
2018-07-12 19:14:43 +00:00
|
|
|
CFG* cfg = context_->cfg();
|
2018-01-29 10:39:55 +00:00
|
|
|
merging_blocks->clear();
|
2018-01-26 12:07:10 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::stack<const BasicBlock*> to_visit;
|
2018-01-26 12:07:10 +00:00
|
|
|
to_visit.push(GetMergeBlock());
|
|
|
|
while (!to_visit.empty()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
const BasicBlock* bb = to_visit.top();
|
2018-01-26 12:07:10 +00:00
|
|
|
to_visit.pop();
|
|
|
|
merging_blocks->insert(bb->id());
|
|
|
|
for (uint32_t pred_id : cfg->preds(bb->id())) {
|
|
|
|
if (!IsInsideLoop(pred_id) && !merging_blocks->count(pred_id)) {
|
|
|
|
to_visit.push(cfg->block(pred_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
namespace {
|
|
|
|
|
2022-11-17 18:02:50 +00:00
|
|
|
inline bool IsBasicBlockSafeToClone(IRContext* context, BasicBlock* bb) {
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Instruction& inst : *bb) {
|
2018-02-12 21:42:15 +00:00
|
|
|
if (!inst.IsBranch() && !context->IsCombinatorInstruction(&inst))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool Loop::IsSafeToClone() const {
|
2018-07-12 19:14:43 +00:00
|
|
|
CFG& cfg = *context_->cfg();
|
2018-02-12 21:42:15 +00:00
|
|
|
|
|
|
|
for (uint32_t bb_id : GetBlocks()) {
|
|
|
|
BasicBlock* bb = cfg.block(bb_id);
|
|
|
|
assert(bb);
|
|
|
|
if (!IsBasicBlockSafeToClone(context_, bb)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look at the merge construct.
|
|
|
|
if (GetHeaderBlock()->GetLoopMergeInst()) {
|
|
|
|
std::unordered_set<uint32_t> blocks;
|
|
|
|
GetMergingBlocks(&blocks);
|
|
|
|
blocks.erase(GetMergeBlock()->id());
|
|
|
|
for (uint32_t bb_id : blocks) {
|
|
|
|
BasicBlock* bb = cfg.block(bb_id);
|
|
|
|
assert(bb);
|
|
|
|
if (!IsBasicBlockSafeToClone(context_, bb)) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:07:10 +00:00
|
|
|
bool Loop::IsLCSSA() const {
|
2018-07-12 19:14:43 +00:00
|
|
|
CFG* cfg = context_->cfg();
|
|
|
|
analysis::DefUseManager* def_use_mgr = context_->get_def_use_mgr();
|
2018-01-26 12:07:10 +00:00
|
|
|
|
|
|
|
std::unordered_set<uint32_t> exit_blocks;
|
2018-01-29 10:39:55 +00:00
|
|
|
GetExitBlocks(&exit_blocks);
|
|
|
|
|
|
|
|
// Declare ir_context so we can capture context_ in the below lambda
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext* ir_context = context_;
|
2018-01-26 12:07:10 +00:00
|
|
|
|
|
|
|
for (uint32_t bb_id : GetBlocks()) {
|
|
|
|
for (Instruction& insn : *cfg->block(bb_id)) {
|
|
|
|
// All uses must be either:
|
|
|
|
// - In the loop;
|
|
|
|
// - In an exit block and in a phi instruction.
|
|
|
|
if (!def_use_mgr->WhileEachUser(
|
|
|
|
&insn,
|
2018-07-12 19:14:43 +00:00
|
|
|
[&exit_blocks, ir_context, this](Instruction* use) -> bool {
|
2018-01-29 10:39:55 +00:00
|
|
|
BasicBlock* parent = ir_context->get_instr_block(use);
|
2018-01-26 12:07:10 +00:00
|
|
|
assert(parent && "Invalid analysis");
|
|
|
|
if (IsInsideLoop(parent)) return true;
|
2022-11-04 21:27:10 +00:00
|
|
|
if (use->opcode() != spv::Op::OpPhi) return false;
|
2018-01-26 12:07:10 +00:00
|
|
|
return exit_blocks.count(parent->id());
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-29 10:39:55 +00:00
|
|
|
bool Loop::ShouldHoistInstruction(IRContext* context, Instruction* inst) {
|
|
|
|
return AreAllOperandsOutsideLoop(context, inst) &&
|
|
|
|
inst->IsOpcodeCodeMotionSafe();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Loop::AreAllOperandsOutsideLoop(IRContext* context, Instruction* inst) {
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
|
2018-01-29 10:39:55 +00:00
|
|
|
bool all_outside_loop = true;
|
|
|
|
|
|
|
|
const std::function<void(uint32_t*)> operand_outside_loop =
|
|
|
|
[this, &def_use_mgr, &all_outside_loop](uint32_t* id) {
|
|
|
|
if (this->IsInsideLoop(def_use_mgr->GetDef(*id))) {
|
|
|
|
all_outside_loop = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inst->ForEachInId(operand_outside_loop);
|
|
|
|
return all_outside_loop;
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:50:08 +00:00
|
|
|
void Loop::ComputeLoopStructuredOrder(
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<BasicBlock*>* ordered_loop_blocks, bool include_pre_header,
|
2018-02-27 11:50:08 +00:00
|
|
|
bool include_merge) const {
|
2018-07-12 19:14:43 +00:00
|
|
|
CFG& cfg = *context_->cfg();
|
2018-02-27 11:50:08 +00:00
|
|
|
|
|
|
|
// Reserve the memory: all blocks in the loop + extra if needed.
|
|
|
|
ordered_loop_blocks->reserve(GetBlocks().size() + include_pre_header +
|
|
|
|
include_merge);
|
|
|
|
|
|
|
|
if (include_pre_header && GetPreHeaderBlock())
|
|
|
|
ordered_loop_blocks->push_back(loop_preheader_);
|
2020-06-18 20:00:34 +00:00
|
|
|
|
|
|
|
bool is_shader =
|
2022-11-04 21:27:10 +00:00
|
|
|
context_->get_feature_mgr()->HasCapability(spv::Capability::Shader);
|
2020-06-18 20:00:34 +00:00
|
|
|
if (!is_shader) {
|
|
|
|
cfg.ForEachBlockInReversePostOrder(
|
|
|
|
loop_header_, [ordered_loop_blocks, this](BasicBlock* bb) {
|
|
|
|
if (IsInsideLoop(bb)) ordered_loop_blocks->push_back(bb);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If this is a shader, it is possible that there are unreachable merge and
|
|
|
|
// continue blocks that must be copied to retain the structured order.
|
|
|
|
// The structured order will include these.
|
|
|
|
std::list<BasicBlock*> order;
|
2022-06-29 13:53:26 +00:00
|
|
|
cfg.ComputeStructuredOrder(loop_header_->GetParent(), loop_header_,
|
|
|
|
loop_merge_, &order);
|
2020-06-18 20:00:34 +00:00
|
|
|
for (BasicBlock* bb : order) {
|
|
|
|
if (bb == GetMergeBlock()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ordered_loop_blocks->push_back(bb);
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 11:50:08 +00:00
|
|
|
if (include_merge && GetMergeBlock())
|
|
|
|
ordered_loop_blocks->push_back(loop_merge_);
|
|
|
|
}
|
|
|
|
|
2018-07-12 18:42:05 +00:00
|
|
|
LoopDescriptor::LoopDescriptor(IRContext* context, const Function* f)
|
2020-07-29 17:50:58 +00:00
|
|
|
: loops_(), placeholder_top_loop_(nullptr) {
|
2018-07-12 18:42:05 +00:00
|
|
|
PopulateList(context, f);
|
2018-02-05 15:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoopDescriptor::~LoopDescriptor() { ClearLoops(); }
|
2017-12-21 14:47:25 +00:00
|
|
|
|
2018-07-12 18:42:05 +00:00
|
|
|
void LoopDescriptor::PopulateList(IRContext* context, const Function* f) {
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorAnalysis* dom_analysis = context->GetDominatorAnalysis(f);
|
2017-12-21 14:47:25 +00:00
|
|
|
|
2018-02-05 15:56:45 +00:00
|
|
|
ClearLoops();
|
2017-12-21 14:47:25 +00:00
|
|
|
|
|
|
|
// Post-order traversal of the dominator tree to find all the OpLoopMerge
|
|
|
|
// instructions.
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorTree& dom_tree = dom_analysis->GetDomTree();
|
|
|
|
for (DominatorTreeNode& node :
|
|
|
|
make_range(dom_tree.post_begin(), dom_tree.post_end())) {
|
2017-12-21 14:47:25 +00:00
|
|
|
Instruction* merge_inst = node.bb_->GetLoopMergeInst();
|
|
|
|
if (merge_inst) {
|
2018-02-12 21:42:15 +00:00
|
|
|
bool all_backedge_unreachable = true;
|
|
|
|
for (uint32_t pid : context->cfg()->preds(node.bb_->id())) {
|
|
|
|
if (dom_analysis->IsReachable(pid) &&
|
|
|
|
dom_analysis->Dominates(node.bb_->id(), pid)) {
|
|
|
|
all_backedge_unreachable = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (all_backedge_unreachable)
|
|
|
|
continue; // ignore this one, we actually never branch back.
|
|
|
|
|
2017-12-21 14:47:25 +00:00
|
|
|
// The id of the merge basic block of this loop.
|
|
|
|
uint32_t merge_bb_id = merge_inst->GetSingleWordOperand(0);
|
|
|
|
|
|
|
|
// The id of the continue basic block of this loop.
|
|
|
|
uint32_t continue_bb_id = merge_inst->GetSingleWordOperand(1);
|
|
|
|
|
|
|
|
// The merge target of this loop.
|
|
|
|
BasicBlock* merge_bb = context->cfg()->block(merge_bb_id);
|
|
|
|
|
|
|
|
// The continue target of this loop.
|
|
|
|
BasicBlock* continue_bb = context->cfg()->block(continue_bb_id);
|
|
|
|
|
|
|
|
// The basic block containing the merge instruction.
|
|
|
|
BasicBlock* header_bb = context->get_instr_block(merge_inst);
|
|
|
|
|
|
|
|
// Add the loop to the list of all the loops in the function.
|
2018-02-05 15:56:45 +00:00
|
|
|
Loop* current_loop =
|
|
|
|
new Loop(context, dom_analysis, header_bb, continue_bb, merge_bb);
|
|
|
|
loops_.push_back(current_loop);
|
2017-12-21 14:47:25 +00:00
|
|
|
|
|
|
|
// We have a bottom-up construction, so if this loop has nested-loops,
|
|
|
|
// they are by construction at the tail of the loop list.
|
|
|
|
for (auto itr = loops_.rbegin() + 1; itr != loops_.rend(); ++itr) {
|
2018-02-05 15:56:45 +00:00
|
|
|
Loop* previous_loop = *itr;
|
2017-12-21 14:47:25 +00:00
|
|
|
|
|
|
|
// If the loop already has a parent, then it has been processed.
|
|
|
|
if (previous_loop->HasParent()) continue;
|
|
|
|
|
|
|
|
// If the current loop does not dominates the previous loop then it is
|
|
|
|
// not nested loop.
|
|
|
|
if (!dom_analysis->Dominates(header_bb,
|
|
|
|
previous_loop->GetHeaderBlock()))
|
|
|
|
continue;
|
|
|
|
// If the current loop merge dominates the previous loop then it is
|
|
|
|
// not nested loop.
|
|
|
|
if (dom_analysis->Dominates(merge_bb, previous_loop->GetHeaderBlock()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
current_loop->AddNestedLoop(previous_loop);
|
|
|
|
}
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorTreeNode* dom_merge_node = dom_tree.GetTreeNode(merge_bb);
|
|
|
|
for (DominatorTreeNode& loop_node :
|
2017-12-21 14:47:25 +00:00
|
|
|
make_range(node.df_begin(), node.df_end())) {
|
|
|
|
// Check if we are in the loop.
|
|
|
|
if (dom_tree.Dominates(dom_merge_node, &loop_node)) continue;
|
2018-02-14 17:03:12 +00:00
|
|
|
current_loop->AddBasicBlock(loop_node.bb_);
|
2017-12-21 14:47:25 +00:00
|
|
|
basic_block_to_loop_.insert(
|
|
|
|
std::make_pair(loop_node.bb_->id(), current_loop));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 15:56:45 +00:00
|
|
|
for (Loop* loop : loops_) {
|
2020-07-29 17:50:58 +00:00
|
|
|
if (!loop->HasParent()) placeholder_top_loop_.nested_loops_.push_back(loop);
|
2017-12-21 14:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Loop*> LoopDescriptor::GetLoopsInBinaryLayoutOrder() {
|
2018-04-20 14:14:45 +00:00
|
|
|
std::vector<uint32_t> ids{};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < NumLoops(); ++i) {
|
|
|
|
ids.push_back(GetLoopByIndex(i).GetHeaderBlock()->id());
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Loop*> loops{};
|
2018-04-20 14:14:45 +00:00
|
|
|
if (!ids.empty()) {
|
|
|
|
auto function = GetLoopByIndex(0).GetHeaderBlock()->GetParent();
|
|
|
|
for (const auto& block : *function) {
|
|
|
|
auto block_id = block.id();
|
|
|
|
|
|
|
|
auto element = std::find(std::begin(ids), std::end(ids), block_id);
|
|
|
|
if (element != std::end(ids)) {
|
|
|
|
loops.push_back(&GetLoopByIndex(element - std::begin(ids)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return loops;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* Loop::FindConditionBlock() const {
|
2018-03-29 11:22:42 +00:00
|
|
|
if (!loop_merge_) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* condition_block = nullptr;
|
2018-02-14 17:03:12 +00:00
|
|
|
|
2018-03-29 11:22:42 +00:00
|
|
|
uint32_t in_loop_pred = 0;
|
|
|
|
for (uint32_t p : context_->cfg()->preds(loop_merge_->id())) {
|
|
|
|
if (IsInsideLoop(p)) {
|
|
|
|
if (in_loop_pred) {
|
|
|
|
// 2 in-loop predecessors.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
in_loop_pred = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!in_loop_pred) {
|
|
|
|
// Merge block is unreachable.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* bb = context_->cfg()->block(in_loop_pred);
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
if (!bb) return nullptr;
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction& branch = *bb->ctail();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Make sure the branch is a conditional branch.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (branch.opcode() != spv::Op::OpBranchConditional) return nullptr;
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Make sure one of the two possible branches is to the merge block.
|
|
|
|
if (branch.GetSingleWordInOperand(1) == loop_merge_->id() ||
|
|
|
|
branch.GetSingleWordInOperand(2) == loop_merge_->id()) {
|
|
|
|
condition_block = bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
return condition_block;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool Loop::FindNumberOfIterations(const Instruction* induction,
|
|
|
|
const Instruction* branch_inst,
|
2018-02-14 17:03:12 +00:00
|
|
|
size_t* iterations_out,
|
|
|
|
int64_t* step_value_out,
|
|
|
|
int64_t* init_value_out) const {
|
|
|
|
// From the branch instruction find the branch condition.
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Condition instruction from the OpConditionalBranch.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* condition =
|
2018-02-14 17:03:12 +00:00
|
|
|
def_use_manager->GetDef(branch_inst->GetSingleWordOperand(0));
|
|
|
|
|
|
|
|
assert(IsSupportedCondition(condition->opcode()));
|
|
|
|
|
|
|
|
// Get the constant manager from the ir context.
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::ConstantManager* const_manager = context_->get_constant_mgr();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Find the constant value used by the condition variable. Exit out if it
|
|
|
|
// isn't a constant int.
|
2018-07-12 19:14:43 +00:00
|
|
|
const analysis::Constant* upper_bound =
|
2018-02-14 17:03:12 +00:00
|
|
|
const_manager->FindDeclaredConstant(condition->GetSingleWordOperand(3));
|
|
|
|
if (!upper_bound) return false;
|
|
|
|
|
|
|
|
// Must be integer because of the opcode on the condition.
|
2020-08-12 17:00:41 +00:00
|
|
|
const analysis::Integer* type = upper_bound->type()->AsInteger();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
2020-08-12 17:00:41 +00:00
|
|
|
if (!type || type->width() > 64) {
|
2018-03-29 11:22:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:00:41 +00:00
|
|
|
int64_t condition_value = type->IsSigned()
|
|
|
|
? upper_bound->GetSignExtendedValue()
|
|
|
|
: upper_bound->GetZeroExtendedValue();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Find the instruction which is stepping through the loop.
|
2020-08-12 17:00:41 +00:00
|
|
|
//
|
|
|
|
// GetInductionStepOperation returns nullptr if |step_inst| is OpConstantNull.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* step_inst = GetInductionStepOperation(induction);
|
2018-02-14 17:03:12 +00:00
|
|
|
if (!step_inst) return false;
|
|
|
|
|
|
|
|
// Find the constant value used by the condition variable.
|
2018-07-12 19:14:43 +00:00
|
|
|
const analysis::Constant* step_constant =
|
2018-02-14 17:03:12 +00:00
|
|
|
const_manager->FindDeclaredConstant(step_inst->GetSingleWordOperand(3));
|
|
|
|
if (!step_constant) return false;
|
|
|
|
|
|
|
|
// Must be integer because of the opcode on the condition.
|
|
|
|
int64_t step_value = 0;
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
const analysis::Integer* step_type =
|
2018-02-14 17:03:12 +00:00
|
|
|
step_constant->AsIntConstant()->type()->AsInteger();
|
|
|
|
|
|
|
|
if (step_type->IsSigned()) {
|
|
|
|
step_value = step_constant->AsIntConstant()->GetS32BitValue();
|
|
|
|
} else {
|
|
|
|
step_value = step_constant->AsIntConstant()->GetU32BitValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a subtraction step we should negate the step value.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (step_inst->opcode() == spv::Op::OpISub) {
|
2018-02-14 17:03:12 +00:00
|
|
|
step_value = -step_value;
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:13:08 +00:00
|
|
|
// Find the initial value of the loop and make sure it is a constant integer.
|
2018-02-14 17:03:12 +00:00
|
|
|
int64_t init_value = 0;
|
2018-02-27 11:50:08 +00:00
|
|
|
if (!GetInductionInitValue(induction, &init_value)) return false;
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// If iterations is non null then store the value in that.
|
2018-02-27 11:50:08 +00:00
|
|
|
int64_t num_itrs = GetIterations(condition->opcode(), condition_value,
|
|
|
|
init_value, step_value);
|
2018-02-14 17:03:12 +00:00
|
|
|
|
2018-02-27 11:50:08 +00:00
|
|
|
// If the loop body will not be reached return false.
|
|
|
|
if (num_itrs <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iterations_out) {
|
2018-02-14 17:03:12 +00:00
|
|
|
assert(static_cast<size_t>(num_itrs) <= std::numeric_limits<size_t>::max());
|
|
|
|
*iterations_out = static_cast<size_t>(num_itrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (step_value_out) {
|
|
|
|
*step_value_out = step_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init_value_out) {
|
|
|
|
*init_value_out = init_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We retrieve the number of iterations using the following formula, diff /
|
|
|
|
// |step_value| where diff is calculated differently according to the
|
|
|
|
// |condition| and uses the |condition_value| and |init_value|. If diff /
|
2022-01-26 20:13:08 +00:00
|
|
|
// |step_value| is NOT cleanly divisible then we add one to the sum.
|
2022-11-04 21:27:10 +00:00
|
|
|
int64_t Loop::GetIterations(spv::Op condition, int64_t condition_value,
|
2018-02-14 17:03:12 +00:00
|
|
|
int64_t init_value, int64_t step_value) const {
|
2022-04-11 14:21:15 +00:00
|
|
|
if (step_value == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
int64_t diff = 0;
|
|
|
|
|
|
|
|
switch (condition) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSLessThan:
|
|
|
|
case spv::Op::OpULessThan: {
|
2018-02-27 11:50:08 +00:00
|
|
|
// If the condition is not met to begin with the loop will never iterate.
|
|
|
|
if (!(init_value < condition_value)) return 0;
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
diff = condition_value - init_value;
|
2018-02-27 11:50:08 +00:00
|
|
|
|
|
|
|
// If the operation is a less then operation then the diff and step must
|
|
|
|
// have the same sign otherwise the induction will never cross the
|
|
|
|
// condition (either never true or always true).
|
|
|
|
if ((diff < 0 && step_value > 0) || (diff > 0 && step_value < 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSGreaterThan:
|
|
|
|
case spv::Op::OpUGreaterThan: {
|
2018-02-27 11:50:08 +00:00
|
|
|
// If the condition is not met to begin with the loop will never iterate.
|
|
|
|
if (!(init_value > condition_value)) return 0;
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
diff = init_value - condition_value;
|
2018-02-27 11:50:08 +00:00
|
|
|
|
|
|
|
// If the operation is a greater than operation then the diff and step
|
|
|
|
// must have opposite signs. Otherwise the condition will always be true
|
|
|
|
// or will never be true.
|
|
|
|
if ((diff < 0 && step_value < 0) || (diff > 0 && step_value > 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSGreaterThanEqual:
|
|
|
|
case spv::Op::OpUGreaterThanEqual: {
|
2018-02-27 11:50:08 +00:00
|
|
|
// If the condition is not met to begin with the loop will never iterate.
|
|
|
|
if (!(init_value >= condition_value)) return 0;
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
// We subtract one to make it the same as spv::Op::OpGreaterThan as it is
|
2018-02-27 11:50:08 +00:00
|
|
|
// functionally equivalent.
|
|
|
|
diff = init_value - (condition_value - 1);
|
|
|
|
|
|
|
|
// If the operation is a greater than operation then the diff and step
|
|
|
|
// must have opposite signs. Otherwise the condition will always be true
|
|
|
|
// or will never be true.
|
|
|
|
if ((diff > 0 && step_value > 0) || (diff < 0 && step_value < 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSLessThanEqual:
|
|
|
|
case spv::Op::OpULessThanEqual: {
|
2018-02-27 11:50:08 +00:00
|
|
|
// If the condition is not met to begin with the loop will never iterate.
|
|
|
|
if (!(init_value <= condition_value)) return 0;
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
// We add one to make it the same as spv::Op::OpLessThan as it is
|
|
|
|
// functionally equivalent.
|
2018-02-27 11:50:08 +00:00
|
|
|
diff = (condition_value + 1) - init_value;
|
|
|
|
|
|
|
|
// If the operation is a less than operation then the diff and step must
|
|
|
|
// have the same sign otherwise the induction will never cross the
|
|
|
|
// condition (either never true or always true).
|
|
|
|
if ((diff < 0 && step_value > 0) || (diff > 0 && step_value < 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-02-27 11:50:08 +00:00
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
default:
|
|
|
|
assert(false &&
|
|
|
|
"Could not retrieve number of iterations from the loop condition. "
|
|
|
|
"Condition is not supported.");
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:50:08 +00:00
|
|
|
// Take the abs of - step values.
|
|
|
|
step_value = llabs(step_value);
|
|
|
|
diff = llabs(diff);
|
2018-02-14 17:03:12 +00:00
|
|
|
int64_t result = diff / step_value;
|
|
|
|
|
|
|
|
if (diff % step_value != 0) {
|
|
|
|
result += 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:50:08 +00:00
|
|
|
// Returns the list of induction variables within the loop.
|
|
|
|
void Loop::GetInductionVariables(
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*>& induction_variables) const {
|
|
|
|
for (Instruction& inst : *loop_header_) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst.opcode() == spv::Op::OpPhi) {
|
2018-02-27 11:50:08 +00:00
|
|
|
induction_variables.push_back(&inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* Loop::FindConditionVariable(
|
|
|
|
const BasicBlock* condition_block) const {
|
2018-02-14 17:03:12 +00:00
|
|
|
// Find the branch instruction.
|
2018-07-12 19:14:43 +00:00
|
|
|
const Instruction& branch_inst = *condition_block->ctail();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* induction = nullptr;
|
2018-02-14 17:03:12 +00:00
|
|
|
// Verify that the branch instruction is a conditional branch.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (branch_inst.opcode() == spv::Op::OpBranchConditional) {
|
2018-02-14 17:03:12 +00:00
|
|
|
// From the branch instruction find the branch condition.
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Find the instruction representing the condition used in the conditional
|
|
|
|
// branch.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* condition =
|
2018-02-14 17:03:12 +00:00
|
|
|
def_use_manager->GetDef(branch_inst.GetSingleWordOperand(0));
|
|
|
|
|
|
|
|
// Ensure that the condition is a less than operation.
|
|
|
|
if (condition && IsSupportedCondition(condition->opcode())) {
|
|
|
|
// The left hand side operand of the operation.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* variable_inst =
|
2018-02-14 17:03:12 +00:00
|
|
|
def_use_manager->GetDef(condition->GetSingleWordOperand(2));
|
|
|
|
|
|
|
|
// Make sure the variable instruction used is a phi.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!variable_inst || variable_inst->opcode() != spv::Op::OpPhi)
|
|
|
|
return nullptr;
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
// Make sure the phi instruction only has two incoming blocks. Each
|
|
|
|
// incoming block will be represented by two in operands in the phi
|
|
|
|
// instruction, the value and the block which that value came from. We
|
|
|
|
// assume the cannocalised phi will have two incoming values, one from the
|
|
|
|
// preheader and one from the continue block.
|
|
|
|
size_t max_supported_operands = 4;
|
|
|
|
if (variable_inst->NumInOperands() == max_supported_operands) {
|
|
|
|
// The operand index of the first incoming block label.
|
|
|
|
uint32_t operand_label_1 = 1;
|
|
|
|
|
|
|
|
// The operand index of the second incoming block label.
|
|
|
|
uint32_t operand_label_2 = 3;
|
|
|
|
|
|
|
|
// Make sure one of them is the preheader.
|
2018-03-29 11:22:42 +00:00
|
|
|
if (!IsInsideLoop(
|
|
|
|
variable_inst->GetSingleWordInOperand(operand_label_1)) &&
|
|
|
|
!IsInsideLoop(
|
|
|
|
variable_inst->GetSingleWordInOperand(operand_label_2))) {
|
2018-02-14 17:03:12 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// And make sure that the other is the latch block.
|
|
|
|
if (variable_inst->GetSingleWordInOperand(operand_label_1) !=
|
2018-05-02 17:01:24 +00:00
|
|
|
loop_latch_->id() &&
|
2018-02-14 17:03:12 +00:00
|
|
|
variable_inst->GetSingleWordInOperand(operand_label_2) !=
|
2018-05-02 17:01:24 +00:00
|
|
|
loop_latch_->id()) {
|
2018-02-14 17:03:12 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FindNumberOfIterations(variable_inst, &branch_inst, nullptr))
|
|
|
|
return nullptr;
|
|
|
|
induction = variable_inst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return induction;
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:14:45 +00:00
|
|
|
bool LoopDescriptor::CreatePreHeaderBlocksIfMissing() {
|
|
|
|
auto modified = false;
|
|
|
|
|
|
|
|
for (auto& loop : *this) {
|
|
|
|
if (!loop.GetPreHeaderBlock()) {
|
|
|
|
modified = true;
|
2018-12-06 14:07:00 +00:00
|
|
|
// TODO(1841): Handle failure to create pre-header.
|
2018-04-20 14:14:45 +00:00
|
|
|
loop.GetOrCreatePreHeaderBlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:03:12 +00:00
|
|
|
// Add and remove loops which have been marked for addition and removal to
|
|
|
|
// maintain the state of the loop descriptor class.
|
|
|
|
void LoopDescriptor::PostModificationCleanup() {
|
|
|
|
LoopContainerType loops_to_remove_;
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Loop* loop : loops_) {
|
2018-02-14 17:03:12 +00:00
|
|
|
if (loop->IsMarkedForRemoval()) {
|
|
|
|
loops_to_remove_.push_back(loop);
|
|
|
|
if (loop->HasParent()) {
|
|
|
|
loop->GetParent()->RemoveChildLoop(loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Loop* loop : loops_to_remove_) {
|
2018-02-14 17:03:12 +00:00
|
|
|
loops_.erase(std::find(loops_.begin(), loops_.end(), loop));
|
2019-01-17 21:02:43 +00:00
|
|
|
delete loop;
|
2018-02-14 17:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& pair : loops_to_add_) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Loop* parent = pair.first;
|
2019-01-17 21:02:43 +00:00
|
|
|
std::unique_ptr<Loop> loop = std::move(pair.second);
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
loop->SetParent(nullptr);
|
2019-01-17 21:02:43 +00:00
|
|
|
parent->AddNestedLoop(loop.get());
|
2018-02-14 17:03:12 +00:00
|
|
|
|
|
|
|
for (uint32_t block_id : loop->GetBlocks()) {
|
|
|
|
parent->AddBasicBlock(block_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 21:02:43 +00:00
|
|
|
loops_.emplace_back(loop.release());
|
2018-02-14 17:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loops_to_add_.clear();
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:56:45 +00:00
|
|
|
void LoopDescriptor::ClearLoops() {
|
|
|
|
for (Loop* loop : loops_) {
|
|
|
|
delete loop;
|
|
|
|
}
|
|
|
|
loops_.clear();
|
|
|
|
}
|
2018-02-12 21:42:15 +00:00
|
|
|
|
|
|
|
// Adds a new loop nest to the descriptor set.
|
2018-07-12 19:14:43 +00:00
|
|
|
Loop* LoopDescriptor::AddLoopNest(std::unique_ptr<Loop> new_loop) {
|
|
|
|
Loop* loop = new_loop.release();
|
2020-07-29 17:50:58 +00:00
|
|
|
if (!loop->HasParent()) placeholder_top_loop_.nested_loops_.push_back(loop);
|
2018-02-12 21:42:15 +00:00
|
|
|
// Iterate from inner to outer most loop, adding basic block to loop mapping
|
|
|
|
// as we go.
|
2018-07-12 19:14:43 +00:00
|
|
|
for (Loop& current_loop :
|
2018-02-12 21:42:15 +00:00
|
|
|
make_range(iterator::begin(loop), iterator::end(nullptr))) {
|
|
|
|
loops_.push_back(¤t_loop);
|
|
|
|
for (uint32_t bb_id : current_loop.GetBlocks())
|
|
|
|
basic_block_to_loop_.insert(std::make_pair(bb_id, ¤t_loop));
|
|
|
|
}
|
|
|
|
|
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
void LoopDescriptor::RemoveLoop(Loop* loop) {
|
2020-07-29 17:50:58 +00:00
|
|
|
Loop* parent = loop->GetParent() ? loop->GetParent() : &placeholder_top_loop_;
|
2018-02-12 21:42:15 +00:00
|
|
|
parent->nested_loops_.erase(std::find(parent->nested_loops_.begin(),
|
|
|
|
parent->nested_loops_.end(), loop));
|
|
|
|
std::for_each(
|
|
|
|
loop->nested_loops_.begin(), loop->nested_loops_.end(),
|
2018-07-12 19:14:43 +00:00
|
|
|
[loop](Loop* sub_loop) { sub_loop->SetParent(loop->GetParent()); });
|
2018-02-12 21:42:15 +00:00
|
|
|
parent->nested_loops_.insert(parent->nested_loops_.end(),
|
|
|
|
loop->nested_loops_.begin(),
|
|
|
|
loop->nested_loops_.end());
|
|
|
|
for (uint32_t bb_id : loop->GetBlocks()) {
|
2018-07-12 19:14:43 +00:00
|
|
|
Loop* l = FindLoopForBasicBlock(bb_id);
|
2018-02-12 21:42:15 +00:00
|
|
|
if (l == loop) {
|
|
|
|
SetBasicBlockToLoop(bb_id, l->GetParent());
|
|
|
|
} else {
|
|
|
|
ForgetBasicBlock(bb_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoopContainerType::iterator it =
|
|
|
|
std::find(loops_.begin(), loops_.end(), loop);
|
|
|
|
assert(it != loops_.end());
|
|
|
|
delete loop;
|
|
|
|
loops_.erase(it);
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2017-12-21 14:47:25 +00:00
|
|
|
} // namespace spvtools
|