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/Instructions.h"
15 #include "llvm/Analysis/BranchProbabilityInfo.h"
19 INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob",
20 "Branch Probability Analysis", false, true)
21 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
22 INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob",
23 "Branch Probability Analysis", false, true)
25 char BranchProbabilityInfo::ID = 0;
28 // Please note that BranchProbabilityAnalysis is not a FunctionPass.
29 // It is created by BranchProbabilityInfo (which is a FunctionPass), which
30 // provides a clear interface. Thanks to that, all heuristics and other
31 // private methods are hidden in the .cpp file.
32 class BranchProbabilityAnalysis {
34 typedef std::pair<BasicBlock *, BasicBlock *> Edge;
36 DenseMap<Edge, unsigned> *Weights;
38 BranchProbabilityInfo *BP;
43 // Weights are for internal use only. They are used by heuristics to help to
44 // estimate edges' probability. Example:
46 // Using "Loop Branch Heuristics" we predict weights of edges for the
61 // Probability of the edge BB2->BB1 = 128 / (128 + 4) = 0.9696..
62 // Probability of the edge BB2->BB3 = 4 / (128 + 4) = 0.0303..
64 static const unsigned int LBH_TAKEN_WEIGHT = 128;
65 static const unsigned int LBH_NONTAKEN_WEIGHT = 4;
67 // Standard weight value. Used when none of the heuristics set weight for
69 static const unsigned int NORMAL_WEIGHT = 16;
71 // Minimum weight of an edge. Please note, that weight is NEVER 0.
72 static const unsigned int MIN_WEIGHT = 1;
74 // Return TRUE if BB leads directly to a Return Instruction.
75 static bool isReturningBlock(BasicBlock *BB) {
76 SmallPtrSet<BasicBlock *, 8> Visited;
79 TerminatorInst *TI = BB->getTerminator();
80 if (isa<ReturnInst>(TI))
83 if (TI->getNumSuccessors() > 1)
86 // It is unreachable block which we can consider as a return instruction.
87 if (TI->getNumSuccessors() == 0)
91 BB = TI->getSuccessor(0);
93 // Stop if cycle is detected.
94 if (Visited.count(BB))
101 // Multiply Edge Weight by two.
102 void incEdgeWeight(BasicBlock *Src, BasicBlock *Dst) {
103 unsigned Weight = BP->getEdgeWeight(Src, Dst);
104 unsigned MaxWeight = getMaxWeightFor(Src);
106 if (Weight * 2 > MaxWeight)
107 BP->setEdgeWeight(Src, Dst, MaxWeight);
109 BP->setEdgeWeight(Src, Dst, Weight * 2);
112 // Divide Edge Weight by two.
113 void decEdgeWeight(BasicBlock *Src, BasicBlock *Dst) {
114 unsigned Weight = BP->getEdgeWeight(Src, Dst);
117 if (Weight / 2 < MIN_WEIGHT)
118 BP->setEdgeWeight(Src, Dst, MIN_WEIGHT);
120 BP->setEdgeWeight(Src, Dst, Weight / 2);
124 unsigned getMaxWeightFor(BasicBlock *BB) const {
125 return UINT_MAX / BB->getTerminator()->getNumSuccessors();
129 BranchProbabilityAnalysis(DenseMap<Edge, unsigned> *W,
130 BranchProbabilityInfo *BP, LoopInfo *LI)
131 : Weights(W), BP(BP), LI(LI) {
135 void calcReturnHeuristics(BasicBlock *BB);
137 // Pointer Heuristics
138 void calcPointerHeuristics(BasicBlock *BB);
140 // Loop Branch Heuristics
141 void calcLoopBranchHeuristics(BasicBlock *BB);
143 bool runOnFunction(Function &F);
146 // Calculate Edge Weights using "Return Heuristics". Predict a successor which
147 // leads directly to Return Instruction will not be taken.
148 void BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
149 if (BB->getTerminator()->getNumSuccessors() == 1)
152 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
153 BasicBlock *Succ = *I;
154 if (isReturningBlock(Succ)) {
155 decEdgeWeight(BB, Succ);
160 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
161 // between two pointer or pointer and NULL will fail.
162 void BranchProbabilityAnalysis::calcPointerHeuristics(BasicBlock *BB) {
163 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
164 if (!BI || !BI->isConditional())
167 Value *Cond = BI->getCondition();
168 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
172 Value *LHS = CI->getOperand(0);
173 Value *RHS = CI->getOperand(1);
175 if (!LHS->getType()->isPointerTy())
178 assert(RHS->getType()->isPointerTy());
180 BasicBlock *Taken = BI->getSuccessor(0);
181 BasicBlock *NonTaken = BI->getSuccessor(1);
183 // p != 0 -> isProb = true
184 // p == 0 -> isProb = false
185 // p != q -> isProb = true
186 // p == q -> isProb = false;
187 bool isProb = !CI->isEquality();
189 std::swap(Taken, NonTaken);
191 incEdgeWeight(BB, Taken);
192 decEdgeWeight(BB, NonTaken);
195 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
196 // as taken, exiting edges as not-taken.
197 void BranchProbabilityAnalysis::calcLoopBranchHeuristics(BasicBlock *BB) {
198 unsigned numSuccs = BB->getTerminator()->getNumSuccessors();
200 Loop *L = LI->getLoopFor(BB);
204 SmallVector<BasicBlock *, 8> BackEdges;
205 SmallVector<BasicBlock *, 8> ExitingEdges;
207 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
208 BasicBlock *Succ = *I;
209 Loop *SuccL = LI->getLoopFor(Succ);
211 ExitingEdges.push_back(Succ);
212 else if (Succ == L->getHeader())
213 BackEdges.push_back(Succ);
216 if (unsigned numBackEdges = BackEdges.size()) {
217 unsigned backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
218 if (backWeight < NORMAL_WEIGHT)
219 backWeight = NORMAL_WEIGHT;
221 for (SmallVector<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
222 EE = BackEdges.end(); EI != EE; ++EI) {
223 BasicBlock *Back = *EI;
224 BP->setEdgeWeight(BB, Back, backWeight);
228 unsigned numExitingEdges = ExitingEdges.size();
229 if (unsigned numNonExitingEdges = numSuccs - numExitingEdges) {
230 unsigned exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
231 if (exitWeight < MIN_WEIGHT)
232 exitWeight = MIN_WEIGHT;
234 for (SmallVector<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
235 EE = ExitingEdges.end(); EI != EE; ++EI) {
236 BasicBlock *Exiting = *EI;
237 BP->setEdgeWeight(BB, Exiting, exitWeight);
242 bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
244 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
245 BasicBlock *BB = I++;
247 // Only LBH uses setEdgeWeight method.
248 calcLoopBranchHeuristics(BB);
250 // PH and RH use only incEdgeWeight and decEwdgeWeight methods to
251 // not efface LBH results.
252 calcPointerHeuristics(BB);
253 calcReturnHeuristics(BB);
260 bool BranchProbabilityInfo::runOnFunction(Function &F) {
261 LoopInfo &LI = getAnalysis<LoopInfo>();
262 BranchProbabilityAnalysis BPA(&Weights, this, &LI);
263 bool ret = BPA.runOnFunction(F);
267 // TODO: This currently hardcodes 80% as a fraction 4/5. We will soon add a
268 // BranchProbability class to encapsulate the fractional probability and
269 // define a few static instances of the class for use as predefined thresholds.
270 bool BranchProbabilityInfo::isEdgeHot(BasicBlock *Src, BasicBlock *Dst) const {
272 for (succ_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) {
273 BasicBlock *Succ = *I;
274 unsigned Weight = getEdgeWeight(Src, Succ);
275 unsigned PrevSum = Sum;
278 assert(Sum > PrevSum); (void) PrevSum;
281 return getEdgeWeight(Src, Dst) * 5 > Sum * 4;
284 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
286 unsigned MaxWeight = 0;
287 BasicBlock *MaxSucc = 0;
289 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
290 BasicBlock *Succ = *I;
291 unsigned Weight = getEdgeWeight(BB, Succ);
292 unsigned PrevSum = Sum;
295 assert(Sum > PrevSum); (void) PrevSum;
297 if (Weight > MaxWeight) {
303 if (MaxWeight * 5 > Sum * 4)
309 // Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
311 BranchProbabilityInfo::getEdgeWeight(BasicBlock *Src, BasicBlock *Dst) const {
313 DenseMap<Edge, unsigned>::const_iterator I = Weights.find(E);
315 if (I != Weights.end())
318 return DEFAULT_WEIGHT;
321 void BranchProbabilityInfo::setEdgeWeight(BasicBlock *Src, BasicBlock *Dst,
323 Weights[std::make_pair(Src, Dst)] = Weight;
324 DEBUG(dbgs() << "setEdgeWeight: " << Src->getNameStr() << " -> "
325 << Dst->getNameStr() << " to " << Weight
326 << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
330 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
331 BasicBlock *Dst) const {
334 for (succ_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) {
335 BasicBlock *Succ = *I;
336 unsigned Weight = getEdgeWeight(Src, Succ);
337 unsigned PrevSum = Sum;
340 assert(Sum > PrevSum); (void) PrevSum;
343 double Prob = (double)getEdgeWeight(Src, Dst) / Sum;
344 OS << "probability (" << Src->getNameStr() << " --> " << Dst->getNameStr()
345 << ") = " << Prob << "\n";