LLVM 19.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
23#include "llvm/Pass.h"
24#include "llvm/PassRegistry.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "tailduplication"
29
30namespace {
31
32class TailDuplicateBase : public MachineFunctionPass {
33 TailDuplicator Duplicator;
34 std::unique_ptr<MBFIWrapper> MBFIW;
35 bool PreRegAlloc;
36public:
37 TailDuplicateBase(char &PassID, bool PreRegAlloc)
38 : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
39
40 bool runOnMachineFunction(MachineFunction &MF) override;
41
42 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 }
48};
49
50class TailDuplicate : public TailDuplicateBase {
51public:
52 static char ID;
53 TailDuplicate() : TailDuplicateBase(ID, false) {
55 }
56};
57
58class EarlyTailDuplicate : public TailDuplicateBase {
59public:
60 static char ID;
61 EarlyTailDuplicate() : TailDuplicateBase(ID, true) {
63 }
64
65 MachineFunctionProperties getClearedProperties() const override {
67 .set(MachineFunctionProperties::Property::NoPHIs);
68 }
69};
70
71} // end anonymous namespace
72
73char TailDuplicate::ID;
74char EarlyTailDuplicate::ID;
75
76char &llvm::TailDuplicateID = TailDuplicate::ID;
77char &llvm::EarlyTailDuplicateID = EarlyTailDuplicate::ID;
78
79INITIALIZE_PASS(TailDuplicate, DEBUG_TYPE, "Tail Duplication", false, false)
80INITIALIZE_PASS(EarlyTailDuplicate, "early-tailduplication",
81 "Early Tail Duplication", false, false)
82
83bool TailDuplicateBase::runOnMachineFunction(MachineFunction &MF) {
84 if (skipFunction(MF.getFunction()))
85 return false;
86
87 auto MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
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}
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
#define DEBUG_TYPE
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
This is an alternative analysis pass to MachineBlockFrequencyInfo.
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)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Utility class to perform tail duplication.
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 & EarlyTailDuplicateID
Duplicate blocks with unconditional branches into tails of their predecessors.
char & TailDuplicateID
TailDuplicate - Duplicate blocks with unconditional branches into tails of their predecessors.
void initializeEarlyTailDuplicatePass(PassRegistry &)
void initializeTailDuplicatePass(PassRegistry &)