1212//
1313// ===----------------------------------------------------------------------===//
1414
15+ #include " llvm/CodeGen/PHIElimination.h"
1516#include " PHIEliminationUtils.h"
1617#include " llvm/ADT/DenseMap.h"
1718#include " llvm/ADT/SmallPtrSet.h"
@@ -64,22 +65,13 @@ static cl::opt<bool> NoPhiElimLiveOutEarlyExit(
6465
6566namespace {
6667
67- class PHIElimination : public MachineFunctionPass {
68+ class PHIEliminationImpl {
6869 MachineRegisterInfo *MRI = nullptr ; // Machine register information
6970 LiveVariables *LV = nullptr ;
7071 LiveIntervals *LIS = nullptr ;
72+ MachineLoopInfo *MLI = nullptr ;
73+ MachineDominatorTree *MDT = nullptr ;
7174
72- public:
73- static char ID; // Pass identification, replacement for typeid
74-
75- PHIElimination () : MachineFunctionPass(ID) {
76- initializePHIEliminationPass (*PassRegistry::getPassRegistry ());
77- }
78-
79- bool runOnMachineFunction (MachineFunction &MF) override ;
80- void getAnalysisUsage (AnalysisUsage &AU) const override ;
81-
82- private:
8375 // / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
8476 // / in predecessor basic blocks.
8577 bool EliminatePHINodes (MachineFunction &MF, MachineBasicBlock &MBB);
@@ -118,10 +110,71 @@ class PHIElimination : public MachineFunctionPass {
118110 using LoweredPHIMap =
119111 DenseMap<MachineInstr *, unsigned , MachineInstrExpressionTrait>;
120112 LoweredPHIMap LoweredPHIs;
113+
114+ MachineFunctionPass *P = nullptr ;
115+ MachineFunctionAnalysisManager *MFAM = nullptr ;
116+
117+ public:
118+ PHIEliminationImpl (MachineFunctionPass *P) : P(P) {
119+ auto *LVWrapper = P->getAnalysisIfAvailable <LiveVariablesWrapperPass>();
120+ auto *LISWrapper = P->getAnalysisIfAvailable <LiveIntervalsWrapperPass>();
121+ auto *MLIWrapper = P->getAnalysisIfAvailable <MachineLoopInfoWrapperPass>();
122+ auto *MDTWrapper =
123+ P->getAnalysisIfAvailable <MachineDominatorTreeWrapperPass>();
124+ LV = LVWrapper ? &LVWrapper->getLV () : nullptr ;
125+ LIS = LISWrapper ? &LISWrapper->getLIS () : nullptr ;
126+ MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
127+ MDT = MDTWrapper ? &MDTWrapper->getDomTree () : nullptr ;
128+ }
129+
130+ PHIEliminationImpl (MachineFunction &MF, MachineFunctionAnalysisManager &AM)
131+ : LV(AM.getCachedResult<LiveVariablesAnalysis>(MF)),
132+ LIS (AM.getCachedResult<LiveIntervalsAnalysis>(MF)),
133+ MLI(AM.getCachedResult<MachineLoopAnalysis>(MF)),
134+ MDT(AM.getCachedResult<MachineDominatorTreeAnalysis>(MF)), MFAM(&AM) {}
135+
136+ bool run (MachineFunction &MF);
137+ };
138+
139+ class PHIElimination : public MachineFunctionPass {
140+ public:
141+ static char ID; // Pass identification, replacement for typeid
142+
143+ PHIElimination () : MachineFunctionPass(ID) {
144+ initializePHIEliminationPass (*PassRegistry::getPassRegistry ());
145+ }
146+
147+ bool runOnMachineFunction (MachineFunction &MF) override {
148+ PHIEliminationImpl Impl (this );
149+ return Impl.run (MF);
150+ }
151+
152+ MachineFunctionProperties getSetProperties () const override {
153+ return MachineFunctionProperties ().set (
154+ MachineFunctionProperties::Property::NoPHIs);
155+ }
156+
157+ void getAnalysisUsage (AnalysisUsage &AU) const override ;
121158};
122159
123160} // end anonymous namespace
124161
162+ PreservedAnalyses
163+ PHIEliminationPass::run (MachineFunction &MF,
164+ MachineFunctionAnalysisManager &MFAM) {
165+ PHIEliminationImpl Impl (MF, MFAM);
166+ bool Changed = Impl.run (MF);
167+ if (!Changed)
168+ return PreservedAnalyses::all ();
169+ auto PA = getMachineFunctionPassPreservedAnalyses ();
170+ PA.preserve <LiveIntervalsAnalysis>();
171+ PA.preserve <LiveVariablesAnalysis>();
172+ PA.preserve <SlotIndexesAnalysis>();
173+ PA.preserve <MachineDominatorTreeAnalysis>();
174+ PA.preserve <MachineLoopAnalysis>();
175+ return PA;
176+ }
177+
125178STATISTIC (NumLowered, " Number of phis lowered" );
126179STATISTIC (NumCriticalEdgesSplit, " Number of critical edges split" );
127180STATISTIC (NumReused, " Number of reused lowered phis" );
@@ -147,12 +200,8 @@ void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
147200 MachineFunctionPass::getAnalysisUsage (AU);
148201}
149202
150- bool PHIElimination::runOnMachineFunction (MachineFunction &MF) {
203+ bool PHIEliminationImpl::run (MachineFunction &MF) {
151204 MRI = &MF.getRegInfo ();
152- auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
153- LV = LVWrapper ? &LVWrapper->getLV () : nullptr ;
154- auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
155- LIS = LISWrapper ? &LISWrapper->getLIS () : nullptr ;
156205
157206 bool Changed = false ;
158207
@@ -187,9 +236,6 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
187236 }
188237 }
189238
190- MachineLoopInfoWrapperPass *MLIWrapper =
191- getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
192- MachineLoopInfo *MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
193239 for (auto &MBB : MF)
194240 Changed |= SplitPHIEdges (MF, MBB, MLI, (LV ? &LiveInSets : nullptr ));
195241 }
@@ -223,9 +269,8 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
223269 }
224270
225271 // TODO: we should use the incremental DomTree updater here.
226- if (Changed)
227- if (auto *MDT = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>())
228- MDT->getDomTree ().getBase ().recalculate (MF);
272+ if (Changed && MDT)
273+ MDT->getBase ().recalculate (MF);
229274
230275 LoweredPHIs.clear ();
231276 ImpDefs.clear ();
@@ -238,8 +283,8 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
238283
239284// / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
240285// / predecessor basic blocks.
241- bool PHIElimination ::EliminatePHINodes (MachineFunction &MF,
242- MachineBasicBlock &MBB) {
286+ bool PHIEliminationImpl ::EliminatePHINodes (MachineFunction &MF,
287+ MachineBasicBlock &MBB) {
243288 if (MBB.empty () || !MBB.front ().isPHI ())
244289 return false ; // Quick exit for basic blocks without PHIs.
245290
@@ -286,9 +331,9 @@ static bool allPhiOperandsUndefined(const MachineInstr &MPhi,
286331 return true ;
287332}
288333// / LowerPHINode - Lower the PHI node at the top of the specified block.
289- void PHIElimination ::LowerPHINode (MachineBasicBlock &MBB,
290- MachineBasicBlock::iterator LastPHIIt,
291- bool AllEdgesCritical) {
334+ void PHIEliminationImpl ::LowerPHINode (MachineBasicBlock &MBB,
335+ MachineBasicBlock::iterator LastPHIIt,
336+ bool AllEdgesCritical) {
292337 ++NumLowered;
293338
294339 MachineBasicBlock::iterator AfterPHIsIt = std::next (LastPHIIt);
@@ -689,7 +734,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
689734// / particular, we want to map the number of uses of a virtual register which is
690735// / used in a PHI node. We map that to the BB the vreg is coming from. This is
691736// / used later to determine when the vreg is killed in the BB.
692- void PHIElimination ::analyzePHINodes (const MachineFunction &MF) {
737+ void PHIEliminationImpl ::analyzePHINodes (const MachineFunction &MF) {
693738 for (const auto &MBB : MF) {
694739 for (const auto &BBI : MBB) {
695740 if (!BBI.isPHI ())
@@ -705,9 +750,9 @@ void PHIElimination::analyzePHINodes(const MachineFunction &MF) {
705750 }
706751}
707752
708- bool PHIElimination ::SplitPHIEdges (MachineFunction &MF, MachineBasicBlock &MBB,
709- MachineLoopInfo *MLI,
710- std::vector<SparseBitVector<>> *LiveInSets) {
753+ bool PHIEliminationImpl ::SplitPHIEdges (
754+ MachineFunction &MF, MachineBasicBlock &MBB, MachineLoopInfo *MLI,
755+ std::vector<SparseBitVector<>> *LiveInSets) {
711756 if (MBB.empty () || !MBB.front ().isPHI () || MBB.isEHPad ())
712757 return false ; // Quick exit for basic blocks without PHIs.
713758
@@ -774,7 +819,8 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
774819 }
775820 if (!ShouldSplit && !SplitAllCriticalEdges)
776821 continue ;
777- if (!PreMBB->SplitCriticalEdge (&MBB, *this , LiveInSets)) {
822+ if (!(P ? PreMBB->SplitCriticalEdge (&MBB, *P, LiveInSets)
823+ : PreMBB->SplitCriticalEdge (&MBB, *MFAM, LiveInSets))) {
778824 LLVM_DEBUG (dbgs () << " Failed to split critical edge.\n " );
779825 continue ;
780826 }
@@ -785,7 +831,7 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
785831 return Changed;
786832}
787833
788- bool PHIElimination ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
834+ bool PHIEliminationImpl ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
789835 assert ((LV || LIS) &&
790836 " isLiveIn() requires either LiveVariables or LiveIntervals" );
791837 if (LIS)
@@ -794,8 +840,8 @@ bool PHIElimination::isLiveIn(Register Reg, const MachineBasicBlock *MBB) {
794840 return LV->isLiveIn (Reg, *MBB);
795841}
796842
797- bool PHIElimination ::isLiveOutPastPHIs (Register Reg,
798- const MachineBasicBlock *MBB) {
843+ bool PHIEliminationImpl ::isLiveOutPastPHIs (Register Reg,
844+ const MachineBasicBlock *MBB) {
799845 assert ((LV || LIS) &&
800846 " isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals" );
801847 // LiveVariables considers uses in PHIs to be in the predecessor basic block,
0 commit comments