Skip to content

Commit 510fb87

Browse files
authored
[IR][TLI] Cache getLibFunc() result on Function (NFC) (#72867)
Cache the result of the TLI libfunc lookup in the Function object. This only caches the actual lookup of the LibFunc in the TLI map, but not the prototype validation, as that might differ between different TLI instances. This uses the existing mechanism for invalidating the intrinsic ID when the function name changes. The libfunc will be invalidated in that case as well. I don't believe this increases the size of Function on 64bit (which currently has a trailing `bool` member), and I don't think we would particularly care if it did, as Functions are uncommon as Values go.
1 parent 74cdb8e commit 510fb87

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

llvm/include/llvm/IR/Function.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ class AssemblyAnnotationWriter;
4848
class Constant;
4949
struct DenormalMode;
5050
class DISubprogram;
51+
enum LibFunc : unsigned;
5152
class LLVMContext;
5253
class Module;
5354
class raw_ostream;
55+
class TargetLibraryInfoImpl;
5456
class Type;
5557
class User;
5658
class BranchProbabilityInfo;
@@ -120,6 +122,15 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
120122
void setIsNewDbgInfoFormat(bool NewVal);
121123

122124
private:
125+
friend class TargetLibraryInfoImpl;
126+
127+
static constexpr LibFunc UnknownLibFunc = LibFunc(-1);
128+
129+
/// Cache for TLI::getLibFunc() result without prototype validation.
130+
/// UnknownLibFunc if uninitialized. NotLibFunc if definitely not lib func.
131+
/// Otherwise may be libfunc if prototype validation passes.
132+
mutable LibFunc LibFuncCache = UnknownLibFunc;
133+
123134
void CheckLazyArguments() const {
124135
if (hasLazyArguments())
125136
BuildLazyArguments();
@@ -239,12 +250,11 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
239250

240251
static Intrinsic::ID lookupIntrinsicID(StringRef Name);
241252

242-
/// Recalculate the ID for this function if it is an Intrinsic defined
243-
/// in llvm/Intrinsics.h. Sets the intrinsic ID to Intrinsic::not_intrinsic
244-
/// if the name of this function does not match an intrinsic in that header.
253+
/// Update internal caches that depend on the function name (such as the
254+
/// intrinsic ID and libcall cache).
245255
/// Note, this method does not need to be called directly, as it is called
246256
/// from Value::setName() whenever the name of this function changes.
247-
void recalculateIntrinsicID();
257+
void updateAfterNameChange();
248258

249259
/// getCallingConv()/setCallingConv(CC) - These method get and set the
250260
/// calling convention of this function. The enum values for the known

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,15 @@ bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
11381138
const Module *M = FDecl.getParent();
11391139
assert(M && "Expecting FDecl to be connected to a Module.");
11401140

1141-
return getLibFunc(FDecl.getName(), F) &&
1142-
isValidProtoForLibFunc(*FDecl.getFunctionType(), F, *M);
1141+
if (FDecl.LibFuncCache == Function::UnknownLibFunc)
1142+
if (!getLibFunc(FDecl.getName(), FDecl.LibFuncCache))
1143+
FDecl.LibFuncCache = NotLibFunc;
1144+
1145+
if (FDecl.LibFuncCache == NotLibFunc)
1146+
return false;
1147+
1148+
F = FDecl.LibFuncCache;
1149+
return isValidProtoForLibFunc(*FDecl.getFunctionType(), F, *M);
11431150
}
11441151

11451152
void TargetLibraryInfoImpl::disableAllFunctions() {

llvm/lib/IR/Function.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,8 @@ Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
912912
: Intrinsic::not_intrinsic;
913913
}
914914

915-
void Function::recalculateIntrinsicID() {
915+
void Function::updateAfterNameChange() {
916+
LibFuncCache = UnknownLibFunc;
916917
StringRef Name = getName();
917918
if (!Name.startswith("llvm.")) {
918919
HasLLVMReservedName = false;

llvm/lib/IR/Value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void Value::setNameImpl(const Twine &NewName) {
377377
void Value::setName(const Twine &NewName) {
378378
setNameImpl(NewName);
379379
if (Function *F = dyn_cast<Function>(this))
380-
F->recalculateIntrinsicID();
380+
F->updateAfterNameChange();
381381
}
382382

383383
void Value::takeName(Value *V) {

0 commit comments

Comments
 (0)