blob: f4d6024f97dd0ca39d7ab4bc03f4036d8730b035 [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 {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000029 enum SpillerName { trivial, standard, splitting, inline_ };
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"),
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000039 "inline", inline_, "inline spiller",
Lang Hames835ca072009-11-19 04:15:33 +000040 clEnumValEnd),
41 cl::init(standard));
42
Lang Hames61945692009-12-09 05:39:12 +000043// Spiller virtual destructor implementation.
Lang Hamese2b201b2009-05-18 19:03:16 +000044Spiller::~Spiller() {}
45
46namespace {
47
Lang Hamesf41538d2009-06-02 16:53:25 +000048/// Utility class for spillers.
49class SpillerBase : public Spiller {
50protected:
Lang Hamesf41538d2009-06-02 16:53:25 +000051 MachineFunction *mf;
52 LiveIntervals *lis;
Lang Hamesf41538d2009-06-02 16:53:25 +000053 MachineFrameInfo *mfi;
54 MachineRegisterInfo *mri;
55 const TargetInstrInfo *tii;
Evan Cheng746ad692010-05-06 19:06:44 +000056 const TargetRegisterInfo *tri;
Lang Hamesf41538d2009-06-02 16:53:25 +000057 VirtRegMap *vrm;
58
59 /// Construct a spiller base.
Lang Hames8783e402009-11-20 00:53:30 +000060 SpillerBase(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
61 : mf(mf), lis(lis), vrm(vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000062 {
63 mfi = mf->getFrameInfo();
64 mri = &mf->getRegInfo();
65 tii = mf->getTarget().getInstrInfo();
Evan Cheng746ad692010-05-06 19:06:44 +000066 tri = mf->getTarget().getRegisterInfo();
Lang Hamese2b201b2009-05-18 19:03:16 +000067 }
68
Lang Hamesf41538d2009-06-02 16:53:25 +000069 /// Add spill ranges for every use/def of the live interval, inserting loads
Lang Hames38283e22009-11-18 20:31:20 +000070 /// immediately before each use, and stores after each def. No folding or
71 /// remat is attempted.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +000072 void trivialSpillEverywhere(LiveInterval *li,
73 std::vector<LiveInterval*> &newIntervals) {
David Greene65de5042010-01-05 01:25:55 +000074 DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
Lang Hamese2b201b2009-05-18 19:03:16 +000075
76 assert(li->weight != HUGE_VALF &&
77 "Attempting to spill already spilled value.");
78
79 assert(!li->isStackSlot() &&
80 "Trying to spill a stack slot.");
81
David Greene65de5042010-01-05 01:25:55 +000082 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
Lang Hames6bbc73d2009-06-24 20:46:24 +000083
Lang Hamese2b201b2009-05-18 19:03:16 +000084 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +000085 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
86
Lang Hames38283e22009-11-18 20:31:20 +000087 // Iterate over reg uses/defs.
Lang Hamesf41538d2009-06-02 16:53:25 +000088 for (MachineRegisterInfo::reg_iterator
89 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +000090
Lang Hames38283e22009-11-18 20:31:20 +000091 // Grab the use/def instr.
Lang Hamese2b201b2009-05-18 19:03:16 +000092 MachineInstr *mi = &*regItr;
Lang Hames6bbc73d2009-06-24 20:46:24 +000093
David Greene65de5042010-01-05 01:25:55 +000094 DEBUG(dbgs() << " Processing " << *mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +000095
Lang Hames38283e22009-11-18 20:31:20 +000096 // Step regItr to the next use/def instr.
Lang Hamesf41538d2009-06-02 16:53:25 +000097 do {
98 ++regItr;
99 } while (regItr != mri->reg_end() && (&*regItr == mi));
100
Lang Hames38283e22009-11-18 20:31:20 +0000101 // Collect uses & defs for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000102 SmallVector<unsigned, 2> indices;
103 bool hasUse = false;
104 bool hasDef = false;
Lang Hamese2b201b2009-05-18 19:03:16 +0000105 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
106 MachineOperand &op = mi->getOperand(i);
Lang Hamese2b201b2009-05-18 19:03:16 +0000107 if (!op.isReg() || op.getReg() != li->reg)
108 continue;
Lang Hamese2b201b2009-05-18 19:03:16 +0000109 hasUse |= mi->getOperand(i).isUse();
110 hasDef |= mi->getOperand(i).isDef();
Lang Hamese2b201b2009-05-18 19:03:16 +0000111 indices.push_back(i);
112 }
113
Lang Hames38283e22009-11-18 20:31:20 +0000114 // Create a new vreg & interval for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000115 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000116 vrm->grow();
117 vrm->assignVirt2StackSlot(newVReg, ss);
Lang Hamesf41538d2009-06-02 16:53:25 +0000118 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
119 newLI->weight = HUGE_VALF;
120
Lang Hames38283e22009-11-18 20:31:20 +0000121 // Update the reg operands & kill flags.
Lang Hamese2b201b2009-05-18 19:03:16 +0000122 for (unsigned i = 0; i < indices.size(); ++i) {
Lang Hames38283e22009-11-18 20:31:20 +0000123 unsigned mopIdx = indices[i];
124 MachineOperand &mop = mi->getOperand(mopIdx);
125 mop.setReg(newVReg);
126 if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
127 mop.setIsKill(true);
Lang Hamese2b201b2009-05-18 19:03:16 +0000128 }
129 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000130 assert(hasUse || hasDef);
131
Lang Hames38283e22009-11-18 20:31:20 +0000132 // Insert reload if necessary.
133 MachineBasicBlock::iterator miItr(mi);
Lang Hamese2b201b2009-05-18 19:03:16 +0000134 if (hasUse) {
Evan Cheng746ad692010-05-06 19:06:44 +0000135 tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc,
136 tri);
Lang Hames38283e22009-11-18 20:31:20 +0000137 MachineInstr *loadInstr(prior(miItr));
138 SlotIndex loadIndex =
139 lis->InsertMachineInstrInMaps(loadInstr).getDefIndex();
140 SlotIndex endIndex = loadIndex.getNextIndex();
141 VNInfo *loadVNI =
142 newLI->getNextValue(loadIndex, 0, true, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000143 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());
Lang Hames38283e22009-11-18 20:31:20 +0000156 newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000157 }
158
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000159 newIntervals.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000160 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000161 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000162};
Lang Hamese2b201b2009-05-18 19:03:16 +0000163
Chris Lattner1ca65312010-04-07 22:44:07 +0000164} // end anonymous namespace
165
166namespace {
Lang Hamese2b201b2009-05-18 19:03:16 +0000167
Lang Hamesf41538d2009-06-02 16:53:25 +0000168/// Spills any live range using the spill-everywhere method with no attempt at
169/// folding.
170class TrivialSpiller : public SpillerBase {
171public:
Lang Hames10382fb2009-06-19 02:17:53 +0000172
Lang Hames8783e402009-11-20 00:53:30 +0000173 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
174 : SpillerBase(mf, lis, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000175
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000176 void spill(LiveInterval *li,
177 std::vector<LiveInterval*> &newIntervals,
178 SmallVectorImpl<LiveInterval*> &,
179 SlotIndex*) {
Lang Hames835ca072009-11-19 04:15:33 +0000180 // Ignore spillIs - we don't use it.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000181 trivialSpillEverywhere(li, newIntervals);
Lang Hamese2b201b2009-05-18 19:03:16 +0000182 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000183};
184
Chris Lattner1ca65312010-04-07 22:44:07 +0000185} // end anonymous namespace
186
187namespace {
188
Lang Hames835ca072009-11-19 04:15:33 +0000189/// Falls back on LiveIntervals::addIntervalsForSpills.
190class StandardSpiller : public Spiller {
Lang Hames61945692009-12-09 05:39:12 +0000191protected:
Lang Hames835ca072009-11-19 04:15:33 +0000192 LiveIntervals *lis;
193 const MachineLoopInfo *loopInfo;
194 VirtRegMap *vrm;
195public:
Lang Hames61945692009-12-09 05:39:12 +0000196 StandardSpiller(LiveIntervals *lis, const MachineLoopInfo *loopInfo,
197 VirtRegMap *vrm)
Lang Hames835ca072009-11-19 04:15:33 +0000198 : lis(lis), loopInfo(loopInfo), vrm(vrm) {}
199
200 /// Falls back on LiveIntervals::addIntervalsForSpills.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000201 void spill(LiveInterval *li,
202 std::vector<LiveInterval*> &newIntervals,
203 SmallVectorImpl<LiveInterval*> &spillIs,
204 SlotIndex*) {
205 std::vector<LiveInterval*> added =
206 lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
207 newIntervals.insert(newIntervals.end(), added.begin(), added.end());
Lang Hames835ca072009-11-19 04:15:33 +0000208 }
Lang Hames835ca072009-11-19 04:15:33 +0000209};
210
Chris Lattner1ca65312010-04-07 22:44:07 +0000211} // end anonymous namespace
212
213namespace {
214
Lang Hames61945692009-12-09 05:39:12 +0000215/// When a call to spill is placed this spiller will first try to break the
216/// interval up into its component values (one new interval per value).
217/// If this fails, or if a call is placed to spill a previously split interval
218/// then the spiller falls back on the standard spilling mechanism.
219class SplittingSpiller : public StandardSpiller {
220public:
221 SplittingSpiller(MachineFunction *mf, LiveIntervals *lis,
222 const MachineLoopInfo *loopInfo, VirtRegMap *vrm)
223 : StandardSpiller(lis, loopInfo, vrm) {
224
225 mri = &mf->getRegInfo();
226 tii = mf->getTarget().getInstrInfo();
227 tri = mf->getTarget().getRegisterInfo();
228 }
229
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000230 void spill(LiveInterval *li,
231 std::vector<LiveInterval*> &newIntervals,
232 SmallVectorImpl<LiveInterval*> &spillIs,
233 SlotIndex *earliestStart) {
234 if (worthTryingToSplit(li))
235 tryVNISplit(li, earliestStart);
236 else
237 StandardSpiller::spill(li, newIntervals, spillIs, earliestStart);
Lang Hames61945692009-12-09 05:39:12 +0000238 }
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
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000266 // Skip unused VNIs.
267 if (vni->isUnused())
Lang Hames61945692009-12-09 05:39:12 +0000268 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());
Lang Hames61945692009-12-09 05:39:12 +0000305
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000306 // Create a new vreg and live interval, copy VNI ranges over.
Lang Hames61945692009-12-09 05:39:12 +0000307 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
308 unsigned newVReg = mri->createVirtualRegister(trc);
309 vrm->grow();
310 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
311 VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator());
312
313 // Start by copying all live ranges in the VN to the new interval.
314 for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end();
315 rItr != rEnd; ++rItr) {
316 if (rItr->valno == vni) {
317 newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI));
318 }
319 }
320
321 // Erase the old VNI & ranges.
322 li->removeValNo(vni);
323
324 // Collect all current uses of the register belonging to the given VNI.
325 // We'll use this to rename the register after we've dealt with the def.
326 std::set<MachineInstr*> uses;
327 for (MachineRegisterInfo::use_iterator
328 useItr = mri->use_begin(li->reg), useEnd = mri->use_end();
329 useItr != useEnd; ++useItr) {
330 uses.insert(&*useItr);
331 }
332
333 // Process the def instruction for this VNI.
334 if (newVNI->isPHIDef()) {
335 // Insert a copy at the start of the MBB. The range proceeding the
336 // copy will be attached to the original LiveInterval.
337 MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def);
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000338 tii->copyRegToReg(*defMBB, defMBB->begin(), newVReg, li->reg, trc, trc,
339 DebugLoc());
Lang Hames61945692009-12-09 05:39:12 +0000340 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);
Lang Hames61945692009-12-09 05:39:12 +0000346 li->addRange(LiveRange(phiDefVNI->def, copyIdx.getDefIndex(), phiDefVNI));
347 LiveRange *oldPHIDefRange =
348 newLI->getLiveRangeContaining(lis->getMBBStartIdx(defMBB));
349
350 // If the old phi def starts in the middle of the range chop it up.
351 if (oldPHIDefRange->start < lis->getMBBStartIdx(defMBB)) {
352 LiveRange oldPHIDefRange2(copyIdx.getDefIndex(), oldPHIDefRange->end,
353 oldPHIDefRange->valno);
354 oldPHIDefRange->end = lis->getMBBStartIdx(defMBB);
355 newLI->addRange(oldPHIDefRange2);
356 } else if (oldPHIDefRange->start == lis->getMBBStartIdx(defMBB)) {
357 // Otherwise if it's at the start of the range just trim it.
358 oldPHIDefRange->start = copyIdx.getDefIndex();
359 } else {
360 assert(false && "PHI def range doesn't cover PHI def?");
361 }
362
363 newVNI->def = copyIdx.getDefIndex();
364 newVNI->setCopy(copyMI);
365 newVNI->setIsPHIDef(false); // not a PHI def anymore.
366 newVNI->setIsDefAccurate(true);
367 } else {
368 // non-PHI def. Rename the def. If it's two-addr that means renaming the use
369 // and inserting a new copy too.
370 MachineInstr *defInst = lis->getInstructionFromIndex(newVNI->def);
371 // We'll rename this now, so we can remove it from uses.
372 uses.erase(defInst);
373 unsigned defOpIdx = defInst->findRegisterDefOperandIdx(li->reg);
374 bool isTwoAddr = defInst->isRegTiedToUseOperand(defOpIdx),
375 twoAddrUseIsUndef = false;
376
377 for (unsigned i = 0; i < defInst->getNumOperands(); ++i) {
378 MachineOperand &mo = defInst->getOperand(i);
379 if (mo.isReg() && (mo.isDef() || isTwoAddr) && (mo.getReg()==li->reg)) {
380 mo.setReg(newVReg);
381 if (isTwoAddr && mo.isUse() && mo.isUndef())
382 twoAddrUseIsUndef = true;
383 }
384 }
385
386 SlotIndex defIdx = lis->getInstructionIndex(defInst);
387 newVNI->def = defIdx.getDefIndex();
388
389 if (isTwoAddr && !twoAddrUseIsUndef) {
390 MachineBasicBlock *defMBB = defInst->getParent();
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000391 tii->copyRegToReg(*defMBB, defInst, newVReg, li->reg, trc, trc,
392 DebugLoc());
Lang Hames61945692009-12-09 05:39:12 +0000393 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());
Lang Hames61945692009-12-09 05:39:12 +0000398 origUseRange->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000399 VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI,
400 true, lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000401 LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI);
402 newLI->addRange(copyRange);
403 }
404 }
405
406 for (std::set<MachineInstr*>::iterator
407 usesItr = uses.begin(), usesEnd = uses.end();
408 usesItr != usesEnd; ++usesItr) {
409 MachineInstr *useInst = *usesItr;
410 SlotIndex useIdx = lis->getInstructionIndex(useInst);
411 LiveRange *useRange =
412 newLI->getLiveRangeContaining(useIdx.getUseIndex());
413
414 // If this use doesn't belong to the new interval skip it.
415 if (useRange == 0)
416 continue;
417
418 // This use doesn't belong to the VNI, skip it.
419 if (useRange->valno != newVNI)
420 continue;
421
422 // Check if this instr is two address.
423 unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg);
424 bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx);
425
426 // Rename uses (and defs for two-address instrs).
427 for (unsigned i = 0; i < useInst->getNumOperands(); ++i) {
428 MachineOperand &mo = useInst->getOperand(i);
429 if (mo.isReg() && (mo.isUse() || isTwoAddress) &&
430 (mo.getReg() == li->reg)) {
431 mo.setReg(newVReg);
432 }
433 }
434
435 // If this is a two address instruction we've got some extra work to do.
436 if (isTwoAddress) {
437 // We modified the def operand, so we need to copy back to the original
438 // reg.
439 MachineBasicBlock *useMBB = useInst->getParent();
440 MachineBasicBlock::iterator useItr(useInst);
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000441 tii->copyRegToReg(*useMBB, llvm::next(useItr), li->reg, newVReg, trc, trc,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000442 DebugLoc());
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000443 MachineInstr *copyMI = llvm::next(useItr);
Lang Hames61945692009-12-09 05:39:12 +0000444 copyMI->addRegisterKilled(newVReg, tri);
445 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
446
447 // Change the old two-address defined range & vni to start at
448 // (and be defined by) the copy.
449 LiveRange *origDefRange =
450 li->getLiveRangeContaining(useIdx.getDefIndex());
451 origDefRange->start = copyIdx.getDefIndex();
452 origDefRange->valno->def = copyIdx.getDefIndex();
453 origDefRange->valno->setCopy(copyMI);
454
455 // Insert a new range & vni for the two-address-to-copy value. This
456 // will be attached to the new live interval.
457 VNInfo *copyVNI =
458 newLI->getNextValue(useIdx.getDefIndex(), 0, true,
459 lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000460 LiveRange copyRange(useIdx.getDefIndex(),copyIdx.getDefIndex(),copyVNI);
461 newLI->addRange(copyRange);
462 }
463 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000464
Lang Hames61945692009-12-09 05:39:12 +0000465 // Iterate over any PHI kills - we'll need to insert new copies for them.
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000466 for (LiveInterval::iterator LRI = newLI->begin(), LRE = newLI->end();
467 LRI != LRE; ++LRI) {
468 if (LRI->valno != newVNI || LRI->end.isPHI())
469 continue;
470 SlotIndex killIdx = LRI->end;
471 MachineBasicBlock *killMBB = lis->getMBBFromIndex(killIdx);
Lang Hames61945692009-12-09 05:39:12 +0000472
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000473 tii->copyRegToReg(*killMBB, killMBB->getFirstTerminator(),
474 li->reg, newVReg, trc, trc,
475 DebugLoc());
476 MachineInstr *copyMI = prior(killMBB->getFirstTerminator());
477 copyMI->addRegisterKilled(newVReg, tri);
478 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000479
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000480 // Save the current end. We may need it to add a new range if the
481 // current range runs of the end of the MBB.
482 SlotIndex newKillRangeEnd = LRI->end;
483 LRI->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000484
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000485 if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) {
486 assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) &&
487 "PHI kill range doesn't reach kill-block end. Not sane.");
488 newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB),
489 newKillRangeEnd, newVNI));
Lang Hames61945692009-12-09 05:39:12 +0000490 }
491
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000492 VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(),
493 copyMI, true,
494 lis->getVNInfoAllocator());
495 newKillVNI->setHasPHIKill(true);
496 li->addRange(LiveRange(copyIdx.getDefIndex(),
497 lis->getMBBEndIdx(killMBB),
498 newKillVNI));
Lang Hames61945692009-12-09 05:39:12 +0000499 }
Lang Hames61945692009-12-09 05:39:12 +0000500 newVNI->setHasPHIKill(false);
501
502 return newLI;
503 }
504
505};
506
Chris Lattner1ca65312010-04-07 22:44:07 +0000507} // end anonymous namespace
508
Lang Hamese2b201b2009-05-18 19:03:16 +0000509
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000510namespace llvm {
511Spiller *createInlineSpiller(MachineFunction*,
512 LiveIntervals*,
513 const MachineLoopInfo*,
514 VirtRegMap*);
515}
516
Lang Hamese2b201b2009-05-18 19:03:16 +0000517llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hames835ca072009-11-19 04:15:33 +0000518 const MachineLoopInfo *loopInfo,
519 VirtRegMap *vrm) {
520 switch (spillerOpt) {
Chris Lattner1ca65312010-04-07 22:44:07 +0000521 default: assert(0 && "unknown spiller");
522 case trivial: return new TrivialSpiller(mf, lis, vrm);
523 case standard: return new StandardSpiller(lis, loopInfo, vrm);
524 case splitting: return new SplittingSpiller(mf, lis, loopInfo, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000525 case inline_: return createInlineSpiller(mf, lis, loopInfo, vrm);
Lang Hames835ca072009-11-19 04:15:33 +0000526 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000527}