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/Constants.h"
15 #include "llvm/Function.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/LLVMContext.h"
18 #include "llvm/Metadata.h"
19 #include "llvm/Analysis/BranchProbabilityInfo.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/ADT/PostOrderIterator.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 SmallPtrSet<BasicBlock *, 4> UnreachableEdges;
119 SmallPtrSet<BasicBlock *, 4> ReachableEdges;
121 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
122 if (PostDominatedByUnreachable.count(*I))
123 UnreachableEdges.insert(*I);
125 ReachableEdges.insert(*I);
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 / UnreachableEdges.size(), MIN_WEIGHT);
140 for (SmallPtrSet<BasicBlock *, 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 / ReachableEdges.size(), NORMAL_WEIGHT);
149 for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReachableEdges.begin(),
150 E = ReachableEdges.end();
152 setEdgeWeight(BB, *I, ReachableWeight);
157 // Propagate existing explicit probabilities from either profile data or
158 // 'expect' intrinsic processing.
159 bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
160 TerminatorInst *TI = BB->getTerminator();
161 if (TI->getNumSuccessors() == 1)
163 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
166 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
170 // Ensure there are weights for all of the successors. Note that the first
171 // operand to the metadata node is a name, not a weight.
172 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
175 // Build up the final weights that will be used in a temporary buffer, but
176 // don't add them until all weihts are present. Each weight value is clamped
177 // to [1, getMaxWeightFor(BB)].
178 uint32_t WeightLimit = getMaxWeightFor(BB);
179 SmallVector<uint32_t, 2> Weights;
180 Weights.reserve(TI->getNumSuccessors());
181 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
182 ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
186 std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
188 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
189 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
190 setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
195 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
196 // between two pointer or pointer and NULL will fail.
197 bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
198 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
199 if (!BI || !BI->isConditional())
202 Value *Cond = BI->getCondition();
203 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
204 if (!CI || !CI->isEquality())
207 Value *LHS = CI->getOperand(0);
209 if (!LHS->getType()->isPointerTy())
212 assert(CI->getOperand(1)->getType()->isPointerTy());
214 BasicBlock *Taken = BI->getSuccessor(0);
215 BasicBlock *NonTaken = BI->getSuccessor(1);
217 // p != 0 -> isProb = true
218 // p == 0 -> isProb = false
219 // p != q -> isProb = true
220 // p == q -> isProb = false;
221 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
223 std::swap(Taken, NonTaken);
225 setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
226 setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
230 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
231 // as taken, exiting edges as not-taken.
232 bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) {
233 Loop *L = LI->getLoopFor(BB);
237 SmallPtrSet<BasicBlock *, 8> BackEdges;
238 SmallPtrSet<BasicBlock *, 8> ExitingEdges;
239 SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
241 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
242 if (!L->contains(*I))
243 ExitingEdges.insert(*I);
244 else if (L->getHeader() == *I)
245 BackEdges.insert(*I);
250 if (uint32_t numBackEdges = BackEdges.size()) {
251 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
252 if (backWeight < NORMAL_WEIGHT)
253 backWeight = NORMAL_WEIGHT;
255 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
256 EE = BackEdges.end(); EI != EE; ++EI) {
257 BasicBlock *Back = *EI;
258 setEdgeWeight(BB, Back, backWeight);
262 if (uint32_t numInEdges = InEdges.size()) {
263 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
264 if (inWeight < NORMAL_WEIGHT)
265 inWeight = NORMAL_WEIGHT;
267 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
268 EE = InEdges.end(); EI != EE; ++EI) {
269 BasicBlock *Back = *EI;
270 setEdgeWeight(BB, Back, inWeight);
274 if (uint32_t numExitingEdges = ExitingEdges.size()) {
275 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;
276 if (exitWeight < MIN_WEIGHT)
277 exitWeight = MIN_WEIGHT;
279 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
280 EE = ExitingEdges.end(); EI != EE; ++EI) {
281 BasicBlock *Exiting = *EI;
282 setEdgeWeight(BB, Exiting, exitWeight);
289 bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
290 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
291 if (!BI || !BI->isConditional())
294 Value *Cond = BI->getCondition();
295 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
299 Value *RHS = CI->getOperand(1);
300 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
306 switch (CI->getPredicate()) {
307 case CmpInst::ICMP_EQ:
308 // X == 0 -> Unlikely
311 case CmpInst::ICMP_NE:
315 case CmpInst::ICMP_SLT:
319 case CmpInst::ICMP_SGT:
326 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
327 // InstCombine canonicalizes X <= 0 into X < 1.
328 // X <= 0 -> Unlikely
330 } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
331 // InstCombine canonicalizes X >= 0 into X > -1.
338 BasicBlock *Taken = BI->getSuccessor(0);
339 BasicBlock *NonTaken = BI->getSuccessor(1);
342 std::swap(Taken, NonTaken);
344 setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
345 setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
350 bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
351 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
352 if (!BI || !BI->isConditional())
355 Value *Cond = BI->getCondition();
356 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
361 if (FCmp->isEquality()) {
362 // f1 == f2 -> Unlikely
363 // f1 != f2 -> Likely
364 isProb = !FCmp->isTrueWhenEqual();
365 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
368 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
375 BasicBlock *Taken = BI->getSuccessor(0);
376 BasicBlock *NonTaken = BI->getSuccessor(1);
379 std::swap(Taken, NonTaken);
381 setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
382 setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
387 bool BranchProbabilityInfo::calcInvokeHeuristics(BasicBlock *BB) {
388 InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator());
392 BasicBlock *Normal = II->getNormalDest();
393 BasicBlock *Unwind = II->getUnwindDest();
395 setEdgeWeight(BB, Normal, IH_TAKEN_WEIGHT);
396 setEdgeWeight(BB, Unwind, IH_NONTAKEN_WEIGHT);
400 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
401 AU.addRequired<LoopInfo>();
402 AU.setPreservesAll();
405 bool BranchProbabilityInfo::runOnFunction(Function &F) {
406 LastF = &F; // Store the last function we ran on for printing.
407 LI = &getAnalysis<LoopInfo>();
408 assert(PostDominatedByUnreachable.empty());
410 // Walk the basic blocks in post-order so that we can build up state about
411 // the successors of a block iteratively.
412 for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()),
413 E = po_end(&F.getEntryBlock());
415 DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
416 if (calcUnreachableHeuristics(*I))
418 if (calcMetadataWeights(*I))
420 if (calcLoopBranchHeuristics(*I))
422 if (calcPointerHeuristics(*I))
424 if (calcZeroHeuristics(*I))
426 if (calcFloatingPointHeuristics(*I))
428 calcInvokeHeuristics(*I);
431 PostDominatedByUnreachable.clear();
435 void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
436 OS << "---- Branch Probabilities ----\n";
437 // We print the probabilities from the last function the analysis ran over,
438 // or the function it is currently running over.
439 assert(LastF && "Cannot print prior to running over a function");
440 for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
442 for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
444 printEdgeProbability(OS << " ", BI, *SI);
449 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
452 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
453 const BasicBlock *Succ = *I;
454 uint32_t Weight = getEdgeWeight(BB, Succ);
455 uint32_t PrevSum = Sum;
458 assert(Sum > PrevSum); (void) PrevSum;
464 bool BranchProbabilityInfo::
465 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
466 // Hot probability is at least 4/5 = 80%
467 // FIXME: Compare against a static "hot" BranchProbability.
468 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
471 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
473 uint32_t MaxWeight = 0;
474 BasicBlock *MaxSucc = 0;
476 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
477 BasicBlock *Succ = *I;
478 uint32_t Weight = getEdgeWeight(BB, Succ);
479 uint32_t PrevSum = Sum;
482 assert(Sum > PrevSum); (void) PrevSum;
484 if (Weight > MaxWeight) {
490 // Hot probability is at least 4/5 = 80%
491 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
497 // Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
498 uint32_t BranchProbabilityInfo::
499 getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
501 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
503 if (I != Weights.end())
506 return DEFAULT_WEIGHT;
509 void BranchProbabilityInfo::
510 setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
511 Weights[std::make_pair(Src, Dst)] = Weight;
512 DEBUG(dbgs() << "set edge " << Src->getName() << " -> "
513 << Dst->getName() << " weight to " << Weight
514 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
518 BranchProbability BranchProbabilityInfo::
519 getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
521 uint32_t N = getEdgeWeight(Src, Dst);
522 uint32_t D = getSumForBlock(Src);
524 return BranchProbability(N, D);
528 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
529 const BasicBlock *Src,
530 const BasicBlock *Dst) const {
532 const BranchProbability Prob = getEdgeProbability(Src, Dst);
533 OS << "edge " << Src->getName() << " -> " << Dst->getName()
534 << " probability is " << Prob
535 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");