blob: 7523d73c2e6c17ac52e5423b0742299d61c099a4 [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,
Chris Lattner35f27052006-05-01 21:16:03 +000084 unsigned OpNo, MachineInstr *NewMI) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000085 // 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 {
Chris Lattner35f27052006-05-01 21:16:03 +0000134 bool runOnMachineFunction(MachineFunction& mf, VirtRegMap &VRM);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000135 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000136}
137
Chris Lattner35f27052006-05-01 21:16:03 +0000138bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, 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 Lattner35f27052006-05-01 21:16:03 +0000211 bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000212 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 Lattner35f27052006-05-01 21:16:03 +0000223 void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM);
Chris Lattner7fb64342004-10-01 19:04:51 +0000224 void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000225 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner7fb64342004-10-01 19:04:51 +0000226 void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000227 std::multimap<unsigned, int> &PhysRegs);
228 void ModifyStackSlot(int Slot, std::map<int, unsigned> &SpillSlots,
229 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000230 };
231}
232
Chris Lattner66cf80f2006-02-03 23:13:58 +0000233/// AvailableSpills - As the local spiller is scanning and rewriting an MBB from
234/// top down, keep track of which spills slots are available in each register.
Chris Lattner593c9582006-02-03 23:28:46 +0000235///
236/// Note that not all physregs are created equal here. In particular, some
237/// physregs are reloads that we are allowed to clobber or ignore at any time.
238/// Other physregs are values that the register allocated program is using that
239/// we cannot CHANGE, but we can read if we like. We keep track of this on a
240/// per-stack-slot basis as the low bit in the value of the SpillSlotsAvailable
241/// entries. The predicate 'canClobberPhysReg()' checks this bit and
242/// addAvailable sets it if.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000243class AvailableSpills {
244 const MRegisterInfo *MRI;
245 const TargetInstrInfo *TII;
246
247 // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
248 // register values that are still available, due to being loaded or stored to,
249 // but not invalidated yet.
250 std::map<int, unsigned> SpillSlotsAvailable;
251
252 // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
253 // which stack slot values are currently held by a physreg. This is used to
254 // invalidate entries in SpillSlotsAvailable when a physreg is modified.
255 std::multimap<unsigned, int> PhysRegsAvailable;
256
257 void ClobberPhysRegOnly(unsigned PhysReg);
258public:
259 AvailableSpills(const MRegisterInfo *mri, const TargetInstrInfo *tii)
260 : MRI(mri), TII(tii) {
261 }
262
263 /// getSpillSlotPhysReg - If the specified stack slot is available in a
264 /// physical register, return that PhysReg, otherwise return 0.
265 unsigned getSpillSlotPhysReg(int Slot) const {
266 std::map<int, unsigned>::const_iterator I = SpillSlotsAvailable.find(Slot);
267 if (I != SpillSlotsAvailable.end())
Chris Lattner593c9582006-02-03 23:28:46 +0000268 return I->second >> 1; // Remove the CanClobber bit.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000269 return 0;
270 }
Chris Lattner540fec62006-02-25 01:51:33 +0000271
272 const MRegisterInfo *getRegInfo() const { return MRI; }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000273
274 /// addAvailable - Mark that the specified stack slot is available in the
Chris Lattner593c9582006-02-03 23:28:46 +0000275 /// specified physreg. If CanClobber is true, the physreg can be modified at
276 /// any time without changing the semantics of the program.
277 void addAvailable(int Slot, unsigned Reg, bool CanClobber = true) {
Chris Lattner86662492006-02-03 23:50:46 +0000278 // If this stack slot is thought to be available in some other physreg,
279 // remove its record.
280 ModifyStackSlot(Slot);
281
Chris Lattner66cf80f2006-02-03 23:13:58 +0000282 PhysRegsAvailable.insert(std::make_pair(Reg, Slot));
Jeff Cohen003cecb2006-02-04 03:27:39 +0000283 SpillSlotsAvailable[Slot] = (Reg << 1) | (unsigned)CanClobber;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000284
285 DEBUG(std::cerr << "Remembering SS#" << Slot << " in physreg "
286 << MRI->getName(Reg) << "\n");
287 }
288
Chris Lattner593c9582006-02-03 23:28:46 +0000289 /// canClobberPhysReg - Return true if the spiller is allowed to change the
290 /// value of the specified stackslot register if it desires. The specified
291 /// stack slot must be available in a physreg for this query to make sense.
292 bool canClobberPhysReg(int Slot) const {
293 assert(SpillSlotsAvailable.count(Slot) && "Slot not available!");
294 return SpillSlotsAvailable.find(Slot)->second & 1;
295 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000296
297 /// ClobberPhysReg - This is called when the specified physreg changes
298 /// value. We use this to invalidate any info about stuff we thing lives in
299 /// it and any of its aliases.
300 void ClobberPhysReg(unsigned PhysReg);
301
302 /// ModifyStackSlot - This method is called when the value in a stack slot
303 /// changes. This removes information about which register the previous value
304 /// for this slot lives in (as the previous value is dead now).
305 void ModifyStackSlot(int Slot);
306};
307
308/// ClobberPhysRegOnly - This is called when the specified physreg changes
309/// value. We use this to invalidate any info about stuff we thing lives in it.
310void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
311 std::multimap<unsigned, int>::iterator I =
312 PhysRegsAvailable.lower_bound(PhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000313 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000314 int Slot = I->second;
Chris Lattner07cf1412006-02-03 00:36:31 +0000315 PhysRegsAvailable.erase(I++);
Chris Lattner593c9582006-02-03 23:28:46 +0000316 assert((SpillSlotsAvailable[Slot] >> 1) == PhysReg &&
Chris Lattner66cf80f2006-02-03 23:13:58 +0000317 "Bidirectional map mismatch!");
318 SpillSlotsAvailable.erase(Slot);
Chris Lattner7fb64342004-10-01 19:04:51 +0000319 DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
Chris Lattner07cf1412006-02-03 00:36:31 +0000320 << " clobbered, invalidating SS#" << Slot << "\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000321 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000322}
323
Chris Lattner66cf80f2006-02-03 23:13:58 +0000324/// ClobberPhysReg - This is called when the specified physreg changes
325/// value. We use this to invalidate any info about stuff we thing lives in
326/// it and any of its aliases.
327void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000328 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
Chris Lattner66cf80f2006-02-03 23:13:58 +0000329 ClobberPhysRegOnly(*AS);
330 ClobberPhysRegOnly(PhysReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000331}
332
Chris Lattner07cf1412006-02-03 00:36:31 +0000333/// ModifyStackSlot - This method is called when the value in a stack slot
334/// changes. This removes information about which register the previous value
335/// for this slot lives in (as the previous value is dead now).
Chris Lattner66cf80f2006-02-03 23:13:58 +0000336void AvailableSpills::ModifyStackSlot(int Slot) {
337 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(Slot);
338 if (It == SpillSlotsAvailable.end()) return;
Chris Lattner593c9582006-02-03 23:28:46 +0000339 unsigned Reg = It->second >> 1;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000340 SpillSlotsAvailable.erase(It);
Chris Lattner07cf1412006-02-03 00:36:31 +0000341
342 // This register may hold the value of multiple stack slots, only remove this
343 // stack slot from the set of values the register contains.
344 std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
345 for (; ; ++I) {
346 assert(I != PhysRegsAvailable.end() && I->first == Reg &&
347 "Map inverse broken!");
348 if (I->second == Slot) break;
349 }
350 PhysRegsAvailable.erase(I);
351}
352
353
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000354
Chris Lattner7fb64342004-10-01 19:04:51 +0000355// ReusedOp - For each reused operand, we keep track of a bit of information, in
356// case we need to rollback upon processing a new operand. See comments below.
357namespace {
358 struct ReusedOp {
359 // The MachineInstr operand that reused an available value.
360 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000361
Chris Lattner7fb64342004-10-01 19:04:51 +0000362 // StackSlot - The spill slot of the value being reused.
363 unsigned StackSlot;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000364
Chris Lattner7fb64342004-10-01 19:04:51 +0000365 // PhysRegReused - The physical register the value was available in.
366 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000367
Chris Lattner7fb64342004-10-01 19:04:51 +0000368 // AssignedPhysReg - The physreg that was assigned for use by the reload.
369 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000370
371 // VirtReg - The virtual register itself.
372 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000373
Chris Lattner8a61a752005-10-06 17:19:06 +0000374 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
375 unsigned vreg)
376 : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
377 VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000378 };
Chris Lattner540fec62006-02-25 01:51:33 +0000379
380 /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
381 /// is reused instead of reloaded.
382 class ReuseInfo {
383 MachineInstr &MI;
384 std::vector<ReusedOp> Reuses;
385 public:
386 ReuseInfo(MachineInstr &mi) : MI(mi) {}
387
388 bool hasReuses() const {
389 return !Reuses.empty();
390 }
391
392 /// addReuse - If we choose to reuse a virtual register that is already
393 /// available instead of reloading it, remember that we did so.
394 void addReuse(unsigned OpNo, unsigned StackSlot,
395 unsigned PhysRegReused, unsigned AssignedPhysReg,
396 unsigned VirtReg) {
397 // If the reload is to the assigned register anyway, no undo will be
398 // required.
399 if (PhysRegReused == AssignedPhysReg) return;
400
401 // Otherwise, remember this.
402 Reuses.push_back(ReusedOp(OpNo, StackSlot, PhysRegReused,
403 AssignedPhysReg, VirtReg));
404 }
405
406 /// GetRegForReload - We are about to emit a reload into PhysReg. If there
407 /// is some other operand that is using the specified register, either pick
408 /// a new register to use, or evict the previous reload and use this reg.
409 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
410 AvailableSpills &Spills,
411 std::map<int, MachineInstr*> &MaybeDeadStores) {
412 if (Reuses.empty()) return PhysReg; // This is most often empty.
413
414 for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
415 ReusedOp &Op = Reuses[ro];
416 // If we find some other reuse that was supposed to use this register
417 // exactly for its reload, we can change this reload to use ITS reload
418 // register.
419 if (Op.PhysRegReused == PhysReg) {
420 // Yup, use the reload register that we didn't use before.
Chris Lattner47cb7172006-02-25 02:03:40 +0000421 unsigned NewReg = Op.AssignedPhysReg;
422
423 // Remove the record for the previous reuse. We know it can never be
424 // invalidated now.
425 Reuses.erase(Reuses.begin()+ro);
426 return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores);
Chris Lattner540fec62006-02-25 01:51:33 +0000427 } else {
428 // Otherwise, we might also have a problem if a previously reused
429 // value aliases the new register. If so, codegen the previous reload
430 // and use this one.
431 unsigned PRRU = Op.PhysRegReused;
432 const MRegisterInfo *MRI = Spills.getRegInfo();
433 if (MRI->areAliases(PRRU, PhysReg)) {
434 // Okay, we found out that an alias of a reused register
435 // was used. This isn't good because it means we have
436 // to undo a previous reuse.
437 MachineBasicBlock *MBB = MI->getParent();
438 const TargetRegisterClass *AliasRC =
Chris Lattner28bad082006-02-25 02:17:31 +0000439 MBB->getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
440
441 // Copy Op out of the vector and remove it, we're going to insert an
442 // explicit load for it.
443 ReusedOp NewOp = Op;
444 Reuses.erase(Reuses.begin()+ro);
445
446 // Ok, we're going to try to reload the assigned physreg into the
447 // slot that we were supposed to in the first place. However, that
448 // register could hold a reuse. Check to see if it conflicts or
449 // would prefer us to use a different register.
450 unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg,
451 MI, Spills, MaybeDeadStores);
452
453 MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg,
454 NewOp.StackSlot, AliasRC);
455 Spills.ClobberPhysReg(NewPhysReg);
456 Spills.ClobberPhysReg(NewOp.PhysRegReused);
Chris Lattner540fec62006-02-25 01:51:33 +0000457
458 // Any stores to this stack slot are not dead anymore.
Chris Lattner28bad082006-02-25 02:17:31 +0000459 MaybeDeadStores.erase(NewOp.StackSlot);
Chris Lattner540fec62006-02-25 01:51:33 +0000460
Chris Lattner28bad082006-02-25 02:17:31 +0000461 MI->SetMachineOperandReg(NewOp.Operand, NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000462
Chris Lattner28bad082006-02-25 02:17:31 +0000463 Spills.addAvailable(NewOp.StackSlot, NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000464 ++NumLoads;
465 DEBUG(MachineBasicBlock::iterator MII = MI;
466 std::cerr << '\t' << *prior(MII));
467
468 DEBUG(std::cerr << "Reuse undone!\n");
Chris Lattner540fec62006-02-25 01:51:33 +0000469 --NumReused;
Chris Lattner28bad082006-02-25 02:17:31 +0000470
471 // Finally, PhysReg is now available, go ahead and use it.
Chris Lattner540fec62006-02-25 01:51:33 +0000472 return PhysReg;
473 }
474 }
475 }
476 return PhysReg;
477 }
478 };
Chris Lattner7fb64342004-10-01 19:04:51 +0000479}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000480
Chris Lattner7fb64342004-10-01 19:04:51 +0000481
482/// rewriteMBB - Keep track of which spills are available even after the
483/// register allocator is done with them. If possible, avoid reloading vregs.
Chris Lattner35f27052006-05-01 21:16:03 +0000484void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000485
Chris Lattner7fb64342004-10-01 19:04:51 +0000486 DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
487
Chris Lattner66cf80f2006-02-03 23:13:58 +0000488 // Spills - Keep track of which spilled values are available in physregs so
489 // that we can choose to reuse the physregs instead of emitting reloads.
490 AvailableSpills Spills(MRI, TII);
491
Chris Lattner7fb64342004-10-01 19:04:51 +0000492 // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
493 // of it. ".first" is the machine operand index (should always be 0 for now),
494 // and ".second" is the virtual register that is spilled.
495 std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
496
Chris Lattner52b25db2004-10-01 19:47:12 +0000497 // MaybeDeadStores - When we need to write a value back into a stack slot,
498 // keep track of the inserted store. If the stack slot value is never read
499 // (because the value was used from some available register, for example), and
500 // subsequently stored to, the original store is dead. This map keeps track
501 // of inserted stores that are not used. If we see a subsequent store to the
502 // same stack slot, the original store is deleted.
503 std::map<int, MachineInstr*> MaybeDeadStores;
504
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000505 bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
506
Chris Lattner7fb64342004-10-01 19:04:51 +0000507 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
508 MII != E; ) {
509 MachineInstr &MI = *MII;
510 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
511
Chris Lattner540fec62006-02-25 01:51:33 +0000512 /// ReusedOperands - Keep track of operand reuse in case we need to undo
513 /// reuse.
514 ReuseInfo ReusedOperands(MI);
515
Chris Lattner7fb64342004-10-01 19:04:51 +0000516 DefAndUseVReg.clear();
517
518 // Process all of the spilled uses and all non spilled reg references.
519 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
520 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000521 if (!MO.isRegister() || MO.getReg() == 0)
522 continue; // Ignore non-register operands.
523
524 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
525 // Ignore physregs for spilling, but remember that it is used by this
526 // function.
Chris Lattner886dd912005-04-04 21:35:34 +0000527 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner50ea01e2005-09-09 20:29:51 +0000528 continue;
529 }
530
531 assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
532 "Not a virtual or a physical register?");
533
534 unsigned VirtReg = MO.getReg();
535 if (!VRM.hasStackSlot(VirtReg)) {
536 // This virtual register was assigned a physreg!
537 unsigned Phys = VRM.getPhys(VirtReg);
538 PhysRegsUsed[Phys] = true;
539 MI.SetMachineOperandReg(i, Phys);
540 continue;
541 }
542
543 // This virtual register is now known to be a spilled value.
544 if (!MO.isUse())
545 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +0000546
Chris Lattner50ea01e2005-09-09 20:29:51 +0000547 // If this is both a def and a use, we need to emit a store to the
548 // stack slot after the instruction. Keep track of D&U operands
549 // because we are about to change it to a physreg here.
550 if (MO.isDef()) {
551 // Remember that this was a def-and-use operand, and that the
552 // stack slot is live after this instruction executes.
553 DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
554 }
555
556 int StackSlot = VRM.getStackSlot(VirtReg);
557 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000558
Chris Lattner50ea01e2005-09-09 20:29:51 +0000559 // Check to see if this stack slot is available.
Chris Lattneraddc55a2006-04-28 01:46:50 +0000560 if ((PhysReg = Spills.getSpillSlotPhysReg(StackSlot))) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000561
Chris Lattneraddc55a2006-04-28 01:46:50 +0000562 // Don't reuse it for a def&use operand if we aren't allowed to change
563 // the physreg!
564 if (!MO.isDef() || Spills.canClobberPhysReg(StackSlot)) {
565 // If this stack slot value is already available, reuse it!
566 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
567 << MRI->getName(PhysReg) << " for vreg"
568 << VirtReg <<" instead of reloading into physreg "
569 << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
570 MI.SetMachineOperandReg(i, PhysReg);
571
572 // The only technical detail we have is that we don't know that
573 // PhysReg won't be clobbered by a reloaded stack slot that occurs
574 // later in the instruction. In particular, consider 'op V1, V2'.
575 // If V1 is available in physreg R0, we would choose to reuse it
576 // here, instead of reloading it into the register the allocator
577 // indicated (say R1). However, V2 might have to be reloaded
578 // later, and it might indicate that it needs to live in R0. When
579 // this occurs, we need to have information available that
580 // indicates it is safe to use R1 for the reload instead of R0.
581 //
582 // To further complicate matters, we might conflict with an alias,
583 // or R0 and R1 might not be compatible with each other. In this
584 // case, we actually insert a reload for V1 in R1, ensuring that
585 // we can get at R0 or its alias.
586 ReusedOperands.addReuse(i, StackSlot, PhysReg,
587 VRM.getPhys(VirtReg), VirtReg);
588 ++NumReused;
589 continue;
590 }
591
592 // Otherwise we have a situation where we have a two-address instruction
593 // whose mod/ref operand needs to be reloaded. This reload is already
594 // available in some register "PhysReg", but if we used PhysReg as the
595 // operand to our 2-addr instruction, the instruction would modify
596 // PhysReg. This isn't cool if something later uses PhysReg and expects
597 // to get its initial value.
Chris Lattner50ea01e2005-09-09 20:29:51 +0000598 //
Chris Lattneraddc55a2006-04-28 01:46:50 +0000599 // To avoid this problem, and to avoid doing a load right after a store,
600 // we emit a copy from PhysReg into the designated register for this
601 // operand.
602 unsigned DesignatedReg = VRM.getPhys(VirtReg);
603 assert(DesignatedReg && "Must map virtreg to physreg!");
604
605 // Note that, if we reused a register for a previous operand, the
606 // register we want to reload into might not actually be
607 // available. If this occurs, use the register indicated by the
608 // reuser.
609 if (ReusedOperands.hasReuses())
610 DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI,
611 Spills, MaybeDeadStores);
612
Chris Lattnerba1fc3d2006-04-28 04:43:18 +0000613 // If the mapped designated register is actually the physreg we have
614 // incoming, we don't need to inserted a dead copy.
615 if (DesignatedReg == PhysReg) {
616 // If this stack slot value is already available, reuse it!
617 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
618 << MRI->getName(PhysReg) << " for vreg"
619 << VirtReg
620 << " instead of reloading into same physreg.\n");
621 MI.SetMachineOperandReg(i, PhysReg);
622 ++NumReused;
623 continue;
624 }
625
Chris Lattneraddc55a2006-04-28 01:46:50 +0000626 const TargetRegisterClass* RC =
627 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
628
629 PhysRegsUsed[DesignatedReg] = true;
630 MRI->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC);
631
632 // This invalidates DesignatedReg.
633 Spills.ClobberPhysReg(DesignatedReg);
634
635 Spills.addAvailable(StackSlot, DesignatedReg);
636 MI.SetMachineOperandReg(i, DesignatedReg);
637 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000638 ++NumReused;
639 continue;
640 }
641
642 // Otherwise, reload it and remember that we have it.
643 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +0000644 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000645 const TargetRegisterClass* RC =
646 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000647
Chris Lattner50ea01e2005-09-09 20:29:51 +0000648 // Note that, if we reused a register for a previous operand, the
649 // register we want to reload into might not actually be
650 // available. If this occurs, use the register indicated by the
651 // reuser.
Chris Lattner540fec62006-02-25 01:51:33 +0000652 if (ReusedOperands.hasReuses())
653 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
654 Spills, MaybeDeadStores);
655
Chris Lattner50ea01e2005-09-09 20:29:51 +0000656 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000657 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000658 // This invalidates PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000659 Spills.ClobberPhysReg(PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000660
661 // Any stores to this stack slot are not dead anymore.
662 MaybeDeadStores.erase(StackSlot);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000663 Spills.addAvailable(StackSlot, PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000664 ++NumLoads;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000665 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000666 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000667 }
668
Chris Lattner7fb64342004-10-01 19:04:51 +0000669 // Loop over all of the implicit defs, clearing them from our available
670 // sets.
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000671 for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
672 *ImpDef; ++ImpDef) {
673 PhysRegsUsed[*ImpDef] = true;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000674 Spills.ClobberPhysReg(*ImpDef);
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000675 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000676
Chris Lattner7fb64342004-10-01 19:04:51 +0000677 DEBUG(std::cerr << '\t' << MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000678
Chris Lattner7fb64342004-10-01 19:04:51 +0000679 // If we have folded references to memory operands, make sure we clear all
680 // physical registers that may contain the value of the spilled virtual
681 // register
Chris Lattner8f1d6402005-01-14 15:54:24 +0000682 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
683 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000684 DEBUG(std::cerr << "Folded vreg: " << I->second.first << " MR: "
685 << I->second.second);
686 unsigned VirtReg = I->second.first;
687 VirtRegMap::ModRef MR = I->second.second;
Chris Lattnercea86882005-09-19 06:56:21 +0000688 if (!VRM.hasStackSlot(VirtReg)) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000689 DEBUG(std::cerr << ": No stack slot!\n");
Chris Lattnercea86882005-09-19 06:56:21 +0000690 continue;
691 }
692 int SS = VRM.getStackSlot(VirtReg);
693 DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
694
695 // If this folded instruction is just a use, check to see if it's a
696 // straight load from the virt reg slot.
697 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
698 int FrameIdx;
Chris Lattner40839602006-02-02 20:12:32 +0000699 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
700 // If this spill slot is available, turn it into a copy (or nothing)
701 // instead of leaving it as a load!
Chris Lattner66cf80f2006-02-03 23:13:58 +0000702 unsigned InReg;
703 if (FrameIdx == SS && (InReg = Spills.getSpillSlotPhysReg(SS))) {
Chris Lattnercea86882005-09-19 06:56:21 +0000704 DEBUG(std::cerr << "Promoted Load To Copy: " << MI);
705 MachineFunction &MF = *MBB.getParent();
Chris Lattner66cf80f2006-02-03 23:13:58 +0000706 if (DestReg != InReg) {
707 MRI->copyRegToReg(MBB, &MI, DestReg, InReg,
Chris Lattnercea86882005-09-19 06:56:21 +0000708 MF.getSSARegMap()->getRegClass(VirtReg));
Chris Lattner22480c42005-10-05 18:30:19 +0000709 // Revisit the copy so we make sure to notice the effects of the
710 // operation on the destreg (either needing to RA it if it's
711 // virtual or needing to clobber any values if it's physical).
712 NextMII = &MI;
713 --NextMII; // backtrack to the copy.
Chris Lattnercea86882005-09-19 06:56:21 +0000714 }
715 MBB.erase(&MI);
716 goto ProcessNextInst;
717 }
718 }
719 }
720
721 // If this reference is not a use, any previous store is now dead.
722 // Otherwise, the store to this stack slot is not dead anymore.
723 std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
724 if (MDSI != MaybeDeadStores.end()) {
725 if (MR & VirtRegMap::isRef) // Previous store is not dead.
726 MaybeDeadStores.erase(MDSI);
727 else {
728 // If we get here, the store is dead, nuke it now.
Chris Lattner35f27052006-05-01 21:16:03 +0000729 assert(VirtRegMap::isMod && "Can't be modref!");
730 DEBUG(std::cerr << "Removed dead store:\t" << *MDSI->second);
731 MBB.erase(MDSI->second);
732 MaybeDeadStores.erase(MDSI);
733 ++NumDSE;
Chris Lattnercea86882005-09-19 06:56:21 +0000734 }
735 }
736
737 // If the spill slot value is available, and this is a new definition of
738 // the value, the value is not available anymore.
739 if (MR & VirtRegMap::isMod) {
Chris Lattner07cf1412006-02-03 00:36:31 +0000740 // Notice that the value in this stack slot has been modified.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000741 Spills.ModifyStackSlot(SS);
Chris Lattnercd816392006-02-02 23:29:36 +0000742
743 // If this is *just* a mod of the value, check to see if this is just a
744 // store to the spill slot (i.e. the spill got merged into the copy). If
745 // so, realize that the vreg is available now, and add the store to the
746 // MaybeDeadStore info.
747 int StackSlot;
748 if (!(MR & VirtRegMap::isRef)) {
749 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
750 assert(MRegisterInfo::isPhysicalRegister(SrcReg) &&
751 "Src hasn't been allocated yet?");
Chris Lattner07cf1412006-02-03 00:36:31 +0000752 // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark
Chris Lattnercd816392006-02-02 23:29:36 +0000753 // this as a potentially dead store in case there is a subsequent
754 // store into the stack slot without a read from it.
755 MaybeDeadStores[StackSlot] = &MI;
756
Chris Lattnercd816392006-02-02 23:29:36 +0000757 // If the stack slot value was previously available in some other
758 // register, change it now. Otherwise, make the register available,
759 // in PhysReg.
Chris Lattner593c9582006-02-03 23:28:46 +0000760 Spills.addAvailable(StackSlot, SrcReg, false /*don't clobber*/);
Chris Lattnercd816392006-02-02 23:29:36 +0000761 }
762 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000763 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000764 }
765
Chris Lattner7fb64342004-10-01 19:04:51 +0000766 // Process all of the spilled defs.
767 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
768 MachineOperand &MO = MI.getOperand(i);
769 if (MO.isRegister() && MO.getReg() && MO.isDef()) {
770 unsigned VirtReg = MO.getReg();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000771
Chris Lattner7fb64342004-10-01 19:04:51 +0000772 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
773 // Check to see if this is a def-and-use vreg operand that we do need
774 // to insert a store for.
775 bool OpTakenCareOf = false;
776 if (MO.isUse() && !DefAndUseVReg.empty()) {
777 for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
778 if (DefAndUseVReg[dau].first == i) {
779 VirtReg = DefAndUseVReg[dau].second;
780 OpTakenCareOf = true;
781 break;
782 }
783 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000784
Chris Lattner7fb64342004-10-01 19:04:51 +0000785 if (!OpTakenCareOf) {
Chris Lattner109afed2006-02-03 03:16:14 +0000786 // Check to see if this is a noop copy. If so, eliminate the
787 // instruction before considering the dest reg to be changed.
788 unsigned Src, Dst;
789 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
790 ++NumDCE;
791 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
792 MBB.erase(&MI);
793 goto ProcessNextInst;
794 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000795 Spills.ClobberPhysReg(VirtReg);
Chris Lattner84e752a2006-02-03 03:06:49 +0000796 continue;
Chris Lattner7fb64342004-10-01 19:04:51 +0000797 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000798 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000799
Chris Lattner84e752a2006-02-03 03:06:49 +0000800 // The only vregs left are stack slot definitions.
801 int StackSlot = VRM.getStackSlot(VirtReg);
802 const TargetRegisterClass *RC =
803 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
804 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000805
Chris Lattner84e752a2006-02-03 03:06:49 +0000806 // If this is a def&use operand, and we used a different physreg for
807 // it than the one assigned, make sure to execute the store from the
808 // correct physical register.
809 if (MO.getReg() == VirtReg)
810 PhysReg = VRM.getPhys(VirtReg);
811 else
812 PhysReg = MO.getReg();
Chris Lattner7fb64342004-10-01 19:04:51 +0000813
Chris Lattner84e752a2006-02-03 03:06:49 +0000814 PhysRegsUsed[PhysReg] = true;
815 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
816 DEBUG(std::cerr << "Store:\t" << *next(MII));
817 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000818
Chris Lattner109afed2006-02-03 03:16:14 +0000819 // Check to see if this is a noop copy. If so, eliminate the
820 // instruction before considering the dest reg to be changed.
821 {
822 unsigned Src, Dst;
823 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
824 ++NumDCE;
825 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
826 MBB.erase(&MI);
827 goto ProcessNextInst;
828 }
829 }
830
Chris Lattner84e752a2006-02-03 03:06:49 +0000831 // If there is a dead store to this stack slot, nuke it now.
832 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
833 if (LastStore) {
Chris Lattner35f27052006-05-01 21:16:03 +0000834 DEBUG(std::cerr << "Removed dead store:\t" << *LastStore);
Chris Lattner84e752a2006-02-03 03:06:49 +0000835 ++NumDSE;
836 MBB.erase(LastStore);
Chris Lattner7fb64342004-10-01 19:04:51 +0000837 }
Chris Lattner84e752a2006-02-03 03:06:49 +0000838 LastStore = next(MII);
839
840 // If the stack slot value was previously available in some other
841 // register, change it now. Otherwise, make the register available,
842 // in PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000843 Spills.ModifyStackSlot(StackSlot);
844 Spills.ClobberPhysReg(PhysReg);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000845 Spills.addAvailable(StackSlot, PhysReg);
Chris Lattner84e752a2006-02-03 03:06:49 +0000846 ++NumStores;
Chris Lattner7fb64342004-10-01 19:04:51 +0000847 }
848 }
Chris Lattnercea86882005-09-19 06:56:21 +0000849 ProcessNextInst:
Chris Lattner7fb64342004-10-01 19:04:51 +0000850 MII = NextMII;
851 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000852}
853
854
Chris Lattner7fb64342004-10-01 19:04:51 +0000855
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000856llvm::Spiller* llvm::createSpiller() {
857 switch (SpillerOpt) {
858 default: assert(0 && "Unreachable!");
859 case local:
860 return new LocalSpiller();
861 case simple:
862 return new SimpleSpiller();
863 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000864}