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/ADT/Optional.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/BlockFrequency.h"
25 class BranchProbabilityInfo;
27 template <class BlockT> class BlockFrequencyInfoImpl;
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;
35 void operator=(const BlockFrequencyInfo &) = delete;
36 BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
40 BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
42 BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
44 BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
46 ~BlockFrequencyInfo();
48 /// Handle invalidation explicitly.
49 bool invalidate(Function &F, const PreservedAnalyses &PA,
50 FunctionAnalysisManager::Invalidator &);
52 const Function *getFunction() const;
53 const BranchProbabilityInfo *getBPI() const;
56 /// getblockFreq - Return block frequency. Return 0 if we don't have the
57 /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
58 /// means that we should not rely on the value itself, but only on the
59 /// comparison to the other block frequencies. We do this to avoid using of
61 BlockFrequency getBlockFreq(const BasicBlock *BB) const;
63 /// \brief Returns the estimated profile count of \p BB.
64 /// This computes the relative block frequency of \p BB and multiplies it by
65 /// the enclosing function's count (if available) and returns the value.
66 Optional<uint64_t> getBlockProfileCount(const BasicBlock *BB) const;
68 /// \brief Returns the estimated profile count of \p Freq.
69 /// This uses the frequency \p Freq and multiplies it by
70 /// the enclosing function's count (if available) and returns the value.
71 Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
73 // Set the frequency of the given basic block.
74 void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
76 /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
77 /// of the blocks in \p BlocksToScale such that their frequencies relative
78 /// to \p ReferenceBB remain unchanged.
79 void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq,
80 SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
82 /// calculate - compute block frequency info for the given function.
83 void calculate(const Function &F, const BranchProbabilityInfo &BPI,
86 // Print the block frequency Freq to OS using the current functions entry
87 // frequency to convert freq into a relative decimal form.
88 raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
90 // Convenience method that attempts to look up the frequency associated with
91 // BB and print it to OS.
92 raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
94 uint64_t getEntryFreq() const;
96 void print(raw_ostream &OS) const;
99 /// \brief Analysis pass which computes \c BlockFrequencyInfo.
100 class BlockFrequencyAnalysis
101 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
102 friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
103 static AnalysisKey Key;
106 /// \brief Provide the result typedef for this analysis pass.
107 typedef BlockFrequencyInfo Result;
109 /// \brief Run the analysis pass over a function and produce BFI.
110 Result run(Function &F, FunctionAnalysisManager &AM);
113 /// \brief Printer pass for the \c BlockFrequencyInfo results.
114 class BlockFrequencyPrinterPass
115 : public PassInfoMixin<BlockFrequencyPrinterPass> {
119 explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
120 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
123 /// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
124 class BlockFrequencyInfoWrapperPass : public FunctionPass {
125 BlockFrequencyInfo BFI;
130 BlockFrequencyInfoWrapperPass();
131 ~BlockFrequencyInfoWrapperPass() override;
133 BlockFrequencyInfo &getBFI() { return BFI; }
134 const BlockFrequencyInfo &getBFI() const { return BFI; }
136 void getAnalysisUsage(AnalysisUsage &AU) const override;
138 bool runOnFunction(Function &F) override;
139 void releaseMemory() override;
140 void print(raw_ostream &OS, const Module *M) const override;