blob: 6122dc1bb499befcb767f4187ce518041d766678 [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));
Evan Cheng200370f2006-04-30 08:41:47 +000053
54 // TEMPORARY option to test a fix.
55 cl::opt<bool>
56 SpillerCheckLiveOut("spiller-check-liveout", cl::Hidden);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000057}
58
Chris Lattner8c4d88d2004-09-30 01:54:45 +000059//===----------------------------------------------------------------------===//
60// VirtRegMap implementation
61//===----------------------------------------------------------------------===//
62
63void VirtRegMap::grow() {
Chris Lattner7f690e62004-09-30 02:15:18 +000064 Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
65 Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000066}
67
Chris Lattner8c4d88d2004-09-30 01:54:45 +000068int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
69 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000070 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000071 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000072 const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
73 int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
74 RC->getAlignment());
75 Virt2StackSlotMap[virtReg] = frameIndex;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000076 ++NumSpills;
77 return frameIndex;
78}
79
80void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
81 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000082 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000083 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000084 Virt2StackSlotMap[virtReg] = frameIndex;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +000085}
86
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000087void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Cheng200370f2006-04-30 08:41:47 +000088 unsigned OpNo, MachineInstr *NewMI,
89 bool LiveOut) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000090 // Move previous memory references folded to new instruction.
91 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +000092 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000093 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
94 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +000095 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +000096 }
Chris Lattnerdbea9732004-09-30 16:35:08 +000097
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000098 ModRef MRInfo;
99 if (!OldMI->getOperand(OpNo).isDef()) {
100 assert(OldMI->getOperand(OpNo).isUse() && "Operand is not use or def?");
101 MRInfo = isRef;
102 } else {
103 MRInfo = OldMI->getOperand(OpNo).isUse() ? isModRef : isMod;
Evan Cheng200370f2006-04-30 08:41:47 +0000104 if (LiveOut) MRInfo = (ModRef)(MRInfo | isLiveOut);
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000105 }
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000106
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000107 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000108 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000109}
110
Chris Lattner7f690e62004-09-30 02:15:18 +0000111void VirtRegMap::print(std::ostream &OS) const {
112 const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000113
Chris Lattner7f690e62004-09-30 02:15:18 +0000114 OS << "********** REGISTER MAP **********\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000115 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000116 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
117 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
118 OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000119
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000120 }
121
122 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000123 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
124 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
125 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
126 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000127}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000128
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000129void VirtRegMap::dump() const { print(std::cerr); }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000130
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000131
132//===----------------------------------------------------------------------===//
133// Simple Spiller Implementation
134//===----------------------------------------------------------------------===//
135
136Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000137
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000138namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000139 struct SimpleSpiller : public Spiller {
140 bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM);
141 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000142}
143
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000144bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF,
145 const VirtRegMap &VRM) {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000146 DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
147 DEBUG(std::cerr << "********** Function: "
148 << MF.getFunction()->getName() << '\n');
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000149 const TargetMachine &TM = MF.getTarget();
150 const MRegisterInfo &MRI = *TM.getRegisterInfo();
151 bool *PhysRegsUsed = MF.getUsedPhysregs();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000152
Chris Lattner4ea1b822004-09-30 02:33:48 +0000153 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
154 // each vreg once (in the case where a spilled vreg is used by multiple
155 // operands). This is always smaller than the number of operands to the
156 // current machine instr, so it should be small.
157 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000158
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000159 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
160 MBBI != E; ++MBBI) {
161 DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n");
162 MachineBasicBlock &MBB = *MBBI;
163 for (MachineBasicBlock::iterator MII = MBB.begin(),
164 E = MBB.end(); MII != E; ++MII) {
165 MachineInstr &MI = *MII;
166 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000167 MachineOperand &MO = MI.getOperand(i);
Chris Lattner886dd912005-04-04 21:35:34 +0000168 if (MO.isRegister() && MO.getReg())
169 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
170 unsigned VirtReg = MO.getReg();
171 unsigned PhysReg = VRM.getPhys(VirtReg);
172 if (VRM.hasStackSlot(VirtReg)) {
173 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000174 const TargetRegisterClass* RC =
175 MF.getSSARegMap()->getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000176
Chris Lattner886dd912005-04-04 21:35:34 +0000177 if (MO.isUse() &&
178 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
179 == LoadedRegs.end()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000180 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000181 LoadedRegs.push_back(VirtReg);
182 ++NumLoads;
183 DEBUG(std::cerr << '\t' << *prior(MII));
184 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000185
Chris Lattner886dd912005-04-04 21:35:34 +0000186 if (MO.isDef()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000187 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000188 ++NumStores;
189 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000190 }
Chris Lattner886dd912005-04-04 21:35:34 +0000191 PhysRegsUsed[PhysReg] = true;
192 MI.SetMachineOperandReg(i, PhysReg);
193 } else {
194 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000195 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000196 }
Chris Lattner886dd912005-04-04 21:35:34 +0000197
Chris Lattner477e4552004-09-30 16:10:45 +0000198 DEBUG(std::cerr << '\t' << MI);
Chris Lattner4ea1b822004-09-30 02:33:48 +0000199 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000200 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000201 }
202 return true;
203}
204
205//===----------------------------------------------------------------------===//
206// Local Spiller Implementation
207//===----------------------------------------------------------------------===//
208
209namespace {
Chris Lattner7fb64342004-10-01 19:04:51 +0000210 /// LocalSpiller - This spiller does a simple pass over the machine basic
211 /// block to attempt to keep spills in registers as much as possible for
212 /// blocks that have low register pressure (the vreg may be spilled due to
213 /// register pressure in other blocks).
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000214 class LocalSpiller : public Spiller {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000215 const MRegisterInfo *MRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000216 const TargetInstrInfo *TII;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000217 public:
Chris Lattner7fb64342004-10-01 19:04:51 +0000218 bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) {
219 MRI = MF.getTarget().getRegisterInfo();
220 TII = MF.getTarget().getInstrInfo();
221 DEBUG(std::cerr << "\n**** Local spiller rewriting function '"
222 << MF.getFunction()->getName() << "':\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000223
Chris Lattner7fb64342004-10-01 19:04:51 +0000224 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
225 MBB != E; ++MBB)
226 RewriteMBB(*MBB, VRM);
227 return true;
228 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000229 private:
Chris Lattner7fb64342004-10-01 19:04:51 +0000230 void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM);
231 void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000232 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner7fb64342004-10-01 19:04:51 +0000233 void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000234 std::multimap<unsigned, int> &PhysRegs);
235 void ModifyStackSlot(int Slot, std::map<int, unsigned> &SpillSlots,
236 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000237 };
238}
239
Chris Lattner66cf80f2006-02-03 23:13:58 +0000240/// AvailableSpills - As the local spiller is scanning and rewriting an MBB from
241/// top down, keep track of which spills slots are available in each register.
Chris Lattner593c9582006-02-03 23:28:46 +0000242///
243/// Note that not all physregs are created equal here. In particular, some
244/// physregs are reloads that we are allowed to clobber or ignore at any time.
245/// Other physregs are values that the register allocated program is using that
246/// we cannot CHANGE, but we can read if we like. We keep track of this on a
247/// per-stack-slot basis as the low bit in the value of the SpillSlotsAvailable
248/// entries. The predicate 'canClobberPhysReg()' checks this bit and
249/// addAvailable sets it if.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000250class AvailableSpills {
251 const MRegisterInfo *MRI;
252 const TargetInstrInfo *TII;
253
254 // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
255 // register values that are still available, due to being loaded or stored to,
256 // but not invalidated yet.
257 std::map<int, unsigned> SpillSlotsAvailable;
258
259 // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
260 // which stack slot values are currently held by a physreg. This is used to
261 // invalidate entries in SpillSlotsAvailable when a physreg is modified.
262 std::multimap<unsigned, int> PhysRegsAvailable;
263
264 void ClobberPhysRegOnly(unsigned PhysReg);
265public:
266 AvailableSpills(const MRegisterInfo *mri, const TargetInstrInfo *tii)
267 : MRI(mri), TII(tii) {
268 }
269
270 /// getSpillSlotPhysReg - If the specified stack slot is available in a
271 /// physical register, return that PhysReg, otherwise return 0.
272 unsigned getSpillSlotPhysReg(int Slot) const {
273 std::map<int, unsigned>::const_iterator I = SpillSlotsAvailable.find(Slot);
274 if (I != SpillSlotsAvailable.end())
Chris Lattner593c9582006-02-03 23:28:46 +0000275 return I->second >> 1; // Remove the CanClobber bit.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000276 return 0;
277 }
Chris Lattner540fec62006-02-25 01:51:33 +0000278
279 const MRegisterInfo *getRegInfo() const { return MRI; }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000280
281 /// addAvailable - Mark that the specified stack slot is available in the
Chris Lattner593c9582006-02-03 23:28:46 +0000282 /// specified physreg. If CanClobber is true, the physreg can be modified at
283 /// any time without changing the semantics of the program.
284 void addAvailable(int Slot, unsigned Reg, bool CanClobber = true) {
Chris Lattner86662492006-02-03 23:50:46 +0000285 // If this stack slot is thought to be available in some other physreg,
286 // remove its record.
287 ModifyStackSlot(Slot);
288
Chris Lattner66cf80f2006-02-03 23:13:58 +0000289 PhysRegsAvailable.insert(std::make_pair(Reg, Slot));
Jeff Cohen003cecb2006-02-04 03:27:39 +0000290 SpillSlotsAvailable[Slot] = (Reg << 1) | (unsigned)CanClobber;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000291
292 DEBUG(std::cerr << "Remembering SS#" << Slot << " in physreg "
293 << MRI->getName(Reg) << "\n");
294 }
295
Chris Lattner593c9582006-02-03 23:28:46 +0000296 /// canClobberPhysReg - Return true if the spiller is allowed to change the
297 /// value of the specified stackslot register if it desires. The specified
298 /// stack slot must be available in a physreg for this query to make sense.
299 bool canClobberPhysReg(int Slot) const {
300 assert(SpillSlotsAvailable.count(Slot) && "Slot not available!");
301 return SpillSlotsAvailable.find(Slot)->second & 1;
302 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000303
304 /// ClobberPhysReg - This is called when the specified physreg changes
305 /// value. We use this to invalidate any info about stuff we thing lives in
306 /// it and any of its aliases.
307 void ClobberPhysReg(unsigned PhysReg);
308
309 /// ModifyStackSlot - This method is called when the value in a stack slot
310 /// changes. This removes information about which register the previous value
311 /// for this slot lives in (as the previous value is dead now).
312 void ModifyStackSlot(int Slot);
313};
314
315/// ClobberPhysRegOnly - This is called when the specified physreg changes
316/// value. We use this to invalidate any info about stuff we thing lives in it.
317void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
318 std::multimap<unsigned, int>::iterator I =
319 PhysRegsAvailable.lower_bound(PhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000320 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000321 int Slot = I->second;
Chris Lattner07cf1412006-02-03 00:36:31 +0000322 PhysRegsAvailable.erase(I++);
Chris Lattner593c9582006-02-03 23:28:46 +0000323 assert((SpillSlotsAvailable[Slot] >> 1) == PhysReg &&
Chris Lattner66cf80f2006-02-03 23:13:58 +0000324 "Bidirectional map mismatch!");
325 SpillSlotsAvailable.erase(Slot);
Chris Lattner7fb64342004-10-01 19:04:51 +0000326 DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
Chris Lattner07cf1412006-02-03 00:36:31 +0000327 << " clobbered, invalidating SS#" << Slot << "\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000328 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000329}
330
Chris Lattner66cf80f2006-02-03 23:13:58 +0000331/// ClobberPhysReg - This is called when the specified physreg changes
332/// value. We use this to invalidate any info about stuff we thing lives in
333/// it and any of its aliases.
334void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000335 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
Chris Lattner66cf80f2006-02-03 23:13:58 +0000336 ClobberPhysRegOnly(*AS);
337 ClobberPhysRegOnly(PhysReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000338}
339
Chris Lattner07cf1412006-02-03 00:36:31 +0000340/// ModifyStackSlot - This method is called when the value in a stack slot
341/// changes. This removes information about which register the previous value
342/// for this slot lives in (as the previous value is dead now).
Chris Lattner66cf80f2006-02-03 23:13:58 +0000343void AvailableSpills::ModifyStackSlot(int Slot) {
344 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(Slot);
345 if (It == SpillSlotsAvailable.end()) return;
Chris Lattner593c9582006-02-03 23:28:46 +0000346 unsigned Reg = It->second >> 1;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000347 SpillSlotsAvailable.erase(It);
Chris Lattner07cf1412006-02-03 00:36:31 +0000348
349 // This register may hold the value of multiple stack slots, only remove this
350 // stack slot from the set of values the register contains.
351 std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
352 for (; ; ++I) {
353 assert(I != PhysRegsAvailable.end() && I->first == Reg &&
354 "Map inverse broken!");
355 if (I->second == Slot) break;
356 }
357 PhysRegsAvailable.erase(I);
358}
359
360
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000361
Chris Lattner7fb64342004-10-01 19:04:51 +0000362// ReusedOp - For each reused operand, we keep track of a bit of information, in
363// case we need to rollback upon processing a new operand. See comments below.
364namespace {
365 struct ReusedOp {
366 // The MachineInstr operand that reused an available value.
367 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000368
Chris Lattner7fb64342004-10-01 19:04:51 +0000369 // StackSlot - The spill slot of the value being reused.
370 unsigned StackSlot;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000371
Chris Lattner7fb64342004-10-01 19:04:51 +0000372 // PhysRegReused - The physical register the value was available in.
373 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000374
Chris Lattner7fb64342004-10-01 19:04:51 +0000375 // AssignedPhysReg - The physreg that was assigned for use by the reload.
376 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000377
378 // VirtReg - The virtual register itself.
379 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000380
Chris Lattner8a61a752005-10-06 17:19:06 +0000381 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
382 unsigned vreg)
383 : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
384 VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000385 };
Chris Lattner540fec62006-02-25 01:51:33 +0000386
387 /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
388 /// is reused instead of reloaded.
389 class ReuseInfo {
390 MachineInstr &MI;
391 std::vector<ReusedOp> Reuses;
392 public:
393 ReuseInfo(MachineInstr &mi) : MI(mi) {}
394
395 bool hasReuses() const {
396 return !Reuses.empty();
397 }
398
399 /// addReuse - If we choose to reuse a virtual register that is already
400 /// available instead of reloading it, remember that we did so.
401 void addReuse(unsigned OpNo, unsigned StackSlot,
402 unsigned PhysRegReused, unsigned AssignedPhysReg,
403 unsigned VirtReg) {
404 // If the reload is to the assigned register anyway, no undo will be
405 // required.
406 if (PhysRegReused == AssignedPhysReg) return;
407
408 // Otherwise, remember this.
409 Reuses.push_back(ReusedOp(OpNo, StackSlot, PhysRegReused,
410 AssignedPhysReg, VirtReg));
411 }
412
413 /// GetRegForReload - We are about to emit a reload into PhysReg. If there
414 /// is some other operand that is using the specified register, either pick
415 /// a new register to use, or evict the previous reload and use this reg.
416 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
417 AvailableSpills &Spills,
418 std::map<int, MachineInstr*> &MaybeDeadStores) {
419 if (Reuses.empty()) return PhysReg; // This is most often empty.
420
421 for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
422 ReusedOp &Op = Reuses[ro];
423 // If we find some other reuse that was supposed to use this register
424 // exactly for its reload, we can change this reload to use ITS reload
425 // register.
426 if (Op.PhysRegReused == PhysReg) {
427 // Yup, use the reload register that we didn't use before.
Chris Lattner47cb7172006-02-25 02:03:40 +0000428 unsigned NewReg = Op.AssignedPhysReg;
429
430 // Remove the record for the previous reuse. We know it can never be
431 // invalidated now.
432 Reuses.erase(Reuses.begin()+ro);
433 return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores);
Chris Lattner540fec62006-02-25 01:51:33 +0000434 } else {
435 // Otherwise, we might also have a problem if a previously reused
436 // value aliases the new register. If so, codegen the previous reload
437 // and use this one.
438 unsigned PRRU = Op.PhysRegReused;
439 const MRegisterInfo *MRI = Spills.getRegInfo();
440 if (MRI->areAliases(PRRU, PhysReg)) {
441 // Okay, we found out that an alias of a reused register
442 // was used. This isn't good because it means we have
443 // to undo a previous reuse.
444 MachineBasicBlock *MBB = MI->getParent();
445 const TargetRegisterClass *AliasRC =
Chris Lattner28bad082006-02-25 02:17:31 +0000446 MBB->getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
447
448 // Copy Op out of the vector and remove it, we're going to insert an
449 // explicit load for it.
450 ReusedOp NewOp = Op;
451 Reuses.erase(Reuses.begin()+ro);
452
453 // Ok, we're going to try to reload the assigned physreg into the
454 // slot that we were supposed to in the first place. However, that
455 // register could hold a reuse. Check to see if it conflicts or
456 // would prefer us to use a different register.
457 unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg,
458 MI, Spills, MaybeDeadStores);
459
460 MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg,
461 NewOp.StackSlot, AliasRC);
462 Spills.ClobberPhysReg(NewPhysReg);
463 Spills.ClobberPhysReg(NewOp.PhysRegReused);
Chris Lattner540fec62006-02-25 01:51:33 +0000464
465 // Any stores to this stack slot are not dead anymore.
Chris Lattner28bad082006-02-25 02:17:31 +0000466 MaybeDeadStores.erase(NewOp.StackSlot);
Chris Lattner540fec62006-02-25 01:51:33 +0000467
Chris Lattner28bad082006-02-25 02:17:31 +0000468 MI->SetMachineOperandReg(NewOp.Operand, NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000469
Chris Lattner28bad082006-02-25 02:17:31 +0000470 Spills.addAvailable(NewOp.StackSlot, NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000471 ++NumLoads;
472 DEBUG(MachineBasicBlock::iterator MII = MI;
473 std::cerr << '\t' << *prior(MII));
474
475 DEBUG(std::cerr << "Reuse undone!\n");
Chris Lattner540fec62006-02-25 01:51:33 +0000476 --NumReused;
Chris Lattner28bad082006-02-25 02:17:31 +0000477
478 // Finally, PhysReg is now available, go ahead and use it.
Chris Lattner540fec62006-02-25 01:51:33 +0000479 return PhysReg;
480 }
481 }
482 }
483 return PhysReg;
484 }
485 };
Chris Lattner7fb64342004-10-01 19:04:51 +0000486}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000487
Chris Lattner7fb64342004-10-01 19:04:51 +0000488
489/// rewriteMBB - Keep track of which spills are available even after the
490/// register allocator is done with them. If possible, avoid reloading vregs.
491void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
492
Chris Lattner7fb64342004-10-01 19:04:51 +0000493 DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
494
Chris Lattner66cf80f2006-02-03 23:13:58 +0000495 // Spills - Keep track of which spilled values are available in physregs so
496 // that we can choose to reuse the physregs instead of emitting reloads.
497 AvailableSpills Spills(MRI, TII);
498
Chris Lattner7fb64342004-10-01 19:04:51 +0000499 // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
500 // of it. ".first" is the machine operand index (should always be 0 for now),
501 // and ".second" is the virtual register that is spilled.
502 std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
503
Chris Lattner52b25db2004-10-01 19:47:12 +0000504 // MaybeDeadStores - When we need to write a value back into a stack slot,
505 // keep track of the inserted store. If the stack slot value is never read
506 // (because the value was used from some available register, for example), and
507 // subsequently stored to, the original store is dead. This map keeps track
508 // of inserted stores that are not used. If we see a subsequent store to the
509 // same stack slot, the original store is deleted.
510 std::map<int, MachineInstr*> MaybeDeadStores;
511
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000512 bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
513
Chris Lattner7fb64342004-10-01 19:04:51 +0000514 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
515 MII != E; ) {
516 MachineInstr &MI = *MII;
517 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
518
Chris Lattner540fec62006-02-25 01:51:33 +0000519 /// ReusedOperands - Keep track of operand reuse in case we need to undo
520 /// reuse.
521 ReuseInfo ReusedOperands(MI);
522
Chris Lattner7fb64342004-10-01 19:04:51 +0000523 DefAndUseVReg.clear();
524
525 // Process all of the spilled uses and all non spilled reg references.
526 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
527 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000528 if (!MO.isRegister() || MO.getReg() == 0)
529 continue; // Ignore non-register operands.
530
531 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
532 // Ignore physregs for spilling, but remember that it is used by this
533 // function.
Chris Lattner886dd912005-04-04 21:35:34 +0000534 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner50ea01e2005-09-09 20:29:51 +0000535 continue;
536 }
537
538 assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
539 "Not a virtual or a physical register?");
540
541 unsigned VirtReg = MO.getReg();
542 if (!VRM.hasStackSlot(VirtReg)) {
543 // This virtual register was assigned a physreg!
544 unsigned Phys = VRM.getPhys(VirtReg);
545 PhysRegsUsed[Phys] = true;
546 MI.SetMachineOperandReg(i, Phys);
547 continue;
548 }
549
550 // This virtual register is now known to be a spilled value.
551 if (!MO.isUse())
552 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +0000553
Chris Lattner50ea01e2005-09-09 20:29:51 +0000554 // If this is both a def and a use, we need to emit a store to the
555 // stack slot after the instruction. Keep track of D&U operands
556 // because we are about to change it to a physreg here.
557 if (MO.isDef()) {
558 // Remember that this was a def-and-use operand, and that the
559 // stack slot is live after this instruction executes.
560 DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
561 }
562
563 int StackSlot = VRM.getStackSlot(VirtReg);
564 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000565
Chris Lattner50ea01e2005-09-09 20:29:51 +0000566 // Check to see if this stack slot is available.
Chris Lattneraddc55a2006-04-28 01:46:50 +0000567 if ((PhysReg = Spills.getSpillSlotPhysReg(StackSlot))) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000568
Chris Lattneraddc55a2006-04-28 01:46:50 +0000569 // Don't reuse it for a def&use operand if we aren't allowed to change
570 // the physreg!
571 if (!MO.isDef() || Spills.canClobberPhysReg(StackSlot)) {
572 // If this stack slot value is already available, reuse it!
573 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
574 << MRI->getName(PhysReg) << " for vreg"
575 << VirtReg <<" instead of reloading into physreg "
576 << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
577 MI.SetMachineOperandReg(i, PhysReg);
578
579 // The only technical detail we have is that we don't know that
580 // PhysReg won't be clobbered by a reloaded stack slot that occurs
581 // later in the instruction. In particular, consider 'op V1, V2'.
582 // If V1 is available in physreg R0, we would choose to reuse it
583 // here, instead of reloading it into the register the allocator
584 // indicated (say R1). However, V2 might have to be reloaded
585 // later, and it might indicate that it needs to live in R0. When
586 // this occurs, we need to have information available that
587 // indicates it is safe to use R1 for the reload instead of R0.
588 //
589 // To further complicate matters, we might conflict with an alias,
590 // or R0 and R1 might not be compatible with each other. In this
591 // case, we actually insert a reload for V1 in R1, ensuring that
592 // we can get at R0 or its alias.
593 ReusedOperands.addReuse(i, StackSlot, PhysReg,
594 VRM.getPhys(VirtReg), VirtReg);
595 ++NumReused;
596 continue;
597 }
598
599 // Otherwise we have a situation where we have a two-address instruction
600 // whose mod/ref operand needs to be reloaded. This reload is already
601 // available in some register "PhysReg", but if we used PhysReg as the
602 // operand to our 2-addr instruction, the instruction would modify
603 // PhysReg. This isn't cool if something later uses PhysReg and expects
604 // to get its initial value.
Chris Lattner50ea01e2005-09-09 20:29:51 +0000605 //
Chris Lattneraddc55a2006-04-28 01:46:50 +0000606 // To avoid this problem, and to avoid doing a load right after a store,
607 // we emit a copy from PhysReg into the designated register for this
608 // operand.
609 unsigned DesignatedReg = VRM.getPhys(VirtReg);
610 assert(DesignatedReg && "Must map virtreg to physreg!");
611
612 // Note that, if we reused a register for a previous operand, the
613 // register we want to reload into might not actually be
614 // available. If this occurs, use the register indicated by the
615 // reuser.
616 if (ReusedOperands.hasReuses())
617 DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI,
618 Spills, MaybeDeadStores);
619
Chris Lattnerba1fc3d2006-04-28 04:43:18 +0000620 // If the mapped designated register is actually the physreg we have
621 // incoming, we don't need to inserted a dead copy.
622 if (DesignatedReg == PhysReg) {
623 // If this stack slot value is already available, reuse it!
624 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
625 << MRI->getName(PhysReg) << " for vreg"
626 << VirtReg
627 << " instead of reloading into same physreg.\n");
628 MI.SetMachineOperandReg(i, PhysReg);
629 ++NumReused;
630 continue;
631 }
632
Chris Lattneraddc55a2006-04-28 01:46:50 +0000633 const TargetRegisterClass* RC =
634 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
635
636 PhysRegsUsed[DesignatedReg] = true;
637 MRI->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC);
638
639 // This invalidates DesignatedReg.
640 Spills.ClobberPhysReg(DesignatedReg);
641
642 Spills.addAvailable(StackSlot, DesignatedReg);
643 MI.SetMachineOperandReg(i, DesignatedReg);
644 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000645 ++NumReused;
646 continue;
647 }
648
649 // Otherwise, reload it and remember that we have it.
650 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +0000651 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000652 const TargetRegisterClass* RC =
653 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000654
Chris Lattner50ea01e2005-09-09 20:29:51 +0000655 // Note that, if we reused a register for a previous operand, the
656 // register we want to reload into might not actually be
657 // available. If this occurs, use the register indicated by the
658 // reuser.
Chris Lattner540fec62006-02-25 01:51:33 +0000659 if (ReusedOperands.hasReuses())
660 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
661 Spills, MaybeDeadStores);
662
Chris Lattner50ea01e2005-09-09 20:29:51 +0000663 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000664 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000665 // This invalidates PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000666 Spills.ClobberPhysReg(PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000667
668 // Any stores to this stack slot are not dead anymore.
669 MaybeDeadStores.erase(StackSlot);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000670 Spills.addAvailable(StackSlot, PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000671 ++NumLoads;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000672 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000673 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000674 }
675
Chris Lattner7fb64342004-10-01 19:04:51 +0000676 // Loop over all of the implicit defs, clearing them from our available
677 // sets.
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000678 for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
679 *ImpDef; ++ImpDef) {
680 PhysRegsUsed[*ImpDef] = true;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000681 Spills.ClobberPhysReg(*ImpDef);
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000682 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000683
Chris Lattner7fb64342004-10-01 19:04:51 +0000684 DEBUG(std::cerr << '\t' << MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000685
Chris Lattner7fb64342004-10-01 19:04:51 +0000686 // If we have folded references to memory operands, make sure we clear all
687 // physical registers that may contain the value of the spilled virtual
688 // register
Chris Lattner8f1d6402005-01-14 15:54:24 +0000689 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
690 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000691 DEBUG(std::cerr << "Folded vreg: " << I->second.first << " MR: "
692 << I->second.second);
693 unsigned VirtReg = I->second.first;
694 VirtRegMap::ModRef MR = I->second.second;
Chris Lattnercea86882005-09-19 06:56:21 +0000695 if (!VRM.hasStackSlot(VirtReg)) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000696 DEBUG(std::cerr << ": No stack slot!\n");
Chris Lattnercea86882005-09-19 06:56:21 +0000697 continue;
698 }
699 int SS = VRM.getStackSlot(VirtReg);
700 DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
701
702 // If this folded instruction is just a use, check to see if it's a
703 // straight load from the virt reg slot.
704 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
705 int FrameIdx;
Chris Lattner40839602006-02-02 20:12:32 +0000706 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
707 // If this spill slot is available, turn it into a copy (or nothing)
708 // instead of leaving it as a load!
Chris Lattner66cf80f2006-02-03 23:13:58 +0000709 unsigned InReg;
710 if (FrameIdx == SS && (InReg = Spills.getSpillSlotPhysReg(SS))) {
Chris Lattnercea86882005-09-19 06:56:21 +0000711 DEBUG(std::cerr << "Promoted Load To Copy: " << MI);
712 MachineFunction &MF = *MBB.getParent();
Chris Lattner66cf80f2006-02-03 23:13:58 +0000713 if (DestReg != InReg) {
714 MRI->copyRegToReg(MBB, &MI, DestReg, InReg,
Chris Lattnercea86882005-09-19 06:56:21 +0000715 MF.getSSARegMap()->getRegClass(VirtReg));
Chris Lattner22480c42005-10-05 18:30:19 +0000716 // Revisit the copy so we make sure to notice the effects of the
717 // operation on the destreg (either needing to RA it if it's
718 // virtual or needing to clobber any values if it's physical).
719 NextMII = &MI;
720 --NextMII; // backtrack to the copy.
Chris Lattnercea86882005-09-19 06:56:21 +0000721 }
722 MBB.erase(&MI);
723 goto ProcessNextInst;
724 }
725 }
726 }
727
728 // If this reference is not a use, any previous store is now dead.
729 // Otherwise, the store to this stack slot is not dead anymore.
730 std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
731 if (MDSI != MaybeDeadStores.end()) {
732 if (MR & VirtRegMap::isRef) // Previous store is not dead.
733 MaybeDeadStores.erase(MDSI);
734 else {
735 // If we get here, the store is dead, nuke it now.
Evan Cheng200370f2006-04-30 08:41:47 +0000736 assert(!(MR & VirtRegMap::isRef) && "Can't be modref!");
737 // Don't nuke it if the value is needed in another block.
738 if (!SpillerCheckLiveOut || !(MR & VirtRegMap::isLiveOut)) {
739 DEBUG(std::cerr << " Killed store:\t" << *MDSI->second);
740 MBB.erase(MDSI->second);
741 MaybeDeadStores.erase(MDSI);
742 ++NumDSE;
743 }
Chris Lattnercea86882005-09-19 06:56:21 +0000744 }
745 }
746
747 // If the spill slot value is available, and this is a new definition of
748 // the value, the value is not available anymore.
749 if (MR & VirtRegMap::isMod) {
Chris Lattner07cf1412006-02-03 00:36:31 +0000750 // Notice that the value in this stack slot has been modified.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000751 Spills.ModifyStackSlot(SS);
Chris Lattnercd816392006-02-02 23:29:36 +0000752
753 // If this is *just* a mod of the value, check to see if this is just a
754 // store to the spill slot (i.e. the spill got merged into the copy). If
755 // so, realize that the vreg is available now, and add the store to the
756 // MaybeDeadStore info.
757 int StackSlot;
758 if (!(MR & VirtRegMap::isRef)) {
759 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
760 assert(MRegisterInfo::isPhysicalRegister(SrcReg) &&
761 "Src hasn't been allocated yet?");
Chris Lattner07cf1412006-02-03 00:36:31 +0000762 // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark
Chris Lattnercd816392006-02-02 23:29:36 +0000763 // this as a potentially dead store in case there is a subsequent
764 // store into the stack slot without a read from it.
765 MaybeDeadStores[StackSlot] = &MI;
766
Chris Lattnercd816392006-02-02 23:29:36 +0000767 // If the stack slot value was previously available in some other
768 // register, change it now. Otherwise, make the register available,
769 // in PhysReg.
Chris Lattner593c9582006-02-03 23:28:46 +0000770 Spills.addAvailable(StackSlot, SrcReg, false /*don't clobber*/);
Chris Lattnercd816392006-02-02 23:29:36 +0000771 }
772 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000773 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000774 }
775
Chris Lattner7fb64342004-10-01 19:04:51 +0000776 // Process all of the spilled defs.
777 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
778 MachineOperand &MO = MI.getOperand(i);
779 if (MO.isRegister() && MO.getReg() && MO.isDef()) {
780 unsigned VirtReg = MO.getReg();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000781
Chris Lattner7fb64342004-10-01 19:04:51 +0000782 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
783 // Check to see if this is a def-and-use vreg operand that we do need
784 // to insert a store for.
785 bool OpTakenCareOf = false;
786 if (MO.isUse() && !DefAndUseVReg.empty()) {
787 for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
788 if (DefAndUseVReg[dau].first == i) {
789 VirtReg = DefAndUseVReg[dau].second;
790 OpTakenCareOf = true;
791 break;
792 }
793 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000794
Chris Lattner7fb64342004-10-01 19:04:51 +0000795 if (!OpTakenCareOf) {
Chris Lattner109afed2006-02-03 03:16:14 +0000796 // Check to see if this is a noop copy. If so, eliminate the
797 // instruction before considering the dest reg to be changed.
798 unsigned Src, Dst;
799 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
800 ++NumDCE;
801 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
802 MBB.erase(&MI);
803 goto ProcessNextInst;
804 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000805 Spills.ClobberPhysReg(VirtReg);
Chris Lattner84e752a2006-02-03 03:06:49 +0000806 continue;
Chris Lattner7fb64342004-10-01 19:04:51 +0000807 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000808 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000809
Chris Lattner84e752a2006-02-03 03:06:49 +0000810 // The only vregs left are stack slot definitions.
811 int StackSlot = VRM.getStackSlot(VirtReg);
812 const TargetRegisterClass *RC =
813 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
814 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000815
Chris Lattner84e752a2006-02-03 03:06:49 +0000816 // If this is a def&use operand, and we used a different physreg for
817 // it than the one assigned, make sure to execute the store from the
818 // correct physical register.
819 if (MO.getReg() == VirtReg)
820 PhysReg = VRM.getPhys(VirtReg);
821 else
822 PhysReg = MO.getReg();
Chris Lattner7fb64342004-10-01 19:04:51 +0000823
Chris Lattner84e752a2006-02-03 03:06:49 +0000824 PhysRegsUsed[PhysReg] = true;
825 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
826 DEBUG(std::cerr << "Store:\t" << *next(MII));
827 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000828
Chris Lattner109afed2006-02-03 03:16:14 +0000829 // Check to see if this is a noop copy. If so, eliminate the
830 // instruction before considering the dest reg to be changed.
831 {
832 unsigned Src, Dst;
833 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
834 ++NumDCE;
835 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
836 MBB.erase(&MI);
837 goto ProcessNextInst;
838 }
839 }
840
Chris Lattner84e752a2006-02-03 03:06:49 +0000841 // If there is a dead store to this stack slot, nuke it now.
842 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
843 if (LastStore) {
844 DEBUG(std::cerr << " Killed store:\t" << *LastStore);
845 ++NumDSE;
846 MBB.erase(LastStore);
Chris Lattner7fb64342004-10-01 19:04:51 +0000847 }
Chris Lattner84e752a2006-02-03 03:06:49 +0000848 LastStore = next(MII);
849
850 // If the stack slot value was previously available in some other
851 // register, change it now. Otherwise, make the register available,
852 // in PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000853 Spills.ModifyStackSlot(StackSlot);
854 Spills.ClobberPhysReg(PhysReg);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000855 Spills.addAvailable(StackSlot, PhysReg);
Chris Lattner84e752a2006-02-03 03:06:49 +0000856 ++NumStores;
Chris Lattner7fb64342004-10-01 19:04:51 +0000857 }
858 }
Chris Lattnercea86882005-09-19 06:56:21 +0000859 ProcessNextInst:
Chris Lattner7fb64342004-10-01 19:04:51 +0000860 MII = NextMII;
861 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000862}
863
864
Chris Lattner7fb64342004-10-01 19:04:51 +0000865
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000866llvm::Spiller* llvm::createSpiller() {
867 switch (SpillerOpt) {
868 default: assert(0 && "Unreachable!");
869 case local:
870 return new LocalSpiller();
871 case simple:
872 return new SimpleSpiller();
873 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000874}