blob: ecb00881837fb407de1749a285984c6e07761969 [file] [log] [blame]
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001//===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// The inline spiller modifies the machine function directly instead of
11// inserting spills and restores in VirtRegMap.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesen376dcbd2010-11-03 20:39:23 +000015#define DEBUG_TYPE "regalloc"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000016#include "Spiller.h"
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000017#include "LiveRangeEdit.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000018#include "VirtRegMap.h"
Jakob Stoklund Olesene93198a2010-11-10 23:55:56 +000019#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen0a12b802010-10-26 00:11:35 +000021#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000022#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
31
32using namespace llvm;
33
34namespace {
35class InlineSpiller : public Spiller {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000036 MachineFunctionPass &Pass;
37 MachineFunction &MF;
38 LiveIntervals &LIS;
39 LiveStacks &LSS;
40 AliasAnalysis *AA;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000041 MachineDominatorTree &MDT;
42 MachineLoopInfo &Loops;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000043 VirtRegMap &VRM;
44 MachineFrameInfo &MFI;
45 MachineRegisterInfo &MRI;
46 const TargetInstrInfo &TII;
47 const TargetRegisterInfo &TRI;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000048
49 // Variables that are valid during spill(), but used by multiple methods.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000050 LiveRangeEdit *Edit;
51 const TargetRegisterClass *RC;
52 int StackSlot;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000053 unsigned Original;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000054
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000055 // All registers to spill to StackSlot, including the main register.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +000056 SmallVector<unsigned, 8> RegsToSpill;
57
58 // All COPY instructions to/from snippets.
59 // They are ignored since both operands refer to the same stack slot.
60 SmallPtrSet<MachineInstr*, 8> SnippetCopies;
61
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000062 // Values that failed to remat at some point.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000063 SmallPtrSet<VNInfo*, 8> UsedValues;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000064
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000065 // Information about a value that was defined by a copy from a sibling
66 // register.
67 struct SibValueInfo {
68 // True when all reaching defs were reloads: No spill is necessary.
69 bool AllDefsAreReloads;
70
71 // The preferred register to spill.
72 unsigned SpillReg;
73
74 // The value of SpillReg that should be spilled.
75 VNInfo *SpillVNI;
76
77 // A defining instruction that is not a sibling copy or a reload, or NULL.
78 // This can be used as a template for rematerialization.
79 MachineInstr *DefMI;
80
81 SibValueInfo(unsigned Reg, VNInfo *VNI)
82 : AllDefsAreReloads(false), SpillReg(Reg), SpillVNI(VNI), DefMI(0) {}
83 };
84
85 // Values in RegsToSpill defined by sibling copies.
86 DenseMap<VNInfo*, SibValueInfo> SibValues;
87
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000088 ~InlineSpiller() {}
89
90public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000091 InlineSpiller(MachineFunctionPass &pass,
92 MachineFunction &mf,
93 VirtRegMap &vrm)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000094 : Pass(pass),
95 MF(mf),
96 LIS(pass.getAnalysis<LiveIntervals>()),
97 LSS(pass.getAnalysis<LiveStacks>()),
98 AA(&pass.getAnalysis<AliasAnalysis>()),
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000099 MDT(pass.getAnalysis<MachineDominatorTree>()),
100 Loops(pass.getAnalysis<MachineLoopInfo>()),
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000101 VRM(vrm),
102 MFI(*mf.getFrameInfo()),
103 MRI(mf.getRegInfo()),
104 TII(*mf.getTarget().getInstrInfo()),
105 TRI(*mf.getTarget().getRegisterInfo()) {}
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000106
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000107 void spill(LiveRangeEdit &);
108
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000109private:
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000110 bool isSnippet(const LiveInterval &SnipLI);
111 void collectRegsToSpill();
112
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000113 void traceSiblingValue(unsigned, VNInfo*, VNInfo*);
114 void analyzeSiblingValues();
115
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000116 bool reMaterializeFor(MachineBasicBlock::iterator MI);
117 void reMaterializeAll();
118
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000119 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000120 bool foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000121 const SmallVectorImpl<unsigned> &Ops,
122 MachineInstr *LoadMI = 0);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000123 void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000124 void insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
125 MachineBasicBlock::iterator MI);
126
127 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000128};
129}
130
131namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000132Spiller *createInlineSpiller(MachineFunctionPass &pass,
133 MachineFunction &mf,
134 VirtRegMap &vrm) {
135 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000136}
137}
138
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000139//===----------------------------------------------------------------------===//
140// Snippets
141//===----------------------------------------------------------------------===//
142
143// When spilling a virtual register, we also spill any snippets it is connected
144// to. The snippets are small live ranges that only have a single real use,
145// leftovers from live range splitting. Spilling them enables memory operand
146// folding or tightens the live range around the single use.
147//
148// This minimizes register pressure and maximizes the store-to-load distance for
149// spill slots which can be important in tight loops.
150
151/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
152/// otherwise return 0.
153static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
154 if (!MI->isCopy())
155 return 0;
156 if (MI->getOperand(0).getSubReg() != 0)
157 return 0;
158 if (MI->getOperand(1).getSubReg() != 0)
159 return 0;
160 if (MI->getOperand(0).getReg() == Reg)
161 return MI->getOperand(1).getReg();
162 if (MI->getOperand(1).getReg() == Reg)
163 return MI->getOperand(0).getReg();
164 return 0;
165}
166
167/// isSnippet - Identify if a live interval is a snippet that should be spilled.
168/// It is assumed that SnipLI is a virtual register with the same original as
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000169/// Edit->getReg().
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000170bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000171 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000172
173 // A snippet is a tiny live range with only a single instruction using it
174 // besides copies to/from Reg or spills/fills. We accept:
175 //
176 // %snip = COPY %Reg / FILL fi#
177 // %snip = USE %snip
178 // %Reg = COPY %snip / SPILL %snip, fi#
179 //
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000180 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000181 return false;
182
183 MachineInstr *UseMI = 0;
184
185 // Check that all uses satisfy our criteria.
186 for (MachineRegisterInfo::reg_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000187 RI = MRI.reg_nodbg_begin(SnipLI.reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000188 MachineInstr *MI = RI.skipInstruction();) {
189
190 // Allow copies to/from Reg.
191 if (isFullCopyOf(MI, Reg))
192 continue;
193
194 // Allow stack slot loads.
195 int FI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000196 if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000197 continue;
198
199 // Allow stack slot stores.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000200 if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000201 continue;
202
203 // Allow a single additional instruction.
204 if (UseMI && MI != UseMI)
205 return false;
206 UseMI = MI;
207 }
208 return true;
209}
210
211/// collectRegsToSpill - Collect live range snippets that only have a single
212/// real use.
213void InlineSpiller::collectRegsToSpill() {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000214 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000215
216 // Main register always spills.
217 RegsToSpill.assign(1, Reg);
218 SnippetCopies.clear();
219
220 // Snippets all have the same original, so there can't be any for an original
221 // register.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000222 if (Original == Reg)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000223 return;
224
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000225 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000226 MachineInstr *MI = RI.skipInstruction();) {
227 unsigned SnipReg = isFullCopyOf(MI, Reg);
228 if (!SnipReg)
229 continue;
230 if (!TargetRegisterInfo::isVirtualRegister(SnipReg))
231 continue;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000232 if (VRM.getOriginal(SnipReg) != Original)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000233 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000234 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000235 if (!isSnippet(SnipLI))
236 continue;
237 SnippetCopies.insert(MI);
238 if (std::find(RegsToSpill.begin(), RegsToSpill.end(),
239 SnipReg) == RegsToSpill.end())
240 RegsToSpill.push_back(SnipReg);
241
242 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
243 }
244}
245
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000246
247//===----------------------------------------------------------------------===//
248// Sibling Values
249//===----------------------------------------------------------------------===//
250
251// After live range splitting, some values to be spilled may be defined by
252// copies from sibling registers. We trace the sibling copies back to the
253// original value if it still exists. We need it for rematerialization.
254//
255// Even when the value can't be rematerialized, we still want to determine if
256// the value has already been spilled, or we may want to hoist the spill from a
257// loop.
258
259/// traceSiblingValue - Trace a value that is about to be spilled back to the
260/// real defining instructions by looking through sibling copies. Always stay
261/// within the range of OrigVNI so the registers are known to carry the same
262/// value.
263///
264/// Determine if the value is defined by all reloads, so spilling isn't
265/// necessary - the value is already in the stack slot.
266///
267/// Find a defining instruction that may be a candidate for rematerialization.
268///
269void InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
270 VNInfo *OrigVNI) {
271 DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
272 << UseVNI->id << '@' << UseVNI->def << '\n');
273 SmallPtrSet<VNInfo*, 8> Visited;
274 SmallVector<std::pair<unsigned,VNInfo*>, 8> WorkList;
275 WorkList.push_back(std::make_pair(UseReg, UseVNI));
276
277 // Best spill candidate seen so far. This must dominate UseVNI.
278 SibValueInfo SVI(UseReg, UseVNI);
279 MachineBasicBlock *UseMBB = LIS.getMBBFromIndex(UseVNI->def);
280 MachineBasicBlock *SpillMBB = UseMBB;
281 unsigned SpillDepth = Loops.getLoopDepth(SpillMBB);
282 bool SeenOrigPHI = false; // Original PHI met.
283
284 do {
285 unsigned Reg;
286 VNInfo *VNI;
287 tie(Reg, VNI) = WorkList.pop_back_val();
288 if (!Visited.insert(VNI))
289 continue;
290
291 // Is this value a better spill candidate?
292 if (VNI != SVI.SpillVNI) {
293 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
294 if (MBB == SpillMBB) {
295 // Prefer to spill a previous value in the same block.
296 if (VNI->def < SVI.SpillVNI->def)
297 SVI.SpillReg = Reg, SVI.SpillVNI = VNI;
298 } else if (MDT.dominates(MBB, UseMBB)) {
299 // This is a valid spill location dominating UseVNI.
300 // Prefer to spill at a smaller loop depth.
301 unsigned Depth = Loops.getLoopDepth(MBB);
302 if (Depth < SpillDepth) {
303 DEBUG(dbgs() << " spill depth " << Depth << ": " << PrintReg(Reg)
304 << ':' << VNI->id << '@' << VNI->def << '\n');
305 SVI.SpillReg = Reg;
306 SVI.SpillVNI = VNI;
307 SpillMBB = MBB;
308 SpillDepth = Depth;
309 }
310 }
311 }
312
313 // Trace through PHI-defs created by live range splitting.
314 if (VNI->isPHIDef()) {
315 if (VNI->def == OrigVNI->def) {
316 DEBUG(dbgs() << " orig phi value " << PrintReg(Reg) << ':'
317 << VNI->id << '@' << VNI->def << '\n');
318 SeenOrigPHI = true;
319 continue;
320 }
321 // Get values live-out of predecessors.
322 LiveInterval &LI = LIS.getInterval(Reg);
323 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
324 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
325 PE = MBB->pred_end(); PI != PE; ++PI) {
326 VNInfo *PVNI = LI.getVNInfoAt(LIS.getMBBEndIdx(*PI).getPrevSlot());
327 if (PVNI)
328 WorkList.push_back(std::make_pair(Reg, PVNI));
329 }
330 continue;
331 }
332
333 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
334 assert(MI && "Missing def");
335
336 // Trace through sibling copies.
337 if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
338 if (TargetRegisterInfo::isVirtualRegister(SrcReg) &&
339 VRM.getOriginal(SrcReg) == Original) {
340 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
341 VNInfo *SrcVNI = SrcLI.getVNInfoAt(VNI->def.getUseIndex());
342 assert(SrcVNI && "Copy from non-existing value");
343 DEBUG(dbgs() << " copy of " << PrintReg(SrcReg) << ':'
344 << SrcVNI->id << '@' << SrcVNI->def << '\n');
345 WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
346 continue;
347 }
348 }
349
350 // Track reachable reloads.
351 int FI;
352 if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
353 DEBUG(dbgs() << " reload " << PrintReg(Reg) << ':'
354 << VNI->id << "@" << VNI->def << '\n');
355 SVI.AllDefsAreReloads = true;
356 continue;
357 }
358
359 // We have an 'original' def. Don't record trivial cases.
360 if (VNI == UseVNI) {
361 DEBUG(dbgs() << "Not a sibling copy.\n");
362 return;
363 }
364
365 // Potential remat candidate.
366 DEBUG(dbgs() << " def " << PrintReg(Reg) << ':'
367 << VNI->id << '@' << VNI->def << '\t' << *MI);
368 SVI.DefMI = MI;
369 } while (!WorkList.empty());
370
371 if (SeenOrigPHI || SVI.DefMI)
372 SVI.AllDefsAreReloads = false;
373
374 DEBUG({
375 if (SVI.AllDefsAreReloads)
376 dbgs() << "All defs are reloads.\n";
377 else
378 dbgs() << "Prefer to spill " << PrintReg(SVI.SpillReg) << ':'
379 << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def << '\n';
380 });
381 SibValues.insert(std::make_pair(UseVNI, SVI));
382}
383
384/// analyzeSiblingValues - Trace values defined by sibling copies back to
385/// something that isn't a sibling copy.
386void InlineSpiller::analyzeSiblingValues() {
387 SibValues.clear();
388
389 // No siblings at all?
390 if (Edit->getReg() == Original)
391 return;
392
393 LiveInterval &OrigLI = LIS.getInterval(Original);
394 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
395 unsigned Reg = RegsToSpill[i];
396 LiveInterval &LI = LIS.getInterval(Reg);
397 for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
398 VE = LI.vni_end(); VI != VE; ++VI) {
399 VNInfo *VNI = *VI;
400 if (VNI->isUnused() || !(VNI->isPHIDef() || VNI->getCopy()))
401 continue;
402 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
403 if (OrigVNI->def != VNI->def)
404 traceSiblingValue(Reg, VNI, OrigVNI);
405 }
406 }
407}
408
Jakob Stoklund Olesen75b54092011-02-22 23:01:49 +0000409/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000410bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000411 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getUseIndex();
412 VNInfo *OrigVNI = Edit->getParent().getVNInfoAt(UseIdx);
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000413
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000414 if (!OrigVNI) {
415 DEBUG(dbgs() << "\tadding <undef> flags: ");
416 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
417 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000418 if (MO.isReg() && MO.isUse() && MO.getReg() == Edit->getReg())
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000419 MO.setIsUndef();
420 }
421 DEBUG(dbgs() << UseIdx << '\t' << *MI);
422 return true;
423 }
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000424
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000425 // FIXME: Properly remat for snippets as well.
426 if (SnippetCopies.count(MI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000427 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000428 return false;
429 }
430
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000431 LiveRangeEdit::Remat RM(OrigVNI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000432 if (!Edit->canRematerializeAt(RM, UseIdx, false, LIS)) {
433 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000434 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
435 return false;
436 }
437
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000438 // If the instruction also writes Edit->getReg(), it had better not require
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000439 // the same register for uses and defs.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000440 bool Reads, Writes;
441 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000442 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Edit->getReg(), &Ops);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000443 if (Writes) {
444 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
445 MachineOperand &MO = MI->getOperand(Ops[i]);
446 if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000447 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000448 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
449 return false;
450 }
451 }
452 }
453
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000454 // Before rematerializing into a register for a single instruction, try to
455 // fold a load into the instruction. That avoids allocating a new register.
456 if (RM.OrigMI->getDesc().canFoldAsLoad() &&
457 foldMemoryOperand(MI, Ops, RM.OrigMI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000458 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000459 return true;
460 }
461
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000462 // Alocate a new register for the remat.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000463 LiveInterval &NewLI = Edit->create(LIS, VRM);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000464 NewLI.markNotSpillable();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000465
Jakob Stoklund Olesenc3dca3f2011-02-09 00:25:36 +0000466 // Rematting for a copy: Set allocation hint to be the destination register.
467 if (MI->isCopy())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000468 MRI.setRegAllocationHint(NewLI.reg, 0, MI->getOperand(0).getReg());
Jakob Stoklund Olesenc3dca3f2011-02-09 00:25:36 +0000469
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000470 // Finally we can rematerialize OrigMI before MI.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000471 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
472 LIS, TII, TRI);
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000473 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000474 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000475
476 // Replace operands
477 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
478 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000479 if (MO.isReg() && MO.isUse() && MO.getReg() == Edit->getReg()) {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000480 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000481 MO.setIsKill();
482 }
483 }
484 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
485
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000486 VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000487 NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000488 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000489 return true;
490}
491
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000492/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000493/// and trim the live ranges after.
494void InlineSpiller::reMaterializeAll() {
495 // Do a quick scan of the interval values to find if any are remattable.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000496 if (!Edit->anyRematerializable(LIS, TII, AA))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000497 return;
498
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000499 UsedValues.clear();
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000500
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000501 // Try to remat before all uses of Edit->getReg().
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000502 bool anyRemat = false;
503 for (MachineRegisterInfo::use_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000504 RI = MRI.use_nodbg_begin(Edit->getReg());
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000505 MachineInstr *MI = RI.skipInstruction();)
506 anyRemat |= reMaterializeFor(MI);
507
508 if (!anyRemat)
509 return;
510
511 // Remove any values that were completely rematted.
512 bool anyRemoved = false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000513 for (LiveInterval::vni_iterator I = Edit->getParent().vni_begin(),
514 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000515 VNInfo *VNI = *I;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000516 if (VNI->hasPHIKill() || !Edit->didRematerialize(VNI) ||
517 UsedValues.count(VNI))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000518 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000519 MachineInstr *DefMI = LIS.getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000520 DEBUG(dbgs() << "\tremoving dead def: " << VNI->def << '\t' << *DefMI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000521 LIS.RemoveMachineInstrFromMaps(DefMI);
522 VRM.RemoveMachineInstrFromMaps(DefMI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000523 DefMI->eraseFromParent();
Lang Hamescec29452010-09-26 03:37:09 +0000524 VNI->def = SlotIndex();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000525 anyRemoved = true;
526 }
527
528 if (!anyRemoved)
529 return;
530
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000531 // Removing values may cause debug uses where parent is not live.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000532 for (MachineRegisterInfo::use_iterator RI = MRI.use_begin(Edit->getReg());
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000533 MachineInstr *MI = RI.skipInstruction();) {
534 if (!MI->isDebugValue())
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000535 continue;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000536 // Try to preserve the debug value if parent is live immediately after it.
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000537 MachineBasicBlock::iterator NextMI = MI;
538 ++NextMI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000539 if (NextMI != MI->getParent()->end() && !LIS.isNotInMIMap(NextMI)) {
540 SlotIndex Idx = LIS.getInstructionIndex(NextMI);
541 VNInfo *VNI = Edit->getParent().getVNInfoAt(Idx);
542 if (VNI && (VNI->hasPHIKill() || UsedValues.count(VNI)))
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000543 continue;
544 }
545 DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000546 MI->eraseFromParent();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000547 }
548}
549
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000550/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000551bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000552 int FI = 0;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000553 unsigned InstrReg;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000554 if (!(InstrReg = TII.isLoadFromStackSlot(MI, FI)) &&
555 !(InstrReg = TII.isStoreToStackSlot(MI, FI)))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000556 return false;
557
558 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000559 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000560 return false;
561
562 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000563 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000564 MI->eraseFromParent();
565 return true;
566}
567
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000568/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000569/// @param MI Instruction using or defining the current register.
Jakob Stoklund Olesen39048252010-12-18 03:28:32 +0000570/// @param Ops Operand indices from readsWritesVirtualRegister().
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000571/// @param LoadMI Load instruction to use instead of stack slot when non-null.
572/// @return True on success, and MI will be erased.
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000573bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000574 const SmallVectorImpl<unsigned> &Ops,
575 MachineInstr *LoadMI) {
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000576 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
577 // operands.
578 SmallVector<unsigned, 8> FoldOps;
579 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
580 unsigned Idx = Ops[i];
581 MachineOperand &MO = MI->getOperand(Idx);
582 if (MO.isImplicit())
583 continue;
584 // FIXME: Teach targets to deal with subregs.
585 if (MO.getSubReg())
586 return false;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000587 // We cannot fold a load instruction into a def.
588 if (LoadMI && MO.isDef())
589 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000590 // Tied use operands should not be passed to foldMemoryOperand.
591 if (!MI->isRegTiedToDefOperand(Idx))
592 FoldOps.push_back(Idx);
593 }
594
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000595 MachineInstr *FoldMI =
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000596 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
597 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000598 if (!FoldMI)
599 return false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000600 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000601 if (!LoadMI)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000602 VRM.addSpillSlotUse(StackSlot, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000603 MI->eraseFromParent();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000604 DEBUG(dbgs() << "\tfolded: " << *FoldMI);
605 return true;
606}
607
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000608/// insertReload - Insert a reload of NewLI.reg before MI.
609void InlineSpiller::insertReload(LiveInterval &NewLI,
610 MachineBasicBlock::iterator MI) {
611 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000612 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
613 TII.loadRegFromStackSlot(MBB, MI, NewLI.reg, StackSlot, RC, &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000614 --MI; // Point to load instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000615 SlotIndex LoadIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
616 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000617 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
Lang Hames6e2968c2010-09-25 12:04:16 +0000618 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000619 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000620 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
621}
622
623/// insertSpill - Insert a spill of NewLI.reg after MI.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000624void InlineSpiller::insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000625 MachineBasicBlock::iterator MI) {
626 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesen68257e62010-11-15 20:55:49 +0000627
628 // Get the defined value. It could be an early clobber so keep the def index.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000629 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000630 VNInfo *VNI = OldLI.getVNInfoAt(Idx);
Jakob Stoklund Olesen68257e62010-11-15 20:55:49 +0000631 assert(VNI && VNI->def.getDefIndex() == Idx && "Inconsistent VNInfo");
632 Idx = VNI->def;
633
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000634 TII.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, StackSlot, RC, &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000635 --MI; // Point to store instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000636 SlotIndex StoreIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
637 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000638 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000639 VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000640 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
641}
642
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000643/// spillAroundUses - insert spill code around each use of Reg.
644void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000645 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000646
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000647 // Iterate over instructions using Reg.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000648 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000649 MachineInstr *MI = RI.skipInstruction();) {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000650
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000651 // Debug values are not allowed to affect codegen.
652 if (MI->isDebugValue()) {
653 // Modify DBG_VALUE now that the value is in a spill slot.
654 uint64_t Offset = MI->getOperand(1).getImm();
655 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
656 DebugLoc DL = MI->getDebugLoc();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000657 if (MachineInstr *NewDV = TII.emitFrameIndexDebugValue(MF, StackSlot,
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000658 Offset, MDPtr, DL)) {
659 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
660 MachineBasicBlock *MBB = MI->getParent();
661 MBB->insert(MBB->erase(MI), NewDV);
662 } else {
663 DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
664 MI->eraseFromParent();
665 }
666 continue;
667 }
668
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000669 // Ignore copies to/from snippets. We'll delete them.
670 if (SnippetCopies.count(MI))
671 continue;
672
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000673 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000674 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000675 continue;
676
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000677 // Analyze instruction.
678 bool Reads, Writes;
679 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000680 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Reg, &Ops);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000681
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000682 // Attempt to fold memory ops.
683 if (foldMemoryOperand(MI, Ops))
684 continue;
685
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000686 // Allocate interval around instruction.
687 // FIXME: Infer regclass from instruction alone.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000688 LiveInterval &NewLI = Edit->create(LIS, VRM);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000689 NewLI.markNotSpillable();
690
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000691 if (Reads)
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000692 insertReload(NewLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000693
694 // Rewrite instruction operands.
695 bool hasLiveDef = false;
696 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
697 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000698 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000699 if (MO.isUse()) {
700 if (!MI->isRegTiedToDefOperand(Ops[i]))
701 MO.setIsKill();
702 } else {
703 if (!MO.isDead())
704 hasLiveDef = true;
705 }
706 }
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000707
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000708 // FIXME: Use a second vreg if instruction has no tied ops.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000709 if (Writes && hasLiveDef)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000710 insertSpill(NewLI, OldLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000711
712 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000713 }
714}
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000715
716void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000717 Edit = &edit;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000718 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
719 && "Trying to spill a stack slot.");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000720
721 // Share a stack slot among all descendants of Original.
722 Original = VRM.getOriginal(edit.getReg());
723 StackSlot = VRM.getStackSlot(Original);
724
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000725 DEBUG(dbgs() << "Inline spilling "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000726 << MRI.getRegClass(edit.getReg())->getName()
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000727 << ':' << edit.getParent() << "\nFrom original "
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000728 << LIS.getInterval(Original) << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000729 assert(edit.getParent().isSpillable() &&
730 "Attempting to spill already spilled value.");
731
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000732 collectRegsToSpill();
733
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000734 analyzeSiblingValues();
735
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000736 reMaterializeAll();
737
738 // Remat may handle everything.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000739 if (Edit->getParent().empty())
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000740 return;
741
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000742 RC = MRI.getRegClass(edit.getReg());
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000743
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000744 if (StackSlot == VirtRegMap::NO_STACK_SLOT)
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000745 StackSlot = VRM.assignVirt2StackSlot(Original);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000746
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000747 if (Original != edit.getReg())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000748 VRM.assignVirt2StackSlot(edit.getReg(), StackSlot);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000749
750 // Update LiveStacks now that we are committed to spilling.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000751 LiveInterval &stacklvr = LSS.getOrCreateInterval(StackSlot, RC);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000752 if (!stacklvr.hasAtLeastOneValue())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000753 stacklvr.getNextValue(SlotIndex(), 0, LSS.getVNInfoAllocator());
Jakob Stoklund Olesenb1adbd12011-03-12 04:25:36 +0000754 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000755 stacklvr.MergeRangesInAsValue(LIS.getInterval(RegsToSpill[i]),
Jakob Stoklund Olesenb1adbd12011-03-12 04:25:36 +0000756 stacklvr.getValNumInfo(0));
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000757
758 // Spill around uses of all RegsToSpill.
759 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
760 spillAroundUses(RegsToSpill[i]);
761
762 // Finally delete the SnippetCopies.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000763 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(edit.getReg());
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000764 MachineInstr *MI = RI.skipInstruction();) {
765 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
766 // FIXME: Do this with a LiveRangeEdit callback.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000767 VRM.RemoveMachineInstrFromMaps(MI);
768 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000769 MI->eraseFromParent();
770 }
771
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000772 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000773 edit.eraseVirtReg(RegsToSpill[i], LIS);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000774}