[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:30:15 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:

You don't need nsz if you also introduce a copysign:

https://alive2.llvm.org/ce/z/Be9G_s



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


More information about the llvm-commits mailing list