Skip to content

Commit 43a21ae

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 628283a commit 43a21ae

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-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: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,28 @@ define i32 @and31_add_sexts(i1 %x, i1 %y) {
14351435
ret i32 %r
14361436
}
14371437

1438+
declare void @use_sexts(i32, i32)
1439+
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(
1443+
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
1444+
; CHECK-NEXT: store i32 [[XS]], ptr [[P:%.*]], align 4
1445+
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1446+
; CHECK-NEXT: call void @use_sexts(i32 [[XS]], i32 [[YS]])
1447+
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1448+
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1449+
; CHECK-NEXT: ret i32 [[R]]
1450+
;
1451+
%xs = sext i1 %x to i32
1452+
store i32 %xs, ptr %p
1453+
%ys = sext i1 %y to i32
1454+
call void @use_sexts(i32 %xs, i32 %ys)
1455+
%sub = add i32 %xs, %ys
1456+
%r = lshr i32 %sub, 31
1457+
ret i32 %r
1458+
}
1459+
14381460
; Negative test - extra use
14391461

14401462
define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
@@ -1458,9 +1480,8 @@ define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
14581480
; CHECK-LABEL: @lshr_add_use_sexts(
14591481
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
14601482
; CHECK-NEXT: store i32 [[XS]], ptr [[P:%.*]], align 4
1461-
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1462-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1463-
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1483+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X]], [[Y:%.*]]
1484+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
14641485
; CHECK-NEXT: ret i32 [[R]]
14651486
;
14661487
%xs = sext i1 %x to i32
@@ -1473,11 +1494,10 @@ define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
14731494

14741495
define i32 @lshr_add_use2_sexts(i1 %x, i1 %y, ptr %p) {
14751496
; CHECK-LABEL: @lshr_add_use2_sexts(
1476-
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
14771497
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
14781498
; CHECK-NEXT: store i32 [[YS]], ptr [[P:%.*]], align 4
1479-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1480-
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1499+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y]]
1500+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
14811501
; CHECK-NEXT: ret i32 [[R]]
14821502
;
14831503
%xs = sext i1 %x to i32
@@ -4033,8 +4053,8 @@ define i32 @add_reduce_sqr_sum_varC_invalid2(i32 %a, i32 %b) {
40334053

40344054
define i32 @fold_sext_addition_or_disjoint(i8 %x) {
40354055
; CHECK-LABEL: @fold_sext_addition_or_disjoint(
4036-
; CHECK-NEXT: [[SE:%.*]] = sext i8 [[XX:%.*]] to i32
4037-
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[SE]], 1246
4056+
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
4057+
; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 1246
40384058
; CHECK-NEXT: ret i32 [[R]]
40394059
;
40404060
%xx = or disjoint i8 %x, 12
@@ -4058,8 +4078,8 @@ define i32 @fold_sext_addition_fail(i8 %x) {
40584078

40594079
define i32 @fold_zext_addition_or_disjoint(i8 %x) {
40604080
; CHECK-LABEL: @fold_zext_addition_or_disjoint(
4061-
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX:%.*]] to i32
4062-
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[SE]], 1246
4081+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[X:%.*]] to i32
4082+
; CHECK-NEXT: [[R:%.*]] = add nuw nsw i32 [[TMP1]], 1246
40634083
; CHECK-NEXT: ret i32 [[R]]
40644084
;
40654085
%xx = or disjoint i8 %x, 12
@@ -4070,9 +4090,9 @@ define i32 @fold_zext_addition_or_disjoint(i8 %x) {
40704090

40714091
define i32 @fold_zext_addition_or_disjoint2(i8 %x) {
40724092
; CHECK-LABEL: @fold_zext_addition_or_disjoint2(
4073-
; CHECK-NEXT: [[XX:%.*]] = add nuw i8 [[X:%.*]], 4
4074-
; CHECK-NEXT: [[SE:%.*]] = zext i8 [[XX]] to i32
4075-
; CHECK-NEXT: ret i32 [[SE]]
4093+
; CHECK-NEXT: [[TMP1:%.*]] = add nuw i8 [[X:%.*]], 4
4094+
; CHECK-NEXT: [[R:%.*]] = zext i8 [[TMP1]] to i32
4095+
; CHECK-NEXT: ret i32 [[R]]
40764096
;
40774097
%xx = or disjoint i8 %x, 18
40784098
%se = zext i8 %xx to i32

0 commit comments

Comments
 (0)