[indvars] Fix bugs in floating point IV range checks noticed by inspection.
authorAndrew Trick <atrick@apple.com>
Tue, 13 Sep 2011 01:59:32 +0000 (01:59 +0000)
committerAndrew Trick <atrick@apple.com>
Tue, 13 Sep 2011 01:59:32 +0000 (01:59 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139574 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/IndVarSimplify.cpp
test/Transforms/IndVarSimplify/floating-point-iv.ll

index 3a4580e1007a637a8c97255460046d277aeee648..b5500dec6e74b73b28a029bd69bdd0490a2cad05 100644 (file)
@@ -351,14 +351,14 @@ void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) {
   // Positive and negative strides have different safety conditions.
   if (IncValue > 0) {
     // If we have a positive stride, we require the init to be less than the
-    // exit value and an equality or less than comparison.
-    if (InitValue >= ExitValue ||
-        NewPred == CmpInst::ICMP_SGT || NewPred == CmpInst::ICMP_SGE)
+    // exit value.
+    if (InitValue >= ExitValue)
       return;
 
     uint32_t Range = uint32_t(ExitValue-InitValue);
-    if (NewPred == CmpInst::ICMP_SLE) {
-      // Normalize SLE -> SLT, check for infinite loop.
+    // Check for infinite loop, either:
+    // while (i <= Exit) or until (i > Exit)
+    if (NewPred == CmpInst::ICMP_SLE || NewPred == CmpInst::ICMP_SGT) {
       if (++Range == 0) return;  // Range overflows.
     }
 
@@ -378,14 +378,14 @@ void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) {
 
   } else {
     // If we have a negative stride, we require the init to be greater than the
-    // exit value and an equality or greater than comparison.
-    if (InitValue >= ExitValue ||
-        NewPred == CmpInst::ICMP_SLT || NewPred == CmpInst::ICMP_SLE)
+    // exit value.
+    if (InitValue <= ExitValue)
       return;
 
     uint32_t Range = uint32_t(InitValue-ExitValue);
-    if (NewPred == CmpInst::ICMP_SGE) {
-      // Normalize SGE -> SGT, check for infinite loop.
+    // Check for infinite loop, either:
+    // while (i >= Exit) or until (i < Exit)
+    if (NewPred == CmpInst::ICMP_SGE || NewPred == CmpInst::ICMP_SLT) {
       if (++Range == 0) return;  // Range overflows.
     }
 
index 8f4b87048a4d35cc315d5179668f1f5c897a6873..e334f069b81d978c57a5f1f761c43b71772f40c4 100644 (file)
@@ -67,7 +67,8 @@ bb:           ; preds = %bb, %entry
 return:
        ret void
 ; CHECK: @test4
-; CHECK: fcmp
+; CHECK-NOT: cmp
+; CHECK: br i1 false
 }
 
 ; PR6761
@@ -84,9 +85,8 @@ define void @test5() nounwind {
 
 exit:
   ret void
-  
+
 ; CHECK: @test5
 ; CHECK: icmp eq i32 {{.*}}, 10
 ; CHECK-NEXT: br i1
 }
-