LLVM  6.0.0svn
BranchProbabilityInfo.cpp
Go to the documentation of this file.
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 
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/CFG.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/PassManager.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/IR/Value.h"
33 #include "llvm/Pass.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/Debug.h"
38 #include <cassert>
39 #include <cstdint>
40 #include <iterator>
41 #include <utility>
42 
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "branch-prob"
46 
48  "print-bpi", cl::init(false), cl::Hidden,
49  cl::desc("Print the branch probability info."));
50 
52  "print-bpi-func-name", cl::Hidden,
53  cl::desc("The option to specify the name of the function "
54  "whose branch probability info is printed."));
55 
57  "Branch Probability Analysis", false, true)
61  "Branch Probability Analysis", false, true)
62 
63 char BranchProbabilityInfoWrapperPass::ID = 0;
64 
65 // Weights are for internal use only. They are used by heuristics to help to
66 // estimate edges' probability. Example:
67 //
68 // Using "Loop Branch Heuristics" we predict weights of edges for the
69 // block BB2.
70 // ...
71 // |
72 // V
73 // BB1<-+
74 // | |
75 // | | (Weight = 124)
76 // V |
77 // BB2--+
78 // |
79 // | (Weight = 4)
80 // V
81 // BB3
82 //
83 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
84 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
87 
88 /// \brief Unreachable-terminating branch taken probability.
89 ///
90 /// This is the probability for a branch being taken to a block that terminates
91 /// (eventually) in unreachable. These are predicted as unlikely as possible.
92 /// All reachable probability will equally share the remaining part.
94 
95 /// \brief Weight for a branch taken going into a cold block.
96 ///
97 /// This is the weight for a branch taken toward a block marked
98 /// cold. A block is marked cold if it's postdominated by a
99 /// block containing a call to a cold function. Cold functions
100 /// are those marked with attribute 'cold'.
102 
103 /// \brief Weight for a branch not-taken into a cold block.
104 ///
105 /// This is the weight for a branch not taken toward a block marked
106 /// cold.
108 
111 
114 
117 
118 /// \brief Invoke-terminating normal branch taken weight
119 ///
120 /// This is the weight for branching to the normal destination of an invoke
121 /// instruction. We expect this to happen most of the time. Set the weight to an
122 /// absurdly high value so that nested loops subsume it.
123 static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1;
124 
125 /// \brief Invoke-terminating normal branch not-taken weight.
126 ///
127 /// This is the weight for branching to the unwind destination of an invoke
128 /// instruction. This is essentially never taken.
130 
131 /// \brief Add \p BB to PostDominatedByUnreachable set if applicable.
132 void
133 BranchProbabilityInfo::updatePostDominatedByUnreachable(const BasicBlock *BB) {
134  const TerminatorInst *TI = BB->getTerminator();
135  if (TI->getNumSuccessors() == 0) {
136  if (isa<UnreachableInst>(TI) ||
137  // If this block is terminated by a call to
138  // @llvm.experimental.deoptimize then treat it like an unreachable since
139  // the @llvm.experimental.deoptimize call is expected to practically
140  // never execute.
141  BB->getTerminatingDeoptimizeCall())
142  PostDominatedByUnreachable.insert(BB);
143  return;
144  }
145 
146  // If the terminator is an InvokeInst, check only the normal destination block
147  // as the unwind edge of InvokeInst is also very unlikely taken.
148  if (auto *II = dyn_cast<InvokeInst>(TI)) {
149  if (PostDominatedByUnreachable.count(II->getNormalDest()))
150  PostDominatedByUnreachable.insert(BB);
151  return;
152  }
153 
154  for (auto *I : successors(BB))
155  // If any of successor is not post dominated then BB is also not.
156  if (!PostDominatedByUnreachable.count(I))
157  return;
158 
159  PostDominatedByUnreachable.insert(BB);
160 }
161 
162 /// \brief Add \p BB to PostDominatedByColdCall set if applicable.
163 void
164 BranchProbabilityInfo::updatePostDominatedByColdCall(const BasicBlock *BB) {
165  assert(!PostDominatedByColdCall.count(BB));
166  const TerminatorInst *TI = BB->getTerminator();
167  if (TI->getNumSuccessors() == 0)
168  return;
169 
170  // If all of successor are post dominated then BB is also done.
171  if (llvm::all_of(successors(BB), [&](const BasicBlock *SuccBB) {
172  return PostDominatedByColdCall.count(SuccBB);
173  })) {
174  PostDominatedByColdCall.insert(BB);
175  return;
176  }
177 
178  // If the terminator is an InvokeInst, check only the normal destination
179  // block as the unwind edge of InvokeInst is also very unlikely taken.
180  if (auto *II = dyn_cast<InvokeInst>(TI))
181  if (PostDominatedByColdCall.count(II->getNormalDest())) {
182  PostDominatedByColdCall.insert(BB);
183  return;
184  }
185 
186  // Otherwise, if the block itself contains a cold function, add it to the
187  // set of blocks post-dominated by a cold call.
188  for (auto &I : *BB)
189  if (const CallInst *CI = dyn_cast<CallInst>(&I))
190  if (CI->hasFnAttr(Attribute::Cold)) {
191  PostDominatedByColdCall.insert(BB);
192  return;
193  }
194 }
195 
196 /// \brief Calculate edge weights for successors lead to unreachable.
197 ///
198 /// Predict that a successor which leads necessarily to an
199 /// unreachable-terminated block as extremely unlikely.
200 bool BranchProbabilityInfo::calcUnreachableHeuristics(const BasicBlock *BB) {
201  const TerminatorInst *TI = BB->getTerminator();
202  assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
203 
204  // Return false here so that edge weights for InvokeInst could be decided
205  // in calcInvokeHeuristics().
206  if (isa<InvokeInst>(TI))
207  return false;
208 
209  SmallVector<unsigned, 4> UnreachableEdges;
210  SmallVector<unsigned, 4> ReachableEdges;
211 
212  for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
213  if (PostDominatedByUnreachable.count(*I))
214  UnreachableEdges.push_back(I.getSuccessorIndex());
215  else
216  ReachableEdges.push_back(I.getSuccessorIndex());
217 
218  // Skip probabilities if all were reachable.
219  if (UnreachableEdges.empty())
220  return false;
221 
222  if (ReachableEdges.empty()) {
223  BranchProbability Prob(1, UnreachableEdges.size());
224  for (unsigned SuccIdx : UnreachableEdges)
225  setEdgeProbability(BB, SuccIdx, Prob);
226  return true;
227  }
228 
229  auto UnreachableProb = UR_TAKEN_PROB;
230  auto ReachableProb =
231  (BranchProbability::getOne() - UR_TAKEN_PROB * UnreachableEdges.size()) /
232  ReachableEdges.size();
233 
234  for (unsigned SuccIdx : UnreachableEdges)
235  setEdgeProbability(BB, SuccIdx, UnreachableProb);
236  for (unsigned SuccIdx : ReachableEdges)
237  setEdgeProbability(BB, SuccIdx, ReachableProb);
238 
239  return true;
240 }
241 
242 // Propagate existing explicit probabilities from either profile data or
243 // 'expect' intrinsic processing. Examine metadata against unreachable
244 // heuristic. The probability of the edge coming to unreachable block is
245 // set to min of metadata and unreachable heuristic.
246 bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
247  const TerminatorInst *TI = BB->getTerminator();
248  assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
249  if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI)))
250  return false;
251 
252  MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
253  if (!WeightsNode)
254  return false;
255 
256  // Check that the number of successors is manageable.
257  assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors");
258 
259  // Ensure there are weights for all of the successors. Note that the first
260  // operand to the metadata node is a name, not a weight.
261  if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
262  return false;
263 
264  // Build up the final weights that will be used in a temporary buffer.
265  // Compute the sum of all weights to later decide whether they need to
266  // be scaled to fit in 32 bits.
267  uint64_t WeightSum = 0;
268  SmallVector<uint32_t, 2> Weights;
269  SmallVector<unsigned, 2> UnreachableIdxs;
270  SmallVector<unsigned, 2> ReachableIdxs;
271  Weights.reserve(TI->getNumSuccessors());
272  for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
273  ConstantInt *Weight =
274  mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(i));
275  if (!Weight)
276  return false;
277  assert(Weight->getValue().getActiveBits() <= 32 &&
278  "Too many bits for uint32_t");
279  Weights.push_back(Weight->getZExtValue());
280  WeightSum += Weights.back();
281  if (PostDominatedByUnreachable.count(TI->getSuccessor(i - 1)))
282  UnreachableIdxs.push_back(i - 1);
283  else
284  ReachableIdxs.push_back(i - 1);
285  }
286  assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
287 
288  // If the sum of weights does not fit in 32 bits, scale every weight down
289  // accordingly.
290  uint64_t ScalingFactor =
291  (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
292 
293  if (ScalingFactor > 1) {
294  WeightSum = 0;
295  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
296  Weights[i] /= ScalingFactor;
297  WeightSum += Weights[i];
298  }
299  }
300  assert(WeightSum <= UINT32_MAX &&
301  "Expected weights to scale down to 32 bits");
302 
303  if (WeightSum == 0 || ReachableIdxs.size() == 0) {
304  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
305  Weights[i] = 1;
306  WeightSum = TI->getNumSuccessors();
307  }
308 
309  // Set the probability.
311  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
312  BP.push_back({ Weights[i], static_cast<uint32_t>(WeightSum) });
313 
314  // Examine the metadata against unreachable heuristic.
315  // If the unreachable heuristic is more strong then we use it for this edge.
316  if (UnreachableIdxs.size() > 0 && ReachableIdxs.size() > 0) {
317  auto ToDistribute = BranchProbability::getZero();
318  auto UnreachableProb = UR_TAKEN_PROB;
319  for (auto i : UnreachableIdxs)
320  if (UnreachableProb < BP[i]) {
321  ToDistribute += BP[i] - UnreachableProb;
322  BP[i] = UnreachableProb;
323  }
324 
325  // If we modified the probability of some edges then we must distribute
326  // the difference between reachable blocks.
327  if (ToDistribute > BranchProbability::getZero()) {
328  BranchProbability PerEdge = ToDistribute / ReachableIdxs.size();
329  for (auto i : ReachableIdxs)
330  BP[i] += PerEdge;
331  }
332  }
333 
334  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
335  setEdgeProbability(BB, i, BP[i]);
336 
337  return true;
338 }
339 
340 /// \brief Calculate edge weights for edges leading to cold blocks.
341 ///
342 /// A cold block is one post-dominated by a block with a call to a
343 /// cold function. Those edges are unlikely to be taken, so we give
344 /// them relatively low weight.
345 ///
346 /// Return true if we could compute the weights for cold edges.
347 /// Return false, otherwise.
348 bool BranchProbabilityInfo::calcColdCallHeuristics(const BasicBlock *BB) {
349  const TerminatorInst *TI = BB->getTerminator();
350  assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
351 
352  // Return false here so that edge weights for InvokeInst could be decided
353  // in calcInvokeHeuristics().
354  if (isa<InvokeInst>(TI))
355  return false;
356 
357  // Determine which successors are post-dominated by a cold block.
358  SmallVector<unsigned, 4> ColdEdges;
359  SmallVector<unsigned, 4> NormalEdges;
360  for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
361  if (PostDominatedByColdCall.count(*I))
362  ColdEdges.push_back(I.getSuccessorIndex());
363  else
364  NormalEdges.push_back(I.getSuccessorIndex());
365 
366  // Skip probabilities if no cold edges.
367  if (ColdEdges.empty())
368  return false;
369 
370  if (NormalEdges.empty()) {
371  BranchProbability Prob(1, ColdEdges.size());
372  for (unsigned SuccIdx : ColdEdges)
373  setEdgeProbability(BB, SuccIdx, Prob);
374  return true;
375  }
376 
379  (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(ColdEdges.size()));
380  auto NormalProb = BranchProbability::getBranchProbability(
382  (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(NormalEdges.size()));
383 
384  for (unsigned SuccIdx : ColdEdges)
385  setEdgeProbability(BB, SuccIdx, ColdProb);
386  for (unsigned SuccIdx : NormalEdges)
387  setEdgeProbability(BB, SuccIdx, NormalProb);
388 
389  return true;
390 }
391 
392 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
393 // between two pointer or pointer and NULL will fail.
394 bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) {
395  const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
396  if (!BI || !BI->isConditional())
397  return false;
398 
399  Value *Cond = BI->getCondition();
400  ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
401  if (!CI || !CI->isEquality())
402  return false;
403 
404  Value *LHS = CI->getOperand(0);
405 
406  if (!LHS->getType()->isPointerTy())
407  return false;
408 
409  assert(CI->getOperand(1)->getType()->isPointerTy());
410 
411  // p != 0 -> isProb = true
412  // p == 0 -> isProb = false
413  // p != q -> isProb = true
414  // p == q -> isProb = false;
415  unsigned TakenIdx = 0, NonTakenIdx = 1;
416  bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
417  if (!isProb)
418  std::swap(TakenIdx, NonTakenIdx);
419 
422  setEdgeProbability(BB, TakenIdx, TakenProb);
423  setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
424  return true;
425 }
426 
427 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
428 // as taken, exiting edges as not-taken.
429 bool BranchProbabilityInfo::calcLoopBranchHeuristics(const BasicBlock *BB,
430  const LoopInfo &LI) {
431  Loop *L = LI.getLoopFor(BB);
432  if (!L)
433  return false;
434 
435  SmallVector<unsigned, 8> BackEdges;
436  SmallVector<unsigned, 8> ExitingEdges;
437  SmallVector<unsigned, 8> InEdges; // Edges from header to the loop.
438 
439  for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
440  if (!L->contains(*I))
441  ExitingEdges.push_back(I.getSuccessorIndex());
442  else if (L->getHeader() == *I)
443  BackEdges.push_back(I.getSuccessorIndex());
444  else
445  InEdges.push_back(I.getSuccessorIndex());
446  }
447 
448  if (BackEdges.empty() && ExitingEdges.empty())
449  return false;
450 
451  // Collect the sum of probabilities of back-edges/in-edges/exiting-edges, and
452  // normalize them so that they sum up to one.
456  unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +
457  (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +
458  (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT);
459  if (!BackEdges.empty())
460  Probs[0] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);
461  if (!InEdges.empty())
462  Probs[1] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);
463  if (!ExitingEdges.empty())
464  Probs[2] = BranchProbability(LBH_NONTAKEN_WEIGHT, Denom);
465 
466  if (uint32_t numBackEdges = BackEdges.size()) {
467  auto Prob = Probs[0] / numBackEdges;
468  for (unsigned SuccIdx : BackEdges)
469  setEdgeProbability(BB, SuccIdx, Prob);
470  }
471 
472  if (uint32_t numInEdges = InEdges.size()) {
473  auto Prob = Probs[1] / numInEdges;
474  for (unsigned SuccIdx : InEdges)
475  setEdgeProbability(BB, SuccIdx, Prob);
476  }
477 
478  if (uint32_t numExitingEdges = ExitingEdges.size()) {
479  auto Prob = Probs[2] / numExitingEdges;
480  for (unsigned SuccIdx : ExitingEdges)
481  setEdgeProbability(BB, SuccIdx, Prob);
482  }
483 
484  return true;
485 }
486 
487 bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB,
488  const TargetLibraryInfo *TLI) {
489  const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
490  if (!BI || !BI->isConditional())
491  return false;
492 
493  Value *Cond = BI->getCondition();
494  ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
495  if (!CI)
496  return false;
497 
498  Value *RHS = CI->getOperand(1);
499  ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
500  if (!CV)
501  return false;
502 
503  // If the LHS is the result of AND'ing a value with a single bit bitmask,
504  // we don't have information about probabilities.
505  if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0)))
506  if (LHS->getOpcode() == Instruction::And)
507  if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1)))
508  if (AndRHS->getValue().isPowerOf2())
509  return false;
510 
511  // Check if the LHS is the return value of a library function
512  LibFunc Func = NumLibFuncs;
513  if (TLI)
514  if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0)))
515  if (Function *CalledFn = Call->getCalledFunction())
516  TLI->getLibFunc(*CalledFn, Func);
517 
518  bool isProb;
519  if (Func == LibFunc_strcasecmp ||
520  Func == LibFunc_strcmp ||
521  Func == LibFunc_strncasecmp ||
522  Func == LibFunc_strncmp ||
523  Func == LibFunc_memcmp) {
524  // strcmp and similar functions return zero, negative, or positive, if the
525  // first string is equal, less, or greater than the second. We consider it
526  // likely that the strings are not equal, so a comparison with zero is
527  // probably false, but also a comparison with any other number is also
528  // probably false given that what exactly is returned for nonzero values is
529  // not specified. Any kind of comparison other than equality we know
530  // nothing about.
531  switch (CI->getPredicate()) {
532  case CmpInst::ICMP_EQ:
533  isProb = false;
534  break;
535  case CmpInst::ICMP_NE:
536  isProb = true;
537  break;
538  default:
539  return false;
540  }
541  } else if (CV->isZero()) {
542  switch (CI->getPredicate()) {
543  case CmpInst::ICMP_EQ:
544  // X == 0 -> Unlikely
545  isProb = false;
546  break;
547  case CmpInst::ICMP_NE:
548  // X != 0 -> Likely
549  isProb = true;
550  break;
551  case CmpInst::ICMP_SLT:
552  // X < 0 -> Unlikely
553  isProb = false;
554  break;
555  case CmpInst::ICMP_SGT:
556  // X > 0 -> Likely
557  isProb = true;
558  break;
559  default:
560  return false;
561  }
562  } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
563  // InstCombine canonicalizes X <= 0 into X < 1.
564  // X <= 0 -> Unlikely
565  isProb = false;
566  } else if (CV->isMinusOne()) {
567  switch (CI->getPredicate()) {
568  case CmpInst::ICMP_EQ:
569  // X == -1 -> Unlikely
570  isProb = false;
571  break;
572  case CmpInst::ICMP_NE:
573  // X != -1 -> Likely
574  isProb = true;
575  break;
576  case CmpInst::ICMP_SGT:
577  // InstCombine canonicalizes X >= 0 into X > -1.
578  // X >= 0 -> Likely
579  isProb = true;
580  break;
581  default:
582  return false;
583  }
584  } else {
585  return false;
586  }
587 
588  unsigned TakenIdx = 0, NonTakenIdx = 1;
589 
590  if (!isProb)
591  std::swap(TakenIdx, NonTakenIdx);
592 
595  setEdgeProbability(BB, TakenIdx, TakenProb);
596  setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
597  return true;
598 }
599 
600 bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) {
601  const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
602  if (!BI || !BI->isConditional())
603  return false;
604 
605  Value *Cond = BI->getCondition();
606  FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
607  if (!FCmp)
608  return false;
609 
610  bool isProb;
611  if (FCmp->isEquality()) {
612  // f1 == f2 -> Unlikely
613  // f1 != f2 -> Likely
614  isProb = !FCmp->isTrueWhenEqual();
615  } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
616  // !isnan -> Likely
617  isProb = true;
618  } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
619  // isnan -> Unlikely
620  isProb = false;
621  } else {
622  return false;
623  }
624 
625  unsigned TakenIdx = 0, NonTakenIdx = 1;
626 
627  if (!isProb)
628  std::swap(TakenIdx, NonTakenIdx);
629 
632  setEdgeProbability(BB, TakenIdx, TakenProb);
633  setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
634  return true;
635 }
636 
637 bool BranchProbabilityInfo::calcInvokeHeuristics(const BasicBlock *BB) {
638  const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator());
639  if (!II)
640  return false;
641 
644  setEdgeProbability(BB, 0 /*Index for Normal*/, TakenProb);
645  setEdgeProbability(BB, 1 /*Index for Unwind*/, TakenProb.getCompl());
646  return true;
647 }
648 
650  Probs.clear();
651 }
652 
654  OS << "---- Branch Probabilities ----\n";
655  // We print the probabilities from the last function the analysis ran over,
656  // or the function it is currently running over.
657  assert(LastF && "Cannot print prior to running over a function");
658  for (const auto &BI : *LastF) {
659  for (succ_const_iterator SI = succ_begin(&BI), SE = succ_end(&BI); SI != SE;
660  ++SI) {
661  printEdgeProbability(OS << " ", &BI, *SI);
662  }
663  }
664 }
665 
667 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
668  // Hot probability is at least 4/5 = 80%
669  // FIXME: Compare against a static "hot" BranchProbability.
670  return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
671 }
672 
673 const BasicBlock *
675  auto MaxProb = BranchProbability::getZero();
676  const BasicBlock *MaxSucc = nullptr;
677 
678  for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
679  const BasicBlock *Succ = *I;
680  auto Prob = getEdgeProbability(BB, Succ);
681  if (Prob > MaxProb) {
682  MaxProb = Prob;
683  MaxSucc = Succ;
684  }
685  }
686 
687  // Hot probability is at least 4/5 = 80%
688  if (MaxProb > BranchProbability(4, 5))
689  return MaxSucc;
690 
691  return nullptr;
692 }
693 
694 /// Get the raw edge probability for the edge. If can't find it, return a
695 /// default probability 1/N where N is the number of successors. Here an edge is
696 /// specified using PredBlock and an
697 /// index to the successors.
700  unsigned IndexInSuccessors) const {
701  auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
702 
703  if (I != Probs.end())
704  return I->second;
705 
706  return {1,
707  static_cast<uint32_t>(std::distance(succ_begin(Src), succ_end(Src)))};
708 }
709 
712  succ_const_iterator Dst) const {
713  return getEdgeProbability(Src, Dst.getSuccessorIndex());
714 }
715 
716 /// Get the raw edge probability calculated for the block pair. This returns the
717 /// sum of all raw edge probabilities from Src to Dst.
720  const BasicBlock *Dst) const {
721  auto Prob = BranchProbability::getZero();
722  bool FoundProb = false;
723  for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)
724  if (*I == Dst) {
725  auto MapI = Probs.find(std::make_pair(Src, I.getSuccessorIndex()));
726  if (MapI != Probs.end()) {
727  FoundProb = true;
728  Prob += MapI->second;
729  }
730  }
731  uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src));
732  return FoundProb ? Prob : BranchProbability(1, succ_num);
733 }
734 
735 /// Set the edge probability for a given edge specified by PredBlock and an
736 /// index to the successors.
738  unsigned IndexInSuccessors,
739  BranchProbability Prob) {
740  Probs[std::make_pair(Src, IndexInSuccessors)] = Prob;
741  Handles.insert(BasicBlockCallbackVH(Src, this));
742  DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << IndexInSuccessors
743  << " successor probability to " << Prob << "\n");
744 }
745 
746 raw_ostream &
748  const BasicBlock *Src,
749  const BasicBlock *Dst) const {
750  const BranchProbability Prob = getEdgeProbability(Src, Dst);
751  OS << "edge " << Src->getName() << " -> " << Dst->getName()
752  << " probability is " << Prob
753  << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
754 
755  return OS;
756 }
757 
759  for (auto I = Probs.begin(), E = Probs.end(); I != E; ++I) {
760  auto Key = I->first;
761  if (Key.first == BB)
762  Probs.erase(Key);
763  }
764 }
765 
767  const TargetLibraryInfo *TLI) {
768  DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName()
769  << " ----\n\n");
770  LastF = &F; // Store the last function we ran on for printing.
771  assert(PostDominatedByUnreachable.empty());
772  assert(PostDominatedByColdCall.empty());
773 
774  // Walk the basic blocks in post-order so that we can build up state about
775  // the successors of a block iteratively.
776  for (auto BB : post_order(&F.getEntryBlock())) {
777  DEBUG(dbgs() << "Computing probabilities for " << BB->getName() << "\n");
778  updatePostDominatedByUnreachable(BB);
779  updatePostDominatedByColdCall(BB);
780  // If there is no at least two successors, no sense to set probability.
781  if (BB->getTerminator()->getNumSuccessors() < 2)
782  continue;
783  if (calcMetadataWeights(BB))
784  continue;
785  if (calcUnreachableHeuristics(BB))
786  continue;
787  if (calcColdCallHeuristics(BB))
788  continue;
789  if (calcLoopBranchHeuristics(BB, LI))
790  continue;
791  if (calcPointerHeuristics(BB))
792  continue;
793  if (calcZeroHeuristics(BB, TLI))
794  continue;
795  if (calcFloatingPointHeuristics(BB))
796  continue;
797  calcInvokeHeuristics(BB);
798  }
799 
800  PostDominatedByUnreachable.clear();
801  PostDominatedByColdCall.clear();
802 
803  if (PrintBranchProb &&
804  (PrintBranchProbFuncName.empty() ||
806  print(dbgs());
807  }
808 }
809 
811  AnalysisUsage &AU) const {
814  AU.setPreservesAll();
815 }
816 
818  const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
819  const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
820  BPI.calculate(F, LI, &TLI);
821  return false;
822 }
823 
824 void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); }
825 
827  const Module *) const {
828  BPI.print(OS);
829 }
830 
831 AnalysisKey BranchProbabilityAnalysis::Key;
836  return BPI;
837 }
838 
841  OS << "Printing analysis results of BPI for function "
842  << "'" << F.getName() << "':"
843  << "\n";
845  return PreservedAnalyses::all();
846 }
void push_back(const T &Elt)
Definition: SmallVector.h:212
static bool isEquality(Predicate Pred)
unsigned getSuccessorIndex() const
This is used to interface between code that wants to operate on terminator instructions directly...
Definition: InstrTypes.h:161
BranchProbability getCompl() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:687
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
void calculate(const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI=nullptr)
This class represents a function call, abstracting a target machine&#39;s calling convention.
This file contains the declarations for metadata subclasses.
static const uint32_t FPH_TAKEN_WEIGHT
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:816
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Metadata node.
Definition: Metadata.h:862
F(f)
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1067
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Value * getCondition() const
static BranchProbability getOne()
void reserve(size_type N)
Definition: SmallVector.h:380
branch prob
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:870
const BasicBlock * getHotSucc(const BasicBlock *BB) const
Retrieve the hot successor of a block if one exists.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:631
INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob", "Branch Probability Analysis", false, true) INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass
This file contains the simple types necessary to represent the attributes associated with functions a...
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:871
BlockT * getHeader() const
Definition: LoopInfo.h:107
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:103
Analysis pass which computes BranchProbabilityInfo.
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1512
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:201
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
This instruction compares its operands according to the predicate given to the constructor.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:194
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition: Constants.h:209
Legacy analysis pass which computes BranchProbabilityInfo.
Value * getOperand(unsigned i) const
Definition: User.h:154
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:106
const BasicBlock & getEntryBlock() const
Definition: Function.h:572
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:149
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:54
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
static const uint32_t IH_NONTAKEN_WEIGHT
Invoke-terminating normal branch not-taken weight.
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
Conditional or Unconditional Branch instruction.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:221
static const uint32_t CC_TAKEN_WEIGHT
Weight for a branch taken going into a cold block.
void eraseBlock(const BasicBlock *BB)
Forget analysis results for the given basic block.
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:869
iterator_range< po_iterator< T > > post_order(const T &G)
static const uint32_t ZH_NONTAKEN_WEIGHT
BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BPI.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
signed greater than
Definition: InstrTypes.h:887
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge&#39;s probability, relative to other out-edges of the Src.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:117
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
Provides information about what library functions are available for the current target.
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
signed less than
Definition: InstrTypes.h:889
void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors, BranchProbability Prob)
Set the raw edge probability for the given edge.
bool isConditional() const
branch Branch Probability Analysis
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:1026
void print(raw_ostream &OS) const
static const uint32_t FPH_NONTAKEN_WEIGHT
void setPreservesAll()
Set by analyses that do not transform their input at all.
static const BranchProbability UR_TAKEN_PROB
Unreachable-terminating branch taken probability.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
static const uint32_t IH_TAKEN_WEIGHT
Invoke-terminating normal branch taken weight.
Basic Alias true
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:934
Analysis providing branch probability information.
static const uint32_t PH_NONTAKEN_WEIGHT
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:404
static const uint32_t LBH_NONTAKEN_WEIGHT
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:218
#define I(x, y, z)
Definition: MD5.cpp:58
static const uint32_t LBH_TAKEN_WEIGHT
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
Analysis pass providing the TargetLibraryInfo.
static const uint32_t CC_NONTAKEN_WEIGHT
Weight for a branch not-taken into a cold block.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:73
succ_range successors(BasicBlock *BB)
Definition: CFG.h:143
bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static const uint32_t ZH_TAKEN_WEIGHT
static const uint32_t PH_TAKEN_WEIGHT
raw_ostream & printEdgeProbability(raw_ostream &OS, const BasicBlock *Src, const BasicBlock *Dst) const
Print an edge&#39;s probability.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Invoke instruction.
#define DEBUG(X)
Definition: Debug.h:118
The legacy pass manager&#39;s analysis pass to compute loop information.
Definition: LoopInfo.h:896
A container for analyses that lazily runs them and caches their results.
static BranchProbability getZero()
cl::opt< std::string > PrintBranchProbFuncName("print-bpi-func-name", cl::Hidden, cl::desc("The option to specify the name of the function " "whose branch probability info is printed."))
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
This header defines various interfaces for pass management in LLVM.
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1073
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
static cl::opt< bool > PrintBranchProb("print-bpi", cl::init(false), cl::Hidden, cl::desc("Print the branch probability info."))
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)