1 //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
15 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
17 #include "llvm/Pass.h"
18 #include "llvm/Support/BlockFrequency.h"
23 class BranchProbabilityInfo;
25 template <class BlockT> class BlockFrequencyInfoImpl;
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;
35 BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
38 const Function *getFunction() const;
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
46 BlockFrequency getBlockFreq(const BasicBlock *BB) const;
48 // Set the frequency of the given basic block.
49 void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
51 /// calculate - compute block frequency info for the given function.
52 void calculate(const Function &F, const BranchProbabilityInfo &BPI,
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;
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;
63 uint64_t getEntryFreq() const;
65 void print(raw_ostream &OS) const;
68 /// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
69 class BlockFrequencyInfoWrapperPass : public FunctionPass {
70 BlockFrequencyInfo BFI;
75 BlockFrequencyInfoWrapperPass();
76 ~BlockFrequencyInfoWrapperPass() override;
78 BlockFrequencyInfo &getBFI() { return BFI; }
79 const BlockFrequencyInfo &getBFI() const { return BFI; }
81 void getAnalysisUsage(AnalysisUsage &AU) const override;
83 bool runOnFunction(Function &F) override;
84 void releaseMemory() override;
85 void print(raw_ostream &OS, const Module *M) const override;