LLVM API Documentation

BranchProbabilityInfo.cpp
Go to the documentation of this file.
00001 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Loops should be simplified before this analysis.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Analysis/BranchProbabilityInfo.h"
00015 #include "llvm/ADT/PostOrderIterator.h"
00016 #include "llvm/Analysis/LoopInfo.h"
00017 #include "llvm/IR/Constants.h"
00018 #include "llvm/IR/Function.h"
00019 #include "llvm/IR/Instructions.h"
00020 #include "llvm/IR/LLVMContext.h"
00021 #include "llvm/IR/Metadata.h"
00022 #include "llvm/Support/CFG.h"
00023 #include "llvm/Support/Debug.h"
00024 
00025 using namespace llvm;
00026 
00027 INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
00028                       "Branch Probability Analysis", false, true)
00029 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
00030 INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
00031                     "Branch Probability Analysis", false, true)
00032 
00033 char BranchProbabilityInfo::ID = 0;
00034 
00035 // Weights are for internal use only. They are used by heuristics to help to
00036 // estimate edges' probability. Example:
00037 //
00038 // Using "Loop Branch Heuristics" we predict weights of edges for the
00039 // block BB2.
00040 //         ...
00041 //          |
00042 //          V
00043 //         BB1<-+
00044 //          |   |
00045 //          |   | (Weight = 124)
00046 //          V   |
00047 //         BB2--+
00048 //          |
00049 //          | (Weight = 4)
00050 //          V
00051 //         BB3
00052 //
00053 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
00054 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
00055 static const uint32_t LBH_TAKEN_WEIGHT = 124;
00056 static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
00057 
00058 /// \brief Unreachable-terminating branch taken weight.
00059 ///
00060 /// This is the weight for a branch being taken to a block that terminates
00061 /// (eventually) in unreachable. These are predicted as unlikely as possible.
00062 static const uint32_t UR_TAKEN_WEIGHT = 1;
00063 
00064 /// \brief Unreachable-terminating branch not-taken weight.
00065 ///
00066 /// This is the weight for a branch not being taken toward a block that
00067 /// terminates (eventually) in unreachable. Such a branch is essentially never
00068 /// taken. Set the weight to an absurdly high value so that nested loops don't
00069 /// easily subsume it.
00070 static const uint32_t UR_NONTAKEN_WEIGHT = 1024*1024 - 1;
00071 
00072 static const uint32_t PH_TAKEN_WEIGHT = 20;
00073 static const uint32_t PH_NONTAKEN_WEIGHT = 12;
00074 
00075 static const uint32_t ZH_TAKEN_WEIGHT = 20;
00076 static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
00077 
00078 static const uint32_t FPH_TAKEN_WEIGHT = 20;
00079 static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
00080 
00081 /// \brief Invoke-terminating normal branch taken weight
00082 ///
00083 /// This is the weight for branching to the normal destination of an invoke
00084 /// instruction. We expect this to happen most of the time. Set the weight to an
00085 /// absurdly high value so that nested loops subsume it.
00086 static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1;
00087 
00088 /// \brief Invoke-terminating normal branch not-taken weight.
00089 ///
00090 /// This is the weight for branching to the unwind destination of an invoke
00091 /// instruction. This is essentially never taken.
00092 static const uint32_t IH_NONTAKEN_WEIGHT = 1;
00093 
00094 // Standard weight value. Used when none of the heuristics set weight for
00095 // the edge.
00096 static const uint32_t NORMAL_WEIGHT = 16;
00097 
00098 // Minimum weight of an edge. Please note, that weight is NEVER 0.
00099 static const uint32_t MIN_WEIGHT = 1;
00100 
00101 static uint32_t getMaxWeightFor(BasicBlock *BB) {
00102   return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
00103 }
00104 
00105 
00106 /// \brief Calculate edge weights for successors lead to unreachable.
00107 ///
00108 /// Predict that a successor which leads necessarily to an
00109 /// unreachable-terminated block as extremely unlikely.
00110 bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) {
00111   TerminatorInst *TI = BB->getTerminator();
00112   if (TI->getNumSuccessors() == 0) {
00113     if (isa<UnreachableInst>(TI))
00114       PostDominatedByUnreachable.insert(BB);
00115     return false;
00116   }
00117 
00118   SmallVector<unsigned, 4> UnreachableEdges;
00119   SmallVector<unsigned, 4> ReachableEdges;
00120 
00121   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
00122     if (PostDominatedByUnreachable.count(*I))
00123       UnreachableEdges.push_back(I.getSuccessorIndex());
00124     else
00125       ReachableEdges.push_back(I.getSuccessorIndex());
00126   }
00127 
00128   // If all successors are in the set of blocks post-dominated by unreachable,
00129   // this block is too.
00130   if (UnreachableEdges.size() == TI->getNumSuccessors())
00131     PostDominatedByUnreachable.insert(BB);
00132 
00133   // Skip probabilities if this block has a single successor or if all were
00134   // reachable.
00135   if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
00136     return false;
00137 
00138   uint32_t UnreachableWeight =
00139     std::max(UR_TAKEN_WEIGHT / (unsigned)UnreachableEdges.size(), MIN_WEIGHT);
00140   for (SmallVector<unsigned, 4>::iterator I = UnreachableEdges.begin(),
00141                                           E = UnreachableEdges.end();
00142        I != E; ++I)
00143     setEdgeWeight(BB, *I, UnreachableWeight);
00144 
00145   if (ReachableEdges.empty())
00146     return true;
00147   uint32_t ReachableWeight =
00148     std::max(UR_NONTAKEN_WEIGHT / (unsigned)ReachableEdges.size(),
00149              NORMAL_WEIGHT);
00150   for (SmallVector<unsigned, 4>::iterator I = ReachableEdges.begin(),
00151                                           E = ReachableEdges.end();
00152        I != E; ++I)
00153     setEdgeWeight(BB, *I, ReachableWeight);
00154 
00155   return true;
00156 }
00157 
00158 // Propagate existing explicit probabilities from either profile data or
00159 // 'expect' intrinsic processing.
00160 bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
00161   TerminatorInst *TI = BB->getTerminator();
00162   if (TI->getNumSuccessors() == 1)
00163     return false;
00164   if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
00165     return false;
00166 
00167   MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
00168   if (!WeightsNode)
00169     return false;
00170 
00171   // Ensure there are weights for all of the successors. Note that the first
00172   // operand to the metadata node is a name, not a weight.
00173   if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
00174     return false;
00175 
00176   // Build up the final weights that will be used in a temporary buffer, but
00177   // don't add them until all weihts are present. Each weight value is clamped
00178   // to [1, getMaxWeightFor(BB)].
00179   uint32_t WeightLimit = getMaxWeightFor(BB);
00180   SmallVector<uint32_t, 2> Weights;
00181   Weights.reserve(TI->getNumSuccessors());
00182   for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
00183     ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
00184     if (!Weight)
00185       return false;
00186     Weights.push_back(
00187       std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
00188   }
00189   assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
00190   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
00191     setEdgeWeight(BB, i, Weights[i]);
00192 
00193   return true;
00194 }
00195 
00196 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
00197 // between two pointer or pointer and NULL will fail.
00198 bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
00199   BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
00200   if (!BI || !BI->isConditional())
00201     return false;
00202 
00203   Value *Cond = BI->getCondition();
00204   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
00205   if (!CI || !CI->isEquality())
00206     return false;
00207 
00208   Value *LHS = CI->getOperand(0);
00209 
00210   if (!LHS->getType()->isPointerTy())
00211     return false;
00212 
00213   assert(CI->getOperand(1)->getType()->isPointerTy());
00214 
00215   // p != 0   ->   isProb = true
00216   // p == 0   ->   isProb = false
00217   // p != q   ->   isProb = true
00218   // p == q   ->   isProb = false;
00219   unsigned TakenIdx = 0, NonTakenIdx = 1;
00220   bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
00221   if (!isProb)
00222     std::swap(TakenIdx, NonTakenIdx);
00223 
00224   setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT);
00225   setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT);
00226   return true;
00227 }
00228 
00229 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
00230 // as taken, exiting edges as not-taken.
00231 bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) {
00232   Loop *L = LI->getLoopFor(BB);
00233   if (!L)
00234     return false;
00235 
00236   SmallVector<unsigned, 8> BackEdges;
00237   SmallVector<unsigned, 8> ExitingEdges;
00238   SmallVector<unsigned, 8> InEdges; // Edges from header to the loop.
00239 
00240   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
00241     if (!L->contains(*I))
00242       ExitingEdges.push_back(I.getSuccessorIndex());
00243     else if (L->getHeader() == *I)
00244       BackEdges.push_back(I.getSuccessorIndex());
00245     else
00246       InEdges.push_back(I.getSuccessorIndex());
00247   }
00248 
00249   if (uint32_t numBackEdges = BackEdges.size()) {
00250     uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
00251     if (backWeight < NORMAL_WEIGHT)
00252       backWeight = NORMAL_WEIGHT;
00253 
00254     for (SmallVector<unsigned, 8>::iterator EI = BackEdges.begin(),
00255          EE = BackEdges.end(); EI != EE; ++EI) {
00256       setEdgeWeight(BB, *EI, backWeight);
00257     }
00258   }
00259 
00260   if (uint32_t numInEdges = InEdges.size()) {
00261     uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
00262     if (inWeight < NORMAL_WEIGHT)
00263       inWeight = NORMAL_WEIGHT;
00264 
00265     for (SmallVector<unsigned, 8>::iterator EI = InEdges.begin(),
00266          EE = InEdges.end(); EI != EE; ++EI) {
00267       setEdgeWeight(BB, *EI, inWeight);
00268     }
00269   }
00270 
00271   if (uint32_t numExitingEdges = ExitingEdges.size()) {
00272     uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;
00273     if (exitWeight < MIN_WEIGHT)
00274       exitWeight = MIN_WEIGHT;
00275 
00276     for (SmallVector<unsigned, 8>::iterator EI = ExitingEdges.begin(),
00277          EE = ExitingEdges.end(); EI != EE; ++EI) {
00278       setEdgeWeight(BB, *EI, exitWeight);
00279     }
00280   }
00281 
00282   return true;
00283 }
00284 
00285 bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
00286   BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
00287   if (!BI || !BI->isConditional())
00288     return false;
00289 
00290   Value *Cond = BI->getCondition();
00291   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
00292   if (!CI)
00293     return false;
00294 
00295   Value *RHS = CI->getOperand(1);
00296   ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
00297   if (!CV)
00298     return false;
00299 
00300   bool isProb;
00301   if (CV->isZero()) {
00302     switch (CI->getPredicate()) {
00303     case CmpInst::ICMP_EQ:
00304       // X == 0   ->  Unlikely
00305       isProb = false;
00306       break;
00307     case CmpInst::ICMP_NE:
00308       // X != 0   ->  Likely
00309       isProb = true;
00310       break;
00311     case CmpInst::ICMP_SLT:
00312       // X < 0   ->  Unlikely
00313       isProb = false;
00314       break;
00315     case CmpInst::ICMP_SGT:
00316       // X > 0   ->  Likely
00317       isProb = true;
00318       break;
00319     default:
00320       return false;
00321     }
00322   } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
00323     // InstCombine canonicalizes X <= 0 into X < 1.
00324     // X <= 0   ->  Unlikely
00325     isProb = false;
00326   } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
00327     // InstCombine canonicalizes X >= 0 into X > -1.
00328     // X >= 0   ->  Likely
00329     isProb = true;
00330   } else {
00331     return false;
00332   }
00333 
00334   unsigned TakenIdx = 0, NonTakenIdx = 1;
00335 
00336   if (!isProb)
00337     std::swap(TakenIdx, NonTakenIdx);
00338 
00339   setEdgeWeight(BB, TakenIdx, ZH_TAKEN_WEIGHT);
00340   setEdgeWeight(BB, NonTakenIdx, ZH_NONTAKEN_WEIGHT);
00341 
00342   return true;
00343 }
00344 
00345 bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
00346   BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
00347   if (!BI || !BI->isConditional())
00348     return false;
00349 
00350   Value *Cond = BI->getCondition();
00351   FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
00352   if (!FCmp)
00353     return false;
00354 
00355   bool isProb;
00356   if (FCmp->isEquality()) {
00357     // f1 == f2 -> Unlikely
00358     // f1 != f2 -> Likely
00359     isProb = !FCmp->isTrueWhenEqual();
00360   } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
00361     // !isnan -> Likely
00362     isProb = true;
00363   } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
00364     // isnan -> Unlikely
00365     isProb = false;
00366   } else {
00367     return false;
00368   }
00369 
00370   unsigned TakenIdx = 0, NonTakenIdx = 1;
00371 
00372   if (!isProb)
00373     std::swap(TakenIdx, NonTakenIdx);
00374 
00375   setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT);
00376   setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT);
00377 
00378   return true;
00379 }
00380 
00381 bool BranchProbabilityInfo::calcInvokeHeuristics(BasicBlock *BB) {
00382   InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator());
00383   if (!II)
00384     return false;
00385 
00386   setEdgeWeight(BB, 0/*Index for Normal*/, IH_TAKEN_WEIGHT);
00387   setEdgeWeight(BB, 1/*Index for Unwind*/, IH_NONTAKEN_WEIGHT);
00388   return true;
00389 }
00390 
00391 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
00392   AU.addRequired<LoopInfo>();
00393   AU.setPreservesAll();
00394 }
00395 
00396 bool BranchProbabilityInfo::runOnFunction(Function &F) {
00397   LastF = &F; // Store the last function we ran on for printing.
00398   LI = &getAnalysis<LoopInfo>();
00399   assert(PostDominatedByUnreachable.empty());
00400 
00401   // Walk the basic blocks in post-order so that we can build up state about
00402   // the successors of a block iteratively.
00403   for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()),
00404                                  E = po_end(&F.getEntryBlock());
00405        I != E; ++I) {
00406     DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
00407     if (calcUnreachableHeuristics(*I))
00408       continue;
00409     if (calcMetadataWeights(*I))
00410       continue;
00411     if (calcLoopBranchHeuristics(*I))
00412       continue;
00413     if (calcPointerHeuristics(*I))
00414       continue;
00415     if (calcZeroHeuristics(*I))
00416       continue;
00417     if (calcFloatingPointHeuristics(*I))
00418       continue;
00419     calcInvokeHeuristics(*I);
00420   }
00421 
00422   PostDominatedByUnreachable.clear();
00423   return false;
00424 }
00425 
00426 void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
00427   OS << "---- Branch Probabilities ----\n";
00428   // We print the probabilities from the last function the analysis ran over,
00429   // or the function it is currently running over.
00430   assert(LastF && "Cannot print prior to running over a function");
00431   for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
00432        BI != BE; ++BI) {
00433     for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
00434          SI != SE; ++SI) {
00435       printEdgeProbability(OS << "  ", BI, *SI);
00436     }
00437   }
00438 }
00439 
00440 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
00441   uint32_t Sum = 0;
00442 
00443   for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
00444     uint32_t Weight = getEdgeWeight(BB, I.getSuccessorIndex());
00445     uint32_t PrevSum = Sum;
00446 
00447     Sum += Weight;
00448     assert(Sum > PrevSum); (void) PrevSum;
00449   }
00450 
00451   return Sum;
00452 }
00453 
00454 bool BranchProbabilityInfo::
00455 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
00456   // Hot probability is at least 4/5 = 80%
00457   // FIXME: Compare against a static "hot" BranchProbability.
00458   return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
00459 }
00460 
00461 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
00462   uint32_t Sum = 0;
00463   uint32_t MaxWeight = 0;
00464   BasicBlock *MaxSucc = 0;
00465 
00466   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
00467     BasicBlock *Succ = *I;
00468     uint32_t Weight = getEdgeWeight(BB, Succ);
00469     uint32_t PrevSum = Sum;
00470 
00471     Sum += Weight;
00472     assert(Sum > PrevSum); (void) PrevSum;
00473 
00474     if (Weight > MaxWeight) {
00475       MaxWeight = Weight;
00476       MaxSucc = Succ;
00477     }
00478   }
00479 
00480   // Hot probability is at least 4/5 = 80%
00481   if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
00482     return MaxSucc;
00483 
00484   return 0;
00485 }
00486 
00487 /// Get the raw edge weight for the edge. If can't find it, return
00488 /// DEFAULT_WEIGHT value. Here an edge is specified using PredBlock and an index
00489 /// to the successors.
00490 uint32_t BranchProbabilityInfo::
00491 getEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors) const {
00492   DenseMap<Edge, uint32_t>::const_iterator I =
00493       Weights.find(std::make_pair(Src, IndexInSuccessors));
00494 
00495   if (I != Weights.end())
00496     return I->second;
00497 
00498   return DEFAULT_WEIGHT;
00499 }
00500 
00501 /// Get the raw edge weight calculated for the block pair. This returns the sum
00502 /// of all raw edge weights from Src to Dst.
00503 uint32_t BranchProbabilityInfo::
00504 getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
00505   uint32_t Weight = 0;
00506   DenseMap<Edge, uint32_t>::const_iterator MapI;
00507   for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)
00508     if (*I == Dst) {
00509       MapI = Weights.find(std::make_pair(Src, I.getSuccessorIndex()));
00510       if (MapI != Weights.end())
00511         Weight += MapI->second;
00512     }
00513   return (Weight == 0) ? DEFAULT_WEIGHT : Weight;
00514 }
00515 
00516 /// Set the edge weight for a given edge specified by PredBlock and an index
00517 /// to the successors.
00518 void BranchProbabilityInfo::
00519 setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
00520               uint32_t Weight) {
00521   Weights[std::make_pair(Src, IndexInSuccessors)] = Weight;
00522   DEBUG(dbgs() << "set edge " << Src->getName() << " -> "
00523                << IndexInSuccessors << " successor weight to "
00524                << Weight << "\n");
00525 }
00526 
00527 /// Get an edge's probability, relative to other out-edges from Src.
00528 BranchProbability BranchProbabilityInfo::
00529 getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const {
00530   uint32_t N = getEdgeWeight(Src, IndexInSuccessors);
00531   uint32_t D = getSumForBlock(Src);
00532 
00533   return BranchProbability(N, D);
00534 }
00535 
00536 /// Get the probability of going from Src to Dst. It returns the sum of all
00537 /// probabilities for edges from Src to Dst.
00538 BranchProbability BranchProbabilityInfo::
00539 getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
00540 
00541   uint32_t N = getEdgeWeight(Src, Dst);
00542   uint32_t D = getSumForBlock(Src);
00543 
00544   return BranchProbability(N, D);
00545 }
00546 
00547 raw_ostream &
00548 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
00549                                             const BasicBlock *Src,
00550                                             const BasicBlock *Dst) const {
00551 
00552   const BranchProbability Prob = getEdgeProbability(Src, Dst);
00553   OS << "edge " << Src->getName() << " -> " << Dst->getName()
00554      << " probability is " << Prob
00555      << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
00556 
00557   return OS;
00558 }