blob: 49e2a1fdd972e1425c8b8c682e06210770714017 [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 Olesend5bd68e2010-06-30 00:24:51 +000039 clEnumValN(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;
Eric Christopher894339e2010-07-06 18:35:20 +000058
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));
Eric Christopher894339e2010-07-06 18:35:20 +0000100
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;
Eric Christopher894339e2010-07-06 18:35:20 +0000120
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();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000140 vrm->addSpillSlotUse(ss, loadInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000141 SlotIndex endIndex = loadIndex.getNextIndex();
142 VNInfo *loadVNI =
143 newLI->getNextValue(loadIndex, 0, true, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000144 newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000145 }
146
Lang Hames38283e22009-11-18 20:31:20 +0000147 // Insert store if necessary.
Lang Hamese2b201b2009-05-18 19:03:16 +0000148 if (hasDef) {
Evan Chenge9b3ac22010-05-06 16:33:12 +0000149 tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg,
Evan Cheng746ad692010-05-06 19:06:44 +0000150 true, ss, trc, tri);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000151 MachineInstr *storeInstr(llvm::next(miItr));
Lang Hames38283e22009-11-18 20:31:20 +0000152 SlotIndex storeIndex =
153 lis->InsertMachineInstrInMaps(storeInstr).getDefIndex();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000154 vrm->addSpillSlotUse(ss, storeInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000155 SlotIndex beginIndex = storeIndex.getPrevIndex();
156 VNInfo *storeVNI =
157 newLI->getNextValue(beginIndex, 0, true, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000158 newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000159 }
160
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000161 newIntervals.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000162 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000163 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000164};
Lang Hamese2b201b2009-05-18 19:03:16 +0000165
Chris Lattner1ca65312010-04-07 22:44:07 +0000166} // end anonymous namespace
167
168namespace {
Lang Hamese2b201b2009-05-18 19:03:16 +0000169
Lang Hamesf41538d2009-06-02 16:53:25 +0000170/// Spills any live range using the spill-everywhere method with no attempt at
171/// folding.
172class TrivialSpiller : public SpillerBase {
173public:
Lang Hames10382fb2009-06-19 02:17:53 +0000174
Lang Hames8783e402009-11-20 00:53:30 +0000175 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
176 : SpillerBase(mf, lis, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000177
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000178 void spill(LiveInterval *li,
179 std::vector<LiveInterval*> &newIntervals,
180 SmallVectorImpl<LiveInterval*> &,
181 SlotIndex*) {
Lang Hames835ca072009-11-19 04:15:33 +0000182 // Ignore spillIs - we don't use it.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000183 trivialSpillEverywhere(li, newIntervals);
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.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000203 void spill(LiveInterval *li,
204 std::vector<LiveInterval*> &newIntervals,
205 SmallVectorImpl<LiveInterval*> &spillIs,
206 SlotIndex*) {
207 std::vector<LiveInterval*> added =
208 lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
209 newIntervals.insert(newIntervals.end(), added.begin(), added.end());
Lang Hames835ca072009-11-19 04:15:33 +0000210 }
Lang Hames835ca072009-11-19 04:15:33 +0000211};
212
Chris Lattner1ca65312010-04-07 22:44:07 +0000213} // end anonymous namespace
214
215namespace {
216
Lang Hames61945692009-12-09 05:39:12 +0000217/// When a call to spill is placed this spiller will first try to break the
218/// interval up into its component values (one new interval per value).
219/// If this fails, or if a call is placed to spill a previously split interval
Eric Christopher894339e2010-07-06 18:35:20 +0000220/// then the spiller falls back on the standard spilling mechanism.
Lang Hames61945692009-12-09 05:39:12 +0000221class SplittingSpiller : public StandardSpiller {
222public:
223 SplittingSpiller(MachineFunction *mf, LiveIntervals *lis,
224 const MachineLoopInfo *loopInfo, VirtRegMap *vrm)
225 : StandardSpiller(lis, loopInfo, vrm) {
226
227 mri = &mf->getRegInfo();
228 tii = mf->getTarget().getInstrInfo();
229 tri = mf->getTarget().getRegisterInfo();
230 }
231
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000232 void spill(LiveInterval *li,
233 std::vector<LiveInterval*> &newIntervals,
234 SmallVectorImpl<LiveInterval*> &spillIs,
235 SlotIndex *earliestStart) {
236 if (worthTryingToSplit(li))
237 tryVNISplit(li, earliestStart);
238 else
239 StandardSpiller::spill(li, newIntervals, spillIs, earliestStart);
Lang Hames61945692009-12-09 05:39:12 +0000240 }
241
242private:
243
244 MachineRegisterInfo *mri;
245 const TargetInstrInfo *tii;
Eric Christopher894339e2010-07-06 18:35:20 +0000246 const TargetRegisterInfo *tri;
Lang Hames61945692009-12-09 05:39:12 +0000247 DenseSet<LiveInterval*> alreadySplit;
248
249 bool worthTryingToSplit(LiveInterval *li) const {
250 return (!alreadySplit.count(li) && li->getNumValNums() > 1);
251 }
252
253 /// Try to break a LiveInterval into its component values.
254 std::vector<LiveInterval*> tryVNISplit(LiveInterval *li,
255 SlotIndex *earliestStart) {
256
David Greene65de5042010-01-05 01:25:55 +0000257 DEBUG(dbgs() << "Trying VNI split of %reg" << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000258
259 std::vector<LiveInterval*> added;
260 SmallVector<VNInfo*, 4> vnis;
261
262 std::copy(li->vni_begin(), li->vni_end(), std::back_inserter(vnis));
Eric Christopher894339e2010-07-06 18:35:20 +0000263
Lang Hames61945692009-12-09 05:39:12 +0000264 for (SmallVectorImpl<VNInfo*>::iterator vniItr = vnis.begin(),
265 vniEnd = vnis.end(); vniItr != vniEnd; ++vniItr) {
266 VNInfo *vni = *vniItr;
Eric Christopher894339e2010-07-06 18:35:20 +0000267
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000268 // Skip unused VNIs.
269 if (vni->isUnused())
Lang Hames61945692009-12-09 05:39:12 +0000270 continue;
271
David Greene65de5042010-01-05 01:25:55 +0000272 DEBUG(dbgs() << " Extracted Val #" << vni->id << " as ");
Lang Hames61945692009-12-09 05:39:12 +0000273 LiveInterval *splitInterval = extractVNI(li, vni);
Eric Christopher894339e2010-07-06 18:35:20 +0000274
Lang Hames61945692009-12-09 05:39:12 +0000275 if (splitInterval != 0) {
David Greene65de5042010-01-05 01:25:55 +0000276 DEBUG(dbgs() << *splitInterval << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000277 added.push_back(splitInterval);
278 alreadySplit.insert(splitInterval);
279 if (earliestStart != 0) {
280 if (splitInterval->beginIndex() < *earliestStart)
281 *earliestStart = splitInterval->beginIndex();
282 }
283 } else {
David Greene65de5042010-01-05 01:25:55 +0000284 DEBUG(dbgs() << "0\n");
Lang Hames61945692009-12-09 05:39:12 +0000285 }
Eric Christopher894339e2010-07-06 18:35:20 +0000286 }
Lang Hames61945692009-12-09 05:39:12 +0000287
David Greene65de5042010-01-05 01:25:55 +0000288 DEBUG(dbgs() << "Original LI: " << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000289
290 // If there original interval still contains some live ranges
Eric Christopher894339e2010-07-06 18:35:20 +0000291 // add it to added and alreadySplit.
Lang Hames61945692009-12-09 05:39:12 +0000292 if (!li->empty()) {
293 added.push_back(li);
294 alreadySplit.insert(li);
295 if (earliestStart != 0) {
296 if (li->beginIndex() < *earliestStart)
297 *earliestStart = li->beginIndex();
298 }
299 }
300
301 return added;
302 }
303
304 /// Extract the given value number from the interval.
305 LiveInterval* extractVNI(LiveInterval *li, VNInfo *vni) const {
306 assert(vni->isDefAccurate() || vni->isPHIDef());
Lang Hames61945692009-12-09 05:39:12 +0000307
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000308 // Create a new vreg and live interval, copy VNI ranges over.
Lang Hames61945692009-12-09 05:39:12 +0000309 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
310 unsigned newVReg = mri->createVirtualRegister(trc);
311 vrm->grow();
312 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
313 VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator());
314
Eric Christopher894339e2010-07-06 18:35:20 +0000315 // Start by copying all live ranges in the VN to the new interval.
Lang Hames61945692009-12-09 05:39:12 +0000316 for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end();
317 rItr != rEnd; ++rItr) {
318 if (rItr->valno == vni) {
319 newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI));
320 }
321 }
322
Eric Christopher894339e2010-07-06 18:35:20 +0000323 // Erase the old VNI & ranges.
Lang Hames61945692009-12-09 05:39:12 +0000324 li->removeValNo(vni);
325
326 // Collect all current uses of the register belonging to the given VNI.
327 // We'll use this to rename the register after we've dealt with the def.
328 std::set<MachineInstr*> uses;
329 for (MachineRegisterInfo::use_iterator
330 useItr = mri->use_begin(li->reg), useEnd = mri->use_end();
331 useItr != useEnd; ++useItr) {
332 uses.insert(&*useItr);
333 }
334
335 // Process the def instruction for this VNI.
336 if (newVNI->isPHIDef()) {
337 // Insert a copy at the start of the MBB. The range proceeding the
338 // copy will be attached to the original LiveInterval.
339 MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def);
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000340 tii->copyRegToReg(*defMBB, defMBB->begin(), newVReg, li->reg, trc, trc,
341 DebugLoc());
Lang Hames61945692009-12-09 05:39:12 +0000342 MachineInstr *copyMI = defMBB->begin();
343 copyMI->addRegisterKilled(li->reg, tri);
344 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
345 VNInfo *phiDefVNI = li->getNextValue(lis->getMBBStartIdx(defMBB),
346 0, false, lis->getVNInfoAllocator());
347 phiDefVNI->setIsPHIDef(true);
Lang Hames61945692009-12-09 05:39:12 +0000348 li->addRange(LiveRange(phiDefVNI->def, copyIdx.getDefIndex(), phiDefVNI));
349 LiveRange *oldPHIDefRange =
350 newLI->getLiveRangeContaining(lis->getMBBStartIdx(defMBB));
351
352 // If the old phi def starts in the middle of the range chop it up.
353 if (oldPHIDefRange->start < lis->getMBBStartIdx(defMBB)) {
354 LiveRange oldPHIDefRange2(copyIdx.getDefIndex(), oldPHIDefRange->end,
355 oldPHIDefRange->valno);
356 oldPHIDefRange->end = lis->getMBBStartIdx(defMBB);
357 newLI->addRange(oldPHIDefRange2);
358 } else if (oldPHIDefRange->start == lis->getMBBStartIdx(defMBB)) {
359 // Otherwise if it's at the start of the range just trim it.
360 oldPHIDefRange->start = copyIdx.getDefIndex();
361 } else {
362 assert(false && "PHI def range doesn't cover PHI def?");
363 }
364
365 newVNI->def = copyIdx.getDefIndex();
366 newVNI->setCopy(copyMI);
367 newVNI->setIsPHIDef(false); // not a PHI def anymore.
368 newVNI->setIsDefAccurate(true);
369 } else {
Eric Christopher894339e2010-07-06 18:35:20 +0000370 // non-PHI def. Rename the def. If it's two-addr that means renaming the
371 // use and inserting a new copy too.
Lang Hames61945692009-12-09 05:39:12 +0000372 MachineInstr *defInst = lis->getInstructionFromIndex(newVNI->def);
373 // We'll rename this now, so we can remove it from uses.
374 uses.erase(defInst);
375 unsigned defOpIdx = defInst->findRegisterDefOperandIdx(li->reg);
376 bool isTwoAddr = defInst->isRegTiedToUseOperand(defOpIdx),
377 twoAddrUseIsUndef = false;
378
379 for (unsigned i = 0; i < defInst->getNumOperands(); ++i) {
380 MachineOperand &mo = defInst->getOperand(i);
381 if (mo.isReg() && (mo.isDef() || isTwoAddr) && (mo.getReg()==li->reg)) {
382 mo.setReg(newVReg);
383 if (isTwoAddr && mo.isUse() && mo.isUndef())
384 twoAddrUseIsUndef = true;
385 }
386 }
Eric Christopher894339e2010-07-06 18:35:20 +0000387
Lang Hames61945692009-12-09 05:39:12 +0000388 SlotIndex defIdx = lis->getInstructionIndex(defInst);
389 newVNI->def = defIdx.getDefIndex();
390
391 if (isTwoAddr && !twoAddrUseIsUndef) {
392 MachineBasicBlock *defMBB = defInst->getParent();
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000393 tii->copyRegToReg(*defMBB, defInst, newVReg, li->reg, trc, trc,
394 DebugLoc());
Lang Hames61945692009-12-09 05:39:12 +0000395 MachineInstr *copyMI = prior(MachineBasicBlock::iterator(defInst));
396 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
397 copyMI->addRegisterKilled(li->reg, tri);
398 LiveRange *origUseRange =
399 li->getLiveRangeContaining(newVNI->def.getUseIndex());
Lang Hames61945692009-12-09 05:39:12 +0000400 origUseRange->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000401 VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI,
402 true, lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000403 LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI);
404 newLI->addRange(copyRange);
Eric Christopher894339e2010-07-06 18:35:20 +0000405 }
Lang Hames61945692009-12-09 05:39:12 +0000406 }
Eric Christopher894339e2010-07-06 18:35:20 +0000407
Lang Hames61945692009-12-09 05:39:12 +0000408 for (std::set<MachineInstr*>::iterator
409 usesItr = uses.begin(), usesEnd = uses.end();
410 usesItr != usesEnd; ++usesItr) {
411 MachineInstr *useInst = *usesItr;
412 SlotIndex useIdx = lis->getInstructionIndex(useInst);
413 LiveRange *useRange =
414 newLI->getLiveRangeContaining(useIdx.getUseIndex());
415
416 // If this use doesn't belong to the new interval skip it.
417 if (useRange == 0)
418 continue;
419
420 // This use doesn't belong to the VNI, skip it.
421 if (useRange->valno != newVNI)
422 continue;
423
424 // Check if this instr is two address.
425 unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg);
426 bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx);
Eric Christopher894339e2010-07-06 18:35:20 +0000427
Lang Hames61945692009-12-09 05:39:12 +0000428 // Rename uses (and defs for two-address instrs).
429 for (unsigned i = 0; i < useInst->getNumOperands(); ++i) {
430 MachineOperand &mo = useInst->getOperand(i);
431 if (mo.isReg() && (mo.isUse() || isTwoAddress) &&
432 (mo.getReg() == li->reg)) {
433 mo.setReg(newVReg);
434 }
435 }
436
437 // If this is a two address instruction we've got some extra work to do.
438 if (isTwoAddress) {
439 // We modified the def operand, so we need to copy back to the original
440 // reg.
441 MachineBasicBlock *useMBB = useInst->getParent();
442 MachineBasicBlock::iterator useItr(useInst);
Eric Christopher894339e2010-07-06 18:35:20 +0000443 tii->copyRegToReg(*useMBB, llvm::next(useItr), li->reg, newVReg, trc,
444 trc, DebugLoc());
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000445 MachineInstr *copyMI = llvm::next(useItr);
Lang Hames61945692009-12-09 05:39:12 +0000446 copyMI->addRegisterKilled(newVReg, tri);
447 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
448
449 // Change the old two-address defined range & vni to start at
450 // (and be defined by) the copy.
451 LiveRange *origDefRange =
452 li->getLiveRangeContaining(useIdx.getDefIndex());
453 origDefRange->start = copyIdx.getDefIndex();
454 origDefRange->valno->def = copyIdx.getDefIndex();
455 origDefRange->valno->setCopy(copyMI);
456
457 // Insert a new range & vni for the two-address-to-copy value. This
458 // will be attached to the new live interval.
459 VNInfo *copyVNI =
460 newLI->getNextValue(useIdx.getDefIndex(), 0, true,
461 lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000462 LiveRange copyRange(useIdx.getDefIndex(),copyIdx.getDefIndex(),copyVNI);
463 newLI->addRange(copyRange);
464 }
465 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000466
Lang Hames61945692009-12-09 05:39:12 +0000467 // Iterate over any PHI kills - we'll need to insert new copies for them.
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000468 for (LiveInterval::iterator LRI = newLI->begin(), LRE = newLI->end();
469 LRI != LRE; ++LRI) {
470 if (LRI->valno != newVNI || LRI->end.isPHI())
471 continue;
472 SlotIndex killIdx = LRI->end;
473 MachineBasicBlock *killMBB = lis->getMBBFromIndex(killIdx);
Lang Hames61945692009-12-09 05:39:12 +0000474
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000475 tii->copyRegToReg(*killMBB, killMBB->getFirstTerminator(),
476 li->reg, newVReg, trc, trc,
477 DebugLoc());
478 MachineInstr *copyMI = prior(killMBB->getFirstTerminator());
479 copyMI->addRegisterKilled(newVReg, tri);
480 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000481
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000482 // Save the current end. We may need it to add a new range if the
483 // current range runs of the end of the MBB.
484 SlotIndex newKillRangeEnd = LRI->end;
485 LRI->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000486
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000487 if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) {
488 assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) &&
489 "PHI kill range doesn't reach kill-block end. Not sane.");
490 newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB),
491 newKillRangeEnd, newVNI));
Lang Hames61945692009-12-09 05:39:12 +0000492 }
493
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000494 VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(),
495 copyMI, true,
496 lis->getVNInfoAllocator());
497 newKillVNI->setHasPHIKill(true);
498 li->addRange(LiveRange(copyIdx.getDefIndex(),
499 lis->getMBBEndIdx(killMBB),
500 newKillVNI));
Lang Hames61945692009-12-09 05:39:12 +0000501 }
Lang Hames61945692009-12-09 05:39:12 +0000502 newVNI->setHasPHIKill(false);
503
504 return newLI;
505 }
506
507};
508
Chris Lattner1ca65312010-04-07 22:44:07 +0000509} // end anonymous namespace
510
Lang Hamese2b201b2009-05-18 19:03:16 +0000511
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000512namespace llvm {
513Spiller *createInlineSpiller(MachineFunction*,
514 LiveIntervals*,
515 const MachineLoopInfo*,
516 VirtRegMap*);
517}
518
Lang Hamese2b201b2009-05-18 19:03:16 +0000519llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hames835ca072009-11-19 04:15:33 +0000520 const MachineLoopInfo *loopInfo,
521 VirtRegMap *vrm) {
522 switch (spillerOpt) {
Chris Lattner1ca65312010-04-07 22:44:07 +0000523 default: assert(0 && "unknown spiller");
524 case trivial: return new TrivialSpiller(mf, lis, vrm);
525 case standard: return new StandardSpiller(lis, loopInfo, vrm);
526 case splitting: return new SplittingSpiller(mf, lis, loopInfo, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000527 case inline_: return createInlineSpiller(mf, lis, loopInfo, vrm);
Lang Hames835ca072009-11-19 04:15:33 +0000528 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000529}