Set the branch probability of branching to the 'normal' destination of an invoke
[oota-llvm.git] / lib / Analysis / BranchProbabilityInfo.cpp
1 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Loops should be simplified before this analysis.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
24
25 using namespace llvm;
26
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)
32
33 char BranchProbabilityInfo::ID = 0;
34
35 // Weights are for internal use only. They are used by heuristics to help to
36 // estimate edges' probability. Example:
37 //
38 // Using "Loop Branch Heuristics" we predict weights of edges for the
39 // block BB2.
40 //         ...
41 //          |
42 //          V
43 //         BB1<-+
44 //          |   |
45 //          |   | (Weight = 124)
46 //          V   |
47 //         BB2--+
48 //          |
49 //          | (Weight = 4)
50 //          V
51 //         BB3
52 //
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;
57
58 /// \brief Unreachable-terminating branch taken weight.
59 ///
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;
63
64 /// \brief Unreachable-terminating branch not-taken weight.
65 ///
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;
71
72 static const uint32_t PH_TAKEN_WEIGHT = 20;
73 static const uint32_t PH_NONTAKEN_WEIGHT = 12;
74
75 static const uint32_t ZH_TAKEN_WEIGHT = 20;
76 static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
77
78 static const uint32_t FPH_TAKEN_WEIGHT = 20;
79 static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
80
81 /// \brief Invoke-terminating normal branch taken weight
82 ///
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;
87
88 /// \brief Invoke-terminating normal branch not-taken weight.
89 ///
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;
93
94 // Standard weight value. Used when none of the heuristics set weight for
95 // the edge.
96 static const uint32_t NORMAL_WEIGHT = 16;
97
98 // Minimum weight of an edge. Please note, that weight is NEVER 0.
99 static const uint32_t MIN_WEIGHT = 1;
100
101 static uint32_t getMaxWeightFor(BasicBlock *BB) {
102   return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
103 }
104
105
106 /// \brief Calculate edge weights for successors lead to unreachable.
107 ///
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);
115     return false;
116   }
117
118   SmallPtrSet<BasicBlock *, 4> UnreachableEdges;
119   SmallPtrSet<BasicBlock *, 4> ReachableEdges;
120
121   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
122     if (PostDominatedByUnreachable.count(*I))
123       UnreachableEdges.insert(*I);
124     else
125       ReachableEdges.insert(*I);
126   }
127
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);
132
133   // Skip probabilities if this block has a single successor or if all were
134   // reachable.
135   if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
136     return false;
137
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();
142        I != E; ++I)
143     setEdgeWeight(BB, *I, UnreachableWeight);
144
145   if (ReachableEdges.empty())
146     return true;
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();
151        I != E; ++I)
152     setEdgeWeight(BB, *I, ReachableWeight);
153
154   return true;
155 }
156
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)
162     return false;
163   if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
164     return false;
165
166   MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
167   if (!WeightsNode)
168     return false;
169
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)
173     return false;
174
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));
183     if (!Weight)
184       return false;
185     Weights.push_back(
186       std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
187   }
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]);
191
192   return true;
193 }
194
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())
200     return false;
201
202   Value *Cond = BI->getCondition();
203   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
204   if (!CI || !CI->isEquality())
205     return false;
206
207   Value *LHS = CI->getOperand(0);
208
209   if (!LHS->getType()->isPointerTy())
210     return false;
211
212   assert(CI->getOperand(1)->getType()->isPointerTy());
213
214   BasicBlock *Taken = BI->getSuccessor(0);
215   BasicBlock *NonTaken = BI->getSuccessor(1);
216
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;
222   if (!isProb)
223     std::swap(Taken, NonTaken);
224
225   setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
226   setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
227   return true;
228 }
229
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);
234   if (!L)
235     return false;
236
237   SmallPtrSet<BasicBlock *, 8> BackEdges;
238   SmallPtrSet<BasicBlock *, 8> ExitingEdges;
239   SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
240
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);
246     else
247       InEdges.insert(*I);
248   }
249
250   if (uint32_t numBackEdges = BackEdges.size()) {
251     uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
252     if (backWeight < NORMAL_WEIGHT)
253       backWeight = NORMAL_WEIGHT;
254
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);
259     }
260   }
261
262   if (uint32_t numInEdges = InEdges.size()) {
263     uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
264     if (inWeight < NORMAL_WEIGHT)
265       inWeight = NORMAL_WEIGHT;
266
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);
271     }
272   }
273
274   if (uint32_t numExitingEdges = ExitingEdges.size()) {
275     uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;
276     if (exitWeight < MIN_WEIGHT)
277       exitWeight = MIN_WEIGHT;
278
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);
283     }
284   }
285
286   return true;
287 }
288
289 bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
290   BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
291   if (!BI || !BI->isConditional())
292     return false;
293
294   Value *Cond = BI->getCondition();
295   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
296   if (!CI)
297     return false;
298
299   Value *RHS = CI->getOperand(1);
300   ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
301   if (!CV)
302     return false;
303
304   bool isProb;
305   if (CV->isZero()) {
306     switch (CI->getPredicate()) {
307     case CmpInst::ICMP_EQ:
308       // X == 0   ->  Unlikely
309       isProb = false;
310       break;
311     case CmpInst::ICMP_NE:
312       // X != 0   ->  Likely
313       isProb = true;
314       break;
315     case CmpInst::ICMP_SLT:
316       // X < 0   ->  Unlikely
317       isProb = false;
318       break;
319     case CmpInst::ICMP_SGT:
320       // X > 0   ->  Likely
321       isProb = true;
322       break;
323     default:
324       return false;
325     }
326   } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
327     // InstCombine canonicalizes X <= 0 into X < 1.
328     // X <= 0   ->  Unlikely
329     isProb = false;
330   } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
331     // InstCombine canonicalizes X >= 0 into X > -1.
332     // X >= 0   ->  Likely
333     isProb = true;
334   } else {
335     return false;
336   }
337
338   BasicBlock *Taken = BI->getSuccessor(0);
339   BasicBlock *NonTaken = BI->getSuccessor(1);
340
341   if (!isProb)
342     std::swap(Taken, NonTaken);
343
344   setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
345   setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
346
347   return true;
348 }
349
350 bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
351   BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
352   if (!BI || !BI->isConditional())
353     return false;
354
355   Value *Cond = BI->getCondition();
356   FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
357   if (!FCmp)
358     return false;
359
360   bool isProb;
361   if (FCmp->isEquality()) {
362     // f1 == f2 -> Unlikely
363     // f1 != f2 -> Likely
364     isProb = !FCmp->isTrueWhenEqual();
365   } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
366     // !isnan -> Likely
367     isProb = true;
368   } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
369     // isnan -> Unlikely
370     isProb = false;
371   } else {
372     return false;
373   }
374
375   BasicBlock *Taken = BI->getSuccessor(0);
376   BasicBlock *NonTaken = BI->getSuccessor(1);
377
378   if (!isProb)
379     std::swap(Taken, NonTaken);
380
381   setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
382   setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
383
384   return true;
385 }
386
387 bool BranchProbabilityInfo::calcInvokeHeuristics(BasicBlock *BB) {
388   InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator());
389   if (!II)
390     return false;
391
392   BasicBlock *Normal = II->getNormalDest();
393   BasicBlock *Unwind = II->getUnwindDest();
394
395   setEdgeWeight(BB, Normal, IH_TAKEN_WEIGHT);
396   setEdgeWeight(BB, Unwind, IH_NONTAKEN_WEIGHT);
397   return true;
398 }
399
400 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
401   AU.addRequired<LoopInfo>();
402   AU.setPreservesAll();
403 }
404
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());
409
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());
414        I != E; ++I) {
415     DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
416     if (calcUnreachableHeuristics(*I))
417       continue;
418     if (calcMetadataWeights(*I))
419       continue;
420     if (calcLoopBranchHeuristics(*I))
421       continue;
422     if (calcPointerHeuristics(*I))
423       continue;
424     if (calcZeroHeuristics(*I))
425       continue;
426     if (calcFloatingPointHeuristics(*I))
427       continue;
428     calcInvokeHeuristics(*I);
429   }
430
431   PostDominatedByUnreachable.clear();
432   return false;
433 }
434
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();
441        BI != BE; ++BI) {
442     for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
443          SI != SE; ++SI) {
444       printEdgeProbability(OS << "  ", BI, *SI);
445     }
446   }
447 }
448
449 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
450   uint32_t Sum = 0;
451
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;
456
457     Sum += Weight;
458     assert(Sum > PrevSum); (void) PrevSum;
459   }
460
461   return Sum;
462 }
463
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);
469 }
470
471 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
472   uint32_t Sum = 0;
473   uint32_t MaxWeight = 0;
474   BasicBlock *MaxSucc = 0;
475
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;
480
481     Sum += Weight;
482     assert(Sum > PrevSum); (void) PrevSum;
483
484     if (Weight > MaxWeight) {
485       MaxWeight = Weight;
486       MaxSucc = Succ;
487     }
488   }
489
490   // Hot probability is at least 4/5 = 80%
491   if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
492     return MaxSucc;
493
494   return 0;
495 }
496
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 {
500   Edge E(Src, Dst);
501   DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
502
503   if (I != Weights.end())
504     return I->second;
505
506   return DEFAULT_WEIGHT;
507 }
508
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"));
515 }
516
517
518 BranchProbability BranchProbabilityInfo::
519 getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
520
521   uint32_t N = getEdgeWeight(Src, Dst);
522   uint32_t D = getSumForBlock(Src);
523
524   return BranchProbability(N, D);
525 }
526
527 raw_ostream &
528 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
529                                             const BasicBlock *Src,
530                                             const BasicBlock *Dst) const {
531
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");
536
537   return OS;
538 }