4747//
4848// ===----------------------------------------------------------------------===//
4949
50+ #include " llvm/CodeGen/ShrinkWrap.h"
5051#include " llvm/ADT/BitVector.h"
5152#include " llvm/ADT/PostOrderIterator.h"
5253#include " llvm/ADT/SetVector.h"
@@ -110,7 +111,7 @@ namespace {
110111// / does not rely on expensive data-flow analysis. Instead we use the
111112// / dominance properties and loop information to decide which point
112113// / are safe for such insertion.
113- class ShrinkWrap : public MachineFunctionPass {
114+ class ShrinkWrapImpl {
114115 // / Hold callee-saved information.
115116 RegisterClassInfo RCI;
116117 MachineDominatorTree *MDT = nullptr ;
@@ -224,13 +225,8 @@ class ShrinkWrap : public MachineFunctionPass {
224225 // / Initialize the pass for \p MF.
225226 void init (MachineFunction &MF) {
226227 RCI.runOnMachineFunction (MF);
227- MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree ();
228- MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree ();
229228 Save = nullptr ;
230229 Restore = nullptr ;
231- MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
232- MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
233- ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE ();
234230 EntryFreq = MBFI->getEntryFreq ();
235231 const TargetSubtargetInfo &Subtarget = MF.getSubtarget ();
236232 const TargetInstrInfo &TII = *Subtarget.getInstrInfo ();
@@ -248,14 +244,24 @@ class ShrinkWrap : public MachineFunctionPass {
248244 // / shrink-wrapping.
249245 bool ArePointsInteresting () const { return Save != Entry && Save && Restore; }
250246
247+ public:
248+ ShrinkWrapImpl (MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT,
249+ MachineBlockFrequencyInfo *MBFI, MachineLoopInfo *MLI,
250+ MachineOptimizationRemarkEmitter *ORE)
251+ : MDT(MDT), MPDT(MPDT), MBFI(MBFI), MLI(MLI), ORE(ORE) {}
252+
251253 // / Check if shrink wrapping is enabled for this target and function.
252254 static bool isShrinkWrapEnabled (const MachineFunction &MF);
253255
256+ bool run (MachineFunction &MF);
257+ };
258+
259+ class ShrinkWrapLegacy : public MachineFunctionPass {
254260public:
255261 static char ID;
256262
257- ShrinkWrap () : MachineFunctionPass(ID) {
258- initializeShrinkWrapPass (*PassRegistry::getPassRegistry ());
263+ ShrinkWrapLegacy () : MachineFunctionPass(ID) {
264+ initializeShrinkWrapLegacyPass (*PassRegistry::getPassRegistry ());
259265 }
260266
261267 void getAnalysisUsage (AnalysisUsage &AU) const override {
@@ -282,20 +288,22 @@ class ShrinkWrap : public MachineFunctionPass {
282288
283289} // end anonymous namespace
284290
285- char ShrinkWrap ::ID = 0 ;
291+ char ShrinkWrapLegacy ::ID = 0 ;
286292
287- char &llvm::ShrinkWrapID = ShrinkWrap ::ID;
293+ char &llvm::ShrinkWrapID = ShrinkWrapLegacy ::ID;
288294
289- INITIALIZE_PASS_BEGIN (ShrinkWrap, DEBUG_TYPE, " Shrink Wrap Pass" , false , false )
295+ INITIALIZE_PASS_BEGIN (ShrinkWrapLegacy, DEBUG_TYPE, " Shrink Wrap Pass" , false ,
296+ false )
290297INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass)
291298INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
292299INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass)
293300INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
294301INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
295- INITIALIZE_PASS_END(ShrinkWrap, DEBUG_TYPE, " Shrink Wrap Pass" , false , false )
302+ INITIALIZE_PASS_END(ShrinkWrapLegacy, DEBUG_TYPE, " Shrink Wrap Pass" , false ,
303+ false )
296304
297- bool ShrinkWrap ::useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS,
298- bool StackAddressUsed) const {
305+ bool ShrinkWrapImpl ::useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS,
306+ bool StackAddressUsed) const {
299307 // / Check if \p Op is known to access an address not on the function's stack .
300308 // / At the moment, accesses where the underlying object is a global, function
301309 // / argument, or jump table are considered non-stack accesses. Note that the
@@ -549,7 +557,7 @@ static void rollbackRestoreSplit(MachineFunction &MF, MachineBasicBlock *NMBB,
549557// A block is deemed fit for restore point split iff there exist
550558// 1. DirtyPreds - preds of CurRestore reachable from use or def of CSR/FI
551559// 2. CleanPreds - preds of CurRestore that arent DirtyPreds
552- bool ShrinkWrap ::checkIfRestoreSplittable (
560+ bool ShrinkWrapImpl ::checkIfRestoreSplittable (
553561 const MachineBasicBlock *CurRestore,
554562 const DenseSet<const MachineBasicBlock *> &ReachableByDirty,
555563 SmallVectorImpl<MachineBasicBlock *> &DirtyPreds,
@@ -572,8 +580,8 @@ bool ShrinkWrap::checkIfRestoreSplittable(
572580 return !(CleanPreds.empty () || DirtyPreds.empty ());
573581}
574582
575- bool ShrinkWrap ::postShrinkWrapping (bool HasCandidate, MachineFunction &MF,
576- RegScavenger *RS) {
583+ bool ShrinkWrapImpl ::postShrinkWrapping (bool HasCandidate, MachineFunction &MF,
584+ RegScavenger *RS) {
577585 if (!EnablePostShrinkWrapOpt)
578586 return false ;
579587
@@ -679,8 +687,8 @@ bool ShrinkWrap::postShrinkWrapping(bool HasCandidate, MachineFunction &MF,
679687 return true ;
680688}
681689
682- void ShrinkWrap ::updateSaveRestorePoints (MachineBasicBlock &MBB,
683- RegScavenger *RS) {
690+ void ShrinkWrapImpl ::updateSaveRestorePoints (MachineBasicBlock &MBB,
691+ RegScavenger *RS) {
684692 // Get rid of the easy cases first.
685693 if (!Save)
686694 Save = &MBB;
@@ -810,7 +818,7 @@ static bool giveUpWithRemarks(MachineOptimizationRemarkEmitter *ORE,
810818 return false ;
811819}
812820
813- bool ShrinkWrap ::performShrinkWrapping (
821+ bool ShrinkWrapImpl ::performShrinkWrapping (
814822 const ReversePostOrderTraversal<MachineBasicBlock *> &RPOT,
815823 RegScavenger *RS) {
816824 for (MachineBasicBlock *MBB : RPOT) {
@@ -919,10 +927,7 @@ bool ShrinkWrap::performShrinkWrapping(
919927 return true ;
920928}
921929
922- bool ShrinkWrap::runOnMachineFunction (MachineFunction &MF) {
923- if (skipFunction (MF.getFunction ()) || MF.empty () || !isShrinkWrapEnabled (MF))
924- return false ;
925-
930+ bool ShrinkWrapImpl::run (MachineFunction &MF) {
926931 LLVM_DEBUG (dbgs () << " **** Analysing " << MF.getName () << ' \n ' );
927932
928933 init (MF);
@@ -969,7 +974,44 @@ bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
969974 return Changed;
970975}
971976
972- bool ShrinkWrap::isShrinkWrapEnabled (const MachineFunction &MF) {
977+ bool ShrinkWrapLegacy::runOnMachineFunction (MachineFunction &MF) {
978+ if (skipFunction (MF.getFunction ()) || MF.empty () ||
979+ !ShrinkWrapImpl::isShrinkWrapEnabled (MF))
980+ return false ;
981+
982+ MachineDominatorTree *MDT =
983+ &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree ();
984+ MachinePostDominatorTree *MPDT =
985+ &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree ();
986+ MachineBlockFrequencyInfo *MBFI =
987+ &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
988+ MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
989+ MachineOptimizationRemarkEmitter *ORE =
990+ &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE ();
991+
992+ return ShrinkWrapImpl (MDT, MPDT, MBFI, MLI, ORE).run (MF);
993+ }
994+
995+ PreservedAnalyses ShrinkWrapPass::run (MachineFunction &MF,
996+ MachineFunctionAnalysisManager &MFAM) {
997+ MFPropsModifier _ (*this , MF);
998+ if (MF.empty () || !ShrinkWrapImpl::isShrinkWrapEnabled (MF))
999+ return PreservedAnalyses::all ();
1000+
1001+ MachineDominatorTree &MDT = MFAM.getResult <MachineDominatorTreeAnalysis>(MF);
1002+ MachinePostDominatorTree &MPDT =
1003+ MFAM.getResult <MachinePostDominatorTreeAnalysis>(MF);
1004+ MachineBlockFrequencyInfo &MBFI =
1005+ MFAM.getResult <MachineBlockFrequencyAnalysis>(MF);
1006+ MachineLoopInfo &MLI = MFAM.getResult <MachineLoopAnalysis>(MF);
1007+ MachineOptimizationRemarkEmitter &ORE =
1008+ MFAM.getResult <MachineOptimizationRemarkEmitterAnalysis>(MF);
1009+
1010+ ShrinkWrapImpl (&MDT, &MPDT, &MBFI, &MLI, &ORE).run (MF);
1011+ return PreservedAnalyses::all ();
1012+ }
1013+
1014+ bool ShrinkWrapImpl::isShrinkWrapEnabled (const MachineFunction &MF) {
9731015 const TargetFrameLowering *TFI = MF.getSubtarget ().getFrameLowering ();
9741016
9751017 switch (EnableShrinkWrapOpt) {
0 commit comments