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"
28
29
#include " llvm/CodeGen/MachineInstrBuilder.h"
29
30
#include " llvm/CodeGen/MachineLoopInfo.h"
30
31
#include " llvm/CodeGen/MachineOperand.h"
32
+ #include " llvm/CodeGen/MachinePostDominators.h"
31
33
#include " llvm/CodeGen/MachineRegisterInfo.h"
32
34
#include " llvm/CodeGen/SlotIndexes.h"
33
35
#include " llvm/CodeGen/TargetInstrInfo.h"
@@ -64,22 +66,13 @@ static cl::opt<bool> NoPhiElimLiveOutEarlyExit(
64
66
65
67
namespace {
66
68
67
- class PHIElimination : public MachineFunctionPass {
69
+ class PHIEliminationImpl {
68
70
MachineRegisterInfo *MRI = nullptr ; // Machine register information
69
71
LiveVariables *LV = nullptr ;
70
72
LiveIntervals *LIS = nullptr ;
73
+ MachineLoopInfo *MLI = nullptr ;
74
+ MachineDominatorTree *MDT = nullptr ;
71
75
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
76
// / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
84
77
// / in predecessor basic blocks.
85
78
bool EliminatePHINodes (MachineFunction &MF, MachineBasicBlock &MBB);
@@ -118,10 +111,72 @@ class PHIElimination : public MachineFunctionPass {
118
111
using LoweredPHIMap =
119
112
DenseMap<MachineInstr *, unsigned , MachineInstrExpressionTrait>;
120
113
LoweredPHIMap LoweredPHIs;
114
+
115
+ MachineFunctionPass *P;
116
+ MachineFunctionAnalysisManager *MFAM;
117
+
118
+ public:
119
+ PHIEliminationImpl (MachineFunctionPass *P) : P(P) {
120
+ auto *LVWrapper = P->getAnalysisIfAvailable <LiveVariablesWrapperPass>();
121
+ auto *LISWrapper = P->getAnalysisIfAvailable <LiveIntervalsWrapperPass>();
122
+ auto *MLIWrapper = P->getAnalysisIfAvailable <MachineLoopInfoWrapperPass>();
123
+ auto *MDTWrapper =
124
+ P->getAnalysisIfAvailable <MachineDominatorTreeWrapperPass>();
125
+ LV = LVWrapper ? &LVWrapper->getLV () : nullptr ;
126
+ LIS = LISWrapper ? &LISWrapper->getLIS () : nullptr ;
127
+ MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
128
+ MDT = MDTWrapper ? &MDTWrapper->getDomTree () : nullptr ;
129
+ }
130
+
131
+ PHIEliminationImpl (MachineFunction &MF, MachineFunctionAnalysisManager &AM)
132
+ : LV(AM.getCachedResult<LiveVariablesAnalysis>(MF)),
133
+ LIS (AM.getCachedResult<LiveIntervalsAnalysis>(MF)),
134
+ MLI(AM.getCachedResult<MachineLoopAnalysis>(MF)),
135
+ MDT(AM.getCachedResult<MachineDominatorTreeAnalysis>(MF)), MFAM(&AM) {}
136
+
137
+ bool run (MachineFunction &MF);
138
+ };
139
+
140
+ class PHIElimination : public MachineFunctionPass {
141
+ public:
142
+ static char ID; // Pass identification, replacement for typeid
143
+
144
+ PHIElimination () : MachineFunctionPass(ID) {
145
+ initializePHIEliminationPass (*PassRegistry::getPassRegistry ());
146
+ }
147
+
148
+ bool runOnMachineFunction (MachineFunction &MF) override {
149
+ PHIEliminationImpl Impl (this );
150
+ return Impl.run (MF);
151
+ }
152
+
153
+ MachineFunctionProperties getSetProperties () const override {
154
+ return MachineFunctionProperties ().set (
155
+ MachineFunctionProperties::Property::NoPHIs);
156
+ }
157
+
158
+ void getAnalysisUsage (AnalysisUsage &AU) const override ;
121
159
};
122
160
123
161
} // end anonymous namespace
124
162
163
+ PreservedAnalyses
164
+ PHIEliminationPass::run (MachineFunction &MF,
165
+ MachineFunctionAnalysisManager &MFAM) {
166
+ PHIEliminationImpl Impl (MF, MFAM);
167
+ bool Changed = Impl.run (MF);
168
+ if (!Changed)
169
+ return PreservedAnalyses::all ();
170
+ auto PA = getMachineFunctionPassPreservedAnalyses ();
171
+ PA.preserve <LiveIntervalsAnalysis>();
172
+ PA.preserve <LiveVariablesAnalysis>();
173
+ PA.preserve <SlotIndexesAnalysis>();
174
+ PA.preserve <MachineDominatorTreeAnalysis>();
175
+ PA.preserve <MachinePostDominatorTreeAnalysis>();
176
+ PA.preserve <MachineLoopAnalysis>();
177
+ return PA;
178
+ }
179
+
125
180
STATISTIC (NumLowered, " Number of phis lowered" );
126
181
STATISTIC (NumCriticalEdgesSplit, " Number of critical edges split" );
127
182
STATISTIC (NumReused, " Number of reused lowered phis" );
@@ -147,12 +202,8 @@ void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
147
202
MachineFunctionPass::getAnalysisUsage (AU);
148
203
}
149
204
150
- bool PHIElimination::runOnMachineFunction (MachineFunction &MF) {
205
+ bool PHIEliminationImpl::run (MachineFunction &MF) {
151
206
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
207
157
208
bool Changed = false ;
158
209
@@ -187,9 +238,6 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
187
238
}
188
239
}
189
240
190
- MachineLoopInfoWrapperPass *MLIWrapper =
191
- getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
192
- MachineLoopInfo *MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
193
241
for (auto &MBB : MF)
194
242
Changed |= SplitPHIEdges (MF, MBB, MLI, (LV ? &LiveInSets : nullptr ));
195
243
}
@@ -223,9 +271,8 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
223
271
}
224
272
225
273
// TODO: we should use the incremental DomTree updater here.
226
- if (Changed)
227
- if (auto *MDT = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>())
228
- MDT->getDomTree ().getBase ().recalculate (MF);
274
+ if (Changed && MDT)
275
+ MDT->getBase ().recalculate (MF);
229
276
230
277
LoweredPHIs.clear ();
231
278
ImpDefs.clear ();
@@ -238,8 +285,8 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
238
285
239
286
// / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
240
287
// / predecessor basic blocks.
241
- bool PHIElimination ::EliminatePHINodes (MachineFunction &MF,
242
- MachineBasicBlock &MBB) {
288
+ bool PHIEliminationImpl ::EliminatePHINodes (MachineFunction &MF,
289
+ MachineBasicBlock &MBB) {
243
290
if (MBB.empty () || !MBB.front ().isPHI ())
244
291
return false ; // Quick exit for basic blocks without PHIs.
245
292
@@ -286,9 +333,9 @@ static bool allPhiOperandsUndefined(const MachineInstr &MPhi,
286
333
return true ;
287
334
}
288
335
// / 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) {
336
+ void PHIEliminationImpl ::LowerPHINode (MachineBasicBlock &MBB,
337
+ MachineBasicBlock::iterator LastPHIIt,
338
+ bool AllEdgesCritical) {
292
339
++NumLowered;
293
340
294
341
MachineBasicBlock::iterator AfterPHIsIt = std::next (LastPHIIt);
@@ -689,7 +736,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
689
736
// / particular, we want to map the number of uses of a virtual register which is
690
737
// / used in a PHI node. We map that to the BB the vreg is coming from. This is
691
738
// / used later to determine when the vreg is killed in the BB.
692
- void PHIElimination ::analyzePHINodes (const MachineFunction &MF) {
739
+ void PHIEliminationImpl ::analyzePHINodes (const MachineFunction &MF) {
693
740
for (const auto &MBB : MF) {
694
741
for (const auto &BBI : MBB) {
695
742
if (!BBI.isPHI ())
@@ -705,9 +752,9 @@ void PHIElimination::analyzePHINodes(const MachineFunction &MF) {
705
752
}
706
753
}
707
754
708
- bool PHIElimination ::SplitPHIEdges (MachineFunction &MF, MachineBasicBlock &MBB,
709
- MachineLoopInfo *MLI,
710
- std::vector<SparseBitVector<>> *LiveInSets) {
755
+ bool PHIEliminationImpl ::SplitPHIEdges (
756
+ MachineFunction &MF, MachineBasicBlock &MBB, MachineLoopInfo *MLI,
757
+ std::vector<SparseBitVector<>> *LiveInSets) {
711
758
if (MBB.empty () || !MBB.front ().isPHI () || MBB.isEHPad ())
712
759
return false ; // Quick exit for basic blocks without PHIs.
713
760
@@ -774,7 +821,8 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
774
821
}
775
822
if (!ShouldSplit && !SplitAllCriticalEdges)
776
823
continue ;
777
- if (!PreMBB->SplitCriticalEdge (&MBB, *this , LiveInSets)) {
824
+ if (!(P ? PreMBB->SplitCriticalEdge (&MBB, *P, LiveInSets)
825
+ : PreMBB->SplitCriticalEdge (&MBB, *MFAM, LiveInSets))) {
778
826
LLVM_DEBUG (dbgs () << " Failed to split critical edge.\n " );
779
827
continue ;
780
828
}
@@ -785,7 +833,7 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
785
833
return Changed;
786
834
}
787
835
788
- bool PHIElimination ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
836
+ bool PHIEliminationImpl ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
789
837
assert ((LV || LIS) &&
790
838
" isLiveIn() requires either LiveVariables or LiveIntervals" );
791
839
if (LIS)
@@ -794,8 +842,8 @@ bool PHIElimination::isLiveIn(Register Reg, const MachineBasicBlock *MBB) {
794
842
return LV->isLiveIn (Reg, *MBB);
795
843
}
796
844
797
- bool PHIElimination ::isLiveOutPastPHIs (Register Reg,
798
- const MachineBasicBlock *MBB) {
845
+ bool PHIEliminationImpl ::isLiveOutPastPHIs (Register Reg,
846
+ const MachineBasicBlock *MBB) {
799
847
assert ((LV || LIS) &&
800
848
" isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals" );
801
849
// LiveVariables considers uses in PHIs to be in the predecessor basic block,
0 commit comments