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/TailDuplication.h" 16 #include "llvm/Analysis/ProfileSummaryInfo.h" 17 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h" 18 #include "llvm/CodeGen/MBFIWrapper.h" 19 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineFunctionPass.h" 22 #include "llvm/CodeGen/MachinePassManager.h" 23 #include "llvm/CodeGen/TailDuplicator.h" 24 #include "llvm/IR/Analysis.h" 25 #include "llvm/InitializePasses.h" 26 #include "llvm/Pass.h" 27 #include "llvm/PassRegistry.h" 28 29 using namespace llvm; 30 31 #define DEBUG_TYPE "tailduplication" 32 33 namespace { 34 35 class TailDuplicateBaseLegacy : public MachineFunctionPass { 36 TailDuplicator Duplicator; 37 std::unique_ptr<MBFIWrapper> MBFIW; 38 bool PreRegAlloc; 39 public: 40 TailDuplicateBaseLegacy(char &PassID, bool PreRegAlloc) 41 : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {} 42 43 bool runOnMachineFunction(MachineFunction &MF) override; 44 45 void getAnalysisUsage(AnalysisUsage &AU) const override { 46 AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); 47 AU.addRequired<LazyMachineBlockFrequencyInfoPass>(); 48 AU.addRequired<ProfileSummaryInfoWrapperPass>(); 49 MachineFunctionPass::getAnalysisUsage(AU); 50 } 51 }; 52 53 class TailDuplicateLegacy : public TailDuplicateBaseLegacy { 54 public: 55 static char ID; 56 TailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, false) { 57 initializeTailDuplicateLegacyPass(*PassRegistry::getPassRegistry()); 58 } 59 }; 60 61 class EarlyTailDuplicateLegacy : public TailDuplicateBaseLegacy { 62 public: 63 static char ID; 64 EarlyTailDuplicateLegacy() : TailDuplicateBaseLegacy(ID, true) { 65 initializeEarlyTailDuplicateLegacyPass(*PassRegistry::getPassRegistry()); 66 } 67 68 MachineFunctionProperties getClearedProperties() const override { 69 return MachineFunctionProperties() 70 .set(MachineFunctionProperties::Property::NoPHIs); 71 } 72 }; 73 74 } // end anonymous namespace 75 76 char TailDuplicateLegacy::ID; 77 char EarlyTailDuplicateLegacy::ID; 78 79 char &llvm::TailDuplicateLegacyID = TailDuplicateLegacy::ID; 80 char &llvm::EarlyTailDuplicateLegacyID = EarlyTailDuplicateLegacy::ID; 81 82 INITIALIZE_PASS(TailDuplicateLegacy, DEBUG_TYPE, "Tail Duplication", false, 83 false) 84 INITIALIZE_PASS(EarlyTailDuplicateLegacy, "early-tailduplication", 85 "Early Tail Duplication", false, false) 86 87 bool TailDuplicateBaseLegacy::runOnMachineFunction(MachineFunction &MF) { 88 if (skipFunction(MF.getFunction())) 89 return false; 90 91 auto MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); 92 auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); 93 auto *MBFI = (PSI && PSI->hasProfileSummary()) ? 94 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() : 95 nullptr; 96 if (MBFI) 97 MBFIW = std::make_unique<MBFIWrapper>(*MBFI); 98 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI, 99 /*LayoutMode=*/false); 100 101 bool MadeChange = false; 102 while (Duplicator.tailDuplicateBlocks()) 103 MadeChange = true; 104 105 return MadeChange; 106 } 107 108 template <typename DerivedT, bool PreRegAlloc> 109 PreservedAnalyses TailDuplicatePassBase<DerivedT, PreRegAlloc>::run( 110 MachineFunction &MF, MachineFunctionAnalysisManager &MFAM) { 111 MFPropsModifier _(static_cast<DerivedT &>(*this), MF); 112 113 auto *MBPI = &MFAM.getResult<MachineBranchProbabilityAnalysis>(MF); 114 auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF) 115 .getCachedResult<ProfileSummaryAnalysis>( 116 *MF.getFunction().getParent()); 117 auto *MBFI = (PSI && PSI->hasProfileSummary() 118 ? &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF) 119 : nullptr); 120 if (MBFI) 121 MBFIW = std::make_unique<MBFIWrapper>(*MBFI); 122 123 TailDuplicator Duplicator; 124 Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI, 125 /*LayoutMode=*/false); 126 bool MadeChange = false; 127 while (Duplicator.tailDuplicateBlocks()) 128 MadeChange = true; 129 130 if (!MadeChange) 131 return PreservedAnalyses::all(); 132 return getMachineFunctionPassPreservedAnalyses(); 133 } 134 135 template class llvm::TailDuplicatePassBase<TailDuplicatePass, false>; 136 template class llvm::TailDuplicatePassBase<EarlyTailDuplicatePass, true>; 137