blob: 3e2adfbe24b49095533ab53b47e9c5ed23a35a0a [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.
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +000086 typedef DenseMap<VNInfo*, SibValueInfo> SibValueMap;
87 SibValueMap SibValues;
88
89 // Dead defs generated during spilling.
90 SmallVector<MachineInstr*, 8> DeadDefs;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000091
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000092 ~InlineSpiller() {}
93
94public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000095 InlineSpiller(MachineFunctionPass &pass,
96 MachineFunction &mf,
97 VirtRegMap &vrm)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000098 : Pass(pass),
99 MF(mf),
100 LIS(pass.getAnalysis<LiveIntervals>()),
101 LSS(pass.getAnalysis<LiveStacks>()),
102 AA(&pass.getAnalysis<AliasAnalysis>()),
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000103 MDT(pass.getAnalysis<MachineDominatorTree>()),
104 Loops(pass.getAnalysis<MachineLoopInfo>()),
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000105 VRM(vrm),
106 MFI(*mf.getFrameInfo()),
107 MRI(mf.getRegInfo()),
108 TII(*mf.getTarget().getInstrInfo()),
109 TRI(*mf.getTarget().getRegisterInfo()) {}
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000110
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000111 void spill(LiveRangeEdit &);
112
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000113private:
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000114 bool isSnippet(const LiveInterval &SnipLI);
115 void collectRegsToSpill();
116
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000117 bool isRegToSpill(unsigned Reg) {
118 return std::find(RegsToSpill.begin(),
119 RegsToSpill.end(), Reg) != RegsToSpill.end();
120 }
121
122 bool isSibling(unsigned Reg);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000123 void traceSiblingValue(unsigned, VNInfo*, VNInfo*);
124 void analyzeSiblingValues();
125
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000126 bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000127 void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000128
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000129 bool reMaterializeFor(MachineBasicBlock::iterator MI);
130 void reMaterializeAll();
131
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000132 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000133 bool foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000134 const SmallVectorImpl<unsigned> &Ops,
135 MachineInstr *LoadMI = 0);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000136 void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000137 void insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
138 MachineBasicBlock::iterator MI);
139
140 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000141};
142}
143
144namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000145Spiller *createInlineSpiller(MachineFunctionPass &pass,
146 MachineFunction &mf,
147 VirtRegMap &vrm) {
148 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000149}
150}
151
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000152//===----------------------------------------------------------------------===//
153// Snippets
154//===----------------------------------------------------------------------===//
155
156// When spilling a virtual register, we also spill any snippets it is connected
157// to. The snippets are small live ranges that only have a single real use,
158// leftovers from live range splitting. Spilling them enables memory operand
159// folding or tightens the live range around the single use.
160//
161// This minimizes register pressure and maximizes the store-to-load distance for
162// spill slots which can be important in tight loops.
163
164/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
165/// otherwise return 0.
166static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
167 if (!MI->isCopy())
168 return 0;
169 if (MI->getOperand(0).getSubReg() != 0)
170 return 0;
171 if (MI->getOperand(1).getSubReg() != 0)
172 return 0;
173 if (MI->getOperand(0).getReg() == Reg)
174 return MI->getOperand(1).getReg();
175 if (MI->getOperand(1).getReg() == Reg)
176 return MI->getOperand(0).getReg();
177 return 0;
178}
179
180/// isSnippet - Identify if a live interval is a snippet that should be spilled.
181/// It is assumed that SnipLI is a virtual register with the same original as
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000182/// Edit->getReg().
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000183bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000184 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000185
186 // A snippet is a tiny live range with only a single instruction using it
187 // besides copies to/from Reg or spills/fills. We accept:
188 //
189 // %snip = COPY %Reg / FILL fi#
190 // %snip = USE %snip
191 // %Reg = COPY %snip / SPILL %snip, fi#
192 //
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000193 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000194 return false;
195
196 MachineInstr *UseMI = 0;
197
198 // Check that all uses satisfy our criteria.
199 for (MachineRegisterInfo::reg_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000200 RI = MRI.reg_nodbg_begin(SnipLI.reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000201 MachineInstr *MI = RI.skipInstruction();) {
202
203 // Allow copies to/from Reg.
204 if (isFullCopyOf(MI, Reg))
205 continue;
206
207 // Allow stack slot loads.
208 int FI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000209 if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000210 continue;
211
212 // Allow stack slot stores.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000213 if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000214 continue;
215
216 // Allow a single additional instruction.
217 if (UseMI && MI != UseMI)
218 return false;
219 UseMI = MI;
220 }
221 return true;
222}
223
224/// collectRegsToSpill - Collect live range snippets that only have a single
225/// real use.
226void InlineSpiller::collectRegsToSpill() {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000227 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000228
229 // Main register always spills.
230 RegsToSpill.assign(1, Reg);
231 SnippetCopies.clear();
232
233 // Snippets all have the same original, so there can't be any for an original
234 // register.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000235 if (Original == Reg)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000236 return;
237
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000238 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000239 MachineInstr *MI = RI.skipInstruction();) {
240 unsigned SnipReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000241 if (!isSibling(SnipReg))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000242 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000243 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000244 if (!isSnippet(SnipLI))
245 continue;
246 SnippetCopies.insert(MI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000247 if (!isRegToSpill(SnipReg))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000248 RegsToSpill.push_back(SnipReg);
249
250 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
251 }
252}
253
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000254
255//===----------------------------------------------------------------------===//
256// Sibling Values
257//===----------------------------------------------------------------------===//
258
259// After live range splitting, some values to be spilled may be defined by
260// copies from sibling registers. We trace the sibling copies back to the
261// original value if it still exists. We need it for rematerialization.
262//
263// Even when the value can't be rematerialized, we still want to determine if
264// the value has already been spilled, or we may want to hoist the spill from a
265// loop.
266
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000267bool InlineSpiller::isSibling(unsigned Reg) {
268 return TargetRegisterInfo::isVirtualRegister(Reg) &&
269 VRM.getOriginal(Reg) == Original;
270}
271
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000272/// traceSiblingValue - Trace a value that is about to be spilled back to the
273/// real defining instructions by looking through sibling copies. Always stay
274/// within the range of OrigVNI so the registers are known to carry the same
275/// value.
276///
277/// Determine if the value is defined by all reloads, so spilling isn't
278/// necessary - the value is already in the stack slot.
279///
280/// Find a defining instruction that may be a candidate for rematerialization.
281///
282void InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
283 VNInfo *OrigVNI) {
284 DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
285 << UseVNI->id << '@' << UseVNI->def << '\n');
286 SmallPtrSet<VNInfo*, 8> Visited;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000287 SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000288 WorkList.push_back(std::make_pair(UseReg, UseVNI));
289
290 // Best spill candidate seen so far. This must dominate UseVNI.
291 SibValueInfo SVI(UseReg, UseVNI);
292 MachineBasicBlock *UseMBB = LIS.getMBBFromIndex(UseVNI->def);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000293 unsigned SpillDepth = Loops.getLoopDepth(UseMBB);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000294 bool SeenOrigPHI = false; // Original PHI met.
295
296 do {
297 unsigned Reg;
298 VNInfo *VNI;
299 tie(Reg, VNI) = WorkList.pop_back_val();
300 if (!Visited.insert(VNI))
301 continue;
302
303 // Is this value a better spill candidate?
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000304 if (!isRegToSpill(Reg)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000305 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000306 if (MBB != UseMBB && MDT.dominates(MBB, UseMBB)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000307 // This is a valid spill location dominating UseVNI.
308 // Prefer to spill at a smaller loop depth.
309 unsigned Depth = Loops.getLoopDepth(MBB);
310 if (Depth < SpillDepth) {
311 DEBUG(dbgs() << " spill depth " << Depth << ": " << PrintReg(Reg)
312 << ':' << VNI->id << '@' << VNI->def << '\n');
313 SVI.SpillReg = Reg;
314 SVI.SpillVNI = VNI;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000315 SpillDepth = Depth;
316 }
317 }
318 }
319
320 // Trace through PHI-defs created by live range splitting.
321 if (VNI->isPHIDef()) {
322 if (VNI->def == OrigVNI->def) {
323 DEBUG(dbgs() << " orig phi value " << PrintReg(Reg) << ':'
324 << VNI->id << '@' << VNI->def << '\n');
325 SeenOrigPHI = true;
326 continue;
327 }
328 // Get values live-out of predecessors.
329 LiveInterval &LI = LIS.getInterval(Reg);
330 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
331 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
332 PE = MBB->pred_end(); PI != PE; ++PI) {
333 VNInfo *PVNI = LI.getVNInfoAt(LIS.getMBBEndIdx(*PI).getPrevSlot());
334 if (PVNI)
335 WorkList.push_back(std::make_pair(Reg, PVNI));
336 }
337 continue;
338 }
339
340 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
341 assert(MI && "Missing def");
342
343 // Trace through sibling copies.
344 if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000345 if (isSibling(SrcReg)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000346 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
347 VNInfo *SrcVNI = SrcLI.getVNInfoAt(VNI->def.getUseIndex());
348 assert(SrcVNI && "Copy from non-existing value");
349 DEBUG(dbgs() << " copy of " << PrintReg(SrcReg) << ':'
350 << SrcVNI->id << '@' << SrcVNI->def << '\n');
351 WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
352 continue;
353 }
354 }
355
356 // Track reachable reloads.
357 int FI;
358 if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
359 DEBUG(dbgs() << " reload " << PrintReg(Reg) << ':'
360 << VNI->id << "@" << VNI->def << '\n');
361 SVI.AllDefsAreReloads = true;
362 continue;
363 }
364
365 // We have an 'original' def. Don't record trivial cases.
366 if (VNI == UseVNI) {
367 DEBUG(dbgs() << "Not a sibling copy.\n");
368 return;
369 }
370
371 // Potential remat candidate.
372 DEBUG(dbgs() << " def " << PrintReg(Reg) << ':'
373 << VNI->id << '@' << VNI->def << '\t' << *MI);
374 SVI.DefMI = MI;
375 } while (!WorkList.empty());
376
377 if (SeenOrigPHI || SVI.DefMI)
378 SVI.AllDefsAreReloads = false;
379
380 DEBUG({
381 if (SVI.AllDefsAreReloads)
382 dbgs() << "All defs are reloads.\n";
383 else
384 dbgs() << "Prefer to spill " << PrintReg(SVI.SpillReg) << ':'
385 << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def << '\n';
386 });
387 SibValues.insert(std::make_pair(UseVNI, SVI));
388}
389
390/// analyzeSiblingValues - Trace values defined by sibling copies back to
391/// something that isn't a sibling copy.
392void InlineSpiller::analyzeSiblingValues() {
393 SibValues.clear();
394
395 // No siblings at all?
396 if (Edit->getReg() == Original)
397 return;
398
399 LiveInterval &OrigLI = LIS.getInterval(Original);
400 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
401 unsigned Reg = RegsToSpill[i];
402 LiveInterval &LI = LIS.getInterval(Reg);
403 for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
404 VE = LI.vni_end(); VI != VE; ++VI) {
405 VNInfo *VNI = *VI;
406 if (VNI->isUnused() || !(VNI->isPHIDef() || VNI->getCopy()))
407 continue;
408 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
409 if (OrigVNI->def != VNI->def)
410 traceSiblingValue(Reg, VNI, OrigVNI);
411 }
412 }
413}
414
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000415/// hoistSpill - Given a sibling copy that defines a value to be spilled, insert
416/// a spill at a better location.
417bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) {
418 SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
419 VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getDefIndex());
420 assert(VNI && VNI->def == Idx.getDefIndex() && "Not defined by copy");
421 SibValueMap::const_iterator I = SibValues.find(VNI);
422 if (I == SibValues.end())
423 return false;
424
425 const SibValueInfo &SVI = I->second;
426
427 // Let the normal folding code deal with the boring case.
428 if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI)
429 return false;
430
431 // Conservatively extend the stack slot range to the range of the original
432 // value. We may be able to do better with stack slot coloring by being more
433 // careful here.
434 LiveInterval &StackInt = LSS.getInterval(StackSlot);
435 LiveInterval &OrigLI = LIS.getInterval(Original);
436 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
437 StackInt.MergeValueInAsValue(OrigLI, OrigVNI, StackInt.getValNumInfo(0));
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +0000438 DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
439 << StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000440
441 // Already spilled everywhere.
442 if (SVI.AllDefsAreReloads)
443 return true;
444
445 // We are going to spill SVI.SpillVNI immediately after its def, so clear out
446 // any later spills of the same value.
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000447 eliminateRedundantSpills(LIS.getInterval(SVI.SpillReg), SVI.SpillVNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000448
449 MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def);
450 MachineBasicBlock::iterator MII;
451 if (SVI.SpillVNI->isPHIDef())
452 MII = MBB->SkipPHIsAndLabels(MBB->begin());
453 else {
454 MII = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
455 ++MII;
456 }
457 // Insert spill without kill flag immediately after def.
458 TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot, RC, &TRI);
459 --MII; // Point to store instruction.
460 LIS.InsertMachineInstrInMaps(MII);
461 VRM.addSpillSlotUse(StackSlot, MII);
462 DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
463 return true;
464}
465
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000466/// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
467/// redundant spills of this value in SLI.reg and sibling copies.
468void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
469 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
470 WorkList.push_back(std::make_pair(&SLI, VNI));
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000471 LiveInterval &StackInt = LSS.getInterval(StackSlot);
472
473 do {
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000474 LiveInterval *LI;
475 tie(LI, VNI) = WorkList.pop_back_val();
476 unsigned Reg = LI->reg;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000477 DEBUG(dbgs() << "Checking redundant spills for " << PrintReg(Reg) << ':'
478 << VNI->id << '@' << VNI->def << '\n');
479
480 // Regs to spill are taken care of.
481 if (isRegToSpill(Reg))
482 continue;
483
484 // Add all of VNI's live range to StackInt.
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000485 StackInt.MergeValueInAsValue(*LI, VNI, StackInt.getValNumInfo(0));
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +0000486 DEBUG(dbgs() << "Merged to stack int: " << StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000487
488 // Find all spills and copies of VNI.
489 for (MachineRegisterInfo::use_nodbg_iterator UI = MRI.use_nodbg_begin(Reg);
490 MachineInstr *MI = UI.skipInstruction();) {
491 if (!MI->isCopy() && !MI->getDesc().mayStore())
492 continue;
493 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000494 if (LI->getVNInfoAt(Idx) != VNI)
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000495 continue;
496
497 // Follow sibling copies down the dominator tree.
498 if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
499 if (isSibling(DstReg)) {
500 LiveInterval &DstLI = LIS.getInterval(DstReg);
501 VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getDefIndex());
502 assert(DstVNI && "Missing defined value");
503 assert(DstVNI->def == Idx.getDefIndex() && "Wrong copy def slot");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000504 WorkList.push_back(std::make_pair(&DstLI, DstVNI));
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000505 }
506 continue;
507 }
508
509 // Erase spills.
510 int FI;
511 if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
512 DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI);
513 // eliminateDeadDefs won't normally remove stores, so switch opcode.
514 MI->setDesc(TII.get(TargetOpcode::KILL));
515 DeadDefs.push_back(MI);
516 }
517 }
518 } while (!WorkList.empty());
519}
520
Jakob Stoklund Olesen75b54092011-02-22 23:01:49 +0000521/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000522bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000523 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getUseIndex();
524 VNInfo *OrigVNI = Edit->getParent().getVNInfoAt(UseIdx);
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000525
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000526 if (!OrigVNI) {
527 DEBUG(dbgs() << "\tadding <undef> flags: ");
528 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
529 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000530 if (MO.isReg() && MO.isUse() && MO.getReg() == Edit->getReg())
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000531 MO.setIsUndef();
532 }
533 DEBUG(dbgs() << UseIdx << '\t' << *MI);
534 return true;
535 }
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000536
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000537 // FIXME: Properly remat for snippets as well.
538 if (SnippetCopies.count(MI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000539 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000540 return false;
541 }
542
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000543 LiveRangeEdit::Remat RM(OrigVNI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000544 if (!Edit->canRematerializeAt(RM, UseIdx, false, LIS)) {
545 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000546 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
547 return false;
548 }
549
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000550 // If the instruction also writes Edit->getReg(), it had better not require
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000551 // the same register for uses and defs.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000552 bool Reads, Writes;
553 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000554 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Edit->getReg(), &Ops);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000555 if (Writes) {
556 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
557 MachineOperand &MO = MI->getOperand(Ops[i]);
558 if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000559 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000560 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
561 return false;
562 }
563 }
564 }
565
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000566 // Before rematerializing into a register for a single instruction, try to
567 // fold a load into the instruction. That avoids allocating a new register.
568 if (RM.OrigMI->getDesc().canFoldAsLoad() &&
569 foldMemoryOperand(MI, Ops, RM.OrigMI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000570 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000571 return true;
572 }
573
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000574 // Alocate a new register for the remat.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000575 LiveInterval &NewLI = Edit->create(LIS, VRM);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000576 NewLI.markNotSpillable();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000577
Jakob Stoklund Olesenc3dca3f2011-02-09 00:25:36 +0000578 // Rematting for a copy: Set allocation hint to be the destination register.
579 if (MI->isCopy())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000580 MRI.setRegAllocationHint(NewLI.reg, 0, MI->getOperand(0).getReg());
Jakob Stoklund Olesenc3dca3f2011-02-09 00:25:36 +0000581
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000582 // Finally we can rematerialize OrigMI before MI.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000583 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
584 LIS, TII, TRI);
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000585 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000586 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000587
588 // Replace operands
589 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
590 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000591 if (MO.isReg() && MO.isUse() && MO.getReg() == Edit->getReg()) {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000592 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000593 MO.setIsKill();
594 }
595 }
596 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
597
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000598 VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000599 NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000600 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000601 return true;
602}
603
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000604/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000605/// and trim the live ranges after.
606void InlineSpiller::reMaterializeAll() {
607 // Do a quick scan of the interval values to find if any are remattable.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000608 if (!Edit->anyRematerializable(LIS, TII, AA))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000609 return;
610
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000611 UsedValues.clear();
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000612
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000613 // Try to remat before all uses of Edit->getReg().
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000614 bool anyRemat = false;
615 for (MachineRegisterInfo::use_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000616 RI = MRI.use_nodbg_begin(Edit->getReg());
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000617 MachineInstr *MI = RI.skipInstruction();)
618 anyRemat |= reMaterializeFor(MI);
619
620 if (!anyRemat)
621 return;
622
623 // Remove any values that were completely rematted.
624 bool anyRemoved = false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000625 for (LiveInterval::vni_iterator I = Edit->getParent().vni_begin(),
626 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000627 VNInfo *VNI = *I;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000628 if (VNI->hasPHIKill() || !Edit->didRematerialize(VNI) ||
629 UsedValues.count(VNI))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000630 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000631 MachineInstr *DefMI = LIS.getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000632 DEBUG(dbgs() << "\tremoving dead def: " << VNI->def << '\t' << *DefMI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000633 LIS.RemoveMachineInstrFromMaps(DefMI);
634 VRM.RemoveMachineInstrFromMaps(DefMI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000635 DefMI->eraseFromParent();
Lang Hamescec29452010-09-26 03:37:09 +0000636 VNI->def = SlotIndex();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000637 anyRemoved = true;
638 }
639
640 if (!anyRemoved)
641 return;
642
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000643 // Removing values may cause debug uses where parent is not live.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000644 for (MachineRegisterInfo::use_iterator RI = MRI.use_begin(Edit->getReg());
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000645 MachineInstr *MI = RI.skipInstruction();) {
646 if (!MI->isDebugValue())
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000647 continue;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000648 // Try to preserve the debug value if parent is live immediately after it.
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000649 MachineBasicBlock::iterator NextMI = MI;
650 ++NextMI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000651 if (NextMI != MI->getParent()->end() && !LIS.isNotInMIMap(NextMI)) {
652 SlotIndex Idx = LIS.getInstructionIndex(NextMI);
653 VNInfo *VNI = Edit->getParent().getVNInfoAt(Idx);
654 if (VNI && (VNI->hasPHIKill() || UsedValues.count(VNI)))
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000655 continue;
656 }
657 DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000658 MI->eraseFromParent();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000659 }
660}
661
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000662/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000663bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000664 int FI = 0;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000665 unsigned InstrReg;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000666 if (!(InstrReg = TII.isLoadFromStackSlot(MI, FI)) &&
667 !(InstrReg = TII.isStoreToStackSlot(MI, FI)))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000668 return false;
669
670 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000671 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000672 return false;
673
674 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000675 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000676 MI->eraseFromParent();
677 return true;
678}
679
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000680/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000681/// @param MI Instruction using or defining the current register.
Jakob Stoklund Olesen39048252010-12-18 03:28:32 +0000682/// @param Ops Operand indices from readsWritesVirtualRegister().
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000683/// @param LoadMI Load instruction to use instead of stack slot when non-null.
684/// @return True on success, and MI will be erased.
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000685bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000686 const SmallVectorImpl<unsigned> &Ops,
687 MachineInstr *LoadMI) {
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000688 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
689 // operands.
690 SmallVector<unsigned, 8> FoldOps;
691 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
692 unsigned Idx = Ops[i];
693 MachineOperand &MO = MI->getOperand(Idx);
694 if (MO.isImplicit())
695 continue;
696 // FIXME: Teach targets to deal with subregs.
697 if (MO.getSubReg())
698 return false;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000699 // We cannot fold a load instruction into a def.
700 if (LoadMI && MO.isDef())
701 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000702 // Tied use operands should not be passed to foldMemoryOperand.
703 if (!MI->isRegTiedToDefOperand(Idx))
704 FoldOps.push_back(Idx);
705 }
706
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000707 MachineInstr *FoldMI =
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000708 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
709 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000710 if (!FoldMI)
711 return false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000712 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000713 if (!LoadMI)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000714 VRM.addSpillSlotUse(StackSlot, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000715 MI->eraseFromParent();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000716 DEBUG(dbgs() << "\tfolded: " << *FoldMI);
717 return true;
718}
719
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000720/// insertReload - Insert a reload of NewLI.reg before MI.
721void InlineSpiller::insertReload(LiveInterval &NewLI,
722 MachineBasicBlock::iterator MI) {
723 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000724 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
725 TII.loadRegFromStackSlot(MBB, MI, NewLI.reg, StackSlot, RC, &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000726 --MI; // Point to load instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000727 SlotIndex LoadIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
728 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000729 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
Lang Hames6e2968c2010-09-25 12:04:16 +0000730 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000731 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000732 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
733}
734
735/// insertSpill - Insert a spill of NewLI.reg after MI.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000736void InlineSpiller::insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000737 MachineBasicBlock::iterator MI) {
738 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesen68257e62010-11-15 20:55:49 +0000739
740 // Get the defined value. It could be an early clobber so keep the def index.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000741 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000742 VNInfo *VNI = OldLI.getVNInfoAt(Idx);
Jakob Stoklund Olesen68257e62010-11-15 20:55:49 +0000743 assert(VNI && VNI->def.getDefIndex() == Idx && "Inconsistent VNInfo");
744 Idx = VNI->def;
745
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000746 TII.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, StackSlot, RC, &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000747 --MI; // Point to store instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000748 SlotIndex StoreIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
749 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000750 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000751 VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000752 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
753}
754
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000755/// spillAroundUses - insert spill code around each use of Reg.
756void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000757 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000758
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000759 // Iterate over instructions using Reg.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000760 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000761 MachineInstr *MI = RI.skipInstruction();) {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000762
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000763 // Debug values are not allowed to affect codegen.
764 if (MI->isDebugValue()) {
765 // Modify DBG_VALUE now that the value is in a spill slot.
766 uint64_t Offset = MI->getOperand(1).getImm();
767 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
768 DebugLoc DL = MI->getDebugLoc();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000769 if (MachineInstr *NewDV = TII.emitFrameIndexDebugValue(MF, StackSlot,
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000770 Offset, MDPtr, DL)) {
771 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
772 MachineBasicBlock *MBB = MI->getParent();
773 MBB->insert(MBB->erase(MI), NewDV);
774 } else {
775 DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
776 MI->eraseFromParent();
777 }
778 continue;
779 }
780
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000781 // Ignore copies to/from snippets. We'll delete them.
782 if (SnippetCopies.count(MI))
783 continue;
784
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000785 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000786 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000787 continue;
788
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000789 // Analyze instruction.
790 bool Reads, Writes;
791 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000792 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Reg, &Ops);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000793
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000794 // Check for a sibling copy.
795 unsigned SibReg = isFullCopyOf(MI, Reg);
796 if (!isSibling(SibReg))
797 SibReg = 0;
798
799 // Hoist the spill of a sib-reg copy.
800 if (SibReg && Writes && !Reads && hoistSpill(OldLI, MI)) {
801 // This COPY is now dead, the value is already in the stack slot.
802 MI->getOperand(0).setIsDead();
803 DeadDefs.push_back(MI);
804 continue;
805 }
806
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000807 // Attempt to fold memory ops.
808 if (foldMemoryOperand(MI, Ops))
809 continue;
810
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000811 // Allocate interval around instruction.
812 // FIXME: Infer regclass from instruction alone.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000813 LiveInterval &NewLI = Edit->create(LIS, VRM);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000814 NewLI.markNotSpillable();
815
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000816 if (Reads)
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000817 insertReload(NewLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000818
819 // Rewrite instruction operands.
820 bool hasLiveDef = false;
821 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
822 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000823 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000824 if (MO.isUse()) {
825 if (!MI->isRegTiedToDefOperand(Ops[i]))
826 MO.setIsKill();
827 } else {
828 if (!MO.isDead())
829 hasLiveDef = true;
830 }
831 }
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000832
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000833 // FIXME: Use a second vreg if instruction has no tied ops.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000834 if (Writes && hasLiveDef)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000835 insertSpill(NewLI, OldLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000836
837 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000838 }
839}
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000840
841void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000842 Edit = &edit;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000843 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
844 && "Trying to spill a stack slot.");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000845 // Share a stack slot among all descendants of Original.
846 Original = VRM.getOriginal(edit.getReg());
847 StackSlot = VRM.getStackSlot(Original);
848
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000849 DEBUG(dbgs() << "Inline spilling "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000850 << MRI.getRegClass(edit.getReg())->getName()
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000851 << ':' << edit.getParent() << "\nFrom original "
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000852 << LIS.getInterval(Original) << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000853 assert(edit.getParent().isSpillable() &&
854 "Attempting to spill already spilled value.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000855 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000856
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000857 collectRegsToSpill();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000858 analyzeSiblingValues();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000859 reMaterializeAll();
860
861 // Remat may handle everything.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000862 if (Edit->getParent().empty())
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000863 return;
864
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000865 RC = MRI.getRegClass(edit.getReg());
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000866
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000867 if (StackSlot == VirtRegMap::NO_STACK_SLOT)
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000868 StackSlot = VRM.assignVirt2StackSlot(Original);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000869
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000870 if (Original != edit.getReg())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000871 VRM.assignVirt2StackSlot(edit.getReg(), StackSlot);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000872
873 // Update LiveStacks now that we are committed to spilling.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000874 LiveInterval &stacklvr = LSS.getOrCreateInterval(StackSlot, RC);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000875 if (!stacklvr.hasAtLeastOneValue())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000876 stacklvr.getNextValue(SlotIndex(), 0, LSS.getVNInfoAllocator());
Jakob Stoklund Olesenb1adbd12011-03-12 04:25:36 +0000877 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000878 stacklvr.MergeRangesInAsValue(LIS.getInterval(RegsToSpill[i]),
Jakob Stoklund Olesenb1adbd12011-03-12 04:25:36 +0000879 stacklvr.getValNumInfo(0));
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +0000880 DEBUG(dbgs() << "Merged spilled regs: " << stacklvr << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000881
882 // Spill around uses of all RegsToSpill.
883 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
884 spillAroundUses(RegsToSpill[i]);
885
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000886 // Hoisted spills may cause dead code.
887 if (!DeadDefs.empty()) {
888 DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
889 Edit->eliminateDeadDefs(DeadDefs, LIS, VRM, TII);
890 }
891
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000892 // Finally delete the SnippetCopies.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000893 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(edit.getReg());
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000894 MachineInstr *MI = RI.skipInstruction();) {
895 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
896 // FIXME: Do this with a LiveRangeEdit callback.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000897 VRM.RemoveMachineInstrFromMaps(MI);
898 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000899 MI->eraseFromParent();
900 }
901
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000902 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000903 edit.eraseVirtReg(RegsToSpill[i], LIS);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000904}