summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSanjoy Das <sanjoy@playingwithpointers.com>2015-01-15 01:46:09 +0000
committerSanjoy Das <sanjoy@playingwithpointers.com>2015-01-15 01:46:09 +0000
commit8c252bde367116a2459c866eef906534b58c3c0a (patch)
treec39325c73db353fe60b82f7b080760fe10c1b4d8
parentd762df8c2478a6bf9ac1f40787c77f7b9dd38f02 (diff)
downloadbcm5719-llvm-8c252bde367116a2459c866eef906534b58c3c0a.tar.gz
bcm5719-llvm-8c252bde367116a2459c866eef906534b58c3c0a.zip
Fix PR22222
The bug was introduced in r225282. r225282 assumed that sub X, Y is the same as add X, -Y. This is not correct if we are going to upgrade the sub to sub nuw. This change fixes the issue by making the optimization ignore sub instructions. Differential Revision: http://reviews.llvm.org/D6979 llvm-svn: 226075
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyIndVar.cpp12
-rw-r--r--llvm/test/Transforms/IndVarSimplify/pr22222.ll46
-rw-r--r--llvm/test/Transforms/IndVarSimplify/strengthen-overflow.ll106
3 files changed, 47 insertions, 117 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index f8aa1d3eec1..6cb91a154f0 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -278,9 +278,8 @@ bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
Value *IVOperand) {
// Currently we only handle instructions of the form "add <indvar> <value>"
- // and "sub <indvar> <value>".
unsigned Op = BO->getOpcode();
- if (!(Op == Instruction::Add || Op == Instruction::Sub))
+ if (Op != Instruction::Add)
return false;
// If BO is already both nuw and nsw then there is nothing left to do
@@ -304,15 +303,6 @@ bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
if (OtherOpSCEV == SE->getCouldNotCompute())
return false;
- if (Op == Instruction::Sub) {
- // If the subtraction is of the form "sub <indvar>, <op>", then pretend it
- // is "add <indvar>, -<op>" and continue, else bail out.
- if (OtherOperandIdx != 1)
- return false;
-
- OtherOpSCEV = SE->getNegativeSCEV(OtherOpSCEV);
- }
-
const SCEV *IVOpSCEV = SE->getSCEV(IVOperand);
const SCEV *ZeroSCEV = SE->getConstant(IVOpSCEV->getType(), 0);
diff --git a/llvm/test/Transforms/IndVarSimplify/pr22222.ll b/llvm/test/Transforms/IndVarSimplify/pr22222.ll
new file mode 100644
index 00000000000..ccdfe538dfa
--- /dev/null
+++ b/llvm/test/Transforms/IndVarSimplify/pr22222.ll
@@ -0,0 +1,46 @@
+; RUN: opt -indvars -S < %s | FileCheck %s
+
+@b = common global i32 0, align 4
+@c = common global i32 0, align 4
+@a = common global i32 0, align 4
+
+declare void @abort() #1
+
+; Function Attrs: nounwind ssp uwtable
+define i32 @main() {
+entry:
+ %a.promoted13 = load i32* @a, align 4
+ br label %for.cond1.preheader
+
+for.cond1.preheader: ; preds = %entry, %for.end
+ %or.lcssa14 = phi i32 [ %a.promoted13, %entry ], [ %or.lcssa, %for.end ]
+ %d.010 = phi i32 [ 1, %entry ], [ 0, %for.end ]
+ br label %for.body3
+
+for.body3: ; preds = %for.cond1.preheader, %for.body3
+ %inc12 = phi i32 [ 0, %for.cond1.preheader ], [ %inc, %for.body3 ]
+ %or11 = phi i32 [ %or.lcssa14, %for.cond1.preheader ], [ %or, %for.body3 ]
+; CHECK-NOT: sub nuw i32 %inc12, %d.010
+; CHECK: sub i32 %inc12, %d.010
+ %add = sub i32 %inc12, %d.010
+ %or = or i32 %or11, %add
+ %inc = add i32 %inc12, 1
+ br i1 false, label %for.body3, label %for.end
+
+for.end: ; preds = %for.body3
+ %or.lcssa = phi i32 [ %or, %for.body3 ]
+ br i1 false, label %for.cond1.preheader, label %for.end6
+
+for.end6: ; preds = %for.end
+ %or.lcssa.lcssa = phi i32 [ %or.lcssa, %for.end ]
+ store i32 %or.lcssa.lcssa, i32* @a, align 4
+ %cmp7 = icmp eq i32 %or.lcssa.lcssa, -1
+ br i1 %cmp7, label %if.end, label %if.then
+
+if.then: ; preds = %for.end6
+ tail call void @abort() #2
+ unreachable
+
+if.end: ; preds = %for.end6
+ ret i32 0
+}
diff --git a/llvm/test/Transforms/IndVarSimplify/strengthen-overflow.ll b/llvm/test/Transforms/IndVarSimplify/strengthen-overflow.ll
index 07e489e0338..2bafe96e1cc 100644
--- a/llvm/test/Transforms/IndVarSimplify/strengthen-overflow.ll
+++ b/llvm/test/Transforms/IndVarSimplify/strengthen-overflow.ll
@@ -52,58 +52,6 @@ define i32 @test.signed.add.1(i32* %array, i32 %length, i32 %init) {
ret i32 42
}
-define i32 @test.signed.sub.0(i32* %array, i32 %length, i32 %init) {
-; CHECK-LABEL: @test.signed.sub.0
- entry:
- %upper = icmp sgt i32 %init, %length
- br i1 %upper, label %loop, label %exit
-
- loop:
-; CHECK-LABEL: loop
- %civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
- %civ.inc = sub i32 %civ, 1
-; CHECK: %civ.inc = sub nsw i32 %civ, 1
- %cmp = icmp slt i32 %civ.inc, %length
- br i1 %cmp, label %latch, label %break
-
- latch:
- store i32 0, i32* %array
- %check = icmp sgt i32 %civ.inc, %length
- br i1 %check, label %loop, label %break
-
- break:
- ret i32 %civ.inc
-
- exit:
- ret i32 42
-}
-
-define i32 @test.signed.sub.1(i32* %array, i32 %length, i32 %init) {
-; CHECK-LABEL: @test.signed.sub.1
- entry:
- %upper = icmp sgt i32 %init, %length
- br i1 %upper, label %loop, label %exit
-
- loop:
-; CHECK-LABEL: loop
- %civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
- %civ.inc = sub i32 %civ, 1
-; CHECK: %civ.inc = sub i32 %civ, 1
- %cmp = icmp slt i32 %civ.inc, %length
- br i1 %cmp, label %latch, label %break
-
- latch:
- store i32 0, i32* %array
- %check = icmp sge i32 %civ.inc, %length
- br i1 %check, label %loop, label %break
-
- break:
- ret i32 %civ.inc
-
- exit:
- ret i32 42
-}
-
define i32 @test.unsigned.add.0(i32* %array, i32 %length, i32 %init) {
; CHECK-LABEL: @test.unsigned.add.0
entry:
@@ -156,59 +104,5 @@ define i32 @test.unsigned.add.1(i32* %array, i32 %length, i32 %init) {
ret i32 42
}
-define i32 @test.unsigned.sub.0(i32* %array, i32* %length_ptr, i32 %init) {
-; CHECK-LABEL: @test.unsigned.sub.0
- entry:
- %length = load i32* %length_ptr, !range !0
- %upper = icmp ult i32 %init, %length
- br i1 %upper, label %loop, label %exit
-
- loop:
-; CHECK-LABEL: loop
- %civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
- %civ.inc = sub i32 %civ, 2
-; CHECK: %civ.inc = sub nuw i32 %civ, 2
- %cmp = icmp slt i32 %civ.inc, %length
- br i1 %cmp, label %latch, label %break
-
- latch:
- store i32 0, i32* %array
- %check = icmp ult i32 %civ.inc, %length
- br i1 %check, label %loop, label %break
-
- break:
- ret i32 %civ.inc
-
- exit:
- ret i32 42
-}
-
-define i32 @test.unsigned.sub.1(i32* %array, i32* %length_ptr, i32 %init) {
-; CHECK-LABEL: @test.unsigned.sub.1
- entry:
- %length = load i32* %length_ptr, !range !1
- %upper = icmp ult i32 %init, %length
- br i1 %upper, label %loop, label %exit
-
- loop:
-; CHECK-LABEL: loop
- %civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
- %civ.inc = sub i32 %civ, 2
-; CHECK: %civ.inc = sub i32 %civ, 2
- %cmp = icmp slt i32 %civ.inc, %length
- br i1 %cmp, label %latch, label %break
-
- latch:
- store i32 0, i32* %array
- %check = icmp ult i32 %civ.inc, %length
- br i1 %check, label %loop, label %break
-
- break:
- ret i32 %civ.inc
-
- exit:
- ret i32 42
-}
-
!0 = !{i32 0, i32 2}
!1 = !{i32 0, i32 42}
OpenPOWER on IntegriCloud