Check sign to detect overflow before changing compare stride.
[oota-llvm.git] / lib / Transforms / Scalar / LoopStrengthReduce.cpp
index 3d82fd279c5432b085ab9fe9e10e06556261cf2b..f47a5afcfc55af82ed48e7eda290621a0ad1769b 100644 (file)
@@ -1542,6 +1542,12 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,
   Value *NewIncV = NULL;
   int64_t Scale = 1;
 
+  // Check stride constant and the comparision constant signs to detect
+  // overflow.
+  if (ICmpInst::isSignedPredicate(Predicate) &&
+      (CmpVal & SignBit) != (CmpSSInt & SignBit))
+    return Cond;
+
   // Look for a suitable stride / iv as replacement.
   std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());
   for (unsigned i = 0, e = StrideOrder.size(); i != e; ++i) {
@@ -1640,11 +1646,12 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,
   // before the branch. See
   // test/Transforms/LoopStrengthReduce/change-compare-stride-trickiness-*.ll
   // for an example of this situation.
-  if (!Cond->hasOneUse())
+  if (!Cond->hasOneUse()) {
     for (BasicBlock::iterator I = Cond, E = Cond->getParent()->end();
          I != E; ++I)
       if (I == NewIncV)
         return Cond;
+  }
 
   if (NewCmpVal != CmpVal) {
     // Create a new compare instruction using new stride / iv.