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/Pass.h"
18 #include "llvm/Support/BlockFrequency.h"
19 #include <climits>
20 
21 namespace llvm {
22 
23 class BranchProbabilityInfo;
24 class LoopInfo;
25 template <class BlockT> class BlockFrequencyInfoImpl;
26 
27 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
28 /// estimate IR basic block frequencies.
29 class BlockFrequencyInfo {
30   typedef BlockFrequencyInfoImpl<BasicBlock> ImplType;
31   std::unique_ptr<ImplType> BFI;
32 
33 public:
34   BlockFrequencyInfo();
35   BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
36                      const LoopInfo &LI);
37 
38   const Function *getFunction() const;
39   void view() const;
40 
41   /// getblockFreq - Return block frequency. Return 0 if we don't have the
42   /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
43   /// means that we should not rely on the value itself, but only on the
44   /// comparison to the other block frequencies. We do this to avoid using of
45   /// floating points.
46   BlockFrequency getBlockFreq(const BasicBlock *BB) const;
47 
48   // Set the frequency of the given basic block.
49   void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
50 
51   /// calculate - compute block frequency info for the given function.
52   void calculate(const Function &F, const BranchProbabilityInfo &BPI,
53                  const LoopInfo &LI);
54 
55   // Print the block frequency Freq to OS using the current functions entry
56   // frequency to convert freq into a relative decimal form.
57   raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
58 
59   // Convenience method that attempts to look up the frequency associated with
60   // BB and print it to OS.
61   raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
62 
63   uint64_t getEntryFreq() const;
64   void releaseMemory();
65   void print(raw_ostream &OS) const;
66 };
67 
68 /// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
69 class BlockFrequencyInfoWrapperPass : public FunctionPass {
70   BlockFrequencyInfo BFI;
71 
72 public:
73   static char ID;
74 
75   BlockFrequencyInfoWrapperPass();
76   ~BlockFrequencyInfoWrapperPass() override;
77 
getBFI()78   BlockFrequencyInfo &getBFI() { return BFI; }
getBFI()79   const BlockFrequencyInfo &getBFI() const { return BFI; }
80 
81   void getAnalysisUsage(AnalysisUsage &AU) const override;
82 
83   bool runOnFunction(Function &F) override;
84   void releaseMemory() override;
85   void print(raw_ostream &OS, const Module *M) const override;
86 };
87 
88 }
89 
90 #endif
91