blob: 77a028e5fcad57cd866a8a48c8a8c14403ea2140 [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>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000032#include <iostream>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000033using namespace llvm;
34
35namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000036 Statistic<> NumSpills("spiller", "Number of register spills");
37 Statistic<> NumStores("spiller", "Number of stores added");
38 Statistic<> NumLoads ("spiller", "Number of loads added");
Chris Lattner7fb64342004-10-01 19:04:51 +000039 Statistic<> NumReused("spiller", "Number of values reused");
Chris Lattner52b25db2004-10-01 19:47:12 +000040 Statistic<> NumDSE ("spiller", "Number of dead stores elided");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000041
Chris Lattner8c4d88d2004-09-30 01:54:45 +000042 enum SpillerName { simple, local };
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000043
Chris Lattner8c4d88d2004-09-30 01:54:45 +000044 cl::opt<SpillerName>
45 SpillerOpt("spiller",
Chris Lattner7fb64342004-10-01 19:04:51 +000046 cl::desc("Spiller to use: (default: local)"),
Chris Lattner8c4d88d2004-09-30 01:54:45 +000047 cl::Prefix,
48 cl::values(clEnumVal(simple, " simple spiller"),
49 clEnumVal(local, " local spiller"),
50 clEnumValEnd),
Chris Lattner7fb64342004-10-01 19:04:51 +000051 cl::init(local));
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000052}
53
Chris Lattner8c4d88d2004-09-30 01:54:45 +000054//===----------------------------------------------------------------------===//
55// VirtRegMap implementation
56//===----------------------------------------------------------------------===//
57
58void VirtRegMap::grow() {
Chris Lattner7f690e62004-09-30 02:15:18 +000059 Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
60 Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000061}
62
Chris Lattner8c4d88d2004-09-30 01:54:45 +000063int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
64 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000065 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000066 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000067 const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
68 int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
69 RC->getAlignment());
70 Virt2StackSlotMap[virtReg] = frameIndex;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000071 ++NumSpills;
72 return frameIndex;
73}
74
75void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
76 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000077 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000078 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000079 Virt2StackSlotMap[virtReg] = frameIndex;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +000080}
81
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000082void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
83 unsigned OpNo, MachineInstr *NewMI) {
84 // Move previous memory references folded to new instruction.
85 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +000086 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000087 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
88 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +000089 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +000090 }
Chris Lattnerdbea9732004-09-30 16:35:08 +000091
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000092 ModRef MRInfo;
93 if (!OldMI->getOperand(OpNo).isDef()) {
94 assert(OldMI->getOperand(OpNo).isUse() && "Operand is not use or def?");
95 MRInfo = isRef;
96 } else {
97 MRInfo = OldMI->getOperand(OpNo).isUse() ? isModRef : isMod;
98 }
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000099
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000100 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000101 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000102}
103
Chris Lattner7f690e62004-09-30 02:15:18 +0000104void VirtRegMap::print(std::ostream &OS) const {
105 const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000106
Chris Lattner7f690e62004-09-30 02:15:18 +0000107 OS << "********** REGISTER MAP **********\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000108 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000109 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
110 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
111 OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000112
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000113 }
114
115 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000116 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
117 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
118 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
119 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000120}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000121
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000122void VirtRegMap::dump() const { print(std::cerr); }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000123
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000124
125//===----------------------------------------------------------------------===//
126// Simple Spiller Implementation
127//===----------------------------------------------------------------------===//
128
129Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000130
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000131namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000132 struct SimpleSpiller : public Spiller {
133 bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM);
134 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000135}
136
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000137bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF,
138 const VirtRegMap &VRM) {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000139 DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
140 DEBUG(std::cerr << "********** Function: "
141 << MF.getFunction()->getName() << '\n');
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000142 const TargetMachine &TM = MF.getTarget();
143 const MRegisterInfo &MRI = *TM.getRegisterInfo();
144 bool *PhysRegsUsed = MF.getUsedPhysregs();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000145
Chris Lattner4ea1b822004-09-30 02:33:48 +0000146 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
147 // each vreg once (in the case where a spilled vreg is used by multiple
148 // operands). This is always smaller than the number of operands to the
149 // current machine instr, so it should be small.
150 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000151
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000152 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
153 MBBI != E; ++MBBI) {
154 DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n");
155 MachineBasicBlock &MBB = *MBBI;
156 for (MachineBasicBlock::iterator MII = MBB.begin(),
157 E = MBB.end(); MII != E; ++MII) {
158 MachineInstr &MI = *MII;
159 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000160 MachineOperand &MO = MI.getOperand(i);
Chris Lattner886dd912005-04-04 21:35:34 +0000161 if (MO.isRegister() && MO.getReg())
162 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
163 unsigned VirtReg = MO.getReg();
164 unsigned PhysReg = VRM.getPhys(VirtReg);
165 if (VRM.hasStackSlot(VirtReg)) {
166 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000167 const TargetRegisterClass* RC =
168 MF.getSSARegMap()->getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000169
Chris Lattner886dd912005-04-04 21:35:34 +0000170 if (MO.isUse() &&
171 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
172 == LoadedRegs.end()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000173 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000174 LoadedRegs.push_back(VirtReg);
175 ++NumLoads;
176 DEBUG(std::cerr << '\t' << *prior(MII));
177 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000178
Chris Lattner886dd912005-04-04 21:35:34 +0000179 if (MO.isDef()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000180 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000181 ++NumStores;
182 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000183 }
Chris Lattner886dd912005-04-04 21:35:34 +0000184 PhysRegsUsed[PhysReg] = true;
185 MI.SetMachineOperandReg(i, PhysReg);
186 } else {
187 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000188 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000189 }
Chris Lattner886dd912005-04-04 21:35:34 +0000190
Chris Lattner477e4552004-09-30 16:10:45 +0000191 DEBUG(std::cerr << '\t' << MI);
Chris Lattner4ea1b822004-09-30 02:33:48 +0000192 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000193 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000194 }
195 return true;
196}
197
198//===----------------------------------------------------------------------===//
199// Local Spiller Implementation
200//===----------------------------------------------------------------------===//
201
202namespace {
Chris Lattner7fb64342004-10-01 19:04:51 +0000203 /// LocalSpiller - This spiller does a simple pass over the machine basic
204 /// block to attempt to keep spills in registers as much as possible for
205 /// blocks that have low register pressure (the vreg may be spilled due to
206 /// register pressure in other blocks).
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000207 class LocalSpiller : public Spiller {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000208 const MRegisterInfo *MRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000209 const TargetInstrInfo *TII;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000210 public:
Chris Lattner7fb64342004-10-01 19:04:51 +0000211 bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) {
212 MRI = MF.getTarget().getRegisterInfo();
213 TII = MF.getTarget().getInstrInfo();
214 DEBUG(std::cerr << "\n**** Local spiller rewriting function '"
215 << MF.getFunction()->getName() << "':\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000216
Chris Lattner7fb64342004-10-01 19:04:51 +0000217 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
218 MBB != E; ++MBB)
219 RewriteMBB(*MBB, VRM);
220 return true;
221 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000222 private:
Chris Lattner7fb64342004-10-01 19:04:51 +0000223 void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM);
224 void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
225 std::map<unsigned, int> &PhysRegs);
226 void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
227 std::map<unsigned, int> &PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000228 };
229}
230
Chris Lattner7fb64342004-10-01 19:04:51 +0000231void LocalSpiller::ClobberPhysRegOnly(unsigned PhysReg,
232 std::map<int, unsigned> &SpillSlots,
233 std::map<unsigned, int> &PhysRegs) {
234 std::map<unsigned, int>::iterator I = PhysRegs.find(PhysReg);
235 if (I != PhysRegs.end()) {
236 int Slot = I->second;
237 PhysRegs.erase(I);
238 assert(SpillSlots[Slot] == PhysReg && "Bidirectional map mismatch!");
239 SpillSlots.erase(Slot);
240 DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
241 << " clobbered, invalidating SS#" << Slot << "\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000242
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000243 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000244}
245
Chris Lattner7fb64342004-10-01 19:04:51 +0000246void LocalSpiller::ClobberPhysReg(unsigned PhysReg,
247 std::map<int, unsigned> &SpillSlots,
248 std::map<unsigned, int> &PhysRegs) {
249 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
250 ClobberPhysRegOnly(*AS, SpillSlots, PhysRegs);
251 ClobberPhysRegOnly(PhysReg, SpillSlots, PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000252}
253
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000254
Chris Lattner7fb64342004-10-01 19:04:51 +0000255// ReusedOp - For each reused operand, we keep track of a bit of information, in
256// case we need to rollback upon processing a new operand. See comments below.
257namespace {
258 struct ReusedOp {
259 // The MachineInstr operand that reused an available value.
260 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261
Chris Lattner7fb64342004-10-01 19:04:51 +0000262 // StackSlot - The spill slot of the value being reused.
263 unsigned StackSlot;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000264
Chris Lattner7fb64342004-10-01 19:04:51 +0000265 // PhysRegReused - The physical register the value was available in.
266 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000267
Chris Lattner7fb64342004-10-01 19:04:51 +0000268 // AssignedPhysReg - The physreg that was assigned for use by the reload.
269 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000270
271 // VirtReg - The virtual register itself.
272 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000273
Chris Lattner8a61a752005-10-06 17:19:06 +0000274 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
275 unsigned vreg)
276 : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
277 VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000278 };
279}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000280
Chris Lattner7fb64342004-10-01 19:04:51 +0000281
282/// rewriteMBB - Keep track of which spills are available even after the
283/// register allocator is done with them. If possible, avoid reloading vregs.
284void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
285
286 // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
Chris Lattnercd816392006-02-02 23:29:36 +0000287 // register values that are still available, due to being loaded or stored to,
Chris Lattner7fb64342004-10-01 19:04:51 +0000288 // but not invalidated yet.
289 std::map<int, unsigned> SpillSlotsAvailable;
290
291 // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
292 // which physregs are in use holding a stack slot value.
293 std::map<unsigned, int> PhysRegsAvailable;
294
295 DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
296
297 std::vector<ReusedOp> ReusedOperands;
298
299 // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
300 // of it. ".first" is the machine operand index (should always be 0 for now),
301 // and ".second" is the virtual register that is spilled.
302 std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
303
Chris Lattner52b25db2004-10-01 19:47:12 +0000304 // MaybeDeadStores - When we need to write a value back into a stack slot,
305 // keep track of the inserted store. If the stack slot value is never read
306 // (because the value was used from some available register, for example), and
307 // subsequently stored to, the original store is dead. This map keeps track
308 // of inserted stores that are not used. If we see a subsequent store to the
309 // same stack slot, the original store is deleted.
310 std::map<int, MachineInstr*> MaybeDeadStores;
311
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000312 bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
313
Chris Lattner7fb64342004-10-01 19:04:51 +0000314 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
315 MII != E; ) {
316 MachineInstr &MI = *MII;
317 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
318
319 ReusedOperands.clear();
320 DefAndUseVReg.clear();
321
322 // Process all of the spilled uses and all non spilled reg references.
323 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
324 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000325 if (!MO.isRegister() || MO.getReg() == 0)
326 continue; // Ignore non-register operands.
327
328 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
329 // Ignore physregs for spilling, but remember that it is used by this
330 // function.
Chris Lattner886dd912005-04-04 21:35:34 +0000331 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner50ea01e2005-09-09 20:29:51 +0000332 continue;
333 }
334
335 assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
336 "Not a virtual or a physical register?");
337
338 unsigned VirtReg = MO.getReg();
339 if (!VRM.hasStackSlot(VirtReg)) {
340 // This virtual register was assigned a physreg!
341 unsigned Phys = VRM.getPhys(VirtReg);
342 PhysRegsUsed[Phys] = true;
343 MI.SetMachineOperandReg(i, Phys);
344 continue;
345 }
346
347 // This virtual register is now known to be a spilled value.
348 if (!MO.isUse())
349 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +0000350
Chris Lattner50ea01e2005-09-09 20:29:51 +0000351 // If this is both a def and a use, we need to emit a store to the
352 // stack slot after the instruction. Keep track of D&U operands
353 // because we are about to change it to a physreg here.
354 if (MO.isDef()) {
355 // Remember that this was a def-and-use operand, and that the
356 // stack slot is live after this instruction executes.
357 DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
358 }
359
360 int StackSlot = VRM.getStackSlot(VirtReg);
361 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000362
Chris Lattner50ea01e2005-09-09 20:29:51 +0000363 // Check to see if this stack slot is available.
364 std::map<int, unsigned>::iterator SSI =
365 SpillSlotsAvailable.find(StackSlot);
366 if (SSI != SpillSlotsAvailable.end()) {
367 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
368 << MRI->getName(SSI->second) << " for vreg"
369 << VirtReg <<" instead of reloading into physreg "
370 << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
371 // If this stack slot value is already available, reuse it!
372 PhysReg = SSI->second;
373 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000374
Chris Lattner50ea01e2005-09-09 20:29:51 +0000375 // The only technical detail we have is that we don't know that
376 // PhysReg won't be clobbered by a reloaded stack slot that occurs
377 // later in the instruction. In particular, consider 'op V1, V2'.
378 // If V1 is available in physreg R0, we would choose to reuse it
379 // here, instead of reloading it into the register the allocator
380 // indicated (say R1). However, V2 might have to be reloaded
381 // later, and it might indicate that it needs to live in R0. When
382 // this occurs, we need to have information available that
383 // indicates it is safe to use R1 for the reload instead of R0.
384 //
385 // To further complicate matters, we might conflict with an alias,
386 // or R0 and R1 might not be compatible with each other. In this
387 // case, we actually insert a reload for V1 in R1, ensuring that
388 // we can get at R0 or its alias.
389 ReusedOperands.push_back(ReusedOp(i, StackSlot, PhysReg,
Chris Lattner8a61a752005-10-06 17:19:06 +0000390 VRM.getPhys(VirtReg), VirtReg));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000391 ++NumReused;
392 continue;
393 }
394
395 // Otherwise, reload it and remember that we have it.
396 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +0000397 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000398 const TargetRegisterClass* RC =
399 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000400
Chris Lattner50ea01e2005-09-09 20:29:51 +0000401 RecheckRegister:
402 // Note that, if we reused a register for a previous operand, the
403 // register we want to reload into might not actually be
404 // available. If this occurs, use the register indicated by the
405 // reuser.
406 if (!ReusedOperands.empty()) // This is most often empty.
407 for (unsigned ro = 0, e = ReusedOperands.size(); ro != e; ++ro)
408 if (ReusedOperands[ro].PhysRegReused == PhysReg) {
409 // Yup, use the reload register that we didn't use before.
410 PhysReg = ReusedOperands[ro].AssignedPhysReg;
411 goto RecheckRegister;
412 } else {
413 ReusedOp &Op = ReusedOperands[ro];
414 unsigned PRRU = Op.PhysRegReused;
415 if (MRI->areAliases(PRRU, PhysReg)) {
416 // Okay, we found out that an alias of a reused register
417 // was used. This isn't good because it means we have
418 // to undo a previous reuse.
Chris Lattner8a61a752005-10-06 17:19:06 +0000419 const TargetRegisterClass *AliasRC =
420 MBB.getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000421 MRI->loadRegFromStackSlot(MBB, &MI, Op.AssignedPhysReg,
Chris Lattner8a61a752005-10-06 17:19:06 +0000422 Op.StackSlot, AliasRC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000423 ClobberPhysReg(Op.AssignedPhysReg, SpillSlotsAvailable,
424 PhysRegsAvailable);
Chris Lattner7fb64342004-10-01 19:04:51 +0000425
Chris Lattner52b25db2004-10-01 19:47:12 +0000426 // Any stores to this stack slot are not dead anymore.
Chris Lattner50ea01e2005-09-09 20:29:51 +0000427 MaybeDeadStores.erase(Op.StackSlot);
Chris Lattner52b25db2004-10-01 19:47:12 +0000428
Chris Lattner50ea01e2005-09-09 20:29:51 +0000429 MI.SetMachineOperandReg(Op.Operand, Op.AssignedPhysReg);
430 PhysRegsAvailable[Op.AssignedPhysReg] = Op.StackSlot;
431 SpillSlotsAvailable[Op.StackSlot] = Op.AssignedPhysReg;
432 PhysRegsAvailable.erase(Op.PhysRegReused);
433 DEBUG(std::cerr << "Remembering SS#" << Op.StackSlot
434 << " in physreg "
435 << MRI->getName(Op.AssignedPhysReg) << "\n");
Chris Lattner7fb64342004-10-01 19:04:51 +0000436 ++NumLoads;
437 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner7fb64342004-10-01 19:04:51 +0000438
Chris Lattner50ea01e2005-09-09 20:29:51 +0000439 DEBUG(std::cerr << "Reuse undone!\n");
440 ReusedOperands.erase(ReusedOperands.begin()+ro);
441 --NumReused;
442 goto ContinueReload;
Chris Lattner7fb64342004-10-01 19:04:51 +0000443 }
444 }
Chris Lattner50ea01e2005-09-09 20:29:51 +0000445 ContinueReload:
446 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000447 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000448 // This invalidates PhysReg.
449 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
450
451 // Any stores to this stack slot are not dead anymore.
452 MaybeDeadStores.erase(StackSlot);
453
454 MI.SetMachineOperandReg(i, PhysReg);
455 PhysRegsAvailable[PhysReg] = StackSlot;
456 SpillSlotsAvailable[StackSlot] = PhysReg;
457 DEBUG(std::cerr << "Remembering SS#" << StackSlot <<" in physreg "
458 << MRI->getName(PhysReg) << "\n");
459 ++NumLoads;
460 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000461 }
462
Chris Lattner7fb64342004-10-01 19:04:51 +0000463 // Loop over all of the implicit defs, clearing them from our available
464 // sets.
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000465 for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
466 *ImpDef; ++ImpDef) {
467 PhysRegsUsed[*ImpDef] = true;
Chris Lattner7fb64342004-10-01 19:04:51 +0000468 ClobberPhysReg(*ImpDef, SpillSlotsAvailable, PhysRegsAvailable);
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000469 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000470
Chris Lattner7fb64342004-10-01 19:04:51 +0000471 DEBUG(std::cerr << '\t' << MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000472
Chris Lattner7fb64342004-10-01 19:04:51 +0000473 // If we have folded references to memory operands, make sure we clear all
474 // physical registers that may contain the value of the spilled virtual
475 // register
Chris Lattner8f1d6402005-01-14 15:54:24 +0000476 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
477 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000478 DEBUG(std::cerr << "Folded vreg: " << I->second.first << " MR: "
479 << I->second.second);
480 unsigned VirtReg = I->second.first;
481 VirtRegMap::ModRef MR = I->second.second;
Chris Lattnercea86882005-09-19 06:56:21 +0000482 if (!VRM.hasStackSlot(VirtReg)) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000483 DEBUG(std::cerr << ": No stack slot!\n");
Chris Lattnercea86882005-09-19 06:56:21 +0000484 continue;
485 }
486 int SS = VRM.getStackSlot(VirtReg);
487 DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
488
489 // If this folded instruction is just a use, check to see if it's a
490 // straight load from the virt reg slot.
491 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
492 int FrameIdx;
Chris Lattner40839602006-02-02 20:12:32 +0000493 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
494 // If this spill slot is available, turn it into a copy (or nothing)
495 // instead of leaving it as a load!
Chris Lattnercea86882005-09-19 06:56:21 +0000496 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
497 if (FrameIdx == SS && It != SpillSlotsAvailable.end()) {
498 DEBUG(std::cerr << "Promoted Load To Copy: " << MI);
499 MachineFunction &MF = *MBB.getParent();
500 if (DestReg != It->second) {
501 MRI->copyRegToReg(MBB, &MI, DestReg, It->second,
502 MF.getSSARegMap()->getRegClass(VirtReg));
Chris Lattner22480c42005-10-05 18:30:19 +0000503 // Revisit the copy so we make sure to notice the effects of the
504 // operation on the destreg (either needing to RA it if it's
505 // virtual or needing to clobber any values if it's physical).
506 NextMII = &MI;
507 --NextMII; // backtrack to the copy.
Chris Lattnercea86882005-09-19 06:56:21 +0000508 }
509 MBB.erase(&MI);
510 goto ProcessNextInst;
511 }
512 }
513 }
514
515 // If this reference is not a use, any previous store is now dead.
516 // Otherwise, the store to this stack slot is not dead anymore.
517 std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
518 if (MDSI != MaybeDeadStores.end()) {
519 if (MR & VirtRegMap::isRef) // Previous store is not dead.
520 MaybeDeadStores.erase(MDSI);
521 else {
522 // If we get here, the store is dead, nuke it now.
523 assert(MR == VirtRegMap::isMod && "Can't be modref!");
524 MBB.erase(MDSI->second);
525 MaybeDeadStores.erase(MDSI);
526 ++NumDSE;
527 }
528 }
529
530 // If the spill slot value is available, and this is a new definition of
531 // the value, the value is not available anymore.
532 if (MR & VirtRegMap::isMod) {
533 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
534 if (It != SpillSlotsAvailable.end()) {
535 PhysRegsAvailable.erase(It->second);
536 SpillSlotsAvailable.erase(It);
537 }
Chris Lattnercd816392006-02-02 23:29:36 +0000538
539 // If this is *just* a mod of the value, check to see if this is just a
540 // store to the spill slot (i.e. the spill got merged into the copy). If
541 // so, realize that the vreg is available now, and add the store to the
542 // MaybeDeadStore info.
543 int StackSlot;
544 if (!(MR & VirtRegMap::isRef)) {
545 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
546 assert(MRegisterInfo::isPhysicalRegister(SrcReg) &&
547 "Src hasn't been allocated yet?");
548 // Okay, this is certainly a store of SrcReg to [FrameIdx]. Mark
549 // this as a potentially dead store in case there is a subsequent
550 // store into the stack slot without a read from it.
551 MaybeDeadStores[StackSlot] = &MI;
552
553 // FIXME: PhysRegsAvailable is a 1-1 map, not a N-1 map, which means
554 // that we have to *forget* that SrcReg contains the old value it
555 // does.
556 ClobberPhysRegOnly(SrcReg, SpillSlotsAvailable, PhysRegsAvailable);
557
558 // If the stack slot value was previously available in some other
559 // register, change it now. Otherwise, make the register available,
560 // in PhysReg.
561 SpillSlotsAvailable[StackSlot] = SrcReg;
562 PhysRegsAvailable[SrcReg] = StackSlot;
563 DEBUG(std::cerr << "Updating SS#" << StackSlot << " in physreg "
564 << MRI->getName(SrcReg) << " for virtreg #"
565 << VirtReg << "\n" << MI);
566 }
567 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000568 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000569 }
570
Chris Lattner7fb64342004-10-01 19:04:51 +0000571 // Process all of the spilled defs.
572 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
573 MachineOperand &MO = MI.getOperand(i);
574 if (MO.isRegister() && MO.getReg() && MO.isDef()) {
575 unsigned VirtReg = MO.getReg();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000576
Chris Lattner7fb64342004-10-01 19:04:51 +0000577 bool TakenCareOf = false;
578 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
579 // Check to see if this is a def-and-use vreg operand that we do need
580 // to insert a store for.
581 bool OpTakenCareOf = false;
582 if (MO.isUse() && !DefAndUseVReg.empty()) {
583 for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
584 if (DefAndUseVReg[dau].first == i) {
585 VirtReg = DefAndUseVReg[dau].second;
586 OpTakenCareOf = true;
587 break;
588 }
589 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000590
Chris Lattner7fb64342004-10-01 19:04:51 +0000591 if (!OpTakenCareOf) {
592 ClobberPhysReg(VirtReg, SpillSlotsAvailable, PhysRegsAvailable);
593 TakenCareOf = true;
594 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000595 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000596
597 if (!TakenCareOf) {
598 // The only vregs left are stack slot definitions.
599 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000600 const TargetRegisterClass *RC =
601 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000602 unsigned PhysReg;
603
604 // If this is a def&use operand, and we used a different physreg for
605 // it than the one assigned, make sure to execute the store from the
606 // correct physical register.
607 if (MO.getReg() == VirtReg)
608 PhysReg = VRM.getPhys(VirtReg);
609 else
610 PhysReg = MO.getReg();
611
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000612 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000613 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
Chris Lattner7fb64342004-10-01 19:04:51 +0000614 DEBUG(std::cerr << "Store:\t" << *next(MII));
615 MI.SetMachineOperandReg(i, PhysReg);
616
Chris Lattner52b25db2004-10-01 19:47:12 +0000617 // If there is a dead store to this stack slot, nuke it now.
618 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
619 if (LastStore) {
Chris Lattner8df6a592004-10-15 03:16:29 +0000620 DEBUG(std::cerr << " Killed store:\t" << *LastStore);
Chris Lattner52b25db2004-10-01 19:47:12 +0000621 ++NumDSE;
622 MBB.erase(LastStore);
623 }
624 LastStore = next(MII);
625
Chris Lattner7fb64342004-10-01 19:04:51 +0000626 // If the stack slot value was previously available in some other
627 // register, change it now. Otherwise, make the register available,
628 // in PhysReg.
629 std::map<int, unsigned>::iterator SSA =
630 SpillSlotsAvailable.find(StackSlot);
631 if (SSA != SpillSlotsAvailable.end()) {
632 // Remove the record for physreg.
633 PhysRegsAvailable.erase(SSA->second);
634 SpillSlotsAvailable.erase(SSA);
635 }
636 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
637
638 PhysRegsAvailable[PhysReg] = StackSlot;
639 SpillSlotsAvailable[StackSlot] = PhysReg;
640 DEBUG(std::cerr << "Updating SS#" << StackSlot <<" in physreg "
Chris Lattner8df6a592004-10-15 03:16:29 +0000641 << MRI->getName(PhysReg) << " for virtreg #"
642 << VirtReg << "\n");
Chris Lattner7fb64342004-10-01 19:04:51 +0000643
644 ++NumStores;
645 VirtReg = PhysReg;
646 }
647 }
648 }
Chris Lattnercea86882005-09-19 06:56:21 +0000649 ProcessNextInst:
Chris Lattner7fb64342004-10-01 19:04:51 +0000650 MII = NextMII;
651 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000652}
653
654
Chris Lattner7fb64342004-10-01 19:04:51 +0000655
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000656llvm::Spiller* llvm::createSpiller() {
657 switch (SpillerOpt) {
658 default: assert(0 && "Unreachable!");
659 case local:
660 return new LocalSpiller();
661 case simple:
662 return new SimpleSpiller();
663 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000664}