blob: 527fe48d4af0612c02c439344df89d416e00846d [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 Olesen914f2ff2010-06-29 23:58:39 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
29
30using namespace llvm;
31
32namespace {
33class InlineSpiller : public Spiller {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000034 MachineFunctionPass &Pass;
35 MachineFunction &MF;
36 LiveIntervals &LIS;
37 LiveStacks &LSS;
38 AliasAnalysis *AA;
39 VirtRegMap &VRM;
40 MachineFrameInfo &MFI;
41 MachineRegisterInfo &MRI;
42 const TargetInstrInfo &TII;
43 const TargetRegisterInfo &TRI;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000044
45 // Variables that are valid during spill(), but used by multiple methods.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000046 LiveRangeEdit *Edit;
47 const TargetRegisterClass *RC;
48 int StackSlot;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000049
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000050 // All registers to spill to StackSlot, including the main register.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +000051 SmallVector<unsigned, 8> RegsToSpill;
52
53 // All COPY instructions to/from snippets.
54 // They are ignored since both operands refer to the same stack slot.
55 SmallPtrSet<MachineInstr*, 8> SnippetCopies;
56
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000057 // Values that failed to remat at some point.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000058 SmallPtrSet<VNInfo*, 8> UsedValues;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000059
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000060 ~InlineSpiller() {}
61
62public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000063 InlineSpiller(MachineFunctionPass &pass,
64 MachineFunction &mf,
65 VirtRegMap &vrm)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +000066 : Pass(pass),
67 MF(mf),
68 LIS(pass.getAnalysis<LiveIntervals>()),
69 LSS(pass.getAnalysis<LiveStacks>()),
70 AA(&pass.getAnalysis<AliasAnalysis>()),
71 VRM(vrm),
72 MFI(*mf.getFrameInfo()),
73 MRI(mf.getRegInfo()),
74 TII(*mf.getTarget().getInstrInfo()),
75 TRI(*mf.getTarget().getRegisterInfo()) {}
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000076
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000077 void spill(LiveRangeEdit &);
78
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000079private:
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +000080 bool isSnippet(const LiveInterval &SnipLI);
81 void collectRegsToSpill();
82
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000083 bool reMaterializeFor(MachineBasicBlock::iterator MI);
84 void reMaterializeAll();
85
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +000086 bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +000087 bool foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +000088 const SmallVectorImpl<unsigned> &Ops,
89 MachineInstr *LoadMI = 0);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000090 void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +000091 void insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
92 MachineBasicBlock::iterator MI);
93
94 void spillAroundUses(unsigned Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000095};
96}
97
98namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000099Spiller *createInlineSpiller(MachineFunctionPass &pass,
100 MachineFunction &mf,
101 VirtRegMap &vrm) {
102 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000103}
104}
105
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000106//===----------------------------------------------------------------------===//
107// Snippets
108//===----------------------------------------------------------------------===//
109
110// When spilling a virtual register, we also spill any snippets it is connected
111// to. The snippets are small live ranges that only have a single real use,
112// leftovers from live range splitting. Spilling them enables memory operand
113// folding or tightens the live range around the single use.
114//
115// This minimizes register pressure and maximizes the store-to-load distance for
116// spill slots which can be important in tight loops.
117
118/// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
119/// otherwise return 0.
120static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
121 if (!MI->isCopy())
122 return 0;
123 if (MI->getOperand(0).getSubReg() != 0)
124 return 0;
125 if (MI->getOperand(1).getSubReg() != 0)
126 return 0;
127 if (MI->getOperand(0).getReg() == Reg)
128 return MI->getOperand(1).getReg();
129 if (MI->getOperand(1).getReg() == Reg)
130 return MI->getOperand(0).getReg();
131 return 0;
132}
133
134/// isSnippet - Identify if a live interval is a snippet that should be spilled.
135/// It is assumed that SnipLI is a virtual register with the same original as
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000136/// Edit->getReg().
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000137bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000138 unsigned Reg = Edit->getReg();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000139
140 // A snippet is a tiny live range with only a single instruction using it
141 // besides copies to/from Reg or spills/fills. We accept:
142 //
143 // %snip = COPY %Reg / FILL fi#
144 // %snip = USE %snip
145 // %Reg = COPY %snip / SPILL %snip, fi#
146 //
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000147 if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000148 return false;
149
150 MachineInstr *UseMI = 0;
151
152 // Check that all uses satisfy our criteria.
153 for (MachineRegisterInfo::reg_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000154 RI = MRI.reg_nodbg_begin(SnipLI.reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000155 MachineInstr *MI = RI.skipInstruction();) {
156
157 // Allow copies to/from Reg.
158 if (isFullCopyOf(MI, Reg))
159 continue;
160
161 // Allow stack slot loads.
162 int FI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000163 if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000164 continue;
165
166 // Allow stack slot stores.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000167 if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000168 continue;
169
170 // Allow a single additional instruction.
171 if (UseMI && MI != UseMI)
172 return false;
173 UseMI = MI;
174 }
175 return true;
176}
177
178/// collectRegsToSpill - Collect live range snippets that only have a single
179/// real use.
180void InlineSpiller::collectRegsToSpill() {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000181 unsigned Reg = Edit->getReg();
182 unsigned Orig = VRM.getOriginal(Reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000183
184 // Main register always spills.
185 RegsToSpill.assign(1, Reg);
186 SnippetCopies.clear();
187
188 // Snippets all have the same original, so there can't be any for an original
189 // register.
190 if (Orig == Reg)
191 return;
192
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000193 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000194 MachineInstr *MI = RI.skipInstruction();) {
195 unsigned SnipReg = isFullCopyOf(MI, Reg);
196 if (!SnipReg)
197 continue;
198 if (!TargetRegisterInfo::isVirtualRegister(SnipReg))
199 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000200 if (VRM.getOriginal(SnipReg) != Orig)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000201 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000202 LiveInterval &SnipLI = LIS.getInterval(SnipReg);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000203 if (!isSnippet(SnipLI))
204 continue;
205 SnippetCopies.insert(MI);
206 if (std::find(RegsToSpill.begin(), RegsToSpill.end(),
207 SnipReg) == RegsToSpill.end())
208 RegsToSpill.push_back(SnipReg);
209
210 DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
211 }
212}
213
Jakob Stoklund Olesen75b54092011-02-22 23:01:49 +0000214/// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000215bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000216 SlotIndex UseIdx = LIS.getInstructionIndex(MI).getUseIndex();
217 VNInfo *OrigVNI = Edit->getParent().getVNInfoAt(UseIdx);
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000218
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000219 if (!OrigVNI) {
220 DEBUG(dbgs() << "\tadding <undef> flags: ");
221 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
222 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000223 if (MO.isReg() && MO.isUse() && MO.getReg() == Edit->getReg())
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000224 MO.setIsUndef();
225 }
226 DEBUG(dbgs() << UseIdx << '\t' << *MI);
227 return true;
228 }
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000229
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000230 // FIXME: Properly remat for snippets as well.
231 if (SnippetCopies.count(MI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000232 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000233 return false;
234 }
235
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000236 LiveRangeEdit::Remat RM(OrigVNI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000237 if (!Edit->canRematerializeAt(RM, UseIdx, false, LIS)) {
238 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000239 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
240 return false;
241 }
242
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000243 // If the instruction also writes Edit->getReg(), it had better not require
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000244 // the same register for uses and defs.
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000245 bool Reads, Writes;
246 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000247 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Edit->getReg(), &Ops);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000248 if (Writes) {
249 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
250 MachineOperand &MO = MI->getOperand(Ops[i]);
251 if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000252 UsedValues.insert(OrigVNI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000253 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
254 return false;
255 }
256 }
257 }
258
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000259 // Before rematerializing into a register for a single instruction, try to
260 // fold a load into the instruction. That avoids allocating a new register.
261 if (RM.OrigMI->getDesc().canFoldAsLoad() &&
262 foldMemoryOperand(MI, Ops, RM.OrigMI)) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000263 Edit->markRematerialized(RM.ParentVNI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000264 return true;
265 }
266
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000267 // Alocate a new register for the remat.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000268 LiveInterval &NewLI = Edit->create(MRI, LIS, VRM);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000269 NewLI.markNotSpillable();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000270
Jakob Stoklund Olesenc3dca3f2011-02-09 00:25:36 +0000271 // Rematting for a copy: Set allocation hint to be the destination register.
272 if (MI->isCopy())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000273 MRI.setRegAllocationHint(NewLI.reg, 0, MI->getOperand(0).getReg());
Jakob Stoklund Olesenc3dca3f2011-02-09 00:25:36 +0000274
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000275 // Finally we can rematerialize OrigMI before MI.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000276 SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
277 LIS, TII, TRI);
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000278 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000279 << *LIS.getInstructionFromIndex(DefIdx));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000280
281 // Replace operands
282 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
283 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000284 if (MO.isReg() && MO.isUse() && MO.getReg() == Edit->getReg()) {
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000285 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000286 MO.setIsKill();
287 }
288 }
289 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
290
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000291 VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000292 NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000293 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000294 return true;
295}
296
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000297/// reMaterializeAll - Try to rematerialize as many uses as possible,
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000298/// and trim the live ranges after.
299void InlineSpiller::reMaterializeAll() {
300 // Do a quick scan of the interval values to find if any are remattable.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000301 if (!Edit->anyRematerializable(LIS, TII, AA))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000302 return;
303
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000304 UsedValues.clear();
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000305
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000306 // Try to remat before all uses of Edit->getReg().
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000307 bool anyRemat = false;
308 for (MachineRegisterInfo::use_nodbg_iterator
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000309 RI = MRI.use_nodbg_begin(Edit->getReg());
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000310 MachineInstr *MI = RI.skipInstruction();)
311 anyRemat |= reMaterializeFor(MI);
312
313 if (!anyRemat)
314 return;
315
316 // Remove any values that were completely rematted.
317 bool anyRemoved = false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000318 for (LiveInterval::vni_iterator I = Edit->getParent().vni_begin(),
319 E = Edit->getParent().vni_end(); I != E; ++I) {
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000320 VNInfo *VNI = *I;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000321 if (VNI->hasPHIKill() || !Edit->didRematerialize(VNI) ||
322 UsedValues.count(VNI))
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000323 continue;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000324 MachineInstr *DefMI = LIS.getInstructionFromIndex(VNI->def);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000325 DEBUG(dbgs() << "\tremoving dead def: " << VNI->def << '\t' << *DefMI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000326 LIS.RemoveMachineInstrFromMaps(DefMI);
327 VRM.RemoveMachineInstrFromMaps(DefMI);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000328 DefMI->eraseFromParent();
Lang Hamescec29452010-09-26 03:37:09 +0000329 VNI->def = SlotIndex();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000330 anyRemoved = true;
331 }
332
333 if (!anyRemoved)
334 return;
335
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000336 // Removing values may cause debug uses where parent is not live.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000337 for (MachineRegisterInfo::use_iterator RI = MRI.use_begin(Edit->getReg());
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000338 MachineInstr *MI = RI.skipInstruction();) {
339 if (!MI->isDebugValue())
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000340 continue;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000341 // Try to preserve the debug value if parent is live immediately after it.
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000342 MachineBasicBlock::iterator NextMI = MI;
343 ++NextMI;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000344 if (NextMI != MI->getParent()->end() && !LIS.isNotInMIMap(NextMI)) {
345 SlotIndex Idx = LIS.getInstructionIndex(NextMI);
346 VNInfo *VNI = Edit->getParent().getVNInfoAt(Idx);
347 if (VNI && (VNI->hasPHIKill() || UsedValues.count(VNI)))
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000348 continue;
349 }
350 DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000351 MI->eraseFromParent();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000352 }
353}
354
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000355/// If MI is a load or store of StackSlot, it can be removed.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000356bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000357 int FI = 0;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000358 unsigned InstrReg;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000359 if (!(InstrReg = TII.isLoadFromStackSlot(MI, FI)) &&
360 !(InstrReg = TII.isStoreToStackSlot(MI, FI)))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000361 return false;
362
363 // We have a stack access. Is it the right register and slot?
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000364 if (InstrReg != Reg || FI != StackSlot)
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000365 return false;
366
367 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000368 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000369 MI->eraseFromParent();
370 return true;
371}
372
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000373/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000374/// @param MI Instruction using or defining the current register.
Jakob Stoklund Olesen39048252010-12-18 03:28:32 +0000375/// @param Ops Operand indices from readsWritesVirtualRegister().
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000376/// @param LoadMI Load instruction to use instead of stack slot when non-null.
377/// @return True on success, and MI will be erased.
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000378bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000379 const SmallVectorImpl<unsigned> &Ops,
380 MachineInstr *LoadMI) {
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000381 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
382 // operands.
383 SmallVector<unsigned, 8> FoldOps;
384 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
385 unsigned Idx = Ops[i];
386 MachineOperand &MO = MI->getOperand(Idx);
387 if (MO.isImplicit())
388 continue;
389 // FIXME: Teach targets to deal with subregs.
390 if (MO.getSubReg())
391 return false;
Jakob Stoklund Olesen7b1f4982011-02-08 19:33:55 +0000392 // We cannot fold a load instruction into a def.
393 if (LoadMI && MO.isDef())
394 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000395 // Tied use operands should not be passed to foldMemoryOperand.
396 if (!MI->isRegTiedToDefOperand(Idx))
397 FoldOps.push_back(Idx);
398 }
399
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000400 MachineInstr *FoldMI =
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000401 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
402 : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000403 if (!FoldMI)
404 return false;
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000405 LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000406 if (!LoadMI)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000407 VRM.addSpillSlotUse(StackSlot, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000408 MI->eraseFromParent();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000409 DEBUG(dbgs() << "\tfolded: " << *FoldMI);
410 return true;
411}
412
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000413/// insertReload - Insert a reload of NewLI.reg before MI.
414void InlineSpiller::insertReload(LiveInterval &NewLI,
415 MachineBasicBlock::iterator MI) {
416 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000417 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
418 TII.loadRegFromStackSlot(MBB, MI, NewLI.reg, StackSlot, RC, &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000419 --MI; // Point to load instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000420 SlotIndex LoadIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
421 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000422 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
Lang Hames6e2968c2010-09-25 12:04:16 +0000423 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0,
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000424 LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000425 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
426}
427
428/// insertSpill - Insert a spill of NewLI.reg after MI.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000429void InlineSpiller::insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000430 MachineBasicBlock::iterator MI) {
431 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesen68257e62010-11-15 20:55:49 +0000432
433 // Get the defined value. It could be an early clobber so keep the def index.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000434 SlotIndex Idx = LIS.getInstructionIndex(MI).getDefIndex();
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000435 VNInfo *VNI = OldLI.getVNInfoAt(Idx);
Jakob Stoklund Olesen68257e62010-11-15 20:55:49 +0000436 assert(VNI && VNI->def.getDefIndex() == Idx && "Inconsistent VNInfo");
437 Idx = VNI->def;
438
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000439 TII.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, StackSlot, RC, &TRI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000440 --MI; // Point to store instruction.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000441 SlotIndex StoreIdx = LIS.InsertMachineInstrInMaps(MI).getDefIndex();
442 VRM.addSpillSlotUse(StackSlot, MI);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000443 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000444 VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, LIS.getVNInfoAllocator());
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000445 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
446}
447
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000448/// spillAroundUses - insert spill code around each use of Reg.
449void InlineSpiller::spillAroundUses(unsigned Reg) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000450 LiveInterval &OldLI = LIS.getInterval(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000451
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000452 // Iterate over instructions using Reg.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000453 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000454 MachineInstr *MI = RI.skipInstruction();) {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000455
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000456 // Debug values are not allowed to affect codegen.
457 if (MI->isDebugValue()) {
458 // Modify DBG_VALUE now that the value is in a spill slot.
459 uint64_t Offset = MI->getOperand(1).getImm();
460 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
461 DebugLoc DL = MI->getDebugLoc();
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000462 if (MachineInstr *NewDV = TII.emitFrameIndexDebugValue(MF, StackSlot,
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000463 Offset, MDPtr, DL)) {
464 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
465 MachineBasicBlock *MBB = MI->getParent();
466 MBB->insert(MBB->erase(MI), NewDV);
467 } else {
468 DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
469 MI->eraseFromParent();
470 }
471 continue;
472 }
473
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000474 // Ignore copies to/from snippets. We'll delete them.
475 if (SnippetCopies.count(MI))
476 continue;
477
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000478 // Stack slot accesses may coalesce away.
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000479 if (coalesceStackAccess(MI, Reg))
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000480 continue;
481
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000482 // Analyze instruction.
483 bool Reads, Writes;
484 SmallVector<unsigned, 8> Ops;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000485 tie(Reads, Writes) = MI->readsWritesVirtualRegister(Reg, &Ops);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000486
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000487 // Attempt to fold memory ops.
488 if (foldMemoryOperand(MI, Ops))
489 continue;
490
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000491 // Allocate interval around instruction.
492 // FIXME: Infer regclass from instruction alone.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000493 LiveInterval &NewLI = Edit->create(MRI, LIS, VRM);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000494 NewLI.markNotSpillable();
495
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000496 if (Reads)
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000497 insertReload(NewLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000498
499 // Rewrite instruction operands.
500 bool hasLiveDef = false;
501 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
502 MachineOperand &MO = MI->getOperand(Ops[i]);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000503 MO.setReg(NewLI.reg);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000504 if (MO.isUse()) {
505 if (!MI->isRegTiedToDefOperand(Ops[i]))
506 MO.setIsKill();
507 } else {
508 if (!MO.isDead())
509 hasLiveDef = true;
510 }
511 }
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000512
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000513 // FIXME: Use a second vreg if instruction has no tied ops.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000514 if (Writes && hasLiveDef)
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000515 insertSpill(NewLI, OldLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000516
517 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000518 }
519}
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000520
521void InlineSpiller::spill(LiveRangeEdit &edit) {
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000522 Edit = &edit;
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000523 assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
524 && "Trying to spill a stack slot.");
525 DEBUG(dbgs() << "Inline spilling "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000526 << MRI.getRegClass(edit.getReg())->getName()
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000527 << ':' << edit.getParent() << "\nFrom original "
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000528 << PrintReg(VRM.getOriginal(edit.getReg())) << '\n');
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000529 assert(edit.getParent().isSpillable() &&
530 "Attempting to spill already spilled value.");
531
532 // Share a stack slot among all descendants of Orig.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000533 unsigned Orig = VRM.getOriginal(edit.getReg());
534 StackSlot = VRM.getStackSlot(Orig);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000535
536 collectRegsToSpill();
537
538 reMaterializeAll();
539
540 // Remat may handle everything.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000541 if (Edit->getParent().empty())
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000542 return;
543
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000544 RC = MRI.getRegClass(edit.getReg());
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000545
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000546 if (StackSlot == VirtRegMap::NO_STACK_SLOT)
547 StackSlot = VRM.assignVirt2StackSlot(Orig);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000548
549 if (Orig != edit.getReg())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000550 VRM.assignVirt2StackSlot(edit.getReg(), StackSlot);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000551
552 // Update LiveStacks now that we are committed to spilling.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000553 LiveInterval &stacklvr = LSS.getOrCreateInterval(StackSlot, RC);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000554 if (!stacklvr.hasAtLeastOneValue())
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000555 stacklvr.getNextValue(SlotIndex(), 0, LSS.getVNInfoAllocator());
Jakob Stoklund Olesenb1adbd12011-03-12 04:25:36 +0000556 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000557 stacklvr.MergeRangesInAsValue(LIS.getInterval(RegsToSpill[i]),
Jakob Stoklund Olesenb1adbd12011-03-12 04:25:36 +0000558 stacklvr.getValNumInfo(0));
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000559
560 // Spill around uses of all RegsToSpill.
561 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
562 spillAroundUses(RegsToSpill[i]);
563
564 // Finally delete the SnippetCopies.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000565 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(edit.getReg());
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000566 MachineInstr *MI = RI.skipInstruction();) {
567 assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
568 // FIXME: Do this with a LiveRangeEdit callback.
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000569 VRM.RemoveMachineInstrFromMaps(MI);
570 LIS.RemoveMachineInstrFromMaps(MI);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000571 MI->eraseFromParent();
572 }
573
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000574 for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
Jakob Stoklund Olesen766faf42011-03-14 19:56:43 +0000575 edit.eraseVirtReg(RegsToSpill[i], LIS);
Jakob Stoklund Olesen10a43322011-03-12 04:17:20 +0000576}