LLVM 23.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
28using namespace llvm;
29
30#define DEBUG_TYPE "tailduplication"
31
32namespace {
33
34class TailDuplicateBaseLegacy : public MachineFunctionPass {
35 TailDuplicator Duplicator;
36 std::unique_ptr<MBFIWrapper> MBFIW;
37 bool PreRegAlloc;
38public:
39 TailDuplicateBaseLegacy(char &PassID, bool PreRegAlloc)
40 : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
41
42 bool runOnMachineFunction(MachineFunction &MF) override;
43
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 }
50};
51
52class TailDuplicateLegacy : public TailDuplicateBaseLegacy {
53public:
54 static char ID;
55 TailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, false) {}
56};
57
58class EarlyTailDuplicateLegacy : public TailDuplicateBaseLegacy {
59public:
60 static char ID;
61 EarlyTailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, true) {}
62
63 MachineFunctionProperties getClearedProperties() const override {
64 return MachineFunctionProperties().setNoPHIs();
65 }
66};
67
68} // end anonymous namespace
69
70char TailDuplicateLegacy::ID;
71char EarlyTailDuplicateLegacy::ID;
72
73char &llvm::TailDuplicateLegacyID = TailDuplicateLegacy::ID;
74char &llvm::EarlyTailDuplicateLegacyID = EarlyTailDuplicateLegacy::ID;
75
76INITIALIZE_PASS(TailDuplicateLegacy, DEBUG_TYPE, "Tail Duplication", false,
77 false)
78INITIALIZE_PASS(EarlyTailDuplicateLegacy, "early-tailduplication",
79 "Early Tail Duplication", false, false)
80
81bool TailDuplicateBaseLegacy::runOnMachineFunction(MachineFunction &MF) {
82 if (skipFunction(MF.getFunction()))
83 return false;
84
85 auto MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
86 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
87 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
88 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
89 nullptr;
90 if (MBFI)
91 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
92 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
93 /*LayoutMode=*/false);
94
95 bool MadeChange = false;
96 while (Duplicator.tailDuplicateBlocks())
97 MadeChange = true;
98
99 return MadeChange;
100}
101
102template <typename DerivedT, bool PreRegAlloc>
105 MFPropsModifier _(static_cast<DerivedT &>(*this), MF);
106
107 auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
109 .getCachedResult<ProfileSummaryAnalysis>(
110 *MF.getFunction().getParent());
111 auto *MBFI = (PSI && PSI->hasProfileSummary()
113 : nullptr);
114 if (MBFI)
115 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
116
117 TailDuplicator Duplicator;
118 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
119 /*LayoutMode=*/false);
120 bool MadeChange = false;
121 while (Duplicator.tailDuplicateBlocks())
122 MadeChange = true;
123
124 if (!MadeChange)
125 return PreservedAnalyses::all();
127}
128
#define DEBUG_TYPE
#define _
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
Module * getParent()
Get the module that this global value is contained inside of...
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.
Properties which a MachineFunction may have at a given point in time.
Function & getFunction()
Return the LLVM function that this machine code represents.
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI char & TailDuplicateLegacyID
TailDuplicate - Duplicate blocks with unconditional branches into tails of their predecessors.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI char & EarlyTailDuplicateLegacyID
Duplicate blocks with unconditional branches into tails of their predecessors.