blob: 2730ce6c63bfe4b3cb9f650e9de8df50c74498f1 [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 Carruthde1c9bb2011-10-24 12:01:08 +000021#include "llvm/ADT/PostOrderIterator.h"
Chandler Carruth14edd312011-10-23 21:21:50 +000022#include "llvm/Support/CFG.h"
Andrew Trickf289df22011-06-11 01:05:22 +000023#include "llvm/Support/Debug.h"
Andrew Trick9e764222011-06-04 01:16:30 +000024
25using namespace llvm;
26
27INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
28 "Branch Probability Analysis", false, true)
29INITIALIZE_PASS_DEPENDENCY(LoopInfo)
30INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
31 "Branch Probability Analysis", false, true)
32
33char BranchProbabilityInfo::ID = 0;
34
Chandler Carruthb068bbba2011-10-24 01:40:45 +000035// Weights are for internal use only. They are used by heuristics to help to
36// estimate edges' probability. Example:
37//
38// Using "Loop Branch Heuristics" we predict weights of edges for the
39// block BB2.
40// ...
41// |
42// V
43// BB1<-+
44// | |
45// | | (Weight = 124)
46// V |
47// BB2--+
48// |
49// | (Weight = 4)
50// V
51// BB3
52//
53// Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
54// Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
55static const uint32_t LBH_TAKEN_WEIGHT = 124;
56static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
Andrew Trick9e764222011-06-04 01:16:30 +000057
Chandler Carruthde1c9bb2011-10-24 12:01:08 +000058/// \brief Unreachable-terminating branch taken weight.
59///
60/// This is the weight for a branch being taken to a block that terminates
61/// (eventually) in unreachable. These are predicted as unlikely as possible.
62static const uint32_t UR_TAKEN_WEIGHT = 1;
63
64/// \brief Unreachable-terminating branch not-taken weight.
65///
66/// This is the weight for a branch not being taken toward a block that
67/// terminates (eventually) in unreachable. Such a branch is essentially never
Chandler Carruth51f40a72011-12-22 09:26:37 +000068/// taken. Set the weight to an absurdly high value so that nested loops don't
69/// easily subsume it.
70static const uint32_t UR_NONTAKEN_WEIGHT = 1024*1024 - 1;
Andrew Trick9e764222011-06-04 01:16:30 +000071
Chandler Carruthb068bbba2011-10-24 01:40:45 +000072static const uint32_t PH_TAKEN_WEIGHT = 20;
73static const uint32_t PH_NONTAKEN_WEIGHT = 12;
Andrew Trick9e764222011-06-04 01:16:30 +000074
Chandler Carruthb068bbba2011-10-24 01:40:45 +000075static const uint32_t ZH_TAKEN_WEIGHT = 20;
76static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
Andrew Trick9e764222011-06-04 01:16:30 +000077
Chandler Carruthb068bbba2011-10-24 01:40:45 +000078static const uint32_t FPH_TAKEN_WEIGHT = 20;
79static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
Andrew Trick9e764222011-06-04 01:16:30 +000080
Chandler Carruthb068bbba2011-10-24 01:40:45 +000081// Standard weight value. Used when none of the heuristics set weight for
82// the edge.
83static const uint32_t NORMAL_WEIGHT = 16;
Andrew Trick9e764222011-06-04 01:16:30 +000084
Chandler Carruthb068bbba2011-10-24 01:40:45 +000085// Minimum weight of an edge. Please note, that weight is NEVER 0.
86static const uint32_t MIN_WEIGHT = 1;
Andrew Trick9e764222011-06-04 01:16:30 +000087
Chandler Carruthb068bbba2011-10-24 01:40:45 +000088static uint32_t getMaxWeightFor(BasicBlock *BB) {
89 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
90}
Andrew Trick9e764222011-06-04 01:16:30 +000091
Andrew Trick9e764222011-06-04 01:16:30 +000092
Chandler Carruthde1c9bb2011-10-24 12:01:08 +000093/// \brief Calculate edge weights for successors lead to unreachable.
94///
95/// Predict that a successor which leads necessarily to an
96/// unreachable-terminated block as extremely unlikely.
97bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) {
98 TerminatorInst *TI = BB->getTerminator();
99 if (TI->getNumSuccessors() == 0) {
100 if (isa<UnreachableInst>(TI))
101 PostDominatedByUnreachable.insert(BB);
102 return false;
103 }
104
105 SmallPtrSet<BasicBlock *, 4> UnreachableEdges;
106 SmallPtrSet<BasicBlock *, 4> ReachableEdges;
107
108 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
109 if (PostDominatedByUnreachable.count(*I))
110 UnreachableEdges.insert(*I);
111 else
112 ReachableEdges.insert(*I);
113 }
114
115 // If all successors are in the set of blocks post-dominated by unreachable,
116 // this block is too.
117 if (UnreachableEdges.size() == TI->getNumSuccessors())
118 PostDominatedByUnreachable.insert(BB);
119
120 // Skip probabilities if this block has a single successor or if all were
121 // reachable.
122 if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
123 return false;
124
125 uint32_t UnreachableWeight =
126 std::max(UR_TAKEN_WEIGHT / UnreachableEdges.size(), MIN_WEIGHT);
127 for (SmallPtrSet<BasicBlock *, 4>::iterator I = UnreachableEdges.begin(),
128 E = UnreachableEdges.end();
129 I != E; ++I)
130 setEdgeWeight(BB, *I, UnreachableWeight);
131
132 if (ReachableEdges.empty())
133 return true;
134 uint32_t ReachableWeight =
135 std::max(UR_NONTAKEN_WEIGHT / ReachableEdges.size(), NORMAL_WEIGHT);
136 for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReachableEdges.begin(),
137 E = ReachableEdges.end();
138 I != E; ++I)
139 setEdgeWeight(BB, *I, ReachableWeight);
140
141 return true;
142}
143
Chandler Carruth99d01c52011-10-19 10:30:30 +0000144// Propagate existing explicit probabilities from either profile data or
145// 'expect' intrinsic processing.
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000146bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000147 TerminatorInst *TI = BB->getTerminator();
148 if (TI->getNumSuccessors() == 1)
149 return false;
150 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
Chandler Carruth99d01c52011-10-19 10:30:30 +0000151 return false;
152
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000153 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
154 if (!WeightsNode)
Chandler Carruth99d01c52011-10-19 10:30:30 +0000155 return false;
156
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000157 // Ensure there are weights for all of the successors. Note that the first
158 // operand to the metadata node is a name, not a weight.
159 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
Chandler Carruth99d01c52011-10-19 10:30:30 +0000160 return false;
161
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000162 // Build up the final weights that will be used in a temporary buffer, but
163 // don't add them until all weihts are present. Each weight value is clamped
164 // to [1, getMaxWeightFor(BB)].
Chandler Carruth99d01c52011-10-19 10:30:30 +0000165 uint32_t WeightLimit = getMaxWeightFor(BB);
Chandler Carruth941aa7b2011-10-19 10:32:19 +0000166 SmallVector<uint32_t, 2> Weights;
167 Weights.reserve(TI->getNumSuccessors());
168 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
169 ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
170 if (!Weight)
171 return false;
172 Weights.push_back(
173 std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
174 }
175 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
176 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000177 setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
Chandler Carruth99d01c52011-10-19 10:30:30 +0000178
179 return true;
180}
181
Andrew Trick9e764222011-06-04 01:16:30 +0000182// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
183// between two pointer or pointer and NULL will fail.
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000184bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
Andrew Trick9e764222011-06-04 01:16:30 +0000185 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
186 if (!BI || !BI->isConditional())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000187 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000188
189 Value *Cond = BI->getCondition();
190 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000191 if (!CI || !CI->isEquality())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000192 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000193
194 Value *LHS = CI->getOperand(0);
Andrew Trick9e764222011-06-04 01:16:30 +0000195
196 if (!LHS->getType()->isPointerTy())
Jakub Staszak7241caf2011-07-28 21:45:07 +0000197 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000198
Nick Lewycky404b53e2011-06-04 02:07:10 +0000199 assert(CI->getOperand(1)->getType()->isPointerTy());
Andrew Trick9e764222011-06-04 01:16:30 +0000200
201 BasicBlock *Taken = BI->getSuccessor(0);
202 BasicBlock *NonTaken = BI->getSuccessor(1);
203
204 // p != 0 -> isProb = true
205 // p == 0 -> isProb = false
206 // p != q -> isProb = true
207 // p == q -> isProb = false;
Jakub Staszakd7932ca2011-07-15 20:51:06 +0000208 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
Andrew Trick9e764222011-06-04 01:16:30 +0000209 if (!isProb)
210 std::swap(Taken, NonTaken);
211
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000212 setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
213 setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
Jakub Staszak7241caf2011-07-28 21:45:07 +0000214 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000215}
216
217// Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
218// as taken, exiting edges as not-taken.
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000219bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) {
Andrew Trick9e764222011-06-04 01:16:30 +0000220 Loop *L = LI->getLoopFor(BB);
221 if (!L)
Jakub Staszak7241caf2011-07-28 21:45:07 +0000222 return false;
Andrew Trick9e764222011-06-04 01:16:30 +0000223
Jakub Staszakb137f162011-08-01 19:16:26 +0000224 SmallPtrSet<BasicBlock *, 8> BackEdges;
225 SmallPtrSet<BasicBlock *, 8> ExitingEdges;
226 SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
Jakub Staszakfa447252011-07-28 21:33:46 +0000227
Andrew Trick9e764222011-06-04 01:16:30 +0000228 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
Chandler Carruth45baf6b2011-10-25 09:47:41 +0000229 if (!L->contains(*I))
230 ExitingEdges.insert(*I);
231 else if (L->getHeader() == *I)
232 BackEdges.insert(*I);
233 else
234 InEdges.insert(*I);
Andrew Trick9e764222011-06-04 01:16:30 +0000235 }
236
Andrew Trickf289df22011-06-11 01:05:22 +0000237 if (uint32_t numBackEdges = BackEdges.size()) {
238 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000239 if (backWeight < NORMAL_WEIGHT)
240 backWeight = NORMAL_WEIGHT;
241
Jakub Staszakb137f162011-08-01 19:16:26 +0000242 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000243 EE = BackEdges.end(); EI != EE; ++EI) {
244 BasicBlock *Back = *EI;
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000245 setEdgeWeight(BB, Back, backWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000246 }
247 }
248
Jakub Staszakfa447252011-07-28 21:33:46 +0000249 if (uint32_t numInEdges = InEdges.size()) {
250 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
251 if (inWeight < NORMAL_WEIGHT)
252 inWeight = NORMAL_WEIGHT;
253
Jakub Staszakb137f162011-08-01 19:16:26 +0000254 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
Jakub Staszakfa447252011-07-28 21:33:46 +0000255 EE = InEdges.end(); EI != EE; ++EI) {
256 BasicBlock *Back = *EI;
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000257 setEdgeWeight(BB, Back, inWeight);
Jakub Staszakfa447252011-07-28 21:33:46 +0000258 }
259 }
260
Chandler Carruth45baf6b2011-10-25 09:47:41 +0000261 if (uint32_t numExitingEdges = ExitingEdges.size()) {
262 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;
Andrew Trick9e764222011-06-04 01:16:30 +0000263 if (exitWeight < MIN_WEIGHT)
264 exitWeight = MIN_WEIGHT;
265
Jakub Staszakb137f162011-08-01 19:16:26 +0000266 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
Andrew Trick9e764222011-06-04 01:16:30 +0000267 EE = ExitingEdges.end(); EI != EE; ++EI) {
268 BasicBlock *Exiting = *EI;
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000269 setEdgeWeight(BB, Exiting, exitWeight);
Andrew Trick9e764222011-06-04 01:16:30 +0000270 }
271 }
Jakub Staszak7241caf2011-07-28 21:45:07 +0000272
273 return true;
Andrew Trick9e764222011-06-04 01:16:30 +0000274}
275
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000276bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000277 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
278 if (!BI || !BI->isConditional())
279 return false;
280
281 Value *Cond = BI->getCondition();
282 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
283 if (!CI)
284 return false;
285
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000286 Value *RHS = CI->getOperand(1);
Jakub Staszaka385c202011-07-31 04:47:20 +0000287 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000288 if (!CV)
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000289 return false;
290
291 bool isProb;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000292 if (CV->isZero()) {
293 switch (CI->getPredicate()) {
294 case CmpInst::ICMP_EQ:
295 // X == 0 -> Unlikely
296 isProb = false;
297 break;
298 case CmpInst::ICMP_NE:
299 // X != 0 -> Likely
300 isProb = true;
301 break;
302 case CmpInst::ICMP_SLT:
303 // X < 0 -> Unlikely
304 isProb = false;
305 break;
306 case CmpInst::ICMP_SGT:
307 // X > 0 -> Likely
308 isProb = true;
309 break;
310 default:
311 return false;
312 }
313 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
314 // InstCombine canonicalizes X <= 0 into X < 1.
315 // X <= 0 -> Unlikely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000316 isProb = false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000317 } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
318 // InstCombine canonicalizes X >= 0 into X > -1.
319 // X >= 0 -> Likely
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000320 isProb = true;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000321 } else {
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000322 return false;
Benjamin Kramer26eb8702011-09-04 23:53:04 +0000323 }
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000324
325 BasicBlock *Taken = BI->getSuccessor(0);
326 BasicBlock *NonTaken = BI->getSuccessor(1);
327
328 if (!isProb)
329 std::swap(Taken, NonTaken);
330
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000331 setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
332 setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000333
334 return true;
335}
336
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000337bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000338 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
339 if (!BI || !BI->isConditional())
340 return false;
341
342 Value *Cond = BI->getCondition();
343 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000344 if (!FCmp)
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000345 return false;
346
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000347 bool isProb;
348 if (FCmp->isEquality()) {
349 // f1 == f2 -> Unlikely
350 // f1 != f2 -> Likely
351 isProb = !FCmp->isTrueWhenEqual();
352 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
353 // !isnan -> Likely
354 isProb = true;
355 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
356 // isnan -> Unlikely
357 isProb = false;
358 } else {
359 return false;
360 }
361
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000362 BasicBlock *Taken = BI->getSuccessor(0);
363 BasicBlock *NonTaken = BI->getSuccessor(1);
364
Benjamin Kramer675c02b2011-10-21 21:13:47 +0000365 if (!isProb)
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000366 std::swap(Taken, NonTaken);
367
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000368 setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
369 setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000370
371 return true;
372}
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000373
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000374void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
375 AU.addRequired<LoopInfo>();
376 AU.setPreservesAll();
377}
378
379bool BranchProbabilityInfo::runOnFunction(Function &F) {
380 LastF = &F; // Store the last function we ran on for printing.
381 LI = &getAnalysis<LoopInfo>();
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000382 assert(PostDominatedByUnreachable.empty());
Chandler Carruthb068bbba2011-10-24 01:40:45 +0000383
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000384 // Walk the basic blocks in post-order so that we can build up state about
385 // the successors of a block iteratively.
386 for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()),
387 E = po_end(&F.getEntryBlock());
388 I != E; ++I) {
389 DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
390 if (calcUnreachableHeuristics(*I))
Chandler Carruth99d01c52011-10-19 10:30:30 +0000391 continue;
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000392 if (calcMetadataWeights(*I))
Jakub Staszak7241caf2011-07-28 21:45:07 +0000393 continue;
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000394 if (calcLoopBranchHeuristics(*I))
Jakub Staszak7241caf2011-07-28 21:45:07 +0000395 continue;
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000396 if (calcPointerHeuristics(*I))
Jakub Staszaka5dd5502011-07-31 03:27:24 +0000397 continue;
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000398 if (calcZeroHeuristics(*I))
Benjamin Kramerc888aa42011-10-21 20:12:47 +0000399 continue;
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000400 calcFloatingPointHeuristics(*I);
Andrew Trick9e764222011-06-04 01:16:30 +0000401 }
Chandler Carruthde1c9bb2011-10-24 12:01:08 +0000402
403 PostDominatedByUnreachable.clear();
Andrew Trick9e764222011-06-04 01:16:30 +0000404 return false;
405}
406
Chandler Carruth14edd312011-10-23 21:21:50 +0000407void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
408 OS << "---- Branch Probabilities ----\n";
409 // We print the probabilities from the last function the analysis ran over,
410 // or the function it is currently running over.
411 assert(LastF && "Cannot print prior to running over a function");
412 for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
413 BI != BE; ++BI) {
414 for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
415 SI != SE; ++SI) {
416 printEdgeProbability(OS << " ", BI, *SI);
417 }
418 }
419}
420
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000421uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000422 uint32_t Sum = 0;
423
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000424 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
425 const BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000426 uint32_t Weight = getEdgeWeight(BB, Succ);
427 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000428
429 Sum += Weight;
430 assert(Sum > PrevSum); (void) PrevSum;
431 }
432
Andrew Trickf289df22011-06-11 01:05:22 +0000433 return Sum;
434}
435
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000436bool BranchProbabilityInfo::
437isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000438 // Hot probability is at least 4/5 = 80%
Benjamin Kramer341473c2011-10-23 11:19:14 +0000439 // FIXME: Compare against a static "hot" BranchProbability.
440 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
Andrew Trick9e764222011-06-04 01:16:30 +0000441}
442
443BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000444 uint32_t Sum = 0;
445 uint32_t MaxWeight = 0;
Andrew Trick9e764222011-06-04 01:16:30 +0000446 BasicBlock *MaxSucc = 0;
447
448 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
449 BasicBlock *Succ = *I;
Andrew Trickf289df22011-06-11 01:05:22 +0000450 uint32_t Weight = getEdgeWeight(BB, Succ);
451 uint32_t PrevSum = Sum;
Andrew Trick9e764222011-06-04 01:16:30 +0000452
453 Sum += Weight;
454 assert(Sum > PrevSum); (void) PrevSum;
455
456 if (Weight > MaxWeight) {
457 MaxWeight = Weight;
458 MaxSucc = Succ;
459 }
460 }
461
Benjamin Kramer341473c2011-10-23 11:19:14 +0000462 // Hot probability is at least 4/5 = 80%
463 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
Andrew Trick9e764222011-06-04 01:16:30 +0000464 return MaxSucc;
465
466 return 0;
467}
468
469// Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000470uint32_t BranchProbabilityInfo::
471getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000472 Edge E(Src, Dst);
Andrew Trickf289df22011-06-11 01:05:22 +0000473 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
Andrew Trick9e764222011-06-04 01:16:30 +0000474
475 if (I != Weights.end())
476 return I->second;
477
478 return DEFAULT_WEIGHT;
479}
480
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000481void BranchProbabilityInfo::
482setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
Andrew Trick9e764222011-06-04 01:16:30 +0000483 Weights[std::make_pair(Src, Dst)] = Weight;
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000484 DEBUG(dbgs() << "set edge " << Src->getName() << " -> "
485 << Dst->getName() << " weight to " << Weight
Andrew Trickf289df22011-06-11 01:05:22 +0000486 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
487}
488
489
490BranchProbability BranchProbabilityInfo::
Jakub Staszak6f6baf12011-07-29 19:30:00 +0000491getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
Andrew Trickf289df22011-06-11 01:05:22 +0000492
493 uint32_t N = getEdgeWeight(Src, Dst);
494 uint32_t D = getSumForBlock(Src);
495
496 return BranchProbability(N, D);
Andrew Trick9e764222011-06-04 01:16:30 +0000497}
498
499raw_ostream &
Chandler Carruth14edd312011-10-23 21:21:50 +0000500BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
501 const BasicBlock *Src,
502 const BasicBlock *Dst) const {
Andrew Trick9e764222011-06-04 01:16:30 +0000503
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000504 const BranchProbability Prob = getEdgeProbability(Src, Dst);
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000505 OS << "edge " << Src->getName() << " -> " << Dst->getName()
Andrew Trickf289df22011-06-11 01:05:22 +0000506 << " probability is " << Prob
507 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
Andrew Trick9e764222011-06-04 01:16:30 +0000508
509 return OS;
510}