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,12 @@ 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 ;
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,68 @@ class PHIElimination : public MachineFunctionPass {
118
110
using LoweredPHIMap =
119
111
DenseMap<MachineInstr *, unsigned , MachineInstrExpressionTrait>;
120
112
LoweredPHIMap LoweredPHIs;
113
+
114
+ MachineFunctionPass *P;
115
+ MachineFunctionAnalysisManager *MFAM;
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
+ LV = LVWrapper ? &LVWrapper->getLV () : nullptr ;
123
+ LIS = LISWrapper ? &LISWrapper->getLIS () : nullptr ;
124
+ MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
125
+ }
126
+
127
+ PHIEliminationImpl (MachineFunction &MF, MachineFunctionAnalysisManager &AM)
128
+ : LV(AM.getCachedResult<LiveVariablesAnalysis>(MF)),
129
+ LIS (AM.getCachedResult<LiveIntervalsAnalysis>(MF)),
130
+ MLI(AM.getCachedResult<MachineLoopAnalysis>(MF)), MFAM(&AM) {}
131
+
132
+ bool run (MachineFunction &MF);
133
+ };
134
+
135
+ class PHIElimination : public MachineFunctionPass {
136
+ public:
137
+ static char ID; // Pass identification, replacement for typeid
138
+
139
+ PHIElimination () : MachineFunctionPass(ID) {
140
+ initializePHIEliminationPass (*PassRegistry::getPassRegistry ());
141
+ }
142
+
143
+ bool runOnMachineFunction (MachineFunction &MF) override {
144
+ PHIEliminationImpl Impl (this );
145
+ return Impl.run (MF);
146
+ }
147
+
148
+ MachineFunctionProperties getSetProperties () const override {
149
+ return MachineFunctionProperties ().set (
150
+ MachineFunctionProperties::Property::NoPHIs);
151
+ }
152
+
153
+ void getAnalysisUsage (AnalysisUsage &AU) const override ;
121
154
};
122
155
123
156
} // end anonymous namespace
124
157
158
+ PreservedAnalyses
159
+ PHIEliminationPass::run (MachineFunction &MF,
160
+ MachineFunctionAnalysisManager &MFAM) {
161
+ PHIEliminationImpl Impl (MF, MFAM);
162
+ bool Changed = Impl.run (MF);
163
+ if (!Changed)
164
+ return PreservedAnalyses::all ();
165
+ auto PA = getMachineFunctionPassPreservedAnalyses ();
166
+ PA.preserve <LiveIntervalsAnalysis>();
167
+ PA.preserve <LiveVariablesAnalysis>();
168
+ PA.preserve <SlotIndexesAnalysis>();
169
+ PA.preserve <MachineDominatorTreeAnalysis>();
170
+ PA.preserve <MachinePostDominatorTreeAnalysis>();
171
+ PA.preserve <MachineLoopAnalysis>();
172
+ return PA;
173
+ }
174
+
125
175
STATISTIC (NumLowered, " Number of phis lowered" );
126
176
STATISTIC (NumCriticalEdgesSplit, " Number of critical edges split" );
127
177
STATISTIC (NumReused, " Number of reused lowered phis" );
@@ -147,12 +197,8 @@ void PHIElimination::getAnalysisUsage(AnalysisUsage &AU) const {
147
197
MachineFunctionPass::getAnalysisUsage (AU);
148
198
}
149
199
150
- bool PHIElimination::runOnMachineFunction (MachineFunction &MF) {
200
+ bool PHIEliminationImpl::run (MachineFunction &MF) {
151
201
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
202
157
203
bool Changed = false ;
158
204
@@ -187,9 +233,6 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
187
233
}
188
234
}
189
235
190
- MachineLoopInfoWrapperPass *MLIWrapper =
191
- getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
192
- MachineLoopInfo *MLI = MLIWrapper ? &MLIWrapper->getLI () : nullptr ;
193
236
for (auto &MBB : MF)
194
237
Changed |= SplitPHIEdges (MF, MBB, MLI, (LV ? &LiveInSets : nullptr ));
195
238
}
@@ -238,8 +281,8 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
238
281
239
282
// / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
240
283
// / predecessor basic blocks.
241
- bool PHIElimination ::EliminatePHINodes (MachineFunction &MF,
242
- MachineBasicBlock &MBB) {
284
+ bool PHIEliminationImpl ::EliminatePHINodes (MachineFunction &MF,
285
+ MachineBasicBlock &MBB) {
243
286
if (MBB.empty () || !MBB.front ().isPHI ())
244
287
return false ; // Quick exit for basic blocks without PHIs.
245
288
@@ -286,9 +329,9 @@ static bool allPhiOperandsUndefined(const MachineInstr &MPhi,
286
329
return true ;
287
330
}
288
331
// / 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) {
332
+ void PHIEliminationImpl ::LowerPHINode (MachineBasicBlock &MBB,
333
+ MachineBasicBlock::iterator LastPHIIt,
334
+ bool AllEdgesCritical) {
292
335
++NumLowered;
293
336
294
337
MachineBasicBlock::iterator AfterPHIsIt = std::next (LastPHIIt);
@@ -689,7 +732,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
689
732
// / particular, we want to map the number of uses of a virtual register which is
690
733
// / used in a PHI node. We map that to the BB the vreg is coming from. This is
691
734
// / used later to determine when the vreg is killed in the BB.
692
- void PHIElimination ::analyzePHINodes (const MachineFunction &MF) {
735
+ void PHIEliminationImpl ::analyzePHINodes (const MachineFunction &MF) {
693
736
for (const auto &MBB : MF) {
694
737
for (const auto &BBI : MBB) {
695
738
if (!BBI.isPHI ())
@@ -705,9 +748,9 @@ void PHIElimination::analyzePHINodes(const MachineFunction &MF) {
705
748
}
706
749
}
707
750
708
- bool PHIElimination ::SplitPHIEdges (MachineFunction &MF, MachineBasicBlock &MBB,
709
- MachineLoopInfo *MLI,
710
- std::vector<SparseBitVector<>> *LiveInSets) {
751
+ bool PHIEliminationImpl ::SplitPHIEdges (
752
+ MachineFunction &MF, MachineBasicBlock &MBB, MachineLoopInfo *MLI,
753
+ std::vector<SparseBitVector<>> *LiveInSets) {
711
754
if (MBB.empty () || !MBB.front ().isPHI () || MBB.isEHPad ())
712
755
return false ; // Quick exit for basic blocks without PHIs.
713
756
@@ -774,7 +817,8 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
774
817
}
775
818
if (!ShouldSplit && !SplitAllCriticalEdges)
776
819
continue ;
777
- if (!PreMBB->SplitCriticalEdge (&MBB, *this , LiveInSets)) {
820
+ if (!(P ? PreMBB->SplitCriticalEdge (&MBB, *P, LiveInSets)
821
+ : PreMBB->SplitCriticalEdge (&MBB, *MFAM, LiveInSets))) {
778
822
LLVM_DEBUG (dbgs () << " Failed to split critical edge.\n " );
779
823
continue ;
780
824
}
@@ -785,7 +829,7 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF, MachineBasicBlock &MBB,
785
829
return Changed;
786
830
}
787
831
788
- bool PHIElimination ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
832
+ bool PHIEliminationImpl ::isLiveIn (Register Reg, const MachineBasicBlock *MBB) {
789
833
assert ((LV || LIS) &&
790
834
" isLiveIn() requires either LiveVariables or LiveIntervals" );
791
835
if (LIS)
@@ -794,8 +838,8 @@ bool PHIElimination::isLiveIn(Register Reg, const MachineBasicBlock *MBB) {
794
838
return LV->isLiveIn (Reg, *MBB);
795
839
}
796
840
797
- bool PHIElimination ::isLiveOutPastPHIs (Register Reg,
798
- const MachineBasicBlock *MBB) {
841
+ bool PHIEliminationImpl ::isLiveOutPastPHIs (Register Reg,
842
+ const MachineBasicBlock *MBB) {
799
843
assert ((LV || LIS) &&
800
844
" isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals" );
801
845
// LiveVariables considers uses in PHIs to be in the predecessor basic block,
0 commit comments