2424//
2525// ===----------------------------------------------------------------------===//
2626
27+ #include " llvm/CodeGen/MachineBlockPlacement.h"
2728#include " BranchFolding.h"
2829#include " llvm/ADT/ArrayRef.h"
2930#include " llvm/ADT/DenseMap.h"
5051#include " llvm/CodeGen/TargetSubtargetInfo.h"
5152#include " llvm/IR/DebugLoc.h"
5253#include " llvm/IR/Function.h"
54+ #include " llvm/IR/PassManager.h"
5355#include " llvm/IR/PrintPasses.h"
5456#include " llvm/InitializePasses.h"
5557#include " llvm/Pass.h"
@@ -357,7 +359,7 @@ class BlockChain {
357359 unsigned UnscheduledPredecessors = 0 ;
358360};
359361
360- class MachineBlockPlacement : public MachineFunctionPass {
362+ class MachineBlockPlacement {
361363 // / A type for a block filter set.
362364 using BlockFilterSet = SmallSetVector<const MachineBasicBlock *, 16 >;
363365
@@ -409,7 +411,11 @@ class MachineBlockPlacement : public MachineFunctionPass {
409411
410412 ProfileSummaryInfo *PSI = nullptr ;
411413
412- TargetPassConfig *PassConfig = nullptr ;
414+ // TargetPassConfig *PassConfig = nullptr;
415+ // Whether to EnableTailMerge.
416+ bool AllowTailMerge;
417+
418+ CodeGenOptLevel OptLevel;
413419
414420 // / Duplicator used to duplicate tails during placement.
415421 // /
@@ -609,17 +615,46 @@ class MachineBlockPlacement : public MachineFunctionPass {
609615 void createCFGChainExtTsp ();
610616
611617public:
612- static char ID; // Pass identification, replacement for typeid
618+ MachineBlockPlacement (const MachineBranchProbabilityInfo *MBPI,
619+ MachineLoopInfo *MLI, ProfileSummaryInfo *PSI,
620+ std::unique_ptr<MBFIWrapper> MBFI,
621+ MachinePostDominatorTree *MPDT, bool AllowTailMerge)
622+ : MBPI(MBPI), MBFI(std::move(MBFI)), MLI(MLI), MPDT(MPDT), PSI(PSI),
623+ AllowTailMerge (AllowTailMerge) {};
624+
625+ bool run (MachineFunction &F);
613626
614- MachineBlockPlacement () : MachineFunctionPass(ID ) {
615- initializeMachineBlockPlacementPass (* PassRegistry::getPassRegistry () );
627+ static bool allowTailDupPlacement (MachineFunction &MF ) {
628+ return TailDupPlacement && !MF. getTarget (). requiresStructuredCFG ( );
616629 }
630+ };
617631
618- bool runOnMachineFunction (MachineFunction &F) override ;
632+ class MachineBlockPlacementLegacy : public MachineFunctionPass {
633+ public:
634+ static char ID; // Pass identification, replacement for typeid
619635
620- bool allowTailDupPlacement () const {
621- assert (F);
622- return TailDupPlacement && !F->getTarget ().requiresStructuredCFG ();
636+ MachineBlockPlacementLegacy () : MachineFunctionPass(ID) {
637+ initializeMachineBlockPlacementLegacyPass (*PassRegistry::getPassRegistry ());
638+ }
639+
640+ bool runOnMachineFunction (MachineFunction &MF) override {
641+ if (skipFunction (MF.getFunction ()))
642+ return false ;
643+ auto *MBPI =
644+ &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI ();
645+ auto MBFI = std::make_unique<MBFIWrapper>(
646+ getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ());
647+ auto *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
648+ auto *MPDT = MachineBlockPlacement::allowTailDupPlacement (MF)
649+ ? &getAnalysis<MachinePostDominatorTreeWrapperPass>()
650+ .getPostDomTree ()
651+ : nullptr ;
652+ auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI ();
653+ auto *PassConfig = &getAnalysis<TargetPassConfig>();
654+ bool AllowTailMerge = PassConfig->getEnableTailMerge ();
655+ return MachineBlockPlacement (MBPI, MLI, PSI, std::move (MBFI), MPDT,
656+ AllowTailMerge)
657+ .run (MF);
623658 }
624659
625660 void getAnalysisUsage (AnalysisUsage &AU) const override {
@@ -636,18 +671,18 @@ class MachineBlockPlacement : public MachineFunctionPass {
636671
637672} // end anonymous namespace
638673
639- char MachineBlockPlacement ::ID = 0 ;
674+ char MachineBlockPlacementLegacy ::ID = 0 ;
640675
641- char &llvm::MachineBlockPlacementID = MachineBlockPlacement ::ID;
676+ char &llvm::MachineBlockPlacementID = MachineBlockPlacementLegacy ::ID;
642677
643- INITIALIZE_PASS_BEGIN (MachineBlockPlacement , DEBUG_TYPE,
678+ INITIALIZE_PASS_BEGIN (MachineBlockPlacementLegacy , DEBUG_TYPE,
644679 " Branch Probability Basic Block Placement" , false , false )
645680INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
646681INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass)
647682INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass)
648683INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
649684INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
650- INITIALIZE_PASS_END(MachineBlockPlacement , DEBUG_TYPE,
685+ INITIALIZE_PASS_END(MachineBlockPlacementLegacy , DEBUG_TYPE,
651686 " Branch Probability Basic Block Placement" , false , false )
652687
653688#ifndef NDEBUG
@@ -1130,7 +1165,7 @@ MachineBlockPlacement::getBestTrellisSuccessor(
11301165 MachineBasicBlock *Succ1 = BestA.Dest ;
11311166 MachineBasicBlock *Succ2 = BestB.Dest ;
11321167 // Check to see if tail-duplication would be profitable.
1133- if (allowTailDupPlacement () && shouldTailDuplicate (Succ2) &&
1168+ if (allowTailDupPlacement (*F ) && shouldTailDuplicate (Succ2) &&
11341169 canTailDuplicateUnplacedPreds (BB, Succ2, Chain, BlockFilter) &&
11351170 isProfitableToTailDup (BB, Succ2, MBPI->getEdgeProbability (BB, Succ1),
11361171 Chain, BlockFilter)) {
@@ -1655,7 +1690,7 @@ MachineBlockPlacement::selectBestSuccessor(const MachineBasicBlock *BB,
16551690 if (hasBetterLayoutPredecessor (BB, Succ, SuccChain, SuccProb, RealSuccProb,
16561691 Chain, BlockFilter)) {
16571692 // If tail duplication would make Succ profitable, place it.
1658- if (allowTailDupPlacement () && shouldTailDuplicate (Succ))
1693+ if (allowTailDupPlacement (*F ) && shouldTailDuplicate (Succ))
16591694 DupCandidates.emplace_back (SuccProb, Succ);
16601695 continue ;
16611696 }
@@ -1883,7 +1918,7 @@ void MachineBlockPlacement::buildChain(const MachineBasicBlock *HeadBB,
18831918 auto Result = selectBestSuccessor (BB, Chain, BlockFilter);
18841919 MachineBasicBlock *BestSucc = Result.BB ;
18851920 bool ShouldTailDup = Result.ShouldTailDup ;
1886- if (allowTailDupPlacement ())
1921+ if (allowTailDupPlacement (*F ))
18871922 ShouldTailDup |= (BestSucc && canTailDuplicateUnplacedPreds (
18881923 BB, BestSucc, Chain, BlockFilter));
18891924
@@ -1910,7 +1945,7 @@ void MachineBlockPlacement::buildChain(const MachineBasicBlock *HeadBB,
19101945
19111946 // Placement may have changed tail duplication opportunities.
19121947 // Check for that now.
1913- if (allowTailDupPlacement () && BestSucc && ShouldTailDup) {
1948+ if (allowTailDupPlacement (*F ) && BestSucc && ShouldTailDup) {
19141949 repeatedlyTailDuplicateBlock (BestSucc, BB, LoopHeaderBB, Chain,
19151950 BlockFilter, PrevUnplacedBlockIt,
19161951 PrevUnplacedBlockInFilterIt);
@@ -3466,7 +3501,7 @@ void MachineBlockPlacement::initTailDupThreshold() {
34663501
34673502 // For aggressive optimization, we can adjust some thresholds to be less
34683503 // conservative.
3469- if (PassConfig-> getOptLevel () >= CodeGenOptLevel::Aggressive) {
3504+ if (OptLevel >= CodeGenOptLevel::Aggressive) {
34703505 // At O3 we should be more willing to copy blocks for tail duplication. This
34713506 // increases size pressure, so we only do it at O3
34723507 // Do this unless only the regular threshold is explicitly set.
@@ -3478,29 +3513,56 @@ void MachineBlockPlacement::initTailDupThreshold() {
34783513 // If there's no threshold provided through options, query the target
34793514 // information for a threshold instead.
34803515 if (TailDupPlacementThreshold.getNumOccurrences () == 0 &&
3481- (PassConfig-> getOptLevel () < CodeGenOptLevel::Aggressive ||
3516+ (OptLevel < CodeGenOptLevel::Aggressive ||
34823517 TailDupPlacementAggressiveThreshold.getNumOccurrences () == 0 ))
3483- TailDupSize = TII->getTailDuplicateSize (PassConfig-> getOptLevel () );
3518+ TailDupSize = TII->getTailDuplicateSize (OptLevel );
34843519}
34853520
3486- bool MachineBlockPlacement::runOnMachineFunction (MachineFunction &MF) {
3487- if (skipFunction (MF.getFunction ()))
3488- return false ;
3521+ PreservedAnalyses
3522+ MachineBlockPlacementPass::run (MachineFunction &MF,
3523+ MachineFunctionAnalysisManager &MFAM) {
3524+ auto *MBPI = &MFAM.getResult <MachineBranchProbabilityAnalysis>(MF);
3525+ auto MBFI = std::make_unique<MBFIWrapper>(
3526+ MFAM.getResult <MachineBlockFrequencyAnalysis>(MF));
3527+ auto *MLI = &MFAM.getResult <MachineLoopAnalysis>(MF);
3528+ auto *MPDT = MachineBlockPlacement::allowTailDupPlacement (MF)
3529+ ? &MFAM.getResult <MachinePostDominatorTreeAnalysis>(MF)
3530+ : nullptr ;
3531+ auto *PSI = MFAM.getResult <ModuleAnalysisManagerMachineFunctionProxy>(MF)
3532+ .getCachedResult <ProfileSummaryAnalysis>(
3533+ *MF.getFunction ().getParent ());
3534+ if (!PSI)
3535+ report_fatal_error (" MachineBlockPlacement requires ProfileSummaryAnalysis" );
3536+ //
3537+ MachineBlockPlacement MBP (MBPI, MLI, PSI, std::move (MBFI), MPDT,
3538+ AllowTailMerge);
3539+
3540+ if (!MBP.run (MF))
3541+ return PreservedAnalyses::all ();
3542+
3543+ return getMachineFunctionPassPreservedAnalyses ();
3544+ }
3545+
3546+ bool MachineBlockPlacement::run (MachineFunction &MF) {
34893547
34903548 // Check for single-block functions and skip them.
34913549 if (std::next (MF.begin ()) == MF.end ())
34923550 return false ;
34933551
34943552 F = &MF;
3495- MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI ();
3496- MBFI = std::make_unique<MBFIWrapper>(
3497- getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ());
3498- MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
3553+ OptLevel = F->getTarget ().getOptLevel ();
3554+ // MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
3555+ // // MBFI = std::make_unique<MBFIWrapper>(MBFI);
3556+ // MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
3557+
34993558 TII = MF.getSubtarget ().getInstrInfo ();
35003559 TLI = MF.getSubtarget ().getTargetLowering ();
3501- MPDT = nullptr ;
3502- PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI ();
3503- PassConfig = &getAnalysis<TargetPassConfig>();
3560+ // MPDT = nullptr;
3561+ // PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
3562+ // PassConfig = &getAnalysis<TargetPassConfig>();
3563+ llvm::dbgs () << " === The values are "
3564+ << (OptLevel == CodeGenOptLevel::Aggressive) << " and "
3565+ << AllowTailMerge;
35043566
35053567 // Initialize PreferredLoopExit to nullptr here since it may never be set if
35063568 // there are no MachineLoops.
@@ -3529,8 +3591,10 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
35293591 }
35303592
35313593 // Apply tail duplication.
3532- if (allowTailDupPlacement ()) {
3533- MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree ();
3594+ if (allowTailDupPlacement (*F)) {
3595+ llvm::dbgs () << " here\n " ;
3596+ // MPDT =
3597+ // &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
35343598 if (OptForSize)
35353599 TailDupSize = 1 ;
35363600 const bool PreRegAlloc = false ;
@@ -3548,8 +3612,8 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
35483612 // TailMerge can create jump into if branches that make CFG irreducible for
35493613 // HW that requires structured CFG.
35503614 const bool EnableTailMerge = !MF.getTarget ().requiresStructuredCFG () &&
3551- PassConfig-> getEnableTailMerge () &&
3552- BranchFoldPlacement && MF.size () > 3 ;
3615+ AllowTailMerge && BranchFoldPlacement &&
3616+ MF.size () > 3 ;
35533617 // No tail merging opportunities if the block number is less than four.
35543618 if (EnableTailMerge) {
35553619 const unsigned TailMergeSize = TailDupSize + 1 ;
0 commit comments