Skip to content

Commit 4c241a9

Browse files
committed
[InstCombine] Fold (-1 + A) & B into A ? 0 : B where A is effectively a bool
Solves issue #63321. This patch explicitly folds `(-1 + A) & B` into `A ? 0 : B`. Additional trunc will be created when `A` is neither i1 nor <N x i1>. https://alive2.llvm.org/ce/z/pWv9jJ Reviewed By: goldstein.w.n Differential Revision: https://reviews.llvm.org/D153148
1 parent 71d2258 commit 4c241a9

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,18 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
26542654
A->getType()->isIntOrIntVectorTy(1))
26552655
return SelectInst::Create(A, Constant::getNullValue(Ty), B);
26562656

2657+
// (-1 + A) & B --> A ? 0 : B where A is 0/1.
2658+
if (match(&I, m_c_And(m_OneUse(m_Add(m_ZExtOrSelf(m_Value(A)), m_AllOnes())),
2659+
m_Value(B)))) {
2660+
if (A->getType()->isIntOrIntVectorTy(1))
2661+
return SelectInst::Create(A, Constant::getNullValue(Ty), B);
2662+
if (computeKnownBits(A, /* Depth */ 0, &I).countMaxActiveBits() <= 1) {
2663+
return SelectInst::Create(
2664+
Builder.CreateICmpEQ(A, Constant::getNullValue(A->getType())), B,
2665+
Constant::getNullValue(Ty));
2666+
}
2667+
}
2668+
26572669
// (iN X s>> (N-1)) & Y --> (X s< 0) ? Y : 0 -- with optional sext
26582670
if (match(&I, m_c_And(m_OneUse(m_SExtOrSelf(
26592671
m_AShr(m_Value(X), m_APIntAllowUndef(C)))),

llvm/test/Transforms/InstCombine/binop-cast.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ define i32 @xor_sext_to_sel_multi_use_constant_mask(i1 %y) {
261261
define i64 @PR63321(ptr %ptr, i64 %c) {
262262
; CHECK-LABEL: @PR63321(
263263
; CHECK-NEXT: [[VAL:%.*]] = load i8, ptr [[PTR:%.*]], align 1, !range [[RNG0:![0-9]+]]
264-
; CHECK-NEXT: [[RHS:%.*]] = zext i8 [[VAL]] to i64
265-
; CHECK-NEXT: [[MASK:%.*]] = add nsw i64 [[RHS]], -1
266-
; CHECK-NEXT: [[RES:%.*]] = and i64 [[MASK]], [[C:%.*]]
264+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[VAL]], 0
265+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[TMP1]], i64 [[C:%.*]], i64 0
267266
; CHECK-NEXT: ret i64 [[RES]]
268267
;
269268
%val = load i8, ptr %ptr, align 1, !range !{i8 0, i8 2}
@@ -300,12 +299,11 @@ define i32 @and_add_bool_to_select(i1 %x, i32 %y) {
300299
ret i32 %res
301300
}
302301

303-
; Negative test of and_add_bool_to_select
304302
define i32 @and_add_bool_no_fold(i32 %y) {
305303
; CHECK-LABEL: @and_add_bool_no_fold(
306304
; CHECK-NEXT: [[X:%.*]] = and i32 [[Y:%.*]], 1
307-
; CHECK-NEXT: [[MASK:%.*]] = add nsw i32 [[X]], -1
308-
; CHECK-NEXT: [[RES:%.*]] = and i32 [[MASK]], [[Y]]
305+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X]], 0
306+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[TMP1]], i32 [[Y]], i32 0
309307
; CHECK-NEXT: ret i32 [[RES]]
310308
;
311309
%x = and i32 %y, 1

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,9 @@ define i32 @and_add_shl_vscale_not_power2_negative() vscale_range(1,16) {
930930
; Negative test: the %sign may be 0, https://alive2.llvm.org/ce/z/WU_j4a
931931
define i32 @and_add_and (i32 %x) {
932932
; 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
933+
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[X:%.*]], 24
934+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], -2147483648
935+
; CHECK-NEXT: [[AND:%.*]] = xor i32 [[TMP2]], -2147483648
937936
; CHECK-NEXT: ret i32 [[AND]]
938937
;
939938
%x1 = lshr i32 %x, 7

0 commit comments

Comments
 (0)