blob: c6f6fa77bc294bb472afc4004cc7ec51eee73cc6 [file] [log] [blame]
Andrew Trick9e764222011-06-04 01:16:30 +00001//===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -*- C++ -*-===//
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// Loops should be simplified before this analysis.
11//
12//===----------------------------------------------------------------------===//
13
Jakub Staszaka5dd5502011-07-31 03:27:24 +000014#include "llvm/Constants.h"
Andrew Trick9e764222011-06-04 01:16:30 +000015#include "llvm/Instructions.h"
16#include "llvm/Analysis/BranchProbabilityInfo.h"
Jakub Staszak12af93a2011-07-16 20:31:15 +000017#include "llvm/Analysis/LoopInfo.h"
Andrew Trickf289df22011-06-11 01:05:22 +000018#include "llvm/Support/Debug.h"
Andrew Trick9e764222011-06-04 01:16:30 +000019
20using namespace llvm;
21
22INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
23 "Branch Probability Analysis", false, true)
24INITIALIZE_PASS_DEPENDENCY(LoopInfo)
25INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
26 "Branch Probability Analysis", false, true)
27
28char BranchProbabilityInfo::ID = 0;
29
Benjamin Kramerafa88ea2011-06-13 18:38:56 +000030namespace {
Andrew Trick9e764222011-06-04 01:16:30 +000031// Please note that BranchProbabilityAnalysis is not a FunctionPass.
32// It is created by BranchProbabilityInfo (which is a FunctionPass), which
33// provides a clear interface. Thanks to that, all heuristics and other
34// private methods are hidden in the .cpp file.
35class BranchProbabilityAnalysis {
36
Jakub Staszak6f6baf12011-07-29 19:30:00 +000037 typedef std::pair<const BasicBlock *, const BasicBlock *> Edge;
Andrew Trick9e764222011-06-04 01:16:30 +000038
Andrew Trickf289df22011-06-11 01:05:22 +000039 DenseMap<Edge, uint32_t> *Weights;
Andrew Trick9e764222011-06-04 01:16:30 +000040
41 BranchProbabilityInfo *BP;
42
43 LoopInfo *LI;
44
45
46 // Weights are for internal use only. They are used by heuristics to help to
47 // estimate edges' probability. Example:
48 //
49 // Using "Loop Branch Heuristics" we predict weights of edges for the
50 // block BB2.
51 // ...
52 // |
53 // V
54 // BB1<-+
55 // | |
Jakub Staszak3d8b15e2011-07-28 23:42:08 +000056 // | | (Weight = 124)
Andrew Trick9e764222011-06-04 01:16:30 +000057 // V |
58 // BB2--+
59 // |
60 // | (Weight = 4)
61 // V
62 // BB3
63 //
Jakub Staszak3d8b15e2011-07-28 23:42:08 +000064 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
65 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
Andrew Trick9e764222011-06-04 01:16:30 +000066
Jakub Staszak3d8b15e2011-07-28 23:42:08 +000067 static const uint32_t LBH_TAKEN_WEIGHT = 124;
Andrew Trickf289df22011-06-11 01:05:22 +000068 static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
Andrew Trick9e764222011-06-04 01:16:30 +000069
Jakub Staszake0058b42011-07-29 02:36:53 +000070 static const uint32_t RH_TAKEN_WEIGHT = 24;
71 static const uint32_t RH_NONTAKEN_WEIGHT = 8;
72
73 static const uint32_t PH_TAKEN_WEIGHT = 20;
74 static const uint32_t PH_NONTAKEN_WEIGHT = 12;
75
Jakub Staszaka5dd5502011-07-31 03:27:24 +000076 static const uint32_t ZH_TAKEN_WEIGHT = 20;
77 static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
78
Andrew Trick9e764222011-06-04 01:16:30 +000079 // Standard weight value. Used when none of the heuristics set weight for
80 // the edge.
Andrew Trickf289df22011-06-11 01:05:22 +000081 static const uint32_t NORMAL_WEIGHT = 16;
Andrew Trick9e764222011-06-04 01:16:30 +000082
83 // Minimum weight of an edge. Please note, that weight is NEVER 0.
Andrew Trickf289df22011-06-11 01:05:22 +000084 static const uint32_t MIN_WEIGHT = 1;
Andrew Trick9e764222011-06-04 01:16:30 +000085
86 // Return TRUE if BB leads directly to a Return Instruction.
87 static bool isReturningBlock(BasicBlock *BB) {
88 SmallPtrSet<BasicBlock *, 8> Visited;
89
90 while (true) {
91 TerminatorInst *TI = BB->getTerminator();
92 if (isa<ReturnInst>(TI))
93 return true;
94
95 if (TI->getNumSuccessors() > 1)
96 break;
97
98 // It is unreachable block which we can consider as a return instruction.
99 if (TI->getNumSuccessors() == 0)
100 return true;
101
102 Visited.insert(BB);
103 BB = TI->getSuccessor(0);
104
105 // Stop if cycle is detected.
106 if (Visited.count(BB))
107 return false;
108 }
109
110 return false;
111 }
112
Andrew Trickf289df22011-06-11 01:05:22 +0000113 uint32_t getMaxWeightFor(BasicBlock *BB) const {
114 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000115 }
116
117public:
Andrew Trickf289df22011-06-11 01:05:22 +0000118 BranchProbabilityAnalysis(DenseMap<Edge, uint32_t> *W,
Andrew Trick9e764222011-06-04 01:16:30 +0000119 BranchProbabilityInfo *BP, LoopInfo *LI)
120 : Weights(W), BP(BP), LI(LI) {
121 }
122
123 // Return Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000124 bool calcReturnHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000125
126 // Pointer Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000127 bool calcPointerHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000128
129 // Loop Branch Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000130 bool calcLoopBranchHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000131
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000132 // Zero Heurestics
133 bool calcZeroHeuristics(BasicBlock *BB);
134
Andrew Trick9e764222011-06-04 01:16:30 +0000135 bool runOnFunction(Function &F);
136};
Benjamin Kramerafa88ea2011-06-13 18:38:56 +0000137} // end anonymous namespace
Andrew Trick9e764222011-06-04 01:16:30 +0000138
139// Calculate Edge Weights using "Return Heuristics". Predict a successor which
140// leads directly to Return Instruction will not be taken.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000141bool BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
Andrew Trick9e764222011-06-04 01:16:30 +0000142 if (BB->getTerminator()->getNumSuccessors() == 1)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000143 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000144
Jakub Staszakb137f162011-08-01 19:16:26 +0000145 SmallPtrSet<BasicBlock *, 4> ReturningEdges;
146 SmallPtrSet<BasicBlock *, 4> StayEdges;
Jakub Staszake0058b42011-07-29 02:36:53 +0000147
Andrew Trick9e764222011-06-04 01:16:30 +0000148 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
149 BasicBlock *Succ = *I;
Jakub Staszake0058b42011-07-29 02:36:53 +0000150 if (isReturningBlock(Succ))
Jakub Staszakb137f162011-08-01 19:16:26 +0000151 ReturningEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000152 else
Jakub Staszakb137f162011-08-01 19:16:26 +0000153 StayEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000154 }
155
156 if (uint32_t numStayEdges = StayEdges.size()) {
157 uint32_t stayWeight = RH_TAKEN_WEIGHT / numStayEdges;
158 if (stayWeight < NORMAL_WEIGHT)
159 stayWeight = NORMAL_WEIGHT;
160
Jakub Staszakb137f162011-08-01 19:16:26 +0000161 for (SmallPtrSet<BasicBlock *, 4>::iterator I = StayEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000162 E = StayEdges.end(); I != E; ++I)
163 BP->setEdgeWeight(BB, *I, stayWeight);
164 }
165
166 if (uint32_t numRetEdges = ReturningEdges.size()) {
167 uint32_t retWeight = RH_NONTAKEN_WEIGHT / numRetEdges;
168 if (retWeight < MIN_WEIGHT)
169 retWeight = MIN_WEIGHT;
Jakub Staszakb137f162011-08-01 19:16:26 +0000170 for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReturningEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000171 E = ReturningEdges.end(); I != E; ++I) {
172 BP->setEdgeWeight(BB, *I, retWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000173 }
174 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000175
Jakub Staszake0058b42011-07-29 02:36:53 +0000176 return ReturningEdges.size() > 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000177}
178
179// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
180// between two pointer or pointer and NULL will fail.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000181bool BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
Andrew Trick9e764222011-06-04 01:16:30 +0000182 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
183 if (!BI || !BI->isConditional())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000184 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000185
186 Value *Cond = BI->getCondition();
187 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000188 if (!CI || !CI->isEquality())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000189 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000190
191 Value *LHS = CI->getOperand(0);
Andrew Trick9e764222011-06-04 01:16:30 +0000192
193 if (!LHS->getType()->isPointerTy())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000194 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000195
Nick Lewycky404b53e2011-06-04 02:07:10 +0000196 assert(CI->getOperand(1)->getType()->isPointerTy());
Andrew Trick9e764222011-06-04 01:16:30 +0000197
198 BasicBlock *Taken = BI->getSuccessor(0);
199 BasicBlock *NonTaken = BI->getSuccessor(1);
200
201 // p != 0 -> isProb = true
202 // p == 0 -> isProb = false
203 // p != q -> isProb = true
204 // p == q -> isProb = false;
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000205 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
Andrew Trick9e764222011-06-04 01:16:30 +0000206 if (!isProb)
207 std::swap(Taken, NonTaken);
208
Jakub Staszake0058b42011-07-29 02:36:53 +0000209 BP->setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
210 BP->setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
Jakub Staszak7241caf2011-07-28 21:45:07 +0000211 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000212}
213
214// Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
215// as taken, exiting edges as not-taken.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000216bool BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
Andrew Trickf289df22011-06-11 01:05:22 +0000217 uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000218
219 Loop *L = LI->getLoopFor(BB);
220 if (!L)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000221 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000222
Jakub Staszakb137f162011-08-01 19:16:26 +0000223 SmallPtrSet<BasicBlock *, 8> BackEdges;
224 SmallPtrSet<BasicBlock *, 8> ExitingEdges;
225 SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
Jakub Staszakfa447252011-07-28 21:33:46 +0000226
227 bool isHeader = BB == L->getHeader();
Andrew Trick9e764222011-06-04 01:16:30 +0000228
229 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
230 BasicBlock *Succ = *I;
231 Loop *SuccL = LI->getLoopFor(Succ);
232 if (SuccL != L)
Jakub Staszakb137f162011-08-01 19:16:26 +0000233 ExitingEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000234 else if (Succ == L->getHeader())
Jakub Staszakb137f162011-08-01 19:16:26 +0000235 BackEdges.insert(Succ);
Jakub Staszakfa447252011-07-28 21:33:46 +0000236 else if (isHeader)
Jakub Staszakb137f162011-08-01 19:16:26 +0000237 InEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000238 }
239
Andrew Trickf289df22011-06-11 01:05:22 +0000240 if (uint32_t numBackEdges = BackEdges.size()) {
241 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000242 if (backWeight < NORMAL_WEIGHT)
243 backWeight = NORMAL_WEIGHT;
244
Jakub Staszakb137f162011-08-01 19:16:26 +0000245 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000246 EE = BackEdges.end(); EI != EE; ++EI) {
247 BasicBlock *Back = *EI;
248 BP->setEdgeWeight(BB, Back, backWeight);
249 }
250 }
251
Jakub Staszakfa447252011-07-28 21:33:46 +0000252 if (uint32_t numInEdges = InEdges.size()) {
253 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
254 if (inWeight < NORMAL_WEIGHT)
255 inWeight = NORMAL_WEIGHT;
256
Jakub Staszakb137f162011-08-01 19:16:26 +0000257 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
Jakub Staszakfa447252011-07-28 21:33:46 +0000258 EE = InEdges.end(); EI != EE; ++EI) {
259 BasicBlock *Back = *EI;
260 BP->setEdgeWeight(BB, Back, inWeight);
261 }
262 }
263
Andrew Trickf289df22011-06-11 01:05:22 +0000264 uint32_t numExitingEdges = ExitingEdges.size();
265 if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
266 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000267 if (exitWeight < MIN_WEIGHT)
268 exitWeight = MIN_WEIGHT;
269
Jakub Staszakb137f162011-08-01 19:16:26 +0000270 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000271 EE = ExitingEdges.end(); EI != EE; ++EI) {
272 BasicBlock *Exiting = *EI;
273 BP->setEdgeWeight(BB, Exiting, exitWeight);
274 }
275 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000276
277 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000278}
279
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000280bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) {
281 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
282 if (!BI || !BI->isConditional())
283 return false;
284
285 Value *Cond = BI->getCondition();
286 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
287 if (!CI)
288 return false;
289
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000290 Value *RHS = CI->getOperand(1);
Jakub Staszaka385c202011-07-31 04:47:20 +0000291 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
292 if (!CV || !CV->isZero())
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000293 return false;
294
295 bool isProb;
296 switch (CI->getPredicate()) {
297 case CmpInst::ICMP_EQ:
298 // Equal to zero is not expected to be taken.
299 isProb = false;
300 break;
301
302 case CmpInst::ICMP_NE:
303 // Not equal to zero is expected.
304 isProb = true;
305 break;
306
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000307 case CmpInst::ICMP_SLT:
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000308 // Less or equal to zero is not expected.
Jakub Staszaka385c202011-07-31 04:47:20 +0000309 // X < 0 -> Unlikely
Jakub Staszaka385c202011-07-31 04:47:20 +0000310 isProb = false;
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000311 break;
312
313 case CmpInst::ICMP_UGT:
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000314 case CmpInst::ICMP_SGT:
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000315 // Greater or equal to zero is expected.
Jakub Staszaka385c202011-07-31 04:47:20 +0000316 // X > 0 -> Likely
Jakub Staszaka385c202011-07-31 04:47:20 +0000317 isProb = true;
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000318 break;
319
320 default:
321 return false;
322 };
323
324 BasicBlock *Taken = BI->getSuccessor(0);
325 BasicBlock *NonTaken = BI->getSuccessor(1);
326
327 if (!isProb)
328 std::swap(Taken, NonTaken);
329
330 BP->setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
331 BP->setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
332
333 return true;
334}
335
336
Andrew Trick9e764222011-06-04 01:16:30 +0000337bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
338
339 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
340 BasicBlock *BB = I++;
341
Jakub Staszak7241caf2011-07-28 21:45:07 +0000342 if (calcLoopBranchHeuristics(BB))
343 continue;
Andrew Trick9e764222011-06-04 01:16:30 +0000344
Jakub Staszak7241caf2011-07-28 21:45:07 +0000345 if (calcReturnHeuristics(BB))
346 continue;
347
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000348 if (calcPointerHeuristics(BB))
349 continue;
350
351 calcZeroHeuristics(BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000352 }
353
354 return false;
355}
356
Jakub Staszak12af93a2011-07-16 20:31:15 +0000357void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
358 AU.addRequired<LoopInfo>();
359 AU.setPreservesAll();
360}
Andrew Trick9e764222011-06-04 01:16:30 +0000361
362bool BranchProbabilityInfo::runOnFunction(Function &F) {
363 LoopInfo &LI = getAnalysis<LoopInfo>();
364 BranchProbabilityAnalysis BPA(&Weights, this, &LI);
Andrew Trickf289df22011-06-11 01:05:22 +0000365 return BPA.runOnFunction(F);
Andrew Trick9e764222011-06-04 01:16:30 +0000366}
367
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000368uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000369 uint32_t Sum = 0;
370
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000371 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
372 const BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000373 uint32_t Weight = getEdgeWeight(BB, Succ);
374 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000375
376 Sum += Weight;
377 assert(Sum > PrevSum); (void) PrevSum;
378 }
379
Andrew Trickf289df22011-06-11 01:05:22 +0000380 return Sum;
381}
382
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000383bool BranchProbabilityInfo::
384isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000385 // Hot probability is at least 4/5 = 80%
386 uint32_t Weight = getEdgeWeight(Src, Dst);
387 uint32_t Sum = getSumForBlock(Src);
388
389 // FIXME: Implement BranchProbability::compare then change this code to
390 // compare this BranchProbability against a static "hot" BranchProbability.
391 return (uint64_t)Weight * 5 > (uint64_t)Sum * 4;
Andrew Trick9e764222011-06-04 01:16:30 +0000392}
393
394BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000395 uint32_t Sum = 0;
396 uint32_t MaxWeight = 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000397 BasicBlock *MaxSucc = 0;
398
399 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
400 BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000401 uint32_t Weight = getEdgeWeight(BB, Succ);
402 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000403
404 Sum += Weight;
405 assert(Sum > PrevSum); (void) PrevSum;
406
407 if (Weight > MaxWeight) {
408 MaxWeight = Weight;
409 MaxSucc = Succ;
410 }
411 }
412
Andrew Trickf289df22011-06-11 01:05:22 +0000413 // FIXME: Use BranchProbability::compare.
414 if ((uint64_t)MaxWeight * 5 > (uint64_t)Sum * 4)
Andrew Trick9e764222011-06-04 01:16:30 +0000415 return MaxSucc;
416
417 return 0;
418}
419
420// Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000421uint32_t BranchProbabilityInfo::
422getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000423 Edge E(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000424 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
Andrew Trick9e764222011-06-04 01:16:30 +0000425
426 if (I != Weights.end())
427 return I->second;
428
429 return DEFAULT_WEIGHT;
430}
431
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000432void BranchProbabilityInfo::
433setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
Andrew Trick9e764222011-06-04 01:16:30 +0000434 Weights[std::make_pair(Src, Dst)] = Weight;
Andrew Trickf289df22011-06-11 01:05:22 +0000435 DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
436 << Dst->getNameStr() << " weight to " << Weight
437 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
438}
439
440
441BranchProbability BranchProbabilityInfo::
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000442getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000443
444 uint32_t N = getEdgeWeight(Src, Dst);
445 uint32_t D = getSumForBlock(Src);
446
447 return BranchProbability(N, D);
Andrew Trick9e764222011-06-04 01:16:30 +0000448}
449
450raw_ostream &
451BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
Andrew Trickf289df22011-06-11 01:05:22 +0000452 BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000453
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000454 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000455 OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
456 << " probability is " << Prob
457 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Andrew Trick9e764222011-06-04 01:16:30 +0000458
459 return OS;
460}