[PATCH] D17873: [InstCombine] (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 3 17:45:24 PST 2016


reames created this revision.
reames added reviewers: sanjoy, majnemer.
reames added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.

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.

http://reviews.llvm.org/D17873

Files:
  lib/Transforms/InstCombine/InstCombineCompares.cpp
  test/Transforms/InstCombine/min-positive.ll

Index: test/Transforms/InstCombine/min-positive.ll
===================================================================
--- test/Transforms/InstCombine/min-positive.ll
+++ test/Transforms/InstCombine/min-positive.ll
@@ -0,0 +1,23 @@
+; 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 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
+}
+
+define i1 @test2(i32 %other) {
+; CHECK-LABEL: @test2
+; CHECK: %test = icmp sgt i32 %other, 0
+  %positive = load i32, i32* @g, !range !{i32 0, 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
+}
Index: lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3173,6 +3173,22 @@
         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);
+        switch (SPR.Flavor) {
+        default: break;
+        case SPF_SMIN:
+          if (isKnownNonNegative(A, DL))
+            return new ICmpInst(I.getPredicate(), B, CI);
+          if (isKnownNonNegative(B, DL))
+            return new ICmpInst(I.getPredicate(), A, CI);
+          break;
+        };
+      }
+    
+
     // The following transforms are only 'worth it' if the only user of the
     // subtraction is the icmp.
     if (Op0->hasOneUse()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17873.49789.patch
Type: text/x-patch
Size: 1901 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160304/d2a6fcb9/attachment.bin>


More information about the llvm-commits mailing list