blob: 872a829df59a89a6b697c517379fa716883f1fc0 [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
15#define DEBUG_TYPE "spiller"
16#include "Spiller.h"
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000017#include "SplitKit.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000018#include "VirtRegMap.h"
19#include "llvm/CodeGen/LiveIntervalAnalysis.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
Jakob Stoklund Olesen9529a1c2010-07-19 18:41:20 +000022#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28
29using namespace llvm;
30
31namespace {
32class InlineSpiller : public Spiller {
33 MachineFunction &mf_;
34 LiveIntervals &lis_;
Jakob Stoklund Olesen9529a1c2010-07-19 18:41:20 +000035 MachineLoopInfo &loops_;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000036 VirtRegMap &vrm_;
37 MachineFrameInfo &mfi_;
38 MachineRegisterInfo &mri_;
39 const TargetInstrInfo &tii_;
40 const TargetRegisterInfo &tri_;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000041 const BitVector reserved_;
42
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000043 SplitAnalysis splitAnalysis_;
44
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000045 // Variables that are valid during spill(), but used by multiple methods.
46 LiveInterval *li_;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000047 std::vector<LiveInterval*> *newIntervals_;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000048 const TargetRegisterClass *rc_;
49 int stackSlot_;
50 const SmallVectorImpl<LiveInterval*> *spillIs_;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000051
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000052 // Values of the current interval that can potentially remat.
53 SmallPtrSet<VNInfo*, 8> reMattable_;
54
55 // Values in reMattable_ that failed to remat at some point.
56 SmallPtrSet<VNInfo*, 8> usedValues_;
57
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000058 ~InlineSpiller() {}
59
60public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000061 InlineSpiller(MachineFunctionPass &pass,
62 MachineFunction &mf,
63 VirtRegMap &vrm)
64 : mf_(mf),
65 lis_(pass.getAnalysis<LiveIntervals>()),
66 loops_(pass.getAnalysis<MachineLoopInfo>()),
67 vrm_(vrm),
68 mfi_(*mf.getFrameInfo()),
69 mri_(mf.getRegInfo()),
70 tii_(*mf.getTarget().getInstrInfo()),
71 tri_(*mf.getTarget().getRegisterInfo()),
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000072 reserved_(tri_.getReservedRegs(mf_)),
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000073 splitAnalysis_(mf, lis_, loops_) {}
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000074
75 void spill(LiveInterval *li,
76 std::vector<LiveInterval*> &newIntervals,
77 SmallVectorImpl<LiveInterval*> &spillIs,
78 SlotIndex *earliestIndex);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000079
80private:
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +000081 bool split();
82
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +000083 bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
84 SlotIndex UseIdx);
85 bool reMaterializeFor(MachineBasicBlock::iterator MI);
86 void reMaterializeAll();
87
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +000088 bool coalesceStackAccess(MachineInstr *MI);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +000089 bool foldMemoryOperand(MachineBasicBlock::iterator MI,
90 const SmallVectorImpl<unsigned> &Ops);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +000091 void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
92 void insertSpill(LiveInterval &NewLI, MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000093};
94}
95
96namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000097Spiller *createInlineSpiller(MachineFunctionPass &pass,
98 MachineFunction &mf,
99 VirtRegMap &vrm) {
100 return new InlineSpiller(pass, mf, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000101}
102}
103
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000104/// split - try splitting the current interval into pieces that may allocate
105/// separately. Return true if successful.
106bool InlineSpiller::split() {
107 // FIXME: Add intra-MBB splitting.
108 if (lis_.intervalIsInOneMBB(*li_))
109 return false;
110
111 splitAnalysis_.analyze(li_);
112
113 if (const MachineLoop *loop = splitAnalysis_.getBestSplitLoop()) {
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000114 SplitEditor(splitAnalysis_, lis_, vrm_, *newIntervals_)
115 .splitAroundLoop(loop);
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000116 return true;
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000117 }
118 return false;
119}
120
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000121/// allUsesAvailableAt - Return true if all registers used by OrigMI at
122/// OrigIdx are also available with the same value at UseIdx.
123bool InlineSpiller::allUsesAvailableAt(const MachineInstr *OrigMI,
124 SlotIndex OrigIdx,
125 SlotIndex UseIdx) {
126 OrigIdx = OrigIdx.getUseIndex();
127 UseIdx = UseIdx.getUseIndex();
128 for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
129 const MachineOperand &MO = OrigMI->getOperand(i);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000130 if (!MO.isReg() || !MO.getReg() || MO.getReg() == li_->reg)
131 continue;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000132 // Reserved registers are OK.
133 if (MO.isUndef() || !lis_.hasInterval(MO.getReg()))
134 continue;
135 // We don't want to move any defs.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000136 if (MO.isDef())
137 return false;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000138 // We cannot depend on virtual registers in spillIs_. They will be spilled.
139 for (unsigned si = 0, se = spillIs_->size(); si != se; ++si)
140 if ((*spillIs_)[si]->reg == MO.getReg())
141 return false;
142
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000143 LiveInterval &LI = lis_.getInterval(MO.getReg());
144 const VNInfo *OVNI = LI.getVNInfoAt(OrigIdx);
145 if (!OVNI)
146 continue;
147 if (OVNI != LI.getVNInfoAt(UseIdx))
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000148 return false;
149 }
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000150 return true;
151}
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000152
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000153/// reMaterializeFor - Attempt to rematerialize li_->reg before MI instead of
154/// reloading it.
155bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) {
156 SlotIndex UseIdx = lis_.getInstructionIndex(MI).getUseIndex();
157 VNInfo *OrigVNI = li_->getVNInfoAt(UseIdx);
158 if (!OrigVNI) {
159 DEBUG(dbgs() << "\tadding <undef> flags: ");
160 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
161 MachineOperand &MO = MI->getOperand(i);
162 if (MO.isReg() && MO.isUse() && MO.getReg() == li_->reg)
163 MO.setIsUndef();
164 }
165 DEBUG(dbgs() << UseIdx << '\t' << *MI);
166 return true;
167 }
168 if (!reMattable_.count(OrigVNI)) {
169 DEBUG(dbgs() << "\tusing non-remat valno " << OrigVNI->id << ": "
170 << UseIdx << '\t' << *MI);
171 return false;
172 }
173 MachineInstr *OrigMI = lis_.getInstructionFromIndex(OrigVNI->def);
174 if (!allUsesAvailableAt(OrigMI, OrigVNI->def, UseIdx)) {
175 usedValues_.insert(OrigVNI);
176 DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
177 return false;
178 }
179
180 // If the instruction also writes li_->reg, it had better not require the same
181 // register for uses and defs.
182 bool Reads, Writes;
183 SmallVector<unsigned, 8> Ops;
184 tie(Reads, Writes) = MI->readsWritesVirtualRegister(li_->reg, &Ops);
185 if (Writes) {
186 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
187 MachineOperand &MO = MI->getOperand(Ops[i]);
188 if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) {
189 usedValues_.insert(OrigVNI);
190 DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
191 return false;
192 }
193 }
194 }
195
196 // Alocate a new register for the remat.
197 unsigned NewVReg = mri_.createVirtualRegister(rc_);
198 vrm_.grow();
199 LiveInterval &NewLI = lis_.getOrCreateInterval(NewVReg);
200 NewLI.markNotSpillable();
201 newIntervals_->push_back(&NewLI);
202
203 // Finally we can rematerialize OrigMI before MI.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000204 MachineBasicBlock &MBB = *MI->getParent();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000205 tii_.reMaterialize(MBB, MI, NewLI.reg, 0, OrigMI, tri_);
206 MachineBasicBlock::iterator RematMI = MI;
207 SlotIndex DefIdx = lis_.InsertMachineInstrInMaps(--RematMI).getDefIndex();
208 DEBUG(dbgs() << "\tremat: " << DefIdx << '\t' << *RematMI);
209
210 // Replace operands
211 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
212 MachineOperand &MO = MI->getOperand(Ops[i]);
213 if (MO.isReg() && MO.isUse() && MO.getReg() == li_->reg) {
214 MO.setReg(NewVReg);
215 MO.setIsKill();
216 }
217 }
218 DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI);
219
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000220 VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, true,
221 lis_.getVNInfoAllocator());
222 NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI));
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000223 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000224 return true;
225}
226
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000227/// reMaterializeAll - Try to rematerialize as many uses of li_ as possible,
228/// and trim the live ranges after.
229void InlineSpiller::reMaterializeAll() {
230 // Do a quick scan of the interval values to find if any are remattable.
231 reMattable_.clear();
232 usedValues_.clear();
233 for (LiveInterval::const_vni_iterator I = li_->vni_begin(),
234 E = li_->vni_end(); I != E; ++I) {
235 VNInfo *VNI = *I;
236 if (VNI->isUnused() || !VNI->isDefAccurate())
237 continue;
238 MachineInstr *DefMI = lis_.getInstructionFromIndex(VNI->def);
239 if (!DefMI || !tii_.isTriviallyReMaterializable(DefMI))
240 continue;
241 reMattable_.insert(VNI);
242 }
243
244 // Often, no defs are remattable.
245 if (reMattable_.empty())
246 return;
247
248 // Try to remat before all uses of li_->reg.
249 bool anyRemat = false;
250 for (MachineRegisterInfo::use_nodbg_iterator
251 RI = mri_.use_nodbg_begin(li_->reg);
252 MachineInstr *MI = RI.skipInstruction();)
253 anyRemat |= reMaterializeFor(MI);
254
255 if (!anyRemat)
256 return;
257
258 // Remove any values that were completely rematted.
259 bool anyRemoved = false;
260 for (SmallPtrSet<VNInfo*, 8>::iterator I = reMattable_.begin(),
261 E = reMattable_.end(); I != E; ++I) {
262 VNInfo *VNI = *I;
263 if (VNI->hasPHIKill() || usedValues_.count(VNI))
264 continue;
265 MachineInstr *DefMI = lis_.getInstructionFromIndex(VNI->def);
266 DEBUG(dbgs() << "\tremoving dead def: " << VNI->def << '\t' << *DefMI);
267 lis_.RemoveMachineInstrFromMaps(DefMI);
268 vrm_.RemoveMachineInstrFromMaps(DefMI);
269 DefMI->eraseFromParent();
270 li_->removeValNo(VNI);
271 anyRemoved = true;
272 }
273
274 if (!anyRemoved)
275 return;
276
277 // Removing values may cause debug uses where li_ is not live.
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000278 for (MachineRegisterInfo::use_iterator RI = mri_.use_begin(li_->reg);
279 MachineInstr *MI = RI.skipInstruction();) {
280 if (!MI->isDebugValue())
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000281 continue;
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000282 // Try to preserve the debug value if li_ is live immediately after it.
283 MachineBasicBlock::iterator NextMI = MI;
284 ++NextMI;
285 if (NextMI != MI->getParent()->end() && !lis_.isNotInMIMap(NextMI)) {
286 SlotIndex NearIdx = lis_.getInstructionIndex(NextMI);
287 if (li_->liveAt(NearIdx))
288 continue;
289 }
290 DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI);
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000291 MI->eraseFromParent();
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000292 }
293}
294
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000295/// If MI is a load or store of stackSlot_, it can be removed.
296bool InlineSpiller::coalesceStackAccess(MachineInstr *MI) {
297 int FI = 0;
298 unsigned reg;
299 if (!(reg = tii_.isLoadFromStackSlot(MI, FI)) &&
300 !(reg = tii_.isStoreToStackSlot(MI, FI)))
301 return false;
302
303 // We have a stack access. Is it the right register and slot?
304 if (reg != li_->reg || FI != stackSlot_)
305 return false;
306
307 DEBUG(dbgs() << "Coalescing stack access: " << *MI);
308 lis_.RemoveMachineInstrFromMaps(MI);
309 MI->eraseFromParent();
310 return true;
311}
312
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000313/// foldMemoryOperand - Try folding stack slot references in Ops into MI.
314/// Return true on success, and MI will be erased.
315bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI,
316 const SmallVectorImpl<unsigned> &Ops) {
317 // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
318 // operands.
319 SmallVector<unsigned, 8> FoldOps;
320 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
321 unsigned Idx = Ops[i];
322 MachineOperand &MO = MI->getOperand(Idx);
323 if (MO.isImplicit())
324 continue;
325 // FIXME: Teach targets to deal with subregs.
326 if (MO.getSubReg())
327 return false;
328 // Tied use operands should not be passed to foldMemoryOperand.
329 if (!MI->isRegTiedToDefOperand(Idx))
330 FoldOps.push_back(Idx);
331 }
332
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000333 MachineInstr *FoldMI = tii_.foldMemoryOperand(MI, FoldOps, stackSlot_);
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000334 if (!FoldMI)
335 return false;
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000336 lis_.ReplaceMachineInstrInMaps(MI, FoldMI);
337 vrm_.addSpillSlotUse(stackSlot_, FoldMI);
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000338 MI->eraseFromParent();
Jakob Stoklund Olesene72a5c52010-07-01 00:13:04 +0000339 DEBUG(dbgs() << "\tfolded: " << *FoldMI);
340 return true;
341}
342
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000343/// insertReload - Insert a reload of NewLI.reg before MI.
344void InlineSpiller::insertReload(LiveInterval &NewLI,
345 MachineBasicBlock::iterator MI) {
346 MachineBasicBlock &MBB = *MI->getParent();
347 SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
348 tii_.loadRegFromStackSlot(MBB, MI, NewLI.reg, stackSlot_, rc_, &tri_);
349 --MI; // Point to load instruction.
350 SlotIndex LoadIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
351 vrm_.addSpillSlotUse(stackSlot_, MI);
352 DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI);
353 VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0, true,
354 lis_.getVNInfoAllocator());
355 NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
356}
357
358/// insertSpill - Insert a spill of NewLI.reg after MI.
359void InlineSpiller::insertSpill(LiveInterval &NewLI,
360 MachineBasicBlock::iterator MI) {
361 MachineBasicBlock &MBB = *MI->getParent();
362 SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex();
363 tii_.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, stackSlot_, rc_, &tri_);
364 --MI; // Point to store instruction.
365 SlotIndex StoreIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex();
366 vrm_.addSpillSlotUse(stackSlot_, MI);
367 DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
368 VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, true,
369 lis_.getVNInfoAllocator());
370 NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
371}
372
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000373void InlineSpiller::spill(LiveInterval *li,
374 std::vector<LiveInterval*> &newIntervals,
375 SmallVectorImpl<LiveInterval*> &spillIs,
376 SlotIndex *earliestIndex) {
377 DEBUG(dbgs() << "Inline spilling " << *li << "\n");
378 assert(li->isSpillable() && "Attempting to spill already spilled value.");
379 assert(!li->isStackSlot() && "Trying to spill a stack slot.");
380
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000381 li_ = li;
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000382 newIntervals_ = &newIntervals;
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000383 rc_ = mri_.getRegClass(li->reg);
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000384 spillIs_ = &spillIs;
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000385
Jakob Stoklund Olesen8ae02632010-07-20 15:41:07 +0000386 if (split())
387 return;
388
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000389 reMaterializeAll();
390
391 // Remat may handle everything.
392 if (li_->empty())
393 return;
394
Jakob Stoklund Olesen7536f722010-08-04 22:08:39 +0000395 stackSlot_ = vrm_.getStackSlot(li->reg);
396 if (stackSlot_ == VirtRegMap::NO_STACK_SLOT)
397 stackSlot_ = vrm_.assignVirt2StackSlot(li->reg);
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000398
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000399 // Iterate over instructions using register.
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000400 for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(li->reg);
401 MachineInstr *MI = RI.skipInstruction();) {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000402
Jakob Stoklund Olesen3b9c7eb2010-07-02 19:54:40 +0000403 // Debug values are not allowed to affect codegen.
404 if (MI->isDebugValue()) {
405 // Modify DBG_VALUE now that the value is in a spill slot.
406 uint64_t Offset = MI->getOperand(1).getImm();
407 const MDNode *MDPtr = MI->getOperand(2).getMetadata();
408 DebugLoc DL = MI->getDebugLoc();
409 if (MachineInstr *NewDV = tii_.emitFrameIndexDebugValue(mf_, stackSlot_,
410 Offset, MDPtr, DL)) {
411 DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
412 MachineBasicBlock *MBB = MI->getParent();
413 MBB->insert(MBB->erase(MI), NewDV);
414 } else {
415 DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
416 MI->eraseFromParent();
417 }
418 continue;
419 }
420
Jakob Stoklund Olesen1a0f91b2010-08-04 22:35:11 +0000421 // Stack slot accesses may coalesce away.
422 if (coalesceStackAccess(MI))
423 continue;
424
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000425 // Analyze instruction.
426 bool Reads, Writes;
427 SmallVector<unsigned, 8> Ops;
428 tie(Reads, Writes) = MI->readsWritesVirtualRegister(li->reg, &Ops);
429
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000430 // Attempt to fold memory ops.
431 if (foldMemoryOperand(MI, Ops))
432 continue;
433
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000434 // Allocate interval around instruction.
435 // FIXME: Infer regclass from instruction alone.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000436 unsigned NewVReg = mri_.createVirtualRegister(rc_);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000437 vrm_.grow();
438 LiveInterval &NewLI = lis_.getOrCreateInterval(NewVReg);
439 NewLI.markNotSpillable();
440
Jakob Stoklund Olesen8de3b1e2010-07-02 17:44:57 +0000441 if (Reads)
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000442 insertReload(NewLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000443
444 // Rewrite instruction operands.
445 bool hasLiveDef = false;
446 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
447 MachineOperand &MO = MI->getOperand(Ops[i]);
448 MO.setReg(NewVReg);
449 if (MO.isUse()) {
450 if (!MI->isRegTiedToDefOperand(Ops[i]))
451 MO.setIsKill();
452 } else {
453 if (!MO.isDead())
454 hasLiveDef = true;
455 }
456 }
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000457
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000458 // FIXME: Use a second vreg if instruction has no tied ops.
Jakob Stoklund Olesen9e55afb2010-06-30 23:03:52 +0000459 if (Writes && hasLiveDef)
460 insertSpill(NewLI, MI);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000461
462 DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
463 newIntervals.push_back(&NewLI);
464 }
465}