blob: 4f15858aa34ff1ef810b2356e1e4bd8cf241d572 [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);
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000394 if (!FCmp)
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000395 return false;
396
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000397 bool isProb;
398 if (FCmp->isEquality()) {
399 // f1 == f2 -> Unlikely
400 // f1 != f2 -> Likely
401 isProb = !FCmp->isTrueWhenEqual();
402 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
403 // !isnan -> Likely
404 isProb = true;
405 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
406 // isnan -> Unlikely
407 isProb = false;
408 } else {
409 return false;
410 }
411
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000412 BasicBlock *Taken = BI->getSuccessor(0);
413 BasicBlock *NonTaken = BI->getSuccessor(1);
414
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000415 if (!isProb)
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000416 std::swap(Taken, NonTaken);
417
418 BP->setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
419 BP->setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
420
421 return true;
422}
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000423
Andrew Trick9e764222011-06-04 01:16:30 +0000424bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
425
426 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
427 BasicBlock *BB = I++;
428
Chandler Carruth99d01c52011-10-19 10:30:30 +0000429 if (calcMetadataWeights(BB))
430 continue;
431
Jakub Staszak7241caf2011-07-28 21:45:07 +0000432 if (calcLoopBranchHeuristics(BB))
433 continue;
Andrew Trick9e764222011-06-04 01:16:30 +0000434
Jakub Staszak7241caf2011-07-28 21:45:07 +0000435 if (calcReturnHeuristics(BB))
436 continue;
437
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000438 if (calcPointerHeuristics(BB))
439 continue;
440
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000441 if (calcZeroHeuristics(BB))
442 continue;
443
444 calcFloatingPointHeuristics(BB);
Andrew Trick9e764222011-06-04 01:16:30 +0000445 }
446
447 return false;
448}
449
Jakub Staszak12af93a2011-07-16 20:31:15 +0000450void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
451 AU.addRequired<LoopInfo>();
452 AU.setPreservesAll();
453}
Andrew Trick9e764222011-06-04 01:16:30 +0000454
455bool BranchProbabilityInfo::runOnFunction(Function &F) {
456 LoopInfo &LI = getAnalysis<LoopInfo>();
Chandler Carruth7a34c8b2011-10-16 22:27:54 +0000457 BranchProbabilityAnalysis BPA(this, &LI);
Andrew Trickf289df22011-06-11 01:05:22 +0000458 return BPA.runOnFunction(F);
Andrew Trick9e764222011-06-04 01:16:30 +0000459}
460
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000461uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000462 uint32_t Sum = 0;
463
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000464 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
465 const BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000466 uint32_t Weight = getEdgeWeight(BB, Succ);
467 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000468
469 Sum += Weight;
470 assert(Sum > PrevSum); (void) PrevSum;
471 }
472
Andrew Trickf289df22011-06-11 01:05:22 +0000473 return Sum;
474}
475
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000476bool BranchProbabilityInfo::
477isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000478 // Hot probability is at least 4/5 = 80%
Benjamin Kramer341473c2011-10-23 11:19:14 +0000479 // FIXME: Compare against a static "hot" BranchProbability.
480 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
Andrew Trick9e764222011-06-04 01:16:30 +0000481}
482
483BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000484 uint32_t Sum = 0;
485 uint32_t MaxWeight = 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000486 BasicBlock *MaxSucc = 0;
487
488 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
489 BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000490 uint32_t Weight = getEdgeWeight(BB, Succ);
491 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000492
493 Sum += Weight;
494 assert(Sum > PrevSum); (void) PrevSum;
495
496 if (Weight > MaxWeight) {
497 MaxWeight = Weight;
498 MaxSucc = Succ;
499 }
500 }
501
Benjamin Kramer341473c2011-10-23 11:19:14 +0000502 // Hot probability is at least 4/5 = 80%
503 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
Andrew Trick9e764222011-06-04 01:16:30 +0000504 return MaxSucc;
505
506 return 0;
507}
508
509// Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000510uint32_t BranchProbabilityInfo::
511getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000512 Edge E(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000513 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
Andrew Trick9e764222011-06-04 01:16:30 +0000514
515 if (I != Weights.end())
516 return I->second;
517
518 return DEFAULT_WEIGHT;
519}
520
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000521void BranchProbabilityInfo::
522setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
Andrew Trick9e764222011-06-04 01:16:30 +0000523 Weights[std::make_pair(Src, Dst)] = Weight;
Andrew Trickf289df22011-06-11 01:05:22 +0000524 DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
525 << Dst->getNameStr() << " weight to " << Weight
526 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
527}
528
529
530BranchProbability BranchProbabilityInfo::
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000531getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000532
533 uint32_t N = getEdgeWeight(Src, Dst);
534 uint32_t D = getSumForBlock(Src);
535
536 return BranchProbability(N, D);
Andrew Trick9e764222011-06-04 01:16:30 +0000537}
538
539raw_ostream &
540BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
Andrew Trickf289df22011-06-11 01:05:22 +0000541 BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000542
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000543 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000544 OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
545 << " probability is " << Prob
546 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Andrew Trick9e764222011-06-04 01:16:30 +0000547
548 return OS;
549}