Make ScalarEvolution::isLoopGuardedByCond work even when the edge
authorDan Gohman <gohman@apple.com>
Mon, 18 May 2009 15:36:09 +0000 (15:36 +0000)
committerDan Gohman <gohman@apple.com>
Mon, 18 May 2009 15:36:09 +0000 (15:36 +0000)
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

include/llvm/Analysis/ScalarEvolution.h
lib/Analysis/ScalarEvolution.cpp
test/Analysis/ScalarEvolution/2008-07-29-SMinExpr.ll

index a5534e89c4e2a5cb883f7c0e2db705dfdab82ad3..d43d2449ebe2baec413d0c33a89baf7062157b6f 100644 (file)
@@ -341,6 +341,10 @@ namespace llvm {
     BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
                                        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
     /// (which may not be an immediate predecessor) which has exactly one
     /// successor from which BB is reachable, or null if no such block is
index 4b99a869ff5fcd2e17033509b80f0c1df874c30c..f3000394a51a7e35c0790a62aeee92f0d1898834 100644 (file)
@@ -3246,6 +3246,21 @@ SCEVHandle ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
   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
 /// (which may not be an immediate predecessor) which has exactly one
 /// successor from which BB is reachable, or null if no such block is
@@ -3260,11 +3275,10 @@ ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
     return Pred;
 
   // 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
-  // one successor that can reach BB. This is slightly more strict
-  // than necessary, but works if critical edges are split.
+  // If the header has a unique predecessor outside the loop, it must be
+  // a block that has exactly one successor that can reach the loop.
   if (Loop *L = LI->getLoopFor(BB))
-    return L->getLoopPreheader();
+    return getLoopPredecessor(L);
 
   return 0;
 }
@@ -3275,18 +3289,18 @@ ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
 bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
                                           ICmpInst::Predicate Pred,
                                           const SCEV *LHS, const SCEV *RHS) {
-  BasicBlock *Preheader = L->getLoopPreheader();
-  BasicBlock *PreheaderDest = L->getHeader();
+  BasicBlock *Predecessor = getLoopPredecessor(L);
+  BasicBlock *PredecessorDest = L->getHeader();
 
-  // Starting at the preheader, climb up the predecessor chain, as long as
-  // there are predecessors that can be found that have unique successors
+  // Starting at the loop predecessor, climb up the predecessor chain, as long
+  // as there are predecessors that can be found that have unique successors
   // leading to the original header.
-  for (; Preheader;
-       PreheaderDest = Preheader,
-       Preheader = getPredecessorWithUniqueSuccessorForBB(Preheader)) {
+  for (; Predecessor;
+       PredecessorDest = Predecessor,
+       Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
 
     BranchInst *LoopEntryPredicate =
-      dyn_cast<BranchInst>(Preheader->getTerminator());
+      dyn_cast<BranchInst>(Predecessor->getTerminator());
     if (!LoopEntryPredicate ||
         LoopEntryPredicate->isUnconditional())
       continue;
@@ -3299,7 +3313,7 @@ bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
     Value *PreCondLHS = ICI->getOperand(0);
     Value *PreCondRHS = ICI->getOperand(1);
     ICmpInst::Predicate Cond;
-    if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest)
+    if (LoopEntryPredicate->getSuccessor(0) == PredecessorDest)
       Cond = ICI->getPredicate();
     else
       Cond = ICI->getInversePredicate();
index d8731b69751119180edff3625fec5395e826bbf3..9051dc7ec515d5cf0fc77c36b191380476802a39 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \
 ; 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
 
 define i32 @b(i32 %x, i32 %y) nounwind {