1 //===- TailDuplication.cpp - Duplicate blocks into predecessors' tails ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// \file This pass duplicates basic blocks ending in unconditional branches
10 /// into the tails of their predecessors, using the TailDuplicator utility
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/TailDuplication.h"
16 #include "llvm/Analysis/ProfileSummaryInfo.h"
17 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
18 #include "llvm/CodeGen/MBFIWrapper.h"
19 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachinePassManager.h"
23 #include "llvm/CodeGen/TailDuplicator.h"
24 #include "llvm/IR/Analysis.h"
25 #include "llvm/InitializePasses.h"
26 #include "llvm/Pass.h"
27 #include "llvm/PassRegistry.h"
31 #define DEBUG_TYPE "tailduplication"
35 class TailDuplicateBaseLegacy
: public MachineFunctionPass
{
36 TailDuplicator Duplicator
;
37 std::unique_ptr
<MBFIWrapper
> MBFIW
;
40 TailDuplicateBaseLegacy(char &PassID
, bool PreRegAlloc
)
41 : MachineFunctionPass(PassID
), PreRegAlloc(PreRegAlloc
) {}
43 bool runOnMachineFunction(MachineFunction
&MF
) override
;
45 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
46 AU
.addRequired
<MachineBranchProbabilityInfoWrapperPass
>();
47 AU
.addRequired
<LazyMachineBlockFrequencyInfoPass
>();
48 AU
.addRequired
<ProfileSummaryInfoWrapperPass
>();
49 MachineFunctionPass::getAnalysisUsage(AU
);
53 class TailDuplicateLegacy
: public TailDuplicateBaseLegacy
{
56 TailDuplicateLegacy() : TailDuplicateBaseLegacy(ID
, false) {
57 initializeTailDuplicateLegacyPass(*PassRegistry::getPassRegistry());
61 class EarlyTailDuplicateLegacy
: public TailDuplicateBaseLegacy
{
64 EarlyTailDuplicateLegacy() : TailDuplicateBaseLegacy(ID
, true) {
65 initializeEarlyTailDuplicateLegacyPass(*PassRegistry::getPassRegistry());
68 MachineFunctionProperties
getClearedProperties() const override
{
69 return MachineFunctionProperties()
70 .set(MachineFunctionProperties::Property::NoPHIs
);
74 } // end anonymous namespace
76 char TailDuplicateLegacy::ID
;
77 char EarlyTailDuplicateLegacy::ID
;
79 char &llvm::TailDuplicateLegacyID
= TailDuplicateLegacy::ID
;
80 char &llvm::EarlyTailDuplicateLegacyID
= EarlyTailDuplicateLegacy::ID
;
82 INITIALIZE_PASS(TailDuplicateLegacy
, DEBUG_TYPE
, "Tail Duplication", false,
84 INITIALIZE_PASS(EarlyTailDuplicateLegacy
, "early-tailduplication",
85 "Early Tail Duplication", false, false)
87 bool TailDuplicateBaseLegacy::runOnMachineFunction(MachineFunction
&MF
) {
88 if (skipFunction(MF
.getFunction()))
91 auto MBPI
= &getAnalysis
<MachineBranchProbabilityInfoWrapperPass
>().getMBPI();
92 auto *PSI
= &getAnalysis
<ProfileSummaryInfoWrapperPass
>().getPSI();
93 auto *MBFI
= (PSI
&& PSI
->hasProfileSummary()) ?
94 &getAnalysis
<LazyMachineBlockFrequencyInfoPass
>().getBFI() :
97 MBFIW
= std::make_unique
<MBFIWrapper
>(*MBFI
);
98 Duplicator
.initMF(MF
, PreRegAlloc
, MBPI
, MBFI
? MBFIW
.get() : nullptr, PSI
,
99 /*LayoutMode=*/false);
101 bool MadeChange
= false;
102 while (Duplicator
.tailDuplicateBlocks())
108 template <typename DerivedT
, bool PreRegAlloc
>
109 PreservedAnalyses TailDuplicatePassBase
<DerivedT
, PreRegAlloc
>::run(
110 MachineFunction
&MF
, MachineFunctionAnalysisManager
&MFAM
) {
111 MFPropsModifier
_(static_cast<DerivedT
&>(*this), MF
);
113 auto *MBPI
= &MFAM
.getResult
<MachineBranchProbabilityAnalysis
>(MF
);
114 auto *PSI
= MFAM
.getResult
<ModuleAnalysisManagerMachineFunctionProxy
>(MF
)
115 .getCachedResult
<ProfileSummaryAnalysis
>(
116 *MF
.getFunction().getParent());
117 auto *MBFI
= (PSI
&& PSI
->hasProfileSummary()
118 ? &MFAM
.getResult
<MachineBlockFrequencyAnalysis
>(MF
)
121 MBFIW
= std::make_unique
<MBFIWrapper
>(*MBFI
);
123 TailDuplicator Duplicator
;
124 Duplicator
.initMF(MF
, PreRegAlloc
, MBPI
, MBFI
? MBFIW
.get() : nullptr, PSI
,
125 /*LayoutMode=*/false);
126 bool MadeChange
= false;
127 while (Duplicator
.tailDuplicateBlocks())
131 return PreservedAnalyses::all();
132 return getMachineFunctionPassPreservedAnalyses();
135 template class llvm::TailDuplicatePassBase
<TailDuplicatePass
, false>;
136 template class llvm::TailDuplicatePassBase
<EarlyTailDuplicatePass
, true>;