Skip to content

Commit de4321e

Browse files
committed
[InstCombine] Remove one-use requirement for add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
Since these remove instructions, we can forgo the one-use check, as long as one of the two sexts are one-use.
1 parent 326667d commit de4321e

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
510510
}
511511

512512
// add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
513-
// TODO: Relax the one-use checks because we are removing an instruction?
514-
if (match(I, m_Add(m_OneUse(m_SExt(m_Value(X))),
515-
m_OneUse(m_SExt(m_Value(Y))))) &&
516-
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
513+
if (match(I, m_Add(m_SExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
514+
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
515+
(I->getOperand(0)->hasOneUse() || I->getOperand(1)->hasOneUse())) {
516+
517517
// Truth table for inputs and output signbits:
518518
// X:0 | X:1
519519
// -----------

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,34 +1435,52 @@ define i32 @and31_add_sexts(i1 %x, i1 %y) {
14351435
ret i32 %r
14361436
}
14371437

1438-
; Negative test - extra use
1438+
declare void @use_sexts(i32, i32)
14391439

1440-
define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
1441-
; CHECK-LABEL: @lshr_add_use_sexts(
1440+
; Negative test
1441+
define i32 @lshr_add_use_sexts_both(i1 %x, i1 %y, ptr %p) {
1442+
; CHECK-LABEL: @lshr_add_use_sexts_both(
14421443
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
14431444
; CHECK-NEXT: store i32 [[XS]], ptr [[P:%.*]], align 4
14441445
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1446+
; CHECK-NEXT: call void @use_sexts(i32 [[XS]], i32 [[YS]])
14451447
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
14461448
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
14471449
; CHECK-NEXT: ret i32 [[R]]
14481450
;
14491451
%xs = sext i1 %x to i32
14501452
store i32 %xs, ptr %p
14511453
%ys = sext i1 %y to i32
1454+
call void @use_sexts(i32 %xs, i32 %ys)
14521455
%sub = add i32 %xs, %ys
14531456
%r = lshr i32 %sub, 31
14541457
ret i32 %r
14551458
}
14561459

14571460
; Negative test - extra use
14581461

1462+
define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
1463+
; CHECK-LABEL: @lshr_add_use_sexts(
1464+
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
1465+
; CHECK-NEXT: store i32 [[XS]], ptr [[P:%.*]], align 4
1466+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X]], [[Y:%.*]]
1467+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
1468+
; CHECK-NEXT: ret i32 [[R]]
1469+
;
1470+
%xs = sext i1 %x to i32
1471+
store i32 %xs, ptr %p
1472+
%ys = sext i1 %y to i32
1473+
%sub = add i32 %xs, %ys
1474+
%r = lshr i32 %sub, 31
1475+
ret i32 %r
1476+
}
1477+
14591478
define i32 @lshr_add_use2_sexts(i1 %x, i1 %y, ptr %p) {
14601479
; CHECK-LABEL: @lshr_add_use2_sexts(
1461-
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
14621480
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
14631481
; CHECK-NEXT: store i32 [[YS]], ptr [[P:%.*]], align 4
1464-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1465-
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1482+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y]]
1483+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
14661484
; CHECK-NEXT: ret i32 [[R]]
14671485
;
14681486
%xs = sext i1 %x to i32
@@ -4018,8 +4036,8 @@ define i32 @add_reduce_sqr_sum_varC_invalid2(i32 %a, i32 %b) {
40184036

40194037
define i32 @fold_sext_addition_or_disjoint(i8 %x) {
40204038
; CHECK-LABEL: @fold_sext_addition_or_disjoint(
4021-
; CHECK-NEXT: [[SE:%.*]] = sext i8 [[XX:%.*]] to i32
4022-
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], 1246
4039+
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
4040+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 1246
40234041
; CHECK-NEXT: ret i32 [[R]]
40244042
;
40254043
%xx = or disjoint i8 %x, 12
@@ -4043,8 +4061,8 @@ define i32 @fold_sext_addition_fail(i8 %x) {
40434061

40444062
define i32 @fold_zext_addition_or_disjoint(i8 %x) {
40454063
; CHECK-LABEL: @fold_zext_addition_or_disjoint(
4046-
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX:%.*]] to i32
4047-
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[SE]], 1246
4064+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[X:%.*]] to i32
4065+
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[TMP1]], 1246
40484066
; CHECK-NEXT: ret i32 [[R]]
40494067
;
40504068
%xx = or disjoint i8 %x, 12
@@ -4055,9 +4073,9 @@ define i32 @fold_zext_addition_or_disjoint(i8 %x) {
40554073

40564074
define i32 @fold_zext_addition_or_disjoint2(i8 %x) {
40574075
; CHECK-LABEL: @fold_zext_addition_or_disjoint2(
4058-
; CHECK-NEXT: [[XX:%.*]] = add nuw i8 [[X:%.*]], 4
4059-
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
4060-
; CHECK-NEXT: ret i32 [[SE]]
4076+
; CHECK-NEXT: [[TMP1:%.*]] = add nuw i8 [[X:%.*]], 4
4077+
; CHECK-NEXT: [[R:%.*]] = zext i8 [[TMP1]] to i32
4078+
; CHECK-NEXT: ret i32 [[R]]
40614079
;
40624080
%xx = or disjoint i8 %x, 18
40634081
%se = zext i8 %xx to i32

0 commit comments

Comments
 (0)