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