diff options
-rw-r--r-- | lib/Analysis/IVUsers.cpp | 42 | ||||
-rw-r--r-- | test/Transforms/LoopStrengthReduce/2012-03-15-nopreheader.ll | 73 |
2 files changed, 96 insertions, 19 deletions
diff --git a/lib/Analysis/IVUsers.cpp b/lib/Analysis/IVUsers.cpp index c598b72c0d..308729fcf1 100644 --- a/lib/Analysis/IVUsers.cpp +++ b/lib/Analysis/IVUsers.cpp @@ -79,22 +79,31 @@ static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L, return false; } -/// Return true if this loop and all loop headers that dominate it are in -/// simplified form. -static bool isSimplifiedLoopNest(Loop *L, const DominatorTree *DT, - const LoopInfo *LI) { - if (!L->isLoopSimplifyForm()) - return false; - - for (DomTreeNode *Rung = DT->getNode(L->getLoopPreheader()); +/// Return true if all loop headers that dominate this block are in simplified +/// form. +static bool isSimplifiedLoopNest(BasicBlock *BB, const DominatorTree *DT, + const LoopInfo *LI, + SmallPtrSet<Loop*,16> &SimpleLoopNests) { + Loop *NearestLoop = 0; + for (DomTreeNode *Rung = DT->getNode(BB); Rung; Rung = Rung->getIDom()) { - BasicBlock *BB = Rung->getBlock(); - const Loop *DomLoop = LI->getLoopFor(BB); - if (DomLoop && DomLoop->getHeader() == BB) { + BasicBlock *DomBB = Rung->getBlock(); + Loop *DomLoop = LI->getLoopFor(DomBB); + if (DomLoop && DomLoop->getHeader() == DomBB) { + // If the domtree walk reaches a loop with no preheader, return false. if (!DomLoop->isLoopSimplifyForm()) return false; + // If we have already checked this loop nest, stop checking. + if (SimpleLoopNests.count(DomLoop)) + break; + // If we have not already checked this loop nest, remember the loop + // header nearest to BB. The nearest loop may not contain BB. + if (!NearestLoop) + NearestLoop = DomLoop; } } + if (NearestLoop) + SimpleLoopNests.insert(NearestLoop); return true; } @@ -137,15 +146,10 @@ bool IVUsers::AddUsersIfInteresting(Instruction *I, if (isa<PHINode>(User) && Processed.count(User)) continue; - Loop *UserLoop = LI->getLoopFor(User->getParent()); - // Only consider IVUsers that are dominated by simplified loop // headers. Otherwise, SCEVExpander will crash. - if (UserLoop && !SimpleLoopNests.count(UserLoop)) { - if (!isSimplifiedLoopNest(UserLoop, DT, LI)) - return false; - SimpleLoopNests.insert(UserLoop); - } + if (!isSimplifiedLoopNest(User->getParent(), DT, LI, SimpleLoopNests)) + return false; // Descend recursively, but not into PHI nodes outside the current loop. // It's important to see the entire expression outside the loop to get @@ -154,7 +158,7 @@ bool IVUsers::AddUsersIfInteresting(Instruction *I, // If User is already in Processed, we don't want to recurse into it again, // but do want to record a second reference in the same instruction. bool AddUserToIVUsers = false; - if (UserLoop != L) { + if (LI->getLoopFor(User->getParent()) != L) { if (isa<PHINode>(User) || Processed.count(User) || !AddUsersIfInteresting(User, SimpleLoopNests)) { DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n' diff --git a/test/Transforms/LoopStrengthReduce/2012-03-15-nopreheader.ll b/test/Transforms/LoopStrengthReduce/2012-03-15-nopreheader.ll index 207e9d74f5..01f9276dc2 100644 --- a/test/Transforms/LoopStrengthReduce/2012-03-15-nopreheader.ll +++ b/test/Transforms/LoopStrengthReduce/2012-03-15-nopreheader.ll @@ -48,3 +48,76 @@ for.inc: ; preds = %lor.rhs234, %land.l return: ; preds = %if.end677, %doshell, %if.then96 ret void } + +define void @nopreheader2() nounwind ssp { +entry: + indirectbr i8* undef, [label %while.cond, label %return] + +while.cond: ; preds = %while.cond.backedge, %entry + indirectbr i8* undef, [label %while.cond.backedge, label %lor.rhs] + +lor.rhs: ; preds = %while.cond + indirectbr i8* undef, [label %while.cond.backedge, label %while.end] + +while.cond.backedge: ; preds = %lor.rhs, %while.cond + indirectbr i8* undef, [label %while.cond] + +while.end: ; preds = %lor.rhs + indirectbr i8* undef, [label %if.then18, label %return] + +if.then18: ; preds = %while.end + indirectbr i8* undef, [label %if.end35, label %lor.lhs.false] + +lor.lhs.false: ; preds = %if.then18 + indirectbr i8* undef, [label %if.end35, label %return] + +if.end35: ; preds = %lor.lhs.false, %if.then18 + indirectbr i8* undef, [label %while.cond36] + +while.cond36: ; preds = %while.body49, %if.end35 + %0 = phi i64 [ %indvar.next13, %while.body49 ], [ 0, %if.end35 ] + indirectbr i8* undef, [label %while.body49, label %lor.rhs42] + +lor.rhs42: ; preds = %while.cond36 + indirectbr i8* undef, [label %while.body49, label %while.end52] + +while.body49: ; preds = %lor.rhs42, %while.cond36 + %indvar.next13 = add i64 %0, 1 + indirectbr i8* undef, [label %while.cond36] + +while.end52: ; preds = %lor.rhs42 + indirectbr i8* undef, [label %land.lhs.true, label %return] + +land.lhs.true: ; preds = %while.end52 + indirectbr i8* undef, [label %while.cond66.preheader, label %return] + +while.cond66.preheader: ; preds = %land.lhs.true + indirectbr i8* undef, [label %while.cond66] + +while.cond66: ; preds = %while.body77, %while.cond66.preheader + indirectbr i8* undef, [label %land.rhs, label %while.cond81.preheader] + +land.rhs: ; preds = %while.cond66 + indirectbr i8* undef, [label %while.body77, label %while.cond81.preheader] + +while.cond81.preheader: ; preds = %land.rhs, %while.cond66 + %tmp45 = add i64 undef, %0 + %tmp46 = add i64 %tmp45, undef + indirectbr i8* undef, [label %while.cond81] + +while.body77: ; preds = %land.rhs + indirectbr i8* undef, [label %while.cond66] + +while.cond81: ; preds = %while.body94, %while.cond81.preheader + %tmp25 = add i64 %tmp46, undef + indirectbr i8* undef, [label %while.body94, label %lor.rhs87] + +lor.rhs87: ; preds = %while.cond81 + indirectbr i8* undef, [label %while.body94, label %return] + +while.body94: ; preds = %lor.rhs87, %while.cond81 + indirectbr i8* undef, [label %while.cond81] + +return: ; preds = %if.end216, %land.lhs.true183, %land.lhs.true, %while.end52, %lor.lhs.false, %while.end, %entry + ret void +} |