[llvm] [IRBuilder] Introduce CreateSelectFMFWithUnknownProfile (PR #174162)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 2 10:06:39 PST 2026


https://github.com/boomanaiden154 updated https://github.com/llvm/llvm-project/pull/174162

>From bb4917ca00c213f052802024b340d1230b69e722 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Thu, 1 Jan 2026 22:32:46 +0000
Subject: [PATCH 1/2] [IRBuilder] Introduce CreateSelectFMFWithUnknownProfile

This came up in review feedback in
c163e7a72249cbc8105fbc5cc6a772e5dc90c94d. This function makes it easier
to create select instructions that propagate fast math flags with
unknown profile info, which is a common case. This mirrors the already
existing CreateSelectWithUknownProfile helper.
---
 llvm/include/llvm/IR/IRBuilder.h                     |  6 ++++++
 llvm/lib/IR/IRBuilder.cpp                            | 12 ++++++++++++
 .../InstCombine/InstCombineSimplifyDemanded.cpp      |  6 ++----
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 972a253344ddf..bc35caabeb849 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -2553,6 +2553,12 @@ class IRBuilderBase {
                                                  StringRef PassName,
                                                  const Twine &Name = "");
 
+  LLVM_ABI Value *CreateSelectFMFWithUnknownProfile(Value *C, Value *True,
+                                                    Value *False,
+                                                    FMFSource FMFSource,
+                                                    StringRef PassName,
+                                                    const Twine &Name = "");
+
   LLVM_ABI Value *CreateSelect(Value *C, Value *True, Value *False,
                                const Twine &Name = "",
                                Instruction *MDFrom = nullptr);
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index a397f6973ee05..6f7ffe9cbe635 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -1014,6 +1014,18 @@ Value *IRBuilderBase::CreateSelectWithUnknownProfile(Value *C, Value *True,
   return Ret;
 }
 
+Value *IRBuilderBase::CreateSelectFMFWithUnknownProfile(Value *C, Value *True,
+                                                        Value *False,
+                                                        FMFSource FMFSource,
+                                                        StringRef PassName,
+                                                        const Twine &Name) {
+  Value *Ret = CreateSelectFMF(C, True, False, FMFSource, Name);
+  if (auto *SI = dyn_cast<SelectInst>(Ret)) {
+    setExplicitlyUnknownBranchWeightsIfProfiled(*SI, PassName);
+  }
+  return Ret;
+}
+
 Value *IRBuilderBase::CreateSelect(Value *C, Value *True, Value *False,
                                    const Twine &Name, Instruction *MDFrom) {
   return CreateSelectFMF(C, True, False, {}, Name, MDFrom);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 47b66a4e0f37d..3ad1caa6baa63 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -2202,12 +2202,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Value *V,
         Value *X = CI->getArgOperand(0);
         Value *IsPosInfOrNan = Builder.CreateFCmpFMF(
             FCmpInst::FCMP_UEQ, X, ConstantFP::getInfinity(VTy), FMF);
-        Value *ZeroOrInf = Builder.CreateSelectFMF(
-            IsPosInfOrNan, X, ConstantFP::getZero(VTy), FMF);
         // We do not know whether an infinity or a NaN is more likely here,
         // so mark the branch weights as unkown.
-        if (auto *SI = dyn_cast<SelectInst>(ZeroOrInf))
-          setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
+        Value *ZeroOrInf = Builder.CreateSelectFMFWithUnknownProfile(
+            IsPosInfOrNan, X, ConstantFP::getZero(VTy), FMF, DEBUG_TYPE);
         return ZeroOrInf;
       }
 

>From 454eaf7803d2d95ad9d29809733571b85dbee1f2 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Fri, 2 Jan 2026 18:06:26 +0000
Subject: [PATCH 2/2] feedbacl

---
 llvm/lib/IR/IRBuilder.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 6f7ffe9cbe635..31e778bbb7911 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -1020,9 +1020,8 @@ Value *IRBuilderBase::CreateSelectFMFWithUnknownProfile(Value *C, Value *True,
                                                         StringRef PassName,
                                                         const Twine &Name) {
   Value *Ret = CreateSelectFMF(C, True, False, FMFSource, Name);
-  if (auto *SI = dyn_cast<SelectInst>(Ret)) {
+  if (auto *SI = dyn_cast<SelectInst>(Ret))
     setExplicitlyUnknownBranchWeightsIfProfiled(*SI, PassName);
-  }
   return Ret;
 }
 



More information about the llvm-commits mailing list