51#define DEBUG_TYPE "branch-prob"
55 cl::desc(
"Print the branch probability info."));
59 cl::desc(
"The option to specify the name of the function "
60 "whose branch probability info is printed."));
63 "Branch Probability Analysis",
false,
true)
214class BPIConstruction {
216 BPIConstruction(BranchProbabilityInfo &BPI) : BPI(BPI) {}
217 void calculate(
const Function &
F,
const LoopInfo &LI,
218 const TargetLibraryInfo *TLI, DominatorTree *DT,
219 PostDominatorTree *PDT);
234 using SccMap = DenseMap<const BasicBlock *, int>;
240 using SccBlockTypeMap = DenseMap<const BasicBlock *, uint32_t>;
243 using SccBlockTypeMaps = std::vector<SccBlockTypeMap>;
246 SccBlockTypeMaps SccBlocks;
257 bool isSCCHeader(
const BasicBlock *BB,
int SccNum)
const {
258 return getSccBlockType(BB, SccNum) & Header;
262 bool isSCCExitingBlock(
const BasicBlock *BB,
int SccNum)
const {
263 return getSccBlockType(BB, SccNum) & Exiting;
269 getSccEnterBlocks(
int SccNum, SmallVectorImpl<BasicBlock *> &Enters)
const;
273 LLVM_ABI void getSccExitBlocks(
int SccNum,
274 SmallVectorImpl<BasicBlock *> &Exits)
const;
282 void calculateSccBlockType(
const BasicBlock *BB,
int SccNum);
287 using LoopData = std::pair<Loop *, int>;
292 const SccInfo &SccI);
294 const BasicBlock *getBlock()
const {
return BB; }
296 LoopData getLoopData()
const {
return LD; }
297 Loop *getLoop()
const {
return LD.first; }
298 int getSccNum()
const {
return LD.second; }
300 bool belongsToLoop()
const {
return getLoop() || getSccNum() != -1; }
301 bool belongsToSameLoop(
const LoopBlock &LB)
const {
302 return (LB.getLoop() && getLoop() == LB.getLoop()) ||
303 (LB.getSccNum() != -1 && getSccNum() == LB.getSccNum());
308 LoopData LD = {
nullptr, -1};
312 using LoopEdge = std::pair<const LoopBlock &, const LoopBlock &>;
315 LoopBlock getLoopBlock(
const BasicBlock *BB)
const {
316 return LoopBlock(BB, *LI, *SccI);
322 bool isLoopEnteringEdge(
const LoopEdge &
Edge)
const;
326 bool isLoopExitingEdge(
const LoopEdge &
Edge)
const;
329 bool isLoopEnteringExitingEdge(
const LoopEdge &
Edge)
const;
332 bool isLoopBackEdge(
const LoopEdge &
Edge)
const;
334 void getLoopEnterBlocks(
const LoopBlock &LB,
335 SmallVectorImpl<BasicBlock *> &Enters)
const;
337 void getLoopExitBlocks(
const LoopBlock &LB,
338 SmallVectorImpl<BasicBlock *> &Exits)
const;
342 std::optional<uint32_t> getEstimatedBlockWeight(
const BasicBlock *BB)
const;
347 std::optional<uint32_t> getEstimatedLoopWeight(
const LoopData &
L)
const;
351 std::optional<uint32_t> getEstimatedEdgeWeight(
const LoopEdge &
Edge)
const;
356 template <
class IterT>
357 std::optional<uint32_t>
358 getMaxEstimatedEdgeWeight(
const LoopBlock &SrcBB,
365 bool updateEstimatedBlockWeight(LoopBlock &LoopBB, uint32_t BBWeight,
366 SmallVectorImpl<BasicBlock *> &BlockWorkList,
367 SmallVectorImpl<LoopBlock> &LoopWorkList);
371 void propagateEstimatedBlockWeight(
const LoopBlock &LoopBB, DominatorTree *DT,
372 PostDominatorTree *PDT, uint32_t BBWeight,
373 SmallVectorImpl<BasicBlock *> &WorkList,
374 SmallVectorImpl<LoopBlock> &LoopWorkList);
377 std::optional<uint32_t> getInitialEstimatedBlockWeight(
const BasicBlock *BB);
380 void estimateBlockWeights(
const Function &
F, DominatorTree *DT,
381 PostDominatorTree *PDT);
385 bool calcEstimatedHeuristics(
const BasicBlock *BB);
386 bool calcMetadataWeights(
const BasicBlock *BB);
387 bool calcPointerHeuristics(
const BasicBlock *BB);
388 bool calcZeroHeuristics(
const BasicBlock *BB,
const TargetLibraryInfo *TLI);
389 bool calcFloatingPointHeuristics(
const BasicBlock *BB);
391 BranchProbabilityInfo &BPI;
393 const LoopInfo *LI =
nullptr;
396 std::unique_ptr<const SccInfo> SccI;
399 SmallDenseMap<const BasicBlock *, uint32_t> EstimatedBlockWeight;
402 SmallDenseMap<LoopData, uint32_t> EstimatedLoopWeight;
405BPIConstruction::SccInfo::SccInfo(
const Function &
F) {
410 for (scc_iterator<const Function *> It =
scc_begin(&
F); !It.isAtEnd();
414 const std::vector<const BasicBlock *> &Scc = *It;
419 for (
const auto *BB : Scc) {
421 SccNums[BB] = SccNum;
422 calculateSccBlockType(BB, SccNum);
428int BPIConstruction::SccInfo::getSCCNum(
const BasicBlock *BB)
const {
429 auto SccIt = SccNums.find(BB);
430 if (SccIt == SccNums.end())
432 return SccIt->second;
435void BPIConstruction::SccInfo::getSccEnterBlocks(
436 int SccNum, SmallVectorImpl<BasicBlock *> &Enters)
const {
438 for (
auto MapIt : SccBlocks[SccNum]) {
439 const auto *BB = MapIt.first;
440 if (isSCCHeader(BB, SccNum))
442 if (getSCCNum(Pred) != SccNum)
447void BPIConstruction::SccInfo::getSccExitBlocks(
448 int SccNum, SmallVectorImpl<BasicBlock *> &Exits)
const {
449 for (
auto MapIt : SccBlocks[SccNum]) {
450 const auto *BB = MapIt.first;
451 if (isSCCExitingBlock(BB, SccNum))
453 if (getSCCNum(Succ) != SccNum)
458uint32_t BPIConstruction::SccInfo::getSccBlockType(
const BasicBlock *BB,
460 assert(getSCCNum(BB) == SccNum);
462 assert(SccBlocks.size() >
static_cast<unsigned>(SccNum) &&
"Unknown SCC");
463 const auto &SccBlockTypes = SccBlocks[SccNum];
465 auto It = SccBlockTypes.find(BB);
466 if (It != SccBlockTypes.end()) {
472void BPIConstruction::SccInfo::calculateSccBlockType(
const BasicBlock *BB,
474 assert(getSCCNum(BB) == SccNum);
480 return getSCCNum(Pred) != SccNum;
485 return getSCCNum(Succ) != SccNum;
487 BlockType |= Exiting;
491 if (SccBlocks.size() <=
static_cast<unsigned>(SccNum))
492 SccBlocks.resize(SccNum + 1);
493 auto &SccBlockTypes = SccBlocks[SccNum];
495 if (BlockType != Inner) {
497 std::tie(std::ignore, IsInserted) =
498 SccBlockTypes.insert(std::make_pair(BB, BlockType));
499 assert(IsInserted &&
"Duplicated block in SCC");
503BPIConstruction::LoopBlock::LoopBlock(
const BasicBlock *BB,
const LoopInfo &LI,
508 LD.second = SccI.getSCCNum(BB);
512bool BPIConstruction::isLoopEnteringEdge(
const LoopEdge &Edge)
const {
513 const auto &SrcBlock =
Edge.first;
514 const auto &DstBlock =
Edge.second;
515 return (DstBlock.getLoop() &&
516 !DstBlock.getLoop()->contains(SrcBlock.getLoop())) ||
518 (DstBlock.getSccNum() != -1 &&
519 SrcBlock.getSccNum() != DstBlock.getSccNum());
522bool BPIConstruction::isLoopExitingEdge(
const LoopEdge &
Edge)
const {
523 return isLoopEnteringEdge({
Edge.second,
Edge.first});
526bool BPIConstruction::isLoopEnteringExitingEdge(
const LoopEdge &
Edge)
const {
527 return isLoopEnteringEdge(
Edge) || isLoopExitingEdge(
Edge);
530bool BPIConstruction::isLoopBackEdge(
const LoopEdge &
Edge)
const {
531 const auto &SrcBlock =
Edge.first;
532 const auto &DstBlock =
Edge.second;
533 return SrcBlock.belongsToSameLoop(DstBlock) &&
534 ((DstBlock.getLoop() &&
535 DstBlock.getLoop()->getHeader() == DstBlock.getBlock()) ||
536 (DstBlock.getSccNum() != -1 &&
537 SccI->isSCCHeader(DstBlock.getBlock(), DstBlock.getSccNum())));
540void BPIConstruction::getLoopEnterBlocks(
541 const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Enters)
const {
543 auto *Header = LB.getLoop()->getHeader();
546 assert(LB.getSccNum() != -1 &&
"LB doesn't belong to any loop?");
547 SccI->getSccEnterBlocks(LB.getSccNum(), Enters);
551void BPIConstruction::getLoopExitBlocks(
552 const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Exits)
const {
554 LB.getLoop()->getExitBlocks(Exits);
556 assert(LB.getSccNum() != -1 &&
"LB doesn't belong to any loop?");
557 SccI->getSccExitBlocks(LB.getSccNum(), Exits);
565bool BPIConstruction::calcMetadataWeights(
const BasicBlock *BB) {
582 uint64_t WeightSum = 0;
584 SmallVector<unsigned, 2> UnreachableIdxs;
585 SmallVector<unsigned, 2> ReachableIdxs;
589 for (
unsigned I = 0,
E = Weights.
size();
I !=
E; ++
I) {
590 WeightSum += Weights[
I];
591 const LoopBlock SrcLoopBB = getLoopBlock(BB);
592 const LoopBlock DstLoopBB = getLoopBlock(*Succs++);
593 auto EstimatedWeight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
594 if (EstimatedWeight &&
604 uint64_t ScalingFactor =
605 (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
607 if (ScalingFactor > 1) {
610 Weights[
I] /= ScalingFactor;
611 WeightSum += Weights[
I];
614 assert(WeightSum <= UINT32_MAX &&
615 "Expected weights to scale down to 32 bits");
617 if (WeightSum == 0 || ReachableIdxs.
size() == 0) {
626 BP.
push_back({ Weights[
I],
static_cast<uint32_t
>(WeightSum) });
630 if (UnreachableIdxs.
size() == 0 || ReachableIdxs.
size() == 0) {
636 for (
auto I : UnreachableIdxs)
637 if (UnreachableProb < BP[
I]) {
638 BP[
I] = UnreachableProb;
662 for (
auto I : UnreachableIdxs)
663 NewUnreachableSum += BP[
I];
665 BranchProbability NewReachableSum =
669 for (
auto I : ReachableIdxs)
670 OldReachableSum += BP[
I];
672 if (OldReachableSum != NewReachableSum) {
673 if (OldReachableSum.
isZero()) {
677 BranchProbability PerEdge = NewReachableSum / ReachableIdxs.size();
678 for (
auto I : ReachableIdxs)
681 for (
auto I : ReachableIdxs) {
687 BP[
I].getNumerator();
688 uint32_t Div =
static_cast<uint32_t
>(
702bool BPIConstruction::calcPointerHeuristics(
const BasicBlock *BB) {
730computeUnlikelySuccessors(
const BasicBlock *BB, Loop *L,
731 SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) {
774 if (!
L->contains(CmpLHS))
781 if (!CmpPHI || !
L->contains(CmpPHI))
785 SmallPtrSet<PHINode*, 8> VisitedInsts;
788 VisitedInsts.
insert(CmpPHI);
789 while (!WorkList.
empty()) {
791 for (BasicBlock *
B :
P->blocks()) {
795 Value *
V =
P->getIncomingValueForBlock(
B);
799 if (VisitedInsts.
insert(PN).second)
831std::optional<uint32_t>
832BPIConstruction::getEstimatedBlockWeight(
const BasicBlock *BB)
const {
833 auto WeightIt = EstimatedBlockWeight.find(BB);
834 if (WeightIt == EstimatedBlockWeight.end())
836 return WeightIt->second;
839std::optional<uint32_t>
840BPIConstruction::getEstimatedLoopWeight(
const LoopData &L)
const {
841 auto WeightIt = EstimatedLoopWeight.find(L);
842 if (WeightIt == EstimatedLoopWeight.end())
844 return WeightIt->second;
847std::optional<uint32_t>
848BPIConstruction::getEstimatedEdgeWeight(
const LoopEdge &
Edge)
const {
851 return isLoopEnteringEdge(
Edge)
852 ? getEstimatedLoopWeight(
Edge.second.getLoopData())
853 : getEstimatedBlockWeight(
Edge.second.getBlock());
856template <
class IterT>
857std::optional<uint32_t> BPIConstruction::getMaxEstimatedEdgeWeight(
859 std::optional<uint32_t> MaxWeight;
860 for (
const BasicBlock *DstBB : Successors) {
861 const LoopBlock DstLoopBB = getLoopBlock(DstBB);
862 auto Weight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
867 if (!MaxWeight || *MaxWeight < *Weight)
879bool BPIConstruction::updateEstimatedBlockWeight(
880 LoopBlock &LoopBB, uint32_t BBWeight,
881 SmallVectorImpl<BasicBlock *> &BlockWorkList,
882 SmallVectorImpl<LoopBlock> &LoopWorkList) {
890 if (!EstimatedBlockWeight.insert({BB, BBWeight}).second)
894 LoopBlock PredLoop = getLoopBlock(PredBlock);
896 if (isLoopExitingEdge({PredLoop, LoopBB})) {
897 if (!EstimatedLoopWeight.count(PredLoop.getLoopData()))
899 }
else if (!EstimatedBlockWeight.count(PredBlock))
917void BPIConstruction::propagateEstimatedBlockWeight(
918 const LoopBlock &LoopBB, DominatorTree *DT, PostDominatorTree *PDT,
919 uint32_t BBWeight, SmallVectorImpl<BasicBlock *> &BlockWorkList,
920 SmallVectorImpl<LoopBlock> &LoopWorkList) {
922 const auto *DTStartNode = DT->
getNode(BB);
923 const auto *PDTStartNode = PDT->
getNode(BB);
926 for (
const auto *DTNode = DTStartNode; DTNode !=
nullptr;
927 DTNode = DTNode->getIDom()) {
928 auto *DomBB = DTNode->getBlock();
935 LoopBlock DomLoopBB = getLoopBlock(DomBB);
936 const LoopEdge
Edge{DomLoopBB, LoopBB};
938 if (!isLoopEnteringExitingEdge(
Edge)) {
939 if (!updateEstimatedBlockWeight(DomLoopBB, BBWeight, BlockWorkList,
944 }
else if (isLoopExitingEdge(
Edge)) {
950std::optional<uint32_t>
951BPIConstruction::getInitialEstimatedBlockWeight(
const BasicBlock *BB) {
953 auto hasNoReturn = [&](
const BasicBlock *BB) {
956 if (CI->hasFnAttr(Attribute::NoReturn))
971 return hasNoReturn(BB)
980 for (
const auto &
I : *BB)
982 if (CI->hasFnAttr(Attribute::Cold))
991void BPIConstruction::estimateBlockWeights(
const Function &
F, DominatorTree *DT,
992 PostDominatorTree *PDT) {
993 SmallVector<BasicBlock *, 8> BlockWorkList;
995 SmallDenseMap<LoopData, SmallVector<BasicBlock *, 4>> LoopExitBlocks;
999 ReversePostOrderTraversal<const Function *> RPOT(&
F);
1000 for (
const auto *BB : RPOT)
1001 if (
auto BBWeight = getInitialEstimatedBlockWeight(BB))
1004 propagateEstimatedBlockWeight(getLoopBlock(BB), DT, PDT, *BBWeight,
1005 BlockWorkList, LoopWorkList);
1012 while (!LoopWorkList.
empty()) {
1014 const LoopData
LD = LoopBB.getLoopData();
1015 if (EstimatedLoopWeight.count(LD))
1019 SmallVectorImpl<BasicBlock *> &Exits = Res.first->second;
1021 getLoopExitBlocks(LoopBB, Exits);
1022 auto LoopWeight = getMaxEstimatedEdgeWeight(
1030 EstimatedLoopWeight.insert({
LD, *LoopWeight});
1032 getLoopEnterBlocks(LoopBB, BlockWorkList);
1036 while (!BlockWorkList.
empty()) {
1039 if (EstimatedBlockWeight.count(BB))
1048 const LoopBlock LoopBB = getLoopBlock(BB);
1049 auto MaxWeight = getMaxEstimatedEdgeWeight(LoopBB,
successors(BB));
1052 propagateEstimatedBlockWeight(LoopBB, DT, PDT, *MaxWeight,
1053 BlockWorkList, LoopWorkList);
1055 }
while (!BlockWorkList.
empty() || !LoopWorkList.
empty());
1061bool BPIConstruction::calcEstimatedHeuristics(
const BasicBlock *BB) {
1063 "expected more than one successor!");
1065 const LoopBlock LoopBB = getLoopBlock(BB);
1067 SmallPtrSet<const BasicBlock *, 8> UnlikelyBlocks;
1069 if (LoopBB.getLoop())
1070 computeUnlikelySuccessors(BB, LoopBB.getLoop(), UnlikelyBlocks);
1073 bool FoundEstimatedWeight =
false;
1074 SmallVector<uint32_t, 4> SuccWeights;
1075 uint64_t TotalWeight = 0;
1077 for (
const BasicBlock *SuccBB :
successors(BB)) {
1078 std::optional<uint32_t> Weight;
1079 const LoopBlock SuccLoopBB = getLoopBlock(SuccBB);
1080 const LoopEdge
Edge{LoopBB, SuccLoopBB};
1082 Weight = getEstimatedEdgeWeight(
Edge);
1084 if (isLoopExitingEdge(
Edge) &&
1093 bool IsUnlikelyEdge = LoopBB.getLoop() && UnlikelyBlocks.
contains(SuccBB);
1094 if (IsUnlikelyEdge &&
1104 FoundEstimatedWeight =
true;
1108 TotalWeight += WeightVal;
1115 if (!FoundEstimatedWeight || TotalWeight == 0)
1119 const unsigned SuccCount = SuccWeights.
size();
1123 if (TotalWeight > UINT32_MAX) {
1124 uint64_t ScalingFactor = TotalWeight / UINT32_MAX + 1;
1126 for (
unsigned Idx = 0; Idx < SuccCount; ++Idx) {
1127 SuccWeights[Idx] /= ScalingFactor;
1131 TotalWeight += SuccWeights[Idx];
1133 assert(TotalWeight <= UINT32_MAX &&
"Total weight overflows");
1140 for (
unsigned Idx = 0; Idx < SuccCount; ++Idx) {
1141 EdgeProbabilities[Idx] =
1142 BranchProbability(SuccWeights[Idx], (uint32_t)TotalWeight);
1148bool BPIConstruction::calcZeroHeuristics(
const BasicBlock *BB,
1149 const TargetLibraryInfo *TLI) {
1159 auto GetConstantInt = [](
Value *
V) {
1166 ConstantInt *CV = GetConstantInt(
RHS);
1173 if (
LHS->getOpcode() == Instruction::And)
1174 if (ConstantInt *AndRHS = GetConstantInt(
LHS->getOperand(1)))
1175 if (AndRHS->getValue().isPowerOf2())
1179 LibFunc
Func = LibFunc::NotLibFunc;
1185 ProbabilityTable::const_iterator Search;
1186 if (Func == LibFunc_strcasecmp ||
1187 Func == LibFunc_strcmp ||
1188 Func == LibFunc_strncasecmp ||
1189 Func == LibFunc_strncmp ||
1190 Func == LibFunc_memcmp ||
1191 Func == LibFunc_bcmp) {
1195 }
else if (CV->
isZero()) {
1199 }
else if (CV->
isOne()) {
1215bool BPIConstruction::calcFloatingPointHeuristics(
const BasicBlock *BB) {
1236 ProbList = Search->second;
1242void BPIConstruction::calculate(
const Function &
F,
const LoopInfo &LoopI,
1243 const TargetLibraryInfo *TLI, DominatorTree *DT,
1244 PostDominatorTree *PDT) {
1247 SccI = std::make_unique<SccInfo>(
F);
1249 std::unique_ptr<DominatorTree> DTPtr;
1250 std::unique_ptr<PostDominatorTree> PDTPtr;
1253 DTPtr = std::make_unique<DominatorTree>(
const_cast<Function &
>(
F));
1258 PDTPtr = std::make_unique<PostDominatorTree>(
const_cast<Function &
>(
F));
1262 estimateBlockWeights(
F, DT, PDT);
1266 for (
const auto *BB :
post_order(&
F.getEntryBlock())) {
1272 if (calcMetadataWeights(BB))
1274 if (calcEstimatedHeuristics(BB))
1276 if (calcPointerHeuristics(BB))
1278 if (calcZeroHeuristics(BB, TLI))
1280 if (calcFloatingPointHeuristics(BB))
1293 FunctionAnalysisManager::Invalidator &) {
1302 OS <<
"---- Branch Probabilities ----\n";
1305 assert(LastF &&
"Cannot print prior to running over a function");
1306 for (
const auto &BI : *LastF) {
1325 unsigned IndexInSuccessors)
const {
1326 auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
1327 assert((Probs.end() == Probs.find(std::make_pair(Src, 0))) ==
1328 (Probs.end() ==
I) &&
1329 "Probability for I-th successor must always be defined along with the "
1330 "probability for the first successor");
1332 if (
I != Probs.end())
1349 if (!Probs.count(std::make_pair(Src, 0)))
1354 if (It.value() == Dst)
1355 Prob += Probs.find(std::make_pair(Src, It.index()))->second;
1363 assert(Src->getTerminator()->getNumSuccessors() == Probs.size());
1365 if (Probs.size() == 0)
1368 Handles.insert(BasicBlockCallbackVH(Src,
this));
1370 for (
unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) {
1371 this->Probs[std::make_pair(Src, SuccIdx)] = Probs[SuccIdx];
1372 LLVM_DEBUG(
dbgs() <<
"set edge " << Src->getName() <<
" -> " << SuccIdx
1373 <<
" successor probability to " << Probs[SuccIdx]
1375 TotalNumerator += Probs[SuccIdx].getNumerator();
1385 (void)TotalNumerator;
1391 unsigned NumSuccessors = Src->getTerminator()->getNumSuccessors();
1392 assert(NumSuccessors == Dst->getTerminator()->getNumSuccessors());
1393 if (NumSuccessors == 0)
1395 if (!this->Probs.contains(std::make_pair(Src, 0)))
1398 Handles.insert(BasicBlockCallbackVH(Dst,
this));
1399 for (
unsigned SuccIdx = 0; SuccIdx < NumSuccessors; ++SuccIdx) {
1400 auto Prob = this->Probs[std::make_pair(Src, SuccIdx)];
1401 this->Probs[std::make_pair(Dst, SuccIdx)] = Prob;
1402 LLVM_DEBUG(
dbgs() <<
"set edge " << Dst->getName() <<
" -> " << SuccIdx
1403 <<
" successor probability to " << Prob <<
"\n");
1408 assert(Src->getTerminator()->getNumSuccessors() == 2);
1409 auto It0 = Probs.find(std::make_pair(Src, 0));
1410 if (It0 == Probs.end())
1412 auto It1 = Probs.find(std::make_pair(Src, 1));
1413 assert(It1 != Probs.end());
1423 Src->printAsOperand(OS,
false, Src->getModule());
1425 Dst->printAsOperand(OS,
false, Dst->getModule());
1426 OS <<
" probability is " << Prob
1427 << (
isEdgeHot(Src, Dst) ?
" [HOT edge]\n" :
"\n");
1442 Handles.erase(BasicBlockCallbackVH(BB,
this));
1443 for (
unsigned I = 0;; ++
I) {
1444 auto MapI = Probs.find(std::make_pair(BB,
I));
1445 if (MapI == Probs.end()) {
1446 assert(Probs.count(std::make_pair(BB,
I + 1)) == 0 &&
1447 "Must be no more successors");
1461 BPIConstruction(*this).calculate(
F, LoopI, TLI, DT, PDT);
1489 BPI.calculate(
F, LI, &TLI, &DT, &PDT);
1514 OS <<
"Printing analysis 'Branch Probability Analysis' for function '"
1515 <<
F.getName() <<
"':\n";
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockExecWeight
Set of dedicated "absolute" execution weights for a block.
@ NORETURN
Weight to a block containing non returning call.
@ UNWIND
Weight to 'unwind' block of an invoke instruction.
@ COLD
Weight to a 'cold' block.
@ ZERO
Special weight used for cases with exact zero probability.
@ UNREACHABLE
Weight to an 'unreachable' block.
@ DEFAULT
Default weight is used in cases when there is no dedicated execution weight set.
@ LOWEST_NON_ZERO
Minimal possible non zero weight.
static const uint32_t FPH_TAKEN_WEIGHT
static const BranchProbability FPUntakenProb(FPH_NONTAKEN_WEIGHT, FPH_TAKEN_WEIGHT+FPH_NONTAKEN_WEIGHT)
static const BranchProbability ZeroUntakenProb(ZH_NONTAKEN_WEIGHT, ZH_TAKEN_WEIGHT+ZH_NONTAKEN_WEIGHT)
static const uint32_t LBH_TAKEN_WEIGHT
static const uint32_t ZH_NONTAKEN_WEIGHT
static const ProbabilityTable ICmpWithLibCallTable
strcmp and similar functions return zero, negative, or positive, if the first string is equal,...
static const ProbabilityTable ICmpWithMinusOneTable
Integer compares with -1:
static const BranchProbability FPOrdTakenProb(FPH_ORD_WEIGHT, FPH_ORD_WEIGHT+FPH_UNO_WEIGHT)
static const ProbabilityTable ICmpWithZeroTable
Integer compares with 0:
static const uint32_t PH_NONTAKEN_WEIGHT
std::map< CmpInst::Predicate, ProbabilityList > ProbabilityTable
static const BranchProbability PtrTakenProb(PH_TAKEN_WEIGHT, PH_TAKEN_WEIGHT+PH_NONTAKEN_WEIGHT)
static const BranchProbability ZeroTakenProb(ZH_TAKEN_WEIGHT, ZH_TAKEN_WEIGHT+ZH_NONTAKEN_WEIGHT)
static const BranchProbability FPOrdUntakenProb(FPH_UNO_WEIGHT, FPH_ORD_WEIGHT+FPH_UNO_WEIGHT)
static const uint32_t PH_TAKEN_WEIGHT
Heuristics and lookup tables for non-loop branches: Pointer Heuristics (PH)
static const BranchProbability FPTakenProb(FPH_TAKEN_WEIGHT, FPH_TAKEN_WEIGHT+FPH_NONTAKEN_WEIGHT)
static const BranchProbability PtrUntakenProb(PH_NONTAKEN_WEIGHT, PH_TAKEN_WEIGHT+PH_NONTAKEN_WEIGHT)
SmallVector< BranchProbability > ProbabilityList
static const ProbabilityTable ICmpWithOneTable
Integer compares with 1:
static const uint32_t ZH_TAKEN_WEIGHT
Zero Heuristics (ZH)
static const uint32_t FPH_NONTAKEN_WEIGHT
static const BranchProbability UR_TAKEN_PROB
Unreachable-terminating branch taken probability.
static const ProbabilityTable FCmpTable
Floating-Point compares:
static const uint32_t LBH_NONTAKEN_WEIGHT
static const ProbabilityTable PointerTable
Pointer comparisons:
static const uint32_t FPH_ORD_WEIGHT
This is the probability for an ordered floating point comparison.
static const uint32_t FPH_UNO_WEIGHT
This is the probability for an unordered floating point comparison, it means one or two of the operan...
static cl::opt< std::string > PrintBranchProbFuncName("print-bpi-func-name", cl::Hidden, cl::desc("The option to specify the name of the function " "whose branch probability info is printed."))
static cl::opt< bool > PrintBranchProb("print-bpi", cl::init(false), cl::Hidden, cl::desc("Print the branch probability info."))
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
std::pair< BasicBlock *, BasicBlock * > Edge
This file defines the SmallVector class.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
LLVM_ABI const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
bool isEHPad() const
Return true if this basic block is an exception handling block.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Analysis pass which computes BranchProbabilityInfo.
LLVM_ABI BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BPI.
Legacy analysis pass which computes BranchProbabilityInfo.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
BranchProbabilityInfoWrapperPass()
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Analysis providing branch probability information.
LLVM_ABI void eraseBlock(const BasicBlock *BB)
Forget analysis results for the given basic block.
LLVM_ABI void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
LLVM_ABI bool invalidate(Function &, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI void calculate(const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI, DominatorTree *DT, PostDominatorTree *PDT)
LLVM_ABI void releaseMemory()
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
LLVM_ABI void swapSuccEdgesProbabilities(const BasicBlock *Src)
Swap outgoing edges probabilities for Src with branch terminator.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI raw_ostream & printEdgeProbability(raw_ostream &OS, const BasicBlock *Src, const BasicBlock *Dst) const
Print an edge's probability.
LLVM_ABI void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst)
Copy outgoing edge probabilities from Src to Dst.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static uint32_t getDenominator()
static BranchProbability getRaw(uint32_t N)
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
static BranchProbability getZero()
Represents analyses that only rely on functions' control flow.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
@ ICMP_SLT
signed less than
@ ICMP_SGT
signed greater than
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
bool isTrueWhenEqual() const
This is just a convenience.
Predicate getPredicate() const
Return the predicate for this instruction.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Analysis pass which computes a DominatorTree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
static bool isEquality(Predicate Pred)
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
Analysis pass that exposes the LoopInfo for a function.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
A Module instance is used to store all the information related to an LLVM module.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
LLVM_ABI bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
bool isPointerTy() const
True if this is an instance of PointerType.
Value * getOperand(unsigned i) const
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
This class implements an extremely fast bulk output stream that can only output to a stream.
@ BasicBlock
Various leaf nodes.
BlockType
Used as immediate MachineOperands for block signatures.
initializer< Ty > init(const Ty &Val)
NodeAddr< FuncNode * > Func
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
auto pred_end(const MachineBasicBlock *BB)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto successors(const MachineBasicBlock *BB)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
iterator_range< po_iterator< T > > post_order(const T &G)
constexpr T divideNearest(U Numerator, V Denominator)
Returns (Numerator / Denominator) rounded by round-half-up.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
LLVM_ABI MDNode * getValidBranchWeightMDNode(const Instruction &I)
Get the valid branch weights metadata node.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto succ_size(const MachineBasicBlock *BB)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
auto pred_begin(const MachineBasicBlock *BB)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Instruction::const_succ_iterator const_succ_iterator
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A special type used by analysis passes to provide an address that identifies that particular analysis...