]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/ProfileSummaryInfo.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302418, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / ProfileSummaryInfo.h
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.
44 class ProfileSummaryInfo {
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) {}
55   ProfileSummaryInfo(ProfileSummaryInfo &&Arg)
56       : M(Arg.M), Summary(std::move(Arg.Summary)) {}
57
58   /// Handle the invalidation of this information.
59   ///
60   /// When used as a result of \c ProfileSummaryAnalysis this method will be
61   /// called when the module this was computed for changes. Since profile
62   /// summary is immutable after it is annotated on the module, we return false
63   /// here.
64   bool invalidate(Module &, const PreservedAnalyses &,
65                   ModuleAnalysisManager::Invalidator &) {
66     return false;
67   }
68
69   /// Returns the profile count for \p CallInst.
70   static Optional<uint64_t> getProfileCount(const Instruction *CallInst,
71                                             BlockFrequencyInfo *BFI);
72   /// \brief Returns true if \p F has hot function entry.
73   bool isFunctionEntryHot(const Function *F);
74   /// Returns true if \p F has hot function entry or hot call edge.
75   bool isFunctionHotInCallGraph(const Function *F);
76   /// \brief Returns true if \p F has cold function entry.
77   bool isFunctionEntryCold(const Function *F);
78   /// Returns true if \p F has cold function entry or cold call edge.
79   bool isFunctionColdInCallGraph(const Function *F);
80   /// \brief Returns true if \p F is a hot function.
81   bool isHotCount(uint64_t C);
82   /// \brief Returns true if count \p C is considered cold.
83   bool isColdCount(uint64_t C);
84   /// \brief Returns true if BasicBlock \p B is considered hot.
85   bool isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
86   /// \brief Returns true if BasicBlock \p B is considered cold.
87   bool isColdBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
88   /// \brief Returns true if CallSite \p CS is considered hot.
89   bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
90   /// \brief Returns true if Callsite \p CS is considered cold.
91   bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
92 };
93
94 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
95 class ProfileSummaryInfoWrapperPass : public ImmutablePass {
96   std::unique_ptr<ProfileSummaryInfo> PSI;
97
98 public:
99   static char ID;
100   ProfileSummaryInfoWrapperPass();
101
102   ProfileSummaryInfo *getPSI() {
103     return &*PSI;
104   }
105
106   bool doInitialization(Module &M) override;
107   bool doFinalization(Module &M) override;
108   void getAnalysisUsage(AnalysisUsage &AU) const override {
109     AU.setPreservesAll();
110   }
111 };
112
113 /// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
114 class ProfileSummaryAnalysis
115     : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
116 public:
117   typedef ProfileSummaryInfo Result;
118
119   Result run(Module &M, ModuleAnalysisManager &);
120
121 private:
122   friend AnalysisInfoMixin<ProfileSummaryAnalysis>;
123   static AnalysisKey Key;
124 };
125
126 /// \brief Printer pass that uses \c ProfileSummaryAnalysis.
127 class ProfileSummaryPrinterPass
128     : public PassInfoMixin<ProfileSummaryPrinterPass> {
129   raw_ostream &OS;
130
131 public:
132   explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
133   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
134 };
135
136 } // end namespace llvm
137
138 #endif