[llvm] [NewPM] Port x86-pad-short-functions to new PM (PR #190886)

Kyungtak Woo via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 19 20:02:02 PDT 2026


https://github.com/kevinwkt updated https://github.com/llvm/llvm-project/pull/190886

>From 7aad0d66ed3e395121ca533b9f68a1a2c9b752bc Mon Sep 17 00:00:00 2001
From: Kyungtak Woo <kevinwkt at google.com>
Date: Wed, 8 Apr 2026 02:04:45 +0000
Subject: [PATCH 1/2] adding x86-pad-short-functions port

---
 llvm/lib/Target/X86/X86.h                     |  10 +-
 llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp |   3 +-
 llvm/lib/Target/X86/X86PadShortFunction.cpp   | 253 ++++++++++--------
 llvm/lib/Target/X86/X86PassRegistry.def       |   2 +-
 llvm/lib/Target/X86/X86TargetMachine.cpp      |   2 +-
 llvm/test/CodeGen/X86/llc-pipeline-npm.ll     |   2 +
 6 files changed, 148 insertions(+), 124 deletions(-)

diff --git a/llvm/lib/Target/X86/X86.h b/llvm/lib/Target/X86/X86.h
index 5ff33a48b63ae..9779b44f381ee 100644
--- a/llvm/lib/Target/X86/X86.h
+++ b/llvm/lib/Target/X86/X86.h
@@ -84,7 +84,15 @@ FunctionPass *createX86IndirectBranchTrackingLegacyPass();
 
 /// Return a pass that pads short functions with NOOPs.
 /// This will prevent a stall when returning on the Atom.
-FunctionPass *createX86PadShortFunctions();
+class X86PadShortFunctionsPass
+    : public PassInfoMixin<X86PadShortFunctionsPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+  static StringRef name() { return "X86PadShortFunctionsPass"; }
+};
+
+FunctionPass *createX86PadShortFunctionsLegacyPass();
 
 /// Return a pass that selectively replaces certain instructions (like add,
 /// sub, inc, dec, some shifts, and some multiplies) by equivalent LEA
diff --git a/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp b/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp
index 8ea34c764975f..c575d20c40ea2 100644
--- a/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp
+++ b/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp
@@ -189,8 +189,7 @@ void X86CodeGenPassBuilder::addPreEmitPass(PassManagerWrapper &PMW) const {
 
   if (getOptLevel() != CodeGenOptLevel::None) {
     addMachineFunctionPass(X86FixupBWInstsPass(), PMW);
-    // TODO(boomanaiden154): Add X86PadShortFunctionsPass here once it has been
-    // ported.
+    addMachineFunctionPass(X86PadShortFunctionsPass(), PMW);
     addMachineFunctionPass(X86FixupLEAsPass(), PMW);
     addMachineFunctionPass(X86FixupInstTuningPass(), PMW);
     addMachineFunctionPass(X86FixupVectorConstantsPass(), PMW);
diff --git a/llvm/lib/Target/X86/X86PadShortFunction.cpp b/llvm/lib/Target/X86/X86PadShortFunction.cpp
index 049384eefa188..d8436881553dd 100644
--- a/llvm/lib/Target/X86/X86PadShortFunction.cpp
+++ b/llvm/lib/Target/X86/X86PadShortFunction.cpp
@@ -33,76 +33,132 @@ using namespace llvm;
 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
 
 namespace {
-  struct VisitedBBInfo {
-    // HasReturn - Whether the BB contains a return instruction
-    bool HasReturn = false;
+struct VisitedBBInfo {
+  // HasReturn - Whether the BB contains a return instruction
+  bool HasReturn = false;
 
-    // Cycles - Number of cycles until return if HasReturn is true, otherwise
-    // number of cycles until end of the BB
-    unsigned int Cycles = 0;
+  // Cycles - Number of cycles until return if HasReturn is true, otherwise
+  // number of cycles until end of the BB
+  unsigned int Cycles = 0;
 
-    VisitedBBInfo() = default;
-    VisitedBBInfo(bool HasReturn, unsigned int Cycles)
+  VisitedBBInfo() = default;
+  VisitedBBInfo(bool HasReturn, unsigned int Cycles)
       : HasReturn(HasReturn), Cycles(Cycles) {}
-  };
+};
 
-  struct PadShortFunc : public MachineFunctionPass {
-    static char ID;
-    PadShortFunc() : MachineFunctionPass(ID) {}
+// Maps basic blocks that return to the minimum number of
+// cycles until the return, starting from the entry block.
+using ReturnBBsMap = DenseMap<MachineBasicBlock *, unsigned int>;
 
-    bool runOnMachineFunction(MachineFunction &MF) override;
+// Cache of previously visited BBs.
+using VisitedBBsMap = DenseMap<MachineBasicBlock *, VisitedBBInfo>;
 
-    void getAnalysisUsage(AnalysisUsage &AU) const override {
-      AU.addRequired<ProfileSummaryInfoWrapperPass>();
-      AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
-      AU.addPreserved<LazyMachineBlockFrequencyInfoPass>();
-      MachineFunctionPass::getAnalysisUsage(AU);
-    }
+class PadShortFuncLegacy : public MachineFunctionPass {
+public:
+  static char ID;
+  PadShortFuncLegacy() : MachineFunctionPass(ID) {}
 
-    MachineFunctionProperties getRequiredProperties() const override {
-      return MachineFunctionProperties().setNoVRegs();
-    }
+  bool runOnMachineFunction(MachineFunction &MF) override;
 
-    StringRef getPassName() const override {
-      return "X86 Atom pad short functions";
-    }
+  MachineFunctionProperties getRequiredProperties() const override {
+    return MachineFunctionProperties().setNoVRegs();
+  }
+
+  StringRef getPassName() const override {
+    return "X86 Atom pad short functions";
+  }
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.addRequired<ProfileSummaryInfoWrapperPass>();
+    AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
+    AU.addPreserved<LazyMachineBlockFrequencyInfoPass>();
+    MachineFunctionPass::getAnalysisUsage(AU);
+  }
+};
+} // end anonymous namespace
+
+char PadShortFuncLegacy::ID = 0;
+
+FunctionPass *llvm::createX86PadShortFunctionsLegacyPass() {
+  return new PadShortFuncLegacy();
+}
 
-  private:
-    void findReturns(MachineBasicBlock *MBB,
-                     unsigned int Cycles = 0);
+/// cyclesUntilReturn - return true if the MBB has a return instruction,
+/// and return false otherwise.
+/// Cycles will be incremented by the number of cycles taken to reach the
+/// return or the end of the BB, whichever occurs first.
+static bool cyclesUntilReturn(MachineBasicBlock *MBB, unsigned int &Cycles,
+                              const TargetSchedModel &TSM,
+                              VisitedBBsMap &VisitedBBs) {
+  // Return cached result if BB was previously visited
+  auto [It, Inserted] = VisitedBBs.try_emplace(MBB);
+  if (!Inserted) {
+    VisitedBBInfo BBInfo = It->second;
+    Cycles += BBInfo.Cycles;
+    return BBInfo.HasReturn;
+  }
 
-    bool cyclesUntilReturn(MachineBasicBlock *MBB,
-                           unsigned int &Cycles);
+  unsigned int CyclesToEnd = 0;
 
-    void addPadding(MachineBasicBlock *MBB,
-                    MachineBasicBlock::iterator &MBBI,
-                    unsigned int NOOPsToAdd);
+  for (MachineInstr &MI : *MBB) {
+    // Mark basic blocks with a return instruction. Calls to other
+    // functions do not count because the called function will be padded,
+    // if necessary.
+    if (MI.isReturn() && !MI.isCall()) {
+      It->second = VisitedBBInfo(true, CyclesToEnd);
+      Cycles += CyclesToEnd;
+      return true;
+    }
 
-    const unsigned int Threshold = 4;
+    CyclesToEnd += TSM.computeInstrLatency(&MI);
+  }
 
-    // ReturnBBs - Maps basic blocks that return to the minimum number of
-    // cycles until the return, starting from the entry block.
-    DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
+  It->second = VisitedBBInfo(false, CyclesToEnd);
+  Cycles += CyclesToEnd;
+  return false;
+}
 
-    // VisitedBBs - Cache of previously visited BBs.
-    DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
+/// findReturns - Starting at MBB, follow control flow and add all
+/// basic blocks that contain a return to ReturnBBs.
+static void findReturns(MachineBasicBlock *MBB, unsigned int Cycles,
+                        const unsigned int Threshold,
+                        const TargetSchedModel &TSM, ReturnBBsMap &ReturnBBs,
+                        VisitedBBsMap &VisitedBBs) {
+  // If this BB has a return, note how many cycles it takes to get there.
+  bool hasReturn = cyclesUntilReturn(MBB, Cycles, TSM, VisitedBBs);
+  if (Cycles >= Threshold)
+    return;
 
-    TargetSchedModel TSM;
-  };
+  if (hasReturn) {
+    unsigned int &NumCycles = ReturnBBs[MBB];
+    if (NumCycles == 0)
+      NumCycles = Cycles;
+    else
+      NumCycles = std::max(NumCycles, Cycles);
+    return;
+  }
 
-  char PadShortFunc::ID = 0;
+  // Follow branches in BB and look for returns
+  for (MachineBasicBlock *Succ : MBB->successors())
+    if (Succ != MBB)
+      findReturns(Succ, Cycles, Threshold, TSM, ReturnBBs, VisitedBBs);
 }
 
-FunctionPass *llvm::createX86PadShortFunctions() {
-  return new PadShortFunc();
+/// addPadding - Add the given number of NOOP instructions to the function
+/// just prior to the return at MBBI
+static void addPadding(MachineBasicBlock *MBB,
+                       MachineBasicBlock::iterator &MBBI,
+                       unsigned int NOOPsToAdd, const TargetSchedModel &TSM) {
+  const DebugLoc &DL = MBBI->getDebugLoc();
+  unsigned IssueWidth = TSM.getIssueWidth();
+
+  for (unsigned i = 0, e = IssueWidth * NOOPsToAdd; i != e; ++i)
+    BuildMI(*MBB, MBBI, DL, TSM.getInstrInfo()->get(X86::NOOP));
 }
 
-/// runOnMachineFunction - Loop over all of the basic blocks, inserting
-/// NOOP instructions before early exits.
-bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
+static bool runPadShortFunctions(MachineFunction &MF, ProfileSummaryInfo *PSI,
+                                 MachineBlockFrequencyInfo *MBFI) {
   LLVM_DEBUG(dbgs() << "Start X86PadShortFunctionPass\n";);
-  if (skipFunction(MF.getFunction()))
-    return false;
 
   if (MF.getFunction().hasOptSize())
     return false;
@@ -110,18 +166,15 @@ bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
   if (!MF.getSubtarget<X86Subtarget>().padShortFunctions())
     return false;
 
+  TargetSchedModel TSM;
   TSM.init(&MF.getSubtarget());
 
-  auto *PSI =
-      &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
-  auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
-               &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
-               nullptr;
-
   // Search through basic blocks and mark the ones that have early returns
-  ReturnBBs.clear();
-  VisitedBBs.clear();
-  findReturns(&MF.front());
+  ReturnBBsMap ReturnBBs;
+  VisitedBBsMap VisitedBBs;
+  const unsigned int Threshold = 4;
+
+  findReturns(&MF.front(), 0, Threshold, TSM, ReturnBBs, VisitedBBs);
 
   bool MadeChange = false;
 
@@ -145,7 +198,7 @@ bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
       assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
              "Basic block does not end with RET");
 
-      addPadding(MBB, ReturnLoc, Threshold - Cycles);
+      addPadding(MBB, ReturnLoc, Threshold - Cycles, TSM);
       NumBBsPadded++;
       MadeChange = true;
     }
@@ -154,68 +207,30 @@ bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
   return MadeChange;
 }
 
-/// findReturn - Starting at MBB, follow control flow and add all
-/// basic blocks that contain a return to ReturnBBs.
-void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
-  // If this BB has a return, note how many cycles it takes to get there.
-  bool hasReturn = cyclesUntilReturn(MBB, Cycles);
-  if (Cycles >= Threshold)
-    return;
-
-  if (hasReturn) {
-    unsigned int &NumCycles = ReturnBBs[MBB];
-    NumCycles = std::max(NumCycles, Cycles);
-    return;
-  }
-
-  // Follow branches in BB and look for returns
-  for (MachineBasicBlock *Succ : MBB->successors())
-    if (Succ != MBB)
-      findReturns(Succ, Cycles);
-}
-
-/// cyclesUntilReturn - return true if the MBB has a return instruction,
-/// and return false otherwise.
-/// Cycles will be incremented by the number of cycles taken to reach the
-/// return or the end of the BB, whichever occurs first.
-bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
-                                     unsigned int &Cycles) {
-  // Return cached result if BB was previously visited
-  auto [It, Inserted] = VisitedBBs.try_emplace(MBB);
-  if (!Inserted) {
-    VisitedBBInfo BBInfo = It->second;
-    Cycles += BBInfo.Cycles;
-    return BBInfo.HasReturn;
-  }
-
-  unsigned int CyclesToEnd = 0;
-
-  for (MachineInstr &MI : *MBB) {
-    // Mark basic blocks with a return instruction. Calls to other
-    // functions do not count because the called function will be padded,
-    // if necessary.
-    if (MI.isReturn() && !MI.isCall()) {
-      It->second = VisitedBBInfo(true, CyclesToEnd);
-      Cycles += CyclesToEnd;
-      return true;
-    }
+bool PadShortFuncLegacy::runOnMachineFunction(MachineFunction &MF) {
+  if (skipFunction(MF.getFunction()))
+    return false;
 
-    CyclesToEnd += TSM.computeInstrLatency(&MI);
-  }
+  auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
+  auto *MBFI = (PSI && PSI->hasProfileSummary())
+                   ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
+                   : nullptr;
 
-  It->second = VisitedBBInfo(false, CyclesToEnd);
-  Cycles += CyclesToEnd;
-  return false;
+  return runPadShortFunctions(MF, PSI, MBFI);
 }
 
-/// addPadding - Add the given number of NOOP instructions to the function
-/// just prior to the return at MBBI
-void PadShortFunc::addPadding(MachineBasicBlock *MBB,
-                              MachineBasicBlock::iterator &MBBI,
-                              unsigned int NOOPsToAdd) {
-  const DebugLoc &DL = MBBI->getDebugLoc();
-  unsigned IssueWidth = TSM.getIssueWidth();
-
-  for (unsigned i = 0, e = IssueWidth * NOOPsToAdd; i != e; ++i)
-    BuildMI(*MBB, MBBI, DL, TSM.getInstrInfo()->get(X86::NOOP));
+PreservedAnalyses
+X86PadShortFunctionsPass::run(MachineFunction &MF,
+                              MachineFunctionAnalysisManager &MFAM) {
+  auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
+                  .getCachedResult<ProfileSummaryAnalysis>(
+                      *MF.getFunction().getParent());
+  auto *MBFI = (PSI && PSI->hasProfileSummary())
+                   ? &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF)
+                   : nullptr;
+
+  return runPadShortFunctions(MF, PSI, MBFI)
+             ? getMachineFunctionPassPreservedAnalyses()
+                   .preserveSet<CFGAnalyses>()
+             : PreservedAnalyses::all();
 }
diff --git a/llvm/lib/Target/X86/X86PassRegistry.def b/llvm/lib/Target/X86/X86PassRegistry.def
index be85b7cf216c8..a6d95eaaaa783 100644
--- a/llvm/lib/Target/X86/X86PassRegistry.def
+++ b/llvm/lib/Target/X86/X86PassRegistry.def
@@ -56,6 +56,7 @@ MACHINE_FUNCTION_PASS("x86-lower-tile-copy", X86LowerTileCopyPass())
 MACHINE_FUNCTION_PASS("x86-lvi-load", X86LoadValueInjectionLoadHardeningPass())
 MACHINE_FUNCTION_PASS("x86-lvi-ret", X86LoadValueInjectionRetHardeningPass())
 MACHINE_FUNCTION_PASS("x86-optimize-leas", X86OptimizeLEAsPass())
+MACHINE_FUNCTION_PASS("x86-pad-short-functions", X86PadShortFunctionsPass())
 MACHINE_FUNCTION_PASS("x86-postlegalizer-combiner-pass", X86PostLegalizerCombinerPass())
 MACHINE_FUNCTION_PASS("x86-pre-tile-config", X86PreTileConfigPass())
 MACHINE_FUNCTION_PASS("x86-return-thunks", X86ReturnThunksPass())
@@ -72,5 +73,4 @@ MACHINE_FUNCTION_PASS("x86-wineh-unwindv2", X86WinEHUnwindV2Pass())
 DUMMY_MACHINE_FUNCTION_PASS("x86-execution-domain-fix", X86ExecutionDomainFix())
 DUMMY_MACHINE_FUNCTION_PASS("x86-indirect-thunks", X86IndirectThunks())
 DUMMY_MACHINE_FUNCTION_PASS("x86-issue-vzero-upper", X86IssueVZeroUpperPass())
-DUMMY_MACHINE_FUNCTION_PASS("x86-pad-short-functions", X86PadShortFunctionsPass())
 #undef DUMMY_MACHINE_FUNCTION_PASS
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index bb7e4d4396305..e909c2ac614c5 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -573,7 +573,7 @@ void X86PassConfig::addPreEmitPass() {
 
   if (getOptLevel() != CodeGenOptLevel::None) {
     addPass(createX86FixupBWInstsLegacyPass());
-    addPass(createX86PadShortFunctions());
+    addPass(createX86PadShortFunctionsLegacyPass());
     addPass(createX86FixupLEAsLegacyPass());
     addPass(createX86FixupInstTuningLegacyPass());
     addPass(createX86FixupVectorConstantsLegacyPass());
diff --git a/llvm/test/CodeGen/X86/llc-pipeline-npm.ll b/llvm/test/CodeGen/X86/llc-pipeline-npm.ll
index e392cf7408301..5517c10fb5172 100644
--- a/llvm/test/CodeGen/X86/llc-pipeline-npm.ll
+++ b/llvm/test/CodeGen/X86/llc-pipeline-npm.ll
@@ -173,6 +173,7 @@
 ; O2-NEXT: BreakFalseDepsPass
 ; O2-NEXT: x86-indirect-branch-tracking
 ; O2-NEXT: x86-fixup-bw-insts
+; O2-NEXT: x86-pad-short-functions
 ; O2-NEXT: x86-fixup-leas
 ; O2-NEXT: x86-fixup-inst-tuning
 ; O2-NEXT: x86-fixup-inst-tuning
@@ -360,6 +361,7 @@
 ; O3-WINDOWS-NEXT: BreakFalseDepsPass
 ; O3-WINDOWS-NEXT: x86-indirect-branch-tracking
 ; O3-WINDOWS-NEXT: x86-fixup-bw-insts
+; O3-WINDOWS-NEXT: x86-pad-short-functions
 ; O3-WINDOWS-NEXT: x86-fixup-leas
 ; O3-WINDOWS-NEXT: x86-fixup-inst-tuning
 ; O3-WINDOWS-NEXT: x86-fixup-inst-tuning

>From 3c786a8a1fe0a0a8f372e1016361f4bcb1308664 Mon Sep 17 00:00:00 2001
From: Kyungtak Woo <kevinwkt at google.com>
Date: Mon, 20 Apr 2026 03:01:29 +0000
Subject: [PATCH 2/2] refactor for impl class

---
 llvm/lib/Target/X86/X86PadShortFunction.cpp | 59 ++++++++++++---------
 1 file changed, 34 insertions(+), 25 deletions(-)

diff --git a/llvm/lib/Target/X86/X86PadShortFunction.cpp b/llvm/lib/Target/X86/X86PadShortFunction.cpp
index d8436881553dd..b2266abfdd3d7 100644
--- a/llvm/lib/Target/X86/X86PadShortFunction.cpp
+++ b/llvm/lib/Target/X86/X86PadShortFunction.cpp
@@ -12,7 +12,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-
 #include "X86.h"
 #include "X86InstrInfo.h"
 #include "X86Subtarget.h"
@@ -53,6 +52,22 @@ using ReturnBBsMap = DenseMap<MachineBasicBlock *, unsigned int>;
 // Cache of previously visited BBs.
 using VisitedBBsMap = DenseMap<MachineBasicBlock *, VisitedBBInfo>;
 
+class X86PadShortFunctionsImpl {
+  ReturnBBsMap ReturnBBs;
+  VisitedBBsMap VisitedBBs;
+  const unsigned int Threshold = 4;
+  TargetSchedModel TSM;
+
+  bool cyclesUntilReturn(MachineBasicBlock *MBB, unsigned int &Cycles);
+  void findReturns(MachineBasicBlock *MBB, unsigned int Cycles);
+  void addPadding(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
+                  unsigned int NOOPsToAdd);
+
+public:
+  bool runOnMachineFunction(MachineFunction &MF, ProfileSummaryInfo *PSI,
+                            MachineBlockFrequencyInfo *MBFI);
+};
+
 class PadShortFuncLegacy : public MachineFunctionPass {
 public:
   static char ID;
@@ -87,9 +102,8 @@ FunctionPass *llvm::createX86PadShortFunctionsLegacyPass() {
 /// and return false otherwise.
 /// Cycles will be incremented by the number of cycles taken to reach the
 /// return or the end of the BB, whichever occurs first.
-static bool cyclesUntilReturn(MachineBasicBlock *MBB, unsigned int &Cycles,
-                              const TargetSchedModel &TSM,
-                              VisitedBBsMap &VisitedBBs) {
+bool X86PadShortFunctionsImpl::cyclesUntilReturn(MachineBasicBlock *MBB,
+                                                 unsigned int &Cycles) {
   // Return cached result if BB was previously visited
   auto [It, Inserted] = VisitedBBs.try_emplace(MBB);
   if (!Inserted) {
@@ -120,12 +134,10 @@ static bool cyclesUntilReturn(MachineBasicBlock *MBB, unsigned int &Cycles,
 
 /// findReturns - Starting at MBB, follow control flow and add all
 /// basic blocks that contain a return to ReturnBBs.
-static void findReturns(MachineBasicBlock *MBB, unsigned int Cycles,
-                        const unsigned int Threshold,
-                        const TargetSchedModel &TSM, ReturnBBsMap &ReturnBBs,
-                        VisitedBBsMap &VisitedBBs) {
+void X86PadShortFunctionsImpl::findReturns(MachineBasicBlock *MBB,
+                                           unsigned int Cycles) {
   // If this BB has a return, note how many cycles it takes to get there.
-  bool hasReturn = cyclesUntilReturn(MBB, Cycles, TSM, VisitedBBs);
+  bool hasReturn = cyclesUntilReturn(MBB, Cycles);
   if (Cycles >= Threshold)
     return;
 
@@ -141,14 +153,14 @@ static void findReturns(MachineBasicBlock *MBB, unsigned int Cycles,
   // Follow branches in BB and look for returns
   for (MachineBasicBlock *Succ : MBB->successors())
     if (Succ != MBB)
-      findReturns(Succ, Cycles, Threshold, TSM, ReturnBBs, VisitedBBs);
+      findReturns(Succ, Cycles);
 }
 
 /// addPadding - Add the given number of NOOP instructions to the function
 /// just prior to the return at MBBI
-static void addPadding(MachineBasicBlock *MBB,
-                       MachineBasicBlock::iterator &MBBI,
-                       unsigned int NOOPsToAdd, const TargetSchedModel &TSM) {
+void X86PadShortFunctionsImpl::addPadding(MachineBasicBlock *MBB,
+                                          MachineBasicBlock::iterator &MBBI,
+                                          unsigned int NOOPsToAdd) {
   const DebugLoc &DL = MBBI->getDebugLoc();
   unsigned IssueWidth = TSM.getIssueWidth();
 
@@ -156,8 +168,9 @@ static void addPadding(MachineBasicBlock *MBB,
     BuildMI(*MBB, MBBI, DL, TSM.getInstrInfo()->get(X86::NOOP));
 }
 
-static bool runPadShortFunctions(MachineFunction &MF, ProfileSummaryInfo *PSI,
-                                 MachineBlockFrequencyInfo *MBFI) {
+bool X86PadShortFunctionsImpl::runOnMachineFunction(
+    MachineFunction &MF, ProfileSummaryInfo *PSI,
+    MachineBlockFrequencyInfo *MBFI) {
   LLVM_DEBUG(dbgs() << "Start X86PadShortFunctionPass\n";);
 
   if (MF.getFunction().hasOptSize())
@@ -166,15 +179,9 @@ static bool runPadShortFunctions(MachineFunction &MF, ProfileSummaryInfo *PSI,
   if (!MF.getSubtarget<X86Subtarget>().padShortFunctions())
     return false;
 
-  TargetSchedModel TSM;
   TSM.init(&MF.getSubtarget());
 
-  // Search through basic blocks and mark the ones that have early returns
-  ReturnBBsMap ReturnBBs;
-  VisitedBBsMap VisitedBBs;
-  const unsigned int Threshold = 4;
-
-  findReturns(&MF.front(), 0, Threshold, TSM, ReturnBBs, VisitedBBs);
+  findReturns(&MF.front(), 0);
 
   bool MadeChange = false;
 
@@ -198,7 +205,7 @@ static bool runPadShortFunctions(MachineFunction &MF, ProfileSummaryInfo *PSI,
       assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
              "Basic block does not end with RET");
 
-      addPadding(MBB, ReturnLoc, Threshold - Cycles, TSM);
+      addPadding(MBB, ReturnLoc, Threshold - Cycles);
       NumBBsPadded++;
       MadeChange = true;
     }
@@ -216,7 +223,8 @@ bool PadShortFuncLegacy::runOnMachineFunction(MachineFunction &MF) {
                    ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
                    : nullptr;
 
-  return runPadShortFunctions(MF, PSI, MBFI);
+  X86PadShortFunctionsImpl Impl;
+  return Impl.runOnMachineFunction(MF, PSI, MBFI);
 }
 
 PreservedAnalyses
@@ -229,7 +237,8 @@ X86PadShortFunctionsPass::run(MachineFunction &MF,
                    ? &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF)
                    : nullptr;
 
-  return runPadShortFunctions(MF, PSI, MBFI)
+  X86PadShortFunctionsImpl Impl;
+  return Impl.runOnMachineFunction(MF, PSI, MBFI)
              ? getMachineFunctionPassPreservedAnalyses()
                    .preserveSet<CFGAnalyses>()
              : PreservedAnalyses::all();



More information about the llvm-commits mailing list