12
12
//
13
13
// ===----------------------------------------------------------------------===//
14
14
15
+ #include " llvm/CodeGen/PHIElimination.h"
15
16
#include " PHIEliminationUtils.h"
16
17
#include " llvm/ADT/DenseMap.h"
17
18
#include " llvm/ADT/SmallPtrSet.h"
@@ -64,22 +65,13 @@ static cl::opt<bool> NoPhiElimLiveOutEarlyExit(
64
65
65
66
namespace {
66
67
67
- class PHIElimination : public MachineFunctionPass {
68
+ class PHIEliminationImpl {
68
69
MachineRegisterInfo *MRI = nullptr ; // Machine register information
69
70
LiveVariables *LV = nullptr ;
70
71
LiveIntervals *LIS = nullptr ;
72
+ MachineLoopInfo *MLI = nullptr ;
73
+ MachineDominatorTree *MDT = nullptr ;
71
74
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:
83
75
// / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
84
76
// / in predecessor basic blocks.
85
77
bool EliminatePHINodes (MachineFunction &MF, MachineBasicBlock &MBB);
@@ -118,10 +110,71 @@ class PHIElimination : public MachineFunctionPass {
118
110
using LoweredPHIMap =
119
111
DenseMap<MachineInstr *, unsigned , MachineInstrExpressionTrait>;
120
112
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 ;
121
158
};
122
159
123
160
} // end anonymous namespace
124
161
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
+
125
178
STATISTIC (NumLowered, " Number of phis lowered" );
126
179
STATISTIC (NumCriticalEdgesSplit, " Number of critical edges split" );
127
180
STATISTIC (NumReused, " Number of reused lowered phis" );
@@ -147,12 +200,8 @@ void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
147
200
MachineFunctionPass::getAnalysisUsage (AU);
148
201
}
149
202
150
- bool PHIElimination::runOnMachineFunction (MachineFunction &MF) {
203
+ bool PHIEliminationImpl::run (MachineFunction &MF) {
151
204
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 ;
156
205
157
206
bool Changed = false ;
158
207
@@ -187,9 +236,6 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
187
236
}
188
237
}
189
238
190
- MachineLoopInfoWrapperPass *MLIWrapper =
191
- getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
192
- MachineLoopInfo *MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
193
239
for (auto &MBB : MF)
194
240
Changed |= SplitPHIEdges (MF, MBB, MLI, (LV ? &LiveInSets : nullptr ));
195
241
}
@@ -223,9 +269,8 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
223
269
}
224
270
225
271
// 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);
229
274
230
275
LoweredPHIs.clear ();
231
276
ImpDefs.clear ();
@@ -238,8 +283,8 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
238
283
239
284
// / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
240
285
// / predecessor basic blocks.
241
- bool PHIElimination ::EliminatePHINodes (MachineFunction &MF,
242
- MachineBasicBlock &MBB) {
286
+ bool PHIEliminationImpl ::EliminatePHINodes (MachineFunction &MF,
287
+ MachineBasicBlock &MBB) {
243
288
if (MBB.empty () || !MBB.front ().isPHI ())
244
289
return false ; // Quick exit for basic blocks without PHIs.
245
290
@@ -286,9 +331,9 @@ static bool allPhiOperandsUndefined(const MachineInstr &MPhi,
286
331
return true ;
287
332
}
288
333
// / 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) {
292
337
++NumLowered;
293
338
294
339
MachineBasicBlock::iterator AfterPHIsIt = std::next (LastPHIIt);
@@ -689,7 +734,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
689
734
// / particular, we want to map the number of uses of a virtual register which is
690
735
// / used in a PHI node. We map that to the BB the vreg is coming from. This is
691
736
// / 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) {
693
738
for (const auto &MBB : MF) {
694
739
for (const auto &BBI : MBB) {
695
740
if (!BBI.isPHI ())
@@ -705,9 +750,9 @@ void PHIElimination::analyzePHINodes(const MachineFunction &MF) {
705
750
}
706
751
}
707
752
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) {
711
756
if (MBB.empty () || !MBB.front ().isPHI () || MBB.isEHPad ())
712
757
return false ; // Quick exit for basic blocks without PHIs.
713
758
@@ -774,7 +819,8 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
774
819
}
775
820
if (!ShouldSplit && !SplitAllCriticalEdges)
776
821
continue ;
777
- if (!PreMBB->SplitCriticalEdge (&MBB, *this , LiveInSets)) {
822
+ if (!(P ? PreMBB->SplitCriticalEdge (&MBB, *P, LiveInSets)
823
+ : PreMBB->SplitCriticalEdge (&MBB, *MFAM, LiveInSets))) {
778
824
LLVM_DEBUG (dbgs () << " Failed to split critical edge.\n " );
779
825
continue ;
780
826
}
@@ -785,7 +831,7 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
785
831
return Changed;
786
832
}
787
833
788
- bool PHIElimination ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
834
+ bool PHIEliminationImpl ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
789
835
assert ((LV || LIS) &&
790
836
" isLiveIn() requires either LiveVariables or LiveIntervals" );
791
837
if (LIS)
@@ -794,8 +840,8 @@ bool PHIElimination::isLiveIn(Register Reg, const MachineBasicBlock *MBB) {
794
840
return LV->isLiveIn (Reg, *MBB);
795
841
}
796
842
797
- bool PHIElimination ::isLiveOutPastPHIs (Register Reg,
798
- const MachineBasicBlock *MBB) {
843
+ bool PHIEliminationImpl ::isLiveOutPastPHIs (Register Reg,
844
+ const MachineBasicBlock *MBB) {
799
845
assert ((LV || LIS) &&
800
846
" isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals" );
801
847
// LiveVariables considers uses in PHIs to be in the predecessor basic block,
0 commit comments