LLVM 20.0.0git
TailDuplication.cpp
Go to the documentation of this file.
1//===- TailDuplication.cpp - Duplicate blocks into predecessors' tails ----===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file This pass duplicates basic blocks ending in unconditional branches
10/// into the tails of their predecessors, using the TailDuplicator utility
11/// class.
12//
13//===----------------------------------------------------------------------===//
14
26#include "llvm/Pass.h"
27#include "llvm/PassRegistry.h"
28
29using namespace llvm;
30
31#define DEBUG_TYPE "tailduplication"
32
33namespace {
34
35class TailDuplicateBaseLegacy : public MachineFunctionPass {
36 TailDuplicator Duplicator;
37 std::unique_ptr<MBFIWrapper> MBFIW;
38 bool PreRegAlloc;
39public:
40 TailDuplicateBaseLegacy(char &PassID, bool PreRegAlloc)
41 : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
42
43 bool runOnMachineFunction(MachineFunction &MF) override;
44
45 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 }
51};
52
53class TailDuplicateLegacy : public TailDuplicateBaseLegacy {
54public:
55 static char ID;
56 TailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, false) {
58 }
59};
60
61class EarlyTailDuplicateLegacy : public TailDuplicateBaseLegacy {
62public:
63 static char ID;
64 EarlyTailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, true) {
66 }
67
68 MachineFunctionProperties getClearedProperties() const override {
70 .set(MachineFunctionProperties::Property::NoPHIs);
71 }
72};
73
74} // end anonymous namespace
75
76char TailDuplicateLegacy::ID;
77char EarlyTailDuplicateLegacy::ID;
78
79char &llvm::TailDuplicateLegacyID = TailDuplicateLegacy::ID;
80char &llvm::EarlyTailDuplicateLegacyID = EarlyTailDuplicateLegacy::ID;
81
82INITIALIZE_PASS(TailDuplicateLegacy, DEBUG_TYPE, "Tail Duplication", false,
83 false)
84INITIALIZE_PASS(EarlyTailDuplicateLegacy, "early-tailduplication",
86
87bool TailDuplicateBaseLegacy::runOnMachineFunction(MachineFunction &MF) {
88 if (skipFunction(MF.getFunction()))
89 return false;
90
91 auto MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
92 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
93 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
94 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
95 nullptr;
96 if (MBFI)
97 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
98 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
99 /*LayoutMode=*/false);
100
101 bool MadeChange = false;
102 while (Duplicator.tailDuplicateBlocks())
103 MadeChange = true;
104
105 return MadeChange;
106}
107
108template <typename DerivedT, bool PreRegAlloc>
111 MFPropsModifier _(static_cast<DerivedT &>(*this), MF);
112
113 auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
115 .getCachedResult<ProfileSummaryAnalysis>(
116 *MF.getFunction().getParent());
117 auto *MBFI = (PSI && PSI->hasProfileSummary()
119 : nullptr);
120 if (MBFI)
121 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
122
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())
128 MadeChange = true;
129
130 if (!MadeChange)
131 return PreservedAnalyses::all();
133}
134
#define _
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
early Early Tail Duplication
early tailduplication
#define DEBUG_TYPE
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
This is an alternative analysis pass to MachineBlockFrequencyInfo.
An RAII based helper class to modify MachineFunctionProperties when running pass.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
Function & getFunction()
Return the LLVM function that this machine code represents.
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:692
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Utility class to perform tail duplication.
void initMF(MachineFunction &MF, bool PreRegAlloc, const MachineBranchProbabilityInfo *MBPI, MBFIWrapper *MBFI, ProfileSummaryInfo *PSI, bool LayoutMode, unsigned TailDupSize=0)
Prepare to run on a specific machine function.
bool tailDuplicateBlocks()
Look for small blocks that are unconditionally branched to and do not fall through.
Pass manager infrastructure for declaring and invalidating analyses.
TargetPassConfig.
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition: CallingConv.h:76
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
char & TailDuplicateLegacyID
TailDuplicate - Duplicate blocks with unconditional branches into tails of their predecessors.
void initializeTailDuplicateLegacyPass(PassRegistry &)
PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void initializeEarlyTailDuplicateLegacyPass(PassRegistry &)
char & EarlyTailDuplicateLegacyID
Duplicate blocks with unconditional branches into tails of their predecessors.