blob: bc8b3aea34c1fdda8c6fbea312cc4ccbde808225 [file] [log] [blame]
Lang Hamese2b201b2009-05-18 19:03:16 +00001//===-- llvm/CodeGen/Spiller.cpp - Spiller -------------------------------===//
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#define DEBUG_TYPE "spiller"
11
12#include "Spiller.h"
13#include "VirtRegMap.h"
14#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000016#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000018#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/TargetInstrInfo.h"
Lang Hames835ca072009-11-19 04:15:33 +000020#include "llvm/Support/CommandLine.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000021#include "llvm/Support/Debug.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000022#include "llvm/Support/raw_ostream.h"
Lang Hames61945692009-12-09 05:39:12 +000023#include <set>
Lang Hamese2b201b2009-05-18 19:03:16 +000024
Lang Hamese2b201b2009-05-18 19:03:16 +000025using namespace llvm;
26
Lang Hames835ca072009-11-19 04:15:33 +000027namespace {
Lang Hames61945692009-12-09 05:39:12 +000028 enum SpillerName { trivial, standard, splitting };
Lang Hames835ca072009-11-19 04:15:33 +000029}
30
31static cl::opt<SpillerName>
32spillerOpt("spiller",
33 cl::desc("Spiller to use: (default: standard)"),
34 cl::Prefix,
Lang Hames61945692009-12-09 05:39:12 +000035 cl::values(clEnumVal(trivial, "trivial spiller"),
36 clEnumVal(standard, "default spiller"),
37 clEnumVal(splitting, "splitting spiller"),
Lang Hames835ca072009-11-19 04:15:33 +000038 clEnumValEnd),
39 cl::init(standard));
40
Lang Hames61945692009-12-09 05:39:12 +000041// Spiller virtual destructor implementation.
Lang Hamese2b201b2009-05-18 19:03:16 +000042Spiller::~Spiller() {}
43
44namespace {
45
Lang Hamesf41538d2009-06-02 16:53:25 +000046/// Utility class for spillers.
47class SpillerBase : public Spiller {
48protected:
Lang Hamesf41538d2009-06-02 16:53:25 +000049 MachineFunction *mf;
50 LiveIntervals *lis;
Lang Hamesf41538d2009-06-02 16:53:25 +000051 MachineFrameInfo *mfi;
52 MachineRegisterInfo *mri;
53 const TargetInstrInfo *tii;
Evan Cheng746ad692010-05-06 19:06:44 +000054 const TargetRegisterInfo *tri;
Lang Hamesf41538d2009-06-02 16:53:25 +000055 VirtRegMap *vrm;
56
57 /// Construct a spiller base.
Lang Hames8783e402009-11-20 00:53:30 +000058 SpillerBase(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
59 : mf(mf), lis(lis), vrm(vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000060 {
61 mfi = mf->getFrameInfo();
62 mri = &mf->getRegInfo();
63 tii = mf->getTarget().getInstrInfo();
Evan Cheng746ad692010-05-06 19:06:44 +000064 tri = mf->getTarget().getRegisterInfo();
Lang Hamese2b201b2009-05-18 19:03:16 +000065 }
66
Lang Hamesf41538d2009-06-02 16:53:25 +000067 /// Add spill ranges for every use/def of the live interval, inserting loads
Lang Hames38283e22009-11-18 20:31:20 +000068 /// immediately before each use, and stores after each def. No folding or
69 /// remat is attempted.
Lang Hamesf41538d2009-06-02 16:53:25 +000070 std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) {
David Greene65de5042010-01-05 01:25:55 +000071 DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
Lang Hamese2b201b2009-05-18 19:03:16 +000072
73 assert(li->weight != HUGE_VALF &&
74 "Attempting to spill already spilled value.");
75
76 assert(!li->isStackSlot() &&
77 "Trying to spill a stack slot.");
78
David Greene65de5042010-01-05 01:25:55 +000079 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
Lang Hames6bbc73d2009-06-24 20:46:24 +000080
Lang Hamese2b201b2009-05-18 19:03:16 +000081 std::vector<LiveInterval*> added;
82
83 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +000084 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
85
Lang Hames38283e22009-11-18 20:31:20 +000086 // Iterate over reg uses/defs.
Lang Hamesf41538d2009-06-02 16:53:25 +000087 for (MachineRegisterInfo::reg_iterator
88 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +000089
Lang Hames38283e22009-11-18 20:31:20 +000090 // Grab the use/def instr.
Lang Hamese2b201b2009-05-18 19:03:16 +000091 MachineInstr *mi = &*regItr;
Lang Hames6bbc73d2009-06-24 20:46:24 +000092
David Greene65de5042010-01-05 01:25:55 +000093 DEBUG(dbgs() << " Processing " << *mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +000094
Lang Hames38283e22009-11-18 20:31:20 +000095 // Step regItr to the next use/def instr.
Lang Hamesf41538d2009-06-02 16:53:25 +000096 do {
97 ++regItr;
98 } while (regItr != mri->reg_end() && (&*regItr == mi));
99
Lang Hames38283e22009-11-18 20:31:20 +0000100 // Collect uses & defs for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000101 SmallVector<unsigned, 2> indices;
102 bool hasUse = false;
103 bool hasDef = false;
Lang Hamese2b201b2009-05-18 19:03:16 +0000104 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
105 MachineOperand &op = mi->getOperand(i);
Lang Hamese2b201b2009-05-18 19:03:16 +0000106 if (!op.isReg() || op.getReg() != li->reg)
107 continue;
Lang Hamese2b201b2009-05-18 19:03:16 +0000108 hasUse |= mi->getOperand(i).isUse();
109 hasDef |= mi->getOperand(i).isDef();
Lang Hamese2b201b2009-05-18 19:03:16 +0000110 indices.push_back(i);
111 }
112
Lang Hames38283e22009-11-18 20:31:20 +0000113 // Create a new vreg & interval for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000114 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000115 vrm->grow();
116 vrm->assignVirt2StackSlot(newVReg, ss);
Lang Hamesf41538d2009-06-02 16:53:25 +0000117 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
118 newLI->weight = HUGE_VALF;
119
Lang Hames38283e22009-11-18 20:31:20 +0000120 // Update the reg operands & kill flags.
Lang Hamese2b201b2009-05-18 19:03:16 +0000121 for (unsigned i = 0; i < indices.size(); ++i) {
Lang Hames38283e22009-11-18 20:31:20 +0000122 unsigned mopIdx = indices[i];
123 MachineOperand &mop = mi->getOperand(mopIdx);
124 mop.setReg(newVReg);
125 if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
126 mop.setIsKill(true);
Lang Hamese2b201b2009-05-18 19:03:16 +0000127 }
128 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000129 assert(hasUse || hasDef);
130
Lang Hames38283e22009-11-18 20:31:20 +0000131 // Insert reload if necessary.
132 MachineBasicBlock::iterator miItr(mi);
Lang Hamese2b201b2009-05-18 19:03:16 +0000133 if (hasUse) {
Evan Cheng746ad692010-05-06 19:06:44 +0000134 tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc,
135 tri);
Lang Hames38283e22009-11-18 20:31:20 +0000136 MachineInstr *loadInstr(prior(miItr));
137 SlotIndex loadIndex =
138 lis->InsertMachineInstrInMaps(loadInstr).getDefIndex();
139 SlotIndex endIndex = loadIndex.getNextIndex();
140 VNInfo *loadVNI =
141 newLI->getNextValue(loadIndex, 0, true, lis->getVNInfoAllocator());
142 loadVNI->addKill(endIndex);
143 newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000144 }
145
Lang Hames38283e22009-11-18 20:31:20 +0000146 // Insert store if necessary.
Lang Hamese2b201b2009-05-18 19:03:16 +0000147 if (hasDef) {
Evan Chenge9b3ac22010-05-06 16:33:12 +0000148 tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg,
Evan Cheng746ad692010-05-06 19:06:44 +0000149 true, ss, trc, tri);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000150 MachineInstr *storeInstr(llvm::next(miItr));
Lang Hames38283e22009-11-18 20:31:20 +0000151 SlotIndex storeIndex =
152 lis->InsertMachineInstrInMaps(storeInstr).getDefIndex();
153 SlotIndex beginIndex = storeIndex.getPrevIndex();
154 VNInfo *storeVNI =
155 newLI->getNextValue(beginIndex, 0, true, lis->getVNInfoAllocator());
156 storeVNI->addKill(storeIndex);
157 newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000158 }
159
Lang Hamesf41538d2009-06-02 16:53:25 +0000160 added.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000161 }
162
Lang Hamese2b201b2009-05-18 19:03:16 +0000163 return added;
164 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000165};
Lang Hamese2b201b2009-05-18 19:03:16 +0000166
Chris Lattner1ca65312010-04-07 22:44:07 +0000167} // end anonymous namespace
168
169namespace {
Lang Hamese2b201b2009-05-18 19:03:16 +0000170
Lang Hamesf41538d2009-06-02 16:53:25 +0000171/// Spills any live range using the spill-everywhere method with no attempt at
172/// folding.
173class TrivialSpiller : public SpillerBase {
174public:
Lang Hames10382fb2009-06-19 02:17:53 +0000175
Lang Hames8783e402009-11-20 00:53:30 +0000176 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
177 : SpillerBase(mf, lis, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000178
Lang Hames835ca072009-11-19 04:15:33 +0000179 std::vector<LiveInterval*> spill(LiveInterval *li,
Lang Hames61945692009-12-09 05:39:12 +0000180 SmallVectorImpl<LiveInterval*> &spillIs,
181 SlotIndex*) {
Lang Hames835ca072009-11-19 04:15:33 +0000182 // Ignore spillIs - we don't use it.
Lang Hamesf41538d2009-06-02 16:53:25 +0000183 return trivialSpillEverywhere(li);
Lang Hamese2b201b2009-05-18 19:03:16 +0000184 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000185};
186
Chris Lattner1ca65312010-04-07 22:44:07 +0000187} // end anonymous namespace
188
189namespace {
190
Lang Hames835ca072009-11-19 04:15:33 +0000191/// Falls back on LiveIntervals::addIntervalsForSpills.
192class StandardSpiller : public Spiller {
Lang Hames61945692009-12-09 05:39:12 +0000193protected:
Lang Hames835ca072009-11-19 04:15:33 +0000194 LiveIntervals *lis;
195 const MachineLoopInfo *loopInfo;
196 VirtRegMap *vrm;
197public:
Lang Hames61945692009-12-09 05:39:12 +0000198 StandardSpiller(LiveIntervals *lis, const MachineLoopInfo *loopInfo,
199 VirtRegMap *vrm)
Lang Hames835ca072009-11-19 04:15:33 +0000200 : lis(lis), loopInfo(loopInfo), vrm(vrm) {}
201
202 /// Falls back on LiveIntervals::addIntervalsForSpills.
203 std::vector<LiveInterval*> spill(LiveInterval *li,
Lang Hames61945692009-12-09 05:39:12 +0000204 SmallVectorImpl<LiveInterval*> &spillIs,
205 SlotIndex*) {
Lang Hames835ca072009-11-19 04:15:33 +0000206 return lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
207 }
Lang Hames835ca072009-11-19 04:15:33 +0000208};
209
Chris Lattner1ca65312010-04-07 22:44:07 +0000210} // end anonymous namespace
211
212namespace {
213
Lang Hames61945692009-12-09 05:39:12 +0000214/// When a call to spill is placed this spiller will first try to break the
215/// interval up into its component values (one new interval per value).
216/// If this fails, or if a call is placed to spill a previously split interval
217/// then the spiller falls back on the standard spilling mechanism.
218class SplittingSpiller : public StandardSpiller {
219public:
220 SplittingSpiller(MachineFunction *mf, LiveIntervals *lis,
221 const MachineLoopInfo *loopInfo, VirtRegMap *vrm)
222 : StandardSpiller(lis, loopInfo, vrm) {
223
224 mri = &mf->getRegInfo();
225 tii = mf->getTarget().getInstrInfo();
226 tri = mf->getTarget().getRegisterInfo();
227 }
228
229 std::vector<LiveInterval*> spill(LiveInterval *li,
230 SmallVectorImpl<LiveInterval*> &spillIs,
231 SlotIndex *earliestStart) {
232
233 if (worthTryingToSplit(li)) {
234 return tryVNISplit(li, earliestStart);
235 }
236 // else
237 return StandardSpiller::spill(li, spillIs, earliestStart);
238 }
239
240private:
241
242 MachineRegisterInfo *mri;
243 const TargetInstrInfo *tii;
244 const TargetRegisterInfo *tri;
245 DenseSet<LiveInterval*> alreadySplit;
246
247 bool worthTryingToSplit(LiveInterval *li) const {
248 return (!alreadySplit.count(li) && li->getNumValNums() > 1);
249 }
250
251 /// Try to break a LiveInterval into its component values.
252 std::vector<LiveInterval*> tryVNISplit(LiveInterval *li,
253 SlotIndex *earliestStart) {
254
David Greene65de5042010-01-05 01:25:55 +0000255 DEBUG(dbgs() << "Trying VNI split of %reg" << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000256
257 std::vector<LiveInterval*> added;
258 SmallVector<VNInfo*, 4> vnis;
259
260 std::copy(li->vni_begin(), li->vni_end(), std::back_inserter(vnis));
261
262 for (SmallVectorImpl<VNInfo*>::iterator vniItr = vnis.begin(),
263 vniEnd = vnis.end(); vniItr != vniEnd; ++vniItr) {
264 VNInfo *vni = *vniItr;
265
266 // Skip unused VNIs, or VNIs with no kills.
267 if (vni->isUnused() || vni->kills.empty())
268 continue;
269
David Greene65de5042010-01-05 01:25:55 +0000270 DEBUG(dbgs() << " Extracted Val #" << vni->id << " as ");
Lang Hames61945692009-12-09 05:39:12 +0000271 LiveInterval *splitInterval = extractVNI(li, vni);
272
273 if (splitInterval != 0) {
David Greene65de5042010-01-05 01:25:55 +0000274 DEBUG(dbgs() << *splitInterval << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000275 added.push_back(splitInterval);
276 alreadySplit.insert(splitInterval);
277 if (earliestStart != 0) {
278 if (splitInterval->beginIndex() < *earliestStart)
279 *earliestStart = splitInterval->beginIndex();
280 }
281 } else {
David Greene65de5042010-01-05 01:25:55 +0000282 DEBUG(dbgs() << "0\n");
Lang Hames61945692009-12-09 05:39:12 +0000283 }
284 }
285
David Greene65de5042010-01-05 01:25:55 +0000286 DEBUG(dbgs() << "Original LI: " << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000287
288 // If there original interval still contains some live ranges
289 // add it to added and alreadySplit.
290 if (!li->empty()) {
291 added.push_back(li);
292 alreadySplit.insert(li);
293 if (earliestStart != 0) {
294 if (li->beginIndex() < *earliestStart)
295 *earliestStart = li->beginIndex();
296 }
297 }
298
299 return added;
300 }
301
302 /// Extract the given value number from the interval.
303 LiveInterval* extractVNI(LiveInterval *li, VNInfo *vni) const {
304 assert(vni->isDefAccurate() || vni->isPHIDef());
305 assert(!vni->kills.empty());
306
307 // Create a new vreg and live interval, copy VNI kills & ranges over.
308 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
309 unsigned newVReg = mri->createVirtualRegister(trc);
310 vrm->grow();
311 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
312 VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator());
313
314 // Start by copying all live ranges in the VN to the new interval.
315 for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end();
316 rItr != rEnd; ++rItr) {
317 if (rItr->valno == vni) {
318 newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI));
319 }
320 }
321
322 // Erase the old VNI & ranges.
323 li->removeValNo(vni);
324
325 // Collect all current uses of the register belonging to the given VNI.
326 // We'll use this to rename the register after we've dealt with the def.
327 std::set<MachineInstr*> uses;
328 for (MachineRegisterInfo::use_iterator
329 useItr = mri->use_begin(li->reg), useEnd = mri->use_end();
330 useItr != useEnd; ++useItr) {
331 uses.insert(&*useItr);
332 }
333
334 // Process the def instruction for this VNI.
335 if (newVNI->isPHIDef()) {
336 // Insert a copy at the start of the MBB. The range proceeding the
337 // copy will be attached to the original LiveInterval.
338 MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def);
339 tii->copyRegToReg(*defMBB, defMBB->begin(), newVReg, li->reg, trc, trc);
340 MachineInstr *copyMI = defMBB->begin();
341 copyMI->addRegisterKilled(li->reg, tri);
342 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
343 VNInfo *phiDefVNI = li->getNextValue(lis->getMBBStartIdx(defMBB),
344 0, false, lis->getVNInfoAllocator());
345 phiDefVNI->setIsPHIDef(true);
346 phiDefVNI->addKill(copyIdx.getDefIndex());
347 li->addRange(LiveRange(phiDefVNI->def, copyIdx.getDefIndex(), phiDefVNI));
348 LiveRange *oldPHIDefRange =
349 newLI->getLiveRangeContaining(lis->getMBBStartIdx(defMBB));
350
351 // If the old phi def starts in the middle of the range chop it up.
352 if (oldPHIDefRange->start < lis->getMBBStartIdx(defMBB)) {
353 LiveRange oldPHIDefRange2(copyIdx.getDefIndex(), oldPHIDefRange->end,
354 oldPHIDefRange->valno);
355 oldPHIDefRange->end = lis->getMBBStartIdx(defMBB);
356 newLI->addRange(oldPHIDefRange2);
357 } else if (oldPHIDefRange->start == lis->getMBBStartIdx(defMBB)) {
358 // Otherwise if it's at the start of the range just trim it.
359 oldPHIDefRange->start = copyIdx.getDefIndex();
360 } else {
361 assert(false && "PHI def range doesn't cover PHI def?");
362 }
363
364 newVNI->def = copyIdx.getDefIndex();
365 newVNI->setCopy(copyMI);
366 newVNI->setIsPHIDef(false); // not a PHI def anymore.
367 newVNI->setIsDefAccurate(true);
368 } else {
369 // non-PHI def. Rename the def. If it's two-addr that means renaming the use
370 // and inserting a new copy too.
371 MachineInstr *defInst = lis->getInstructionFromIndex(newVNI->def);
372 // We'll rename this now, so we can remove it from uses.
373 uses.erase(defInst);
374 unsigned defOpIdx = defInst->findRegisterDefOperandIdx(li->reg);
375 bool isTwoAddr = defInst->isRegTiedToUseOperand(defOpIdx),
376 twoAddrUseIsUndef = false;
377
378 for (unsigned i = 0; i < defInst->getNumOperands(); ++i) {
379 MachineOperand &mo = defInst->getOperand(i);
380 if (mo.isReg() && (mo.isDef() || isTwoAddr) && (mo.getReg()==li->reg)) {
381 mo.setReg(newVReg);
382 if (isTwoAddr && mo.isUse() && mo.isUndef())
383 twoAddrUseIsUndef = true;
384 }
385 }
386
387 SlotIndex defIdx = lis->getInstructionIndex(defInst);
388 newVNI->def = defIdx.getDefIndex();
389
390 if (isTwoAddr && !twoAddrUseIsUndef) {
391 MachineBasicBlock *defMBB = defInst->getParent();
392 tii->copyRegToReg(*defMBB, defInst, newVReg, li->reg, trc, trc);
393 MachineInstr *copyMI = prior(MachineBasicBlock::iterator(defInst));
394 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
395 copyMI->addRegisterKilled(li->reg, tri);
396 LiveRange *origUseRange =
397 li->getLiveRangeContaining(newVNI->def.getUseIndex());
398 VNInfo *origUseVNI = origUseRange->valno;
399 origUseRange->end = copyIdx.getDefIndex();
400 bool updatedKills = false;
401 for (unsigned k = 0; k < origUseVNI->kills.size(); ++k) {
402 if (origUseVNI->kills[k] == defIdx.getDefIndex()) {
403 origUseVNI->kills[k] = copyIdx.getDefIndex();
404 updatedKills = true;
405 break;
406 }
407 }
408 assert(updatedKills && "Failed to update VNI kill list.");
409 VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI,
410 true, lis->getVNInfoAllocator());
411 copyVNI->addKill(defIdx.getDefIndex());
412 LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI);
413 newLI->addRange(copyRange);
414 }
415 }
416
417 for (std::set<MachineInstr*>::iterator
418 usesItr = uses.begin(), usesEnd = uses.end();
419 usesItr != usesEnd; ++usesItr) {
420 MachineInstr *useInst = *usesItr;
421 SlotIndex useIdx = lis->getInstructionIndex(useInst);
422 LiveRange *useRange =
423 newLI->getLiveRangeContaining(useIdx.getUseIndex());
424
425 // If this use doesn't belong to the new interval skip it.
426 if (useRange == 0)
427 continue;
428
429 // This use doesn't belong to the VNI, skip it.
430 if (useRange->valno != newVNI)
431 continue;
432
433 // Check if this instr is two address.
434 unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg);
435 bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx);
436
437 // Rename uses (and defs for two-address instrs).
438 for (unsigned i = 0; i < useInst->getNumOperands(); ++i) {
439 MachineOperand &mo = useInst->getOperand(i);
440 if (mo.isReg() && (mo.isUse() || isTwoAddress) &&
441 (mo.getReg() == li->reg)) {
442 mo.setReg(newVReg);
443 }
444 }
445
446 // If this is a two address instruction we've got some extra work to do.
447 if (isTwoAddress) {
448 // We modified the def operand, so we need to copy back to the original
449 // reg.
450 MachineBasicBlock *useMBB = useInst->getParent();
451 MachineBasicBlock::iterator useItr(useInst);
452 tii->copyRegToReg(*useMBB, next(useItr), li->reg, newVReg, trc, trc);
453 MachineInstr *copyMI = next(useItr);
454 copyMI->addRegisterKilled(newVReg, tri);
455 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
456
457 // Change the old two-address defined range & vni to start at
458 // (and be defined by) the copy.
459 LiveRange *origDefRange =
460 li->getLiveRangeContaining(useIdx.getDefIndex());
461 origDefRange->start = copyIdx.getDefIndex();
462 origDefRange->valno->def = copyIdx.getDefIndex();
463 origDefRange->valno->setCopy(copyMI);
464
465 // Insert a new range & vni for the two-address-to-copy value. This
466 // will be attached to the new live interval.
467 VNInfo *copyVNI =
468 newLI->getNextValue(useIdx.getDefIndex(), 0, true,
469 lis->getVNInfoAllocator());
470 copyVNI->addKill(copyIdx.getDefIndex());
471 LiveRange copyRange(useIdx.getDefIndex(),copyIdx.getDefIndex(),copyVNI);
472 newLI->addRange(copyRange);
473 }
474 }
475
476 // Iterate over any PHI kills - we'll need to insert new copies for them.
477 for (VNInfo::KillSet::iterator
478 killItr = newVNI->kills.begin(), killEnd = newVNI->kills.end();
479 killItr != killEnd; ++killItr) {
480 SlotIndex killIdx(*killItr);
481 if (killItr->isPHI()) {
482 MachineBasicBlock *killMBB = lis->getMBBFromIndex(killIdx);
483 LiveRange *oldKillRange =
484 newLI->getLiveRangeContaining(killIdx);
485
486 assert(oldKillRange != 0 && "No kill range?");
487
488 tii->copyRegToReg(*killMBB, killMBB->getFirstTerminator(),
489 li->reg, newVReg, trc, trc);
490 MachineInstr *copyMI = prior(killMBB->getFirstTerminator());
491 copyMI->addRegisterKilled(newVReg, tri);
492 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
493
494 // Save the current end. We may need it to add a new range if the
495 // current range runs of the end of the MBB.
496 SlotIndex newKillRangeEnd = oldKillRange->end;
497 oldKillRange->end = copyIdx.getDefIndex();
498
Lang Hames74ab5ee2009-12-22 00:11:50 +0000499 if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) {
500 assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) &&
Lang Hames61945692009-12-09 05:39:12 +0000501 "PHI kill range doesn't reach kill-block end. Not sane.");
Lang Hames74ab5ee2009-12-22 00:11:50 +0000502 newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB),
Lang Hames61945692009-12-09 05:39:12 +0000503 newKillRangeEnd, newVNI));
504 }
505
506 *killItr = oldKillRange->end;
507 VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(),
508 copyMI, true,
509 lis->getVNInfoAllocator());
510 newKillVNI->addKill(lis->getMBBTerminatorGap(killMBB));
511 newKillVNI->setHasPHIKill(true);
512 li->addRange(LiveRange(copyIdx.getDefIndex(),
Lang Hames74ab5ee2009-12-22 00:11:50 +0000513 lis->getMBBEndIdx(killMBB),
Lang Hames61945692009-12-09 05:39:12 +0000514 newKillVNI));
515 }
516
517 }
518
519 newVNI->setHasPHIKill(false);
520
521 return newLI;
522 }
523
524};
525
Chris Lattner1ca65312010-04-07 22:44:07 +0000526} // end anonymous namespace
527
Lang Hamese2b201b2009-05-18 19:03:16 +0000528
Lang Hamese2b201b2009-05-18 19:03:16 +0000529llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hames835ca072009-11-19 04:15:33 +0000530 const MachineLoopInfo *loopInfo,
531 VirtRegMap *vrm) {
532 switch (spillerOpt) {
Chris Lattner1ca65312010-04-07 22:44:07 +0000533 default: assert(0 && "unknown spiller");
534 case trivial: return new TrivialSpiller(mf, lis, vrm);
535 case standard: return new StandardSpiller(lis, loopInfo, vrm);
536 case splitting: return new SplittingSpiller(mf, lis, loopInfo, vrm);
Lang Hames835ca072009-11-19 04:15:33 +0000537 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000538}