[llvm-branch-commits] [llvm] [AMDGPU] Fold fmul with zero checks to fmul.legacy (PR #229357)

Steffen Larsen via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 18 23:15:38 PST 2026


================
@@ -1578,11 +1582,68 @@ bool AMDGPUCodeGenPrepareImpl::visitLoadInst(LoadInst &I) {
   return false;
 }
 
+// select (X == 0 || Y == 0), +0.0, (X * Y) -> fmul_legacy(X, Y)
+bool AMDGPUCodeGenPrepareImpl::foldSelectToFMulLegacy(SelectInst &I) const {
+  if (!I.getType()->isFloatTy())
+    return false;
+
+  // Inverted form: select (X != 0 && Y != 0), X * Y, +0.0.
+  bool Inverted = match(I.getFalseValue(), m_PosZeroFP());
+  if (!Inverted && !match(I.getTrueValue(), m_PosZeroFP()))
+    return false;
+
+  Value *Mul = Inverted ? I.getTrueValue() : I.getFalseValue();
+  Value *X, *Y;
+  if (!match(Mul, m_OneUse(m_FMul(m_Value(X), m_Value(Y)))))
+    return false;
+
+  FCmpInst::Predicate ZeroCmpPred =
+      Inverted ? FCmpInst::FCMP_UNE : FCmpInst::FCMP_OEQ;
+  Value *A, *B;
+  auto ZeroCmpA = m_SpecificFCmp(ZeroCmpPred, m_Value(A), m_AnyZeroFP());
+  auto ZeroCmpB = m_SpecificFCmp(ZeroCmpPred, m_Value(B), m_AnyZeroFP());
+  Value *Cond = I.getCondition();
+  if (Inverted ? !match(Cond, m_And(ZeroCmpA, ZeroCmpB))
+               : !match(Cond, m_Or(ZeroCmpA, ZeroCmpB))) {
+    if (!match(Cond, ZeroCmpA))
+      return false;
+    B = A;
+  }
----------------
steffenlarsen wrote:

This part could use a small comment.

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


More information about the llvm-branch-commits mailing list