[llvm] [InstCombine] Fold `mul (sext bool X), Y` into `select X, -Y, 0` (PR #84792)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 13 02:01:36 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)))) &&
----------------
SahilPatidar wrote:

```llvm
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
}
```
seems incorrect?

https://github.com/llvm/llvm-project/pull/84792


More information about the llvm-commits mailing list