[llvm] [InstCombine] Optimize x * !x to 0 for vector #84608 (PR #84792)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 23:21:57 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)))) &&
+      SExtOp->getType()->isIntOrIntVectorTy(1)) {
+    return SelectInst::Create(SExtOp, Builder.CreateNeg(Op1),
+                              ConstantInt::getNullValue(Op1->getType()));
+  }
+  if (match(Op1, m_OneUse(m_SExt(m_Value(SExtOp)))) &&
+      SExtOp->getType()->isIntOrIntVectorTy(1)) {
+    return SelectInst::Create(SExtOp, Builder.CreateNeg(Op0),
+                              ConstantInt::getNullValue(Op0->getType()));
----------------
goldsteinn wrote:

Instead of dupicating logic other Op0/Op1 you could do:

```
if(match(&I, m_c_Mul(m_OneUse(m_SExt(m_Value(SExtOp))), m_Value(OtherOp) && SExtOp->getType()->isIntOrIntVectorTy(1)) {
  return SelectInst::Create(SExtOp, Builder.CreateNeg(OtherOp), Null);
}
```

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


More information about the llvm-commits mailing list