Skip to content

Conversation

@boomanaiden154
Copy link
Contributor

This came up in review feedback in
c163e7a. 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.

This came up in review feedback in
c163e7a. 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.
@boomanaiden154 boomanaiden154 requested a review from arsenm January 1, 2026 22:34
@boomanaiden154 boomanaiden154 requested a review from nikic as a code owner January 1, 2026 22:34
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:ir llvm:transforms labels Jan 1, 2026
@llvmbot
Copy link
Member

llvmbot commented Jan 1, 2026

@llvm/pr-subscribers-llvm-transforms

Author: Aiden Grossman (boomanaiden154)

Changes

This came up in review feedback in
c163e7a. 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.


Full diff: https://github.com/llvm/llvm-project/pull/174162.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/IRBuilder.h (+6)
  • (modified) llvm/lib/IR/IRBuilder.cpp (+12)
  • (modified) llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp (+2-4)
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;
       }
 

@llvmbot
Copy link
Member

llvmbot commented Jan 1, 2026

@llvm/pr-subscribers-llvm-ir

Author: Aiden Grossman (boomanaiden154)

Changes

This came up in review feedback in
c163e7a. 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.


Full diff: https://github.com/llvm/llvm-project/pull/174162.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/IRBuilder.h (+6)
  • (modified) llvm/lib/IR/IRBuilder.cpp (+12)
  • (modified) llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp (+2-4)
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;
       }
 

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@boomanaiden154 boomanaiden154 enabled auto-merge (squash) January 2, 2026 18:06
@boomanaiden154 boomanaiden154 merged commit b45c20d into llvm:main Jan 2, 2026
9 of 10 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jan 6, 2026
This came up in review feedback in
c163e7a. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:ir llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants