Make ScalarEvolution::isLoopGuardedByCond work even when the edge

entering a loop is a non-split critical edge.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@72004 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-05-18 15:36:09 +00:00
parent 70a1fe7048
commit 859b4824ee
3 changed files with 32 additions and 14 deletions

View File

@ -341,6 +341,10 @@ namespace llvm {
BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS, BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
const Loop *L, bool isSigned); const Loop *L, bool isSigned);
/// getLoopPredecessor - If the given loop's header has exactly one unique
/// predecessor outside the loop, return it. Otherwise return null.
BasicBlock *getLoopPredecessor(const Loop *L);
/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
/// (which may not be an immediate predecessor) which has exactly one /// (which may not be an immediate predecessor) which has exactly one
/// successor from which BB is reachable, or null if no such block is /// successor from which BB is reachable, or null if no such block is

View File

@ -3246,6 +3246,21 @@ SCEVHandle ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
return UnknownValue; return UnknownValue;
} }
/// getLoopPredecessor - If the given loop's header has exactly one unique
/// predecessor outside the loop, return it. Otherwise return null.
///
BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) {
BasicBlock *Header = L->getHeader();
BasicBlock *Pred = 0;
for (pred_iterator PI = pred_begin(Header), E = pred_end(Header);
PI != E; ++PI)
if (!L->contains(*PI)) {
if (Pred && Pred != *PI) return 0; // Multiple predecessors.
Pred = *PI;
}
return Pred;
}
/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
/// (which may not be an immediate predecessor) which has exactly one /// (which may not be an immediate predecessor) which has exactly one
/// successor from which BB is reachable, or null if no such block is /// successor from which BB is reachable, or null if no such block is
@ -3260,11 +3275,10 @@ ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
return Pred; return Pred;
// A loop's header is defined to be a block that dominates the loop. // A loop's header is defined to be a block that dominates the loop.
// If the loop has a preheader, it must be a block that has exactly // If the header has a unique predecessor outside the loop, it must be
// one successor that can reach BB. This is slightly more strict // a block that has exactly one successor that can reach the loop.
// than necessary, but works if critical edges are split.
if (Loop *L = LI->getLoopFor(BB)) if (Loop *L = LI->getLoopFor(BB))
return L->getLoopPreheader(); return getLoopPredecessor(L);
return 0; return 0;
} }
@ -3275,18 +3289,18 @@ ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
ICmpInst::Predicate Pred, ICmpInst::Predicate Pred,
const SCEV *LHS, const SCEV *RHS) { const SCEV *LHS, const SCEV *RHS) {
BasicBlock *Preheader = L->getLoopPreheader(); BasicBlock *Predecessor = getLoopPredecessor(L);
BasicBlock *PreheaderDest = L->getHeader(); BasicBlock *PredecessorDest = L->getHeader();
// Starting at the preheader, climb up the predecessor chain, as long as // Starting at the loop predecessor, climb up the predecessor chain, as long
// there are predecessors that can be found that have unique successors // as there are predecessors that can be found that have unique successors
// leading to the original header. // leading to the original header.
for (; Preheader; for (; Predecessor;
PreheaderDest = Preheader, PredecessorDest = Predecessor,
Preheader = getPredecessorWithUniqueSuccessorForBB(Preheader)) { Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
BranchInst *LoopEntryPredicate = BranchInst *LoopEntryPredicate =
dyn_cast<BranchInst>(Preheader->getTerminator()); dyn_cast<BranchInst>(Predecessor->getTerminator());
if (!LoopEntryPredicate || if (!LoopEntryPredicate ||
LoopEntryPredicate->isUnconditional()) LoopEntryPredicate->isUnconditional())
continue; continue;
@ -3299,7 +3313,7 @@ bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
Value *PreCondLHS = ICI->getOperand(0); Value *PreCondLHS = ICI->getOperand(0);
Value *PreCondRHS = ICI->getOperand(1); Value *PreCondRHS = ICI->getOperand(1);
ICmpInst::Predicate Cond; ICmpInst::Predicate Cond;
if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest) if (LoopEntryPredicate->getSuccessor(0) == PredecessorDest)
Cond = ICI->getPredicate(); Cond = ICI->getPredicate();
else else
Cond = ICI->getInversePredicate(); Cond = ICI->getInversePredicate();

View File

@ -1,6 +1,6 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \ ; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \
; RUN: -scalar-evolution-max-iterations=0 | \ ; RUN: -scalar-evolution-max-iterations=0 | \
; RUN: grep -F "backedge-taken count is (-2147483632 + (2147483632 smax (-1 + (-1 * %x)) smax (-1 + (-1 * %y))))" ; RUN: grep -F "backedge-taken count is (-2147483632 + ((-1 + (-1 * %x)) smax (-1 + (-1 * %y))))"
; PR2607 ; PR2607
define i32 @b(i32 %x, i32 %y) nounwind { define i32 @b(i32 %x, i32 %y) nounwind {