Revert r146822 at Pete Cooper's request as it broke clang self hosting.
[oota-llvm.git] / lib / Transforms / Utils / SimplifyIndVar.cpp
index be6a1e0019dcfa2d2efc067d95aa6dfb220df208..6732a789d5f1a93c9f047b161feee58b82eff277 100644 (file)
@@ -54,16 +54,16 @@ namespace {
     bool Changed;
 
   public:
-    SimplifyIndvar(Loop *Loop, LPPassManager *LPM,
+    SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, LPPassManager *LPM,
                    SmallVectorImpl<WeakVH> &Dead, IVUsers *IVU = NULL) :
       L(Loop),
       LI(LPM->getAnalysisIfAvailable<LoopInfo>()),
-      SE(LPM->getAnalysisIfAvailable<ScalarEvolution>()),
+      SE(SE),
       IU(IVU),
       TD(LPM->getAnalysisIfAvailable<TargetData>()),
       DeadInsts(Dead),
       Changed(false) {
-      assert(LI && SE && "IV simplification requires ScalarEvolution");
+      assert(LI && "IV simplification requires LoopInfo");
     }
 
     bool hasChanged() const { return Changed; }
@@ -85,8 +85,10 @@ namespace {
 /// foldIVUser - Fold an IV operand into its use.  This removes increments of an
 /// aligned IV when used by a instruction that ignores the low bits.
 ///
+/// IVOperand is guaranteed SCEVable, but UseInst may not be.
+///
 /// Return the operand of IVOperand for this induction variable if IVOperand can
-/// be folded (in case more folding opportunity has been exposed).
+/// be folded (in case more folding opportunities have been exposed).
 /// Otherwise return null.
 Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
   Value *IVSrc = 0;
@@ -105,8 +107,8 @@ Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand)
 
     // Attempt to fold a binary operator with constant operand.
     // e.g. ((I + 1) >> 2) => I >> 2
-    if (IVOperand->getNumOperands() != 2 ||
-        !isa<ConstantInt>(IVOperand->getOperand(1)))
+    if (!isa<BinaryOperator>(IVOperand)
+        || !isa<ConstantInt>(IVOperand->getOperand(1)))
       return 0;
 
     IVSrc = IVOperand->getOperand(0);
@@ -219,8 +221,7 @@ void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem,
       return;
 
     ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ,
-                                  Rem->getOperand(0), Rem->getOperand(1),
-                                  "tmp");
+                                  Rem->getOperand(0), Rem->getOperand(1));
     SelectInst *Sel =
       SelectInst::Create(ICmp,
                          ConstantInt::get(Rem->getType(), 0),
@@ -241,6 +242,7 @@ void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem,
 
 /// eliminateIVUser - Eliminate an operation that consumes a simple IV and has
 /// no observable side-effect given the range of IV values.
+/// IVOperand is guaranteed SCEVable, but UseInst may not be.
 bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
                                      Instruction *IVOperand) {
   if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
@@ -293,7 +295,7 @@ static void pushIVUsers(
 /// isSimpleIVUser - Return true if this instruction generates a simple SCEV
 /// expression in terms of that IV.
 ///
-/// This is similar to IVUsers' isInsteresting() but processes each instruction
+/// This is similar to IVUsers' isInteresting() but processes each instruction
 /// non-recursively when the operand is already known to be a simpleIVUser.
 ///
 static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
@@ -324,6 +326,9 @@ static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
 /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
 ///
 void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
+  if (!SE->isSCEVable(CurrIV->getType()))
+    return;
+
   // Instructions processed by SimplifyIndvar for CurrIV.
   SmallPtrSet<Instruction*,16> Simplified;
 
@@ -372,22 +377,22 @@ namespace llvm {
 
 /// simplifyUsersOfIV - Simplify instructions that use this induction variable
 /// by using ScalarEvolution to analyze the IV's recurrence.
-bool simplifyUsersOfIV(PHINode *CurrIV, LPPassManager *LPM,
+bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM,
                        SmallVectorImpl<WeakVH> &Dead, IVVisitor *V)
 {
   LoopInfo *LI = &LPM->getAnalysis<LoopInfo>();
-  SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), LPM, Dead);
+  SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, LPM, Dead);
   SIV.simplifyUsers(CurrIV, V);
   return SIV.hasChanged();
 }
 
 /// simplifyLoopIVs - Simplify users of induction variables within this
 /// loop. This does not actually change or add IVs.
-bool simplifyLoopIVs(Loop *L, LPPassManager *LPM,
+bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM,
                      SmallVectorImpl<WeakVH> &Dead) {
   bool Changed = false;
   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
-    Changed |= simplifyUsersOfIV(cast<PHINode>(I), LPM, Dead);
+    Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, LPM, Dead);
   }
   return Changed;
 }
@@ -397,9 +402,9 @@ bool simplifyLoopIVs(Loop *L, LPPassManager *LPM,
 ///
 /// This is the old approach to IV simplification to be replaced by
 /// SimplifyLoopIVs.
-bool simplifyIVUsers(IVUsers *IU, LPPassManager *LPM,
+bool simplifyIVUsers(IVUsers *IU, ScalarEvolution *SE, LPPassManager *LPM,
                      SmallVectorImpl<WeakVH> &Dead) {
-  SimplifyIndvar SIV(IU->getLoop(), LPM, Dead);
+  SimplifyIndvar SIV(IU->getLoop(), SE, LPM, Dead);
 
   // Each round of simplification involves a round of eliminating operations
   // followed by a round of widening IVs. A single IVUsers worklist is used