xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/BranchProbabilityInfo.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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)
64*e8d8bef9SDimitry 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 
1070b57cec5SDimitry Andric static const uint32_t PH_TAKEN_WEIGHT = 20;
1080b57cec5SDimitry Andric static const uint32_t PH_NONTAKEN_WEIGHT = 12;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric static const uint32_t ZH_TAKEN_WEIGHT = 20;
1110b57cec5SDimitry Andric static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric static const uint32_t FPH_TAKEN_WEIGHT = 20;
1140b57cec5SDimitry Andric static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
1150b57cec5SDimitry Andric 
1168bcb0991SDimitry Andric /// This is the probability for an ordered floating point comparison.
1178bcb0991SDimitry Andric static const uint32_t FPH_ORD_WEIGHT = 1024 * 1024 - 1;
1188bcb0991SDimitry Andric /// This is the probability for an unordered floating point comparison, it means
1198bcb0991SDimitry Andric /// one or two of the operands are NaN. Usually it is used to test for an
1208bcb0991SDimitry Andric /// exceptional case, so the result is unlikely.
1218bcb0991SDimitry Andric static const uint32_t FPH_UNO_WEIGHT = 1;
1228bcb0991SDimitry Andric 
123*e8d8bef9SDimitry Andric /// Set of dedicated "absolute" execution weights for a block. These weights are
124*e8d8bef9SDimitry Andric /// meaningful relative to each other and their derivatives only.
125*e8d8bef9SDimitry Andric enum class BlockExecWeight : std::uint32_t {
126*e8d8bef9SDimitry Andric   /// Special weight used for cases with exact zero probability.
127*e8d8bef9SDimitry Andric   ZERO = 0x0,
128*e8d8bef9SDimitry Andric   /// Minimal possible non zero weight.
129*e8d8bef9SDimitry Andric   LOWEST_NON_ZERO = 0x1,
130*e8d8bef9SDimitry Andric   /// Weight to an 'unreachable' block.
131*e8d8bef9SDimitry Andric   UNREACHABLE = ZERO,
132*e8d8bef9SDimitry Andric   /// Weight to a block containing non returning call.
133*e8d8bef9SDimitry Andric   NORETURN = LOWEST_NON_ZERO,
134*e8d8bef9SDimitry Andric   /// Weight to 'unwind' block of an invoke instruction.
135*e8d8bef9SDimitry Andric   UNWIND = LOWEST_NON_ZERO,
136*e8d8bef9SDimitry Andric   /// Weight to a 'cold' block. Cold blocks are the ones containing calls marked
137*e8d8bef9SDimitry Andric   /// with attribute 'cold'.
138*e8d8bef9SDimitry Andric   COLD = 0xffff,
139*e8d8bef9SDimitry Andric   /// Default weight is used in cases when there is no dedicated execution
140*e8d8bef9SDimitry Andric   /// weight set. It is not propagated through the domination line either.
141*e8d8bef9SDimitry Andric   DEFAULT = 0xfffff
142*e8d8bef9SDimitry Andric };
1430b57cec5SDimitry Andric 
144*e8d8bef9SDimitry Andric BranchProbabilityInfo::SccInfo::SccInfo(const Function &F) {
145*e8d8bef9SDimitry Andric   // Record SCC numbers of blocks in the CFG to identify irreducible loops.
146*e8d8bef9SDimitry Andric   // FIXME: We could only calculate this if the CFG is known to be irreducible
147*e8d8bef9SDimitry Andric   // (perhaps cache this info in LoopInfo if we can easily calculate it there?).
148*e8d8bef9SDimitry Andric   int SccNum = 0;
149*e8d8bef9SDimitry Andric   for (scc_iterator<const Function *> It = scc_begin(&F); !It.isAtEnd();
150*e8d8bef9SDimitry Andric        ++It, ++SccNum) {
151*e8d8bef9SDimitry Andric     // Ignore single-block SCCs since they either aren't loops or LoopInfo will
152*e8d8bef9SDimitry Andric     // catch them.
153*e8d8bef9SDimitry Andric     const std::vector<const BasicBlock *> &Scc = *It;
154*e8d8bef9SDimitry Andric     if (Scc.size() == 1)
155480093f4SDimitry Andric       continue;
156*e8d8bef9SDimitry Andric 
157*e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "BPI: SCC " << SccNum << ":");
158*e8d8bef9SDimitry Andric     for (const auto *BB : Scc) {
159*e8d8bef9SDimitry Andric       LLVM_DEBUG(dbgs() << " " << BB->getName());
160*e8d8bef9SDimitry Andric       SccNums[BB] = SccNum;
161*e8d8bef9SDimitry Andric       calculateSccBlockType(BB, SccNum);
162480093f4SDimitry Andric     }
163*e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "\n");
164*e8d8bef9SDimitry Andric   }
165*e8d8bef9SDimitry Andric }
166*e8d8bef9SDimitry Andric 
167*e8d8bef9SDimitry Andric int BranchProbabilityInfo::SccInfo::getSCCNum(const BasicBlock *BB) const {
168*e8d8bef9SDimitry Andric   auto SccIt = SccNums.find(BB);
169*e8d8bef9SDimitry Andric   if (SccIt == SccNums.end())
170*e8d8bef9SDimitry Andric     return -1;
171*e8d8bef9SDimitry Andric   return SccIt->second;
172*e8d8bef9SDimitry Andric }
173*e8d8bef9SDimitry Andric 
174*e8d8bef9SDimitry Andric void BranchProbabilityInfo::SccInfo::getSccEnterBlocks(
175*e8d8bef9SDimitry Andric     int SccNum, SmallVectorImpl<BasicBlock *> &Enters) const {
176*e8d8bef9SDimitry Andric 
177*e8d8bef9SDimitry Andric   for (auto MapIt : SccBlocks[SccNum]) {
178*e8d8bef9SDimitry Andric     const auto *BB = MapIt.first;
179*e8d8bef9SDimitry Andric     if (isSCCHeader(BB, SccNum))
180*e8d8bef9SDimitry Andric       for (const auto *Pred : predecessors(BB))
181*e8d8bef9SDimitry Andric         if (getSCCNum(Pred) != SccNum)
182*e8d8bef9SDimitry Andric           Enters.push_back(const_cast<BasicBlock *>(BB));
183*e8d8bef9SDimitry Andric   }
184*e8d8bef9SDimitry Andric }
185*e8d8bef9SDimitry Andric 
186*e8d8bef9SDimitry Andric void BranchProbabilityInfo::SccInfo::getSccExitBlocks(
187*e8d8bef9SDimitry Andric     int SccNum, SmallVectorImpl<BasicBlock *> &Exits) const {
188*e8d8bef9SDimitry Andric   for (auto MapIt : SccBlocks[SccNum]) {
189*e8d8bef9SDimitry Andric     const auto *BB = MapIt.first;
190*e8d8bef9SDimitry Andric     if (isSCCExitingBlock(BB, SccNum))
191*e8d8bef9SDimitry Andric       for (const auto *Succ : successors(BB))
192*e8d8bef9SDimitry Andric         if (getSCCNum(Succ) != SccNum)
193*e8d8bef9SDimitry Andric           Exits.push_back(const_cast<BasicBlock *>(BB));
194*e8d8bef9SDimitry Andric   }
195*e8d8bef9SDimitry Andric }
196*e8d8bef9SDimitry Andric 
197*e8d8bef9SDimitry Andric uint32_t BranchProbabilityInfo::SccInfo::getSccBlockType(const BasicBlock *BB,
198*e8d8bef9SDimitry Andric                                                          int SccNum) const {
199*e8d8bef9SDimitry Andric   assert(getSCCNum(BB) == SccNum);
200*e8d8bef9SDimitry Andric 
201*e8d8bef9SDimitry Andric   assert(SccBlocks.size() > static_cast<unsigned>(SccNum) && "Unknown SCC");
202*e8d8bef9SDimitry Andric   const auto &SccBlockTypes = SccBlocks[SccNum];
203*e8d8bef9SDimitry Andric 
204*e8d8bef9SDimitry Andric   auto It = SccBlockTypes.find(BB);
205*e8d8bef9SDimitry Andric   if (It != SccBlockTypes.end()) {
206*e8d8bef9SDimitry Andric     return It->second;
207*e8d8bef9SDimitry Andric   }
208*e8d8bef9SDimitry Andric   return Inner;
209*e8d8bef9SDimitry Andric }
210*e8d8bef9SDimitry Andric 
211*e8d8bef9SDimitry Andric void BranchProbabilityInfo::SccInfo::calculateSccBlockType(const BasicBlock *BB,
212*e8d8bef9SDimitry Andric                                                            int SccNum) {
213*e8d8bef9SDimitry Andric   assert(getSCCNum(BB) == SccNum);
214*e8d8bef9SDimitry Andric   uint32_t BlockType = Inner;
215*e8d8bef9SDimitry Andric 
216*e8d8bef9SDimitry Andric   if (llvm::any_of(predecessors(BB), [&](const BasicBlock *Pred) {
217*e8d8bef9SDimitry Andric         // Consider any block that is an entry point to the SCC as
218*e8d8bef9SDimitry Andric         // a header.
219*e8d8bef9SDimitry Andric         return getSCCNum(Pred) != SccNum;
220480093f4SDimitry Andric       }))
221*e8d8bef9SDimitry Andric     BlockType |= Header;
2220b57cec5SDimitry Andric 
223*e8d8bef9SDimitry Andric   if (llvm::any_of(successors(BB), [&](const BasicBlock *Succ) {
224*e8d8bef9SDimitry Andric         return getSCCNum(Succ) != SccNum;
225480093f4SDimitry Andric       }))
226*e8d8bef9SDimitry Andric     BlockType |= Exiting;
227*e8d8bef9SDimitry Andric 
228*e8d8bef9SDimitry Andric   // Lazily compute the set of headers for a given SCC and cache the results
229*e8d8bef9SDimitry Andric   // in the SccHeaderMap.
230*e8d8bef9SDimitry Andric   if (SccBlocks.size() <= static_cast<unsigned>(SccNum))
231*e8d8bef9SDimitry Andric     SccBlocks.resize(SccNum + 1);
232*e8d8bef9SDimitry Andric   auto &SccBlockTypes = SccBlocks[SccNum];
233*e8d8bef9SDimitry Andric 
234*e8d8bef9SDimitry Andric   if (BlockType != Inner) {
235*e8d8bef9SDimitry Andric     bool IsInserted;
236*e8d8bef9SDimitry Andric     std::tie(std::ignore, IsInserted) =
237*e8d8bef9SDimitry Andric         SccBlockTypes.insert(std::make_pair(BB, BlockType));
238*e8d8bef9SDimitry Andric     assert(IsInserted && "Duplicated block in SCC");
2390b57cec5SDimitry Andric   }
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric 
242*e8d8bef9SDimitry Andric BranchProbabilityInfo::LoopBlock::LoopBlock(const BasicBlock *BB,
243*e8d8bef9SDimitry Andric                                             const LoopInfo &LI,
244*e8d8bef9SDimitry Andric                                             const SccInfo &SccI)
245*e8d8bef9SDimitry Andric     : BB(BB) {
246*e8d8bef9SDimitry Andric   LD.first = LI.getLoopFor(BB);
247*e8d8bef9SDimitry Andric   if (!LD.first) {
248*e8d8bef9SDimitry Andric     LD.second = SccI.getSCCNum(BB);
249*e8d8bef9SDimitry Andric   }
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
252*e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopEnteringEdge(const LoopEdge &Edge) const {
253*e8d8bef9SDimitry Andric   const auto &SrcBlock = Edge.first;
254*e8d8bef9SDimitry Andric   const auto &DstBlock = Edge.second;
255*e8d8bef9SDimitry Andric   return (DstBlock.getLoop() &&
256*e8d8bef9SDimitry Andric           !DstBlock.getLoop()->contains(SrcBlock.getLoop())) ||
257*e8d8bef9SDimitry Andric          // Assume that SCCs can't be nested.
258*e8d8bef9SDimitry Andric          (DstBlock.getSccNum() != -1 &&
259*e8d8bef9SDimitry Andric           SrcBlock.getSccNum() != DstBlock.getSccNum());
260*e8d8bef9SDimitry Andric }
2610b57cec5SDimitry Andric 
262*e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopExitingEdge(const LoopEdge &Edge) const {
263*e8d8bef9SDimitry Andric   return isLoopEnteringEdge({Edge.second, Edge.first});
264*e8d8bef9SDimitry Andric }
2650b57cec5SDimitry Andric 
266*e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopEnteringExitingEdge(
267*e8d8bef9SDimitry Andric     const LoopEdge &Edge) const {
268*e8d8bef9SDimitry Andric   return isLoopEnteringEdge(Edge) || isLoopExitingEdge(Edge);
269*e8d8bef9SDimitry Andric }
270*e8d8bef9SDimitry Andric 
271*e8d8bef9SDimitry Andric bool BranchProbabilityInfo::isLoopBackEdge(const LoopEdge &Edge) const {
272*e8d8bef9SDimitry Andric   const auto &SrcBlock = Edge.first;
273*e8d8bef9SDimitry Andric   const auto &DstBlock = Edge.second;
274*e8d8bef9SDimitry Andric   return SrcBlock.belongsToSameLoop(DstBlock) &&
275*e8d8bef9SDimitry Andric          ((DstBlock.getLoop() &&
276*e8d8bef9SDimitry Andric            DstBlock.getLoop()->getHeader() == DstBlock.getBlock()) ||
277*e8d8bef9SDimitry Andric           (DstBlock.getSccNum() != -1 &&
278*e8d8bef9SDimitry Andric            SccI->isSCCHeader(DstBlock.getBlock(), DstBlock.getSccNum())));
279*e8d8bef9SDimitry Andric }
280*e8d8bef9SDimitry Andric 
281*e8d8bef9SDimitry Andric void BranchProbabilityInfo::getLoopEnterBlocks(
282*e8d8bef9SDimitry Andric     const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Enters) const {
283*e8d8bef9SDimitry Andric   if (LB.getLoop()) {
284*e8d8bef9SDimitry Andric     auto *Header = LB.getLoop()->getHeader();
285*e8d8bef9SDimitry Andric     Enters.append(pred_begin(Header), pred_end(Header));
286*e8d8bef9SDimitry Andric   } else {
287*e8d8bef9SDimitry Andric     assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?");
288*e8d8bef9SDimitry Andric     SccI->getSccEnterBlocks(LB.getSccNum(), Enters);
289*e8d8bef9SDimitry Andric   }
290*e8d8bef9SDimitry Andric }
291*e8d8bef9SDimitry Andric 
292*e8d8bef9SDimitry Andric void BranchProbabilityInfo::getLoopExitBlocks(
293*e8d8bef9SDimitry Andric     const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Exits) const {
294*e8d8bef9SDimitry Andric   if (LB.getLoop()) {
295*e8d8bef9SDimitry Andric     LB.getLoop()->getExitBlocks(Exits);
296*e8d8bef9SDimitry Andric   } else {
297*e8d8bef9SDimitry Andric     assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?");
298*e8d8bef9SDimitry Andric     SccI->getSccExitBlocks(LB.getSccNum(), Exits);
299*e8d8bef9SDimitry Andric   }
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric // Propagate existing explicit probabilities from either profile data or
3030b57cec5SDimitry Andric // 'expect' intrinsic processing. Examine metadata against unreachable
3040b57cec5SDimitry Andric // heuristic. The probability of the edge coming to unreachable block is
3050b57cec5SDimitry Andric // set to min of metadata and unreachable heuristic.
3060b57cec5SDimitry Andric bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
3070b57cec5SDimitry Andric   const Instruction *TI = BB->getTerminator();
3080b57cec5SDimitry Andric   assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
3095ffd83dbSDimitry Andric   if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI) ||
3105ffd83dbSDimitry Andric         isa<InvokeInst>(TI)))
3110b57cec5SDimitry Andric     return false;
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
3140b57cec5SDimitry Andric   if (!WeightsNode)
3150b57cec5SDimitry Andric     return false;
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   // Check that the number of successors is manageable.
3180b57cec5SDimitry Andric   assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors");
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   // Ensure there are weights for all of the successors. Note that the first
3210b57cec5SDimitry Andric   // operand to the metadata node is a name, not a weight.
3220b57cec5SDimitry Andric   if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
3230b57cec5SDimitry Andric     return false;
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric   // Build up the final weights that will be used in a temporary buffer.
3260b57cec5SDimitry Andric   // Compute the sum of all weights to later decide whether they need to
3270b57cec5SDimitry Andric   // be scaled to fit in 32 bits.
3280b57cec5SDimitry Andric   uint64_t WeightSum = 0;
3290b57cec5SDimitry Andric   SmallVector<uint32_t, 2> Weights;
3300b57cec5SDimitry Andric   SmallVector<unsigned, 2> UnreachableIdxs;
3310b57cec5SDimitry Andric   SmallVector<unsigned, 2> ReachableIdxs;
3320b57cec5SDimitry Andric   Weights.reserve(TI->getNumSuccessors());
3335ffd83dbSDimitry Andric   for (unsigned I = 1, E = WeightsNode->getNumOperands(); I != E; ++I) {
3340b57cec5SDimitry Andric     ConstantInt *Weight =
3355ffd83dbSDimitry Andric         mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(I));
3360b57cec5SDimitry Andric     if (!Weight)
3370b57cec5SDimitry Andric       return false;
3380b57cec5SDimitry Andric     assert(Weight->getValue().getActiveBits() <= 32 &&
3390b57cec5SDimitry Andric            "Too many bits for uint32_t");
3400b57cec5SDimitry Andric     Weights.push_back(Weight->getZExtValue());
3410b57cec5SDimitry Andric     WeightSum += Weights.back();
342*e8d8bef9SDimitry Andric     const LoopBlock SrcLoopBB = getLoopBlock(BB);
343*e8d8bef9SDimitry Andric     const LoopBlock DstLoopBB = getLoopBlock(TI->getSuccessor(I - 1));
344*e8d8bef9SDimitry Andric     auto EstimatedWeight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
345*e8d8bef9SDimitry Andric     if (EstimatedWeight &&
346*e8d8bef9SDimitry Andric         EstimatedWeight.getValue() <=
347*e8d8bef9SDimitry Andric             static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))
3485ffd83dbSDimitry Andric       UnreachableIdxs.push_back(I - 1);
3490b57cec5SDimitry Andric     else
3505ffd83dbSDimitry Andric       ReachableIdxs.push_back(I - 1);
3510b57cec5SDimitry Andric   }
3520b57cec5SDimitry Andric   assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   // If the sum of weights does not fit in 32 bits, scale every weight down
3550b57cec5SDimitry Andric   // accordingly.
3560b57cec5SDimitry Andric   uint64_t ScalingFactor =
3570b57cec5SDimitry Andric       (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   if (ScalingFactor > 1) {
3600b57cec5SDimitry Andric     WeightSum = 0;
3615ffd83dbSDimitry Andric     for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) {
3625ffd83dbSDimitry Andric       Weights[I] /= ScalingFactor;
3635ffd83dbSDimitry Andric       WeightSum += Weights[I];
3640b57cec5SDimitry Andric     }
3650b57cec5SDimitry Andric   }
3660b57cec5SDimitry Andric   assert(WeightSum <= UINT32_MAX &&
3670b57cec5SDimitry Andric          "Expected weights to scale down to 32 bits");
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   if (WeightSum == 0 || ReachableIdxs.size() == 0) {
3705ffd83dbSDimitry Andric     for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
3715ffd83dbSDimitry Andric       Weights[I] = 1;
3720b57cec5SDimitry Andric     WeightSum = TI->getNumSuccessors();
3730b57cec5SDimitry Andric   }
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   // Set the probability.
3760b57cec5SDimitry Andric   SmallVector<BranchProbability, 2> BP;
3775ffd83dbSDimitry Andric   for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
3785ffd83dbSDimitry Andric     BP.push_back({ Weights[I], static_cast<uint32_t>(WeightSum) });
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   // Examine the metadata against unreachable heuristic.
3810b57cec5SDimitry Andric   // If the unreachable heuristic is more strong then we use it for this edge.
3825ffd83dbSDimitry Andric   if (UnreachableIdxs.size() == 0 || ReachableIdxs.size() == 0) {
3835ffd83dbSDimitry Andric     setEdgeProbability(BB, BP);
3845ffd83dbSDimitry Andric     return true;
3855ffd83dbSDimitry Andric   }
3865ffd83dbSDimitry Andric 
3870b57cec5SDimitry Andric   auto UnreachableProb = UR_TAKEN_PROB;
3885ffd83dbSDimitry Andric   for (auto I : UnreachableIdxs)
3895ffd83dbSDimitry Andric     if (UnreachableProb < BP[I]) {
3905ffd83dbSDimitry Andric       BP[I] = UnreachableProb;
3910b57cec5SDimitry Andric     }
3920b57cec5SDimitry Andric 
3935ffd83dbSDimitry Andric   // Sum of all edge probabilities must be 1.0. If we modified the probability
3945ffd83dbSDimitry Andric   // of some edges then we must distribute the introduced difference over the
3955ffd83dbSDimitry Andric   // reachable blocks.
3965ffd83dbSDimitry Andric   //
3975ffd83dbSDimitry Andric   // Proportional distribution: the relation between probabilities of the
3985ffd83dbSDimitry Andric   // reachable edges is kept unchanged. That is for any reachable edges i and j:
3995ffd83dbSDimitry Andric   //   newBP[i] / newBP[j] == oldBP[i] / oldBP[j] =>
4005ffd83dbSDimitry Andric   //   newBP[i] / oldBP[i] == newBP[j] / oldBP[j] == K
4015ffd83dbSDimitry Andric   // Where K is independent of i,j.
4025ffd83dbSDimitry Andric   //   newBP[i] == oldBP[i] * K
4035ffd83dbSDimitry Andric   // We need to find K.
4045ffd83dbSDimitry Andric   // Make sum of all reachables of the left and right parts:
4055ffd83dbSDimitry Andric   //   sum_of_reachable(newBP) == K * sum_of_reachable(oldBP)
4065ffd83dbSDimitry Andric   // Sum of newBP must be equal to 1.0:
4075ffd83dbSDimitry Andric   //   sum_of_reachable(newBP) + sum_of_unreachable(newBP) == 1.0 =>
4085ffd83dbSDimitry Andric   //   sum_of_reachable(newBP) = 1.0 - sum_of_unreachable(newBP)
4095ffd83dbSDimitry Andric   // Where sum_of_unreachable(newBP) is what has been just changed.
4105ffd83dbSDimitry Andric   // Finally:
4115ffd83dbSDimitry Andric   //   K == sum_of_reachable(newBP) / sum_of_reachable(oldBP) =>
4125ffd83dbSDimitry Andric   //   K == (1.0 - sum_of_unreachable(newBP)) / sum_of_reachable(oldBP)
4135ffd83dbSDimitry Andric   BranchProbability NewUnreachableSum = BranchProbability::getZero();
4145ffd83dbSDimitry Andric   for (auto I : UnreachableIdxs)
4155ffd83dbSDimitry Andric     NewUnreachableSum += BP[I];
4165ffd83dbSDimitry Andric 
4175ffd83dbSDimitry Andric   BranchProbability NewReachableSum =
4185ffd83dbSDimitry Andric       BranchProbability::getOne() - NewUnreachableSum;
4195ffd83dbSDimitry Andric 
4205ffd83dbSDimitry Andric   BranchProbability OldReachableSum = BranchProbability::getZero();
4215ffd83dbSDimitry Andric   for (auto I : ReachableIdxs)
4225ffd83dbSDimitry Andric     OldReachableSum += BP[I];
4235ffd83dbSDimitry Andric 
4245ffd83dbSDimitry Andric   if (OldReachableSum != NewReachableSum) { // Anything to dsitribute?
4255ffd83dbSDimitry Andric     if (OldReachableSum.isZero()) {
4265ffd83dbSDimitry Andric       // If all oldBP[i] are zeroes then the proportional distribution results
4275ffd83dbSDimitry Andric       // in all zero probabilities and the error stays big. In this case we
4285ffd83dbSDimitry Andric       // evenly spread NewReachableSum over the reachable edges.
4295ffd83dbSDimitry Andric       BranchProbability PerEdge = NewReachableSum / ReachableIdxs.size();
4305ffd83dbSDimitry Andric       for (auto I : ReachableIdxs)
4315ffd83dbSDimitry Andric         BP[I] = PerEdge;
4325ffd83dbSDimitry Andric     } else {
4335ffd83dbSDimitry Andric       for (auto I : ReachableIdxs) {
4345ffd83dbSDimitry Andric         // We use uint64_t to avoid double rounding error of the following
4355ffd83dbSDimitry Andric         // calculation: BP[i] = BP[i] * NewReachableSum / OldReachableSum
4365ffd83dbSDimitry Andric         // The formula is taken from the private constructor
4375ffd83dbSDimitry Andric         // BranchProbability(uint32_t Numerator, uint32_t Denominator)
4385ffd83dbSDimitry Andric         uint64_t Mul = static_cast<uint64_t>(NewReachableSum.getNumerator()) *
4395ffd83dbSDimitry Andric                        BP[I].getNumerator();
4405ffd83dbSDimitry Andric         uint32_t Div = static_cast<uint32_t>(
4415ffd83dbSDimitry Andric             divideNearest(Mul, OldReachableSum.getNumerator()));
4425ffd83dbSDimitry Andric         BP[I] = BranchProbability::getRaw(Div);
4435ffd83dbSDimitry Andric       }
4440b57cec5SDimitry Andric     }
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric 
4475ffd83dbSDimitry Andric   setEdgeProbability(BB, BP);
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   return true;
4500b57cec5SDimitry Andric }
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric // Calculate Edge Weights using "Pointer Heuristics". Predict a comparison
4530b57cec5SDimitry Andric // between two pointer or pointer and NULL will fail.
4540b57cec5SDimitry Andric bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) {
4550b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
4560b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
4570b57cec5SDimitry Andric     return false;
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   Value *Cond = BI->getCondition();
4600b57cec5SDimitry Andric   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
4610b57cec5SDimitry Andric   if (!CI || !CI->isEquality())
4620b57cec5SDimitry Andric     return false;
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   Value *LHS = CI->getOperand(0);
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric   if (!LHS->getType()->isPointerTy())
4670b57cec5SDimitry Andric     return false;
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric   assert(CI->getOperand(1)->getType()->isPointerTy());
4700b57cec5SDimitry Andric 
4715ffd83dbSDimitry Andric   BranchProbability TakenProb(PH_TAKEN_WEIGHT,
4725ffd83dbSDimitry Andric                               PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
4735ffd83dbSDimitry Andric   BranchProbability UntakenProb(PH_NONTAKEN_WEIGHT,
4745ffd83dbSDimitry Andric                                 PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
4755ffd83dbSDimitry Andric 
4760b57cec5SDimitry Andric   // p != 0   ->   isProb = true
4770b57cec5SDimitry Andric   // p == 0   ->   isProb = false
4780b57cec5SDimitry Andric   // p != q   ->   isProb = true
4790b57cec5SDimitry Andric   // p == q   ->   isProb = false;
4800b57cec5SDimitry Andric   bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE;
4810b57cec5SDimitry Andric   if (!isProb)
4825ffd83dbSDimitry Andric     std::swap(TakenProb, UntakenProb);
4830b57cec5SDimitry Andric 
4845ffd83dbSDimitry Andric   setEdgeProbability(
4855ffd83dbSDimitry Andric       BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb}));
4860b57cec5SDimitry Andric   return true;
4870b57cec5SDimitry Andric }
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric // Compute the unlikely successors to the block BB in the loop L, specifically
4900b57cec5SDimitry Andric // those that are unlikely because this is a loop, and add them to the
4910b57cec5SDimitry Andric // UnlikelyBlocks set.
4920b57cec5SDimitry Andric static void
4930b57cec5SDimitry Andric computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,
4940b57cec5SDimitry Andric                           SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) {
4950b57cec5SDimitry Andric   // Sometimes in a loop we have a branch whose condition is made false by
4960b57cec5SDimitry Andric   // taking it. This is typically something like
4970b57cec5SDimitry Andric   //  int n = 0;
4980b57cec5SDimitry Andric   //  while (...) {
4990b57cec5SDimitry Andric   //    if (++n >= MAX) {
5000b57cec5SDimitry Andric   //      n = 0;
5010b57cec5SDimitry Andric   //    }
5020b57cec5SDimitry Andric   //  }
5030b57cec5SDimitry Andric   // In this sort of situation taking the branch means that at the very least it
5040b57cec5SDimitry Andric   // won't be taken again in the next iteration of the loop, so we should
5050b57cec5SDimitry Andric   // consider it less likely than a typical branch.
5060b57cec5SDimitry Andric   //
5070b57cec5SDimitry Andric   // We detect this by looking back through the graph of PHI nodes that sets the
5080b57cec5SDimitry Andric   // value that the condition depends on, and seeing if we can reach a successor
5090b57cec5SDimitry Andric   // block which can be determined to make the condition false.
5100b57cec5SDimitry Andric   //
5110b57cec5SDimitry Andric   // FIXME: We currently consider unlikely blocks to be half as likely as other
5120b57cec5SDimitry Andric   // blocks, but if we consider the example above the likelyhood is actually
5130b57cec5SDimitry Andric   // 1/MAX. We could therefore be more precise in how unlikely we consider
5140b57cec5SDimitry Andric   // blocks to be, but it would require more careful examination of the form
5150b57cec5SDimitry Andric   // of the comparison expression.
5160b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
5170b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
5180b57cec5SDimitry Andric     return;
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   // Check if the branch is based on an instruction compared with a constant
5210b57cec5SDimitry Andric   CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
5220b57cec5SDimitry Andric   if (!CI || !isa<Instruction>(CI->getOperand(0)) ||
5230b57cec5SDimitry Andric       !isa<Constant>(CI->getOperand(1)))
5240b57cec5SDimitry Andric     return;
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric   // Either the instruction must be a PHI, or a chain of operations involving
5270b57cec5SDimitry Andric   // constants that ends in a PHI which we can then collapse into a single value
5280b57cec5SDimitry Andric   // if the PHI value is known.
5290b57cec5SDimitry Andric   Instruction *CmpLHS = dyn_cast<Instruction>(CI->getOperand(0));
5300b57cec5SDimitry Andric   PHINode *CmpPHI = dyn_cast<PHINode>(CmpLHS);
5310b57cec5SDimitry Andric   Constant *CmpConst = dyn_cast<Constant>(CI->getOperand(1));
5320b57cec5SDimitry Andric   // Collect the instructions until we hit a PHI
5330b57cec5SDimitry Andric   SmallVector<BinaryOperator *, 1> InstChain;
5340b57cec5SDimitry Andric   while (!CmpPHI && CmpLHS && isa<BinaryOperator>(CmpLHS) &&
5350b57cec5SDimitry Andric          isa<Constant>(CmpLHS->getOperand(1))) {
5360b57cec5SDimitry Andric     // Stop if the chain extends outside of the loop
5370b57cec5SDimitry Andric     if (!L->contains(CmpLHS))
5380b57cec5SDimitry Andric       return;
5390b57cec5SDimitry Andric     InstChain.push_back(cast<BinaryOperator>(CmpLHS));
5400b57cec5SDimitry Andric     CmpLHS = dyn_cast<Instruction>(CmpLHS->getOperand(0));
5410b57cec5SDimitry Andric     if (CmpLHS)
5420b57cec5SDimitry Andric       CmpPHI = dyn_cast<PHINode>(CmpLHS);
5430b57cec5SDimitry Andric   }
5440b57cec5SDimitry Andric   if (!CmpPHI || !L->contains(CmpPHI))
5450b57cec5SDimitry Andric     return;
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric   // Trace the phi node to find all values that come from successors of BB
5480b57cec5SDimitry Andric   SmallPtrSet<PHINode*, 8> VisitedInsts;
5490b57cec5SDimitry Andric   SmallVector<PHINode*, 8> WorkList;
5500b57cec5SDimitry Andric   WorkList.push_back(CmpPHI);
5510b57cec5SDimitry Andric   VisitedInsts.insert(CmpPHI);
5520b57cec5SDimitry Andric   while (!WorkList.empty()) {
5530b57cec5SDimitry Andric     PHINode *P = WorkList.back();
5540b57cec5SDimitry Andric     WorkList.pop_back();
5550b57cec5SDimitry Andric     for (BasicBlock *B : P->blocks()) {
5560b57cec5SDimitry Andric       // Skip blocks that aren't part of the loop
5570b57cec5SDimitry Andric       if (!L->contains(B))
5580b57cec5SDimitry Andric         continue;
5590b57cec5SDimitry Andric       Value *V = P->getIncomingValueForBlock(B);
5600b57cec5SDimitry Andric       // If the source is a PHI add it to the work list if we haven't
5610b57cec5SDimitry Andric       // already visited it.
5620b57cec5SDimitry Andric       if (PHINode *PN = dyn_cast<PHINode>(V)) {
5630b57cec5SDimitry Andric         if (VisitedInsts.insert(PN).second)
5640b57cec5SDimitry Andric           WorkList.push_back(PN);
5650b57cec5SDimitry Andric         continue;
5660b57cec5SDimitry Andric       }
5670b57cec5SDimitry Andric       // If this incoming value is a constant and B is a successor of BB, then
5680b57cec5SDimitry Andric       // we can constant-evaluate the compare to see if it makes the branch be
5690b57cec5SDimitry Andric       // taken or not.
5700b57cec5SDimitry Andric       Constant *CmpLHSConst = dyn_cast<Constant>(V);
571*e8d8bef9SDimitry Andric       if (!CmpLHSConst || !llvm::is_contained(successors(BB), B))
5720b57cec5SDimitry Andric         continue;
5730b57cec5SDimitry Andric       // First collapse InstChain
5740b57cec5SDimitry Andric       for (Instruction *I : llvm::reverse(InstChain)) {
5750b57cec5SDimitry Andric         CmpLHSConst = ConstantExpr::get(I->getOpcode(), CmpLHSConst,
5760b57cec5SDimitry Andric                                         cast<Constant>(I->getOperand(1)), true);
5770b57cec5SDimitry Andric         if (!CmpLHSConst)
5780b57cec5SDimitry Andric           break;
5790b57cec5SDimitry Andric       }
5800b57cec5SDimitry Andric       if (!CmpLHSConst)
5810b57cec5SDimitry Andric         continue;
5820b57cec5SDimitry Andric       // Now constant-evaluate the compare
5830b57cec5SDimitry Andric       Constant *Result = ConstantExpr::getCompare(CI->getPredicate(),
5840b57cec5SDimitry Andric                                                   CmpLHSConst, CmpConst, true);
5850b57cec5SDimitry Andric       // If the result means we don't branch to the block then that block is
5860b57cec5SDimitry Andric       // unlikely.
5870b57cec5SDimitry Andric       if (Result &&
5880b57cec5SDimitry Andric           ((Result->isZeroValue() && B == BI->getSuccessor(0)) ||
5890b57cec5SDimitry Andric            (Result->isOneValue() && B == BI->getSuccessor(1))))
5900b57cec5SDimitry Andric         UnlikelyBlocks.insert(B);
5910b57cec5SDimitry Andric     }
5920b57cec5SDimitry Andric   }
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
595*e8d8bef9SDimitry Andric Optional<uint32_t>
596*e8d8bef9SDimitry Andric BranchProbabilityInfo::getEstimatedBlockWeight(const BasicBlock *BB) const {
597*e8d8bef9SDimitry Andric   auto WeightIt = EstimatedBlockWeight.find(BB);
598*e8d8bef9SDimitry Andric   if (WeightIt == EstimatedBlockWeight.end())
599*e8d8bef9SDimitry Andric     return None;
600*e8d8bef9SDimitry Andric   return WeightIt->second;
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric 
603*e8d8bef9SDimitry Andric Optional<uint32_t>
604*e8d8bef9SDimitry Andric BranchProbabilityInfo::getEstimatedLoopWeight(const LoopData &L) const {
605*e8d8bef9SDimitry Andric   auto WeightIt = EstimatedLoopWeight.find(L);
606*e8d8bef9SDimitry Andric   if (WeightIt == EstimatedLoopWeight.end())
607*e8d8bef9SDimitry Andric     return None;
608*e8d8bef9SDimitry Andric   return WeightIt->second;
609*e8d8bef9SDimitry Andric }
610*e8d8bef9SDimitry Andric 
611*e8d8bef9SDimitry Andric Optional<uint32_t>
612*e8d8bef9SDimitry Andric BranchProbabilityInfo::getEstimatedEdgeWeight(const LoopEdge &Edge) const {
613*e8d8bef9SDimitry Andric   // For edges entering a loop take weight of a loop rather than an individual
614*e8d8bef9SDimitry Andric   // block in the loop.
615*e8d8bef9SDimitry Andric   return isLoopEnteringEdge(Edge)
616*e8d8bef9SDimitry Andric              ? getEstimatedLoopWeight(Edge.second.getLoopData())
617*e8d8bef9SDimitry Andric              : getEstimatedBlockWeight(Edge.second.getBlock());
618*e8d8bef9SDimitry Andric }
619*e8d8bef9SDimitry Andric 
620*e8d8bef9SDimitry Andric template <class IterT>
621*e8d8bef9SDimitry Andric Optional<uint32_t> BranchProbabilityInfo::getMaxEstimatedEdgeWeight(
622*e8d8bef9SDimitry Andric     const LoopBlock &SrcLoopBB, iterator_range<IterT> Successors) const {
623*e8d8bef9SDimitry Andric   SmallVector<uint32_t, 4> Weights;
624*e8d8bef9SDimitry Andric   Optional<uint32_t> MaxWeight;
625*e8d8bef9SDimitry Andric   for (const BasicBlock *DstBB : Successors) {
626*e8d8bef9SDimitry Andric     const LoopBlock DstLoopBB = getLoopBlock(DstBB);
627*e8d8bef9SDimitry Andric     auto Weight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
628*e8d8bef9SDimitry Andric 
629*e8d8bef9SDimitry Andric     if (!Weight)
630*e8d8bef9SDimitry Andric       return None;
631*e8d8bef9SDimitry Andric 
632*e8d8bef9SDimitry Andric     if (!MaxWeight || MaxWeight.getValue() < Weight.getValue())
633*e8d8bef9SDimitry Andric       MaxWeight = Weight;
634*e8d8bef9SDimitry Andric   }
635*e8d8bef9SDimitry Andric 
636*e8d8bef9SDimitry Andric   return MaxWeight;
637*e8d8bef9SDimitry Andric }
638*e8d8bef9SDimitry Andric 
639*e8d8bef9SDimitry Andric // Updates \p LoopBB's weight and returns true. If \p LoopBB has already
640*e8d8bef9SDimitry Andric // an associated weight it is unchanged and false is returned.
641*e8d8bef9SDimitry Andric //
642*e8d8bef9SDimitry Andric // Please note by the algorithm the weight is not expected to change once set
643*e8d8bef9SDimitry Andric // thus 'false' status is used to track visited blocks.
644*e8d8bef9SDimitry Andric bool BranchProbabilityInfo::updateEstimatedBlockWeight(
645*e8d8bef9SDimitry Andric     LoopBlock &LoopBB, uint32_t BBWeight,
646*e8d8bef9SDimitry Andric     SmallVectorImpl<BasicBlock *> &BlockWorkList,
647*e8d8bef9SDimitry Andric     SmallVectorImpl<LoopBlock> &LoopWorkList) {
648*e8d8bef9SDimitry Andric   BasicBlock *BB = LoopBB.getBlock();
649*e8d8bef9SDimitry Andric 
650*e8d8bef9SDimitry Andric   // In general, weight is assigned to a block when it has final value and
651*e8d8bef9SDimitry Andric   // can't/shouldn't be changed.  However, there are cases when a block
652*e8d8bef9SDimitry Andric   // inherently has several (possibly "contradicting") weights. For example,
653*e8d8bef9SDimitry Andric   // "unwind" block may also contain "cold" call. In that case the first
654*e8d8bef9SDimitry Andric   // set weight is favored and all consequent weights are ignored.
655*e8d8bef9SDimitry Andric   if (!EstimatedBlockWeight.insert({BB, BBWeight}).second)
656*e8d8bef9SDimitry Andric     return false;
657*e8d8bef9SDimitry Andric 
658*e8d8bef9SDimitry Andric   for (BasicBlock *PredBlock : predecessors(BB)) {
659*e8d8bef9SDimitry Andric     LoopBlock PredLoop = getLoopBlock(PredBlock);
660*e8d8bef9SDimitry Andric     // Add affected block/loop to a working list.
661*e8d8bef9SDimitry Andric     if (isLoopExitingEdge({PredLoop, LoopBB})) {
662*e8d8bef9SDimitry Andric       if (!EstimatedLoopWeight.count(PredLoop.getLoopData()))
663*e8d8bef9SDimitry Andric         LoopWorkList.push_back(PredLoop);
664*e8d8bef9SDimitry Andric     } else if (!EstimatedBlockWeight.count(PredBlock))
665*e8d8bef9SDimitry Andric       BlockWorkList.push_back(PredBlock);
666*e8d8bef9SDimitry Andric   }
667*e8d8bef9SDimitry Andric   return true;
668*e8d8bef9SDimitry Andric }
669*e8d8bef9SDimitry Andric 
670*e8d8bef9SDimitry Andric // Starting from \p BB traverse through dominator blocks and assign \p BBWeight
671*e8d8bef9SDimitry Andric // to all such blocks that are post dominated by \BB. In other words to all
672*e8d8bef9SDimitry Andric // blocks that the one is executed if and only if another one is executed.
673*e8d8bef9SDimitry Andric // Importantly, we skip loops here for two reasons. First weights of blocks in
674*e8d8bef9SDimitry Andric // a loop should be scaled by trip count (yet possibly unknown). Second there is
675*e8d8bef9SDimitry Andric // no any value in doing that because that doesn't give any additional
676*e8d8bef9SDimitry Andric // information regarding distribution of probabilities inside the loop.
677*e8d8bef9SDimitry Andric // Exception is loop 'enter' and 'exit' edges that are handled in a special way
678*e8d8bef9SDimitry Andric // at calcEstimatedHeuristics.
679*e8d8bef9SDimitry Andric //
680*e8d8bef9SDimitry Andric // In addition, \p WorkList is populated with basic blocks if at leas one
681*e8d8bef9SDimitry Andric // successor has updated estimated weight.
682*e8d8bef9SDimitry Andric void BranchProbabilityInfo::propagateEstimatedBlockWeight(
683*e8d8bef9SDimitry Andric     const LoopBlock &LoopBB, DominatorTree *DT, PostDominatorTree *PDT,
684*e8d8bef9SDimitry Andric     uint32_t BBWeight, SmallVectorImpl<BasicBlock *> &BlockWorkList,
685*e8d8bef9SDimitry Andric     SmallVectorImpl<LoopBlock> &LoopWorkList) {
686*e8d8bef9SDimitry Andric   const BasicBlock *BB = LoopBB.getBlock();
687*e8d8bef9SDimitry Andric   const auto *DTStartNode = DT->getNode(BB);
688*e8d8bef9SDimitry Andric   const auto *PDTStartNode = PDT->getNode(BB);
689*e8d8bef9SDimitry Andric 
690*e8d8bef9SDimitry Andric   // TODO: Consider propagating weight down the domination line as well.
691*e8d8bef9SDimitry Andric   for (const auto *DTNode = DTStartNode; DTNode != nullptr;
692*e8d8bef9SDimitry Andric        DTNode = DTNode->getIDom()) {
693*e8d8bef9SDimitry Andric     auto *DomBB = DTNode->getBlock();
694*e8d8bef9SDimitry Andric     // Consider blocks which lie on one 'line'.
695*e8d8bef9SDimitry Andric     if (!PDT->dominates(PDTStartNode, PDT->getNode(DomBB)))
696*e8d8bef9SDimitry Andric       // If BB doesn't post dominate DomBB it will not post dominate dominators
697*e8d8bef9SDimitry Andric       // of DomBB as well.
698*e8d8bef9SDimitry Andric       break;
699*e8d8bef9SDimitry Andric 
700*e8d8bef9SDimitry Andric     LoopBlock DomLoopBB = getLoopBlock(DomBB);
701*e8d8bef9SDimitry Andric     const LoopEdge Edge{DomLoopBB, LoopBB};
702*e8d8bef9SDimitry Andric     // Don't propagate weight to blocks belonging to different loops.
703*e8d8bef9SDimitry Andric     if (!isLoopEnteringExitingEdge(Edge)) {
704*e8d8bef9SDimitry Andric       if (!updateEstimatedBlockWeight(DomLoopBB, BBWeight, BlockWorkList,
705*e8d8bef9SDimitry Andric                                       LoopWorkList))
706*e8d8bef9SDimitry Andric         // If DomBB has weight set then all it's predecessors are already
707*e8d8bef9SDimitry Andric         // processed (since we propagate weight up to the top of IR each time).
708*e8d8bef9SDimitry Andric         break;
709*e8d8bef9SDimitry Andric     } else if (isLoopExitingEdge(Edge)) {
710*e8d8bef9SDimitry Andric       LoopWorkList.push_back(DomLoopBB);
711*e8d8bef9SDimitry Andric     }
712*e8d8bef9SDimitry Andric   }
713*e8d8bef9SDimitry Andric }
714*e8d8bef9SDimitry Andric 
715*e8d8bef9SDimitry Andric Optional<uint32_t> BranchProbabilityInfo::getInitialEstimatedBlockWeight(
716*e8d8bef9SDimitry Andric     const BasicBlock *BB) {
717*e8d8bef9SDimitry Andric   // Returns true if \p BB has call marked with "NoReturn" attribute.
718*e8d8bef9SDimitry Andric   auto hasNoReturn = [&](const BasicBlock *BB) {
719*e8d8bef9SDimitry Andric     for (const auto &I : reverse(*BB))
720*e8d8bef9SDimitry Andric       if (const CallInst *CI = dyn_cast<CallInst>(&I))
721*e8d8bef9SDimitry Andric         if (CI->hasFnAttr(Attribute::NoReturn))
722*e8d8bef9SDimitry Andric           return true;
723*e8d8bef9SDimitry Andric 
724*e8d8bef9SDimitry Andric     return false;
725*e8d8bef9SDimitry Andric   };
726*e8d8bef9SDimitry Andric 
727*e8d8bef9SDimitry Andric   // Important note regarding the order of checks. They are ordered by weight
728*e8d8bef9SDimitry Andric   // from lowest to highest. Doing that allows to avoid "unstable" results
729*e8d8bef9SDimitry Andric   // when several conditions heuristics can be applied simultaneously.
730*e8d8bef9SDimitry Andric   if (isa<UnreachableInst>(BB->getTerminator()) ||
731*e8d8bef9SDimitry Andric       // If this block is terminated by a call to
732*e8d8bef9SDimitry Andric       // @llvm.experimental.deoptimize then treat it like an unreachable
733*e8d8bef9SDimitry Andric       // since it is expected to practically never execute.
734*e8d8bef9SDimitry Andric       // TODO: Should we actually treat as never returning call?
735*e8d8bef9SDimitry Andric       BB->getTerminatingDeoptimizeCall())
736*e8d8bef9SDimitry Andric     return hasNoReturn(BB)
737*e8d8bef9SDimitry Andric                ? static_cast<uint32_t>(BlockExecWeight::NORETURN)
738*e8d8bef9SDimitry Andric                : static_cast<uint32_t>(BlockExecWeight::UNREACHABLE);
739*e8d8bef9SDimitry Andric 
740*e8d8bef9SDimitry Andric   // Check if the block is 'unwind' handler of  some invoke instruction.
741*e8d8bef9SDimitry Andric   for (const auto *Pred : predecessors(BB))
742*e8d8bef9SDimitry Andric     if (Pred)
743*e8d8bef9SDimitry Andric       if (const auto *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
744*e8d8bef9SDimitry Andric         if (II->getUnwindDest() == BB)
745*e8d8bef9SDimitry Andric           return static_cast<uint32_t>(BlockExecWeight::UNWIND);
746*e8d8bef9SDimitry Andric 
747*e8d8bef9SDimitry Andric   // Check if the block contains 'cold' call.
748*e8d8bef9SDimitry Andric   for (const auto &I : *BB)
749*e8d8bef9SDimitry Andric     if (const CallInst *CI = dyn_cast<CallInst>(&I))
750*e8d8bef9SDimitry Andric       if (CI->hasFnAttr(Attribute::Cold))
751*e8d8bef9SDimitry Andric         return static_cast<uint32_t>(BlockExecWeight::COLD);
752*e8d8bef9SDimitry Andric 
753*e8d8bef9SDimitry Andric   return None;
754*e8d8bef9SDimitry Andric }
755*e8d8bef9SDimitry Andric 
756*e8d8bef9SDimitry Andric // Does RPO traversal over all blocks in \p F and assigns weights to
757*e8d8bef9SDimitry Andric // 'unreachable', 'noreturn', 'cold', 'unwind' blocks. In addition it does its
758*e8d8bef9SDimitry Andric // best to propagate the weight to up/down the IR.
759*e8d8bef9SDimitry Andric void BranchProbabilityInfo::computeEestimateBlockWeight(
760*e8d8bef9SDimitry Andric     const Function &F, DominatorTree *DT, PostDominatorTree *PDT) {
761*e8d8bef9SDimitry Andric   SmallVector<BasicBlock *, 8> BlockWorkList;
762*e8d8bef9SDimitry Andric   SmallVector<LoopBlock, 8> LoopWorkList;
763*e8d8bef9SDimitry Andric 
764*e8d8bef9SDimitry Andric   // By doing RPO we make sure that all predecessors already have weights
765*e8d8bef9SDimitry Andric   // calculated before visiting theirs successors.
766*e8d8bef9SDimitry Andric   ReversePostOrderTraversal<const Function *> RPOT(&F);
767*e8d8bef9SDimitry Andric   for (const auto *BB : RPOT)
768*e8d8bef9SDimitry Andric     if (auto BBWeight = getInitialEstimatedBlockWeight(BB))
769*e8d8bef9SDimitry Andric       // If we were able to find estimated weight for the block set it to this
770*e8d8bef9SDimitry Andric       // block and propagate up the IR.
771*e8d8bef9SDimitry Andric       propagateEstimatedBlockWeight(getLoopBlock(BB), DT, PDT,
772*e8d8bef9SDimitry Andric                                     BBWeight.getValue(), BlockWorkList,
773*e8d8bef9SDimitry Andric                                     LoopWorkList);
774*e8d8bef9SDimitry Andric 
775*e8d8bef9SDimitry Andric   // BlockWorklist/LoopWorkList contains blocks/loops with at least one
776*e8d8bef9SDimitry Andric   // successor/exit having estimated weight. Try to propagate weight to such
777*e8d8bef9SDimitry Andric   // blocks/loops from successors/exits.
778*e8d8bef9SDimitry Andric   // Process loops and blocks. Order is not important.
779*e8d8bef9SDimitry Andric   do {
780*e8d8bef9SDimitry Andric     while (!LoopWorkList.empty()) {
781*e8d8bef9SDimitry Andric       const LoopBlock LoopBB = LoopWorkList.pop_back_val();
782*e8d8bef9SDimitry Andric 
783*e8d8bef9SDimitry Andric       if (EstimatedLoopWeight.count(LoopBB.getLoopData()))
784*e8d8bef9SDimitry Andric         continue;
785*e8d8bef9SDimitry Andric 
786*e8d8bef9SDimitry Andric       SmallVector<BasicBlock *, 4> Exits;
787*e8d8bef9SDimitry Andric       getLoopExitBlocks(LoopBB, Exits);
788*e8d8bef9SDimitry Andric       auto LoopWeight = getMaxEstimatedEdgeWeight(
789*e8d8bef9SDimitry Andric           LoopBB, make_range(Exits.begin(), Exits.end()));
790*e8d8bef9SDimitry Andric 
791*e8d8bef9SDimitry Andric       if (LoopWeight) {
792*e8d8bef9SDimitry Andric         // If we never exit the loop then we can enter it once at maximum.
793*e8d8bef9SDimitry Andric         if (LoopWeight <= static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))
794*e8d8bef9SDimitry Andric           LoopWeight = static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO);
795*e8d8bef9SDimitry Andric 
796*e8d8bef9SDimitry Andric         EstimatedLoopWeight.insert(
797*e8d8bef9SDimitry Andric             {LoopBB.getLoopData(), LoopWeight.getValue()});
798*e8d8bef9SDimitry Andric         // Add all blocks entering the loop into working list.
799*e8d8bef9SDimitry Andric         getLoopEnterBlocks(LoopBB, BlockWorkList);
800*e8d8bef9SDimitry Andric       }
801*e8d8bef9SDimitry Andric     }
802*e8d8bef9SDimitry Andric 
803*e8d8bef9SDimitry Andric     while (!BlockWorkList.empty()) {
804*e8d8bef9SDimitry Andric       // We can reach here only if BlockWorkList is not empty.
805*e8d8bef9SDimitry Andric       const BasicBlock *BB = BlockWorkList.pop_back_val();
806*e8d8bef9SDimitry Andric       if (EstimatedBlockWeight.count(BB))
807*e8d8bef9SDimitry Andric         continue;
808*e8d8bef9SDimitry Andric 
809*e8d8bef9SDimitry Andric       // We take maximum over all weights of successors. In other words we take
810*e8d8bef9SDimitry Andric       // weight of "hot" path. In theory we can probably find a better function
811*e8d8bef9SDimitry Andric       // which gives higher accuracy results (comparing to "maximum") but I
812*e8d8bef9SDimitry Andric       // can't
813*e8d8bef9SDimitry Andric       // think of any right now. And I doubt it will make any difference in
814*e8d8bef9SDimitry Andric       // practice.
815*e8d8bef9SDimitry Andric       const LoopBlock LoopBB = getLoopBlock(BB);
816*e8d8bef9SDimitry Andric       auto MaxWeight = getMaxEstimatedEdgeWeight(LoopBB, successors(BB));
817*e8d8bef9SDimitry Andric 
818*e8d8bef9SDimitry Andric       if (MaxWeight)
819*e8d8bef9SDimitry Andric         propagateEstimatedBlockWeight(LoopBB, DT, PDT, MaxWeight.getValue(),
820*e8d8bef9SDimitry Andric                                       BlockWorkList, LoopWorkList);
821*e8d8bef9SDimitry Andric     }
822*e8d8bef9SDimitry Andric   } while (!BlockWorkList.empty() || !LoopWorkList.empty());
823*e8d8bef9SDimitry Andric }
824*e8d8bef9SDimitry Andric 
825*e8d8bef9SDimitry Andric // Calculate edge probabilities based on block's estimated weight.
826*e8d8bef9SDimitry Andric // Note that gathered weights were not scaled for loops. Thus edges entering
827*e8d8bef9SDimitry Andric // and exiting loops requires special processing.
828*e8d8bef9SDimitry Andric bool BranchProbabilityInfo::calcEstimatedHeuristics(const BasicBlock *BB) {
829*e8d8bef9SDimitry Andric   assert(BB->getTerminator()->getNumSuccessors() > 1 &&
830*e8d8bef9SDimitry Andric          "expected more than one successor!");
831*e8d8bef9SDimitry Andric 
832*e8d8bef9SDimitry Andric   const LoopBlock LoopBB = getLoopBlock(BB);
833*e8d8bef9SDimitry Andric 
8340b57cec5SDimitry Andric   SmallPtrSet<const BasicBlock *, 8> UnlikelyBlocks;
835*e8d8bef9SDimitry Andric   uint32_t TC = LBH_TAKEN_WEIGHT / LBH_NONTAKEN_WEIGHT;
836*e8d8bef9SDimitry Andric   if (LoopBB.getLoop())
837*e8d8bef9SDimitry Andric     computeUnlikelySuccessors(BB, LoopBB.getLoop(), UnlikelyBlocks);
8380b57cec5SDimitry Andric 
839*e8d8bef9SDimitry Andric   // Changed to 'true' if at least one successor has estimated weight.
840*e8d8bef9SDimitry Andric   bool FoundEstimatedWeight = false;
841*e8d8bef9SDimitry Andric   SmallVector<uint32_t, 4> SuccWeights;
842*e8d8bef9SDimitry Andric   uint64_t TotalWeight = 0;
843*e8d8bef9SDimitry Andric   // Go over all successors of BB and put their weights into SuccWeights.
8445ffd83dbSDimitry Andric   for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
845*e8d8bef9SDimitry Andric     const BasicBlock *SuccBB = *I;
846*e8d8bef9SDimitry Andric     Optional<uint32_t> Weight;
847*e8d8bef9SDimitry Andric     const LoopBlock SuccLoopBB = getLoopBlock(SuccBB);
848*e8d8bef9SDimitry Andric     const LoopEdge Edge{LoopBB, SuccLoopBB};
849*e8d8bef9SDimitry Andric 
850*e8d8bef9SDimitry Andric     Weight = getEstimatedEdgeWeight(Edge);
851*e8d8bef9SDimitry Andric 
852*e8d8bef9SDimitry Andric     if (isLoopExitingEdge(Edge) &&
853*e8d8bef9SDimitry Andric         // Avoid adjustment of ZERO weight since it should remain unchanged.
854*e8d8bef9SDimitry Andric         Weight != static_cast<uint32_t>(BlockExecWeight::ZERO)) {
855*e8d8bef9SDimitry Andric       // Scale down loop exiting weight by trip count.
856*e8d8bef9SDimitry Andric       Weight = std::max(
857*e8d8bef9SDimitry Andric           static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO),
858*e8d8bef9SDimitry Andric           Weight.getValueOr(static_cast<uint32_t>(BlockExecWeight::DEFAULT)) /
859*e8d8bef9SDimitry Andric               TC);
8600b57cec5SDimitry Andric     }
861*e8d8bef9SDimitry Andric     bool IsUnlikelyEdge = LoopBB.getLoop() && UnlikelyBlocks.contains(SuccBB);
862*e8d8bef9SDimitry Andric     if (IsUnlikelyEdge &&
863*e8d8bef9SDimitry Andric         // Avoid adjustment of ZERO weight since it should remain unchanged.
864*e8d8bef9SDimitry Andric         Weight != static_cast<uint32_t>(BlockExecWeight::ZERO)) {
865*e8d8bef9SDimitry Andric       // 'Unlikely' blocks have twice lower weight.
866*e8d8bef9SDimitry Andric       Weight = std::max(
867*e8d8bef9SDimitry Andric           static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO),
868*e8d8bef9SDimitry Andric           Weight.getValueOr(static_cast<uint32_t>(BlockExecWeight::DEFAULT)) /
869*e8d8bef9SDimitry Andric               2);
8700b57cec5SDimitry Andric     }
8710b57cec5SDimitry Andric 
872*e8d8bef9SDimitry Andric     if (Weight)
873*e8d8bef9SDimitry Andric       FoundEstimatedWeight = true;
874*e8d8bef9SDimitry Andric 
875*e8d8bef9SDimitry Andric     auto WeightVal =
876*e8d8bef9SDimitry Andric         Weight.getValueOr(static_cast<uint32_t>(BlockExecWeight::DEFAULT));
877*e8d8bef9SDimitry Andric     TotalWeight += WeightVal;
878*e8d8bef9SDimitry Andric     SuccWeights.push_back(WeightVal);
879*e8d8bef9SDimitry Andric   }
880*e8d8bef9SDimitry Andric 
881*e8d8bef9SDimitry Andric   // If non of blocks have estimated weight bail out.
882*e8d8bef9SDimitry Andric   // If TotalWeight is 0 that means weight of each successor is 0 as well and
883*e8d8bef9SDimitry Andric   // equally likely. Bail out early to not deal with devision by zero.
884*e8d8bef9SDimitry Andric   if (!FoundEstimatedWeight || TotalWeight == 0)
8850b57cec5SDimitry Andric     return false;
8860b57cec5SDimitry Andric 
887*e8d8bef9SDimitry Andric   assert(SuccWeights.size() == succ_size(BB) && "Missed successor?");
888*e8d8bef9SDimitry Andric   const unsigned SuccCount = SuccWeights.size();
8890b57cec5SDimitry Andric 
890*e8d8bef9SDimitry Andric   // If the sum of weights does not fit in 32 bits, scale every weight down
891*e8d8bef9SDimitry Andric   // accordingly.
892*e8d8bef9SDimitry Andric   if (TotalWeight > UINT32_MAX) {
893*e8d8bef9SDimitry Andric     uint64_t ScalingFactor = TotalWeight / UINT32_MAX + 1;
894*e8d8bef9SDimitry Andric     TotalWeight = 0;
895*e8d8bef9SDimitry Andric     for (unsigned Idx = 0; Idx < SuccCount; ++Idx) {
896*e8d8bef9SDimitry Andric       SuccWeights[Idx] /= ScalingFactor;
897*e8d8bef9SDimitry Andric       if (SuccWeights[Idx] == static_cast<uint32_t>(BlockExecWeight::ZERO))
898*e8d8bef9SDimitry Andric         SuccWeights[Idx] =
899*e8d8bef9SDimitry Andric             static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO);
900*e8d8bef9SDimitry Andric       TotalWeight += SuccWeights[Idx];
901*e8d8bef9SDimitry Andric     }
902*e8d8bef9SDimitry Andric     assert(TotalWeight <= UINT32_MAX && "Total weight overflows");
903*e8d8bef9SDimitry Andric   }
904*e8d8bef9SDimitry Andric 
905*e8d8bef9SDimitry Andric   // Finally set probabilities to edges according to estimated block weights.
9065ffd83dbSDimitry Andric   SmallVector<BranchProbability, 4> EdgeProbabilities(
907*e8d8bef9SDimitry Andric       SuccCount, BranchProbability::getUnknown());
9080b57cec5SDimitry Andric 
909*e8d8bef9SDimitry Andric   for (unsigned Idx = 0; Idx < SuccCount; ++Idx) {
910*e8d8bef9SDimitry Andric     EdgeProbabilities[Idx] =
911*e8d8bef9SDimitry Andric         BranchProbability(SuccWeights[Idx], (uint32_t)TotalWeight);
9120b57cec5SDimitry Andric   }
9135ffd83dbSDimitry Andric   setEdgeProbability(BB, EdgeProbabilities);
9140b57cec5SDimitry Andric   return true;
9150b57cec5SDimitry Andric }
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB,
9180b57cec5SDimitry Andric                                                const TargetLibraryInfo *TLI) {
9190b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
9200b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
9210b57cec5SDimitry Andric     return false;
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric   Value *Cond = BI->getCondition();
9240b57cec5SDimitry Andric   ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
9250b57cec5SDimitry Andric   if (!CI)
9260b57cec5SDimitry Andric     return false;
9270b57cec5SDimitry Andric 
9280b57cec5SDimitry Andric   auto GetConstantInt = [](Value *V) {
9290b57cec5SDimitry Andric     if (auto *I = dyn_cast<BitCastInst>(V))
9300b57cec5SDimitry Andric       return dyn_cast<ConstantInt>(I->getOperand(0));
9310b57cec5SDimitry Andric     return dyn_cast<ConstantInt>(V);
9320b57cec5SDimitry Andric   };
9330b57cec5SDimitry Andric 
9340b57cec5SDimitry Andric   Value *RHS = CI->getOperand(1);
9350b57cec5SDimitry Andric   ConstantInt *CV = GetConstantInt(RHS);
9360b57cec5SDimitry Andric   if (!CV)
9370b57cec5SDimitry Andric     return false;
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric   // If the LHS is the result of AND'ing a value with a single bit bitmask,
9400b57cec5SDimitry Andric   // we don't have information about probabilities.
9410b57cec5SDimitry Andric   if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0)))
9420b57cec5SDimitry Andric     if (LHS->getOpcode() == Instruction::And)
943*e8d8bef9SDimitry Andric       if (ConstantInt *AndRHS = GetConstantInt(LHS->getOperand(1)))
9440b57cec5SDimitry Andric         if (AndRHS->getValue().isPowerOf2())
9450b57cec5SDimitry Andric           return false;
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   // Check if the LHS is the return value of a library function
9480b57cec5SDimitry Andric   LibFunc Func = NumLibFuncs;
9490b57cec5SDimitry Andric   if (TLI)
9500b57cec5SDimitry Andric     if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0)))
9510b57cec5SDimitry Andric       if (Function *CalledFn = Call->getCalledFunction())
9520b57cec5SDimitry Andric         TLI->getLibFunc(*CalledFn, Func);
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric   bool isProb;
9550b57cec5SDimitry Andric   if (Func == LibFunc_strcasecmp ||
9560b57cec5SDimitry Andric       Func == LibFunc_strcmp ||
9570b57cec5SDimitry Andric       Func == LibFunc_strncasecmp ||
9580b57cec5SDimitry Andric       Func == LibFunc_strncmp ||
959*e8d8bef9SDimitry Andric       Func == LibFunc_memcmp ||
960*e8d8bef9SDimitry Andric       Func == LibFunc_bcmp) {
9610b57cec5SDimitry Andric     // strcmp and similar functions return zero, negative, or positive, if the
9620b57cec5SDimitry Andric     // first string is equal, less, or greater than the second. We consider it
9630b57cec5SDimitry Andric     // likely that the strings are not equal, so a comparison with zero is
9640b57cec5SDimitry Andric     // probably false, but also a comparison with any other number is also
9650b57cec5SDimitry Andric     // probably false given that what exactly is returned for nonzero values is
9660b57cec5SDimitry Andric     // not specified. Any kind of comparison other than equality we know
9670b57cec5SDimitry Andric     // nothing about.
9680b57cec5SDimitry Andric     switch (CI->getPredicate()) {
9690b57cec5SDimitry Andric     case CmpInst::ICMP_EQ:
9700b57cec5SDimitry Andric       isProb = false;
9710b57cec5SDimitry Andric       break;
9720b57cec5SDimitry Andric     case CmpInst::ICMP_NE:
9730b57cec5SDimitry Andric       isProb = true;
9740b57cec5SDimitry Andric       break;
9750b57cec5SDimitry Andric     default:
9760b57cec5SDimitry Andric       return false;
9770b57cec5SDimitry Andric     }
9780b57cec5SDimitry Andric   } else if (CV->isZero()) {
9790b57cec5SDimitry Andric     switch (CI->getPredicate()) {
9800b57cec5SDimitry Andric     case CmpInst::ICMP_EQ:
9810b57cec5SDimitry Andric       // X == 0   ->  Unlikely
9820b57cec5SDimitry Andric       isProb = false;
9830b57cec5SDimitry Andric       break;
9840b57cec5SDimitry Andric     case CmpInst::ICMP_NE:
9850b57cec5SDimitry Andric       // X != 0   ->  Likely
9860b57cec5SDimitry Andric       isProb = true;
9870b57cec5SDimitry Andric       break;
9880b57cec5SDimitry Andric     case CmpInst::ICMP_SLT:
9890b57cec5SDimitry Andric       // X < 0   ->  Unlikely
9900b57cec5SDimitry Andric       isProb = false;
9910b57cec5SDimitry Andric       break;
9920b57cec5SDimitry Andric     case CmpInst::ICMP_SGT:
9930b57cec5SDimitry Andric       // X > 0   ->  Likely
9940b57cec5SDimitry Andric       isProb = true;
9950b57cec5SDimitry Andric       break;
9960b57cec5SDimitry Andric     default:
9970b57cec5SDimitry Andric       return false;
9980b57cec5SDimitry Andric     }
9990b57cec5SDimitry Andric   } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
10000b57cec5SDimitry Andric     // InstCombine canonicalizes X <= 0 into X < 1.
10010b57cec5SDimitry Andric     // X <= 0   ->  Unlikely
10020b57cec5SDimitry Andric     isProb = false;
10030b57cec5SDimitry Andric   } else if (CV->isMinusOne()) {
10040b57cec5SDimitry Andric     switch (CI->getPredicate()) {
10050b57cec5SDimitry Andric     case CmpInst::ICMP_EQ:
10060b57cec5SDimitry Andric       // X == -1  ->  Unlikely
10070b57cec5SDimitry Andric       isProb = false;
10080b57cec5SDimitry Andric       break;
10090b57cec5SDimitry Andric     case CmpInst::ICMP_NE:
10100b57cec5SDimitry Andric       // X != -1  ->  Likely
10110b57cec5SDimitry Andric       isProb = true;
10120b57cec5SDimitry Andric       break;
10130b57cec5SDimitry Andric     case CmpInst::ICMP_SGT:
10140b57cec5SDimitry Andric       // InstCombine canonicalizes X >= 0 into X > -1.
10150b57cec5SDimitry Andric       // X >= 0   ->  Likely
10160b57cec5SDimitry Andric       isProb = true;
10170b57cec5SDimitry Andric       break;
10180b57cec5SDimitry Andric     default:
10190b57cec5SDimitry Andric       return false;
10200b57cec5SDimitry Andric     }
10210b57cec5SDimitry Andric   } else {
10220b57cec5SDimitry Andric     return false;
10230b57cec5SDimitry Andric   }
10240b57cec5SDimitry Andric 
10250b57cec5SDimitry Andric   BranchProbability TakenProb(ZH_TAKEN_WEIGHT,
10260b57cec5SDimitry Andric                               ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
10275ffd83dbSDimitry Andric   BranchProbability UntakenProb(ZH_NONTAKEN_WEIGHT,
10285ffd83dbSDimitry Andric                                 ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
10295ffd83dbSDimitry Andric   if (!isProb)
10305ffd83dbSDimitry Andric     std::swap(TakenProb, UntakenProb);
10315ffd83dbSDimitry Andric 
10325ffd83dbSDimitry Andric   setEdgeProbability(
10335ffd83dbSDimitry Andric       BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb}));
10340b57cec5SDimitry Andric   return true;
10350b57cec5SDimitry Andric }
10360b57cec5SDimitry Andric 
10370b57cec5SDimitry Andric bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) {
10380b57cec5SDimitry Andric   const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
10390b57cec5SDimitry Andric   if (!BI || !BI->isConditional())
10400b57cec5SDimitry Andric     return false;
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric   Value *Cond = BI->getCondition();
10430b57cec5SDimitry Andric   FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
10440b57cec5SDimitry Andric   if (!FCmp)
10450b57cec5SDimitry Andric     return false;
10460b57cec5SDimitry Andric 
10478bcb0991SDimitry Andric   uint32_t TakenWeight = FPH_TAKEN_WEIGHT;
10488bcb0991SDimitry Andric   uint32_t NontakenWeight = FPH_NONTAKEN_WEIGHT;
10490b57cec5SDimitry Andric   bool isProb;
10500b57cec5SDimitry Andric   if (FCmp->isEquality()) {
10510b57cec5SDimitry Andric     // f1 == f2 -> Unlikely
10520b57cec5SDimitry Andric     // f1 != f2 -> Likely
10530b57cec5SDimitry Andric     isProb = !FCmp->isTrueWhenEqual();
10540b57cec5SDimitry Andric   } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
10550b57cec5SDimitry Andric     // !isnan -> Likely
10560b57cec5SDimitry Andric     isProb = true;
10578bcb0991SDimitry Andric     TakenWeight = FPH_ORD_WEIGHT;
10588bcb0991SDimitry Andric     NontakenWeight = FPH_UNO_WEIGHT;
10590b57cec5SDimitry Andric   } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
10600b57cec5SDimitry Andric     // isnan -> Unlikely
10610b57cec5SDimitry Andric     isProb = false;
10628bcb0991SDimitry Andric     TakenWeight = FPH_ORD_WEIGHT;
10638bcb0991SDimitry Andric     NontakenWeight = FPH_UNO_WEIGHT;
10640b57cec5SDimitry Andric   } else {
10650b57cec5SDimitry Andric     return false;
10660b57cec5SDimitry Andric   }
10670b57cec5SDimitry Andric 
10688bcb0991SDimitry Andric   BranchProbability TakenProb(TakenWeight, TakenWeight + NontakenWeight);
10695ffd83dbSDimitry Andric   BranchProbability UntakenProb(NontakenWeight, TakenWeight + NontakenWeight);
10705ffd83dbSDimitry Andric   if (!isProb)
10715ffd83dbSDimitry Andric     std::swap(TakenProb, UntakenProb);
10725ffd83dbSDimitry Andric 
10735ffd83dbSDimitry Andric   setEdgeProbability(
10745ffd83dbSDimitry Andric       BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb}));
10750b57cec5SDimitry Andric   return true;
10760b57cec5SDimitry Andric }
10770b57cec5SDimitry Andric 
10780b57cec5SDimitry Andric void BranchProbabilityInfo::releaseMemory() {
10790b57cec5SDimitry Andric   Probs.clear();
10805ffd83dbSDimitry Andric   Handles.clear();
10815ffd83dbSDimitry Andric }
10825ffd83dbSDimitry Andric 
10835ffd83dbSDimitry Andric bool BranchProbabilityInfo::invalidate(Function &, const PreservedAnalyses &PA,
10845ffd83dbSDimitry Andric                                        FunctionAnalysisManager::Invalidator &) {
10855ffd83dbSDimitry Andric   // Check whether the analysis, all analyses on functions, or the function's
10865ffd83dbSDimitry Andric   // CFG have been preserved.
10875ffd83dbSDimitry Andric   auto PAC = PA.getChecker<BranchProbabilityAnalysis>();
10885ffd83dbSDimitry Andric   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
10895ffd83dbSDimitry Andric            PAC.preservedSet<CFGAnalyses>());
10900b57cec5SDimitry Andric }
10910b57cec5SDimitry Andric 
10920b57cec5SDimitry Andric void BranchProbabilityInfo::print(raw_ostream &OS) const {
10930b57cec5SDimitry Andric   OS << "---- Branch Probabilities ----\n";
10940b57cec5SDimitry Andric   // We print the probabilities from the last function the analysis ran over,
10950b57cec5SDimitry Andric   // or the function it is currently running over.
10960b57cec5SDimitry Andric   assert(LastF && "Cannot print prior to running over a function");
10970b57cec5SDimitry Andric   for (const auto &BI : *LastF) {
10985ffd83dbSDimitry Andric     for (const_succ_iterator SI = succ_begin(&BI), SE = succ_end(&BI); SI != SE;
10990b57cec5SDimitry Andric          ++SI) {
11000b57cec5SDimitry Andric       printEdgeProbability(OS << "  ", &BI, *SI);
11010b57cec5SDimitry Andric     }
11020b57cec5SDimitry Andric   }
11030b57cec5SDimitry Andric }
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric bool BranchProbabilityInfo::
11060b57cec5SDimitry Andric isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
11070b57cec5SDimitry Andric   // Hot probability is at least 4/5 = 80%
11080b57cec5SDimitry Andric   // FIXME: Compare against a static "hot" BranchProbability.
11090b57cec5SDimitry Andric   return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
11100b57cec5SDimitry Andric }
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric const BasicBlock *
11130b57cec5SDimitry Andric BranchProbabilityInfo::getHotSucc(const BasicBlock *BB) const {
11140b57cec5SDimitry Andric   auto MaxProb = BranchProbability::getZero();
11150b57cec5SDimitry Andric   const BasicBlock *MaxSucc = nullptr;
11160b57cec5SDimitry Andric 
1117*e8d8bef9SDimitry Andric   for (const auto *Succ : successors(BB)) {
11180b57cec5SDimitry Andric     auto Prob = getEdgeProbability(BB, Succ);
11190b57cec5SDimitry Andric     if (Prob > MaxProb) {
11200b57cec5SDimitry Andric       MaxProb = Prob;
11210b57cec5SDimitry Andric       MaxSucc = Succ;
11220b57cec5SDimitry Andric     }
11230b57cec5SDimitry Andric   }
11240b57cec5SDimitry Andric 
11250b57cec5SDimitry Andric   // Hot probability is at least 4/5 = 80%
11260b57cec5SDimitry Andric   if (MaxProb > BranchProbability(4, 5))
11270b57cec5SDimitry Andric     return MaxSucc;
11280b57cec5SDimitry Andric 
11290b57cec5SDimitry Andric   return nullptr;
11300b57cec5SDimitry Andric }
11310b57cec5SDimitry Andric 
11320b57cec5SDimitry Andric /// Get the raw edge probability for the edge. If can't find it, return a
11330b57cec5SDimitry Andric /// default probability 1/N where N is the number of successors. Here an edge is
11340b57cec5SDimitry Andric /// specified using PredBlock and an
11350b57cec5SDimitry Andric /// index to the successors.
11360b57cec5SDimitry Andric BranchProbability
11370b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
11380b57cec5SDimitry Andric                                           unsigned IndexInSuccessors) const {
11390b57cec5SDimitry Andric   auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
1140*e8d8bef9SDimitry Andric   assert((Probs.end() == Probs.find(std::make_pair(Src, 0))) ==
1141*e8d8bef9SDimitry Andric              (Probs.end() == I) &&
1142*e8d8bef9SDimitry Andric          "Probability for I-th successor must always be defined along with the "
1143*e8d8bef9SDimitry Andric          "probability for the first successor");
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric   if (I != Probs.end())
11460b57cec5SDimitry Andric     return I->second;
11470b57cec5SDimitry Andric 
11480b57cec5SDimitry Andric   return {1, static_cast<uint32_t>(succ_size(Src))};
11490b57cec5SDimitry Andric }
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric BranchProbability
11520b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
11535ffd83dbSDimitry Andric                                           const_succ_iterator Dst) const {
11540b57cec5SDimitry Andric   return getEdgeProbability(Src, Dst.getSuccessorIndex());
11550b57cec5SDimitry Andric }
11560b57cec5SDimitry Andric 
11570b57cec5SDimitry Andric /// Get the raw edge probability calculated for the block pair. This returns the
11580b57cec5SDimitry Andric /// sum of all raw edge probabilities from Src to Dst.
11590b57cec5SDimitry Andric BranchProbability
11600b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
11610b57cec5SDimitry Andric                                           const BasicBlock *Dst) const {
1162*e8d8bef9SDimitry Andric   if (!Probs.count(std::make_pair(Src, 0)))
1163*e8d8bef9SDimitry Andric     return BranchProbability(llvm::count(successors(Src), Dst), succ_size(Src));
11640b57cec5SDimitry Andric 
1165*e8d8bef9SDimitry Andric   auto Prob = BranchProbability::getZero();
1166*e8d8bef9SDimitry Andric   for (const_succ_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)
1167*e8d8bef9SDimitry Andric     if (*I == Dst)
1168*e8d8bef9SDimitry Andric       Prob += Probs.find(std::make_pair(Src, I.getSuccessorIndex()))->second;
1169*e8d8bef9SDimitry Andric 
1170*e8d8bef9SDimitry Andric   return Prob;
11710b57cec5SDimitry Andric }
11720b57cec5SDimitry Andric 
11735ffd83dbSDimitry Andric /// Set the edge probability for all edges at once.
11745ffd83dbSDimitry Andric void BranchProbabilityInfo::setEdgeProbability(
11755ffd83dbSDimitry Andric     const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) {
11765ffd83dbSDimitry Andric   assert(Src->getTerminator()->getNumSuccessors() == Probs.size());
1177*e8d8bef9SDimitry Andric   eraseBlock(Src); // Erase stale data if any.
11785ffd83dbSDimitry Andric   if (Probs.size() == 0)
11795ffd83dbSDimitry Andric     return; // Nothing to set.
11805ffd83dbSDimitry Andric 
1181*e8d8bef9SDimitry Andric   Handles.insert(BasicBlockCallbackVH(Src, this));
11825ffd83dbSDimitry Andric   uint64_t TotalNumerator = 0;
11835ffd83dbSDimitry Andric   for (unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) {
1184*e8d8bef9SDimitry Andric     this->Probs[std::make_pair(Src, SuccIdx)] = Probs[SuccIdx];
1185*e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << SuccIdx
1186*e8d8bef9SDimitry Andric                       << " successor probability to " << Probs[SuccIdx]
1187*e8d8bef9SDimitry Andric                       << "\n");
11885ffd83dbSDimitry Andric     TotalNumerator += Probs[SuccIdx].getNumerator();
11895ffd83dbSDimitry Andric   }
11905ffd83dbSDimitry Andric 
11915ffd83dbSDimitry Andric   // Because of rounding errors the total probability cannot be checked to be
11925ffd83dbSDimitry Andric   // 1.0 exactly. That is TotalNumerator == BranchProbability::getDenominator.
11935ffd83dbSDimitry Andric   // Instead, every single probability in Probs must be as accurate as possible.
11945ffd83dbSDimitry Andric   // This results in error 1/denominator at most, thus the total absolute error
11955ffd83dbSDimitry Andric   // should be within Probs.size / BranchProbability::getDenominator.
11965ffd83dbSDimitry Andric   assert(TotalNumerator <= BranchProbability::getDenominator() + Probs.size());
11975ffd83dbSDimitry Andric   assert(TotalNumerator >= BranchProbability::getDenominator() - Probs.size());
11985ffd83dbSDimitry Andric }
11995ffd83dbSDimitry Andric 
1200*e8d8bef9SDimitry Andric void BranchProbabilityInfo::copyEdgeProbabilities(BasicBlock *Src,
1201*e8d8bef9SDimitry Andric                                                   BasicBlock *Dst) {
1202*e8d8bef9SDimitry Andric   eraseBlock(Dst); // Erase stale data if any.
1203*e8d8bef9SDimitry Andric   unsigned NumSuccessors = Src->getTerminator()->getNumSuccessors();
1204*e8d8bef9SDimitry Andric   assert(NumSuccessors == Dst->getTerminator()->getNumSuccessors());
1205*e8d8bef9SDimitry Andric   if (NumSuccessors == 0)
1206*e8d8bef9SDimitry Andric     return; // Nothing to set.
1207*e8d8bef9SDimitry Andric   if (this->Probs.find(std::make_pair(Src, 0)) == this->Probs.end())
1208*e8d8bef9SDimitry Andric     return; // No probability is set for edges from Src. Keep the same for Dst.
1209*e8d8bef9SDimitry Andric 
1210*e8d8bef9SDimitry Andric   Handles.insert(BasicBlockCallbackVH(Dst, this));
1211*e8d8bef9SDimitry Andric   for (unsigned SuccIdx = 0; SuccIdx < NumSuccessors; ++SuccIdx) {
1212*e8d8bef9SDimitry Andric     auto Prob = this->Probs[std::make_pair(Src, SuccIdx)];
1213*e8d8bef9SDimitry Andric     this->Probs[std::make_pair(Dst, SuccIdx)] = Prob;
1214*e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "set edge " << Dst->getName() << " -> " << SuccIdx
1215*e8d8bef9SDimitry Andric                       << " successor probability to " << Prob << "\n");
1216*e8d8bef9SDimitry Andric   }
1217*e8d8bef9SDimitry Andric }
1218*e8d8bef9SDimitry Andric 
12190b57cec5SDimitry Andric raw_ostream &
12200b57cec5SDimitry Andric BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
12210b57cec5SDimitry Andric                                             const BasicBlock *Src,
12220b57cec5SDimitry Andric                                             const BasicBlock *Dst) const {
12230b57cec5SDimitry Andric   const BranchProbability Prob = getEdgeProbability(Src, Dst);
12240b57cec5SDimitry Andric   OS << "edge " << Src->getName() << " -> " << Dst->getName()
12250b57cec5SDimitry Andric      << " probability is " << Prob
12260b57cec5SDimitry Andric      << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
12270b57cec5SDimitry Andric 
12280b57cec5SDimitry Andric   return OS;
12290b57cec5SDimitry Andric }
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) {
1232*e8d8bef9SDimitry Andric   LLVM_DEBUG(dbgs() << "eraseBlock " << BB->getName() << "\n");
1233*e8d8bef9SDimitry Andric 
1234*e8d8bef9SDimitry Andric   // Note that we cannot use successors of BB because the terminator of BB may
1235*e8d8bef9SDimitry Andric   // have changed when eraseBlock is called as a BasicBlockCallbackVH callback.
1236*e8d8bef9SDimitry Andric   // Instead we remove prob data for the block by iterating successors by their
1237*e8d8bef9SDimitry Andric   // indices from 0 till the last which exists. There could not be prob data for
1238*e8d8bef9SDimitry Andric   // a pair (BB, N) if there is no data for (BB, N-1) because the data is always
1239*e8d8bef9SDimitry Andric   // set for all successors from 0 to M at once by the method
1240*e8d8bef9SDimitry Andric   // setEdgeProbability().
1241*e8d8bef9SDimitry Andric   Handles.erase(BasicBlockCallbackVH(BB, this));
1242*e8d8bef9SDimitry Andric   for (unsigned I = 0;; ++I) {
1243*e8d8bef9SDimitry Andric     auto MapI = Probs.find(std::make_pair(BB, I));
1244*e8d8bef9SDimitry Andric     if (MapI == Probs.end()) {
1245*e8d8bef9SDimitry Andric       assert(Probs.count(std::make_pair(BB, I + 1)) == 0 &&
1246*e8d8bef9SDimitry Andric              "Must be no more successors");
1247*e8d8bef9SDimitry Andric       return;
1248*e8d8bef9SDimitry Andric     }
12495ffd83dbSDimitry Andric     Probs.erase(MapI);
12500b57cec5SDimitry Andric   }
12510b57cec5SDimitry Andric }
12520b57cec5SDimitry Andric 
1253*e8d8bef9SDimitry Andric void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LoopI,
12545ffd83dbSDimitry Andric                                       const TargetLibraryInfo *TLI,
1255*e8d8bef9SDimitry Andric                                       DominatorTree *DT,
12565ffd83dbSDimitry Andric                                       PostDominatorTree *PDT) {
12570b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName()
12580b57cec5SDimitry Andric                     << " ----\n\n");
12590b57cec5SDimitry Andric   LastF = &F; // Store the last function we ran on for printing.
1260*e8d8bef9SDimitry Andric   LI = &LoopI;
12610b57cec5SDimitry Andric 
1262*e8d8bef9SDimitry Andric   SccI = std::make_unique<SccInfo>(F);
12630b57cec5SDimitry Andric 
1264*e8d8bef9SDimitry Andric   assert(EstimatedBlockWeight.empty());
1265*e8d8bef9SDimitry Andric   assert(EstimatedLoopWeight.empty());
12660b57cec5SDimitry Andric 
1267*e8d8bef9SDimitry Andric   std::unique_ptr<DominatorTree> DTPtr;
12685ffd83dbSDimitry Andric   std::unique_ptr<PostDominatorTree> PDTPtr;
12695ffd83dbSDimitry Andric 
1270*e8d8bef9SDimitry Andric   if (!DT) {
1271*e8d8bef9SDimitry Andric     DTPtr = std::make_unique<DominatorTree>(const_cast<Function &>(F));
1272*e8d8bef9SDimitry Andric     DT = DTPtr.get();
1273*e8d8bef9SDimitry Andric   }
1274*e8d8bef9SDimitry Andric 
12755ffd83dbSDimitry Andric   if (!PDT) {
12765ffd83dbSDimitry Andric     PDTPtr = std::make_unique<PostDominatorTree>(const_cast<Function &>(F));
12775ffd83dbSDimitry Andric     PDT = PDTPtr.get();
12785ffd83dbSDimitry Andric   }
12795ffd83dbSDimitry Andric 
1280*e8d8bef9SDimitry Andric   computeEestimateBlockWeight(F, DT, PDT);
1281480093f4SDimitry Andric 
12820b57cec5SDimitry Andric   // Walk the basic blocks in post-order so that we can build up state about
12830b57cec5SDimitry Andric   // the successors of a block iteratively.
12840b57cec5SDimitry Andric   for (auto BB : post_order(&F.getEntryBlock())) {
12850b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Computing probabilities for " << BB->getName()
12860b57cec5SDimitry Andric                       << "\n");
12870b57cec5SDimitry Andric     // If there is no at least two successors, no sense to set probability.
12880b57cec5SDimitry Andric     if (BB->getTerminator()->getNumSuccessors() < 2)
12890b57cec5SDimitry Andric       continue;
12900b57cec5SDimitry Andric     if (calcMetadataWeights(BB))
12910b57cec5SDimitry Andric       continue;
1292*e8d8bef9SDimitry Andric     if (calcEstimatedHeuristics(BB))
12930b57cec5SDimitry Andric       continue;
12940b57cec5SDimitry Andric     if (calcPointerHeuristics(BB))
12950b57cec5SDimitry Andric       continue;
12960b57cec5SDimitry Andric     if (calcZeroHeuristics(BB, TLI))
12970b57cec5SDimitry Andric       continue;
12980b57cec5SDimitry Andric     if (calcFloatingPointHeuristics(BB))
12990b57cec5SDimitry Andric       continue;
13000b57cec5SDimitry Andric   }
13010b57cec5SDimitry Andric 
1302*e8d8bef9SDimitry Andric   EstimatedLoopWeight.clear();
1303*e8d8bef9SDimitry Andric   EstimatedBlockWeight.clear();
1304*e8d8bef9SDimitry Andric   SccI.reset();
13050b57cec5SDimitry Andric 
13060b57cec5SDimitry Andric   if (PrintBranchProb &&
13070b57cec5SDimitry Andric       (PrintBranchProbFuncName.empty() ||
13080b57cec5SDimitry Andric        F.getName().equals(PrintBranchProbFuncName))) {
13090b57cec5SDimitry Andric     print(dbgs());
13100b57cec5SDimitry Andric   }
13110b57cec5SDimitry Andric }
13120b57cec5SDimitry Andric 
13130b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::getAnalysisUsage(
13140b57cec5SDimitry Andric     AnalysisUsage &AU) const {
13150b57cec5SDimitry Andric   // We require DT so it's available when LI is available. The LI updating code
13160b57cec5SDimitry Andric   // asserts that DT is also present so if we don't make sure that we have DT
13170b57cec5SDimitry Andric   // here, that assert will trigger.
13180b57cec5SDimitry Andric   AU.addRequired<DominatorTreeWrapperPass>();
13190b57cec5SDimitry Andric   AU.addRequired<LoopInfoWrapperPass>();
13200b57cec5SDimitry Andric   AU.addRequired<TargetLibraryInfoWrapperPass>();
1321*e8d8bef9SDimitry Andric   AU.addRequired<DominatorTreeWrapperPass>();
13225ffd83dbSDimitry Andric   AU.addRequired<PostDominatorTreeWrapperPass>();
13230b57cec5SDimitry Andric   AU.setPreservesAll();
13240b57cec5SDimitry Andric }
13250b57cec5SDimitry Andric 
13260b57cec5SDimitry Andric bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) {
13270b57cec5SDimitry Andric   const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
13288bcb0991SDimitry Andric   const TargetLibraryInfo &TLI =
13298bcb0991SDimitry Andric       getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1330*e8d8bef9SDimitry Andric   DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
13315ffd83dbSDimitry Andric   PostDominatorTree &PDT =
13325ffd83dbSDimitry Andric       getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
1333*e8d8bef9SDimitry Andric   BPI.calculate(F, LI, &TLI, &DT, &PDT);
13340b57cec5SDimitry Andric   return false;
13350b57cec5SDimitry Andric }
13360b57cec5SDimitry Andric 
13370b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); }
13380b57cec5SDimitry Andric 
13390b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS,
13400b57cec5SDimitry Andric                                              const Module *) const {
13410b57cec5SDimitry Andric   BPI.print(OS);
13420b57cec5SDimitry Andric }
13430b57cec5SDimitry Andric 
13440b57cec5SDimitry Andric AnalysisKey BranchProbabilityAnalysis::Key;
13450b57cec5SDimitry Andric BranchProbabilityInfo
13460b57cec5SDimitry Andric BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
13470b57cec5SDimitry Andric   BranchProbabilityInfo BPI;
13485ffd83dbSDimitry Andric   BPI.calculate(F, AM.getResult<LoopAnalysis>(F),
13495ffd83dbSDimitry Andric                 &AM.getResult<TargetLibraryAnalysis>(F),
1350*e8d8bef9SDimitry Andric                 &AM.getResult<DominatorTreeAnalysis>(F),
13515ffd83dbSDimitry Andric                 &AM.getResult<PostDominatorTreeAnalysis>(F));
13520b57cec5SDimitry Andric   return BPI;
13530b57cec5SDimitry Andric }
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric PreservedAnalyses
13560b57cec5SDimitry Andric BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
13570b57cec5SDimitry Andric   OS << "Printing analysis results of BPI for function "
13580b57cec5SDimitry Andric      << "'" << F.getName() << "':"
13590b57cec5SDimitry Andric      << "\n";
13600b57cec5SDimitry Andric   AM.getResult<BranchProbabilityAnalysis>(F).print(OS);
13610b57cec5SDimitry Andric   return PreservedAnalyses::all();
13620b57cec5SDimitry Andric }
1363