28 #define DEBUG_TYPE "block-placement2"
49 STATISTIC(NumCondBranches,
"Number of conditional branches");
50 STATISTIC(NumUncondBranches,
"Number of uncondittional branches");
52 "Potential frequency of taking conditional branches");
54 "Potential frequency of taking unconditional branches");
57 cl::desc(
"Force the alignment of all "
58 "blocks in the function."),
92 BlockToChainMapType &BlockToChain;
101 : Blocks(1, BB), BlockToChain(BlockToChain), LoopPredecessors(0) {
102 assert(BB &&
"Cannot create a chain with a null basic block");
103 BlockToChain[BB] =
this;
113 iterator
end() {
return Blocks.
end(); }
123 assert(!Blocks.empty());
127 assert(!BlockToChain[BB]);
128 Blocks.push_back(BB);
129 BlockToChain[BB] =
this;
133 assert(BB == *Chain->begin());
134 assert(Chain->begin() != Chain->end());
140 Blocks.push_back(*BI);
141 assert(BlockToChain[*BI] == Chain &&
"Incoming blocks not in chain");
142 BlockToChain[*BI] =
this;
158 unsigned LoopPredecessors;
199 void markChainSuccessors(BlockChain &Chain,
202 const BlockFilterSet *BlockFilter = 0);
205 const BlockFilterSet *BlockFilter);
208 const BlockFilterSet *BlockFilter);
211 const BlockChain &PlacedChain,
213 const BlockFilterSet *BlockFilter);
216 const BlockFilterSet *BlockFilter = 0);
218 const BlockFilterSet &LoopBlockSet);
221 const BlockFilterSet &LoopBlockSet);
224 const BlockFilterSet &LoopBlockSet);
247 "Branch Probability Basic Block Placement",
false,
false)
262 <<
" (derived from LLVM BB '" << BB->
getName() <<
"')";
285 void MachineBlockPlacement::markChainSuccessors(
289 const BlockFilterSet *BlockFilter) {
299 SE = (*CBI)->succ_end();
301 if (BlockFilter && !BlockFilter->count(*SI))
303 BlockChain &SuccChain = *BlockToChain[*SI];
305 if (&Chain == &SuccChain || *SI == LoopHeaderBB)
310 if (SuccChain.LoopPredecessors > 0 && --SuccChain.LoopPredecessors == 0)
311 BlockWorkList.
push_back(*SuccChain.begin());
327 const BlockFilterSet *BlockFilter) {
337 uint32_t BestWeight = 0;
338 uint32_t WeightScale = 0;
339 uint32_t SumWeight = MBPI->getSumForBlock(BB, WeightScale);
344 if (BlockFilter && !BlockFilter->count(*SI))
346 BlockChain &SuccChain = *BlockToChain[*SI];
347 if (&SuccChain == &Chain) {
351 if (*SI != *SuccChain.begin()) {
356 uint32_t SuccWeight = MBPI->getEdgeWeight(BB, *SI);
361 if (SuccChain.LoopPredecessors != 0) {
362 if (SuccProb < HotProb) {
370 = MBFI->getBlockFreq(BB) * SuccProb * HotProb.getCompl();
371 bool BadCFGConflict =
false;
373 PE = (*SI)->pred_end();
375 if (*PI == *SI || (BlockFilter && !BlockFilter->count(*PI)) ||
376 BlockToChain[*PI] == &Chain)
379 = MBFI->getBlockFreq(*PI) * MBPI->getEdgeProbability(*PI, *SI);
380 if (PredEdgeFreq >= CandidateEdgeFreq) {
381 BadCFGConflict =
true;
385 if (BadCFGConflict) {
387 <<
" -> non-cold CFG conflict\n");
394 << (SuccChain.LoopPredecessors != 0 ?
" (CFG break)" :
"")
396 if (BestSucc && BestWeight >= SuccWeight)
399 BestWeight = SuccWeight;
406 class IsBlockPlaced {
407 const BlockChain &PlacedChain;
408 const BlockToChainMapType &BlockToChain;
411 IsBlockPlaced(
const BlockChain &PlacedChain,
412 const BlockToChainMapType &BlockToChain)
413 : PlacedChain(PlacedChain), BlockToChain(BlockToChain) {}
416 return BlockToChain.lookup(BB) == &PlacedChain;
433 const BlockFilterSet *BlockFilter) {
438 WorkList.
erase(std::remove_if(WorkList.
begin(), WorkList.
end(),
439 IsBlockPlaced(Chain, BlockToChain)),
445 WBE = WorkList.
end();
447 BlockChain &SuccChain = *BlockToChain[*WBI];
448 if (&SuccChain == &Chain) {
450 <<
" -> Already merged!\n");
453 assert(SuccChain.LoopPredecessors == 0 &&
"Found CFG-violating block");
458 if (BestBlock && BestFreq >= CandidateFreq)
461 BestFreq = CandidateFreq;
476 const BlockFilterSet *BlockFilter) {
479 if (BlockFilter && !BlockFilter->count(
I))
481 if (BlockToChain[
I] != &PlacedChain) {
482 PrevUnplacedBlockIt =
I;
486 return *BlockToChain[
I]->begin();
492 void MachineBlockPlacement::buildChain(
496 const BlockFilterSet *BlockFilter) {
498 assert(BlockToChain[BB] == &Chain);
503 markChainSuccessors(Chain, LoopHeaderBB, BlockWorkList, BlockFilter);
507 assert(BlockToChain[BB] == &Chain);
518 BestSucc = selectBestCandidateBlock(Chain, BlockWorkList, BlockFilter);
521 BestSucc = getFirstUnplacedBlock(F, Chain, PrevUnplacedBlockIt,
526 DEBUG(
dbgs() <<
"Unnatural loop CFG detected, forcibly merging the "
527 "layout successor until the CFG reduces\n");
531 BlockChain &SuccChain = *BlockToChain[BestSucc];
534 SuccChain.LoopPredecessors = 0;
537 markChainSuccessors(SuccChain, LoopHeaderBB, BlockWorkList, BlockFilter);
538 Chain.merge(BestSucc, &SuccChain);
542 DEBUG(
dbgs() <<
"Finished forming chain for header block "
557 MachineBlockPlacement::findBestLoopTop(
MachineLoop &L,
558 const BlockFilterSet &LoopBlockSet) {
562 BlockChain &HeaderChain = *BlockToChain[L.
getHeader()];
563 if (!LoopBlockSet.count(*HeaderChain.begin()))
566 DEBUG(
dbgs() <<
"Finding best loop top for: "
575 if (!LoopBlockSet.count(Pred))
579 << MBFI->getBlockFreq(Pred) <<
" freq\n");
584 if (!BestPred || PredFreq > BestPredFreq ||
585 (!(PredFreq < BestPredFreq) &&
588 BestPredFreq = PredFreq;
598 (*BestPred->
pred_begin())->succ_size() == 1 &&
615 const BlockFilterSet &LoopBlockSet) {
624 BlockChain &HeaderChain = *BlockToChain[L.
getHeader()];
625 if (!LoopBlockSet.count(*HeaderChain.begin()))
629 unsigned BestExitLoopDepth = 0;
636 DEBUG(
dbgs() <<
"Finding best loop exit for: "
641 BlockChain &Chain = *BlockToChain[*
I];
653 bool HasLoopingSucc =
false;
657 uint32_t WeightScale = 0;
658 uint32_t SumWeight = MBPI->getSumForBlock(*
I, WeightScale);
660 SE = (*I)->succ_end();
662 if ((*SI)->isLandingPad())
666 BlockChain &SuccChain = *BlockToChain[*SI];
668 if (&Chain == &SuccChain) {
674 uint32_t SuccWeight = MBPI->getEdgeWeight(*
I, *SI);
675 if (LoopBlockSet.count(*SI)) {
678 HasLoopingSucc =
true;
682 unsigned SuccLoopDepth = 0;
683 if (
MachineLoop *ExitLoop = MLI->getLoopFor(*SI)) {
684 SuccLoopDepth = ExitLoop->getLoopDepth();
685 if (ExitLoop->contains(&L))
686 BlocksExitingToOuterLoop.
insert(*
I);
693 <<
"] (" << ExitEdgeFreq <<
")\n");
697 if (!ExitingBB || BestExitLoopDepth < SuccLoopDepth ||
698 ExitEdgeFreq > BestExitEdgeFreq ||
699 ((*I)->isLayoutSuccessor(*SI) &&
700 !(ExitEdgeFreq < BestExitEdgeFreq))) {
701 BestExitEdgeFreq = ExitEdgeFreq;
707 if (!HasLoopingSucc) {
708 ExitingBB = OldExitingBB;
709 BestExitEdgeFreq = OldBestExitEdgeFreq;
721 if (!BlocksExitingToOuterLoop.
empty() &&
722 !BlocksExitingToOuterLoop.
count(ExitingBB))
735 void MachineBlockPlacement::rotateLoop(BlockChain &LoopChain,
737 const BlockFilterSet &LoopBlockSet) {
742 bool ViableTopFallthrough =
false;
746 BlockChain *PredChain = BlockToChain[*PI];
747 if (!LoopBlockSet.count(*PI) &&
748 (!PredChain || *PI == *
llvm::prior(PredChain->end()))) {
749 ViableTopFallthrough =
true;
757 if (ViableTopFallthrough) {
762 BlockChain *SuccChain = BlockToChain[*SI];
763 if (!LoopBlockSet.count(*SI) &&
764 (!SuccChain || *SI == *SuccChain->begin()))
771 if (ExitIt == LoopChain.end())
788 buildLoopChains(F, **
LI);
804 ExitingBB = findBestLoopExit(F, L, LoopBlockSet);
806 BlockChain &LoopChain = *BlockToChain[LoopTop];
812 assert(LoopChain.LoopPredecessors == 0);
813 UpdatedPreds.
insert(&LoopChain);
817 BlockChain &Chain = *BlockToChain[*BI];
818 if (!UpdatedPreds.
insert(&Chain))
821 assert(Chain.LoopPredecessors == 0);
824 assert(BlockToChain[*BCI] == &Chain);
826 PE = (*BCI)->pred_end();
828 if (BlockToChain[*PI] == &Chain || !LoopBlockSet.count(*PI))
830 ++Chain.LoopPredecessors;
834 if (Chain.LoopPredecessors == 0)
838 buildChain(LoopTop, LoopChain, BlockWorkList, &LoopBlockSet);
839 rotateLoop(LoopChain, ExitingBB, LoopBlockSet);
843 bool BadLoop =
false;
844 if (LoopChain.LoopPredecessors) {
846 dbgs() <<
"Loop chain contains a block without its preds placed!\n"
848 <<
" Chain header: " <<
getBlockName(*LoopChain.begin()) <<
"\n";
853 if (!LoopBlockSet.erase(*BCI)) {
857 dbgs() <<
"Loop chain contains a block not contained by the loop!\n"
859 <<
" Chain header: " <<
getBlockName(*LoopChain.begin()) <<
"\n"
864 if (!LoopBlockSet.empty()) {
866 for (BlockFilterSet::iterator LBI = LoopBlockSet.begin(),
867 LBE = LoopBlockSet.end();
869 dbgs() <<
"Loop contains blocks never placed into a chain!\n"
871 <<
" Chain header: " <<
getBlockName(*LoopChain.begin()) <<
"\n"
874 assert(!BadLoop &&
"Detected problems with the placement of this loop.");
885 =
new (ChainAllocator.Allocate()) BlockChain(BlockToChain, BB);
898 assert(NextFI != FE &&
"Can't fallthrough past the last block.");
899 DEBUG(
dbgs() <<
"Pre-merging due to unanalyzable fallthrough: "
902 Chain->merge(NextBB, 0);
911 buildLoopChains(F, **
LI);
918 BlockChain &Chain = *BlockToChain[BB];
919 if (!UpdatedPreds.
insert(&Chain))
922 assert(Chain.LoopPredecessors == 0);
925 assert(BlockToChain[*BCI] == &Chain);
927 PE = (*BCI)->pred_end();
929 if (BlockToChain[*PI] == &Chain)
931 ++Chain.LoopPredecessors;
935 if (Chain.LoopPredecessors == 0)
939 BlockChain &FunctionChain = *BlockToChain[&F.
front()];
940 buildChain(&F.
front(), FunctionChain, BlockWorkList);
945 bool BadFunc =
false;
946 FunctionBlockSetType FunctionBlockSet;
948 FunctionBlockSet.insert(FI);
951 BCE = FunctionChain.end();
953 if (!FunctionBlockSet.erase(*BCI)) {
955 dbgs() <<
"Function chain contains a block not in the function!\n"
959 if (!FunctionBlockSet.empty()) {
961 for (FunctionBlockSetType::iterator FBI = FunctionBlockSet.begin(),
962 FBE = FunctionBlockSet.end();
964 dbgs() <<
"Function contains blocks never placed into a chain!\n"
967 assert(!BadFunc &&
"Detected problems with the block placement.");
973 BE = FunctionChain.end();
975 DEBUG(
dbgs() << (BI == FunctionChain.begin() ?
"Placing chain "
984 if (BI == FunctionChain.begin())
1004 bool needUpdateBr =
true;
1005 if (!Cond.
empty() && (!FBB || FBB == *BI)) {
1007 needUpdateBr =
false;
1018 if (TBB && !Cond.
empty() && FBB &&
1019 MBPI->getEdgeWeight(PrevBB, FBB) > MBPI->getEdgeWeight(PrevBB, TBB) &&
1021 DEBUG(
dbgs() <<
"Reverse order of the two branches: "
1023 DEBUG(
dbgs() <<
" Edge weight: " << MBPI->getEdgeWeight(PrevBB, FBB)
1024 <<
" vs " << MBPI->getEdgeWeight(PrevBB, TBB) <<
"\n");
1028 needUpdateBr =
true;
1047 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize))
1049 unsigned Align = TLI->getPrefLoopAlignment();
1052 if (FunctionChain.begin() == FunctionChain.end())
1059 BE = FunctionChain.end();
1072 if (Freq < WeightedEntryFreq)
1079 if (Freq < (LoopHeaderFreq * ColdProb))
1089 (*BI)->setAlignment(Align);
1098 BlockFrequency LayoutEdgeFreq = MBFI->getBlockFreq(LayoutPred) * LayoutProb;
1099 if (LayoutEdgeFreq <= (Freq * ColdProb))
1100 (*BI)->setAlignment(Align);
1104 bool MachineBlockPlacement::runOnMachineFunction(
MachineFunction &F) {
1109 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
1110 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
1111 MLI = &getAnalysis<MachineLoopInfo>();
1114 assert(BlockToChain.empty());
1118 BlockToChain.clear();
1119 ChainAllocator.DestroyAll();
1166 "Basic Block Placement Stats",
false,
false)
1170 "Basic Block Placement
Stats", false, false)
1172 bool MachineBlockPlacementStats::runOnMachineFunction(
MachineFunction &F) {
1177 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
1178 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
1182 Statistic &NumBranches = (
I->succ_size() > 1) ? NumCondBranches
1183 : NumUncondBranches;
1184 Statistic &BranchTakenFreq = (
I->succ_size() > 1) ? CondBranchTakenFreq
1185 : UncondBranchTakenFreq;
1190 if (
I->isLayoutSuccessor(*SI))
1193 BlockFrequency EdgeFreq = BlockFreq * MBPI->getEdgeProbability(
I, *SI);
virtual bool ReverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const
unsigned succ_size() const
static cl::opt< unsigned > AlignAllBlock("align-all-blocks", cl::desc("Force the alignment of all ""blocks in the function."), cl::init(0), cl::Hidden)
void push_back(const T &Elt)
const MachineFunction * getParent() const
const_iterator end(StringRef path)
Get end iterator over path.
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const
virtual const TargetLowering * getTargetLowering() const
STATISTIC(NumCondBranches,"Number of conditional branches")
static PassRegistry * getPassRegistry()
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const
void initializeMachineBlockPlacementStatsPass(PassRegistry &)
const_iterator begin(StringRef path)
Get begin iterator over path.
const Function * getFunction() const
static std::string getBlockNum(MachineBasicBlock *BB)
Helper to print the number of a MBB.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
char & MachineBlockPlacementStatsID
BlockT * getHeader() const
LoopInfoBase< BlockT, LoopT > * LI
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const HexagonInstrInfo * TII
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
std::vector< MachineBasicBlock * >::iterator succ_iterator
block placement Basic Block Placement Stats
ID
LLVM Calling Convention Representation.
const MachineBasicBlock & front() const
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
uint64_t rotate(uint64_t val, size_t shift)
Bitwise right rotate. Normally this will compile to a single instruction, especially if the shift is ...
std::vector< MachineBasicBlock * >::iterator pred_iterator
initializer< Ty > init(const Ty &Val)
friend const_iterator end(StringRef path)
Get end iterator over path.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
ItTy next(ItTy it, Dist n)
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, const SmallVectorImpl< MachineOperand > &Cond, DebugLoc DL) const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
INITIALIZE_PASS_BEGIN(MachineBlockPlacement,"block-placement2","Branch Probability Basic Block Placement", false, false) INITIALIZE_PASS_END(MachineBlockPlacement
succ_iterator succ_begin()
iterator erase(iterator I)
pred_iterator pred_begin()
void initializeMachineBlockPlacementPass(PassRegistry &)
virtual const TargetInstrInfo * getInstrInfo() const
block Branch Probability Basic Block Placement
virtual bool runOnMachineFunction(MachineFunction &MF)=0
void splice(iterator InsertPt, iterator MBBI)
friend const_iterator begin(StringRef path)
Get begin iterator over path.
bool isSuccessor(const MachineBasicBlock *MBB) const
block Branch Probability Basic Block static false std::string getBlockName(MachineBasicBlock *BB)
Helper to print the name of a MBB.
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
AttributeSet getAttributes() const
Return the attribute list for this Function.
StringRef getName() const
std::vector< BlockT * >::const_iterator block_iterator
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
block_iterator block_end() const
unsigned getNumBlocks() const
getNumBlocks - Get the number of blocks in this loop in constant time.
virtual void getAnalysisUsage(AnalysisUsage &AU) const
char & MachineBlockPlacementID
const TargetMachine & getTarget() const
block Branch Probability Basic Block false
BasicBlockListType::iterator iterator
ItTy prior(ItTy it, Dist n)
const MachineBasicBlock & back() const
block_iterator block_begin() const
std::vector< LoopT * >::const_iterator iterator
LoopInfoBase< MachineBasicBlock, MachineLoop >::iterator iterator
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
unsigned pred_size() const
#define LLVM_ATTRIBUTE_USED