[LoopFlatten] FlattenInfo bookkeeping. NFC.

Introduce struct FlattenInfo to group some of the bookkeeping. Besides this
being a bit of a clean-up, it is a prep step for next additions (D90640). I
could take things a bit further, but thought this was a good first step also
not to make this change too large.

Differential Revision: https://reviews.llvm.org/D90408
This commit is contained in:
Sjoerd Meijer 2020-11-09 14:33:52 +00:00
parent 65c489852c
commit e2dcea4489
1 changed files with 90 additions and 87 deletions

View File

@ -62,6 +62,23 @@ static cl::opt<bool>
cl::desc("Assume that the product of the two iteration " cl::desc("Assume that the product of the two iteration "
"limits will never overflow")); "limits will never overflow"));
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;
FlattenInfo(Loop *OL, Loop *IL) : OuterLoop(OL), InnerLoop(IL) {};
};
// Finds the induction variable, increment and limit for a simple loop that we // Finds the induction variable, increment and limit for a simple loop that we
// can flatten. // can flatten.
static bool findLoopComponents( static bool findLoopComponents(
@ -161,10 +178,8 @@ static bool findLoopComponents(
return true; return true;
} }
static bool checkPHIs(Loop *OuterLoop, Loop *InnerLoop, static bool checkPHIs(struct FlattenInfo &FI,
SmallPtrSetImpl<PHINode *> &InnerPHIsToTransform, const TargetTransformInfo *TTI) {
PHINode *InnerInductionPHI, PHINode *OuterInductionPHI,
TargetTransformInfo *TTI) {
// All PHIs in the inner and outer headers must either be: // 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 induction PHI, which we are going to rewrite as one induction in
// the new loop. This is already checked by findLoopComponents. // the new loop. This is already checked by findLoopComponents.
@ -180,29 +195,29 @@ static bool checkPHIs(Loop *OuterLoop, Loop *InnerLoop,
// the exception of the induction variable), but we do need to check that // the exception of the induction variable), but we do need to check that
// there are no unsafe PHI nodes. // there are no unsafe PHI nodes.
SmallPtrSet<PHINode *, 4> SafeOuterPHIs; SmallPtrSet<PHINode *, 4> SafeOuterPHIs;
SafeOuterPHIs.insert(OuterInductionPHI); SafeOuterPHIs.insert(FI.OuterInductionPHI);
// Check that all PHI nodes in the inner loop header match one of the valid // Check that all PHI nodes in the inner loop header match one of the valid
// patterns. // patterns.
for (PHINode &InnerPHI : InnerLoop->getHeader()->phis()) { for (PHINode &InnerPHI : FI.InnerLoop->getHeader()->phis()) {
// The induction PHIs break these rules, and that's OK because we treat // The induction PHIs break these rules, and that's OK because we treat
// them specially when doing the transformation. // them specially when doing the transformation.
if (&InnerPHI == InnerInductionPHI) if (&InnerPHI == FI.InnerInductionPHI)
continue; continue;
// Each inner loop PHI node must have two incoming values/blocks - one // Each inner loop PHI node must have two incoming values/blocks - one
// from the pre-header, and one from the latch. // from the pre-header, and one from the latch.
assert(InnerPHI.getNumIncomingValues() == 2); assert(InnerPHI.getNumIncomingValues() == 2);
Value *PreHeaderValue = Value *PreHeaderValue =
InnerPHI.getIncomingValueForBlock(InnerLoop->getLoopPreheader()); InnerPHI.getIncomingValueForBlock(FI.InnerLoop->getLoopPreheader());
Value *LatchValue = Value *LatchValue =
InnerPHI.getIncomingValueForBlock(InnerLoop->getLoopLatch()); InnerPHI.getIncomingValueForBlock(FI.InnerLoop->getLoopLatch());
// The incoming value from the outer loop must be the PHI node in the // 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 // outer loop header, with no modifications made in the top of the outer
// loop. // loop.
PHINode *OuterPHI = dyn_cast<PHINode>(PreHeaderValue); PHINode *OuterPHI = dyn_cast<PHINode>(PreHeaderValue);
if (!OuterPHI || OuterPHI->getParent() != OuterLoop->getHeader()) { if (!OuterPHI || OuterPHI->getParent() != FI.OuterLoop->getHeader()) {
LLVM_DEBUG(dbgs() << "value modified in top of outer loop\n"); LLVM_DEBUG(dbgs() << "value modified in top of outer loop\n");
return false; return false;
} }
@ -212,7 +227,7 @@ static bool checkPHIs(Loop *OuterLoop, Loop *InnerLoop,
// so this will actually be a PHI in the inner loop's exit block, which // so this will actually be a PHI in the inner loop's exit block, which
// only uses values from inside the inner loop. // only uses values from inside the inner loop.
PHINode *LCSSAPHI = dyn_cast<PHINode>( PHINode *LCSSAPHI = dyn_cast<PHINode>(
OuterPHI->getIncomingValueForBlock(OuterLoop->getLoopLatch())); OuterPHI->getIncomingValueForBlock(FI.OuterLoop->getLoopLatch()));
if (!LCSSAPHI) { if (!LCSSAPHI) {
LLVM_DEBUG(dbgs() << "could not find LCSSA PHI\n"); LLVM_DEBUG(dbgs() << "could not find LCSSA PHI\n");
return false; return false;
@ -230,10 +245,10 @@ static bool checkPHIs(Loop *OuterLoop, Loop *InnerLoop,
LLVM_DEBUG(dbgs() << " Inner: "; InnerPHI.dump()); LLVM_DEBUG(dbgs() << " Inner: "; InnerPHI.dump());
LLVM_DEBUG(dbgs() << " Outer: "; OuterPHI->dump()); LLVM_DEBUG(dbgs() << " Outer: "; OuterPHI->dump());
SafeOuterPHIs.insert(OuterPHI); SafeOuterPHIs.insert(OuterPHI);
InnerPHIsToTransform.insert(&InnerPHI); FI.InnerPHIsToTransform.insert(&InnerPHI);
} }
for (PHINode &OuterPHI : OuterLoop->getHeader()->phis()) { for (PHINode &OuterPHI : FI.OuterLoop->getHeader()->phis()) {
if (!SafeOuterPHIs.count(&OuterPHI)) { if (!SafeOuterPHIs.count(&OuterPHI)) {
LLVM_DEBUG(dbgs() << "found unsafe PHI in outer loop: "; OuterPHI.dump()); LLVM_DEBUG(dbgs() << "found unsafe PHI in outer loop: "; OuterPHI.dump());
return false; return false;
@ -244,18 +259,17 @@ static bool checkPHIs(Loop *OuterLoop, Loop *InnerLoop,
} }
static bool static bool
checkOuterLoopInsts(Loop *OuterLoop, Loop *InnerLoop, checkOuterLoopInsts(struct FlattenInfo &FI,
SmallPtrSetImpl<Instruction *> &IterationInstructions, SmallPtrSetImpl<Instruction *> &IterationInstructions,
Value *InnerLimit, PHINode *OuterPHI, const TargetTransformInfo *TTI) {
TargetTransformInfo *TTI) {
// Check for instructions in the outer but not inner loop. If any of these // 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 // 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 // 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 // not profitable (as these instructions would get executed for each
// iteration of the inner loop). // iteration of the inner loop).
unsigned RepeatedInstrCost = 0; unsigned RepeatedInstrCost = 0;
for (auto *B : OuterLoop->getBlocks()) { for (auto *B : FI.OuterLoop->getBlocks()) {
if (InnerLoop->contains(B)) if (FI.InnerLoop->contains(B))
continue; continue;
for (auto &I : *B) { for (auto &I : *B) {
@ -276,11 +290,12 @@ checkOuterLoopInsts(Loop *OuterLoop, Loop *InnerLoop,
// a fall-through, so adds no cost. // a fall-through, so adds no cost.
BranchInst *Br = dyn_cast<BranchInst>(&I); BranchInst *Br = dyn_cast<BranchInst>(&I);
if (Br && Br->isUnconditional() && if (Br && Br->isUnconditional() &&
Br->getSuccessor(0) == InnerLoop->getHeader()) Br->getSuccessor(0) == FI.InnerLoop->getHeader())
continue; continue;
// Multiplies of the outer iteration variable and inner iteration // Multiplies of the outer iteration variable and inner iteration
// count will be optimised out. // count will be optimised out.
if (match(&I, m_c_Mul(m_Specific(OuterPHI), m_Specific(InnerLimit)))) if (match(&I, m_c_Mul(m_Specific(FI.OuterInductionPHI),
m_Specific(FI.InnerLimit))))
continue; continue;
int Cost = TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency); int Cost = TTI->getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency);
LLVM_DEBUG(dbgs() << "Cost " << Cost << ": "; I.dump()); LLVM_DEBUG(dbgs() << "Cost " << Cost << ": "; I.dump());
@ -298,10 +313,7 @@ checkOuterLoopInsts(Loop *OuterLoop, Loop *InnerLoop,
return true; return true;
} }
static bool checkIVUsers(PHINode *InnerPHI, PHINode *OuterPHI, static bool checkIVUsers(struct FlattenInfo &FI) {
BinaryOperator *InnerIncrement,
BinaryOperator *OuterIncrement, Value *InnerLimit,
SmallPtrSetImpl<Value *> &LinearIVUses) {
// We require all uses of both induction variables to match this pattern: // We require all uses of both induction variables to match this pattern:
// //
// (OuterPHI * InnerLimit) + InnerPHI // (OuterPHI * InnerLimit) + InnerPHI
@ -313,20 +325,22 @@ static bool checkIVUsers(PHINode *InnerPHI, PHINode *OuterPHI,
// Check that all uses of the inner loop's induction variable match the // Check that all uses of the inner loop's induction variable match the
// expected pattern, recording the uses of the outer IV. // expected pattern, recording the uses of the outer IV.
SmallPtrSet<Value *, 4> ValidOuterPHIUses; SmallPtrSet<Value *, 4> ValidOuterPHIUses;
for (User *U : InnerPHI->users()) { for (User *U : FI.InnerInductionPHI->users()) {
if (U == InnerIncrement) if (U == FI.InnerIncrement)
continue; continue;
LLVM_DEBUG(dbgs() << "Found use of inner induction variable: "; U->dump()); LLVM_DEBUG(dbgs() << "Found use of inner induction variable: "; U->dump());
Value *MatchedMul, *MatchedItCount; Value *MatchedMul, *MatchedItCount;
if (match(U, m_c_Add(m_Specific(InnerPHI), m_Value(MatchedMul))) && if (match(U, m_c_Add(m_Specific(FI.InnerInductionPHI),
m_Value(MatchedMul))) &&
match(MatchedMul, match(MatchedMul,
m_c_Mul(m_Specific(OuterPHI), m_Value(MatchedItCount))) && m_c_Mul(m_Specific(FI.OuterInductionPHI),
MatchedItCount == InnerLimit) { m_Value(MatchedItCount))) &&
MatchedItCount == FI.InnerLimit) {
LLVM_DEBUG(dbgs() << "Use is optimisable\n"); LLVM_DEBUG(dbgs() << "Use is optimisable\n");
ValidOuterPHIUses.insert(MatchedMul); ValidOuterPHIUses.insert(MatchedMul);
LinearIVUses.insert(U); FI.LinearIVUses.insert(U);
} else { } else {
LLVM_DEBUG(dbgs() << "Did not match expected pattern, bailing\n"); LLVM_DEBUG(dbgs() << "Did not match expected pattern, bailing\n");
return false; return false;
@ -335,8 +349,8 @@ static bool checkIVUsers(PHINode *InnerPHI, PHINode *OuterPHI,
// Check that there are no uses of the outer IV other than the ones found // Check that there are no uses of the outer IV other than the ones found
// as part of the pattern above. // as part of the pattern above.
for (User *U : OuterPHI->users()) { for (User *U : FI.OuterInductionPHI->users()) {
if (U == OuterIncrement) if (U == FI.OuterIncrement)
continue; continue;
LLVM_DEBUG(dbgs() << "Found use of outer induction variable: "; U->dump()); LLVM_DEBUG(dbgs() << "Found use of outer induction variable: "; U->dump());
@ -349,9 +363,9 @@ static bool checkIVUsers(PHINode *InnerPHI, PHINode *OuterPHI,
} }
} }
LLVM_DEBUG(dbgs() << "Found " << LinearIVUses.size() LLVM_DEBUG(dbgs() << "Found " << FI.LinearIVUses.size()
<< " value(s) that can be replaced:\n"; << " value(s) that can be replaced:\n";
for (Value *V : LinearIVUses) { for (Value *V : FI.LinearIVUses) {
dbgs() << " "; dbgs() << " ";
V->dump(); V->dump();
}); });
@ -361,11 +375,9 @@ static bool checkIVUsers(PHINode *InnerPHI, PHINode *OuterPHI,
// Return an OverflowResult dependant on if overflow of the multiplication of // Return an OverflowResult dependant on if overflow of the multiplication of
// InnerLimit and OuterLimit can be assumed not to happen. // InnerLimit and OuterLimit can be assumed not to happen.
static OverflowResult checkOverflow(Loop *OuterLoop, Value *InnerLimit, static OverflowResult checkOverflow(struct FlattenInfo &FI,
Value *OuterLimit,
SmallPtrSetImpl<Value *> &LinearIVUses,
DominatorTree *DT, AssumptionCache *AC) { DominatorTree *DT, AssumptionCache *AC) {
Function *F = OuterLoop->getHeader()->getParent(); Function *F = FI.OuterLoop->getHeader()->getParent();
const DataLayout &DL = F->getParent()->getDataLayout(); const DataLayout &DL = F->getParent()->getDataLayout();
// For debugging/testing. // For debugging/testing.
@ -375,12 +387,12 @@ static OverflowResult checkOverflow(Loop *OuterLoop, Value *InnerLimit,
// Check if the multiply could not overflow due to known ranges of the // Check if the multiply could not overflow due to known ranges of the
// input values. // input values.
OverflowResult OR = computeOverflowForUnsignedMul( OverflowResult OR = computeOverflowForUnsignedMul(
InnerLimit, OuterLimit, DL, AC, FI.InnerLimit, FI.OuterLimit, DL, AC,
OuterLoop->getLoopPreheader()->getTerminator(), DT); FI.OuterLoop->getLoopPreheader()->getTerminator(), DT);
if (OR != OverflowResult::MayOverflow) if (OR != OverflowResult::MayOverflow)
return OR; return OR;
for (Value *V : LinearIVUses) { for (Value *V : FI.LinearIVUses) {
for (Value *U : V->users()) { for (Value *U : V->users()) {
if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) { if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) {
// The IV is used as the operand of a GEP, and the IV is at least as // The IV is used as the operand of a GEP, and the IV is at least as
@ -402,53 +414,45 @@ static OverflowResult checkOverflow(Loop *OuterLoop, Value *InnerLimit,
return OverflowResult::MayOverflow; return OverflowResult::MayOverflow;
} }
static bool FlattenLoopPair(Loop *OuterLoop, Loop *InnerLoop, DominatorTree *DT, static bool FlattenLoopPair(struct FlattenInfo &FI, DominatorTree *DT,
LoopInfo *LI, ScalarEvolution *SE, LoopInfo *LI, ScalarEvolution *SE,
AssumptionCache *AC, TargetTransformInfo *TTI, AssumptionCache *AC, const TargetTransformInfo *TTI,
std::function<void(Loop *)> markLoopAsDeleted) { std::function<void(Loop *)> markLoopAsDeleted) {
Function *F = OuterLoop->getHeader()->getParent(); Function *F = FI.OuterLoop->getHeader()->getParent();
LLVM_DEBUG(dbgs() << "Loop flattening running on outer loop " LLVM_DEBUG(dbgs() << "Loop flattening running on outer loop "
<< OuterLoop->getHeader()->getName() << " and inner loop " << FI.OuterLoop->getHeader()->getName() << " and inner loop "
<< InnerLoop->getHeader()->getName() << " in " << FI.InnerLoop->getHeader()->getName() << " in "
<< F->getName() << "\n"); << F->getName() << "\n");
SmallPtrSet<Instruction *, 8> IterationInstructions; SmallPtrSet<Instruction *, 8> IterationInstructions;
PHINode *InnerInductionPHI, *OuterInductionPHI; if (!findLoopComponents(FI.InnerLoop, IterationInstructions, FI.InnerInductionPHI,
Value *InnerLimit, *OuterLimit; FI.InnerLimit, FI.InnerIncrement, FI.InnerBranch, SE))
BinaryOperator *InnerIncrement, *OuterIncrement;
BranchInst *InnerBranch, *OuterBranch;
if (!findLoopComponents(InnerLoop, IterationInstructions, InnerInductionPHI,
InnerLimit, InnerIncrement, InnerBranch, SE))
return false; return false;
if (!findLoopComponents(OuterLoop, IterationInstructions, OuterInductionPHI, if (!findLoopComponents(FI.OuterLoop, IterationInstructions, FI.OuterInductionPHI,
OuterLimit, OuterIncrement, OuterBranch, SE)) FI.OuterLimit, FI.OuterIncrement, FI.OuterBranch, SE))
return false; return false;
// Both of the loop limit values must be invariant in the outer loop // Both of the loop limit values must be invariant in the outer loop
// (non-instructions are all inherently invariant). // (non-instructions are all inherently invariant).
if (!OuterLoop->isLoopInvariant(InnerLimit)) { if (!FI.OuterLoop->isLoopInvariant(FI.InnerLimit)) {
LLVM_DEBUG(dbgs() << "inner loop limit not invariant\n"); LLVM_DEBUG(dbgs() << "inner loop limit not invariant\n");
return false; return false;
} }
if (!OuterLoop->isLoopInvariant(OuterLimit)) { if (!FI.OuterLoop->isLoopInvariant(FI.OuterLimit)) {
LLVM_DEBUG(dbgs() << "outer loop limit not invariant\n"); LLVM_DEBUG(dbgs() << "outer loop limit not invariant\n");
return false; return false;
} }
SmallPtrSet<PHINode *, 4> InnerPHIsToTransform; if (!checkPHIs(FI, TTI))
if (!checkPHIs(OuterLoop, InnerLoop, InnerPHIsToTransform, InnerInductionPHI,
OuterInductionPHI, TTI))
return false; return false;
// FIXME: it should be possible to handle different types correctly. // FIXME: it should be possible to handle different types correctly.
if (InnerInductionPHI->getType() != OuterInductionPHI->getType()) if (FI.InnerInductionPHI->getType() != FI.OuterInductionPHI->getType())
return false; return false;
if (!checkOuterLoopInsts(OuterLoop, InnerLoop, IterationInstructions, if (!checkOuterLoopInsts(FI, IterationInstructions, TTI))
InnerLimit, OuterInductionPHI, TTI))
return false; return false;
// Find the values in the loop that can be replaced with the linearized // Find the values in the loop that can be replaced with the linearized
@ -456,9 +460,7 @@ static bool FlattenLoopPair(Loop *OuterLoop, Loop *InnerLoop, DominatorTree *DT,
// or outer induction variable. If there were, we could still do this // 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 // transformation, but we'd have to insert a div/mod to calculate the
// original IVs, so it wouldn't be profitable. // original IVs, so it wouldn't be profitable.
SmallPtrSet<Value *, 4> LinearIVUses; if (!checkIVUsers(FI))
if (!checkIVUsers(InnerInductionPHI, OuterInductionPHI, InnerIncrement,
OuterIncrement, InnerLimit, LinearIVUses))
return false; return false;
// Check if the new iteration variable might overflow. In this case, we // Check if the new iteration variable might overflow. In this case, we
@ -468,8 +470,7 @@ static bool FlattenLoopPair(Loop *OuterLoop, Loop *InnerLoop, DominatorTree *DT,
// TODO: it might be worth using a wider iteration variable rather than // TODO: it might be worth using a wider iteration variable rather than
// versioning the loop, if a wide enough type is legal. // versioning the loop, if a wide enough type is legal.
bool MustVersionLoop = true; bool MustVersionLoop = true;
OverflowResult OR = OverflowResult OR = checkOverflow(FI, DT, AC);
checkOverflow(OuterLoop, InnerLimit, OuterLimit, LinearIVUses, DT, AC);
if (OR == OverflowResult::AlwaysOverflowsHigh || if (OR == OverflowResult::AlwaysOverflowsHigh ||
OR == OverflowResult::AlwaysOverflowsLow) { OR == OverflowResult::AlwaysOverflowsLow) {
LLVM_DEBUG(dbgs() << "Multiply would always overflow, so not profitable\n"); LLVM_DEBUG(dbgs() << "Multiply would always overflow, so not profitable\n");
@ -490,47 +491,47 @@ static bool FlattenLoopPair(Loop *OuterLoop, Loop *InnerLoop, DominatorTree *DT,
{ {
using namespace ore; using namespace ore;
OptimizationRemark Remark(DEBUG_TYPE, "Flattened", InnerLoop->getStartLoc(), OptimizationRemark Remark(DEBUG_TYPE, "Flattened", FI.InnerLoop->getStartLoc(),
InnerLoop->getHeader()); FI.InnerLoop->getHeader());
OptimizationRemarkEmitter ORE(F); OptimizationRemarkEmitter ORE(F);
Remark << "Flattened into outer loop"; Remark << "Flattened into outer loop";
ORE.emit(Remark); ORE.emit(Remark);
} }
Value *NewTripCount = Value *NewTripCount =
BinaryOperator::CreateMul(InnerLimit, OuterLimit, "flatten.tripcount", BinaryOperator::CreateMul(FI.InnerLimit, FI.OuterLimit, "flatten.tripcount",
OuterLoop->getLoopPreheader()->getTerminator()); FI.OuterLoop->getLoopPreheader()->getTerminator());
LLVM_DEBUG(dbgs() << "Created new trip count in preheader: "; LLVM_DEBUG(dbgs() << "Created new trip count in preheader: ";
NewTripCount->dump()); NewTripCount->dump());
// Fix up PHI nodes that take values from the inner loop back-edge, which // Fix up PHI nodes that take values from the inner loop back-edge, which
// we are about to remove. // we are about to remove.
InnerInductionPHI->removeIncomingValue(InnerLoop->getLoopLatch()); FI.InnerInductionPHI->removeIncomingValue(FI.InnerLoop->getLoopLatch());
for (PHINode *PHI : InnerPHIsToTransform) for (PHINode *PHI : FI.InnerPHIsToTransform)
PHI->removeIncomingValue(InnerLoop->getLoopLatch()); PHI->removeIncomingValue(FI.InnerLoop->getLoopLatch());
// Modify the trip count of the outer loop to be the product of the two // Modify the trip count of the outer loop to be the product of the two
// trip counts. // trip counts.
cast<User>(OuterBranch->getCondition())->setOperand(1, NewTripCount); cast<User>(FI.OuterBranch->getCondition())->setOperand(1, NewTripCount);
// Replace the inner loop backedge with an unconditional branch to the exit. // Replace the inner loop backedge with an unconditional branch to the exit.
BasicBlock *InnerExitBlock = InnerLoop->getExitBlock(); BasicBlock *InnerExitBlock = FI.InnerLoop->getExitBlock();
BasicBlock *InnerExitingBlock = InnerLoop->getExitingBlock(); BasicBlock *InnerExitingBlock = FI.InnerLoop->getExitingBlock();
InnerExitingBlock->getTerminator()->eraseFromParent(); InnerExitingBlock->getTerminator()->eraseFromParent();
BranchInst::Create(InnerExitBlock, InnerExitingBlock); BranchInst::Create(InnerExitBlock, InnerExitingBlock);
DT->deleteEdge(InnerExitingBlock, InnerLoop->getHeader()); DT->deleteEdge(InnerExitingBlock, FI.InnerLoop->getHeader());
// Replace all uses of the polynomial calculated from the two induction // Replace all uses of the polynomial calculated from the two induction
// variables with the one new one. // variables with the one new one.
for (Value *V : LinearIVUses) for (Value *V : FI.LinearIVUses)
V->replaceAllUsesWith(OuterInductionPHI); V->replaceAllUsesWith(FI.OuterInductionPHI);
// Tell LoopInfo, SCEV and the pass manager that the inner loop has been // Tell LoopInfo, SCEV and the pass manager that the inner loop has been
// deleted, and any information that have about the outer loop invalidated. // deleted, and any information that have about the outer loop invalidated.
markLoopAsDeleted(InnerLoop); markLoopAsDeleted(FI.InnerLoop);
SE->forgetLoop(OuterLoop); SE->forgetLoop(FI.OuterLoop);
SE->forgetLoop(InnerLoop); SE->forgetLoop(FI.InnerLoop);
LI->erase(InnerLoop); LI->erase(FI.InnerLoop);
return true; return true;
} }
@ -543,8 +544,9 @@ PreservedAnalyses LoopFlattenPass::run(Loop &L, LoopAnalysisManager &AM,
Loop *InnerLoop = *L.begin(); Loop *InnerLoop = *L.begin();
std::string LoopName(InnerLoop->getName()); std::string LoopName(InnerLoop->getName());
struct FlattenInfo FI(InnerLoop->getParentLoop(), InnerLoop);
if (!FlattenLoopPair( if (!FlattenLoopPair(
&L, InnerLoop, &AR.DT, &AR.LI, &AR.SE, &AR.AC, &AR.TTI, FI, &AR.DT, &AR.LI, &AR.SE, &AR.AC, &AR.TTI,
[&](Loop *L) { Updater.markLoopAsDeleted(*L, LoopName); })) [&](Loop *L) { Updater.markLoopAsDeleted(*L, LoopName); }))
return PreservedAnalyses::all(); return PreservedAnalyses::all();
return getLoopPassPreservedAnalyses(); return getLoopPassPreservedAnalyses();
@ -600,6 +602,7 @@ bool LoopFlattenLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
*L->getHeader()->getParent()); *L->getHeader()->getParent());
Loop *InnerLoop = *L->begin(); Loop *InnerLoop = *L->begin();
return FlattenLoopPair(L, InnerLoop, DT, LI, SE, AC, TTI, struct FlattenInfo FI(InnerLoop->getParentLoop(), InnerLoop);
return FlattenLoopPair(FI, DT, LI, SE, AC, TTI,
[&](Loop *L) { LPM.markLoopAsDeleted(*L); }); [&](Loop *L) { LPM.markLoopAsDeleted(*L); });
} }