blob: 405cd8087ac5851b1c21d462d1ca9298dd1a1d36 [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"
Lang Hamese2b201b2009-05-18 19:03:16 +000016#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Support/Debug.h"
22
Lang Hamese2b201b2009-05-18 19:03:16 +000023using namespace llvm;
24
25Spiller::~Spiller() {}
26
27namespace {
28
Lang Hamesf41538d2009-06-02 16:53:25 +000029/// Utility class for spillers.
30class SpillerBase : public Spiller {
31protected:
32
33 MachineFunction *mf;
34 LiveIntervals *lis;
35 LiveStacks *ls;
36 MachineFrameInfo *mfi;
37 MachineRegisterInfo *mri;
38 const TargetInstrInfo *tii;
39 VirtRegMap *vrm;
40
41 /// Construct a spiller base.
Lang Hames10382fb2009-06-19 02:17:53 +000042 SpillerBase(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls,
43 VirtRegMap *vrm) :
Lang Hamesf41538d2009-06-02 16:53:25 +000044 mf(mf), lis(lis), ls(ls), vrm(vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000045 {
46 mfi = mf->getFrameInfo();
47 mri = &mf->getRegInfo();
48 tii = mf->getTarget().getInstrInfo();
49 }
50
Lang Hames857c4e02009-06-17 21:01:20 +000051 /// Ensures there is space before the given machine instruction, returns the
52 /// instruction's new number.
53 unsigned makeSpaceBefore(MachineInstr *mi) {
54 if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) {
55 lis->scaleNumbering(2);
56 ls->scaleNumbering(2);
57 }
Lang Hamese2b201b2009-05-18 19:03:16 +000058
Lang Hames857c4e02009-06-17 21:01:20 +000059 unsigned miIdx = lis->getInstructionIndex(mi);
60
61 assert(lis->hasGapBeforeInstr(miIdx));
62
63 return miIdx;
64 }
65
66 /// Ensure there is space after the given machine instruction, returns the
67 /// instruction's new number.
68 unsigned makeSpaceAfter(MachineInstr *mi) {
Lang Hamesf41538d2009-06-02 16:53:25 +000069 if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) {
70 lis->scaleNumbering(2);
71 ls->scaleNumbering(2);
72 }
73
74 unsigned miIdx = lis->getInstructionIndex(mi);
75
76 assert(lis->hasGapAfterInstr(miIdx));
77
Lang Hames857c4e02009-06-17 21:01:20 +000078 return miIdx;
79 }
80
Lang Hames857c4e02009-06-17 21:01:20 +000081 /// Insert a store of the given vreg to the given stack slot immediately
82 /// after the given instruction. Returns the base index of the inserted
83 /// instruction. The caller is responsible for adding an appropriate
84 /// LiveInterval to the LiveIntervals analysis.
Lang Hames6bbc73d2009-06-24 20:46:24 +000085 unsigned insertStoreAfter(MachineInstr *mi, unsigned ss,
Lang Hames857c4e02009-06-17 21:01:20 +000086 unsigned vreg,
87 const TargetRegisterClass *trc) {
Lang Hames10382fb2009-06-19 02:17:53 +000088
Lang Hames6bbc73d2009-06-24 20:46:24 +000089 MachineBasicBlock::iterator nextInstItr(next(mi));
Lang Hames857c4e02009-06-17 21:01:20 +000090
91 unsigned miIdx = makeSpaceAfter(mi);
92
93 tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, vreg,
Lang Hamesf41538d2009-06-02 16:53:25 +000094 true, ss, trc);
Lang Hames6bbc73d2009-06-24 20:46:24 +000095 MachineBasicBlock::iterator storeInstItr(next(mi));
Lang Hamesf41538d2009-06-02 16:53:25 +000096 MachineInstr *storeInst = &*storeInstItr;
97 unsigned storeInstIdx = miIdx + LiveInterval::InstrSlots::NUM;
98
99 assert(lis->getInstructionFromIndex(storeInstIdx) == 0 &&
100 "Store inst index already in use.");
101
102 lis->InsertMachineInstrInMaps(storeInst, storeInstIdx);
103
104 return storeInstIdx;
105 }
106
Lang Hames6bbc73d2009-06-24 20:46:24 +0000107 /// Insert a store of the given vreg to the given stack slot immediately
108 /// before the given instructnion. Returns the base index of the inserted
109 /// Instruction.
110 unsigned insertStoreBefore(MachineInstr *mi, unsigned ss,
111 unsigned vreg,
112 const TargetRegisterClass *trc) {
113 unsigned miIdx = makeSpaceBefore(mi);
114
115 tii->storeRegToStackSlot(*mi->getParent(), mi, vreg, true, ss, trc);
116 MachineBasicBlock::iterator storeInstItr(prior(mi));
117 MachineInstr *storeInst = &*storeInstItr;
118 unsigned storeInstIdx = miIdx - LiveInterval::InstrSlots::NUM;
Lang Hames10382fb2009-06-19 02:17:53 +0000119
Lang Hames6bbc73d2009-06-24 20:46:24 +0000120 assert(lis->getInstructionFromIndex(storeInstIdx) == 0 &&
121 "Store inst index already in use.");
122
123 lis->InsertMachineInstrInMaps(storeInst, storeInstIdx);
124
125 return storeInstIdx;
126 }
127
128 void insertStoreAfterInstOnInterval(LiveInterval *li,
129 MachineInstr *mi, unsigned ss,
130 unsigned vreg,
131 const TargetRegisterClass *trc) {
132
133 unsigned storeInstIdx = insertStoreAfter(mi, ss, vreg, trc);
Lang Hames10382fb2009-06-19 02:17:53 +0000134 unsigned start = lis->getDefIndex(lis->getInstructionIndex(mi)),
135 end = lis->getUseIndex(storeInstIdx);
136
137 VNInfo *vni =
138 li->getNextValue(storeInstIdx, 0, true, lis->getVNInfoAllocator());
139 vni->kills.push_back(storeInstIdx);
Lang Hames6bbc73d2009-06-24 20:46:24 +0000140 DOUT << " Inserting store range: [" << start << ", " << end << ")\n";
Lang Hames10382fb2009-06-19 02:17:53 +0000141 LiveRange lr(start, end, vni);
142
143 li->addRange(lr);
144 }
145
Lang Hames6bbc73d2009-06-24 20:46:24 +0000146 /// Insert a load of the given vreg from the given stack slot immediately
147 /// after the given instruction. Returns the base index of the inserted
148 /// instruction. The caller is responsibel for adding/removing an appropriate
149 /// range vreg's LiveInterval.
150 unsigned insertLoadAfter(MachineInstr *mi, unsigned ss,
151 unsigned vreg,
152 const TargetRegisterClass *trc) {
153
154 MachineBasicBlock::iterator nextInstItr(next(mi));
155
156 unsigned miIdx = makeSpaceAfter(mi);
157
158 tii->loadRegFromStackSlot(*mi->getParent(), nextInstItr, vreg, ss, trc);
159 MachineBasicBlock::iterator loadInstItr(next(mi));
160 MachineInstr *loadInst = &*loadInstItr;
161 unsigned loadInstIdx = miIdx + LiveInterval::InstrSlots::NUM;
162
163 assert(lis->getInstructionFromIndex(loadInstIdx) == 0 &&
164 "Store inst index already in use.");
165
166 lis->InsertMachineInstrInMaps(loadInst, loadInstIdx);
167
168 return loadInstIdx;
169 }
170
171 /// Insert a load of the given vreg from the given stack slot immediately
Lang Hamesf41538d2009-06-02 16:53:25 +0000172 /// before the given instruction. Returns the base index of the inserted
173 /// instruction. The caller is responsible for adding an appropriate
174 /// LiveInterval to the LiveIntervals analysis.
Lang Hames6bbc73d2009-06-24 20:46:24 +0000175 unsigned insertLoadBefore(MachineInstr *mi, unsigned ss,
176 unsigned vreg,
177 const TargetRegisterClass *trc) {
Lang Hames857c4e02009-06-17 21:01:20 +0000178 unsigned miIdx = makeSpaceBefore(mi);
179
Lang Hames6bbc73d2009-06-24 20:46:24 +0000180 tii->loadRegFromStackSlot(*mi->getParent(), mi, vreg, ss, trc);
181 MachineBasicBlock::iterator loadInstItr(prior(mi));
Lang Hamesf41538d2009-06-02 16:53:25 +0000182 MachineInstr *loadInst = &*loadInstItr;
183 unsigned loadInstIdx = miIdx - LiveInterval::InstrSlots::NUM;
184
185 assert(lis->getInstructionFromIndex(loadInstIdx) == 0 &&
186 "Load inst index already in use.");
187
188 lis->InsertMachineInstrInMaps(loadInst, loadInstIdx);
189
190 return loadInstIdx;
191 }
192
Lang Hames6bbc73d2009-06-24 20:46:24 +0000193 void insertLoadBeforeInstOnInterval(LiveInterval *li,
194 MachineInstr *mi, unsigned ss,
195 unsigned vreg,
196 const TargetRegisterClass *trc) {
Lang Hames10382fb2009-06-19 02:17:53 +0000197
Lang Hames6bbc73d2009-06-24 20:46:24 +0000198 unsigned loadInstIdx = insertLoadBefore(mi, ss, vreg, trc);
Lang Hames10382fb2009-06-19 02:17:53 +0000199 unsigned start = lis->getDefIndex(loadInstIdx),
200 end = lis->getUseIndex(lis->getInstructionIndex(mi));
201
202 VNInfo *vni =
203 li->getNextValue(loadInstIdx, 0, true, lis->getVNInfoAllocator());
204 vni->kills.push_back(lis->getInstructionIndex(mi));
Lang Hames6bbc73d2009-06-24 20:46:24 +0000205 DOUT << " Intserting load range: [" << start << ", " << end << ")\n";
Lang Hames10382fb2009-06-19 02:17:53 +0000206 LiveRange lr(start, end, vni);
207
208 li->addRange(lr);
209 }
210
211
212
Lang Hamesf41538d2009-06-02 16:53:25 +0000213 /// Add spill ranges for every use/def of the live interval, inserting loads
214 /// immediately before each use, and stores after each def. No folding is
215 /// attempted.
216 std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) {
217 DOUT << "Spilling everywhere " << *li << "\n";
Lang Hamese2b201b2009-05-18 19:03:16 +0000218
219 assert(li->weight != HUGE_VALF &&
220 "Attempting to spill already spilled value.");
221
222 assert(!li->isStackSlot() &&
223 "Trying to spill a stack slot.");
224
Lang Hames6bbc73d2009-06-24 20:46:24 +0000225 DOUT << "Trivial spill everywhere of reg" << li->reg << "\n";
226
Lang Hamese2b201b2009-05-18 19:03:16 +0000227 std::vector<LiveInterval*> added;
228
229 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +0000230 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
231
Lang Hamesf41538d2009-06-02 16:53:25 +0000232 for (MachineRegisterInfo::reg_iterator
233 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +0000234
235 MachineInstr *mi = &*regItr;
Lang Hames6bbc73d2009-06-24 20:46:24 +0000236
237 DOUT << " Processing " << *mi;
238
Lang Hamesf41538d2009-06-02 16:53:25 +0000239 do {
240 ++regItr;
241 } while (regItr != mri->reg_end() && (&*regItr == mi));
242
Lang Hamese2b201b2009-05-18 19:03:16 +0000243 SmallVector<unsigned, 2> indices;
244 bool hasUse = false;
245 bool hasDef = false;
246
247 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
248 MachineOperand &op = mi->getOperand(i);
249
250 if (!op.isReg() || op.getReg() != li->reg)
251 continue;
252
253 hasUse |= mi->getOperand(i).isUse();
254 hasDef |= mi->getOperand(i).isDef();
255
256 indices.push_back(i);
257 }
258
259 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000260 vrm->grow();
261 vrm->assignVirt2StackSlot(newVReg, ss);
262
Lang Hamesf41538d2009-06-02 16:53:25 +0000263 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
264 newLI->weight = HUGE_VALF;
265
Lang Hamese2b201b2009-05-18 19:03:16 +0000266 for (unsigned i = 0; i < indices.size(); ++i) {
267 mi->getOperand(indices[i]).setReg(newVReg);
268
269 if (mi->getOperand(indices[i]).isUse()) {
270 mi->getOperand(indices[i]).setIsKill(true);
271 }
272 }
273
Lang Hamesf41538d2009-06-02 16:53:25 +0000274 assert(hasUse || hasDef);
275
Lang Hamese2b201b2009-05-18 19:03:16 +0000276 if (hasUse) {
Lang Hames6bbc73d2009-06-24 20:46:24 +0000277 insertLoadBeforeInstOnInterval(newLI, mi, ss, newVReg, trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000278 }
279
280 if (hasDef) {
Lang Hames6bbc73d2009-06-24 20:46:24 +0000281 insertStoreAfterInstOnInterval(newLI, mi, ss, newVReg, trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000282 }
283
Lang Hamesf41538d2009-06-02 16:53:25 +0000284 added.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000285 }
286
Lang Hamese2b201b2009-05-18 19:03:16 +0000287 return added;
288 }
289
Lang Hamesf41538d2009-06-02 16:53:25 +0000290};
Lang Hamese2b201b2009-05-18 19:03:16 +0000291
292
Lang Hamesf41538d2009-06-02 16:53:25 +0000293/// Spills any live range using the spill-everywhere method with no attempt at
294/// folding.
295class TrivialSpiller : public SpillerBase {
296public:
Lang Hames10382fb2009-06-19 02:17:53 +0000297
298 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls,
299 VirtRegMap *vrm) :
Lang Hamesf41538d2009-06-02 16:53:25 +0000300 SpillerBase(mf, lis, ls, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000301
Lang Hamesf41538d2009-06-02 16:53:25 +0000302 std::vector<LiveInterval*> spill(LiveInterval *li) {
303 return trivialSpillEverywhere(li);
Lang Hamese2b201b2009-05-18 19:03:16 +0000304 }
305
Lang Hames10382fb2009-06-19 02:17:53 +0000306 std::vector<LiveInterval*> intraBlockSplit(LiveInterval *li, VNInfo *valno) {
307 std::vector<LiveInterval*> spillIntervals;
Lang Hames6bbc73d2009-06-24 20:46:24 +0000308
309 if (!valno->isDefAccurate() && !valno->isPHIDef()) {
310 // Early out for values which have no well defined def point.
311 return spillIntervals;
312 }
313
314 // Ok.. we should be able to proceed...
315 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
316 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
317 vrm->grow();
318 vrm->assignVirt2StackSlot(li->reg, ss);
319
320 MachineInstr *mi = 0;
321 unsigned storeIdx = 0;
Lang Hames10382fb2009-06-19 02:17:53 +0000322
323 if (valno->isDefAccurate()) {
324 // If we have an accurate def we can just grab an iterator to the instr
325 // after the def.
Lang Hames6bbc73d2009-06-24 20:46:24 +0000326 mi = lis->getInstructionFromIndex(valno->def);
327 storeIdx = insertStoreAfter(mi, ss, li->reg, trc) +
328 LiveInterval::InstrSlots::DEF;
Lang Hames10382fb2009-06-19 02:17:53 +0000329 } else {
Lang Hames6bbc73d2009-06-24 20:46:24 +0000330 // if we get here we have a PHI def.
331 mi = &lis->getMBBFromIndex(valno->def)->front();
332 storeIdx = insertStoreBefore(mi, ss, li->reg, trc) +
333 LiveInterval::InstrSlots::DEF;
Lang Hames10382fb2009-06-19 02:17:53 +0000334 }
335
Lang Hames6bbc73d2009-06-24 20:46:24 +0000336 MachineBasicBlock *defBlock = mi->getParent();
337 unsigned loadIdx = 0;
338
339 // Now we need to find the load...
340 MachineBasicBlock::iterator useItr(mi);
341 for (; !useItr->readsRegister(li->reg); ++useItr) {}
342
343 if (useItr != defBlock->end()) {
344 MachineInstr *loadInst = useItr;
345 loadIdx = insertLoadBefore(loadInst, ss, li->reg, trc) +
346 LiveInterval::InstrSlots::USE;
347 }
348 else {
349 MachineInstr *loadInst = &defBlock->back();
350 loadIdx = insertLoadAfter(loadInst, ss, li->reg, trc) +
351 LiveInterval::InstrSlots::USE;
352 }
353
354 li->removeRange(storeIdx, loadIdx, true);
Lang Hames10382fb2009-06-19 02:17:53 +0000355
356 return spillIntervals;
357 }
358
Lang Hamese2b201b2009-05-18 19:03:16 +0000359};
360
361}
362
Lang Hamese2b201b2009-05-18 19:03:16 +0000363llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hamesf41538d2009-06-02 16:53:25 +0000364 LiveStacks *ls, VirtRegMap *vrm) {
365 return new TrivialSpiller(mf, lis, ls, vrm);
Lang Hamese2b201b2009-05-18 19:03:16 +0000366}