[PATCH] D53036: [InstCombine] Demand bits of UMin

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 9 12:18:26 PDT 2018


dmgreen created this revision.
dmgreen added reviewers: spatel, efriedma, craig.topper, lebedev.ri.

This is the umin alternative to the umax code from https://reviews.llvm.org/D53033, If I'm getting the maths correct.

As I put this up, I figure I should probably run this through a bunch more testing to be sure about it.


https://reviews.llvm.org/D53036

Files:
  lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
  test/Transforms/InstCombine/minmax-demandbits.ll


Index: test/Transforms/InstCombine/minmax-demandbits.ll
===================================================================
--- test/Transforms/InstCombine/minmax-demandbits.ll
+++ test/Transforms/InstCombine/minmax-demandbits.ll
@@ -190,9 +190,7 @@
 
 define i8 @or_min_31_30(i8 %A) {
 ; CHECK-LABEL: @or_min_31_30(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i8 [[A:%.*]], -30
-; CHECK-NEXT:    [[MIN:%.*]] = select i1 [[CMP]], i8 [[A]], i8 -30
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[MIN]], 31
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[A:%.*]], 31
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %cmp = icmp ult i8 %A, -30
@@ -203,9 +201,7 @@
 
 define i8 @or_min_7_6(i8 %A) {
 ; CHECK-LABEL: @or_min_7_6(
-; CHECK-NEXT:    [[L2:%.*]] = icmp ult i8 [[A:%.*]], -6
-; CHECK-NEXT:    [[MIN:%.*]] = select i1 [[L2]], i8 [[A]], i8 -6
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[MIN]], 7
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[A:%.*]], 7
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %l2 = icmp ult i8 %A, -6
@@ -216,9 +212,7 @@
 
 define i8 @or_min_7_5(i8 %A) {
 ; CHECK-LABEL: @or_min_7_5(
-; CHECK-NEXT:    [[L2:%.*]] = icmp ult i8 [[A:%.*]], -5
-; CHECK-NEXT:    [[MIN:%.*]] = select i1 [[L2]], i8 [[A]], i8 -5
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[MIN]], 7
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[A:%.*]], 7
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %l2 = icmp ult i8 %A, -5
Index: lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -326,6 +326,16 @@
           DemandedMask.countTrailingZeros() + 1 >
               C->getBitWidth() - C->countLeadingZeros())
         return LHS;
+    } else if (SPF == SPF_UMIN) {
+      // UMin(A, C) == A if ...
+      // The lowest non-zero bit of DemandMask is higher than the highest
+      // non-one bit of C
+      // This comes about from some maths on the above umax example
+      const APInt *C;
+      if (match(RHS, m_APInt(C)) &&
+          DemandedMask.countTrailingZeros() + 1 >
+              C->getBitWidth() - C->countLeadingOnes())
+        return LHS;
     }
 
     // If this is a select as part of any other min/max pattern, don't simplify


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53036.168853.patch
Type: text/x-patch
Size: 2263 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181009/1182db55/attachment.bin>


More information about the llvm-commits mailing list