[llvm] r263059 - [InstCombine] (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 9 13:05:08 PST 2016


Author: reames
Date: Wed Mar  9 15:05:07 2016
New Revision: 263059

URL: http://llvm.org/viewvc/llvm-project?rev=263059&view=rev
Log:
[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


Added:
    llvm/trunk/test/Transforms/InstCombine/min-positive.ll
Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=263059&r1=263058&r2=263059&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Wed Mar  9 15:05:07 2016
@@ -3173,6 +3173,19 @@ Instruction *InstCombiner::visitICmpInst
         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()) {

Added: llvm/trunk/test/Transforms/InstCombine/min-positive.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/min-positive.ll?rev=263059&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/min-positive.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/min-positive.ll Wed Mar  9 15:05:07 2016
@@ -0,0 +1,34 @@
+; RUN: opt -S -instcombine < %s | FileCheck %s
+
+ at 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
+}




More information about the llvm-commits mailing list