blob: 70de3d1c49d948533a837f5272216faf9fd57220 [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"
Chandler Carruth99d01c52011-10-19 10:30:30 +000016#include "llvm/LLVMContext.h"
17#include "llvm/Metadata.h"
Andrew Trick9e764222011-06-04 01:16:30 +000018#include "llvm/Analysis/BranchProbabilityInfo.h"
Jakub Staszak12af93a2011-07-16 20:31:15 +000019#include "llvm/Analysis/LoopInfo.h"
Andrew Trickf289df22011-06-11 01:05:22 +000020#include "llvm/Support/Debug.h"
Andrew Trick9e764222011-06-04 01:16:30 +000021
22using namespace llvm;
23
24INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
25 "Branch Probability Analysis", false, true)
26INITIALIZE_PASS_DEPENDENCY(LoopInfo)
27INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
28 "Branch Probability Analysis", false, true)
29
30char BranchProbabilityInfo::ID = 0;
31
Benjamin Kramerafa88ea2011-06-13 18:38:56 +000032namespace {
Andrew Trick9e764222011-06-04 01:16:30 +000033// Please note that BranchProbabilityAnalysis is not a FunctionPass.
34// It is created by BranchProbabilityInfo (which is a FunctionPass), which
35// provides a clear interface. Thanks to that, all heuristics and other
36// private methods are hidden in the .cpp file.
37class BranchProbabilityAnalysis {
38
Jakub Staszak6f6baf12011-07-29 19:30:00 +000039 typedef std::pair<const BasicBlock *, const BasicBlock *> Edge;
Andrew Trick9e764222011-06-04 01:16:30 +000040
Andrew Trick9e764222011-06-04 01:16:30 +000041 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:
Chandler Carruth7a34c8b2011-10-16 22:27:54 +0000118 BranchProbabilityAnalysis(BranchProbabilityInfo *BP, LoopInfo *LI)
119 : BP(BP), LI(LI) {
Andrew Trick9e764222011-06-04 01:16:30 +0000120 }
121
Chandler Carruth99d01c52011-10-19 10:30:30 +0000122 // Metadata Weights
123 bool calcMetadataWeights(BasicBlock *BB);
124
Andrew Trick9e764222011-06-04 01:16:30 +0000125 // Return Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000126 bool calcReturnHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000127
128 // Pointer Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000129 bool calcPointerHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000130
131 // Loop Branch Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000132 bool calcLoopBranchHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000133
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000134 // Zero Heurestics
135 bool calcZeroHeuristics(BasicBlock *BB);
136
Andrew Trick9e764222011-06-04 01:16:30 +0000137 bool runOnFunction(Function &F);
138};
Benjamin Kramerafa88ea2011-06-13 18:38:56 +0000139} // end anonymous namespace
Andrew Trick9e764222011-06-04 01:16:30 +0000140
Chandler Carruth99d01c52011-10-19 10:30:30 +0000141// Propagate existing explicit probabilities from either profile data or
142// 'expect' intrinsic processing.
143// FIXME: This doesn't correctly extract probabilities for switches.
144bool BranchProbabilityAnalysis::calcMetadataWeights(BasicBlock *BB) {
145 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
146 if (!BI || !BI->isConditional())
147 return false;
148
149 MDNode *WeightsNode = BI->getMetadata(LLVMContext::MD_prof);
150 if (!WeightsNode || WeightsNode->getNumOperands() < 3)
151 return false;
152
153 // Pull the weights out of the metadata. Note that the zero operand is the
154 // name.
155 ConstantInt *Weights[] = {
156 dyn_cast<ConstantInt>(WeightsNode->getOperand(1)),
157 dyn_cast<ConstantInt>(WeightsNode->getOperand(2))
158 };
159 if (!Weights[0] || !Weights[1])
160 return false;
161
162 uint32_t WeightLimit = getMaxWeightFor(BB);
163 BP->setEdgeWeight(BB, BI->getSuccessor(0),
164 Weights[0]->getLimitedValue(WeightLimit));
165 BP->setEdgeWeight(BB, BI->getSuccessor(1),
166 Weights[1]->getLimitedValue(WeightLimit));
167
168 return true;
169}
170
Andrew Trick9e764222011-06-04 01:16:30 +0000171// Calculate Edge Weights using "Return Heuristics". Predict a successor which
172// leads directly to Return Instruction will not be taken.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000173bool BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
Andrew Trick9e764222011-06-04 01:16:30 +0000174 if (BB->getTerminator()->getNumSuccessors() == 1)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000175 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000176
Jakub Staszakb137f162011-08-01 19:16:26 +0000177 SmallPtrSet<BasicBlock *, 4> ReturningEdges;
178 SmallPtrSet<BasicBlock *, 4> StayEdges;
Jakub Staszake0058b42011-07-29 02:36:53 +0000179
Andrew Trick9e764222011-06-04 01:16:30 +0000180 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
181 BasicBlock *Succ = *I;
Jakub Staszake0058b42011-07-29 02:36:53 +0000182 if (isReturningBlock(Succ))
Jakub Staszakb137f162011-08-01 19:16:26 +0000183 ReturningEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000184 else
Jakub Staszakb137f162011-08-01 19:16:26 +0000185 StayEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000186 }
187
188 if (uint32_t numStayEdges = StayEdges.size()) {
189 uint32_t stayWeight = RH_TAKEN_WEIGHT / numStayEdges;
190 if (stayWeight < NORMAL_WEIGHT)
191 stayWeight = NORMAL_WEIGHT;
192
Jakub Staszakb137f162011-08-01 19:16:26 +0000193 for (SmallPtrSet<BasicBlock *, 4>::iterator I = StayEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000194 E = StayEdges.end(); I != E; ++I)
195 BP->setEdgeWeight(BB, *I, stayWeight);
196 }
197
198 if (uint32_t numRetEdges = ReturningEdges.size()) {
199 uint32_t retWeight = RH_NONTAKEN_WEIGHT / numRetEdges;
200 if (retWeight < MIN_WEIGHT)
201 retWeight = MIN_WEIGHT;
Jakub Staszakb137f162011-08-01 19:16:26 +0000202 for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReturningEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000203 E = ReturningEdges.end(); I != E; ++I) {
204 BP->setEdgeWeight(BB, *I, retWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000205 }
206 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000207
Jakub Staszake0058b42011-07-29 02:36:53 +0000208 return ReturningEdges.size() > 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000209}
210
211// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
212// between two pointer or pointer and NULL will fail.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000213bool BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
Andrew Trick9e764222011-06-04 01:16:30 +0000214 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
215 if (!BI || !BI->isConditional())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000216 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000217
218 Value *Cond = BI->getCondition();
219 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000220 if (!CI || !CI->isEquality())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000221 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000222
223 Value *LHS = CI->getOperand(0);
Andrew Trick9e764222011-06-04 01:16:30 +0000224
225 if (!LHS->getType()->isPointerTy())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000226 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000227
Nick Lewycky404b53e2011-06-04 02:07:10 +0000228 assert(CI->getOperand(1)->getType()->isPointerTy());
Andrew Trick9e764222011-06-04 01:16:30 +0000229
230 BasicBlock *Taken = BI->getSuccessor(0);
231 BasicBlock *NonTaken = BI->getSuccessor(1);
232
233 // p != 0 -> isProb = true
234 // p == 0 -> isProb = false
235 // p != q -> isProb = true
236 // p == q -> isProb = false;
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000237 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
Andrew Trick9e764222011-06-04 01:16:30 +0000238 if (!isProb)
239 std::swap(Taken, NonTaken);
240
Jakub Staszake0058b42011-07-29 02:36:53 +0000241 BP->setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
242 BP->setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
Jakub Staszak7241caf2011-07-28 21:45:07 +0000243 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000244}
245
246// Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
247// as taken, exiting edges as not-taken.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000248bool BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
Andrew Trickf289df22011-06-11 01:05:22 +0000249 uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000250
251 Loop *L = LI->getLoopFor(BB);
252 if (!L)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000253 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000254
Jakub Staszakb137f162011-08-01 19:16:26 +0000255 SmallPtrSet<BasicBlock *, 8> BackEdges;
256 SmallPtrSet<BasicBlock *, 8> ExitingEdges;
257 SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
Jakub Staszakfa447252011-07-28 21:33:46 +0000258
259 bool isHeader = BB == L->getHeader();
Andrew Trick9e764222011-06-04 01:16:30 +0000260
261 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
262 BasicBlock *Succ = *I;
263 Loop *SuccL = LI->getLoopFor(Succ);
264 if (SuccL != L)
Jakub Staszakb137f162011-08-01 19:16:26 +0000265 ExitingEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000266 else if (Succ == L->getHeader())
Jakub Staszakb137f162011-08-01 19:16:26 +0000267 BackEdges.insert(Succ);
Jakub Staszakfa447252011-07-28 21:33:46 +0000268 else if (isHeader)
Jakub Staszakb137f162011-08-01 19:16:26 +0000269 InEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000270 }
271
Andrew Trickf289df22011-06-11 01:05:22 +0000272 if (uint32_t numBackEdges = BackEdges.size()) {
273 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000274 if (backWeight < NORMAL_WEIGHT)
275 backWeight = NORMAL_WEIGHT;
276
Jakub Staszakb137f162011-08-01 19:16:26 +0000277 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000278 EE = BackEdges.end(); EI != EE; ++EI) {
279 BasicBlock *Back = *EI;
280 BP->setEdgeWeight(BB, Back, backWeight);
281 }
282 }
283
Jakub Staszakfa447252011-07-28 21:33:46 +0000284 if (uint32_t numInEdges = InEdges.size()) {
285 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
286 if (inWeight < NORMAL_WEIGHT)
287 inWeight = NORMAL_WEIGHT;
288
Jakub Staszakb137f162011-08-01 19:16:26 +0000289 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
Jakub Staszakfa447252011-07-28 21:33:46 +0000290 EE = InEdges.end(); EI != EE; ++EI) {
291 BasicBlock *Back = *EI;
292 BP->setEdgeWeight(BB, Back, inWeight);
293 }
294 }
295
Andrew Trickf289df22011-06-11 01:05:22 +0000296 uint32_t numExitingEdges = ExitingEdges.size();
297 if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
298 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000299 if (exitWeight < MIN_WEIGHT)
300 exitWeight = MIN_WEIGHT;
301
Jakub Staszakb137f162011-08-01 19:16:26 +0000302 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000303 EE = ExitingEdges.end(); EI != EE; ++EI) {
304 BasicBlock *Exiting = *EI;
305 BP->setEdgeWeight(BB, Exiting, exitWeight);
306 }
307 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000308
309 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000310}
311
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000312bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) {
313 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
314 if (!BI || !BI->isConditional())
315 return false;
316
317 Value *Cond = BI->getCondition();
318 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
319 if (!CI)
320 return false;
321
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000322 Value *RHS = CI->getOperand(1);
Jakub Staszaka385c202011-07-31 04:47:20 +0000323 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000324 if (!CV)
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000325 return false;
326
327 bool isProb;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000328 if (CV->isZero()) {
329 switch (CI->getPredicate()) {
330 case CmpInst::ICMP_EQ:
331 // X == 0 -> Unlikely
332 isProb = false;
333 break;
334 case CmpInst::ICMP_NE:
335 // X != 0 -> Likely
336 isProb = true;
337 break;
338 case CmpInst::ICMP_SLT:
339 // X < 0 -> Unlikely
340 isProb = false;
341 break;
342 case CmpInst::ICMP_SGT:
343 // X > 0 -> Likely
344 isProb = true;
345 break;
346 default:
347 return false;
348 }
349 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
350 // InstCombine canonicalizes X <= 0 into X < 1.
351 // X <= 0 -> Unlikely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000352 isProb = false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000353 } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
354 // InstCombine canonicalizes X >= 0 into X > -1.
355 // X >= 0 -> Likely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000356 isProb = true;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000357 } else {
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000358 return false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000359 }
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000360
361 BasicBlock *Taken = BI->getSuccessor(0);
362 BasicBlock *NonTaken = BI->getSuccessor(1);
363
364 if (!isProb)
365 std::swap(Taken, NonTaken);
366
367 BP->setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
368 BP->setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
369
370 return true;
371}
372
373
Andrew Trick9e764222011-06-04 01:16:30 +0000374bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
375
376 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
377 BasicBlock *BB = I++;
378
Chandler Carruth99d01c52011-10-19 10:30:30 +0000379 if (calcMetadataWeights(BB))
380 continue;
381
Jakub Staszak7241caf2011-07-28 21:45:07 +0000382 if (calcLoopBranchHeuristics(BB))
383 continue;
Andrew Trick9e764222011-06-04 01:16:30 +0000384
Jakub Staszak7241caf2011-07-28 21:45:07 +0000385 if (calcReturnHeuristics(BB))
386 continue;
387
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000388 if (calcPointerHeuristics(BB))
389 continue;
390
391 calcZeroHeuristics(BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000392 }
393
394 return false;
395}
396
Jakub Staszak12af93a2011-07-16 20:31:15 +0000397void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
398 AU.addRequired<LoopInfo>();
399 AU.setPreservesAll();
400}
Andrew Trick9e764222011-06-04 01:16:30 +0000401
402bool BranchProbabilityInfo::runOnFunction(Function &F) {
403 LoopInfo &LI = getAnalysis<LoopInfo>();
Chandler Carruth7a34c8b2011-10-16 22:27:54 +0000404 BranchProbabilityAnalysis BPA(this, &LI);
Andrew Trickf289df22011-06-11 01:05:22 +0000405 return BPA.runOnFunction(F);
Andrew Trick9e764222011-06-04 01:16:30 +0000406}
407
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000408uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000409 uint32_t Sum = 0;
410
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000411 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
412 const BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000413 uint32_t Weight = getEdgeWeight(BB, Succ);
414 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000415
416 Sum += Weight;
417 assert(Sum > PrevSum); (void) PrevSum;
418 }
419
Andrew Trickf289df22011-06-11 01:05:22 +0000420 return Sum;
421}
422
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000423bool BranchProbabilityInfo::
424isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000425 // Hot probability is at least 4/5 = 80%
426 uint32_t Weight = getEdgeWeight(Src, Dst);
427 uint32_t Sum = getSumForBlock(Src);
428
429 // FIXME: Implement BranchProbability::compare then change this code to
430 // compare this BranchProbability against a static "hot" BranchProbability.
431 return (uint64_t)Weight * 5 > (uint64_t)Sum * 4;
Andrew Trick9e764222011-06-04 01:16:30 +0000432}
433
434BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000435 uint32_t Sum = 0;
436 uint32_t MaxWeight = 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000437 BasicBlock *MaxSucc = 0;
438
439 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
440 BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000441 uint32_t Weight = getEdgeWeight(BB, Succ);
442 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000443
444 Sum += Weight;
445 assert(Sum > PrevSum); (void) PrevSum;
446
447 if (Weight > MaxWeight) {
448 MaxWeight = Weight;
449 MaxSucc = Succ;
450 }
451 }
452
Andrew Trickf289df22011-06-11 01:05:22 +0000453 // FIXME: Use BranchProbability::compare.
454 if ((uint64_t)MaxWeight * 5 > (uint64_t)Sum * 4)
Andrew Trick9e764222011-06-04 01:16:30 +0000455 return MaxSucc;
456
457 return 0;
458}
459
460// Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000461uint32_t BranchProbabilityInfo::
462getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000463 Edge E(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000464 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
Andrew Trick9e764222011-06-04 01:16:30 +0000465
466 if (I != Weights.end())
467 return I->second;
468
469 return DEFAULT_WEIGHT;
470}
471
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000472void BranchProbabilityInfo::
473setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
Andrew Trick9e764222011-06-04 01:16:30 +0000474 Weights[std::make_pair(Src, Dst)] = Weight;
Andrew Trickf289df22011-06-11 01:05:22 +0000475 DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
476 << Dst->getNameStr() << " weight to " << Weight
477 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
478}
479
480
481BranchProbability BranchProbabilityInfo::
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000482getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000483
484 uint32_t N = getEdgeWeight(Src, Dst);
485 uint32_t D = getSumForBlock(Src);
486
487 return BranchProbability(N, D);
Andrew Trick9e764222011-06-04 01:16:30 +0000488}
489
490raw_ostream &
491BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
Andrew Trickf289df22011-06-11 01:05:22 +0000492 BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000493
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000494 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000495 OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
496 << " probability is " << Prob
497 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Andrew Trick9e764222011-06-04 01:16:30 +0000498
499 return OS;
500}