blob: ce63121251e3248bb62fcbb440acbe1f4fc6110e [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.
42 SpillerBase(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, VirtRegMap *vrm) :
43 mf(mf), lis(lis), ls(ls), vrm(vrm)
Lang Hamese2b201b2009-05-18 19:03:16 +000044 {
45 mfi = mf->getFrameInfo();
46 mri = &mf->getRegInfo();
47 tii = mf->getTarget().getInstrInfo();
48 }
49
Lang Hamesf41538d2009-06-02 16:53:25 +000050 /// Insert a store of the given vreg to the given stack slot immediately
51 /// after the given instruction. Returns the base index of the inserted
52 /// instruction. The caller is responsible for adding an appropriate
53 /// LiveInterval to the LiveIntervals analysis.
54 unsigned insertStoreFor(MachineInstr *mi, unsigned ss,
55 unsigned newVReg,
56 const TargetRegisterClass *trc) {
57 MachineBasicBlock::iterator nextInstItr(mi);
58 ++nextInstItr;
Lang Hamese2b201b2009-05-18 19:03:16 +000059
Lang Hamesf41538d2009-06-02 16:53:25 +000060 if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) {
61 lis->scaleNumbering(2);
62 ls->scaleNumbering(2);
63 }
64
65 unsigned miIdx = lis->getInstructionIndex(mi);
66
67 assert(lis->hasGapAfterInstr(miIdx));
68
69 tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, newVReg,
70 true, ss, trc);
71 MachineBasicBlock::iterator storeInstItr(mi);
72 ++storeInstItr;
73 MachineInstr *storeInst = &*storeInstItr;
74 unsigned storeInstIdx = miIdx + LiveInterval::InstrSlots::NUM;
75
76 assert(lis->getInstructionFromIndex(storeInstIdx) == 0 &&
77 "Store inst index already in use.");
78
79 lis->InsertMachineInstrInMaps(storeInst, storeInstIdx);
80
81 return storeInstIdx;
82 }
83
84 /// Insert a load of the given veg from the given stack slot immediately
85 /// before the given instruction. Returns the base index of the inserted
86 /// instruction. The caller is responsible for adding an appropriate
87 /// LiveInterval to the LiveIntervals analysis.
88 unsigned insertLoadFor(MachineInstr *mi, unsigned ss,
89 unsigned newVReg,
90 const TargetRegisterClass *trc) {
91 MachineBasicBlock::iterator useInstItr(mi);
92
93 if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) {
94 lis->scaleNumbering(2);
95 ls->scaleNumbering(2);
96 }
97
98 unsigned miIdx = lis->getInstructionIndex(mi);
99
100 assert(lis->hasGapBeforeInstr(miIdx));
101
102 tii->loadRegFromStackSlot(*mi->getParent(), useInstItr, newVReg, ss, trc);
103 MachineBasicBlock::iterator loadInstItr(mi);
104 --loadInstItr;
105 MachineInstr *loadInst = &*loadInstItr;
106 unsigned loadInstIdx = miIdx - LiveInterval::InstrSlots::NUM;
107
108 assert(lis->getInstructionFromIndex(loadInstIdx) == 0 &&
109 "Load inst index already in use.");
110
111 lis->InsertMachineInstrInMaps(loadInst, loadInstIdx);
112
113 return loadInstIdx;
114 }
115
116
117 /// Add spill ranges for every use/def of the live interval, inserting loads
118 /// immediately before each use, and stores after each def. No folding is
119 /// attempted.
120 std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) {
121 DOUT << "Spilling everywhere " << *li << "\n";
Lang Hamese2b201b2009-05-18 19:03:16 +0000122
123 assert(li->weight != HUGE_VALF &&
124 "Attempting to spill already spilled value.");
125
126 assert(!li->isStackSlot() &&
127 "Trying to spill a stack slot.");
128
129 std::vector<LiveInterval*> added;
130
131 const TargetRegisterClass *trc = mri->getRegClass(li->reg);
Lang Hamese2b201b2009-05-18 19:03:16 +0000132 unsigned ss = vrm->assignVirt2StackSlot(li->reg);
133
Lang Hamesf41538d2009-06-02 16:53:25 +0000134 for (MachineRegisterInfo::reg_iterator
135 regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) {
Lang Hamese2b201b2009-05-18 19:03:16 +0000136
137 MachineInstr *mi = &*regItr;
Lang Hamesf41538d2009-06-02 16:53:25 +0000138 do {
139 ++regItr;
140 } while (regItr != mri->reg_end() && (&*regItr == mi));
141
Lang Hamese2b201b2009-05-18 19:03:16 +0000142 SmallVector<unsigned, 2> indices;
143 bool hasUse = false;
144 bool hasDef = false;
145
146 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
147 MachineOperand &op = mi->getOperand(i);
148
149 if (!op.isReg() || op.getReg() != li->reg)
150 continue;
151
152 hasUse |= mi->getOperand(i).isUse();
153 hasDef |= mi->getOperand(i).isDef();
154
155 indices.push_back(i);
156 }
157
158 unsigned newVReg = mri->createVirtualRegister(trc);
Lang Hamese2b201b2009-05-18 19:03:16 +0000159 vrm->grow();
160 vrm->assignVirt2StackSlot(newVReg, ss);
161
Lang Hamesf41538d2009-06-02 16:53:25 +0000162 LiveInterval *newLI = &lis->getOrCreateInterval(newVReg);
163 newLI->weight = HUGE_VALF;
164
Lang Hamese2b201b2009-05-18 19:03:16 +0000165 for (unsigned i = 0; i < indices.size(); ++i) {
166 mi->getOperand(indices[i]).setReg(newVReg);
167
168 if (mi->getOperand(indices[i]).isUse()) {
169 mi->getOperand(indices[i]).setIsKill(true);
170 }
171 }
172
Lang Hamesf41538d2009-06-02 16:53:25 +0000173 assert(hasUse || hasDef);
174
Lang Hamese2b201b2009-05-18 19:03:16 +0000175 if (hasUse) {
176 unsigned loadInstIdx = insertLoadFor(mi, ss, newVReg, trc);
177 unsigned start = lis->getDefIndex(loadInstIdx),
178 end = lis->getUseIndex(lis->getInstructionIndex(mi));
179
180 VNInfo *vni =
181 newLI->getNextValue(loadInstIdx, 0, lis->getVNInfoAllocator());
182 vni->kills.push_back(lis->getInstructionIndex(mi));
183 LiveRange lr(start, end, vni);
184
185 newLI->addRange(lr);
Lang Hamese2b201b2009-05-18 19:03:16 +0000186 }
187
188 if (hasDef) {
189 unsigned storeInstIdx = insertStoreFor(mi, ss, newVReg, trc);
190 unsigned start = lis->getDefIndex(lis->getInstructionIndex(mi)),
191 end = lis->getUseIndex(storeInstIdx);
192
193 VNInfo *vni =
194 newLI->getNextValue(storeInstIdx, 0, lis->getVNInfoAllocator());
195 vni->kills.push_back(storeInstIdx);
196 LiveRange lr(start, end, vni);
197
198 newLI->addRange(lr);
Lang Hamese2b201b2009-05-18 19:03:16 +0000199 }
200
Lang Hamesf41538d2009-06-02 16:53:25 +0000201 added.push_back(newLI);
Lang Hamese2b201b2009-05-18 19:03:16 +0000202 }
203
204
205 return added;
206 }
207
Lang Hamesf41538d2009-06-02 16:53:25 +0000208};
Lang Hamese2b201b2009-05-18 19:03:16 +0000209
210
Lang Hamesf41538d2009-06-02 16:53:25 +0000211/// Spills any live range using the spill-everywhere method with no attempt at
212/// folding.
213class TrivialSpiller : public SpillerBase {
214public:
215 TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, VirtRegMap *vrm) :
216 SpillerBase(mf, lis, ls, vrm) {}
Lang Hamese2b201b2009-05-18 19:03:16 +0000217
Lang Hamesf41538d2009-06-02 16:53:25 +0000218 std::vector<LiveInterval*> spill(LiveInterval *li) {
219 return trivialSpillEverywhere(li);
Lang Hamese2b201b2009-05-18 19:03:16 +0000220 }
221
222};
223
224}
225
Lang Hamese2b201b2009-05-18 19:03:16 +0000226llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis,
Lang Hamesf41538d2009-06-02 16:53:25 +0000227 LiveStacks *ls, VirtRegMap *vrm) {
228 return new TrivialSpiller(mf, lis, ls, vrm);
Lang Hamese2b201b2009-05-18 19:03:16 +0000229}