[llvm] [InstCombine] Fix for folding `select` into floating point binary operators. (PR #83200)

Michele Scandale via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 14 11:08:33 PDT 2024


================
@@ -536,19 +536,24 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
     // between 0, 1 and -1.
     const APInt *OOpC;
     bool OOpIsAPInt = match(OOp, m_APInt(OOpC));
-    if (!isa<Constant>(OOp) ||
-        (OOpIsAPInt && isSelect01(C->getUniqueInteger(), *OOpC))) {
-      Value *NewSel = Builder.CreateSelect(SI.getCondition(), Swapped ? C : OOp,
-                                           Swapped ? OOp : C, "", &SI);
-      if (isa<FPMathOperator>(&SI))
-        cast<Instruction>(NewSel)->setFastMathFlags(FMF);
-      NewSel->takeName(TVI);
-      BinaryOperator *BO =
-          BinaryOperator::Create(TVI->getOpcode(), FalseVal, NewSel);
-      BO->copyIRFlags(TVI);
-      return BO;
-    }
-    return nullptr;
+    if (isa<Constant>(OOp) &&
+        (!OOpIsAPInt || !isSelect01(C->getUniqueInteger(), *OOpC)))
+      return nullptr;
+
+    // Cannot fold if the false value might be a NaN.
----------------
michele-scandale wrote:

Updated the comment. I'll wait few days before landing the change in case there are further comments. Thanks.

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


More information about the llvm-commits mailing list