xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/BranchProbabilityInfo.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
10b57cec5SDimitry Andric //===- BranchProbabilityInfo.cpp - Branch Probability Analysis ------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Loops should be simplified before this analysis.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
140b57cec5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SCCIterator.h"
160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
170b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
19480093f4SDimitry Andric #include "llvm/Analysis/PostDominators.h"
200b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
210b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
220b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
230b57cec5SDimitry Andric #include "llvm/IR/CFG.h"
240b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
250b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
260b57cec5SDimitry Andric #include "llvm/IR/Function.h"
270b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
280b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
290b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
300b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
310b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
320b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
330b57cec5SDimitry Andric #include "llvm/IR/Type.h"
340b57cec5SDimitry Andric #include "llvm/IR/Value.h"
35480093f4SDimitry Andric #include "llvm/InitializePasses.h"
360b57cec5SDimitry Andric #include "llvm/Pass.h"
370b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
380b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
39480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
400b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
410b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
420b57cec5SDimitry Andric #include <cassert>
430b57cec5SDimitry Andric #include <cstdint>
440b57cec5SDimitry Andric #include <iterator>
450b57cec5SDimitry Andric #include <utility>
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric using namespace llvm;
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric #define DEBUG_TYPE "branch-prob"
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric static cl::opt<bool> PrintBranchProb(
520b57cec5SDimitry Andric     "print-bpi", cl::init(false), cl::Hidden,
530b57cec5SDimitry Andric     cl::desc("Print the branch probability info."));
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric cl::opt<std::string> PrintBranchProbFuncName(
560b57cec5SDimitry Andric     "print-bpi-func-name", cl::Hidden,
570b57cec5SDimitry Andric     cl::desc("The option to specify the name of the function "
580b57cec5SDimitry Andric              "whose branch probability info is printed."));
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob",
610b57cec5SDimitry Andric                       "Branch Probability Analysis", false, true)
620b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
630b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
64e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
655ffd83dbSDimitry Andric INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
660b57cec5SDimitry Andric INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob",
670b57cec5SDimitry Andric                     "Branch Probability Analysis", false, true)
680b57cec5SDimitry Andric 
69480093f4SDimitry Andric BranchProbabilityInfoWrapperPass::BranchProbabilityInfoWrapperPass()
70480093f4SDimitry Andric     : FunctionPass(ID) {
71480093f4SDimitry Andric   initializeBranchProbabilityInfoWrapperPassPass(
72480093f4SDimitry Andric       *PassRegistry::getPassRegistry());
73480093f4SDimitry Andric }
74480093f4SDimitry Andric 
750b57cec5SDimitry Andric char BranchProbabilityInfoWrapperPass::ID = 0;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric // Weights are for internal use only. They are used by heuristics to help to
780b57cec5SDimitry Andric // estimate edges' probability. Example:
790b57cec5SDimitry Andric //
800b57cec5SDimitry Andric // Using "Loop Branch Heuristics" we predict weights of edges for the
810b57cec5SDimitry Andric // block BB2.
820b57cec5SDimitry Andric //         ...
830b57cec5SDimitry Andric //          |
840b57cec5SDimitry Andric //          V
850b57cec5SDimitry Andric //         BB1<-+
860b57cec5SDimitry Andric //          |   |
870b57cec5SDimitry Andric //          |   | (Weight = 124)
880b57cec5SDimitry Andric //          V   |
890b57cec5SDimitry Andric //         BB2--+
900b57cec5SDimitry Andric //          |
910b57cec5SDimitry Andric //          | (Weight = 4)
920b57cec5SDimitry Andric //          V
930b57cec5SDimitry Andric //         BB3
940b57cec5SDimitry Andric //
950b57cec5SDimitry Andric // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875
960b57cec5SDimitry Andric // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125
970b57cec5SDimitry Andric static const uint32_t LBH_TAKEN_WEIGHT = 124;
980b57cec5SDimitry Andric static const uint32_t LBH_NONTAKEN_WEIGHT = 4;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric /// Unreachable-terminating branch taken probability.
1010b57cec5SDimitry Andric ///
1020b57cec5SDimitry Andric /// This is the probability for a branch being taken to a block that terminates
1030b57cec5SDimitry Andric /// (eventually) in unreachable. These are predicted as unlikely as possible.
1045ffd83dbSDimitry Andric /// All reachable probability will proportionally share the remaining part.
1050b57cec5SDimitry Andric static const BranchProbability UR_TAKEN_PROB = BranchProbability::getRaw(1);
1060b57cec5SDimitry Andric 
107*4824e7fdSDimitry Andric /// Heuristics and lookup tables for non-loop branches:
108*4824e7fdSDimitry Andric /// Pointer Heuristics (PH)
1090b57cec5SDimitry Andric static const uint32_t PH_TAKEN_WEIGHT = 20;
1100b57cec5SDimitry Andric static const uint32_t PH_NONTAKEN_WEIGHT = 12;
111*4824e7fdSDimitry Andric static const BranchProbability
112*4824e7fdSDimitry Andric     PtrTakenProb(PH_TAKEN_WEIGHT, PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
113*4824e7fdSDimitry Andric static const BranchProbability
114*4824e7fdSDimitry Andric     PtrUntakenProb(PH_NONTAKEN_WEIGHT, PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
1150b57cec5SDimitry Andric 
116*4824e7fdSDimitry Andric using ProbabilityList = SmallVector<BranchProbability>;
117*4824e7fdSDimitry Andric using ProbabilityTable = std::map<CmpInst::Predicate, ProbabilityList>;
118*4824e7fdSDimitry Andric 
119*4824e7fdSDimitry Andric /// Pointer comparisons:
120*4824e7fdSDimitry Andric static const ProbabilityTable PointerTable{
121*4824e7fdSDimitry Andric     {ICmpInst::ICMP_NE, {PtrTakenProb, PtrUntakenProb}}, /// p != q -> Likely
122*4824e7fdSDimitry Andric     {ICmpInst::ICMP_EQ, {PtrUntakenProb, PtrTakenProb}}, /// p == q -> Unlikely
123*4824e7fdSDimitry Andric };
124*4824e7fdSDimitry Andric 
125*4824e7fdSDimitry Andric /// Zero Heuristics (ZH)
1260b57cec5SDimitry Andric static const uint32_t ZH_TAKEN_WEIGHT = 20;
1270b57cec5SDimitry Andric static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
128*4824e7fdSDimitry Andric static const BranchProbability
129*4824e7fdSDimitry Andric     ZeroTakenProb(ZH_TAKEN_WEIGHT, ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
130*4824e7fdSDimitry Andric static const BranchProbability
131*4824e7fdSDimitry Andric     ZeroUntakenProb(ZH_NONTAKEN_WEIGHT, ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
1320b57cec5SDimitry Andric 
133*4824e7fdSDimitry Andric /// Integer compares with 0:
134*4824e7fdSDimitry Andric static const ProbabilityTable ICmpWithZeroTable{
135*4824e7fdSDimitry Andric     {CmpInst::ICMP_EQ, {ZeroUntakenProb, ZeroTakenProb}},  /// X == 0 -> Unlikely
136*4824e7fdSDimitry Andric     {CmpInst::ICMP_NE, {ZeroTakenProb, ZeroUntakenProb}},  /// X != 0 -> Likely
137*4824e7fdSDimitry Andric     {CmpInst::ICMP_SLT, {ZeroUntakenProb, ZeroTakenProb}}, /// X < 0  -> Unlikely
138*4824e7fdSDimitry Andric     {CmpInst::ICMP_SGT, {ZeroTakenProb, ZeroUntakenProb}}, /// X > 0  -> Likely
139*4824e7fdSDimitry Andric };
140*4824e7fdSDimitry Andric 
141*4824e7fdSDimitry Andric /// Integer compares with -1:
142*4824e7fdSDimitry Andric static const ProbabilityTable ICmpWithMinusOneTable{
143*4824e7fdSDimitry Andric     {CmpInst::ICMP_EQ, {ZeroUntakenProb, ZeroTakenProb}},  /// X == -1 -> Unlikely
144*4824e7fdSDimitry Andric     {CmpInst::ICMP_NE, {ZeroTakenProb, ZeroUntakenProb}},  /// X != -1 -> Likely
145*4824e7fdSDimitry Andric     // InstCombine canonicalizes X >= 0 into X > -1
146*4824e7fdSDimitry Andric     {CmpInst::ICMP_SGT, {ZeroTakenProb, ZeroUntakenProb}}, /// X >= 0  -> Likely
147*4824e7fdSDimitry Andric };
148*4824e7fdSDimitry Andric 
149*4824e7fdSDimitry Andric /// Integer compares with 1:
150*4824e7fdSDimitry Andric static const ProbabilityTable ICmpWithOneTable{
151*4824e7fdSDimitry Andric     // InstCombine canonicalizes X <= 0 into X < 1
152*4824e7fdSDimitry Andric     {CmpInst::ICMP_SLT, {ZeroUntakenProb, ZeroTakenProb}}, /// X <= 0 -> Unlikely
153*4824e7fdSDimitry Andric };
154*4824e7fdSDimitry Andric 
155*4824e7fdSDimitry Andric /// strcmp and similar functions return zero, negative, or positive, if the
156*4824e7fdSDimitry Andric /// first string is equal, less, or greater than the second. We consider it
157*4824e7fdSDimitry Andric /// likely that the strings are not equal, so a comparison with zero is
158*4824e7fdSDimitry Andric /// probably false, but also a comparison with any other number is also
159*4824e7fdSDimitry Andric /// probably false given that what exactly is returned for nonzero values is
160*4824e7fdSDimitry Andric /// not specified. Any kind of comparison other than equality we know
161*4824e7fdSDimitry Andric /// nothing about.
162*4824e7fdSDimitry Andric static const ProbabilityTable ICmpWithLibCallTable{
163*4824e7fdSDimitry Andric     {CmpInst::ICMP_EQ, {ZeroUntakenProb, ZeroTakenProb}},
164*4824e7fdSDimitry Andric     {CmpInst::ICMP_NE, {ZeroTakenProb, ZeroUntakenProb}},
165*4824e7fdSDimitry Andric };
166*4824e7fdSDimitry Andric 
167*4824e7fdSDimitry Andric // Floating-Point Heuristics (FPH)
1680b57cec5SDimitry Andric static const uint32_t FPH_TAKEN_WEIGHT = 20;
1690b57cec5SDimitry Andric static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
1700b57cec5SDimitry Andric 
1718bcb0991SDimitry Andric /// This is the probability for an ordered floating point comparison.
1728bcb0991SDimitry Andric static const uint32_t FPH_ORD_WEIGHT = 1024 * 1024 - 1;
1738bcb0991SDimitry Andric /// This is the probability for an unordered floating point comparison, it means
1748bcb0991SDimitry Andric /// one or two of the operands are NaN. Usually it is used to test for an
1758bcb0991SDimitry Andric /// exceptional case, so the result is unlikely.
1768bcb0991SDimitry Andric static const uint32_t FPH_UNO_WEIGHT = 1;
1778bcb0991SDimitry Andric 
178*4824e7fdSDimitry Andric static const BranchProbability FPOrdTakenProb(FPH_ORD_WEIGHT,
179*4824e7fdSDimitry Andric                                               FPH_ORD_WEIGHT + FPH_UNO_WEIGHT);
180*4824e7fdSDimitry Andric static const BranchProbability
181*4824e7fdSDimitry Andric     FPOrdUntakenProb(FPH_UNO_WEIGHT, FPH_ORD_WEIGHT + FPH_UNO_WEIGHT);
182*4824e7fdSDimitry Andric static const BranchProbability
183*4824e7fdSDimitry Andric     FPTakenProb(FPH_TAKEN_WEIGHT, FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);
184*4824e7fdSDimitry Andric static const BranchProbability
185*4824e7fdSDimitry Andric     FPUntakenProb(FPH_NONTAKEN_WEIGHT, FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);
186*4824e7fdSDimitry Andric 
187*4824e7fdSDimitry Andric /// Floating-Point compares:
188*4824e7fdSDimitry Andric static const ProbabilityTable FCmpTable{
189*4824e7fdSDimitry Andric     {FCmpInst::FCMP_ORD, {FPOrdTakenProb, FPOrdUntakenProb}}, /// !isnan -> Likely
190*4824e7fdSDimitry Andric     {FCmpInst::FCMP_UNO, {FPOrdUntakenProb, FPOrdTakenProb}}, /// isnan -> Unlikely
191*4824e7fdSDimitry Andric };
192*4824e7fdSDimitry Andric 
193e8d8bef9SDimitry Andric /// Set of dedicated "absolute" execution weights for a block. These weights are
194e8d8bef9SDimitry Andric /// meaningful relative to each other and their derivatives only.
195e8d8bef9SDimitry Andric enum class BlockExecWeight : std::uint32_t {
196e8d8bef9SDimitry Andric   /// Special weight used for cases with exact zero probability.
197e8d8bef9SDimitry Andric   ZERO = 0x0,
198e8d8bef9SDimitry Andric   /// Minimal possible non zero weight.
199e8d8bef9SDimitry Andric   LOWEST_NON_ZERO = 0x1,
200e8d8bef9SDimitry Andric   /// Weight to an 'unreachable' block.
201e8d8bef9SDimitry Andric   UNREACHABLE = ZERO,
202e8d8bef9SDimitry Andric   /// Weight to a block containing non returning call.
203e8d8bef9SDimitry Andric   NORETURN = LOWEST_NON_ZERO,
204e8d8bef9SDimitry Andric   /// Weight to 'unwind' block of an invoke instruction.
205e8d8bef9SDimitry Andric   UNWIND = LOWEST_NON_ZERO,
206e8d8bef9SDimitry Andric   /// Weight to a 'cold' block. Cold blocks are the ones containing calls marked
207e8d8bef9SDimitry Andric   /// with attribute 'cold'.
208e8d8bef9SDimitry Andric   COLD = 0xffff,
209e8d8bef9SDimitry Andric   /// Default weight is used in cases when there is no dedicated execution
210e8d8bef9SDimitry Andric   /// weight set. It is not propagated through the domination line either.
211e8d8bef9SDimitry Andric   DEFAULT = 0xfffff
212e8d8bef9SDimitry Andric };
2130b57cec5SDimitry Andric 
214e8d8bef9SDimitry Andric BranchProbabilityInfo::SccInfo::SccInfo(const Function &F) {
215e8d8bef9SDimitry Andric   // Record SCC numbers of blocks in the CFG to identify irreducible loops.
216e8d8bef9SDimitry Andric   // FIXME: We could only calculate this if the CFG is known to be irreducible
217e8d8bef9SDimitry Andric   // (perhaps cache this info in LoopInfo if we can easily calculate it there?).
218e8d8bef9SDimitry Andric   int SccNum = 0;
219e8d8bef9SDimitry Andric   for (scc_iterator<const Function *> It = scc_begin(&F); !It.isAtEnd();
220e8d8bef9SDimitry Andric        ++It, ++SccNum) {
221e8d8bef9SDimitry Andric     // Ignore single-block SCCs since they either aren't loops or LoopInfo will
222e8d8bef9SDimitry Andric     // catch them.
223e8d8bef9SDimitry Andric     const std::vector<const BasicBlock *> &Scc = *It;
224e8d8bef9SDimitry Andric     if (Scc.size() == 1)
225480093f4SDimitry Andric       continue;
226e8d8bef9SDimitry Andric 
227e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "BPI: SCC " << SccNum << ":");
228e8d8bef9SDimitry Andric     for (const auto *BB : Scc) {
229e8d8bef9SDimitry Andric       LLVM_DEBUG(dbgs() << " " << BB->getName());
230e8d8bef9SDimitry Andric       SccNums[BB] = SccNum;
231e8d8bef9SDimitry Andric       calculateSccBlockType(BB, SccNum);
232480093f4SDimitry Andric     }
233e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "\n");
234e8d8bef9SDimitry Andric   }
235e8d8bef9SDimitry Andric }
236e8d8bef9SDimitry Andric 
237e8d8bef9SDimitry Andric int BranchProbabilityInfo::SccInfo::getSCCNum(const BasicBlock *BB) const {
238e8d8bef9SDimitry Andric   auto SccIt = SccNums.find(BB);
239e8d8bef9SDimitry Andric   if (SccIt == SccNums.end())
240e8d8bef9SDimitry Andric     return -1;
241e8d8bef9SDimitry Andric   return SccIt->second;
242e8d8bef9SDimitry Andric }
243e8d8bef9SDimitry Andric 
244e8d8bef9SDimitry Andric void BranchProbabilityInfo::SccInfo::getSccEnterBlocks(
245e8d8bef9SDimitry Andric     int SccNum, SmallVectorImpl<BasicBlock *> &Enters) const {
246e8d8bef9SDimitry Andric 
247e8d8bef9SDimitry Andric   for (auto MapIt : SccBlocks[SccNum]) {
248e8d8bef9SDimitry Andric     const auto *BB = MapIt.first;
249e8d8bef9SDimitry Andric     if (isSCCHeader(BB, SccNum))
250e8d8bef9SDimitry Andric       for (const auto *Pred : predecessors(BB))
251e8d8bef9SDimitry Andric         if (getSCCNum(Pred) != SccNum)
252e8d8bef9SDimitry Andric           Enters.push_back(const_cast<BasicBlock *>(BB));
253e8d8bef9SDimitry Andric   }
254e8d8bef9SDimitry Andric }
255e8d8bef9SDimitry Andric 
256e8d8bef9SDimitry Andric void BranchProbabilityInfo::SccInfo::getSccExitBlocks(
257e8d8bef9SDimitry Andric     int SccNum, SmallVectorImpl<BasicBlock *> &Exits) const {
258e8d8bef9SDimitry Andric   for (auto MapIt : SccBlocks[SccNum]) {
259e8d8bef9SDimitry Andric     const auto *BB = MapIt.first;
260e8d8bef9SDimitry Andric     if (isSCCExitingBlock(BB, SccNum))
261e8d8bef9SDimitry Andric       for (const auto *Succ : successors(BB))
262e8d8bef9SDimitry Andric         if (getSCCNum(Succ) != SccNum)
263349cc55cSDimitry Andric           Exits.push_back(const_cast<BasicBlock *>(Succ));
264e8d8bef9SDimitry Andric   }
265e8d8bef9SDimitry Andric }
266e8d8bef9SDimitry Andric 
267e8d8bef9SDimitry Andric uint32_t BranchProbabilityInfo::SccInfo::getSccBlockType(const BasicBlock *BB,
268e8d8bef9SDimitry Andric                                                          int SccNum) const {
269e8d8bef9SDimitry Andric   assert(getSCCNum(BB) == SccNum);
270e8d8bef9SDimitry Andric 
271e8d8bef9SDimitry Andric   assert(SccBlocks.size() > static_cast<unsigned>(SccNum) && "Unknown SCC");
272e8d8bef9SDimitry Andric   const auto &SccBlockTypes = SccBlocks[SccNum];
273e8d8bef9SDimitry Andric 
274e8d8bef9SDimitry Andric   auto It = SccBlockTypes.find(BB);
275e8d8bef9SDimitry Andric   if (It != SccBlockTypes.end()) {
276e8d8bef9SDimitry Andric     return It->second;
277e8d8bef9SDimitry Andric   }
278e8d8bef9SDimitry Andric   return Inner;
279e8d8bef9SDimitry Andric }
280e8d8bef9SDimitry Andric 
281e8d8bef9SDimitry Andric void BranchProbabilityInfo::SccInfo::calculateSccBlockType(const BasicBlock *BB,
282e8d8bef9SDimitry Andric                                                            int SccNum) {
283e8d8bef9SDimitry Andric   assert(getSCCNum(BB) == SccNum);
284e8d8bef9SDimitry Andric   uint32_t BlockType = Inner;
285e8d8bef9SDimitry Andric 
286e8d8bef9SDimitry Andric   if (llvm::any_of(predecessors(BB), [&](const BasicBlock *Pred) {
287e8d8bef9SDimitry Andric         // Consider any block that is an entry point to the SCC as
288e8d8bef9SDimitry Andric         // a header.
289e8d8bef9SDimitry Andric         return getSCCNum(Pred) != SccNum;
290480093f4SDimitry Andric       }))
291e8d8bef9SDimitry Andric     BlockType |= Header;
2920b57cec5SDimitry Andric 
293e8d8bef9SDimitry Andric   if (llvm::any_of(successors(BB), [&](const BasicBlock *Succ) {
294e8d8bef9SDimitry Andric         return getSCCNum(Succ) != SccNum;
295480093f4SDimitry Andric       }))
296e8d8bef9SDimitry Andric     BlockType |= Exiting;
297e8d8bef9SDimitry Andric 
298e8d8bef9SDimitry Andric   // Lazily compute the set of headers for a given SCC and cache the results
299e8d8bef9SDimitry Andric   // in the SccHeaderMap.
300e8d8bef9SDimitry Andric   if (SccBlocks.size() <= static_cast<unsigned>(SccNum))
301e8d8bef9SDimitry Andric     SccBlocks.resize(SccNum + 1);
302e8d8bef9SDimitry Andric   auto &SccBlockTypes = SccBlocks[SccNum];
303e8d8bef9SDimitry Andric 
304e8d8bef9SDimitry Andric   if (BlockType != Inner) {
305e8d8bef9SDimitry Andric     bool IsInserted;
306e8d8bef9SDimitry Andric     std::tie(std::ignore, IsInserted) =
307e8d8bef9SDimitry Andric         SccBlockTypes.insert(std::make_pair(BB, BlockType));
308e8d8bef9SDimitry Andric     assert(IsInserted && "Duplicated block in SCC");
3090b57cec5SDimitry Andric   }
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric 
312e8d8bef9SDimitry Andric BranchProbabilityInfo::LoopBlock::LoopBlock(const BasicBlock *BB,
313e8d8bef9SDimitry Andric                                             const LoopInfo &LI,
314e8d8bef9SDimitry Andric                                             const SccInfo &SccI)
315e8d8bef9SDimitry Andric     : BB(BB) {
316e8d8bef9SDimitry Andric   LD.first = LI.getLoopFor(BB);
317e8d8bef9SDimitry Andric   if (!LD.first) {
318e8d8bef9SDimitry Andric     LD.second = SccI.getSCCNum(BB);
319e8d8bef9SDimitry Andric   }
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric 
322e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopEnteringEdge(const LoopEdge &Edge) const {
323e8d8bef9SDimitry Andric   const auto &SrcBlock = Edge.first;
324e8d8bef9SDimitry Andric   const auto &DstBlock = Edge.second;
325e8d8bef9SDimitry Andric   return (DstBlock.getLoop() &&
326e8d8bef9SDimitry Andric           !DstBlock.getLoop()->contains(SrcBlock.getLoop())) ||
327e8d8bef9SDimitry Andric          // Assume that SCCs can't be nested.
328e8d8bef9SDimitry Andric          (DstBlock.getSccNum() != -1 &&
329e8d8bef9SDimitry Andric           SrcBlock.getSccNum() != DstBlock.getSccNum());
330e8d8bef9SDimitry Andric }
3310b57cec5SDimitry Andric 
332e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopExitingEdge(const LoopEdge &Edge) const {
333e8d8bef9SDimitry Andric   return isLoopEnteringEdge({Edge.second, Edge.first});
334e8d8bef9SDimitry Andric }
3350b57cec5SDimitry Andric 
336e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopEnteringExitingEdge(
337e8d8bef9SDimitry Andric     const LoopEdge &Edge) const {
338e8d8bef9SDimitry Andric   return isLoopEnteringEdge(Edge) || isLoopExitingEdge(Edge);
339e8d8bef9SDimitry Andric }
340e8d8bef9SDimitry Andric 
341e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopBackEdge(const LoopEdge &Edge) const {
342e8d8bef9SDimitry Andric   const auto &SrcBlock = Edge.first;
343e8d8bef9SDimitry Andric   const auto &DstBlock = Edge.second;
344e8d8bef9SDimitry Andric   return SrcBlock.belongsToSameLoop(DstBlock) &&
345e8d8bef9SDimitry Andric          ((DstBlock.getLoop() &&
346e8d8bef9SDimitry Andric            DstBlock.getLoop()->getHeader() == DstBlock.getBlock()) ||
347e8d8bef9SDimitry Andric           (DstBlock.getSccNum() != -1 &&
348e8d8bef9SDimitry Andric            SccI->isSCCHeader(DstBlock.getBlock(), DstBlock.getSccNum())));
349e8d8bef9SDimitry Andric }
350e8d8bef9SDimitry Andric 
351e8d8bef9SDimitry Andric void BranchProbabilityInfo::getLoopEnterBlocks(
352e8d8bef9SDimitry Andric     const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Enters) const {
353e8d8bef9SDimitry Andric   if (LB.getLoop()) {
354e8d8bef9SDimitry Andric     auto *Header = LB.getLoop()->getHeader();
355e8d8bef9SDimitry Andric     Enters.append(pred_begin(Header), pred_end(Header));
356e8d8bef9SDimitry Andric   } else {
357e8d8bef9SDimitry Andric     assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?");
358e8d8bef9SDimitry Andric     SccI->getSccEnterBlocks(LB.getSccNum(), Enters);
359e8d8bef9SDimitry Andric   }
360e8d8bef9SDimitry Andric }
361e8d8bef9SDimitry Andric 
362e8d8bef9SDimitry Andric void BranchProbabilityInfo::getLoopExitBlocks(
363e8d8bef9SDimitry Andric     const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Exits) const {
364e8d8bef9SDimitry Andric   if (LB.getLoop()) {
365e8d8bef9SDimitry Andric     LB.getLoop()->getExitBlocks(Exits);
366e8d8bef9SDimitry Andric   } else {
367e8d8bef9SDimitry Andric     assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?");
368e8d8bef9SDimitry Andric     SccI->getSccExitBlocks(LB.getSccNum(), Exits);
369e8d8bef9SDimitry Andric   }
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric // Propagate existing explicit probabilities from either profile data or
3730b57cec5SDimitry Andric // 'expect' intrinsic processing. Examine metadata against unreachable
3740b57cec5SDimitry Andric // heuristic. The probability of the edge coming to unreachable block is
3750b57cec5SDimitry Andric // set to min of metadata and unreachable heuristic.
3760b57cec5SDimitry Andric bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
3770b57cec5SDimitry Andric   const Instruction *TI = BB->getTerminator();
3780b57cec5SDimitry Andric   assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
3795ffd83dbSDimitry Andric   if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI) ||
3805ffd83dbSDimitry Andric         isa<InvokeInst>(TI)))
3810b57cec5SDimitry Andric     return false;
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
3840b57cec5SDimitry Andric   if (!WeightsNode)
3850b57cec5SDimitry Andric     return false;
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   // Check that the number of successors is manageable.
3880b57cec5SDimitry Andric   assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors");
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   // Ensure there are weights for all of the successors. Note that the first
3910b57cec5SDimitry Andric   // operand to the metadata node is a name, not a weight.
3920b57cec5SDimitry Andric   if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
3930b57cec5SDimitry Andric     return false;
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   // Build up the final weights that will be used in a temporary buffer.
3960b57cec5SDimitry Andric   // Compute the sum of all weights to later decide whether they need to
3970b57cec5SDimitry Andric   // be scaled to fit in 32 bits.
3980b57cec5SDimitry Andric   uint64_t WeightSum = 0;
3990b57cec5SDimitry Andric   SmallVector<uint32_t, 2> Weights;
4000b57cec5SDimitry Andric   SmallVector<unsigned, 2> UnreachableIdxs;
4010b57cec5SDimitry Andric   SmallVector<unsigned, 2> ReachableIdxs;
4020b57cec5SDimitry Andric   Weights.reserve(TI->getNumSuccessors());
4035ffd83dbSDimitry Andric   for (unsigned I = 1, E = WeightsNode->getNumOperands(); I != E; ++I) {
4040b57cec5SDimitry Andric     ConstantInt *Weight =
4055ffd83dbSDimitry Andric         mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(I));
4060b57cec5SDimitry Andric     if (!Weight)
4070b57cec5SDimitry Andric       return false;
4080b57cec5SDimitry Andric     assert(Weight->getValue().getActiveBits() <= 32 &&
4090b57cec5SDimitry Andric            "Too many bits for uint32_t");
4100b57cec5SDimitry Andric     Weights.push_back(Weight->getZExtValue());
4110b57cec5SDimitry Andric     WeightSum += Weights.back();
412e8d8bef9SDimitry Andric     const LoopBlock SrcLoopBB = getLoopBlock(BB);
413e8d8bef9SDimitry Andric     const LoopBlock DstLoopBB = getLoopBlock(TI->getSuccessor(I - 1));
414e8d8bef9SDimitry Andric     auto EstimatedWeight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
415e8d8bef9SDimitry Andric     if (EstimatedWeight &&
416e8d8bef9SDimitry Andric         EstimatedWeight.getValue() <=
417e8d8bef9SDimitry Andric             static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))
4185ffd83dbSDimitry Andric       UnreachableIdxs.push_back(I - 1);
4190b57cec5SDimitry Andric     else
4205ffd83dbSDimitry Andric       ReachableIdxs.push_back(I - 1);
4210b57cec5SDimitry Andric   }
4220b57cec5SDimitry Andric   assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   // If the sum of weights does not fit in 32 bits, scale every weight down
4250b57cec5SDimitry Andric   // accordingly.
4260b57cec5SDimitry Andric   uint64_t ScalingFactor =
4270b57cec5SDimitry Andric       (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric   if (ScalingFactor > 1) {
4300b57cec5SDimitry Andric     WeightSum = 0;
4315ffd83dbSDimitry Andric     for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) {
4325ffd83dbSDimitry Andric       Weights[I] /= ScalingFactor;
4335ffd83dbSDimitry Andric       WeightSum += Weights[I];
4340b57cec5SDimitry Andric     }
4350b57cec5SDimitry Andric   }
4360b57cec5SDimitry Andric   assert(WeightSum <= UINT32_MAX &&
4370b57cec5SDimitry Andric          "Expected weights to scale down to 32 bits");
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric   if (WeightSum == 0 || ReachableIdxs.size() == 0) {
4405ffd83dbSDimitry Andric     for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
4415ffd83dbSDimitry Andric       Weights[I] = 1;
4420b57cec5SDimitry Andric     WeightSum = TI->getNumSuccessors();
4430b57cec5SDimitry Andric   }
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   // Set the probability.
4460b57cec5SDimitry Andric   SmallVector<BranchProbability, 2> BP;
4475ffd83dbSDimitry Andric   for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
4485ffd83dbSDimitry Andric     BP.push_back({ Weights[I], static_cast<uint32_t>(WeightSum) });
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric   // Examine the metadata against unreachable heuristic.
4510b57cec5SDimitry Andric   // If the unreachable heuristic is more strong then we use it for this edge.
4525ffd83dbSDimitry Andric   if (UnreachableIdxs.size() == 0 || ReachableIdxs.size() == 0) {
4535ffd83dbSDimitry Andric     setEdgeProbability(BB, BP);
4545ffd83dbSDimitry Andric     return true;
4555ffd83dbSDimitry Andric   }
4565ffd83dbSDimitry Andric 
4570b57cec5SDimitry Andric   auto UnreachableProb = UR_TAKEN_PROB;
4585ffd83dbSDimitry Andric   for (auto I : UnreachableIdxs)
4595ffd83dbSDimitry Andric     if (UnreachableProb < BP[I]) {
4605ffd83dbSDimitry Andric       BP[I] = UnreachableProb;
4610b57cec5SDimitry Andric     }
4620b57cec5SDimitry Andric 
4635ffd83dbSDimitry Andric   // Sum of all edge probabilities must be 1.0. If we modified the probability
4645ffd83dbSDimitry Andric   // of some edges then we must distribute the introduced difference over the
4655ffd83dbSDimitry Andric   // reachable blocks.
4665ffd83dbSDimitry Andric   //
4675ffd83dbSDimitry Andric   // Proportional distribution: the relation between probabilities of the
4685ffd83dbSDimitry Andric   // reachable edges is kept unchanged. That is for any reachable edges i and j:
4695ffd83dbSDimitry Andric   //   newBP[i] / newBP[j] == oldBP[i] / oldBP[j] =>
4705ffd83dbSDimitry Andric   //   newBP[i] / oldBP[i] == newBP[j] / oldBP[j] == K
4715ffd83dbSDimitry Andric   // Where K is independent of i,j.
4725ffd83dbSDimitry Andric   //   newBP[i] == oldBP[i] * K
4735ffd83dbSDimitry Andric   // We need to find K.
4745ffd83dbSDimitry Andric   // Make sum of all reachables of the left and right parts:
4755ffd83dbSDimitry Andric   //   sum_of_reachable(newBP) == K * sum_of_reachable(oldBP)
4765ffd83dbSDimitry Andric   // Sum of newBP must be equal to 1.0:
4775ffd83dbSDimitry Andric   //   sum_of_reachable(newBP) + sum_of_unreachable(newBP) == 1.0 =>
4785ffd83dbSDimitry Andric   //   sum_of_reachable(newBP) = 1.0 - sum_of_unreachable(newBP)
4795ffd83dbSDimitry Andric   // Where sum_of_unreachable(newBP) is what has been just changed.
4805ffd83dbSDimitry Andric   // Finally:
4815ffd83dbSDimitry Andric   //   K == sum_of_reachable(newBP) / sum_of_reachable(oldBP) =>
4825ffd83dbSDimitry Andric   //   K == (1.0 - sum_of_unreachable(newBP)) / sum_of_reachable(oldBP)
4835ffd83dbSDimitry Andric   BranchProbability NewUnreachableSum = BranchProbability::getZero();
4845ffd83dbSDimitry Andric   for (auto I : UnreachableIdxs)
4855ffd83dbSDimitry Andric     NewUnreachableSum += BP[I];
4865ffd83dbSDimitry Andric 
4875ffd83dbSDimitry Andric   BranchProbability NewReachableSum =
4885ffd83dbSDimitry Andric       BranchProbability::getOne() - NewUnreachableSum;
4895ffd83dbSDimitry Andric 
4905ffd83dbSDimitry Andric   BranchProbability OldReachableSum = BranchProbability::getZero();
4915ffd83dbSDimitry Andric   for (auto I : ReachableIdxs)
4925ffd83dbSDimitry Andric     OldReachableSum += BP[I];
4935ffd83dbSDimitry Andric 
4945ffd83dbSDimitry Andric   if (OldReachableSum != NewReachableSum) { // Anything to dsitribute?
4955ffd83dbSDimitry Andric     if (OldReachableSum.isZero()) {
4965ffd83dbSDimitry Andric       // If all oldBP[i] are zeroes then the proportional distribution results
4975ffd83dbSDimitry Andric       // in all zero probabilities and the error stays big. In this case we
4985ffd83dbSDimitry Andric       // evenly spread NewReachableSum over the reachable edges.
4995ffd83dbSDimitry Andric       BranchProbability PerEdge = NewReachableSum / ReachableIdxs.size();
5005ffd83dbSDimitry Andric       for (auto I : ReachableIdxs)
5015ffd83dbSDimitry Andric         BP[I] = PerEdge;
5025ffd83dbSDimitry Andric     } else {
5035ffd83dbSDimitry Andric       for (auto I : ReachableIdxs) {
5045ffd83dbSDimitry Andric         // We use uint64_t to avoid double rounding error of the following
5055ffd83dbSDimitry Andric         // calculation: BP[i] = BP[i] * NewReachableSum / OldReachableSum
5065ffd83dbSDimitry Andric         // The formula is taken from the private constructor
5075ffd83dbSDimitry Andric         // BranchProbability(uint32_t Numerator, uint32_t Denominator)
5085ffd83dbSDimitry Andric         uint64_t Mul = static_cast<uint64_t>(NewReachableSum.getNumerator()) *
5095ffd83dbSDimitry Andric                        BP[I].getNumerator();
5105ffd83dbSDimitry Andric         uint32_t Div = static_cast<uint32_t>(
5115ffd83dbSDimitry Andric             divideNearest(Mul, OldReachableSum.getNumerator()));
5125ffd83dbSDimitry Andric         BP[I] = BranchProbability::getRaw(Div);
5135ffd83dbSDimitry Andric       }
5140b57cec5SDimitry Andric     }
5150b57cec5SDimitry Andric   }
5160b57cec5SDimitry Andric 
5175ffd83dbSDimitry Andric   setEdgeProbability(BB, BP);
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   return true;
5200b57cec5SDimitry Andric }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric // Calculate Edge Weights using "Pointer Heuristics". Predict a comparison
5230b57cec5SDimitry Andric // between two pointer or pointer and NULL will fail.
5240b57cec5SDimitry Andric bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) {
5250b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
5260b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
5270b57cec5SDimitry Andric     return false;
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric   Value *Cond = BI->getCondition();
5300b57cec5SDimitry Andric   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
5310b57cec5SDimitry Andric   if (!CI || !CI->isEquality())
5320b57cec5SDimitry Andric     return false;
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   Value *LHS = CI->getOperand(0);
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   if (!LHS->getType()->isPointerTy())
5370b57cec5SDimitry Andric     return false;
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric   assert(CI->getOperand(1)->getType()->isPointerTy());
5400b57cec5SDimitry Andric 
541*4824e7fdSDimitry Andric   auto Search = PointerTable.find(CI->getPredicate());
542*4824e7fdSDimitry Andric   if (Search == PointerTable.end())
543*4824e7fdSDimitry Andric     return false;
544*4824e7fdSDimitry Andric   setEdgeProbability(BB, Search->second);
5450b57cec5SDimitry Andric   return true;
5460b57cec5SDimitry Andric }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric // Compute the unlikely successors to the block BB in the loop L, specifically
5490b57cec5SDimitry Andric // those that are unlikely because this is a loop, and add them to the
5500b57cec5SDimitry Andric // UnlikelyBlocks set.
5510b57cec5SDimitry Andric static void
5520b57cec5SDimitry Andric computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,
5530b57cec5SDimitry Andric                           SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) {
5540b57cec5SDimitry Andric   // Sometimes in a loop we have a branch whose condition is made false by
5550b57cec5SDimitry Andric   // taking it. This is typically something like
5560b57cec5SDimitry Andric   //  int n = 0;
5570b57cec5SDimitry Andric   //  while (...) {
5580b57cec5SDimitry Andric   //    if (++n >= MAX) {
5590b57cec5SDimitry Andric   //      n = 0;
5600b57cec5SDimitry Andric   //    }
5610b57cec5SDimitry Andric   //  }
5620b57cec5SDimitry Andric   // In this sort of situation taking the branch means that at the very least it
5630b57cec5SDimitry Andric   // won't be taken again in the next iteration of the loop, so we should
5640b57cec5SDimitry Andric   // consider it less likely than a typical branch.
5650b57cec5SDimitry Andric   //
5660b57cec5SDimitry Andric   // We detect this by looking back through the graph of PHI nodes that sets the
5670b57cec5SDimitry Andric   // value that the condition depends on, and seeing if we can reach a successor
5680b57cec5SDimitry Andric   // block which can be determined to make the condition false.
5690b57cec5SDimitry Andric   //
5700b57cec5SDimitry Andric   // FIXME: We currently consider unlikely blocks to be half as likely as other
5710b57cec5SDimitry Andric   // blocks, but if we consider the example above the likelyhood is actually
5720b57cec5SDimitry Andric   // 1/MAX. We could therefore be more precise in how unlikely we consider
5730b57cec5SDimitry Andric   // blocks to be, but it would require more careful examination of the form
5740b57cec5SDimitry Andric   // of the comparison expression.
5750b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
5760b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
5770b57cec5SDimitry Andric     return;
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric   // Check if the branch is based on an instruction compared with a constant
5800b57cec5SDimitry Andric   CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
5810b57cec5SDimitry Andric   if (!CI || !isa<Instruction>(CI->getOperand(0)) ||
5820b57cec5SDimitry Andric       !isa<Constant>(CI->getOperand(1)))
5830b57cec5SDimitry Andric     return;
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric   // Either the instruction must be a PHI, or a chain of operations involving
5860b57cec5SDimitry Andric   // constants that ends in a PHI which we can then collapse into a single value
5870b57cec5SDimitry Andric   // if the PHI value is known.
5880b57cec5SDimitry Andric   Instruction *CmpLHS = dyn_cast<Instruction>(CI->getOperand(0));
5890b57cec5SDimitry Andric   PHINode *CmpPHI = dyn_cast<PHINode>(CmpLHS);
5900b57cec5SDimitry Andric   Constant *CmpConst = dyn_cast<Constant>(CI->getOperand(1));
5910b57cec5SDimitry Andric   // Collect the instructions until we hit a PHI
5920b57cec5SDimitry Andric   SmallVector<BinaryOperator *, 1> InstChain;
5930b57cec5SDimitry Andric   while (!CmpPHI && CmpLHS && isa<BinaryOperator>(CmpLHS) &&
5940b57cec5SDimitry Andric          isa<Constant>(CmpLHS->getOperand(1))) {
5950b57cec5SDimitry Andric     // Stop if the chain extends outside of the loop
5960b57cec5SDimitry Andric     if (!L->contains(CmpLHS))
5970b57cec5SDimitry Andric       return;
5980b57cec5SDimitry Andric     InstChain.push_back(cast<BinaryOperator>(CmpLHS));
5990b57cec5SDimitry Andric     CmpLHS = dyn_cast<Instruction>(CmpLHS->getOperand(0));
6000b57cec5SDimitry Andric     if (CmpLHS)
6010b57cec5SDimitry Andric       CmpPHI = dyn_cast<PHINode>(CmpLHS);
6020b57cec5SDimitry Andric   }
6030b57cec5SDimitry Andric   if (!CmpPHI || !L->contains(CmpPHI))
6040b57cec5SDimitry Andric     return;
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   // Trace the phi node to find all values that come from successors of BB
6070b57cec5SDimitry Andric   SmallPtrSet<PHINode*, 8> VisitedInsts;
6080b57cec5SDimitry Andric   SmallVector<PHINode*, 8> WorkList;
6090b57cec5SDimitry Andric   WorkList.push_back(CmpPHI);
6100b57cec5SDimitry Andric   VisitedInsts.insert(CmpPHI);
6110b57cec5SDimitry Andric   while (!WorkList.empty()) {
612fe6060f1SDimitry Andric     PHINode *P = WorkList.pop_back_val();
6130b57cec5SDimitry Andric     for (BasicBlock *B : P->blocks()) {
6140b57cec5SDimitry Andric       // Skip blocks that aren't part of the loop
6150b57cec5SDimitry Andric       if (!L->contains(B))
6160b57cec5SDimitry Andric         continue;
6170b57cec5SDimitry Andric       Value *V = P->getIncomingValueForBlock(B);
6180b57cec5SDimitry Andric       // If the source is a PHI add it to the work list if we haven't
6190b57cec5SDimitry Andric       // already visited it.
6200b57cec5SDimitry Andric       if (PHINode *PN = dyn_cast<PHINode>(V)) {
6210b57cec5SDimitry Andric         if (VisitedInsts.insert(PN).second)
6220b57cec5SDimitry Andric           WorkList.push_back(PN);
6230b57cec5SDimitry Andric         continue;
6240b57cec5SDimitry Andric       }
6250b57cec5SDimitry Andric       // If this incoming value is a constant and B is a successor of BB, then
6260b57cec5SDimitry Andric       // we can constant-evaluate the compare to see if it makes the branch be
6270b57cec5SDimitry Andric       // taken or not.
6280b57cec5SDimitry Andric       Constant *CmpLHSConst = dyn_cast<Constant>(V);
629e8d8bef9SDimitry Andric       if (!CmpLHSConst || !llvm::is_contained(successors(BB), B))
6300b57cec5SDimitry Andric         continue;
6310b57cec5SDimitry Andric       // First collapse InstChain
6320b57cec5SDimitry Andric       for (Instruction *I : llvm::reverse(InstChain)) {
6330b57cec5SDimitry Andric         CmpLHSConst = ConstantExpr::get(I->getOpcode(), CmpLHSConst,
6340b57cec5SDimitry Andric                                         cast<Constant>(I->getOperand(1)), true);
6350b57cec5SDimitry Andric         if (!CmpLHSConst)
6360b57cec5SDimitry Andric           break;
6370b57cec5SDimitry Andric       }
6380b57cec5SDimitry Andric       if (!CmpLHSConst)
6390b57cec5SDimitry Andric         continue;
6400b57cec5SDimitry Andric       // Now constant-evaluate the compare
6410b57cec5SDimitry Andric       Constant *Result = ConstantExpr::getCompare(CI->getPredicate(),
6420b57cec5SDimitry Andric                                                   CmpLHSConst, CmpConst, true);
6430b57cec5SDimitry Andric       // If the result means we don't branch to the block then that block is
6440b57cec5SDimitry Andric       // unlikely.
6450b57cec5SDimitry Andric       if (Result &&
6460b57cec5SDimitry Andric           ((Result->isZeroValue() && B == BI->getSuccessor(0)) ||
6470b57cec5SDimitry Andric            (Result->isOneValue() && B == BI->getSuccessor(1))))
6480b57cec5SDimitry Andric         UnlikelyBlocks.insert(B);
6490b57cec5SDimitry Andric     }
6500b57cec5SDimitry Andric   }
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric 
653e8d8bef9SDimitry Andric Optional<uint32_t>
654e8d8bef9SDimitry Andric BranchProbabilityInfo::getEstimatedBlockWeight(const BasicBlock *BB) const {
655e8d8bef9SDimitry Andric   auto WeightIt = EstimatedBlockWeight.find(BB);
656e8d8bef9SDimitry Andric   if (WeightIt == EstimatedBlockWeight.end())
657e8d8bef9SDimitry Andric     return None;
658e8d8bef9SDimitry Andric   return WeightIt->second;
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric 
661e8d8bef9SDimitry Andric Optional<uint32_t>
662e8d8bef9SDimitry Andric BranchProbabilityInfo::getEstimatedLoopWeight(const LoopData &L) const {
663e8d8bef9SDimitry Andric   auto WeightIt = EstimatedLoopWeight.find(L);
664e8d8bef9SDimitry Andric   if (WeightIt == EstimatedLoopWeight.end())
665e8d8bef9SDimitry Andric     return None;
666e8d8bef9SDimitry Andric   return WeightIt->second;
667e8d8bef9SDimitry Andric }
668e8d8bef9SDimitry Andric 
669e8d8bef9SDimitry Andric Optional<uint32_t>
670e8d8bef9SDimitry Andric BranchProbabilityInfo::getEstimatedEdgeWeight(const LoopEdge &Edge) const {
671e8d8bef9SDimitry Andric   // For edges entering a loop take weight of a loop rather than an individual
672e8d8bef9SDimitry Andric   // block in the loop.
673e8d8bef9SDimitry Andric   return isLoopEnteringEdge(Edge)
674e8d8bef9SDimitry Andric              ? getEstimatedLoopWeight(Edge.second.getLoopData())
675e8d8bef9SDimitry Andric              : getEstimatedBlockWeight(Edge.second.getBlock());
676e8d8bef9SDimitry Andric }
677e8d8bef9SDimitry Andric 
678e8d8bef9SDimitry Andric template <class IterT>
679e8d8bef9SDimitry Andric Optional<uint32_t> BranchProbabilityInfo::getMaxEstimatedEdgeWeight(
680e8d8bef9SDimitry Andric     const LoopBlock &SrcLoopBB, iterator_range<IterT> Successors) const {
681e8d8bef9SDimitry Andric   SmallVector<uint32_t, 4> Weights;
682e8d8bef9SDimitry Andric   Optional<uint32_t> MaxWeight;
683e8d8bef9SDimitry Andric   for (const BasicBlock *DstBB : Successors) {
684e8d8bef9SDimitry Andric     const LoopBlock DstLoopBB = getLoopBlock(DstBB);
685e8d8bef9SDimitry Andric     auto Weight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
686e8d8bef9SDimitry Andric 
687e8d8bef9SDimitry Andric     if (!Weight)
688e8d8bef9SDimitry Andric       return None;
689e8d8bef9SDimitry Andric 
690e8d8bef9SDimitry Andric     if (!MaxWeight || MaxWeight.getValue() < Weight.getValue())
691e8d8bef9SDimitry Andric       MaxWeight = Weight;
692e8d8bef9SDimitry Andric   }
693e8d8bef9SDimitry Andric 
694e8d8bef9SDimitry Andric   return MaxWeight;
695e8d8bef9SDimitry Andric }
696e8d8bef9SDimitry Andric 
697e8d8bef9SDimitry Andric // Updates \p LoopBB's weight and returns true. If \p LoopBB has already
698e8d8bef9SDimitry Andric // an associated weight it is unchanged and false is returned.
699e8d8bef9SDimitry Andric //
700e8d8bef9SDimitry Andric // Please note by the algorithm the weight is not expected to change once set
701e8d8bef9SDimitry Andric // thus 'false' status is used to track visited blocks.
702e8d8bef9SDimitry Andric bool BranchProbabilityInfo::updateEstimatedBlockWeight(
703e8d8bef9SDimitry Andric     LoopBlock &LoopBB, uint32_t BBWeight,
704e8d8bef9SDimitry Andric     SmallVectorImpl<BasicBlock *> &BlockWorkList,
705e8d8bef9SDimitry Andric     SmallVectorImpl<LoopBlock> &LoopWorkList) {
706e8d8bef9SDimitry Andric   BasicBlock *BB = LoopBB.getBlock();
707e8d8bef9SDimitry Andric 
708e8d8bef9SDimitry Andric   // In general, weight is assigned to a block when it has final value and
709e8d8bef9SDimitry Andric   // can't/shouldn't be changed.  However, there are cases when a block
710e8d8bef9SDimitry Andric   // inherently has several (possibly "contradicting") weights. For example,
711e8d8bef9SDimitry Andric   // "unwind" block may also contain "cold" call. In that case the first
712e8d8bef9SDimitry Andric   // set weight is favored and all consequent weights are ignored.
713e8d8bef9SDimitry Andric   if (!EstimatedBlockWeight.insert({BB, BBWeight}).second)
714e8d8bef9SDimitry Andric     return false;
715e8d8bef9SDimitry Andric 
716e8d8bef9SDimitry Andric   for (BasicBlock *PredBlock : predecessors(BB)) {
717e8d8bef9SDimitry Andric     LoopBlock PredLoop = getLoopBlock(PredBlock);
718e8d8bef9SDimitry Andric     // Add affected block/loop to a working list.
719e8d8bef9SDimitry Andric     if (isLoopExitingEdge({PredLoop, LoopBB})) {
720e8d8bef9SDimitry Andric       if (!EstimatedLoopWeight.count(PredLoop.getLoopData()))
721e8d8bef9SDimitry Andric         LoopWorkList.push_back(PredLoop);
722e8d8bef9SDimitry Andric     } else if (!EstimatedBlockWeight.count(PredBlock))
723e8d8bef9SDimitry Andric       BlockWorkList.push_back(PredBlock);
724e8d8bef9SDimitry Andric   }
725e8d8bef9SDimitry Andric   return true;
726e8d8bef9SDimitry Andric }
727e8d8bef9SDimitry Andric 
728e8d8bef9SDimitry Andric // Starting from \p BB traverse through dominator blocks and assign \p BBWeight
729e8d8bef9SDimitry Andric // to all such blocks that are post dominated by \BB. In other words to all
730e8d8bef9SDimitry Andric // blocks that the one is executed if and only if another one is executed.
731e8d8bef9SDimitry Andric // Importantly, we skip loops here for two reasons. First weights of blocks in
732e8d8bef9SDimitry Andric // a loop should be scaled by trip count (yet possibly unknown). Second there is
733e8d8bef9SDimitry Andric // no any value in doing that because that doesn't give any additional
734e8d8bef9SDimitry Andric // information regarding distribution of probabilities inside the loop.
735e8d8bef9SDimitry Andric // Exception is loop 'enter' and 'exit' edges that are handled in a special way
736e8d8bef9SDimitry Andric // at calcEstimatedHeuristics.
737e8d8bef9SDimitry Andric //
738e8d8bef9SDimitry Andric // In addition, \p WorkList is populated with basic blocks if at leas one
739e8d8bef9SDimitry Andric // successor has updated estimated weight.
740e8d8bef9SDimitry Andric void BranchProbabilityInfo::propagateEstimatedBlockWeight(
741e8d8bef9SDimitry Andric     const LoopBlock &LoopBB, DominatorTree *DT, PostDominatorTree *PDT,
742e8d8bef9SDimitry Andric     uint32_t BBWeight, SmallVectorImpl<BasicBlock *> &BlockWorkList,
743e8d8bef9SDimitry Andric     SmallVectorImpl<LoopBlock> &LoopWorkList) {
744e8d8bef9SDimitry Andric   const BasicBlock *BB = LoopBB.getBlock();
745e8d8bef9SDimitry Andric   const auto *DTStartNode = DT->getNode(BB);
746e8d8bef9SDimitry Andric   const auto *PDTStartNode = PDT->getNode(BB);
747e8d8bef9SDimitry Andric 
748e8d8bef9SDimitry Andric   // TODO: Consider propagating weight down the domination line as well.
749e8d8bef9SDimitry Andric   for (const auto *DTNode = DTStartNode; DTNode != nullptr;
750e8d8bef9SDimitry Andric        DTNode = DTNode->getIDom()) {
751e8d8bef9SDimitry Andric     auto *DomBB = DTNode->getBlock();
752e8d8bef9SDimitry Andric     // Consider blocks which lie on one 'line'.
753e8d8bef9SDimitry Andric     if (!PDT->dominates(PDTStartNode, PDT->getNode(DomBB)))
754e8d8bef9SDimitry Andric       // If BB doesn't post dominate DomBB it will not post dominate dominators
755e8d8bef9SDimitry Andric       // of DomBB as well.
756e8d8bef9SDimitry Andric       break;
757e8d8bef9SDimitry Andric 
758e8d8bef9SDimitry Andric     LoopBlock DomLoopBB = getLoopBlock(DomBB);
759e8d8bef9SDimitry Andric     const LoopEdge Edge{DomLoopBB, LoopBB};
760e8d8bef9SDimitry Andric     // Don't propagate weight to blocks belonging to different loops.
761e8d8bef9SDimitry Andric     if (!isLoopEnteringExitingEdge(Edge)) {
762e8d8bef9SDimitry Andric       if (!updateEstimatedBlockWeight(DomLoopBB, BBWeight, BlockWorkList,
763e8d8bef9SDimitry Andric                                       LoopWorkList))
764e8d8bef9SDimitry Andric         // If DomBB has weight set then all it's predecessors are already
765e8d8bef9SDimitry Andric         // processed (since we propagate weight up to the top of IR each time).
766e8d8bef9SDimitry Andric         break;
767e8d8bef9SDimitry Andric     } else if (isLoopExitingEdge(Edge)) {
768e8d8bef9SDimitry Andric       LoopWorkList.push_back(DomLoopBB);
769e8d8bef9SDimitry Andric     }
770e8d8bef9SDimitry Andric   }
771e8d8bef9SDimitry Andric }
772e8d8bef9SDimitry Andric 
773e8d8bef9SDimitry Andric Optional<uint32_t> BranchProbabilityInfo::getInitialEstimatedBlockWeight(
774e8d8bef9SDimitry Andric     const BasicBlock *BB) {
775e8d8bef9SDimitry Andric   // Returns true if \p BB has call marked with "NoReturn" attribute.
776e8d8bef9SDimitry Andric   auto hasNoReturn = [&](const BasicBlock *BB) {
777e8d8bef9SDimitry Andric     for (const auto &I : reverse(*BB))
778e8d8bef9SDimitry Andric       if (const CallInst *CI = dyn_cast<CallInst>(&I))
779e8d8bef9SDimitry Andric         if (CI->hasFnAttr(Attribute::NoReturn))
780e8d8bef9SDimitry Andric           return true;
781e8d8bef9SDimitry Andric 
782e8d8bef9SDimitry Andric     return false;
783e8d8bef9SDimitry Andric   };
784e8d8bef9SDimitry Andric 
785e8d8bef9SDimitry Andric   // Important note regarding the order of checks. They are ordered by weight
786e8d8bef9SDimitry Andric   // from lowest to highest. Doing that allows to avoid "unstable" results
787e8d8bef9SDimitry Andric   // when several conditions heuristics can be applied simultaneously.
788e8d8bef9SDimitry Andric   if (isa<UnreachableInst>(BB->getTerminator()) ||
789e8d8bef9SDimitry Andric       // If this block is terminated by a call to
790e8d8bef9SDimitry Andric       // @llvm.experimental.deoptimize then treat it like an unreachable
791e8d8bef9SDimitry Andric       // since it is expected to practically never execute.
792e8d8bef9SDimitry Andric       // TODO: Should we actually treat as never returning call?
793e8d8bef9SDimitry Andric       BB->getTerminatingDeoptimizeCall())
794e8d8bef9SDimitry Andric     return hasNoReturn(BB)
795e8d8bef9SDimitry Andric                ? static_cast<uint32_t>(BlockExecWeight::NORETURN)
796e8d8bef9SDimitry Andric                : static_cast<uint32_t>(BlockExecWeight::UNREACHABLE);
797e8d8bef9SDimitry Andric 
798e8d8bef9SDimitry Andric   // Check if the block is 'unwind' handler of  some invoke instruction.
799e8d8bef9SDimitry Andric   for (const auto *Pred : predecessors(BB))
800e8d8bef9SDimitry Andric     if (Pred)
801e8d8bef9SDimitry Andric       if (const auto *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
802e8d8bef9SDimitry Andric         if (II->getUnwindDest() == BB)
803e8d8bef9SDimitry Andric           return static_cast<uint32_t>(BlockExecWeight::UNWIND);
804e8d8bef9SDimitry Andric 
805e8d8bef9SDimitry Andric   // Check if the block contains 'cold' call.
806e8d8bef9SDimitry Andric   for (const auto &I : *BB)
807e8d8bef9SDimitry Andric     if (const CallInst *CI = dyn_cast<CallInst>(&I))
808e8d8bef9SDimitry Andric       if (CI->hasFnAttr(Attribute::Cold))
809e8d8bef9SDimitry Andric         return static_cast<uint32_t>(BlockExecWeight::COLD);
810e8d8bef9SDimitry Andric 
811e8d8bef9SDimitry Andric   return None;
812e8d8bef9SDimitry Andric }
813e8d8bef9SDimitry Andric 
814e8d8bef9SDimitry Andric // Does RPO traversal over all blocks in \p F and assigns weights to
815e8d8bef9SDimitry Andric // 'unreachable', 'noreturn', 'cold', 'unwind' blocks. In addition it does its
816e8d8bef9SDimitry Andric // best to propagate the weight to up/down the IR.
817e8d8bef9SDimitry Andric void BranchProbabilityInfo::computeEestimateBlockWeight(
818e8d8bef9SDimitry Andric     const Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
819e8d8bef9SDimitry Andric   SmallVector<BasicBlock *, 8> BlockWorkList;
820e8d8bef9SDimitry Andric   SmallVector<LoopBlock, 8> LoopWorkList;
821e8d8bef9SDimitry Andric 
822e8d8bef9SDimitry Andric   // By doing RPO we make sure that all predecessors already have weights
823e8d8bef9SDimitry Andric   // calculated before visiting theirs successors.
824e8d8bef9SDimitry Andric   ReversePostOrderTraversal<const Function *> RPOT(&F);
825e8d8bef9SDimitry Andric   for (const auto *BB : RPOT)
826e8d8bef9SDimitry Andric     if (auto BBWeight = getInitialEstimatedBlockWeight(BB))
827e8d8bef9SDimitry Andric       // If we were able to find estimated weight for the block set it to this
828e8d8bef9SDimitry Andric       // block and propagate up the IR.
829e8d8bef9SDimitry Andric       propagateEstimatedBlockWeight(getLoopBlock(BB), DT, PDT,
830e8d8bef9SDimitry Andric                                     BBWeight.getValue(), BlockWorkList,
831e8d8bef9SDimitry Andric                                     LoopWorkList);
832e8d8bef9SDimitry Andric 
833e8d8bef9SDimitry Andric   // BlockWorklist/LoopWorkList contains blocks/loops with at least one
834e8d8bef9SDimitry Andric   // successor/exit having estimated weight. Try to propagate weight to such
835e8d8bef9SDimitry Andric   // blocks/loops from successors/exits.
836e8d8bef9SDimitry Andric   // Process loops and blocks. Order is not important.
837e8d8bef9SDimitry Andric   do {
838e8d8bef9SDimitry Andric     while (!LoopWorkList.empty()) {
839e8d8bef9SDimitry Andric       const LoopBlock LoopBB = LoopWorkList.pop_back_val();
840e8d8bef9SDimitry Andric 
841e8d8bef9SDimitry Andric       if (EstimatedLoopWeight.count(LoopBB.getLoopData()))
842e8d8bef9SDimitry Andric         continue;
843e8d8bef9SDimitry Andric 
844e8d8bef9SDimitry Andric       SmallVector<BasicBlock *, 4> Exits;
845e8d8bef9SDimitry Andric       getLoopExitBlocks(LoopBB, Exits);
846e8d8bef9SDimitry Andric       auto LoopWeight = getMaxEstimatedEdgeWeight(
847e8d8bef9SDimitry Andric           LoopBB, make_range(Exits.begin(), Exits.end()));
848e8d8bef9SDimitry Andric 
849e8d8bef9SDimitry Andric       if (LoopWeight) {
850e8d8bef9SDimitry Andric         // If we never exit the loop then we can enter it once at maximum.
851e8d8bef9SDimitry Andric         if (LoopWeight <= static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))
852e8d8bef9SDimitry Andric           LoopWeight = static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO);
853e8d8bef9SDimitry Andric 
854e8d8bef9SDimitry Andric         EstimatedLoopWeight.insert(
855e8d8bef9SDimitry Andric             {LoopBB.getLoopData(), LoopWeight.getValue()});
856e8d8bef9SDimitry Andric         // Add all blocks entering the loop into working list.
857e8d8bef9SDimitry Andric         getLoopEnterBlocks(LoopBB, BlockWorkList);
858e8d8bef9SDimitry Andric       }
859e8d8bef9SDimitry Andric     }
860e8d8bef9SDimitry Andric 
861e8d8bef9SDimitry Andric     while (!BlockWorkList.empty()) {
862e8d8bef9SDimitry Andric       // We can reach here only if BlockWorkList is not empty.
863e8d8bef9SDimitry Andric       const BasicBlock *BB = BlockWorkList.pop_back_val();
864e8d8bef9SDimitry Andric       if (EstimatedBlockWeight.count(BB))
865e8d8bef9SDimitry Andric         continue;
866e8d8bef9SDimitry Andric 
867e8d8bef9SDimitry Andric       // We take maximum over all weights of successors. In other words we take
868e8d8bef9SDimitry Andric       // weight of "hot" path. In theory we can probably find a better function
869e8d8bef9SDimitry Andric       // which gives higher accuracy results (comparing to "maximum") but I
870e8d8bef9SDimitry Andric       // can't
871e8d8bef9SDimitry Andric       // think of any right now. And I doubt it will make any difference in
872e8d8bef9SDimitry Andric       // practice.
873e8d8bef9SDimitry Andric       const LoopBlock LoopBB = getLoopBlock(BB);
874e8d8bef9SDimitry Andric       auto MaxWeight = getMaxEstimatedEdgeWeight(LoopBB, successors(BB));
875e8d8bef9SDimitry Andric 
876e8d8bef9SDimitry Andric       if (MaxWeight)
877e8d8bef9SDimitry Andric         propagateEstimatedBlockWeight(LoopBB, DT, PDT, MaxWeight.getValue(),
878e8d8bef9SDimitry Andric                                       BlockWorkList, LoopWorkList);
879e8d8bef9SDimitry Andric     }
880e8d8bef9SDimitry Andric   } while (!BlockWorkList.empty() || !LoopWorkList.empty());
881e8d8bef9SDimitry Andric }
882e8d8bef9SDimitry Andric 
883e8d8bef9SDimitry Andric // Calculate edge probabilities based on block's estimated weight.
884e8d8bef9SDimitry Andric // Note that gathered weights were not scaled for loops. Thus edges entering
885e8d8bef9SDimitry Andric // and exiting loops requires special processing.
886e8d8bef9SDimitry Andric bool BranchProbabilityInfo::calcEstimatedHeuristics(const BasicBlock *BB) {
887e8d8bef9SDimitry Andric   assert(BB->getTerminator()->getNumSuccessors() > 1 &&
888e8d8bef9SDimitry Andric          "expected more than one successor!");
889e8d8bef9SDimitry Andric 
890e8d8bef9SDimitry Andric   const LoopBlock LoopBB = getLoopBlock(BB);
891e8d8bef9SDimitry Andric 
8920b57cec5SDimitry Andric   SmallPtrSet<const BasicBlock *, 8> UnlikelyBlocks;
893e8d8bef9SDimitry Andric   uint32_t TC = LBH_TAKEN_WEIGHT / LBH_NONTAKEN_WEIGHT;
894e8d8bef9SDimitry Andric   if (LoopBB.getLoop())
895e8d8bef9SDimitry Andric     computeUnlikelySuccessors(BB, LoopBB.getLoop(), UnlikelyBlocks);
8960b57cec5SDimitry Andric 
897e8d8bef9SDimitry Andric   // Changed to 'true' if at least one successor has estimated weight.
898e8d8bef9SDimitry Andric   bool FoundEstimatedWeight = false;
899e8d8bef9SDimitry Andric   SmallVector<uint32_t, 4> SuccWeights;
900e8d8bef9SDimitry Andric   uint64_t TotalWeight = 0;
901e8d8bef9SDimitry Andric   // Go over all successors of BB and put their weights into SuccWeights.
902fe6060f1SDimitry Andric   for (const BasicBlock *SuccBB : successors(BB)) {
903e8d8bef9SDimitry Andric     Optional<uint32_t> Weight;
904e8d8bef9SDimitry Andric     const LoopBlock SuccLoopBB = getLoopBlock(SuccBB);
905e8d8bef9SDimitry Andric     const LoopEdge Edge{LoopBB, SuccLoopBB};
906e8d8bef9SDimitry Andric 
907e8d8bef9SDimitry Andric     Weight = getEstimatedEdgeWeight(Edge);
908e8d8bef9SDimitry Andric 
909e8d8bef9SDimitry Andric     if (isLoopExitingEdge(Edge) &&
910e8d8bef9SDimitry Andric         // Avoid adjustment of ZERO weight since it should remain unchanged.
911e8d8bef9SDimitry Andric         Weight != static_cast<uint32_t>(BlockExecWeight::ZERO)) {
912e8d8bef9SDimitry Andric       // Scale down loop exiting weight by trip count.
913e8d8bef9SDimitry Andric       Weight = std::max(
914e8d8bef9SDimitry Andric           static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO),
915e8d8bef9SDimitry Andric           Weight.getValueOr(static_cast<uint32_t>(BlockExecWeight::DEFAULT)) /
916e8d8bef9SDimitry Andric               TC);
9170b57cec5SDimitry Andric     }
918e8d8bef9SDimitry Andric     bool IsUnlikelyEdge = LoopBB.getLoop() && UnlikelyBlocks.contains(SuccBB);
919e8d8bef9SDimitry Andric     if (IsUnlikelyEdge &&
920e8d8bef9SDimitry Andric         // Avoid adjustment of ZERO weight since it should remain unchanged.
921e8d8bef9SDimitry Andric         Weight != static_cast<uint32_t>(BlockExecWeight::ZERO)) {
922e8d8bef9SDimitry Andric       // 'Unlikely' blocks have twice lower weight.
923e8d8bef9SDimitry Andric       Weight = std::max(
924e8d8bef9SDimitry Andric           static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO),
925e8d8bef9SDimitry Andric           Weight.getValueOr(static_cast<uint32_t>(BlockExecWeight::DEFAULT)) /
926e8d8bef9SDimitry Andric               2);
9270b57cec5SDimitry Andric     }
9280b57cec5SDimitry Andric 
929e8d8bef9SDimitry Andric     if (Weight)
930e8d8bef9SDimitry Andric       FoundEstimatedWeight = true;
931e8d8bef9SDimitry Andric 
932e8d8bef9SDimitry Andric     auto WeightVal =
933e8d8bef9SDimitry Andric         Weight.getValueOr(static_cast<uint32_t>(BlockExecWeight::DEFAULT));
934e8d8bef9SDimitry Andric     TotalWeight += WeightVal;
935e8d8bef9SDimitry Andric     SuccWeights.push_back(WeightVal);
936e8d8bef9SDimitry Andric   }
937e8d8bef9SDimitry Andric 
938e8d8bef9SDimitry Andric   // If non of blocks have estimated weight bail out.
939e8d8bef9SDimitry Andric   // If TotalWeight is 0 that means weight of each successor is 0 as well and
940e8d8bef9SDimitry Andric   // equally likely. Bail out early to not deal with devision by zero.
941e8d8bef9SDimitry Andric   if (!FoundEstimatedWeight || TotalWeight == 0)
9420b57cec5SDimitry Andric     return false;
9430b57cec5SDimitry Andric 
944e8d8bef9SDimitry Andric   assert(SuccWeights.size() == succ_size(BB) && "Missed successor?");
945e8d8bef9SDimitry Andric   const unsigned SuccCount = SuccWeights.size();
9460b57cec5SDimitry Andric 
947e8d8bef9SDimitry Andric   // If the sum of weights does not fit in 32 bits, scale every weight down
948e8d8bef9SDimitry Andric   // accordingly.
949e8d8bef9SDimitry Andric   if (TotalWeight > UINT32_MAX) {
950e8d8bef9SDimitry Andric     uint64_t ScalingFactor = TotalWeight / UINT32_MAX + 1;
951e8d8bef9SDimitry Andric     TotalWeight = 0;
952e8d8bef9SDimitry Andric     for (unsigned Idx = 0; Idx < SuccCount; ++Idx) {
953e8d8bef9SDimitry Andric       SuccWeights[Idx] /= ScalingFactor;
954e8d8bef9SDimitry Andric       if (SuccWeights[Idx] == static_cast<uint32_t>(BlockExecWeight::ZERO))
955e8d8bef9SDimitry Andric         SuccWeights[Idx] =
956e8d8bef9SDimitry Andric             static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO);
957e8d8bef9SDimitry Andric       TotalWeight += SuccWeights[Idx];
958e8d8bef9SDimitry Andric     }
959e8d8bef9SDimitry Andric     assert(TotalWeight <= UINT32_MAX && "Total weight overflows");
960e8d8bef9SDimitry Andric   }
961e8d8bef9SDimitry Andric 
962e8d8bef9SDimitry Andric   // Finally set probabilities to edges according to estimated block weights.
9635ffd83dbSDimitry Andric   SmallVector<BranchProbability, 4> EdgeProbabilities(
964e8d8bef9SDimitry Andric       SuccCount, BranchProbability::getUnknown());
9650b57cec5SDimitry Andric 
966e8d8bef9SDimitry Andric   for (unsigned Idx = 0; Idx < SuccCount; ++Idx) {
967e8d8bef9SDimitry Andric     EdgeProbabilities[Idx] =
968e8d8bef9SDimitry Andric         BranchProbability(SuccWeights[Idx], (uint32_t)TotalWeight);
9690b57cec5SDimitry Andric   }
9705ffd83dbSDimitry Andric   setEdgeProbability(BB, EdgeProbabilities);
9710b57cec5SDimitry Andric   return true;
9720b57cec5SDimitry Andric }
9730b57cec5SDimitry Andric 
9740b57cec5SDimitry Andric bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB,
9750b57cec5SDimitry Andric                                                const TargetLibraryInfo *TLI) {
9760b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
9770b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
9780b57cec5SDimitry Andric     return false;
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric   Value *Cond = BI->getCondition();
9810b57cec5SDimitry Andric   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
9820b57cec5SDimitry Andric   if (!CI)
9830b57cec5SDimitry Andric     return false;
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   auto GetConstantInt = [](Value *V) {
9860b57cec5SDimitry Andric     if (auto *I = dyn_cast<BitCastInst>(V))
9870b57cec5SDimitry Andric       return dyn_cast<ConstantInt>(I->getOperand(0));
9880b57cec5SDimitry Andric     return dyn_cast<ConstantInt>(V);
9890b57cec5SDimitry Andric   };
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   Value *RHS = CI->getOperand(1);
9920b57cec5SDimitry Andric   ConstantInt *CV = GetConstantInt(RHS);
9930b57cec5SDimitry Andric   if (!CV)
9940b57cec5SDimitry Andric     return false;
9950b57cec5SDimitry Andric 
9960b57cec5SDimitry Andric   // If the LHS is the result of AND'ing a value with a single bit bitmask,
9970b57cec5SDimitry Andric   // we don't have information about probabilities.
9980b57cec5SDimitry Andric   if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0)))
9990b57cec5SDimitry Andric     if (LHS->getOpcode() == Instruction::And)
1000e8d8bef9SDimitry Andric       if (ConstantInt *AndRHS = GetConstantInt(LHS->getOperand(1)))
10010b57cec5SDimitry Andric         if (AndRHS->getValue().isPowerOf2())
10020b57cec5SDimitry Andric           return false;
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric   // Check if the LHS is the return value of a library function
10050b57cec5SDimitry Andric   LibFunc Func = NumLibFuncs;
10060b57cec5SDimitry Andric   if (TLI)
10070b57cec5SDimitry Andric     if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0)))
10080b57cec5SDimitry Andric       if (Function *CalledFn = Call->getCalledFunction())
10090b57cec5SDimitry Andric         TLI->getLibFunc(*CalledFn, Func);
10100b57cec5SDimitry Andric 
1011*4824e7fdSDimitry Andric   ProbabilityTable::const_iterator Search;
10120b57cec5SDimitry Andric   if (Func == LibFunc_strcasecmp ||
10130b57cec5SDimitry Andric       Func == LibFunc_strcmp ||
10140b57cec5SDimitry Andric       Func == LibFunc_strncasecmp ||
10150b57cec5SDimitry Andric       Func == LibFunc_strncmp ||
1016e8d8bef9SDimitry Andric       Func == LibFunc_memcmp ||
1017e8d8bef9SDimitry Andric       Func == LibFunc_bcmp) {
1018*4824e7fdSDimitry Andric     Search = ICmpWithLibCallTable.find(CI->getPredicate());
1019*4824e7fdSDimitry Andric     if (Search == ICmpWithLibCallTable.end())
10200b57cec5SDimitry Andric       return false;
10210b57cec5SDimitry Andric   } else if (CV->isZero()) {
1022*4824e7fdSDimitry Andric     Search = ICmpWithZeroTable.find(CI->getPredicate());
1023*4824e7fdSDimitry Andric     if (Search == ICmpWithZeroTable.end())
10240b57cec5SDimitry Andric       return false;
1025*4824e7fdSDimitry Andric   } else if (CV->isOne()) {
1026*4824e7fdSDimitry Andric     Search = ICmpWithOneTable.find(CI->getPredicate());
1027*4824e7fdSDimitry Andric     if (Search == ICmpWithOneTable.end())
1028*4824e7fdSDimitry Andric       return false;
10290b57cec5SDimitry Andric   } else if (CV->isMinusOne()) {
1030*4824e7fdSDimitry Andric     Search = ICmpWithMinusOneTable.find(CI->getPredicate());
1031*4824e7fdSDimitry Andric     if (Search == ICmpWithMinusOneTable.end())
10320b57cec5SDimitry Andric       return false;
10330b57cec5SDimitry Andric   } else {
10340b57cec5SDimitry Andric     return false;
10350b57cec5SDimitry Andric   }
10360b57cec5SDimitry Andric 
1037*4824e7fdSDimitry Andric   setEdgeProbability(BB, Search->second);
10380b57cec5SDimitry Andric   return true;
10390b57cec5SDimitry Andric }
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) {
10420b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
10430b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
10440b57cec5SDimitry Andric     return false;
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric   Value *Cond = BI->getCondition();
10470b57cec5SDimitry Andric   FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
10480b57cec5SDimitry Andric   if (!FCmp)
10490b57cec5SDimitry Andric     return false;
10500b57cec5SDimitry Andric 
1051*4824e7fdSDimitry Andric   ProbabilityList ProbList;
10520b57cec5SDimitry Andric   if (FCmp->isEquality()) {
1053*4824e7fdSDimitry Andric     ProbList = !FCmp->isTrueWhenEqual() ?
10540b57cec5SDimitry Andric       // f1 == f2 -> Unlikely
1055*4824e7fdSDimitry Andric       ProbabilityList({FPTakenProb, FPUntakenProb}) :
10560b57cec5SDimitry Andric       // f1 != f2 -> Likely
1057*4824e7fdSDimitry Andric       ProbabilityList({FPUntakenProb, FPTakenProb});
10580b57cec5SDimitry Andric   } else {
1059*4824e7fdSDimitry Andric     auto Search = FCmpTable.find(FCmp->getPredicate());
1060*4824e7fdSDimitry Andric     if (Search == FCmpTable.end())
10610b57cec5SDimitry Andric       return false;
1062*4824e7fdSDimitry Andric     ProbList = Search->second;
10630b57cec5SDimitry Andric   }
10640b57cec5SDimitry Andric 
1065*4824e7fdSDimitry Andric   setEdgeProbability(BB, ProbList);
10660b57cec5SDimitry Andric   return true;
10670b57cec5SDimitry Andric }
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric void BranchProbabilityInfo::releaseMemory() {
10700b57cec5SDimitry Andric   Probs.clear();
10715ffd83dbSDimitry Andric   Handles.clear();
10725ffd83dbSDimitry Andric }
10735ffd83dbSDimitry Andric 
10745ffd83dbSDimitry Andric bool BranchProbabilityInfo::invalidate(Function &, const PreservedAnalyses &PA,
10755ffd83dbSDimitry Andric                                        FunctionAnalysisManager::Invalidator &) {
10765ffd83dbSDimitry Andric   // Check whether the analysis, all analyses on functions, or the function's
10775ffd83dbSDimitry Andric   // CFG have been preserved.
10785ffd83dbSDimitry Andric   auto PAC = PA.getChecker<BranchProbabilityAnalysis>();
10795ffd83dbSDimitry Andric   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
10805ffd83dbSDimitry Andric            PAC.preservedSet<CFGAnalyses>());
10810b57cec5SDimitry Andric }
10820b57cec5SDimitry Andric 
10830b57cec5SDimitry Andric void BranchProbabilityInfo::print(raw_ostream &OS) const {
10840b57cec5SDimitry Andric   OS << "---- Branch Probabilities ----\n";
10850b57cec5SDimitry Andric   // We print the probabilities from the last function the analysis ran over,
10860b57cec5SDimitry Andric   // or the function it is currently running over.
10870b57cec5SDimitry Andric   assert(LastF && "Cannot print prior to running over a function");
10880b57cec5SDimitry Andric   for (const auto &BI : *LastF) {
1089fe6060f1SDimitry Andric     for (const BasicBlock *Succ : successors(&BI))
1090fe6060f1SDimitry Andric       printEdgeProbability(OS << "  ", &BI, Succ);
10910b57cec5SDimitry Andric   }
10920b57cec5SDimitry Andric }
10930b57cec5SDimitry Andric 
10940b57cec5SDimitry Andric bool BranchProbabilityInfo::
10950b57cec5SDimitry Andric isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
10960b57cec5SDimitry Andric   // Hot probability is at least 4/5 = 80%
10970b57cec5SDimitry Andric   // FIXME: Compare against a static "hot" BranchProbability.
10980b57cec5SDimitry Andric   return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
10990b57cec5SDimitry Andric }
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric /// Get the raw edge probability for the edge. If can't find it, return a
11020b57cec5SDimitry Andric /// default probability 1/N where N is the number of successors. Here an edge is
11030b57cec5SDimitry Andric /// specified using PredBlock and an
11040b57cec5SDimitry Andric /// index to the successors.
11050b57cec5SDimitry Andric BranchProbability
11060b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
11070b57cec5SDimitry Andric                                           unsigned IndexInSuccessors) const {
11080b57cec5SDimitry Andric   auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
1109e8d8bef9SDimitry Andric   assert((Probs.end() == Probs.find(std::make_pair(Src, 0))) ==
1110e8d8bef9SDimitry Andric              (Probs.end() == I) &&
1111e8d8bef9SDimitry Andric          "Probability for I-th successor must always be defined along with the "
1112e8d8bef9SDimitry Andric          "probability for the first successor");
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric   if (I != Probs.end())
11150b57cec5SDimitry Andric     return I->second;
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric   return {1, static_cast<uint32_t>(succ_size(Src))};
11180b57cec5SDimitry Andric }
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric BranchProbability
11210b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
11225ffd83dbSDimitry Andric                                           const_succ_iterator Dst) const {
11230b57cec5SDimitry Andric   return getEdgeProbability(Src, Dst.getSuccessorIndex());
11240b57cec5SDimitry Andric }
11250b57cec5SDimitry Andric 
11260b57cec5SDimitry Andric /// Get the raw edge probability calculated for the block pair. This returns the
11270b57cec5SDimitry Andric /// sum of all raw edge probabilities from Src to Dst.
11280b57cec5SDimitry Andric BranchProbability
11290b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
11300b57cec5SDimitry Andric                                           const BasicBlock *Dst) const {
1131e8d8bef9SDimitry Andric   if (!Probs.count(std::make_pair(Src, 0)))
1132e8d8bef9SDimitry Andric     return BranchProbability(llvm::count(successors(Src), Dst), succ_size(Src));
11330b57cec5SDimitry Andric 
1134e8d8bef9SDimitry Andric   auto Prob = BranchProbability::getZero();
1135e8d8bef9SDimitry Andric   for (const_succ_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)
1136e8d8bef9SDimitry Andric     if (*I == Dst)
1137e8d8bef9SDimitry Andric       Prob += Probs.find(std::make_pair(Src, I.getSuccessorIndex()))->second;
1138e8d8bef9SDimitry Andric 
1139e8d8bef9SDimitry Andric   return Prob;
11400b57cec5SDimitry Andric }
11410b57cec5SDimitry Andric 
11425ffd83dbSDimitry Andric /// Set the edge probability for all edges at once.
11435ffd83dbSDimitry Andric void BranchProbabilityInfo::setEdgeProbability(
11445ffd83dbSDimitry Andric     const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) {
11455ffd83dbSDimitry Andric   assert(Src->getTerminator()->getNumSuccessors() == Probs.size());
1146e8d8bef9SDimitry Andric   eraseBlock(Src); // Erase stale data if any.
11475ffd83dbSDimitry Andric   if (Probs.size() == 0)
11485ffd83dbSDimitry Andric     return; // Nothing to set.
11495ffd83dbSDimitry Andric 
1150e8d8bef9SDimitry Andric   Handles.insert(BasicBlockCallbackVH(Src, this));
11515ffd83dbSDimitry Andric   uint64_t TotalNumerator = 0;
11525ffd83dbSDimitry Andric   for (unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) {
1153e8d8bef9SDimitry Andric     this->Probs[std::make_pair(Src, SuccIdx)] = Probs[SuccIdx];
1154e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << SuccIdx
1155e8d8bef9SDimitry Andric                       << " successor probability to " << Probs[SuccIdx]
1156e8d8bef9SDimitry Andric                       << "\n");
11575ffd83dbSDimitry Andric     TotalNumerator += Probs[SuccIdx].getNumerator();
11585ffd83dbSDimitry Andric   }
11595ffd83dbSDimitry Andric 
11605ffd83dbSDimitry Andric   // Because of rounding errors the total probability cannot be checked to be
11615ffd83dbSDimitry Andric   // 1.0 exactly. That is TotalNumerator == BranchProbability::getDenominator.
11625ffd83dbSDimitry Andric   // Instead, every single probability in Probs must be as accurate as possible.
11635ffd83dbSDimitry Andric   // This results in error 1/denominator at most, thus the total absolute error
11645ffd83dbSDimitry Andric   // should be within Probs.size / BranchProbability::getDenominator.
11655ffd83dbSDimitry Andric   assert(TotalNumerator <= BranchProbability::getDenominator() + Probs.size());
11665ffd83dbSDimitry Andric   assert(TotalNumerator >= BranchProbability::getDenominator() - Probs.size());
1167fe6060f1SDimitry Andric   (void)TotalNumerator;
11685ffd83dbSDimitry Andric }
11695ffd83dbSDimitry Andric 
1170e8d8bef9SDimitry Andric void BranchProbabilityInfo::copyEdgeProbabilities(BasicBlock *Src,
1171e8d8bef9SDimitry Andric                                                   BasicBlock *Dst) {
1172e8d8bef9SDimitry Andric   eraseBlock(Dst); // Erase stale data if any.
1173e8d8bef9SDimitry Andric   unsigned NumSuccessors = Src->getTerminator()->getNumSuccessors();
1174e8d8bef9SDimitry Andric   assert(NumSuccessors == Dst->getTerminator()->getNumSuccessors());
1175e8d8bef9SDimitry Andric   if (NumSuccessors == 0)
1176e8d8bef9SDimitry Andric     return; // Nothing to set.
1177e8d8bef9SDimitry Andric   if (this->Probs.find(std::make_pair(Src, 0)) == this->Probs.end())
1178e8d8bef9SDimitry Andric     return; // No probability is set for edges from Src. Keep the same for Dst.
1179e8d8bef9SDimitry Andric 
1180e8d8bef9SDimitry Andric   Handles.insert(BasicBlockCallbackVH(Dst, this));
1181e8d8bef9SDimitry Andric   for (unsigned SuccIdx = 0; SuccIdx < NumSuccessors; ++SuccIdx) {
1182e8d8bef9SDimitry Andric     auto Prob = this->Probs[std::make_pair(Src, SuccIdx)];
1183e8d8bef9SDimitry Andric     this->Probs[std::make_pair(Dst, SuccIdx)] = Prob;
1184e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "set edge " << Dst->getName() << " -> " << SuccIdx
1185e8d8bef9SDimitry Andric                       << " successor probability to " << Prob << "\n");
1186e8d8bef9SDimitry Andric   }
1187e8d8bef9SDimitry Andric }
1188e8d8bef9SDimitry Andric 
11890b57cec5SDimitry Andric raw_ostream &
11900b57cec5SDimitry Andric BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
11910b57cec5SDimitry Andric                                             const BasicBlock *Src,
11920b57cec5SDimitry Andric                                             const BasicBlock *Dst) const {
11930b57cec5SDimitry Andric   const BranchProbability Prob = getEdgeProbability(Src, Dst);
11940b57cec5SDimitry Andric   OS << "edge " << Src->getName() << " -> " << Dst->getName()
11950b57cec5SDimitry Andric      << " probability is " << Prob
11960b57cec5SDimitry Andric      << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
11970b57cec5SDimitry Andric 
11980b57cec5SDimitry Andric   return OS;
11990b57cec5SDimitry Andric }
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) {
1202e8d8bef9SDimitry Andric   LLVM_DEBUG(dbgs() << "eraseBlock " << BB->getName() << "\n");
1203e8d8bef9SDimitry Andric 
1204e8d8bef9SDimitry Andric   // Note that we cannot use successors of BB because the terminator of BB may
1205e8d8bef9SDimitry Andric   // have changed when eraseBlock is called as a BasicBlockCallbackVH callback.
1206e8d8bef9SDimitry Andric   // Instead we remove prob data for the block by iterating successors by their
1207e8d8bef9SDimitry Andric   // indices from 0 till the last which exists. There could not be prob data for
1208e8d8bef9SDimitry Andric   // a pair (BB, N) if there is no data for (BB, N-1) because the data is always
1209e8d8bef9SDimitry Andric   // set for all successors from 0 to M at once by the method
1210e8d8bef9SDimitry Andric   // setEdgeProbability().
1211e8d8bef9SDimitry Andric   Handles.erase(BasicBlockCallbackVH(BB, this));
1212e8d8bef9SDimitry Andric   for (unsigned I = 0;; ++I) {
1213e8d8bef9SDimitry Andric     auto MapI = Probs.find(std::make_pair(BB, I));
1214e8d8bef9SDimitry Andric     if (MapI == Probs.end()) {
1215e8d8bef9SDimitry Andric       assert(Probs.count(std::make_pair(BB, I + 1)) == 0 &&
1216e8d8bef9SDimitry Andric              "Must be no more successors");
1217e8d8bef9SDimitry Andric       return;
1218e8d8bef9SDimitry Andric     }
12195ffd83dbSDimitry Andric     Probs.erase(MapI);
12200b57cec5SDimitry Andric   }
12210b57cec5SDimitry Andric }
12220b57cec5SDimitry Andric 
1223e8d8bef9SDimitry Andric void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LoopI,
12245ffd83dbSDimitry Andric                                       const TargetLibraryInfo *TLI,
1225e8d8bef9SDimitry Andric                                       DominatorTree *DT,
12265ffd83dbSDimitry Andric                                       PostDominatorTree *PDT) {
12270b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName()
12280b57cec5SDimitry Andric                     << " ----\n\n");
12290b57cec5SDimitry Andric   LastF = &F; // Store the last function we ran on for printing.
1230e8d8bef9SDimitry Andric   LI = &LoopI;
12310b57cec5SDimitry Andric 
1232e8d8bef9SDimitry Andric   SccI = std::make_unique<SccInfo>(F);
12330b57cec5SDimitry Andric 
1234e8d8bef9SDimitry Andric   assert(EstimatedBlockWeight.empty());
1235e8d8bef9SDimitry Andric   assert(EstimatedLoopWeight.empty());
12360b57cec5SDimitry Andric 
1237e8d8bef9SDimitry Andric   std::unique_ptr<DominatorTree> DTPtr;
12385ffd83dbSDimitry Andric   std::unique_ptr<PostDominatorTree> PDTPtr;
12395ffd83dbSDimitry Andric 
1240e8d8bef9SDimitry Andric   if (!DT) {
1241e8d8bef9SDimitry Andric     DTPtr = std::make_unique<DominatorTree>(const_cast<Function &>(F));
1242e8d8bef9SDimitry Andric     DT = DTPtr.get();
1243e8d8bef9SDimitry Andric   }
1244e8d8bef9SDimitry Andric 
12455ffd83dbSDimitry Andric   if (!PDT) {
12465ffd83dbSDimitry Andric     PDTPtr = std::make_unique<PostDominatorTree>(const_cast<Function &>(F));
12475ffd83dbSDimitry Andric     PDT = PDTPtr.get();
12485ffd83dbSDimitry Andric   }
12495ffd83dbSDimitry Andric 
1250e8d8bef9SDimitry Andric   computeEestimateBlockWeight(F, DT, PDT);
1251480093f4SDimitry Andric 
12520b57cec5SDimitry Andric   // Walk the basic blocks in post-order so that we can build up state about
12530b57cec5SDimitry Andric   // the successors of a block iteratively.
12540b57cec5SDimitry Andric   for (auto BB : post_order(&F.getEntryBlock())) {
12550b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Computing probabilities for " << BB->getName()
12560b57cec5SDimitry Andric                       << "\n");
12570b57cec5SDimitry Andric     // If there is no at least two successors, no sense to set probability.
12580b57cec5SDimitry Andric     if (BB->getTerminator()->getNumSuccessors() < 2)
12590b57cec5SDimitry Andric       continue;
12600b57cec5SDimitry Andric     if (calcMetadataWeights(BB))
12610b57cec5SDimitry Andric       continue;
1262e8d8bef9SDimitry Andric     if (calcEstimatedHeuristics(BB))
12630b57cec5SDimitry Andric       continue;
12640b57cec5SDimitry Andric     if (calcPointerHeuristics(BB))
12650b57cec5SDimitry Andric       continue;
12660b57cec5SDimitry Andric     if (calcZeroHeuristics(BB, TLI))
12670b57cec5SDimitry Andric       continue;
12680b57cec5SDimitry Andric     if (calcFloatingPointHeuristics(BB))
12690b57cec5SDimitry Andric       continue;
12700b57cec5SDimitry Andric   }
12710b57cec5SDimitry Andric 
1272e8d8bef9SDimitry Andric   EstimatedLoopWeight.clear();
1273e8d8bef9SDimitry Andric   EstimatedBlockWeight.clear();
1274e8d8bef9SDimitry Andric   SccI.reset();
12750b57cec5SDimitry Andric 
12760b57cec5SDimitry Andric   if (PrintBranchProb &&
12770b57cec5SDimitry Andric       (PrintBranchProbFuncName.empty() ||
12780b57cec5SDimitry Andric        F.getName().equals(PrintBranchProbFuncName))) {
12790b57cec5SDimitry Andric     print(dbgs());
12800b57cec5SDimitry Andric   }
12810b57cec5SDimitry Andric }
12820b57cec5SDimitry Andric 
12830b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::getAnalysisUsage(
12840b57cec5SDimitry Andric     AnalysisUsage &AU) const {
12850b57cec5SDimitry Andric   // We require DT so it's available when LI is available. The LI updating code
12860b57cec5SDimitry Andric   // asserts that DT is also present so if we don't make sure that we have DT
12870b57cec5SDimitry Andric   // here, that assert will trigger.
12880b57cec5SDimitry Andric   AU.addRequired<DominatorTreeWrapperPass>();
12890b57cec5SDimitry Andric   AU.addRequired<LoopInfoWrapperPass>();
12900b57cec5SDimitry Andric   AU.addRequired<TargetLibraryInfoWrapperPass>();
1291e8d8bef9SDimitry Andric   AU.addRequired<DominatorTreeWrapperPass>();
12925ffd83dbSDimitry Andric   AU.addRequired<PostDominatorTreeWrapperPass>();
12930b57cec5SDimitry Andric   AU.setPreservesAll();
12940b57cec5SDimitry Andric }
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) {
12970b57cec5SDimitry Andric   const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
12988bcb0991SDimitry Andric   const TargetLibraryInfo &TLI =
12998bcb0991SDimitry Andric       getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1300e8d8bef9SDimitry Andric   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
13015ffd83dbSDimitry Andric   PostDominatorTree &PDT =
13025ffd83dbSDimitry Andric       getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
1303e8d8bef9SDimitry Andric   BPI.calculate(F, LI, &TLI, &DT, &PDT);
13040b57cec5SDimitry Andric   return false;
13050b57cec5SDimitry Andric }
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); }
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS,
13100b57cec5SDimitry Andric                                              const Module *) const {
13110b57cec5SDimitry Andric   BPI.print(OS);
13120b57cec5SDimitry Andric }
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric AnalysisKey BranchProbabilityAnalysis::Key;
13150b57cec5SDimitry Andric BranchProbabilityInfo
13160b57cec5SDimitry Andric BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
13170b57cec5SDimitry Andric   BranchProbabilityInfo BPI;
13185ffd83dbSDimitry Andric   BPI.calculate(F, AM.getResult<LoopAnalysis>(F),
13195ffd83dbSDimitry Andric                 &AM.getResult<TargetLibraryAnalysis>(F),
1320e8d8bef9SDimitry Andric                 &AM.getResult<DominatorTreeAnalysis>(F),
13215ffd83dbSDimitry Andric                 &AM.getResult<PostDominatorTreeAnalysis>(F));
13220b57cec5SDimitry Andric   return BPI;
13230b57cec5SDimitry Andric }
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric PreservedAnalyses
13260b57cec5SDimitry Andric BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
13270b57cec5SDimitry Andric   OS << "Printing analysis results of BPI for function "
13280b57cec5SDimitry Andric      << "'" << F.getName() << "':"
13290b57cec5SDimitry Andric      << "\n";
13300b57cec5SDimitry Andric   AM.getResult<BranchProbabilityAnalysis>(F).print(OS);
13310b57cec5SDimitry Andric   return PreservedAnalyses::all();
13320b57cec5SDimitry Andric }
1333