• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ProfileSummary;
31 /// \brief Analysis providing profile information.
32 ///
33 /// This is an immutable analysis pass that provides ability to query global
34 /// (program-level) profile information. The main APIs are isHotCount and
35 /// isColdCount that tells whether a given profile count is considered hot/cold
36 /// based on the profile summary. This also provides convenience methods to
37 /// check whether a function is hot or cold.
38 
39 // FIXME: Provide convenience methods to determine hotness/coldness of other IR
40 // units. This would require making this depend on BFI.
41 class ProfileSummaryInfo {
42 private:
43   Module &M;
44   std::unique_ptr<ProfileSummary> Summary;
45   void computeSummary();
46   void computeThresholds();
47   // Count thresholds to answer isHotCount and isColdCount queries.
48   Optional<uint64_t> HotCountThreshold, ColdCountThreshold;
49 
50 public:
ProfileSummaryInfo(Module & M)51   ProfileSummaryInfo(Module &M) : M(M) {}
ProfileSummaryInfo(ProfileSummaryInfo && Arg)52   ProfileSummaryInfo(ProfileSummaryInfo &&Arg)
53       : M(Arg.M), Summary(std::move(Arg.Summary)) {}
54   /// \brief Returns true if \p F is a hot function.
55   bool isHotFunction(const Function *F);
56   /// \brief Returns true if \p F is a cold function.
57   bool isColdFunction(const Function *F);
58   /// \brief Returns true if count \p C is considered hot.
59   bool isHotCount(uint64_t C);
60   /// \brief Returns true if count \p C is considered cold.
61   bool isColdCount(uint64_t C);
62 };
63 
64 /// An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
65 class ProfileSummaryInfoWrapperPass : public ImmutablePass {
66   std::unique_ptr<ProfileSummaryInfo> PSI;
67 
68 public:
69   static char ID;
70   ProfileSummaryInfoWrapperPass();
71 
72   ProfileSummaryInfo *getPSI(Module &M);
getAnalysisUsage(AnalysisUsage & AU)73   void getAnalysisUsage(AnalysisUsage &AU) const override {
74     AU.setPreservesAll();
75   }
76 };
77 
78 /// An analysis pass based on the new PM to deliver ProfileSummaryInfo.
79 class ProfileSummaryAnalysis
80     : public AnalysisInfoMixin<ProfileSummaryAnalysis> {
81 public:
82   typedef ProfileSummaryInfo Result;
83 
ProfileSummaryAnalysis()84   ProfileSummaryAnalysis() {}
ProfileSummaryAnalysis(const ProfileSummaryAnalysis & Arg)85   ProfileSummaryAnalysis(const ProfileSummaryAnalysis &Arg) {}
ProfileSummaryAnalysis(ProfileSummaryAnalysis && Arg)86   ProfileSummaryAnalysis(ProfileSummaryAnalysis &&Arg) {}
87   ProfileSummaryAnalysis &operator=(const ProfileSummaryAnalysis &RHS) {
88     return *this;
89   }
90   ProfileSummaryAnalysis &operator=(ProfileSummaryAnalysis &&RHS) {
91     return *this;
92   }
93 
94   Result run(Module &M, ModuleAnalysisManager &);
95 
96 private:
97   friend AnalysisInfoMixin<ProfileSummaryAnalysis>;
98   static char PassID;
99 };
100 
101 /// \brief Printer pass that uses \c ProfileSummaryAnalysis.
102 class ProfileSummaryPrinterPass
103     : public PassInfoMixin<ProfileSummaryPrinterPass> {
104   raw_ostream &OS;
105 
106 public:
ProfileSummaryPrinterPass(raw_ostream & OS)107   explicit ProfileSummaryPrinterPass(raw_ostream &OS) : OS(OS) {}
108   PreservedAnalyses run(Module &M, AnalysisManager<Module> &AM);
109 };
110 
111 } // end namespace llvm
112 
113 #endif
114