[llvm] 59441c7 - [InstCombine] fold signbit check of X | (X -1)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 11 13:14:22 PDT 2021


Author: Sanjay Patel
Date: 2021-10-11T16:14:13-04:00
New Revision: 59441c73296e7c489ac1d71ffda2de3060bdd4f8

URL: https://github.com/llvm/llvm-project/commit/59441c73296e7c489ac1d71ffda2de3060bdd4f8
DIFF: https://github.com/llvm/llvm-project/commit/59441c73296e7c489ac1d71ffda2de3060bdd4f8.diff

LOG: [InstCombine] fold signbit check of X | (X -1)

There may be some other patterns like this or a generalization,
but this is an example that I noticed would definitely regress
with a planned follow-up to D111410.

https://alive2.llvm.org/ce/z/GVpQDb

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/test/Transforms/InstCombine/icmp-or.ll
    llvm/test/Transforms/InstCombine/or.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index d47e6160acbeb..2f25be4fce08c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1950,6 +1950,17 @@ Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,
     }
   }
 
+  // (X | (X-1)) s<  0 --> X < 1
+  // (X | (X-1)) s> -1 --> X > 0
+  Value *X;
+  bool TrueIfSigned;
+  if (isSignBitCheck(Pred, C, TrueIfSigned) &&
+      match(Or, m_c_Or(m_Add(m_Value(X), m_AllOnes()), m_Deferred(X)))) {
+    auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
+    Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
+    return new ICmpInst(NewPred, X, NewC);
+  }
+
   if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
     return nullptr;
 

diff  --git a/llvm/test/Transforms/InstCombine/icmp-or.ll b/llvm/test/Transforms/InstCombine/icmp-or.ll
index 4df08814aaa51..de3f998814b79 100644
--- a/llvm/test/Transforms/InstCombine/icmp-or.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-or.ll
@@ -220,9 +220,7 @@ define i1 @eq_const_mask_use2(i8 %x, i8 %y) {
 
 define <2 x i1> @decrement_slt_0(<2 x i8> %x) {
 ; CHECK-LABEL: @decrement_slt_0(
-; CHECK-NEXT:    [[DEC:%.*]] = add <2 x i8> [[X:%.*]], <i8 -1, i8 -1>
-; CHECK-NEXT:    [[OR:%.*]] = or <2 x i8> [[DEC]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = icmp slt <2 x i8> [[OR]], zeroinitializer
+; CHECK-NEXT:    [[R:%.*]] = icmp slt <2 x i8> [[X:%.*]], <i8 1, i8 1>
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %dec = add <2 x i8> %x, <i8 -1, i8 -1>
@@ -236,8 +234,7 @@ define i1 @decrement_slt_0_commute_use1(i8 %px) {
 ; CHECK-NEXT:    [[X:%.*]] = mul i8 [[PX:%.*]], 42
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X]], -1
 ; CHECK-NEXT:    call void @use(i8 [[DEC]])
-; CHECK-NEXT:    [[OR:%.*]] = or i8 [[X]], [[DEC]]
-; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[OR]], 0
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[X]], 1
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %x = mul i8 %px, 42 ; thwart complexity-based canonicalization
@@ -253,7 +250,7 @@ define i1 @decrement_slt_0_use2(i8 %x) {
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X:%.*]], -1
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[DEC]], [[X]]
 ; CHECK-NEXT:    call void @use(i8 [[OR]])
-; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[OR]], 0
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[X]], 1
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %dec = add i8 %x, -1
@@ -263,6 +260,8 @@ define i1 @decrement_slt_0_use2(i8 %x) {
   ret i1 %r
 }
 
+; negative test - wrong cmp constant
+
 define i1 @decrement_slt_n1(i8 %x) {
 ; CHECK-LABEL: @decrement_slt_n1(
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X:%.*]], -1
@@ -276,6 +275,8 @@ define i1 @decrement_slt_n1(i8 %x) {
   ret i1 %r
 }
 
+; negative test - wrong add constant
+
 define i1 @not_decrement_slt_0(i8 %x) {
 ; CHECK-LABEL: @not_decrement_slt_0(
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X:%.*]], -2
@@ -293,9 +294,7 @@ define i1 @not_decrement_slt_0(i8 %x) {
 
 define <2 x i1> @decrement_sgt_n1(<2 x i8> %x) {
 ; CHECK-LABEL: @decrement_sgt_n1(
-; CHECK-NEXT:    [[DEC:%.*]] = add <2 x i8> [[X:%.*]], <i8 -1, i8 -1>
-; CHECK-NEXT:    [[OR:%.*]] = or <2 x i8> [[DEC]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = icmp sgt <2 x i8> [[OR]], <i8 -1, i8 -1>
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %dec = add <2 x i8> %x, <i8 -1, i8 -1>
@@ -309,8 +308,7 @@ define i1 @decrement_sgt_n1_commute_use1(i8 %px) {
 ; CHECK-NEXT:    [[X:%.*]] = mul i8 [[PX:%.*]], 42
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X]], -1
 ; CHECK-NEXT:    call void @use(i8 [[DEC]])
-; CHECK-NEXT:    [[OR:%.*]] = or i8 [[X]], [[DEC]]
-; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[OR]], -1
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[X]], 0
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %x = mul i8 %px, 42 ; thwart complexity-based canonicalization
@@ -326,7 +324,7 @@ define i1 @decrement_sgt_n1_use2(i8 %x) {
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X:%.*]], -1
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[DEC]], [[X]]
 ; CHECK-NEXT:    call void @use(i8 [[OR]])
-; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[OR]], -1
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[X]], 0
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %dec = add i8 %x, -1
@@ -336,6 +334,8 @@ define i1 @decrement_sgt_n1_use2(i8 %x) {
   ret i1 %r
 }
 
+; negative test - wrong cmp constant
+
 define i1 @decrement_sgt_0(i8 %x) {
 ; CHECK-LABEL: @decrement_sgt_0(
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X:%.*]], -1
@@ -349,6 +349,8 @@ define i1 @decrement_sgt_0(i8 %x) {
   ret i1 %r
 }
 
+; negative test - wrong add constant
+
 define i1 @not_decrement_sgt_n1(i8 %x) {
 ; CHECK-LABEL: @not_decrement_sgt_n1(
 ; CHECK-NEXT:    [[DEC:%.*]] = add i8 [[X:%.*]], -2

diff  --git a/llvm/test/Transforms/InstCombine/or.ll b/llvm/test/Transforms/InstCombine/or.ll
index 5b4bff6f0c291..c50f226438eca 100644
--- a/llvm/test/Transforms/InstCombine/or.ll
+++ b/llvm/test/Transforms/InstCombine/or.ll
@@ -1447,10 +1447,8 @@ define i8 @lshr_bitwidth_mask(i8 %x, i8 %y) {
 
 define i1 @cmp_overlap(i32 %x) {
 ; CHECK-LABEL: @cmp_overlap(
-; CHECK-NEXT:    [[NOTSUB:%.*]] = add i32 [[X:%.*]], -1
-; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[NOTSUB]], [[X]]
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 0
-; CHECK-NEXT:    ret i1 [[TMP2]]
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 1
+; CHECK-NEXT:    ret i1 [[TMP1]]
 ;
   %isneg = icmp slt i32 %x, 0
   %negx = sub i32 0, %x


        


More information about the llvm-commits mailing list