Home | History | Annotate | Download | only in CodeGen
      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/MachineFunctionPass.h"
     16 #include "llvm/CodeGen/Passes.h"
     17 #include "llvm/CodeGen/TailDuplicator.h"
     18 #include "llvm/IR/Function.h"
     19 #include "llvm/Support/Debug.h"
     20 using namespace llvm;
     21 
     22 #define DEBUG_TYPE "tailduplication"
     23 
     24 namespace {
     25 /// Perform tail duplication. Delegates to TailDuplicator
     26 class TailDuplicatePass : public MachineFunctionPass {
     27   TailDuplicator Duplicator;
     28 
     29 public:
     30   static char ID;
     31   explicit TailDuplicatePass() : MachineFunctionPass(ID) {}
     32 
     33   bool runOnMachineFunction(MachineFunction &MF) override;
     34 
     35   void getAnalysisUsage(AnalysisUsage &AU) const override;
     36 };
     37 
     38 char TailDuplicatePass::ID = 0;
     39 }
     40 
     41 char &llvm::TailDuplicateID = TailDuplicatePass::ID;
     42 
     43 INITIALIZE_PASS(TailDuplicatePass, "tailduplication", "Tail Duplication", false,
     44                 false)
     45 
     46 bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) {
     47   if (skipFunction(*MF.getFunction()))
     48     return false;
     49 
     50   auto MMI = getAnalysisIfAvailable<MachineModuleInfo>();
     51   auto MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
     52 
     53   Duplicator.initMF(MF, MMI, MBPI);
     54 
     55   bool MadeChange = false;
     56   while (Duplicator.tailDuplicateBlocks(MF))
     57     MadeChange = true;
     58 
     59   return MadeChange;
     60 }
     61 
     62 void TailDuplicatePass::getAnalysisUsage(AnalysisUsage &AU) const {
     63   AU.addRequired<MachineBranchProbabilityInfo>();
     64   MachineFunctionPass::getAnalysisUsage(AU);
     65 }
     66