24
24
//
25
25
// ===----------------------------------------------------------------------===//
26
26
27
+ #include " llvm/CodeGen/MachineBlockPlacement.h"
27
28
#include " BranchFolding.h"
28
29
#include " llvm/ADT/ArrayRef.h"
29
30
#include " llvm/ADT/DenseMap.h"
@@ -357,7 +358,7 @@ class BlockChain {
357
358
unsigned UnscheduledPredecessors = 0 ;
358
359
};
359
360
360
- class MachineBlockPlacement : public MachineFunctionPass {
361
+ class MachineBlockPlacement {
361
362
// / A type for a block filter set.
362
363
using BlockFilterSet = SmallSetVector<const MachineBasicBlock *, 16 >;
363
364
@@ -409,7 +410,11 @@ class MachineBlockPlacement : public MachineFunctionPass {
409
410
410
411
ProfileSummaryInfo *PSI = nullptr ;
411
412
412
- TargetPassConfig *PassConfig = nullptr ;
413
+ // Tail merging is also determined based on
414
+ // whether structured CFG is required.
415
+ bool AllowTailMerge;
416
+
417
+ CodeGenOptLevel OptLevel;
413
418
414
419
// / Duplicator used to duplicate tails during placement.
415
420
// /
@@ -608,18 +613,48 @@ class MachineBlockPlacement : public MachineFunctionPass {
608
613
// / Create a single CFG chain from the current block order.
609
614
void createCFGChainExtTsp ();
610
615
616
+ public:
617
+ MachineBlockPlacement (const MachineBranchProbabilityInfo *MBPI,
618
+ MachineLoopInfo *MLI, ProfileSummaryInfo *PSI,
619
+ std::unique_ptr<MBFIWrapper> MBFI,
620
+ MachinePostDominatorTree *MPDT, bool AllowTailMerge)
621
+ : MBPI(MBPI), MBFI(std::move(MBFI)), MLI(MLI), MPDT(MPDT), PSI(PSI),
622
+ AllowTailMerge (AllowTailMerge) {};
623
+
624
+ bool run (MachineFunction &F);
625
+
626
+ static bool allowTailDupPlacement (MachineFunction &MF) {
627
+ return TailDupPlacement && !MF.getTarget ().requiresStructuredCFG ();
628
+ }
629
+ };
630
+
631
+ class MachineBlockPlacementLegacy : public MachineFunctionPass {
611
632
public:
612
633
static char ID; // Pass identification, replacement for typeid
613
634
614
- MachineBlockPlacement () : MachineFunctionPass(ID) {
615
- initializeMachineBlockPlacementPass (*PassRegistry::getPassRegistry ());
635
+ MachineBlockPlacementLegacy () : MachineFunctionPass(ID) {
636
+ initializeMachineBlockPlacementLegacyPass (*PassRegistry::getPassRegistry ());
616
637
}
617
638
618
- bool runOnMachineFunction (MachineFunction &F) override ;
639
+ bool runOnMachineFunction (MachineFunction &MF) override {
640
+ if (skipFunction (MF.getFunction ()))
641
+ return false ;
619
642
620
- bool allowTailDupPlacement () const {
621
- assert (F);
622
- return TailDupPlacement && !F->getTarget ().requiresStructuredCFG ();
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);
623
658
}
624
659
625
660
void getAnalysisUsage (AnalysisUsage &AU) const override {
@@ -636,18 +671,18 @@ class MachineBlockPlacement : public MachineFunctionPass {
636
671
637
672
} // end anonymous namespace
638
673
639
- char MachineBlockPlacement ::ID = 0 ;
674
+ char MachineBlockPlacementLegacy ::ID = 0 ;
640
675
641
- char &llvm::MachineBlockPlacementID = MachineBlockPlacement ::ID;
676
+ char &llvm::MachineBlockPlacementID = MachineBlockPlacementLegacy ::ID;
642
677
643
- INITIALIZE_PASS_BEGIN (MachineBlockPlacement , DEBUG_TYPE,
678
+ INITIALIZE_PASS_BEGIN (MachineBlockPlacementLegacy , DEBUG_TYPE,
644
679
" Branch Probability Basic Block Placement" , false , false )
645
680
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
646
681
INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass)
647
682
INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass)
648
683
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
649
684
INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
650
- INITIALIZE_PASS_END(MachineBlockPlacement , DEBUG_TYPE,
685
+ INITIALIZE_PASS_END(MachineBlockPlacementLegacy , DEBUG_TYPE,
651
686
" Branch Probability Basic Block Placement" , false , false )
652
687
653
688
#ifndef NDEBUG
@@ -1130,7 +1165,7 @@ MachineBlockPlacement::getBestTrellisSuccessor(
1130
1165
MachineBasicBlock *Succ1 = BestA.Dest ;
1131
1166
MachineBasicBlock *Succ2 = BestB.Dest ;
1132
1167
// Check to see if tail-duplication would be profitable.
1133
- if (allowTailDupPlacement () && shouldTailDuplicate (Succ2) &&
1168
+ if (allowTailDupPlacement (*F ) && shouldTailDuplicate (Succ2) &&
1134
1169
canTailDuplicateUnplacedPreds (BB, Succ2, Chain, BlockFilter) &&
1135
1170
isProfitableToTailDup (BB, Succ2, MBPI->getEdgeProbability (BB, Succ1),
1136
1171
Chain, BlockFilter)) {
@@ -1655,7 +1690,7 @@ MachineBlockPlacement::selectBestSuccessor(const MachineBasicBlock *BB,
1655
1690
if (hasBetterLayoutPredecessor (BB, Succ, SuccChain, SuccProb, RealSuccProb,
1656
1691
Chain, BlockFilter)) {
1657
1692
// If tail duplication would make Succ profitable, place it.
1658
- if (allowTailDupPlacement () && shouldTailDuplicate (Succ))
1693
+ if (allowTailDupPlacement (*F ) && shouldTailDuplicate (Succ))
1659
1694
DupCandidates.emplace_back (SuccProb, Succ);
1660
1695
continue ;
1661
1696
}
@@ -1883,7 +1918,7 @@ void MachineBlockPlacement::buildChain(const MachineBasicBlock *HeadBB,
1883
1918
auto Result = selectBestSuccessor (BB, Chain, BlockFilter);
1884
1919
MachineBasicBlock *BestSucc = Result.BB ;
1885
1920
bool ShouldTailDup = Result.ShouldTailDup ;
1886
- if (allowTailDupPlacement ())
1921
+ if (allowTailDupPlacement (*F ))
1887
1922
ShouldTailDup |= (BestSucc && canTailDuplicateUnplacedPreds (
1888
1923
BB, BestSucc, Chain, BlockFilter));
1889
1924
@@ -1910,7 +1945,7 @@ void MachineBlockPlacement::buildChain(const MachineBasicBlock *HeadBB,
1910
1945
1911
1946
// Placement may have changed tail duplication opportunities.
1912
1947
// Check for that now.
1913
- if (allowTailDupPlacement () && BestSucc && ShouldTailDup) {
1948
+ if (allowTailDupPlacement (*F ) && BestSucc && ShouldTailDup) {
1914
1949
repeatedlyTailDuplicateBlock (BestSucc, BB, LoopHeaderBB, Chain,
1915
1950
BlockFilter, PrevUnplacedBlockIt,
1916
1951
PrevUnplacedBlockInFilterIt);
@@ -3466,7 +3501,7 @@ void MachineBlockPlacement::initTailDupThreshold() {
3466
3501
3467
3502
// For aggressive optimization, we can adjust some thresholds to be less
3468
3503
// conservative.
3469
- if (PassConfig-> getOptLevel () >= CodeGenOptLevel::Aggressive) {
3504
+ if (OptLevel >= CodeGenOptLevel::Aggressive) {
3470
3505
// At O3 we should be more willing to copy blocks for tail duplication. This
3471
3506
// increases size pressure, so we only do it at O3
3472
3507
// Do this unless only the regular threshold is explicitly set.
@@ -3478,29 +3513,56 @@ void MachineBlockPlacement::initTailDupThreshold() {
3478
3513
// If there's no threshold provided through options, query the target
3479
3514
// information for a threshold instead.
3480
3515
if (TailDupPlacementThreshold.getNumOccurrences () == 0 &&
3481
- (PassConfig-> getOptLevel () < CodeGenOptLevel::Aggressive ||
3516
+ (OptLevel < CodeGenOptLevel::Aggressive ||
3482
3517
TailDupPlacementAggressiveThreshold.getNumOccurrences () == 0 ))
3483
- TailDupSize = TII->getTailDuplicateSize (PassConfig-> getOptLevel () );
3518
+ TailDupSize = TII->getTailDuplicateSize (OptLevel );
3484
3519
}
3485
3520
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
+ false );
3537
+
3538
+ MachineBlockPlacement MBP (MBPI, MLI, PSI, std::move (MBFI), MPDT,
3539
+ AllowTailMerge);
3540
+
3541
+ if (!MBP.run (MF))
3542
+ return PreservedAnalyses::all ();
3543
+
3544
+ return getMachineFunctionPassPreservedAnalyses ();
3545
+ }
3546
+
3547
+ void MachineBlockPlacementPass::printPipeline (
3548
+ raw_ostream &OS,
3549
+ function_ref<StringRef(StringRef)> MapClassName2PassName) const {
3550
+ OS << MapClassName2PassName (name ());
3551
+ if (!AllowTailMerge)
3552
+ OS << " <no-tail-merge>" ;
3553
+ }
3554
+
3555
+ bool MachineBlockPlacement::run (MachineFunction &MF) {
3489
3556
3490
3557
// Check for single-block functions and skip them.
3491
3558
if (std::next (MF.begin ()) == MF.end ())
3492
3559
return false ;
3493
3560
3494
3561
F = &MF;
3495
- MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI ();
3496
- MBFI = std::make_unique<MBFIWrapper>(
3497
- getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ());
3498
- MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
3562
+ OptLevel = F->getTarget ().getOptLevel ();
3563
+
3499
3564
TII = MF.getSubtarget ().getInstrInfo ();
3500
3565
TLI = MF.getSubtarget ().getTargetLowering ();
3501
- MPDT = nullptr ;
3502
- PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI ();
3503
- PassConfig = &getAnalysis<TargetPassConfig>();
3504
3566
3505
3567
// Initialize PreferredLoopExit to nullptr here since it may never be set if
3506
3568
// there are no MachineLoops.
@@ -3529,8 +3591,7 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
3529
3591
}
3530
3592
3531
3593
// Apply tail duplication.
3532
- if (allowTailDupPlacement ()) {
3533
- MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree ();
3594
+ if (allowTailDupPlacement (*F)) {
3534
3595
if (OptForSize)
3535
3596
TailDupSize = 1 ;
3536
3597
const bool PreRegAlloc = false ;
@@ -3548,8 +3609,8 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
3548
3609
// TailMerge can create jump into if branches that make CFG irreducible for
3549
3610
// HW that requires structured CFG.
3550
3611
const bool EnableTailMerge = !MF.getTarget ().requiresStructuredCFG () &&
3551
- PassConfig-> getEnableTailMerge () &&
3552
- BranchFoldPlacement && MF.size () > 3 ;
3612
+ AllowTailMerge && BranchFoldPlacement &&
3613
+ MF.size () > 3 ;
3553
3614
// No tail merging opportunities if the block number is less than four.
3554
3615
if (EnableTailMerge) {
3555
3616
const unsigned TailMergeSize = TailDupSize + 1 ;
0 commit comments