LLVM  5.0.0
ProfileSummaryInfo.h
Go to the documentation of this file.
1 //===- llvm/Analysis/ProfileSummaryInfo.h - profile summary ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a pass that provides access to profile summary
11 // information.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H
16 #define LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/IR/ProfileSummary.h"
25 #include "llvm/IR/ValueHandle.h"
26 #include "llvm/Pass.h"
27 #include <memory>
28 
29 namespace llvm {
30 class BasicBlock;
31 class BlockFrequencyInfo;
32 class CallSite;
33 class ProfileSummary;
34 /// \brief Analysis providing profile information.
35 ///
36 /// This is an immutable analysis pass that provides ability to query global
37 /// (program-level) profile information. The main APIs are isHotCount and
38 /// isColdCount that tells whether a given profile count is considered hot/cold
39 /// based on the profile summary. This also provides convenience methods to
40 /// check whether a function is hot or cold.
41 
42 // FIXME: Provide convenience methods to determine hotness/coldness of other IR
43 // units. This would require making this depend on BFI.
45 private:
46  Module &M;
47  std::unique_ptr<ProfileSummary> Summary;
48  bool computeSummary();
49  void computeThresholds();
50  // Count thresholds to answer isHotCount and isColdCount queries.
51  Optional<uint64_t> HotCountThreshold, ColdCountThreshold;
52 
53 public:
54  ProfileSummaryInfo(Module &M) : M(M) {}
56  : M(Arg.M), Summary(std::move(Arg.Summary)) {}
57 
58  /// \brief Returns true if profile summary is available.
59  bool hasProfileSummary() { return computeSummary(); }
60 
61  /// \brief Returns true if module \c M has sample profile.
63  return hasProfileSummary() &&
64  Summary->getKind() == ProfileSummary::PSK_Sample;
65  }
66 
67  /// \brief Returns true if module \c M has instrumentation profile.
69  return hasProfileSummary() &&
70  Summary->getKind() == ProfileSummary::PSK_Instr;
71  }
72 
73  /// Handle the invalidation of this information.
74  ///
75  /// When used as a result of \c ProfileSummaryAnalysis this method will be
76  /// called when the module this was computed for changes. Since profile
77  /// summary is immutable after it is annotated on the module, we return false
78  /// here.
81  return false;
82  }
83 
84  /// Returns the profile count for \p CallInst.
87  /// \brief Returns true if \p F has hot function entry.
88  bool isFunctionEntryHot(const Function *F);
89  /// Returns true if \p F has hot function entry or hot call edge.
90  bool isFunctionHotInCallGraph(const Function *F);
91  /// \brief Returns true if \p F has cold function entry.
92  bool isFunctionEntryCold(const Function *F);
93  /// Returns true if \p F has cold function entry or cold call edge.
95  /// \brief Returns true if \p F is a hot function.
96  bool isHotCount(uint64_t C);
97  /// \brief Returns true if count \p C is considered cold.
98  bool isColdCount(uint64_t C);
99  /// \brief Returns true if BasicBlock \p B is considered hot.
100  bool isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
101  /// \brief Returns true if BasicBlock \p B is considered cold.
102  bool isColdBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
103  /// \brief Returns true if CallSite \p CS is considered hot.
104  bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
105  /// \brief Returns true if Callsite \p CS is considered cold.
106  bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
107 };
108 
109 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
111  std::unique_ptr<ProfileSummaryInfo> PSI;
112 
113 public:
114  static char ID;
116 
118  return &*PSI;
119  }
120 
121  bool doInitialization(Module &M) override;
122  bool doFinalization(Module &M) override;
123  void getAnalysisUsage(AnalysisUsage &AU) const override {
124  AU.setPreservesAll();
125  }
126 };
127 
128 /// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
130  : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
131 public:
133 
135 
136 private:
138  static AnalysisKey Key;
139 };
140 
141 /// \brief Printer pass that uses \c ProfileSummaryAnalysis.
143  : public PassInfoMixin<ProfileSummaryPrinterPass> {
144  raw_ostream &OS;
145 
146 public:
147  explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
149 };
150 
151 } // end namespace llvm
152 
153 #endif
bool hasInstrumentationProfile()
Returns true if module M has instrumentation profile.
Various leaf nodes.
Definition: ISDOpcodes.h:60
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
bool isColdBB(const BasicBlock *B, BlockFrequencyInfo *BFI)
Returns true if BasicBlock B is considered cold.
bool isFunctionHotInCallGraph(const Function *F)
Returns true if F has hot function entry or hot call edge.
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's calling convention.
Optional< uint64_t > getProfileCount(const Instruction *CallInst, BlockFrequencyInfo *BFI)
Returns the profile count for CallInst.
bool isHotCount(uint64_t C)
Returns true if F is a hot function.
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.
Result run(Module &M, ModuleAnalysisManager &)
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
bool isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI)
Returns true if BasicBlock B is considered hot.
#define F(x, y, z)
Definition: MD5.cpp:55
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
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:59
bool isFunctionColdInCallGraph(const Function *F)
Returns true if F has cold function entry or cold call edge.
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.
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:266
#define B
Definition: LargeTest.cpp:24
void setPreservesAll()
Set by analyses that do not transform their input at all.
ProfileSummaryInfo(ProfileSummaryInfo &&Arg)
Printer pass that uses ProfileSummaryAnalysis.
bool isFunctionEntryHot(const Function *F)
Returns true if F has hot function entry.
#define C
Definition: LargeTest.cpp:25
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:559
bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI)
Returns true if Callsite CS is considered cold.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
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