TailDuplication.cpp 3.3 KB

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