[llvm] 5e6dc5e - [InstSimplify] generalize ctlz-of-shifted-constant

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 21 11:24:04 PDT 2021


Author: Sanjay Patel
Date: 2021-04-21T14:23:55-04:00
New Revision: 5e6dc5e404f4ddd9667974caf3ba355bd7b01ab2

URL: https://github.com/llvm/llvm-project/commit/5e6dc5e404f4ddd9667974caf3ba355bd7b01ab2
DIFF: https://github.com/llvm/llvm-project/commit/5e6dc5e404f4ddd9667974caf3ba355bd7b01ab2.diff

LOG: [InstSimplify] generalize ctlz-of-shifted-constant

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

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/call.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 08f504a0ce37b..73b760ff66057 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5477,8 +5477,10 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
   }
   case Intrinsic::ctlz: {
     Value *X;
-    if (match(Op0, m_LShr(m_SignMask(), m_Value(X))))
+    if (match(Op0, m_LShr(m_Negative(), m_Value(X))))
       return X;
+    if (match(Op0, m_AShr(m_Negative(), m_Value())))
+      return Constant::getNullValue(ReturnType);
     break;
   }
   case Intrinsic::smax:

diff  --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll
index 6245a9be92f51..99a4a96d33b1a 100644
--- a/llvm/test/Transforms/InstSimplify/call.ll
+++ b/llvm/test/Transforms/InstSimplify/call.ll
@@ -1408,9 +1408,7 @@ define i32 @ctlz_lshr_sign_bit(i32 %x) {
 
 define i32 @ctlz_lshr_negative(i32 %x) {
 ; CHECK-LABEL: @ctlz_lshr_negative(
-; CHECK-NEXT:    [[S:%.*]] = lshr i32 -42, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ctlz.i32(i32 [[S]], i1 true)
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    ret i32 [[X:%.*]]
 ;
   %s = lshr i32 -42, %x
   %r = call i32 @llvm.ctlz.i32(i32 %s, i1 true)
@@ -1428,24 +1426,20 @@ define <3 x i33> @ctlz_lshr_sign_bit_vec(<3 x i33> %x) {
 
 ; Negative test - this could be generalized in instcombine though.
 
-define i32 @ctlz_lshr_not_sign_bit(i32 %x) {
-; CHECK-LABEL: @ctlz_lshr_not_sign_bit(
-; CHECK-NEXT:    [[S:%.*]] = lshr i32 -1, [[X:%.*]]
+define i32 @ctlz_lshr_not_negative(i32 %x) {
+; CHECK-LABEL: @ctlz_lshr_not_negative(
+; CHECK-NEXT:    [[S:%.*]] = lshr i32 42, [[X:%.*]]
 ; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ctlz.i32(i32 [[S]], i1 true)
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
-  %s = lshr i32 4294967295, %x
+  %s = lshr i32 42, %x
   %r = call i32 @llvm.ctlz.i32(i32 %s, i1 true)
   ret i32 %r
 }
 
-; TODO: Reduce to 0.
-
 define i32 @ctlz_ashr_sign_bit(i32 %x) {
 ; CHECK-LABEL: @ctlz_ashr_sign_bit(
-; CHECK-NEXT:    [[S:%.*]] = ashr i32 -2147483648, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ctlz.i32(i32 [[S]], i1 false)
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    ret i32 0
 ;
   %s = ashr i32 2147483648, %x
   %r = call i32 @llvm.ctlz.i32(i32 %s, i1 false)
@@ -1454,9 +1448,7 @@ define i32 @ctlz_ashr_sign_bit(i32 %x) {
 
 define i32 @ctlz_ashr_negative(i32 %x) {
 ; CHECK-LABEL: @ctlz_ashr_negative(
-; CHECK-NEXT:    [[S:%.*]] = ashr i32 -42, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ctlz.i32(i32 [[S]], i1 false)
-; CHECK-NEXT:    ret i32 [[R]]
+; CHECK-NEXT:    ret i32 0
 ;
   %s = ashr i32 -42, %x
   %r = call i32 @llvm.ctlz.i32(i32 %s, i1 false)
@@ -1465,9 +1457,7 @@ define i32 @ctlz_ashr_negative(i32 %x) {
 
 define <3 x i33> @ctlz_ashr_sign_bit_vec(<3 x i33> %x) {
 ; CHECK-LABEL: @ctlz_ashr_sign_bit_vec(
-; CHECK-NEXT:    [[S:%.*]] = ashr <3 x i33> <i33 -4294967296, i33 undef, i33 -4294967296>, [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = call <3 x i33> @llvm.ctlz.v3i33(<3 x i33> [[S]], i1 true)
-; CHECK-NEXT:    ret <3 x i33> [[R]]
+; CHECK-NEXT:    ret <3 x i33> zeroinitializer
 ;
   %s = ashr <3 x i33> <i33 4294967296, i33 undef, i33 4294967296>, %x
   %r = call <3 x i33> @llvm.ctlz.v3i33(<3 x i33> %s, i1 true)


        


More information about the llvm-commits mailing list