LLVM 22.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 {
69 return MachineFunctionProperties().setNoPHIs();
70 }
71};
72
73} // end anonymous namespace
74
75char TailDuplicateLegacy::ID;
76char EarlyTailDuplicateLegacy::ID;
77
78char &llvm::TailDuplicateLegacyID = TailDuplicateLegacy::ID;
79char &llvm::EarlyTailDuplicateLegacyID = EarlyTailDuplicateLegacy::ID;
80
81INITIALIZE_PASS(TailDuplicateLegacy, DEBUG_TYPE, "Tail Duplication", false,
82 false)
83INITIALIZE_PASS(EarlyTailDuplicateLegacy, "early-tailduplication",
85
86bool TailDuplicateBaseLegacy::runOnMachineFunction(MachineFunction &MF) {
87 if (skipFunction(MF.getFunction()))
88 return false;
89
90 auto MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
91 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
92 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
93 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
94 nullptr;
95 if (MBFI)
96 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
97 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
98 /*LayoutMode=*/false);
99
100 bool MadeChange = false;
101 while (Duplicator.tailDuplicateBlocks())
102 MadeChange = true;
103
104 return MadeChange;
105}
106
107template <typename DerivedT, bool PreRegAlloc>
110 MFPropsModifier _(static_cast<DerivedT &>(*this), MF);
111
112 auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
114 .getCachedResult<ProfileSummaryAnalysis>(
115 *MF.getFunction().getParent());
116 auto *MBFI = (PSI && PSI->hasProfileSummary()
118 : nullptr);
119 if (MBFI)
120 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
121
122 TailDuplicator Duplicator;
123 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
124 /*LayoutMode=*/false);
125 bool MadeChange = false;
126 while (Duplicator.tailDuplicateBlocks())
127 MadeChange = true;
128
129 if (!MadeChange)
130 return PreservedAnalyses::all();
132}
133
#define _
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
early Early Tail Duplication
early tailduplication
#define DEBUG_TYPE
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
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:663
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.
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:716
static LLVM_ABI 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:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
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
LLVM_ABI char & TailDuplicateLegacyID
TailDuplicate - Duplicate blocks with unconditional branches into tails of their predecessors.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI void initializeTailDuplicateLegacyPass(PassRegistry &)
LLVM_ABI char & EarlyTailDuplicateLegacyID
Duplicate blocks with unconditional branches into tails of their predecessors.
LLVM_ABI void initializeEarlyTailDuplicateLegacyPass(PassRegistry &)