LLVM 24.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
25#include "llvm/IR/Analysis.h"
27#include "llvm/Pass.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 {
51 }
52};
53
54class TailDuplicateLegacy : public TailDuplicateBaseLegacy {
55public:
56 static char ID;
57 TailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, false) {}
58};
59
60class EarlyTailDuplicateLegacy : public TailDuplicateBaseLegacy {
61public:
62 static char ID;
63 EarlyTailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, true) {}
64
65 MachineFunctionProperties getClearedProperties() const override {
66 return MachineFunctionProperties().setNoPHIs();
67 }
68};
69
70} // end anonymous namespace
71
72char TailDuplicateLegacy::ID;
73char EarlyTailDuplicateLegacy::ID;
74
75char &llvm::TailDuplicateLegacyID = TailDuplicateLegacy::ID;
76char &llvm::EarlyTailDuplicateLegacyID = EarlyTailDuplicateLegacy::ID;
77
78INITIALIZE_PASS(TailDuplicateLegacy, DEBUG_TYPE, "Tail Duplication", false,
79 false)
80INITIALIZE_PASS(EarlyTailDuplicateLegacy, "early-tailduplication",
81 "Early Tail Duplication", false, false)
82
83bool TailDuplicateBaseLegacy::runOnMachineFunction(MachineFunction &MF) {
84 if (skipFunction(MF.getFunction()))
85 return false;
86
87 auto MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
88 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
89 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
90 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
91 nullptr;
92 if (MBFI)
93 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
94 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
95 /*LayoutMode=*/false);
96
97 bool MadeChange = false;
98 while (Duplicator.tailDuplicateBlocks())
99 MadeChange = true;
100
101 return MadeChange;
102}
103
104template <typename DerivedT, bool PreRegAlloc>
107 MFPropsModifier _(static_cast<DerivedT &>(*this), MF);
108
109 auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
111 .getCachedResult<ProfileSummaryAnalysis>(
112 *MF.getFunction().getParent());
113 auto *MBFI = (PSI && PSI->hasProfileSummary()
115 : nullptr);
116 if (MBFI)
117 MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
118
119 TailDuplicator Duplicator;
120 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
121 /*LayoutMode=*/false);
122 bool MadeChange = false;
123 while (Duplicator.tailDuplicateBlocks())
124 MadeChange = true;
125
126 if (!MadeChange)
127 return PreservedAnalyses::all();
129}
130
#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()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
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.
LLVM_ABI 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.
LLVM_ABI 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.
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.