blob: 6e8d7e0115d402bb3eefe5bd4216f8dacb84d466 [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
Benjamin Kramerc888aa42011-10-21 20:12:47 +000079 static const uint32_t FPH_TAKEN_WEIGHT = 20;
80 static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
81
Andrew Trick9e764222011-06-04 01:16:30 +000082 // Standard weight value. Used when none of the heuristics set weight for
83 // the edge.
Andrew Trickf289df22011-06-11 01:05:22 +000084 static const uint32_t NORMAL_WEIGHT = 16;
Andrew Trick9e764222011-06-04 01:16:30 +000085
86 // Minimum weight of an edge. Please note, that weight is NEVER 0.
Andrew Trickf289df22011-06-11 01:05:22 +000087 static const uint32_t MIN_WEIGHT = 1;
Andrew Trick9e764222011-06-04 01:16:30 +000088
89 // Return TRUE if BB leads directly to a Return Instruction.
90 static bool isReturningBlock(BasicBlock *BB) {
91 SmallPtrSet<BasicBlock *, 8> Visited;
92
93 while (true) {
94 TerminatorInst *TI = BB->getTerminator();
95 if (isa<ReturnInst>(TI))
96 return true;
97
98 if (TI->getNumSuccessors() > 1)
99 break;
100
101 // It is unreachable block which we can consider as a return instruction.
102 if (TI->getNumSuccessors() == 0)
103 return true;
104
105 Visited.insert(BB);
106 BB = TI->getSuccessor(0);
107
108 // Stop if cycle is detected.
109 if (Visited.count(BB))
110 return false;
111 }
112
113 return false;
114 }
115
Andrew Trickf289df22011-06-11 01:05:22 +0000116 uint32_t getMaxWeightFor(BasicBlock *BB) const {
117 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000118 }
119
120public:
Chandler Carruth7a34c8b2011-10-16 22:27:54 +0000121 BranchProbabilityAnalysis(BranchProbabilityInfo *BP, LoopInfo *LI)
122 : BP(BP), LI(LI) {
Andrew Trick9e764222011-06-04 01:16:30 +0000123 }
124
Chandler Carruth99d01c52011-10-19 10:30:30 +0000125 // Metadata Weights
126 bool calcMetadataWeights(BasicBlock *BB);
127
Andrew Trick9e764222011-06-04 01:16:30 +0000128 // Return Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000129 bool calcReturnHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000130
131 // Pointer Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000132 bool calcPointerHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000133
134 // Loop Branch Heuristics
Jakub Staszak7241caf2011-07-28 21:45:07 +0000135 bool calcLoopBranchHeuristics(BasicBlock *BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000136
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000137 // Zero Heuristics
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000138 bool calcZeroHeuristics(BasicBlock *BB);
139
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000140 // Floating Point Heuristics
141 bool calcFloatingPointHeuristics(BasicBlock *BB);
142
Andrew Trick9e764222011-06-04 01:16:30 +0000143 bool runOnFunction(Function &F);
144};
Benjamin Kramerafa88ea2011-06-13 18:38:56 +0000145} // end anonymous namespace
Andrew Trick9e764222011-06-04 01:16:30 +0000146
Chandler Carruth99d01c52011-10-19 10:30:30 +0000147// Propagate existing explicit probabilities from either profile data or
148// 'expect' intrinsic processing.
Chandler Carruth99d01c52011-10-19 10:30:30 +0000149bool BranchProbabilityAnalysis::calcMetadataWeights(BasicBlock *BB) {
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000150 TerminatorInst *TI = BB->getTerminator();
151 if (TI->getNumSuccessors() == 1)
152 return false;
153 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
Chandler Carruth99d01c52011-10-19 10:30:30 +0000154 return false;
155
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000156 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
157 if (!WeightsNode)
Chandler Carruth99d01c52011-10-19 10:30:30 +0000158 return false;
159
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000160 // Ensure there are weights for all of the successors. Note that the first
161 // operand to the metadata node is a name, not a weight.
162 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
Chandler Carruth99d01c52011-10-19 10:30:30 +0000163 return false;
164
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000165 // Build up the final weights that will be used in a temporary buffer, but
166 // don't add them until all weihts are present. Each weight value is clamped
167 // to [1, getMaxWeightFor(BB)].
Chandler Carruth99d01c52011-10-19 10:30:30 +0000168 uint32_t WeightLimit = getMaxWeightFor(BB);
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000169 SmallVector<uint32_t, 2> Weights;
170 Weights.reserve(TI->getNumSuccessors());
171 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
172 ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
173 if (!Weight)
174 return false;
175 Weights.push_back(
176 std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
177 }
178 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
179 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
180 BP->setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
Chandler Carruth99d01c52011-10-19 10:30:30 +0000181
182 return true;
183}
184
Andrew Trick9e764222011-06-04 01:16:30 +0000185// Calculate Edge Weights using "Return Heuristics". Predict a successor which
186// leads directly to Return Instruction will not be taken.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000187bool BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
Andrew Trick9e764222011-06-04 01:16:30 +0000188 if (BB->getTerminator()->getNumSuccessors() == 1)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000189 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000190
Jakub Staszakb137f162011-08-01 19:16:26 +0000191 SmallPtrSet<BasicBlock *, 4> ReturningEdges;
192 SmallPtrSet<BasicBlock *, 4> StayEdges;
Jakub Staszake0058b42011-07-29 02:36:53 +0000193
Andrew Trick9e764222011-06-04 01:16:30 +0000194 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
195 BasicBlock *Succ = *I;
Jakub Staszake0058b42011-07-29 02:36:53 +0000196 if (isReturningBlock(Succ))
Jakub Staszakb137f162011-08-01 19:16:26 +0000197 ReturningEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000198 else
Jakub Staszakb137f162011-08-01 19:16:26 +0000199 StayEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000200 }
201
202 if (uint32_t numStayEdges = StayEdges.size()) {
203 uint32_t stayWeight = RH_TAKEN_WEIGHT / numStayEdges;
204 if (stayWeight < NORMAL_WEIGHT)
205 stayWeight = NORMAL_WEIGHT;
206
Jakub Staszakb137f162011-08-01 19:16:26 +0000207 for (SmallPtrSet<BasicBlock *, 4>::iterator I = StayEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000208 E = StayEdges.end(); I != E; ++I)
209 BP->setEdgeWeight(BB, *I, stayWeight);
210 }
211
212 if (uint32_t numRetEdges = ReturningEdges.size()) {
213 uint32_t retWeight = RH_NONTAKEN_WEIGHT / numRetEdges;
214 if (retWeight < MIN_WEIGHT)
215 retWeight = MIN_WEIGHT;
Jakub Staszakb137f162011-08-01 19:16:26 +0000216 for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReturningEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000217 E = ReturningEdges.end(); I != E; ++I) {
218 BP->setEdgeWeight(BB, *I, retWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000219 }
220 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000221
Jakub Staszake0058b42011-07-29 02:36:53 +0000222 return ReturningEdges.size() > 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000223}
224
225// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
226// between two pointer or pointer and NULL will fail.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000227bool BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
Andrew Trick9e764222011-06-04 01:16:30 +0000228 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
229 if (!BI || !BI->isConditional())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000230 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000231
232 Value *Cond = BI->getCondition();
233 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000234 if (!CI || !CI->isEquality())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000235 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000236
237 Value *LHS = CI->getOperand(0);
Andrew Trick9e764222011-06-04 01:16:30 +0000238
239 if (!LHS->getType()->isPointerTy())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000240 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000241
Nick Lewycky404b53e2011-06-04 02:07:10 +0000242 assert(CI->getOperand(1)->getType()->isPointerTy());
Andrew Trick9e764222011-06-04 01:16:30 +0000243
244 BasicBlock *Taken = BI->getSuccessor(0);
245 BasicBlock *NonTaken = BI->getSuccessor(1);
246
247 // p != 0 -> isProb = true
248 // p == 0 -> isProb = false
249 // p != q -> isProb = true
250 // p == q -> isProb = false;
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000251 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
Andrew Trick9e764222011-06-04 01:16:30 +0000252 if (!isProb)
253 std::swap(Taken, NonTaken);
254
Jakub Staszake0058b42011-07-29 02:36:53 +0000255 BP->setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
256 BP->setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
Jakub Staszak7241caf2011-07-28 21:45:07 +0000257 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000258}
259
260// Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
261// as taken, exiting edges as not-taken.
Jakub Staszak7241caf2011-07-28 21:45:07 +0000262bool BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
Andrew Trickf289df22011-06-11 01:05:22 +0000263 uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000264
265 Loop *L = LI->getLoopFor(BB);
266 if (!L)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000267 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000268
Jakub Staszakb137f162011-08-01 19:16:26 +0000269 SmallPtrSet<BasicBlock *, 8> BackEdges;
270 SmallPtrSet<BasicBlock *, 8> ExitingEdges;
271 SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
Jakub Staszakfa447252011-07-28 21:33:46 +0000272
273 bool isHeader = BB == L->getHeader();
Andrew Trick9e764222011-06-04 01:16:30 +0000274
275 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
276 BasicBlock *Succ = *I;
277 Loop *SuccL = LI->getLoopFor(Succ);
278 if (SuccL != L)
Jakub Staszakb137f162011-08-01 19:16:26 +0000279 ExitingEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000280 else if (Succ == L->getHeader())
Jakub Staszakb137f162011-08-01 19:16:26 +0000281 BackEdges.insert(Succ);
Jakub Staszakfa447252011-07-28 21:33:46 +0000282 else if (isHeader)
Jakub Staszakb137f162011-08-01 19:16:26 +0000283 InEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000284 }
285
Andrew Trickf289df22011-06-11 01:05:22 +0000286 if (uint32_t numBackEdges = BackEdges.size()) {
287 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000288 if (backWeight < NORMAL_WEIGHT)
289 backWeight = NORMAL_WEIGHT;
290
Jakub Staszakb137f162011-08-01 19:16:26 +0000291 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000292 EE = BackEdges.end(); EI != EE; ++EI) {
293 BasicBlock *Back = *EI;
294 BP->setEdgeWeight(BB, Back, backWeight);
295 }
296 }
297
Jakub Staszakfa447252011-07-28 21:33:46 +0000298 if (uint32_t numInEdges = InEdges.size()) {
299 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
300 if (inWeight < NORMAL_WEIGHT)
301 inWeight = NORMAL_WEIGHT;
302
Jakub Staszakb137f162011-08-01 19:16:26 +0000303 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
Jakub Staszakfa447252011-07-28 21:33:46 +0000304 EE = InEdges.end(); EI != EE; ++EI) {
305 BasicBlock *Back = *EI;
306 BP->setEdgeWeight(BB, Back, inWeight);
307 }
308 }
309
Andrew Trickf289df22011-06-11 01:05:22 +0000310 uint32_t numExitingEdges = ExitingEdges.size();
311 if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
312 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000313 if (exitWeight < MIN_WEIGHT)
314 exitWeight = MIN_WEIGHT;
315
Jakub Staszakb137f162011-08-01 19:16:26 +0000316 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000317 EE = ExitingEdges.end(); EI != EE; ++EI) {
318 BasicBlock *Exiting = *EI;
319 BP->setEdgeWeight(BB, Exiting, exitWeight);
320 }
321 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000322
323 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000324}
325
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000326bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) {
327 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
328 if (!BI || !BI->isConditional())
329 return false;
330
331 Value *Cond = BI->getCondition();
332 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
333 if (!CI)
334 return false;
335
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000336 Value *RHS = CI->getOperand(1);
Jakub Staszaka385c202011-07-31 04:47:20 +0000337 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000338 if (!CV)
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000339 return false;
340
341 bool isProb;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000342 if (CV->isZero()) {
343 switch (CI->getPredicate()) {
344 case CmpInst::ICMP_EQ:
345 // X == 0 -> Unlikely
346 isProb = false;
347 break;
348 case CmpInst::ICMP_NE:
349 // X != 0 -> Likely
350 isProb = true;
351 break;
352 case CmpInst::ICMP_SLT:
353 // X < 0 -> Unlikely
354 isProb = false;
355 break;
356 case CmpInst::ICMP_SGT:
357 // X > 0 -> Likely
358 isProb = true;
359 break;
360 default:
361 return false;
362 }
363 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
364 // InstCombine canonicalizes X <= 0 into X < 1.
365 // X <= 0 -> Unlikely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000366 isProb = false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000367 } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
368 // InstCombine canonicalizes X >= 0 into X > -1.
369 // X >= 0 -> Likely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000370 isProb = true;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000371 } else {
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000372 return false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000373 }
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000374
375 BasicBlock *Taken = BI->getSuccessor(0);
376 BasicBlock *NonTaken = BI->getSuccessor(1);
377
378 if (!isProb)
379 std::swap(Taken, NonTaken);
380
381 BP->setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
382 BP->setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
383
384 return true;
385}
386
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000387bool BranchProbabilityAnalysis::calcFloatingPointHeuristics(BasicBlock *BB) {
388 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
389 if (!BI || !BI->isConditional())
390 return false;
391
392 Value *Cond = BI->getCondition();
393 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
394 if (!FCmp || !FCmp->isEquality())
395 return false;
396
397 BasicBlock *Taken = BI->getSuccessor(0);
398 BasicBlock *NonTaken = BI->getSuccessor(1);
399
400 // f1 == f2 -> Unlikely
401 // f1 != f2 -> Likely
402 if (FCmp->isTrueWhenEqual())
403 std::swap(Taken, NonTaken);
404
405 BP->setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
406 BP->setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
407
408 return true;
409}
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000410
Andrew Trick9e764222011-06-04 01:16:30 +0000411bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
412
413 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
414 BasicBlock *BB = I++;
415
Chandler Carruth99d01c52011-10-19 10:30:30 +0000416 if (calcMetadataWeights(BB))
417 continue;
418
Jakub Staszak7241caf2011-07-28 21:45:07 +0000419 if (calcLoopBranchHeuristics(BB))
420 continue;
Andrew Trick9e764222011-06-04 01:16:30 +0000421
Jakub Staszak7241caf2011-07-28 21:45:07 +0000422 if (calcReturnHeuristics(BB))
423 continue;
424
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000425 if (calcPointerHeuristics(BB))
426 continue;
427
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000428 if (calcZeroHeuristics(BB))
429 continue;
430
431 calcFloatingPointHeuristics(BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000432 }
433
434 return false;
435}
436
Jakub Staszak12af93a2011-07-16 20:31:15 +0000437void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
438 AU.addRequired<LoopInfo>();
439 AU.setPreservesAll();
440}
Andrew Trick9e764222011-06-04 01:16:30 +0000441
442bool BranchProbabilityInfo::runOnFunction(Function &F) {
443 LoopInfo &LI = getAnalysis<LoopInfo>();
Chandler Carruth7a34c8b2011-10-16 22:27:54 +0000444 BranchProbabilityAnalysis BPA(this, &LI);
Andrew Trickf289df22011-06-11 01:05:22 +0000445 return BPA.runOnFunction(F);
Andrew Trick9e764222011-06-04 01:16:30 +0000446}
447
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000448uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000449 uint32_t Sum = 0;
450
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000451 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
452 const BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000453 uint32_t Weight = getEdgeWeight(BB, Succ);
454 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000455
456 Sum += Weight;
457 assert(Sum > PrevSum); (void) PrevSum;
458 }
459
Andrew Trickf289df22011-06-11 01:05:22 +0000460 return Sum;
461}
462
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000463bool BranchProbabilityInfo::
464isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000465 // Hot probability is at least 4/5 = 80%
466 uint32_t Weight = getEdgeWeight(Src, Dst);
467 uint32_t Sum = getSumForBlock(Src);
468
469 // FIXME: Implement BranchProbability::compare then change this code to
470 // compare this BranchProbability against a static "hot" BranchProbability.
471 return (uint64_t)Weight * 5 > (uint64_t)Sum * 4;
Andrew Trick9e764222011-06-04 01:16:30 +0000472}
473
474BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000475 uint32_t Sum = 0;
476 uint32_t MaxWeight = 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000477 BasicBlock *MaxSucc = 0;
478
479 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
480 BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000481 uint32_t Weight = getEdgeWeight(BB, Succ);
482 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000483
484 Sum += Weight;
485 assert(Sum > PrevSum); (void) PrevSum;
486
487 if (Weight > MaxWeight) {
488 MaxWeight = Weight;
489 MaxSucc = Succ;
490 }
491 }
492
Andrew Trickf289df22011-06-11 01:05:22 +0000493 // FIXME: Use BranchProbability::compare.
494 if ((uint64_t)MaxWeight * 5 > (uint64_t)Sum * 4)
Andrew Trick9e764222011-06-04 01:16:30 +0000495 return MaxSucc;
496
497 return 0;
498}
499
500// Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000501uint32_t BranchProbabilityInfo::
502getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000503 Edge E(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000504 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
Andrew Trick9e764222011-06-04 01:16:30 +0000505
506 if (I != Weights.end())
507 return I->second;
508
509 return DEFAULT_WEIGHT;
510}
511
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000512void BranchProbabilityInfo::
513setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
Andrew Trick9e764222011-06-04 01:16:30 +0000514 Weights[std::make_pair(Src, Dst)] = Weight;
Andrew Trickf289df22011-06-11 01:05:22 +0000515 DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
516 << Dst->getNameStr() << " weight to " << Weight
517 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
518}
519
520
521BranchProbability BranchProbabilityInfo::
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000522getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000523
524 uint32_t N = getEdgeWeight(Src, Dst);
525 uint32_t D = getSumForBlock(Src);
526
527 return BranchProbability(N, D);
Andrew Trick9e764222011-06-04 01:16:30 +0000528}
529
530raw_ostream &
531BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
Andrew Trickf289df22011-06-11 01:05:22 +0000532 BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000533
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000534 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000535 OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
536 << " probability is " << Prob
537 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Andrew Trick9e764222011-06-04 01:16:30 +0000538
539 return OS;
540}