blob: b1a33a6afa42c3a6f20cfe4838f116dd3c707a0f [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;
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +000051 LiveInterval *StackInt;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000052 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 Olesen2ef661b2011-03-29 03:12:02 +0000123 MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000124 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 Olesen2ef661b2011-03-29 03:12:02 +0000129 void markValueUsed(LiveInterval*, VNInfo*);
130 bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000131 void reMaterializeAll();
132
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000133 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000134 bool foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000135 const SmallVectorImpl<unsigned> &Ops,
136 MachineInstr *LoadMI = 0);
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000137 void insertReload(LiveInterval &NewLI, SlotIndex,
138 MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000139 void insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000140 SlotIndex, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000141
142 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000143 void spillAll();
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000144};
145}
146
147namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000148Spiller *createInlineSpiller(MachineFunctionPass &pass,
149 MachineFunction &mf,
150 VirtRegMap &vrm) {
151 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000152}
153}
154
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000155//===----------------------------------------------------------------------===//
156// Snippets
157//===----------------------------------------------------------------------===//
158
159// When spilling a virtual register, we also spill any snippets it is connected
160// to. The snippets are small live ranges that only have a single real use,
161// leftovers from live range splitting. Spilling them enables memory operand
162// folding or tightens the live range around the single use.
163//
164// This minimizes register pressure and maximizes the store-to-load distance for
165// spill slots which can be important in tight loops.
166
167/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
168/// otherwise return 0.
169static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
170 if (!MI->isCopy())
171 return 0;
172 if (MI->getOperand(0).getSubReg() != 0)
173 return 0;
174 if (MI->getOperand(1).getSubReg() != 0)
175 return 0;
176 if (MI->getOperand(0).getReg() == Reg)
177 return MI->getOperand(1).getReg();
178 if (MI->getOperand(1).getReg() == Reg)
179 return MI->getOperand(0).getReg();
180 return 0;
181}
182
183/// isSnippet - Identify if a live interval is a snippet that should be spilled.
184/// It is assumed that SnipLI is a virtual register with the same original as
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000185/// Edit->getReg().
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000186bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000187 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000188
189 // A snippet is a tiny live range with only a single instruction using it
190 // besides copies to/from Reg or spills/fills. We accept:
191 //
192 // %snip = COPY %Reg / FILL fi#
193 // %snip = USE %snip
194 // %Reg = COPY %snip / SPILL %snip, fi#
195 //
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000196 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000197 return false;
198
199 MachineInstr *UseMI = 0;
200
201 // Check that all uses satisfy our criteria.
202 for (MachineRegisterInfo::reg_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000203 RI = MRI.reg_nodbg_begin(SnipLI.reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000204 MachineInstr *MI = RI.skipInstruction();) {
205
206 // Allow copies to/from Reg.
207 if (isFullCopyOf(MI, Reg))
208 continue;
209
210 // Allow stack slot loads.
211 int FI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000212 if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000213 continue;
214
215 // Allow stack slot stores.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000216 if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000217 continue;
218
219 // Allow a single additional instruction.
220 if (UseMI && MI != UseMI)
221 return false;
222 UseMI = MI;
223 }
224 return true;
225}
226
227/// collectRegsToSpill - Collect live range snippets that only have a single
228/// real use.
229void InlineSpiller::collectRegsToSpill() {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000230 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000231
232 // Main register always spills.
233 RegsToSpill.assign(1, Reg);
234 SnippetCopies.clear();
235
236 // Snippets all have the same original, so there can't be any for an original
237 // register.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000238 if (Original == Reg)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000239 return;
240
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000241 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000242 MachineInstr *MI = RI.skipInstruction();) {
243 unsigned SnipReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000244 if (!isSibling(SnipReg))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000245 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000246 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000247 if (!isSnippet(SnipLI))
248 continue;
249 SnippetCopies.insert(MI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000250 if (!isRegToSpill(SnipReg))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000251 RegsToSpill.push_back(SnipReg);
252
253 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
254 }
255}
256
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000257
258//===----------------------------------------------------------------------===//
259// Sibling Values
260//===----------------------------------------------------------------------===//
261
262// After live range splitting, some values to be spilled may be defined by
263// copies from sibling registers. We trace the sibling copies back to the
264// original value if it still exists. We need it for rematerialization.
265//
266// Even when the value can't be rematerialized, we still want to determine if
267// the value has already been spilled, or we may want to hoist the spill from a
268// loop.
269
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000270bool InlineSpiller::isSibling(unsigned Reg) {
271 return TargetRegisterInfo::isVirtualRegister(Reg) &&
272 VRM.getOriginal(Reg) == Original;
273}
274
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000275/// traceSiblingValue - Trace a value that is about to be spilled back to the
276/// real defining instructions by looking through sibling copies. Always stay
277/// within the range of OrigVNI so the registers are known to carry the same
278/// value.
279///
280/// Determine if the value is defined by all reloads, so spilling isn't
281/// necessary - the value is already in the stack slot.
282///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000283/// Return a defining instruction that may be a candidate for rematerialization.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000284///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000285MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
286 VNInfo *OrigVNI) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000287 DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
288 << UseVNI->id << '@' << UseVNI->def << '\n');
289 SmallPtrSet<VNInfo*, 8> Visited;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000290 SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000291 WorkList.push_back(std::make_pair(UseReg, UseVNI));
292
293 // Best spill candidate seen so far. This must dominate UseVNI.
294 SibValueInfo SVI(UseReg, UseVNI);
295 MachineBasicBlock *UseMBB = LIS.getMBBFromIndex(UseVNI->def);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000296 unsigned SpillDepth = Loops.getLoopDepth(UseMBB);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000297 bool SeenOrigPHI = false; // Original PHI met.
298
299 do {
300 unsigned Reg;
301 VNInfo *VNI;
302 tie(Reg, VNI) = WorkList.pop_back_val();
303 if (!Visited.insert(VNI))
304 continue;
305
306 // Is this value a better spill candidate?
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000307 if (!isRegToSpill(Reg)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000308 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000309 if (MBB != UseMBB && MDT.dominates(MBB, UseMBB)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000310 // This is a valid spill location dominating UseVNI.
311 // Prefer to spill at a smaller loop depth.
312 unsigned Depth = Loops.getLoopDepth(MBB);
313 if (Depth < SpillDepth) {
314 DEBUG(dbgs() << " spill depth " << Depth << ": " << PrintReg(Reg)
315 << ':' << VNI->id << '@' << VNI->def << '\n');
316 SVI.SpillReg = Reg;
317 SVI.SpillVNI = VNI;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000318 SpillDepth = Depth;
319 }
320 }
321 }
322
323 // Trace through PHI-defs created by live range splitting.
324 if (VNI->isPHIDef()) {
325 if (VNI->def == OrigVNI->def) {
326 DEBUG(dbgs() << " orig phi value " << PrintReg(Reg) << ':'
327 << VNI->id << '@' << VNI->def << '\n');
328 SeenOrigPHI = true;
329 continue;
330 }
331 // Get values live-out of predecessors.
332 LiveInterval &LI = LIS.getInterval(Reg);
333 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
334 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
335 PE = MBB->pred_end(); PI != PE; ++PI) {
336 VNInfo *PVNI = LI.getVNInfoAt(LIS.getMBBEndIdx(*PI).getPrevSlot());
337 if (PVNI)
338 WorkList.push_back(std::make_pair(Reg, PVNI));
339 }
340 continue;
341 }
342
343 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
344 assert(MI && "Missing def");
345
346 // Trace through sibling copies.
347 if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000348 if (isSibling(SrcReg)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000349 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
350 VNInfo *SrcVNI = SrcLI.getVNInfoAt(VNI->def.getUseIndex());
351 assert(SrcVNI && "Copy from non-existing value");
352 DEBUG(dbgs() << " copy of " << PrintReg(SrcReg) << ':'
353 << SrcVNI->id << '@' << SrcVNI->def << '\n');
354 WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
355 continue;
356 }
357 }
358
359 // Track reachable reloads.
360 int FI;
361 if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
362 DEBUG(dbgs() << " reload " << PrintReg(Reg) << ':'
363 << VNI->id << "@" << VNI->def << '\n');
364 SVI.AllDefsAreReloads = true;
365 continue;
366 }
367
368 // We have an 'original' def. Don't record trivial cases.
369 if (VNI == UseVNI) {
370 DEBUG(dbgs() << "Not a sibling copy.\n");
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000371 return MI;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000372 }
373
374 // Potential remat candidate.
375 DEBUG(dbgs() << " def " << PrintReg(Reg) << ':'
376 << VNI->id << '@' << VNI->def << '\t' << *MI);
377 SVI.DefMI = MI;
378 } while (!WorkList.empty());
379
380 if (SeenOrigPHI || SVI.DefMI)
381 SVI.AllDefsAreReloads = false;
382
383 DEBUG({
384 if (SVI.AllDefsAreReloads)
385 dbgs() << "All defs are reloads.\n";
386 else
387 dbgs() << "Prefer to spill " << PrintReg(SVI.SpillReg) << ':'
388 << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def << '\n';
389 });
390 SibValues.insert(std::make_pair(UseVNI, SVI));
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000391 return SVI.DefMI;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000392}
393
394/// analyzeSiblingValues - Trace values defined by sibling copies back to
395/// something that isn't a sibling copy.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000396///
397/// Keep track of values that may be rematerializable.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000398void InlineSpiller::analyzeSiblingValues() {
399 SibValues.clear();
400
401 // No siblings at all?
402 if (Edit->getReg() == Original)
403 return;
404
405 LiveInterval &OrigLI = LIS.getInterval(Original);
406 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
407 unsigned Reg = RegsToSpill[i];
408 LiveInterval &LI = LIS.getInterval(Reg);
409 for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
410 VE = LI.vni_end(); VI != VE; ++VI) {
411 VNInfo *VNI = *VI;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000412 if (VNI->isUnused())
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000413 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000414 MachineInstr *DefMI = 0;
415 // Check possible sibling copies.
416 if (VNI->isPHIDef() || VNI->getCopy()) {
417 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
418 if (OrigVNI->def != VNI->def)
419 DefMI = traceSiblingValue(Reg, VNI, OrigVNI);
420 }
421 if (!DefMI && !VNI->isPHIDef())
422 DefMI = LIS.getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen3b7d9172011-04-20 22:14:20 +0000423 if (DefMI && Edit->checkRematerializable(VNI, DefMI, TII, AA)) {
424 DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@'
425 << VNI->def << " may remat from " << *DefMI);
426 }
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000427 }
428 }
429}
430
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000431/// hoistSpill - Given a sibling copy that defines a value to be spilled, insert
432/// a spill at a better location.
433bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) {
434 SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
435 VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getDefIndex());
436 assert(VNI && VNI->def == Idx.getDefIndex() && "Not defined by copy");
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000437 SibValueMap::iterator I = SibValues.find(VNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000438 if (I == SibValues.end())
439 return false;
440
441 const SibValueInfo &SVI = I->second;
442
443 // Let the normal folding code deal with the boring case.
444 if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI)
445 return false;
446
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000447 // SpillReg may have been deleted by remat and DCE.
448 if (!LIS.hasInterval(SVI.SpillReg)) {
449 DEBUG(dbgs() << "Stale interval: " << PrintReg(SVI.SpillReg) << '\n');
450 SibValues.erase(I);
451 return false;
452 }
453
454 LiveInterval &SibLI = LIS.getInterval(SVI.SpillReg);
455 if (!SibLI.containsValue(SVI.SpillVNI)) {
456 DEBUG(dbgs() << "Stale value: " << PrintReg(SVI.SpillReg) << '\n');
457 SibValues.erase(I);
458 return false;
459 }
460
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000461 // Conservatively extend the stack slot range to the range of the original
462 // value. We may be able to do better with stack slot coloring by being more
463 // careful here.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000464 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000465 LiveInterval &OrigLI = LIS.getInterval(Original);
466 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000467 StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +0000468 DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000469 << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000470
471 // Already spilled everywhere.
472 if (SVI.AllDefsAreReloads)
473 return true;
474
475 // We are going to spill SVI.SpillVNI immediately after its def, so clear out
476 // any later spills of the same value.
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000477 eliminateRedundantSpills(SibLI, SVI.SpillVNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000478
479 MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def);
480 MachineBasicBlock::iterator MII;
481 if (SVI.SpillVNI->isPHIDef())
482 MII = MBB->SkipPHIsAndLabels(MBB->begin());
483 else {
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000484 MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
485 assert(DefMI && "Defining instruction disappeared");
486 MII = DefMI;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000487 ++MII;
488 }
489 // Insert spill without kill flag immediately after def.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000490 TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot,
491 MRI.getRegClass(SVI.SpillReg), &TRI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000492 --MII; // Point to store instruction.
493 LIS.InsertMachineInstrInMaps(MII);
494 VRM.addSpillSlotUse(StackSlot, MII);
495 DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
496 return true;
497}
498
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000499/// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
500/// redundant spills of this value in SLI.reg and sibling copies.
501void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +0000502 assert(VNI && "Missing value");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000503 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
504 WorkList.push_back(std::make_pair(&SLI, VNI));
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000505 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000506
507 do {
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000508 LiveInterval *LI;
509 tie(LI, VNI) = WorkList.pop_back_val();
510 unsigned Reg = LI->reg;
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000511 DEBUG(dbgs() << "Checking redundant spills for "
512 << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000513
514 // Regs to spill are taken care of.
515 if (isRegToSpill(Reg))
516 continue;
517
518 // Add all of VNI's live range to StackInt.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000519 StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
520 DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000521
522 // Find all spills and copies of VNI.
523 for (MachineRegisterInfo::use_nodbg_iterator UI = MRI.use_nodbg_begin(Reg);
524 MachineInstr *MI = UI.skipInstruction();) {
525 if (!MI->isCopy() && !MI->getDesc().mayStore())
526 continue;
527 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000528 if (LI->getVNInfoAt(Idx) != VNI)
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000529 continue;
530
531 // Follow sibling copies down the dominator tree.
532 if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
533 if (isSibling(DstReg)) {
534 LiveInterval &DstLI = LIS.getInterval(DstReg);
535 VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getDefIndex());
536 assert(DstVNI && "Missing defined value");
537 assert(DstVNI->def == Idx.getDefIndex() && "Wrong copy def slot");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000538 WorkList.push_back(std::make_pair(&DstLI, DstVNI));
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000539 }
540 continue;
541 }
542
543 // Erase spills.
544 int FI;
545 if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
546 DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI);
547 // eliminateDeadDefs won't normally remove stores, so switch opcode.
548 MI->setDesc(TII.get(TargetOpcode::KILL));
549 DeadDefs.push_back(MI);
550 }
551 }
552 } while (!WorkList.empty());
553}
554
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000555
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000556//===----------------------------------------------------------------------===//
557// Rematerialization
558//===----------------------------------------------------------------------===//
559
560/// markValueUsed - Remember that VNI failed to rematerialize, so its defining
561/// instruction cannot be eliminated. See through snippet copies
562void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
563 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
564 WorkList.push_back(std::make_pair(LI, VNI));
565 do {
566 tie(LI, VNI) = WorkList.pop_back_val();
567 if (!UsedValues.insert(VNI))
568 continue;
569
570 if (VNI->isPHIDef()) {
571 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
572 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
573 PE = MBB->pred_end(); PI != PE; ++PI) {
574 VNInfo *PVNI = LI->getVNInfoAt(LIS.getMBBEndIdx(*PI).getPrevSlot());
575 if (PVNI)
576 WorkList.push_back(std::make_pair(LI, PVNI));
577 }
578 continue;
579 }
580
581 // Follow snippet copies.
582 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
583 if (!SnippetCopies.count(MI))
584 continue;
585 LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
586 assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
587 VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getUseIndex());
588 assert(SnipVNI && "Snippet undefined before copy");
589 WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
590 } while (!WorkList.empty());
591}
592
593/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
594bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg,
595 MachineBasicBlock::iterator MI) {
596 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getUseIndex();
597 VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx);
598
599 if (!ParentVNI) {
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000600 DEBUG(dbgs() << "\tadding <undef> flags: ");
601 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
602 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000603 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000604 MO.setIsUndef();
605 }
606 DEBUG(dbgs() << UseIdx << '\t' << *MI);
607 return true;
608 }
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000609
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000610 if (SnippetCopies.count(MI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000611 return false;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000612
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000613 // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy.
614 LiveRangeEdit::Remat RM(ParentVNI);
615 SibValueMap::const_iterator SibI = SibValues.find(ParentVNI);
616 if (SibI != SibValues.end())
617 RM.OrigMI = SibI->second.DefMI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000618 if (!Edit->canRematerializeAt(RM, UseIdx, false, LIS)) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000619 markValueUsed(&VirtReg, ParentVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000620 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
621 return false;
622 }
623
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000624 // If the instruction also writes VirtReg.reg, it had better not require the
625 // same register for uses and defs.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000626 bool Reads, Writes;
627 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000628 tie(Reads, Writes) = MI->readsWritesVirtualRegister(VirtReg.reg, &Ops);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000629 if (Writes) {
630 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
631 MachineOperand &MO = MI->getOperand(Ops[i]);
632 if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000633 markValueUsed(&VirtReg, ParentVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000634 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
635 return false;
636 }
637 }
638 }
639
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000640 // Before rematerializing into a register for a single instruction, try to
641 // fold a load into the instruction. That avoids allocating a new register.
642 if (RM.OrigMI->getDesc().canFoldAsLoad() &&
643 foldMemoryOperand(MI, Ops, RM.OrigMI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000644 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000645 return true;
646 }
647
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000648 // Alocate a new register for the remat.
Jakob Stoklund Olesen312babc2011-03-31 03:54:44 +0000649 LiveInterval &NewLI = Edit->createFrom(Original, LIS, VRM);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000650 NewLI.markNotSpillable();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000651
652 // Finally we can rematerialize OrigMI before MI.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000653 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
654 LIS, TII, TRI);
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000655 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000656 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000657
658 // Replace operands
659 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
660 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000661 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000662 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000663 MO.setIsKill();
664 }
665 }
666 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
667
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000668 VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000669 NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000670 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000671 return true;
672}
673
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000674/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000675/// and trim the live ranges after.
676void InlineSpiller::reMaterializeAll() {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000677 // analyzeSiblingValues has already tested all relevant defining instructions.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000678 if (!Edit->anyRematerializable(LIS, TII, AA))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000679 return;
680
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000681 UsedValues.clear();
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000682
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000683 // Try to remat before all uses of snippets.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000684 bool anyRemat = false;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000685 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
686 unsigned Reg = RegsToSpill[i];
687 LiveInterval &LI = LIS.getInterval(Reg);
688 for (MachineRegisterInfo::use_nodbg_iterator
689 RI = MRI.use_nodbg_begin(Reg);
690 MachineInstr *MI = RI.skipInstruction();)
691 anyRemat |= reMaterializeFor(LI, MI);
692 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000693 if (!anyRemat)
694 return;
695
696 // Remove any values that were completely rematted.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000697 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
698 unsigned Reg = RegsToSpill[i];
699 LiveInterval &LI = LIS.getInterval(Reg);
700 for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
701 I != E; ++I) {
702 VNInfo *VNI = *I;
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000703 if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000704 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000705 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
706 MI->addRegisterDead(Reg, &TRI);
707 if (!MI->allDefsAreDead())
708 continue;
709 DEBUG(dbgs() << "All defs dead: " << *MI);
710 DeadDefs.push_back(MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000711 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000712 }
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000713
714 // Eliminate dead code after remat. Note that some snippet copies may be
715 // deleted here.
716 if (DeadDefs.empty())
717 return;
718 DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
719 Edit->eliminateDeadDefs(DeadDefs, LIS, VRM, TII);
720
721 // Get rid of deleted and empty intervals.
722 for (unsigned i = RegsToSpill.size(); i != 0; --i) {
723 unsigned Reg = RegsToSpill[i-1];
724 if (!LIS.hasInterval(Reg)) {
725 RegsToSpill.erase(RegsToSpill.begin() + (i - 1));
726 continue;
727 }
728 LiveInterval &LI = LIS.getInterval(Reg);
729 if (!LI.empty())
730 continue;
731 Edit->eraseVirtReg(Reg, LIS);
732 RegsToSpill.erase(RegsToSpill.begin() + (i - 1));
733 }
734 DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000735}
736
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000737
738//===----------------------------------------------------------------------===//
739// Spilling
740//===----------------------------------------------------------------------===//
741
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000742/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000743bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000744 int FI = 0;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000745 unsigned InstrReg;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000746 if (!(InstrReg = TII.isLoadFromStackSlot(MI, FI)) &&
747 !(InstrReg = TII.isStoreToStackSlot(MI, FI)))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000748 return false;
749
750 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000751 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000752 return false;
753
754 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000755 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000756 MI->eraseFromParent();
757 return true;
758}
759
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000760/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000761/// @param MI Instruction using or defining the current register.
Jakob Stoklund Olesen39048252010-12-18 03:28:32 +0000762/// @param Ops Operand indices from readsWritesVirtualRegister().
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000763/// @param LoadMI Load instruction to use instead of stack slot when non-null.
764/// @return True on success, and MI will be erased.
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000765bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000766 const SmallVectorImpl<unsigned> &Ops,
767 MachineInstr *LoadMI) {
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000768 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
769 // operands.
770 SmallVector<unsigned, 8> FoldOps;
771 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
772 unsigned Idx = Ops[i];
773 MachineOperand &MO = MI->getOperand(Idx);
774 if (MO.isImplicit())
775 continue;
776 // FIXME: Teach targets to deal with subregs.
777 if (MO.getSubReg())
778 return false;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000779 // We cannot fold a load instruction into a def.
780 if (LoadMI && MO.isDef())
781 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000782 // Tied use operands should not be passed to foldMemoryOperand.
783 if (!MI->isRegTiedToDefOperand(Idx))
784 FoldOps.push_back(Idx);
785 }
786
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000787 MachineInstr *FoldMI =
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000788 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
789 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000790 if (!FoldMI)
791 return false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000792 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000793 if (!LoadMI)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000794 VRM.addSpillSlotUse(StackSlot, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000795 MI->eraseFromParent();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000796 DEBUG(dbgs() << "\tfolded: " << *FoldMI);
797 return true;
798}
799
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000800/// insertReload - Insert a reload of NewLI.reg before MI.
801void InlineSpiller::insertReload(LiveInterval &NewLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000802 SlotIndex Idx,
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000803 MachineBasicBlock::iterator MI) {
804 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000805 TII.loadRegFromStackSlot(MBB, MI, NewLI.reg, StackSlot,
806 MRI.getRegClass(NewLI.reg), &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000807 --MI; // Point to load instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000808 SlotIndex LoadIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
809 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000810 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
Lang Hames6e2968c2010-09-25 12:04:16 +0000811 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000812 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000813 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
814}
815
816/// insertSpill - Insert a spill of NewLI.reg after MI.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000817void InlineSpiller::insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000818 SlotIndex Idx, MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000819 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000820 TII.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, StackSlot,
821 MRI.getRegClass(NewLI.reg), &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000822 --MI; // Point to store instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000823 SlotIndex StoreIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
824 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000825 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000826 VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000827 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
828}
829
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000830/// spillAroundUses - insert spill code around each use of Reg.
831void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000832 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000833
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000834 // Iterate over instructions using Reg.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000835 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000836 MachineInstr *MI = RI.skipInstruction();) {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000837
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000838 // Debug values are not allowed to affect codegen.
839 if (MI->isDebugValue()) {
840 // Modify DBG_VALUE now that the value is in a spill slot.
841 uint64_t Offset = MI->getOperand(1).getImm();
842 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
843 DebugLoc DL = MI->getDebugLoc();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000844 if (MachineInstr *NewDV = TII.emitFrameIndexDebugValue(MF, StackSlot,
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000845 Offset, MDPtr, DL)) {
846 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
847 MachineBasicBlock *MBB = MI->getParent();
848 MBB->insert(MBB->erase(MI), NewDV);
849 } else {
850 DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
851 MI->eraseFromParent();
852 }
853 continue;
854 }
855
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000856 // Ignore copies to/from snippets. We'll delete them.
857 if (SnippetCopies.count(MI))
858 continue;
859
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000860 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000861 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000862 continue;
863
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000864 // Analyze instruction.
865 bool Reads, Writes;
866 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000867 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Reg, &Ops);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000868
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000869 // Find the slot index where this instruction reads and writes OldLI.
870 // This is usually the def slot, except for tied early clobbers.
871 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
872 if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getUseIndex()))
873 if (SlotIndex::isSameInstr(Idx, VNI->def))
874 Idx = VNI->def;
875
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000876 // Check for a sibling copy.
877 unsigned SibReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +0000878 if (SibReg && isSibling(SibReg)) {
879 if (Writes) {
880 // Hoist the spill of a sib-reg copy.
881 if (hoistSpill(OldLI, MI)) {
882 // This COPY is now dead, the value is already in the stack slot.
883 MI->getOperand(0).setIsDead();
884 DeadDefs.push_back(MI);
885 continue;
886 }
887 } else {
888 // This is a reload for a sib-reg copy. Drop spills downstream.
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +0000889 LiveInterval &SibLI = LIS.getInterval(SibReg);
890 eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
891 // The COPY will fold to a reload below.
892 }
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000893 }
894
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000895 // Attempt to fold memory ops.
896 if (foldMemoryOperand(MI, Ops))
897 continue;
898
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000899 // Allocate interval around instruction.
900 // FIXME: Infer regclass from instruction alone.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000901 LiveInterval &NewLI = Edit->createFrom(Reg, LIS, VRM);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000902 NewLI.markNotSpillable();
903
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000904 if (Reads)
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000905 insertReload(NewLI, Idx, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000906
907 // Rewrite instruction operands.
908 bool hasLiveDef = false;
909 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
910 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000911 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000912 if (MO.isUse()) {
913 if (!MI->isRegTiedToDefOperand(Ops[i]))
914 MO.setIsKill();
915 } else {
916 if (!MO.isDead())
917 hasLiveDef = true;
918 }
919 }
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000920 DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000921
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000922 // FIXME: Use a second vreg if instruction has no tied ops.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000923 if (Writes && hasLiveDef)
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000924 insertSpill(NewLI, OldLI, Idx, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000925
926 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000927 }
928}
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000929
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000930/// spillAll - Spill all registers remaining after rematerialization.
931void InlineSpiller::spillAll() {
932 // Update LiveStacks now that we are committed to spilling.
933 if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
934 StackSlot = VRM.assignVirt2StackSlot(Original);
935 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
936 StackInt->getNextValue(SlotIndex(), 0, LSS.getVNInfoAllocator());
937 } else
938 StackInt = &LSS.getInterval(StackSlot);
939
940 if (Original != Edit->getReg())
941 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
942
943 assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
944 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
945 StackInt->MergeRangesInAsValue(LIS.getInterval(RegsToSpill[i]),
946 StackInt->getValNumInfo(0));
947 DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
948
949 // Spill around uses of all RegsToSpill.
950 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
951 spillAroundUses(RegsToSpill[i]);
952
953 // Hoisted spills may cause dead code.
954 if (!DeadDefs.empty()) {
955 DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
956 Edit->eliminateDeadDefs(DeadDefs, LIS, VRM, TII);
957 }
958
959 // Finally delete the SnippetCopies.
960 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Edit->getReg());
961 MachineInstr *MI = RI.skipInstruction();) {
962 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
963 // FIXME: Do this with a LiveRangeEdit callback.
964 VRM.RemoveMachineInstrFromMaps(MI);
965 LIS.RemoveMachineInstrFromMaps(MI);
966 MI->eraseFromParent();
967 }
968
969 // Delete all spilled registers.
970 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
971 Edit->eraseVirtReg(RegsToSpill[i], LIS);
972}
973
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000974void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000975 Edit = &edit;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000976 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
977 && "Trying to spill a stack slot.");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000978 // Share a stack slot among all descendants of Original.
979 Original = VRM.getOriginal(edit.getReg());
980 StackSlot = VRM.getStackSlot(Original);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000981 StackInt = 0;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000982
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000983 DEBUG(dbgs() << "Inline spilling "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000984 << MRI.getRegClass(edit.getReg())->getName()
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000985 << ':' << edit.getParent() << "\nFrom original "
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000986 << LIS.getInterval(Original) << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000987 assert(edit.getParent().isSpillable() &&
988 "Attempting to spill already spilled value.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000989 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000990
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000991 collectRegsToSpill();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000992 analyzeSiblingValues();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000993 reMaterializeAll();
994
995 // Remat may handle everything.
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000996 if (!RegsToSpill.empty())
997 spillAll();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000998
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000999 Edit->calculateRegClassAndHint(MF, LIS, Loops);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001000}