Missing files for the BlockFrequency analysis added.
[oota-llvm.git] / include / llvm / Analysis / BlockFrequencyImpl.h
1 //===---- BlockFrequencyImpl.h - Machine Block Frequency Implementation ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Shared implementation of BlockFrequency for IR and Machine Instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
15 #define LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
16
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"
23 #include <vector>
24 #include <sstream>
25 #include <string>
26
27 namespace llvm {
28
29
30 class BlockFrequency;
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 {
38
39   DenseMap<BlockT *, uint32_t> Freqs;
40
41   BlockProbInfoT *BPI;
42
43   FunctionT *Fn;
44
45   typedef GraphTraits< Inverse<BlockT *> > GT;
46
47   static const uint32_t START_FREQ = 1024;
48
49   std::string getBlockName(BasicBlock *BB) const {
50     return BB->getNameStr();
51   }
52
53   std::string getBlockName(MachineBasicBlock *MBB) const {
54     std::stringstream ss;
55     ss << "BB#" << MBB->getNumber();
56     const BasicBlock *BB = MBB->getBasicBlock();
57
58     if (BB)
59       ss << " derived from LLVM BB " << BB->getNameStr();
60
61     return ss.str();
62   }
63
64   void setBlockFreq(BlockT *BB, uint32_t Freq) {
65     Freqs[BB] = Freq;
66     DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") = " << Freq << "\n");
67   }
68
69   /// getEdgeFreq - Return edge frequency based on SRC frequency and Src -> Dst
70   /// edge probability.
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;
76
77     assert(Res <= UINT32_MAX);
78     return (uint32_t) Res;
79   }
80
81   /// incBlockFreq - Increase BB block frequency by FREQ.
82   ///
83   void incBlockFreq(BlockT *BB, uint32_t Freq) {
84     Freqs[BB] += Freq;
85     DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") += " << Freq
86                  << " --> " << Freqs[BB] << "\n");
87   }
88
89   /// divBlockFreq - Divide BB block frequency by PROB. If Prob = 0 do nothing.
90   ///
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;
96
97     // Should we assert it?
98     if (Freq > UINT32_MAX)
99       Freq = UINT32_MAX;
100
101     Freqs[BB] = (uint32_t) Freq;
102     DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") /= (" << Prob
103                  << ") --> " << Freqs[BB] << "\n");
104   }
105
106   // All blocks in postorder.
107   std::vector<BlockT *> POT;
108
109   // Map Block -> Position in reverse-postorder list.
110   DenseMap<BlockT *, unsigned> RPO;
111
112   // Cycle Probability for each bloch.
113   DenseMap<BlockT *, uint32_t> CycleProb;
114
115   // (reverse-)postorder traversal iterators.
116   typedef typename std::vector<BlockT *>::iterator pot_iterator;
117   typedef typename std::vector<BlockT *>::reverse_iterator rpot_iterator;
118
119   pot_iterator pot_begin() { return POT.begin(); }
120   pot_iterator pot_end() { return POT.end(); }
121
122   rpot_iterator rpot_begin() { return POT.rbegin(); }
123   rpot_iterator rpot_end() { return POT.rend(); }
124
125   rpot_iterator rpot_at(BlockT *BB) {
126     rpot_iterator I = rpot_begin();
127     unsigned idx = RPO[BB];
128     assert(idx);
129     std::advance(I, idx - 1);
130
131     assert(*I == BB);
132     return I;
133   }
134
135
136   /// isReachable - Returns if BB block is reachable from the entry.
137   ///
138   bool isReachable(BlockT *BB) {
139     return RPO.count(BB);
140   }
141
142   /// isBackedge - Return if edge Src -> Dst is a backedge.
143   ///
144   bool isBackedge(BlockT *Src, BlockT *Dst) {
145     assert(isReachable(Src));
146     assert(isReachable(Dst));
147
148     unsigned a = RPO[Src];
149     unsigned b = RPO[Dst];
150
151     return a > b;
152   }
153
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);
160
161     if (PI == PE)
162       return 0;
163
164     BlockT *Pred = *PI;
165
166     ++PI;
167     if (PI != PE)
168       return 0;
169
170     return Pred;
171   }
172
173   void doBlock(BlockT *BB, BlockT *LoopHead,
174                SmallPtrSet<BlockT *, 8> &BlocksInLoop) {
175
176     DEBUG(dbgs() << "doBlock(" << getBlockName(BB) << ")\n");
177     setBlockFreq(BB, 0);
178
179     if (BB == LoopHead) {
180       setBlockFreq(BB, START_FREQ);
181       return;
182     }
183
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.
188       return;
189     }
190
191     bool isInLoop = false;
192     bool isLoopHead = false;
193
194     for (typename GT::ChildIteratorType
195          PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
196          PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
197          PI != PE; ++PI) {
198       BlockT *Pred = *PI;
199
200       if (isReachable(Pred) && isBackedge(Pred, BB)) {
201         isLoopHead = true;
202       } else if (BlocksInLoop.count(Pred)) {
203         incBlockFreq(BB, getEdgeFreq(Pred, BB));
204         isInLoop = true;
205       }
206       // TODO: else? irreducible.
207     }
208
209     if (!isInLoop)
210       return;
211
212     if (!isLoopHead)
213       return;
214
215     assert(START_FREQ >= CycleProb[BB]);
216     divBlockFreq(BB, BranchProbability(START_FREQ - CycleProb[BB], START_FREQ));
217   }
218
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");
223
224     SmallPtrSet<BlockT *, 8> BlocksInLoop;
225
226     for (rpot_iterator I = rpot_at(Head), E = rpot_end(); I != E; ++I) {
227       BlockT *BB = *I;
228       doBlock(BB, Head, BlocksInLoop);
229
230       BlocksInLoop.insert(BB);
231     }
232
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);
237          PI != PE; ++PI) {
238       BlockT *Pred = *PI;
239       assert(Pred);
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;
245
246         // CycleProb[Head] += getEdgeFreq(Pred, Head);
247         assert(Res <= UINT32_MAX);
248         CycleProb[Head] += (uint32_t) Res;
249       }
250     }
251   }
252
253
254   friend class BlockFrequency;
255
256   void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {
257     Fn = fn;
258     BPI = bpi;
259
260     // Clear everything.
261     RPO.clear();
262     POT.clear();
263     CycleProb.clear();
264     Freqs.clear();
265
266     BlockT *EntryBlock = fn->begin();
267
268     copy(po_begin(EntryBlock), po_end(EntryBlock), back_inserter(POT));
269
270     unsigned RPOidx = 0;
271     for (rpot_iterator I = rpot_begin(), E = rpot_end(); I != E; ++I) {
272       BlockT *BB = *I;
273       RPO[BB] = ++RPOidx;
274       DEBUG(dbgs() << "RPO[" << getBlockName(BB) << "] = " << RPO[BB] << "\n");
275     }
276
277     // Travel over all blocks in postorder.
278     for (pot_iterator I = pot_begin(), E = pot_end(); I != E; ++I) {
279       BlockT *BB = *I;
280       BlockT *LastTail = 0;
281       DEBUG(dbgs() << "POT: " << getBlockName(BB) << "\n");
282
283       for (typename GT::ChildIteratorType
284            PI = GraphTraits< Inverse<BlockT *> >::child_begin(BB),
285            PE = GraphTraits< Inverse<BlockT *> >::child_end(BB);
286            PI != PE; ++PI) {
287
288         BlockT *Pred = *PI;
289         if (isReachable(Pred) && isBackedge(Pred, BB)
290             && (!LastTail || RPO[Pred] > RPO[LastTail]))
291           LastTail = Pred;
292       }
293
294       if (LastTail)
295         doLoop(BB, LastTail);
296     }
297
298     // At the end assume the whole function as a loop, and travel over it once
299     // again.
300     doLoop(*(rpot_begin()), *(pot_begin()));
301   }
302
303 public:
304   /// getBlockFreq - Return block frequency. Never return 0, value must be
305   /// positive.
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;
310     return 1;
311   }
312
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;) {
316       BlockT *BB = I++;
317       OS << " " << getBlockName(BB) << " = " << getBlockFreq(BB) << "\n";
318
319       for (typename GraphTraits<BlockT *>::ChildIteratorType
320            SI = GraphTraits<BlockT *>::child_begin(BB),
321            SE = GraphTraits<BlockT *>::child_end(BB); SI != SE; ++SI) {
322         BlockT *Succ = *SI;
323         OS << "  " << getBlockName(BB) << " -> " << getBlockName(Succ)
324            << " = " << getEdgeFreq(BB, Succ) << "\n";
325       }
326     }
327   }
328
329   void dump() const {
330     print(dbgs());
331   }
332 };
333
334 }
335
336 #endif