1 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -----------===//
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/Analysis/BranchProbabilityInfo.h"
15 #include "llvm/ADT/PostOrderIterator.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/IR/CFG.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
28 #define DEBUG_TYPE "branch-prob"
30 INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob",
31 "Branch Probability Analysis", false, true)
32 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
33 INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob",
34 "Branch Probability Analysis", false, true)
36 char BranchProbabilityInfoWrapperPass::ID = 0;
38 // Weights are for internal use only. They are used by heuristics to help to
39 // estimate edges' probability. Example:
41 // Using "Loop Branch Heuristics" we predict weights of edges for the
56 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
57 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
58 static const uint32_t LBH_TAKEN_WEIGHT = 124;
59 static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
61 /// \brief Unreachable-terminating branch taken weight.
63 /// This is the weight for a branch being taken to a block that terminates
64 /// (eventually) in unreachable. These are predicted as unlikely as possible.
65 static const uint32_t UR_TAKEN_WEIGHT = 1;
67 /// \brief Unreachable-terminating branch not-taken weight.
69 /// This is the weight for a branch not being taken toward a block that
70 /// terminates (eventually) in unreachable. Such a branch is essentially never
71 /// taken. Set the weight to an absurdly high value so that nested loops don't
72 /// easily subsume it.
73 static const uint32_t UR_NONTAKEN_WEIGHT = 1024*1024 - 1;
75 /// \brief Weight for a branch taken going into a cold block.
77 /// This is the weight for a branch taken toward a block marked
78 /// cold. A block is marked cold if it's postdominated by a
79 /// block containing a call to a cold function. Cold functions
80 /// are those marked with attribute 'cold'.
81 static const uint32_t CC_TAKEN_WEIGHT = 4;
83 /// \brief Weight for a branch not-taken into a cold block.
85 /// This is the weight for a branch not taken toward a block marked
87 static const uint32_t CC_NONTAKEN_WEIGHT = 64;
89 static const uint32_t PH_TAKEN_WEIGHT = 20;
90 static const uint32_t PH_NONTAKEN_WEIGHT = 12;
92 static const uint32_t ZH_TAKEN_WEIGHT = 20;
93 static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
95 static const uint32_t FPH_TAKEN_WEIGHT = 20;
96 static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
98 /// \brief Invoke-terminating normal branch taken weight
100 /// This is the weight for branching to the normal destination of an invoke
101 /// instruction. We expect this to happen most of the time. Set the weight to an
102 /// absurdly high value so that nested loops subsume it.
103 static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1;
105 /// \brief Invoke-terminating normal branch not-taken weight.
107 /// This is the weight for branching to the unwind destination of an invoke
108 /// instruction. This is essentially never taken.
109 static const uint32_t IH_NONTAKEN_WEIGHT = 1;
111 /// \brief Add \p BB to PostDominatedByUnreachable set if applicable.
113 BranchProbabilityInfo::updatePostDominatedByUnreachable(const BasicBlock *BB) {
114 const TerminatorInst *TI = BB->getTerminator();
115 if (TI->getNumSuccessors() == 0) {
116 if (isa<UnreachableInst>(TI) ||
117 // If this block is terminated by a call to
118 // @llvm.experimental.deoptimize then treat it like an unreachable since
119 // the @llvm.experimental.deoptimize call is expected to practically
121 BB->getTerminatingDeoptimizeCall())
122 PostDominatedByUnreachable.insert(BB);
126 // If the terminator is an InvokeInst, check only the normal destination block
127 // as the unwind edge of InvokeInst is also very unlikely taken.
128 if (auto *II = dyn_cast<InvokeInst>(TI)) {
129 if (PostDominatedByUnreachable.count(II->getNormalDest()))
130 PostDominatedByUnreachable.insert(BB);
134 for (auto *I : successors(BB))
135 // If any of successor is not post dominated then BB is also not.
136 if (!PostDominatedByUnreachable.count(I))
139 PostDominatedByUnreachable.insert(BB);
142 /// \brief Add \p BB to PostDominatedByColdCall set if applicable.
144 BranchProbabilityInfo::updatePostDominatedByColdCall(const BasicBlock *BB) {
145 assert(!PostDominatedByColdCall.count(BB));
146 const TerminatorInst *TI = BB->getTerminator();
147 if (TI->getNumSuccessors() == 0)
150 // If all of successor are post dominated then BB is also done.
151 if (llvm::all_of(successors(BB), [&](const BasicBlock *SuccBB) {
152 return PostDominatedByColdCall.count(SuccBB);
154 PostDominatedByColdCall.insert(BB);
158 // If the terminator is an InvokeInst, check only the normal destination
159 // block as the unwind edge of InvokeInst is also very unlikely taken.
160 if (auto *II = dyn_cast<InvokeInst>(TI))
161 if (PostDominatedByColdCall.count(II->getNormalDest())) {
162 PostDominatedByColdCall.insert(BB);
166 // Otherwise, if the block itself contains a cold function, add it to the
167 // set of blocks post-dominated by a cold call.
169 if (const CallInst *CI = dyn_cast<CallInst>(&I))
170 if (CI->hasFnAttr(Attribute::Cold)) {
171 PostDominatedByColdCall.insert(BB);
176 /// \brief Calculate edge weights for successors lead to unreachable.
178 /// Predict that a successor which leads necessarily to an
179 /// unreachable-terminated block as extremely unlikely.
180 bool BranchProbabilityInfo::calcUnreachableHeuristics(const BasicBlock *BB) {
181 const TerminatorInst *TI = BB->getTerminator();
182 if (TI->getNumSuccessors() == 0)
185 SmallVector<unsigned, 4> UnreachableEdges;
186 SmallVector<unsigned, 4> ReachableEdges;
188 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
189 if (PostDominatedByUnreachable.count(*I))
190 UnreachableEdges.push_back(I.getSuccessorIndex());
192 ReachableEdges.push_back(I.getSuccessorIndex());
194 // Skip probabilities if this block has a single successor or if all were
196 if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
199 // Return false here so that edge weights for InvokeInst could be decided
200 // in calcInvokeHeuristics().
201 if (isa<InvokeInst>(TI))
204 if (ReachableEdges.empty()) {
205 BranchProbability Prob(1, UnreachableEdges.size());
206 for (unsigned SuccIdx : UnreachableEdges)
207 setEdgeProbability(BB, SuccIdx, Prob);
211 auto UnreachableProb = BranchProbability::getBranchProbability(
212 UR_TAKEN_WEIGHT, (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) *
213 uint64_t(UnreachableEdges.size()));
214 auto ReachableProb = BranchProbability::getBranchProbability(
216 (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) * uint64_t(ReachableEdges.size()));
218 for (unsigned SuccIdx : UnreachableEdges)
219 setEdgeProbability(BB, SuccIdx, UnreachableProb);
220 for (unsigned SuccIdx : ReachableEdges)
221 setEdgeProbability(BB, SuccIdx, ReachableProb);
226 // Propagate existing explicit probabilities from either profile data or
227 // 'expect' intrinsic processing.
228 bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
229 const TerminatorInst *TI = BB->getTerminator();
230 if (TI->getNumSuccessors() == 1)
232 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
235 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
239 // Check that the number of successors is manageable.
240 assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors");
242 // Ensure there are weights for all of the successors. Note that the first
243 // operand to the metadata node is a name, not a weight.
244 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
247 // Build up the final weights that will be used in a temporary buffer.
248 // Compute the sum of all weights to later decide whether they need to
249 // be scaled to fit in 32 bits.
250 uint64_t WeightSum = 0;
251 SmallVector<uint32_t, 2> Weights;
252 Weights.reserve(TI->getNumSuccessors());
253 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) {
254 ConstantInt *Weight =
255 mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(i));
258 assert(Weight->getValue().getActiveBits() <= 32 &&
259 "Too many bits for uint32_t");
260 Weights.push_back(Weight->getZExtValue());
261 WeightSum += Weights.back();
263 assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
265 // If the sum of weights does not fit in 32 bits, scale every weight down
267 uint64_t ScalingFactor =
268 (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
271 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
272 Weights[i] /= ScalingFactor;
273 WeightSum += Weights[i];
276 if (WeightSum == 0) {
277 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
278 setEdgeProbability(BB, i, {1, e});
280 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
281 setEdgeProbability(BB, i, {Weights[i], static_cast<uint32_t>(WeightSum)});
284 assert(WeightSum <= UINT32_MAX &&
285 "Expected weights to scale down to 32 bits");
290 /// \brief Calculate edge weights for edges leading to cold blocks.
292 /// A cold block is one post-dominated by a block with a call to a
293 /// cold function. Those edges are unlikely to be taken, so we give
294 /// them relatively low weight.
296 /// Return true if we could compute the weights for cold edges.
297 /// Return false, otherwise.
298 bool BranchProbabilityInfo::calcColdCallHeuristics(const BasicBlock *BB) {
299 const TerminatorInst *TI = BB->getTerminator();
300 if (TI->getNumSuccessors() == 0)
303 // Determine which successors are post-dominated by a cold block.
304 SmallVector<unsigned, 4> ColdEdges;
305 SmallVector<unsigned, 4> NormalEdges;
306 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
307 if (PostDominatedByColdCall.count(*I))
308 ColdEdges.push_back(I.getSuccessorIndex());
310 NormalEdges.push_back(I.getSuccessorIndex());
312 // Return false here so that edge weights for InvokeInst could be decided
313 // in calcInvokeHeuristics().
314 if (isa<InvokeInst>(TI))
317 // Skip probabilities if this block has a single successor.
318 if (TI->getNumSuccessors() == 1 || ColdEdges.empty())
321 if (NormalEdges.empty()) {
322 BranchProbability Prob(1, ColdEdges.size());
323 for (unsigned SuccIdx : ColdEdges)
324 setEdgeProbability(BB, SuccIdx, Prob);
328 auto ColdProb = BranchProbability::getBranchProbability(
330 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(ColdEdges.size()));
331 auto NormalProb = BranchProbability::getBranchProbability(
333 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(NormalEdges.size()));
335 for (unsigned SuccIdx : ColdEdges)
336 setEdgeProbability(BB, SuccIdx, ColdProb);
337 for (unsigned SuccIdx : NormalEdges)
338 setEdgeProbability(BB, SuccIdx, NormalProb);
343 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion
344 // between two pointer or pointer and NULL will fail.
345 bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) {
346 const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
347 if (!BI || !BI->isConditional())
350 Value *Cond = BI->getCondition();
351 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
352 if (!CI || !CI->isEquality())
355 Value *LHS = CI->getOperand(0);
357 if (!LHS->getType()->isPointerTy())
360 assert(CI->getOperand(1)->getType()->isPointerTy());
362 // p != 0 -> isProb = true
363 // p == 0 -> isProb = false
364 // p != q -> isProb = true
365 // p == q -> isProb = false;
366 unsigned TakenIdx = 0, NonTakenIdx = 1;
367 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
369 std::swap(TakenIdx, NonTakenIdx);
371 BranchProbability TakenProb(PH_TAKEN_WEIGHT,
372 PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
373 setEdgeProbability(BB, TakenIdx, TakenProb);
374 setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
378 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges
379 // as taken, exiting edges as not-taken.
380 bool BranchProbabilityInfo::calcLoopBranchHeuristics(const BasicBlock *BB,
381 const LoopInfo &LI) {
382 Loop *L = LI.getLoopFor(BB);
386 SmallVector<unsigned, 8> BackEdges;
387 SmallVector<unsigned, 8> ExitingEdges;
388 SmallVector<unsigned, 8> InEdges; // Edges from header to the loop.
390 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
391 if (!L->contains(*I))
392 ExitingEdges.push_back(I.getSuccessorIndex());
393 else if (L->getHeader() == *I)
394 BackEdges.push_back(I.getSuccessorIndex());
396 InEdges.push_back(I.getSuccessorIndex());
399 if (BackEdges.empty() && ExitingEdges.empty())
402 // Collect the sum of probabilities of back-edges/in-edges/exiting-edges, and
403 // normalize them so that they sum up to one.
404 BranchProbability Probs[] = {BranchProbability::getZero(),
405 BranchProbability::getZero(),
406 BranchProbability::getZero()};
407 unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +
408 (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +
409 (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT);
410 if (!BackEdges.empty())
411 Probs[0] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);
412 if (!InEdges.empty())
413 Probs[1] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);
414 if (!ExitingEdges.empty())
415 Probs[2] = BranchProbability(LBH_NONTAKEN_WEIGHT, Denom);
417 if (uint32_t numBackEdges = BackEdges.size()) {
418 auto Prob = Probs[0] / numBackEdges;
419 for (unsigned SuccIdx : BackEdges)
420 setEdgeProbability(BB, SuccIdx, Prob);
423 if (uint32_t numInEdges = InEdges.size()) {
424 auto Prob = Probs[1] / numInEdges;
425 for (unsigned SuccIdx : InEdges)
426 setEdgeProbability(BB, SuccIdx, Prob);
429 if (uint32_t numExitingEdges = ExitingEdges.size()) {
430 auto Prob = Probs[2] / numExitingEdges;
431 for (unsigned SuccIdx : ExitingEdges)
432 setEdgeProbability(BB, SuccIdx, Prob);
438 bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB) {
439 const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
440 if (!BI || !BI->isConditional())
443 Value *Cond = BI->getCondition();
444 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
448 Value *RHS = CI->getOperand(1);
449 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
453 // If the LHS is the result of AND'ing a value with a single bit bitmask,
454 // we don't have information about probabilities.
455 if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0)))
456 if (LHS->getOpcode() == Instruction::And)
457 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1)))
458 if (AndRHS->getUniqueInteger().isPowerOf2())
463 switch (CI->getPredicate()) {
464 case CmpInst::ICMP_EQ:
465 // X == 0 -> Unlikely
468 case CmpInst::ICMP_NE:
472 case CmpInst::ICMP_SLT:
476 case CmpInst::ICMP_SGT:
483 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
484 // InstCombine canonicalizes X <= 0 into X < 1.
485 // X <= 0 -> Unlikely
487 } else if (CV->isAllOnesValue()) {
488 switch (CI->getPredicate()) {
489 case CmpInst::ICMP_EQ:
490 // X == -1 -> Unlikely
493 case CmpInst::ICMP_NE:
497 case CmpInst::ICMP_SGT:
498 // InstCombine canonicalizes X >= 0 into X > -1.
509 unsigned TakenIdx = 0, NonTakenIdx = 1;
512 std::swap(TakenIdx, NonTakenIdx);
514 BranchProbability TakenProb(ZH_TAKEN_WEIGHT,
515 ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
516 setEdgeProbability(BB, TakenIdx, TakenProb);
517 setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
521 bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) {
522 const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
523 if (!BI || !BI->isConditional())
526 Value *Cond = BI->getCondition();
527 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
532 if (FCmp->isEquality()) {
533 // f1 == f2 -> Unlikely
534 // f1 != f2 -> Likely
535 isProb = !FCmp->isTrueWhenEqual();
536 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
539 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
546 unsigned TakenIdx = 0, NonTakenIdx = 1;
549 std::swap(TakenIdx, NonTakenIdx);
551 BranchProbability TakenProb(FPH_TAKEN_WEIGHT,
552 FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);
553 setEdgeProbability(BB, TakenIdx, TakenProb);
554 setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
558 bool BranchProbabilityInfo::calcInvokeHeuristics(const BasicBlock *BB) {
559 const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator());
563 BranchProbability TakenProb(IH_TAKEN_WEIGHT,
564 IH_TAKEN_WEIGHT + IH_NONTAKEN_WEIGHT);
565 setEdgeProbability(BB, 0 /*Index for Normal*/, TakenProb);
566 setEdgeProbability(BB, 1 /*Index for Unwind*/, TakenProb.getCompl());
570 void BranchProbabilityInfo::releaseMemory() {
574 void BranchProbabilityInfo::print(raw_ostream &OS) const {
575 OS << "---- Branch Probabilities ----\n";
576 // We print the probabilities from the last function the analysis ran over,
577 // or the function it is currently running over.
578 assert(LastF && "Cannot print prior to running over a function");
579 for (const auto &BI : *LastF) {
580 for (succ_const_iterator SI = succ_begin(&BI), SE = succ_end(&BI); SI != SE;
582 printEdgeProbability(OS << " ", &BI, *SI);
587 bool BranchProbabilityInfo::
588 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
589 // Hot probability is at least 4/5 = 80%
590 // FIXME: Compare against a static "hot" BranchProbability.
591 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
595 BranchProbabilityInfo::getHotSucc(const BasicBlock *BB) const {
596 auto MaxProb = BranchProbability::getZero();
597 const BasicBlock *MaxSucc = nullptr;
599 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
600 const BasicBlock *Succ = *I;
601 auto Prob = getEdgeProbability(BB, Succ);
602 if (Prob > MaxProb) {
608 // Hot probability is at least 4/5 = 80%
609 if (MaxProb > BranchProbability(4, 5))
615 /// Get the raw edge probability for the edge. If can't find it, return a
616 /// default probability 1/N where N is the number of successors. Here an edge is
617 /// specified using PredBlock and an
618 /// index to the successors.
620 BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
621 unsigned IndexInSuccessors) const {
622 auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
624 if (I != Probs.end())
628 static_cast<uint32_t>(std::distance(succ_begin(Src), succ_end(Src)))};
632 BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
633 succ_const_iterator Dst) const {
634 return getEdgeProbability(Src, Dst.getSuccessorIndex());
637 /// Get the raw edge probability calculated for the block pair. This returns the
638 /// sum of all raw edge probabilities from Src to Dst.
640 BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
641 const BasicBlock *Dst) const {
642 auto Prob = BranchProbability::getZero();
643 bool FoundProb = false;
644 for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)
646 auto MapI = Probs.find(std::make_pair(Src, I.getSuccessorIndex()));
647 if (MapI != Probs.end()) {
649 Prob += MapI->second;
652 uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src));
653 return FoundProb ? Prob : BranchProbability(1, succ_num);
656 /// Set the edge probability for a given edge specified by PredBlock and an
657 /// index to the successors.
658 void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src,
659 unsigned IndexInSuccessors,
660 BranchProbability Prob) {
661 Probs[std::make_pair(Src, IndexInSuccessors)] = Prob;
662 Handles.insert(BasicBlockCallbackVH(Src, this));
663 DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << IndexInSuccessors
664 << " successor probability to " << Prob << "\n");
668 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
669 const BasicBlock *Src,
670 const BasicBlock *Dst) const {
672 const BranchProbability Prob = getEdgeProbability(Src, Dst);
673 OS << "edge " << Src->getName() << " -> " << Dst->getName()
674 << " probability is " << Prob
675 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
680 void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) {
681 for (auto I = Probs.begin(), E = Probs.end(); I != E; ++I) {
688 void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LI) {
689 DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName()
691 LastF = &F; // Store the last function we ran on for printing.
692 assert(PostDominatedByUnreachable.empty());
693 assert(PostDominatedByColdCall.empty());
695 // Walk the basic blocks in post-order so that we can build up state about
696 // the successors of a block iteratively.
697 for (auto BB : post_order(&F.getEntryBlock())) {
698 DEBUG(dbgs() << "Computing probabilities for " << BB->getName() << "\n");
699 updatePostDominatedByUnreachable(BB);
700 updatePostDominatedByColdCall(BB);
701 if (calcUnreachableHeuristics(BB))
703 if (calcMetadataWeights(BB))
705 if (calcColdCallHeuristics(BB))
707 if (calcLoopBranchHeuristics(BB, LI))
709 if (calcPointerHeuristics(BB))
711 if (calcZeroHeuristics(BB))
713 if (calcFloatingPointHeuristics(BB))
715 calcInvokeHeuristics(BB);
718 PostDominatedByUnreachable.clear();
719 PostDominatedByColdCall.clear();
722 void BranchProbabilityInfoWrapperPass::getAnalysisUsage(
723 AnalysisUsage &AU) const {
724 AU.addRequired<LoopInfoWrapperPass>();
725 AU.setPreservesAll();
728 bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) {
729 const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
730 BPI.calculate(F, LI);
734 void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); }
736 void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS,
737 const Module *) const {
741 AnalysisKey BranchProbabilityAnalysis::Key;
742 BranchProbabilityInfo
743 BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
744 BranchProbabilityInfo BPI;
745 BPI.calculate(F, AM.getResult<LoopAnalysis>(F));
750 BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
751 OS << "Printing analysis results of BPI for function "
752 << "'" << F.getName() << "':"
754 AM.getResult<BranchProbabilityAnalysis>(F).print(OS);
755 return PreservedAnalyses::all();