[llvm] [InstCombine] Fold `mul (sext bool X), Y` into `select X, -Y, 0` (PR #84792)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 12 23:55:00 PDT 2024
================
@@ -448,6 +448,20 @@ 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
+ Value *SExtOp;
+ if (match(Op0, m_OneUse(m_SExt(m_Value(SExtOp)))) &&
----------------
dtcxzyw wrote:
Please add the following tests: https://godbolt.org/z/9856M5M8d
```
define i32 @test_mul_sext_bool(i1 %x, i32 %y) {
%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) {
%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) {
%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) {
%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) {
%yy = xor i32 %y, 1 ; thwart complexity-based canonicalization
%sext = sext i1 %x to i32
%mul = mul i32 %yy, %sext
ret i32 %mul
}
define i32 @test_mul_sext_nonbool(i2 %x, i32 %y) {
%sext = sext i2 %x to i32
%mul = mul i32 %sext, %y
ret i32 %mul
}
define i32 @test_mul_sext_multiuse(i1 %x, i32 %y) {
%sext = sext i1 %x to i32
call void @use(i32 %sext)
%mul = mul i32 %sext, %y
ret i32 %mul
}
declare void @use(i32 %x)
```
https://github.com/llvm/llvm-project/pull/84792
More information about the llvm-commits
mailing list