blob: 56bcb2824ae8afb442945e556e19f1919c8d2eab [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"
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +000017#include "llvm/CodeGen/MachineInstrBuilder.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
Lang Hames835ca072009-11-19 04:15:33 +000021#include "llvm/Support/CommandLine.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000022#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000023#include "llvm/Support/ErrorHandling.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000024#include "llvm/Support/raw_ostream.h"
Lang Hames61945692009-12-09 05:39:12 +000025#include <set>
Lang Hamese2b201b2009-05-18 19:03:16 +000026
Lang Hamese2b201b2009-05-18 19:03:16 +000027using namespace llvm;
28
Lang Hames835ca072009-11-19 04:15:33 +000029namespace {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000030 enum SpillerName { trivial, standard, splitting, inline_ };
Lang Hames835ca072009-11-19 04:15:33 +000031}
32
33static cl::opt<SpillerName>
34spillerOpt("spiller",
35 cl::desc("Spiller to use: (default: standard)"),
36 cl::Prefix,
Lang Hames61945692009-12-09 05:39:12 +000037 cl::values(clEnumVal(trivial, "trivial spiller"),
38 clEnumVal(standard, "default spiller"),
39 clEnumVal(splitting, "splitting spiller"),
Jakob Stoklund Olesend5bd68e2010-06-30 00:24:51 +000040 clEnumValN(inline_, "inline", "inline spiller"),
Lang Hames835ca072009-11-19 04:15:33 +000041 clEnumValEnd),
42 cl::init(standard));
43
Lang Hames61945692009-12-09 05:39:12 +000044// Spiller virtual destructor implementation.
Lang Hamese2b201b2009-05-18 19:03:16 +000045Spiller::~Spiller() {}
46
47namespace {
48
Lang Hamesf41538d2009-06-02 16:53:25 +000049/// Utility class for spillers.
50class SpillerBase : public Spiller {
51protected:
Lang Hamesf41538d2009-06-02 16:53:25 +000052 MachineFunction *mf;
53 LiveIntervals *lis;
Lang Hamesf41538d2009-06-02 16:53:25 +000054 MachineFrameInfo *mfi;
55 MachineRegisterInfo *mri;
56 const TargetInstrInfo *tii;
Evan Cheng746ad692010-05-06 19:06:44 +000057 const TargetRegisterInfo *tri;
Lang Hamesf41538d2009-06-02 16:53:25 +000058 VirtRegMap *vrm;
Eric Christopher894339e2010-07-06 18:35:20 +000059
60 /// Construct a spiller base.
Lang Hames8783e402009-11-20 00:53:30 +000061 SpillerBase(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm)
62 : mf(mf), lis(lis), vrm(vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000063 {
64 mfi = mf->getFrameInfo();
65 mri = &mf->getRegInfo();
66 tii = mf->getTarget().getInstrInfo();
Evan Cheng746ad692010-05-06 19:06:44 +000067 tri = mf->getTarget().getRegisterInfo();
Lang Hamese2b201b2009-05-18 19:03:16 +000068 }
69
Lang Hamesf41538d2009-06-02 16:53:25 +000070 /// Add spill ranges for every use/def of the live interval, inserting loads
Lang Hames38283e22009-11-18 20:31:20 +000071 /// immediately before each use, and stores after each def. No folding or
72 /// remat is attempted.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +000073 void trivialSpillEverywhere(LiveInterval *li,
74 std::vector<LiveInterval*> &newIntervals) {
David Greene65de5042010-01-05 01:25:55 +000075 DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
Lang Hamese2b201b2009-05-18 19:03:16 +000076
77 assert(li->weight != HUGE_VALF &&
78 "Attempting to spill already spilled value.");
79
80 assert(!li->isStackSlot() &&
81 "Trying to spill a stack slot.");
82
David Greene65de5042010-01-05 01:25:55 +000083 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
Lang Hames6bbc73d2009-06-24 20:46:24 +000084
Lang Hamese2b201b2009-05-18 19:03:16 +000085 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +000086 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
87
Lang Hames38283e22009-11-18 20:31:20 +000088 // Iterate over reg uses/defs.
Lang Hamesf41538d2009-06-02 16:53:25 +000089 for (MachineRegisterInfo::reg_iterator
90 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +000091
Lang Hames38283e22009-11-18 20:31:20 +000092 // Grab the use/def instr.
Lang Hamese2b201b2009-05-18 19:03:16 +000093 MachineInstr *mi = &*regItr;
Lang Hames6bbc73d2009-06-24 20:46:24 +000094
David Greene65de5042010-01-05 01:25:55 +000095 DEBUG(dbgs() << " Processing " << *mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +000096
Lang Hames38283e22009-11-18 20:31:20 +000097 // Step regItr to the next use/def instr.
Lang Hamesf41538d2009-06-02 16:53:25 +000098 do {
99 ++regItr;
100 } while (regItr != mri->reg_end() && (&*regItr == mi));
Eric Christopher894339e2010-07-06 18:35:20 +0000101
Lang Hames38283e22009-11-18 20:31:20 +0000102 // Collect uses & defs for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000103 SmallVector<unsigned, 2> indices;
104 bool hasUse = false;
105 bool hasDef = false;
Lang Hamese2b201b2009-05-18 19:03:16 +0000106 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
107 MachineOperand &op = mi->getOperand(i);
Lang Hamese2b201b2009-05-18 19:03:16 +0000108 if (!op.isReg() || op.getReg() != li->reg)
109 continue;
Lang Hamese2b201b2009-05-18 19:03:16 +0000110 hasUse |= mi->getOperand(i).isUse();
111 hasDef |= mi->getOperand(i).isDef();
Lang Hamese2b201b2009-05-18 19:03:16 +0000112 indices.push_back(i);
113 }
114
Lang Hames38283e22009-11-18 20:31:20 +0000115 // Create a new vreg & interval for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000116 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000117 vrm->grow();
118 vrm->assignVirt2StackSlot(newVReg, ss);
Lang Hamesf41538d2009-06-02 16:53:25 +0000119 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
120 newLI->weight = HUGE_VALF;
Eric Christopher894339e2010-07-06 18:35:20 +0000121
Lang Hames38283e22009-11-18 20:31:20 +0000122 // Update the reg operands & kill flags.
Lang Hamese2b201b2009-05-18 19:03:16 +0000123 for (unsigned i = 0; i < indices.size(); ++i) {
Lang Hames38283e22009-11-18 20:31:20 +0000124 unsigned mopIdx = indices[i];
125 MachineOperand &mop = mi->getOperand(mopIdx);
126 mop.setReg(newVReg);
127 if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
128 mop.setIsKill(true);
Lang Hamese2b201b2009-05-18 19:03:16 +0000129 }
130 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000131 assert(hasUse || hasDef);
132
Lang Hames38283e22009-11-18 20:31:20 +0000133 // Insert reload if necessary.
134 MachineBasicBlock::iterator miItr(mi);
Lang Hamese2b201b2009-05-18 19:03:16 +0000135 if (hasUse) {
Evan Cheng746ad692010-05-06 19:06:44 +0000136 tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc,
137 tri);
Lang Hames38283e22009-11-18 20:31:20 +0000138 MachineInstr *loadInstr(prior(miItr));
139 SlotIndex loadIndex =
140 lis->InsertMachineInstrInMaps(loadInstr).getDefIndex();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000141 vrm->addSpillSlotUse(ss, loadInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000142 SlotIndex endIndex = loadIndex.getNextIndex();
143 VNInfo *loadVNI =
144 newLI->getNextValue(loadIndex, 0, true, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000145 newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000146 }
147
Lang Hames38283e22009-11-18 20:31:20 +0000148 // Insert store if necessary.
Lang Hamese2b201b2009-05-18 19:03:16 +0000149 if (hasDef) {
Evan Chenge9b3ac22010-05-06 16:33:12 +0000150 tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg,
Evan Cheng746ad692010-05-06 19:06:44 +0000151 true, ss, trc, tri);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000152 MachineInstr *storeInstr(llvm::next(miItr));
Lang Hames38283e22009-11-18 20:31:20 +0000153 SlotIndex storeIndex =
154 lis->InsertMachineInstrInMaps(storeInstr).getDefIndex();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000155 vrm->addSpillSlotUse(ss, storeInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000156 SlotIndex beginIndex = storeIndex.getPrevIndex();
157 VNInfo *storeVNI =
158 newLI->getNextValue(beginIndex, 0, true, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000159 newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000160 }
161
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000162 newIntervals.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000163 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000164 }
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
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000179 void spill(LiveInterval *li,
180 std::vector<LiveInterval*> &newIntervals,
181 SmallVectorImpl<LiveInterval*> &,
182 SlotIndex*) {
Lang Hames835ca072009-11-19 04:15:33 +0000183 // Ignore spillIs - we don't use it.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000184 trivialSpillEverywhere(li, newIntervals);
Lang Hamese2b201b2009-05-18 19:03:16 +0000185 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000186};
187
Chris Lattner1ca65312010-04-07 22:44:07 +0000188} // end anonymous namespace
189
190namespace {
191
Lang Hames835ca072009-11-19 04:15:33 +0000192/// Falls back on LiveIntervals::addIntervalsForSpills.
193class StandardSpiller : public Spiller {
Lang Hames61945692009-12-09 05:39:12 +0000194protected:
Lang Hames835ca072009-11-19 04:15:33 +0000195 LiveIntervals *lis;
196 const MachineLoopInfo *loopInfo;
197 VirtRegMap *vrm;
198public:
Lang Hames61945692009-12-09 05:39:12 +0000199 StandardSpiller(LiveIntervals *lis, const MachineLoopInfo *loopInfo,
200 VirtRegMap *vrm)
Lang Hames835ca072009-11-19 04:15:33 +0000201 : lis(lis), loopInfo(loopInfo), vrm(vrm) {}
202
203 /// Falls back on LiveIntervals::addIntervalsForSpills.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000204 void spill(LiveInterval *li,
205 std::vector<LiveInterval*> &newIntervals,
206 SmallVectorImpl<LiveInterval*> &spillIs,
207 SlotIndex*) {
208 std::vector<LiveInterval*> added =
209 lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
210 newIntervals.insert(newIntervals.end(), added.begin(), added.end());
Lang Hames835ca072009-11-19 04:15:33 +0000211 }
Lang Hames835ca072009-11-19 04:15:33 +0000212};
213
Chris Lattner1ca65312010-04-07 22:44:07 +0000214} // end anonymous namespace
215
216namespace {
217
Lang Hames61945692009-12-09 05:39:12 +0000218/// When a call to spill is placed this spiller will first try to break the
219/// interval up into its component values (one new interval per value).
220/// If this fails, or if a call is placed to spill a previously split interval
Eric Christopher894339e2010-07-06 18:35:20 +0000221/// then the spiller falls back on the standard spilling mechanism.
Lang Hames61945692009-12-09 05:39:12 +0000222class SplittingSpiller : public StandardSpiller {
223public:
224 SplittingSpiller(MachineFunction *mf, LiveIntervals *lis,
225 const MachineLoopInfo *loopInfo, VirtRegMap *vrm)
226 : StandardSpiller(lis, loopInfo, vrm) {
227
228 mri = &mf->getRegInfo();
229 tii = mf->getTarget().getInstrInfo();
230 tri = mf->getTarget().getRegisterInfo();
231 }
232
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000233 void spill(LiveInterval *li,
234 std::vector<LiveInterval*> &newIntervals,
235 SmallVectorImpl<LiveInterval*> &spillIs,
236 SlotIndex *earliestStart) {
237 if (worthTryingToSplit(li))
238 tryVNISplit(li, earliestStart);
239 else
240 StandardSpiller::spill(li, newIntervals, spillIs, earliestStart);
Lang Hames61945692009-12-09 05:39:12 +0000241 }
242
243private:
244
245 MachineRegisterInfo *mri;
246 const TargetInstrInfo *tii;
Eric Christopher894339e2010-07-06 18:35:20 +0000247 const TargetRegisterInfo *tri;
Lang Hames61945692009-12-09 05:39:12 +0000248 DenseSet<LiveInterval*> alreadySplit;
249
250 bool worthTryingToSplit(LiveInterval *li) const {
251 return (!alreadySplit.count(li) && li->getNumValNums() > 1);
252 }
253
254 /// Try to break a LiveInterval into its component values.
255 std::vector<LiveInterval*> tryVNISplit(LiveInterval *li,
256 SlotIndex *earliestStart) {
257
David Greene65de5042010-01-05 01:25:55 +0000258 DEBUG(dbgs() << "Trying VNI split of %reg" << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000259
260 std::vector<LiveInterval*> added;
261 SmallVector<VNInfo*, 4> vnis;
262
263 std::copy(li->vni_begin(), li->vni_end(), std::back_inserter(vnis));
Eric Christopher894339e2010-07-06 18:35:20 +0000264
Lang Hames61945692009-12-09 05:39:12 +0000265 for (SmallVectorImpl<VNInfo*>::iterator vniItr = vnis.begin(),
266 vniEnd = vnis.end(); vniItr != vniEnd; ++vniItr) {
267 VNInfo *vni = *vniItr;
Eric Christopher894339e2010-07-06 18:35:20 +0000268
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000269 // Skip unused VNIs.
270 if (vni->isUnused())
Lang Hames61945692009-12-09 05:39:12 +0000271 continue;
272
David Greene65de5042010-01-05 01:25:55 +0000273 DEBUG(dbgs() << " Extracted Val #" << vni->id << " as ");
Lang Hames61945692009-12-09 05:39:12 +0000274 LiveInterval *splitInterval = extractVNI(li, vni);
Eric Christopher894339e2010-07-06 18:35:20 +0000275
Lang Hames61945692009-12-09 05:39:12 +0000276 if (splitInterval != 0) {
David Greene65de5042010-01-05 01:25:55 +0000277 DEBUG(dbgs() << *splitInterval << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000278 added.push_back(splitInterval);
279 alreadySplit.insert(splitInterval);
280 if (earliestStart != 0) {
281 if (splitInterval->beginIndex() < *earliestStart)
282 *earliestStart = splitInterval->beginIndex();
283 }
284 } else {
David Greene65de5042010-01-05 01:25:55 +0000285 DEBUG(dbgs() << "0\n");
Lang Hames61945692009-12-09 05:39:12 +0000286 }
Eric Christopher894339e2010-07-06 18:35:20 +0000287 }
Lang Hames61945692009-12-09 05:39:12 +0000288
David Greene65de5042010-01-05 01:25:55 +0000289 DEBUG(dbgs() << "Original LI: " << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000290
291 // If there original interval still contains some live ranges
Eric Christopher894339e2010-07-06 18:35:20 +0000292 // add it to added and alreadySplit.
Lang Hames61945692009-12-09 05:39:12 +0000293 if (!li->empty()) {
294 added.push_back(li);
295 alreadySplit.insert(li);
296 if (earliestStart != 0) {
297 if (li->beginIndex() < *earliestStart)
298 *earliestStart = li->beginIndex();
299 }
300 }
301
302 return added;
303 }
304
305 /// Extract the given value number from the interval.
306 LiveInterval* extractVNI(LiveInterval *li, VNInfo *vni) const {
307 assert(vni->isDefAccurate() || vni->isPHIDef());
Lang Hames61945692009-12-09 05:39:12 +0000308
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000309 // Create a new vreg and live interval, copy VNI ranges over.
Lang Hames61945692009-12-09 05:39:12 +0000310 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
311 unsigned newVReg = mri->createVirtualRegister(trc);
312 vrm->grow();
313 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
314 VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator());
315
Eric Christopher894339e2010-07-06 18:35:20 +0000316 // Start by copying all live ranges in the VN to the new interval.
Lang Hames61945692009-12-09 05:39:12 +0000317 for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end();
318 rItr != rEnd; ++rItr) {
319 if (rItr->valno == vni) {
320 newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI));
321 }
322 }
323
Eric Christopher894339e2010-07-06 18:35:20 +0000324 // Erase the old VNI & ranges.
Lang Hames61945692009-12-09 05:39:12 +0000325 li->removeValNo(vni);
326
327 // Collect all current uses of the register belonging to the given VNI.
328 // We'll use this to rename the register after we've dealt with the def.
329 std::set<MachineInstr*> uses;
330 for (MachineRegisterInfo::use_iterator
331 useItr = mri->use_begin(li->reg), useEnd = mri->use_end();
332 useItr != useEnd; ++useItr) {
333 uses.insert(&*useItr);
334 }
335
336 // Process the def instruction for this VNI.
337 if (newVNI->isPHIDef()) {
338 // Insert a copy at the start of the MBB. The range proceeding the
339 // copy will be attached to the original LiveInterval.
340 MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000341 MachineInstr *copyMI = BuildMI(*defMBB, defMBB->begin(), DebugLoc(),
342 tii->get(TargetOpcode::COPY), newVReg)
343 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000344 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();
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000393 MachineInstr *copyMI = BuildMI(*defMBB, defInst, DebugLoc(),
394 tii->get(TargetOpcode::COPY), newVReg)
395 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000396 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000397 LiveRange *origUseRange =
398 li->getLiveRangeContaining(newVNI->def.getUseIndex());
Lang Hames61945692009-12-09 05:39:12 +0000399 origUseRange->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000400 VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI,
401 true, lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000402 LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI);
403 newLI->addRange(copyRange);
Eric Christopher894339e2010-07-06 18:35:20 +0000404 }
Lang Hames61945692009-12-09 05:39:12 +0000405 }
Eric Christopher894339e2010-07-06 18:35:20 +0000406
Lang Hames61945692009-12-09 05:39:12 +0000407 for (std::set<MachineInstr*>::iterator
408 usesItr = uses.begin(), usesEnd = uses.end();
409 usesItr != usesEnd; ++usesItr) {
410 MachineInstr *useInst = *usesItr;
411 SlotIndex useIdx = lis->getInstructionIndex(useInst);
412 LiveRange *useRange =
413 newLI->getLiveRangeContaining(useIdx.getUseIndex());
414
415 // If this use doesn't belong to the new interval skip it.
416 if (useRange == 0)
417 continue;
418
419 // This use doesn't belong to the VNI, skip it.
420 if (useRange->valno != newVNI)
421 continue;
422
423 // Check if this instr is two address.
424 unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg);
425 bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx);
Eric Christopher894339e2010-07-06 18:35:20 +0000426
Lang Hames61945692009-12-09 05:39:12 +0000427 // Rename uses (and defs for two-address instrs).
428 for (unsigned i = 0; i < useInst->getNumOperands(); ++i) {
429 MachineOperand &mo = useInst->getOperand(i);
430 if (mo.isReg() && (mo.isUse() || isTwoAddress) &&
431 (mo.getReg() == li->reg)) {
432 mo.setReg(newVReg);
433 }
434 }
435
436 // If this is a two address instruction we've got some extra work to do.
437 if (isTwoAddress) {
438 // We modified the def operand, so we need to copy back to the original
439 // reg.
440 MachineBasicBlock *useMBB = useInst->getParent();
441 MachineBasicBlock::iterator useItr(useInst);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000442 MachineInstr *copyMI = BuildMI(*useMBB, llvm::next(useItr), DebugLoc(),
443 tii->get(TargetOpcode::COPY), newVReg)
444 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000445 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);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000472 MachineInstr *copyMI = BuildMI(*killMBB, killMBB->getFirstTerminator(),
473 DebugLoc(), tii->get(TargetOpcode::COPY),
474 li->reg)
475 .addReg(newVReg, RegState::Kill);
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000476 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000477
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000478 // Save the current end. We may need it to add a new range if the
479 // current range runs of the end of the MBB.
480 SlotIndex newKillRangeEnd = LRI->end;
481 LRI->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000482
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000483 if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) {
484 assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) &&
485 "PHI kill range doesn't reach kill-block end. Not sane.");
486 newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB),
487 newKillRangeEnd, newVNI));
Lang Hames61945692009-12-09 05:39:12 +0000488 }
489
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000490 VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(),
491 copyMI, true,
492 lis->getVNInfoAllocator());
493 newKillVNI->setHasPHIKill(true);
494 li->addRange(LiveRange(copyIdx.getDefIndex(),
495 lis->getMBBEndIdx(killMBB),
496 newKillVNI));
Lang Hames61945692009-12-09 05:39:12 +0000497 }
Lang Hames61945692009-12-09 05:39:12 +0000498 newVNI->setHasPHIKill(false);
499
500 return newLI;
501 }
502
503};
504
Chris Lattner1ca65312010-04-07 22:44:07 +0000505} // end anonymous namespace
506
Lang Hamese2b201b2009-05-18 19:03:16 +0000507
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000508namespace llvm {
509Spiller *createInlineSpiller(MachineFunction*,
510 LiveIntervals*,
511 const MachineLoopInfo*,
512 VirtRegMap*);
513}
514
Lang Hamese2b201b2009-05-18 19:03:16 +0000515llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hames835ca072009-11-19 04:15:33 +0000516 const MachineLoopInfo *loopInfo,
517 VirtRegMap *vrm) {
518 switch (spillerOpt) {
Chris Lattner1ca65312010-04-07 22:44:07 +0000519 default: assert(0 && "unknown spiller");
520 case trivial: return new TrivialSpiller(mf, lis, vrm);
521 case standard: return new StandardSpiller(lis, loopInfo, vrm);
522 case splitting: return new SplittingSpiller(mf, lis, loopInfo, vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000523 case inline_: return createInlineSpiller(mf, lis, loopInfo, vrm);
Lang Hames835ca072009-11-19 04:15:33 +0000524 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000525}