2020-09-30 12:16:22 +02:00
|
|
|
//===- LoopFlatten.cpp - Loop flattening pass------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass flattens pairs nested loops into a single loop.
|
|
|
|
//
|
|
|
|
// The intention is to optimise loop nests like this, which together access an
|
|
|
|
// array linearly:
|
|
|
|
// for (int i = 0; i < N; ++i)
|
|
|
|
// for (int j = 0; j < M; ++j)
|
|
|
|
// f(A[i*M+j]);
|
|
|
|
// into one loop:
|
|
|
|
// for (int i = 0; i < (N*M); ++i)
|
|
|
|
// f(A[i]);
|
|
|
|
//
|
|
|
|
// It can also flatten loops where the induction variables are not used in the
|
|
|
|
// loop. This is only worth doing if the induction variables are only used in an
|
|
|
|
// expression like i*M+j. If they had any other uses, we would have to insert a
|
|
|
|
// div/mod to reconstruct the original values, so this wouldn't be profitable.
|
|
|
|
//
|
|
|
|
// We also need to prove that N*M will not overflow.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar/LoopFlatten.h"
|
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2020-11-21 15:13:36 +01:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2020-09-30 12:16:22 +02:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/PatternMatch.h"
|
|
|
|
#include "llvm/IR/Verifier.h"
|
|
|
|
#include "llvm/InitializePasses.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2020-11-09 18:32:25 +01:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2020-09-30 12:16:22 +02:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
2020-11-09 18:32:25 +01:00
|
|
|
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
|
|
|
|
#include "llvm/Transforms/Utils/SimplifyIndVar.h"
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "loop-flatten"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::PatternMatch;
|
|
|
|
|
|
|
|
static cl::opt<unsigned> RepeatedInstructionThreshold(
|
|
|
|
"loop-flatten-cost-threshold", cl::Hidden, cl::init(2),
|
|
|
|
cl::desc("Limit on the cost of instructions that can be repeated due to "
|
|
|
|
"loop flattening"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
AssumeNoOverflow("loop-flatten-assume-no-overflow", cl::Hidden,
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("Assume that the product of the two iteration "
|
|
|
|
"limits will never overflow"));
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
WidenIV("loop-flatten-widen-iv", cl::Hidden,
|
2020-11-21 15:13:36 +01:00
|
|
|
cl::init(true),
|
2020-11-09 18:32:25 +01:00
|
|
|
cl::desc("Widen the loop induction variables, if possible, so "
|
|
|
|
"overflow checks won't reject flattening"));
|
|
|
|
|
2020-11-09 15:33:52 +01:00
|
|
|
struct FlattenInfo {
|
|
|
|
Loop *OuterLoop = nullptr;
|
|
|
|
Loop *InnerLoop = nullptr;
|
|
|
|
PHINode *InnerInductionPHI = nullptr;
|
|
|
|
PHINode *OuterInductionPHI = nullptr;
|
|
|
|
Value *InnerLimit = nullptr;
|
|
|
|
Value *OuterLimit = nullptr;
|
|
|
|
BinaryOperator *InnerIncrement = nullptr;
|
|
|
|
BinaryOperator *OuterIncrement = nullptr;
|
|
|
|
BranchInst *InnerBranch = nullptr;
|
|
|
|
BranchInst *OuterBranch = nullptr;
|
|
|
|
SmallPtrSet<Value *, 4> LinearIVUses;
|
|
|
|
SmallPtrSet<PHINode *, 4> InnerPHIsToTransform;
|
|
|
|
|
2020-11-21 15:13:36 +01:00
|
|
|
// Whether this holds the flatten info before or after widening.
|
|
|
|
bool Widened = false;
|
|
|
|
|
2020-11-09 15:33:52 +01:00
|
|
|
FlattenInfo(Loop *OL, Loop *IL) : OuterLoop(OL), InnerLoop(IL) {};
|
|
|
|
};
|
|
|
|
|
2020-09-30 12:16:22 +02:00
|
|
|
// Finds the induction variable, increment and limit for a simple loop that we
|
|
|
|
// can flatten.
|
|
|
|
static bool findLoopComponents(
|
|
|
|
Loop *L, SmallPtrSetImpl<Instruction *> &IterationInstructions,
|
|
|
|
PHINode *&InductionPHI, Value *&Limit, BinaryOperator *&Increment,
|
|
|
|
BranchInst *&BackBranch, ScalarEvolution *SE) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Finding components of loop: " << L->getName() << "\n");
|
|
|
|
|
|
|
|
if (!L->isLoopSimplifyForm()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Loop is not in normal form\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There must be exactly one exiting block, and it must be the same at the
|
|
|
|
// latch.
|
|
|
|
BasicBlock *Latch = L->getLoopLatch();
|
|
|
|
if (L->getExitingBlock() != Latch) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Exiting and latch block are different\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Latch block must end in a conditional branch.
|
|
|
|
BackBranch = dyn_cast<BranchInst>(Latch->getTerminator());
|
|
|
|
if (!BackBranch || !BackBranch->isConditional()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Could not find back-branch\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IterationInstructions.insert(BackBranch);
|
|
|
|
LLVM_DEBUG(dbgs() << "Found back branch: "; BackBranch->dump());
|
|
|
|
bool ContinueOnTrue = L->contains(BackBranch->getSuccessor(0));
|
|
|
|
|
|
|
|
// Find the induction PHI. If there is no induction PHI, we can't do the
|
|
|
|
// transformation. TODO: could other variables trigger this? Do we have to
|
|
|
|
// search for the best one?
|
|
|
|
InductionPHI = nullptr;
|
|
|
|
for (PHINode &PHI : L->getHeader()->phis()) {
|
|
|
|
InductionDescriptor ID;
|
|
|
|
if (InductionDescriptor::isInductionPHI(&PHI, L, SE, ID)) {
|
|
|
|
InductionPHI = &PHI;
|
|
|
|
LLVM_DEBUG(dbgs() << "Found induction PHI: "; InductionPHI->dump());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!InductionPHI) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Could not find induction PHI\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto IsValidPredicate = [&](ICmpInst::Predicate Pred) {
|
|
|
|
if (ContinueOnTrue)
|
|
|
|
return Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT;
|
|
|
|
else
|
|
|
|
return Pred == CmpInst::ICMP_EQ;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find Compare and make sure it is valid
|
|
|
|
ICmpInst *Compare = dyn_cast<ICmpInst>(BackBranch->getCondition());
|
|
|
|
if (!Compare || !IsValidPredicate(Compare->getUnsignedPredicate()) ||
|
|
|
|
Compare->hasNUsesOrMore(2)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Could not find valid comparison\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IterationInstructions.insert(Compare);
|
|
|
|
LLVM_DEBUG(dbgs() << "Found comparison: "; Compare->dump());
|
|
|
|
|
|
|
|
// Find increment and limit from the compare
|
|
|
|
Increment = nullptr;
|
|
|
|
if (match(Compare->getOperand(0),
|
|
|
|
m_c_Add(m_Specific(InductionPHI), m_ConstantInt<1>()))) {
|
|
|
|
Increment = dyn_cast<BinaryOperator>(Compare->getOperand(0));
|
|
|
|
Limit = Compare->getOperand(1);
|
|
|
|
} else if (Compare->getUnsignedPredicate() == CmpInst::ICMP_NE &&
|
|
|
|
match(Compare->getOperand(1),
|
|
|
|
m_c_Add(m_Specific(InductionPHI), m_ConstantInt<1>()))) {
|
|
|
|
Increment = dyn_cast<BinaryOperator>(Compare->getOperand(1));
|
|
|
|
Limit = Compare->getOperand(0);
|
|
|
|
}
|
|
|
|
if (!Increment || Increment->hasNUsesOrMore(3)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Cound not find valid increment\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IterationInstructions.insert(Increment);
|
|
|
|
LLVM_DEBUG(dbgs() << "Found increment: "; Increment->dump());
|
|
|
|
LLVM_DEBUG(dbgs() << "Found limit: "; Limit->dump());
|
|
|
|
|
|
|
|
assert(InductionPHI->getNumIncomingValues() == 2);
|
2021-03-24 11:07:34 +01:00
|
|
|
|
|
|
|
if (InductionPHI->getIncomingValueForBlock(Latch) != Increment) {
|
2021-03-24 11:21:13 +01:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Incoming value from latch is not the increment inst\n");
|
2021-03-24 11:07:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
auto *CI = dyn_cast<ConstantInt>(
|
|
|
|
InductionPHI->getIncomingValueForBlock(L->getLoopPreheader()));
|
|
|
|
if (!CI || !CI->isZero()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "PHI value is not zero: "; CI->dump());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Successfully found all loop components\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:58:12 +01:00
|
|
|
static bool checkPHIs(FlattenInfo &FI, const TargetTransformInfo *TTI) {
|
2020-09-30 12:16:22 +02:00
|
|
|
// All PHIs in the inner and outer headers must either be:
|
|
|
|
// - The induction PHI, which we are going to rewrite as one induction in
|
|
|
|
// the new loop. This is already checked by findLoopComponents.
|
|
|
|
// - An outer header PHI with all incoming values from outside the loop.
|
|
|
|
// LoopSimplify guarantees we have a pre-header, so we don't need to
|
|
|
|
// worry about that here.
|
|
|
|
// - Pairs of PHIs in the inner and outer headers, which implement a
|
|
|
|
// loop-carried dependency that will still be valid in the new loop. To
|
|
|
|
// be valid, this variable must be modified only in the inner loop.
|
|
|
|
|
|
|
|
// The set of PHI nodes in the outer loop header that we know will still be
|
|
|
|
// valid after the transformation. These will not need to be modified (with
|
|
|
|
// the exception of the induction variable), but we do need to check that
|
|
|
|
// there are no unsafe PHI nodes.
|
|
|
|
SmallPtrSet<PHINode *, 4> SafeOuterPHIs;
|
2020-11-09 15:33:52 +01:00
|
|
|
SafeOuterPHIs.insert(FI.OuterInductionPHI);
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
// Check that all PHI nodes in the inner loop header match one of the valid
|
|
|
|
// patterns.
|
2020-11-09 15:33:52 +01:00
|
|
|
for (PHINode &InnerPHI : FI.InnerLoop->getHeader()->phis()) {
|
2020-09-30 12:16:22 +02:00
|
|
|
// The induction PHIs break these rules, and that's OK because we treat
|
|
|
|
// them specially when doing the transformation.
|
2020-11-09 15:33:52 +01:00
|
|
|
if (&InnerPHI == FI.InnerInductionPHI)
|
2020-09-30 12:16:22 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Each inner loop PHI node must have two incoming values/blocks - one
|
|
|
|
// from the pre-header, and one from the latch.
|
|
|
|
assert(InnerPHI.getNumIncomingValues() == 2);
|
|
|
|
Value *PreHeaderValue =
|
2020-11-09 15:33:52 +01:00
|
|
|
InnerPHI.getIncomingValueForBlock(FI.InnerLoop->getLoopPreheader());
|
2020-09-30 12:16:22 +02:00
|
|
|
Value *LatchValue =
|
2020-11-09 15:33:52 +01:00
|
|
|
InnerPHI.getIncomingValueForBlock(FI.InnerLoop->getLoopLatch());
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
// The incoming value from the outer loop must be the PHI node in the
|
|
|
|
// outer loop header, with no modifications made in the top of the outer
|
|
|
|
// loop.
|
|
|
|
PHINode *OuterPHI = dyn_cast<PHINode>(PreHeaderValue);
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!OuterPHI || OuterPHI->getParent() != FI.OuterLoop->getHeader()) {
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "value modified in top of outer loop\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The other incoming value must come from the inner loop, without any
|
|
|
|
// modifications in the tail end of the outer loop. We are in LCSSA form,
|
|
|
|
// so this will actually be a PHI in the inner loop's exit block, which
|
|
|
|
// only uses values from inside the inner loop.
|
|
|
|
PHINode *LCSSAPHI = dyn_cast<PHINode>(
|
2020-11-09 15:33:52 +01:00
|
|
|
OuterPHI->getIncomingValueForBlock(FI.OuterLoop->getLoopLatch()));
|
2020-09-30 12:16:22 +02:00
|
|
|
if (!LCSSAPHI) {
|
|
|
|
LLVM_DEBUG(dbgs() << "could not find LCSSA PHI\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The value used by the LCSSA PHI must be the same one that the inner
|
|
|
|
// loop's PHI uses.
|
|
|
|
if (LCSSAPHI->hasConstantValue() != LatchValue) {
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "LCSSA PHI incoming value does not match latch value\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "PHI pair is safe:\n");
|
|
|
|
LLVM_DEBUG(dbgs() << " Inner: "; InnerPHI.dump());
|
|
|
|
LLVM_DEBUG(dbgs() << " Outer: "; OuterPHI->dump());
|
|
|
|
SafeOuterPHIs.insert(OuterPHI);
|
2020-11-09 15:33:52 +01:00
|
|
|
FI.InnerPHIsToTransform.insert(&InnerPHI);
|
2020-09-30 12:16:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 15:33:52 +01:00
|
|
|
for (PHINode &OuterPHI : FI.OuterLoop->getHeader()->phis()) {
|
2020-09-30 12:16:22 +02:00
|
|
|
if (!SafeOuterPHIs.count(&OuterPHI)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "found unsafe PHI in outer loop: "; OuterPHI.dump());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
LLVM_DEBUG(dbgs() << "checkPHIs: OK\n");
|
2020-09-30 12:16:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-03-24 10:58:12 +01:00
|
|
|
checkOuterLoopInsts(FlattenInfo &FI,
|
2020-09-30 12:16:22 +02:00
|
|
|
SmallPtrSetImpl<Instruction *> &IterationInstructions,
|
2020-11-09 15:33:52 +01:00
|
|
|
const TargetTransformInfo *TTI) {
|
2020-09-30 12:16:22 +02:00
|
|
|
// Check for instructions in the outer but not inner loop. If any of these
|
|
|
|
// have side-effects then this transformation is not legal, and if there is
|
|
|
|
// a significant amount of code here which can't be optimised out that it's
|
|
|
|
// not profitable (as these instructions would get executed for each
|
|
|
|
// iteration of the inner loop).
|
2021-02-05 13:01:21 +01:00
|
|
|
InstructionCost RepeatedInstrCost = 0;
|
2020-11-09 15:33:52 +01:00
|
|
|
for (auto *B : FI.OuterLoop->getBlocks()) {
|
|
|
|
if (FI.InnerLoop->contains(B))
|
2020-09-30 12:16:22 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto &I : *B) {
|
|
|
|
if (!isa<PHINode>(&I) && !I.isTerminator() &&
|
|
|
|
!isSafeToSpeculativelyExecute(&I)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Cannot flatten because instruction may have "
|
|
|
|
"side effects: ";
|
|
|
|
I.dump());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// The execution count of the outer loop's iteration instructions
|
|
|
|
// (increment, compare and branch) will be increased, but the
|
|
|
|
// equivalent instructions will be removed from the inner loop, so
|
|
|
|
// they make a net difference of zero.
|
|
|
|
if (IterationInstructions.count(&I))
|
|
|
|
continue;
|
|
|
|
// The uncoditional branch to the inner loop's header will turn into
|
|
|
|
// a fall-through, so adds no cost.
|
|
|
|
BranchInst *Br = dyn_cast<BranchInst>(&I);
|
|
|
|
if (Br && Br->isUnconditional() &&
|
2020-11-09 15:33:52 +01:00
|
|
|
Br->getSuccessor(0) == FI.InnerLoop->getHeader())
|
2020-09-30 12:16:22 +02:00
|
|
|
continue;
|
|
|
|
// Multiplies of the outer iteration variable and inner iteration
|
|
|
|
// count will be optimised out.
|
2020-11-09 15:33:52 +01:00
|
|
|
if (match(&I, m_c_Mul(m_Specific(FI.OuterInductionPHI),
|
|
|
|
m_Specific(FI.InnerLimit))))
|
2020-09-30 12:16:22 +02:00
|
|
|
continue;
|
2021-02-05 13:01:21 +01:00
|
|
|
InstructionCost Cost =
|
|
|
|
TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency);
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Cost " << Cost << ": "; I.dump());
|
|
|
|
RepeatedInstrCost += Cost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Cost of instructions that will be repeated: "
|
|
|
|
<< RepeatedInstrCost << "\n");
|
|
|
|
// Bail out if flattening the loops would cause instructions in the outer
|
|
|
|
// loop but not in the inner loop to be executed extra times.
|
2020-11-09 18:32:25 +01:00
|
|
|
if (RepeatedInstrCost > RepeatedInstructionThreshold) {
|
|
|
|
LLVM_DEBUG(dbgs() << "checkOuterLoopInsts: not profitable, bailing.\n");
|
2020-09-30 12:16:22 +02:00
|
|
|
return false;
|
2020-11-09 18:32:25 +01:00
|
|
|
}
|
2020-09-30 12:16:22 +02:00
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
LLVM_DEBUG(dbgs() << "checkOuterLoopInsts: OK\n");
|
2020-09-30 12:16:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:58:12 +01:00
|
|
|
static bool checkIVUsers(FlattenInfo &FI) {
|
2020-09-30 12:16:22 +02:00
|
|
|
// We require all uses of both induction variables to match this pattern:
|
|
|
|
//
|
|
|
|
// (OuterPHI * InnerLimit) + InnerPHI
|
|
|
|
//
|
|
|
|
// Any uses of the induction variables not matching that pattern would
|
|
|
|
// require a div/mod to reconstruct in the flattened loop, so the
|
|
|
|
// transformation wouldn't be profitable.
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
Value *InnerLimit = FI.InnerLimit;
|
2020-11-21 15:13:36 +01:00
|
|
|
if (FI.Widened &&
|
|
|
|
(isa<SExtInst>(InnerLimit) || isa<ZExtInst>(InnerLimit)))
|
|
|
|
InnerLimit = cast<Instruction>(InnerLimit)->getOperand(0);
|
2020-11-09 18:32:25 +01:00
|
|
|
|
2020-09-30 12:16:22 +02:00
|
|
|
// Check that all uses of the inner loop's induction variable match the
|
|
|
|
// expected pattern, recording the uses of the outer IV.
|
|
|
|
SmallPtrSet<Value *, 4> ValidOuterPHIUses;
|
2020-11-09 15:33:52 +01:00
|
|
|
for (User *U : FI.InnerInductionPHI->users()) {
|
|
|
|
if (U == FI.InnerIncrement)
|
2020-09-30 12:16:22 +02:00
|
|
|
continue;
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
// After widening the IVs, a trunc instruction might have been introduced, so
|
|
|
|
// look through truncs.
|
2020-11-21 15:13:36 +01:00
|
|
|
if (isa<TruncInst>(U)) {
|
2020-11-09 18:32:25 +01:00
|
|
|
if (!U->hasOneUse())
|
|
|
|
return false;
|
|
|
|
U = *U->user_begin();
|
|
|
|
}
|
|
|
|
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Found use of inner induction variable: "; U->dump());
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
Value *MatchedMul;
|
|
|
|
Value *MatchedItCount;
|
|
|
|
bool IsAdd = match(U, m_c_Add(m_Specific(FI.InnerInductionPHI),
|
|
|
|
m_Value(MatchedMul))) &&
|
|
|
|
match(MatchedMul, m_c_Mul(m_Specific(FI.OuterInductionPHI),
|
|
|
|
m_Value(MatchedItCount)));
|
|
|
|
|
|
|
|
// Matches the same pattern as above, except it also looks for truncs
|
|
|
|
// on the phi, which can be the result of widening the induction variables.
|
|
|
|
bool IsAddTrunc = match(U, m_c_Add(m_Trunc(m_Specific(FI.InnerInductionPHI)),
|
|
|
|
m_Value(MatchedMul))) &&
|
|
|
|
match(MatchedMul,
|
|
|
|
m_c_Mul(m_Trunc(m_Specific(FI.OuterInductionPHI)),
|
|
|
|
m_Value(MatchedItCount)));
|
|
|
|
|
|
|
|
if ((IsAdd || IsAddTrunc) && MatchedItCount == InnerLimit) {
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Use is optimisable\n");
|
|
|
|
ValidOuterPHIUses.insert(MatchedMul);
|
2020-11-09 15:33:52 +01:00
|
|
|
FI.LinearIVUses.insert(U);
|
2020-09-30 12:16:22 +02:00
|
|
|
} else {
|
|
|
|
LLVM_DEBUG(dbgs() << "Did not match expected pattern, bailing\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there are no uses of the outer IV other than the ones found
|
|
|
|
// as part of the pattern above.
|
2020-11-09 15:33:52 +01:00
|
|
|
for (User *U : FI.OuterInductionPHI->users()) {
|
|
|
|
if (U == FI.OuterIncrement)
|
2020-09-30 12:16:22 +02:00
|
|
|
continue;
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
auto IsValidOuterPHIUses = [&] (User *U) -> bool {
|
|
|
|
LLVM_DEBUG(dbgs() << "Found use of outer induction variable: "; U->dump());
|
|
|
|
if (!ValidOuterPHIUses.count(U)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Did not match expected pattern, bailing\n");
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Use is optimisable\n");
|
2020-11-09 18:32:25 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (auto *V = dyn_cast<TruncInst>(U)) {
|
|
|
|
for (auto *K : V->users()) {
|
|
|
|
if (!IsValidOuterPHIUses(K))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
2020-09-30 12:16:22 +02:00
|
|
|
}
|
2020-11-09 18:32:25 +01:00
|
|
|
|
|
|
|
if (!IsValidOuterPHIUses(U))
|
|
|
|
return false;
|
2020-09-30 12:16:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
LLVM_DEBUG(dbgs() << "checkIVUsers: OK\n";
|
|
|
|
dbgs() << "Found " << FI.LinearIVUses.size()
|
2020-09-30 12:16:22 +02:00
|
|
|
<< " value(s) that can be replaced:\n";
|
2020-11-09 15:33:52 +01:00
|
|
|
for (Value *V : FI.LinearIVUses) {
|
2020-09-30 12:16:22 +02:00
|
|
|
dbgs() << " ";
|
|
|
|
V->dump();
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an OverflowResult dependant on if overflow of the multiplication of
|
|
|
|
// InnerLimit and OuterLimit can be assumed not to happen.
|
2021-03-24 10:58:12 +01:00
|
|
|
static OverflowResult checkOverflow(FlattenInfo &FI, DominatorTree *DT,
|
|
|
|
AssumptionCache *AC) {
|
2020-11-09 15:33:52 +01:00
|
|
|
Function *F = FI.OuterLoop->getHeader()->getParent();
|
2020-09-30 12:16:22 +02:00
|
|
|
const DataLayout &DL = F->getParent()->getDataLayout();
|
|
|
|
|
|
|
|
// For debugging/testing.
|
|
|
|
if (AssumeNoOverflow)
|
|
|
|
return OverflowResult::NeverOverflows;
|
|
|
|
|
|
|
|
// Check if the multiply could not overflow due to known ranges of the
|
|
|
|
// input values.
|
|
|
|
OverflowResult OR = computeOverflowForUnsignedMul(
|
2020-11-09 15:33:52 +01:00
|
|
|
FI.InnerLimit, FI.OuterLimit, DL, AC,
|
|
|
|
FI.OuterLoop->getLoopPreheader()->getTerminator(), DT);
|
2020-09-30 12:16:22 +02:00
|
|
|
if (OR != OverflowResult::MayOverflow)
|
|
|
|
return OR;
|
|
|
|
|
2020-11-09 15:33:52 +01:00
|
|
|
for (Value *V : FI.LinearIVUses) {
|
2020-09-30 12:16:22 +02:00
|
|
|
for (Value *U : V->users()) {
|
|
|
|
if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) {
|
|
|
|
// The IV is used as the operand of a GEP, and the IV is at least as
|
|
|
|
// wide as the address space of the GEP. In this case, the GEP would
|
|
|
|
// wrap around the address space before the IV increment wraps, which
|
|
|
|
// would be UB.
|
|
|
|
if (GEP->isInBounds() &&
|
|
|
|
V->getType()->getIntegerBitWidth() >=
|
|
|
|
DL.getPointerTypeSizeInBits(GEP->getType())) {
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "use of linear IV would be UB if overflow occurred: ";
|
|
|
|
GEP->dump());
|
|
|
|
return OverflowResult::NeverOverflows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OverflowResult::MayOverflow;
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:58:12 +01:00
|
|
|
static bool CanFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
|
|
|
|
ScalarEvolution *SE, AssumptionCache *AC,
|
|
|
|
const TargetTransformInfo *TTI) {
|
2020-09-30 12:16:22 +02:00
|
|
|
SmallPtrSet<Instruction *, 8> IterationInstructions;
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!findLoopComponents(FI.InnerLoop, IterationInstructions, FI.InnerInductionPHI,
|
|
|
|
FI.InnerLimit, FI.InnerIncrement, FI.InnerBranch, SE))
|
2020-09-30 12:16:22 +02:00
|
|
|
return false;
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!findLoopComponents(FI.OuterLoop, IterationInstructions, FI.OuterInductionPHI,
|
|
|
|
FI.OuterLimit, FI.OuterIncrement, FI.OuterBranch, SE))
|
2020-09-30 12:16:22 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Both of the loop limit values must be invariant in the outer loop
|
|
|
|
// (non-instructions are all inherently invariant).
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!FI.OuterLoop->isLoopInvariant(FI.InnerLimit)) {
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "inner loop limit not invariant\n");
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!FI.OuterLoop->isLoopInvariant(FI.OuterLimit)) {
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "outer loop limit not invariant\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!checkPHIs(FI, TTI))
|
2020-09-30 12:16:22 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: it should be possible to handle different types correctly.
|
2020-11-09 15:33:52 +01:00
|
|
|
if (FI.InnerInductionPHI->getType() != FI.OuterInductionPHI->getType())
|
2020-09-30 12:16:22 +02:00
|
|
|
return false;
|
|
|
|
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!checkOuterLoopInsts(FI, IterationInstructions, TTI))
|
2020-09-30 12:16:22 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the values in the loop that can be replaced with the linearized
|
|
|
|
// induction variable, and check that there are no other uses of the inner
|
|
|
|
// or outer induction variable. If there were, we could still do this
|
|
|
|
// transformation, but we'd have to insert a div/mod to calculate the
|
|
|
|
// original IVs, so it wouldn't be profitable.
|
2020-11-09 15:33:52 +01:00
|
|
|
if (!checkIVUsers(FI))
|
2020-09-30 12:16:22 +02:00
|
|
|
return false;
|
|
|
|
|
2020-11-09 18:32:25 +01:00
|
|
|
LLVM_DEBUG(dbgs() << "CanFlattenLoopPair: OK\n");
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-30 12:16:22 +02:00
|
|
|
|
2021-03-24 10:58:12 +01:00
|
|
|
static bool DoFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
|
|
|
|
ScalarEvolution *SE, AssumptionCache *AC,
|
2020-11-09 18:32:25 +01:00
|
|
|
const TargetTransformInfo *TTI) {
|
|
|
|
Function *F = FI.OuterLoop->getHeader()->getParent();
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Checks all passed, doing the transformation\n");
|
|
|
|
{
|
|
|
|
using namespace ore;
|
2020-11-09 15:33:52 +01:00
|
|
|
OptimizationRemark Remark(DEBUG_TYPE, "Flattened", FI.InnerLoop->getStartLoc(),
|
|
|
|
FI.InnerLoop->getHeader());
|
2020-09-30 12:16:22 +02:00
|
|
|
OptimizationRemarkEmitter ORE(F);
|
|
|
|
Remark << "Flattened into outer loop";
|
|
|
|
ORE.emit(Remark);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *NewTripCount =
|
2020-11-09 15:33:52 +01:00
|
|
|
BinaryOperator::CreateMul(FI.InnerLimit, FI.OuterLimit, "flatten.tripcount",
|
|
|
|
FI.OuterLoop->getLoopPreheader()->getTerminator());
|
2020-09-30 12:16:22 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Created new trip count in preheader: ";
|
|
|
|
NewTripCount->dump());
|
|
|
|
|
|
|
|
// Fix up PHI nodes that take values from the inner loop back-edge, which
|
|
|
|
// we are about to remove.
|
2020-11-09 15:33:52 +01:00
|
|
|
FI.InnerInductionPHI->removeIncomingValue(FI.InnerLoop->getLoopLatch());
|
2020-11-09 18:32:25 +01:00
|
|
|
|
|
|
|
// The old Phi will be optimised away later, but for now we can't leave
|
|
|
|
// leave it in an invalid state, so are updating them too.
|
2020-11-09 15:33:52 +01:00
|
|
|
for (PHINode *PHI : FI.InnerPHIsToTransform)
|
|
|
|
PHI->removeIncomingValue(FI.InnerLoop->getLoopLatch());
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
// Modify the trip count of the outer loop to be the product of the two
|
|
|
|
// trip counts.
|
2020-11-09 15:33:52 +01:00
|
|
|
cast<User>(FI.OuterBranch->getCondition())->setOperand(1, NewTripCount);
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
// Replace the inner loop backedge with an unconditional branch to the exit.
|
2020-11-09 15:33:52 +01:00
|
|
|
BasicBlock *InnerExitBlock = FI.InnerLoop->getExitBlock();
|
|
|
|
BasicBlock *InnerExitingBlock = FI.InnerLoop->getExitingBlock();
|
2020-09-30 12:16:22 +02:00
|
|
|
InnerExitingBlock->getTerminator()->eraseFromParent();
|
|
|
|
BranchInst::Create(InnerExitBlock, InnerExitingBlock);
|
2020-11-09 15:33:52 +01:00
|
|
|
DT->deleteEdge(InnerExitingBlock, FI.InnerLoop->getHeader());
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
// Replace all uses of the polynomial calculated from the two induction
|
|
|
|
// variables with the one new one.
|
2020-11-21 15:13:36 +01:00
|
|
|
IRBuilder<> Builder(FI.OuterInductionPHI->getParent()->getTerminator());
|
2020-11-09 18:32:25 +01:00
|
|
|
for (Value *V : FI.LinearIVUses) {
|
2020-11-21 15:13:36 +01:00
|
|
|
Value *OuterValue = FI.OuterInductionPHI;
|
|
|
|
if (FI.Widened)
|
|
|
|
OuterValue = Builder.CreateTrunc(FI.OuterInductionPHI, V->getType(),
|
|
|
|
"flatten.trunciv");
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Replacing: "; V->dump();
|
|
|
|
dbgs() << "with: "; OuterValue->dump());
|
|
|
|
V->replaceAllUsesWith(OuterValue);
|
2020-11-09 18:32:25 +01:00
|
|
|
}
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
// Tell LoopInfo, SCEV and the pass manager that the inner loop has been
|
|
|
|
// deleted, and any information that have about the outer loop invalidated.
|
2020-11-09 15:33:52 +01:00
|
|
|
SE->forgetLoop(FI.OuterLoop);
|
|
|
|
SE->forgetLoop(FI.InnerLoop);
|
|
|
|
LI->erase(FI.InnerLoop);
|
2020-09-30 12:16:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:58:12 +01:00
|
|
|
static bool CanWidenIV(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
|
|
|
|
ScalarEvolution *SE, AssumptionCache *AC,
|
|
|
|
const TargetTransformInfo *TTI) {
|
2020-11-09 18:32:25 +01:00
|
|
|
if (!WidenIV) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Widening the IVs is disabled\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Try widening the IVs\n");
|
|
|
|
Module *M = FI.InnerLoop->getHeader()->getParent()->getParent();
|
|
|
|
auto &DL = M->getDataLayout();
|
|
|
|
auto *InnerType = FI.InnerInductionPHI->getType();
|
|
|
|
auto *OuterType = FI.OuterInductionPHI->getType();
|
|
|
|
unsigned MaxLegalSize = DL.getLargestLegalIntTypeSizeInBits();
|
|
|
|
auto *MaxLegalType = DL.getLargestLegalIntType(M->getContext());
|
|
|
|
|
|
|
|
// If both induction types are less than the maximum legal integer width,
|
|
|
|
// promote both to the widest type available so we know calculating
|
|
|
|
// (OuterLimit * InnerLimit) as the new trip count is safe.
|
|
|
|
if (InnerType != OuterType ||
|
|
|
|
InnerType->getScalarSizeInBits() >= MaxLegalSize ||
|
|
|
|
MaxLegalType->getScalarSizeInBits() < InnerType->getScalarSizeInBits() * 2) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Can't widen the IV\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCEVExpander Rewriter(*SE, DL, "loopflatten");
|
|
|
|
SmallVector<WideIVInfo, 2> WideIVs;
|
|
|
|
SmallVector<WeakTrackingVH, 4> DeadInsts;
|
|
|
|
WideIVs.push_back( {FI.InnerInductionPHI, MaxLegalType, false });
|
|
|
|
WideIVs.push_back( {FI.OuterInductionPHI, MaxLegalType, false });
|
2021-04-06 13:24:04 +02:00
|
|
|
unsigned ElimExt = 0;
|
|
|
|
unsigned Widened = 0;
|
2020-11-09 18:32:25 +01:00
|
|
|
|
2021-04-06 13:24:04 +02:00
|
|
|
for (const auto &WideIV : WideIVs) {
|
|
|
|
PHINode *WidePhi = createWideIV(WideIV, LI, SE, Rewriter, DT, DeadInsts,
|
2020-11-09 18:32:25 +01:00
|
|
|
ElimExt, Widened, true /* HasGuards */,
|
|
|
|
true /* UsePostIncrementRanges */);
|
|
|
|
if (!WidePhi)
|
|
|
|
return false;
|
|
|
|
LLVM_DEBUG(dbgs() << "Created wide phi: "; WidePhi->dump());
|
2021-04-06 13:24:04 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Deleting old phi: "; WideIV.NarrowIV->dump());
|
|
|
|
RecursivelyDeleteDeadPHINode(WideIV.NarrowIV);
|
2020-11-09 18:32:25 +01:00
|
|
|
}
|
|
|
|
// After widening, rediscover all the loop components.
|
2021-04-06 13:24:04 +02:00
|
|
|
assert(Widened && "Widened IV expected");
|
2020-11-21 15:13:36 +01:00
|
|
|
FI.Widened = true;
|
2020-11-09 18:32:25 +01:00
|
|
|
return CanFlattenLoopPair(FI, DT, LI, SE, AC, TTI);
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:58:12 +01:00
|
|
|
static bool FlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
|
|
|
|
ScalarEvolution *SE, AssumptionCache *AC,
|
2020-11-09 18:32:25 +01:00
|
|
|
const TargetTransformInfo *TTI) {
|
2020-11-16 11:51:39 +01:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "Loop flattening running on outer loop "
|
|
|
|
<< FI.OuterLoop->getHeader()->getName() << " and inner loop "
|
|
|
|
<< FI.InnerLoop->getHeader()->getName() << " in "
|
|
|
|
<< FI.OuterLoop->getHeader()->getParent()->getName() << "\n");
|
2020-11-09 18:32:25 +01:00
|
|
|
|
|
|
|
if (!CanFlattenLoopPair(FI, DT, LI, SE, AC, TTI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if we can widen the induction variables to avoid overflow checks.
|
|
|
|
if (CanWidenIV(FI, DT, LI, SE, AC, TTI))
|
|
|
|
return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI);
|
|
|
|
|
|
|
|
// Check if the new iteration variable might overflow. In this case, we
|
|
|
|
// need to version the loop, and select the original version at runtime if
|
|
|
|
// the iteration space is too large.
|
|
|
|
// TODO: We currently don't version the loop.
|
|
|
|
OverflowResult OR = checkOverflow(FI, DT, AC);
|
|
|
|
if (OR == OverflowResult::AlwaysOverflowsHigh ||
|
|
|
|
OR == OverflowResult::AlwaysOverflowsLow) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Multiply would always overflow, so not profitable\n");
|
|
|
|
return false;
|
|
|
|
} else if (OR == OverflowResult::MayOverflow) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Multiply might overflow, not flattening\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Multiply cannot overflow, modifying loop in-place\n");
|
|
|
|
return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI);
|
|
|
|
}
|
|
|
|
|
2020-11-09 16:59:50 +01:00
|
|
|
bool Flatten(DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE,
|
|
|
|
AssumptionCache *AC, TargetTransformInfo *TTI) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto *InnerLoop : LI->getLoopsInPreorder()) {
|
|
|
|
auto *OuterLoop = InnerLoop->getParentLoop();
|
|
|
|
if (!OuterLoop)
|
|
|
|
continue;
|
2021-03-24 10:58:12 +01:00
|
|
|
FlattenInfo FI(OuterLoop, InnerLoop);
|
2020-11-09 16:59:50 +01:00
|
|
|
Changed |= FlattenLoopPair(FI, DT, LI, SE, AC, TTI);
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
2020-09-30 12:16:22 +02:00
|
|
|
|
2020-11-09 16:59:50 +01:00
|
|
|
PreservedAnalyses LoopFlattenPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
|
|
|
|
auto *LI = &AM.getResult<LoopAnalysis>(F);
|
|
|
|
auto *SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
|
|
|
|
auto *AC = &AM.getResult<AssumptionAnalysis>(F);
|
|
|
|
auto *TTI = &AM.getResult<TargetIRAnalysis>(F);
|
|
|
|
|
|
|
|
if (!Flatten(DT, LI, SE, AC, TTI))
|
2020-09-30 12:16:22 +02:00
|
|
|
return PreservedAnalyses::all();
|
2020-11-09 16:59:50 +01:00
|
|
|
|
2021-04-01 10:33:00 +02:00
|
|
|
return PreservedAnalyses::none();
|
2020-09-30 12:16:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2020-11-09 16:59:50 +01:00
|
|
|
class LoopFlattenLegacyPass : public FunctionPass {
|
2020-09-30 12:16:22 +02:00
|
|
|
public:
|
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2020-11-09 16:59:50 +01:00
|
|
|
LoopFlattenLegacyPass() : FunctionPass(ID) {
|
2020-09-30 12:16:22 +02:00
|
|
|
initializeLoopFlattenLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possibly flatten loop L into its child.
|
2020-11-09 16:59:50 +01:00
|
|
|
bool runOnFunction(Function &F) override;
|
2020-09-30 12:16:22 +02:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
getLoopAnalysisUsage(AU);
|
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
|
|
|
AU.addPreserved<TargetTransformInfoWrapperPass>();
|
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
|
|
|
AU.addPreserved<AssumptionCacheTracker>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
char LoopFlattenLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LoopFlattenLegacyPass, "loop-flatten", "Flattens loops",
|
|
|
|
false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
|
|
|
INITIALIZE_PASS_END(LoopFlattenLegacyPass, "loop-flatten", "Flattens loops",
|
|
|
|
false, false)
|
|
|
|
|
2020-11-09 16:59:50 +01:00
|
|
|
FunctionPass *llvm::createLoopFlattenPass() { return new LoopFlattenLegacyPass(); }
|
2020-09-30 12:16:22 +02:00
|
|
|
|
2020-11-09 16:59:50 +01:00
|
|
|
bool LoopFlattenLegacyPass::runOnFunction(Function &F) {
|
2020-09-30 12:16:22 +02:00
|
|
|
ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
|
|
|
LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
|
|
|
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
|
|
|
DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
|
|
|
|
auto &TTIP = getAnalysis<TargetTransformInfoWrapperPass>();
|
2020-11-09 16:59:50 +01:00
|
|
|
auto *TTI = &TTIP.getTTI(F);
|
|
|
|
auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
|
|
|
return Flatten(DT, LI, SE, AC, TTI);
|
2020-09-30 12:16:22 +02:00
|
|
|
}
|