blob: 919a0ce160f41390934171a005147a8c385be436 [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
81
82 /// 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.
86 unsigned insertStoreFor(MachineInstr *mi, unsigned ss,
87 unsigned vreg,
88 const TargetRegisterClass *trc) {
Lang Hames10382fb2009-06-19 02:17:53 +000089
Lang Hames857c4e02009-06-17 21:01:20 +000090 MachineBasicBlock::iterator nextInstItr(mi);
91 ++nextInstItr;
92
93 unsigned miIdx = makeSpaceAfter(mi);
94
95 tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, vreg,
Lang Hamesf41538d2009-06-02 16:53:25 +000096 true, ss, trc);
97 MachineBasicBlock::iterator storeInstItr(mi);
98 ++storeInstItr;
99 MachineInstr *storeInst = &*storeInstItr;
100 unsigned storeInstIdx = miIdx + LiveInterval::InstrSlots::NUM;
101
102 assert(lis->getInstructionFromIndex(storeInstIdx) == 0 &&
103 "Store inst index already in use.");
104
105 lis->InsertMachineInstrInMaps(storeInst, storeInstIdx);
106
107 return storeInstIdx;
108 }
109
Lang Hames10382fb2009-06-19 02:17:53 +0000110 void insertStoreOnInterval(LiveInterval *li,
111 MachineInstr *mi, unsigned ss,
112 unsigned vreg,
113 const TargetRegisterClass *trc) {
114
115 unsigned storeInstIdx = insertStoreFor(mi, ss, vreg, trc);
116 unsigned start = lis->getDefIndex(lis->getInstructionIndex(mi)),
117 end = lis->getUseIndex(storeInstIdx);
118
119 VNInfo *vni =
120 li->getNextValue(storeInstIdx, 0, true, lis->getVNInfoAllocator());
121 vni->kills.push_back(storeInstIdx);
122 LiveRange lr(start, end, vni);
123
124 li->addRange(lr);
125 }
126
Lang Hamesf41538d2009-06-02 16:53:25 +0000127 /// Insert a load of the given veg from the given stack slot immediately
128 /// before the given instruction. Returns the base index of the inserted
129 /// instruction. The caller is responsible for adding an appropriate
130 /// LiveInterval to the LiveIntervals analysis.
131 unsigned insertLoadFor(MachineInstr *mi, unsigned ss,
Lang Hames857c4e02009-06-17 21:01:20 +0000132 unsigned vreg,
Lang Hamesf41538d2009-06-02 16:53:25 +0000133 const TargetRegisterClass *trc) {
134 MachineBasicBlock::iterator useInstItr(mi);
Lang Hames857c4e02009-06-17 21:01:20 +0000135
136 unsigned miIdx = makeSpaceBefore(mi);
137
138 tii->loadRegFromStackSlot(*mi->getParent(), useInstItr, vreg, ss, trc);
Lang Hamesf41538d2009-06-02 16:53:25 +0000139 MachineBasicBlock::iterator loadInstItr(mi);
140 --loadInstItr;
141 MachineInstr *loadInst = &*loadInstItr;
142 unsigned loadInstIdx = miIdx - LiveInterval::InstrSlots::NUM;
143
144 assert(lis->getInstructionFromIndex(loadInstIdx) == 0 &&
145 "Load inst index already in use.");
146
147 lis->InsertMachineInstrInMaps(loadInst, loadInstIdx);
148
149 return loadInstIdx;
150 }
151
Lang Hames10382fb2009-06-19 02:17:53 +0000152 void insertLoadOnInterval(LiveInterval *li,
153 MachineInstr *mi, unsigned ss,
154 unsigned vreg,
155 const TargetRegisterClass *trc) {
156
157 unsigned loadInstIdx = insertLoadFor(mi, ss, vreg, trc);
158 unsigned start = lis->getDefIndex(loadInstIdx),
159 end = lis->getUseIndex(lis->getInstructionIndex(mi));
160
161 VNInfo *vni =
162 li->getNextValue(loadInstIdx, 0, true, lis->getVNInfoAllocator());
163 vni->kills.push_back(lis->getInstructionIndex(mi));
164 LiveRange lr(start, end, vni);
165
166 li->addRange(lr);
167 }
168
169
170
Lang Hamesf41538d2009-06-02 16:53:25 +0000171 /// Add spill ranges for every use/def of the live interval, inserting loads
172 /// immediately before each use, and stores after each def. No folding is
173 /// attempted.
174 std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) {
175 DOUT << "Spilling everywhere " << *li << "\n";
Lang Hamese2b201b2009-05-18 19:03:16 +0000176
177 assert(li->weight != HUGE_VALF &&
178 "Attempting to spill already spilled value.");
179
180 assert(!li->isStackSlot() &&
181 "Trying to spill a stack slot.");
182
183 std::vector<LiveInterval*> added;
184
185 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +0000186 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
187
Lang Hamesf41538d2009-06-02 16:53:25 +0000188 for (MachineRegisterInfo::reg_iterator
189 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +0000190
191 MachineInstr *mi = &*regItr;
Lang Hamesf41538d2009-06-02 16:53:25 +0000192 do {
193 ++regItr;
194 } while (regItr != mri->reg_end() && (&*regItr == mi));
195
Lang Hamese2b201b2009-05-18 19:03:16 +0000196 SmallVector<unsigned, 2> indices;
197 bool hasUse = false;
198 bool hasDef = false;
199
200 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
201 MachineOperand &op = mi->getOperand(i);
202
203 if (!op.isReg() || op.getReg() != li->reg)
204 continue;
205
206 hasUse |= mi->getOperand(i).isUse();
207 hasDef |= mi->getOperand(i).isDef();
208
209 indices.push_back(i);
210 }
211
212 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000213 vrm->grow();
214 vrm->assignVirt2StackSlot(newVReg, ss);
215
Lang Hamesf41538d2009-06-02 16:53:25 +0000216 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
217 newLI->weight = HUGE_VALF;
218
Lang Hamese2b201b2009-05-18 19:03:16 +0000219 for (unsigned i = 0; i < indices.size(); ++i) {
220 mi->getOperand(indices[i]).setReg(newVReg);
221
222 if (mi->getOperand(indices[i]).isUse()) {
223 mi->getOperand(indices[i]).setIsKill(true);
224 }
225 }
226
Lang Hamesf41538d2009-06-02 16:53:25 +0000227 assert(hasUse || hasDef);
228
Lang Hamese2b201b2009-05-18 19:03:16 +0000229 if (hasUse) {
Lang Hames10382fb2009-06-19 02:17:53 +0000230 insertLoadOnInterval(newLI, mi, ss, newVReg, trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000231 }
232
233 if (hasDef) {
Lang Hames10382fb2009-06-19 02:17:53 +0000234 insertStoreOnInterval(newLI, mi, ss, newVReg, trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000235 }
236
Lang Hamesf41538d2009-06-02 16:53:25 +0000237 added.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000238 }
239
Lang Hamese2b201b2009-05-18 19:03:16 +0000240 return added;
241 }
242
Lang Hamesf41538d2009-06-02 16:53:25 +0000243};
Lang Hamese2b201b2009-05-18 19:03:16 +0000244
245
Lang Hamesf41538d2009-06-02 16:53:25 +0000246/// Spills any live range using the spill-everywhere method with no attempt at
247/// folding.
248class TrivialSpiller : public SpillerBase {
249public:
Lang Hames10382fb2009-06-19 02:17:53 +0000250
251 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls,
252 VirtRegMap *vrm) :
Lang Hamesf41538d2009-06-02 16:53:25 +0000253 SpillerBase(mf, lis, ls, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000254
Lang Hamesf41538d2009-06-02 16:53:25 +0000255 std::vector<LiveInterval*> spill(LiveInterval *li) {
256 return trivialSpillEverywhere(li);
Lang Hamese2b201b2009-05-18 19:03:16 +0000257 }
258
Lang Hames10382fb2009-06-19 02:17:53 +0000259 std::vector<LiveInterval*> intraBlockSplit(LiveInterval *li, VNInfo *valno) {
260 std::vector<LiveInterval*> spillIntervals;
261 MachineBasicBlock::iterator storeInsertPoint;
262
263 if (valno->isDefAccurate()) {
264 // If we have an accurate def we can just grab an iterator to the instr
265 // after the def.
266 storeInsertPoint =
267 next(MachineBasicBlock::iterator(lis->getInstructionFromIndex(valno->def)));
268 } else {
269 // If the def info isn't accurate we check if this is a PHI def.
270 // If it is then def holds the index of the defining Basic Block, and we
271 // can use that to get an insertion point.
272 if (valno->isPHIDef()) {
273
274 } else {
275 // We have no usable def info. We can't split this value sensibly.
276 // FIXME: Need sensible feedback for "failure to split", an empty
277 // set of spill intervals could be reasonably returned from a
278 // split where both the store and load are folded.
279 return spillIntervals;
280 }
281 }
282
283
284
285 return spillIntervals;
286 }
287
Lang Hamese2b201b2009-05-18 19:03:16 +0000288};
289
290}
291
Lang Hamese2b201b2009-05-18 19:03:16 +0000292llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hamesf41538d2009-06-02 16:53:25 +0000293 LiveStacks *ls, VirtRegMap *vrm) {
294 return new TrivialSpiller(mf, lis, ls, vrm);
Lang Hamese2b201b2009-05-18 19:03:16 +0000295}