blob: aed18ed9ac4f574e311a5156767622ffa51ead09 [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"
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000022#include "llvm/Support/ErrorHandling.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000023#include "llvm/Support/raw_ostream.h"
Lang Hames61945692009-12-09 05:39:12 +000024#include <set>
Lang Hamese2b201b2009-05-18 19:03:16 +000025
Lang Hamese2b201b2009-05-18 19:03:16 +000026using namespace llvm;
27
Lang Hames835ca072009-11-19 04:15:33 +000028namespace {
Lang Hames61945692009-12-09 05:39:12 +000029 enum SpillerName { trivial, standard, splitting };
Lang Hames835ca072009-11-19 04:15:33 +000030}
31
32static cl::opt<SpillerName>
33spillerOpt("spiller",
34 cl::desc("Spiller to use: (default: standard)"),
35 cl::Prefix,
Lang Hames61945692009-12-09 05:39:12 +000036 cl::values(clEnumVal(trivial, "trivial spiller"),
37 clEnumVal(standard, "default spiller"),
38 clEnumVal(splitting, "splitting spiller"),
Lang Hames835ca072009-11-19 04:15:33 +000039 clEnumValEnd),
40 cl::init(standard));
41
Lang Hames61945692009-12-09 05:39:12 +000042// Spiller virtual destructor implementation.
Lang Hamese2b201b2009-05-18 19:03:16 +000043Spiller::~Spiller() {}
44
45namespace {
46
Lang Hamesf41538d2009-06-02 16:53:25 +000047/// Utility class for spillers.
48class SpillerBase : public Spiller {
49protected:
Lang Hamesf41538d2009-06-02 16:53:25 +000050 MachineFunction *mf;
51 LiveIntervals *lis;
Lang Hamesf41538d2009-06-02 16:53:25 +000052 MachineFrameInfo *mfi;
53 MachineRegisterInfo *mri;
54 const TargetInstrInfo *tii;
Evan Cheng746ad692010-05-06 19:06:44 +000055 const TargetRegisterInfo *tri;
Lang Hamesf41538d2009-06-02 16:53:25 +000056 VirtRegMap *vrm;
57
58 /// Construct a spiller base.
Lang Hames8783e402009-11-20 00:53:30 +000059 SpillerBase(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
60 : mf(mf), lis(lis), vrm(vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000061 {
62 mfi = mf->getFrameInfo();
63 mri = &mf->getRegInfo();
64 tii = mf->getTarget().getInstrInfo();
Evan Cheng746ad692010-05-06 19:06:44 +000065 tri = mf->getTarget().getRegisterInfo();
Lang Hamese2b201b2009-05-18 19:03:16 +000066 }
67
Lang Hamesf41538d2009-06-02 16:53:25 +000068 /// Add spill ranges for every use/def of the live interval, inserting loads
Lang Hames38283e22009-11-18 20:31:20 +000069 /// immediately before each use, and stores after each def. No folding or
70 /// remat is attempted.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +000071 void trivialSpillEverywhere(LiveInterval *li,
72 std::vector<LiveInterval*> &newIntervals) {
David Greene65de5042010-01-05 01:25:55 +000073 DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
Lang Hamese2b201b2009-05-18 19:03:16 +000074
75 assert(li->weight != HUGE_VALF &&
76 "Attempting to spill already spilled value.");
77
78 assert(!li->isStackSlot() &&
79 "Trying to spill a stack slot.");
80
David Greene65de5042010-01-05 01:25:55 +000081 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
Lang Hames6bbc73d2009-06-24 20:46:24 +000082
Lang Hamese2b201b2009-05-18 19:03:16 +000083 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());
Lang Hames38283e22009-11-18 20:31:20 +0000142 newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000143 }
144
Lang Hames38283e22009-11-18 20:31:20 +0000145 // Insert store if necessary.
Lang Hamese2b201b2009-05-18 19:03:16 +0000146 if (hasDef) {
Evan Chenge9b3ac22010-05-06 16:33:12 +0000147 tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg,
Evan Cheng746ad692010-05-06 19:06:44 +0000148 true, ss, trc, tri);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000149 MachineInstr *storeInstr(llvm::next(miItr));
Lang Hames38283e22009-11-18 20:31:20 +0000150 SlotIndex storeIndex =
151 lis->InsertMachineInstrInMaps(storeInstr).getDefIndex();
152 SlotIndex beginIndex = storeIndex.getPrevIndex();
153 VNInfo *storeVNI =
154 newLI->getNextValue(beginIndex, 0, true, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000155 newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000156 }
157
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000158 newIntervals.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000159 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000160 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000161};
Lang Hamese2b201b2009-05-18 19:03:16 +0000162
Chris Lattner1ca65312010-04-07 22:44:07 +0000163} // end anonymous namespace
164
165namespace {
Lang Hamese2b201b2009-05-18 19:03:16 +0000166
Lang Hamesf41538d2009-06-02 16:53:25 +0000167/// Spills any live range using the spill-everywhere method with no attempt at
168/// folding.
169class TrivialSpiller : public SpillerBase {
170public:
Lang Hames10382fb2009-06-19 02:17:53 +0000171
Lang Hames8783e402009-11-20 00:53:30 +0000172 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
173 : SpillerBase(mf, lis, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000174
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000175 void spill(LiveInterval *li,
176 std::vector<LiveInterval*> &newIntervals,
177 SmallVectorImpl<LiveInterval*> &,
178 SlotIndex*) {
Lang Hames835ca072009-11-19 04:15:33 +0000179 // Ignore spillIs - we don't use it.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000180 trivialSpillEverywhere(li, newIntervals);
Lang Hamese2b201b2009-05-18 19:03:16 +0000181 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000182};
183
Chris Lattner1ca65312010-04-07 22:44:07 +0000184} // end anonymous namespace
185
186namespace {
187
Lang Hames835ca072009-11-19 04:15:33 +0000188/// Falls back on LiveIntervals::addIntervalsForSpills.
189class StandardSpiller : public Spiller {
Lang Hames61945692009-12-09 05:39:12 +0000190protected:
Lang Hames835ca072009-11-19 04:15:33 +0000191 LiveIntervals *lis;
192 const MachineLoopInfo *loopInfo;
193 VirtRegMap *vrm;
194public:
Lang Hames61945692009-12-09 05:39:12 +0000195 StandardSpiller(LiveIntervals *lis, const MachineLoopInfo *loopInfo,
196 VirtRegMap *vrm)
Lang Hames835ca072009-11-19 04:15:33 +0000197 : lis(lis), loopInfo(loopInfo), vrm(vrm) {}
198
199 /// Falls back on LiveIntervals::addIntervalsForSpills.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000200 void spill(LiveInterval *li,
201 std::vector<LiveInterval*> &newIntervals,
202 SmallVectorImpl<LiveInterval*> &spillIs,
203 SlotIndex*) {
204 std::vector<LiveInterval*> added =
205 lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
206 newIntervals.insert(newIntervals.end(), added.begin(), added.end());
Lang Hames835ca072009-11-19 04:15:33 +0000207 }
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
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000229 void spill(LiveInterval *li,
230 std::vector<LiveInterval*> &newIntervals,
231 SmallVectorImpl<LiveInterval*> &spillIs,
232 SlotIndex *earliestStart) {
233 if (worthTryingToSplit(li))
234 tryVNISplit(li, earliestStart);
235 else
236 StandardSpiller::spill(li, newIntervals, spillIs, earliestStart);
Lang Hames61945692009-12-09 05:39:12 +0000237 }
238
239private:
240
241 MachineRegisterInfo *mri;
242 const TargetInstrInfo *tii;
243 const TargetRegisterInfo *tri;
244 DenseSet<LiveInterval*> alreadySplit;
245
246 bool worthTryingToSplit(LiveInterval *li) const {
247 return (!alreadySplit.count(li) && li->getNumValNums() > 1);
248 }
249
250 /// Try to break a LiveInterval into its component values.
251 std::vector<LiveInterval*> tryVNISplit(LiveInterval *li,
252 SlotIndex *earliestStart) {
253
David Greene65de5042010-01-05 01:25:55 +0000254 DEBUG(dbgs() << "Trying VNI split of %reg" << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000255
256 std::vector<LiveInterval*> added;
257 SmallVector<VNInfo*, 4> vnis;
258
259 std::copy(li->vni_begin(), li->vni_end(), std::back_inserter(vnis));
260
261 for (SmallVectorImpl<VNInfo*>::iterator vniItr = vnis.begin(),
262 vniEnd = vnis.end(); vniItr != vniEnd; ++vniItr) {
263 VNInfo *vni = *vniItr;
264
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000265 // Skip unused VNIs.
266 if (vni->isUnused())
Lang Hames61945692009-12-09 05:39:12 +0000267 continue;
268
David Greene65de5042010-01-05 01:25:55 +0000269 DEBUG(dbgs() << " Extracted Val #" << vni->id << " as ");
Lang Hames61945692009-12-09 05:39:12 +0000270 LiveInterval *splitInterval = extractVNI(li, vni);
271
272 if (splitInterval != 0) {
David Greene65de5042010-01-05 01:25:55 +0000273 DEBUG(dbgs() << *splitInterval << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000274 added.push_back(splitInterval);
275 alreadySplit.insert(splitInterval);
276 if (earliestStart != 0) {
277 if (splitInterval->beginIndex() < *earliestStart)
278 *earliestStart = splitInterval->beginIndex();
279 }
280 } else {
David Greene65de5042010-01-05 01:25:55 +0000281 DEBUG(dbgs() << "0\n");
Lang Hames61945692009-12-09 05:39:12 +0000282 }
283 }
284
David Greene65de5042010-01-05 01:25:55 +0000285 DEBUG(dbgs() << "Original LI: " << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000286
287 // If there original interval still contains some live ranges
288 // add it to added and alreadySplit.
289 if (!li->empty()) {
290 added.push_back(li);
291 alreadySplit.insert(li);
292 if (earliestStart != 0) {
293 if (li->beginIndex() < *earliestStart)
294 *earliestStart = li->beginIndex();
295 }
296 }
297
298 return added;
299 }
300
301 /// Extract the given value number from the interval.
302 LiveInterval* extractVNI(LiveInterval *li, VNInfo *vni) const {
303 assert(vni->isDefAccurate() || vni->isPHIDef());
Lang Hames61945692009-12-09 05:39:12 +0000304
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000305 // Create a new vreg and live interval, copy VNI ranges over.
Lang Hames61945692009-12-09 05:39:12 +0000306 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
307 unsigned newVReg = mri->createVirtualRegister(trc);
308 vrm->grow();
309 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
310 VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator());
311
312 // Start by copying all live ranges in the VN to the new interval.
313 for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end();
314 rItr != rEnd; ++rItr) {
315 if (rItr->valno == vni) {
316 newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI));
317 }
318 }
319
320 // Erase the old VNI & ranges.
321 li->removeValNo(vni);
322
323 // Collect all current uses of the register belonging to the given VNI.
324 // We'll use this to rename the register after we've dealt with the def.
325 std::set<MachineInstr*> uses;
326 for (MachineRegisterInfo::use_iterator
327 useItr = mri->use_begin(li->reg), useEnd = mri->use_end();
328 useItr != useEnd; ++useItr) {
329 uses.insert(&*useItr);
330 }
331
332 // Process the def instruction for this VNI.
333 if (newVNI->isPHIDef()) {
334 // Insert a copy at the start of the MBB. The range proceeding the
335 // copy will be attached to the original LiveInterval.
336 MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def);
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000337 tii->copyRegToReg(*defMBB, defMBB->begin(), newVReg, li->reg, trc, trc,
338 DebugLoc());
Lang Hames61945692009-12-09 05:39:12 +0000339 MachineInstr *copyMI = defMBB->begin();
340 copyMI->addRegisterKilled(li->reg, tri);
341 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
342 VNInfo *phiDefVNI = li->getNextValue(lis->getMBBStartIdx(defMBB),
343 0, false, lis->getVNInfoAllocator());
344 phiDefVNI->setIsPHIDef(true);
Lang Hames61945692009-12-09 05:39:12 +0000345 li->addRange(LiveRange(phiDefVNI->def, copyIdx.getDefIndex(), phiDefVNI));
346 LiveRange *oldPHIDefRange =
347 newLI->getLiveRangeContaining(lis->getMBBStartIdx(defMBB));
348
349 // If the old phi def starts in the middle of the range chop it up.
350 if (oldPHIDefRange->start < lis->getMBBStartIdx(defMBB)) {
351 LiveRange oldPHIDefRange2(copyIdx.getDefIndex(), oldPHIDefRange->end,
352 oldPHIDefRange->valno);
353 oldPHIDefRange->end = lis->getMBBStartIdx(defMBB);
354 newLI->addRange(oldPHIDefRange2);
355 } else if (oldPHIDefRange->start == lis->getMBBStartIdx(defMBB)) {
356 // Otherwise if it's at the start of the range just trim it.
357 oldPHIDefRange->start = copyIdx.getDefIndex();
358 } else {
359 assert(false && "PHI def range doesn't cover PHI def?");
360 }
361
362 newVNI->def = copyIdx.getDefIndex();
363 newVNI->setCopy(copyMI);
364 newVNI->setIsPHIDef(false); // not a PHI def anymore.
365 newVNI->setIsDefAccurate(true);
366 } else {
367 // non-PHI def. Rename the def. If it's two-addr that means renaming the use
368 // and inserting a new copy too.
369 MachineInstr *defInst = lis->getInstructionFromIndex(newVNI->def);
370 // We'll rename this now, so we can remove it from uses.
371 uses.erase(defInst);
372 unsigned defOpIdx = defInst->findRegisterDefOperandIdx(li->reg);
373 bool isTwoAddr = defInst->isRegTiedToUseOperand(defOpIdx),
374 twoAddrUseIsUndef = false;
375
376 for (unsigned i = 0; i < defInst->getNumOperands(); ++i) {
377 MachineOperand &mo = defInst->getOperand(i);
378 if (mo.isReg() && (mo.isDef() || isTwoAddr) && (mo.getReg()==li->reg)) {
379 mo.setReg(newVReg);
380 if (isTwoAddr && mo.isUse() && mo.isUndef())
381 twoAddrUseIsUndef = true;
382 }
383 }
384
385 SlotIndex defIdx = lis->getInstructionIndex(defInst);
386 newVNI->def = defIdx.getDefIndex();
387
388 if (isTwoAddr && !twoAddrUseIsUndef) {
389 MachineBasicBlock *defMBB = defInst->getParent();
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000390 tii->copyRegToReg(*defMBB, defInst, newVReg, li->reg, trc, trc,
391 DebugLoc());
Lang Hames61945692009-12-09 05:39:12 +0000392 MachineInstr *copyMI = prior(MachineBasicBlock::iterator(defInst));
393 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
394 copyMI->addRegisterKilled(li->reg, tri);
395 LiveRange *origUseRange =
396 li->getLiveRangeContaining(newVNI->def.getUseIndex());
Lang Hames61945692009-12-09 05:39:12 +0000397 origUseRange->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000398 VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI,
399 true, lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000400 LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI);
401 newLI->addRange(copyRange);
402 }
403 }
404
405 for (std::set<MachineInstr*>::iterator
406 usesItr = uses.begin(), usesEnd = uses.end();
407 usesItr != usesEnd; ++usesItr) {
408 MachineInstr *useInst = *usesItr;
409 SlotIndex useIdx = lis->getInstructionIndex(useInst);
410 LiveRange *useRange =
411 newLI->getLiveRangeContaining(useIdx.getUseIndex());
412
413 // If this use doesn't belong to the new interval skip it.
414 if (useRange == 0)
415 continue;
416
417 // This use doesn't belong to the VNI, skip it.
418 if (useRange->valno != newVNI)
419 continue;
420
421 // Check if this instr is two address.
422 unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg);
423 bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx);
424
425 // Rename uses (and defs for two-address instrs).
426 for (unsigned i = 0; i < useInst->getNumOperands(); ++i) {
427 MachineOperand &mo = useInst->getOperand(i);
428 if (mo.isReg() && (mo.isUse() || isTwoAddress) &&
429 (mo.getReg() == li->reg)) {
430 mo.setReg(newVReg);
431 }
432 }
433
434 // If this is a two address instruction we've got some extra work to do.
435 if (isTwoAddress) {
436 // We modified the def operand, so we need to copy back to the original
437 // reg.
438 MachineBasicBlock *useMBB = useInst->getParent();
439 MachineBasicBlock::iterator useItr(useInst);
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000440 tii->copyRegToReg(*useMBB, llvm::next(useItr), li->reg, newVReg, trc, trc,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000441 DebugLoc());
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000442 MachineInstr *copyMI = llvm::next(useItr);
Lang Hames61945692009-12-09 05:39:12 +0000443 copyMI->addRegisterKilled(newVReg, tri);
444 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
445
446 // Change the old two-address defined range & vni to start at
447 // (and be defined by) the copy.
448 LiveRange *origDefRange =
449 li->getLiveRangeContaining(useIdx.getDefIndex());
450 origDefRange->start = copyIdx.getDefIndex();
451 origDefRange->valno->def = copyIdx.getDefIndex();
452 origDefRange->valno->setCopy(copyMI);
453
454 // Insert a new range & vni for the two-address-to-copy value. This
455 // will be attached to the new live interval.
456 VNInfo *copyVNI =
457 newLI->getNextValue(useIdx.getDefIndex(), 0, true,
458 lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000459 LiveRange copyRange(useIdx.getDefIndex(),copyIdx.getDefIndex(),copyVNI);
460 newLI->addRange(copyRange);
461 }
462 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000463
Lang Hames61945692009-12-09 05:39:12 +0000464 // Iterate over any PHI kills - we'll need to insert new copies for them.
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000465 for (LiveInterval::iterator LRI = newLI->begin(), LRE = newLI->end();
466 LRI != LRE; ++LRI) {
467 if (LRI->valno != newVNI || LRI->end.isPHI())
468 continue;
469 SlotIndex killIdx = LRI->end;
470 MachineBasicBlock *killMBB = lis->getMBBFromIndex(killIdx);
Lang Hames61945692009-12-09 05:39:12 +0000471
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000472 tii->copyRegToReg(*killMBB, killMBB->getFirstTerminator(),
473 li->reg, newVReg, trc, trc,
474 DebugLoc());
475 MachineInstr *copyMI = prior(killMBB->getFirstTerminator());
476 copyMI->addRegisterKilled(newVReg, tri);
477 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000478
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000479 // Save the current end. We may need it to add a new range if the
480 // current range runs of the end of the MBB.
481 SlotIndex newKillRangeEnd = LRI->end;
482 LRI->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000483
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000484 if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) {
485 assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) &&
486 "PHI kill range doesn't reach kill-block end. Not sane.");
487 newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB),
488 newKillRangeEnd, newVNI));
Lang Hames61945692009-12-09 05:39:12 +0000489 }
490
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000491 VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(),
492 copyMI, true,
493 lis->getVNInfoAllocator());
494 newKillVNI->setHasPHIKill(true);
495 li->addRange(LiveRange(copyIdx.getDefIndex(),
496 lis->getMBBEndIdx(killMBB),
497 newKillVNI));
Lang Hames61945692009-12-09 05:39:12 +0000498 }
Lang Hames61945692009-12-09 05:39:12 +0000499 newVNI->setHasPHIKill(false);
500
501 return newLI;
502 }
503
504};
505
Chris Lattner1ca65312010-04-07 22:44:07 +0000506} // end anonymous namespace
507
Lang Hamese2b201b2009-05-18 19:03:16 +0000508
Lang Hamese2b201b2009-05-18 19:03:16 +0000509llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hames835ca072009-11-19 04:15:33 +0000510 const MachineLoopInfo *loopInfo,
511 VirtRegMap *vrm) {
512 switch (spillerOpt) {
Chris Lattner1ca65312010-04-07 22:44:07 +0000513 default: assert(0 && "unknown spiller");
514 case trivial: return new TrivialSpiller(mf, lis, vrm);
515 case standard: return new StandardSpiller(lis, loopInfo, vrm);
516 case splitting: return new SplittingSpiller(mf, lis, loopInfo, vrm);
Lang Hames835ca072009-11-19 04:15:33 +0000517 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000518}