blob: 169a867aa7a5500eabc2eeb1936fc5c1b657c947 [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 Olesene9bd4ea2011-05-05 17:22:53 +000019#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000020#include "llvm/ADT/TinyPtrVector.h"
Jakob Stoklund Olesene93198a2010-11-10 23:55:56 +000021#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000022#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen0a12b802010-10-26 00:11:35 +000023#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000024#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33
34using namespace llvm;
35
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000036STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000037STATISTIC(NumSnippets, "Number of spilled snippets");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000038STATISTIC(NumSpills, "Number of spills inserted");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000039STATISTIC(NumSpillsRemoved, "Number of spills removed");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000040STATISTIC(NumReloads, "Number of reloads inserted");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000041STATISTIC(NumReloadsRemoved, "Number of reloads removed");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000042STATISTIC(NumFolded, "Number of folded stack accesses");
43STATISTIC(NumFoldedLoads, "Number of folded loads");
44STATISTIC(NumRemats, "Number of rematerialized defs for spilling");
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +000045STATISTIC(NumOmitReloadSpill, "Number of omitted spills of reloads");
46STATISTIC(NumHoists, "Number of hoisted spills");
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +000047
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000048namespace {
49class InlineSpiller : public Spiller {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000050 MachineFunctionPass &Pass;
51 MachineFunction &MF;
52 LiveIntervals &LIS;
53 LiveStacks &LSS;
54 AliasAnalysis *AA;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000055 MachineDominatorTree &MDT;
56 MachineLoopInfo &Loops;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000057 VirtRegMap &VRM;
58 MachineFrameInfo &MFI;
59 MachineRegisterInfo &MRI;
60 const TargetInstrInfo &TII;
61 const TargetRegisterInfo &TRI;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000062
63 // Variables that are valid during spill(), but used by multiple methods.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000064 LiveRangeEdit *Edit;
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +000065 LiveInterval *StackInt;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000066 int StackSlot;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000067 unsigned Original;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000068
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000069 // All registers to spill to StackSlot, including the main register.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +000070 SmallVector<unsigned, 8> RegsToSpill;
71
72 // All COPY instructions to/from snippets.
73 // They are ignored since both operands refer to the same stack slot.
74 SmallPtrSet<MachineInstr*, 8> SnippetCopies;
75
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000076 // Values that failed to remat at some point.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000077 SmallPtrSet<VNInfo*, 8> UsedValues;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000078
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000079public:
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000080 // Information about a value that was defined by a copy from a sibling
81 // register.
82 struct SibValueInfo {
83 // True when all reaching defs were reloads: No spill is necessary.
84 bool AllDefsAreReloads;
85
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000086 // True when value is defined by an original PHI not from splitting.
87 bool DefByOrigPHI;
88
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000089 // The preferred register to spill.
90 unsigned SpillReg;
91
92 // The value of SpillReg that should be spilled.
93 VNInfo *SpillVNI;
94
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +000095 // The block where SpillVNI should be spilled. Currently, this must be the
96 // block containing SpillVNI->def.
97 MachineBasicBlock *SpillMBB;
98
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +000099 // A defining instruction that is not a sibling copy or a reload, or NULL.
100 // This can be used as a template for rematerialization.
101 MachineInstr *DefMI;
102
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000103 // List of values that depend on this one. These values are actually the
104 // same, but live range splitting has placed them in different registers,
105 // or SSA update needed to insert PHI-defs to preserve SSA form. This is
106 // copies of the current value and phi-kills. Usually only phi-kills cause
107 // more than one dependent value.
108 TinyPtrVector<VNInfo*> Deps;
109
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000110 SibValueInfo(unsigned Reg, VNInfo *VNI)
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000111 : AllDefsAreReloads(true), DefByOrigPHI(false),
112 SpillReg(Reg), SpillVNI(VNI), SpillMBB(0), DefMI(0) {}
113
114 // Returns true when a def has been found.
115 bool hasDef() const { return DefByOrigPHI || DefMI; }
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000116 };
117
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000118private:
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000119 // Values in RegsToSpill defined by sibling copies.
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000120 typedef DenseMap<VNInfo*, SibValueInfo> SibValueMap;
121 SibValueMap SibValues;
122
123 // Dead defs generated during spilling.
124 SmallVector<MachineInstr*, 8> DeadDefs;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000125
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000126 ~InlineSpiller() {}
127
128public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000129 InlineSpiller(MachineFunctionPass &pass,
130 MachineFunction &mf,
131 VirtRegMap &vrm)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000132 : Pass(pass),
133 MF(mf),
134 LIS(pass.getAnalysis<LiveIntervals>()),
135 LSS(pass.getAnalysis<LiveStacks>()),
136 AA(&pass.getAnalysis<AliasAnalysis>()),
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000137 MDT(pass.getAnalysis<MachineDominatorTree>()),
138 Loops(pass.getAnalysis<MachineLoopInfo>()),
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000139 VRM(vrm),
140 MFI(*mf.getFrameInfo()),
141 MRI(mf.getRegInfo()),
142 TII(*mf.getTarget().getInstrInfo()),
143 TRI(*mf.getTarget().getRegisterInfo()) {}
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000144
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000145 void spill(LiveRangeEdit &);
146
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000147private:
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000148 bool isSnippet(const LiveInterval &SnipLI);
149 void collectRegsToSpill();
150
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000151 bool isRegToSpill(unsigned Reg) {
152 return std::find(RegsToSpill.begin(),
153 RegsToSpill.end(), Reg) != RegsToSpill.end();
154 }
155
156 bool isSibling(unsigned Reg);
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000157 MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*);
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000158 void propagateSiblingValue(SibValueMap::iterator, VNInfo *VNI = 0);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000159 void analyzeSiblingValues();
160
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000161 bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000162 void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000163
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000164 void markValueUsed(LiveInterval*, VNInfo*);
165 bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000166 void reMaterializeAll();
167
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000168 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000169 bool foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000170 const SmallVectorImpl<unsigned> &Ops,
171 MachineInstr *LoadMI = 0);
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000172 void insertReload(LiveInterval &NewLI, SlotIndex,
173 MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000174 void insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +0000175 SlotIndex, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000176
177 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000178 void spillAll();
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000179};
180}
181
182namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000183Spiller *createInlineSpiller(MachineFunctionPass &pass,
184 MachineFunction &mf,
185 VirtRegMap &vrm) {
186 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000187}
188}
189
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000190//===----------------------------------------------------------------------===//
191// Snippets
192//===----------------------------------------------------------------------===//
193
194// When spilling a virtual register, we also spill any snippets it is connected
195// to. The snippets are small live ranges that only have a single real use,
196// leftovers from live range splitting. Spilling them enables memory operand
197// folding or tightens the live range around the single use.
198//
199// This minimizes register pressure and maximizes the store-to-load distance for
200// spill slots which can be important in tight loops.
201
202/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
203/// otherwise return 0.
204static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
Rafael Espindolacfe52542011-06-30 21:15:52 +0000205 if (!MI->isFullCopy())
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000206 return 0;
207 if (MI->getOperand(0).getReg() == Reg)
208 return MI->getOperand(1).getReg();
209 if (MI->getOperand(1).getReg() == Reg)
210 return MI->getOperand(0).getReg();
211 return 0;
212}
213
214/// isSnippet - Identify if a live interval is a snippet that should be spilled.
215/// It is assumed that SnipLI is a virtual register with the same original as
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000216/// Edit->getReg().
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000217bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000218 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000219
220 // A snippet is a tiny live range with only a single instruction using it
221 // besides copies to/from Reg or spills/fills. We accept:
222 //
223 // %snip = COPY %Reg / FILL fi#
224 // %snip = USE %snip
225 // %Reg = COPY %snip / SPILL %snip, fi#
226 //
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000227 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000228 return false;
229
230 MachineInstr *UseMI = 0;
231
232 // Check that all uses satisfy our criteria.
233 for (MachineRegisterInfo::reg_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000234 RI = MRI.reg_nodbg_begin(SnipLI.reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000235 MachineInstr *MI = RI.skipInstruction();) {
236
237 // Allow copies to/from Reg.
238 if (isFullCopyOf(MI, Reg))
239 continue;
240
241 // Allow stack slot loads.
242 int FI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000243 if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000244 continue;
245
246 // Allow stack slot stores.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000247 if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000248 continue;
249
250 // Allow a single additional instruction.
251 if (UseMI && MI != UseMI)
252 return false;
253 UseMI = MI;
254 }
255 return true;
256}
257
258/// collectRegsToSpill - Collect live range snippets that only have a single
259/// real use.
260void InlineSpiller::collectRegsToSpill() {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000261 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000262
263 // Main register always spills.
264 RegsToSpill.assign(1, Reg);
265 SnippetCopies.clear();
266
267 // Snippets all have the same original, so there can't be any for an original
268 // register.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000269 if (Original == Reg)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000270 return;
271
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000272 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000273 MachineInstr *MI = RI.skipInstruction();) {
274 unsigned SnipReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000275 if (!isSibling(SnipReg))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000276 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000277 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000278 if (!isSnippet(SnipLI))
279 continue;
280 SnippetCopies.insert(MI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000281 if (isRegToSpill(SnipReg))
282 continue;
283 RegsToSpill.push_back(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000284 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000285 ++NumSnippets;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000286 }
287}
288
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000289
290//===----------------------------------------------------------------------===//
291// Sibling Values
292//===----------------------------------------------------------------------===//
293
294// After live range splitting, some values to be spilled may be defined by
295// copies from sibling registers. We trace the sibling copies back to the
296// original value if it still exists. We need it for rematerialization.
297//
298// Even when the value can't be rematerialized, we still want to determine if
299// the value has already been spilled, or we may want to hoist the spill from a
300// loop.
301
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000302bool InlineSpiller::isSibling(unsigned Reg) {
303 return TargetRegisterInfo::isVirtualRegister(Reg) &&
304 VRM.getOriginal(Reg) == Original;
305}
306
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000307#ifndef NDEBUG
308static raw_ostream &operator<<(raw_ostream &OS,
309 const InlineSpiller::SibValueInfo &SVI) {
310 OS << "spill " << PrintReg(SVI.SpillReg) << ':'
311 << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def;
312 if (SVI.SpillMBB)
313 OS << " in BB#" << SVI.SpillMBB->getNumber();
314 if (SVI.AllDefsAreReloads)
315 OS << " all-reloads";
316 if (SVI.DefByOrigPHI)
317 OS << " orig-phi";
318 OS << " deps[";
319 for (unsigned i = 0, e = SVI.Deps.size(); i != e; ++i)
320 OS << ' ' << SVI.Deps[i]->id << '@' << SVI.Deps[i]->def;
321 OS << " ]";
322 if (SVI.DefMI)
323 OS << " def: " << *SVI.DefMI;
324 else
325 OS << '\n';
326 return OS;
327}
328#endif
329
330/// propagateSiblingValue - Propagate the value in SVI to dependents if it is
331/// known. Otherwise remember the dependency for later.
332///
333/// @param SVI SibValues entry to propagate.
334/// @param VNI Dependent value, or NULL to propagate to all saved dependents.
335void InlineSpiller::propagateSiblingValue(SibValueMap::iterator SVI,
336 VNInfo *VNI) {
337 // When VNI is non-NULL, add it to SVI's deps, and only propagate to that.
338 TinyPtrVector<VNInfo*> FirstDeps;
339 if (VNI) {
340 FirstDeps.push_back(VNI);
341 SVI->second.Deps.push_back(VNI);
342 }
343
344 // Has the value been completely determined yet? If not, defer propagation.
345 if (!SVI->second.hasDef())
346 return;
347
348 // Work list of values to propagate. It would be nice to use a SetVector
349 // here, but then we would be forced to use a SmallSet.
350 SmallVector<SibValueMap::iterator, 8> WorkList(1, SVI);
351 SmallPtrSet<VNInfo*, 8> WorkSet;
352
353 do {
354 SVI = WorkList.pop_back_val();
355 WorkSet.erase(SVI->first);
356 TinyPtrVector<VNInfo*> *Deps = VNI ? &FirstDeps : &SVI->second.Deps;
357 VNI = 0;
358
359 SibValueInfo &SV = SVI->second;
360 if (!SV.SpillMBB)
361 SV.SpillMBB = LIS.getMBBFromIndex(SV.SpillVNI->def);
362
363 DEBUG(dbgs() << " prop to " << Deps->size() << ": "
364 << SVI->first->id << '@' << SVI->first->def << ":\t" << SV);
365
366 assert(SV.hasDef() && "Propagating undefined value");
367
368 // Should this value be propagated as a preferred spill candidate? We don't
369 // propagate values of registers that are about to spill.
370 bool PropSpill = !isRegToSpill(SV.SpillReg);
371 unsigned SpillDepth = ~0u;
372
373 for (TinyPtrVector<VNInfo*>::iterator DepI = Deps->begin(),
374 DepE = Deps->end(); DepI != DepE; ++DepI) {
375 SibValueMap::iterator DepSVI = SibValues.find(*DepI);
376 assert(DepSVI != SibValues.end() && "Dependent value not in SibValues");
377 SibValueInfo &DepSV = DepSVI->second;
378 if (!DepSV.SpillMBB)
379 DepSV.SpillMBB = LIS.getMBBFromIndex(DepSV.SpillVNI->def);
380
381 bool Changed = false;
382
383 // Propagate defining instruction.
384 if (!DepSV.hasDef()) {
385 Changed = true;
386 DepSV.DefMI = SV.DefMI;
387 DepSV.DefByOrigPHI = SV.DefByOrigPHI;
388 }
389
390 // Propagate AllDefsAreReloads. For PHI values, this computes an AND of
391 // all predecessors.
392 if (!SV.AllDefsAreReloads && DepSV.AllDefsAreReloads) {
393 Changed = true;
394 DepSV.AllDefsAreReloads = false;
395 }
396
397 // Propagate best spill value.
398 if (PropSpill && SV.SpillVNI != DepSV.SpillVNI) {
399 if (SV.SpillMBB == DepSV.SpillMBB) {
400 // DepSV is in the same block. Hoist when dominated.
401 if (SV.SpillVNI->def < DepSV.SpillVNI->def) {
402 // This is an alternative def earlier in the same MBB.
403 // Hoist the spill as far as possible in SpillMBB. This can ease
404 // register pressure:
405 //
406 // x = def
407 // y = use x
408 // s = copy x
409 //
410 // Hoisting the spill of s to immediately after the def removes the
411 // interference between x and y:
412 //
413 // x = def
414 // spill x
415 // y = use x<kill>
416 //
417 Changed = true;
418 DepSV.SpillReg = SV.SpillReg;
419 DepSV.SpillVNI = SV.SpillVNI;
420 DepSV.SpillMBB = SV.SpillMBB;
421 }
422 } else {
423 // DepSV is in a different block.
424 if (SpillDepth == ~0u)
425 SpillDepth = Loops.getLoopDepth(SV.SpillMBB);
426
427 // Also hoist spills to blocks with smaller loop depth, but make sure
428 // that the new value dominates. Non-phi dependents are always
429 // dominated, phis need checking.
430 if ((Loops.getLoopDepth(DepSV.SpillMBB) > SpillDepth) &&
431 (!DepSVI->first->isPHIDef() ||
432 MDT.dominates(SV.SpillMBB, DepSV.SpillMBB))) {
433 Changed = true;
434 DepSV.SpillReg = SV.SpillReg;
435 DepSV.SpillVNI = SV.SpillVNI;
436 DepSV.SpillMBB = SV.SpillMBB;
437 }
438 }
439 }
440
441 if (!Changed)
442 continue;
443
444 // Something changed in DepSVI. Propagate to dependents.
445 if (WorkSet.insert(DepSVI->first))
446 WorkList.push_back(DepSVI);
447
448 DEBUG(dbgs() << " update " << DepSVI->first->id << '@'
449 << DepSVI->first->def << " to:\t" << DepSV);
450 }
451 } while (!WorkList.empty());
452}
453
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000454/// traceSiblingValue - Trace a value that is about to be spilled back to the
455/// real defining instructions by looking through sibling copies. Always stay
456/// within the range of OrigVNI so the registers are known to carry the same
457/// value.
458///
459/// Determine if the value is defined by all reloads, so spilling isn't
460/// necessary - the value is already in the stack slot.
461///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000462/// Return a defining instruction that may be a candidate for rematerialization.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000463///
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000464MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
465 VNInfo *OrigVNI) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000466 // Check if a cached value already exists.
467 SibValueMap::iterator SVI;
468 bool Inserted;
469 tie(SVI, Inserted) =
470 SibValues.insert(std::make_pair(UseVNI, SibValueInfo(UseReg, UseVNI)));
471 if (!Inserted) {
472 DEBUG(dbgs() << "Cached value " << PrintReg(UseReg) << ':'
473 << UseVNI->id << '@' << UseVNI->def << ' ' << SVI->second);
474 return SVI->second.DefMI;
475 }
476
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000477 DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
478 << UseVNI->id << '@' << UseVNI->def << '\n');
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000479
480 // List of (Reg, VNI) that have been inserted into SibValues, but need to be
481 // processed.
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000482 SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000483 WorkList.push_back(std::make_pair(UseReg, UseVNI));
484
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000485 do {
486 unsigned Reg;
487 VNInfo *VNI;
488 tie(Reg, VNI) = WorkList.pop_back_val();
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000489 DEBUG(dbgs() << " " << PrintReg(Reg) << ':' << VNI->id << '@' << VNI->def
490 << ":\t");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000491
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000492 // First check if this value has already been computed.
493 SVI = SibValues.find(VNI);
494 assert(SVI != SibValues.end() && "Missing SibValues entry");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000495
496 // Trace through PHI-defs created by live range splitting.
497 if (VNI->isPHIDef()) {
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000498 // Stop at original PHIs. We don't know the value at the predecessors.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000499 if (VNI->def == OrigVNI->def) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000500 DEBUG(dbgs() << "orig phi value\n");
501 SVI->second.DefByOrigPHI = true;
502 SVI->second.AllDefsAreReloads = false;
503 propagateSiblingValue(SVI);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000504 continue;
505 }
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000506
507 // This is a PHI inserted by live range splitting. We could trace the
508 // live-out value from predecessor blocks, but that search can be very
509 // expensive if there are many predecessors and many more PHIs as
510 // generated by tail-dup when it sees an indirectbr. Instead, look at
511 // all the non-PHI defs that have the same value as OrigVNI. They must
512 // jointly dominate VNI->def. This is not optimal since VNI may actually
513 // be jointly dominated by a smaller subset of defs, so there is a change
514 // we will miss a AllDefsAreReloads optimization.
515
516 // Separate all values dominated by OrigVNI into PHIs and non-PHIs.
517 SmallVector<VNInfo*, 8> PHIs, NonPHIs;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000518 LiveInterval &LI = LIS.getInterval(Reg);
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000519 LiveInterval &OrigLI = LIS.getInterval(Original);
520
521 for (LiveInterval::vni_iterator VI = LI.vni_begin(), VE = LI.vni_end();
522 VI != VE; ++VI) {
523 VNInfo *VNI2 = *VI;
524 if (VNI2->isUnused())
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000525 continue;
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000526 if (!OrigLI.containsOneValue() &&
527 OrigLI.getVNInfoAt(VNI2->def) != OrigVNI)
528 continue;
529 if (VNI2->isPHIDef() && VNI2->def != OrigVNI->def)
530 PHIs.push_back(VNI2);
531 else
532 NonPHIs.push_back(VNI2);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000533 }
Jakob Stoklund Olesen6b6e32d2011-09-15 16:41:12 +0000534 DEBUG(dbgs() << "split phi value, checking " << PHIs.size()
535 << " phi-defs, and " << NonPHIs.size()
536 << " non-phi/orig defs\n");
537
538 // Create entries for all the PHIs. Don't add them to the worklist, we
539 // are processing all of them in one go here.
540 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
541 SibValues.insert(std::make_pair(PHIs[i], SibValueInfo(Reg, PHIs[i])));
542
543 // Add every PHI as a dependent of all the non-PHIs.
544 for (unsigned i = 0, e = NonPHIs.size(); i != e; ++i) {
545 VNInfo *NonPHI = NonPHIs[i];
546 // Known value? Try an insertion.
547 tie(SVI, Inserted) =
548 SibValues.insert(std::make_pair(NonPHI, SibValueInfo(Reg, NonPHI)));
549 // Add all the PHIs as dependents of NonPHI.
550 for (unsigned pi = 0, pe = PHIs.size(); pi != pe; ++pi)
551 SVI->second.Deps.push_back(PHIs[pi]);
552 // This is the first time we see NonPHI, add it to the worklist.
553 if (Inserted)
554 WorkList.push_back(std::make_pair(Reg, NonPHI));
555 else
556 // Propagate to all inserted PHIs, not just VNI.
557 propagateSiblingValue(SVI);
558 }
559
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000560 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000561 continue;
562 }
563
564 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
565 assert(MI && "Missing def");
566
567 // Trace through sibling copies.
568 if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000569 if (isSibling(SrcReg)) {
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000570 LiveInterval &SrcLI = LIS.getInterval(SrcReg);
571 VNInfo *SrcVNI = SrcLI.getVNInfoAt(VNI->def.getUseIndex());
572 assert(SrcVNI && "Copy from non-existing value");
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000573 DEBUG(dbgs() << "copy of " << PrintReg(SrcReg) << ':'
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000574 << SrcVNI->id << '@' << SrcVNI->def << '\n');
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000575 // Known sibling source value? Try an insertion.
576 tie(SVI, Inserted) = SibValues.insert(std::make_pair(SrcVNI,
577 SibValueInfo(SrcReg, SrcVNI)));
578 // This is the first time we see Src, add it to the worklist.
579 if (Inserted)
580 WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
581 propagateSiblingValue(SVI, VNI);
582 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000583 continue;
584 }
585 }
586
587 // Track reachable reloads.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000588 SVI->second.DefMI = MI;
589 SVI->second.SpillMBB = MI->getParent();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000590 int FI;
591 if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000592 DEBUG(dbgs() << "reload\n");
593 propagateSiblingValue(SVI);
594 // Next work list item.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000595 continue;
596 }
597
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000598 // Potential remat candidate.
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000599 DEBUG(dbgs() << "def " << *MI);
600 SVI->second.AllDefsAreReloads = false;
601 propagateSiblingValue(SVI);
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000602 } while (!WorkList.empty());
603
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000604 // Look up the value we were looking for. We already did this lokup at the
605 // top of the function, but SibValues may have been invalidated.
606 SVI = SibValues.find(UseVNI);
607 assert(SVI != SibValues.end() && "Didn't compute requested info");
608 DEBUG(dbgs() << " traced to:\t" << SVI->second);
609 return SVI->second.DefMI;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000610}
611
612/// analyzeSiblingValues - Trace values defined by sibling copies back to
613/// something that isn't a sibling copy.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000614///
615/// Keep track of values that may be rematerializable.
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000616void InlineSpiller::analyzeSiblingValues() {
617 SibValues.clear();
618
619 // No siblings at all?
620 if (Edit->getReg() == Original)
621 return;
622
623 LiveInterval &OrigLI = LIS.getInterval(Original);
624 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
625 unsigned Reg = RegsToSpill[i];
626 LiveInterval &LI = LIS.getInterval(Reg);
627 for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
628 VE = LI.vni_end(); VI != VE; ++VI) {
629 VNInfo *VNI = *VI;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000630 if (VNI->isUnused())
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000631 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000632 MachineInstr *DefMI = 0;
633 // Check possible sibling copies.
634 if (VNI->isPHIDef() || VNI->getCopy()) {
635 VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
Jakob Stoklund Olesen9693d4c2011-07-05 15:38:41 +0000636 assert(OrigVNI && "Def outside original live range");
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000637 if (OrigVNI->def != VNI->def)
638 DefMI = traceSiblingValue(Reg, VNI, OrigVNI);
639 }
640 if (!DefMI && !VNI->isPHIDef())
641 DefMI = LIS.getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen3b7d9172011-04-20 22:14:20 +0000642 if (DefMI && Edit->checkRematerializable(VNI, DefMI, TII, AA)) {
643 DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@'
644 << VNI->def << " may remat from " << *DefMI);
645 }
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +0000646 }
647 }
648}
649
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000650/// hoistSpill - Given a sibling copy that defines a value to be spilled, insert
651/// a spill at a better location.
652bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) {
653 SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
654 VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getDefIndex());
655 assert(VNI && VNI->def == Idx.getDefIndex() && "Not defined by copy");
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000656 SibValueMap::iterator I = SibValues.find(VNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000657 if (I == SibValues.end())
658 return false;
659
660 const SibValueInfo &SVI = I->second;
661
662 // Let the normal folding code deal with the boring case.
663 if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI)
664 return false;
665
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000666 // SpillReg may have been deleted by remat and DCE.
667 if (!LIS.hasInterval(SVI.SpillReg)) {
668 DEBUG(dbgs() << "Stale interval: " << PrintReg(SVI.SpillReg) << '\n');
669 SibValues.erase(I);
670 return false;
671 }
672
673 LiveInterval &SibLI = LIS.getInterval(SVI.SpillReg);
674 if (!SibLI.containsValue(SVI.SpillVNI)) {
675 DEBUG(dbgs() << "Stale value: " << PrintReg(SVI.SpillReg) << '\n');
676 SibValues.erase(I);
677 return false;
678 }
679
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000680 // Conservatively extend the stack slot range to the range of the original
681 // value. We may be able to do better with stack slot coloring by being more
682 // careful here.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000683 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000684 LiveInterval &OrigLI = LIS.getInterval(Original);
685 VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000686 StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +0000687 DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000688 << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000689
690 // Already spilled everywhere.
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000691 if (SVI.AllDefsAreReloads) {
Jakob Stoklund Olesen1ab7c8e2011-09-09 18:11:41 +0000692 DEBUG(dbgs() << "\tno spill needed: " << SVI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000693 ++NumOmitReloadSpill;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000694 return true;
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000695 }
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000696 // We are going to spill SVI.SpillVNI immediately after its def, so clear out
697 // any later spills of the same value.
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000698 eliminateRedundantSpills(SibLI, SVI.SpillVNI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000699
700 MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def);
701 MachineBasicBlock::iterator MII;
702 if (SVI.SpillVNI->isPHIDef())
703 MII = MBB->SkipPHIsAndLabels(MBB->begin());
704 else {
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000705 MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
706 assert(DefMI && "Defining instruction disappeared");
707 MII = DefMI;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000708 ++MII;
709 }
710 // Insert spill without kill flag immediately after def.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000711 TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot,
712 MRI.getRegClass(SVI.SpillReg), &TRI);
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000713 --MII; // Point to store instruction.
714 LIS.InsertMachineInstrInMaps(MII);
715 VRM.addSpillSlotUse(StackSlot, MII);
716 DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000717
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000718 ++NumSpills;
719 ++NumHoists;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000720 return true;
721}
722
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000723/// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
724/// redundant spills of this value in SLI.reg and sibling copies.
725void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +0000726 assert(VNI && "Missing value");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000727 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
728 WorkList.push_back(std::make_pair(&SLI, VNI));
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000729 assert(StackInt && "No stack slot assigned yet.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000730
731 do {
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000732 LiveInterval *LI;
733 tie(LI, VNI) = WorkList.pop_back_val();
734 unsigned Reg = LI->reg;
Jakob Stoklund Olesen6ee56e62011-04-30 06:42:21 +0000735 DEBUG(dbgs() << "Checking redundant spills for "
736 << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000737
738 // Regs to spill are taken care of.
739 if (isRegToSpill(Reg))
740 continue;
741
742 // Add all of VNI's live range to StackInt.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000743 StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
744 DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000745
746 // Find all spills and copies of VNI.
747 for (MachineRegisterInfo::use_nodbg_iterator UI = MRI.use_nodbg_begin(Reg);
748 MachineInstr *MI = UI.skipInstruction();) {
749 if (!MI->isCopy() && !MI->getDesc().mayStore())
750 continue;
751 SlotIndex Idx = LIS.getInstructionIndex(MI);
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000752 if (LI->getVNInfoAt(Idx) != VNI)
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000753 continue;
754
755 // Follow sibling copies down the dominator tree.
756 if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
757 if (isSibling(DstReg)) {
758 LiveInterval &DstLI = LIS.getInterval(DstReg);
759 VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getDefIndex());
760 assert(DstVNI && "Missing defined value");
761 assert(DstVNI->def == Idx.getDefIndex() && "Wrong copy def slot");
Jakob Stoklund Olesen01a46c82011-03-20 05:44:55 +0000762 WorkList.push_back(std::make_pair(&DstLI, DstVNI));
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000763 }
764 continue;
765 }
766
767 // Erase spills.
768 int FI;
769 if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
770 DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI);
771 // eliminateDeadDefs won't normally remove stores, so switch opcode.
772 MI->setDesc(TII.get(TargetOpcode::KILL));
773 DeadDefs.push_back(MI);
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000774 ++NumSpillsRemoved;
775 --NumSpills;
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +0000776 }
777 }
778 } while (!WorkList.empty());
779}
780
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000781
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000782//===----------------------------------------------------------------------===//
783// Rematerialization
784//===----------------------------------------------------------------------===//
785
786/// markValueUsed - Remember that VNI failed to rematerialize, so its defining
787/// instruction cannot be eliminated. See through snippet copies
788void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
789 SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
790 WorkList.push_back(std::make_pair(LI, VNI));
791 do {
792 tie(LI, VNI) = WorkList.pop_back_val();
793 if (!UsedValues.insert(VNI))
794 continue;
795
796 if (VNI->isPHIDef()) {
797 MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
798 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
799 PE = MBB->pred_end(); PI != PE; ++PI) {
800 VNInfo *PVNI = LI->getVNInfoAt(LIS.getMBBEndIdx(*PI).getPrevSlot());
801 if (PVNI)
802 WorkList.push_back(std::make_pair(LI, PVNI));
803 }
804 continue;
805 }
806
807 // Follow snippet copies.
808 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
809 if (!SnippetCopies.count(MI))
810 continue;
811 LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
812 assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
813 VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getUseIndex());
814 assert(SnipVNI && "Snippet undefined before copy");
815 WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
816 } while (!WorkList.empty());
817}
818
819/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
820bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg,
821 MachineBasicBlock::iterator MI) {
822 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getUseIndex();
Jakob Stoklund Olesen79413502011-07-18 05:31:59 +0000823 VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000824
825 if (!ParentVNI) {
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000826 DEBUG(dbgs() << "\tadding <undef> flags: ");
827 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
828 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000829 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000830 MO.setIsUndef();
831 }
832 DEBUG(dbgs() << UseIdx << '\t' << *MI);
833 return true;
834 }
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000835
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000836 if (SnippetCopies.count(MI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000837 return false;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000838
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000839 // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy.
840 LiveRangeEdit::Remat RM(ParentVNI);
841 SibValueMap::const_iterator SibI = SibValues.find(ParentVNI);
842 if (SibI != SibValues.end())
843 RM.OrigMI = SibI->second.DefMI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000844 if (!Edit->canRematerializeAt(RM, UseIdx, false, LIS)) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000845 markValueUsed(&VirtReg, ParentVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000846 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
847 return false;
848 }
849
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000850 // If the instruction also writes VirtReg.reg, it had better not require the
851 // same register for uses and defs.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000852 bool Reads, Writes;
853 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000854 tie(Reads, Writes) = MI->readsWritesVirtualRegister(VirtReg.reg, &Ops);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000855 if (Writes) {
856 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
857 MachineOperand &MO = MI->getOperand(Ops[i]);
858 if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000859 markValueUsed(&VirtReg, ParentVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000860 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
861 return false;
862 }
863 }
864 }
865
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000866 // Before rematerializing into a register for a single instruction, try to
867 // fold a load into the instruction. That avoids allocating a new register.
868 if (RM.OrigMI->getDesc().canFoldAsLoad() &&
869 foldMemoryOperand(MI, Ops, RM.OrigMI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000870 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000871 ++NumFoldedLoads;
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000872 return true;
873 }
874
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000875 // Alocate a new register for the remat.
Jakob Stoklund Olesen312babc2011-03-31 03:54:44 +0000876 LiveInterval &NewLI = Edit->createFrom(Original, LIS, VRM);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000877 NewLI.markNotSpillable();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000878
879 // Finally we can rematerialize OrigMI before MI.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000880 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
881 LIS, TII, TRI);
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000882 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000883 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000884
885 // Replace operands
886 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
887 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesencf610d02011-03-29 17:47:02 +0000888 if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000889 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000890 MO.setIsKill();
891 }
892 }
893 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
894
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000895 VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000896 NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000897 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +0000898 ++NumRemats;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000899 return true;
900}
901
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000902/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000903/// and trim the live ranges after.
904void InlineSpiller::reMaterializeAll() {
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000905 // analyzeSiblingValues has already tested all relevant defining instructions.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000906 if (!Edit->anyRematerializable(LIS, TII, AA))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000907 return;
908
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000909 UsedValues.clear();
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000910
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000911 // Try to remat before all uses of snippets.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000912 bool anyRemat = false;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000913 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
914 unsigned Reg = RegsToSpill[i];
915 LiveInterval &LI = LIS.getInterval(Reg);
916 for (MachineRegisterInfo::use_nodbg_iterator
917 RI = MRI.use_nodbg_begin(Reg);
918 MachineInstr *MI = RI.skipInstruction();)
919 anyRemat |= reMaterializeFor(LI, MI);
920 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000921 if (!anyRemat)
922 return;
923
924 // Remove any values that were completely rematted.
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000925 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
926 unsigned Reg = RegsToSpill[i];
927 LiveInterval &LI = LIS.getInterval(Reg);
928 for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
929 I != E; ++I) {
930 VNInfo *VNI = *I;
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000931 if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000932 continue;
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000933 MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
934 MI->addRegisterDead(Reg, &TRI);
935 if (!MI->allDefsAreDead())
936 continue;
937 DEBUG(dbgs() << "All defs dead: " << *MI);
938 DeadDefs.push_back(MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000939 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000940 }
Jakob Stoklund Olesenc1d22d82011-03-29 17:47:00 +0000941
942 // Eliminate dead code after remat. Note that some snippet copies may be
943 // deleted here.
944 if (DeadDefs.empty())
945 return;
946 DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
947 Edit->eliminateDeadDefs(DeadDefs, LIS, VRM, TII);
948
949 // Get rid of deleted and empty intervals.
950 for (unsigned i = RegsToSpill.size(); i != 0; --i) {
951 unsigned Reg = RegsToSpill[i-1];
952 if (!LIS.hasInterval(Reg)) {
953 RegsToSpill.erase(RegsToSpill.begin() + (i - 1));
954 continue;
955 }
956 LiveInterval &LI = LIS.getInterval(Reg);
957 if (!LI.empty())
958 continue;
959 Edit->eraseVirtReg(Reg, LIS);
960 RegsToSpill.erase(RegsToSpill.begin() + (i - 1));
961 }
962 DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000963}
964
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000965
966//===----------------------------------------------------------------------===//
967// Spilling
968//===----------------------------------------------------------------------===//
969
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000970/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000971bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000972 int FI = 0;
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000973 unsigned InstrReg = TII.isLoadFromStackSlot(MI, FI);
974 bool IsLoad = InstrReg;
975 if (!IsLoad)
976 InstrReg = TII.isStoreToStackSlot(MI, FI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000977
978 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000979 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000980 return false;
981
982 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000983 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000984 MI->eraseFromParent();
Jakob Stoklund Olesen79c40a02011-09-15 17:54:28 +0000985
986 if (IsLoad) {
987 ++NumReloadsRemoved;
988 --NumReloads;
989 } else {
990 ++NumSpillsRemoved;
991 --NumSpills;
992 }
993
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000994 return true;
995}
996
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000997/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000998/// @param MI Instruction using or defining the current register.
Jakob Stoklund Olesen39048252010-12-18 03:28:32 +0000999/// @param Ops Operand indices from readsWritesVirtualRegister().
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001000/// @param LoadMI Load instruction to use instead of stack slot when non-null.
1001/// @return True on success, and MI will be erased.
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001002bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001003 const SmallVectorImpl<unsigned> &Ops,
1004 MachineInstr *LoadMI) {
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001005 bool WasCopy = MI->isCopy();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001006 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
1007 // operands.
1008 SmallVector<unsigned, 8> FoldOps;
1009 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1010 unsigned Idx = Ops[i];
1011 MachineOperand &MO = MI->getOperand(Idx);
1012 if (MO.isImplicit())
1013 continue;
1014 // FIXME: Teach targets to deal with subregs.
1015 if (MO.getSubReg())
1016 return false;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +00001017 // We cannot fold a load instruction into a def.
1018 if (LoadMI && MO.isDef())
1019 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001020 // Tied use operands should not be passed to foldMemoryOperand.
1021 if (!MI->isRegTiedToDefOperand(Idx))
1022 FoldOps.push_back(Idx);
1023 }
1024
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001025 MachineInstr *FoldMI =
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001026 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
1027 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001028 if (!FoldMI)
1029 return false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001030 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +00001031 if (!LoadMI)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001032 VRM.addSpillSlotUse(StackSlot, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +00001033 MI->eraseFromParent();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001034 DEBUG(dbgs() << "\tfolded: " << *FoldMI);
Jakob Stoklund Olesend205f7a2011-09-15 18:22:52 +00001035 if (!WasCopy)
1036 ++NumFolded;
1037 else if (Ops.front() == 0)
1038 ++NumSpills;
1039 else
1040 ++NumReloads;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +00001041 return true;
1042}
1043
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001044/// insertReload - Insert a reload of NewLI.reg before MI.
1045void InlineSpiller::insertReload(LiveInterval &NewLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001046 SlotIndex Idx,
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001047 MachineBasicBlock::iterator MI) {
1048 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001049 TII.loadRegFromStackSlot(MBB, MI, NewLI.reg, StackSlot,
1050 MRI.getRegClass(NewLI.reg), &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001051 --MI; // Point to load instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001052 SlotIndex LoadIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
1053 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001054 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
Lang Hames6e2968c2010-09-25 12:04:16 +00001055 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001056 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001057 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001058 ++NumReloads;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001059}
1060
1061/// insertSpill - Insert a spill of NewLI.reg after MI.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001062void InlineSpiller::insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001063 SlotIndex Idx, MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001064 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001065 TII.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, StackSlot,
1066 MRI.getRegClass(NewLI.reg), &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001067 --MI; // Point to store instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001068 SlotIndex StoreIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
1069 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001070 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001071 VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001072 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001073 ++NumSpills;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001074}
1075
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001076/// spillAroundUses - insert spill code around each use of Reg.
1077void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001078 DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n');
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001079 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001080
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001081 // Iterate over instructions using Reg.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001082 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001083 MachineInstr *MI = RI.skipInstruction();) {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001084
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +00001085 // Debug values are not allowed to affect codegen.
1086 if (MI->isDebugValue()) {
1087 // Modify DBG_VALUE now that the value is in a spill slot.
1088 uint64_t Offset = MI->getOperand(1).getImm();
1089 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
1090 DebugLoc DL = MI->getDebugLoc();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001091 if (MachineInstr *NewDV = TII.emitFrameIndexDebugValue(MF, StackSlot,
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +00001092 Offset, MDPtr, DL)) {
1093 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
1094 MachineBasicBlock *MBB = MI->getParent();
1095 MBB->insert(MBB->erase(MI), NewDV);
1096 } else {
1097 DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
1098 MI->eraseFromParent();
1099 }
1100 continue;
1101 }
1102
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001103 // Ignore copies to/from snippets. We'll delete them.
1104 if (SnippetCopies.count(MI))
1105 continue;
1106
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001107 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001108 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +00001109 continue;
1110
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001111 // Analyze instruction.
1112 bool Reads, Writes;
1113 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001114 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Reg, &Ops);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001115
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001116 // Find the slot index where this instruction reads and writes OldLI.
1117 // This is usually the def slot, except for tied early clobbers.
1118 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
1119 if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getUseIndex()))
1120 if (SlotIndex::isSameInstr(Idx, VNI->def))
1121 Idx = VNI->def;
1122
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001123 // Check for a sibling copy.
1124 unsigned SibReg = isFullCopyOf(MI, Reg);
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001125 if (SibReg && isSibling(SibReg)) {
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001126 // This may actually be a copy between snippets.
1127 if (isRegToSpill(SibReg)) {
1128 DEBUG(dbgs() << "Found new snippet copy: " << *MI);
1129 SnippetCopies.insert(MI);
1130 continue;
1131 }
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001132 if (Writes) {
1133 // Hoist the spill of a sib-reg copy.
1134 if (hoistSpill(OldLI, MI)) {
1135 // This COPY is now dead, the value is already in the stack slot.
1136 MI->getOperand(0).setIsDead();
1137 DeadDefs.push_back(MI);
1138 continue;
1139 }
1140 } else {
1141 // This is a reload for a sib-reg copy. Drop spills downstream.
Jakob Stoklund Olesen682eed02011-03-20 05:44:58 +00001142 LiveInterval &SibLI = LIS.getInterval(SibReg);
1143 eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
1144 // The COPY will fold to a reload below.
1145 }
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001146 }
1147
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +00001148 // Attempt to fold memory ops.
1149 if (foldMemoryOperand(MI, Ops))
1150 continue;
1151
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001152 // Allocate interval around instruction.
1153 // FIXME: Infer regclass from instruction alone.
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001154 LiveInterval &NewLI = Edit->createFrom(Reg, LIS, VRM);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001155 NewLI.markNotSpillable();
1156
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +00001157 if (Reads)
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001158 insertReload(NewLI, Idx, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001159
1160 // Rewrite instruction operands.
1161 bool hasLiveDef = false;
1162 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1163 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +00001164 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001165 if (MO.isUse()) {
1166 if (!MI->isRegTiedToDefOperand(Ops[i]))
1167 MO.setIsKill();
1168 } else {
1169 if (!MO.isDead())
1170 hasLiveDef = true;
1171 }
1172 }
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001173 DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001174
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001175 // FIXME: Use a second vreg if instruction has no tied ops.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +00001176 if (Writes && hasLiveDef)
Jakob Stoklund Olesen5d5ef4a2011-04-18 20:23:27 +00001177 insertSpill(NewLI, OldLI, Idx, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001178
1179 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +00001180 }
1181}
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001182
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001183/// spillAll - Spill all registers remaining after rematerialization.
1184void InlineSpiller::spillAll() {
1185 // Update LiveStacks now that we are committed to spilling.
1186 if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
1187 StackSlot = VRM.assignVirt2StackSlot(Original);
1188 StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
1189 StackInt->getNextValue(SlotIndex(), 0, LSS.getVNInfoAllocator());
1190 } else
1191 StackInt = &LSS.getInterval(StackSlot);
1192
1193 if (Original != Edit->getReg())
1194 VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
1195
1196 assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
1197 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
1198 StackInt->MergeRangesInAsValue(LIS.getInterval(RegsToSpill[i]),
1199 StackInt->getValNumInfo(0));
1200 DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
1201
1202 // Spill around uses of all RegsToSpill.
1203 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
1204 spillAroundUses(RegsToSpill[i]);
1205
1206 // Hoisted spills may cause dead code.
1207 if (!DeadDefs.empty()) {
1208 DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
1209 Edit->eliminateDeadDefs(DeadDefs, LIS, VRM, TII);
1210 }
1211
1212 // Finally delete the SnippetCopies.
Jakob Stoklund Olesen443443c2011-05-11 18:25:10 +00001213 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
1214 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(RegsToSpill[i]);
1215 MachineInstr *MI = RI.skipInstruction();) {
1216 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
1217 // FIXME: Do this with a LiveRangeEdit callback.
1218 VRM.RemoveMachineInstrFromMaps(MI);
1219 LIS.RemoveMachineInstrFromMaps(MI);
1220 MI->eraseFromParent();
1221 }
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001222 }
1223
1224 // Delete all spilled registers.
1225 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
1226 Edit->eraseVirtReg(RegsToSpill[i], LIS);
1227}
1228
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001229void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesene9bd4ea2011-05-05 17:22:53 +00001230 ++NumSpilledRanges;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001231 Edit = &edit;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001232 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
1233 && "Trying to spill a stack slot.");
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001234 // Share a stack slot among all descendants of Original.
1235 Original = VRM.getOriginal(edit.getReg());
1236 StackSlot = VRM.getStackSlot(Original);
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +00001237 StackInt = 0;
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001238
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001239 DEBUG(dbgs() << "Inline spilling "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +00001240 << MRI.getRegClass(edit.getReg())->getName()
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001241 << ':' << edit.getParent() << "\nFrom original "
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001242 << LIS.getInterval(Original) << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001243 assert(edit.getParent().isSpillable() &&
1244 "Attempting to spill already spilled value.");
Jakob Stoklund Olesen2a72bfa2011-03-18 04:23:06 +00001245 assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001246
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001247 collectRegsToSpill();
Jakob Stoklund Olesen13ba2522011-03-15 21:13:25 +00001248 analyzeSiblingValues();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001249 reMaterializeAll();
1250
1251 // Remat may handle everything.
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001252 if (!RegsToSpill.empty())
1253 spillAll();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001254
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001255 Edit->calculateRegClassAndHint(MF, LIS, Loops);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +00001256}