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 // This pass duplicates basic blocks ending in unconditional branches into 11 // the tails of their predecessors, using the TailDuplicator utility 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 /// Perform tail duplication. Delegates to TailDuplicator 30 class TailDuplicatePass : public MachineFunctionPass { 31 TailDuplicator Duplicator; 32 33 public: 34 static char ID; 35 36 explicit TailDuplicatePass() : MachineFunctionPass(ID) {} 37 38 bool runOnMachineFunction(MachineFunction &MF) override; 39 40 void getAnalysisUsage(AnalysisUsage &AU) const override; 41 }; 42 43 } // end anonymous namespace 44 45 char TailDuplicatePass::ID = 0; 46 47 char &llvm::TailDuplicateID = TailDuplicatePass::ID; 48 49 INITIALIZE_PASS(TailDuplicatePass, DEBUG_TYPE, "Tail Duplication", false, false) 50 51 bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) { 52 if (skipFunction(MF.getFunction())) 53 return false; 54 55 auto MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); 56 57 // TODO: Querying isSSA() to determine pre-/post-regalloc is fragile, better 58 // split this into two passes instead. 59 bool PreRegAlloc = MF.getRegInfo().isSSA(); 60 Duplicator.initMF(MF, PreRegAlloc, MBPI, /* LayoutMode */ false); 61 62 bool MadeChange = false; 63 while (Duplicator.tailDuplicateBlocks()) 64 MadeChange = true; 65 66 return MadeChange; 67 } 68 69 void TailDuplicatePass::getAnalysisUsage(AnalysisUsage &AU) const { 70 AU.addRequired<MachineBranchProbabilityInfo>(); 71 MachineFunctionPass::getAnalysisUsage(AU); 72 } 73