blob: c73c7faba0e79a08e59b672c65b6572258d2a5b6 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// Loops should be simplified before this analysis.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
14#define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
15
16#include "llvm/ADT/Optional.h"
17#include "llvm/IR/PassManager.h"
18#include "llvm/Pass.h"
19#include "llvm/Support/BlockFrequency.h"
20#include <cstdint>
21#include <memory>
22
23namespace llvm {
24
25class BasicBlock;
26class BranchProbabilityInfo;
27class Function;
28class LoopInfo;
29class Module;
30class raw_ostream;
31template <class BlockT> class BlockFrequencyInfoImpl;
32
33enum PGOViewCountsType { PGOVCT_None, PGOVCT_Graph, PGOVCT_Text };
34
35/// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
36/// estimate IR basic block frequencies.
37class BlockFrequencyInfo {
38 using ImplType = BlockFrequencyInfoImpl<BasicBlock>;
39
40 std::unique_ptr<ImplType> BFI;
41
42public:
43 BlockFrequencyInfo();
44 BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
45 const LoopInfo &LI);
46 BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
47 BlockFrequencyInfo &operator=(const BlockFrequencyInfo &) = delete;
48 BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
49 BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
50 ~BlockFrequencyInfo();
51
52 /// Handle invalidation explicitly.
53 bool invalidate(Function &F, const PreservedAnalyses &PA,
54 FunctionAnalysisManager::Invalidator &);
55
56 const Function *getFunction() const;
57 const BranchProbabilityInfo *getBPI() const;
Andrew Walbran16937d02019-10-22 13:54:20 +010058 void view(StringRef = "BlockFrequencyDAGs") const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059
60 /// getblockFreq - Return block frequency. Return 0 if we don't have the
61 /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
62 /// means that we should not rely on the value itself, but only on the
63 /// comparison to the other block frequencies. We do this to avoid using of
64 /// floating points.
65 BlockFrequency getBlockFreq(const BasicBlock *BB) const;
66
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067 /// Returns the estimated profile count of \p BB.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068 /// This computes the relative block frequency of \p BB and multiplies it by
69 /// the enclosing function's count (if available) and returns the value.
70 Optional<uint64_t> getBlockProfileCount(const BasicBlock *BB) const;
71
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 /// Returns the estimated profile count of \p Freq.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 /// This uses the frequency \p Freq and multiplies it by
74 /// the enclosing function's count (if available) and returns the value.
75 Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
76
Andrew Scullcdfcccc2018-10-05 20:58:37 +010077 /// Returns true if \p BB is an irreducible loop header
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078 /// block. Otherwise false.
79 bool isIrrLoopHeader(const BasicBlock *BB);
80
81 // Set the frequency of the given basic block.
82 void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
83
84 /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
85 /// of the blocks in \p BlocksToScale such that their frequencies relative
86 /// to \p ReferenceBB remain unchanged.
87 void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq,
88 SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
89
90 /// calculate - compute block frequency info for the given function.
91 void calculate(const Function &F, const BranchProbabilityInfo &BPI,
92 const LoopInfo &LI);
93
94 // Print the block frequency Freq to OS using the current functions entry
95 // frequency to convert freq into a relative decimal form.
96 raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
97
98 // Convenience method that attempts to look up the frequency associated with
99 // BB and print it to OS.
100 raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
101
102 uint64_t getEntryFreq() const;
103 void releaseMemory();
104 void print(raw_ostream &OS) const;
105};
106
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100107/// Analysis pass which computes \c BlockFrequencyInfo.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108class BlockFrequencyAnalysis
109 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
110 friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
111
112 static AnalysisKey Key;
113
114public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100115 /// Provide the result type for this analysis pass.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100116 using Result = BlockFrequencyInfo;
117
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100118 /// Run the analysis pass over a function and produce BFI.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100119 Result run(Function &F, FunctionAnalysisManager &AM);
120};
121
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100122/// Printer pass for the \c BlockFrequencyInfo results.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100123class BlockFrequencyPrinterPass
124 : public PassInfoMixin<BlockFrequencyPrinterPass> {
125 raw_ostream &OS;
126
127public:
128 explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
129
130 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
131};
132
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100133/// Legacy analysis pass which computes \c BlockFrequencyInfo.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100134class BlockFrequencyInfoWrapperPass : public FunctionPass {
135 BlockFrequencyInfo BFI;
136
137public:
138 static char ID;
139
140 BlockFrequencyInfoWrapperPass();
141 ~BlockFrequencyInfoWrapperPass() override;
142
143 BlockFrequencyInfo &getBFI() { return BFI; }
144 const BlockFrequencyInfo &getBFI() const { return BFI; }
145
146 void getAnalysisUsage(AnalysisUsage &AU) const override;
147
148 bool runOnFunction(Function &F) override;
149 void releaseMemory() override;
150 void print(raw_ostream &OS, const Module *M) const override;
151};
152
153} // end namespace llvm
154
155#endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H