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
15#include "llvm/CodeGen/TailDuplication.h"
16#include "llvm/Analysis/ProfileSummaryInfo.h"
17#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
18#include "llvm/CodeGen/MBFIWrapper.h"
19#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachinePassManager.h"
23#include "llvm/CodeGen/RegisterClassInfo.h"
24#include "llvm/CodeGen/TailDuplicator.h"
25#include "llvm/IR/Analysis.h"
26#include "llvm/InitializePasses.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 {
46 AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
47 AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
48 AU.addRequired<ProfileSummaryInfoWrapperPass>();
49 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
50 MachineFunctionPass::getAnalysisUsage(AU);
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(F: 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>(args&: *MBFI);
94 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI: 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>
105PreservedAnalyses TailDuplicatePassBase<DerivedT, PreRegAlloc>::run(
106 MachineFunction &MF, MachineFunctionAnalysisManager &MFAM) {
107 MFPropsModifier _(static_cast<DerivedT &>(*this), MF);
108
109 auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(IR&: MF);
110 auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(IR&: MF)
111 .getCachedResult<ProfileSummaryAnalysis>(
112 IR&: *MF.getFunction().getParent());
113 auto *MBFI = (PSI && PSI->hasProfileSummary()
114 ? &MFAM.getResult<MachineBlockFrequencyAnalysis>(IR&: MF)
115 : nullptr);
116 if (MBFI)
117 MBFIW = std::make_unique<MBFIWrapper>(args&: *MBFI);
118
119 TailDuplicator Duplicator;
120 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI: 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();
128 return getMachineFunctionPassPreservedAnalyses();
129}
130
131template class llvm::TailDuplicatePassBase<TailDuplicatePass, false>;
132template class llvm::TailDuplicatePassBase<EarlyTailDuplicatePass, true>;
133