[llvm] 68ea002 - [InstSimplify] Check the NonZero for power of two value

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 3 18:15:40 PDT 2023


Author: Zhongyunde
Date: 2023-08-04T09:14:45+08:00
New Revision: 68ea002a63ecd89f742d0721e3d12df7840c06cd

URL: https://github.com/llvm/llvm-project/commit/68ea002a63ecd89f742d0721e3d12df7840c06cd
DIFF: https://github.com/llvm/llvm-project/commit/68ea002a63ecd89f742d0721e3d12df7840c06cd.diff

LOG: [InstSimplify] Check the NonZero for power of two value

Fixes https://github.com/llvm/llvm-project/issues/64339

proofs: https://alive2.llvm.org/ce/z/yZ_I2a

Reviewed By: goldstein.w.n
Differential Revision: https://reviews.llvm.org/D156881

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstCombine/rem-mul-shl.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index dcd1cee653b1d0..a0fea0804d6cfb 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2121,7 +2121,7 @@ static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
   Value *Shift;
   if (match(Op1, m_Power2(PowerC)) &&
       match(Op0, m_Add(m_Value(Shift), m_AllOnes())) &&
-      isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
+      isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ false, 0, Q.AC, Q.CxtI,
                              Q.DT)) {
     KnownBits Known = computeKnownBits(Shift, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
     // Use getActiveBits() to make use of the additional power of two knowledge

diff  --git a/llvm/test/Transforms/InstCombine/rem-mul-shl.ll b/llvm/test/Transforms/InstCombine/rem-mul-shl.ll
index 94f2003e76e2f6..257e9762e7b32c 100644
--- a/llvm/test/Transforms/InstCombine/rem-mul-shl.ll
+++ b/llvm/test/Transforms/InstCombine/rem-mul-shl.ll
@@ -2,6 +2,7 @@
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 declare void @use8(i8)
 declare i64 @llvm.vscale.i64()
+declare i32 @llvm.vscale.i32()
 
 define i8 @srem_non_matching(i8 %X, i8 %Y) {
 ; CHECK-LABEL: @srem_non_matching(
@@ -913,3 +914,32 @@ define i64 @and_add_shl_vscale_not_power2() vscale_range(1,16) {
   %rem = and i64 3072, %add
   ret i64 %rem
 }
+
+; Allow for INT_MIN, https://alive2.llvm.org/ce/z/yZ_I2a
+define i32 @and_add_shl_vscale_not_power2_negative() vscale_range(1,16) {
+; CHECK-LABEL: @and_add_shl_vscale_not_power2_negative(
+; CHECK-NEXT:    ret i32 0
+;
+  %vscale = call i32 @llvm.vscale.i32()
+  %shift = shl nuw nsw i32 %vscale, 6
+  %add = add i32 %shift, -1
+  %rem = and i32 -2147483648, %add
+  ret i32 %rem
+}
+
+; Negative test: the %sign may be 0, https://alive2.llvm.org/ce/z/WU_j4a
+define i32 @and_add_and (i32 %x) {
+; CHECK-LABEL: @and_add_and(
+; CHECK-NEXT:    [[X1:%.*]] = lshr i32 [[X:%.*]], 7
+; CHECK-NEXT:    [[SIGN:%.*]] = and i32 [[X1]], 1
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[SIGN]], -1
+; CHECK-NEXT:    [[AND:%.*]] = and i32 [[ADD]], -2147483648
+; CHECK-NEXT:    ret i32 [[AND]]
+;
+  %x1 = lshr i32 %x, 7
+  %sign = and i32 %x1, 1  ; %sign = (%x >> 7) & 1
+  %add = add i32 %sign, -1
+  %and = and i32 %add, 2147483648
+  ret i32 %and
+}
+


        


More information about the llvm-commits mailing list