1 //===- llvm/Analysis/ProfileSummaryInfo.h - profile summary ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that provides access to profile summary
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H
16 #define LLVM_ANALYSIS_PROFILE_SUMMARY_INFO_H
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"
31 class BlockFrequencyInfo;
34 /// \brief Analysis providing profile information.
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.
42 // FIXME: Provide convenience methods to determine hotness/coldness of other IR
43 // units. This would require making this depend on BFI.
44 class ProfileSummaryInfo {
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;
54 ProfileSummaryInfo(Module &M) : M(M) {}
55 ProfileSummaryInfo(ProfileSummaryInfo &&Arg)
56 : M(Arg.M), Summary(std::move(Arg.Summary)) {}
57 /// Returns the profile count for \p CallInst.
58 static Optional<uint64_t> getProfileCount(const Instruction *CallInst,
59 BlockFrequencyInfo *BFI);
60 /// \brief Returns true if \p F has hot function entry.
61 bool isFunctionEntryHot(const Function *F);
62 /// Returns true if \p F has hot function entry or hot call edge.
63 bool isFunctionHotInCallGraph(const Function *F);
64 /// \brief Returns true if \p F has cold function entry.
65 bool isFunctionEntryCold(const Function *F);
66 /// Returns true if \p F has cold function entry or cold call edge.
67 bool isFunctionColdInCallGraph(const Function *F);
68 /// \brief Returns true if \p F is a hot function.
69 bool isHotCount(uint64_t C);
70 /// \brief Returns true if count \p C is considered cold.
71 bool isColdCount(uint64_t C);
72 /// \brief Returns true if BasicBlock \p B is considered hot.
73 bool isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
74 /// \brief Returns true if BasicBlock \p B is considered cold.
75 bool isColdBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
76 /// \brief Returns true if CallSite \p CS is considered hot.
77 bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
78 /// \brief Returns true if Callsite \p CS is considered cold.
79 bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
82 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
83 class ProfileSummaryInfoWrapperPass : public ImmutablePass {
84 std::unique_ptr<ProfileSummaryInfo> PSI;
88 ProfileSummaryInfoWrapperPass();
90 ProfileSummaryInfo *getPSI() {
94 bool doInitialization(Module &M) override;
95 bool doFinalization(Module &M) override;
96 void getAnalysisUsage(AnalysisUsage &AU) const override {
101 /// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
102 class ProfileSummaryAnalysis
103 : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
105 typedef ProfileSummaryInfo Result;
107 Result run(Module &M, ModuleAnalysisManager &);
110 friend AnalysisInfoMixin<ProfileSummaryAnalysis>;
111 static AnalysisKey Key;
114 /// \brief Printer pass that uses \c ProfileSummaryAnalysis.
115 class ProfileSummaryPrinterPass
116 : public PassInfoMixin<ProfileSummaryPrinterPass> {
120 explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
121 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
124 } // end namespace llvm