1 //==- BlockFrequencyInfoImpl.h - Block Frequency Implementation -*- 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 // Shared implementation of BlockFrequencyInfo for IR and Machine Instructions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFOIMPL_H
15 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFOIMPL_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PostOrderIterator.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/Support/BlockFrequency.h"
23 #include "llvm/Support/BranchProbability.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
32 class BranchProbabilityInfo;
33 class BlockFrequencyInfo;
34 class MachineBranchProbabilityInfo;
35 class MachineBlockFrequencyInfo;
37 namespace bfi_detail {
38 template <class BlockT> struct TypeMap {};
39 template <> struct TypeMap<BasicBlock> {
40 typedef BasicBlock BlockT;
41 typedef Function FunctionT;
42 typedef BranchProbabilityInfo BlockProbInfoT;
44 template <> struct TypeMap<MachineBasicBlock> {
45 typedef MachineBasicBlock BlockT;
46 typedef MachineFunction FunctionT;
47 typedef MachineBranchProbabilityInfo BlockProbInfoT;
51 /// BlockFrequencyInfoImpl implements block frequency algorithm for IR and
52 /// Machine Instructions. Algorithm starts with value ENTRY_FREQ
53 /// for the entry block and then propagates frequencies using branch weights
54 /// from (Machine)BranchProbabilityInfo. LoopInfo is not required because
55 /// algorithm can find "backedges" by itself.
57 class BlockFrequencyInfoImpl {
58 typedef typename bfi_detail::TypeMap<BT>::BlockT BlockT;
59 typedef typename bfi_detail::TypeMap<BT>::FunctionT FunctionT;
60 typedef typename bfi_detail::TypeMap<BT>::BlockProbInfoT BlockProbInfoT;
62 DenseMap<const BlockT *, BlockFrequency> Freqs;
68 typedef GraphTraits< Inverse<BlockT *> > GT;
70 static const uint64_t EntryFreq = 1 << 14;
72 std::string getBlockName(BasicBlock *BB) const {
73 return BB->getName().str();
76 std::string getBlockName(MachineBasicBlock *MBB) const {
78 raw_string_ostream ss(str);
79 ss << "BB#" << MBB->getNumber();
81 if (const BasicBlock *BB = MBB->getBasicBlock())
82 ss << " derived from LLVM BB " << BB->getName();
87 void setBlockFreq(BlockT *BB, BlockFrequency Freq) {
89 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") = ";
90 printBlockFreq(dbgs(), Freq) << "\n");
93 /// getEdgeFreq - Return edge frequency based on SRC frequency and Src -> Dst
95 BlockFrequency getEdgeFreq(BlockT *Src, BlockT *Dst) const {
96 BranchProbability Prob = BPI->getEdgeProbability(Src, Dst);
97 return getBlockFreq(Src) * Prob;
100 /// incBlockFreq - Increase BB block frequency by FREQ.
102 void incBlockFreq(BlockT *BB, BlockFrequency Freq) {
104 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") += ";
105 printBlockFreq(dbgs(), Freq) << " --> ";
106 printBlockFreq(dbgs(), Freqs[BB]) << "\n");
109 // All blocks in postorder.
110 std::vector<BlockT *> POT;
112 // Map Block -> Position in reverse-postorder list.
113 DenseMap<BlockT *, unsigned> RPO;
115 // For each loop header, record the per-iteration probability of exiting the
116 // loop. This is the reciprocal of the expected number of loop iterations.
117 typedef DenseMap<BlockT*, BranchProbability> LoopExitProbMap;
118 LoopExitProbMap LoopExitProb;
120 // (reverse-)postorder traversal iterators.
121 typedef typename std::vector<BlockT *>::iterator pot_iterator;
122 typedef typename std::vector<BlockT *>::reverse_iterator rpot_iterator;
124 pot_iterator pot_begin() { return POT.begin(); }
125 pot_iterator pot_end() { return POT.end(); }
127 rpot_iterator rpot_begin() { return POT.rbegin(); }
128 rpot_iterator rpot_end() { return POT.rend(); }
130 rpot_iterator rpot_at(BlockT *BB) {
131 rpot_iterator I = rpot_begin();
132 unsigned idx = RPO.lookup(BB);
134 std::advance(I, idx - 1);
140 /// isBackedge - Return if edge Src -> Dst is a reachable backedge.
142 bool isBackedge(BlockT *Src, BlockT *Dst) const {
143 unsigned a = RPO.lookup(Src);
146 unsigned b = RPO.lookup(Dst);
147 assert(b && "Destination block should be reachable");
151 /// getSingleBlockPred - return single BB block predecessor or NULL if
152 /// BB has none or more predecessors.
153 BlockT *getSingleBlockPred(BlockT *BB) {
154 typename GT::ChildIteratorType
155 PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
156 PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
170 void doBlock(BlockT *BB, BlockT *LoopHead,
171 SmallPtrSet<BlockT *, 8> &BlocksInLoop) {
173 DEBUG(dbgs() << "doBlock(" << getBlockName(BB) << ")\n");
176 if (BB == LoopHead) {
177 setBlockFreq(BB, EntryFreq);
181 if (BlockT *Pred = getSingleBlockPred(BB)) {
182 if (BlocksInLoop.count(Pred))
183 setBlockFreq(BB, getEdgeFreq(Pred, BB));
184 // TODO: else? irreducible, ignore it for now.
188 bool isInLoop = false;
189 bool isLoopHead = false;
191 for (typename GT::ChildIteratorType
192 PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
193 PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
197 if (isBackedge(Pred, BB)) {
199 } else if (BlocksInLoop.count(Pred)) {
200 incBlockFreq(BB, getEdgeFreq(Pred, BB));
203 // TODO: else? irreducible.
212 // This block is a loop header, so boost its frequency by the expected
213 // number of loop iterations. The loop blocks will be revisited so they all
215 typename LoopExitProbMap::const_iterator I = LoopExitProb.find(BB);
216 assert(I != LoopExitProb.end() && "Loop header missing from table");
217 Freqs[BB] /= I->second;
218 DEBUG(dbgs() << "Loop header scaled to ";
219 printBlockFreq(dbgs(), Freqs[BB]) << ".\n");
222 /// doLoop - Propagate block frequency down through the loop.
223 void doLoop(BlockT *Head, BlockT *Tail) {
224 DEBUG(dbgs() << "doLoop(" << getBlockName(Head) << ", "
225 << getBlockName(Tail) << ")\n");
227 SmallPtrSet<BlockT *, 8> BlocksInLoop;
229 for (rpot_iterator I = rpot_at(Head), E = rpot_at(Tail); ; ++I) {
231 doBlock(BB, Head, BlocksInLoop);
233 BlocksInLoop.insert(BB);
238 // Compute loop's cyclic probability using backedges probabilities.
239 BlockFrequency BackFreq;
240 for (typename GT::ChildIteratorType
241 PI = GraphTraits< Inverse<BlockT *> >::child_begin(Head),
242 PE = GraphTraits< Inverse<BlockT *> >::child_end(Head);
246 if (isBackedge(Pred, Head))
247 BackFreq += getEdgeFreq(Pred, Head);
250 // The cyclic probability is freq(BackEdges) / freq(Head), where freq(Head)
251 // only counts edges entering the loop, not the loop backedges.
252 // The probability of leaving the loop on each iteration is:
254 // ExitProb = 1 - CyclicProb
256 // The Expected number of loop iterations is:
258 // Iterations = 1 / ExitProb
260 uint64_t D = std::max(getBlockFreq(Head).getFrequency(), UINT64_C(1));
261 uint64_t N = std::max(BackFreq.getFrequency(), UINT64_C(1));
265 // We'd expect N < D, but rounding and saturation means that can't be
269 // Now ExitProb = N / D, make sure it fits in an i32/i32 fraction.
271 if (D > UINT32_MAX) {
272 unsigned Shift = 32 - countLeadingZeros(D);
278 BranchProbability LEP = BranchProbability(N, D);
279 LoopExitProb.insert(std::make_pair(Head, LEP));
280 DEBUG(dbgs() << "LoopExitProb[" << getBlockName(Head) << "] = " << LEP
282 printBlockFreq(dbgs(), BackFreq) << " / ";
283 printBlockFreq(dbgs(), getBlockFreq(Head)) << ".\n");
286 friend class BlockFrequencyInfo;
287 friend class MachineBlockFrequencyInfo;
289 BlockFrequencyInfoImpl() { }
291 void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {
298 LoopExitProb.clear();
301 BlockT *EntryBlock = fn->begin();
303 std::copy(po_begin(EntryBlock), po_end(EntryBlock), std::back_inserter(POT));
306 for (rpot_iterator I = rpot_begin(), E = rpot_end(); I != E; ++I) {
309 DEBUG(dbgs() << "RPO[" << getBlockName(BB) << "] = " << RPO[BB] << "\n");
312 // Travel over all blocks in postorder.
313 for (pot_iterator I = pot_begin(), E = pot_end(); I != E; ++I) {
315 BlockT *LastTail = 0;
316 DEBUG(dbgs() << "POT: " << getBlockName(BB) << "\n");
318 for (typename GT::ChildIteratorType
319 PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
320 PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
324 if (isBackedge(Pred, BB) && (!LastTail || RPO[Pred] > RPO[LastTail]))
329 doLoop(BB, LastTail);
332 // At the end assume the whole function as a loop, and travel over it once
334 doLoop(*(rpot_begin()), *(pot_begin()));
339 uint64_t getEntryFreq() { return EntryFreq; }
341 /// getBlockFreq - Return block frequency. Return 0 if we don't have it.
342 BlockFrequency getBlockFreq(const BlockT *BB) const {
343 typename DenseMap<const BlockT *, BlockFrequency>::const_iterator
345 if (I != Freqs.end())
350 void print(raw_ostream &OS) const {
351 OS << "\n\n---- Block Freqs ----\n";
352 for (typename FunctionT::iterator I = Fn->begin(), E = Fn->end(); I != E;) {
354 OS << " " << getBlockName(BB) << " = ";
355 printBlockFreq(OS, getBlockFreq(BB)) << "\n";
357 for (typename GraphTraits<BlockT *>::ChildIteratorType
358 SI = GraphTraits<BlockT *>::child_begin(BB),
359 SE = GraphTraits<BlockT *>::child_end(BB); SI != SE; ++SI) {
361 OS << " " << getBlockName(BB) << " -> " << getBlockName(Succ)
362 << " = "; printBlockFreq(OS, getEdgeFreq(BB, Succ)) << "\n";
371 // Utility method that looks up the block frequency associated with BB and
373 raw_ostream &printBlockFreq(raw_ostream &OS,
375 return printBlockFreq(OS, getBlockFreq(BB));
378 raw_ostream &printBlockFreq(raw_ostream &OS,
379 const BlockFrequency &Freq) const {
380 // Convert fixed-point number to decimal.
381 uint64_t Frequency = Freq.getFrequency();
382 OS << Frequency / EntryFreq << ".";
383 uint64_t Rem = Frequency % EntryFreq;
388 OS << Rem / EntryFreq;
389 Rem = Rem % EntryFreq;
390 } while (Rem >= Eps/2);