Skip to content

[InstCombine] Relax one-use requirement for add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN #90509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
}

// add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
// TODO: Relax the one-use checks because we are removing an instruction?
if (match(I, m_Add(m_OneUse(m_SExt(m_Value(X))),
m_OneUse(m_SExt(m_Value(Y))))) &&
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
if (match(I, m_Add(m_SExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
(I->getOperand(0)->hasOneUse() || I->getOperand(1)->hasOneUse())) {

// Truth table for inputs and output signbits:
// X:0 | X:1
// -----------
Expand Down
33 changes: 24 additions & 9 deletions llvm/test/Transforms/InstCombine/add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1435,15 +1435,28 @@ define i32 @and31_add_sexts(i1 %x, i1 %y) {
ret i32 %r
}

; Negative test - extra use

define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {
; CHECK-LABEL: @lshr_add_use_sexts(
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
; CHECK-NEXT: store i32 [[YS]], ptr [[P:%.*]], align 4
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%xs = sext i1 %x to i32
%ys = sext i1 %y to i32
store i32 %ys, ptr %p
%sub = add i32 %xs, %ys
%r = lshr i32 %sub, 31
ret i32 %r
}

define i32 @lshr_add_use_sexts_2(i1 %x, i1 %y, ptr %p) {
; CHECK-LABEL: @lshr_add_use_sexts_2(
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
; CHECK-NEXT: store i32 [[XS]], ptr [[P:%.*]], align 4
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X]], [[Y:%.*]]
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%xs = sext i1 %x to i32
Expand All @@ -1456,18 +1469,20 @@ define i32 @lshr_add_use_sexts(i1 %x, i1 %y, ptr %p) {

; Negative test - extra use

define i32 @lshr_add_use2_sexts(i1 %x, i1 %y, ptr %p) {
; CHECK-LABEL: @lshr_add_use2_sexts(
declare void @use_sexts(i32, i32)

define i32 @lshr_add_use_sexts_both(i1 %x, i1 %y) {
; CHECK-LABEL: @lshr_add_use_sexts_both(
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
; CHECK-NEXT: store i32 [[YS]], ptr [[P:%.*]], align 4
; CHECK-NEXT: call void @use_sexts(i32 [[XS]], i32 [[YS]])
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
; CHECK-NEXT: ret i32 [[R]]
;
%xs = sext i1 %x to i32
%ys = sext i1 %y to i32
store i32 %ys, ptr %p
call void @use_sexts(i32 %xs, i32 %ys)
%sub = add i32 %xs, %ys
%r = lshr i32 %sub, 31
ret i32 %r
Expand Down
Loading