Remove return heuristics from the static branch probabilities, and
[oota-llvm.git] / lib / Analysis / BranchProbabilityInfo.cpp
1 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -*- C++ -*-===//
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.
69 static const uint32_t UR_NONTAKEN_WEIGHT = 1023;
70
71 static const uint32_t PH_TAKEN_WEIGHT = 20;
72 static const uint32_t PH_NONTAKEN_WEIGHT = 12;
73
74 static const uint32_t ZH_TAKEN_WEIGHT = 20;
75 static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
76
77 static const uint32_t FPH_TAKEN_WEIGHT = 20;
78 static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
79
80 // Standard weight value. Used when none of the heuristics set weight for
81 // the edge.
82 static const uint32_t NORMAL_WEIGHT = 16;
83
84 // Minimum weight of an edge. Please note, that weight is NEVER 0.
85 static const uint32_t MIN_WEIGHT = 1;
86
87 static uint32_t getMaxWeightFor(BasicBlock *BB) {
88   return UINT32_MAX / BB->getTerminator()->getNumSuccessors();
89 }
90
91
92 /// \brief Calculate edge weights for successors lead to unreachable.
93 ///
94 /// Predict that a successor which leads necessarily to an
95 /// unreachable-terminated block as extremely unlikely.
96 bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) {
97   TerminatorInst *TI = BB->getTerminator();
98   if (TI->getNumSuccessors() == 0) {
99     if (isa<UnreachableInst>(TI))
100       PostDominatedByUnreachable.insert(BB);
101     return false;
102   }
103
104   SmallPtrSet<BasicBlock *, 4> UnreachableEdges;
105   SmallPtrSet<BasicBlock *, 4> ReachableEdges;
106
107   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
108     if (PostDominatedByUnreachable.count(*I))
109       UnreachableEdges.insert(*I);
110     else
111       ReachableEdges.insert(*I);
112   }
113
114   // If all successors are in the set of blocks post-dominated by unreachable,
115   // this block is too.
116   if (UnreachableEdges.size() == TI->getNumSuccessors())
117     PostDominatedByUnreachable.insert(BB);
118
119   // Skip probabilities if this block has a single successor or if all were
120   // reachable.
121   if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
122     return false;
123
124   uint32_t UnreachableWeight =
125     std::max(UR_TAKEN_WEIGHT / UnreachableEdges.size(), MIN_WEIGHT);
126   for (SmallPtrSet<BasicBlock *, 4>::iterator I = UnreachableEdges.begin(),
127                                               E = UnreachableEdges.end();
128        I != E; ++I)
129     setEdgeWeight(BB, *I, UnreachableWeight);
130
131   if (ReachableEdges.empty())
132     return true;
133   uint32_t ReachableWeight =
134     std::max(UR_NONTAKEN_WEIGHT / ReachableEdges.size(), NORMAL_WEIGHT);
135   for (SmallPtrSet<BasicBlock *, 4>::iterator I = ReachableEdges.begin(),
136                                               E = ReachableEdges.end();
137        I != E; ++I)
138     setEdgeWeight(BB, *I, ReachableWeight);
139
140   return true;
141 }
142
143 // Propagate existing explicit probabilities from either profile data or
144 // 'expect' intrinsic processing.
145 bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
146   TerminatorInst *TI = BB->getTerminator();
147   if (TI->getNumSuccessors() == 1)
148     return false;
149   if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
150     return false;
151
152   MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
153   if (!WeightsNode)
154     return false;
155
156   // Ensure there are weights for all of the successors. Note that the first
157   // operand to the metadata node is a name, not a weight.
158   if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
159     return false;
160
161   // Build up the final weights that will be used in a temporary buffer, but
162   // don't add them until all weihts are present. Each weight value is clamped
163   // to [1, getMaxWeightFor(BB)].
164   uint32_t WeightLimit = getMaxWeightFor(BB);
165   SmallVector<uint32_t, 2> Weights;
166   Weights.reserve(TI->getNumSuccessors());
167   for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
168     ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i));
169     if (!Weight)
170       return false;
171     Weights.push_back(
172       std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit)));
173   }
174   assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
175   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
176     setEdgeWeight(BB, TI->getSuccessor(i), Weights[i]);
177
178   return true;
179 }
180
181 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
182 // between two pointer or pointer and NULL will fail.
183 bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) {
184   BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
185   if (!BI || !BI->isConditional())
186     return false;
187
188   Value *Cond = BI->getCondition();
189   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
190   if (!CI || !CI->isEquality())
191     return false;
192
193   Value *LHS = CI->getOperand(0);
194
195   if (!LHS->getType()->isPointerTy())
196     return false;
197
198   assert(CI->getOperand(1)->getType()->isPointerTy());
199
200   BasicBlock *Taken = BI->getSuccessor(0);
201   BasicBlock *NonTaken = BI->getSuccessor(1);
202
203   // p != 0   ->   isProb = true
204   // p == 0   ->   isProb = false
205   // p != q   ->   isProb = true
206   // p == q   ->   isProb = false;
207   bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
208   if (!isProb)
209     std::swap(Taken, NonTaken);
210
211   setEdgeWeight(BB, Taken, PH_TAKEN_WEIGHT);
212   setEdgeWeight(BB, NonTaken, PH_NONTAKEN_WEIGHT);
213   return true;
214 }
215
216 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
217 // as taken, exiting edges as not-taken.
218 bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) {
219   uint32_t numSuccs = BB->getTerminator()->getNumSuccessors();
220
221   Loop *L = LI->getLoopFor(BB);
222   if (!L)
223     return false;
224
225   SmallPtrSet<BasicBlock *, 8> BackEdges;
226   SmallPtrSet<BasicBlock *, 8> ExitingEdges;
227   SmallPtrSet<BasicBlock *, 8> InEdges; // Edges from header to the loop.
228
229   bool isHeader = BB == L->getHeader();
230
231   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
232     BasicBlock *Succ = *I;
233     Loop *SuccL = LI->getLoopFor(Succ);
234     if (SuccL != L)
235       ExitingEdges.insert(Succ);
236     else if (Succ == L->getHeader())
237       BackEdges.insert(Succ);
238     else if (isHeader)
239       InEdges.insert(Succ);
240   }
241
242   if (uint32_t numBackEdges = BackEdges.size()) {
243     uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
244     if (backWeight < NORMAL_WEIGHT)
245       backWeight = NORMAL_WEIGHT;
246
247     for (SmallPtrSet<BasicBlock *, 8>::iterator EI = BackEdges.begin(),
248          EE = BackEdges.end(); EI != EE; ++EI) {
249       BasicBlock *Back = *EI;
250       setEdgeWeight(BB, Back, backWeight);
251     }
252   }
253
254   if (uint32_t numInEdges = InEdges.size()) {
255     uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
256     if (inWeight < NORMAL_WEIGHT)
257       inWeight = NORMAL_WEIGHT;
258
259     for (SmallPtrSet<BasicBlock *, 8>::iterator EI = InEdges.begin(),
260          EE = InEdges.end(); EI != EE; ++EI) {
261       BasicBlock *Back = *EI;
262       setEdgeWeight(BB, Back, inWeight);
263     }
264   }
265
266   uint32_t numExitingEdges = ExitingEdges.size();
267   if (uint32_t numNonExitingEdges = numSuccs - numExitingEdges) {
268     uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numNonExitingEdges;
269     if (exitWeight < MIN_WEIGHT)
270       exitWeight = MIN_WEIGHT;
271
272     for (SmallPtrSet<BasicBlock *, 8>::iterator EI = ExitingEdges.begin(),
273          EE = ExitingEdges.end(); EI != EE; ++EI) {
274       BasicBlock *Exiting = *EI;
275       setEdgeWeight(BB, Exiting, exitWeight);
276     }
277   }
278
279   return true;
280 }
281
282 bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
283   BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator());
284   if (!BI || !BI->isConditional())
285     return false;
286
287   Value *Cond = BI->getCondition();
288   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
289   if (!CI)
290     return false;
291
292   Value *RHS = CI->getOperand(1);
293   ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
294   if (!CV)
295     return false;
296
297   bool isProb;
298   if (CV->isZero()) {
299     switch (CI->getPredicate()) {
300     case CmpInst::ICMP_EQ:
301       // X == 0   ->  Unlikely
302       isProb = false;
303       break;
304     case CmpInst::ICMP_NE:
305       // X != 0   ->  Likely
306       isProb = true;
307       break;
308     case CmpInst::ICMP_SLT:
309       // X < 0   ->  Unlikely
310       isProb = false;
311       break;
312     case CmpInst::ICMP_SGT:
313       // X > 0   ->  Likely
314       isProb = true;
315       break;
316     default:
317       return false;
318     }
319   } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
320     // InstCombine canonicalizes X <= 0 into X < 1.
321     // X <= 0   ->  Unlikely
322     isProb = false;
323   } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) {
324     // InstCombine canonicalizes X >= 0 into X > -1.
325     // X >= 0   ->  Likely
326     isProb = true;
327   } else {
328     return false;
329   }
330
331   BasicBlock *Taken = BI->getSuccessor(0);
332   BasicBlock *NonTaken = BI->getSuccessor(1);
333
334   if (!isProb)
335     std::swap(Taken, NonTaken);
336
337   setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT);
338   setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT);
339
340   return true;
341 }
342
343 bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) {
344   BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
345   if (!BI || !BI->isConditional())
346     return false;
347
348   Value *Cond = BI->getCondition();
349   FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
350   if (!FCmp)
351     return false;
352
353   bool isProb;
354   if (FCmp->isEquality()) {
355     // f1 == f2 -> Unlikely
356     // f1 != f2 -> Likely
357     isProb = !FCmp->isTrueWhenEqual();
358   } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
359     // !isnan -> Likely
360     isProb = true;
361   } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
362     // isnan -> Unlikely
363     isProb = false;
364   } else {
365     return false;
366   }
367
368   BasicBlock *Taken = BI->getSuccessor(0);
369   BasicBlock *NonTaken = BI->getSuccessor(1);
370
371   if (!isProb)
372     std::swap(Taken, NonTaken);
373
374   setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
375   setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
376
377   return true;
378 }
379
380 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
381   AU.addRequired<LoopInfo>();
382   AU.setPreservesAll();
383 }
384
385 bool BranchProbabilityInfo::runOnFunction(Function &F) {
386   LastF = &F; // Store the last function we ran on for printing.
387   LI = &getAnalysis<LoopInfo>();
388   assert(PostDominatedByUnreachable.empty());
389
390   // Walk the basic blocks in post-order so that we can build up state about
391   // the successors of a block iteratively.
392   for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()),
393                                  E = po_end(&F.getEntryBlock());
394        I != E; ++I) {
395     DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n");
396     if (calcUnreachableHeuristics(*I))
397       continue;
398     if (calcMetadataWeights(*I))
399       continue;
400     if (calcLoopBranchHeuristics(*I))
401       continue;
402     if (calcPointerHeuristics(*I))
403       continue;
404     if (calcZeroHeuristics(*I))
405       continue;
406     calcFloatingPointHeuristics(*I);
407   }
408
409   PostDominatedByUnreachable.clear();
410   return false;
411 }
412
413 void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
414   OS << "---- Branch Probabilities ----\n";
415   // We print the probabilities from the last function the analysis ran over,
416   // or the function it is currently running over.
417   assert(LastF && "Cannot print prior to running over a function");
418   for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
419        BI != BE; ++BI) {
420     for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
421          SI != SE; ++SI) {
422       printEdgeProbability(OS << "  ", BI, *SI);
423     }
424   }
425 }
426
427 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
428   uint32_t Sum = 0;
429
430   for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
431     const BasicBlock *Succ = *I;
432     uint32_t Weight = getEdgeWeight(BB, Succ);
433     uint32_t PrevSum = Sum;
434
435     Sum += Weight;
436     assert(Sum > PrevSum); (void) PrevSum;
437   }
438
439   return Sum;
440 }
441
442 bool BranchProbabilityInfo::
443 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
444   // Hot probability is at least 4/5 = 80%
445   // FIXME: Compare against a static "hot" BranchProbability.
446   return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
447 }
448
449 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
450   uint32_t Sum = 0;
451   uint32_t MaxWeight = 0;
452   BasicBlock *MaxSucc = 0;
453
454   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
455     BasicBlock *Succ = *I;
456     uint32_t Weight = getEdgeWeight(BB, Succ);
457     uint32_t PrevSum = Sum;
458
459     Sum += Weight;
460     assert(Sum > PrevSum); (void) PrevSum;
461
462     if (Weight > MaxWeight) {
463       MaxWeight = Weight;
464       MaxSucc = Succ;
465     }
466   }
467
468   // Hot probability is at least 4/5 = 80%
469   if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
470     return MaxSucc;
471
472   return 0;
473 }
474
475 // Return edge's weight. If can't find it, return DEFAULT_WEIGHT value.
476 uint32_t BranchProbabilityInfo::
477 getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
478   Edge E(Src, Dst);
479   DenseMap<Edge, uint32_t>::const_iterator I = Weights.find(E);
480
481   if (I != Weights.end())
482     return I->second;
483
484   return DEFAULT_WEIGHT;
485 }
486
487 void BranchProbabilityInfo::
488 setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, uint32_t Weight) {
489   Weights[std::make_pair(Src, Dst)] = Weight;
490   DEBUG(dbgs() << "set edge " << Src->getNameStr() << " -> "
491                << Dst->getNameStr() << " weight to " << Weight
492                << (isEdgeHot(Src, Dst) ? " [is HOT now]\n" : "\n"));
493 }
494
495
496 BranchProbability BranchProbabilityInfo::
497 getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
498
499   uint32_t N = getEdgeWeight(Src, Dst);
500   uint32_t D = getSumForBlock(Src);
501
502   return BranchProbability(N, D);
503 }
504
505 raw_ostream &
506 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
507                                             const BasicBlock *Src,
508                                             const BasicBlock *Dst) const {
509
510   const BranchProbability Prob = getEdgeProbability(Src, Dst);
511   OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()
512      << " probability is " << Prob
513      << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
514
515   return OS;
516 }