14
14
15
15
#define DEBUG_TYPE " spiller"
16
16
#include " Spiller.h"
17
+ #include " LiveRangeEdit.h"
17
18
#include " SplitKit.h"
18
19
#include " VirtRegMap.h"
19
20
#include " llvm/CodeGen/LiveIntervalAnalysis.h"
@@ -44,11 +45,9 @@ class InlineSpiller : public Spiller {
44
45
SplitAnalysis splitAnalysis_;
45
46
46
47
// Variables that are valid during spill(), but used by multiple methods.
47
- LiveInterval *li_;
48
- SmallVectorImpl<LiveInterval*> *newIntervals_;
48
+ LiveRangeEdit *edit_;
49
49
const TargetRegisterClass *rc_;
50
50
int stackSlot_;
51
- const SmallVectorImpl<LiveInterval*> *spillIs_;
52
51
53
52
// Values of the current interval that can potentially remat.
54
53
SmallPtrSet<VNInfo*, 8 > reMattable_;
@@ -78,11 +77,11 @@ class InlineSpiller : public Spiller {
78
77
SmallVectorImpl<LiveInterval*> &newIntervals,
79
78
SmallVectorImpl<LiveInterval*> &spillIs);
80
79
80
+ void spill (LiveRangeEdit &);
81
+
81
82
private:
82
83
bool split ();
83
84
84
- bool allUsesAvailableAt (const MachineInstr *OrigMI, SlotIndex OrigIdx,
85
- SlotIndex UseIdx);
86
85
bool reMaterializeFor (MachineBasicBlock::iterator MI);
87
86
void reMaterializeAll ();
88
87
@@ -105,75 +104,43 @@ Spiller *createInlineSpiller(MachineFunctionPass &pass,
105
104
// / split - try splitting the current interval into pieces that may allocate
106
105
// / separately. Return true if successful.
107
106
bool InlineSpiller::split () {
108
- splitAnalysis_.analyze (li_ );
107
+ splitAnalysis_.analyze (&edit_-> getParent () );
109
108
110
109
// Try splitting around loops.
111
110
if (const MachineLoop *loop = splitAnalysis_.getBestSplitLoop ()) {
112
- SplitEditor (splitAnalysis_, lis_, vrm_, *newIntervals_ )
111
+ SplitEditor (splitAnalysis_, lis_, vrm_, *edit_ )
113
112
.splitAroundLoop (loop);
114
113
return true ;
115
114
}
116
115
117
116
// Try splitting into single block intervals.
118
117
SplitAnalysis::BlockPtrSet blocks;
119
118
if (splitAnalysis_.getMultiUseBlocks (blocks)) {
120
- SplitEditor (splitAnalysis_, lis_, vrm_, *newIntervals_ )
119
+ SplitEditor (splitAnalysis_, lis_, vrm_, *edit_ )
121
120
.splitSingleBlocks (blocks);
122
121
return true ;
123
122
}
124
123
125
124
// Try splitting inside a basic block.
126
125
if (const MachineBasicBlock *MBB = splitAnalysis_.getBlockForInsideSplit ()) {
127
- SplitEditor (splitAnalysis_, lis_, vrm_, *newIntervals_ )
126
+ SplitEditor (splitAnalysis_, lis_, vrm_, *edit_ )
128
127
.splitInsideBlock (MBB);
129
128
return true ;
130
129
}
131
130
132
131
return false ;
133
132
}
134
133
135
- // / allUsesAvailableAt - Return true if all registers used by OrigMI at
136
- // / OrigIdx are also available with the same value at UseIdx.
137
- bool InlineSpiller::allUsesAvailableAt (const MachineInstr *OrigMI,
138
- SlotIndex OrigIdx,
139
- SlotIndex UseIdx) {
140
- OrigIdx = OrigIdx.getUseIndex ();
141
- UseIdx = UseIdx.getUseIndex ();
142
- for (unsigned i = 0 , e = OrigMI->getNumOperands (); i != e; ++i) {
143
- const MachineOperand &MO = OrigMI->getOperand (i);
144
- if (!MO.isReg () || !MO.getReg () || MO.getReg () == li_->reg )
145
- continue ;
146
- // Reserved registers are OK.
147
- if (MO.isUndef () || !lis_.hasInterval (MO.getReg ()))
148
- continue ;
149
- // We don't want to move any defs.
150
- if (MO.isDef ())
151
- return false ;
152
- // We cannot depend on virtual registers in spillIs_. They will be spilled.
153
- for (unsigned si = 0 , se = spillIs_->size (); si != se; ++si)
154
- if ((*spillIs_)[si]->reg == MO.getReg ())
155
- return false ;
156
-
157
- LiveInterval &LI = lis_.getInterval (MO.getReg ());
158
- const VNInfo *OVNI = LI.getVNInfoAt (OrigIdx);
159
- if (!OVNI)
160
- continue ;
161
- if (OVNI != LI.getVNInfoAt (UseIdx))
162
- return false ;
163
- }
164
- return true ;
165
- }
166
-
167
- // / reMaterializeFor - Attempt to rematerialize li_->reg before MI instead of
134
+ // / reMaterializeFor - Attempt to rematerialize edit_->getReg() before MI instead of
168
135
// / reloading it.
169
136
bool InlineSpiller::reMaterializeFor (MachineBasicBlock::iterator MI) {
170
137
SlotIndex UseIdx = lis_.getInstructionIndex (MI).getUseIndex ();
171
- VNInfo *OrigVNI = li_-> getVNInfoAt (UseIdx);
138
+ VNInfo *OrigVNI = edit_-> getParent (). getVNInfoAt (UseIdx);
172
139
if (!OrigVNI) {
173
140
DEBUG (dbgs () << " \t adding <undef> flags: " );
174
141
for (unsigned i = 0 , e = MI->getNumOperands (); i != e; ++i) {
175
142
MachineOperand &MO = MI->getOperand (i);
176
- if (MO.isReg () && MO.isUse () && MO.getReg () == li_-> reg )
143
+ if (MO.isReg () && MO.isUse () && MO.getReg () == edit_-> getReg () )
177
144
MO.setIsUndef ();
178
145
}
179
146
DEBUG (dbgs () << UseIdx << ' \t ' << *MI);
@@ -185,17 +152,17 @@ bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
185
152
return false ;
186
153
}
187
154
MachineInstr *OrigMI = lis_.getInstructionFromIndex (OrigVNI->def );
188
- if (!allUsesAvailableAt (OrigMI, OrigVNI->def , UseIdx)) {
155
+ if (!edit_-> allUsesAvailableAt (OrigMI, OrigVNI->def , UseIdx, lis_ )) {
189
156
usedValues_.insert (OrigVNI);
190
157
DEBUG (dbgs () << " \t cannot remat for " << UseIdx << ' \t ' << *MI);
191
158
return false ;
192
159
}
193
160
194
- // If the instruction also writes li_->reg , it had better not require the same
161
+ // If the instruction also writes edit_->getReg() , it had better not require the same
195
162
// register for uses and defs.
196
163
bool Reads, Writes;
197
164
SmallVector<unsigned , 8 > Ops;
198
- tie (Reads, Writes) = MI->readsWritesVirtualRegister (li_-> reg , &Ops);
165
+ tie (Reads, Writes) = MI->readsWritesVirtualRegister (edit_-> getReg () , &Ops);
199
166
if (Writes) {
200
167
for (unsigned i = 0 , e = Ops.size (); i != e; ++i) {
201
168
MachineOperand &MO = MI->getOperand (Ops[i]);
@@ -208,11 +175,8 @@ bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
208
175
}
209
176
210
177
// Alocate a new register for the remat.
211
- unsigned NewVReg = mri_.createVirtualRegister (rc_);
212
- vrm_.grow ();
213
- LiveInterval &NewLI = lis_.getOrCreateInterval (NewVReg);
178
+ LiveInterval &NewLI = edit_->create (mri_, lis_, vrm_);
214
179
NewLI.markNotSpillable ();
215
- newIntervals_->push_back (&NewLI);
216
180
217
181
// Finally we can rematerialize OrigMI before MI.
218
182
MachineBasicBlock &MBB = *MI->getParent ();
@@ -224,8 +188,8 @@ bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
224
188
// Replace operands
225
189
for (unsigned i = 0 , e = Ops.size (); i != e; ++i) {
226
190
MachineOperand &MO = MI->getOperand (Ops[i]);
227
- if (MO.isReg () && MO.isUse () && MO.getReg () == li_-> reg ) {
228
- MO.setReg (NewVReg );
191
+ if (MO.isReg () && MO.isUse () && MO.getReg () == edit_-> getReg () ) {
192
+ MO.setReg (NewLI. reg );
229
193
MO.setIsKill ();
230
194
}
231
195
}
@@ -237,14 +201,14 @@ bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
237
201
return true ;
238
202
}
239
203
240
- // / reMaterializeAll - Try to rematerialize as many uses of li_ as possible,
204
+ // / reMaterializeAll - Try to rematerialize as many uses as possible,
241
205
// / and trim the live ranges after.
242
206
void InlineSpiller::reMaterializeAll () {
243
207
// Do a quick scan of the interval values to find if any are remattable.
244
208
reMattable_.clear ();
245
209
usedValues_.clear ();
246
- for (LiveInterval::const_vni_iterator I = li_-> vni_begin (),
247
- E = li_-> vni_end (); I != E; ++I) {
210
+ for (LiveInterval::const_vni_iterator I = edit_-> getParent (). vni_begin (),
211
+ E = edit_-> getParent (). vni_end (); I != E; ++I) {
248
212
VNInfo *VNI = *I;
249
213
if (VNI->isUnused ())
250
214
continue ;
@@ -258,10 +222,10 @@ void InlineSpiller::reMaterializeAll() {
258
222
if (reMattable_.empty ())
259
223
return ;
260
224
261
- // Try to remat before all uses of li_->reg .
225
+ // Try to remat before all uses of edit_->getReg() .
262
226
bool anyRemat = false ;
263
227
for (MachineRegisterInfo::use_nodbg_iterator
264
- RI = mri_.use_nodbg_begin (li_-> reg );
228
+ RI = mri_.use_nodbg_begin (edit_-> getReg () );
265
229
MachineInstr *MI = RI.skipInstruction ();)
266
230
anyRemat |= reMaterializeFor (MI);
267
231
@@ -287,16 +251,17 @@ void InlineSpiller::reMaterializeAll() {
287
251
if (!anyRemoved)
288
252
return ;
289
253
290
- // Removing values may cause debug uses where li_ is not live.
291
- for (MachineRegisterInfo::use_iterator RI = mri_.use_begin (li_-> reg );
254
+ // Removing values may cause debug uses where parent is not live.
255
+ for (MachineRegisterInfo::use_iterator RI = mri_.use_begin (edit_-> getReg () );
292
256
MachineInstr *MI = RI.skipInstruction ();) {
293
257
if (!MI->isDebugValue ())
294
258
continue ;
295
- // Try to preserve the debug value if li_ is live immediately after it.
259
+ // Try to preserve the debug value if parent is live immediately after it.
296
260
MachineBasicBlock::iterator NextMI = MI;
297
261
++NextMI;
298
262
if (NextMI != MI->getParent ()->end () && !lis_.isNotInMIMap (NextMI)) {
299
- VNInfo *VNI = li_->getVNInfoAt (lis_.getInstructionIndex (NextMI));
263
+ SlotIndex Idx = lis_.getInstructionIndex (NextMI);
264
+ VNInfo *VNI = edit_->getParent ().getVNInfoAt (Idx);
300
265
if (VNI && (VNI->hasPHIKill () || usedValues_.count (VNI)))
301
266
continue ;
302
267
}
@@ -314,7 +279,7 @@ bool InlineSpiller::coalesceStackAccess(MachineInstr *MI) {
314
279
return false ;
315
280
316
281
// We have a stack access. Is it the right register and slot?
317
- if (reg != li_-> reg || FI != stackSlot_)
282
+ if (reg != edit_-> getReg () || FI != stackSlot_)
318
283
return false ;
319
284
320
285
DEBUG (dbgs () << " Coalescing stack access: " << *MI);
@@ -385,30 +350,33 @@ void InlineSpiller::insertSpill(LiveInterval &NewLI,
385
350
void InlineSpiller::spill (LiveInterval *li,
386
351
SmallVectorImpl<LiveInterval*> &newIntervals,
387
352
SmallVectorImpl<LiveInterval*> &spillIs) {
388
- DEBUG ( dbgs () << " Inline spilling " << *li << " \n " );
389
- assert (li-> isSpillable () && " Attempting to spill already spilled value. " );
390
- assert (!li-> isStackSlot () && " Trying to spill a stack slot. " );
353
+ LiveRangeEdit edit ( *li, newIntervals, spillIs );
354
+ spill (edit );
355
+ }
391
356
392
- li_ = li;
393
- newIntervals_ = &newIntervals;
394
- rc_ = mri_.getRegClass (li->reg );
395
- spillIs_ = &spillIs;
357
+ void InlineSpiller::spill (LiveRangeEdit &edit) {
358
+ edit_ = &edit;
359
+ DEBUG (dbgs () << " Inline spilling " << edit.getParent () << " \n " );
360
+ assert (edit.getParent ().isSpillable () &&
361
+ " Attempting to spill already spilled value." );
362
+ assert (!edit.getParent ().isStackSlot () && " Trying to spill a stack slot." );
396
363
397
364
if (split ())
398
365
return ;
399
366
400
367
reMaterializeAll ();
401
368
402
369
// Remat may handle everything.
403
- if (li_-> empty ())
370
+ if (edit_-> getParent (). empty ())
404
371
return ;
405
372
406
- stackSlot_ = vrm_.getStackSlot (li->reg );
373
+ rc_ = mri_.getRegClass (edit.getReg ());
374
+ stackSlot_ = vrm_.getStackSlot (edit.getReg ());
407
375
if (stackSlot_ == VirtRegMap::NO_STACK_SLOT)
408
- stackSlot_ = vrm_.assignVirt2StackSlot (li-> reg );
376
+ stackSlot_ = vrm_.assignVirt2StackSlot (edit. getReg () );
409
377
410
378
// Iterate over instructions using register.
411
- for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin (li-> reg );
379
+ for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin (edit. getReg () );
412
380
MachineInstr *MI = RI.skipInstruction ();) {
413
381
414
382
// Debug values are not allowed to affect codegen.
@@ -436,17 +404,15 @@ void InlineSpiller::spill(LiveInterval *li,
436
404
// Analyze instruction.
437
405
bool Reads, Writes;
438
406
SmallVector<unsigned , 8 > Ops;
439
- tie (Reads, Writes) = MI->readsWritesVirtualRegister (li-> reg , &Ops);
407
+ tie (Reads, Writes) = MI->readsWritesVirtualRegister (edit. getReg () , &Ops);
440
408
441
409
// Attempt to fold memory ops.
442
410
if (foldMemoryOperand (MI, Ops))
443
411
continue ;
444
412
445
413
// Allocate interval around instruction.
446
414
// FIXME: Infer regclass from instruction alone.
447
- unsigned NewVReg = mri_.createVirtualRegister (rc_);
448
- vrm_.grow ();
449
- LiveInterval &NewLI = lis_.getOrCreateInterval (NewVReg);
415
+ LiveInterval &NewLI = edit.create (mri_, lis_, vrm_);
450
416
NewLI.markNotSpillable ();
451
417
452
418
if (Reads)
@@ -456,7 +422,7 @@ void InlineSpiller::spill(LiveInterval *li,
456
422
bool hasLiveDef = false ;
457
423
for (unsigned i = 0 , e = Ops.size (); i != e; ++i) {
458
424
MachineOperand &MO = MI->getOperand (Ops[i]);
459
- MO.setReg (NewVReg );
425
+ MO.setReg (NewLI. reg );
460
426
if (MO.isUse ()) {
461
427
if (!MI->isRegTiedToDefOperand (Ops[i]))
462
428
MO.setIsKill ();
@@ -471,6 +437,5 @@ void InlineSpiller::spill(LiveInterval *li,
471
437
insertSpill (NewLI, MI);
472
438
473
439
DEBUG (dbgs () << " \t interval: " << NewLI << ' \n ' );
474
- newIntervals.push_back (&NewLI);
475
440
}
476
441
}
0 commit comments