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