Skip to content

Commit

Permalink
[InstCombine] Fold mul (sext bool X), Y into select X, -Y, 0 (#84792
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilPatidar authored Mar 15, 2024
1 parent ff2fb2a commit e61e260
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
8 changes: 8 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,14 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
return SelectInst::Create(X, Op0, ConstantInt::getNullValue(Ty));

// mul (sext X), Y -> select X, -Y, 0
// mul Y, (sext X) -> select X, -Y, 0
if (match(&I, m_c_Mul(m_OneUse(m_SExt(m_Value(X))), m_Value(Y))) &&
X->getType()->isIntOrIntVectorTy(1))
return SelectInst::Create(
X, Builder.CreateNeg(Y, "", /*HasNUW=*/false, I.hasNoSignedWrap()),
ConstantInt::getNullValue(Op0->getType()));

Constant *ImmC;
if (match(Op1, m_ImmConstant(ImmC))) {
// (sext bool X) * C --> X ? -C : 0
Expand Down
91 changes: 91 additions & 0 deletions llvm/test/Transforms/InstCombine/mul.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2049,3 +2049,94 @@ define i32 @zext_negpow2_use(i8 %x) {
%r = mul i32 %zx, -16777216 ; -1 << 24
ret i32 %r
}

define i32 @mul_sext_icmp_with_zero(i32 %x) {
; CHECK-LABEL: @mul_sext_icmp_with_zero(
; CHECK-NEXT: ret i32 0
;
%cmp = icmp eq i32 %x, 0
%sext = sext i1 %cmp to i32
%mul = mul i32 %sext, %x
ret i32 %mul
}

define i32 @test_mul_sext_bool(i1 %x, i32 %y) {
; CHECK-LABEL: @test_mul_sext_bool(
; CHECK-NEXT: [[Y_NEG:%.*]] = sub i32 0, [[Y:%.*]]
; CHECK-NEXT: [[MUL:%.*]] = select i1 [[X:%.*]], i32 [[Y_NEG]], i32 0
; CHECK-NEXT: ret i32 [[MUL]]
;
%sext = sext i1 %x to i32
%mul = mul i32 %sext, %y
ret i32 %mul
}

define i32 @test_mul_sext_bool_nuw(i1 %x, i32 %y) {
; CHECK-LABEL: @test_mul_sext_bool_nuw(
; CHECK-NEXT: [[Y_NEG:%.*]] = sub i32 0, [[Y:%.*]]
; CHECK-NEXT: [[MUL:%.*]] = select i1 [[X:%.*]], i32 [[Y_NEG]], i32 0
; CHECK-NEXT: ret i32 [[MUL]]
;
%sext = sext i1 %x to i32
%mul = mul nuw i32 %sext, %y
ret i32 %mul
}

define i32 @test_mul_sext_bool_nsw(i1 %x, i32 %y) {
; CHECK-LABEL: @test_mul_sext_bool_nsw(
; CHECK-NEXT: [[Y_NEG:%.*]] = sub nsw i32 0, [[Y:%.*]]
; CHECK-NEXT: [[MUL:%.*]] = select i1 [[X:%.*]], i32 [[Y_NEG]], i32 0
; CHECK-NEXT: ret i32 [[MUL]]
;
%sext = sext i1 %x to i32
%mul = mul nsw i32 %sext, %y
ret i32 %mul
}

define i32 @test_mul_sext_bool_nuw_nsw(i1 %x, i32 %y) {
; CHECK-LABEL: @test_mul_sext_bool_nuw_nsw(
; CHECK-NEXT: [[Y_NEG:%.*]] = sub nsw i32 0, [[Y:%.*]]
; CHECK-NEXT: [[MUL:%.*]] = select i1 [[X:%.*]], i32 [[Y_NEG]], i32 0
; CHECK-NEXT: ret i32 [[MUL]]
;
%sext = sext i1 %x to i32
%mul = mul nuw nsw i32 %sext, %y
ret i32 %mul
}

define i32 @test_mul_sext_bool_commuted(i1 %x, i32 %y) {
; CHECK-LABEL: @test_mul_sext_bool_commuted(
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[Y:%.*]], -2
; CHECK-NEXT: [[YY_NEG1:%.*]] = add i32 [[TMP1]], 1
; CHECK-NEXT: [[MUL:%.*]] = select i1 [[X:%.*]], i32 [[YY_NEG1]], i32 0
; CHECK-NEXT: ret i32 [[MUL]]
;
%yy = xor i32 %y, 1
%sext = sext i1 %x to i32
%mul = mul i32 %yy, %sext
ret i32 %mul
}

define i32 @test_mul_sext_nonbool(i2 %x, i32 %y) {
; CHECK-LABEL: @test_mul_sext_nonbool(
; CHECK-NEXT: [[SEXT:%.*]] = sext i2 [[X:%.*]] to i32
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[SEXT]], [[Y:%.*]]
; CHECK-NEXT: ret i32 [[MUL]]
;
%sext = sext i2 %x to i32
%mul = mul i32 %sext, %y
ret i32 %mul
}

define i32 @test_mul_sext_multiuse(i1 %x, i32 %y) {
; CHECK-LABEL: @test_mul_sext_multiuse(
; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[X:%.*]] to i32
; CHECK-NEXT: tail call void @use(i32 [[SEXT]])
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[SEXT]], [[Y:%.*]]
; CHECK-NEXT: ret i32 [[MUL]]
;
%sext = sext i1 %x to i32
tail call void @use(i32 %sext)
%mul = mul i32 %sext, %y
ret i32 %mul
}
8 changes: 3 additions & 5 deletions llvm/test/Transforms/InstCombine/sub-xor-cmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ define i64 @sext_diff_i1_xor_sub_1(i64 %a, i1 %b, i1 %c) {
define i64 @sext_multi_uses(i64 %a, i1 %b, i64 %x) {
; CHECK-LABEL: define i64 @sext_multi_uses(
; CHECK-SAME: i64 [[A:%.*]], i1 [[B:%.*]], i64 [[X:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = sext i1 [[B]] to i64
; CHECK-NEXT: [[TMP1:%.*]] = sub i64 0, [[A]]
; CHECK-NEXT: [[E:%.*]] = select i1 [[B]], i64 [[TMP1]], i64 [[A]]
; CHECK-NEXT: [[F:%.*]] = mul i64 [[C]], [[X]]
; CHECK-NEXT: [[R:%.*]] = add i64 [[F]], [[E]]
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[X]], [[A]]
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 0, [[TMP1]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[B]], i64 [[TMP2]], i64 [[A]]
; CHECK-NEXT: ret i64 [[R]]
;
%c = sext i1 %b to i64
Expand Down

0 comments on commit e61e260

Please sign in to comment.