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
7879namespace {
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 )
150156INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
151157INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
152158INITIALIZE_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
157163MachineFunctionPass *llvm::createPrologEpilogInserterPass() {
158- return new PEI ();
164+ return new PEILegacy ();
159165}
160166
161167STATISTIC (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 ();
0 commit comments