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