Skip to content

Commit f83858f

Browse files
[nfc][InstrProfiling]Compute a boolean state as a constant and use it everywhere (#83756)
1 parent 57a7208 commit f83858f

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,33 @@ cl::opt<bool> SkipRetExitBlock(
168168

169169
using LoadStorePair = std::pair<Instruction *, Instruction *>;
170170

171+
static uint64_t getIntModuleFlagOrZero(const Module &M, StringRef Flag) {
172+
auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M.getModuleFlag(Flag));
173+
if (!MD)
174+
return 0;
175+
176+
// If the flag is a ConstantAsMetadata, it should be an integer representable
177+
// in 64-bits.
178+
return cast<ConstantInt>(MD->getValue())->getZExtValue();
179+
}
180+
181+
static bool enablesValueProfiling(const Module &M) {
182+
return isIRPGOFlagSet(&M) ||
183+
getIntModuleFlagOrZero(M, "EnableValueProfiling") != 0;
184+
}
185+
186+
// Conservatively returns true if value profiling is enabled.
187+
static bool profDataReferencedByCode(const Module &M) {
188+
return enablesValueProfiling(M);
189+
}
190+
171191
class InstrLowerer final {
172192
public:
173193
InstrLowerer(Module &M, const InstrProfOptions &Options,
174194
std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
175195
bool IsCS)
176196
: M(M), Options(Options), TT(Triple(M.getTargetTriple())), IsCS(IsCS),
177-
GetTLI(GetTLI) {}
197+
GetTLI(GetTLI), DataReferencedByCode(profDataReferencedByCode(M)) {}
178198

179199
bool lower();
180200

@@ -186,6 +206,8 @@ class InstrLowerer final {
186206
const bool IsCS;
187207

188208
std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
209+
210+
const bool DataReferencedByCode;
189211
struct PerFunctionProfileData {
190212
uint32_t NumValueSites[IPVK_Last + 1] = {};
191213
GlobalVariable *RegionCounters = nullptr;
@@ -1057,26 +1079,6 @@ static std::string getVarName(InstrProfInstBase *Inc, StringRef Prefix,
10571079
return (Prefix + Name + "." + Twine(FuncHash)).str();
10581080
}
10591081

1060-
static uint64_t getIntModuleFlagOrZero(const Module &M, StringRef Flag) {
1061-
auto *MD = dyn_cast_or_null<ConstantAsMetadata>(M.getModuleFlag(Flag));
1062-
if (!MD)
1063-
return 0;
1064-
1065-
// If the flag is a ConstantAsMetadata, it should be an integer representable
1066-
// in 64-bits.
1067-
return cast<ConstantInt>(MD->getValue())->getZExtValue();
1068-
}
1069-
1070-
static bool enablesValueProfiling(const Module &M) {
1071-
return isIRPGOFlagSet(&M) ||
1072-
getIntModuleFlagOrZero(M, "EnableValueProfiling") != 0;
1073-
}
1074-
1075-
// Conservatively returns true if data variables may be referenced by code.
1076-
static bool profDataReferencedByCode(const Module &M) {
1077-
return enablesValueProfiling(M);
1078-
}
1079-
10801082
static inline bool shouldRecordFunctionAddr(Function *F) {
10811083
// Only record function addresses if IR PGO is enabled or if clang value
10821084
// profiling is enabled. Recording function addresses greatly increases object
@@ -1192,7 +1194,6 @@ static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
11921194

11931195
void InstrLowerer::maybeSetComdat(GlobalVariable *GV, Function *Fn,
11941196
StringRef VarName) {
1195-
bool DataReferencedByCode = profDataReferencedByCode(M);
11961197
bool NeedComdat = needsComdatForCounter(*Fn, M);
11971198
bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
11981199

@@ -1418,7 +1419,6 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
14181419
Visibility = GlobalValue::DefaultVisibility;
14191420
}
14201421

1421-
bool DataReferencedByCode = profDataReferencedByCode(M);
14221422
bool NeedComdat = needsComdatForCounter(*Fn, M);
14231423
bool Renamed;
14241424

@@ -1719,7 +1719,7 @@ void InstrLowerer::emitUses() {
17191719
// and ensure this GC property as well. Otherwise, we have to conservatively
17201720
// make all of the sections retained by the linker.
17211721
if (TT.isOSBinFormatELF() || TT.isOSBinFormatMachO() ||
1722-
(TT.isOSBinFormatCOFF() && !profDataReferencedByCode(M)))
1722+
(TT.isOSBinFormatCOFF() && !DataReferencedByCode))
17231723
appendToCompilerUsed(M, CompilerUsedVars);
17241724
else
17251725
appendToUsed(M, CompilerUsedVars);

0 commit comments

Comments
 (0)