blob: a03d9d85b834a6eb7ffd671f197d24ad0c41bd18 [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"
Chandler Carruth14edd312011-10-23 21:21:50 +000015#include "llvm/Function.h"
Andrew Trick9e764222011-06-04 01:16:30 +000016#include "llvm/Instructions.h"
Chandler Carruth99d01c52011-10-19 10:30:30 +000017#include "llvm/LLVMContext.h"
18#include "llvm/Metadata.h"
Andrew Trick9e764222011-06-04 01:16:30 +000019#include "llvm/Analysis/BranchProbabilityInfo.h"
Jakub Staszak12af93a2011-07-16 20:31:15 +000020#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth14edd312011-10-23 21:21:50 +000021#include "llvm/Support/CFG.h"
Andrew Trickf289df22011-06-11 01:05:22 +000022#include "llvm/Support/Debug.h"
Andrew Trick9e764222011-06-04 01:16:30 +000023
24using namespace llvm;
25
26INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
27 "Branch Probability Analysis", false, true)
28INITIALIZE_PASS_DEPENDENCY(LoopInfo)
29INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
30 "Branch Probability Analysis", false, true)
31
32char BranchProbabilityInfo::ID = 0;
33
Chandler Carruthb068bbba2011-10-24 01:40:45 +000034// Weights are for internal use only. They are used by heuristics to help to
35// estimate edges' probability. Example:
36//
37// Using "Loop Branch Heuristics" we predict weights of edges for the
38// block BB2.
39// ...
40// |
41// V
42// BB1<-+
43// | |
44// | | (Weight = 124)
45// V |
46// BB2--+
47// |
48// | (Weight = 4)
49// V
50// BB3
51//
52// Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
53// Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
54static const uint32_t LBH_TAKEN_WEIGHT = 124;
55static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
Andrew Trick9e764222011-06-04 01:16:30 +000056
Chandler Carruthb068bbba2011-10-24 01:40:45 +000057static const uint32_t RH_TAKEN_WEIGHT = 24;
58static const uint32_t RH_NONTAKEN_WEIGHT = 8;
Andrew Trick9e764222011-06-04 01:16:30 +000059
Chandler Carruthb068bbba2011-10-24 01:40:45 +000060static const uint32_t PH_TAKEN_WEIGHT = 20;
61static const uint32_t PH_NONTAKEN_WEIGHT = 12;
Andrew Trick9e764222011-06-04 01:16:30 +000062
Chandler Carruthb068bbba2011-10-24 01:40:45 +000063static const uint32_t ZH_TAKEN_WEIGHT = 20;
64static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
Andrew Trick9e764222011-06-04 01:16:30 +000065
Chandler Carruthb068bbba2011-10-24 01:40:45 +000066static const uint32_t FPH_TAKEN_WEIGHT = 20;
67static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
Andrew Trick9e764222011-06-04 01:16:30 +000068
Chandler Carruthb068bbba2011-10-24 01:40:45 +000069// Standard weight value. Used when none of the heuristics set weight for
70// the edge.
71static const uint32_t NORMAL_WEIGHT = 16;
Andrew Trick9e764222011-06-04 01:16:30 +000072
Chandler Carruthb068bbba2011-10-24 01:40:45 +000073// Minimum weight of an edge. Please note, that weight is NEVER 0.
74static const uint32_t MIN_WEIGHT = 1;
Andrew Trick9e764222011-06-04 01:16:30 +000075
Chandler Carruthb068bbba2011-10-24 01:40:45 +000076// Return TRUE if BB leads directly to a Return Instruction.
77static bool isReturningBlock(BasicBlock *BB) {
78 SmallPtrSet<BasicBlock *, 8> Visited;
Jakub Staszake0058b42011-07-29 02:36:53 +000079
Chandler Carruthb068bbba2011-10-24 01:40:45 +000080 while (true) {
81 TerminatorInst *TI = BB->getTerminator();
82 if (isa<ReturnInst>(TI))
83 return true;
Jakub Staszake0058b42011-07-29 02:36:53 +000084
Chandler Carruthb068bbba2011-10-24 01:40:45 +000085 if (TI->getNumSuccessors() > 1)
86 break;
Jakub Staszaka5dd5502011-07-31 03:27:24 +000087
Chandler Carruthb068bbba2011-10-24 01:40:45 +000088 // It is unreachable block which we can consider as a return instruction.
89 if (TI->getNumSuccessors() == 0)
90 return true;
Benjamin Kramerc888aa42011-10-21 20:12:47 +000091
Chandler Carruthb068bbba2011-10-24 01:40:45 +000092 Visited.insert(BB);
93 BB = TI->getSuccessor(0);
Andrew Trick9e764222011-06-04 01:16:30 +000094
Chandler Carruthb068bbba2011-10-24 01:40:45 +000095 // Stop if cycle is detected.
96 if (Visited.count(BB))
97 return false;
Andrew Trick9e764222011-06-04 01:16:30 +000098 }
99
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000100 return false;
101}
Andrew Trick9e764222011-06-04 01:16:30 +0000102
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000103static uint32_t getMaxWeightFor(BasicBlock *BB) {
104 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
105}
Andrew Trick9e764222011-06-04 01:16:30 +0000106
Andrew Trick9e764222011-06-04 01:16:30 +0000107
Chandler Carruth99d01c52011-10-19 10:30:30 +0000108// Propagate existing explicit probabilities from either profile data or
109// 'expect' intrinsic processing.
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000110bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000111 TerminatorInst *TI = BB->getTerminator();
112 if (TI->getNumSuccessors() == 1)
113 return false;
114 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
Chandler Carruth99d01c52011-10-19 10:30:30 +0000115 return false;
116
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000117 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
118 if (!WeightsNode)
Chandler Carruth99d01c52011-10-19 10:30:30 +0000119 return false;
120
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000121 // Ensure there are weights for all of the successors. Note that the first
122 // operand to the metadata node is a name, not a weight.
123 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
Chandler Carruth99d01c52011-10-19 10:30:30 +0000124 return false;
125
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000126 // Build up the final weights that will be used in a temporary buffer, but
127 // don't add them until all weihts are present. Each weight value is clamped
128 // to [1, getMaxWeightFor(BB)].
Chandler Carruth99d01c52011-10-19 10:30:30 +0000129 uint32_t WeightLimit = getMaxWeightFor(BB);
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000130 SmallVector<uint32_t, 2> Weights;
131 Weights.reserve(TI->getNumSuccessors());
132 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
133 ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
134 if (!Weight)
135 return false;
136 Weights.push_back(
137 std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
138 }
139 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
140 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000141 setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
Chandler Carruth99d01c52011-10-19 10:30:30 +0000142
143 return true;
144}
145
Andrew Trick9e764222011-06-04 01:16:30 +0000146// Calculate Edge Weights using "Return Heuristics". Predict a successor which
147// leads directly to Return Instruction will not be taken.
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000148bool BranchProbabilityInfo::calcReturnHeuristics(BasicBlock *BB){
Andrew Trick9e764222011-06-04 01:16:30 +0000149 if (BB->getTerminator()->getNumSuccessors() == 1)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000150 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000151
Jakub Staszakb137f162011-08-01 19:16:26 +0000152 SmallPtrSet<BasicBlock *, 4> ReturningEdges;
153 SmallPtrSet<BasicBlock *, 4> StayEdges;
Jakub Staszake0058b42011-07-29 02:36:53 +0000154
Andrew Trick9e764222011-06-04 01:16:30 +0000155 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
156 BasicBlock *Succ = *I;
Jakub Staszake0058b42011-07-29 02:36:53 +0000157 if (isReturningBlock(Succ))
Jakub Staszakb137f162011-08-01 19:16:26 +0000158 ReturningEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000159 else
Jakub Staszakb137f162011-08-01 19:16:26 +0000160 StayEdges.insert(Succ);
Jakub Staszake0058b42011-07-29 02:36:53 +0000161 }
162
163 if (uint32_t numStayEdges = StayEdges.size()) {
164 uint32_t stayWeight = RH_TAKEN_WEIGHT / numStayEdges;
165 if (stayWeight < NORMAL_WEIGHT)
166 stayWeight = NORMAL_WEIGHT;
167
Jakub Staszakb137f162011-08-01 19:16:26 +0000168 for (SmallPtrSet<BasicBlock *, 4>::iterator I = StayEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000169 E = StayEdges.end(); I != E; ++I)
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000170 setEdgeWeight(BB, *I, stayWeight);
Jakub Staszake0058b42011-07-29 02:36:53 +0000171 }
172
173 if (uint32_t numRetEdges = ReturningEdges.size()) {
174 uint32_t retWeight = RH_NONTAKEN_WEIGHT / numRetEdges;
175 if (retWeight < MIN_WEIGHT)
176 retWeight = MIN_WEIGHT;
Jakub Staszakb137f162011-08-01 19:16:26 +0000177 for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReturningEdges.begin(),
Jakub Staszake0058b42011-07-29 02:36:53 +0000178 E = ReturningEdges.end(); I != E; ++I) {
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000179 setEdgeWeight(BB, *I, retWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000180 }
181 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000182
Jakub Staszake0058b42011-07-29 02:36:53 +0000183 return ReturningEdges.size() > 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000184}
185
186// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
187// between two pointer or pointer and NULL will fail.
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000188bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
Andrew Trick9e764222011-06-04 01:16:30 +0000189 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
190 if (!BI || !BI->isConditional())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000191 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000192
193 Value *Cond = BI->getCondition();
194 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000195 if (!CI || !CI->isEquality())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000196 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000197
198 Value *LHS = CI->getOperand(0);
Andrew Trick9e764222011-06-04 01:16:30 +0000199
200 if (!LHS->getType()->isPointerTy())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000201 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000202
Nick Lewycky404b53e2011-06-04 02:07:10 +0000203 assert(CI->getOperand(1)->getType()->isPointerTy());
Andrew Trick9e764222011-06-04 01:16:30 +0000204
205 BasicBlock *Taken = BI->getSuccessor(0);
206 BasicBlock *NonTaken = BI->getSuccessor(1);
207
208 // p != 0 -> isProb = true
209 // p == 0 -> isProb = false
210 // p != q -> isProb = true
211 // p == q -> isProb = false;
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000212 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
Andrew Trick9e764222011-06-04 01:16:30 +0000213 if (!isProb)
214 std::swap(Taken, NonTaken);
215
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000216 setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
217 setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
Jakub Staszak7241caf2011-07-28 21:45:07 +0000218 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000219}
220
221// Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
222// as taken, exiting edges as not-taken.
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000223bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) {
Andrew Trickf289df22011-06-11 01:05:22 +0000224 uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
Andrew Trick9e764222011-06-04 01:16:30 +0000225
226 Loop *L = LI->getLoopFor(BB);
227 if (!L)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000228 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000229
Jakub Staszakb137f162011-08-01 19:16:26 +0000230 SmallPtrSet<BasicBlock *, 8> BackEdges;
231 SmallPtrSet<BasicBlock *, 8> ExitingEdges;
232 SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
Jakub Staszakfa447252011-07-28 21:33:46 +0000233
234 bool isHeader = BB == L->getHeader();
Andrew Trick9e764222011-06-04 01:16:30 +0000235
236 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
237 BasicBlock *Succ = *I;
238 Loop *SuccL = LI->getLoopFor(Succ);
239 if (SuccL != L)
Jakub Staszakb137f162011-08-01 19:16:26 +0000240 ExitingEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000241 else if (Succ == L->getHeader())
Jakub Staszakb137f162011-08-01 19:16:26 +0000242 BackEdges.insert(Succ);
Jakub Staszakfa447252011-07-28 21:33:46 +0000243 else if (isHeader)
Jakub Staszakb137f162011-08-01 19:16:26 +0000244 InEdges.insert(Succ);
Andrew Trick9e764222011-06-04 01:16:30 +0000245 }
246
Andrew Trickf289df22011-06-11 01:05:22 +0000247 if (uint32_t numBackEdges = BackEdges.size()) {
248 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000249 if (backWeight < NORMAL_WEIGHT)
250 backWeight = NORMAL_WEIGHT;
251
Jakub Staszakb137f162011-08-01 19:16:26 +0000252 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000253 EE = BackEdges.end(); EI != EE; ++EI) {
254 BasicBlock *Back = *EI;
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000255 setEdgeWeight(BB, Back, backWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000256 }
257 }
258
Jakub Staszakfa447252011-07-28 21:33:46 +0000259 if (uint32_t numInEdges = InEdges.size()) {
260 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
261 if (inWeight < NORMAL_WEIGHT)
262 inWeight = NORMAL_WEIGHT;
263
Jakub Staszakb137f162011-08-01 19:16:26 +0000264 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
Jakub Staszakfa447252011-07-28 21:33:46 +0000265 EE = InEdges.end(); EI != EE; ++EI) {
266 BasicBlock *Back = *EI;
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000267 setEdgeWeight(BB, Back, inWeight);
Jakub Staszakfa447252011-07-28 21:33:46 +0000268 }
269 }
270
Andrew Trickf289df22011-06-11 01:05:22 +0000271 uint32_t numExitingEdges = ExitingEdges.size();
272 if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
273 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000274 if (exitWeight < MIN_WEIGHT)
275 exitWeight = MIN_WEIGHT;
276
Jakub Staszakb137f162011-08-01 19:16:26 +0000277 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000278 EE = ExitingEdges.end(); EI != EE; ++EI) {
279 BasicBlock *Exiting = *EI;
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000280 setEdgeWeight(BB, Exiting, exitWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000281 }
282 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000283
284 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000285}
286
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000287bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000288 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
289 if (!BI || !BI->isConditional())
290 return false;
291
292 Value *Cond = BI->getCondition();
293 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
294 if (!CI)
295 return false;
296
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000297 Value *RHS = CI->getOperand(1);
Jakub Staszaka385c202011-07-31 04:47:20 +0000298 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000299 if (!CV)
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000300 return false;
301
302 bool isProb;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000303 if (CV->isZero()) {
304 switch (CI->getPredicate()) {
305 case CmpInst::ICMP_EQ:
306 // X == 0 -> Unlikely
307 isProb = false;
308 break;
309 case CmpInst::ICMP_NE:
310 // X != 0 -> Likely
311 isProb = true;
312 break;
313 case CmpInst::ICMP_SLT:
314 // X < 0 -> Unlikely
315 isProb = false;
316 break;
317 case CmpInst::ICMP_SGT:
318 // X > 0 -> Likely
319 isProb = true;
320 break;
321 default:
322 return false;
323 }
324 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
325 // InstCombine canonicalizes X <= 0 into X < 1.
326 // X <= 0 -> Unlikely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000327 isProb = false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000328 } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
329 // InstCombine canonicalizes X >= 0 into X > -1.
330 // X >= 0 -> Likely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000331 isProb = true;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000332 } else {
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000333 return false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000334 }
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000335
336 BasicBlock *Taken = BI->getSuccessor(0);
337 BasicBlock *NonTaken = BI->getSuccessor(1);
338
339 if (!isProb)
340 std::swap(Taken, NonTaken);
341
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000342 setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
343 setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000344
345 return true;
346}
347
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000348bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000349 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
350 if (!BI || !BI->isConditional())
351 return false;
352
353 Value *Cond = BI->getCondition();
354 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000355 if (!FCmp)
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000356 return false;
357
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000358 bool isProb;
359 if (FCmp->isEquality()) {
360 // f1 == f2 -> Unlikely
361 // f1 != f2 -> Likely
362 isProb = !FCmp->isTrueWhenEqual();
363 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
364 // !isnan -> Likely
365 isProb = true;
366 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
367 // isnan -> Unlikely
368 isProb = false;
369 } else {
370 return false;
371 }
372
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000373 BasicBlock *Taken = BI->getSuccessor(0);
374 BasicBlock *NonTaken = BI->getSuccessor(1);
375
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000376 if (!isProb)
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000377 std::swap(Taken, NonTaken);
378
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000379 setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
380 setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000381
382 return true;
383}
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000384
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000385void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
386 AU.addRequired<LoopInfo>();
387 AU.setPreservesAll();
388}
389
390bool BranchProbabilityInfo::runOnFunction(Function &F) {
391 LastF = &F; // Store the last function we ran on for printing.
392 LI = &getAnalysis<LoopInfo>();
393
Chandler Carruth22c89462011-10-23 22:40:13 +0000394 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
395 if (calcMetadataWeights(I))
Chandler Carruth99d01c52011-10-19 10:30:30 +0000396 continue;
Chandler Carruth22c89462011-10-23 22:40:13 +0000397 if (calcLoopBranchHeuristics(I))
Jakub Staszak7241caf2011-07-28 21:45:07 +0000398 continue;
Chandler Carruth22c89462011-10-23 22:40:13 +0000399 if (calcReturnHeuristics(I))
Jakub Staszak7241caf2011-07-28 21:45:07 +0000400 continue;
Chandler Carruth22c89462011-10-23 22:40:13 +0000401 if (calcPointerHeuristics(I))
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000402 continue;
Chandler Carruth22c89462011-10-23 22:40:13 +0000403 if (calcZeroHeuristics(I))
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000404 continue;
Chandler Carruth22c89462011-10-23 22:40:13 +0000405 calcFloatingPointHeuristics(I);
Andrew Trick9e764222011-06-04 01:16:30 +0000406 }
Andrew Trick9e764222011-06-04 01:16:30 +0000407 return false;
408}
409
Chandler Carruth14edd312011-10-23 21:21:50 +0000410void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
411 OS << "---- Branch Probabilities ----\n";
412 // We print the probabilities from the last function the analysis ran over,
413 // or the function it is currently running over.
414 assert(LastF && "Cannot print prior to running over a function");
415 for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
416 BI != BE; ++BI) {
417 for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
418 SI != SE; ++SI) {
419 printEdgeProbability(OS << " ", BI, *SI);
420 }
421 }
422}
423
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000424uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000425 uint32_t Sum = 0;
426
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000427 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
428 const BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000429 uint32_t Weight = getEdgeWeight(BB, Succ);
430 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000431
432 Sum += Weight;
433 assert(Sum > PrevSum); (void) PrevSum;
434 }
435
Andrew Trickf289df22011-06-11 01:05:22 +0000436 return Sum;
437}
438
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000439bool BranchProbabilityInfo::
440isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000441 // Hot probability is at least 4/5 = 80%
Benjamin Kramer341473c2011-10-23 11:19:14 +0000442 // FIXME: Compare against a static "hot" BranchProbability.
443 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
Andrew Trick9e764222011-06-04 01:16:30 +0000444}
445
446BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000447 uint32_t Sum = 0;
448 uint32_t MaxWeight = 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000449 BasicBlock *MaxSucc = 0;
450
451 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
452 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 if (Weight > MaxWeight) {
460 MaxWeight = Weight;
461 MaxSucc = Succ;
462 }
463 }
464
Benjamin Kramer341473c2011-10-23 11:19:14 +0000465 // Hot probability is at least 4/5 = 80%
466 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
Andrew Trick9e764222011-06-04 01:16:30 +0000467 return MaxSucc;
468
469 return 0;
470}
471
472// Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000473uint32_t BranchProbabilityInfo::
474getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000475 Edge E(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000476 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
Andrew Trick9e764222011-06-04 01:16:30 +0000477
478 if (I != Weights.end())
479 return I->second;
480
481 return DEFAULT_WEIGHT;
482}
483
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000484void BranchProbabilityInfo::
485setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
Andrew Trick9e764222011-06-04 01:16:30 +0000486 Weights[std::make_pair(Src, Dst)] = Weight;
Andrew Trickf289df22011-06-11 01:05:22 +0000487 DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
488 << Dst->getNameStr() << " weight to " << Weight
489 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
490}
491
492
493BranchProbability BranchProbabilityInfo::
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000494getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000495
496 uint32_t N = getEdgeWeight(Src, Dst);
497 uint32_t D = getSumForBlock(Src);
498
499 return BranchProbability(N, D);
Andrew Trick9e764222011-06-04 01:16:30 +0000500}
501
502raw_ostream &
Chandler Carruth14edd312011-10-23 21:21:50 +0000503BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
504 const BasicBlock *Src,
505 const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000506
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000507 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000508 OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
509 << " probability is " << Prob
510 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Andrew Trick9e764222011-06-04 01:16:30 +0000511
512 return OS;
513}