aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/InlineCost.cpp
diff options
context:
space:
mode:
authorMatthias Braun <matze@braunis.de>2023-09-14 11:11:27 -0700
committerGitHub <noreply@github.com>2023-09-14 11:11:27 -0700
commitb0c8c454238b495ebe4df25a161a120d1707c230 (patch)
treeb985db632cc8d541c3e0dd642df4b828fe935874 /llvm/lib/Analysis/InlineCost.cpp
parente80a8b4ab61ef6aba119adf5f64a4644b7e0a44b (diff)
downloadllvm-b0c8c454238b495ebe4df25a161a120d1707c230.zip
llvm-b0c8c454238b495ebe4df25a161a120d1707c230.tar.gz
llvm-b0c8c454238b495ebe4df25a161a120d1707c230.tar.bz2
Avoid BlockFrequency overflow problems (#66280)
Multiplying raw block frequency with an integer carries a high risk of overflow. - Add `BlockFrequency::mul` return an std::optional with the product or `nullopt` to indicate an overflow. - Fix two instances where overflow was likely.
Diffstat (limited to 'llvm/lib/Analysis/InlineCost.cpp')
-rw-r--r--llvm/lib/Analysis/InlineCost.cpp11
1 files changed, 6 insertions, 5 deletions
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index a9de1dd..84dc412 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -118,7 +118,7 @@ static cl::opt<int> ColdCallSiteRelFreq(
"entry frequency, for a callsite to be cold in the absence of "
"profile information."));
-static cl::opt<int> HotCallSiteRelFreq(
+static cl::opt<uint64_t> HotCallSiteRelFreq(
"hot-callsite-rel-freq", cl::Hidden, cl::init(60),
cl::desc("Minimum block frequency, expressed as a multiple of caller's "
"entry frequency, for a callsite to be hot in the absence of "
@@ -1820,10 +1820,11 @@ InlineCostCallAnalyzer::getHotCallSiteThreshold(CallBase &Call,
// potentially cache the computation of scaled entry frequency, but the added
// complexity is not worth it unless this scaling shows up high in the
// profiles.
- auto CallSiteBB = Call.getParent();
- auto CallSiteFreq = CallerBFI->getBlockFreq(CallSiteBB).getFrequency();
- auto CallerEntryFreq = CallerBFI->getEntryFreq();
- if (CallSiteFreq >= CallerEntryFreq * HotCallSiteRelFreq)
+ const BasicBlock *CallSiteBB = Call.getParent();
+ BlockFrequency CallSiteFreq = CallerBFI->getBlockFreq(CallSiteBB);
+ BlockFrequency CallerEntryFreq = CallerBFI->getEntryFreq();
+ std::optional<BlockFrequency> Limit = CallerEntryFreq.mul(HotCallSiteRelFreq);
+ if (Limit && CallSiteFreq >= *Limit)
return Params.LocallyHotCallSiteThreshold;
// Otherwise treat it normally.