1 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -*- C++ -*-===//
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 // Standard weight value. Used when none of the heuristics set weight for
83 static const uint32_t NORMAL_WEIGHT = 16;
85 // Minimum weight of an edge. Please note, that weight is NEVER 0.
86 static const uint32_t MIN_WEIGHT = 1;
88 static uint32_t getMaxWeightFor(BasicBlock *BB) {
89 return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
93 /// \brief Calculate edge weights for successors lead to unreachable.
95 /// Predict that a successor which leads necessarily to an
96 /// unreachable-terminated block as extremely unlikely.
97 bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) {
98 TerminatorInst *TI = BB->getTerminator();
99 if (TI->getNumSuccessors() == 0) {
100 if (isa<UnreachableInst>(TI))
101 PostDominatedByUnreachable.insert(BB);
105 SmallPtrSet<BasicBlock *, 4> UnreachableEdges;
106 SmallPtrSet<BasicBlock *, 4> ReachableEdges;
108 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
109 if (PostDominatedByUnreachable.count(*I))
110 UnreachableEdges.insert(*I);
112 ReachableEdges.insert(*I);
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);
120 // Skip probabilities if this block has a single successor or if all were
122 if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
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();
130 setEdgeWeight(BB, *I, UnreachableWeight);
132 if (ReachableEdges.empty())
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();
139 setEdgeWeight(BB, *I, ReachableWeight);
144 // Propagate existing explicit probabilities from either profile data or
145 // 'expect' intrinsic processing.
146 bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
147 TerminatorInst *TI = BB->getTerminator();
148 if (TI->getNumSuccessors() == 1)
150 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
153 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
157 // 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)
162 // 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)].
165 uint32_t WeightLimit = getMaxWeightFor(BB);
166 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));
173 std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
175 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
176 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
177 setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
182 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
183 // between two pointer or pointer and NULL will fail.
184 bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
185 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
186 if (!BI || !BI->isConditional())
189 Value *Cond = BI->getCondition();
190 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
191 if (!CI || !CI->isEquality())
194 Value *LHS = CI->getOperand(0);
196 if (!LHS->getType()->isPointerTy())
199 assert(CI->getOperand(1)->getType()->isPointerTy());
201 BasicBlock *Taken = BI->getSuccessor(0);
202 BasicBlock *NonTaken = BI->getSuccessor(1);
204 // p != 0 -> isProb = true
205 // p == 0 -> isProb = false
206 // p != q -> isProb = true
207 // p == q -> isProb = false;
208 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
210 std::swap(Taken, NonTaken);
212 setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
213 setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
217 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
218 // as taken, exiting edges as not-taken.
219 bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) {
220 Loop *L = LI->getLoopFor(BB);
224 SmallPtrSet<BasicBlock *, 8> BackEdges;
225 SmallPtrSet<BasicBlock *, 8> ExitingEdges;
226 SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
228 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
229 if (!L->contains(*I))
230 ExitingEdges.insert(*I);
231 else if (L->getHeader() == *I)
232 BackEdges.insert(*I);
237 if (uint32_t numBackEdges = BackEdges.size()) {
238 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
239 if (backWeight < NORMAL_WEIGHT)
240 backWeight = NORMAL_WEIGHT;
242 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
243 EE = BackEdges.end(); EI != EE; ++EI) {
244 BasicBlock *Back = *EI;
245 setEdgeWeight(BB, Back, backWeight);
249 if (uint32_t numInEdges = InEdges.size()) {
250 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
251 if (inWeight < NORMAL_WEIGHT)
252 inWeight = NORMAL_WEIGHT;
254 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
255 EE = InEdges.end(); EI != EE; ++EI) {
256 BasicBlock *Back = *EI;
257 setEdgeWeight(BB, Back, inWeight);
261 if (uint32_t numExitingEdges = ExitingEdges.size()) {
262 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;
263 if (exitWeight < MIN_WEIGHT)
264 exitWeight = MIN_WEIGHT;
266 for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
267 EE = ExitingEdges.end(); EI != EE; ++EI) {
268 BasicBlock *Exiting = *EI;
269 setEdgeWeight(BB, Exiting, exitWeight);
276 bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
277 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
278 if (!BI || !BI->isConditional())
281 Value *Cond = BI->getCondition();
282 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
286 Value *RHS = CI->getOperand(1);
287 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
293 switch (CI->getPredicate()) {
294 case CmpInst::ICMP_EQ:
295 // X == 0 -> Unlikely
298 case CmpInst::ICMP_NE:
302 case CmpInst::ICMP_SLT:
306 case CmpInst::ICMP_SGT:
313 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
314 // InstCombine canonicalizes X <= 0 into X < 1.
315 // X <= 0 -> Unlikely
317 } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
318 // InstCombine canonicalizes X >= 0 into X > -1.
325 BasicBlock *Taken = BI->getSuccessor(0);
326 BasicBlock *NonTaken = BI->getSuccessor(1);
329 std::swap(Taken, NonTaken);
331 setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
332 setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
337 bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
338 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
339 if (!BI || !BI->isConditional())
342 Value *Cond = BI->getCondition();
343 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
348 if (FCmp->isEquality()) {
349 // f1 == f2 -> Unlikely
350 // f1 != f2 -> Likely
351 isProb = !FCmp->isTrueWhenEqual();
352 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
355 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
362 BasicBlock *Taken = BI->getSuccessor(0);
363 BasicBlock *NonTaken = BI->getSuccessor(1);
366 std::swap(Taken, NonTaken);
368 setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
369 setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
374 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
375 AU.addRequired<LoopInfo>();
376 AU.setPreservesAll();
379 bool BranchProbabilityInfo::runOnFunction(Function &F) {
380 LastF = &F; // Store the last function we ran on for printing.
381 LI = &getAnalysis<LoopInfo>();
382 assert(PostDominatedByUnreachable.empty());
384 // 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());
389 DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
390 if (calcUnreachableHeuristics(*I))
392 if (calcMetadataWeights(*I))
394 if (calcLoopBranchHeuristics(*I))
396 if (calcPointerHeuristics(*I))
398 if (calcZeroHeuristics(*I))
400 calcFloatingPointHeuristics(*I);
403 PostDominatedByUnreachable.clear();
407 void 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();
414 for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
416 printEdgeProbability(OS << " ", BI, *SI);
421 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
424 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
425 const BasicBlock *Succ = *I;
426 uint32_t Weight = getEdgeWeight(BB, Succ);
427 uint32_t PrevSum = Sum;
430 assert(Sum > PrevSum); (void) PrevSum;
436 bool BranchProbabilityInfo::
437 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
438 // Hot probability is at least 4/5 = 80%
439 // FIXME: Compare against a static "hot" BranchProbability.
440 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
443 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
445 uint32_t MaxWeight = 0;
446 BasicBlock *MaxSucc = 0;
448 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
449 BasicBlock *Succ = *I;
450 uint32_t Weight = getEdgeWeight(BB, Succ);
451 uint32_t PrevSum = Sum;
454 assert(Sum > PrevSum); (void) PrevSum;
456 if (Weight > MaxWeight) {
462 // Hot probability is at least 4/5 = 80%
463 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
469 // Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
470 uint32_t BranchProbabilityInfo::
471 getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
473 DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
475 if (I != Weights.end())
478 return DEFAULT_WEIGHT;
481 void BranchProbabilityInfo::
482 setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
483 Weights[std::make_pair(Src, Dst)] = Weight;
484 DEBUG(dbgs() << "set edge " << Src->getName() << " -> "
485 << Dst->getName() << " weight to " << Weight
486 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
490 BranchProbability BranchProbabilityInfo::
491 getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
493 uint32_t N = getEdgeWeight(Src, Dst);
494 uint32_t D = getSumForBlock(Src);
496 return BranchProbability(N, D);
500 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
501 const BasicBlock *Src,
502 const BasicBlock *Dst) const {
504 const BranchProbability Prob = getEdgeProbability(Src, Dst);
505 OS << "edge " << Src->getName() << " -> " << Dst->getName()
506 << " probability is " << Prob
507 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");