blob: 7e6f18789796666b707a23c4dd6b9e10e7ae65f4 [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"
Lang Hamesf41538d2009-06-02 16:53:25 +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"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Support/Debug.h"
Bill Wendlingc75e7d22009-08-22 20:54:03 +000022#include "llvm/Support/raw_ostream.h"
Lang Hamese2b201b2009-05-18 19:03:16 +000023
Lang Hamese2b201b2009-05-18 19:03:16 +000024using namespace llvm;
25
26Spiller::~Spiller() {}
27
28namespace {
29
Lang Hamesf41538d2009-06-02 16:53:25 +000030/// Utility class for spillers.
31class SpillerBase : public Spiller {
32protected:
33
34 MachineFunction *mf;
35 LiveIntervals *lis;
36 LiveStacks *ls;
37 MachineFrameInfo *mfi;
38 MachineRegisterInfo *mri;
39 const TargetInstrInfo *tii;
40 VirtRegMap *vrm;
41
42 /// Construct a spiller base.
Lang Hames10382fb2009-06-19 02:17:53 +000043 SpillerBase(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls,
44 VirtRegMap *vrm) :
Lang Hamesf41538d2009-06-02 16:53:25 +000045 mf(mf), lis(lis), ls(ls), vrm(vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000046 {
47 mfi = mf->getFrameInfo();
48 mri = &mf->getRegInfo();
49 tii = mf->getTarget().getInstrInfo();
50 }
51
Lang Hames857c4e02009-06-17 21:01:20 +000052 /// Ensures there is space before the given machine instruction, returns the
53 /// instruction's new number.
Lang Hamescc3b0652009-10-03 04:21:37 +000054 LiveIndex makeSpaceBefore(MachineInstr *mi) {
Lang Hames857c4e02009-06-17 21:01:20 +000055 if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) {
56 lis->scaleNumbering(2);
57 ls->scaleNumbering(2);
58 }
Lang Hamese2b201b2009-05-18 19:03:16 +000059
Lang Hamescc3b0652009-10-03 04:21:37 +000060 LiveIndex miIdx = lis->getInstructionIndex(mi);
Lang Hames857c4e02009-06-17 21:01:20 +000061
62 assert(lis->hasGapBeforeInstr(miIdx));
63
64 return miIdx;
65 }
66
67 /// Ensure there is space after the given machine instruction, returns the
68 /// instruction's new number.
Lang Hamescc3b0652009-10-03 04:21:37 +000069 LiveIndex makeSpaceAfter(MachineInstr *mi) {
Lang Hamesf41538d2009-06-02 16:53:25 +000070 if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) {
71 lis->scaleNumbering(2);
72 ls->scaleNumbering(2);
73 }
74
Lang Hamescc3b0652009-10-03 04:21:37 +000075 LiveIndex miIdx = lis->getInstructionIndex(mi);
Lang Hamesf41538d2009-06-02 16:53:25 +000076
77 assert(lis->hasGapAfterInstr(miIdx));
78
Lang Hames857c4e02009-06-17 21:01:20 +000079 return miIdx;
80 }
81
Lang Hames857c4e02009-06-17 21:01:20 +000082 /// Insert a store of the given vreg to the given stack slot immediately
83 /// after the given instruction. Returns the base index of the inserted
84 /// instruction. The caller is responsible for adding an appropriate
85 /// LiveInterval to the LiveIntervals analysis.
Lang Hamescc3b0652009-10-03 04:21:37 +000086 LiveIndex insertStoreAfter(MachineInstr *mi, unsigned ss,
Lang Hames86511252009-09-04 20:41:11 +000087 unsigned vreg,
88 const TargetRegisterClass *trc) {
Lang Hames10382fb2009-06-19 02:17:53 +000089
Lang Hames6bbc73d2009-06-24 20:46:24 +000090 MachineBasicBlock::iterator nextInstItr(next(mi));
Lang Hames857c4e02009-06-17 21:01:20 +000091
Lang Hamescc3b0652009-10-03 04:21:37 +000092 LiveIndex miIdx = makeSpaceAfter(mi);
Lang Hames857c4e02009-06-17 21:01:20 +000093
94 tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, vreg,
Lang Hamesf41538d2009-06-02 16:53:25 +000095 true, ss, trc);
Lang Hames6bbc73d2009-06-24 20:46:24 +000096 MachineBasicBlock::iterator storeInstItr(next(mi));
Lang Hamesf41538d2009-06-02 16:53:25 +000097 MachineInstr *storeInst = &*storeInstItr;
Lang Hamescc3b0652009-10-03 04:21:37 +000098 LiveIndex storeInstIdx = lis->getNextIndex(miIdx);
Lang Hamesf41538d2009-06-02 16:53:25 +000099
100 assert(lis->getInstructionFromIndex(storeInstIdx) == 0 &&
101 "Store inst index already in use.");
102
103 lis->InsertMachineInstrInMaps(storeInst, storeInstIdx);
104
105 return storeInstIdx;
106 }
107
Lang Hames6bbc73d2009-06-24 20:46:24 +0000108 /// Insert a store of the given vreg to the given stack slot immediately
109 /// before the given instructnion. Returns the base index of the inserted
110 /// Instruction.
Lang Hamescc3b0652009-10-03 04:21:37 +0000111 LiveIndex insertStoreBefore(MachineInstr *mi, unsigned ss,
Lang Hames86511252009-09-04 20:41:11 +0000112 unsigned vreg,
113 const TargetRegisterClass *trc) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000114 LiveIndex miIdx = makeSpaceBefore(mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +0000115
116 tii->storeRegToStackSlot(*mi->getParent(), mi, vreg, true, ss, trc);
117 MachineBasicBlock::iterator storeInstItr(prior(mi));
118 MachineInstr *storeInst = &*storeInstItr;
Lang Hamescc3b0652009-10-03 04:21:37 +0000119 LiveIndex storeInstIdx = lis->getPrevIndex(miIdx);
Lang Hames10382fb2009-06-19 02:17:53 +0000120
Lang Hames6bbc73d2009-06-24 20:46:24 +0000121 assert(lis->getInstructionFromIndex(storeInstIdx) == 0 &&
122 "Store inst index already in use.");
123
124 lis->InsertMachineInstrInMaps(storeInst, storeInstIdx);
125
126 return storeInstIdx;
127 }
128
129 void insertStoreAfterInstOnInterval(LiveInterval *li,
130 MachineInstr *mi, unsigned ss,
131 unsigned vreg,
132 const TargetRegisterClass *trc) {
133
Lang Hamescc3b0652009-10-03 04:21:37 +0000134 LiveIndex storeInstIdx = insertStoreAfter(mi, ss, vreg, trc);
135 LiveIndex start = lis->getDefIndex(lis->getInstructionIndex(mi)),
Lang Hames86511252009-09-04 20:41:11 +0000136 end = lis->getUseIndex(storeInstIdx);
Lang Hames10382fb2009-06-19 02:17:53 +0000137
138 VNInfo *vni =
139 li->getNextValue(storeInstIdx, 0, true, lis->getVNInfoAllocator());
Lang Hames86511252009-09-04 20:41:11 +0000140 vni->addKill(storeInstIdx);
Bill Wendlingc75e7d22009-08-22 20:54:03 +0000141 DEBUG(errs() << " Inserting store range: [" << start
142 << ", " << end << ")\n");
Lang Hames10382fb2009-06-19 02:17:53 +0000143 LiveRange lr(start, end, vni);
144
145 li->addRange(lr);
146 }
147
Lang Hames6bbc73d2009-06-24 20:46:24 +0000148 /// Insert a load of the given vreg from the given stack slot immediately
149 /// after the given instruction. Returns the base index of the inserted
150 /// instruction. The caller is responsibel for adding/removing an appropriate
151 /// range vreg's LiveInterval.
Lang Hamescc3b0652009-10-03 04:21:37 +0000152 LiveIndex insertLoadAfter(MachineInstr *mi, unsigned ss,
Lang Hames86511252009-09-04 20:41:11 +0000153 unsigned vreg,
154 const TargetRegisterClass *trc) {
Lang Hames6bbc73d2009-06-24 20:46:24 +0000155
156 MachineBasicBlock::iterator nextInstItr(next(mi));
157
Lang Hamescc3b0652009-10-03 04:21:37 +0000158 LiveIndex miIdx = makeSpaceAfter(mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +0000159
160 tii->loadRegFromStackSlot(*mi->getParent(), nextInstItr, vreg, ss, trc);
161 MachineBasicBlock::iterator loadInstItr(next(mi));
162 MachineInstr *loadInst = &*loadInstItr;
Lang Hamescc3b0652009-10-03 04:21:37 +0000163 LiveIndex loadInstIdx = lis->getNextIndex(miIdx);
Lang Hames6bbc73d2009-06-24 20:46:24 +0000164
165 assert(lis->getInstructionFromIndex(loadInstIdx) == 0 &&
166 "Store inst index already in use.");
167
168 lis->InsertMachineInstrInMaps(loadInst, loadInstIdx);
169
170 return loadInstIdx;
171 }
172
173 /// Insert a load of the given vreg from the given stack slot immediately
Lang Hamesf41538d2009-06-02 16:53:25 +0000174 /// before the given instruction. Returns the base index of the inserted
175 /// instruction. The caller is responsible for adding an appropriate
176 /// LiveInterval to the LiveIntervals analysis.
Lang Hamescc3b0652009-10-03 04:21:37 +0000177 LiveIndex insertLoadBefore(MachineInstr *mi, unsigned ss,
Lang Hames86511252009-09-04 20:41:11 +0000178 unsigned vreg,
179 const TargetRegisterClass *trc) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000180 LiveIndex miIdx = makeSpaceBefore(mi);
Lang Hames857c4e02009-06-17 21:01:20 +0000181
Lang Hames6bbc73d2009-06-24 20:46:24 +0000182 tii->loadRegFromStackSlot(*mi->getParent(), mi, vreg, ss, trc);
183 MachineBasicBlock::iterator loadInstItr(prior(mi));
Lang Hamesf41538d2009-06-02 16:53:25 +0000184 MachineInstr *loadInst = &*loadInstItr;
Lang Hamescc3b0652009-10-03 04:21:37 +0000185 LiveIndex loadInstIdx = lis->getPrevIndex(miIdx);
Lang Hamesf41538d2009-06-02 16:53:25 +0000186
187 assert(lis->getInstructionFromIndex(loadInstIdx) == 0 &&
188 "Load inst index already in use.");
189
190 lis->InsertMachineInstrInMaps(loadInst, loadInstIdx);
191
192 return loadInstIdx;
193 }
194
Lang Hames6bbc73d2009-06-24 20:46:24 +0000195 void insertLoadBeforeInstOnInterval(LiveInterval *li,
196 MachineInstr *mi, unsigned ss,
197 unsigned vreg,
198 const TargetRegisterClass *trc) {
Lang Hames10382fb2009-06-19 02:17:53 +0000199
Lang Hamescc3b0652009-10-03 04:21:37 +0000200 LiveIndex loadInstIdx = insertLoadBefore(mi, ss, vreg, trc);
201 LiveIndex start = lis->getDefIndex(loadInstIdx),
Lang Hames86511252009-09-04 20:41:11 +0000202 end = lis->getUseIndex(lis->getInstructionIndex(mi));
Lang Hames10382fb2009-06-19 02:17:53 +0000203
204 VNInfo *vni =
205 li->getNextValue(loadInstIdx, 0, true, lis->getVNInfoAllocator());
Lang Hames86511252009-09-04 20:41:11 +0000206 vni->addKill(lis->getInstructionIndex(mi));
Bill Wendlingc75e7d22009-08-22 20:54:03 +0000207 DEBUG(errs() << " Intserting load range: [" << start
208 << ", " << end << ")\n");
Lang Hames10382fb2009-06-19 02:17:53 +0000209 LiveRange lr(start, end, vni);
210
211 li->addRange(lr);
212 }
213
214
215
Lang Hamesf41538d2009-06-02 16:53:25 +0000216 /// Add spill ranges for every use/def of the live interval, inserting loads
217 /// immediately before each use, and stores after each def. No folding is
218 /// attempted.
219 std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) {
Bill Wendlingc75e7d22009-08-22 20:54:03 +0000220 DEBUG(errs() << "Spilling everywhere " << *li << "\n");
Lang Hamese2b201b2009-05-18 19:03:16 +0000221
222 assert(li->weight != HUGE_VALF &&
223 "Attempting to spill already spilled value.");
224
225 assert(!li->isStackSlot() &&
226 "Trying to spill a stack slot.");
227
Bill Wendlingc75e7d22009-08-22 20:54:03 +0000228 DEBUG(errs() << "Trivial spill everywhere of reg" << li->reg << "\n");
Lang Hames6bbc73d2009-06-24 20:46:24 +0000229
Lang Hamese2b201b2009-05-18 19:03:16 +0000230 std::vector<LiveInterval*> added;
231
232 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +0000233 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
234
Lang Hamesf41538d2009-06-02 16:53:25 +0000235 for (MachineRegisterInfo::reg_iterator
236 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +0000237
238 MachineInstr *mi = &*regItr;
Lang Hames6bbc73d2009-06-24 20:46:24 +0000239
Bill Wendlingc75e7d22009-08-22 20:54:03 +0000240 DEBUG(errs() << " Processing " << *mi);
Lang Hames6bbc73d2009-06-24 20:46:24 +0000241
Lang Hamesf41538d2009-06-02 16:53:25 +0000242 do {
243 ++regItr;
244 } while (regItr != mri->reg_end() && (&*regItr == mi));
245
Lang Hamese2b201b2009-05-18 19:03:16 +0000246 SmallVector<unsigned, 2> indices;
247 bool hasUse = false;
248 bool hasDef = false;
249
250 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
251 MachineOperand &op = mi->getOperand(i);
252
253 if (!op.isReg() || op.getReg() != li->reg)
254 continue;
255
256 hasUse |= mi->getOperand(i).isUse();
257 hasDef |= mi->getOperand(i).isDef();
258
259 indices.push_back(i);
260 }
261
262 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000263 vrm->grow();
264 vrm->assignVirt2StackSlot(newVReg, ss);
265
Lang Hamesf41538d2009-06-02 16:53:25 +0000266 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
267 newLI->weight = HUGE_VALF;
268
Lang Hamese2b201b2009-05-18 19:03:16 +0000269 for (unsigned i = 0; i < indices.size(); ++i) {
270 mi->getOperand(indices[i]).setReg(newVReg);
271
272 if (mi->getOperand(indices[i]).isUse()) {
273 mi->getOperand(indices[i]).setIsKill(true);
274 }
275 }
276
Lang Hamesf41538d2009-06-02 16:53:25 +0000277 assert(hasUse || hasDef);
278
Lang Hamese2b201b2009-05-18 19:03:16 +0000279 if (hasUse) {
Lang Hames6bbc73d2009-06-24 20:46:24 +0000280 insertLoadBeforeInstOnInterval(newLI, mi, ss, newVReg, trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000281 }
282
283 if (hasDef) {
Lang Hames6bbc73d2009-06-24 20:46:24 +0000284 insertStoreAfterInstOnInterval(newLI, mi, ss, newVReg, trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000285 }
286
Lang Hamesf41538d2009-06-02 16:53:25 +0000287 added.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000288 }
289
Lang Hamese2b201b2009-05-18 19:03:16 +0000290 return added;
291 }
292
Lang Hamesf41538d2009-06-02 16:53:25 +0000293};
Lang Hamese2b201b2009-05-18 19:03:16 +0000294
295
Lang Hamesf41538d2009-06-02 16:53:25 +0000296/// Spills any live range using the spill-everywhere method with no attempt at
297/// folding.
298class TrivialSpiller : public SpillerBase {
299public:
Lang Hames10382fb2009-06-19 02:17:53 +0000300
301 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls,
302 VirtRegMap *vrm) :
Lang Hamesf41538d2009-06-02 16:53:25 +0000303 SpillerBase(mf, lis, ls, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000304
Lang Hamesf41538d2009-06-02 16:53:25 +0000305 std::vector<LiveInterval*> spill(LiveInterval *li) {
306 return trivialSpillEverywhere(li);
Lang Hamese2b201b2009-05-18 19:03:16 +0000307 }
308
Lang Hames10382fb2009-06-19 02:17:53 +0000309 std::vector<LiveInterval*> intraBlockSplit(LiveInterval *li, VNInfo *valno) {
310 std::vector<LiveInterval*> spillIntervals;
Lang Hames6bbc73d2009-06-24 20:46:24 +0000311
312 if (!valno->isDefAccurate() && !valno->isPHIDef()) {
313 // Early out for values which have no well defined def point.
314 return spillIntervals;
315 }
316
317 // Ok.. we should be able to proceed...
318 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
319 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
320 vrm->grow();
321 vrm->assignVirt2StackSlot(li->reg, ss);
322
323 MachineInstr *mi = 0;
Lang Hamescc3b0652009-10-03 04:21:37 +0000324 LiveIndex storeIdx = MachineInstrIndex();
Lang Hames10382fb2009-06-19 02:17:53 +0000325
326 if (valno->isDefAccurate()) {
327 // If we have an accurate def we can just grab an iterator to the instr
328 // after the def.
Lang Hames6bbc73d2009-06-24 20:46:24 +0000329 mi = lis->getInstructionFromIndex(valno->def);
Lang Hames86511252009-09-04 20:41:11 +0000330 storeIdx = lis->getDefIndex(insertStoreAfter(mi, ss, li->reg, trc));
Lang Hames10382fb2009-06-19 02:17:53 +0000331 } else {
Lang Hames6bbc73d2009-06-24 20:46:24 +0000332 // if we get here we have a PHI def.
333 mi = &lis->getMBBFromIndex(valno->def)->front();
Lang Hames86511252009-09-04 20:41:11 +0000334 storeIdx = lis->getDefIndex(insertStoreBefore(mi, ss, li->reg, trc));
Lang Hames10382fb2009-06-19 02:17:53 +0000335 }
336
Lang Hames6bbc73d2009-06-24 20:46:24 +0000337 MachineBasicBlock *defBlock = mi->getParent();
Lang Hamescc3b0652009-10-03 04:21:37 +0000338 LiveIndex loadIdx = MachineInstrIndex();
Lang Hames6bbc73d2009-06-24 20:46:24 +0000339
340 // Now we need to find the load...
341 MachineBasicBlock::iterator useItr(mi);
342 for (; !useItr->readsRegister(li->reg); ++useItr) {}
343
344 if (useItr != defBlock->end()) {
345 MachineInstr *loadInst = useItr;
Lang Hames86511252009-09-04 20:41:11 +0000346 loadIdx = lis->getUseIndex(insertLoadBefore(loadInst, ss, li->reg, trc));
Lang Hames6bbc73d2009-06-24 20:46:24 +0000347 }
348 else {
349 MachineInstr *loadInst = &defBlock->back();
Lang Hames86511252009-09-04 20:41:11 +0000350 loadIdx = lis->getUseIndex(insertLoadAfter(loadInst, ss, li->reg, trc));
Lang Hames6bbc73d2009-06-24 20:46:24 +0000351 }
352
353 li->removeRange(storeIdx, loadIdx, true);
Lang Hames10382fb2009-06-19 02:17:53 +0000354
355 return spillIntervals;
356 }
357
Lang Hamese2b201b2009-05-18 19:03:16 +0000358};
359
360}
361
Lang Hamese2b201b2009-05-18 19:03:16 +0000362llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hamesf41538d2009-06-02 16:53:25 +0000363 LiveStacks *ls, VirtRegMap *vrm) {
364 return new TrivialSpiller(mf, lis, ls, vrm);
Lang Hamese2b201b2009-05-18 19:03:16 +0000365}