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/Analysis/ProfileSummaryInfo.h"
16 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
17 #include "llvm/CodeGen/MBFIWrapper.h"
18 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/TailDuplicator.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Pass.h"
24 #include "llvm/PassRegistry.h"
28 #define DEBUG_TYPE "tailduplication"
32 class TailDuplicateBase
: public MachineFunctionPass
{
33 TailDuplicator Duplicator
;
34 std::unique_ptr
<MBFIWrapper
> MBFIW
;
37 TailDuplicateBase(char &PassID
, bool PreRegAlloc
)
38 : MachineFunctionPass(PassID
), PreRegAlloc(PreRegAlloc
) {}
40 bool runOnMachineFunction(MachineFunction
&MF
) override
;
42 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
43 AU
.addRequired
<MachineBranchProbabilityInfoWrapperPass
>();
44 AU
.addRequired
<LazyMachineBlockFrequencyInfoPass
>();
45 AU
.addRequired
<ProfileSummaryInfoWrapperPass
>();
46 MachineFunctionPass::getAnalysisUsage(AU
);
50 class TailDuplicate
: public TailDuplicateBase
{
53 TailDuplicate() : TailDuplicateBase(ID
, false) {
54 initializeTailDuplicatePass(*PassRegistry::getPassRegistry());
58 class EarlyTailDuplicate
: public TailDuplicateBase
{
61 EarlyTailDuplicate() : TailDuplicateBase(ID
, true) {
62 initializeEarlyTailDuplicatePass(*PassRegistry::getPassRegistry());
65 MachineFunctionProperties
getClearedProperties() const override
{
66 return MachineFunctionProperties()
67 .set(MachineFunctionProperties::Property::NoPHIs
);
71 } // end anonymous namespace
73 char TailDuplicate::ID
;
74 char EarlyTailDuplicate::ID
;
76 char &llvm::TailDuplicateID
= TailDuplicate::ID
;
77 char &llvm::EarlyTailDuplicateID
= EarlyTailDuplicate::ID
;
79 INITIALIZE_PASS(TailDuplicate
, DEBUG_TYPE
, "Tail Duplication", false, false)
80 INITIALIZE_PASS(EarlyTailDuplicate
, "early-tailduplication",
81 "Early Tail Duplication", false, false)
83 bool TailDuplicateBase::runOnMachineFunction(MachineFunction
&MF
) {
84 if (skipFunction(MF
.getFunction()))
87 auto MBPI
= &getAnalysis
<MachineBranchProbabilityInfoWrapperPass
>().getMBPI();
88 auto *PSI
= &getAnalysis
<ProfileSummaryInfoWrapperPass
>().getPSI();
89 auto *MBFI
= (PSI
&& PSI
->hasProfileSummary()) ?
90 &getAnalysis
<LazyMachineBlockFrequencyInfoPass
>().getBFI() :
93 MBFIW
= std::make_unique
<MBFIWrapper
>(*MBFI
);
94 Duplicator
.initMF(MF
, PreRegAlloc
, MBPI
, MBFI
? MBFIW
.get() : nullptr, PSI
,
95 /*LayoutMode=*/false);
97 bool MadeChange
= false;
98 while (Duplicator
.tailDuplicateBlocks())