blob: 661b79c1b36217b5b95c7f4bbadbe639a5a38cc6 [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"
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000018#include "llvm/CodeGen/MachineLoopInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/TargetInstrInfo.h"
Lang Hames835ca072009-11-19 04:15:33 +000022#include "llvm/Support/CommandLine.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000023#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +000024#include "llvm/Support/ErrorHandling.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000025#include "llvm/Support/raw_ostream.h"
Lang Hames61945692009-12-09 05:39:12 +000026#include <set>
Lang Hamese2b201b2009-05-18 19:03:16 +000027
Lang Hamese2b201b2009-05-18 19:03:16 +000028using namespace llvm;
29
Lang Hames835ca072009-11-19 04:15:33 +000030namespace {
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +000031 enum SpillerName { trivial, standard, splitting, inline_ };
Lang Hames835ca072009-11-19 04:15:33 +000032}
33
34static cl::opt<SpillerName>
35spillerOpt("spiller",
36 cl::desc("Spiller to use: (default: standard)"),
37 cl::Prefix,
Lang Hames61945692009-12-09 05:39:12 +000038 cl::values(clEnumVal(trivial, "trivial spiller"),
39 clEnumVal(standard, "default spiller"),
40 clEnumVal(splitting, "splitting spiller"),
Jakob Stoklund Olesend5bd68e2010-06-30 00:24:51 +000041 clEnumValN(inline_, "inline", "inline spiller"),
Lang Hames835ca072009-11-19 04:15:33 +000042 clEnumValEnd),
43 cl::init(standard));
44
Lang Hames61945692009-12-09 05:39:12 +000045// Spiller virtual destructor implementation.
Lang Hamese2b201b2009-05-18 19:03:16 +000046Spiller::~Spiller() {}
47
48namespace {
49
Lang Hamesf41538d2009-06-02 16:53:25 +000050/// Utility class for spillers.
51class SpillerBase : public Spiller {
52protected:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000053 MachineFunctionPass *pass;
Lang Hamesf41538d2009-06-02 16:53:25 +000054 MachineFunction *mf;
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000055 VirtRegMap *vrm;
Lang Hamesf41538d2009-06-02 16:53:25 +000056 LiveIntervals *lis;
Lang Hamesf41538d2009-06-02 16:53:25 +000057 MachineFrameInfo *mfi;
58 MachineRegisterInfo *mri;
59 const TargetInstrInfo *tii;
Evan Cheng746ad692010-05-06 19:06:44 +000060 const TargetRegisterInfo *tri;
Eric Christopher894339e2010-07-06 18:35:20 +000061
62 /// Construct a spiller base.
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000063 SpillerBase(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
64 : pass(&pass), mf(&mf), vrm(&vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000065 {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +000066 lis = &pass.getAnalysis<LiveIntervals>();
67 mfi = mf.getFrameInfo();
68 mri = &mf.getRegInfo();
69 tii = mf.getTarget().getInstrInfo();
70 tri = mf.getTarget().getRegisterInfo();
Lang Hamese2b201b2009-05-18 19:03:16 +000071 }
72
Lang Hamesf41538d2009-06-02 16:53:25 +000073 /// Add spill ranges for every use/def of the live interval, inserting loads
Lang Hames38283e22009-11-18 20:31:20 +000074 /// immediately before each use, and stores after each def. No folding or
75 /// remat is attempted.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +000076 void trivialSpillEverywhere(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +000077 SmallVectorImpl<LiveInterval*> &newIntervals) {
David Greene65de5042010-01-05 01:25:55 +000078 DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
Lang Hamese2b201b2009-05-18 19:03:16 +000079
80 assert(li->weight != HUGE_VALF &&
81 "Attempting to spill already spilled value.");
82
83 assert(!li->isStackSlot() &&
84 "Trying to spill a stack slot.");
85
David Greene65de5042010-01-05 01:25:55 +000086 DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
Lang Hames6bbc73d2009-06-24 20:46:24 +000087
Lang Hamese2b201b2009-05-18 19:03:16 +000088 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +000089 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
90
Lang Hames38283e22009-11-18 20:31:20 +000091 // Iterate over reg uses/defs.
Lang Hamesf41538d2009-06-02 16:53:25 +000092 for (MachineRegisterInfo::reg_iterator
93 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +000094
Lang Hames38283e22009-11-18 20:31:20 +000095 // Grab the use/def instr.
Lang Hamese2b201b2009-05-18 19:03:16 +000096 MachineInstr *mi = &*regItr;
Lang Hames6bbc73d2009-06-24 20:46:24 +000097
David Greene65de5042010-01-05 01:25:55 +000098 DEBUG(dbgs() << " Processing " << *mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +000099
Lang Hames38283e22009-11-18 20:31:20 +0000100 // Step regItr to the next use/def instr.
Lang Hamesf41538d2009-06-02 16:53:25 +0000101 do {
102 ++regItr;
103 } while (regItr != mri->reg_end() && (&*regItr == mi));
Eric Christopher894339e2010-07-06 18:35:20 +0000104
Lang Hames38283e22009-11-18 20:31:20 +0000105 // Collect uses & defs for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000106 SmallVector<unsigned, 2> indices;
107 bool hasUse = false;
108 bool hasDef = false;
Lang Hamese2b201b2009-05-18 19:03:16 +0000109 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
110 MachineOperand &op = mi->getOperand(i);
Lang Hamese2b201b2009-05-18 19:03:16 +0000111 if (!op.isReg() || op.getReg() != li->reg)
112 continue;
Lang Hamese2b201b2009-05-18 19:03:16 +0000113 hasUse |= mi->getOperand(i).isUse();
114 hasDef |= mi->getOperand(i).isDef();
Lang Hamese2b201b2009-05-18 19:03:16 +0000115 indices.push_back(i);
116 }
117
Lang Hames38283e22009-11-18 20:31:20 +0000118 // Create a new vreg & interval for this instr.
Lang Hamese2b201b2009-05-18 19:03:16 +0000119 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000120 vrm->grow();
121 vrm->assignVirt2StackSlot(newVReg, ss);
Lang Hamesf41538d2009-06-02 16:53:25 +0000122 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
123 newLI->weight = HUGE_VALF;
Eric Christopher894339e2010-07-06 18:35:20 +0000124
Lang Hames38283e22009-11-18 20:31:20 +0000125 // Update the reg operands & kill flags.
Lang Hamese2b201b2009-05-18 19:03:16 +0000126 for (unsigned i = 0; i < indices.size(); ++i) {
Lang Hames38283e22009-11-18 20:31:20 +0000127 unsigned mopIdx = indices[i];
128 MachineOperand &mop = mi->getOperand(mopIdx);
129 mop.setReg(newVReg);
130 if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
131 mop.setIsKill(true);
Lang Hamese2b201b2009-05-18 19:03:16 +0000132 }
133 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000134 assert(hasUse || hasDef);
135
Lang Hames38283e22009-11-18 20:31:20 +0000136 // Insert reload if necessary.
137 MachineBasicBlock::iterator miItr(mi);
Lang Hamese2b201b2009-05-18 19:03:16 +0000138 if (hasUse) {
Evan Cheng746ad692010-05-06 19:06:44 +0000139 tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc,
140 tri);
Lang Hames38283e22009-11-18 20:31:20 +0000141 MachineInstr *loadInstr(prior(miItr));
142 SlotIndex loadIndex =
143 lis->InsertMachineInstrInMaps(loadInstr).getDefIndex();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000144 vrm->addSpillSlotUse(ss, loadInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000145 SlotIndex endIndex = loadIndex.getNextIndex();
146 VNInfo *loadVNI =
Lang Hames6e2968c2010-09-25 12:04:16 +0000147 newLI->getNextValue(loadIndex, 0, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000148 newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000149 }
150
Lang Hames38283e22009-11-18 20:31:20 +0000151 // Insert store if necessary.
Lang Hamese2b201b2009-05-18 19:03:16 +0000152 if (hasDef) {
Evan Chenge9b3ac22010-05-06 16:33:12 +0000153 tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg,
Evan Cheng746ad692010-05-06 19:06:44 +0000154 true, ss, trc, tri);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000155 MachineInstr *storeInstr(llvm::next(miItr));
Lang Hames38283e22009-11-18 20:31:20 +0000156 SlotIndex storeIndex =
157 lis->InsertMachineInstrInMaps(storeInstr).getDefIndex();
Jakob Stoklund Olesend5408012010-06-30 18:41:20 +0000158 vrm->addSpillSlotUse(ss, storeInstr);
Lang Hames38283e22009-11-18 20:31:20 +0000159 SlotIndex beginIndex = storeIndex.getPrevIndex();
160 VNInfo *storeVNI =
Lang Hames6e2968c2010-09-25 12:04:16 +0000161 newLI->getNextValue(beginIndex, 0, lis->getVNInfoAllocator());
Lang Hames38283e22009-11-18 20:31:20 +0000162 newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI));
Lang Hamese2b201b2009-05-18 19:03:16 +0000163 }
164
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000165 newIntervals.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000166 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000167 }
Lang Hamesf41538d2009-06-02 16:53:25 +0000168};
Lang Hamese2b201b2009-05-18 19:03:16 +0000169
Chris Lattner1ca65312010-04-07 22:44:07 +0000170} // end anonymous namespace
171
172namespace {
Lang Hamese2b201b2009-05-18 19:03:16 +0000173
Lang Hamesf41538d2009-06-02 16:53:25 +0000174/// Spills any live range using the spill-everywhere method with no attempt at
175/// folding.
176class TrivialSpiller : public SpillerBase {
177public:
Lang Hames10382fb2009-06-19 02:17:53 +0000178
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000179 TrivialSpiller(MachineFunctionPass &pass, MachineFunction &mf,
180 VirtRegMap &vrm)
181 : SpillerBase(pass, mf, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000182
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000183 void spill(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000184 SmallVectorImpl<LiveInterval*> &newIntervals,
185 SmallVectorImpl<LiveInterval*> &) {
Lang Hames835ca072009-11-19 04:15:33 +0000186 // Ignore spillIs - we don't use it.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000187 trivialSpillEverywhere(li, newIntervals);
Lang Hamese2b201b2009-05-18 19:03:16 +0000188 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000189};
190
Chris Lattner1ca65312010-04-07 22:44:07 +0000191} // end anonymous namespace
192
193namespace {
194
Lang Hames835ca072009-11-19 04:15:33 +0000195/// Falls back on LiveIntervals::addIntervalsForSpills.
196class StandardSpiller : public Spiller {
Lang Hames61945692009-12-09 05:39:12 +0000197protected:
Lang Hames835ca072009-11-19 04:15:33 +0000198 LiveIntervals *lis;
Jakob Stoklund Olesen9529a1c2010-07-19 18:41:20 +0000199 MachineLoopInfo *loopInfo;
Lang Hames835ca072009-11-19 04:15:33 +0000200 VirtRegMap *vrm;
201public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000202 StandardSpiller(MachineFunctionPass &pass, MachineFunction &mf,
203 VirtRegMap &vrm)
204 : lis(&pass.getAnalysis<LiveIntervals>()),
205 loopInfo(pass.getAnalysisIfAvailable<MachineLoopInfo>()),
206 vrm(&vrm) {}
Lang Hames835ca072009-11-19 04:15:33 +0000207
208 /// Falls back on LiveIntervals::addIntervalsForSpills.
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000209 void spill(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000210 SmallVectorImpl<LiveInterval*> &newIntervals,
211 SmallVectorImpl<LiveInterval*> &spillIs) {
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000212 std::vector<LiveInterval*> added =
213 lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm);
214 newIntervals.insert(newIntervals.end(), added.begin(), added.end());
Lang Hames835ca072009-11-19 04:15:33 +0000215 }
Lang Hames835ca072009-11-19 04:15:33 +0000216};
217
Chris Lattner1ca65312010-04-07 22:44:07 +0000218} // end anonymous namespace
219
220namespace {
221
Lang Hames61945692009-12-09 05:39:12 +0000222/// When a call to spill is placed this spiller will first try to break the
223/// interval up into its component values (one new interval per value).
224/// If this fails, or if a call is placed to spill a previously split interval
Eric Christopher894339e2010-07-06 18:35:20 +0000225/// then the spiller falls back on the standard spilling mechanism.
Lang Hames61945692009-12-09 05:39:12 +0000226class SplittingSpiller : public StandardSpiller {
227public:
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000228 SplittingSpiller(MachineFunctionPass &pass, MachineFunction &mf,
229 VirtRegMap &vrm)
230 : StandardSpiller(pass, mf, vrm) {
231 mri = &mf.getRegInfo();
232 tii = mf.getTarget().getInstrInfo();
233 tri = mf.getTarget().getRegisterInfo();
Lang Hames61945692009-12-09 05:39:12 +0000234 }
235
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000236 void spill(LiveInterval *li,
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000237 SmallVectorImpl<LiveInterval*> &newIntervals,
238 SmallVectorImpl<LiveInterval*> &spillIs) {
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000239 if (worthTryingToSplit(li))
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000240 tryVNISplit(li);
Jakob Stoklund Olesen67674e22010-06-24 20:54:29 +0000241 else
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000242 StandardSpiller::spill(li, newIntervals, spillIs);
Lang Hames61945692009-12-09 05:39:12 +0000243 }
244
245private:
246
247 MachineRegisterInfo *mri;
248 const TargetInstrInfo *tii;
Eric Christopher894339e2010-07-06 18:35:20 +0000249 const TargetRegisterInfo *tri;
Lang Hames61945692009-12-09 05:39:12 +0000250 DenseSet<LiveInterval*> alreadySplit;
251
252 bool worthTryingToSplit(LiveInterval *li) const {
253 return (!alreadySplit.count(li) && li->getNumValNums() > 1);
254 }
255
256 /// Try to break a LiveInterval into its component values.
Jakob Stoklund Olesen0a2b2a12010-08-13 22:56:53 +0000257 std::vector<LiveInterval*> tryVNISplit(LiveInterval *li) {
Lang Hames61945692009-12-09 05:39:12 +0000258
David Greene65de5042010-01-05 01:25:55 +0000259 DEBUG(dbgs() << "Trying VNI split of %reg" << *li << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000260
261 std::vector<LiveInterval*> added;
262 SmallVector<VNInfo*, 4> vnis;
263
264 std::copy(li->vni_begin(), li->vni_end(), std::back_inserter(vnis));
Eric Christopher894339e2010-07-06 18:35:20 +0000265
Lang Hames61945692009-12-09 05:39:12 +0000266 for (SmallVectorImpl<VNInfo*>::iterator vniItr = vnis.begin(),
267 vniEnd = vnis.end(); vniItr != vniEnd; ++vniItr) {
268 VNInfo *vni = *vniItr;
Eric Christopher894339e2010-07-06 18:35:20 +0000269
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000270 // Skip unused VNIs.
271 if (vni->isUnused())
Lang Hames61945692009-12-09 05:39:12 +0000272 continue;
273
David Greene65de5042010-01-05 01:25:55 +0000274 DEBUG(dbgs() << " Extracted Val #" << vni->id << " as ");
Lang Hames61945692009-12-09 05:39:12 +0000275 LiveInterval *splitInterval = extractVNI(li, vni);
Eric Christopher894339e2010-07-06 18:35:20 +0000276
Lang Hames61945692009-12-09 05:39:12 +0000277 if (splitInterval != 0) {
David Greene65de5042010-01-05 01:25:55 +0000278 DEBUG(dbgs() << *splitInterval << "\n");
Lang Hames61945692009-12-09 05:39:12 +0000279 added.push_back(splitInterval);
280 alreadySplit.insert(splitInterval);
Lang Hames61945692009-12-09 05:39:12 +0000281 } else {
David Greene65de5042010-01-05 01:25:55 +0000282 DEBUG(dbgs() << "0\n");
Lang Hames61945692009-12-09 05:39:12 +0000283 }
Eric Christopher894339e2010-07-06 18:35:20 +0000284 }
Lang Hames61945692009-12-09 05:39:12 +0000285
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
Eric Christopher894339e2010-07-06 18:35:20 +0000289 // add it to added and alreadySplit.
Lang Hames61945692009-12-09 05:39:12 +0000290 if (!li->empty()) {
291 added.push_back(li);
292 alreadySplit.insert(li);
Lang Hames61945692009-12-09 05:39:12 +0000293 }
294
295 return added;
296 }
297
298 /// Extract the given value number from the interval.
299 LiveInterval* extractVNI(LiveInterval *li, VNInfo *vni) const {
Lang Hames6e2968c2010-09-25 12:04:16 +0000300 assert((lis->getInstructionFromIndex(vni->def) != 0 || vni->isPHIDef()) &&
301 "Def index not sane?");
Lang Hames61945692009-12-09 05:39:12 +0000302
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000303 // Create a new vreg and live interval, copy VNI ranges over.
Lang Hames61945692009-12-09 05:39:12 +0000304 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
305 unsigned newVReg = mri->createVirtualRegister(trc);
306 vrm->grow();
307 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
308 VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator());
309
Eric Christopher894339e2010-07-06 18:35:20 +0000310 // Start by copying all live ranges in the VN to the new interval.
Lang Hames61945692009-12-09 05:39:12 +0000311 for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end();
312 rItr != rEnd; ++rItr) {
313 if (rItr->valno == vni) {
314 newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI));
315 }
316 }
317
Eric Christopher894339e2010-07-06 18:35:20 +0000318 // Erase the old VNI & ranges.
Lang Hames61945692009-12-09 05:39:12 +0000319 li->removeValNo(vni);
320
321 // Collect all current uses of the register belonging to the given VNI.
322 // We'll use this to rename the register after we've dealt with the def.
323 std::set<MachineInstr*> uses;
324 for (MachineRegisterInfo::use_iterator
325 useItr = mri->use_begin(li->reg), useEnd = mri->use_end();
326 useItr != useEnd; ++useItr) {
327 uses.insert(&*useItr);
328 }
329
330 // Process the def instruction for this VNI.
331 if (newVNI->isPHIDef()) {
332 // Insert a copy at the start of the MBB. The range proceeding the
333 // copy will be attached to the original LiveInterval.
334 MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000335 MachineInstr *copyMI = BuildMI(*defMBB, defMBB->begin(), DebugLoc(),
336 tii->get(TargetOpcode::COPY), newVReg)
337 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000338 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames6e2968c2010-09-25 12:04:16 +0000339 SlotIndex phiDefIdx = lis->getMBBStartIdx(defMBB);
340 assert(lis->getInstructionFromIndex(phiDefIdx) == 0 &&
341 "PHI def index points at actual instruction.");
342 VNInfo *phiDefVNI = li->getNextValue(phiDefIdx,
343 0, lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000344 phiDefVNI->setIsPHIDef(true);
Lang Hames61945692009-12-09 05:39:12 +0000345 li->addRange(LiveRange(phiDefVNI->def, copyIdx.getDefIndex(), phiDefVNI));
346 LiveRange *oldPHIDefRange =
347 newLI->getLiveRangeContaining(lis->getMBBStartIdx(defMBB));
348
349 // If the old phi def starts in the middle of the range chop it up.
350 if (oldPHIDefRange->start < lis->getMBBStartIdx(defMBB)) {
351 LiveRange oldPHIDefRange2(copyIdx.getDefIndex(), oldPHIDefRange->end,
352 oldPHIDefRange->valno);
353 oldPHIDefRange->end = lis->getMBBStartIdx(defMBB);
354 newLI->addRange(oldPHIDefRange2);
355 } else if (oldPHIDefRange->start == lis->getMBBStartIdx(defMBB)) {
356 // Otherwise if it's at the start of the range just trim it.
357 oldPHIDefRange->start = copyIdx.getDefIndex();
358 } else {
359 assert(false && "PHI def range doesn't cover PHI def?");
360 }
361
362 newVNI->def = copyIdx.getDefIndex();
363 newVNI->setCopy(copyMI);
364 newVNI->setIsPHIDef(false); // not a PHI def anymore.
Lang Hames61945692009-12-09 05:39:12 +0000365 } else {
Eric Christopher894339e2010-07-06 18:35:20 +0000366 // non-PHI def. Rename the def. If it's two-addr that means renaming the
367 // use and inserting a new copy too.
Lang Hames61945692009-12-09 05:39:12 +0000368 MachineInstr *defInst = lis->getInstructionFromIndex(newVNI->def);
369 // We'll rename this now, so we can remove it from uses.
370 uses.erase(defInst);
371 unsigned defOpIdx = defInst->findRegisterDefOperandIdx(li->reg);
372 bool isTwoAddr = defInst->isRegTiedToUseOperand(defOpIdx),
373 twoAddrUseIsUndef = false;
374
375 for (unsigned i = 0; i < defInst->getNumOperands(); ++i) {
376 MachineOperand &mo = defInst->getOperand(i);
377 if (mo.isReg() && (mo.isDef() || isTwoAddr) && (mo.getReg()==li->reg)) {
378 mo.setReg(newVReg);
379 if (isTwoAddr && mo.isUse() && mo.isUndef())
380 twoAddrUseIsUndef = true;
381 }
382 }
Eric Christopher894339e2010-07-06 18:35:20 +0000383
Lang Hames61945692009-12-09 05:39:12 +0000384 SlotIndex defIdx = lis->getInstructionIndex(defInst);
385 newVNI->def = defIdx.getDefIndex();
386
387 if (isTwoAddr && !twoAddrUseIsUndef) {
388 MachineBasicBlock *defMBB = defInst->getParent();
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000389 MachineInstr *copyMI = BuildMI(*defMBB, defInst, DebugLoc(),
390 tii->get(TargetOpcode::COPY), newVReg)
391 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000392 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000393 LiveRange *origUseRange =
394 li->getLiveRangeContaining(newVNI->def.getUseIndex());
Lang Hames61945692009-12-09 05:39:12 +0000395 origUseRange->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000396 VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI,
Lang Hames6e2968c2010-09-25 12:04:16 +0000397 lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000398 LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI);
399 newLI->addRange(copyRange);
Eric Christopher894339e2010-07-06 18:35:20 +0000400 }
Lang Hames61945692009-12-09 05:39:12 +0000401 }
Eric Christopher894339e2010-07-06 18:35:20 +0000402
Lang Hames61945692009-12-09 05:39:12 +0000403 for (std::set<MachineInstr*>::iterator
404 usesItr = uses.begin(), usesEnd = uses.end();
405 usesItr != usesEnd; ++usesItr) {
406 MachineInstr *useInst = *usesItr;
407 SlotIndex useIdx = lis->getInstructionIndex(useInst);
408 LiveRange *useRange =
409 newLI->getLiveRangeContaining(useIdx.getUseIndex());
410
411 // If this use doesn't belong to the new interval skip it.
412 if (useRange == 0)
413 continue;
414
415 // This use doesn't belong to the VNI, skip it.
416 if (useRange->valno != newVNI)
417 continue;
418
419 // Check if this instr is two address.
420 unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg);
421 bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx);
Eric Christopher894339e2010-07-06 18:35:20 +0000422
Lang Hames61945692009-12-09 05:39:12 +0000423 // Rename uses (and defs for two-address instrs).
424 for (unsigned i = 0; i < useInst->getNumOperands(); ++i) {
425 MachineOperand &mo = useInst->getOperand(i);
426 if (mo.isReg() && (mo.isUse() || isTwoAddress) &&
427 (mo.getReg() == li->reg)) {
428 mo.setReg(newVReg);
429 }
430 }
431
432 // If this is a two address instruction we've got some extra work to do.
433 if (isTwoAddress) {
434 // We modified the def operand, so we need to copy back to the original
435 // reg.
436 MachineBasicBlock *useMBB = useInst->getParent();
437 MachineBasicBlock::iterator useItr(useInst);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000438 MachineInstr *copyMI = BuildMI(*useMBB, llvm::next(useItr), DebugLoc(),
439 tii->get(TargetOpcode::COPY), newVReg)
440 .addReg(li->reg, RegState::Kill);
Lang Hames61945692009-12-09 05:39:12 +0000441 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
442
443 // Change the old two-address defined range & vni to start at
444 // (and be defined by) the copy.
445 LiveRange *origDefRange =
446 li->getLiveRangeContaining(useIdx.getDefIndex());
447 origDefRange->start = copyIdx.getDefIndex();
448 origDefRange->valno->def = copyIdx.getDefIndex();
449 origDefRange->valno->setCopy(copyMI);
450
451 // Insert a new range & vni for the two-address-to-copy value. This
452 // will be attached to the new live interval.
453 VNInfo *copyVNI =
Lang Hames6e2968c2010-09-25 12:04:16 +0000454 newLI->getNextValue(useIdx.getDefIndex(), 0,
Lang Hames61945692009-12-09 05:39:12 +0000455 lis->getVNInfoAllocator());
Lang Hames61945692009-12-09 05:39:12 +0000456 LiveRange copyRange(useIdx.getDefIndex(),copyIdx.getDefIndex(),copyVNI);
457 newLI->addRange(copyRange);
458 }
459 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000460
Lang Hames61945692009-12-09 05:39:12 +0000461 // Iterate over any PHI kills - we'll need to insert new copies for them.
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000462 for (LiveInterval::iterator LRI = newLI->begin(), LRE = newLI->end();
463 LRI != LRE; ++LRI) {
Jakob Stoklund Olesen7c78e262010-09-25 00:45:15 +0000464 if (LRI->valno != newVNI)
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000465 continue;
466 SlotIndex killIdx = LRI->end;
467 MachineBasicBlock *killMBB = lis->getMBBFromIndex(killIdx);
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000468 MachineInstr *copyMI = BuildMI(*killMBB, killMBB->getFirstTerminator(),
469 DebugLoc(), tii->get(TargetOpcode::COPY),
470 li->reg)
471 .addReg(newVReg, RegState::Kill);
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000472 SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI);
Lang Hames61945692009-12-09 05:39:12 +0000473
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000474 // Save the current end. We may need it to add a new range if the
475 // current range runs of the end of the MBB.
476 SlotIndex newKillRangeEnd = LRI->end;
477 LRI->end = copyIdx.getDefIndex();
Lang Hames61945692009-12-09 05:39:12 +0000478
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000479 if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) {
480 assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) &&
481 "PHI kill range doesn't reach kill-block end. Not sane.");
482 newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB),
483 newKillRangeEnd, newVNI));
Lang Hames61945692009-12-09 05:39:12 +0000484 }
485
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000486 VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(),
Lang Hames6e2968c2010-09-25 12:04:16 +0000487 copyMI, lis->getVNInfoAllocator());
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000488 newKillVNI->setHasPHIKill(true);
489 li->addRange(LiveRange(copyIdx.getDefIndex(),
490 lis->getMBBEndIdx(killMBB),
491 newKillVNI));
Lang Hames61945692009-12-09 05:39:12 +0000492 }
Lang Hames61945692009-12-09 05:39:12 +0000493 newVNI->setHasPHIKill(false);
494
495 return newLI;
496 }
497
498};
499
Chris Lattner1ca65312010-04-07 22:44:07 +0000500} // end anonymous namespace
501
Lang Hamese2b201b2009-05-18 19:03:16 +0000502
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000503namespace llvm {
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000504Spiller *createInlineSpiller(MachineFunctionPass &pass,
505 MachineFunction &mf,
506 VirtRegMap &vrm);
Jakob Stoklund Olesen914f2ff2010-06-29 23:58:39 +0000507}
508
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000509llvm::Spiller* llvm::createSpiller(MachineFunctionPass &pass,
510 MachineFunction &mf,
511 VirtRegMap &vrm) {
Lang Hames835ca072009-11-19 04:15:33 +0000512 switch (spillerOpt) {
Chris Lattner1ca65312010-04-07 22:44:07 +0000513 default: assert(0 && "unknown spiller");
Jakob Stoklund Olesenf2c6e362010-07-20 23:50:15 +0000514 case trivial: return new TrivialSpiller(pass, mf, vrm);
515 case standard: return new StandardSpiller(pass, mf, vrm);
516 case splitting: return new SplittingSpiller(pass, mf, vrm);
517 case inline_: return createInlineSpiller(pass, mf, vrm);
Lang Hames835ca072009-11-19 04:15:33 +0000518 }
Lang Hamese2b201b2009-05-18 19:03:16 +0000519}