blob: 26498b7ec1fcb0c3d73cdcafecfdb84f58e3adb2 [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"
Chris Lattner27f29162004-10-26 15:35:58 +000031#include <algorithm>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000032using namespace llvm;
33
34namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000035 Statistic<> NumSpills("spiller", "Number of register spills");
36 Statistic<> NumStores("spiller", "Number of stores added");
37 Statistic<> NumLoads ("spiller", "Number of loads added");
Chris Lattner7fb64342004-10-01 19:04:51 +000038 Statistic<> NumReused("spiller", "Number of values reused");
Chris Lattner52b25db2004-10-01 19:47:12 +000039 Statistic<> NumDSE ("spiller", "Number of dead stores elided");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000040
Chris Lattner8c4d88d2004-09-30 01:54:45 +000041 enum SpillerName { simple, local };
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000042
Chris Lattner8c4d88d2004-09-30 01:54:45 +000043 cl::opt<SpillerName>
44 SpillerOpt("spiller",
Chris Lattner7fb64342004-10-01 19:04:51 +000045 cl::desc("Spiller to use: (default: local)"),
Chris Lattner8c4d88d2004-09-30 01:54:45 +000046 cl::Prefix,
47 cl::values(clEnumVal(simple, " simple spiller"),
48 clEnumVal(local, " local spiller"),
49 clEnumValEnd),
Chris Lattner7fb64342004-10-01 19:04:51 +000050 cl::init(local));
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000051}
52
Chris Lattner8c4d88d2004-09-30 01:54:45 +000053//===----------------------------------------------------------------------===//
54// VirtRegMap implementation
55//===----------------------------------------------------------------------===//
56
57void VirtRegMap::grow() {
Chris Lattner7f690e62004-09-30 02:15:18 +000058 Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
59 Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000060}
61
Chris Lattner8c4d88d2004-09-30 01:54:45 +000062int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
63 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000064 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000065 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000066 const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
67 int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
68 RC->getAlignment());
69 Virt2StackSlotMap[virtReg] = frameIndex;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000070 ++NumSpills;
71 return frameIndex;
72}
73
74void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
75 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000076 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000077 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000078 Virt2StackSlotMap[virtReg] = frameIndex;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +000079}
80
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000081void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
82 unsigned OpNo, MachineInstr *NewMI) {
83 // Move previous memory references folded to new instruction.
84 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +000085 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000086 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
87 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +000088 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +000089 }
Chris Lattnerdbea9732004-09-30 16:35:08 +000090
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000091 ModRef MRInfo;
92 if (!OldMI->getOperand(OpNo).isDef()) {
93 assert(OldMI->getOperand(OpNo).isUse() && "Operand is not use or def?");
94 MRInfo = isRef;
95 } else {
96 MRInfo = OldMI->getOperand(OpNo).isUse() ? isModRef : isMod;
97 }
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000098
Chris Lattner8c4d88d2004-09-30 01:54:45 +000099 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000100 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000101}
102
Chris Lattner7f690e62004-09-30 02:15:18 +0000103void VirtRegMap::print(std::ostream &OS) const {
104 const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000105
Chris Lattner7f690e62004-09-30 02:15:18 +0000106 OS << "********** REGISTER MAP **********\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000107 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000108 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
109 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
110 OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000111
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000112 }
113
114 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000115 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
116 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
117 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
118 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000119}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000120
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000121void VirtRegMap::dump() const { print(std::cerr); }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000122
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000123
124//===----------------------------------------------------------------------===//
125// Simple Spiller Implementation
126//===----------------------------------------------------------------------===//
127
128Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000129
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000130namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000131 struct SimpleSpiller : public Spiller {
132 bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM);
133 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000134}
135
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000136bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF,
137 const VirtRegMap &VRM) {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000138 DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
139 DEBUG(std::cerr << "********** Function: "
140 << MF.getFunction()->getName() << '\n');
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000141 const TargetMachine &TM = MF.getTarget();
142 const MRegisterInfo &MRI = *TM.getRegisterInfo();
143 bool *PhysRegsUsed = MF.getUsedPhysregs();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000144
Chris Lattner4ea1b822004-09-30 02:33:48 +0000145 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
146 // each vreg once (in the case where a spilled vreg is used by multiple
147 // operands). This is always smaller than the number of operands to the
148 // current machine instr, so it should be small.
149 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000150
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000151 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
152 MBBI != E; ++MBBI) {
153 DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n");
154 MachineBasicBlock &MBB = *MBBI;
155 for (MachineBasicBlock::iterator MII = MBB.begin(),
156 E = MBB.end(); MII != E; ++MII) {
157 MachineInstr &MI = *MII;
158 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000159 MachineOperand &MO = MI.getOperand(i);
Chris Lattner886dd912005-04-04 21:35:34 +0000160 if (MO.isRegister() && MO.getReg())
161 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
162 unsigned VirtReg = MO.getReg();
163 unsigned PhysReg = VRM.getPhys(VirtReg);
164 if (VRM.hasStackSlot(VirtReg)) {
165 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000166 const TargetRegisterClass* RC =
167 MF.getSSARegMap()->getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000168
Chris Lattner886dd912005-04-04 21:35:34 +0000169 if (MO.isUse() &&
170 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
171 == LoadedRegs.end()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000172 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000173 LoadedRegs.push_back(VirtReg);
174 ++NumLoads;
175 DEBUG(std::cerr << '\t' << *prior(MII));
176 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000177
Chris Lattner886dd912005-04-04 21:35:34 +0000178 if (MO.isDef()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000179 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000180 ++NumStores;
181 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000182 }
Chris Lattner886dd912005-04-04 21:35:34 +0000183 PhysRegsUsed[PhysReg] = true;
184 MI.SetMachineOperandReg(i, PhysReg);
185 } else {
186 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000187 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000188 }
Chris Lattner886dd912005-04-04 21:35:34 +0000189
Chris Lattner477e4552004-09-30 16:10:45 +0000190 DEBUG(std::cerr << '\t' << MI);
Chris Lattner4ea1b822004-09-30 02:33:48 +0000191 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000192 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000193 }
194 return true;
195}
196
197//===----------------------------------------------------------------------===//
198// Local Spiller Implementation
199//===----------------------------------------------------------------------===//
200
201namespace {
Chris Lattner7fb64342004-10-01 19:04:51 +0000202 /// LocalSpiller - This spiller does a simple pass over the machine basic
203 /// block to attempt to keep spills in registers as much as possible for
204 /// blocks that have low register pressure (the vreg may be spilled due to
205 /// register pressure in other blocks).
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000206 class LocalSpiller : public Spiller {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000207 const MRegisterInfo *MRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000208 const TargetInstrInfo *TII;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000209 public:
Chris Lattner7fb64342004-10-01 19:04:51 +0000210 bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) {
211 MRI = MF.getTarget().getRegisterInfo();
212 TII = MF.getTarget().getInstrInfo();
213 DEBUG(std::cerr << "\n**** Local spiller rewriting function '"
214 << MF.getFunction()->getName() << "':\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000215
Chris Lattner7fb64342004-10-01 19:04:51 +0000216 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
217 MBB != E; ++MBB)
218 RewriteMBB(*MBB, VRM);
219 return true;
220 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000221 private:
Chris Lattner7fb64342004-10-01 19:04:51 +0000222 void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM);
223 void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
224 std::map<unsigned, int> &PhysRegs);
225 void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
226 std::map<unsigned, int> &PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000227 };
228}
229
Chris Lattner7fb64342004-10-01 19:04:51 +0000230void LocalSpiller::ClobberPhysRegOnly(unsigned PhysReg,
231 std::map<int, unsigned> &SpillSlots,
232 std::map<unsigned, int> &PhysRegs) {
233 std::map<unsigned, int>::iterator I = PhysRegs.find(PhysReg);
234 if (I != PhysRegs.end()) {
235 int Slot = I->second;
236 PhysRegs.erase(I);
237 assert(SpillSlots[Slot] == PhysReg && "Bidirectional map mismatch!");
238 SpillSlots.erase(Slot);
239 DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
240 << " clobbered, invalidating SS#" << Slot << "\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000241
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000242 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000243}
244
Chris Lattner7fb64342004-10-01 19:04:51 +0000245void LocalSpiller::ClobberPhysReg(unsigned PhysReg,
246 std::map<int, unsigned> &SpillSlots,
247 std::map<unsigned, int> &PhysRegs) {
248 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
249 ClobberPhysRegOnly(*AS, SpillSlots, PhysRegs);
250 ClobberPhysRegOnly(PhysReg, SpillSlots, PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000251}
252
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000253
Chris Lattner7fb64342004-10-01 19:04:51 +0000254// ReusedOp - For each reused operand, we keep track of a bit of information, in
255// case we need to rollback upon processing a new operand. See comments below.
256namespace {
257 struct ReusedOp {
258 // The MachineInstr operand that reused an available value.
259 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattner7fb64342004-10-01 19:04:51 +0000261 // StackSlot - The spill slot of the value being reused.
262 unsigned StackSlot;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattner7fb64342004-10-01 19:04:51 +0000264 // PhysRegReused - The physical register the value was available in.
265 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattner7fb64342004-10-01 19:04:51 +0000267 // AssignedPhysReg - The physreg that was assigned for use by the reload.
268 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000269
270 // VirtReg - The virtual register itself.
271 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000272
Chris Lattner8a61a752005-10-06 17:19:06 +0000273 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
274 unsigned vreg)
275 : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
276 VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000277 };
278}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000279
Chris Lattner7fb64342004-10-01 19:04:51 +0000280
281/// rewriteMBB - Keep track of which spills are available even after the
282/// register allocator is done with them. If possible, avoid reloading vregs.
283void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
284
285 // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
286 // register values that are still available, due to being loaded to stored to,
287 // but not invalidated yet.
288 std::map<int, unsigned> SpillSlotsAvailable;
289
290 // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
291 // which physregs are in use holding a stack slot value.
292 std::map<unsigned, int> PhysRegsAvailable;
293
294 DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
295
296 std::vector<ReusedOp> ReusedOperands;
297
298 // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
299 // of it. ".first" is the machine operand index (should always be 0 for now),
300 // and ".second" is the virtual register that is spilled.
301 std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
302
Chris Lattner52b25db2004-10-01 19:47:12 +0000303 // MaybeDeadStores - When we need to write a value back into a stack slot,
304 // keep track of the inserted store. If the stack slot value is never read
305 // (because the value was used from some available register, for example), and
306 // subsequently stored to, the original store is dead. This map keeps track
307 // of inserted stores that are not used. If we see a subsequent store to the
308 // same stack slot, the original store is deleted.
309 std::map<int, MachineInstr*> MaybeDeadStores;
310
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000311 bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
312
Chris Lattner7fb64342004-10-01 19:04:51 +0000313 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
314 MII != E; ) {
315 MachineInstr &MI = *MII;
316 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
317
318 ReusedOperands.clear();
319 DefAndUseVReg.clear();
320
321 // Process all of the spilled uses and all non spilled reg references.
322 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
323 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000324 if (!MO.isRegister() || MO.getReg() == 0)
325 continue; // Ignore non-register operands.
326
327 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
328 // Ignore physregs for spilling, but remember that it is used by this
329 // function.
Chris Lattner886dd912005-04-04 21:35:34 +0000330 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner50ea01e2005-09-09 20:29:51 +0000331 continue;
332 }
333
334 assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
335 "Not a virtual or a physical register?");
336
337 unsigned VirtReg = MO.getReg();
338 if (!VRM.hasStackSlot(VirtReg)) {
339 // This virtual register was assigned a physreg!
340 unsigned Phys = VRM.getPhys(VirtReg);
341 PhysRegsUsed[Phys] = true;
342 MI.SetMachineOperandReg(i, Phys);
343 continue;
344 }
345
346 // This virtual register is now known to be a spilled value.
347 if (!MO.isUse())
348 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +0000349
Chris Lattner50ea01e2005-09-09 20:29:51 +0000350 // If this is both a def and a use, we need to emit a store to the
351 // stack slot after the instruction. Keep track of D&U operands
352 // because we are about to change it to a physreg here.
353 if (MO.isDef()) {
354 // Remember that this was a def-and-use operand, and that the
355 // stack slot is live after this instruction executes.
356 DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
357 }
358
359 int StackSlot = VRM.getStackSlot(VirtReg);
360 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000361
Chris Lattner50ea01e2005-09-09 20:29:51 +0000362 // Check to see if this stack slot is available.
363 std::map<int, unsigned>::iterator SSI =
364 SpillSlotsAvailable.find(StackSlot);
365 if (SSI != SpillSlotsAvailable.end()) {
366 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
367 << MRI->getName(SSI->second) << " for vreg"
368 << VirtReg <<" instead of reloading into physreg "
369 << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
370 // If this stack slot value is already available, reuse it!
371 PhysReg = SSI->second;
372 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000373
Chris Lattner50ea01e2005-09-09 20:29:51 +0000374 // The only technical detail we have is that we don't know that
375 // PhysReg won't be clobbered by a reloaded stack slot that occurs
376 // later in the instruction. In particular, consider 'op V1, V2'.
377 // If V1 is available in physreg R0, we would choose to reuse it
378 // here, instead of reloading it into the register the allocator
379 // indicated (say R1). However, V2 might have to be reloaded
380 // later, and it might indicate that it needs to live in R0. When
381 // this occurs, we need to have information available that
382 // indicates it is safe to use R1 for the reload instead of R0.
383 //
384 // To further complicate matters, we might conflict with an alias,
385 // or R0 and R1 might not be compatible with each other. In this
386 // case, we actually insert a reload for V1 in R1, ensuring that
387 // we can get at R0 or its alias.
388 ReusedOperands.push_back(ReusedOp(i, StackSlot, PhysReg,
Chris Lattner8a61a752005-10-06 17:19:06 +0000389 VRM.getPhys(VirtReg), VirtReg));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000390 ++NumReused;
391 continue;
392 }
393
394 // Otherwise, reload it and remember that we have it.
395 PhysReg = VRM.getPhys(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000396 const TargetRegisterClass* RC =
397 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000398
Chris Lattner50ea01e2005-09-09 20:29:51 +0000399 RecheckRegister:
400 // Note that, if we reused a register for a previous operand, the
401 // register we want to reload into might not actually be
402 // available. If this occurs, use the register indicated by the
403 // reuser.
404 if (!ReusedOperands.empty()) // This is most often empty.
405 for (unsigned ro = 0, e = ReusedOperands.size(); ro != e; ++ro)
406 if (ReusedOperands[ro].PhysRegReused == PhysReg) {
407 // Yup, use the reload register that we didn't use before.
408 PhysReg = ReusedOperands[ro].AssignedPhysReg;
409 goto RecheckRegister;
410 } else {
411 ReusedOp &Op = ReusedOperands[ro];
412 unsigned PRRU = Op.PhysRegReused;
413 if (MRI->areAliases(PRRU, PhysReg)) {
414 // Okay, we found out that an alias of a reused register
415 // was used. This isn't good because it means we have
416 // to undo a previous reuse.
Chris Lattner8a61a752005-10-06 17:19:06 +0000417 const TargetRegisterClass *AliasRC =
418 MBB.getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000419 MRI->loadRegFromStackSlot(MBB, &MI, Op.AssignedPhysReg,
Chris Lattner8a61a752005-10-06 17:19:06 +0000420 Op.StackSlot, AliasRC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000421 ClobberPhysReg(Op.AssignedPhysReg, SpillSlotsAvailable,
422 PhysRegsAvailable);
Chris Lattner7fb64342004-10-01 19:04:51 +0000423
Chris Lattner52b25db2004-10-01 19:47:12 +0000424 // Any stores to this stack slot are not dead anymore.
Chris Lattner50ea01e2005-09-09 20:29:51 +0000425 MaybeDeadStores.erase(Op.StackSlot);
Chris Lattner52b25db2004-10-01 19:47:12 +0000426
Chris Lattner50ea01e2005-09-09 20:29:51 +0000427 MI.SetMachineOperandReg(Op.Operand, Op.AssignedPhysReg);
428 PhysRegsAvailable[Op.AssignedPhysReg] = Op.StackSlot;
429 SpillSlotsAvailable[Op.StackSlot] = Op.AssignedPhysReg;
430 PhysRegsAvailable.erase(Op.PhysRegReused);
431 DEBUG(std::cerr << "Remembering SS#" << Op.StackSlot
432 << " in physreg "
433 << MRI->getName(Op.AssignedPhysReg) << "\n");
Chris Lattner7fb64342004-10-01 19:04:51 +0000434 ++NumLoads;
435 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner7fb64342004-10-01 19:04:51 +0000436
Chris Lattner50ea01e2005-09-09 20:29:51 +0000437 DEBUG(std::cerr << "Reuse undone!\n");
438 ReusedOperands.erase(ReusedOperands.begin()+ro);
439 --NumReused;
440 goto ContinueReload;
Chris Lattner7fb64342004-10-01 19:04:51 +0000441 }
442 }
Chris Lattner50ea01e2005-09-09 20:29:51 +0000443 ContinueReload:
444 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000445 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000446 // This invalidates PhysReg.
447 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
448
449 // Any stores to this stack slot are not dead anymore.
450 MaybeDeadStores.erase(StackSlot);
451
452 MI.SetMachineOperandReg(i, PhysReg);
453 PhysRegsAvailable[PhysReg] = StackSlot;
454 SpillSlotsAvailable[StackSlot] = PhysReg;
455 DEBUG(std::cerr << "Remembering SS#" << StackSlot <<" in physreg "
456 << MRI->getName(PhysReg) << "\n");
457 ++NumLoads;
458 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000459 }
460
Chris Lattner7fb64342004-10-01 19:04:51 +0000461 // Loop over all of the implicit defs, clearing them from our available
462 // sets.
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000463 for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
464 *ImpDef; ++ImpDef) {
465 PhysRegsUsed[*ImpDef] = true;
Chris Lattner7fb64342004-10-01 19:04:51 +0000466 ClobberPhysReg(*ImpDef, SpillSlotsAvailable, PhysRegsAvailable);
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000467 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000468
Chris Lattner7fb64342004-10-01 19:04:51 +0000469 DEBUG(std::cerr << '\t' << MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000470
Chris Lattner7fb64342004-10-01 19:04:51 +0000471 // If we have folded references to memory operands, make sure we clear all
472 // physical registers that may contain the value of the spilled virtual
473 // register
Chris Lattner8f1d6402005-01-14 15:54:24 +0000474 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
475 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000476 DEBUG(std::cerr << "Folded vreg: " << I->second.first << " MR: "
477 << I->second.second);
478 unsigned VirtReg = I->second.first;
479 VirtRegMap::ModRef MR = I->second.second;
Chris Lattnercea86882005-09-19 06:56:21 +0000480 if (!VRM.hasStackSlot(VirtReg)) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000481 DEBUG(std::cerr << ": No stack slot!\n");
Chris Lattnercea86882005-09-19 06:56:21 +0000482 continue;
483 }
484 int SS = VRM.getStackSlot(VirtReg);
485 DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
486
487 // If this folded instruction is just a use, check to see if it's a
488 // straight load from the virt reg slot.
489 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
490 int FrameIdx;
491 if (unsigned DestReg = MRI->isLoadFromStackSlot(&MI, FrameIdx)) {
492 // If this spill slot is available, insert a copy for it!
493 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
494 if (FrameIdx == SS && It != SpillSlotsAvailable.end()) {
495 DEBUG(std::cerr << "Promoted Load To Copy: " << MI);
496 MachineFunction &MF = *MBB.getParent();
497 if (DestReg != It->second) {
498 MRI->copyRegToReg(MBB, &MI, DestReg, It->second,
499 MF.getSSARegMap()->getRegClass(VirtReg));
Chris Lattner22480c42005-10-05 18:30:19 +0000500 // Revisit the copy so we make sure to notice the effects of the
501 // operation on the destreg (either needing to RA it if it's
502 // virtual or needing to clobber any values if it's physical).
503 NextMII = &MI;
504 --NextMII; // backtrack to the copy.
Chris Lattnercea86882005-09-19 06:56:21 +0000505 }
506 MBB.erase(&MI);
507 goto ProcessNextInst;
508 }
509 }
510 }
511
512 // If this reference is not a use, any previous store is now dead.
513 // Otherwise, the store to this stack slot is not dead anymore.
514 std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
515 if (MDSI != MaybeDeadStores.end()) {
516 if (MR & VirtRegMap::isRef) // Previous store is not dead.
517 MaybeDeadStores.erase(MDSI);
518 else {
519 // If we get here, the store is dead, nuke it now.
520 assert(MR == VirtRegMap::isMod && "Can't be modref!");
521 MBB.erase(MDSI->second);
522 MaybeDeadStores.erase(MDSI);
523 ++NumDSE;
524 }
525 }
526
527 // If the spill slot value is available, and this is a new definition of
528 // the value, the value is not available anymore.
529 if (MR & VirtRegMap::isMod) {
530 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
531 if (It != SpillSlotsAvailable.end()) {
532 PhysRegsAvailable.erase(It->second);
533 SpillSlotsAvailable.erase(It);
534 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000535 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000536 }
537
Chris Lattner7fb64342004-10-01 19:04:51 +0000538 // Process all of the spilled defs.
539 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
540 MachineOperand &MO = MI.getOperand(i);
541 if (MO.isRegister() && MO.getReg() && MO.isDef()) {
542 unsigned VirtReg = MO.getReg();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000543
Chris Lattner7fb64342004-10-01 19:04:51 +0000544 bool TakenCareOf = false;
545 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
546 // Check to see if this is a def-and-use vreg operand that we do need
547 // to insert a store for.
548 bool OpTakenCareOf = false;
549 if (MO.isUse() && !DefAndUseVReg.empty()) {
550 for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
551 if (DefAndUseVReg[dau].first == i) {
552 VirtReg = DefAndUseVReg[dau].second;
553 OpTakenCareOf = true;
554 break;
555 }
556 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000557
Chris Lattner7fb64342004-10-01 19:04:51 +0000558 if (!OpTakenCareOf) {
559 ClobberPhysReg(VirtReg, SpillSlotsAvailable, PhysRegsAvailable);
560 TakenCareOf = true;
561 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000562 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000563
564 if (!TakenCareOf) {
565 // The only vregs left are stack slot definitions.
566 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000567 const TargetRegisterClass *RC =
568 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000569 unsigned PhysReg;
570
571 // If this is a def&use operand, and we used a different physreg for
572 // it than the one assigned, make sure to execute the store from the
573 // correct physical register.
574 if (MO.getReg() == VirtReg)
575 PhysReg = VRM.getPhys(VirtReg);
576 else
577 PhysReg = MO.getReg();
578
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000579 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000580 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
Chris Lattner7fb64342004-10-01 19:04:51 +0000581 DEBUG(std::cerr << "Store:\t" << *next(MII));
582 MI.SetMachineOperandReg(i, PhysReg);
583
Chris Lattner52b25db2004-10-01 19:47:12 +0000584 // If there is a dead store to this stack slot, nuke it now.
585 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
586 if (LastStore) {
Chris Lattner8df6a592004-10-15 03:16:29 +0000587 DEBUG(std::cerr << " Killed store:\t" << *LastStore);
Chris Lattner52b25db2004-10-01 19:47:12 +0000588 ++NumDSE;
589 MBB.erase(LastStore);
590 }
591 LastStore = next(MII);
592
Chris Lattner7fb64342004-10-01 19:04:51 +0000593 // If the stack slot value was previously available in some other
594 // register, change it now. Otherwise, make the register available,
595 // in PhysReg.
596 std::map<int, unsigned>::iterator SSA =
597 SpillSlotsAvailable.find(StackSlot);
598 if (SSA != SpillSlotsAvailable.end()) {
599 // Remove the record for physreg.
600 PhysRegsAvailable.erase(SSA->second);
601 SpillSlotsAvailable.erase(SSA);
602 }
603 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
604
605 PhysRegsAvailable[PhysReg] = StackSlot;
606 SpillSlotsAvailable[StackSlot] = PhysReg;
607 DEBUG(std::cerr << "Updating SS#" << StackSlot <<" in physreg "
Chris Lattner8df6a592004-10-15 03:16:29 +0000608 << MRI->getName(PhysReg) << " for virtreg #"
609 << VirtReg << "\n");
Chris Lattner7fb64342004-10-01 19:04:51 +0000610
611 ++NumStores;
612 VirtReg = PhysReg;
613 }
614 }
615 }
Chris Lattnercea86882005-09-19 06:56:21 +0000616 ProcessNextInst:
Chris Lattner7fb64342004-10-01 19:04:51 +0000617 MII = NextMII;
618 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000619}
620
621
Chris Lattner7fb64342004-10-01 19:04:51 +0000622
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000623llvm::Spiller* llvm::createSpiller() {
624 switch (SpillerOpt) {
625 default: assert(0 && "Unreachable!");
626 case local:
627 return new LocalSpiller();
628 case simple:
629 return new SimpleSpiller();
630 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000631}