OSDN Git Service

Refactor the PSI to extract getCallSiteCount and remove checks for profile type.
[android-x86/external-llvm.git] / 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   /// 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   /// \brief Returns true if \p F has cold function entry.
63   bool isFunctionEntryCold(const Function *F);
64   /// \brief Returns true if \p F is a hot function.
65   bool isHotCount(uint64_t C);
66   /// \brief Returns true if count \p C is considered cold.
67   bool isColdCount(uint64_t C);
68   /// \brief Returns true if BasicBlock \p B is considered hot.
69   bool isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
70   /// \brief Returns true if BasicBlock \p B is considered cold.
71   bool isColdBB(const BasicBlock *B, BlockFrequencyInfo *BFI);
72   /// \brief Returns true if CallSite \p CS is considered hot.
73   bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
74   /// \brief Returns true if Callsite \p CS is considered cold.
75   bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI);
76 };
77
78 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
79 class ProfileSummaryInfoWrapperPass : public ImmutablePass {
80   std::unique_ptr<ProfileSummaryInfo> PSI;
81
82 public:
83   static char ID;
84   ProfileSummaryInfoWrapperPass();
85
86   ProfileSummaryInfo *getPSI() {
87     return &*PSI;
88   }
89
90   bool doInitialization(Module &M) override;
91   bool doFinalization(Module &M) override;
92   void getAnalysisUsage(AnalysisUsage &AU) const override {
93     AU.setPreservesAll();
94   }
95 };
96
97 /// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
98 class ProfileSummaryAnalysis
99     : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
100 public:
101   typedef ProfileSummaryInfo Result;
102
103   Result run(Module &M, ModuleAnalysisManager &);
104
105 private:
106   friend AnalysisInfoMixin<ProfileSummaryAnalysis>;
107   static AnalysisKey Key;
108 };
109
110 /// \brief Printer pass that uses \c ProfileSummaryAnalysis.
111 class ProfileSummaryPrinterPass
112     : public PassInfoMixin<ProfileSummaryPrinterPass> {
113   raw_ostream &OS;
114
115 public:
116   explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
117   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
118 };
119
120 } // end namespace llvm
121
122 #endif