blob: 23495eb103dfbab66e9134fe9f01400b707cc3c7 [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"
Jakob Stoklund Olesen2d172932010-10-26 00:11:33 +000015#include "llvm/CodeGen/LiveStackAnalysis.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000017#include "llvm/CodeGen/MachineFunction.h"
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000019#include "llvm/CodeGen/MachineLoopInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetInstrInfo.h"
Lang Hames835ca072009-11-19 04:15:33 +000023#include "llvm/Support/CommandLine.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000024#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000025#include "llvm/Support/ErrorHandling.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000026#include "llvm/Support/raw_ostream.h"
Lang Hames61945692009-12-09 05:39:12 +000027#include <set>
Lang Hamese2b201b2009-05-18 19:03:16 +000028
Lang Hamese2b201b2009-05-18 19:03:16 +000029using namespace llvm;
30
Lang Hames835ca072009-11-19 04:15:33 +000031namespace {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000032 enum SpillerName { trivial, standard, splitting, inline_ };
Lang Hames835ca072009-11-19 04:15:33 +000033}
34
35static cl::opt<SpillerName>
36spillerOpt("spiller",
37 cl::desc("Spiller to use: (default: standard)"),
38 cl::Prefix,
Lang Hames61945692009-12-09 05:39:12 +000039 cl::values(clEnumVal(trivial, "trivial spiller"),
40 clEnumVal(standard, "default spiller"),
41 clEnumVal(splitting, "splitting spiller"),
Jakob Stoklund Olesend5bd68e2010-06-30 00:24:51 +000042 clEnumValN(inline_, "inline", "inline spiller"),
Lang Hames835ca072009-11-19 04:15:33 +000043 clEnumValEnd),
44 cl::init(standard));
45
Lang Hames61945692009-12-09 05:39:12 +000046// Spiller virtual destructor implementation.
Lang Hamese2b201b2009-05-18 19:03:16 +000047Spiller::~Spiller() {}
48
49namespace {
50
Lang Hamesf41538d2009-06-02 16:53:25 +000051/// Utility class for spillers.
52class SpillerBase : public Spiller {
53protected:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000054 MachineFunctionPass *pass;
Lang Hamesf41538d2009-06-02 16:53:25 +000055 MachineFunction *mf;
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000056 VirtRegMap *vrm;
Lang Hamesf41538d2009-06-02 16:53:25 +000057 LiveIntervals *lis;
Lang Hamesf41538d2009-06-02 16:53:25 +000058 MachineFrameInfo *mfi;
59 MachineRegisterInfo *mri;
60 const TargetInstrInfo *tii;
Evan Cheng746ad692010-05-06 19:06:44 +000061 const TargetRegisterInfo *tri;
Eric Christopher894339e2010-07-06 18:35:20 +000062
63 /// Construct a spiller base.
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000064 SpillerBase(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
65 : pass(&pass), mf(&mf), vrm(&vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000066 {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000067 lis = &pass.getAnalysis<LiveIntervals>();
68 mfi = mf.getFrameInfo();
69 mri = &mf.getRegInfo();
70 tii = mf.getTarget().getInstrInfo();
71 tri = mf.getTarget().getRegisterInfo();
Lang Hamese2b201b2009-05-18 19:03:16 +000072 }
73
Lang Hamesf41538d2009-06-02 16:53:25 +000074 /// Add spill ranges for every use/def of the live interval, inserting loads
Lang Hames38283e22009-11-18 20:31:20 +000075 /// immediately before each use, and stores after each def. No folding or
76 /// remat is attempted.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +000077 void trivialSpillEverywhere(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +000078 SmallVectorImpl<LiveInterval*> &newIntervals) {
David Greene65de5042010-01-05 01:25:55 +000079 DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
Lang Hamese2b201b2009-05-18 19:03:16 +000080
81 assert(li->weight != HUGE_VALF &&
82 "Attempting to spill already spilled value.");
83
84 assert(!li->isStackSlot() &&
85 "Trying to spill a stack slot.");
86
David Greene65de5042010-01-05 01:25:55 +000087 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
Lang Hames6bbc73d2009-06-24 20:46:24 +000088
Lang Hamese2b201b2009-05-18 19:03:16 +000089 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +000090 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
91
Lang Hames38283e22009-11-18 20:31:20 +000092 // Iterate over reg uses/defs.
Lang Hamesf41538d2009-06-02 16:53:25 +000093 for (MachineRegisterInfo::reg_iterator
94 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +000095
Lang Hames38283e22009-11-18 20:31:20 +000096 // Grab the use/def instr.
Lang Hamese2b201b2009-05-18 19:03:16 +000097 MachineInstr *mi = &*regItr;
Lang Hames6bbc73d2009-06-24 20:46:24 +000098
David Greene65de5042010-01-05 01:25:55 +000099 DEBUG(dbgs() << " Processing " << *mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +0000100
Lang Hames38283e22009-11-18 20:31:20 +0000101 // Step regItr to the next use/def instr.
Lang Hamesf41538d2009-06-02 16:53:25 +0000102 do {
103 ++regItr;
104 } while (regItr != mri->reg_end() && (&*regItr == mi));
Eric Christopher894339e2010-07-06 18:35:20 +0000105
Lang Hames38283e22009-11-18 20:31:20 +0000106 // Collect uses & defs for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000107 SmallVector<unsigned, 2> indices;
108 bool hasUse = false;
109 bool hasDef = false;
Lang Hamese2b201b2009-05-18 19:03:16 +0000110 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
111 MachineOperand &op = mi->getOperand(i);
Lang Hamese2b201b2009-05-18 19:03:16 +0000112 if (!op.isReg() || op.getReg() != li->reg)
113 continue;
Lang Hamese2b201b2009-05-18 19:03:16 +0000114 hasUse |= mi->getOperand(i).isUse();
115 hasDef |= mi->getOperand(i).isDef();
Lang Hamese2b201b2009-05-18 19:03:16 +0000116 indices.push_back(i);
117 }
118
Lang Hames38283e22009-11-18 20:31:20 +0000119 // Create a new vreg & interval for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000120 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000121 vrm->grow();
122 vrm->assignVirt2StackSlot(newVReg, ss);
Lang Hamesf41538d2009-06-02 16:53:25 +0000123 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
124 newLI->weight = HUGE_VALF;
Eric Christopher894339e2010-07-06 18:35:20 +0000125
Lang Hames38283e22009-11-18 20:31:20 +0000126 // Update the reg operands & kill flags.
Lang Hamese2b201b2009-05-18 19:03:16 +0000127 for (unsigned i = 0; i < indices.size(); ++i) {
Lang Hames38283e22009-11-18 20:31:20 +0000128 unsigned mopIdx = indices[i];
129 MachineOperand &mop = mi->getOperand(mopIdx);
130 mop.setReg(newVReg);
131 if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
132 mop.setIsKill(true);
Lang Hamese2b201b2009-05-18 19:03:16 +0000133 }
134 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000135 assert(hasUse || hasDef);
136
Lang Hames38283e22009-11-18 20:31:20 +0000137 // Insert reload if necessary.
138 MachineBasicBlock::iterator miItr(mi);
Lang Hamese2b201b2009-05-18 19:03:16 +0000139 if (hasUse) {
Evan Cheng746ad692010-05-06 19:06:44 +0000140 tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc,
141 tri);
Lang Hames38283e22009-11-18 20:31:20 +0000142 MachineInstr *loadInstr(prior(miItr));
143 SlotIndex loadIndex =
144 lis->InsertMachineInstrInMaps(loadInstr).getDefIndex();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000145 vrm->addSpillSlotUse(ss, loadInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000146 SlotIndex endIndex = loadIndex.getNextIndex();
147 VNInfo *loadVNI =
Lang Hames6e2968c2010-09-25 12:04:16 +0000148 newLI->getNextValue(loadIndex, 0, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000149 newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000150 }
151
Lang Hames38283e22009-11-18 20:31:20 +0000152 // Insert store if necessary.
Lang Hamese2b201b2009-05-18 19:03:16 +0000153 if (hasDef) {
Evan Chenge9b3ac22010-05-06 16:33:12 +0000154 tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg,
Evan Cheng746ad692010-05-06 19:06:44 +0000155 true, ss, trc, tri);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000156 MachineInstr *storeInstr(llvm::next(miItr));
Lang Hames38283e22009-11-18 20:31:20 +0000157 SlotIndex storeIndex =
158 lis->InsertMachineInstrInMaps(storeInstr).getDefIndex();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000159 vrm->addSpillSlotUse(ss, storeInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000160 SlotIndex beginIndex = storeIndex.getPrevIndex();
161 VNInfo *storeVNI =
Lang Hames6e2968c2010-09-25 12:04:16 +0000162 newLI->getNextValue(beginIndex, 0, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000163 newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000164 }
165
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000166 newIntervals.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000167 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000168 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000169};
Lang Hamese2b201b2009-05-18 19:03:16 +0000170
Chris Lattner1ca65312010-04-07 22:44:07 +0000171} // end anonymous namespace
172
173namespace {
Lang Hamese2b201b2009-05-18 19:03:16 +0000174
Lang Hamesf41538d2009-06-02 16:53:25 +0000175/// Spills any live range using the spill-everywhere method with no attempt at
176/// folding.
177class TrivialSpiller : public SpillerBase {
178public:
Lang Hames10382fb2009-06-19 02:17:53 +0000179
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000180 TrivialSpiller(MachineFunctionPass &pass, MachineFunction &mf,
181 VirtRegMap &vrm)
182 : SpillerBase(pass, mf, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000183
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000184 void spill(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000185 SmallVectorImpl<LiveInterval*> &newIntervals,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000186 const SmallVectorImpl<LiveInterval*> &) {
Lang Hames835ca072009-11-19 04:15:33 +0000187 // Ignore spillIs - we don't use it.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000188 trivialSpillEverywhere(li, newIntervals);
Lang Hamese2b201b2009-05-18 19:03:16 +0000189 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000190};
191
Chris Lattner1ca65312010-04-07 22:44:07 +0000192} // end anonymous namespace
193
194namespace {
195
Lang Hames835ca072009-11-19 04:15:33 +0000196/// Falls back on LiveIntervals::addIntervalsForSpills.
197class StandardSpiller : public Spiller {
Lang Hames61945692009-12-09 05:39:12 +0000198protected:
Jakob Stoklund Olesen2d172932010-10-26 00:11:33 +0000199 MachineFunction *mf;
Lang Hames835ca072009-11-19 04:15:33 +0000200 LiveIntervals *lis;
Jakob Stoklund Olesen2d172932010-10-26 00:11:33 +0000201 LiveStacks *lss;
Jakob Stoklund Olesen9529a1c2010-07-19 18:41:20 +0000202 MachineLoopInfo *loopInfo;
Lang Hames835ca072009-11-19 04:15:33 +0000203 VirtRegMap *vrm;
204public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000205 StandardSpiller(MachineFunctionPass &pass, MachineFunction &mf,
206 VirtRegMap &vrm)
Jakob Stoklund Olesen2d172932010-10-26 00:11:33 +0000207 : mf(&mf),
208 lis(&pass.getAnalysis<LiveIntervals>()),
209 lss(&pass.getAnalysis<LiveStacks>()),
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000210 loopInfo(pass.getAnalysisIfAvailable<MachineLoopInfo>()),
211 vrm(&vrm) {}
Lang Hames835ca072009-11-19 04:15:33 +0000212
213 /// Falls back on LiveIntervals::addIntervalsForSpills.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000214 void spill(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000215 SmallVectorImpl<LiveInterval*> &newIntervals,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000216 const SmallVectorImpl<LiveInterval*> &spillIs) {
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000217 std::vector<LiveInterval*> added =
218 lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
219 newIntervals.insert(newIntervals.end(), added.begin(), added.end());
Jakob Stoklund Olesen2d172932010-10-26 00:11:33 +0000220
221 // Update LiveStacks.
222 int SS = vrm->getStackSlot(li->reg);
223 if (SS == VirtRegMap::NO_STACK_SLOT)
224 return;
225 const TargetRegisterClass *RC = mf->getRegInfo().getRegClass(li->reg);
226 LiveInterval &SI = lss->getOrCreateInterval(SS, RC);
227 if (!SI.hasAtLeastOneValue())
228 SI.getNextValue(SlotIndex(), 0, lss->getVNInfoAllocator());
229 SI.MergeRangesInAsValue(*li, SI.getValNumInfo(0));
Lang Hames835ca072009-11-19 04:15:33 +0000230 }
Lang Hames835ca072009-11-19 04:15:33 +0000231};
232
Chris Lattner1ca65312010-04-07 22:44:07 +0000233} // end anonymous namespace
234
235namespace {
236
Lang Hames61945692009-12-09 05:39:12 +0000237/// When a call to spill is placed this spiller will first try to break the
238/// interval up into its component values (one new interval per value).
239/// If this fails, or if a call is placed to spill a previously split interval
Eric Christopher894339e2010-07-06 18:35:20 +0000240/// then the spiller falls back on the standard spilling mechanism.
Lang Hames61945692009-12-09 05:39:12 +0000241class SplittingSpiller : public StandardSpiller {
242public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000243 SplittingSpiller(MachineFunctionPass &pass, MachineFunction &mf,
244 VirtRegMap &vrm)
245 : StandardSpiller(pass, mf, vrm) {
246 mri = &mf.getRegInfo();
247 tii = mf.getTarget().getInstrInfo();
248 tri = mf.getTarget().getRegisterInfo();
Lang Hames61945692009-12-09 05:39:12 +0000249 }
250
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000251 void spill(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000252 SmallVectorImpl<LiveInterval*> &newIntervals,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000253 const SmallVectorImpl<LiveInterval*> &spillIs) {
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000254 if (worthTryingToSplit(li))
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000255 tryVNISplit(li);
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000256 else
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000257 StandardSpiller::spill(li, newIntervals, spillIs);
Lang Hames61945692009-12-09 05:39:12 +0000258 }
259
260private:
261
262 MachineRegisterInfo *mri;
263 const TargetInstrInfo *tii;
Eric Christopher894339e2010-07-06 18:35:20 +0000264 const TargetRegisterInfo *tri;
Lang Hames61945692009-12-09 05:39:12 +0000265 DenseSet<LiveInterval*> alreadySplit;
266
267 bool worthTryingToSplit(LiveInterval *li) const {
268 return (!alreadySplit.count(li) && li->getNumValNums() > 1);
269 }
270
271 /// Try to break a LiveInterval into its component values.
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000272 std::vector<LiveInterval*> tryVNISplit(LiveInterval *li) {
Lang Hames61945692009-12-09 05:39:12 +0000273
David Greene65de5042010-01-05 01:25:55 +0000274 DEBUG(dbgs() << "Trying VNI split of %reg" << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000275
276 std::vector<LiveInterval*> added;
277 SmallVector<VNInfo*, 4> vnis;
278
279 std::copy(li->vni_begin(), li->vni_end(), std::back_inserter(vnis));
Eric Christopher894339e2010-07-06 18:35:20 +0000280
Lang Hames61945692009-12-09 05:39:12 +0000281 for (SmallVectorImpl<VNInfo*>::iterator vniItr = vnis.begin(),
282 vniEnd = vnis.end(); vniItr != vniEnd; ++vniItr) {
283 VNInfo *vni = *vniItr;
Eric Christopher894339e2010-07-06 18:35:20 +0000284
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000285 // Skip unused VNIs.
286 if (vni->isUnused())
Lang Hames61945692009-12-09 05:39:12 +0000287 continue;
288
David Greene65de5042010-01-05 01:25:55 +0000289 DEBUG(dbgs() << " Extracted Val #" << vni->id << " as ");
Lang Hames61945692009-12-09 05:39:12 +0000290 LiveInterval *splitInterval = extractVNI(li, vni);
Eric Christopher894339e2010-07-06 18:35:20 +0000291
Lang Hames61945692009-12-09 05:39:12 +0000292 if (splitInterval != 0) {
David Greene65de5042010-01-05 01:25:55 +0000293 DEBUG(dbgs() << *splitInterval << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000294 added.push_back(splitInterval);
295 alreadySplit.insert(splitInterval);
Lang Hames61945692009-12-09 05:39:12 +0000296 } else {
David Greene65de5042010-01-05 01:25:55 +0000297 DEBUG(dbgs() << "0\n");
Lang Hames61945692009-12-09 05:39:12 +0000298 }
Eric Christopher894339e2010-07-06 18:35:20 +0000299 }
Lang Hames61945692009-12-09 05:39:12 +0000300
David Greene65de5042010-01-05 01:25:55 +0000301 DEBUG(dbgs() << "Original LI: " << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000302
303 // If there original interval still contains some live ranges
Eric Christopher894339e2010-07-06 18:35:20 +0000304 // add it to added and alreadySplit.
Lang Hames61945692009-12-09 05:39:12 +0000305 if (!li->empty()) {
306 added.push_back(li);
307 alreadySplit.insert(li);
Lang Hames61945692009-12-09 05:39:12 +0000308 }
309
310 return added;
311 }
312
313 /// Extract the given value number from the interval.
314 LiveInterval* extractVNI(LiveInterval *li, VNInfo *vni) const {
Lang Hames6e2968c2010-09-25 12:04:16 +0000315 assert((lis->getInstructionFromIndex(vni->def) != 0 || vni->isPHIDef()) &&
316 "Def index not sane?");
Lang Hames61945692009-12-09 05:39:12 +0000317
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000318 // Create a new vreg and live interval, copy VNI ranges over.
Lang Hames61945692009-12-09 05:39:12 +0000319 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
320 unsigned newVReg = mri->createVirtualRegister(trc);
321 vrm->grow();
322 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
323 VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator());
324
Eric Christopher894339e2010-07-06 18:35:20 +0000325 // Start by copying all live ranges in the VN to the new interval.
Lang Hames61945692009-12-09 05:39:12 +0000326 for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end();
327 rItr != rEnd; ++rItr) {
328 if (rItr->valno == vni) {
329 newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI));
330 }
331 }
332
Eric Christopher894339e2010-07-06 18:35:20 +0000333 // Erase the old VNI & ranges.
Lang Hames61945692009-12-09 05:39:12 +0000334 li->removeValNo(vni);
335
336 // Collect all current uses of the register belonging to the given VNI.
337 // We'll use this to rename the register after we've dealt with the def.
338 std::set<MachineInstr*> uses;
339 for (MachineRegisterInfo::use_iterator
340 useItr = mri->use_begin(li->reg), useEnd = mri->use_end();
341 useItr != useEnd; ++useItr) {
342 uses.insert(&*useItr);
343 }
344
345 // Process the def instruction for this VNI.
346 if (newVNI->isPHIDef()) {
347 // Insert a copy at the start of the MBB. The range proceeding the
348 // copy will be attached to the original LiveInterval.
349 MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000350 MachineInstr *copyMI = BuildMI(*defMBB, defMBB->begin(), DebugLoc(),
351 tii->get(TargetOpcode::COPY), newVReg)
352 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000353 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames6e2968c2010-09-25 12:04:16 +0000354 SlotIndex phiDefIdx = lis->getMBBStartIdx(defMBB);
355 assert(lis->getInstructionFromIndex(phiDefIdx) == 0 &&
356 "PHI def index points at actual instruction.");
357 VNInfo *phiDefVNI = li->getNextValue(phiDefIdx,
358 0, lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000359 phiDefVNI->setIsPHIDef(true);
Lang Hames61945692009-12-09 05:39:12 +0000360 li->addRange(LiveRange(phiDefVNI->def, copyIdx.getDefIndex(), phiDefVNI));
361 LiveRange *oldPHIDefRange =
362 newLI->getLiveRangeContaining(lis->getMBBStartIdx(defMBB));
363
364 // If the old phi def starts in the middle of the range chop it up.
365 if (oldPHIDefRange->start < lis->getMBBStartIdx(defMBB)) {
366 LiveRange oldPHIDefRange2(copyIdx.getDefIndex(), oldPHIDefRange->end,
367 oldPHIDefRange->valno);
368 oldPHIDefRange->end = lis->getMBBStartIdx(defMBB);
369 newLI->addRange(oldPHIDefRange2);
370 } else if (oldPHIDefRange->start == lis->getMBBStartIdx(defMBB)) {
371 // Otherwise if it's at the start of the range just trim it.
372 oldPHIDefRange->start = copyIdx.getDefIndex();
373 } else {
374 assert(false && "PHI def range doesn't cover PHI def?");
375 }
376
377 newVNI->def = copyIdx.getDefIndex();
378 newVNI->setCopy(copyMI);
379 newVNI->setIsPHIDef(false); // not a PHI def anymore.
Lang Hames61945692009-12-09 05:39:12 +0000380 } else {
Eric Christopher894339e2010-07-06 18:35:20 +0000381 // non-PHI def. Rename the def. If it's two-addr that means renaming the
382 // use and inserting a new copy too.
Lang Hames61945692009-12-09 05:39:12 +0000383 MachineInstr *defInst = lis->getInstructionFromIndex(newVNI->def);
384 // We'll rename this now, so we can remove it from uses.
385 uses.erase(defInst);
386 unsigned defOpIdx = defInst->findRegisterDefOperandIdx(li->reg);
387 bool isTwoAddr = defInst->isRegTiedToUseOperand(defOpIdx),
388 twoAddrUseIsUndef = false;
389
390 for (unsigned i = 0; i < defInst->getNumOperands(); ++i) {
391 MachineOperand &mo = defInst->getOperand(i);
392 if (mo.isReg() && (mo.isDef() || isTwoAddr) && (mo.getReg()==li->reg)) {
393 mo.setReg(newVReg);
394 if (isTwoAddr && mo.isUse() && mo.isUndef())
395 twoAddrUseIsUndef = true;
396 }
397 }
Eric Christopher894339e2010-07-06 18:35:20 +0000398
Lang Hames61945692009-12-09 05:39:12 +0000399 SlotIndex defIdx = lis->getInstructionIndex(defInst);
400 newVNI->def = defIdx.getDefIndex();
401
402 if (isTwoAddr && !twoAddrUseIsUndef) {
403 MachineBasicBlock *defMBB = defInst->getParent();
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000404 MachineInstr *copyMI = BuildMI(*defMBB, defInst, DebugLoc(),
405 tii->get(TargetOpcode::COPY), newVReg)
406 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000407 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000408 LiveRange *origUseRange =
409 li->getLiveRangeContaining(newVNI->def.getUseIndex());
Lang Hames61945692009-12-09 05:39:12 +0000410 origUseRange->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000411 VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI,
Lang Hames6e2968c2010-09-25 12:04:16 +0000412 lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000413 LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI);
414 newLI->addRange(copyRange);
Eric Christopher894339e2010-07-06 18:35:20 +0000415 }
Lang Hames61945692009-12-09 05:39:12 +0000416 }
Eric Christopher894339e2010-07-06 18:35:20 +0000417
Lang Hames61945692009-12-09 05:39:12 +0000418 for (std::set<MachineInstr*>::iterator
419 usesItr = uses.begin(), usesEnd = uses.end();
420 usesItr != usesEnd; ++usesItr) {
421 MachineInstr *useInst = *usesItr;
422 SlotIndex useIdx = lis->getInstructionIndex(useInst);
423 LiveRange *useRange =
424 newLI->getLiveRangeContaining(useIdx.getUseIndex());
425
426 // If this use doesn't belong to the new interval skip it.
427 if (useRange == 0)
428 continue;
429
430 // This use doesn't belong to the VNI, skip it.
431 if (useRange->valno != newVNI)
432 continue;
433
434 // Check if this instr is two address.
435 unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg);
436 bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx);
Eric Christopher894339e2010-07-06 18:35:20 +0000437
Lang Hames61945692009-12-09 05:39:12 +0000438 // Rename uses (and defs for two-address instrs).
439 for (unsigned i = 0; i < useInst->getNumOperands(); ++i) {
440 MachineOperand &mo = useInst->getOperand(i);
441 if (mo.isReg() && (mo.isUse() || isTwoAddress) &&
442 (mo.getReg() == li->reg)) {
443 mo.setReg(newVReg);
444 }
445 }
446
447 // If this is a two address instruction we've got some extra work to do.
448 if (isTwoAddress) {
449 // We modified the def operand, so we need to copy back to the original
450 // reg.
451 MachineBasicBlock *useMBB = useInst->getParent();
452 MachineBasicBlock::iterator useItr(useInst);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000453 MachineInstr *copyMI = BuildMI(*useMBB, llvm::next(useItr), DebugLoc(),
454 tii->get(TargetOpcode::COPY), newVReg)
455 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000456 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
457
458 // Change the old two-address defined range & vni to start at
459 // (and be defined by) the copy.
460 LiveRange *origDefRange =
461 li->getLiveRangeContaining(useIdx.getDefIndex());
462 origDefRange->start = copyIdx.getDefIndex();
463 origDefRange->valno->def = copyIdx.getDefIndex();
464 origDefRange->valno->setCopy(copyMI);
465
466 // Insert a new range & vni for the two-address-to-copy value. This
467 // will be attached to the new live interval.
468 VNInfo *copyVNI =
Lang Hames6e2968c2010-09-25 12:04:16 +0000469 newLI->getNextValue(useIdx.getDefIndex(), 0,
Lang Hames61945692009-12-09 05:39:12 +0000470 lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000471 LiveRange copyRange(useIdx.getDefIndex(),copyIdx.getDefIndex(),copyVNI);
472 newLI->addRange(copyRange);
473 }
474 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000475
Lang Hames61945692009-12-09 05:39:12 +0000476 // Iterate over any PHI kills - we'll need to insert new copies for them.
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000477 for (LiveInterval::iterator LRI = newLI->begin(), LRE = newLI->end();
478 LRI != LRE; ++LRI) {
Jakob Stoklund Olesen7c78e262010-09-25 00:45:15 +0000479 if (LRI->valno != newVNI)
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000480 continue;
481 SlotIndex killIdx = LRI->end;
482 MachineBasicBlock *killMBB = lis->getMBBFromIndex(killIdx);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000483 MachineInstr *copyMI = BuildMI(*killMBB, killMBB->getFirstTerminator(),
484 DebugLoc(), tii->get(TargetOpcode::COPY),
485 li->reg)
486 .addReg(newVReg, RegState::Kill);
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000487 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000488
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000489 // Save the current end. We may need it to add a new range if the
490 // current range runs of the end of the MBB.
491 SlotIndex newKillRangeEnd = LRI->end;
492 LRI->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000493
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000494 if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) {
495 assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) &&
496 "PHI kill range doesn't reach kill-block end. Not sane.");
497 newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB),
498 newKillRangeEnd, newVNI));
Lang Hames61945692009-12-09 05:39:12 +0000499 }
500
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000501 VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(),
Lang Hames6e2968c2010-09-25 12:04:16 +0000502 copyMI, lis->getVNInfoAllocator());
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000503 newKillVNI->setHasPHIKill(true);
504 li->addRange(LiveRange(copyIdx.getDefIndex(),
505 lis->getMBBEndIdx(killMBB),
506 newKillVNI));
Lang Hames61945692009-12-09 05:39:12 +0000507 }
Lang Hames61945692009-12-09 05:39:12 +0000508 newVNI->setHasPHIKill(false);
509
510 return newLI;
511 }
512
513};
514
Chris Lattner1ca65312010-04-07 22:44:07 +0000515} // end anonymous namespace
516
Lang Hamese2b201b2009-05-18 19:03:16 +0000517
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000518namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000519Spiller *createInlineSpiller(MachineFunctionPass &pass,
520 MachineFunction &mf,
521 VirtRegMap &vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000522}
523
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000524llvm::Spiller* llvm::createSpiller(MachineFunctionPass &pass,
525 MachineFunction &mf,
526 VirtRegMap &vrm) {
Lang Hames835ca072009-11-19 04:15:33 +0000527 switch (spillerOpt) {
Chris Lattner1ca65312010-04-07 22:44:07 +0000528 default: assert(0 && "unknown spiller");
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000529 case trivial: return new TrivialSpiller(pass, mf, vrm);
530 case standard: return new StandardSpiller(pass, mf, vrm);
531 case splitting: return new SplittingSpiller(pass, mf, vrm);
532 case inline_: return createInlineSpiller(pass, mf, vrm);
Lang Hames835ca072009-11-19 04:15:33 +0000533 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000534}