[llvm] [IR] Make intrinsic checks more efficient (NFC) (PR #148682)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 14 10:30:09 PDT 2025


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/148682

Directly cast the callee operand instead of going through getCalledFunction(). We can do this because for intrinsics the function type between the call and the function is guaranteed to match.

This is a minor compile-time improvement as is, but has a much bigger impact with a future change that makes getCalledFunction() more expensive.

There is some code duplication between these four uses, but they are each just different enough that representing one in terms of another would be less efficient.

Compile-time: https://llvm-compile-time-tracker.com/compare.php?from=65d20bb0ab27f1b0078cc8811d02f7b47eb779bf&to=bd48e3670dd5100808deb1b74294e6bcd4966d3f&stat=instructions:u

>From c85894cad7c7f2834d39dfa792a5d0fd1b580c41 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 14 Jul 2025 16:58:27 +0200
Subject: [PATCH] [IR] Make intrinsic checks more efficient (NFC)

Directly cast the callee operand instead of going through
getCalledFunction(). We can do this because for intrinsics the
function type between the call and the function is guaranteed to
match.

This is a minor compile-time improvement as is, but has a much
bigger impact with a future change that makes getCalledFunction()
more expensive.
---
 llvm/include/llvm/IR/IntrinsicInst.h | 7 +++----
 llvm/include/llvm/IR/PatternMatch.h  | 2 +-
 llvm/lib/IR/Instructions.cpp         | 2 +-
 3 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/IR/IntrinsicInst.h b/llvm/include/llvm/IR/IntrinsicInst.h
index d8f8bdeb01d43..0318427bf8a1c 100644
--- a/llvm/include/llvm/IR/IntrinsicInst.h
+++ b/llvm/include/llvm/IR/IntrinsicInst.h
@@ -54,7 +54,7 @@ class IntrinsicInst : public CallInst {
 
   /// Return the intrinsic ID of this intrinsic.
   Intrinsic::ID getIntrinsicID() const {
-    return getCalledFunction()->getIntrinsicID();
+    return cast<Function>(getCalledOperand())->getIntrinsicID();
   }
 
   bool isAssociative() const {
@@ -131,9 +131,8 @@ class IntrinsicInst : public CallInst {
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static bool classof(const CallInst *I) {
-    if (const Function *CF = I->getCalledFunction())
-      return CF->isIntrinsic();
-    return false;
+    auto *F = dyn_cast_or_null<Function>(I->getCalledOperand());
+    return F && F->isIntrinsic();
   }
   static bool classof(const Value *V) {
     return isa<CallInst>(V) && classof(cast<CallInst>(V));
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index ed9b83d5d4361..50e50a91389e2 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -2633,7 +2633,7 @@ struct IntrinsicID_match {
 
   template <typename OpTy> bool match(OpTy *V) const {
     if (const auto *CI = dyn_cast<CallInst>(V))
-      if (const auto *F = CI->getCalledFunction())
+      if (const auto *F = dyn_cast_or_null<Function>(CI->getCalledOperand()))
         return F->getIntrinsicID() == ID;
     return false;
   }
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 9fe79366f9b2f..b8963823f1c67 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -355,7 +355,7 @@ bool CallBase::isTailCall() const {
 }
 
 Intrinsic::ID CallBase::getIntrinsicID() const {
-  if (auto *F = getCalledFunction())
+  if (auto *F = dyn_cast_or_null<Function>(getCalledOperand()))
     return F->getIntrinsicID();
   return Intrinsic::not_intrinsic;
 }



More information about the llvm-commits mailing list