blob: a4d0733e88e64dbdb737451d5bbf4c538a92e134 [file] [log] [blame]
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00001//===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner8c4d88d2004-09-30 01:54:45 +000010// This file implements the VirtRegMap class.
11//
12// It also contains implementations of the the Spiller interface, which, given a
13// virtual register map and a machine function, eliminates all virtual
14// references by replacing them with physical register references - adding spill
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000015// code as necessary.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000016//
17//===----------------------------------------------------------------------===//
18
Chris Lattner8c4d88d2004-09-30 01:54:45 +000019#define DEBUG_TYPE "spiller"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000020#include "VirtRegMap.h"
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000021#include "llvm/Function.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner8c4d88d2004-09-30 01:54:45 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/SSARegMap.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000025#include "llvm/Target/TargetMachine.h"
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000026#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000031using namespace llvm;
32
33namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000034 Statistic<> NumSpills("spiller", "Number of register spills");
35 Statistic<> NumStores("spiller", "Number of stores added");
36 Statistic<> NumLoads ("spiller", "Number of loads added");
Chris Lattner7fb64342004-10-01 19:04:51 +000037 Statistic<> NumReused("spiller", "Number of values reused");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000038
Chris Lattner8c4d88d2004-09-30 01:54:45 +000039 enum SpillerName { simple, local };
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000040
Chris Lattner8c4d88d2004-09-30 01:54:45 +000041 cl::opt<SpillerName>
42 SpillerOpt("spiller",
Chris Lattner7fb64342004-10-01 19:04:51 +000043 cl::desc("Spiller to use: (default: local)"),
Chris Lattner8c4d88d2004-09-30 01:54:45 +000044 cl::Prefix,
45 cl::values(clEnumVal(simple, " simple spiller"),
46 clEnumVal(local, " local spiller"),
47 clEnumValEnd),
Chris Lattner7fb64342004-10-01 19:04:51 +000048 cl::init(local));
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000049}
50
Chris Lattner8c4d88d2004-09-30 01:54:45 +000051//===----------------------------------------------------------------------===//
52// VirtRegMap implementation
53//===----------------------------------------------------------------------===//
54
55void VirtRegMap::grow() {
Chris Lattner7f690e62004-09-30 02:15:18 +000056 Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
57 Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000058}
59
Chris Lattner8c4d88d2004-09-30 01:54:45 +000060int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
61 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000062 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000063 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000064 const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
65 int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
66 RC->getAlignment());
67 Virt2StackSlotMap[virtReg] = frameIndex;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000068 ++NumSpills;
69 return frameIndex;
70}
71
72void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
73 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000074 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000075 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000076 Virt2StackSlotMap[virtReg] = frameIndex;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +000077}
78
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000079void VirtRegMap::virtFolded(unsigned virtReg,
80 MachineInstr* oldMI,
Chris Lattner8c4d88d2004-09-30 01:54:45 +000081 MachineInstr* newMI) {
82 // move previous memory references folded to new instruction
Chris Lattner7f690e62004-09-30 02:15:18 +000083 std::vector<MI2VirtMapTy::mapped_type> regs;
Chris Lattnerdbea9732004-09-30 16:35:08 +000084 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(oldMI),
85 E = MI2VirtMap.end(); I != E && I->first == oldMI; ) {
86 regs.push_back(I->second);
87 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +000088 }
Chris Lattnerdbea9732004-09-30 16:35:08 +000089
90 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(newMI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +000091 for (unsigned i = 0, e = regs.size(); i != e; ++i)
Chris Lattnerdbea9732004-09-30 16:35:08 +000092 MI2VirtMap.insert(IP, std::make_pair(newMI, regs[i]));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000093
Chris Lattner8c4d88d2004-09-30 01:54:45 +000094 // add new memory reference
Chris Lattnerdbea9732004-09-30 16:35:08 +000095 MI2VirtMap.insert(IP, std::make_pair(newMI, virtReg));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000096}
97
Chris Lattner7f690e62004-09-30 02:15:18 +000098void VirtRegMap::print(std::ostream &OS) const {
99 const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000100
Chris Lattner7f690e62004-09-30 02:15:18 +0000101 OS << "********** REGISTER MAP **********\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000102 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000103 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
104 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
105 OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
106
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000107 }
108
109 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000110 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
111 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
112 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
113 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000114}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000115
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000116void VirtRegMap::dump() const { print(std::cerr); }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000117
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000118
119//===----------------------------------------------------------------------===//
120// Simple Spiller Implementation
121//===----------------------------------------------------------------------===//
122
123Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000124
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000125namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000126 struct SimpleSpiller : public Spiller {
127 bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM);
128 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000129}
130
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000131bool SimpleSpiller::runOnMachineFunction(MachineFunction& MF,
132 const VirtRegMap& VRM) {
133 DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
134 DEBUG(std::cerr << "********** Function: "
135 << MF.getFunction()->getName() << '\n');
136 const TargetMachine& TM = MF.getTarget();
Chris Lattner7f690e62004-09-30 02:15:18 +0000137 const MRegisterInfo& MRI = *TM.getRegisterInfo();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000138
Chris Lattner4ea1b822004-09-30 02:33:48 +0000139 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
140 // each vreg once (in the case where a spilled vreg is used by multiple
141 // operands). This is always smaller than the number of operands to the
142 // current machine instr, so it should be small.
143 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000144
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000145 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
146 MBBI != E; ++MBBI) {
147 DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n");
148 MachineBasicBlock &MBB = *MBBI;
149 for (MachineBasicBlock::iterator MII = MBB.begin(),
150 E = MBB.end(); MII != E; ++MII) {
151 MachineInstr &MI = *MII;
152 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000153 MachineOperand &MO = MI.getOperand(i);
154 if (MO.isRegister() && MO.getReg() &&
155 MRegisterInfo::isVirtualRegister(MO.getReg())) {
156 unsigned VirtReg = MO.getReg();
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000157 unsigned PhysReg = VRM.getPhys(VirtReg);
158 if (VRM.hasStackSlot(VirtReg)) {
Chris Lattner477e4552004-09-30 16:10:45 +0000159 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000160
Chris Lattner7fb64342004-10-01 19:04:51 +0000161 if (MO.isUse() &&
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000162 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
163 == LoadedRegs.end()) {
164 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot);
165 LoadedRegs.push_back(VirtReg);
166 ++NumLoads;
Chris Lattner477e4552004-09-30 16:10:45 +0000167 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000168 }
169
Chris Lattner7fb64342004-10-01 19:04:51 +0000170 if (MO.isDef()) {
171 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot);
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000172 ++NumStores;
173 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000174 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000175 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000176 }
177 }
Chris Lattner477e4552004-09-30 16:10:45 +0000178 DEBUG(std::cerr << '\t' << MI);
Chris Lattner4ea1b822004-09-30 02:33:48 +0000179 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000180 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000181 }
182 return true;
183}
184
185//===----------------------------------------------------------------------===//
186// Local Spiller Implementation
187//===----------------------------------------------------------------------===//
188
189namespace {
Chris Lattner7fb64342004-10-01 19:04:51 +0000190 /// LocalSpiller - This spiller does a simple pass over the machine basic
191 /// block to attempt to keep spills in registers as much as possible for
192 /// blocks that have low register pressure (the vreg may be spilled due to
193 /// register pressure in other blocks).
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000194 class LocalSpiller : public Spiller {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000195 const MRegisterInfo *MRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000196 const TargetInstrInfo *TII;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000197 public:
Chris Lattner7fb64342004-10-01 19:04:51 +0000198 bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) {
199 MRI = MF.getTarget().getRegisterInfo();
200 TII = MF.getTarget().getInstrInfo();
201 DEBUG(std::cerr << "\n**** Local spiller rewriting function '"
202 << MF.getFunction()->getName() << "':\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000203
Chris Lattner7fb64342004-10-01 19:04:51 +0000204 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
205 MBB != E; ++MBB)
206 RewriteMBB(*MBB, VRM);
207 return true;
208 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000209 private:
Chris Lattner7fb64342004-10-01 19:04:51 +0000210 void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM);
211 void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
212 std::map<unsigned, int> &PhysRegs);
213 void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
214 std::map<unsigned, int> &PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000215 };
216}
217
Chris Lattner7fb64342004-10-01 19:04:51 +0000218void LocalSpiller::ClobberPhysRegOnly(unsigned PhysReg,
219 std::map<int, unsigned> &SpillSlots,
220 std::map<unsigned, int> &PhysRegs) {
221 std::map<unsigned, int>::iterator I = PhysRegs.find(PhysReg);
222 if (I != PhysRegs.end()) {
223 int Slot = I->second;
224 PhysRegs.erase(I);
225 assert(SpillSlots[Slot] == PhysReg && "Bidirectional map mismatch!");
226 SpillSlots.erase(Slot);
227 DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
228 << " clobbered, invalidating SS#" << Slot << "\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000229
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000230 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000231}
232
Chris Lattner7fb64342004-10-01 19:04:51 +0000233void LocalSpiller::ClobberPhysReg(unsigned PhysReg,
234 std::map<int, unsigned> &SpillSlots,
235 std::map<unsigned, int> &PhysRegs) {
236 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
237 ClobberPhysRegOnly(*AS, SpillSlots, PhysRegs);
238 ClobberPhysRegOnly(PhysReg, SpillSlots, PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000239}
240
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000241
Chris Lattner7fb64342004-10-01 19:04:51 +0000242// ReusedOp - For each reused operand, we keep track of a bit of information, in
243// case we need to rollback upon processing a new operand. See comments below.
244namespace {
245 struct ReusedOp {
246 // The MachineInstr operand that reused an available value.
247 unsigned Operand;
248
249 // StackSlot - The spill slot of the value being reused.
250 unsigned StackSlot;
251
252 // PhysRegReused - The physical register the value was available in.
253 unsigned PhysRegReused;
254
255 // AssignedPhysReg - The physreg that was assigned for use by the reload.
256 unsigned AssignedPhysReg;
257
258 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr)
259 : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr) {}
260 };
261}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000262
Chris Lattner7fb64342004-10-01 19:04:51 +0000263
264/// rewriteMBB - Keep track of which spills are available even after the
265/// register allocator is done with them. If possible, avoid reloading vregs.
266void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
267
268 // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
269 // register values that are still available, due to being loaded to stored to,
270 // but not invalidated yet.
271 std::map<int, unsigned> SpillSlotsAvailable;
272
273 // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
274 // which physregs are in use holding a stack slot value.
275 std::map<unsigned, int> PhysRegsAvailable;
276
277 DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
278
279 std::vector<ReusedOp> ReusedOperands;
280
281 // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
282 // of it. ".first" is the machine operand index (should always be 0 for now),
283 // and ".second" is the virtual register that is spilled.
284 std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
285
286 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
287 MII != E; ) {
288 MachineInstr &MI = *MII;
289 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
290
291 ReusedOperands.clear();
292 DefAndUseVReg.clear();
293
294 // Process all of the spilled uses and all non spilled reg references.
295 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
296 MachineOperand &MO = MI.getOperand(i);
297 if (MO.isRegister() && MO.getReg() &&
298 MRegisterInfo::isVirtualRegister(MO.getReg())) {
299 unsigned VirtReg = MO.getReg();
300
301 if (!VRM.hasStackSlot(VirtReg)) {
302 // This virtual register was assigned a physreg!
303 MI.SetMachineOperandReg(i, VRM.getPhys(VirtReg));
304 } else {
305 // Is this virtual register a spilled value?
306 if (MO.isUse()) {
307 int StackSlot = VRM.getStackSlot(VirtReg);
308 unsigned PhysReg;
309
310 // Check to see if this stack slot is available.
311 std::map<int, unsigned>::iterator SSI =
312 SpillSlotsAvailable.find(StackSlot);
313 if (SSI != SpillSlotsAvailable.end()) {
314 // If this stack slot value is already available, reuse it!
315 PhysReg = SSI->second;
316 MI.SetMachineOperandReg(i, PhysReg);
317 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
318 << MRI->getName(SSI->second) << "\n");
319
320 // The only technical detail we have is that we don't know that
321 // PhysReg won't be clobbered by a reloaded stack slot that occurs
322 // later in the instruction. In particular, consider 'op V1, V2'.
323 // If V1 is available in physreg R0, we would choose to reuse it
324 // here, instead of reloading it into the register the allocator
325 // indicated (say R1). However, V2 might have to be reloaded
326 // later, and it might indicate that it needs to live in R0. When
327 // this occurs, we need to have information available that
328 // indicates it is safe to use R1 for the reload instead of R0.
329 //
330 // To further complicate matters, we might conflict with an alias,
331 // or R0 and R1 might not be compatible with each other. In this
332 // case, we actually insert a reload for V1 in R1, ensuring that
333 // we can get at R0 or its alias.
334 ReusedOperands.push_back(ReusedOp(i, StackSlot, PhysReg,
335 VRM.getPhys(VirtReg)));
336 ++NumReused;
337 } else {
338 // Otherwise, reload it and remember that we have it.
339 PhysReg = VRM.getPhys(VirtReg);
340
341 // Note that, if we reused a register for a previous operand, the
342 // register we want to reload into might not actually be
343 // available. If this occurs, use the register indicated by the
344 // reuser.
345 if (!ReusedOperands.empty()) // This is most often empty.
346 for (unsigned ro = 0, e = ReusedOperands.size(); ro != e; ++ro)
347 if (ReusedOperands[ro].PhysRegReused == PhysReg) {
348 // Yup, use the reload register that we didn't use before.
349 PhysReg = ReusedOperands[ro].AssignedPhysReg;
350 break;
351 } else {
352 ReusedOp &Op = ReusedOperands[ro];
353 unsigned PRRU = Op.PhysRegReused;
354 for (const unsigned *AS = MRI->getAliasSet(PRRU); *AS; ++AS)
355 if (*AS == PhysReg) {
356 // Okay, we found out that an alias of a reused register
357 // was used. This isn't good because it means we have
358 // to undo a previous reuse.
359 MRI->loadRegFromStackSlot(MBB, &MI, Op.AssignedPhysReg,
360 Op.StackSlot);
361 ClobberPhysReg(Op.AssignedPhysReg, SpillSlotsAvailable,
362 PhysRegsAvailable);
363
364 MI.SetMachineOperandReg(Op.Operand, Op.AssignedPhysReg);
365 PhysRegsAvailable[Op.AssignedPhysReg] = Op.StackSlot;
366 SpillSlotsAvailable[Op.StackSlot] = Op.AssignedPhysReg;
367 PhysRegsAvailable.erase(Op.PhysRegReused);
368 DEBUG(std::cerr << "Remembering SS#" << Op.StackSlot
369 << " in physreg "
370 << MRI->getName(Op.AssignedPhysReg) << "\n");
371 ++NumLoads;
372 DEBUG(std::cerr << '\t' << *prior(MII));
373
374 DEBUG(std::cerr << "Reuse undone!\n");
375 ReusedOperands.erase(ReusedOperands.begin()+ro);
376 --NumReused;
377 goto ContinueReload;
378 }
379 }
380 ContinueReload:
381
382 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot);
383 // This invalidates PhysReg.
384 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
385
386 MI.SetMachineOperandReg(i, PhysReg);
387 PhysRegsAvailable[PhysReg] = StackSlot;
388 SpillSlotsAvailable[StackSlot] = PhysReg;
389 DEBUG(std::cerr << "Remembering SS#" << StackSlot <<" in physreg "
390 << MRI->getName(PhysReg) << "\n");
391 ++NumLoads;
392 DEBUG(std::cerr << '\t' << *prior(MII));
393 }
394
395 // If this is both a def and a use, we need to emit a store to the
396 // stack slot after the instruction. Keep track of D&U operands
397 // because we already changed it to a physreg here.
398 if (MO.isDef()) {
399 // Remember that this was a def-and-use operand, and that the
400 // stack slot is live after this instruction executes.
401 DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
402 }
403 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000404 }
405 }
406 }
407
Chris Lattner7fb64342004-10-01 19:04:51 +0000408 // Loop over all of the implicit defs, clearing them from our available
409 // sets.
410 const TargetInstrDescriptor &InstrDesc = TII->get(MI.getOpcode());
411 for (const unsigned* ImpDef = InstrDesc.ImplicitDefs; *ImpDef; ++ImpDef)
412 ClobberPhysReg(*ImpDef, SpillSlotsAvailable, PhysRegsAvailable);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000413
Chris Lattner7fb64342004-10-01 19:04:51 +0000414 DEBUG(std::cerr << '\t' << MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000415
Chris Lattner7fb64342004-10-01 19:04:51 +0000416 // If we have folded references to memory operands, make sure we clear all
417 // physical registers that may contain the value of the spilled virtual
418 // register
419 VirtRegMap::MI2VirtMapTy::const_iterator I, E;
420 for (tie(I, E) = VRM.getFoldedVirts(&MI); I != E; ++I) {
421 DEBUG(std::cerr << "Folded vreg: " << I->second);
422 if (VRM.hasStackSlot(I->second)) {
423 int SS = VRM.getStackSlot(I->second);
424 DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
425
426 std::map<int, unsigned>::iterator I = SpillSlotsAvailable.find(SS);
427 if (I != SpillSlotsAvailable.end()) {
428 PhysRegsAvailable.erase(I->second);
429 SpillSlotsAvailable.erase(I);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000430 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000431 } else {
432 DEBUG(std::cerr << ": No stack slot!\n");
433 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000434 }
435
Chris Lattner7fb64342004-10-01 19:04:51 +0000436 // Process all of the spilled defs.
437 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
438 MachineOperand &MO = MI.getOperand(i);
439 if (MO.isRegister() && MO.getReg() && MO.isDef()) {
440 unsigned VirtReg = MO.getReg();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000441
Chris Lattner7fb64342004-10-01 19:04:51 +0000442 bool TakenCareOf = false;
443 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
444 // Check to see if this is a def-and-use vreg operand that we do need
445 // to insert a store for.
446 bool OpTakenCareOf = false;
447 if (MO.isUse() && !DefAndUseVReg.empty()) {
448 for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
449 if (DefAndUseVReg[dau].first == i) {
450 VirtReg = DefAndUseVReg[dau].second;
451 OpTakenCareOf = true;
452 break;
453 }
454 }
455
456 if (!OpTakenCareOf) {
457 ClobberPhysReg(VirtReg, SpillSlotsAvailable, PhysRegsAvailable);
458 TakenCareOf = true;
459 }
460 }
461
462 if (!TakenCareOf) {
463 // The only vregs left are stack slot definitions.
464 int StackSlot = VRM.getStackSlot(VirtReg);
465 unsigned PhysReg;
466
467 // If this is a def&use operand, and we used a different physreg for
468 // it than the one assigned, make sure to execute the store from the
469 // correct physical register.
470 if (MO.getReg() == VirtReg)
471 PhysReg = VRM.getPhys(VirtReg);
472 else
473 PhysReg = MO.getReg();
474
475 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot);
476 DEBUG(std::cerr << "Store:\t" << *next(MII));
477 MI.SetMachineOperandReg(i, PhysReg);
478
479 // If the stack slot value was previously available in some other
480 // register, change it now. Otherwise, make the register available,
481 // in PhysReg.
482 std::map<int, unsigned>::iterator SSA =
483 SpillSlotsAvailable.find(StackSlot);
484 if (SSA != SpillSlotsAvailable.end()) {
485 // Remove the record for physreg.
486 PhysRegsAvailable.erase(SSA->second);
487 SpillSlotsAvailable.erase(SSA);
488 }
489 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
490
491 PhysRegsAvailable[PhysReg] = StackSlot;
492 SpillSlotsAvailable[StackSlot] = PhysReg;
493 DEBUG(std::cerr << "Updating SS#" << StackSlot <<" in physreg "
494 << MRI->getName(PhysReg) << "\n");
495
496 ++NumStores;
497 VirtReg = PhysReg;
498 }
499 }
500 }
501 MII = NextMII;
502 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000503}
504
505
Chris Lattner7fb64342004-10-01 19:04:51 +0000506
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000507llvm::Spiller* llvm::createSpiller() {
508 switch (SpillerOpt) {
509 default: assert(0 && "Unreachable!");
510 case local:
511 return new LocalSpiller();
512 case simple:
513 return new SimpleSpiller();
514 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000515}