43
43
#include " llvm/CodeGen/MachineLoopInfo.h"
44
44
#include " llvm/CodeGen/MachineOperand.h"
45
45
#include " llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
46
+ #include " llvm/CodeGen/MachinePassManager.h"
46
47
#include " llvm/CodeGen/MachineRegisterInfo.h"
47
48
#include " llvm/CodeGen/RegAllocEvictionAdvisor.h"
49
+ #include " llvm/CodeGen/RegAllocGreedyPass.h"
48
50
#include " llvm/CodeGen/RegAllocPriorityAdvisor.h"
49
51
#include " llvm/CodeGen/RegAllocRegistry.h"
50
52
#include " llvm/CodeGen/RegisterClassInfo.h"
55
57
#include " llvm/CodeGen/TargetRegisterInfo.h"
56
58
#include " llvm/CodeGen/TargetSubtargetInfo.h"
57
59
#include " llvm/CodeGen/VirtRegMap.h"
60
+ #include " llvm/IR/Analysis.h"
58
61
#include " llvm/IR/DebugInfoMetadata.h"
59
62
#include " llvm/IR/Function.h"
60
63
#include " llvm/IR/LLVMContext.h"
@@ -146,11 +149,134 @@ static cl::opt<unsigned> SplitThresholdForRegWithHint(
146
149
static RegisterRegAlloc greedyRegAlloc (" greedy" , " greedy register allocator" ,
147
150
createGreedyRegisterAllocator);
148
151
149
- char RAGreedy::ID = 0 ;
150
- char &llvm::RAGreedyID = RAGreedy::ID;
152
+ namespace {
153
+ class RAGreedyLegacy : public MachineFunctionPass {
154
+ RegAllocFilterFunc F;
151
155
152
- INITIALIZE_PASS_BEGIN (RAGreedy, " greedy" ,
153
- " Greedy Register Allocator" , false , false )
156
+ public:
157
+ RAGreedyLegacy (const RegAllocFilterFunc F = nullptr );
158
+
159
+ static char ID;
160
+ // / Return the pass name.
161
+ StringRef getPassName () const override { return " Greedy Register Allocator" ; }
162
+
163
+ // / RAGreedy analysis usage.
164
+ void getAnalysisUsage (AnalysisUsage &AU) const override ;
165
+ // / Perform register allocation.
166
+ bool runOnMachineFunction (MachineFunction &mf) override ;
167
+
168
+ MachineFunctionProperties getRequiredProperties () const override {
169
+ return MachineFunctionProperties ().set (
170
+ MachineFunctionProperties::Property::NoPHIs);
171
+ }
172
+
173
+ MachineFunctionProperties getClearedProperties () const override {
174
+ return MachineFunctionProperties ().set (
175
+ MachineFunctionProperties::Property::IsSSA);
176
+ }
177
+ };
178
+
179
+ } // end anonymous namespace
180
+
181
+ RAGreedyLegacy::RAGreedyLegacy (const RegAllocFilterFunc F)
182
+ : MachineFunctionPass(ID), F(F) {
183
+ initializeRAGreedyLegacyPass (*PassRegistry::getPassRegistry ());
184
+ }
185
+
186
+ RAGreedy::RAGreedy (const RegAllocFilterFunc F) : RegAllocBase(F) {}
187
+
188
+ void RAGreedy::setAnalyses (RequiredAnalyses &Analyses) {
189
+ VRM = Analyses.VRM ;
190
+ LIS = Analyses.LIS ;
191
+ Matrix = Analyses.LRM ;
192
+ Indexes = Analyses.Indexes ;
193
+ MBFI = Analyses.MBFI ;
194
+ DomTree = Analyses.DomTree ;
195
+ Loops = Analyses.Loops ;
196
+ ORE = Analyses.ORE ;
197
+ Bundles = Analyses.Bundles ;
198
+ SpillPlacer = Analyses.SpillPlacer ;
199
+ DebugVars = Analyses.DebugVars ;
200
+ LSS = Analyses.LSS ;
201
+ EvictProvider = Analyses.EvictProvider ;
202
+ PriorityProvider = Analyses.PriorityProvider ;
203
+ }
204
+
205
+ PreservedAnalyses RAGreedyPass::run (MachineFunction &MF,
206
+ MachineFunctionAnalysisManager &MFAM) {
207
+ MFPropsModifier _ (*this , MF);
208
+
209
+ RAGreedy Impl (Filter);
210
+ RAGreedy::RequiredAnalyses Analyses;
211
+
212
+ Analyses.VRM = &MFAM.getResult <VirtRegMapAnalysis>(MF);
213
+ Analyses.LIS = &MFAM.getResult <LiveIntervalsAnalysis>(MF);
214
+ Analyses.LRM = &MFAM.getResult <LiveRegMatrixAnalysis>(MF);
215
+ Analyses.LSS = &MFAM.getResult <LiveStacksAnalysis>(MF);
216
+ Analyses.Indexes = &MFAM.getResult <SlotIndexesAnalysis>(MF);
217
+ Analyses.MBFI = &MFAM.getResult <MachineBlockFrequencyAnalysis>(MF);
218
+ Analyses.DomTree = &MFAM.getResult <MachineDominatorTreeAnalysis>(MF);
219
+ Analyses.ORE = &MFAM.getResult <MachineOptimizationRemarkEmitterAnalysis>(MF);
220
+ Analyses.Loops = &MFAM.getResult <MachineLoopAnalysis>(MF);
221
+ Analyses.Bundles = &MFAM.getResult <EdgeBundlesAnalysis>(MF);
222
+ Analyses.SpillPlacer = &MFAM.getResult <SpillPlacementAnalysis>(MF);
223
+ Analyses.DebugVars = &MFAM.getResult <LiveDebugVariablesAnalysis>(MF);
224
+ Analyses.EvictProvider =
225
+ MFAM.getResult <RegAllocEvictionAdvisorAnalysis>(MF).Provider ;
226
+ Analyses.PriorityProvider =
227
+ MFAM.getResult <RegAllocPriorityAdvisorAnalysis>(MF).Provider ;
228
+
229
+ Impl.setAnalyses (Analyses);
230
+ bool Changed = Impl.run (MF);
231
+ if (!Changed)
232
+ return PreservedAnalyses::all ();
233
+ auto PA = getMachineFunctionPassPreservedAnalyses ();
234
+ PA.preserveSet <CFGAnalyses>();
235
+ PA.preserve <MachineBlockFrequencyAnalysis>();
236
+ PA.preserve <LiveIntervalsAnalysis>();
237
+ PA.preserve <SlotIndexesAnalysis>();
238
+ PA.preserve <LiveDebugVariablesAnalysis>();
239
+ PA.preserve <LiveStacksAnalysis>();
240
+ PA.preserve <MachineDominatorTreeAnalysis>();
241
+ PA.preserve <MachineLoopAnalysis>();
242
+ PA.preserve <VirtRegMapAnalysis>();
243
+ PA.preserve <LiveRegMatrixAnalysis>();
244
+ return PA;
245
+ }
246
+
247
+ bool RAGreedyLegacy::runOnMachineFunction (MachineFunction &MF) {
248
+ RAGreedy Impl (F);
249
+
250
+ RAGreedy::RequiredAnalyses Analyses;
251
+ Analyses.VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM ();
252
+ Analyses.LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
253
+ Analyses.LSS = &getAnalysis<LiveStacksWrapperLegacy>().getLS ();
254
+ Analyses.LRM = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM ();
255
+ Analyses.Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI ();
256
+ Analyses.MBFI =
257
+ &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
258
+ Analyses.DomTree =
259
+ &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree ();
260
+ Analyses.ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE ();
261
+ Analyses.Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
262
+ Analyses.Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles ();
263
+ Analyses.SpillPlacer =
264
+ &getAnalysis<SpillPlacementWrapperLegacy>().getResult ();
265
+ Analyses.DebugVars = &getAnalysis<LiveDebugVariablesWrapperLegacy>().getLDV ();
266
+ Analyses.EvictProvider =
267
+ getAnalysis<RegAllocEvictionAdvisorAnalysisLegacy>().getProvider ().get ();
268
+ Analyses.PriorityProvider =
269
+ &getAnalysis<RegAllocPriorityAdvisorAnalysisLegacy>().getProvider ();
270
+
271
+ Impl.setAnalyses (Analyses);
272
+ return Impl.run (MF);
273
+ }
274
+
275
+ char RAGreedyLegacy::ID = 0 ;
276
+ char &llvm::RAGreedyLegacyID = RAGreedyLegacy::ID;
277
+
278
+ INITIALIZE_PASS_BEGIN (RAGreedyLegacy, " greedy" , " Greedy Register Allocator" ,
279
+ false , false )
154
280
INITIALIZE_PASS_DEPENDENCY(LiveDebugVariablesWrapperLegacy)
155
281
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
156
282
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
@@ -166,8 +292,8 @@ INITIALIZE_PASS_DEPENDENCY(SpillPlacementWrapperLegacy)
166
292
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
167
293
INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysisLegacy)
168
294
INITIALIZE_PASS_DEPENDENCY(RegAllocPriorityAdvisorAnalysisLegacy)
169
- INITIALIZE_PASS_END(RAGreedy , " greedy" ,
170
- " Greedy Register Allocator " , false , false )
295
+ INITIALIZE_PASS_END(RAGreedyLegacy , " greedy" , " Greedy Register Allocator " ,
296
+ false , false )
171
297
172
298
#ifndef NDEBUG
173
299
const char *const RAGreedy::StageName[] = {
@@ -186,17 +312,14 @@ const char *const RAGreedy::StageName[] = {
186
312
const float Hysteresis = (2007 / 2048 .0f ); // 0.97998046875
187
313
188
314
FunctionPass* llvm::createGreedyRegisterAllocator () {
189
- return new RAGreedy ();
315
+ return new RAGreedyLegacy ();
190
316
}
191
317
192
318
FunctionPass *llvm::createGreedyRegisterAllocator (RegAllocFilterFunc Ftor) {
193
- return new RAGreedy (Ftor);
319
+ return new RAGreedyLegacy (Ftor);
194
320
}
195
321
196
- RAGreedy::RAGreedy (RegAllocFilterFunc F)
197
- : MachineFunctionPass(ID), RegAllocBase(F) {}
198
-
199
- void RAGreedy::getAnalysisUsage (AnalysisUsage &AU) const {
322
+ void RAGreedyLegacy::getAnalysisUsage (AnalysisUsage &AU) const {
200
323
AU.setPreservesCFG ();
201
324
AU.addRequired <MachineBlockFrequencyInfoWrapperPass>();
202
325
AU.addPreserved <MachineBlockFrequencyInfoWrapperPass>();
@@ -1057,7 +1180,8 @@ void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
1057
1180
}
1058
1181
1059
1182
if (VerifyEnabled)
1060
- MF->verify (this , " After splitting live range around region" , &errs ());
1183
+ MF->verify (LIS, Indexes, " After splitting live range around region" ,
1184
+ &errs ());
1061
1185
}
1062
1186
1063
1187
MCRegister RAGreedy::tryRegionSplit (const LiveInterval &VirtReg,
@@ -1326,7 +1450,8 @@ Register RAGreedy::tryBlockSplit(const LiveInterval &VirtReg,
1326
1450
}
1327
1451
1328
1452
if (VerifyEnabled)
1329
- MF->verify (this , " After splitting live range around basic blocks" , &errs ());
1453
+ MF->verify (LIS, Indexes, " After splitting live range around basic blocks" ,
1454
+ &errs ());
1330
1455
return Register ();
1331
1456
}
1332
1457
@@ -2524,7 +2649,7 @@ MCRegister RAGreedy::selectOrSplitImpl(const LiveInterval &VirtReg,
2524
2649
DebugVars->splitRegister (r, LRE.regs (), *LIS);
2525
2650
2526
2651
if (VerifyEnabled)
2527
- MF->verify (this , " After spilling" , &errs ());
2652
+ MF->verify (LIS, Indexes , " After spilling" , &errs ());
2528
2653
}
2529
2654
2530
2655
// The live virtual register requesting allocation was spilled, so tell
@@ -2720,37 +2845,26 @@ bool RAGreedy::hasVirtRegAlloc() {
2720
2845
return false ;
2721
2846
}
2722
2847
2723
- bool RAGreedy::runOnMachineFunction (MachineFunction &mf) {
2848
+ bool RAGreedy::run (MachineFunction &mf) {
2724
2849
LLVM_DEBUG (dbgs () << " ********** GREEDY REGISTER ALLOCATION **********\n "
2725
2850
<< " ********** Function: " << mf.getName () << ' \n ' );
2726
2851
2727
2852
MF = &mf;
2728
2853
TII = MF->getSubtarget ().getInstrInfo ();
2729
2854
2730
2855
if (VerifyEnabled)
2731
- MF->verify (this , " Before greedy register allocator" , &errs ());
2856
+ MF->verify (LIS, Indexes , " Before greedy register allocator" , &errs ());
2732
2857
2733
- RegAllocBase::init (getAnalysis<VirtRegMapWrapperLegacy>().getVRM (),
2734
- getAnalysis<LiveIntervalsWrapperPass>().getLIS (),
2735
- getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM ());
2858
+ RegAllocBase::init (*this ->VRM , *this ->LIS , *this ->Matrix );
2736
2859
2737
2860
// Early return if there is no virtual register to be allocated to a
2738
2861
// physical register.
2739
2862
if (!hasVirtRegAlloc ())
2740
2863
return false ;
2741
2864
2742
- Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI ();
2743
2865
// Renumber to get accurate and consistent results from
2744
2866
// SlotIndexes::getApproxInstrDistance.
2745
2867
Indexes->packIndexes ();
2746
- MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
2747
- DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree ();
2748
- ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE ();
2749
- Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
2750
- Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles ();
2751
- SpillPlacer = &getAnalysis<SpillPlacementWrapperLegacy>().getResult ();
2752
- DebugVars = &getAnalysis<LiveDebugVariablesWrapperLegacy>().getLDV ();
2753
- auto &LSS = getAnalysis<LiveStacksWrapperLegacy>().getLS ();
2754
2868
2755
2869
initializeCSRCost ();
2756
2870
@@ -2766,17 +2880,12 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
2766
2880
2767
2881
ExtraInfo.emplace ();
2768
2882
2769
- auto &EvictAdvisorProvider =
2770
- getAnalysis<RegAllocEvictionAdvisorAnalysisLegacy>().getProvider ();
2771
- EvictAdvisor = EvictAdvisorProvider.getAdvisor (*MF, *this , MBFI, Loops);
2772
-
2773
- PriorityAdvisor = getAnalysis<RegAllocPriorityAdvisorAnalysisLegacy>()
2774
- .getProvider ()
2775
- .getAdvisor (*MF, *this , *Indexes);
2883
+ EvictAdvisor = EvictProvider->getAdvisor (*MF, *this , MBFI, Loops);
2884
+ PriorityAdvisor = PriorityProvider->getAdvisor (*MF, *this , *Indexes);
2776
2885
2777
2886
VRAI = std::make_unique<VirtRegAuxInfo>(*MF, *LIS, *VRM, *Loops, *MBFI);
2778
2887
SpillerInstance.reset (
2779
- createInlineSpiller ({*LIS, LSS, *DomTree, *MBFI}, *MF, *VRM, *VRAI));
2888
+ createInlineSpiller ({*LIS, * LSS, *DomTree, *MBFI}, *MF, *VRM, *VRAI));
2780
2889
2781
2890
VRAI->calculateSpillWeightsAndHints ();
2782
2891
@@ -2793,7 +2902,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
2793
2902
tryHintsRecoloring ();
2794
2903
2795
2904
if (VerifyEnabled)
2796
- MF->verify (this , " Before post optimization" , &errs ());
2905
+ MF->verify (LIS, Indexes , " Before post optimization" , &errs ());
2797
2906
postOptimization ();
2798
2907
reportStats ();
2799
2908
0 commit comments