1 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/BranchProbabilityInfo.h"
15 #include "llvm/ADT/PostOrderIterator.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Metadata.h"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Support/Debug.h"
27 INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
28 "Branch Probability Analysis", false, true)
29 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
30 INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
31 "Branch Probability Analysis", false, true)
33 char BranchProbabilityInfo::ID = 0;
35 // Weights are for internal use only. They are used by heuristics to help to
36 // estimate edges' probability. Example:
38 // Using "Loop Branch Heuristics" we predict weights of edges for the
53 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
54 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
55 static const uint32_t LBH_TAKEN_WEIGHT = 124;
56 static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
58 /// \brief Unreachable-terminating branch taken weight.
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.
62 static const uint32_t UR_TAKEN_WEIGHT = 1;
64 /// \brief Unreachable-terminating branch not-taken weight.
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
68 /// taken. Set the weight to an absurdly high value so that nested loops don't
69 /// easily subsume it.
70 static const uint32_t UR_NONTAKEN_WEIGHT = 1024*1024 - 1;
72 static const uint32_t PH_TAKEN_WEIGHT = 20;
73 static const uint32_t PH_NONTAKEN_WEIGHT = 12;
75 static const uint32_t ZH_TAKEN_WEIGHT = 20;
76 static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
78 static const uint32_t FPH_TAKEN_WEIGHT = 20;
79 static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
81 /// \brief Invoke-terminating normal branch taken weight
83 /// This is the weight for branching to the normal destination of an invoke
84 /// instruction. We expect this to happen most of the time. Set the weight to an
85 /// absurdly high value so that nested loops subsume it.
86 static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1;
88 /// \brief Invoke-terminating normal branch not-taken weight.
90 /// This is the weight for branching to the unwind destination of an invoke
91 /// instruction. This is essentially never taken.
92 static const uint32_t IH_NONTAKEN_WEIGHT = 1;
94 // Standard weight value. Used when none of the heuristics set weight for
96 static const uint32_t NORMAL_WEIGHT = 16;
98 // Minimum weight of an edge. Please note, that weight is NEVER 0.
99 static const uint32_t MIN_WEIGHT = 1;
101 static uint32_t getMaxWeightFor(BasicBlock *BB) {
102 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
106 /// \brief Calculate edge weights for successors lead to unreachable.
108 /// Predict that a successor which leads necessarily to an
109 /// unreachable-terminated block as extremely unlikely.
110 bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) {
111 TerminatorInst *TI = BB->getTerminator();
112 if (TI->getNumSuccessors() == 0) {
113 if (isa<UnreachableInst>(TI))
114 PostDominatedByUnreachable.insert(BB);
118 SmallVector<unsigned, 4> UnreachableEdges;
119 SmallVector<unsigned, 4> ReachableEdges;
121 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
122 if (PostDominatedByUnreachable.count(*I))
123 UnreachableEdges.push_back(I.getSuccessorIndex());
125 ReachableEdges.push_back(I.getSuccessorIndex());
128 // If all successors are in the set of blocks post-dominated by unreachable,
129 // this block is too.
130 if (UnreachableEdges.size() == TI->getNumSuccessors())
131 PostDominatedByUnreachable.insert(BB);
133 // Skip probabilities if this block has a single successor or if all were
135 if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
138 uint32_t UnreachableWeight =
139 std::max(UR_TAKEN_WEIGHT / (unsigned)UnreachableEdges.size(), MIN_WEIGHT);
140 for (SmallVector<unsigned, 4>::iterator I = UnreachableEdges.begin(),
141 E = UnreachableEdges.end();
143 setEdgeWeight(BB, *I, UnreachableWeight);
145 if (ReachableEdges.empty())
147 uint32_t ReachableWeight =
148 std::max(UR_NONTAKEN_WEIGHT / (unsigned)ReachableEdges.size(),
150 for (SmallVector<unsigned, 4>::iterator I = ReachableEdges.begin(),
151 E = ReachableEdges.end();
153 setEdgeWeight(BB, *I, ReachableWeight);
158 // Propagate existing explicit probabilities from either profile data or
159 // 'expect' intrinsic processing.
160 bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
161 TerminatorInst *TI = BB->getTerminator();
162 if (TI->getNumSuccessors() == 1)
164 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
167 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
171 // Ensure there are weights for all of the successors. Note that the first
172 // operand to the metadata node is a name, not a weight.
173 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
176 // Build up the final weights that will be used in a temporary buffer, but
177 // don't add them until all weihts are present. Each weight value is clamped
178 // to [1, getMaxWeightFor(BB)].
179 uint32_t WeightLimit = getMaxWeightFor(BB);
180 SmallVector<uint32_t, 2> Weights;
181 Weights.reserve(TI->getNumSuccessors());
182 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
183 ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
187 std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
189 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
190 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
191 setEdgeWeight(BB, i, Weights[i]);
196 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
197 // between two pointer or pointer and NULL will fail.
198 bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
199 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
200 if (!BI || !BI->isConditional())
203 Value *Cond = BI->getCondition();
204 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
205 if (!CI || !CI->isEquality())
208 Value *LHS = CI->getOperand(0);
210 if (!LHS->getType()->isPointerTy())
213 assert(CI->getOperand(1)->getType()->isPointerTy());
215 // p != 0 -> isProb = true
216 // p == 0 -> isProb = false
217 // p != q -> isProb = true
218 // p == q -> isProb = false;
219 unsigned TakenIdx = 0, NonTakenIdx = 1;
220 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
222 std::swap(TakenIdx, NonTakenIdx);
224 setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT);
225 setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT);
229 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
230 // as taken, exiting edges as not-taken.
231 bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) {
232 Loop *L = LI->getLoopFor(BB);
236 SmallVector<unsigned, 8> BackEdges;
237 SmallVector<unsigned, 8> ExitingEdges;
238 SmallVector<unsigned, 8> InEdges; // Edges from header to the loop.
240 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
241 if (!L->contains(*I))
242 ExitingEdges.push_back(I.getSuccessorIndex());
243 else if (L->getHeader() == *I)
244 BackEdges.push_back(I.getSuccessorIndex());
246 InEdges.push_back(I.getSuccessorIndex());
249 if (uint32_t numBackEdges = BackEdges.size()) {
250 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
251 if (backWeight < NORMAL_WEIGHT)
252 backWeight = NORMAL_WEIGHT;
254 for (SmallVector<unsigned, 8>::iterator EI = BackEdges.begin(),
255 EE = BackEdges.end(); EI != EE; ++EI) {
256 setEdgeWeight(BB, *EI, backWeight);
260 if (uint32_t numInEdges = InEdges.size()) {
261 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
262 if (inWeight < NORMAL_WEIGHT)
263 inWeight = NORMAL_WEIGHT;
265 for (SmallVector<unsigned, 8>::iterator EI = InEdges.begin(),
266 EE = InEdges.end(); EI != EE; ++EI) {
267 setEdgeWeight(BB, *EI, inWeight);
271 if (uint32_t numExitingEdges = ExitingEdges.size()) {
272 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;
273 if (exitWeight < MIN_WEIGHT)
274 exitWeight = MIN_WEIGHT;
276 for (SmallVector<unsigned, 8>::iterator EI = ExitingEdges.begin(),
277 EE = ExitingEdges.end(); EI != EE; ++EI) {
278 setEdgeWeight(BB, *EI, exitWeight);
285 bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
286 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
287 if (!BI || !BI->isConditional())
290 Value *Cond = BI->getCondition();
291 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
295 Value *RHS = CI->getOperand(1);
296 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
302 switch (CI->getPredicate()) {
303 case CmpInst::ICMP_EQ:
304 // X == 0 -> Unlikely
307 case CmpInst::ICMP_NE:
311 case CmpInst::ICMP_SLT:
315 case CmpInst::ICMP_SGT:
322 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
323 // InstCombine canonicalizes X <= 0 into X < 1.
324 // X <= 0 -> Unlikely
326 } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
327 // InstCombine canonicalizes X >= 0 into X > -1.
334 unsigned TakenIdx = 0, NonTakenIdx = 1;
337 std::swap(TakenIdx, NonTakenIdx);
339 setEdgeWeight(BB, TakenIdx, ZH_TAKEN_WEIGHT);
340 setEdgeWeight(BB, NonTakenIdx, ZH_NONTAKEN_WEIGHT);
345 bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
346 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
347 if (!BI || !BI->isConditional())
350 Value *Cond = BI->getCondition();
351 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
356 if (FCmp->isEquality()) {
357 // f1 == f2 -> Unlikely
358 // f1 != f2 -> Likely
359 isProb = !FCmp->isTrueWhenEqual();
360 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
363 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
370 unsigned TakenIdx = 0, NonTakenIdx = 1;
373 std::swap(TakenIdx, NonTakenIdx);
375 setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT);
376 setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT);
381 bool BranchProbabilityInfo::calcInvokeHeuristics(BasicBlock *BB) {
382 InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator());
386 setEdgeWeight(BB, 0/*Index for Normal*/, IH_TAKEN_WEIGHT);
387 setEdgeWeight(BB, 1/*Index for Unwind*/, IH_NONTAKEN_WEIGHT);
391 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
392 AU.addRequired<LoopInfo>();
393 AU.setPreservesAll();
396 bool BranchProbabilityInfo::runOnFunction(Function &F) {
397 LastF = &F; // Store the last function we ran on for printing.
398 LI = &getAnalysis<LoopInfo>();
399 assert(PostDominatedByUnreachable.empty());
401 // Walk the basic blocks in post-order so that we can build up state about
402 // the successors of a block iteratively.
403 for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()),
404 E = po_end(&F.getEntryBlock());
406 DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
407 if (calcUnreachableHeuristics(*I))
409 if (calcMetadataWeights(*I))
411 if (calcLoopBranchHeuristics(*I))
413 if (calcPointerHeuristics(*I))
415 if (calcZeroHeuristics(*I))
417 if (calcFloatingPointHeuristics(*I))
419 calcInvokeHeuristics(*I);
422 PostDominatedByUnreachable.clear();
426 void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
427 OS << "---- Branch Probabilities ----\n";
428 // We print the probabilities from the last function the analysis ran over,
429 // or the function it is currently running over.
430 assert(LastF && "Cannot print prior to running over a function");
431 for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
433 for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
435 printEdgeProbability(OS << " ", BI, *SI);
440 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
443 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
444 uint32_t Weight = getEdgeWeight(BB, I.getSuccessorIndex());
445 uint32_t PrevSum = Sum;
448 assert(Sum > PrevSum); (void) PrevSum;
454 bool BranchProbabilityInfo::
455 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
456 // Hot probability is at least 4/5 = 80%
457 // FIXME: Compare against a static "hot" BranchProbability.
458 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
461 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
463 uint32_t MaxWeight = 0;
464 BasicBlock *MaxSucc = 0;
466 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
467 BasicBlock *Succ = *I;
468 uint32_t Weight = getEdgeWeight(BB, Succ);
469 uint32_t PrevSum = Sum;
472 assert(Sum > PrevSum); (void) PrevSum;
474 if (Weight > MaxWeight) {
480 // Hot probability is at least 4/5 = 80%
481 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
487 /// Get the raw edge weight for the edge. If can't find it, return
488 /// DEFAULT_WEIGHT value. Here an edge is specified using PredBlock and an index
489 /// to the successors.
490 uint32_t BranchProbabilityInfo::
491 getEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors) const {
492 DenseMap<Edge, uint32_t>::const_iterator I =
493 Weights.find(std::make_pair(Src, IndexInSuccessors));
495 if (I != Weights.end())
498 return DEFAULT_WEIGHT;
501 /// Get the raw edge weight calculated for the block pair. This returns the sum
502 /// of all raw edge weights from Src to Dst.
503 uint32_t BranchProbabilityInfo::
504 getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
506 DenseMap<Edge, uint32_t>::const_iterator MapI;
507 for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)
509 MapI = Weights.find(std::make_pair(Src, I.getSuccessorIndex()));
510 if (MapI != Weights.end())
511 Weight += MapI->second;
513 return (Weight == 0) ? DEFAULT_WEIGHT : Weight;
516 /// Set the edge weight for a given edge specified by PredBlock and an index
517 /// to the successors.
518 void BranchProbabilityInfo::
519 setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
521 Weights[std::make_pair(Src, IndexInSuccessors)] = Weight;
522 DEBUG(dbgs() << "set edge " << Src->getName() << " -> "
523 << IndexInSuccessors << " successor weight to "
527 /// Get an edge's probability, relative to other out-edges from Src.
528 BranchProbability BranchProbabilityInfo::
529 getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const {
530 uint32_t N = getEdgeWeight(Src, IndexInSuccessors);
531 uint32_t D = getSumForBlock(Src);
533 return BranchProbability(N, D);
536 /// Get the probability of going from Src to Dst. It returns the sum of all
537 /// probabilities for edges from Src to Dst.
538 BranchProbability BranchProbabilityInfo::
539 getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
541 uint32_t N = getEdgeWeight(Src, Dst);
542 uint32_t D = getSumForBlock(Src);
544 return BranchProbability(N, D);
548 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
549 const BasicBlock *Src,
550 const BasicBlock *Dst) const {
552 const BranchProbability Prob = getEdgeProbability(Src, Dst);
553 OS << "edge " << Src->getName() << " -> " << Dst->getName()
554 << " probability is " << Prob
555 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");