LLVM  9.0.0svn
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_PROFILE_SUMMARY_INFO_H
15 #define LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/IR/ProfileSummary.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/Pass.h"
26 #include <memory>
27 
28 namespace llvm {
29 class BasicBlock;
30 class BlockFrequencyInfo;
31 class CallSite;
32 class ProfileSummary;
33 /// Analysis providing profile information.
34 ///
35 /// This is an immutable analysis pass that provides ability to query global
36 /// (program-level) profile information. The main APIs are isHotCount and
37 /// isColdCount that tells whether a given profile count is considered hot/cold
38 /// based on the profile summary. This also provides convenience methods to
39 /// check whether a function is hot or cold.
40 
41 // FIXME: Provide convenience methods to determine hotness/coldness of other IR
42 // units. This would require making this depend on BFI.
44 private:
45  Module &M;
46  std::unique_ptr<ProfileSummary> Summary;
47  bool computeSummary();
48  void computeThresholds();
49  // Count thresholds to answer isHotCount and isColdCount queries.
50  Optional<uint64_t> HotCountThreshold, ColdCountThreshold;
51  // True if the working set size of the code is considered huge,
52  // because the number of profile counts required to reach the hot
53  // percentile is above a huge threshold.
54  Optional<bool> HasHugeWorkingSetSize;
55 
56 public:
57  ProfileSummaryInfo(Module &M) : M(M) {}
59  : M(Arg.M), Summary(std::move(Arg.Summary)) {}
60 
61  /// Returns true if profile summary is available.
62  bool hasProfileSummary() { return computeSummary(); }
63 
64  /// Returns true if module \c M has sample profile.
66  return hasProfileSummary() &&
67  Summary->getKind() == ProfileSummary::PSK_Sample;
68  }
69 
70  /// Returns true if module \c M has instrumentation profile.
72  return hasProfileSummary() &&
73  Summary->getKind() == ProfileSummary::PSK_Instr;
74  }
75 
76  /// Returns true if module \c M has context sensitive instrumentation profile.
78  return hasProfileSummary() &&
79  Summary->getKind() == ProfileSummary::PSK_CSInstr;
80  }
81 
82  /// Handle the invalidation of this information.
83  ///
84  /// When used as a result of \c ProfileSummaryAnalysis this method will be
85  /// called when the module this was computed for changes. Since profile
86  /// summary is immutable after it is annotated on the module, we return false
87  /// here.
90  return false;
91  }
92 
93  /// Returns the profile count for \p CallInst.
96  /// Returns true if the working set size of the code is considered huge.
97  bool hasHugeWorkingSetSize();
98  /// Returns true if \p F has hot function entry.
99  bool isFunctionEntryHot(const Function *F);
100  /// Returns true if \p F contains hot code.
102  /// Returns true if \p F has cold function entry.
103  bool isFunctionEntryCold(const Function *F);
104  /// Returns true if \p F contains only cold code.
106  /// Returns true if count \p C is considered hot.
107  bool isHotCount(uint64_t C);
108  /// Returns true if count \p C is considered cold.
109  bool isColdCount(uint64_t C);
110  /// Returns true if BasicBlock \p BB is considered hot.
111  bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI);
112  /// Returns true if BasicBlock \p BB is considered cold.
113  bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI);
114  /// Returns true if CallSite \p CS is considered hot.
115  bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
116  /// Returns true if Callsite \p CS is considered cold.
117  bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
118  /// Returns HotCountThreshold if set. Recompute HotCountThreshold
119  /// if not set.
120  uint64_t getOrCompHotCountThreshold();
121  /// Returns ColdCountThreshold if set. Recompute HotCountThreshold
122  /// if not set.
123  uint64_t getOrCompColdCountThreshold();
124  /// Returns HotCountThreshold if set.
125  uint64_t getHotCountThreshold() {
126  return HotCountThreshold ? HotCountThreshold.getValue() : 0;
127  }
128  /// Returns ColdCountThreshold if set.
130  return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
131  }
132 };
133 
134 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
136  std::unique_ptr<ProfileSummaryInfo> PSI;
137 
138 public:
139  static char ID;
141 
142  ProfileSummaryInfo &getPSI() { return *PSI; }
143  const ProfileSummaryInfo &getPSI() const { return *PSI; }
144 
145  bool doInitialization(Module &M) override;
146  bool doFinalization(Module &M) override;
147  void getAnalysisUsage(AnalysisUsage &AU) const override {
148  AU.setPreservesAll();
149  }
150 };
151 
152 /// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
154  : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
155 public:
157 
158  Result run(Module &M, ModuleAnalysisManager &);
159 
160 private:
162  static AnalysisKey Key;
163 };
164 
165 /// Printer pass that uses \c ProfileSummaryAnalysis.
167  : public PassInfoMixin<ProfileSummaryPrinterPass> {
168  raw_ostream &OS;
169 
170 public:
171  explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
173 };
174 
175 } // end namespace llvm
176 
177 #endif
uint64_t CallInst * C
bool hasInstrumentationProfile()
Returns true if module M has instrumentation profile.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
uint64_t getHotCountThreshold()
Returns HotCountThreshold if set.
bool isColdCount(uint64_t C)
Returns true if count C is considered cold.
Analysis providing profile information.
bool invalidate(Module &, const PreservedAnalyses &, ModuleAnalysisManager::Invalidator &)
Handle the invalidation of this information.
This class represents a function call, abstracting a target machine&#39;s calling convention.
Optional< uint64_t > getProfileCount(const Instruction *CallInst, BlockFrequencyInfo *BFI)
Returns the profile count for CallInst.
F(f)
uint64_t getOrCompHotCountThreshold()
Returns HotCountThreshold if set.
bool isHotCount(uint64_t C)
Returns true if count C is considered hot.
bool isFunctionEntryCold(const Function *F)
Returns true if F has cold function entry.
bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI)
Returns true if CallSite CS is considered hot.
uint64_t getOrCompColdCountThreshold()
Returns ColdCountThreshold if set.
bool isFunctionHotInCallGraph(const Function *F, BlockFrequencyInfo &BFI)
Returns true if F contains hot code.
Definition: BitVector.h:937
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
const ProfileSummaryInfo & getPSI() const
Key
PAL metadata keys.
bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI)
Returns true if BasicBlock BB is considered cold.
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
bool hasProfileSummary()
Returns true if profile summary is available.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
bool hasSampleProfile()
Returns true if module M has sample profile.
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
Represent the analysis usage information of a pass.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:255
uint64_t getColdCountThreshold()
Returns ColdCountThreshold if set.
bool isFunctionColdInCallGraph(const Function *F, BlockFrequencyInfo &BFI)
Returns true if F contains only cold code.
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool hasCSInstrumentationProfile()
Returns true if module M has context sensitive instrumentation profile.
ProfileSummaryInfo(ProfileSummaryInfo &&Arg)
Printer pass that uses ProfileSummaryAnalysis.
bool isFunctionEntryHot(const Function *F)
Returns true if F has hot function entry.
bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI)
Returns true if BasicBlock BB is considered hot.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:641
bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI)
Returns true if Callsite CS is considered cold.
bool hasHugeWorkingSetSize()
Returns true if the working set size of the code is considered huge.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
A container for analyses that lazily runs them and caches their results.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70