• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- TailDuplication.cpp - Duplicate blocks into predecessors' tails ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file This pass duplicates basic blocks ending in unconditional branches
11 /// into the tails of their predecessors, using the TailDuplicator utility
12 /// class.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/CodeGen/TailDuplicator.h"
22 #include "llvm/Pass.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "tailduplication"
27 
28 namespace {
29 
30 class TailDuplicateBase : public MachineFunctionPass {
31   TailDuplicator Duplicator;
32   bool PreRegAlloc;
33 public:
TailDuplicateBase(char & PassID,bool PreRegAlloc)34   TailDuplicateBase(char &PassID, bool PreRegAlloc)
35     : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
36 
37   bool runOnMachineFunction(MachineFunction &MF) override;
38 
getAnalysisUsage(AnalysisUsage & AU) const39   void getAnalysisUsage(AnalysisUsage &AU) const override {
40     AU.addRequired<MachineBranchProbabilityInfo>();
41     MachineFunctionPass::getAnalysisUsage(AU);
42   }
43 };
44 
45 class TailDuplicate : public TailDuplicateBase {
46 public:
47   static char ID;
TailDuplicate()48   TailDuplicate() : TailDuplicateBase(ID, false) {
49     initializeTailDuplicatePass(*PassRegistry::getPassRegistry());
50   }
51 };
52 
53 class EarlyTailDuplicate : public TailDuplicateBase {
54 public:
55   static char ID;
EarlyTailDuplicate()56   EarlyTailDuplicate() : TailDuplicateBase(ID, true) {
57     initializeEarlyTailDuplicatePass(*PassRegistry::getPassRegistry());
58   }
59 };
60 
61 } // end anonymous namespace
62 
63 char TailDuplicate::ID;
64 char EarlyTailDuplicate::ID;
65 
66 char &llvm::TailDuplicateID = TailDuplicate::ID;
67 char &llvm::EarlyTailDuplicateID = EarlyTailDuplicate::ID;
68 
69 INITIALIZE_PASS(TailDuplicate, DEBUG_TYPE, "Tail Duplication", false, false)
70 INITIALIZE_PASS(EarlyTailDuplicate, "early-tailduplication",
71                 "Early Tail Duplication", false, false)
72 
runOnMachineFunction(MachineFunction & MF)73 bool TailDuplicateBase::runOnMachineFunction(MachineFunction &MF) {
74   if (skipFunction(MF.getFunction()))
75     return false;
76 
77   auto MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
78   Duplicator.initMF(MF, PreRegAlloc, MBPI, /*LayoutMode=*/false);
79 
80   bool MadeChange = false;
81   while (Duplicator.tailDuplicateBlocks())
82     MadeChange = true;
83 
84   return MadeChange;
85 }
86