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/Analysis/ProfileSummaryInfo.h"
16 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
17 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/CodeGen/TailDuplicator.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Pass.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "tailduplication"
29 
30 namespace {
31 
32 class TailDuplicateBase : public MachineFunctionPass {
33   TailDuplicator Duplicator;
34   bool PreRegAlloc;
35 public:
TailDuplicateBase(char & PassID,bool PreRegAlloc)36   TailDuplicateBase(char &PassID, bool PreRegAlloc)
37     : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
38 
39   bool runOnMachineFunction(MachineFunction &MF) override;
40 
getAnalysisUsage(AnalysisUsage & AU) const41   void getAnalysisUsage(AnalysisUsage &AU) const override {
42     AU.addRequired<MachineBranchProbabilityInfo>();
43     AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
44     AU.addRequired<ProfileSummaryInfoWrapperPass>();
45     MachineFunctionPass::getAnalysisUsage(AU);
46   }
47 };
48 
49 class TailDuplicate : public TailDuplicateBase {
50 public:
51   static char ID;
TailDuplicate()52   TailDuplicate() : TailDuplicateBase(ID, false) {
53     initializeTailDuplicatePass(*PassRegistry::getPassRegistry());
54   }
55 };
56 
57 class EarlyTailDuplicate : public TailDuplicateBase {
58 public:
59   static char ID;
EarlyTailDuplicate()60   EarlyTailDuplicate() : TailDuplicateBase(ID, true) {
61     initializeEarlyTailDuplicatePass(*PassRegistry::getPassRegistry());
62   }
63 
getClearedProperties() const64   MachineFunctionProperties getClearedProperties() const override {
65     return MachineFunctionProperties()
66       .set(MachineFunctionProperties::Property::NoPHIs);
67   }
68 };
69 
70 } // end anonymous namespace
71 
72 char TailDuplicate::ID;
73 char EarlyTailDuplicate::ID;
74 
75 char &llvm::TailDuplicateID = TailDuplicate::ID;
76 char &llvm::EarlyTailDuplicateID = EarlyTailDuplicate::ID;
77 
78 INITIALIZE_PASS(TailDuplicate, DEBUG_TYPE, "Tail Duplication", false, false)
79 INITIALIZE_PASS(EarlyTailDuplicate, "early-tailduplication",
80                 "Early Tail Duplication", false, false)
81 
runOnMachineFunction(MachineFunction & MF)82 bool TailDuplicateBase::runOnMachineFunction(MachineFunction &MF) {
83   if (skipFunction(MF.getFunction()))
84     return false;
85 
86   auto MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
87   auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
88   auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
89                &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
90                nullptr;
91   Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI, PSI, /*LayoutMode=*/false);
92 
93   bool MadeChange = false;
94   while (Duplicator.tailDuplicateBlocks())
95     MadeChange = true;
96 
97   return MadeChange;
98 }
99