summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Reames <listmail@philipreames.com>2016-03-09 21:05:07 +0000
committerPhilip Reames <listmail@philipreames.com>2016-03-09 21:05:07 +0000
commitec8a8b54379c1f4fef5233eed9b75bf12f4fdba1 (patch)
treee7327bcd4b109127fbd7823fbd690f05b8540e78
parent660748ca8c70e3afd8414ff45ef99feeca3f3d3c (diff)
downloadbcm5719-llvm-ec8a8b54379c1f4fef5233eed9b75bf12f4fdba1.tar.gz
bcm5719-llvm-ec8a8b54379c1f4fef5233eed9b75bf12f4fdba1.zip
[InstCombine] (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
When checking whether an smin is positive, we can move the comparison to one of the inputs if the other is known positive. If the known positive one is the min, then the other can't be negative. If the other is the min, then we compute the min. Differential Revision: http://reviews.llvm.org/D17873 llvm-svn: 263059
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp13
-rw-r--r--llvm/test/Transforms/InstCombine/min-positive.ll34
2 files changed, 47 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 6ee0525ea18..703506d8098 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3173,6 +3173,19 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
return Res;
}
+ // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)
+ if (CI->isZero() && I.getPredicate() == ICmpInst::ICMP_SGT)
+ if (auto *SI = dyn_cast<SelectInst>(Op0)) {
+ SelectPatternResult SPR = matchSelectPattern(SI, A, B);
+ if (SPR.Flavor == SPF_SMIN) {
+ if (isKnownNonNegative(A, DL) && isKnownNonZero(A, DL))
+ return new ICmpInst(I.getPredicate(), B, CI);
+ if (isKnownNonNegative(B, DL) && isKnownNonZero(B, DL))
+ return new ICmpInst(I.getPredicate(), A, CI);
+ }
+ }
+
+
// The following transforms are only 'worth it' if the only user of the
// subtraction is the icmp.
if (Op0->hasOneUse()) {
diff --git a/llvm/test/Transforms/InstCombine/min-positive.ll b/llvm/test/Transforms/InstCombine/min-positive.ll
new file mode 100644
index 00000000000..9bbdb2944a3
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/min-positive.ll
@@ -0,0 +1,34 @@
+; RUN: opt -S -instcombine < %s | FileCheck %s
+
+@g = external global i32
+
+define i1 @test(i32 %other) {
+; CHECK-LABEL: @test
+; CHECK: %test = icmp sgt i32 %other, 0
+ %positive = load i32, i32* @g, !range !{i32 1, i32 2048}
+ %cmp = icmp slt i32 %positive, %other
+ %sel = select i1 %cmp, i32 %positive, i32 %other
+ %test = icmp sgt i32 %sel, 0
+ ret i1 %test
+}
+
+define i1 @test2(i32 %other) {
+; CHECK-LABEL: @test2
+; CHECK: %test = icmp sgt i32 %other, 0
+ %positive = load i32, i32* @g, !range !{i32 1, i32 2048}
+ %cmp = icmp slt i32 %other, %positive
+ %sel = select i1 %cmp, i32 %other, i32 %positive
+ %test = icmp sgt i32 %sel, 0
+ ret i1 %test
+}
+
+; %positive might be zero
+define i1 @test3(i32 %other) {
+; CHECK-LABEL: @test3
+; CHECK: %test = icmp sgt i32 %sel, 0
+ %positive = load i32, i32* @g, !range !{i32 0, i32 2048}
+ %cmp = icmp slt i32 %positive, %other
+ %sel = select i1 %cmp, i32 %positive, i32 %other
+ %test = icmp sgt i32 %sel, 0
+ ret i1 %test
+}
OpenPOWER on IntegriCloud