Skip to content

Commit 86d8e8d

Browse files
authored
[CodeGen][NewPM] Port "PrologEpilogInserter" to NPM (llvm#130550)
1 parent a9b6185 commit 86d8e8d

10 files changed

+103
-43
lines changed

llvm/include/llvm/CodeGen/PEI.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- llvm/CodeGen/PEI.h ---------------------------------------*- C++ -*-===//
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+
#ifndef LLVM_CODEGEN_PEI_H
10+
#define LLVM_CODEGEN_PEI_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class PrologEpilogInserterPass
17+
: public PassInfoMixin<PrologEpilogInserterPass> {
18+
public:
19+
PreservedAnalyses run(MachineFunction &MF,
20+
MachineFunctionAnalysisManager &MFAM);
21+
22+
static bool isRequired() { return true; }
23+
};
24+
25+
} // namespace llvm
26+
27+
#endif // LLVM_CODEGEN_PEI_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void initializeNaryReassociateLegacyPassPass(PassRegistry &);
225225
void initializeObjCARCContractLegacyPassPass(PassRegistry &);
226226
void initializeOptimizationRemarkEmitterWrapperPassPass(PassRegistry &);
227227
void initializeOptimizePHIsLegacyPass(PassRegistry &);
228-
void initializePEIPass(PassRegistry &);
228+
void initializePEILegacyPass(PassRegistry &);
229229
void initializePHIEliminationPass(PassRegistry &);
230230
void initializePartiallyInlineLibCallsLegacyPassPass(PassRegistry &);
231231
void initializePatchableFunctionLegacyPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "llvm/CodeGen/MachineSink.h"
6464
#include "llvm/CodeGen/MachineVerifier.h"
6565
#include "llvm/CodeGen/OptimizePHIs.h"
66+
#include "llvm/CodeGen/PEI.h"
6667
#include "llvm/CodeGen/PHIElimination.h"
6768
#include "llvm/CodeGen/PatchableFunction.h"
6869
#include "llvm/CodeGen/PeepholeOptimizer.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ MACHINE_FUNCTION_PASS("print<machine-post-dom-tree>",
180180
MachinePostDominatorTreePrinterPass(errs()))
181181
MACHINE_FUNCTION_PASS("print<slot-indexes>", SlotIndexesPrinterPass(errs()))
182182
MACHINE_FUNCTION_PASS("print<virtregmap>", VirtRegMapPrinterPass(errs()))
183+
MACHINE_FUNCTION_PASS("prolog-epilog", PrologEpilogInserterPass())
183184
MACHINE_FUNCTION_PASS("reg-usage-collector", RegUsageInfoCollectorPass())
184185
MACHINE_FUNCTION_PASS("reg-usage-propagation", RegUsageInfoPropagationPass())
185186
MACHINE_FUNCTION_PASS("register-coalescer", RegisterCoalescerPass())
@@ -308,7 +309,6 @@ DUMMY_MACHINE_FUNCTION_PASS("mirfs-discriminators", MIRAddFSDiscriminatorsPass)
308309
DUMMY_MACHINE_FUNCTION_PASS("postra-machine-sink", PostRAMachineSinkingPass)
309310
DUMMY_MACHINE_FUNCTION_PASS("print-machine-uniformity", MachineUniformityInfoPrinterPass)
310311
DUMMY_MACHINE_FUNCTION_PASS("processimpdefs", ProcessImplicitDefsPass)
311-
DUMMY_MACHINE_FUNCTION_PASS("prologepilog", PrologEpilogInserterPass)
312312
DUMMY_MACHINE_FUNCTION_PASS("prologepilog-code", PrologEpilogCodeInserterPass)
313313
DUMMY_MACHINE_FUNCTION_PASS("ra-basic", RABasicPass)
314314
DUMMY_MACHINE_FUNCTION_PASS("ra-pbqp", RAPBQPPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
101101
initializeMachineVerifierLegacyPassPass(Registry);
102102
initializeObjCARCContractLegacyPassPass(Registry);
103103
initializeOptimizePHIsLegacyPass(Registry);
104-
initializePEIPass(Registry);
104+
initializePEILegacyPass(Registry);
105105
initializePHIEliminationPass(Registry);
106106
initializePatchableFunctionLegacyPass(Registry);
107107
initializePeepholeOptimizerLegacyPass(Registry);

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/CodeGen/MachineOperand.h"
3737
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
3838
#include "llvm/CodeGen/MachineRegisterInfo.h"
39+
#include "llvm/CodeGen/PEI.h"
3940
#include "llvm/CodeGen/RegisterScavenging.h"
4041
#include "llvm/CodeGen/TargetFrameLowering.h"
4142
#include "llvm/CodeGen/TargetInstrInfo.h"
@@ -77,21 +78,7 @@ STATISTIC(NumFuncSeen, "Number of functions seen in PEI");
7778

7879
namespace {
7980

80-
class PEI : public MachineFunctionPass {
81-
public:
82-
static char ID;
83-
84-
PEI() : MachineFunctionPass(ID) {
85-
initializePEIPass(*PassRegistry::getPassRegistry());
86-
}
87-
88-
void getAnalysisUsage(AnalysisUsage &AU) const override;
89-
90-
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
91-
/// frame indexes with appropriate references.
92-
bool runOnMachineFunction(MachineFunction &MF) override;
93-
94-
private:
81+
class PEIImpl {
9582
RegScavenger *RS = nullptr;
9683

9784
// MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
@@ -137,31 +124,50 @@ class PEI : public MachineFunctionPass {
137124

138125
void insertPrologEpilogCode(MachineFunction &MF);
139126
void insertZeroCallUsedRegs(MachineFunction &MF);
127+
128+
public:
129+
PEIImpl(MachineOptimizationRemarkEmitter *ORE) : ORE(ORE) {}
130+
bool run(MachineFunction &MF);
131+
};
132+
133+
class PEILegacy : public MachineFunctionPass {
134+
public:
135+
static char ID;
136+
137+
PEILegacy() : MachineFunctionPass(ID) {
138+
initializePEILegacyPass(*PassRegistry::getPassRegistry());
139+
}
140+
141+
void getAnalysisUsage(AnalysisUsage &AU) const override;
142+
143+
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
144+
/// frame indexes with appropriate references.
145+
bool runOnMachineFunction(MachineFunction &MF) override;
140146
};
141147

142148
} // end anonymous namespace
143149

144-
char PEI::ID = 0;
150+
char PEILegacy::ID = 0;
145151

146-
char &llvm::PrologEpilogCodeInserterID = PEI::ID;
152+
char &llvm::PrologEpilogCodeInserterID = PEILegacy::ID;
147153

148-
INITIALIZE_PASS_BEGIN(PEI, DEBUG_TYPE, "Prologue/Epilogue Insertion", false,
149-
false)
154+
INITIALIZE_PASS_BEGIN(PEILegacy, DEBUG_TYPE, "Prologue/Epilogue Insertion",
155+
false, false)
150156
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
151157
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
152158
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
153-
INITIALIZE_PASS_END(PEI, DEBUG_TYPE,
159+
INITIALIZE_PASS_END(PEILegacy, DEBUG_TYPE,
154160
"Prologue/Epilogue Insertion & Frame Finalization", false,
155161
false)
156162

157163
MachineFunctionPass *llvm::createPrologEpilogInserterPass() {
158-
return new PEI();
164+
return new PEILegacy();
159165
}
160166

161167
STATISTIC(NumBytesStackSpace,
162168
"Number of bytes used for stack in all functions");
163169

164-
void PEI::getAnalysisUsage(AnalysisUsage &AU) const {
170+
void PEILegacy::getAnalysisUsage(AnalysisUsage &AU) const {
165171
AU.setPreservesCFG();
166172
AU.addPreserved<MachineLoopInfoWrapperPass>();
167173
AU.addPreserved<MachineDominatorTreeWrapperPass>();
@@ -213,17 +219,14 @@ static void stashEntryDbgValues(MachineBasicBlock &MBB,
213219
MI->removeFromParent();
214220
}
215221

216-
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
217-
/// frame indexes with appropriate references.
218-
bool PEI::runOnMachineFunction(MachineFunction &MF) {
222+
bool PEIImpl::run(MachineFunction &MF) {
219223
NumFuncSeen++;
220224
const Function &F = MF.getFunction();
221225
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
222226
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
223227

224228
RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr;
225229
FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(MF);
226-
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
227230

228231
// Spill frame pointer and/or base pointer registers if they are clobbered.
229232
// It is placed before call frame instruction elimination so it will not mess
@@ -354,9 +357,31 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) {
354357
return true;
355358
}
356359

360+
/// runOnMachineFunction - Insert prolog/epilog code and replace abstract
361+
/// frame indexes with appropriate references.
362+
bool PEILegacy::runOnMachineFunction(MachineFunction &MF) {
363+
MachineOptimizationRemarkEmitter *ORE =
364+
&getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
365+
return PEIImpl(ORE).run(MF);
366+
}
367+
368+
PreservedAnalyses
369+
PrologEpilogInserterPass::run(MachineFunction &MF,
370+
MachineFunctionAnalysisManager &MFAM) {
371+
MachineOptimizationRemarkEmitter &ORE =
372+
MFAM.getResult<MachineOptimizationRemarkEmitterAnalysis>(MF);
373+
if (!PEIImpl(&ORE).run(MF))
374+
return PreservedAnalyses::all();
375+
376+
return getMachineFunctionPassPreservedAnalyses()
377+
.preserveSet<CFGAnalyses>()
378+
.preserve<MachineDominatorTreeAnalysis>()
379+
.preserve<MachineLoopAnalysis>();
380+
}
381+
357382
/// Calculate the MaxCallFrameSize variable for the function's frame
358383
/// information and eliminate call frame pseudo instructions.
359-
void PEI::calculateCallFrameInfo(MachineFunction &MF) {
384+
void PEIImpl::calculateCallFrameInfo(MachineFunction &MF) {
360385
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
361386
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
362387
MachineFrameInfo &MFI = MF.getFrameInfo();
@@ -397,7 +422,7 @@ void PEI::calculateCallFrameInfo(MachineFunction &MF) {
397422

398423
/// Compute the sets of entry and return blocks for saving and restoring
399424
/// callee-saved registers, and placing prolog and epilog code.
400-
void PEI::calculateSaveRestoreBlocks(MachineFunction &MF) {
425+
void PEIImpl::calculateSaveRestoreBlocks(MachineFunction &MF) {
401426
const MachineFrameInfo &MFI = MF.getFrameInfo();
402427

403428
// Even when we do not change any CSR, we still want to insert the
@@ -628,7 +653,7 @@ static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
628653
}
629654
}
630655

631-
void PEI::spillCalleeSavedRegs(MachineFunction &MF) {
656+
void PEIImpl::spillCalleeSavedRegs(MachineFunction &MF) {
632657
// We can't list this requirement in getRequiredProperties because some
633658
// targets (WebAssembly) use virtual registers past this point, and the pass
634659
// pipeline is set up without giving the passes a chance to look at the
@@ -820,7 +845,7 @@ static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs,
820845

821846
/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
822847
/// abstract stack objects.
823-
void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
848+
void PEIImpl::calculateFrameObjectOffsets(MachineFunction &MF) {
824849
const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
825850

826851
bool StackGrowsDown =
@@ -1135,7 +1160,7 @@ void PEI::calculateFrameObjectOffsets(MachineFunction &MF) {
11351160
/// insertPrologEpilogCode - Scan the function for modified callee saved
11361161
/// registers, insert spill code for these callee saved registers, then add
11371162
/// prolog and epilog code to the function.
1138-
void PEI::insertPrologEpilogCode(MachineFunction &MF) {
1163+
void PEIImpl::insertPrologEpilogCode(MachineFunction &MF) {
11391164
const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
11401165

11411166
// Add prologue to the function...
@@ -1172,7 +1197,7 @@ void PEI::insertPrologEpilogCode(MachineFunction &MF) {
11721197
}
11731198

11741199
/// insertZeroCallUsedRegs - Zero out call used registers.
1175-
void PEI::insertZeroCallUsedRegs(MachineFunction &MF) {
1200+
void PEIImpl::insertZeroCallUsedRegs(MachineFunction &MF) {
11761201
const Function &F = MF.getFunction();
11771202

11781203
if (!F.hasFnAttribute("zero-call-used-regs"))
@@ -1315,7 +1340,7 @@ void PEI::insertZeroCallUsedRegs(MachineFunction &MF) {
13151340

13161341
/// Replace all FrameIndex operands with physical register references and actual
13171342
/// offsets.
1318-
void PEI::replaceFrameIndicesBackward(MachineFunction &MF) {
1343+
void PEIImpl::replaceFrameIndicesBackward(MachineFunction &MF) {
13191344
const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
13201345

13211346
for (auto &MBB : MF) {
@@ -1343,7 +1368,7 @@ void PEI::replaceFrameIndicesBackward(MachineFunction &MF) {
13431368

13441369
/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
13451370
/// register references and actual offsets.
1346-
void PEI::replaceFrameIndices(MachineFunction &MF) {
1371+
void PEIImpl::replaceFrameIndices(MachineFunction &MF) {
13471372
const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
13481373

13491374
for (auto &MBB : MF) {
@@ -1359,8 +1384,8 @@ void PEI::replaceFrameIndices(MachineFunction &MF) {
13591384
}
13601385
}
13611386

1362-
bool PEI::replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI,
1363-
unsigned OpIdx, int SPAdj) {
1387+
bool PEIImpl::replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI,
1388+
unsigned OpIdx, int SPAdj) {
13641389
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
13651390
const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
13661391
if (MI.isDebugValue()) {
@@ -1441,8 +1466,8 @@ bool PEI::replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI,
14411466
return false;
14421467
}
14431468

1444-
void PEI::replaceFrameIndicesBackward(MachineBasicBlock *BB,
1445-
MachineFunction &MF, int &SPAdj) {
1469+
void PEIImpl::replaceFrameIndicesBackward(MachineBasicBlock *BB,
1470+
MachineFunction &MF, int &SPAdj) {
14461471
assert(MF.getSubtarget().getRegisterInfo() &&
14471472
"getRegisterInfo() must be implemented!");
14481473

@@ -1486,8 +1511,8 @@ void PEI::replaceFrameIndicesBackward(MachineBasicBlock *BB,
14861511
}
14871512
}
14881513

1489-
void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1490-
int &SPAdj) {
1514+
void PEIImpl::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1515+
int &SPAdj) {
14911516
assert(MF.getSubtarget().getRegisterInfo() &&
14921517
"getRegisterInfo() must be implemented!");
14931518
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
#include "llvm/CodeGen/MachineTraceMetrics.h"
136136
#include "llvm/CodeGen/MachineVerifier.h"
137137
#include "llvm/CodeGen/OptimizePHIs.h"
138+
#include "llvm/CodeGen/PEI.h"
138139
#include "llvm/CodeGen/PHIElimination.h"
139140
#include "llvm/CodeGen/PatchableFunction.h"
140141
#include "llvm/CodeGen/PeepholeOptimizer.h"

llvm/test/CodeGen/AArch64/aarch64-large-stack-spbump.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=aarch64 -run-pass=prologepilog %s -o - | FileCheck %s
2+
# RUN: llc -mtriple=aarch64 -passes='prolog-epilog' %s -o - | FileCheck %s
23
--- |
34
define i32 @_Z4funcv() {
45
entry:

llvm/test/CodeGen/AMDGPU/accvgpr-spill-scc-clobber.mir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -verify-machineinstrs -run-pass=prologepilog %s -o - | FileCheck -check-prefix=GFX908 %s
3+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -passes='prolog-epilog' %s -o - | FileCheck -check-prefix=GFX908 %s
34
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -verify-machineinstrs -run-pass=prologepilog %s -o - | FileCheck -check-prefix=GFX90A %s
5+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -passes='prolog-epilog' %s -o - | FileCheck -check-prefix=GFX90A %s
46
# RUN: llc -mattr=+enable-flat-scratch -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -verify-machineinstrs -run-pass=prologepilog %s -o - | FileCheck -check-prefix=GFX908-FLATSCR %s
7+
# RUN: llc -mattr=+enable-flat-scratch -mtriple=amdgcn-amd-amdhsa -mcpu=gfx908 -passes='prolog-epilog' %s -o - | FileCheck -check-prefix=GFX908-FLATSCR %s
58
# RUN: llc -mattr=+enable-flat-scratch -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -verify-machineinstrs -run-pass=prologepilog %s -o - | FileCheck -check-prefix=GFX90A-FLATSCR %s
9+
# RUN: llc -mattr=+enable-flat-scratch -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -passes='prolog-epilog' %s -o - | FileCheck -check-prefix=GFX90A-FLATSCR %s
610

711
---
812
name: agpr32_restore_clobber_scc

llvm/test/CodeGen/AMDGPU/agpr-copy-reuse-writes.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -mtriple=amdgcn -mcpu=gfx908 -run-pass=prologepilog,postrapseudos -verify-machineinstrs -o - %s | FileCheck -check-prefix=GFX908 %s
3+
# RUN: llc -mtriple=amdgcn -mcpu=gfx908 -passes='prolog-epilog,post-ra-pseudos' -o - %s | FileCheck -check-prefix=GFX908 %s
34

45
---
56
name: standard

0 commit comments

Comments
 (0)