1 //===---- BlockFrequencyImpl.h - Machine Block Frequency Implementation ---===//
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 BlockFrequency for IR and Machine Instructions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
15 #define LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
17 #include "llvm/BasicBlock.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/PostOrderIterator.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/Support/BranchProbability.h"
22 #include "llvm/Support/Debug.h"
31 /// BlockFrequencyImpl implements block frequency algorithm for IR and
32 /// Machine Instructions. Algorithm starts with value 1024 (START_FREQ)
33 /// for the entry block and then propagates frequencies using branch weights
34 /// from (Machine)BranchProbabilityInfo. LoopInfo is not required because
35 /// algorithm can find "backedges" by itself.
36 template<class BlockT, class FunctionT, class BlockProbInfoT>
37 class BlockFrequencyImpl {
39 DenseMap<BlockT *, uint32_t> Freqs;
45 typedef GraphTraits< Inverse<BlockT *> > GT;
47 static const uint32_t START_FREQ = 1024;
49 std::string getBlockName(BasicBlock *BB) const {
50 return BB->getNameStr();
53 std::string getBlockName(MachineBasicBlock *MBB) const {
55 ss << "BB#" << MBB->getNumber();
56 const BasicBlock *BB = MBB->getBasicBlock();
59 ss << " derived from LLVM BB " << BB->getNameStr();
64 void setBlockFreq(BlockT *BB, uint32_t Freq) {
66 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") = " << Freq << "\n");
69 /// getEdgeFreq - Return edge frequency based on SRC frequency and Src -> Dst
71 uint32_t getEdgeFreq(BlockT *Src, BlockT *Dst) const {
72 BranchProbability Prob = BPI->getEdgeProbability(Src, Dst);
73 uint64_t N = Prob.getNumerator();
74 uint64_t D = Prob.getDenominator();
75 uint64_t Res = (N * getBlockFreq(Src)) / D;
77 assert(Res <= UINT32_MAX);
78 return (uint32_t) Res;
81 /// incBlockFreq - Increase BB block frequency by FREQ.
83 void incBlockFreq(BlockT *BB, uint32_t Freq) {
85 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") += " << Freq
86 << " --> " << Freqs[BB] << "\n");
89 /// divBlockFreq - Divide BB block frequency by PROB. If Prob = 0 do nothing.
91 void divBlockFreq(BlockT *BB, BranchProbability Prob) {
92 uint64_t N = Prob.getNumerator();
93 assert(N && "Illegal division by zero!");
94 uint64_t D = Prob.getDenominator();
95 uint64_t Freq = (Freqs[BB] * D) / N;
97 // Should we assert it?
98 if (Freq > UINT32_MAX)
101 Freqs[BB] = (uint32_t) Freq;
102 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") /= (" << Prob
103 << ") --> " << Freqs[BB] << "\n");
106 // All blocks in postorder.
107 std::vector<BlockT *> POT;
109 // Map Block -> Position in reverse-postorder list.
110 DenseMap<BlockT *, unsigned> RPO;
112 // Cycle Probability for each bloch.
113 DenseMap<BlockT *, uint32_t> CycleProb;
115 // (reverse-)postorder traversal iterators.
116 typedef typename std::vector<BlockT *>::iterator pot_iterator;
117 typedef typename std::vector<BlockT *>::reverse_iterator rpot_iterator;
119 pot_iterator pot_begin() { return POT.begin(); }
120 pot_iterator pot_end() { return POT.end(); }
122 rpot_iterator rpot_begin() { return POT.rbegin(); }
123 rpot_iterator rpot_end() { return POT.rend(); }
125 rpot_iterator rpot_at(BlockT *BB) {
126 rpot_iterator I = rpot_begin();
127 unsigned idx = RPO[BB];
129 std::advance(I, idx - 1);
136 /// isReachable - Returns if BB block is reachable from the entry.
138 bool isReachable(BlockT *BB) {
139 return RPO.count(BB);
142 /// isBackedge - Return if edge Src -> Dst is a backedge.
144 bool isBackedge(BlockT *Src, BlockT *Dst) {
145 assert(isReachable(Src));
146 assert(isReachable(Dst));
148 unsigned a = RPO[Src];
149 unsigned b = RPO[Dst];
154 /// getSingleBlockPred - return single BB block predecessor or NULL if
155 /// BB has none or more predecessors.
156 BlockT *getSingleBlockPred(BlockT *BB) {
157 typename GT::ChildIteratorType
158 PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
159 PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
173 void doBlock(BlockT *BB, BlockT *LoopHead,
174 SmallPtrSet<BlockT *, 8> &BlocksInLoop) {
176 DEBUG(dbgs() << "doBlock(" << getBlockName(BB) << ")\n");
179 if (BB == LoopHead) {
180 setBlockFreq(BB, START_FREQ);
184 if(BlockT *Pred = getSingleBlockPred(BB)) {
185 if (BlocksInLoop.count(Pred))
186 setBlockFreq(BB, getEdgeFreq(Pred, BB));
187 // TODO: else? irreducible, ignore it for now.
191 bool isInLoop = false;
192 bool isLoopHead = false;
194 for (typename GT::ChildIteratorType
195 PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
196 PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
200 if (isReachable(Pred) && isBackedge(Pred, BB)) {
202 } else if (BlocksInLoop.count(Pred)) {
203 incBlockFreq(BB, getEdgeFreq(Pred, BB));
206 // TODO: else? irreducible.
215 assert(START_FREQ >= CycleProb[BB]);
216 divBlockFreq(BB, BranchProbability(START_FREQ - CycleProb[BB], START_FREQ));
219 /// doLoop - Propagate block frequency down throught the loop.
220 void doLoop(BlockT *Head, BlockT *Tail) {
221 DEBUG(dbgs() << "doLoop(" << getBlockName(Head) << ", "
222 << getBlockName(Tail) << ")\n");
224 SmallPtrSet<BlockT *, 8> BlocksInLoop;
226 for (rpot_iterator I = rpot_at(Head), E = rpot_end(); I != E; ++I) {
228 doBlock(BB, Head, BlocksInLoop);
230 BlocksInLoop.insert(BB);
233 // Compute loop's cyclic probability using backedges probabilities.
234 for (typename GT::ChildIteratorType
235 PI = GraphTraits< Inverse<BlockT *> >::child_begin(Head),
236 PE = GraphTraits< Inverse<BlockT *> >::child_end(Head);
240 if (isReachable(Pred) && isBackedge(Pred, Head)) {
241 BranchProbability Prob = BPI->getBackEdgeProbability(Pred, Head);
242 uint64_t N = Prob.getNumerator();
243 uint64_t D = Prob.getDenominator();
244 uint64_t Res = (N * START_FREQ) / D;
246 // CycleProb[Head] += getEdgeFreq(Pred, Head);
247 assert(Res <= UINT32_MAX);
248 CycleProb[Head] += (uint32_t) Res;
254 friend class BlockFrequency;
256 void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {
266 BlockT *EntryBlock = fn->begin();
268 copy(po_begin(EntryBlock), po_end(EntryBlock), back_inserter(POT));
271 for (rpot_iterator I = rpot_begin(), E = rpot_end(); I != E; ++I) {
274 DEBUG(dbgs() << "RPO[" << getBlockName(BB) << "] = " << RPO[BB] << "\n");
277 // Travel over all blocks in postorder.
278 for (pot_iterator I = pot_begin(), E = pot_end(); I != E; ++I) {
280 BlockT *LastTail = 0;
281 DEBUG(dbgs() << "POT: " << getBlockName(BB) << "\n");
283 for (typename GT::ChildIteratorType
284 PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
285 PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
289 if (isReachable(Pred) && isBackedge(Pred, BB)
290 && (!LastTail || RPO[Pred] > RPO[LastTail]))
295 doLoop(BB, LastTail);
298 // At the end assume the whole function as a loop, and travel over it once
300 doLoop(*(rpot_begin()), *(pot_begin()));
304 /// getBlockFreq - Return block frequency. Never return 0, value must be
306 uint32_t getBlockFreq(BlockT *BB) const {
307 typename DenseMap<BlockT *, uint32_t>::const_iterator I = Freqs.find(BB);
308 if (I != Freqs.end())
309 return I->second ? I->second : 1;
313 void print(raw_ostream &OS) const {
314 OS << "\n\n---- Block Freqs ----\n";
315 for (typename FunctionT::iterator I = Fn->begin(), E = Fn->end(); I != E;) {
317 OS << " " << getBlockName(BB) << " = " << getBlockFreq(BB) << "\n";
319 for (typename GraphTraits<BlockT *>::ChildIteratorType
320 SI = GraphTraits<BlockT *>::child_begin(BB),
321 SE = GraphTraits<BlockT *>::child_end(BB); SI != SE; ++SI) {
323 OS << " " << getBlockName(BB) << " -> " << getBlockName(Succ)
324 << " = " << getEdgeFreq(BB, Succ) << "\n";