blob: b30c61c8382a961aea62233ea8ae95fb62c8cf4a [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");
Chris Lattner1118d252006-02-03 02:02:59 +000041 Statistic<> NumDCE ("spiller", "Number of copies elided");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000042
Chris Lattner8c4d88d2004-09-30 01:54:45 +000043 enum SpillerName { simple, local };
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000044
Chris Lattner8c4d88d2004-09-30 01:54:45 +000045 cl::opt<SpillerName>
46 SpillerOpt("spiller",
Chris Lattner7fb64342004-10-01 19:04:51 +000047 cl::desc("Spiller to use: (default: local)"),
Chris Lattner8c4d88d2004-09-30 01:54:45 +000048 cl::Prefix,
49 cl::values(clEnumVal(simple, " simple spiller"),
50 clEnumVal(local, " local spiller"),
51 clEnumValEnd),
Chris Lattner7fb64342004-10-01 19:04:51 +000052 cl::init(local));
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000053}
54
Chris Lattner8c4d88d2004-09-30 01:54:45 +000055//===----------------------------------------------------------------------===//
56// VirtRegMap implementation
57//===----------------------------------------------------------------------===//
58
59void VirtRegMap::grow() {
Chris Lattner7f690e62004-09-30 02:15:18 +000060 Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
61 Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000062}
63
Chris Lattner8c4d88d2004-09-30 01:54:45 +000064int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
65 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000066 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000067 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000068 const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
69 int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
70 RC->getAlignment());
71 Virt2StackSlotMap[virtReg] = frameIndex;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000072 ++NumSpills;
73 return frameIndex;
74}
75
76void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
77 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000078 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000079 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000080 Virt2StackSlotMap[virtReg] = frameIndex;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +000081}
82
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000083void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
84 unsigned OpNo, MachineInstr *NewMI) {
85 // Move previous memory references folded to new instruction.
86 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +000087 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000088 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
89 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +000090 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +000091 }
Chris Lattnerdbea9732004-09-30 16:35:08 +000092
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000093 ModRef MRInfo;
94 if (!OldMI->getOperand(OpNo).isDef()) {
95 assert(OldMI->getOperand(OpNo).isUse() && "Operand is not use or def?");
96 MRInfo = isRef;
97 } else {
98 MRInfo = OldMI->getOperand(OpNo).isUse() ? isModRef : isMod;
99 }
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000100
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000101 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000102 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000103}
104
Chris Lattner7f690e62004-09-30 02:15:18 +0000105void VirtRegMap::print(std::ostream &OS) const {
106 const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000107
Chris Lattner7f690e62004-09-30 02:15:18 +0000108 OS << "********** REGISTER MAP **********\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000109 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000110 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
111 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
112 OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000113
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000114 }
115
116 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000117 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
118 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
119 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
120 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000121}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000122
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000123void VirtRegMap::dump() const { print(std::cerr); }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000124
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000125
126//===----------------------------------------------------------------------===//
127// Simple Spiller Implementation
128//===----------------------------------------------------------------------===//
129
130Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000131
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000132namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000133 struct SimpleSpiller : public Spiller {
134 bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM);
135 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000136}
137
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000138bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF,
139 const VirtRegMap &VRM) {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000140 DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
141 DEBUG(std::cerr << "********** Function: "
142 << MF.getFunction()->getName() << '\n');
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000143 const TargetMachine &TM = MF.getTarget();
144 const MRegisterInfo &MRI = *TM.getRegisterInfo();
145 bool *PhysRegsUsed = MF.getUsedPhysregs();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000146
Chris Lattner4ea1b822004-09-30 02:33:48 +0000147 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
148 // each vreg once (in the case where a spilled vreg is used by multiple
149 // operands). This is always smaller than the number of operands to the
150 // current machine instr, so it should be small.
151 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000152
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000153 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
154 MBBI != E; ++MBBI) {
155 DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n");
156 MachineBasicBlock &MBB = *MBBI;
157 for (MachineBasicBlock::iterator MII = MBB.begin(),
158 E = MBB.end(); MII != E; ++MII) {
159 MachineInstr &MI = *MII;
160 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000161 MachineOperand &MO = MI.getOperand(i);
Chris Lattner886dd912005-04-04 21:35:34 +0000162 if (MO.isRegister() && MO.getReg())
163 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
164 unsigned VirtReg = MO.getReg();
165 unsigned PhysReg = VRM.getPhys(VirtReg);
166 if (VRM.hasStackSlot(VirtReg)) {
167 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000168 const TargetRegisterClass* RC =
169 MF.getSSARegMap()->getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000170
Chris Lattner886dd912005-04-04 21:35:34 +0000171 if (MO.isUse() &&
172 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
173 == LoadedRegs.end()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000174 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000175 LoadedRegs.push_back(VirtReg);
176 ++NumLoads;
177 DEBUG(std::cerr << '\t' << *prior(MII));
178 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000179
Chris Lattner886dd912005-04-04 21:35:34 +0000180 if (MO.isDef()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000181 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000182 ++NumStores;
183 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000184 }
Chris Lattner886dd912005-04-04 21:35:34 +0000185 PhysRegsUsed[PhysReg] = true;
186 MI.SetMachineOperandReg(i, PhysReg);
187 } else {
188 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000189 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000190 }
Chris Lattner886dd912005-04-04 21:35:34 +0000191
Chris Lattner477e4552004-09-30 16:10:45 +0000192 DEBUG(std::cerr << '\t' << MI);
Chris Lattner4ea1b822004-09-30 02:33:48 +0000193 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000194 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000195 }
196 return true;
197}
198
199//===----------------------------------------------------------------------===//
200// Local Spiller Implementation
201//===----------------------------------------------------------------------===//
202
203namespace {
Chris Lattner7fb64342004-10-01 19:04:51 +0000204 /// LocalSpiller - This spiller does a simple pass over the machine basic
205 /// block to attempt to keep spills in registers as much as possible for
206 /// blocks that have low register pressure (the vreg may be spilled due to
207 /// register pressure in other blocks).
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000208 class LocalSpiller : public Spiller {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000209 const MRegisterInfo *MRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000210 const TargetInstrInfo *TII;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000211 public:
Chris Lattner7fb64342004-10-01 19:04:51 +0000212 bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) {
213 MRI = MF.getTarget().getRegisterInfo();
214 TII = MF.getTarget().getInstrInfo();
215 DEBUG(std::cerr << "\n**** Local spiller rewriting function '"
216 << MF.getFunction()->getName() << "':\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000217
Chris Lattner7fb64342004-10-01 19:04:51 +0000218 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
219 MBB != E; ++MBB)
220 RewriteMBB(*MBB, VRM);
221 return true;
222 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000223 private:
Chris Lattner7fb64342004-10-01 19:04:51 +0000224 void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM);
225 void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000226 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner7fb64342004-10-01 19:04:51 +0000227 void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000228 std::multimap<unsigned, int> &PhysRegs);
229 void ModifyStackSlot(int Slot, std::map<int, unsigned> &SpillSlots,
230 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000231 };
232}
233
Chris Lattner66cf80f2006-02-03 23:13:58 +0000234/// AvailableSpills - As the local spiller is scanning and rewriting an MBB from
235/// top down, keep track of which spills slots are available in each register.
Chris Lattner593c9582006-02-03 23:28:46 +0000236///
237/// Note that not all physregs are created equal here. In particular, some
238/// physregs are reloads that we are allowed to clobber or ignore at any time.
239/// Other physregs are values that the register allocated program is using that
240/// we cannot CHANGE, but we can read if we like. We keep track of this on a
241/// per-stack-slot basis as the low bit in the value of the SpillSlotsAvailable
242/// entries. The predicate 'canClobberPhysReg()' checks this bit and
243/// addAvailable sets it if.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000244class AvailableSpills {
245 const MRegisterInfo *MRI;
246 const TargetInstrInfo *TII;
247
248 // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
249 // register values that are still available, due to being loaded or stored to,
250 // but not invalidated yet.
251 std::map<int, unsigned> SpillSlotsAvailable;
252
253 // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
254 // which stack slot values are currently held by a physreg. This is used to
255 // invalidate entries in SpillSlotsAvailable when a physreg is modified.
256 std::multimap<unsigned, int> PhysRegsAvailable;
257
258 void ClobberPhysRegOnly(unsigned PhysReg);
259public:
260 AvailableSpills(const MRegisterInfo *mri, const TargetInstrInfo *tii)
261 : MRI(mri), TII(tii) {
262 }
263
264 /// getSpillSlotPhysReg - If the specified stack slot is available in a
265 /// physical register, return that PhysReg, otherwise return 0.
266 unsigned getSpillSlotPhysReg(int Slot) const {
267 std::map<int, unsigned>::const_iterator I = SpillSlotsAvailable.find(Slot);
268 if (I != SpillSlotsAvailable.end())
Chris Lattner593c9582006-02-03 23:28:46 +0000269 return I->second >> 1; // Remove the CanClobber bit.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000270 return 0;
271 }
Chris Lattner540fec62006-02-25 01:51:33 +0000272
273 const MRegisterInfo *getRegInfo() const { return MRI; }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000274
275 /// addAvailable - Mark that the specified stack slot is available in the
Chris Lattner593c9582006-02-03 23:28:46 +0000276 /// specified physreg. If CanClobber is true, the physreg can be modified at
277 /// any time without changing the semantics of the program.
278 void addAvailable(int Slot, unsigned Reg, bool CanClobber = true) {
Chris Lattner86662492006-02-03 23:50:46 +0000279 // If this stack slot is thought to be available in some other physreg,
280 // remove its record.
281 ModifyStackSlot(Slot);
282
Chris Lattner66cf80f2006-02-03 23:13:58 +0000283 PhysRegsAvailable.insert(std::make_pair(Reg, Slot));
Jeff Cohen003cecb2006-02-04 03:27:39 +0000284 SpillSlotsAvailable[Slot] = (Reg << 1) | (unsigned)CanClobber;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000285
286 DEBUG(std::cerr << "Remembering SS#" << Slot << " in physreg "
287 << MRI->getName(Reg) << "\n");
288 }
289
Chris Lattner593c9582006-02-03 23:28:46 +0000290 /// canClobberPhysReg - Return true if the spiller is allowed to change the
291 /// value of the specified stackslot register if it desires. The specified
292 /// stack slot must be available in a physreg for this query to make sense.
293 bool canClobberPhysReg(int Slot) const {
294 assert(SpillSlotsAvailable.count(Slot) && "Slot not available!");
295 return SpillSlotsAvailable.find(Slot)->second & 1;
296 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000297
298 /// ClobberPhysReg - This is called when the specified physreg changes
299 /// value. We use this to invalidate any info about stuff we thing lives in
300 /// it and any of its aliases.
301 void ClobberPhysReg(unsigned PhysReg);
302
303 /// ModifyStackSlot - This method is called when the value in a stack slot
304 /// changes. This removes information about which register the previous value
305 /// for this slot lives in (as the previous value is dead now).
306 void ModifyStackSlot(int Slot);
307};
308
309/// ClobberPhysRegOnly - This is called when the specified physreg changes
310/// value. We use this to invalidate any info about stuff we thing lives in it.
311void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
312 std::multimap<unsigned, int>::iterator I =
313 PhysRegsAvailable.lower_bound(PhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000314 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000315 int Slot = I->second;
Chris Lattner07cf1412006-02-03 00:36:31 +0000316 PhysRegsAvailable.erase(I++);
Chris Lattner593c9582006-02-03 23:28:46 +0000317 assert((SpillSlotsAvailable[Slot] >> 1) == PhysReg &&
Chris Lattner66cf80f2006-02-03 23:13:58 +0000318 "Bidirectional map mismatch!");
319 SpillSlotsAvailable.erase(Slot);
Chris Lattner7fb64342004-10-01 19:04:51 +0000320 DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
Chris Lattner07cf1412006-02-03 00:36:31 +0000321 << " clobbered, invalidating SS#" << Slot << "\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000322 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000323}
324
Chris Lattner66cf80f2006-02-03 23:13:58 +0000325/// ClobberPhysReg - This is called when the specified physreg changes
326/// value. We use this to invalidate any info about stuff we thing lives in
327/// it and any of its aliases.
328void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000329 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
Chris Lattner66cf80f2006-02-03 23:13:58 +0000330 ClobberPhysRegOnly(*AS);
331 ClobberPhysRegOnly(PhysReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000332}
333
Chris Lattner07cf1412006-02-03 00:36:31 +0000334/// ModifyStackSlot - This method is called when the value in a stack slot
335/// changes. This removes information about which register the previous value
336/// for this slot lives in (as the previous value is dead now).
Chris Lattner66cf80f2006-02-03 23:13:58 +0000337void AvailableSpills::ModifyStackSlot(int Slot) {
338 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(Slot);
339 if (It == SpillSlotsAvailable.end()) return;
Chris Lattner593c9582006-02-03 23:28:46 +0000340 unsigned Reg = It->second >> 1;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000341 SpillSlotsAvailable.erase(It);
Chris Lattner07cf1412006-02-03 00:36:31 +0000342
343 // This register may hold the value of multiple stack slots, only remove this
344 // stack slot from the set of values the register contains.
345 std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
346 for (; ; ++I) {
347 assert(I != PhysRegsAvailable.end() && I->first == Reg &&
348 "Map inverse broken!");
349 if (I->second == Slot) break;
350 }
351 PhysRegsAvailable.erase(I);
352}
353
354
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000355
Chris Lattner7fb64342004-10-01 19:04:51 +0000356// ReusedOp - For each reused operand, we keep track of a bit of information, in
357// case we need to rollback upon processing a new operand. See comments below.
358namespace {
359 struct ReusedOp {
360 // The MachineInstr operand that reused an available value.
361 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000362
Chris Lattner7fb64342004-10-01 19:04:51 +0000363 // StackSlot - The spill slot of the value being reused.
364 unsigned StackSlot;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000365
Chris Lattner7fb64342004-10-01 19:04:51 +0000366 // PhysRegReused - The physical register the value was available in.
367 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000368
Chris Lattner7fb64342004-10-01 19:04:51 +0000369 // AssignedPhysReg - The physreg that was assigned for use by the reload.
370 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000371
372 // VirtReg - The virtual register itself.
373 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000374
Chris Lattner8a61a752005-10-06 17:19:06 +0000375 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
376 unsigned vreg)
377 : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
378 VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000379 };
Chris Lattner540fec62006-02-25 01:51:33 +0000380
381 /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
382 /// is reused instead of reloaded.
383 class ReuseInfo {
384 MachineInstr &MI;
385 std::vector<ReusedOp> Reuses;
386 public:
387 ReuseInfo(MachineInstr &mi) : MI(mi) {}
388
389 bool hasReuses() const {
390 return !Reuses.empty();
391 }
392
393 /// addReuse - If we choose to reuse a virtual register that is already
394 /// available instead of reloading it, remember that we did so.
395 void addReuse(unsigned OpNo, unsigned StackSlot,
396 unsigned PhysRegReused, unsigned AssignedPhysReg,
397 unsigned VirtReg) {
398 // If the reload is to the assigned register anyway, no undo will be
399 // required.
400 if (PhysRegReused == AssignedPhysReg) return;
401
402 // Otherwise, remember this.
403 Reuses.push_back(ReusedOp(OpNo, StackSlot, PhysRegReused,
404 AssignedPhysReg, VirtReg));
405 }
406
407 /// GetRegForReload - We are about to emit a reload into PhysReg. If there
408 /// is some other operand that is using the specified register, either pick
409 /// a new register to use, or evict the previous reload and use this reg.
410 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
411 AvailableSpills &Spills,
412 std::map<int, MachineInstr*> &MaybeDeadStores) {
413 if (Reuses.empty()) return PhysReg; // This is most often empty.
414
415 for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
416 ReusedOp &Op = Reuses[ro];
417 // If we find some other reuse that was supposed to use this register
418 // exactly for its reload, we can change this reload to use ITS reload
419 // register.
420 if (Op.PhysRegReused == PhysReg) {
421 // Yup, use the reload register that we didn't use before.
Chris Lattner47cb7172006-02-25 02:03:40 +0000422 unsigned NewReg = Op.AssignedPhysReg;
423
424 // Remove the record for the previous reuse. We know it can never be
425 // invalidated now.
426 Reuses.erase(Reuses.begin()+ro);
427 return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores);
Chris Lattner540fec62006-02-25 01:51:33 +0000428 } else {
429 // Otherwise, we might also have a problem if a previously reused
430 // value aliases the new register. If so, codegen the previous reload
431 // and use this one.
432 unsigned PRRU = Op.PhysRegReused;
433 const MRegisterInfo *MRI = Spills.getRegInfo();
434 if (MRI->areAliases(PRRU, PhysReg)) {
435 // Okay, we found out that an alias of a reused register
436 // was used. This isn't good because it means we have
437 // to undo a previous reuse.
438 MachineBasicBlock *MBB = MI->getParent();
439 const TargetRegisterClass *AliasRC =
Chris Lattner28bad082006-02-25 02:17:31 +0000440 MBB->getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
441
442 // Copy Op out of the vector and remove it, we're going to insert an
443 // explicit load for it.
444 ReusedOp NewOp = Op;
445 Reuses.erase(Reuses.begin()+ro);
446
447 // Ok, we're going to try to reload the assigned physreg into the
448 // slot that we were supposed to in the first place. However, that
449 // register could hold a reuse. Check to see if it conflicts or
450 // would prefer us to use a different register.
451 unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg,
452 MI, Spills, MaybeDeadStores);
453
454 MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg,
455 NewOp.StackSlot, AliasRC);
456 Spills.ClobberPhysReg(NewPhysReg);
457 Spills.ClobberPhysReg(NewOp.PhysRegReused);
Chris Lattner540fec62006-02-25 01:51:33 +0000458
459 // Any stores to this stack slot are not dead anymore.
Chris Lattner28bad082006-02-25 02:17:31 +0000460 MaybeDeadStores.erase(NewOp.StackSlot);
Chris Lattner540fec62006-02-25 01:51:33 +0000461
Chris Lattner28bad082006-02-25 02:17:31 +0000462 MI->SetMachineOperandReg(NewOp.Operand, NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000463
Chris Lattner28bad082006-02-25 02:17:31 +0000464 Spills.addAvailable(NewOp.StackSlot, NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000465 ++NumLoads;
466 DEBUG(MachineBasicBlock::iterator MII = MI;
467 std::cerr << '\t' << *prior(MII));
468
469 DEBUG(std::cerr << "Reuse undone!\n");
Chris Lattner540fec62006-02-25 01:51:33 +0000470 --NumReused;
Chris Lattner28bad082006-02-25 02:17:31 +0000471
472 // Finally, PhysReg is now available, go ahead and use it.
Chris Lattner540fec62006-02-25 01:51:33 +0000473 return PhysReg;
474 }
475 }
476 }
477 return PhysReg;
478 }
479 };
Chris Lattner7fb64342004-10-01 19:04:51 +0000480}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000481
Chris Lattner7fb64342004-10-01 19:04:51 +0000482
483/// rewriteMBB - Keep track of which spills are available even after the
484/// register allocator is done with them. If possible, avoid reloading vregs.
485void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
486
Chris Lattner7fb64342004-10-01 19:04:51 +0000487 DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
488
Chris Lattner66cf80f2006-02-03 23:13:58 +0000489 // Spills - Keep track of which spilled values are available in physregs so
490 // that we can choose to reuse the physregs instead of emitting reloads.
491 AvailableSpills Spills(MRI, TII);
492
Chris Lattner7fb64342004-10-01 19:04:51 +0000493 // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
494 // of it. ".first" is the machine operand index (should always be 0 for now),
495 // and ".second" is the virtual register that is spilled.
496 std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
497
Chris Lattner52b25db2004-10-01 19:47:12 +0000498 // MaybeDeadStores - When we need to write a value back into a stack slot,
499 // keep track of the inserted store. If the stack slot value is never read
500 // (because the value was used from some available register, for example), and
501 // subsequently stored to, the original store is dead. This map keeps track
502 // of inserted stores that are not used. If we see a subsequent store to the
503 // same stack slot, the original store is deleted.
504 std::map<int, MachineInstr*> MaybeDeadStores;
505
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000506 bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
507
Chris Lattner7fb64342004-10-01 19:04:51 +0000508 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
509 MII != E; ) {
510 MachineInstr &MI = *MII;
511 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
512
Chris Lattner540fec62006-02-25 01:51:33 +0000513 /// ReusedOperands - Keep track of operand reuse in case we need to undo
514 /// reuse.
515 ReuseInfo ReusedOperands(MI);
516
Chris Lattner7fb64342004-10-01 19:04:51 +0000517 DefAndUseVReg.clear();
518
519 // Process all of the spilled uses and all non spilled reg references.
520 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
521 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000522 if (!MO.isRegister() || MO.getReg() == 0)
523 continue; // Ignore non-register operands.
524
525 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
526 // Ignore physregs for spilling, but remember that it is used by this
527 // function.
Chris Lattner886dd912005-04-04 21:35:34 +0000528 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner50ea01e2005-09-09 20:29:51 +0000529 continue;
530 }
531
532 assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
533 "Not a virtual or a physical register?");
534
535 unsigned VirtReg = MO.getReg();
536 if (!VRM.hasStackSlot(VirtReg)) {
537 // This virtual register was assigned a physreg!
538 unsigned Phys = VRM.getPhys(VirtReg);
539 PhysRegsUsed[Phys] = true;
540 MI.SetMachineOperandReg(i, Phys);
541 continue;
542 }
543
544 // This virtual register is now known to be a spilled value.
545 if (!MO.isUse())
546 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +0000547
Chris Lattner50ea01e2005-09-09 20:29:51 +0000548 // If this is both a def and a use, we need to emit a store to the
549 // stack slot after the instruction. Keep track of D&U operands
550 // because we are about to change it to a physreg here.
551 if (MO.isDef()) {
552 // Remember that this was a def-and-use operand, and that the
553 // stack slot is live after this instruction executes.
554 DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
555 }
556
557 int StackSlot = VRM.getStackSlot(VirtReg);
558 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000559
Chris Lattner50ea01e2005-09-09 20:29:51 +0000560 // Check to see if this stack slot is available.
Chris Lattneraddc55a2006-04-28 01:46:50 +0000561 if ((PhysReg = Spills.getSpillSlotPhysReg(StackSlot))) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000562
Chris Lattneraddc55a2006-04-28 01:46:50 +0000563 // Don't reuse it for a def&use operand if we aren't allowed to change
564 // the physreg!
565 if (!MO.isDef() || Spills.canClobberPhysReg(StackSlot)) {
566 // If this stack slot value is already available, reuse it!
567 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
568 << MRI->getName(PhysReg) << " for vreg"
569 << VirtReg <<" instead of reloading into physreg "
570 << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
571 MI.SetMachineOperandReg(i, PhysReg);
572
573 // The only technical detail we have is that we don't know that
574 // PhysReg won't be clobbered by a reloaded stack slot that occurs
575 // later in the instruction. In particular, consider 'op V1, V2'.
576 // If V1 is available in physreg R0, we would choose to reuse it
577 // here, instead of reloading it into the register the allocator
578 // indicated (say R1). However, V2 might have to be reloaded
579 // later, and it might indicate that it needs to live in R0. When
580 // this occurs, we need to have information available that
581 // indicates it is safe to use R1 for the reload instead of R0.
582 //
583 // To further complicate matters, we might conflict with an alias,
584 // or R0 and R1 might not be compatible with each other. In this
585 // case, we actually insert a reload for V1 in R1, ensuring that
586 // we can get at R0 or its alias.
587 ReusedOperands.addReuse(i, StackSlot, PhysReg,
588 VRM.getPhys(VirtReg), VirtReg);
589 ++NumReused;
590 continue;
591 }
592
593 // Otherwise we have a situation where we have a two-address instruction
594 // whose mod/ref operand needs to be reloaded. This reload is already
595 // available in some register "PhysReg", but if we used PhysReg as the
596 // operand to our 2-addr instruction, the instruction would modify
597 // PhysReg. This isn't cool if something later uses PhysReg and expects
598 // to get its initial value.
Chris Lattner50ea01e2005-09-09 20:29:51 +0000599 //
Chris Lattneraddc55a2006-04-28 01:46:50 +0000600 // To avoid this problem, and to avoid doing a load right after a store,
601 // we emit a copy from PhysReg into the designated register for this
602 // operand.
603 unsigned DesignatedReg = VRM.getPhys(VirtReg);
604 assert(DesignatedReg && "Must map virtreg to physreg!");
605
606 // Note that, if we reused a register for a previous operand, the
607 // register we want to reload into might not actually be
608 // available. If this occurs, use the register indicated by the
609 // reuser.
610 if (ReusedOperands.hasReuses())
611 DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI,
612 Spills, MaybeDeadStores);
613
614 const TargetRegisterClass* RC =
615 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
616
617 PhysRegsUsed[DesignatedReg] = true;
618 MRI->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC);
619
620 // This invalidates DesignatedReg.
621 Spills.ClobberPhysReg(DesignatedReg);
622
623 Spills.addAvailable(StackSlot, DesignatedReg);
624 MI.SetMachineOperandReg(i, DesignatedReg);
625 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000626 ++NumReused;
627 continue;
628 }
629
630 // Otherwise, reload it and remember that we have it.
631 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +0000632 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000633 const TargetRegisterClass* RC =
634 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000635
Chris Lattner50ea01e2005-09-09 20:29:51 +0000636 // Note that, if we reused a register for a previous operand, the
637 // register we want to reload into might not actually be
638 // available. If this occurs, use the register indicated by the
639 // reuser.
Chris Lattner540fec62006-02-25 01:51:33 +0000640 if (ReusedOperands.hasReuses())
641 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
642 Spills, MaybeDeadStores);
643
Chris Lattner50ea01e2005-09-09 20:29:51 +0000644 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000645 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000646 // This invalidates PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000647 Spills.ClobberPhysReg(PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000648
649 // Any stores to this stack slot are not dead anymore.
650 MaybeDeadStores.erase(StackSlot);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000651 Spills.addAvailable(StackSlot, PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000652 ++NumLoads;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000653 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000654 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000655 }
656
Chris Lattner7fb64342004-10-01 19:04:51 +0000657 // Loop over all of the implicit defs, clearing them from our available
658 // sets.
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000659 for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
660 *ImpDef; ++ImpDef) {
661 PhysRegsUsed[*ImpDef] = true;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000662 Spills.ClobberPhysReg(*ImpDef);
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000663 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000664
Chris Lattner7fb64342004-10-01 19:04:51 +0000665 DEBUG(std::cerr << '\t' << MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000666
Chris Lattner7fb64342004-10-01 19:04:51 +0000667 // If we have folded references to memory operands, make sure we clear all
668 // physical registers that may contain the value of the spilled virtual
669 // register
Chris Lattner8f1d6402005-01-14 15:54:24 +0000670 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
671 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000672 DEBUG(std::cerr << "Folded vreg: " << I->second.first << " MR: "
673 << I->second.second);
674 unsigned VirtReg = I->second.first;
675 VirtRegMap::ModRef MR = I->second.second;
Chris Lattnercea86882005-09-19 06:56:21 +0000676 if (!VRM.hasStackSlot(VirtReg)) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000677 DEBUG(std::cerr << ": No stack slot!\n");
Chris Lattnercea86882005-09-19 06:56:21 +0000678 continue;
679 }
680 int SS = VRM.getStackSlot(VirtReg);
681 DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
682
683 // If this folded instruction is just a use, check to see if it's a
684 // straight load from the virt reg slot.
685 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
686 int FrameIdx;
Chris Lattner40839602006-02-02 20:12:32 +0000687 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
688 // If this spill slot is available, turn it into a copy (or nothing)
689 // instead of leaving it as a load!
Chris Lattner66cf80f2006-02-03 23:13:58 +0000690 unsigned InReg;
691 if (FrameIdx == SS && (InReg = Spills.getSpillSlotPhysReg(SS))) {
Chris Lattnercea86882005-09-19 06:56:21 +0000692 DEBUG(std::cerr << "Promoted Load To Copy: " << MI);
693 MachineFunction &MF = *MBB.getParent();
Chris Lattner66cf80f2006-02-03 23:13:58 +0000694 if (DestReg != InReg) {
695 MRI->copyRegToReg(MBB, &MI, DestReg, InReg,
Chris Lattnercea86882005-09-19 06:56:21 +0000696 MF.getSSARegMap()->getRegClass(VirtReg));
Chris Lattner22480c42005-10-05 18:30:19 +0000697 // Revisit the copy so we make sure to notice the effects of the
698 // operation on the destreg (either needing to RA it if it's
699 // virtual or needing to clobber any values if it's physical).
700 NextMII = &MI;
701 --NextMII; // backtrack to the copy.
Chris Lattnercea86882005-09-19 06:56:21 +0000702 }
703 MBB.erase(&MI);
704 goto ProcessNextInst;
705 }
706 }
707 }
708
709 // If this reference is not a use, any previous store is now dead.
710 // Otherwise, the store to this stack slot is not dead anymore.
711 std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
712 if (MDSI != MaybeDeadStores.end()) {
713 if (MR & VirtRegMap::isRef) // Previous store is not dead.
714 MaybeDeadStores.erase(MDSI);
715 else {
716 // If we get here, the store is dead, nuke it now.
717 assert(MR == VirtRegMap::isMod && "Can't be modref!");
718 MBB.erase(MDSI->second);
719 MaybeDeadStores.erase(MDSI);
720 ++NumDSE;
721 }
722 }
723
724 // If the spill slot value is available, and this is a new definition of
725 // the value, the value is not available anymore.
726 if (MR & VirtRegMap::isMod) {
Chris Lattner07cf1412006-02-03 00:36:31 +0000727 // Notice that the value in this stack slot has been modified.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000728 Spills.ModifyStackSlot(SS);
Chris Lattnercd816392006-02-02 23:29:36 +0000729
730 // If this is *just* a mod of the value, check to see if this is just a
731 // store to the spill slot (i.e. the spill got merged into the copy). If
732 // so, realize that the vreg is available now, and add the store to the
733 // MaybeDeadStore info.
734 int StackSlot;
735 if (!(MR & VirtRegMap::isRef)) {
736 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
737 assert(MRegisterInfo::isPhysicalRegister(SrcReg) &&
738 "Src hasn't been allocated yet?");
Chris Lattner07cf1412006-02-03 00:36:31 +0000739 // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark
Chris Lattnercd816392006-02-02 23:29:36 +0000740 // this as a potentially dead store in case there is a subsequent
741 // store into the stack slot without a read from it.
742 MaybeDeadStores[StackSlot] = &MI;
743
Chris Lattnercd816392006-02-02 23:29:36 +0000744 // If the stack slot value was previously available in some other
745 // register, change it now. Otherwise, make the register available,
746 // in PhysReg.
Chris Lattner593c9582006-02-03 23:28:46 +0000747 Spills.addAvailable(StackSlot, SrcReg, false /*don't clobber*/);
Chris Lattnercd816392006-02-02 23:29:36 +0000748 }
749 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000750 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000751 }
752
Chris Lattner7fb64342004-10-01 19:04:51 +0000753 // Process all of the spilled defs.
754 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
755 MachineOperand &MO = MI.getOperand(i);
756 if (MO.isRegister() && MO.getReg() && MO.isDef()) {
757 unsigned VirtReg = MO.getReg();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000758
Chris Lattner7fb64342004-10-01 19:04:51 +0000759 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
760 // Check to see if this is a def-and-use vreg operand that we do need
761 // to insert a store for.
762 bool OpTakenCareOf = false;
763 if (MO.isUse() && !DefAndUseVReg.empty()) {
764 for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
765 if (DefAndUseVReg[dau].first == i) {
766 VirtReg = DefAndUseVReg[dau].second;
767 OpTakenCareOf = true;
768 break;
769 }
770 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000771
Chris Lattner7fb64342004-10-01 19:04:51 +0000772 if (!OpTakenCareOf) {
Chris Lattner109afed2006-02-03 03:16:14 +0000773 // Check to see if this is a noop copy. If so, eliminate the
774 // instruction before considering the dest reg to be changed.
775 unsigned Src, Dst;
776 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
777 ++NumDCE;
778 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
779 MBB.erase(&MI);
780 goto ProcessNextInst;
781 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000782 Spills.ClobberPhysReg(VirtReg);
Chris Lattner84e752a2006-02-03 03:06:49 +0000783 continue;
Chris Lattner7fb64342004-10-01 19:04:51 +0000784 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000785 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000786
Chris Lattner84e752a2006-02-03 03:06:49 +0000787 // The only vregs left are stack slot definitions.
788 int StackSlot = VRM.getStackSlot(VirtReg);
789 const TargetRegisterClass *RC =
790 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
791 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000792
Chris Lattner84e752a2006-02-03 03:06:49 +0000793 // If this is a def&use operand, and we used a different physreg for
794 // it than the one assigned, make sure to execute the store from the
795 // correct physical register.
796 if (MO.getReg() == VirtReg)
797 PhysReg = VRM.getPhys(VirtReg);
798 else
799 PhysReg = MO.getReg();
Chris Lattner7fb64342004-10-01 19:04:51 +0000800
Chris Lattner84e752a2006-02-03 03:06:49 +0000801 PhysRegsUsed[PhysReg] = true;
802 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
803 DEBUG(std::cerr << "Store:\t" << *next(MII));
804 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000805
Chris Lattner109afed2006-02-03 03:16:14 +0000806 // Check to see if this is a noop copy. If so, eliminate the
807 // instruction before considering the dest reg to be changed.
808 {
809 unsigned Src, Dst;
810 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
811 ++NumDCE;
812 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
813 MBB.erase(&MI);
814 goto ProcessNextInst;
815 }
816 }
817
Chris Lattner84e752a2006-02-03 03:06:49 +0000818 // If there is a dead store to this stack slot, nuke it now.
819 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
820 if (LastStore) {
821 DEBUG(std::cerr << " Killed store:\t" << *LastStore);
822 ++NumDSE;
823 MBB.erase(LastStore);
Chris Lattner7fb64342004-10-01 19:04:51 +0000824 }
Chris Lattner84e752a2006-02-03 03:06:49 +0000825 LastStore = next(MII);
826
827 // If the stack slot value was previously available in some other
828 // register, change it now. Otherwise, make the register available,
829 // in PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000830 Spills.ModifyStackSlot(StackSlot);
831 Spills.ClobberPhysReg(PhysReg);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000832 Spills.addAvailable(StackSlot, PhysReg);
Chris Lattner84e752a2006-02-03 03:06:49 +0000833 ++NumStores;
Chris Lattner7fb64342004-10-01 19:04:51 +0000834 }
835 }
Chris Lattnercea86882005-09-19 06:56:21 +0000836 ProcessNextInst:
Chris Lattner7fb64342004-10-01 19:04:51 +0000837 MII = NextMII;
838 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000839}
840
841
Chris Lattner7fb64342004-10-01 19:04:51 +0000842
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000843llvm::Spiller* llvm::createSpiller() {
844 switch (SpillerOpt) {
845 default: assert(0 && "Unreachable!");
846 case local:
847 return new LocalSpiller();
848 case simple:
849 return new SimpleSpiller();
850 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000851}