blob: 8b08841ee9cd20a2ecb5bc85c73470333ba70b1f [file] [log] [blame]
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00001//===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner8c4d88d2004-09-30 01:54:45 +000010// This file implements the VirtRegMap class.
11//
12// It also contains implementations of the the Spiller interface, which, given a
13// virtual register map and a machine function, eliminates all virtual
14// references by replacing them with physical register references - adding spill
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000015// code as necessary.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000016//
17//===----------------------------------------------------------------------===//
18
Chris Lattner8c4d88d2004-09-30 01:54:45 +000019#define DEBUG_TYPE "spiller"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000020#include "VirtRegMap.h"
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000021#include "llvm/Function.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner8c4d88d2004-09-30 01:54:45 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/SSARegMap.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000025#include "llvm/Target/TargetMachine.h"
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000026#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/STLExtras.h"
Chris Lattner27f29162004-10-26 15:35:58 +000031#include <algorithm>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000032#include <iostream>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000033using namespace llvm;
34
35namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000036 Statistic<> NumSpills("spiller", "Number of register spills");
37 Statistic<> NumStores("spiller", "Number of stores added");
38 Statistic<> NumLoads ("spiller", "Number of loads added");
Chris Lattner7fb64342004-10-01 19:04:51 +000039 Statistic<> NumReused("spiller", "Number of values reused");
Chris Lattner52b25db2004-10-01 19:47:12 +000040 Statistic<> NumDSE ("spiller", "Number of dead stores elided");
Chris Lattner1118d252006-02-03 02:02:59 +000041 Statistic<> NumDCE ("spiller", "Number of copies elided");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000042
Chris Lattner8c4d88d2004-09-30 01:54:45 +000043 enum SpillerName { simple, local };
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000044
Chris Lattner8c4d88d2004-09-30 01:54:45 +000045 cl::opt<SpillerName>
46 SpillerOpt("spiller",
Chris Lattner7fb64342004-10-01 19:04:51 +000047 cl::desc("Spiller to use: (default: local)"),
Chris Lattner8c4d88d2004-09-30 01:54:45 +000048 cl::Prefix,
49 cl::values(clEnumVal(simple, " simple spiller"),
50 clEnumVal(local, " local spiller"),
51 clEnumValEnd),
Chris Lattner7fb64342004-10-01 19:04:51 +000052 cl::init(local));
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000053}
54
Chris Lattner8c4d88d2004-09-30 01:54:45 +000055//===----------------------------------------------------------------------===//
56// VirtRegMap implementation
57//===----------------------------------------------------------------------===//
58
59void VirtRegMap::grow() {
Chris Lattner7f690e62004-09-30 02:15:18 +000060 Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg());
61 Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg());
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000062}
63
Chris Lattner8c4d88d2004-09-30 01:54:45 +000064int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
65 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000066 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000067 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000068 const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg);
69 int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
70 RC->getAlignment());
71 Virt2StackSlotMap[virtReg] = frameIndex;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000072 ++NumSpills;
73 return frameIndex;
74}
75
76void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
77 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000078 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000079 "attempt to assign stack slot to already spilled register");
Chris Lattner7f690e62004-09-30 02:15:18 +000080 Virt2StackSlotMap[virtReg] = frameIndex;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +000081}
82
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000083void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
84 unsigned OpNo, MachineInstr *NewMI) {
85 // Move previous memory references folded to new instruction.
86 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +000087 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000088 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
89 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +000090 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +000091 }
Chris Lattnerdbea9732004-09-30 16:35:08 +000092
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000093 ModRef MRInfo;
94 if (!OldMI->getOperand(OpNo).isDef()) {
95 assert(OldMI->getOperand(OpNo).isUse() && "Operand is not use or def?");
96 MRInfo = isRef;
97 } else {
98 MRInfo = OldMI->getOperand(OpNo).isUse() ? isModRef : isMod;
99 }
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000100
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000101 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000102 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000103}
104
Chris Lattner7f690e62004-09-30 02:15:18 +0000105void VirtRegMap::print(std::ostream &OS) const {
106 const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000107
Chris Lattner7f690e62004-09-30 02:15:18 +0000108 OS << "********** REGISTER MAP **********\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000109 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000110 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) {
111 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
112 OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000113
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000114 }
115
116 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner7f690e62004-09-30 02:15:18 +0000117 e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i)
118 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
119 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
120 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000121}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000122
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000123void VirtRegMap::dump() const { print(std::cerr); }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000124
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000125
126//===----------------------------------------------------------------------===//
127// Simple Spiller Implementation
128//===----------------------------------------------------------------------===//
129
130Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000131
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000132namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000133 struct SimpleSpiller : public Spiller {
134 bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM);
135 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000136}
137
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000138bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF,
139 const VirtRegMap &VRM) {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000140 DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n");
141 DEBUG(std::cerr << "********** Function: "
142 << MF.getFunction()->getName() << '\n');
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000143 const TargetMachine &TM = MF.getTarget();
144 const MRegisterInfo &MRI = *TM.getRegisterInfo();
145 bool *PhysRegsUsed = MF.getUsedPhysregs();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000146
Chris Lattner4ea1b822004-09-30 02:33:48 +0000147 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
148 // each vreg once (in the case where a spilled vreg is used by multiple
149 // operands). This is always smaller than the number of operands to the
150 // current machine instr, so it should be small.
151 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000152
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000153 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
154 MBBI != E; ++MBBI) {
155 DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n");
156 MachineBasicBlock &MBB = *MBBI;
157 for (MachineBasicBlock::iterator MII = MBB.begin(),
158 E = MBB.end(); MII != E; ++MII) {
159 MachineInstr &MI = *MII;
160 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000161 MachineOperand &MO = MI.getOperand(i);
Chris Lattner886dd912005-04-04 21:35:34 +0000162 if (MO.isRegister() && MO.getReg())
163 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
164 unsigned VirtReg = MO.getReg();
165 unsigned PhysReg = VRM.getPhys(VirtReg);
166 if (VRM.hasStackSlot(VirtReg)) {
167 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000168 const TargetRegisterClass* RC =
169 MF.getSSARegMap()->getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000170
Chris Lattner886dd912005-04-04 21:35:34 +0000171 if (MO.isUse() &&
172 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
173 == LoadedRegs.end()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000174 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000175 LoadedRegs.push_back(VirtReg);
176 ++NumLoads;
177 DEBUG(std::cerr << '\t' << *prior(MII));
178 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000179
Chris Lattner886dd912005-04-04 21:35:34 +0000180 if (MO.isDef()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000181 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000182 ++NumStores;
183 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000184 }
Chris Lattner886dd912005-04-04 21:35:34 +0000185 PhysRegsUsed[PhysReg] = true;
186 MI.SetMachineOperandReg(i, PhysReg);
187 } else {
188 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000189 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000190 }
Chris Lattner886dd912005-04-04 21:35:34 +0000191
Chris Lattner477e4552004-09-30 16:10:45 +0000192 DEBUG(std::cerr << '\t' << MI);
Chris Lattner4ea1b822004-09-30 02:33:48 +0000193 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000194 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000195 }
196 return true;
197}
198
199//===----------------------------------------------------------------------===//
200// Local Spiller Implementation
201//===----------------------------------------------------------------------===//
202
203namespace {
Chris Lattner7fb64342004-10-01 19:04:51 +0000204 /// LocalSpiller - This spiller does a simple pass over the machine basic
205 /// block to attempt to keep spills in registers as much as possible for
206 /// blocks that have low register pressure (the vreg may be spilled due to
207 /// register pressure in other blocks).
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000208 class LocalSpiller : public Spiller {
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000209 const MRegisterInfo *MRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000210 const TargetInstrInfo *TII;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000211 public:
Chris Lattner7fb64342004-10-01 19:04:51 +0000212 bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) {
213 MRI = MF.getTarget().getRegisterInfo();
214 TII = MF.getTarget().getInstrInfo();
215 DEBUG(std::cerr << "\n**** Local spiller rewriting function '"
216 << MF.getFunction()->getName() << "':\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000217
Chris Lattner7fb64342004-10-01 19:04:51 +0000218 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
219 MBB != E; ++MBB)
220 RewriteMBB(*MBB, VRM);
221 return true;
222 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000223 private:
Chris Lattner7fb64342004-10-01 19:04:51 +0000224 void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM);
225 void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000226 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner7fb64342004-10-01 19:04:51 +0000227 void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000228 std::multimap<unsigned, int> &PhysRegs);
229 void ModifyStackSlot(int Slot, std::map<int, unsigned> &SpillSlots,
230 std::multimap<unsigned, int> &PhysRegs);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000231 };
232}
233
Chris Lattner7fb64342004-10-01 19:04:51 +0000234void LocalSpiller::ClobberPhysRegOnly(unsigned PhysReg,
235 std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000236 std::multimap<unsigned, int> &PhysRegsAvailable) {
237 std::map<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(PhysReg);
238 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000239 int Slot = I->second;
Chris Lattner07cf1412006-02-03 00:36:31 +0000240 PhysRegsAvailable.erase(I++);
Chris Lattner7fb64342004-10-01 19:04:51 +0000241 assert(SpillSlots[Slot] == PhysReg && "Bidirectional map mismatch!");
242 SpillSlots.erase(Slot);
243 DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg)
Chris Lattner07cf1412006-02-03 00:36:31 +0000244 << " clobbered, invalidating SS#" << Slot << "\n");
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000245
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000246 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000247}
248
Chris Lattner7fb64342004-10-01 19:04:51 +0000249void LocalSpiller::ClobberPhysReg(unsigned PhysReg,
250 std::map<int, unsigned> &SpillSlots,
Chris Lattner07cf1412006-02-03 00:36:31 +0000251 std::multimap<unsigned, int> &PhysRegsAvailable) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000252 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
Chris Lattner07cf1412006-02-03 00:36:31 +0000253 ClobberPhysRegOnly(*AS, SpillSlots, PhysRegsAvailable);
254 ClobberPhysRegOnly(PhysReg, SpillSlots, PhysRegsAvailable);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000255}
256
Chris Lattner07cf1412006-02-03 00:36:31 +0000257/// ModifyStackSlot - This method is called when the value in a stack slot
258/// changes. This removes information about which register the previous value
259/// for this slot lives in (as the previous value is dead now).
260void LocalSpiller::ModifyStackSlot(int Slot, std::map<int,unsigned> &SpillSlots,
261 std::multimap<unsigned, int> &PhysRegsAvailable) {
262 std::map<int, unsigned>::iterator It = SpillSlots.find(Slot);
263 if (It == SpillSlots.end()) return;
264 unsigned Reg = It->second;
265 SpillSlots.erase(It);
266
267 // This register may hold the value of multiple stack slots, only remove this
268 // stack slot from the set of values the register contains.
269 std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
270 for (; ; ++I) {
271 assert(I != PhysRegsAvailable.end() && I->first == Reg &&
272 "Map inverse broken!");
273 if (I->second == Slot) break;
274 }
275 PhysRegsAvailable.erase(I);
276}
277
278
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000279
Chris Lattner7fb64342004-10-01 19:04:51 +0000280// ReusedOp - For each reused operand, we keep track of a bit of information, in
281// case we need to rollback upon processing a new operand. See comments below.
282namespace {
283 struct ReusedOp {
284 // The MachineInstr operand that reused an available value.
285 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000286
Chris Lattner7fb64342004-10-01 19:04:51 +0000287 // StackSlot - The spill slot of the value being reused.
288 unsigned StackSlot;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000289
Chris Lattner7fb64342004-10-01 19:04:51 +0000290 // PhysRegReused - The physical register the value was available in.
291 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000292
Chris Lattner7fb64342004-10-01 19:04:51 +0000293 // AssignedPhysReg - The physreg that was assigned for use by the reload.
294 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000295
296 // VirtReg - The virtual register itself.
297 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000298
Chris Lattner8a61a752005-10-06 17:19:06 +0000299 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
300 unsigned vreg)
301 : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr),
302 VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000303 };
304}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000305
Chris Lattner7fb64342004-10-01 19:04:51 +0000306
307/// rewriteMBB - Keep track of which spills are available even after the
308/// register allocator is done with them. If possible, avoid reloading vregs.
309void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) {
310
311 // SpillSlotsAvailable - This map keeps track of all of the spilled virtual
Chris Lattnercd816392006-02-02 23:29:36 +0000312 // register values that are still available, due to being loaded or stored to,
Chris Lattner7fb64342004-10-01 19:04:51 +0000313 // but not invalidated yet.
314 std::map<int, unsigned> SpillSlotsAvailable;
315
316 // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating
Chris Lattner07cf1412006-02-03 00:36:31 +0000317 // which stack slot values are currently held by a physreg. This is used to
318 // invalidate entries in SpillSlotsAvailable when a physreg is modified.
319 std::multimap<unsigned, int> PhysRegsAvailable;
Chris Lattner7fb64342004-10-01 19:04:51 +0000320
321 DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n");
322
323 std::vector<ReusedOp> ReusedOperands;
324
325 // DefAndUseVReg - When we see a def&use operand that is spilled, keep track
326 // of it. ".first" is the machine operand index (should always be 0 for now),
327 // and ".second" is the virtual register that is spilled.
328 std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg;
329
Chris Lattner52b25db2004-10-01 19:47:12 +0000330 // MaybeDeadStores - When we need to write a value back into a stack slot,
331 // keep track of the inserted store. If the stack slot value is never read
332 // (because the value was used from some available register, for example), and
333 // subsequently stored to, the original store is dead. This map keeps track
334 // of inserted stores that are not used. If we see a subsequent store to the
335 // same stack slot, the original store is deleted.
336 std::map<int, MachineInstr*> MaybeDeadStores;
337
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000338 bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
339
Chris Lattner7fb64342004-10-01 19:04:51 +0000340 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
341 MII != E; ) {
342 MachineInstr &MI = *MII;
343 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
344
345 ReusedOperands.clear();
346 DefAndUseVReg.clear();
347
348 // Process all of the spilled uses and all non spilled reg references.
349 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
350 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000351 if (!MO.isRegister() || MO.getReg() == 0)
352 continue; // Ignore non-register operands.
353
354 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
355 // Ignore physregs for spilling, but remember that it is used by this
356 // function.
Chris Lattner886dd912005-04-04 21:35:34 +0000357 PhysRegsUsed[MO.getReg()] = true;
Chris Lattner50ea01e2005-09-09 20:29:51 +0000358 continue;
359 }
360
361 assert(MRegisterInfo::isVirtualRegister(MO.getReg()) &&
362 "Not a virtual or a physical register?");
363
364 unsigned VirtReg = MO.getReg();
365 if (!VRM.hasStackSlot(VirtReg)) {
366 // This virtual register was assigned a physreg!
367 unsigned Phys = VRM.getPhys(VirtReg);
368 PhysRegsUsed[Phys] = true;
369 MI.SetMachineOperandReg(i, Phys);
370 continue;
371 }
372
373 // This virtual register is now known to be a spilled value.
374 if (!MO.isUse())
375 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +0000376
Chris Lattner50ea01e2005-09-09 20:29:51 +0000377 // If this is both a def and a use, we need to emit a store to the
378 // stack slot after the instruction. Keep track of D&U operands
379 // because we are about to change it to a physreg here.
380 if (MO.isDef()) {
381 // Remember that this was a def-and-use operand, and that the
382 // stack slot is live after this instruction executes.
383 DefAndUseVReg.push_back(std::make_pair(i, VirtReg));
384 }
385
386 int StackSlot = VRM.getStackSlot(VirtReg);
387 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000388
Chris Lattner50ea01e2005-09-09 20:29:51 +0000389 // Check to see if this stack slot is available.
390 std::map<int, unsigned>::iterator SSI =
391 SpillSlotsAvailable.find(StackSlot);
392 if (SSI != SpillSlotsAvailable.end()) {
393 DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg "
394 << MRI->getName(SSI->second) << " for vreg"
395 << VirtReg <<" instead of reloading into physreg "
396 << MRI->getName(VRM.getPhys(VirtReg)) << "\n");
397 // If this stack slot value is already available, reuse it!
398 PhysReg = SSI->second;
399 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000400
Chris Lattner50ea01e2005-09-09 20:29:51 +0000401 // The only technical detail we have is that we don't know that
402 // PhysReg won't be clobbered by a reloaded stack slot that occurs
403 // later in the instruction. In particular, consider 'op V1, V2'.
404 // If V1 is available in physreg R0, we would choose to reuse it
405 // here, instead of reloading it into the register the allocator
406 // indicated (say R1). However, V2 might have to be reloaded
407 // later, and it might indicate that it needs to live in R0. When
408 // this occurs, we need to have information available that
409 // indicates it is safe to use R1 for the reload instead of R0.
410 //
411 // To further complicate matters, we might conflict with an alias,
412 // or R0 and R1 might not be compatible with each other. In this
413 // case, we actually insert a reload for V1 in R1, ensuring that
414 // we can get at R0 or its alias.
415 ReusedOperands.push_back(ReusedOp(i, StackSlot, PhysReg,
Chris Lattner8a61a752005-10-06 17:19:06 +0000416 VRM.getPhys(VirtReg), VirtReg));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000417 ++NumReused;
418 continue;
419 }
420
421 // Otherwise, reload it and remember that we have it.
422 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +0000423 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000424 const TargetRegisterClass* RC =
425 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000426
Chris Lattner50ea01e2005-09-09 20:29:51 +0000427 RecheckRegister:
428 // Note that, if we reused a register for a previous operand, the
429 // register we want to reload into might not actually be
430 // available. If this occurs, use the register indicated by the
431 // reuser.
432 if (!ReusedOperands.empty()) // This is most often empty.
433 for (unsigned ro = 0, e = ReusedOperands.size(); ro != e; ++ro)
434 if (ReusedOperands[ro].PhysRegReused == PhysReg) {
435 // Yup, use the reload register that we didn't use before.
436 PhysReg = ReusedOperands[ro].AssignedPhysReg;
437 goto RecheckRegister;
438 } else {
439 ReusedOp &Op = ReusedOperands[ro];
440 unsigned PRRU = Op.PhysRegReused;
441 if (MRI->areAliases(PRRU, PhysReg)) {
442 // Okay, we found out that an alias of a reused register
443 // was used. This isn't good because it means we have
444 // to undo a previous reuse.
Chris Lattner8a61a752005-10-06 17:19:06 +0000445 const TargetRegisterClass *AliasRC =
446 MBB.getParent()->getSSARegMap()->getRegClass(Op.VirtReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000447 MRI->loadRegFromStackSlot(MBB, &MI, Op.AssignedPhysReg,
Chris Lattner8a61a752005-10-06 17:19:06 +0000448 Op.StackSlot, AliasRC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000449 ClobberPhysReg(Op.AssignedPhysReg, SpillSlotsAvailable,
450 PhysRegsAvailable);
Chris Lattner7fb64342004-10-01 19:04:51 +0000451
Chris Lattner52b25db2004-10-01 19:47:12 +0000452 // Any stores to this stack slot are not dead anymore.
Chris Lattner50ea01e2005-09-09 20:29:51 +0000453 MaybeDeadStores.erase(Op.StackSlot);
Chris Lattner52b25db2004-10-01 19:47:12 +0000454
Chris Lattner50ea01e2005-09-09 20:29:51 +0000455 MI.SetMachineOperandReg(Op.Operand, Op.AssignedPhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000456 PhysRegsAvailable.insert(std::make_pair(Op.AssignedPhysReg,
457 Op.StackSlot));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000458 SpillSlotsAvailable[Op.StackSlot] = Op.AssignedPhysReg;
459 PhysRegsAvailable.erase(Op.PhysRegReused);
460 DEBUG(std::cerr << "Remembering SS#" << Op.StackSlot
Chris Lattner07cf1412006-02-03 00:36:31 +0000461 << " in physreg "
462 << MRI->getName(Op.AssignedPhysReg) << "\n");
Chris Lattner7fb64342004-10-01 19:04:51 +0000463 ++NumLoads;
464 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner7fb64342004-10-01 19:04:51 +0000465
Chris Lattner50ea01e2005-09-09 20:29:51 +0000466 DEBUG(std::cerr << "Reuse undone!\n");
467 ReusedOperands.erase(ReusedOperands.begin()+ro);
468 --NumReused;
469 goto ContinueReload;
Chris Lattner7fb64342004-10-01 19:04:51 +0000470 }
471 }
Chris Lattner50ea01e2005-09-09 20:29:51 +0000472 ContinueReload:
473 PhysRegsUsed[PhysReg] = true;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000474 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner50ea01e2005-09-09 20:29:51 +0000475 // This invalidates PhysReg.
476 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
477
478 // Any stores to this stack slot are not dead anymore.
479 MaybeDeadStores.erase(StackSlot);
480
481 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000482 PhysRegsAvailable.insert(std::make_pair(PhysReg, StackSlot));
Chris Lattner50ea01e2005-09-09 20:29:51 +0000483 SpillSlotsAvailable[StackSlot] = PhysReg;
484 DEBUG(std::cerr << "Remembering SS#" << StackSlot <<" in physreg "
485 << MRI->getName(PhysReg) << "\n");
486 ++NumLoads;
487 DEBUG(std::cerr << '\t' << *prior(MII));
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000488 }
489
Chris Lattner7fb64342004-10-01 19:04:51 +0000490 // Loop over all of the implicit defs, clearing them from our available
491 // sets.
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000492 for (const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode());
493 *ImpDef; ++ImpDef) {
494 PhysRegsUsed[*ImpDef] = true;
Chris Lattner7fb64342004-10-01 19:04:51 +0000495 ClobberPhysReg(*ImpDef, SpillSlotsAvailable, PhysRegsAvailable);
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000496 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000497
Chris Lattner7fb64342004-10-01 19:04:51 +0000498 DEBUG(std::cerr << '\t' << MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000499
Chris Lattner7fb64342004-10-01 19:04:51 +0000500 // If we have folded references to memory operands, make sure we clear all
501 // physical registers that may contain the value of the spilled virtual
502 // register
Chris Lattner8f1d6402005-01-14 15:54:24 +0000503 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
504 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000505 DEBUG(std::cerr << "Folded vreg: " << I->second.first << " MR: "
506 << I->second.second);
507 unsigned VirtReg = I->second.first;
508 VirtRegMap::ModRef MR = I->second.second;
Chris Lattnercea86882005-09-19 06:56:21 +0000509 if (!VRM.hasStackSlot(VirtReg)) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000510 DEBUG(std::cerr << ": No stack slot!\n");
Chris Lattnercea86882005-09-19 06:56:21 +0000511 continue;
512 }
513 int SS = VRM.getStackSlot(VirtReg);
514 DEBUG(std::cerr << " - StackSlot: " << SS << "\n");
515
516 // If this folded instruction is just a use, check to see if it's a
517 // straight load from the virt reg slot.
518 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
519 int FrameIdx;
Chris Lattner40839602006-02-02 20:12:32 +0000520 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
521 // If this spill slot is available, turn it into a copy (or nothing)
522 // instead of leaving it as a load!
Chris Lattnercea86882005-09-19 06:56:21 +0000523 std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(SS);
524 if (FrameIdx == SS && It != SpillSlotsAvailable.end()) {
525 DEBUG(std::cerr << "Promoted Load To Copy: " << MI);
526 MachineFunction &MF = *MBB.getParent();
527 if (DestReg != It->second) {
528 MRI->copyRegToReg(MBB, &MI, DestReg, It->second,
529 MF.getSSARegMap()->getRegClass(VirtReg));
Chris Lattner22480c42005-10-05 18:30:19 +0000530 // Revisit the copy so we make sure to notice the effects of the
531 // operation on the destreg (either needing to RA it if it's
532 // virtual or needing to clobber any values if it's physical).
533 NextMII = &MI;
534 --NextMII; // backtrack to the copy.
Chris Lattnercea86882005-09-19 06:56:21 +0000535 }
536 MBB.erase(&MI);
537 goto ProcessNextInst;
538 }
539 }
540 }
541
542 // If this reference is not a use, any previous store is now dead.
543 // Otherwise, the store to this stack slot is not dead anymore.
544 std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS);
545 if (MDSI != MaybeDeadStores.end()) {
546 if (MR & VirtRegMap::isRef) // Previous store is not dead.
547 MaybeDeadStores.erase(MDSI);
548 else {
549 // If we get here, the store is dead, nuke it now.
550 assert(MR == VirtRegMap::isMod && "Can't be modref!");
551 MBB.erase(MDSI->second);
552 MaybeDeadStores.erase(MDSI);
553 ++NumDSE;
554 }
555 }
556
557 // If the spill slot value is available, and this is a new definition of
558 // the value, the value is not available anymore.
559 if (MR & VirtRegMap::isMod) {
Chris Lattner07cf1412006-02-03 00:36:31 +0000560 // Notice that the value in this stack slot has been modified.
561 ModifyStackSlot(SS, SpillSlotsAvailable, PhysRegsAvailable);
Chris Lattnercd816392006-02-02 23:29:36 +0000562
563 // If this is *just* a mod of the value, check to see if this is just a
564 // store to the spill slot (i.e. the spill got merged into the copy). If
565 // so, realize that the vreg is available now, and add the store to the
566 // MaybeDeadStore info.
567 int StackSlot;
568 if (!(MR & VirtRegMap::isRef)) {
569 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
570 assert(MRegisterInfo::isPhysicalRegister(SrcReg) &&
571 "Src hasn't been allocated yet?");
Chris Lattner07cf1412006-02-03 00:36:31 +0000572 // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark
Chris Lattnercd816392006-02-02 23:29:36 +0000573 // this as a potentially dead store in case there is a subsequent
574 // store into the stack slot without a read from it.
575 MaybeDeadStores[StackSlot] = &MI;
576
Chris Lattnercd816392006-02-02 23:29:36 +0000577 // If the stack slot value was previously available in some other
578 // register, change it now. Otherwise, make the register available,
579 // in PhysReg.
580 SpillSlotsAvailable[StackSlot] = SrcReg;
Chris Lattner07cf1412006-02-03 00:36:31 +0000581 PhysRegsAvailable.insert(std::make_pair(SrcReg, StackSlot));
Chris Lattnercd816392006-02-02 23:29:36 +0000582 DEBUG(std::cerr << "Updating SS#" << StackSlot << " in physreg "
583 << MRI->getName(SrcReg) << " for virtreg #"
584 << VirtReg << "\n" << MI);
585 }
586 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000587 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000588 }
589
Chris Lattner7fb64342004-10-01 19:04:51 +0000590 // Process all of the spilled defs.
591 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
592 MachineOperand &MO = MI.getOperand(i);
593 if (MO.isRegister() && MO.getReg() && MO.isDef()) {
594 unsigned VirtReg = MO.getReg();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000595
Chris Lattner7fb64342004-10-01 19:04:51 +0000596 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
597 // Check to see if this is a def-and-use vreg operand that we do need
598 // to insert a store for.
599 bool OpTakenCareOf = false;
600 if (MO.isUse() && !DefAndUseVReg.empty()) {
601 for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau)
602 if (DefAndUseVReg[dau].first == i) {
603 VirtReg = DefAndUseVReg[dau].second;
604 OpTakenCareOf = true;
605 break;
606 }
607 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000608
Chris Lattner7fb64342004-10-01 19:04:51 +0000609 if (!OpTakenCareOf) {
Chris Lattner109afed2006-02-03 03:16:14 +0000610 // Check to see if this is a noop copy. If so, eliminate the
611 // instruction before considering the dest reg to be changed.
612 unsigned Src, Dst;
613 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
614 ++NumDCE;
615 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
616 MBB.erase(&MI);
617 goto ProcessNextInst;
618 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000619 ClobberPhysReg(VirtReg, SpillSlotsAvailable, PhysRegsAvailable);
Chris Lattner84e752a2006-02-03 03:06:49 +0000620 continue;
Chris Lattner7fb64342004-10-01 19:04:51 +0000621 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000622 }
Chris Lattner7fb64342004-10-01 19:04:51 +0000623
Chris Lattner84e752a2006-02-03 03:06:49 +0000624 // The only vregs left are stack slot definitions.
625 int StackSlot = VRM.getStackSlot(VirtReg);
626 const TargetRegisterClass *RC =
627 MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
628 unsigned PhysReg;
Chris Lattner7fb64342004-10-01 19:04:51 +0000629
Chris Lattner84e752a2006-02-03 03:06:49 +0000630 // If this is a def&use operand, and we used a different physreg for
631 // it than the one assigned, make sure to execute the store from the
632 // correct physical register.
633 if (MO.getReg() == VirtReg)
634 PhysReg = VRM.getPhys(VirtReg);
635 else
636 PhysReg = MO.getReg();
Chris Lattner7fb64342004-10-01 19:04:51 +0000637
Chris Lattner84e752a2006-02-03 03:06:49 +0000638 PhysRegsUsed[PhysReg] = true;
639 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
640 DEBUG(std::cerr << "Store:\t" << *next(MII));
641 MI.SetMachineOperandReg(i, PhysReg);
Chris Lattner7fb64342004-10-01 19:04:51 +0000642
Chris Lattner109afed2006-02-03 03:16:14 +0000643 // Check to see if this is a noop copy. If so, eliminate the
644 // instruction before considering the dest reg to be changed.
645 {
646 unsigned Src, Dst;
647 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
648 ++NumDCE;
649 DEBUG(std::cerr << "Removing now-noop copy: " << MI);
650 MBB.erase(&MI);
651 goto ProcessNextInst;
652 }
653 }
654
Chris Lattner84e752a2006-02-03 03:06:49 +0000655 // If there is a dead store to this stack slot, nuke it now.
656 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
657 if (LastStore) {
658 DEBUG(std::cerr << " Killed store:\t" << *LastStore);
659 ++NumDSE;
660 MBB.erase(LastStore);
Chris Lattner7fb64342004-10-01 19:04:51 +0000661 }
Chris Lattner84e752a2006-02-03 03:06:49 +0000662 LastStore = next(MII);
663
664 // If the stack slot value was previously available in some other
665 // register, change it now. Otherwise, make the register available,
666 // in PhysReg.
667 ModifyStackSlot(StackSlot, SpillSlotsAvailable, PhysRegsAvailable);
668 ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable);
669
670 PhysRegsAvailable.insert(std::make_pair(PhysReg, StackSlot));
671 SpillSlotsAvailable[StackSlot] = PhysReg;
672 DEBUG(std::cerr << "Updating SS#" << StackSlot <<" in physreg "
673 << MRI->getName(PhysReg) << " for virtreg #"
674 << VirtReg << "\n");
675 ++NumStores;
Chris Lattner7fb64342004-10-01 19:04:51 +0000676 }
677 }
Chris Lattnercea86882005-09-19 06:56:21 +0000678 ProcessNextInst:
Chris Lattner7fb64342004-10-01 19:04:51 +0000679 MII = NextMII;
680 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000681}
682
683
Chris Lattner7fb64342004-10-01 19:04:51 +0000684
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000685llvm::Spiller* llvm::createSpiller() {
686 switch (SpillerOpt) {
687 default: assert(0 && "Unreachable!");
688 case local:
689 return new LocalSpiller();
690 case simple:
691 return new SimpleSpiller();
692 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000693}