LLVM  14.0.0git
ProfileSummaryInfo.h
Go to the documentation of this file.
1 //===- llvm/Analysis/ProfileSummaryInfo.h - profile summary ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a pass that provides access to profile summary
10 // information.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_PROFILESUMMARYINFO_H
15 #define LLVM_ANALYSIS_PROFILESUMMARYINFO_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/IR/ProfileSummary.h"
20 #include "llvm/Pass.h"
21 #include <memory>
22 
23 namespace llvm {
24 class BasicBlock;
25 class BlockFrequencyInfo;
26 class CallBase;
27 class Function;
28 
29 /// Analysis providing profile information.
30 ///
31 /// This is an immutable analysis pass that provides ability to query global
32 /// (program-level) profile information. The main APIs are isHotCount and
33 /// isColdCount that tells whether a given profile count is considered hot/cold
34 /// based on the profile summary. This also provides convenience methods to
35 /// check whether a function is hot or cold.
36 
37 // FIXME: Provide convenience methods to determine hotness/coldness of other IR
38 // units. This would require making this depend on BFI.
40 private:
41  const Module *M;
42  std::unique_ptr<ProfileSummary> Summary;
43  void computeThresholds();
44  // Count thresholds to answer isHotCount and isColdCount queries.
45  Optional<uint64_t> HotCountThreshold, ColdCountThreshold;
46  // True if the working set size of the code is considered huge,
47  // because the number of profile counts required to reach the hot
48  // percentile is above a huge threshold.
49  Optional<bool> HasHugeWorkingSetSize;
50  // True if the working set size of the code is considered large,
51  // because the number of profile counts required to reach the hot
52  // percentile is above a large threshold.
53  Optional<bool> HasLargeWorkingSetSize;
54  // Compute the threshold for a given cutoff.
55  Optional<uint64_t> computeThreshold(int PercentileCutoff) const;
56  // The map that caches the threshold values. The keys are the percentile
57  // cutoff values and the values are the corresponding threshold values.
58  mutable DenseMap<int, uint64_t> ThresholdCache;
59 
60 public:
61  ProfileSummaryInfo(const Module &M) : M(&M) { refresh(); }
63 
64  /// If no summary is present, attempt to refresh.
65  void refresh();
66 
67  /// Returns true if profile summary is available.
68  bool hasProfileSummary() const { return Summary != nullptr; }
69 
70  /// Returns true if module \c M has sample profile.
71  bool hasSampleProfile() const {
72  return hasProfileSummary() &&
73  Summary->getKind() == ProfileSummary::PSK_Sample;
74  }
75 
76  /// Returns true if module \c M has instrumentation profile.
78  return hasProfileSummary() &&
79  Summary->getKind() == ProfileSummary::PSK_Instr;
80  }
81 
82  /// Returns true if module \c M has context sensitive instrumentation profile.
84  return hasProfileSummary() &&
85  Summary->getKind() == ProfileSummary::PSK_CSInstr;
86  }
87 
88  /// Handle the invalidation of this information.
89  ///
90  /// When used as a result of \c ProfileSummaryAnalysis this method will be
91  /// called when the module this was computed for changes. Since profile
92  /// summary is immutable after it is annotated on the module, we return false
93  /// here.
96  return false;
97  }
98 
99  /// Returns the profile count for \p CallInst.
102  bool AllowSynthetic = false) const;
103  /// Returns true if module \c M has partial-profile sample profile.
104  bool hasPartialSampleProfile() const;
105  /// Returns true if the working set size of the code is considered huge.
106  bool hasHugeWorkingSetSize() const;
107  /// Returns true if the working set size of the code is considered large.
108  bool hasLargeWorkingSetSize() const;
109  /// Returns true if \p F has hot function entry.
110  bool isFunctionEntryHot(const Function *F) const;
111  /// Returns true if \p F contains hot code.
112  bool isFunctionHotInCallGraph(const Function *F,
113  BlockFrequencyInfo &BFI) const;
114  /// Returns true if \p F has cold function entry.
115  bool isFunctionEntryCold(const Function *F) const;
116  /// Returns true if \p F contains only cold code.
118  BlockFrequencyInfo &BFI) const;
119  /// Returns true if the hotness of \p F is unknown.
120  bool isFunctionHotnessUnknown(const Function &F) const;
121  /// Returns true if \p F contains hot code with regard to a given hot
122  /// percentile cutoff value.
124  const Function *F,
125  BlockFrequencyInfo &BFI) const;
126  /// Returns true if \p F contains cold code with regard to a given cold
127  /// percentile cutoff value.
129  const Function *F,
130  BlockFrequencyInfo &BFI) const;
131  /// Returns true if count \p C is considered hot.
132  bool isHotCount(uint64_t C) const;
133  /// Returns true if count \p C is considered cold.
134  bool isColdCount(uint64_t C) const;
135  /// Returns true if count \p C is considered hot with regard to a given
136  /// hot percentile cutoff value.
137  /// PercentileCutoff is encoded as a 6 digit decimal fixed point number, where
138  /// the first two digits are the whole part. E.g. 995000 for 99.5 percentile.
140  /// Returns true if count \p C is considered cold with regard to a given
141  /// cold percentile cutoff value.
142  /// PercentileCutoff is encoded as a 6 digit decimal fixed point number, where
143  /// the first two digits are the whole part. E.g. 995000 for 99.5 percentile.
145  /// Returns true if BasicBlock \p BB is considered hot.
146  bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const;
147  /// Returns true if BasicBlock \p BB is considered cold.
148  bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const;
149  /// Returns true if BasicBlock \p BB is considered hot with regard to a given
150  /// hot percentile cutoff value.
151  /// PercentileCutoff is encoded as a 6 digit decimal fixed point number, where
152  /// the first two digits are the whole part. E.g. 995000 for 99.5 percentile.
154  BlockFrequencyInfo *BFI) const;
155  /// Returns true if BasicBlock \p BB is considered cold with regard to a given
156  /// cold percentile cutoff value.
157  /// PercentileCutoff is encoded as a 6 digit decimal fixed point number, where
158  /// the first two digits are the whole part. E.g. 995000 for 99.5 percentile.
160  BlockFrequencyInfo *BFI) const;
161  /// Returns true if the call site \p CB is considered hot.
162  bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const;
163  /// Returns true if call site \p CB is considered cold.
164  bool isColdCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const;
165  /// Returns HotCountThreshold if set. Recompute HotCountThreshold
166  /// if not set.
168  /// Returns ColdCountThreshold if set. Recompute HotCountThreshold
169  /// if not set.
171  /// Returns HotCountThreshold if set.
173  return HotCountThreshold ? HotCountThreshold.getValue() : 0;
174  }
175  /// Returns ColdCountThreshold if set.
177  return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
178  }
179 
180  private:
181  template <bool isHot>
182  bool isFunctionHotOrColdInCallGraphNthPercentile(
183  int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const;
184  template <bool isHot>
185  bool isHotOrColdCountNthPercentile(int PercentileCutoff, uint64_t C) const;
186  template <bool isHot>
187  bool isHotOrColdBlockNthPercentile(int PercentileCutoff,
188  const BasicBlock *BB,
189  BlockFrequencyInfo *BFI) const;
190 };
191 
192 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
194  std::unique_ptr<ProfileSummaryInfo> PSI;
195 
196 public:
197  static char ID;
199 
200  ProfileSummaryInfo &getPSI() { return *PSI; }
201  const ProfileSummaryInfo &getPSI() const { return *PSI; }
202 
203  bool doInitialization(Module &M) override;
204  bool doFinalization(Module &M) override;
205  void getAnalysisUsage(AnalysisUsage &AU) const override {
206  AU.setPreservesAll();
207  }
208 };
209 
210 /// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
212  : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
213 public:
215 
217 
218 private:
220  static AnalysisKey Key;
221 };
222 
223 /// Printer pass that uses \c ProfileSummaryAnalysis.
225  : public PassInfoMixin<ProfileSummaryPrinterPass> {
226  raw_ostream &OS;
227 
228 public:
229  explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
231 };
232 
233 } // end namespace llvm
234 
235 #endif
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ProfileSummaryInfo::isColdCallSite
bool isColdCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const
Returns true if call site CB is considered cold.
Definition: ProfileSummaryInfo.cpp:364
llvm::ProfileSummaryInfo::isColdBlockNthPercentile
bool isColdBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const
Returns true if BasicBlock BB is considered cold with regard to a given cold percentile cutoff value.
Definition: ProfileSummaryInfo.cpp:353
llvm::ProfileSummaryInfo::hasPartialSampleProfile
bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
Definition: ProfileSummaryInfo.cpp:375
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:269
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:62
ProfileSummary.h
Pass.h
llvm::ProfileSummaryInfo::hasProfileSummary
bool hasProfileSummary() const
Returns true if profile summary is available.
Definition: ProfileSummaryInfo.h:68
llvm::ProfileSummaryInfo::isFunctionEntryHot
bool isFunctionEntryHot(const Function *F) const
Returns true if F has hot function entry.
Definition: ProfileSummaryInfo.cpp:99
llvm::ProfileSummaryInfo::getOrCompHotCountThreshold
uint64_t getOrCompHotCountThreshold() const
Returns HotCountThreshold if set.
Definition: ProfileSummaryInfo.cpp:318
llvm::ProfileSummaryInfo::isHotBlock
bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const
Returns true if BasicBlock BB is considered hot.
Definition: ProfileSummaryInfo.cpp:326
DenseMap.h
llvm::ProfileSummaryInfo::hasCSInstrumentationProfile
bool hasCSInstrumentationProfile() const
Returns true if module M has context sensitive instrumentation profile.
Definition: ProfileSummaryInfo.h:83
llvm::ProfileSummaryInfo::isFunctionEntryCold
bool isFunctionEntryCold(const Function *F) const
Returns true if F has cold function entry.
Definition: ProfileSummaryInfo.cpp:222
llvm::Optional< uint64_t >
llvm::ProfileSummaryInfo::getProfileCount
Optional< uint64_t > getProfileCount(const CallBase &CallInst, BlockFrequencyInfo *BFI, bool AllowSynthetic=false) const
Returns the profile count for CallInst.
Definition: ProfileSummaryInfo.cpp:77
llvm::ProfileSummaryInfo::isHotCallSite
bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const
Returns true if the call site CB is considered hot.
Definition: ProfileSummaryInfo.cpp:358
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::ProfileSummaryInfoWrapperPass::getPSI
ProfileSummaryInfo & getPSI()
Definition: ProfileSummaryInfo.h:200
llvm::ProfileSummaryInfo::isFunctionHotInCallGraph
bool isFunctionHotInCallGraph(const Function *F, BlockFrequencyInfo &BFI) const
Returns true if F contains hot code.
Definition: ProfileSummaryInfo.cpp:114
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::ProfileSummaryInfo::isColdCount
bool isColdCount(uint64_t C) const
Returns true if count C is considered cold.
Definition: ProfileSummaryInfo.cpp:294
llvm::ProfileSummaryInfo::isHotCountNthPercentile
bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C) const
Returns true if count C is considered hot with regard to a given hot percentile cutoff value.
Definition: ProfileSummaryInfo.cpp:308
llvm::ProfileSummaryInfoWrapperPass::doInitialization
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: ProfileSummaryInfo.cpp:389
llvm::ProfileSummaryInfo::isHotBlockNthPercentile
bool isHotBlockNthPercentile(int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const
Returns true if BasicBlock BB is considered hot with regard to a given hot percentile cutoff value.
Definition: ProfileSummaryInfo.cpp:348
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:670
llvm::ProfileSummary::PSK_Sample
@ PSK_Sample
Definition: ProfileSummary.h:47
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition: ProfileSummaryInfo.h:39
llvm::ProfileSummaryInfo::ProfileSummaryInfo
ProfileSummaryInfo(const Module &M)
Definition: ProfileSummaryInfo.h:61
llvm::ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile
bool isFunctionHotInCallGraphNthPercentile(int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const
Returns true if F contains hot code with regard to a given hot percentile cutoff value.
Definition: ProfileSummaryInfo.cpp:207
llvm::ProfileSummaryPrinterPass
Printer pass that uses ProfileSummaryAnalysis.
Definition: ProfileSummaryInfo.h:224
llvm::ProfileSummaryInfo::isHotCount
bool isHotCount(uint64_t C) const
Returns true if count C is considered hot.
Definition: ProfileSummaryInfo.cpp:290
PercentileCutoff
static cl::opt< unsigned > PercentileCutoff("mfs-psi-cutoff", cl::desc("Percentile profile summary cutoff used to " "determine cold blocks. Unused if set to zero."), cl::init(999950), cl::Hidden)
uint64_t
llvm::ProfileSummaryInfoWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: ProfileSummaryInfo.h:205
llvm::ProfileSummaryAnalysis::Result
ProfileSummaryInfo Result
Definition: ProfileSummaryInfo.h:214
llvm::ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass
ProfileSummaryInfoWrapperPass()
llvm::DenseMap< int, uint64_t >
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::ProfileSummaryInfo::getColdCountThreshold
uint64_t getColdCountThreshold() const
Returns ColdCountThreshold if set.
Definition: ProfileSummaryInfo.h:176
llvm::ProfileSummaryInfoWrapperPass
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Definition: ProfileSummaryInfo.h:193
llvm::ProfileSummaryPrinterPass::ProfileSummaryPrinterPass
ProfileSummaryPrinterPass(raw_ostream &OS)
Definition: ProfileSummaryInfo.h:229
llvm::ProfileSummaryInfo::hasSampleProfile
bool hasSampleProfile() const
Returns true if module M has sample profile.
Definition: ProfileSummaryInfo.h:71
llvm::ProfileSummaryInfoWrapperPass::doFinalization
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: ProfileSummaryInfo.cpp:394
llvm::ProfileSummaryInfo::hasInstrumentationProfile
bool hasInstrumentationProfile() const
Returns true if module M has instrumentation profile.
Definition: ProfileSummaryInfo.h:77
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::ProfileSummaryPrinterPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: ProfileSummaryInfo.cpp:405
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:397
llvm::ProfileSummaryInfo::isFunctionColdInCallGraphNthPercentile
bool isFunctionColdInCallGraphNthPercentile(int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const
Returns true if F contains cold code with regard to a given cold percentile cutoff value.
Definition: ProfileSummaryInfo.cpp:213
llvm::ProfileSummaryAnalysis
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Definition: ProfileSummaryInfo.h:211
llvm::ReplayInlineScope::Function
@ Function
llvm::ProfileSummaryInfo::refresh
void refresh()
If no summary is present, attempt to refresh.
Definition: ProfileSummaryInfo.cpp:58
llvm::ProfileSummaryInfo::isFunctionHotnessUnknown
bool isFunctionHotnessUnknown(const Function &F) const
Returns true if the hotness of F is unknown.
Definition: ProfileSummaryInfo.cpp:167
llvm::ProfileSummary::PSK_CSInstr
@ PSK_CSInstr
Definition: ProfileSummary.h:47
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:421
llvm::ProfileSummaryAnalysis::run
Result run(Module &M, ModuleAnalysisManager &)
Definition: ProfileSummaryInfo.cpp:400
llvm::ProfileSummaryInfo::hasLargeWorkingSetSize
bool hasLargeWorkingSetSize() const
Returns true if the working set size of the code is considered large.
Definition: ProfileSummaryInfo.cpp:286
llvm::ProfileSummaryInfoWrapperPass::getPSI
const ProfileSummaryInfo & getPSI() const
Definition: ProfileSummaryInfo.h:201
llvm::ProfileSummaryInfo::hasHugeWorkingSetSize
bool hasHugeWorkingSetSize() const
Returns true if the working set size of the code is considered huge.
Definition: ProfileSummaryInfo.cpp:282
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::ProfileSummaryInfo::isFunctionColdInCallGraph
bool isFunctionColdInCallGraph(const Function *F, BlockFrequencyInfo &BFI) const
Returns true if F contains only cold code.
Definition: ProfileSummaryInfo.cpp:143
llvm::ProfileSummaryInfo::getOrCompColdCountThreshold
uint64_t getOrCompColdCountThreshold() const
Returns ColdCountThreshold if set.
Definition: ProfileSummaryInfo.cpp:322
PassManager.h
llvm::ProfileSummaryInfo::isColdBlock
bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) const
Returns true if BasicBlock BB is considered cold.
Definition: ProfileSummaryInfo.cpp:332
llvm::ProfileSummaryInfo::invalidate
bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
Handle the invalidation of this information.
Definition: ProfileSummaryInfo.h:94
llvm::ProfileSummary::PSK_Instr
@ PSK_Instr
Definition: ProfileSummary.h:47
llvm::ProfileSummaryInfo::getHotCountThreshold
uint64_t getHotCountThreshold() const
Returns HotCountThreshold if set.
Definition: ProfileSummaryInfo.h:172
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1475
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::ProfileSummaryInfoWrapperPass::ID
static char ID
Definition: ProfileSummaryInfo.h:197
llvm::ProfileSummaryInfo::isColdCountNthPercentile
bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C) const
Returns true if count C is considered cold with regard to a given cold percentile cutoff value.
Definition: ProfileSummaryInfo.cpp:313
llvm::Optional::getValue
constexpr const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:282