Skip to content

Commit 68ea002

Browse files
committed
[InstSimplify] Check the NonZero for power of two value
Fixes llvm#64339 proofs: https://alive2.llvm.org/ce/z/yZ_I2a Reviewed By: goldstein.w.n Differential Revision: https://reviews.llvm.org/D156881
1 parent 9c837b7 commit 68ea002

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
21212121
Value *Shift;
21222122
if (match(Op1, m_Power2(PowerC)) &&
21232123
match(Op0, m_Add(m_Value(Shift), m_AllOnes())) &&
2124-
isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
2124+
isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ false, 0, Q.AC, Q.CxtI,
21252125
Q.DT)) {
21262126
KnownBits Known = computeKnownBits(Shift, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
21272127
// Use getActiveBits() to make use of the additional power of two knowledge

llvm/test/Transforms/InstCombine/rem-mul-shl.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
33
declare void @use8(i8)
44
declare i64 @llvm.vscale.i64()
5+
declare i32 @llvm.vscale.i32()
56

67
define i8 @srem_non_matching(i8 %X, i8 %Y) {
78
; CHECK-LABEL: @srem_non_matching(
@@ -913,3 +914,32 @@ define i64 @and_add_shl_vscale_not_power2() vscale_range(1,16) {
913914
%rem = and i64 3072, %add
914915
ret i64 %rem
915916
}
917+
918+
; Allow for INT_MIN, https://alive2.llvm.org/ce/z/yZ_I2a
919+
define i32 @and_add_shl_vscale_not_power2_negative() vscale_range(1,16) {
920+
; CHECK-LABEL: @and_add_shl_vscale_not_power2_negative(
921+
; CHECK-NEXT: ret i32 0
922+
;
923+
%vscale = call i32 @llvm.vscale.i32()
924+
%shift = shl nuw nsw i32 %vscale, 6
925+
%add = add i32 %shift, -1
926+
%rem = and i32 -2147483648, %add
927+
ret i32 %rem
928+
}
929+
930+
; Negative test: the %sign may be 0, https://alive2.llvm.org/ce/z/WU_j4a
931+
define i32 @and_add_and (i32 %x) {
932+
; CHECK-LABEL: @and_add_and(
933+
; CHECK-NEXT: [[X1:%.*]] = lshr i32 [[X:%.*]], 7
934+
; CHECK-NEXT: [[SIGN:%.*]] = and i32 [[X1]], 1
935+
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SIGN]], -1
936+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[ADD]], -2147483648
937+
; CHECK-NEXT: ret i32 [[AND]]
938+
;
939+
%x1 = lshr i32 %x, 7
940+
%sign = and i32 %x1, 1 ; %sign = (%x >> 7) & 1
941+
%add = add i32 %sign, -1
942+
%and = and i32 %add, 2147483648
943+
ret i32 %and
944+
}
945+

0 commit comments

Comments
 (0)