]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/BlockFrequencyInfo.h
MFV: r313101
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / BlockFrequencyInfo.h
1 //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- 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 // Loops should be simplified before this analysis.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
15 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
16
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/BlockFrequency.h"
21 #include <climits>
22
23 namespace llvm {
24
25 class BranchProbabilityInfo;
26 class LoopInfo;
27 template <class BlockT> class BlockFrequencyInfoImpl;
28
29 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
30 /// estimate IR basic block frequencies.
31 class BlockFrequencyInfo {
32   typedef BlockFrequencyInfoImpl<BasicBlock> ImplType;
33   std::unique_ptr<ImplType> BFI;
34
35   void operator=(const BlockFrequencyInfo &) = delete;
36   BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
37
38 public:
39   BlockFrequencyInfo();
40   BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
41                      const LoopInfo &LI);
42   BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
43
44   BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
45
46   ~BlockFrequencyInfo();
47
48   const Function *getFunction() const;
49   const BranchProbabilityInfo *getBPI() const;
50   void view() const;
51
52   /// getblockFreq - Return block frequency. Return 0 if we don't have the
53   /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
54   /// means that we should not rely on the value itself, but only on the
55   /// comparison to the other block frequencies. We do this to avoid using of
56   /// floating points.
57   BlockFrequency getBlockFreq(const BasicBlock *BB) const;
58
59   /// \brief Returns the estimated profile count of \p BB.
60   /// This computes the relative block frequency of \p BB and multiplies it by
61   /// the enclosing function's count (if available) and returns the value.
62   Optional<uint64_t> getBlockProfileCount(const BasicBlock *BB) const;
63
64   // Set the frequency of the given basic block.
65   void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
66
67   /// calculate - compute block frequency info for the given function.
68   void calculate(const Function &F, const BranchProbabilityInfo &BPI,
69                  const LoopInfo &LI);
70
71   // Print the block frequency Freq to OS using the current functions entry
72   // frequency to convert freq into a relative decimal form.
73   raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
74
75   // Convenience method that attempts to look up the frequency associated with
76   // BB and print it to OS.
77   raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
78
79   uint64_t getEntryFreq() const;
80   void releaseMemory();
81   void print(raw_ostream &OS) const;
82 };
83
84 /// \brief Analysis pass which computes \c BlockFrequencyInfo.
85 class BlockFrequencyAnalysis
86     : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
87   friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
88   static char PassID;
89
90 public:
91   /// \brief Provide the result typedef for this analysis pass.
92   typedef BlockFrequencyInfo Result;
93
94   /// \brief Run the analysis pass over a function and produce BFI.
95   Result run(Function &F, AnalysisManager<Function> &AM);
96 };
97
98 /// \brief Printer pass for the \c BlockFrequencyInfo results.
99 class BlockFrequencyPrinterPass
100     : public PassInfoMixin<BlockFrequencyPrinterPass> {
101   raw_ostream &OS;
102
103 public:
104   explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
105   PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
106 };
107
108 /// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
109 class BlockFrequencyInfoWrapperPass : public FunctionPass {
110   BlockFrequencyInfo BFI;
111
112 public:
113   static char ID;
114
115   BlockFrequencyInfoWrapperPass();
116   ~BlockFrequencyInfoWrapperPass() override;
117
118   BlockFrequencyInfo &getBFI() { return BFI; }
119   const BlockFrequencyInfo &getBFI() const { return BFI; }
120
121   void getAnalysisUsage(AnalysisUsage &AU) const override;
122
123   bool runOnFunction(Function &F) override;
124   void releaseMemory() override;
125   void print(raw_ostream &OS, const Module *M) const override;
126 };
127
128 }
129
130 #endif