Skip to content

Commit 03c8939

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 at least ONE of the two sexts are one-use.
1 parent d60bb01 commit 03c8939

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

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

501501
// add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
502-
// TODO: Relax the one-use checks because we are removing an instruction?
503-
if (match(I, m_Add(m_OneUse(m_SExt(m_Value(X))),
504-
m_OneUse(m_SExt(m_Value(Y))))) &&
505-
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
502+
if (match(I, m_Add(m_SExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
503+
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
504+
(I->getOperand(0)->hasOneUse() || I->getOperand(1)->hasOneUse())) {
505+
506506
// Truth table for inputs and output signbits:
507507
// X:0 | X:1
508508
// -----------

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,11 +1437,10 @@ define i32 @and31_add_sexts(i1 %x, i1 %y) {
14371437

14381438
define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
14391439
; CHECK-LABEL: @lshr_add_use_sexts(
1440-
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
14411440
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
14421441
; CHECK-NEXT: store i32 [[YS]], ptr [[P:%.*]], align 4
1443-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1444-
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1442+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y]]
1443+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
14451444
; CHECK-NEXT: ret i32 [[R]]
14461445
;
14471446
%xs = sext i1 %x to i32
@@ -1456,9 +1455,8 @@ define i32 @lshr_add_use_sexts_2(i1 %x, i1 %y, ptr %p) {
14561455
; CHECK-LABEL: @lshr_add_use_sexts_2(
14571456
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
14581457
; CHECK-NEXT: store i32 [[XS]], ptr [[P:%.*]], align 4
1459-
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1460-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1461-
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1458+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X]], [[Y:%.*]]
1459+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
14621460
; CHECK-NEXT: ret i32 [[R]]
14631461
;
14641462
%xs = sext i1 %x to i32

0 commit comments

Comments
 (0)