llvm.org GIT mirror llvm / e9139c6
blockfreq: Rename BlockFrequencyImpl to BlockFrequencyInfoImpl This is a shared implementation class for BlockFrequencyInfo and MachineBlockFrequencyInfo, not for BlockFrequency, a related (but distinct) class. No functionality change. <rdar://problem/14292693> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206083 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 6 years ago
6 changed file(s) with 396 addition(s) and 396 deletion(s). Raw diff Collapse all Expand all
+0
-379
include/llvm/Analysis/BlockFrequencyImpl.h less more
None //===-- BlockFrequencyImpl.h - Block Frequency Implementation --*- C++ -*--===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Shared implementation of BlockFrequency for IR and Machine Instructions.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
14 #define LLVM_ANALYSIS_BLOCKFREQUENCYIMPL_H
15
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/PostOrderIterator.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/Support/BlockFrequency.h"
22 #include "llvm/Support/BranchProbability.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include
26 #include
27
28 namespace llvm {
29
30
31 class BlockFrequencyInfo;
32 class MachineBlockFrequencyInfo;
33
34 /// BlockFrequencyImpl implements block frequency algorithm for IR and
35 /// Machine Instructions. Algorithm starts with value ENTRY_FREQ
36 /// for the entry block and then propagates frequencies using branch weights
37 /// from (Machine)BranchProbabilityInfo. LoopInfo is not required because
38 /// algorithm can find "backedges" by itself.
39 template
40 class BlockFrequencyImpl {
41
42 DenseMap Freqs;
43
44 BlockProbInfoT *BPI;
45
46 FunctionT *Fn;
47
48 typedef GraphTraits< Inverse > GT;
49
50 static const uint64_t EntryFreq = 1 << 14;
51
52 std::string getBlockName(BasicBlock *BB) const {
53 return BB->getName().str();
54 }
55
56 std::string getBlockName(MachineBasicBlock *MBB) const {
57 std::string str;
58 raw_string_ostream ss(str);
59 ss << "BB#" << MBB->getNumber();
60
61 if (const BasicBlock *BB = MBB->getBasicBlock())
62 ss << " derived from LLVM BB " << BB->getName();
63
64 return ss.str();
65 }
66
67 void setBlockFreq(BlockT *BB, BlockFrequency Freq) {
68 Freqs[BB] = Freq;
69 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") = ";
70 printBlockFreq(dbgs(), Freq) << "\n");
71 }
72
73 /// getEdgeFreq - Return edge frequency based on SRC frequency and Src -> Dst
74 /// edge probability.
75 BlockFrequency getEdgeFreq(BlockT *Src, BlockT *Dst) const {
76 BranchProbability Prob = BPI->getEdgeProbability(Src, Dst);
77 return getBlockFreq(Src) * Prob;
78 }
79
80 /// incBlockFreq - Increase BB block frequency by FREQ.
81 ///
82 void incBlockFreq(BlockT *BB, BlockFrequency Freq) {
83 Freqs[BB] += Freq;
84 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") += ";
85 printBlockFreq(dbgs(), Freq) << " --> ";
86 printBlockFreq(dbgs(), Freqs[BB]) << "\n");
87 }
88
89 // All blocks in postorder.
90 std::vector POT;
91
92 // Map Block -> Position in reverse-postorder list.
93 DenseMap RPO;
94
95 // For each loop header, record the per-iteration probability of exiting the
96 // loop. This is the reciprocal of the expected number of loop iterations.
97 typedef DenseMap LoopExitProbMap;
98 LoopExitProbMap LoopExitProb;
99
100 // (reverse-)postorder traversal iterators.
101 typedef typename std::vector::iterator pot_iterator;
102 typedef typename std::vector::reverse_iterator rpot_iterator;
103
104 pot_iterator pot_begin() { return POT.begin(); }
105 pot_iterator pot_end() { return POT.end(); }
106
107 rpot_iterator rpot_begin() { return POT.rbegin(); }
108 rpot_iterator rpot_end() { return POT.rend(); }
109
110 rpot_iterator rpot_at(BlockT *BB) {
111 rpot_iterator I = rpot_begin();
112 unsigned idx = RPO.lookup(BB);
113 assert(idx);
114 std::advance(I, idx - 1);
115
116 assert(*I == BB);
117 return I;
118 }
119
120 /// isBackedge - Return if edge Src -> Dst is a reachable backedge.
121 ///
122 bool isBackedge(BlockT *Src, BlockT *Dst) const {
123 unsigned a = RPO.lookup(Src);
124 if (!a)
125 return false;
126 unsigned b = RPO.lookup(Dst);
127 assert(b && "Destination block should be reachable");
128 return a >= b;
129 }
130
131 /// getSingleBlockPred - return single BB block predecessor or NULL if
132 /// BB has none or more predecessors.
133 BlockT *getSingleBlockPred(BlockT *BB) {
134 typename GT::ChildIteratorType
135 PI = GraphTraits< Inverse >::child_begin(BB),
136 PE = GraphTraits< Inverse >::child_end(BB);
137
138 if (PI == PE)
139 return 0;
140
141 BlockT *Pred = *PI;
142
143 ++PI;
144 if (PI != PE)
145 return 0;
146
147 return Pred;
148 }
149
150 void doBlock(BlockT *BB, BlockT *LoopHead,
151 SmallPtrSet &BlocksInLoop) {
152
153 DEBUG(dbgs() << "doBlock(" << getBlockName(BB) << ")\n");
154 setBlockFreq(BB, 0);
155
156 if (BB == LoopHead) {
157 setBlockFreq(BB, EntryFreq);
158 return;
159 }
160
161 if (BlockT *Pred = getSingleBlockPred(BB)) {
162 if (BlocksInLoop.count(Pred))
163 setBlockFreq(BB, getEdgeFreq(Pred, BB));
164 // TODO: else? irreducible, ignore it for now.
165 return;
166 }
167
168 bool isInLoop = false;
169 bool isLoopHead = false;
170
171 for (typename GT::ChildIteratorType
172 PI = GraphTraits< Inverse >::child_begin(BB),
173 PE = GraphTraits< Inverse >::child_end(BB);
174 PI != PE; ++PI) {
175 BlockT *Pred = *PI;
176
177 if (isBackedge(Pred, BB)) {
178 isLoopHead = true;
179 } else if (BlocksInLoop.count(Pred)) {
180 incBlockFreq(BB, getEdgeFreq(Pred, BB));
181 isInLoop = true;
182 }
183 // TODO: else? irreducible.
184 }
185
186 if (!isInLoop)
187 return;
188
189 if (!isLoopHead)
190 return;
191
192 // This block is a loop header, so boost its frequency by the expected
193 // number of loop iterations. The loop blocks will be revisited so they all
194 // get this boost.
195 typename LoopExitProbMap::const_iterator I = LoopExitProb.find(BB);
196 assert(I != LoopExitProb.end() && "Loop header missing from table");
197 Freqs[BB] /= I->second;
198 DEBUG(dbgs() << "Loop header scaled to ";
199 printBlockFreq(dbgs(), Freqs[BB]) << ".\n");
200 }
201
202 /// doLoop - Propagate block frequency down through the loop.
203 void doLoop(BlockT *Head, BlockT *Tail) {
204 DEBUG(dbgs() << "doLoop(" << getBlockName(Head) << ", "
205 << getBlockName(Tail) << ")\n");
206
207 SmallPtrSet BlocksInLoop;
208
209 for (rpot_iterator I = rpot_at(Head), E = rpot_at(Tail); ; ++I) {
210 BlockT *BB = *I;
211 doBlock(BB, Head, BlocksInLoop);
212
213 BlocksInLoop.insert(BB);
214 if (I == E)
215 break;
216 }
217
218 // Compute loop's cyclic probability using backedges probabilities.
219 BlockFrequency BackFreq;
220 for (typename GT::ChildIteratorType
221 PI = GraphTraits< Inverse >::child_begin(Head),
222 PE = GraphTraits< Inverse >::child_end(Head);
223 PI != PE; ++PI) {
224 BlockT *Pred = *PI;
225 assert(Pred);
226 if (isBackedge(Pred, Head))
227 BackFreq += getEdgeFreq(Pred, Head);
228 }
229
230 // The cyclic probability is freq(BackEdges) / freq(Head), where freq(Head)
231 // only counts edges entering the loop, not the loop backedges.
232 // The probability of leaving the loop on each iteration is:
233 //
234 // ExitProb = 1 - CyclicProb
235 //
236 // The Expected number of loop iterations is:
237 //
238 // Iterations = 1 / ExitProb
239 //
240 uint64_t D = std::max(getBlockFreq(Head).getFrequency(), UINT64_C(1));
241 uint64_t N = std::max(BackFreq.getFrequency(), UINT64_C(1));
242 if (N < D)
243 N = D - N;
244 else
245 // We'd expect N < D, but rounding and saturation means that can't be
246 // guaranteed.
247 N = 1;
248
249 // Now ExitProb = N / D, make sure it fits in an i32/i32 fraction.
250 assert(N <= D);
251 if (D > UINT32_MAX) {
252 unsigned Shift = 32 - countLeadingZeros(D);
253 D >>= Shift;
254 N >>= Shift;
255 if (N == 0)
256 N = 1;
257 }
258 BranchProbability LEP = BranchProbability(N, D);
259 LoopExitProb.insert(std::make_pair(Head, LEP));
260 DEBUG(dbgs() << "LoopExitProb[" << getBlockName(Head) << "] = " << LEP
261 << " from 1 - ";
262 printBlockFreq(dbgs(), BackFreq) << " / ";
263 printBlockFreq(dbgs(), getBlockFreq(Head)) << ".\n");
264 }
265
266 friend class BlockFrequencyInfo;
267 friend class MachineBlockFrequencyInfo;
268
269 BlockFrequencyImpl() { }
270
271 void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {
272 Fn = fn;
273 BPI = bpi;
274
275 // Clear everything.
276 RPO.clear();
277 POT.clear();
278 LoopExitProb.clear();
279 Freqs.clear();
280
281 BlockT *EntryBlock = fn->begin();
282
283 std::copy(po_begin(EntryBlock), po_end(EntryBlock), std::back_inserter(POT));
284
285 unsigned RPOidx = 0;
286 for (rpot_iterator I = rpot_begin(), E = rpot_end(); I != E; ++I) {
287 BlockT *BB = *I;
288 RPO[BB] = ++RPOidx;
289 DEBUG(dbgs() << "RPO[" << getBlockName(BB) << "] = " << RPO[BB] << "\n");
290 }
291
292 // Travel over all blocks in postorder.
293 for (pot_iterator I = pot_begin(), E = pot_end(); I != E; ++I) {
294 BlockT *BB = *I;
295 BlockT *LastTail = 0;
296 DEBUG(dbgs() << "POT: " << getBlockName(BB) << "\n");
297
298 for (typename GT::ChildIteratorType
299 PI = GraphTraits< Inverse >::child_begin(BB),
300 PE = GraphTraits< Inverse >::child_end(BB);
301 PI != PE; ++PI) {
302
303 BlockT *Pred = *PI;
304 if (isBackedge(Pred, BB) && (!LastTail || RPO[Pred] > RPO[LastTail]))
305 LastTail = Pred;
306 }
307
308 if (LastTail)
309 doLoop(BB, LastTail);
310 }
311
312 // At the end assume the whole function as a loop, and travel over it once
313 // again.
314 doLoop(*(rpot_begin()), *(pot_begin()));
315 }
316
317 public:
318
319 uint64_t getEntryFreq() { return EntryFreq; }
320
321 /// getBlockFreq - Return block frequency. Return 0 if we don't have it.
322 BlockFrequency getBlockFreq(const BlockT *BB) const {
323 typename DenseMap::const_iterator
324 I = Freqs.find(BB);
325 if (I != Freqs.end())
326 return I->second;
327 return 0;
328 }
329
330 void print(raw_ostream &OS) const {
331 OS << "\n\n---- Block Freqs ----\n";
332 for (typename FunctionT::iterator I = Fn->begin(), E = Fn->end(); I != E;) {
333 BlockT *BB = I++;
334 OS << " " << getBlockName(BB) << " = ";
335 printBlockFreq(OS, getBlockFreq(BB)) << "\n";
336
337 for (typename GraphTraits::ChildIteratorType
338 SI = GraphTraits::child_begin(BB),
339 SE = GraphTraits::child_end(BB); SI != SE; ++SI) {
340 BlockT *Succ = *SI;
341 OS << " " << getBlockName(BB) << " -> " << getBlockName(Succ)
342 << " = "; printBlockFreq(OS, getEdgeFreq(BB, Succ)) << "\n";
343 }
344 }
345 }
346
347 void dump() const {
348 print(dbgs());
349 }
350
351 // Utility method that looks up the block frequency associated with BB and
352 // prints it to OS.
353 raw_ostream &printBlockFreq(raw_ostream &OS,
354 const BlockT *BB) {
355 return printBlockFreq(OS, getBlockFreq(BB));
356 }
357
358 raw_ostream &printBlockFreq(raw_ostream &OS,
359 const BlockFrequency &Freq) const {
360 // Convert fixed-point number to decimal.
361 uint64_t Frequency = Freq.getFrequency();
362 OS << Frequency / EntryFreq << ".";
363 uint64_t Rem = Frequency % EntryFreq;
364 uint64_t Eps = 1;
365 do {
366 Rem *= 10;
367 Eps *= 10;
368 OS << Rem / EntryFreq;
369 Rem = Rem % EntryFreq;
370 } while (Rem >= Eps/2);
371 return OS;
372 }
373
374 };
375
376 }
377
378 #endif
None //===------- BlockFrequencyInfo.h - Block Frequency Analysis --*- C++ -*---===//
0 //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
2020 namespace llvm {
2121
2222 class BranchProbabilityInfo;
23 template
24 class BlockFrequencyImpl;
23 template
24 class BlockFrequencyInfoImpl;
2525
26 /// BlockFrequencyInfo pass uses BlockFrequencyImpl implementation to estimate
27 /// IR basic block frequencies.
26 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
27 /// estimate IR basic block frequencies.
2828 class BlockFrequencyInfo : public FunctionPass {
29 typedef BlockFrequencyImpl
29 typedef BlockFrequencyInfoImpl
3030 ImplType;
3131 std::unique_ptr BFI;
3232
0 //==- BlockFrequencyInfoImpl.h - Block Frequency Implementation -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Shared implementation of BlockFrequencyInfo for IR and Machine Instructions.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFOIMPL_H
14 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFOIMPL_H
15
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/PostOrderIterator.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/Support/BlockFrequency.h"
22 #include "llvm/Support/BranchProbability.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include
26 #include
27
28 namespace llvm {
29
30
31 class BlockFrequencyInfo;
32 class MachineBlockFrequencyInfo;
33
34 /// BlockFrequencyInfoImpl implements block frequency algorithm for IR and
35 /// Machine Instructions. Algorithm starts with value ENTRY_FREQ
36 /// for the entry block and then propagates frequencies using branch weights
37 /// from (Machine)BranchProbabilityInfo. LoopInfo is not required because
38 /// algorithm can find "backedges" by itself.
39 template
40 class BlockFrequencyInfoImpl {
41
42 DenseMap Freqs;
43
44 BlockProbInfoT *BPI;
45
46 FunctionT *Fn;
47
48 typedef GraphTraits< Inverse > GT;
49
50 static const uint64_t EntryFreq = 1 << 14;
51
52 std::string getBlockName(BasicBlock *BB) const {
53 return BB->getName().str();
54 }
55
56 std::string getBlockName(MachineBasicBlock *MBB) const {
57 std::string str;
58 raw_string_ostream ss(str);
59 ss << "BB#" << MBB->getNumber();
60
61 if (const BasicBlock *BB = MBB->getBasicBlock())
62 ss << " derived from LLVM BB " << BB->getName();
63
64 return ss.str();
65 }
66
67 void setBlockFreq(BlockT *BB, BlockFrequency Freq) {
68 Freqs[BB] = Freq;
69 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") = ";
70 printBlockFreq(dbgs(), Freq) << "\n");
71 }
72
73 /// getEdgeFreq - Return edge frequency based on SRC frequency and Src -> Dst
74 /// edge probability.
75 BlockFrequency getEdgeFreq(BlockT *Src, BlockT *Dst) const {
76 BranchProbability Prob = BPI->getEdgeProbability(Src, Dst);
77 return getBlockFreq(Src) * Prob;
78 }
79
80 /// incBlockFreq - Increase BB block frequency by FREQ.
81 ///
82 void incBlockFreq(BlockT *BB, BlockFrequency Freq) {
83 Freqs[BB] += Freq;
84 DEBUG(dbgs() << "Frequency(" << getBlockName(BB) << ") += ";
85 printBlockFreq(dbgs(), Freq) << " --> ";
86 printBlockFreq(dbgs(), Freqs[BB]) << "\n");
87 }
88
89 // All blocks in postorder.
90 std::vector POT;
91
92 // Map Block -> Position in reverse-postorder list.
93 DenseMap RPO;
94
95 // For each loop header, record the per-iteration probability of exiting the
96 // loop. This is the reciprocal of the expected number of loop iterations.
97 typedef DenseMap LoopExitProbMap;
98 LoopExitProbMap LoopExitProb;
99
100 // (reverse-)postorder traversal iterators.
101 typedef typename std::vector::iterator pot_iterator;
102 typedef typename std::vector::reverse_iterator rpot_iterator;
103
104 pot_iterator pot_begin() { return POT.begin(); }
105 pot_iterator pot_end() { return POT.end(); }
106
107 rpot_iterator rpot_begin() { return POT.rbegin(); }
108 rpot_iterator rpot_end() { return POT.rend(); }
109
110 rpot_iterator rpot_at(BlockT *BB) {
111 rpot_iterator I = rpot_begin();
112 unsigned idx = RPO.lookup(BB);
113 assert(idx);
114 std::advance(I, idx - 1);
115
116 assert(*I == BB);
117 return I;
118 }
119
120 /// isBackedge - Return if edge Src -> Dst is a reachable backedge.
121 ///
122 bool isBackedge(BlockT *Src, BlockT *Dst) const {
123 unsigned a = RPO.lookup(Src);
124 if (!a)
125 return false;
126 unsigned b = RPO.lookup(Dst);
127 assert(b && "Destination block should be reachable");
128 return a >= b;
129 }
130
131 /// getSingleBlockPred - return single BB block predecessor or NULL if
132 /// BB has none or more predecessors.
133 BlockT *getSingleBlockPred(BlockT *BB) {
134 typename GT::ChildIteratorType
135 PI = GraphTraits< Inverse >::child_begin(BB),
136 PE = GraphTraits< Inverse >::child_end(BB);
137
138 if (PI == PE)
139 return 0;
140
141 BlockT *Pred = *PI;
142
143 ++PI;
144 if (PI != PE)
145 return 0;
146
147 return Pred;
148 }
149
150 void doBlock(BlockT *BB, BlockT *LoopHead,
151 SmallPtrSet &BlocksInLoop) {
152
153 DEBUG(dbgs() << "doBlock(" << getBlockName(BB) << ")\n");
154 setBlockFreq(BB, 0);
155
156 if (BB == LoopHead) {
157 setBlockFreq(BB, EntryFreq);
158 return;
159 }
160
161 if (BlockT *Pred = getSingleBlockPred(BB)) {
162 if (BlocksInLoop.count(Pred))
163 setBlockFreq(BB, getEdgeFreq(Pred, BB));
164 // TODO: else? irreducible, ignore it for now.
165 return;
166 }
167
168 bool isInLoop = false;
169 bool isLoopHead = false;
170
171 for (typename GT::ChildIteratorType
172 PI = GraphTraits< Inverse >::child_begin(BB),
173 PE = GraphTraits< Inverse >::child_end(BB);
174 PI != PE; ++PI) {
175 BlockT *Pred = *PI;
176
177 if (isBackedge(Pred, BB)) {
178 isLoopHead = true;
179 } else if (BlocksInLoop.count(Pred)) {
180 incBlockFreq(BB, getEdgeFreq(Pred, BB));
181 isInLoop = true;
182 }
183 // TODO: else? irreducible.
184 }
185
186 if (!isInLoop)
187 return;
188
189 if (!isLoopHead)
190 return;
191
192 // This block is a loop header, so boost its frequency by the expected
193 // number of loop iterations. The loop blocks will be revisited so they all
194 // get this boost.
195 typename LoopExitProbMap::const_iterator I = LoopExitProb.find(BB);
196 assert(I != LoopExitProb.end() && "Loop header missing from table");
197 Freqs[BB] /= I->second;
198 DEBUG(dbgs() << "Loop header scaled to ";
199 printBlockFreq(dbgs(), Freqs[BB]) << ".\n");
200 }
201
202 /// doLoop - Propagate block frequency down through the loop.
203 void doLoop(BlockT *Head, BlockT *Tail) {
204 DEBUG(dbgs() << "doLoop(" << getBlockName(Head) << ", "
205 << getBlockName(Tail) << ")\n");
206
207 SmallPtrSet BlocksInLoop;
208
209 for (rpot_iterator I = rpot_at(Head), E = rpot_at(Tail); ; ++I) {
210 BlockT *BB = *I;
211 doBlock(BB, Head, BlocksInLoop);
212
213 BlocksInLoop.insert(BB);
214 if (I == E)
215 break;
216 }
217
218 // Compute loop's cyclic probability using backedges probabilities.
219 BlockFrequency BackFreq;
220 for (typename GT::ChildIteratorType
221 PI = GraphTraits< Inverse >::child_begin(Head),
222 PE = GraphTraits< Inverse >::child_end(Head);
223 PI != PE; ++PI) {
224 BlockT *Pred = *PI;
225 assert(Pred);
226 if (isBackedge(Pred, Head))
227 BackFreq += getEdgeFreq(Pred, Head);
228 }
229
230 // The cyclic probability is freq(BackEdges) / freq(Head), where freq(Head)
231 // only counts edges entering the loop, not the loop backedges.
232 // The probability of leaving the loop on each iteration is:
233 //
234 // ExitProb = 1 - CyclicProb
235 //
236 // The Expected number of loop iterations is:
237 //
238 // Iterations = 1 / ExitProb
239 //
240 uint64_t D = std::max(getBlockFreq(Head).getFrequency(), UINT64_C(1));
241 uint64_t N = std::max(BackFreq.getFrequency(), UINT64_C(1));
242 if (N < D)
243 N = D - N;
244 else
245 // We'd expect N < D, but rounding and saturation means that can't be
246 // guaranteed.
247 N = 1;
248
249 // Now ExitProb = N / D, make sure it fits in an i32/i32 fraction.
250 assert(N <= D);
251 if (D > UINT32_MAX) {
252 unsigned Shift = 32 - countLeadingZeros(D);
253 D >>= Shift;
254 N >>= Shift;
255 if (N == 0)
256 N = 1;
257 }
258 BranchProbability LEP = BranchProbability(N, D);
259 LoopExitProb.insert(std::make_pair(Head, LEP));
260 DEBUG(dbgs() << "LoopExitProb[" << getBlockName(Head) << "] = " << LEP
261 << " from 1 - ";
262 printBlockFreq(dbgs(), BackFreq) << " / ";
263 printBlockFreq(dbgs(), getBlockFreq(Head)) << ".\n");
264 }
265
266 friend class BlockFrequencyInfo;
267 friend class MachineBlockFrequencyInfo;
268
269 BlockFrequencyInfoImpl() { }
270
271 void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {
272 Fn = fn;
273 BPI = bpi;
274
275 // Clear everything.
276 RPO.clear();
277 POT.clear();
278 LoopExitProb.clear();
279 Freqs.clear();
280
281 BlockT *EntryBlock = fn->begin();
282
283 std::copy(po_begin(EntryBlock), po_end(EntryBlock), std::back_inserter(POT));
284
285 unsigned RPOidx = 0;
286 for (rpot_iterator I = rpot_begin(), E = rpot_end(); I != E; ++I) {
287 BlockT *BB = *I;
288 RPO[BB] = ++RPOidx;
289 DEBUG(dbgs() << "RPO[" << getBlockName(BB) << "] = " << RPO[BB] << "\n");
290 }
291
292 // Travel over all blocks in postorder.
293 for (pot_iterator I = pot_begin(), E = pot_end(); I != E; ++I) {
294 BlockT *BB = *I;
295 BlockT *LastTail = 0;
296 DEBUG(dbgs() << "POT: " << getBlockName(BB) << "\n");
297
298 for (typename GT::ChildIteratorType
299 PI = GraphTraits< Inverse >::child_begin(BB),
300 PE = GraphTraits< Inverse >::child_end(BB);
301 PI != PE; ++PI) {
302
303 BlockT *Pred = *PI;
304 if (isBackedge(Pred, BB) && (!LastTail || RPO[Pred] > RPO[LastTail]))
305 LastTail = Pred;
306 }
307
308 if (LastTail)
309 doLoop(BB, LastTail);
310 }
311
312 // At the end assume the whole function as a loop, and travel over it once
313 // again.
314 doLoop(*(rpot_begin()), *(pot_begin()));
315 }
316
317 public:
318
319 uint64_t getEntryFreq() { return EntryFreq; }
320
321 /// getBlockFreq - Return block frequency. Return 0 if we don't have it.
322 BlockFrequency getBlockFreq(const BlockT *BB) const {
323 typename DenseMap::const_iterator
324 I = Freqs.find(BB);
325 if (I != Freqs.end())
326 return I->second;
327 return 0;
328 }
329
330 void print(raw_ostream &OS) const {
331 OS << "\n\n---- Block Freqs ----\n";
332 for (typename FunctionT::iterator I = Fn->begin(), E = Fn->end(); I != E;) {
333 BlockT *BB = I++;
334 OS << " " << getBlockName(BB) << " = ";
335 printBlockFreq(OS, getBlockFreq(BB)) << "\n";
336
337 for (typename GraphTraits::ChildIteratorType
338 SI = GraphTraits::child_begin(BB),
339 SE = GraphTraits::child_end(BB); SI != SE; ++SI) {
340 BlockT *Succ = *SI;
341 OS << " " << getBlockName(BB) << " -> " << getBlockName(Succ)
342 << " = "; printBlockFreq(OS, getEdgeFreq(BB, Succ)) << "\n";
343 }
344 }
345 }
346
347 void dump() const {
348 print(dbgs());
349 }
350
351 // Utility method that looks up the block frequency associated with BB and
352 // prints it to OS.
353 raw_ostream &printBlockFreq(raw_ostream &OS,
354 const BlockT *BB) {
355 return printBlockFreq(OS, getBlockFreq(BB));
356 }
357
358 raw_ostream &printBlockFreq(raw_ostream &OS,
359 const BlockFrequency &Freq) const {
360 // Convert fixed-point number to decimal.
361 uint64_t Frequency = Freq.getFrequency();
362 OS << Frequency / EntryFreq << ".";
363 uint64_t Rem = Frequency % EntryFreq;
364 uint64_t Eps = 1;
365 do {
366 Rem *= 10;
367 Eps *= 10;
368 OS << Rem / EntryFreq;
369 Rem = Rem % EntryFreq;
370 } while (Rem >= Eps/2);
371 return OS;
372 }
373
374 };
375
376 }
377
378 #endif
None //====-- MachineBlockFrequencyInfo.h - MBB Frequency Analysis -*- C++ -*--====//
0 //===- MachineBlockFrequencyInfo.h - MBB Frequency Analysis -*- C++ -*-----===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
2121
2222 class MachineBasicBlock;
2323 class MachineBranchProbabilityInfo;
24 template
25 class BlockFrequencyImpl;
24 template
25 class BlockFrequencyInfoImpl;
2626
27 /// MachineBlockFrequencyInfo pass uses BlockFrequencyImpl implementation to estimate
28 /// machine basic block frequencies.
27 /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
28 /// to estimate machine basic block frequencies.
2929 class MachineBlockFrequencyInfo : public MachineFunctionPass {
30 typedef BlockFrequencyImpl
31 MachineBranchProbabilityInfo> ImplType;
30 typedef BlockFrequencyInfoImpl
31 MachineBranchProbabilityInfo> ImplType;
3232 std::unique_ptr MBFI;
3333
3434 public:
None //=======-------- BlockFrequencyInfo.cpp - Block Frequency Analysis -------===//
0 //===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Analysis/BlockFrequencyInfo.h"
14 #include "llvm/Analysis/BlockFrequencyImpl.h"
14 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
1515 #include "llvm/Analysis/BranchProbabilityInfo.h"
1616 #include "llvm/Analysis/LoopInfo.h"
1717 #include "llvm/Analysis/Passes.h"
None //====------ MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis ------====//
0 //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
14 #include "llvm/Analysis/BlockFrequencyImpl.h"
14 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
1515 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
1616 #include "llvm/CodeGen/Passes.h"
1717 #include "llvm/InitializePasses.h"