[llvm] [InstCombine] (uitofp bool X) * Y --> X ? Y : 0 (PR #96216)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 02:32:00 PDT 2024


================
@@ -879,6 +879,16 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
     if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
       return BinaryOperator::CreateFMulFMF(X, NegC, &I);
 
+  if (I.hasNoNaNs() && I.hasNoSignedZeros()) {
+    // (uitofp bool X) * Y --> X ? Y : 0
+    // Y * (uitofp bool X) --> X ? Y : 0
+    // Note INF * 0 is NaN.
+    if (match(Op0, m_UIToFP(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
+      return SelectInst::Create(X, Op1, ConstantFP::get(I.getType(), 0.0));
+    if (match(Op1, m_UIToFP(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
+      return SelectInst::Create(X, Op0, ConstantFP::get(I.getType(), 0.0));
+  }
+
----------------
arsenm wrote:

Not obvious to me if the form with copysign is better or not, it's 2 instructions -> 2 instructions 

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


More information about the llvm-commits mailing list