blob: 461d839f1c0965027bbd822edfeb229922fe2e6a [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00007//
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"
Evan Cheng4cce6b42008-04-11 17:53:36 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000026#include "llvm/Target/TargetMachine.h"
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000027#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Evan Cheng752272a2009-02-11 08:24:21 +000030#include "llvm/Support/Debug.h"
Evan Cheng957840b2007-02-21 02:22:03 +000031#include "llvm/ADT/BitVector.h"
Evan Chengcb742662008-06-04 09:16:33 +000032#include "llvm/ADT/DenseMap.h"
Evan Cheng752272a2009-02-11 08:24:21 +000033#include "llvm/ADT/DepthFirstIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/STLExtras.h"
Chris Lattner08a4d5a2007-01-23 00:59:48 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner27f29162004-10-26 15:35:58 +000037#include <algorithm>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000038using namespace llvm;
39
Evan Cheng87bb9912008-06-13 23:58:02 +000040STATISTIC(NumSpills , "Number of register spills");
Evan Cheng625986a2008-06-18 07:47:28 +000041STATISTIC(NumPSpills , "Number of physical register spills");
Evan Cheng87bb9912008-06-13 23:58:02 +000042STATISTIC(NumReMats , "Number of re-materialization");
43STATISTIC(NumDRM , "Number of re-materializable defs elided");
44STATISTIC(NumStores , "Number of stores added");
45STATISTIC(NumLoads , "Number of loads added");
46STATISTIC(NumReused , "Number of values reused");
47STATISTIC(NumDSE , "Number of dead stores elided");
48STATISTIC(NumDCE , "Number of copies elided");
49STATISTIC(NumDSS , "Number of dead spill slots removed");
50STATISTIC(NumCommutes, "Number of instructions commuted");
Evan Cheng752272a2009-02-11 08:24:21 +000051STATISTIC(NumOmitted , "Number of reloads omited");
52STATISTIC(NumCopified, "Number of available reloads turned into copies");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000053
Chris Lattnercd3245a2006-12-19 22:41:21 +000054namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000055 enum SpillerName { simple, local };
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000056}
57
Dan Gohman844731a2008-05-13 00:00:25 +000058static cl::opt<SpillerName>
59SpillerOpt("spiller",
60 cl::desc("Spiller to use: (default: local)"),
61 cl::Prefix,
Dan Gohmanb8cab922008-10-14 20:25:08 +000062 cl::values(clEnumVal(simple, "simple spiller"),
63 clEnumVal(local, "local spiller"),
Dan Gohman844731a2008-05-13 00:00:25 +000064 clEnumValEnd),
65 cl::init(local));
66
Chris Lattner8c4d88d2004-09-30 01:54:45 +000067//===----------------------------------------------------------------------===//
68// VirtRegMap implementation
69//===----------------------------------------------------------------------===//
70
Chris Lattner29268692006-09-05 02:12:02 +000071VirtRegMap::VirtRegMap(MachineFunction &mf)
72 : TII(*mf.getTarget().getInstrInfo()), MF(mf),
Evan Cheng2638e1a2007-03-20 08:13:50 +000073 Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT),
Evan Cheng81a03822007-11-17 00:40:40 +000074 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
Evan Chengd3653122008-02-27 03:04:06 +000075 Virt2SplitKillMap(0), ReMatMap(NULL), ReMatId(MAX_STACK_SLOT+1),
76 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) {
77 SpillSlotToUsesMap.resize(8);
Evan Cheng4cce6b42008-04-11 17:53:36 +000078 ImplicitDefed.resize(MF.getRegInfo().getLastVirtReg()+1-
79 TargetRegisterInfo::FirstVirtualRegister);
Chris Lattner29268692006-09-05 02:12:02 +000080 grow();
81}
82
Chris Lattner8c4d88d2004-09-30 01:54:45 +000083void VirtRegMap::grow() {
Chris Lattner84bc5422007-12-31 04:13:23 +000084 unsigned LastVirtReg = MF.getRegInfo().getLastVirtReg();
Evan Cheng549f27d32007-08-13 23:45:17 +000085 Virt2PhysMap.grow(LastVirtReg);
86 Virt2StackSlotMap.grow(LastVirtReg);
87 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Cheng81a03822007-11-17 00:40:40 +000088 Virt2SplitMap.grow(LastVirtReg);
Evan Chengadf85902007-12-05 09:51:10 +000089 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +000090 ReMatMap.grow(LastVirtReg);
Evan Cheng4cce6b42008-04-11 17:53:36 +000091 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000092}
93
Chris Lattner8c4d88d2004-09-30 01:54:45 +000094int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000095 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000096 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000097 "attempt to assign stack slot to already spilled register");
Chris Lattner84bc5422007-12-31 04:13:23 +000098 const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(virtReg);
Evan Chengd3653122008-02-27 03:04:06 +000099 int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
100 RC->getAlignment());
101 if (LowSpillSlot == NO_STACK_SLOT)
102 LowSpillSlot = SS;
103 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
104 HighSpillSlot = SS;
105 unsigned Idx = SS-LowSpillSlot;
106 while (Idx >= SpillSlotToUsesMap.size())
107 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
108 Virt2StackSlotMap[virtReg] = SS;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000109 ++NumSpills;
Evan Chengd3653122008-02-27 03:04:06 +0000110 return SS;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000111}
112
Evan Chengd3653122008-02-27 03:04:06 +0000113void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000114 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000115 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000116 "attempt to assign stack slot to already spilled register");
Evan Chengd3653122008-02-27 03:04:06 +0000117 assert((SS >= 0 ||
118 (SS >= MF.getFrameInfo()->getObjectIndexBegin())) &&
Evan Cheng91935142007-04-04 07:40:01 +0000119 "illegal fixed frame index");
Evan Chengd3653122008-02-27 03:04:06 +0000120 Virt2StackSlotMap[virtReg] = SS;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +0000121}
122
Evan Cheng2638e1a2007-03-20 08:13:50 +0000123int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000124 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000125 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Evan Cheng2638e1a2007-03-20 08:13:50 +0000126 "attempt to assign re-mat id to already spilled register");
Evan Cheng549f27d32007-08-13 23:45:17 +0000127 Virt2ReMatIdMap[virtReg] = ReMatId;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000128 return ReMatId++;
129}
130
Evan Cheng549f27d32007-08-13 23:45:17 +0000131void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000132 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000133 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
134 "attempt to assign re-mat id to already spilled register");
135 Virt2ReMatIdMap[virtReg] = id;
136}
137
Evan Cheng676dd7c2008-03-11 07:19:34 +0000138int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
139 std::map<const TargetRegisterClass*, int>::iterator I =
140 EmergencySpillSlots.find(RC);
141 if (I != EmergencySpillSlots.end())
142 return I->second;
143 int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
144 RC->getAlignment());
145 if (LowSpillSlot == NO_STACK_SLOT)
146 LowSpillSlot = SS;
147 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
148 HighSpillSlot = SS;
Dan Gohman4daa9072008-10-06 18:00:07 +0000149 EmergencySpillSlots[RC] = SS;
Evan Cheng676dd7c2008-03-11 07:19:34 +0000150 return SS;
151}
152
Evan Chengd3653122008-02-27 03:04:06 +0000153void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
154 if (!MF.getFrameInfo()->isFixedObjectIndex(FI)) {
David Greenecff86082008-05-22 21:12:21 +0000155 // If FI < LowSpillSlot, this stack reference was produced by
156 // instruction selection and is not a spill
157 if (FI >= LowSpillSlot) {
158 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendlingf3061f82008-05-23 01:29:08 +0000159 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greenecff86082008-05-22 21:12:21 +0000160 && "Invalid spill slot");
161 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
162 }
Evan Chengd3653122008-02-27 03:04:06 +0000163 }
164}
165
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000166void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengaee4af62007-12-02 08:30:39 +0000167 MachineInstr *NewMI, ModRef MRInfo) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000168 // Move previous memory references folded to new instruction.
169 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000170 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000171 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
172 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +0000173 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000174 }
Chris Lattnerdbea9732004-09-30 16:35:08 +0000175
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000176 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000177 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000178}
179
Evan Cheng7f566252007-10-13 02:50:24 +0000180void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
181 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
182 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
183}
184
Evan Chengd3653122008-02-27 03:04:06 +0000185void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
186 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
187 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000188 if (!MO.isFI())
Evan Chengd3653122008-02-27 03:04:06 +0000189 continue;
190 int FI = MO.getIndex();
191 if (MF.getFrameInfo()->isFixedObjectIndex(FI))
192 continue;
David Greenecff86082008-05-22 21:12:21 +0000193 // This stack reference was produced by instruction selection and
194 // is not a spill
195 if (FI < LowSpillSlot)
196 continue;
Bill Wendlingf3061f82008-05-23 01:29:08 +0000197 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greenecff86082008-05-22 21:12:21 +0000198 && "Invalid spill slot");
Evan Chengd3653122008-02-27 03:04:06 +0000199 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
200 }
201 MI2VirtMap.erase(MI);
202 SpillPt2VirtMap.erase(MI);
203 RestorePt2VirtMap.erase(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +0000204 EmergencySpillMap.erase(MI);
Evan Chengd3653122008-02-27 03:04:06 +0000205}
206
Chris Lattner7f690e62004-09-30 02:15:18 +0000207void VirtRegMap::print(std::ostream &OS) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000208 const TargetRegisterInfo* TRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000209
Chris Lattner7f690e62004-09-30 02:15:18 +0000210 OS << "********** REGISTER MAP **********\n";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000211 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +0000212 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i) {
Chris Lattner7f690e62004-09-30 02:15:18 +0000213 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000214 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling74ab84c2008-02-26 21:11:01 +0000215 << "]\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000216 }
217
Dan Gohman6f0d0242008-02-10 18:45:23 +0000218 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +0000219 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattner7f690e62004-09-30 02:15:18 +0000220 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
221 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
222 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000223}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000224
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000225void VirtRegMap::dump() const {
Dan Gohmanb5769312008-03-12 20:52:10 +0000226 print(cerr);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000227}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000228
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000229
230//===----------------------------------------------------------------------===//
231// Simple Spiller Implementation
232//===----------------------------------------------------------------------===//
233
234Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000235
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000236namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000237 struct VISIBILITY_HIDDEN SimpleSpiller : public Spiller {
Chris Lattner35f27052006-05-01 21:16:03 +0000238 bool runOnMachineFunction(MachineFunction& mf, VirtRegMap &VRM);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000239 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000240}
241
Chris Lattner35f27052006-05-01 21:16:03 +0000242bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000243 DOUT << "********** REWRITE MACHINE CODE **********\n";
244 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000245 const TargetMachine &TM = MF.getTarget();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000246 const TargetInstrInfo &TII = *TM.getInstrInfo();
Owen Anderson724651a2008-08-19 01:05:33 +0000247 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000248
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000249
Chris Lattner4ea1b822004-09-30 02:33:48 +0000250 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
251 // each vreg once (in the case where a spilled vreg is used by multiple
252 // operands). This is always smaller than the number of operands to the
253 // current machine instr, so it should be small.
254 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000255
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000256 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
257 MBBI != E; ++MBBI) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000258 DOUT << MBBI->getBasicBlock()->getName() << ":\n";
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000259 MachineBasicBlock &MBB = *MBBI;
260 for (MachineBasicBlock::iterator MII = MBB.begin(),
261 E = MBB.end(); MII != E; ++MII) {
262 MachineInstr &MI = *MII;
263 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000264 MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000265 if (MO.isReg() && MO.getReg()) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000266 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Chris Lattner886dd912005-04-04 21:35:34 +0000267 unsigned VirtReg = MO.getReg();
Owen Anderson724651a2008-08-19 01:05:33 +0000268 unsigned SubIdx = MO.getSubReg();
Chris Lattner886dd912005-04-04 21:35:34 +0000269 unsigned PhysReg = VRM.getPhys(VirtReg);
Owen Anderson724651a2008-08-19 01:05:33 +0000270 unsigned RReg = SubIdx ? TRI.getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000271 if (!VRM.isAssignedReg(VirtReg)) {
Chris Lattner886dd912005-04-04 21:35:34 +0000272 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000273 const TargetRegisterClass* RC =
Chris Lattner84bc5422007-12-31 04:13:23 +0000274 MF.getRegInfo().getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000275
Chris Lattner886dd912005-04-04 21:35:34 +0000276 if (MO.isUse() &&
277 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
278 == LoadedRegs.end()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000279 TII.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Evan Chengd3653122008-02-27 03:04:06 +0000280 MachineInstr *LoadMI = prior(MII);
281 VRM.addSpillSlotUse(StackSlot, LoadMI);
Chris Lattner886dd912005-04-04 21:35:34 +0000282 LoadedRegs.push_back(VirtReg);
283 ++NumLoads;
Evan Chengd3653122008-02-27 03:04:06 +0000284 DOUT << '\t' << *LoadMI;
Chris Lattner886dd912005-04-04 21:35:34 +0000285 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000286
Chris Lattner886dd912005-04-04 21:35:34 +0000287 if (MO.isDef()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000288 TII.storeRegToStackSlot(MBB, next(MII), PhysReg, true,
Evan Chengd64b5c82007-12-05 03:14:33 +0000289 StackSlot, RC);
Evan Chengd3653122008-02-27 03:04:06 +0000290 MachineInstr *StoreMI = next(MII);
291 VRM.addSpillSlotUse(StackSlot, StoreMI);
Chris Lattner886dd912005-04-04 21:35:34 +0000292 ++NumStores;
293 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000294 }
Owen Anderson724651a2008-08-19 01:05:33 +0000295 MF.getRegInfo().setPhysRegUsed(RReg);
296 MI.getOperand(i).setReg(RReg);
Chris Lattner886dd912005-04-04 21:35:34 +0000297 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +0000298 MF.getRegInfo().setPhysRegUsed(MO.getReg());
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000299 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000300 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000301 }
Chris Lattner886dd912005-04-04 21:35:34 +0000302
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000303 DOUT << '\t' << MI;
Chris Lattner4ea1b822004-09-30 02:33:48 +0000304 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000305 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000306 }
307 return true;
308}
309
310//===----------------------------------------------------------------------===//
311// Local Spiller Implementation
312//===----------------------------------------------------------------------===//
313
Chris Lattner66cf80f2006-02-03 23:13:58 +0000314/// AvailableSpills - As the local spiller is scanning and rewriting an MBB from
Evan Cheng549f27d32007-08-13 23:45:17 +0000315/// top down, keep track of which spills slots or remat are available in each
316/// register.
Chris Lattner593c9582006-02-03 23:28:46 +0000317///
318/// Note that not all physregs are created equal here. In particular, some
319/// physregs are reloads that we are allowed to clobber or ignore at any time.
320/// Other physregs are values that the register allocated program is using that
321/// we cannot CHANGE, but we can read if we like. We keep track of this on a
Evan Cheng549f27d32007-08-13 23:45:17 +0000322/// per-stack-slot / remat id basis as the low bit in the value of the
323/// SpillSlotsAvailable entries. The predicate 'canClobberPhysReg()' checks
324/// this bit and addAvailable sets it if.
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000325namespace {
326class VISIBILITY_HIDDEN AvailableSpills {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000327 const TargetRegisterInfo *TRI;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000328 const TargetInstrInfo *TII;
329
Evan Cheng549f27d32007-08-13 23:45:17 +0000330 // SpillSlotsOrReMatsAvailable - This map keeps track of all of the spilled
331 // or remat'ed virtual register values that are still available, due to being
332 // loaded or stored to, but not invalidated yet.
333 std::map<int, unsigned> SpillSlotsOrReMatsAvailable;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000334
Evan Cheng549f27d32007-08-13 23:45:17 +0000335 // PhysRegsAvailable - This is the inverse of SpillSlotsOrReMatsAvailable,
336 // indicating which stack slot values are currently held by a physreg. This
337 // is used to invalidate entries in SpillSlotsOrReMatsAvailable when a
338 // physreg is modified.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000339 std::multimap<unsigned, int> PhysRegsAvailable;
340
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000341 void disallowClobberPhysRegOnly(unsigned PhysReg);
342
Chris Lattner66cf80f2006-02-03 23:13:58 +0000343 void ClobberPhysRegOnly(unsigned PhysReg);
344public:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000345 AvailableSpills(const TargetRegisterInfo *tri, const TargetInstrInfo *tii)
346 : TRI(tri), TII(tii) {
Chris Lattner66cf80f2006-02-03 23:13:58 +0000347 }
Evan Cheng752272a2009-02-11 08:24:21 +0000348
349 /// clear - Reset the state.
350 void clear() {
351 SpillSlotsOrReMatsAvailable.clear();
352 PhysRegsAvailable.clear();
353 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000354
Dan Gohman6f0d0242008-02-10 18:45:23 +0000355 const TargetRegisterInfo *getRegInfo() const { return TRI; }
Evan Cheng91e23902007-02-23 01:13:26 +0000356
Evan Cheng549f27d32007-08-13 23:45:17 +0000357 /// getSpillSlotOrReMatPhysReg - If the specified stack slot or remat is
358 /// available in a physical register, return that PhysReg, otherwise
359 /// return 0.
360 unsigned getSpillSlotOrReMatPhysReg(int Slot) const {
361 std::map<int, unsigned>::const_iterator I =
362 SpillSlotsOrReMatsAvailable.find(Slot);
363 if (I != SpillSlotsOrReMatsAvailable.end()) {
Evan Chengb9591c62007-07-11 08:47:44 +0000364 return I->second >> 1; // Remove the CanClobber bit.
Evan Cheng91e23902007-02-23 01:13:26 +0000365 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000366 return 0;
367 }
Evan Chengde4e9422007-02-25 09:51:27 +0000368
Evan Cheng549f27d32007-08-13 23:45:17 +0000369 /// addAvailable - Mark that the specified stack slot / remat is available in
370 /// the specified physreg. If CanClobber is true, the physreg can be modified
371 /// at any time without changing the semantics of the program.
Evan Cheng752272a2009-02-11 08:24:21 +0000372 void addAvailable(int SlotOrReMat, unsigned Reg, bool CanClobber = true) {
Chris Lattner86662492006-02-03 23:50:46 +0000373 // If this stack slot is thought to be available in some other physreg,
374 // remove its record.
Evan Cheng549f27d32007-08-13 23:45:17 +0000375 ModifyStackSlotOrReMat(SlotOrReMat);
Chris Lattner86662492006-02-03 23:50:46 +0000376
Evan Cheng549f27d32007-08-13 23:45:17 +0000377 PhysRegsAvailable.insert(std::make_pair(Reg, SlotOrReMat));
Evan Cheng90a43c32007-08-15 20:20:34 +0000378 SpillSlotsOrReMatsAvailable[SlotOrReMat]= (Reg << 1) | (unsigned)CanClobber;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000379
Evan Cheng549f27d32007-08-13 23:45:17 +0000380 if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
381 DOUT << "Remembering RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000382 else
Evan Cheng549f27d32007-08-13 23:45:17 +0000383 DOUT << "Remembering SS#" << SlotOrReMat;
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000384 DOUT << " in physreg " << TRI->getName(Reg) << "\n";
Chris Lattner66cf80f2006-02-03 23:13:58 +0000385 }
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000386
Chris Lattner593c9582006-02-03 23:28:46 +0000387 /// canClobberPhysReg - Return true if the spiller is allowed to change the
388 /// value of the specified stackslot register if it desires. The specified
389 /// stack slot must be available in a physreg for this query to make sense.
Evan Cheng549f27d32007-08-13 23:45:17 +0000390 bool canClobberPhysReg(int SlotOrReMat) const {
Evan Cheng90a43c32007-08-15 20:20:34 +0000391 assert(SpillSlotsOrReMatsAvailable.count(SlotOrReMat) &&
392 "Value not available!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000393 return SpillSlotsOrReMatsAvailable.find(SlotOrReMat)->second & 1;
Chris Lattner593c9582006-02-03 23:28:46 +0000394 }
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000395
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000396 /// disallowClobberPhysReg - Unset the CanClobber bit of the specified
397 /// stackslot register. The register is still available but is no longer
398 /// allowed to be modifed.
399 void disallowClobberPhysReg(unsigned PhysReg);
400
Chris Lattner66cf80f2006-02-03 23:13:58 +0000401 /// ClobberPhysReg - This is called when the specified physreg changes
Evan Cheng66f71632007-10-19 21:23:22 +0000402 /// value. We use this to invalidate any info about stuff that lives in
Chris Lattner66cf80f2006-02-03 23:13:58 +0000403 /// it and any of its aliases.
404 void ClobberPhysReg(unsigned PhysReg);
405
Evan Cheng90a43c32007-08-15 20:20:34 +0000406 /// ModifyStackSlotOrReMat - This method is called when the value in a stack
407 /// slot changes. This removes information about which register the previous
408 /// value for this slot lives in (as the previous value is dead now).
Evan Cheng549f27d32007-08-13 23:45:17 +0000409 void ModifyStackSlotOrReMat(int SlotOrReMat);
Evan Cheng6d209c42009-02-12 09:43:23 +0000410
Evan Chengf7923522009-02-26 02:30:42 +0000411 /// AddAvailableRegsToLiveIn - Availability information is being kept coming
412 /// into the specified MBB. Add available physical registers as potential
413 /// live-in's. If they are reused in the MBB, they will be added to the
414 /// live-in set to make register scavenger and post-allocation scheduler.
415 void AddAvailableRegsToLiveIn(MachineBasicBlock &MBB, BitVector &RegKills,
416 std::vector<MachineOperand*> &KillOps);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000417};
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000418}
Chris Lattner66cf80f2006-02-03 23:13:58 +0000419
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000420/// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified
421/// stackslot register. The register is still available but is no longer
422/// allowed to be modifed.
423void AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) {
424 std::multimap<unsigned, int>::iterator I =
425 PhysRegsAvailable.lower_bound(PhysReg);
426 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000427 int SlotOrReMat = I->second;
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000428 I++;
Evan Cheng549f27d32007-08-13 23:45:17 +0000429 assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000430 "Bidirectional map mismatch!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000431 SpillSlotsOrReMatsAvailable[SlotOrReMat] &= ~1;
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000432 DOUT << "PhysReg " << TRI->getName(PhysReg)
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000433 << " copied, it is available for use but can no longer be modified\n";
434 }
435}
436
437/// disallowClobberPhysReg - Unset the CanClobber bit of the specified
438/// stackslot register and its aliases. The register and its aliases may
439/// still available but is no longer allowed to be modifed.
440void AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000441 for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS)
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000442 disallowClobberPhysRegOnly(*AS);
443 disallowClobberPhysRegOnly(PhysReg);
444}
445
Chris Lattner66cf80f2006-02-03 23:13:58 +0000446/// ClobberPhysRegOnly - This is called when the specified physreg changes
447/// value. We use this to invalidate any info about stuff we thing lives in it.
448void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
449 std::multimap<unsigned, int>::iterator I =
450 PhysRegsAvailable.lower_bound(PhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000451 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000452 int SlotOrReMat = I->second;
Chris Lattner07cf1412006-02-03 00:36:31 +0000453 PhysRegsAvailable.erase(I++);
Evan Cheng549f27d32007-08-13 23:45:17 +0000454 assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
Chris Lattner66cf80f2006-02-03 23:13:58 +0000455 "Bidirectional map mismatch!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000456 SpillSlotsOrReMatsAvailable.erase(SlotOrReMat);
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000457 DOUT << "PhysReg " << TRI->getName(PhysReg)
Evan Cheng2638e1a2007-03-20 08:13:50 +0000458 << " clobbered, invalidating ";
Evan Cheng549f27d32007-08-13 23:45:17 +0000459 if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
460 DOUT << "RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1 << "\n";
Evan Cheng2638e1a2007-03-20 08:13:50 +0000461 else
Evan Cheng549f27d32007-08-13 23:45:17 +0000462 DOUT << "SS#" << SlotOrReMat << "\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000463 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000464}
465
Chris Lattner66cf80f2006-02-03 23:13:58 +0000466/// ClobberPhysReg - This is called when the specified physreg changes
467/// value. We use this to invalidate any info about stuff we thing lives in
468/// it and any of its aliases.
469void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000470 for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS)
Chris Lattner66cf80f2006-02-03 23:13:58 +0000471 ClobberPhysRegOnly(*AS);
472 ClobberPhysRegOnly(PhysReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000473}
474
Evan Cheng90a43c32007-08-15 20:20:34 +0000475/// ModifyStackSlotOrReMat - This method is called when the value in a stack
476/// slot changes. This removes information about which register the previous
477/// value for this slot lives in (as the previous value is dead now).
Evan Cheng549f27d32007-08-13 23:45:17 +0000478void AvailableSpills::ModifyStackSlotOrReMat(int SlotOrReMat) {
Evan Cheng90a43c32007-08-15 20:20:34 +0000479 std::map<int, unsigned>::iterator It =
480 SpillSlotsOrReMatsAvailable.find(SlotOrReMat);
Evan Cheng549f27d32007-08-13 23:45:17 +0000481 if (It == SpillSlotsOrReMatsAvailable.end()) return;
Evan Chengb9591c62007-07-11 08:47:44 +0000482 unsigned Reg = It->second >> 1;
Evan Cheng549f27d32007-08-13 23:45:17 +0000483 SpillSlotsOrReMatsAvailable.erase(It);
Chris Lattner07cf1412006-02-03 00:36:31 +0000484
485 // This register may hold the value of multiple stack slots, only remove this
486 // stack slot from the set of values the register contains.
487 std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
488 for (; ; ++I) {
489 assert(I != PhysRegsAvailable.end() && I->first == Reg &&
490 "Map inverse broken!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000491 if (I->second == SlotOrReMat) break;
Chris Lattner07cf1412006-02-03 00:36:31 +0000492 }
493 PhysRegsAvailable.erase(I);
494}
495
Evan Chengf7923522009-02-26 02:30:42 +0000496/// InvalidateKill - A MI that defines the specified register is being deleted,
497/// invalidate the register kill information.
498static void InvalidateKill(unsigned Reg, BitVector &RegKills,
499 std::vector<MachineOperand*> &KillOps) {
500 if (RegKills[Reg]) {
501 KillOps[Reg]->setIsKill(false);
502 KillOps[Reg] = NULL;
503 RegKills.reset(Reg);
504 }
505}
506
Evan Cheng6d209c42009-02-12 09:43:23 +0000507/// AddAvailableRegsToLiveIn - Availability information is being kept coming
Evan Chengf7923522009-02-26 02:30:42 +0000508/// into the specified MBB. Add available physical registers as potential
509/// live-in's. If they are reused in the MBB, they will be added to the
510/// live-in set to make register scavenger and post-allocation scheduler.
511void AvailableSpills::AddAvailableRegsToLiveIn(MachineBasicBlock &MBB,
512 BitVector &RegKills,
513 std::vector<MachineOperand*> &KillOps) {
514 std::set<unsigned> NotAvailable;
Evan Cheng6d209c42009-02-12 09:43:23 +0000515 for (std::multimap<unsigned, int>::iterator
516 I = PhysRegsAvailable.begin(), E = PhysRegsAvailable.end();
517 I != E; ++I) {
Evan Chengf7923522009-02-26 02:30:42 +0000518 unsigned Reg = I->first;
Evan Cheng86791192009-02-12 10:32:17 +0000519 const TargetRegisterClass* RC = TRI->getPhysicalRegisterRegClass(Reg);
520 // FIXME: A temporary workaround. We can't reuse available value if it's
521 // not safe to move the def of the virtual register's class. e.g.
522 // X86::RFP* register classes. Do not add it as a live-in.
523 if (!TII->isSafeToMoveRegClassDefs(RC))
Evan Chengf7923522009-02-26 02:30:42 +0000524 // This is no longer available.
525 NotAvailable.insert(Reg);
526 else {
Evan Cheng6d209c42009-02-12 09:43:23 +0000527 MBB.addLiveIn(Reg);
Evan Chengf7923522009-02-26 02:30:42 +0000528 InvalidateKill(Reg, RegKills, KillOps);
529 }
530
531 // Skip over the same register.
532 std::multimap<unsigned, int>::iterator NI = next(I);
533 while (NI != E && NI->first == Reg) {
534 ++I;
535 ++NI;
536 }
537 }
538
539 for (std::set<unsigned>::iterator I = NotAvailable.begin(),
540 E = NotAvailable.end(); I != E; ++I) {
541 ClobberPhysReg(*I);
542 for (const unsigned *SubRegs = TRI->getSubRegisters(*I);
543 *SubRegs; ++SubRegs)
544 ClobberPhysReg(*SubRegs);
Evan Cheng6d209c42009-02-12 09:43:23 +0000545 }
546}
547
548/// findSinglePredSuccessor - Return via reference a vector of machine basic
549/// blocks each of which is a successor of the specified BB and has no other
550/// predecessor.
Evan Cheng752272a2009-02-11 08:24:21 +0000551static void findSinglePredSuccessor(MachineBasicBlock *MBB,
552 SmallVectorImpl<MachineBasicBlock *> &Succs) {
553 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
554 SE = MBB->succ_end(); SI != SE; ++SI) {
555 MachineBasicBlock *SuccMBB = *SI;
556 if (SuccMBB->pred_size() == 1)
557 Succs.push_back(SuccMBB);
558 }
559}
Chris Lattner07cf1412006-02-03 00:36:31 +0000560
Evan Cheng752272a2009-02-11 08:24:21 +0000561namespace {
Evan Cheng752272a2009-02-11 08:24:21 +0000562 /// LocalSpiller - This spiller does a simple pass over the machine basic
563 /// block to attempt to keep spills in registers as much as possible for
564 /// blocks that have low register pressure (the vreg may be spilled due to
565 /// register pressure in other blocks).
566 class VISIBILITY_HIDDEN LocalSpiller : public Spiller {
567 MachineRegisterInfo *RegInfo;
568 const TargetRegisterInfo *TRI;
569 const TargetInstrInfo *TII;
570 DenseMap<MachineInstr*, unsigned> DistanceMap;
571 public:
572 bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
573 RegInfo = &MF.getRegInfo();
574 TRI = MF.getTarget().getRegisterInfo();
575 TII = MF.getTarget().getInstrInfo();
576 DOUT << "\n**** Local spiller rewriting function '"
577 << MF.getFunction()->getName() << "':\n";
578 DOUT << "**** Machine Instrs (NOTE! Does not include spills and reloads!)"
579 " ****\n";
580 DEBUG(MF.dump());
581
582 // Spills - Keep track of which spilled values are available in physregs
583 // so that we can choose to reuse the physregs instead of emitting
584 // reloads. This is usually refreshed per basic block.
585 AvailableSpills Spills(TRI, TII);
586
Evan Chengf7923522009-02-26 02:30:42 +0000587 // Keep track of kill information.
588 BitVector RegKills(TRI->getNumRegs());
589 std::vector<MachineOperand*> KillOps;
590 KillOps.resize(TRI->getNumRegs(), NULL);
591
Evan Cheng752272a2009-02-11 08:24:21 +0000592 // SingleEntrySuccs - Successor blocks which have a single predecessor.
593 SmallVector<MachineBasicBlock*, 4> SinglePredSuccs;
594 SmallPtrSet<MachineBasicBlock*,16> EarlyVisited;
595
596 // Traverse the basic blocks depth first.
597 MachineBasicBlock *Entry = MF.begin();
598 SmallPtrSet<MachineBasicBlock*,16> Visited;
599 for (df_ext_iterator<MachineBasicBlock*,
600 SmallPtrSet<MachineBasicBlock*,16> >
601 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
602 DFI != E; ++DFI) {
603 MachineBasicBlock *MBB = *DFI;
604 if (!EarlyVisited.count(MBB))
Evan Chengf7923522009-02-26 02:30:42 +0000605 RewriteMBB(*MBB, VRM, Spills, RegKills, KillOps);
Evan Cheng752272a2009-02-11 08:24:21 +0000606
607 // If this MBB is the only predecessor of a successor. Keep the
608 // availability information and visit it next.
609 do {
610 // Keep visiting single predecessor successor as long as possible.
611 SinglePredSuccs.clear();
612 findSinglePredSuccessor(MBB, SinglePredSuccs);
613 if (SinglePredSuccs.empty())
614 MBB = 0;
615 else {
616 // FIXME: More than one successors, each of which has MBB has
617 // the only predecessor.
618 MBB = SinglePredSuccs[0];
Evan Cheng6d209c42009-02-12 09:43:23 +0000619 if (!Visited.count(MBB) && EarlyVisited.insert(MBB)) {
Evan Chengf7923522009-02-26 02:30:42 +0000620 Spills.AddAvailableRegsToLiveIn(*MBB, RegKills, KillOps);
621 RewriteMBB(*MBB, VRM, Spills, RegKills, KillOps);
Evan Cheng6d209c42009-02-12 09:43:23 +0000622 }
Evan Cheng752272a2009-02-11 08:24:21 +0000623 }
624 } while (MBB);
625
626 // Clear the availability info.
627 Spills.clear();
628 }
629
630 DOUT << "**** Post Machine Instrs ****\n";
631 DEBUG(MF.dump());
632
633 // Mark unused spill slots.
634 MachineFrameInfo *MFI = MF.getFrameInfo();
635 int SS = VRM.getLowSpillSlot();
636 if (SS != VirtRegMap::NO_STACK_SLOT)
637 for (int e = VRM.getHighSpillSlot(); SS <= e; ++SS)
638 if (!VRM.isSpillSlotUsed(SS)) {
639 MFI->RemoveStackObject(SS);
640 ++NumDSS;
641 }
642
643 return true;
644 }
645 private:
646 void TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist,
647 unsigned Reg, BitVector &RegKills,
648 std::vector<MachineOperand*> &KillOps);
649 bool PrepForUnfoldOpti(MachineBasicBlock &MBB,
650 MachineBasicBlock::iterator &MII,
651 std::vector<MachineInstr*> &MaybeDeadStores,
652 AvailableSpills &Spills, BitVector &RegKills,
653 std::vector<MachineOperand*> &KillOps,
654 VirtRegMap &VRM);
655 bool CommuteToFoldReload(MachineBasicBlock &MBB,
656 MachineBasicBlock::iterator &MII,
657 unsigned VirtReg, unsigned SrcReg, int SS,
Evan Chengf7923522009-02-26 02:30:42 +0000658 AvailableSpills &Spills,
Evan Cheng752272a2009-02-11 08:24:21 +0000659 BitVector &RegKills,
660 std::vector<MachineOperand*> &KillOps,
661 const TargetRegisterInfo *TRI,
662 VirtRegMap &VRM);
663 void SpillRegToStackSlot(MachineBasicBlock &MBB,
664 MachineBasicBlock::iterator &MII,
665 int Idx, unsigned PhysReg, int StackSlot,
666 const TargetRegisterClass *RC,
667 bool isAvailable, MachineInstr *&LastStore,
668 AvailableSpills &Spills,
669 SmallSet<MachineInstr*, 4> &ReMatDefs,
670 BitVector &RegKills,
671 std::vector<MachineOperand*> &KillOps,
672 VirtRegMap &VRM);
673 void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
Evan Chengf7923522009-02-26 02:30:42 +0000674 AvailableSpills &Spills,
675 BitVector &RegKills, std::vector<MachineOperand*> &KillOps);
Evan Cheng752272a2009-02-11 08:24:21 +0000676 };
677}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000678
Evan Cheng28bb4622007-07-11 19:17:18 +0000679/// InvalidateKills - MI is going to be deleted. If any of its operands are
680/// marked kill, then invalidate the information.
681static void InvalidateKills(MachineInstr &MI, BitVector &RegKills,
Evan Chengc91f0b82007-08-14 20:23:13 +0000682 std::vector<MachineOperand*> &KillOps,
Evan Cheng66f71632007-10-19 21:23:22 +0000683 SmallVector<unsigned, 2> *KillRegs = NULL) {
Evan Cheng28bb4622007-07-11 19:17:18 +0000684 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
685 MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000686 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
Evan Cheng28bb4622007-07-11 19:17:18 +0000687 continue;
688 unsigned Reg = MO.getReg();
Evan Chenge3b8a482008-08-05 21:51:46 +0000689 if (TargetRegisterInfo::isVirtualRegister(Reg))
690 continue;
Evan Chengb6ca4b32007-08-14 23:25:37 +0000691 if (KillRegs)
692 KillRegs->push_back(Reg);
Evan Chenge3b8a482008-08-05 21:51:46 +0000693 assert(Reg < KillOps.size());
Evan Cheng28bb4622007-07-11 19:17:18 +0000694 if (KillOps[Reg] == &MO) {
695 RegKills.reset(Reg);
696 KillOps[Reg] = NULL;
697 }
698 }
699}
700
Evan Chengb6ca4b32007-08-14 23:25:37 +0000701/// InvalidateRegDef - If the def operand of the specified def MI is now dead
702/// (since it's spill instruction is removed), mark it isDead. Also checks if
703/// the def MI has other definition operands that are not dead. Returns it by
704/// reference.
705static bool InvalidateRegDef(MachineBasicBlock::iterator I,
706 MachineInstr &NewDef, unsigned Reg,
707 bool &HasLiveDef) {
708 // Due to remat, it's possible this reg isn't being reused. That is,
709 // the def of this reg (by prev MI) is now dead.
710 MachineInstr *DefMI = I;
711 MachineOperand *DefOp = NULL;
712 for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) {
713 MachineOperand &MO = DefMI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000714 if (MO.isReg() && MO.isDef()) {
Evan Chengb6ca4b32007-08-14 23:25:37 +0000715 if (MO.getReg() == Reg)
716 DefOp = &MO;
717 else if (!MO.isDead())
718 HasLiveDef = true;
719 }
720 }
721 if (!DefOp)
722 return false;
723
724 bool FoundUse = false, Done = false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000725 MachineBasicBlock::iterator E = &NewDef;
Evan Chengb6ca4b32007-08-14 23:25:37 +0000726 ++I; ++E;
727 for (; !Done && I != E; ++I) {
728 MachineInstr *NMI = I;
729 for (unsigned j = 0, ee = NMI->getNumOperands(); j != ee; ++j) {
730 MachineOperand &MO = NMI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +0000731 if (!MO.isReg() || MO.getReg() != Reg)
Evan Chengb6ca4b32007-08-14 23:25:37 +0000732 continue;
733 if (MO.isUse())
734 FoundUse = true;
735 Done = true; // Stop after scanning all the operands of this MI.
736 }
737 }
738 if (!FoundUse) {
739 // Def is dead!
740 DefOp->setIsDead();
741 return true;
742 }
743 return false;
744}
745
Evan Cheng28bb4622007-07-11 19:17:18 +0000746/// UpdateKills - Track and update kill info. If a MI reads a register that is
747/// marked kill, then it must be due to register reuse. Transfer the kill info
748/// over.
749static void UpdateKills(MachineInstr &MI, BitVector &RegKills,
Evan Cheng67845982008-10-17 06:16:07 +0000750 std::vector<MachineOperand*> &KillOps,
751 const TargetRegisterInfo* TRI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000752 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng28bb4622007-07-11 19:17:18 +0000753 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
754 MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000755 if (!MO.isReg() || !MO.isUse())
Evan Cheng28bb4622007-07-11 19:17:18 +0000756 continue;
757 unsigned Reg = MO.getReg();
758 if (Reg == 0)
759 continue;
760
Evan Cheng70366b92008-03-21 19:09:30 +0000761 if (RegKills[Reg] && KillOps[Reg]->getParent() != &MI) {
Evan Cheng28bb4622007-07-11 19:17:18 +0000762 // That can't be right. Register is killed but not re-defined and it's
763 // being reused. Let's fix that.
Chris Lattnerf7382302007-12-30 21:56:09 +0000764 KillOps[Reg]->setIsKill(false);
Evan Cheng39c883c2007-12-11 23:36:57 +0000765 KillOps[Reg] = NULL;
766 RegKills.reset(Reg);
Chris Lattner749c6f62008-01-07 07:27:27 +0000767 if (i < TID.getNumOperands() &&
768 TID.getOperandConstraint(i, TOI::TIED_TO) == -1)
Evan Cheng28bb4622007-07-11 19:17:18 +0000769 // Unless it's a two-address operand, this is the new kill.
770 MO.setIsKill();
771 }
Evan Cheng28bb4622007-07-11 19:17:18 +0000772 if (MO.isKill()) {
773 RegKills.set(Reg);
774 KillOps[Reg] = &MO;
775 }
776 }
777
778 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
779 const MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000780 if (!MO.isReg() || !MO.isDef())
Evan Cheng28bb4622007-07-11 19:17:18 +0000781 continue;
782 unsigned Reg = MO.getReg();
783 RegKills.reset(Reg);
784 KillOps[Reg] = NULL;
Evan Cheng67845982008-10-17 06:16:07 +0000785 // It also defines (or partially define) aliases.
786 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
787 RegKills.reset(*AS);
788 KillOps[*AS] = NULL;
789 }
Evan Cheng28bb4622007-07-11 19:17:18 +0000790 }
791}
792
Evan Chengd70dbb52008-02-22 09:24:50 +0000793/// ReMaterialize - Re-materialize definition for Reg targetting DestReg.
794///
795static void ReMaterialize(MachineBasicBlock &MBB,
796 MachineBasicBlock::iterator &MII,
797 unsigned DestReg, unsigned Reg,
Evan Chengca1267c2008-03-31 20:40:39 +0000798 const TargetInstrInfo *TII,
Evan Chengd70dbb52008-02-22 09:24:50 +0000799 const TargetRegisterInfo *TRI,
800 VirtRegMap &VRM) {
Evan Chengca1267c2008-03-31 20:40:39 +0000801 TII->reMaterialize(MBB, MII, DestReg, VRM.getReMaterializedMI(Reg));
Evan Chengd70dbb52008-02-22 09:24:50 +0000802 MachineInstr *NewMI = prior(MII);
803 for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
804 MachineOperand &MO = NewMI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000805 if (!MO.isReg() || MO.getReg() == 0)
Evan Chengd70dbb52008-02-22 09:24:50 +0000806 continue;
807 unsigned VirtReg = MO.getReg();
808 if (TargetRegisterInfo::isPhysicalRegister(VirtReg))
809 continue;
810 assert(MO.isUse());
811 unsigned SubIdx = MO.getSubReg();
812 unsigned Phys = VRM.getPhys(VirtReg);
813 assert(Phys);
814 unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
815 MO.setReg(RReg);
816 }
817 ++NumReMats;
818}
819
Evan Cheng28bb4622007-07-11 19:17:18 +0000820
Chris Lattner7fb64342004-10-01 19:04:51 +0000821// ReusedOp - For each reused operand, we keep track of a bit of information, in
822// case we need to rollback upon processing a new operand. See comments below.
823namespace {
824 struct ReusedOp {
825 // The MachineInstr operand that reused an available value.
826 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000827
Evan Cheng549f27d32007-08-13 23:45:17 +0000828 // StackSlotOrReMat - The spill slot or remat id of the value being reused.
829 unsigned StackSlotOrReMat;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000830
Chris Lattner7fb64342004-10-01 19:04:51 +0000831 // PhysRegReused - The physical register the value was available in.
832 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000833
Chris Lattner7fb64342004-10-01 19:04:51 +0000834 // AssignedPhysReg - The physreg that was assigned for use by the reload.
835 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000836
837 // VirtReg - The virtual register itself.
838 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000839
Chris Lattner8a61a752005-10-06 17:19:06 +0000840 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
841 unsigned vreg)
Evan Cheng90a43c32007-08-15 20:20:34 +0000842 : Operand(o), StackSlotOrReMat(ss), PhysRegReused(prr),
843 AssignedPhysReg(apr), VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000844 };
Chris Lattner540fec62006-02-25 01:51:33 +0000845
846 /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
847 /// is reused instead of reloaded.
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000848 class VISIBILITY_HIDDEN ReuseInfo {
Chris Lattner540fec62006-02-25 01:51:33 +0000849 MachineInstr &MI;
850 std::vector<ReusedOp> Reuses;
Evan Cheng957840b2007-02-21 02:22:03 +0000851 BitVector PhysRegsClobbered;
Chris Lattner540fec62006-02-25 01:51:33 +0000852 public:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000853 ReuseInfo(MachineInstr &mi, const TargetRegisterInfo *tri) : MI(mi) {
854 PhysRegsClobbered.resize(tri->getNumRegs());
Evan Chenge077ef62006-11-04 00:21:55 +0000855 }
Chris Lattner540fec62006-02-25 01:51:33 +0000856
857 bool hasReuses() const {
858 return !Reuses.empty();
859 }
860
861 /// addReuse - If we choose to reuse a virtual register that is already
862 /// available instead of reloading it, remember that we did so.
Evan Cheng549f27d32007-08-13 23:45:17 +0000863 void addReuse(unsigned OpNo, unsigned StackSlotOrReMat,
Chris Lattner540fec62006-02-25 01:51:33 +0000864 unsigned PhysRegReused, unsigned AssignedPhysReg,
865 unsigned VirtReg) {
866 // If the reload is to the assigned register anyway, no undo will be
867 // required.
868 if (PhysRegReused == AssignedPhysReg) return;
869
870 // Otherwise, remember this.
Evan Cheng549f27d32007-08-13 23:45:17 +0000871 Reuses.push_back(ReusedOp(OpNo, StackSlotOrReMat, PhysRegReused,
Chris Lattner540fec62006-02-25 01:51:33 +0000872 AssignedPhysReg, VirtReg));
873 }
Evan Chenge077ef62006-11-04 00:21:55 +0000874
875 void markClobbered(unsigned PhysReg) {
Evan Cheng957840b2007-02-21 02:22:03 +0000876 PhysRegsClobbered.set(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +0000877 }
878
879 bool isClobbered(unsigned PhysReg) const {
Evan Cheng957840b2007-02-21 02:22:03 +0000880 return PhysRegsClobbered.test(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +0000881 }
Chris Lattner540fec62006-02-25 01:51:33 +0000882
883 /// GetRegForReload - We are about to emit a reload into PhysReg. If there
884 /// is some other operand that is using the specified register, either pick
885 /// a new register to use, or evict the previous reload and use this reg.
886 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
887 AvailableSpills &Spills,
Evan Chengfff3e192007-08-14 09:11:18 +0000888 std::vector<MachineInstr*> &MaybeDeadStores,
Evan Cheng28bb4622007-07-11 19:17:18 +0000889 SmallSet<unsigned, 8> &Rejected,
890 BitVector &RegKills,
Evan Cheng549f27d32007-08-13 23:45:17 +0000891 std::vector<MachineOperand*> &KillOps,
892 VirtRegMap &VRM) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000893 const TargetInstrInfo* TII = MI->getParent()->getParent()->getTarget()
894 .getInstrInfo();
895
Chris Lattner540fec62006-02-25 01:51:33 +0000896 if (Reuses.empty()) return PhysReg; // This is most often empty.
897
898 for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
899 ReusedOp &Op = Reuses[ro];
900 // If we find some other reuse that was supposed to use this register
901 // exactly for its reload, we can change this reload to use ITS reload
Evan Cheng3c82cab2007-01-19 22:40:14 +0000902 // register. That is, unless its reload register has already been
903 // considered and subsequently rejected because it has also been reused
904 // by another operand.
905 if (Op.PhysRegReused == PhysReg &&
906 Rejected.count(Op.AssignedPhysReg) == 0) {
Chris Lattner540fec62006-02-25 01:51:33 +0000907 // Yup, use the reload register that we didn't use before.
Evan Cheng3c82cab2007-01-19 22:40:14 +0000908 unsigned NewReg = Op.AssignedPhysReg;
909 Rejected.insert(PhysReg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000910 return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores, Rejected,
Evan Cheng549f27d32007-08-13 23:45:17 +0000911 RegKills, KillOps, VRM);
Chris Lattner540fec62006-02-25 01:51:33 +0000912 } else {
913 // Otherwise, we might also have a problem if a previously reused
914 // value aliases the new register. If so, codegen the previous reload
915 // and use this one.
916 unsigned PRRU = Op.PhysRegReused;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000917 const TargetRegisterInfo *TRI = Spills.getRegInfo();
918 if (TRI->areAliases(PRRU, PhysReg)) {
Chris Lattner540fec62006-02-25 01:51:33 +0000919 // Okay, we found out that an alias of a reused register
920 // was used. This isn't good because it means we have
921 // to undo a previous reuse.
922 MachineBasicBlock *MBB = MI->getParent();
923 const TargetRegisterClass *AliasRC =
Chris Lattner84bc5422007-12-31 04:13:23 +0000924 MBB->getParent()->getRegInfo().getRegClass(Op.VirtReg);
Chris Lattner28bad082006-02-25 02:17:31 +0000925
926 // Copy Op out of the vector and remove it, we're going to insert an
927 // explicit load for it.
928 ReusedOp NewOp = Op;
929 Reuses.erase(Reuses.begin()+ro);
930
931 // Ok, we're going to try to reload the assigned physreg into the
932 // slot that we were supposed to in the first place. However, that
933 // register could hold a reuse. Check to see if it conflicts or
934 // would prefer us to use a different register.
935 unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg,
Evan Cheng28bb4622007-07-11 19:17:18 +0000936 MI, Spills, MaybeDeadStores,
Evan Cheng549f27d32007-08-13 23:45:17 +0000937 Rejected, RegKills, KillOps, VRM);
Chris Lattner28bad082006-02-25 02:17:31 +0000938
Evan Chengd70dbb52008-02-22 09:24:50 +0000939 MachineBasicBlock::iterator MII = MI;
Evan Cheng549f27d32007-08-13 23:45:17 +0000940 if (NewOp.StackSlotOrReMat > VirtRegMap::MAX_STACK_SLOT) {
Evan Chengca1267c2008-03-31 20:40:39 +0000941 ReMaterialize(*MBB, MII, NewPhysReg, NewOp.VirtReg, TII, TRI,VRM);
Evan Cheng549f27d32007-08-13 23:45:17 +0000942 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +0000943 TII->loadRegFromStackSlot(*MBB, MII, NewPhysReg,
Evan Cheng549f27d32007-08-13 23:45:17 +0000944 NewOp.StackSlotOrReMat, AliasRC);
Evan Chengd3653122008-02-27 03:04:06 +0000945 MachineInstr *LoadMI = prior(MII);
946 VRM.addSpillSlotUse(NewOp.StackSlotOrReMat, LoadMI);
Evan Chengfff3e192007-08-14 09:11:18 +0000947 // Any stores to this stack slot are not dead anymore.
948 MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL;
Evan Cheng549f27d32007-08-13 23:45:17 +0000949 ++NumLoads;
950 }
Chris Lattner28bad082006-02-25 02:17:31 +0000951 Spills.ClobberPhysReg(NewPhysReg);
952 Spills.ClobberPhysReg(NewOp.PhysRegReused);
Evan Cheng014264b2008-09-10 20:08:45 +0000953
954 unsigned SubIdx = MI->getOperand(NewOp.Operand).getSubReg();
955 unsigned RReg = SubIdx ? TRI->getSubReg(NewPhysReg, SubIdx) : NewPhysReg;
956 MI->getOperand(NewOp.Operand).setReg(RReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000957
Evan Cheng752272a2009-02-11 08:24:21 +0000958 Spills.addAvailable(NewOp.StackSlotOrReMat, NewPhysReg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000959 --MII;
Evan Cheng67845982008-10-17 06:16:07 +0000960 UpdateKills(*MII, RegKills, KillOps, TRI);
Evan Cheng28bb4622007-07-11 19:17:18 +0000961 DOUT << '\t' << *MII;
Chris Lattner540fec62006-02-25 01:51:33 +0000962
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000963 DOUT << "Reuse undone!\n";
Chris Lattner540fec62006-02-25 01:51:33 +0000964 --NumReused;
Chris Lattner28bad082006-02-25 02:17:31 +0000965
966 // Finally, PhysReg is now available, go ahead and use it.
Chris Lattner540fec62006-02-25 01:51:33 +0000967 return PhysReg;
968 }
969 }
970 }
971 return PhysReg;
972 }
Evan Cheng3c82cab2007-01-19 22:40:14 +0000973
974 /// GetRegForReload - Helper for the above GetRegForReload(). Add a
975 /// 'Rejected' set to remember which registers have been considered and
976 /// rejected for the reload. This avoids infinite looping in case like
977 /// this:
978 /// t1 := op t2, t3
979 /// t2 <- assigned r0 for use by the reload but ended up reuse r1
980 /// t3 <- assigned r1 for use by the reload but ended up reuse r0
981 /// t1 <- desires r1
982 /// sees r1 is taken by t2, tries t2's reload register r0
983 /// sees r0 is taken by t3, tries t3's reload register r1
984 /// sees r1 is taken by t2, tries t2's reload register r0 ...
985 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
986 AvailableSpills &Spills,
Evan Chengfff3e192007-08-14 09:11:18 +0000987 std::vector<MachineInstr*> &MaybeDeadStores,
Evan Cheng28bb4622007-07-11 19:17:18 +0000988 BitVector &RegKills,
Evan Cheng549f27d32007-08-13 23:45:17 +0000989 std::vector<MachineOperand*> &KillOps,
990 VirtRegMap &VRM) {
Chris Lattner08a4d5a2007-01-23 00:59:48 +0000991 SmallSet<unsigned, 8> Rejected;
Evan Cheng28bb4622007-07-11 19:17:18 +0000992 return GetRegForReload(PhysReg, MI, Spills, MaybeDeadStores, Rejected,
Evan Cheng549f27d32007-08-13 23:45:17 +0000993 RegKills, KillOps, VRM);
Evan Cheng3c82cab2007-01-19 22:40:14 +0000994 }
Chris Lattner540fec62006-02-25 01:51:33 +0000995 };
Chris Lattner7fb64342004-10-01 19:04:51 +0000996}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000997
Evan Cheng66f71632007-10-19 21:23:22 +0000998/// PrepForUnfoldOpti - Turn a store folding instruction into a load folding
999/// instruction. e.g.
1000/// xorl %edi, %eax
1001/// movl %eax, -32(%ebp)
1002/// movl -36(%ebp), %eax
Bill Wendlingf059deb2008-02-26 10:51:52 +00001003/// orl %eax, -32(%ebp)
Evan Cheng66f71632007-10-19 21:23:22 +00001004/// ==>
1005/// xorl %edi, %eax
1006/// orl -36(%ebp), %eax
1007/// mov %eax, -32(%ebp)
1008/// This enables unfolding optimization for a subsequent instruction which will
1009/// also eliminate the newly introduced store instruction.
1010bool LocalSpiller::PrepForUnfoldOpti(MachineBasicBlock &MBB,
Evan Cheng87bb9912008-06-13 23:58:02 +00001011 MachineBasicBlock::iterator &MII,
Evan Cheng66f71632007-10-19 21:23:22 +00001012 std::vector<MachineInstr*> &MaybeDeadStores,
Evan Cheng87bb9912008-06-13 23:58:02 +00001013 AvailableSpills &Spills,
1014 BitVector &RegKills,
1015 std::vector<MachineOperand*> &KillOps,
1016 VirtRegMap &VRM) {
Evan Cheng66f71632007-10-19 21:23:22 +00001017 MachineFunction &MF = *MBB.getParent();
1018 MachineInstr &MI = *MII;
1019 unsigned UnfoldedOpc = 0;
1020 unsigned UnfoldPR = 0;
1021 unsigned UnfoldVR = 0;
1022 int FoldedSS = VirtRegMap::NO_STACK_SLOT;
1023 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
Evan Chengc17ba8a2008-03-14 20:44:01 +00001024 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) {
Evan Cheng66f71632007-10-19 21:23:22 +00001025 // Only transform a MI that folds a single register.
1026 if (UnfoldedOpc)
1027 return false;
1028 UnfoldVR = I->second.first;
1029 VirtRegMap::ModRef MR = I->second.second;
Evan Chengc17ba8a2008-03-14 20:44:01 +00001030 // MI2VirtMap be can updated which invalidate the iterator.
1031 // Increment the iterator first.
1032 ++I;
Evan Cheng66f71632007-10-19 21:23:22 +00001033 if (VRM.isAssignedReg(UnfoldVR))
1034 continue;
1035 // If this reference is not a use, any previous store is now dead.
1036 // Otherwise, the store to this stack slot is not dead anymore.
1037 FoldedSS = VRM.getStackSlot(UnfoldVR);
1038 MachineInstr* DeadStore = MaybeDeadStores[FoldedSS];
1039 if (DeadStore && (MR & VirtRegMap::isModRef)) {
1040 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(FoldedSS);
Evan Cheng6130f662008-03-05 00:59:57 +00001041 if (!PhysReg || !DeadStore->readsRegister(PhysReg))
Evan Cheng66f71632007-10-19 21:23:22 +00001042 continue;
1043 UnfoldPR = PhysReg;
Owen Anderson6425f8b2008-01-07 01:35:56 +00001044 UnfoldedOpc = TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(),
Evan Cheng66f71632007-10-19 21:23:22 +00001045 false, true);
1046 }
1047 }
1048
1049 if (!UnfoldedOpc)
1050 return false;
1051
1052 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1053 MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001054 if (!MO.isReg() || MO.getReg() == 0 || !MO.isUse())
Evan Cheng66f71632007-10-19 21:23:22 +00001055 continue;
1056 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001057 if (TargetRegisterInfo::isPhysicalRegister(VirtReg) || MO.getSubReg())
Evan Cheng66f71632007-10-19 21:23:22 +00001058 continue;
1059 if (VRM.isAssignedReg(VirtReg)) {
1060 unsigned PhysReg = VRM.getPhys(VirtReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +00001061 if (PhysReg && TRI->regsOverlap(PhysReg, UnfoldPR))
Evan Cheng66f71632007-10-19 21:23:22 +00001062 return false;
1063 } else if (VRM.isReMaterialized(VirtReg))
1064 continue;
1065 int SS = VRM.getStackSlot(VirtReg);
1066 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1067 if (PhysReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001068 if (TRI->regsOverlap(PhysReg, UnfoldPR))
Evan Cheng66f71632007-10-19 21:23:22 +00001069 return false;
1070 continue;
1071 }
Evan Chenge3b8a482008-08-05 21:51:46 +00001072 if (VRM.hasPhys(VirtReg)) {
1073 PhysReg = VRM.getPhys(VirtReg);
1074 if (!TRI->regsOverlap(PhysReg, UnfoldPR))
1075 continue;
1076 }
Evan Cheng66f71632007-10-19 21:23:22 +00001077
1078 // Ok, we'll need to reload the value into a register which makes
1079 // it impossible to perform the store unfolding optimization later.
1080 // Let's see if it is possible to fold the load if the store is
1081 // unfolded. This allows us to perform the store unfolding
1082 // optimization.
1083 SmallVector<MachineInstr*, 4> NewMIs;
Owen Anderson6425f8b2008-01-07 01:35:56 +00001084 if (TII->unfoldMemoryOperand(MF, &MI, UnfoldVR, false, false, NewMIs)) {
Evan Cheng66f71632007-10-19 21:23:22 +00001085 assert(NewMIs.size() == 1);
1086 MachineInstr *NewMI = NewMIs.back();
1087 NewMIs.clear();
Evan Cheng6130f662008-03-05 00:59:57 +00001088 int Idx = NewMI->findRegisterUseOperandIdx(VirtReg, false);
Evan Cheng81a03822007-11-17 00:40:40 +00001089 assert(Idx != -1);
Dan Gohman4ed76e72009-02-12 17:29:01 +00001090 SmallVector<unsigned, 1> Ops;
Evan Chengaee4af62007-12-02 08:30:39 +00001091 Ops.push_back(Idx);
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001092 MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, NewMI, Ops, SS);
Evan Cheng66f71632007-10-19 21:23:22 +00001093 if (FoldedMI) {
Evan Cheng21b3f312008-02-27 19:57:11 +00001094 VRM.addSpillSlotUse(SS, FoldedMI);
Evan Chengcbfb9b22007-10-22 03:01:44 +00001095 if (!VRM.hasPhys(UnfoldVR))
Evan Cheng66f71632007-10-19 21:23:22 +00001096 VRM.assignVirt2Phys(UnfoldVR, UnfoldPR);
Evan Cheng66f71632007-10-19 21:23:22 +00001097 VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
1098 MII = MBB.insert(MII, FoldedMI);
Evan Cheng7a0f1852008-05-20 08:13:21 +00001099 InvalidateKills(MI, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001100 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng66f71632007-10-19 21:23:22 +00001101 MBB.erase(&MI);
Dan Gohmanfa828572008-07-18 18:28:56 +00001102 MF.DeleteMachineInstr(NewMI);
Evan Cheng66f71632007-10-19 21:23:22 +00001103 return true;
1104 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001105 MF.DeleteMachineInstr(NewMI);
Evan Cheng66f71632007-10-19 21:23:22 +00001106 }
1107 }
1108 return false;
1109}
Chris Lattner7fb64342004-10-01 19:04:51 +00001110
Evan Cheng87bb9912008-06-13 23:58:02 +00001111/// CommuteToFoldReload -
1112/// Look for
1113/// r1 = load fi#1
1114/// r1 = op r1, r2<kill>
1115/// store r1, fi#1
1116///
1117/// If op is commutable and r2 is killed, then we can xform these to
1118/// r2 = op r2, fi#1
1119/// store r2, fi#1
1120bool LocalSpiller::CommuteToFoldReload(MachineBasicBlock &MBB,
1121 MachineBasicBlock::iterator &MII,
1122 unsigned VirtReg, unsigned SrcReg, int SS,
Evan Chengf7923522009-02-26 02:30:42 +00001123 AvailableSpills &Spills,
Evan Cheng87bb9912008-06-13 23:58:02 +00001124 BitVector &RegKills,
1125 std::vector<MachineOperand*> &KillOps,
1126 const TargetRegisterInfo *TRI,
1127 VirtRegMap &VRM) {
1128 if (MII == MBB.begin() || !MII->killsRegister(SrcReg))
1129 return false;
1130
1131 MachineFunction &MF = *MBB.getParent();
1132 MachineInstr &MI = *MII;
1133 MachineBasicBlock::iterator DefMII = prior(MII);
1134 MachineInstr *DefMI = DefMII;
1135 const TargetInstrDesc &TID = DefMI->getDesc();
1136 unsigned NewDstIdx;
1137 if (DefMII != MBB.begin() &&
1138 TID.isCommutable() &&
1139 TII->CommuteChangesDestination(DefMI, NewDstIdx)) {
1140 MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx);
1141 unsigned NewReg = NewDstMO.getReg();
1142 if (!NewDstMO.isKill() || TRI->regsOverlap(NewReg, SrcReg))
1143 return false;
1144 MachineInstr *ReloadMI = prior(DefMII);
1145 int FrameIdx;
1146 unsigned DestReg = TII->isLoadFromStackSlot(ReloadMI, FrameIdx);
1147 if (DestReg != SrcReg || FrameIdx != SS)
1148 return false;
1149 int UseIdx = DefMI->findRegisterUseOperandIdx(DestReg, false);
1150 if (UseIdx == -1)
1151 return false;
1152 int DefIdx = TID.getOperandConstraint(UseIdx, TOI::TIED_TO);
1153 if (DefIdx == -1)
1154 return false;
Dan Gohmand735b802008-10-03 15:45:36 +00001155 assert(DefMI->getOperand(DefIdx).isReg() &&
Evan Cheng87bb9912008-06-13 23:58:02 +00001156 DefMI->getOperand(DefIdx).getReg() == SrcReg);
1157
1158 // Now commute def instruction.
Evan Cheng7a153912008-06-16 07:34:17 +00001159 MachineInstr *CommutedMI = TII->commuteInstruction(DefMI, true);
Evan Cheng87bb9912008-06-13 23:58:02 +00001160 if (!CommutedMI)
1161 return false;
Dan Gohman4ed76e72009-02-12 17:29:01 +00001162 SmallVector<unsigned, 1> Ops;
Evan Cheng87bb9912008-06-13 23:58:02 +00001163 Ops.push_back(NewDstIdx);
1164 MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, CommutedMI, Ops, SS);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001165 // Not needed since foldMemoryOperand returns new MI.
1166 MF.DeleteMachineInstr(CommutedMI);
Evan Cheng7a153912008-06-16 07:34:17 +00001167 if (!FoldedMI)
Evan Cheng87bb9912008-06-13 23:58:02 +00001168 return false;
Evan Cheng87bb9912008-06-13 23:58:02 +00001169
1170 VRM.addSpillSlotUse(SS, FoldedMI);
1171 VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
1172 // Insert new def MI and spill MI.
1173 const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(VirtReg);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001174 TII->storeRegToStackSlot(MBB, &MI, NewReg, true, SS, RC);
Evan Cheng87bb9912008-06-13 23:58:02 +00001175 MII = prior(MII);
1176 MachineInstr *StoreMI = MII;
1177 VRM.addSpillSlotUse(SS, StoreMI);
1178 VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod);
1179 MII = MBB.insert(MII, FoldedMI); // Update MII to backtrack.
1180
1181 // Delete all 3 old instructions.
Evan Cheng87bb9912008-06-13 23:58:02 +00001182 InvalidateKills(*ReloadMI, RegKills, KillOps);
1183 VRM.RemoveMachineInstrFromMaps(ReloadMI);
1184 MBB.erase(ReloadMI);
Evan Cheng7a153912008-06-16 07:34:17 +00001185 InvalidateKills(*DefMI, RegKills, KillOps);
1186 VRM.RemoveMachineInstrFromMaps(DefMI);
1187 MBB.erase(DefMI);
1188 InvalidateKills(MI, RegKills, KillOps);
1189 VRM.RemoveMachineInstrFromMaps(&MI);
1190 MBB.erase(&MI);
1191
Evan Chengf7923522009-02-26 02:30:42 +00001192 // If NewReg was previously holding value of some SS, it's now clobbered.
1193 // This has to be done now because it's a physical register. When this
1194 // instruction is re-visited, it's ignored.
1195 Spills.ClobberPhysReg(NewReg);
1196
Evan Cheng87bb9912008-06-13 23:58:02 +00001197 ++NumCommutes;
1198 return true;
1199 }
1200
1201 return false;
1202}
1203
Evan Cheng7277a7d2007-11-02 17:35:08 +00001204/// findSuperReg - Find the SubReg's super-register of given register class
1205/// where its SubIdx sub-register is SubReg.
1206static unsigned findSuperReg(const TargetRegisterClass *RC, unsigned SubReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001207 unsigned SubIdx, const TargetRegisterInfo *TRI) {
Evan Cheng7277a7d2007-11-02 17:35:08 +00001208 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
1209 I != E; ++I) {
1210 unsigned Reg = *I;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001211 if (TRI->getSubReg(Reg, SubIdx) == SubReg)
Evan Cheng7277a7d2007-11-02 17:35:08 +00001212 return Reg;
1213 }
1214 return 0;
1215}
1216
Evan Cheng81a03822007-11-17 00:40:40 +00001217/// SpillRegToStackSlot - Spill a register to a specified stack slot. Check if
1218/// the last store to the same slot is now dead. If so, remove the last store.
1219void LocalSpiller::SpillRegToStackSlot(MachineBasicBlock &MBB,
1220 MachineBasicBlock::iterator &MII,
1221 int Idx, unsigned PhysReg, int StackSlot,
1222 const TargetRegisterClass *RC,
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001223 bool isAvailable, MachineInstr *&LastStore,
Evan Cheng81a03822007-11-17 00:40:40 +00001224 AvailableSpills &Spills,
1225 SmallSet<MachineInstr*, 4> &ReMatDefs,
1226 BitVector &RegKills,
1227 std::vector<MachineOperand*> &KillOps,
Evan Chenge4b39002007-12-03 21:31:55 +00001228 VirtRegMap &VRM) {
Owen Andersonf6372aa2008-01-01 21:11:32 +00001229 TII->storeRegToStackSlot(MBB, next(MII), PhysReg, true, StackSlot, RC);
Evan Chengd3653122008-02-27 03:04:06 +00001230 MachineInstr *StoreMI = next(MII);
1231 VRM.addSpillSlotUse(StackSlot, StoreMI);
1232 DOUT << "Store:\t" << *StoreMI;
Evan Cheng81a03822007-11-17 00:40:40 +00001233
1234 // If there is a dead store to this stack slot, nuke it now.
1235 if (LastStore) {
1236 DOUT << "Removed dead store:\t" << *LastStore;
1237 ++NumDSE;
1238 SmallVector<unsigned, 2> KillRegs;
1239 InvalidateKills(*LastStore, RegKills, KillOps, &KillRegs);
1240 MachineBasicBlock::iterator PrevMII = LastStore;
1241 bool CheckDef = PrevMII != MBB.begin();
1242 if (CheckDef)
1243 --PrevMII;
Evan Chengcada2452007-11-28 01:28:46 +00001244 VRM.RemoveMachineInstrFromMaps(LastStore);
Evan Chengd3653122008-02-27 03:04:06 +00001245 MBB.erase(LastStore);
Evan Cheng81a03822007-11-17 00:40:40 +00001246 if (CheckDef) {
1247 // Look at defs of killed registers on the store. Mark the defs
1248 // as dead since the store has been deleted and they aren't
1249 // being reused.
1250 for (unsigned j = 0, ee = KillRegs.size(); j != ee; ++j) {
1251 bool HasOtherDef = false;
1252 if (InvalidateRegDef(PrevMII, *MII, KillRegs[j], HasOtherDef)) {
1253 MachineInstr *DeadDef = PrevMII;
1254 if (ReMatDefs.count(DeadDef) && !HasOtherDef) {
1255 // FIXME: This assumes a remat def does not have side
1256 // effects.
Evan Chengcada2452007-11-28 01:28:46 +00001257 VRM.RemoveMachineInstrFromMaps(DeadDef);
Evan Chengd3653122008-02-27 03:04:06 +00001258 MBB.erase(DeadDef);
Evan Cheng81a03822007-11-17 00:40:40 +00001259 ++NumDRM;
1260 }
1261 }
1262 }
1263 }
1264 }
1265
Evan Chenge4b39002007-12-03 21:31:55 +00001266 LastStore = next(MII);
Evan Cheng81a03822007-11-17 00:40:40 +00001267
1268 // If the stack slot value was previously available in some other
1269 // register, change it now. Otherwise, make the register available,
1270 // in PhysReg.
1271 Spills.ModifyStackSlotOrReMat(StackSlot);
1272 Spills.ClobberPhysReg(PhysReg);
Evan Cheng752272a2009-02-11 08:24:21 +00001273 Spills.addAvailable(StackSlot, PhysReg, isAvailable);
Evan Cheng81a03822007-11-17 00:40:40 +00001274 ++NumStores;
1275}
1276
Evan Cheng7a0f1852008-05-20 08:13:21 +00001277/// TransferDeadness - A identity copy definition is dead and it's being
1278/// removed. Find the last def or use and mark it as dead / kill.
1279void LocalSpiller::TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist,
1280 unsigned Reg, BitVector &RegKills,
1281 std::vector<MachineOperand*> &KillOps) {
1282 int LastUDDist = -1;
1283 MachineInstr *LastUDMI = NULL;
1284 for (MachineRegisterInfo::reg_iterator RI = RegInfo->reg_begin(Reg),
1285 RE = RegInfo->reg_end(); RI != RE; ++RI) {
1286 MachineInstr *UDMI = &*RI;
1287 if (UDMI->getParent() != MBB)
1288 continue;
1289 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI);
1290 if (DI == DistanceMap.end() || DI->second > CurDist)
1291 continue;
1292 if ((int)DI->second < LastUDDist)
1293 continue;
1294 LastUDDist = DI->second;
1295 LastUDMI = UDMI;
1296 }
1297
1298 if (LastUDMI) {
1299 const TargetInstrDesc &TID = LastUDMI->getDesc();
1300 MachineOperand *LastUD = NULL;
1301 for (unsigned i = 0, e = LastUDMI->getNumOperands(); i != e; ++i) {
1302 MachineOperand &MO = LastUDMI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001303 if (!MO.isReg() || MO.getReg() != Reg)
Evan Cheng7a0f1852008-05-20 08:13:21 +00001304 continue;
1305 if (!LastUD || (LastUD->isUse() && MO.isDef()))
1306 LastUD = &MO;
1307 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1)
1308 return;
1309 }
1310 if (LastUD->isDef())
1311 LastUD->setIsDead();
1312 else {
1313 LastUD->setIsKill();
1314 RegKills.set(Reg);
1315 KillOps[Reg] = LastUD;
1316 }
1317 }
1318}
1319
Chris Lattner7fb64342004-10-01 19:04:51 +00001320/// rewriteMBB - Keep track of which spills are available even after the
Bill Wendling92c1e122009-02-13 02:16:35 +00001321/// register allocator is done with them. If possible, avid reloading vregs.
Evan Cheng752272a2009-02-11 08:24:21 +00001322void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
Evan Chengf7923522009-02-26 02:30:42 +00001323 AvailableSpills &Spills, BitVector &RegKills,
1324 std::vector<MachineOperand*> &KillOps) {
Evan Cheng752272a2009-02-11 08:24:21 +00001325 DOUT << "\n**** Local spiller rewriting MBB '"
1326 << MBB.getBasicBlock()->getName() << ":\n";
Chris Lattner7fb64342004-10-01 19:04:51 +00001327
Evan Chengfff3e192007-08-14 09:11:18 +00001328 MachineFunction &MF = *MBB.getParent();
Owen Andersond10fd972007-12-31 06:32:00 +00001329
Chris Lattner52b25db2004-10-01 19:47:12 +00001330 // MaybeDeadStores - When we need to write a value back into a stack slot,
1331 // keep track of the inserted store. If the stack slot value is never read
1332 // (because the value was used from some available register, for example), and
1333 // subsequently stored to, the original store is dead. This map keeps track
1334 // of inserted stores that are not used. If we see a subsequent store to the
1335 // same stack slot, the original store is deleted.
Evan Chengfff3e192007-08-14 09:11:18 +00001336 std::vector<MachineInstr*> MaybeDeadStores;
1337 MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL);
Chris Lattner52b25db2004-10-01 19:47:12 +00001338
Evan Chengb6ca4b32007-08-14 23:25:37 +00001339 // ReMatDefs - These are rematerializable def MIs which are not deleted.
1340 SmallSet<MachineInstr*, 4> ReMatDefs;
1341
Evan Chengf7923522009-02-26 02:30:42 +00001342 // Clear kill info.
1343 RegKills.reset();
1344 KillOps.clear();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001345 KillOps.resize(TRI->getNumRegs(), NULL);
Evan Cheng0c40d722007-07-11 05:28:39 +00001346
Evan Cheng7a0f1852008-05-20 08:13:21 +00001347 unsigned Dist = 0;
1348 DistanceMap.clear();
Chris Lattner7fb64342004-10-01 19:04:51 +00001349 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
1350 MII != E; ) {
Chris Lattner7fb64342004-10-01 19:04:51 +00001351 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
Evan Cheng0c40d722007-07-11 05:28:39 +00001352
Evan Cheng66f71632007-10-19 21:23:22 +00001353 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
Evan Cheng0c40d722007-07-11 05:28:39 +00001354 bool Erased = false;
1355 bool BackTracked = false;
Evan Cheng66f71632007-10-19 21:23:22 +00001356 if (PrepForUnfoldOpti(MBB, MII,
1357 MaybeDeadStores, Spills, RegKills, KillOps, VRM))
1358 NextMII = next(MII);
Chris Lattner7fb64342004-10-01 19:04:51 +00001359
Evan Cheng66f71632007-10-19 21:23:22 +00001360 MachineInstr &MI = *MII;
Chris Lattner749c6f62008-01-07 07:27:27 +00001361 const TargetInstrDesc &TID = MI.getDesc();
Evan Chenge077ef62006-11-04 00:21:55 +00001362
Evan Cheng676dd7c2008-03-11 07:19:34 +00001363 if (VRM.hasEmergencySpills(&MI)) {
1364 // Spill physical register(s) in the rare case the allocator has run out
1365 // of registers to allocate.
1366 SmallSet<int, 4> UsedSS;
1367 std::vector<unsigned> &EmSpills = VRM.getEmergencySpills(&MI);
1368 for (unsigned i = 0, e = EmSpills.size(); i != e; ++i) {
1369 unsigned PhysReg = EmSpills[i];
1370 const TargetRegisterClass *RC =
1371 TRI->getPhysicalRegisterRegClass(PhysReg);
1372 assert(RC && "Unable to determine register class!");
1373 int SS = VRM.getEmergencySpillSlot(RC);
1374 if (UsedSS.count(SS))
1375 assert(0 && "Need to spill more than one physical registers!");
1376 UsedSS.insert(SS);
1377 TII->storeRegToStackSlot(MBB, MII, PhysReg, true, SS, RC);
1378 MachineInstr *StoreMI = prior(MII);
1379 VRM.addSpillSlotUse(SS, StoreMI);
1380 TII->loadRegFromStackSlot(MBB, next(MII), PhysReg, SS, RC);
1381 MachineInstr *LoadMI = next(MII);
1382 VRM.addSpillSlotUse(SS, LoadMI);
Evan Chengc1f53c72008-03-11 21:34:46 +00001383 ++NumPSpills;
Evan Cheng676dd7c2008-03-11 07:19:34 +00001384 }
Evan Cheng17d5f542008-03-12 00:14:07 +00001385 NextMII = next(MII);
Evan Cheng676dd7c2008-03-11 07:19:34 +00001386 }
1387
Evan Cheng0cbb1162007-11-29 01:06:25 +00001388 // Insert restores here if asked to.
1389 if (VRM.isRestorePt(&MI)) {
1390 std::vector<unsigned> &RestoreRegs = VRM.getRestorePtRestores(&MI);
1391 for (unsigned i = 0, e = RestoreRegs.size(); i != e; ++i) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001392 unsigned VirtReg = RestoreRegs[e-i-1]; // Reverse order.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001393 if (!VRM.getPreSplitReg(VirtReg))
1394 continue; // Split interval spilled again.
1395 unsigned Phys = VRM.getPhys(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001396 RegInfo->setPhysRegUsed(Phys);
Evan Cheng752272a2009-02-11 08:24:21 +00001397
1398 // Check if the value being restored if available. If so, it must be
1399 // from a predecessor BB that fallthrough into this BB. We do not
1400 // expect:
1401 // BB1:
1402 // r1 = load fi#1
1403 // ...
1404 // = r1<kill>
1405 // ... # r1 not clobbered
1406 // ...
1407 // = load fi#1
1408 bool DoReMat = VRM.isReMaterialized(VirtReg);
1409 int SSorRMId = DoReMat
1410 ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg);
Evan Cheng86791192009-02-12 10:32:17 +00001411 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Evan Chengf7923522009-02-26 02:30:42 +00001412 unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId);
Evan Cheng752272a2009-02-11 08:24:21 +00001413 if (InReg == Phys) {
1414 // If the value is already available in the expected register, save
1415 // a reload / remat.
1416 if (SSorRMId)
1417 DOUT << "Reusing RM#" << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1;
1418 else
1419 DOUT << "Reusing SS#" << SSorRMId;
1420 DOUT << " from physreg "
1421 << TRI->getName(InReg) << " for vreg"
1422 << VirtReg <<" instead of reloading into physreg "
1423 << TRI->getName(Phys) << "\n";
1424 ++NumOmitted;
1425 continue;
1426 } else if (InReg && InReg != Phys) {
1427 if (SSorRMId)
1428 DOUT << "Reusing RM#" << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1;
1429 else
1430 DOUT << "Reusing SS#" << SSorRMId;
1431 DOUT << " from physreg "
1432 << TRI->getName(InReg) << " for vreg"
1433 << VirtReg <<" by copying it into physreg "
1434 << TRI->getName(Phys) << "\n";
1435
1436 // If the reloaded / remat value is available in another register,
1437 // copy it to the desired register.
Evan Cheng752272a2009-02-11 08:24:21 +00001438 TII->copyRegToReg(MBB, &MI, Phys, InReg, RC, RC);
1439
1440 // This invalidates Phys.
1441 Spills.ClobberPhysReg(Phys);
1442 // Remember it's available.
1443 Spills.addAvailable(SSorRMId, Phys);
1444
1445 // Mark is killed.
1446 MachineInstr *CopyMI = prior(MII);
1447 MachineOperand *KillOpnd = CopyMI->findRegisterUseOperand(InReg);
1448 KillOpnd->setIsKill();
1449 UpdateKills(*CopyMI, RegKills, KillOps, TRI);
1450
1451 DOUT << '\t' << *CopyMI;
1452 ++NumCopified;
1453 continue;
1454 }
1455
Evan Cheng0cbb1162007-11-29 01:06:25 +00001456 if (VRM.isReMaterialized(VirtReg)) {
Evan Chengca1267c2008-03-31 20:40:39 +00001457 ReMaterialize(MBB, MII, Phys, VirtReg, TII, TRI, VRM);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001458 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +00001459 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Evan Cheng752272a2009-02-11 08:24:21 +00001460 TII->loadRegFromStackSlot(MBB, &MI, Phys, SSorRMId, RC);
Evan Chengd3653122008-02-27 03:04:06 +00001461 MachineInstr *LoadMI = prior(MII);
Evan Cheng752272a2009-02-11 08:24:21 +00001462 VRM.addSpillSlotUse(SSorRMId, LoadMI);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001463 ++NumLoads;
1464 }
Evan Cheng752272a2009-02-11 08:24:21 +00001465
Evan Cheng0cbb1162007-11-29 01:06:25 +00001466 // This invalidates Phys.
1467 Spills.ClobberPhysReg(Phys);
Evan Cheng752272a2009-02-11 08:24:21 +00001468 // Remember it's available.
1469 Spills.addAvailable(SSorRMId, Phys);
1470
Evan Cheng67845982008-10-17 06:16:07 +00001471 UpdateKills(*prior(MII), RegKills, KillOps, TRI);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001472 DOUT << '\t' << *prior(MII);
1473 }
1474 }
1475
Evan Cheng81a03822007-11-17 00:40:40 +00001476 // Insert spills here if asked to.
Evan Chengcada2452007-11-28 01:28:46 +00001477 if (VRM.isSpillPt(&MI)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001478 std::vector<std::pair<unsigned,bool> > &SpillRegs =
1479 VRM.getSpillPtSpills(&MI);
Evan Chengcada2452007-11-28 01:28:46 +00001480 for (unsigned i = 0, e = SpillRegs.size(); i != e; ++i) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001481 unsigned VirtReg = SpillRegs[i].first;
1482 bool isKill = SpillRegs[i].second;
Evan Chengcada2452007-11-28 01:28:46 +00001483 if (!VRM.getPreSplitReg(VirtReg))
1484 continue; // Split interval spilled again.
Chris Lattner84bc5422007-12-31 04:13:23 +00001485 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Evan Chengcada2452007-11-28 01:28:46 +00001486 unsigned Phys = VRM.getPhys(VirtReg);
1487 int StackSlot = VRM.getStackSlot(VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001488 TII->storeRegToStackSlot(MBB, next(MII), Phys, isKill, StackSlot, RC);
Evan Chengd64b5c82007-12-05 03:14:33 +00001489 MachineInstr *StoreMI = next(MII);
Evan Chengd3653122008-02-27 03:04:06 +00001490 VRM.addSpillSlotUse(StackSlot, StoreMI);
Evan Cheng4191b962008-03-12 00:02:46 +00001491 DOUT << "Store:\t" << *StoreMI;
Evan Chengd64b5c82007-12-05 03:14:33 +00001492 VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod);
Evan Chengcada2452007-11-28 01:28:46 +00001493 }
Evan Chenge4b39002007-12-03 21:31:55 +00001494 NextMII = next(MII);
Evan Cheng81a03822007-11-17 00:40:40 +00001495 }
1496
1497 /// ReusedOperands - Keep track of operand reuse in case we need to undo
1498 /// reuse.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001499 ReuseInfo ReusedOperands(MI, TRI);
Evan Chengb2fd65f2008-02-22 19:22:06 +00001500 SmallVector<unsigned, 4> VirtUseOps;
Chris Lattner7fb64342004-10-01 19:04:51 +00001501 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1502 MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001503 if (!MO.isReg() || MO.getReg() == 0)
Chris Lattner50ea01e2005-09-09 20:29:51 +00001504 continue; // Ignore non-register operands.
1505
Evan Cheng32dfbea2007-10-12 08:50:34 +00001506 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001507 if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) {
Chris Lattner50ea01e2005-09-09 20:29:51 +00001508 // Ignore physregs for spilling, but remember that it is used by this
1509 // function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001510 RegInfo->setPhysRegUsed(VirtReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001511 continue;
1512 }
Evan Chengb2fd65f2008-02-22 19:22:06 +00001513
1514 // We want to process implicit virtual register uses first.
1515 if (MO.isImplicit())
Evan Cheng4cce6b42008-04-11 17:53:36 +00001516 // If the virtual register is implicitly defined, emit a implicit_def
1517 // before so scavenger knows it's "defined".
Evan Chengb2fd65f2008-02-22 19:22:06 +00001518 VirtUseOps.insert(VirtUseOps.begin(), i);
1519 else
1520 VirtUseOps.push_back(i);
1521 }
1522
1523 // Process all of the spilled uses and all non spilled reg references.
Evan Chengaf42fe32008-10-17 20:56:41 +00001524 SmallVector<int, 2> PotentialDeadStoreSlots;
Evan Chengb2fd65f2008-02-22 19:22:06 +00001525 for (unsigned j = 0, e = VirtUseOps.size(); j != e; ++j) {
1526 unsigned i = VirtUseOps[j];
1527 MachineOperand &MO = MI.getOperand(i);
1528 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001529 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
Evan Chengb2fd65f2008-02-22 19:22:06 +00001530 "Not a virtual register?");
Evan Cheng70306f82007-12-03 09:58:48 +00001531
Evan Chengc498b022007-11-14 07:59:08 +00001532 unsigned SubIdx = MO.getSubReg();
Evan Cheng549f27d32007-08-13 23:45:17 +00001533 if (VRM.isAssignedReg(VirtReg)) {
Chris Lattner50ea01e2005-09-09 20:29:51 +00001534 // This virtual register was assigned a physreg!
1535 unsigned Phys = VRM.getPhys(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001536 RegInfo->setPhysRegUsed(Phys);
Evan Chenge077ef62006-11-04 00:21:55 +00001537 if (MO.isDef())
1538 ReusedOperands.markClobbered(Phys);
Dan Gohman6f0d0242008-02-10 18:45:23 +00001539 unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001540 MI.getOperand(i).setReg(RReg);
Evan Cheng4cce6b42008-04-11 17:53:36 +00001541 if (VRM.isImplicitlyDefined(VirtReg))
Bill Wendlingd62e06c2009-02-03 02:29:34 +00001542 BuildMI(MBB, &MI, MI.getDebugLoc(),
1543 TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001544 continue;
1545 }
1546
1547 // This virtual register is now known to be a spilled value.
1548 if (!MO.isUse())
1549 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +00001550
Evan Cheng549f27d32007-08-13 23:45:17 +00001551 bool DoReMat = VRM.isReMaterialized(VirtReg);
1552 int SSorRMId = DoReMat
1553 ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg);
Evan Chengdc6be192007-08-14 05:42:54 +00001554 int ReuseSlot = SSorRMId;
Chris Lattner7fb64342004-10-01 19:04:51 +00001555
Chris Lattner50ea01e2005-09-09 20:29:51 +00001556 // Check to see if this stack slot is available.
Evan Chengdc6be192007-08-14 05:42:54 +00001557 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001558
1559 // If this is a sub-register use, make sure the reuse register is in the
1560 // right register class. For example, for x86 not all of the 32-bit
1561 // registers have accessible sub-registers.
1562 // Similarly so for EXTRACT_SUBREG. Consider this:
1563 // EDI = op
1564 // MOV32_mr fi#1, EDI
1565 // ...
1566 // = EXTRACT_SUBREG fi#1
1567 // fi#1 is available in EDI, but it cannot be reused because it's not in
1568 // the right register file.
1569 if (PhysReg &&
Evan Chengc498b022007-11-14 07:59:08 +00001570 (SubIdx || MI.getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001571 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001572 if (!RC->contains(PhysReg))
1573 PhysReg = 0;
1574 }
1575
Evan Chengdc6be192007-08-14 05:42:54 +00001576 if (PhysReg) {
Chris Lattner29268692006-09-05 02:12:02 +00001577 // This spilled operand might be part of a two-address operand. If this
1578 // is the case, then changing it will necessarily require changing the
1579 // def part of the instruction as well. However, in some cases, we
1580 // aren't allowed to modify the reused register. If none of these cases
1581 // apply, reuse it.
1582 bool CanReuse = true;
Chris Lattner749c6f62008-01-07 07:27:27 +00001583 int ti = TID.getOperandConstraint(i, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +00001584 if (ti != -1 &&
Dan Gohmand735b802008-10-03 15:45:36 +00001585 MI.getOperand(ti).isReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +00001586 MI.getOperand(ti).getReg() == VirtReg) {
Chris Lattner29268692006-09-05 02:12:02 +00001587 // Okay, we have a two address operand. We can reuse this physreg as
Evan Cheng3c82cab2007-01-19 22:40:14 +00001588 // long as we are allowed to clobber the value and there isn't an
1589 // earlier def that has already clobbered the physreg.
Evan Chengdc6be192007-08-14 05:42:54 +00001590 CanReuse = Spills.canClobberPhysReg(ReuseSlot) &&
Evan Chenge077ef62006-11-04 00:21:55 +00001591 !ReusedOperands.isClobbered(PhysReg);
Chris Lattner29268692006-09-05 02:12:02 +00001592 }
1593
1594 if (CanReuse) {
Chris Lattneraddc55a2006-04-28 01:46:50 +00001595 // If this stack slot value is already available, reuse it!
Evan Chengdc6be192007-08-14 05:42:54 +00001596 if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1597 DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001598 else
Evan Chengdc6be192007-08-14 05:42:54 +00001599 DOUT << "Reusing SS#" << ReuseSlot;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001600 DOUT << " from physreg "
Bill Wendlinge6d088a2008-02-26 21:47:57 +00001601 << TRI->getName(PhysReg) << " for vreg"
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001602 << VirtReg <<" instead of reloading into physreg "
Bill Wendlinge6d088a2008-02-26 21:47:57 +00001603 << TRI->getName(VRM.getPhys(VirtReg)) << "\n";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001604 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001605 MI.getOperand(i).setReg(RReg);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001606
1607 // The only technical detail we have is that we don't know that
1608 // PhysReg won't be clobbered by a reloaded stack slot that occurs
1609 // later in the instruction. In particular, consider 'op V1, V2'.
1610 // If V1 is available in physreg R0, we would choose to reuse it
1611 // here, instead of reloading it into the register the allocator
1612 // indicated (say R1). However, V2 might have to be reloaded
1613 // later, and it might indicate that it needs to live in R0. When
1614 // this occurs, we need to have information available that
1615 // indicates it is safe to use R1 for the reload instead of R0.
1616 //
1617 // To further complicate matters, we might conflict with an alias,
1618 // or R0 and R1 might not be compatible with each other. In this
1619 // case, we actually insert a reload for V1 in R1, ensuring that
1620 // we can get at R0 or its alias.
Evan Chengdc6be192007-08-14 05:42:54 +00001621 ReusedOperands.addReuse(i, ReuseSlot, PhysReg,
Chris Lattneraddc55a2006-04-28 01:46:50 +00001622 VRM.getPhys(VirtReg), VirtReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001623 if (ti != -1)
1624 // Only mark it clobbered if this is a use&def operand.
1625 ReusedOperands.markClobbered(PhysReg);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001626 ++NumReused;
Evan Chengfff3e192007-08-14 09:11:18 +00001627
1628 if (MI.getOperand(i).isKill() &&
1629 ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) {
Evan Chengaf42fe32008-10-17 20:56:41 +00001630
1631 // The store of this spilled value is potentially dead, but we
1632 // won't know for certain until we've confirmed that the re-use
1633 // above is valid, which means waiting until the other operands
1634 // are processed. For now we just track the spill slot, we'll
1635 // remove it after the other operands are processed if valid.
1636
1637 PotentialDeadStoreSlots.push_back(ReuseSlot);
Evan Chengfff3e192007-08-14 09:11:18 +00001638 }
Evan Chengbf189392009-02-17 06:41:03 +00001639
Evan Chengc8bb37a2009-02-28 06:02:14 +00001640 // Assumes this is the last use. IsKill will be unset if reg is reused
1641 // unless it's a two-address operand.
1642 if (ti == -1)
1643 MI.getOperand(i).setIsKill();
Chris Lattneraddc55a2006-04-28 01:46:50 +00001644 continue;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001645 } // CanReuse
Chris Lattneraddc55a2006-04-28 01:46:50 +00001646
1647 // Otherwise we have a situation where we have a two-address instruction
1648 // whose mod/ref operand needs to be reloaded. This reload is already
1649 // available in some register "PhysReg", but if we used PhysReg as the
1650 // operand to our 2-addr instruction, the instruction would modify
1651 // PhysReg. This isn't cool if something later uses PhysReg and expects
1652 // to get its initial value.
Chris Lattner50ea01e2005-09-09 20:29:51 +00001653 //
Chris Lattneraddc55a2006-04-28 01:46:50 +00001654 // To avoid this problem, and to avoid doing a load right after a store,
1655 // we emit a copy from PhysReg into the designated register for this
1656 // operand.
1657 unsigned DesignatedReg = VRM.getPhys(VirtReg);
1658 assert(DesignatedReg && "Must map virtreg to physreg!");
1659
1660 // Note that, if we reused a register for a previous operand, the
1661 // register we want to reload into might not actually be
1662 // available. If this occurs, use the register indicated by the
1663 // reuser.
1664 if (ReusedOperands.hasReuses())
1665 DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI,
Evan Cheng549f27d32007-08-13 23:45:17 +00001666 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001667
Chris Lattnerba1fc3d2006-04-28 04:43:18 +00001668 // If the mapped designated register is actually the physreg we have
1669 // incoming, we don't need to inserted a dead copy.
1670 if (DesignatedReg == PhysReg) {
1671 // If this stack slot value is already available, reuse it!
Evan Chengdc6be192007-08-14 05:42:54 +00001672 if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1673 DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001674 else
Evan Chengdc6be192007-08-14 05:42:54 +00001675 DOUT << "Reusing SS#" << ReuseSlot;
Bill Wendlinge6d088a2008-02-26 21:47:57 +00001676 DOUT << " from physreg " << TRI->getName(PhysReg)
Bill Wendling6ef781f2008-02-27 06:33:05 +00001677 << " for vreg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001678 << " instead of reloading into same physreg.\n";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001679 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001680 MI.getOperand(i).setReg(RReg);
Evan Cheng7277a7d2007-11-02 17:35:08 +00001681 ReusedOperands.markClobbered(RReg);
Chris Lattnerba1fc3d2006-04-28 04:43:18 +00001682 ++NumReused;
1683 continue;
1684 }
1685
Chris Lattner84bc5422007-12-31 04:13:23 +00001686 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1687 RegInfo->setPhysRegUsed(DesignatedReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001688 ReusedOperands.markClobbered(DesignatedReg);
Owen Andersond10fd972007-12-31 06:32:00 +00001689 TII->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC, RC);
Evan Chengde4e9422007-02-25 09:51:27 +00001690
Evan Cheng6b448092007-03-02 08:52:00 +00001691 MachineInstr *CopyMI = prior(MII);
Evan Cheng67845982008-10-17 06:16:07 +00001692 UpdateKills(*CopyMI, RegKills, KillOps, TRI);
Evan Chengde4e9422007-02-25 09:51:27 +00001693
Chris Lattneraddc55a2006-04-28 01:46:50 +00001694 // This invalidates DesignatedReg.
1695 Spills.ClobberPhysReg(DesignatedReg);
1696
Evan Cheng752272a2009-02-11 08:24:21 +00001697 Spills.addAvailable(ReuseSlot, DesignatedReg);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001698 unsigned RReg =
Dan Gohman6f0d0242008-02-10 18:45:23 +00001699 SubIdx ? TRI->getSubReg(DesignatedReg, SubIdx) : DesignatedReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001700 MI.getOperand(i).setReg(RReg);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001701 DOUT << '\t' << *prior(MII);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001702 ++NumReused;
1703 continue;
Evan Cheng66f71632007-10-19 21:23:22 +00001704 } // if (PhysReg)
Chris Lattner50ea01e2005-09-09 20:29:51 +00001705
1706 // Otherwise, reload it and remember that we have it.
1707 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +00001708 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattner7fb64342004-10-01 19:04:51 +00001709
Chris Lattner50ea01e2005-09-09 20:29:51 +00001710 // Note that, if we reused a register for a previous operand, the
1711 // register we want to reload into might not actually be
1712 // available. If this occurs, use the register indicated by the
1713 // reuser.
Chris Lattner540fec62006-02-25 01:51:33 +00001714 if (ReusedOperands.hasReuses())
1715 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
Evan Cheng549f27d32007-08-13 23:45:17 +00001716 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
Chris Lattner540fec62006-02-25 01:51:33 +00001717
Chris Lattner84bc5422007-12-31 04:13:23 +00001718 RegInfo->setPhysRegUsed(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001719 ReusedOperands.markClobbered(PhysReg);
Evan Cheng549f27d32007-08-13 23:45:17 +00001720 if (DoReMat) {
Evan Chengca1267c2008-03-31 20:40:39 +00001721 ReMaterialize(MBB, MII, PhysReg, VirtReg, TII, TRI, VRM);
Evan Cheng91935142007-04-04 07:40:01 +00001722 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +00001723 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001724 TII->loadRegFromStackSlot(MBB, &MI, PhysReg, SSorRMId, RC);
Evan Chengd3653122008-02-27 03:04:06 +00001725 MachineInstr *LoadMI = prior(MII);
1726 VRM.addSpillSlotUse(SSorRMId, LoadMI);
Evan Cheng91935142007-04-04 07:40:01 +00001727 ++NumLoads;
1728 }
Chris Lattner50ea01e2005-09-09 20:29:51 +00001729 // This invalidates PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +00001730 Spills.ClobberPhysReg(PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001731
1732 // Any stores to this stack slot are not dead anymore.
Evan Cheng549f27d32007-08-13 23:45:17 +00001733 if (!DoReMat)
Evan Chengfff3e192007-08-14 09:11:18 +00001734 MaybeDeadStores[SSorRMId] = NULL;
Evan Cheng752272a2009-02-11 08:24:21 +00001735 Spills.addAvailable(SSorRMId, PhysReg);
Evan Chengde4e9422007-02-25 09:51:27 +00001736 // Assumes this is the last use. IsKill will be unset if reg is reused
1737 // unless it's a two-address operand.
Chris Lattner749c6f62008-01-07 07:27:27 +00001738 if (TID.getOperandConstraint(i, TOI::TIED_TO) == -1)
Evan Chengde4e9422007-02-25 09:51:27 +00001739 MI.getOperand(i).setIsKill();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001740 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001741 MI.getOperand(i).setReg(RReg);
Evan Cheng67845982008-10-17 06:16:07 +00001742 UpdateKills(*prior(MII), RegKills, KillOps, TRI);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001743 DOUT << '\t' << *prior(MII);
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001744 }
1745
Evan Chengaf42fe32008-10-17 20:56:41 +00001746 // Ok - now we can remove stores that have been confirmed dead.
1747 for (unsigned j = 0, e = PotentialDeadStoreSlots.size(); j != e; ++j) {
1748 // This was the last use and the spilled value is still available
1749 // for reuse. That means the spill was unnecessary!
1750 int PDSSlot = PotentialDeadStoreSlots[j];
1751 MachineInstr* DeadStore = MaybeDeadStores[PDSSlot];
1752 if (DeadStore) {
1753 DOUT << "Removed dead store:\t" << *DeadStore;
1754 InvalidateKills(*DeadStore, RegKills, KillOps);
1755 VRM.RemoveMachineInstrFromMaps(DeadStore);
1756 MBB.erase(DeadStore);
1757 MaybeDeadStores[PDSSlot] = NULL;
1758 ++NumDSE;
1759 }
1760 }
1761
1762
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001763 DOUT << '\t' << MI;
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001764
Evan Cheng81a03822007-11-17 00:40:40 +00001765
Chris Lattner7fb64342004-10-01 19:04:51 +00001766 // If we have folded references to memory operands, make sure we clear all
1767 // physical registers that may contain the value of the spilled virtual
1768 // register
Evan Cheng66f71632007-10-19 21:23:22 +00001769 SmallSet<int, 2> FoldedSS;
Evan Chengc17ba8a2008-03-14 20:44:01 +00001770 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +00001771 unsigned VirtReg = I->second.first;
1772 VirtRegMap::ModRef MR = I->second.second;
Evan Cheng66f71632007-10-19 21:23:22 +00001773 DOUT << "Folded vreg: " << VirtReg << " MR: " << MR;
Evan Cheng81a03822007-11-17 00:40:40 +00001774
Evan Chengc17ba8a2008-03-14 20:44:01 +00001775 // MI2VirtMap be can updated which invalidate the iterator.
1776 // Increment the iterator first.
1777 ++I;
Chris Lattnercea86882005-09-19 06:56:21 +00001778 int SS = VRM.getStackSlot(VirtReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001779 if (SS == VirtRegMap::NO_STACK_SLOT)
1780 continue;
Evan Cheng90a43c32007-08-15 20:20:34 +00001781 FoldedSS.insert(SS);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001782 DOUT << " - StackSlot: " << SS << "\n";
Chris Lattnercea86882005-09-19 06:56:21 +00001783
1784 // If this folded instruction is just a use, check to see if it's a
1785 // straight load from the virt reg slot.
1786 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
1787 int FrameIdx;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001788 unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx);
1789 if (DestReg && FrameIdx == SS) {
1790 // If this spill slot is available, turn it into a copy (or nothing)
1791 // instead of leaving it as a load!
1792 if (unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SS)) {
1793 DOUT << "Promoted Load To Copy: " << MI;
1794 if (DestReg != InReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001795 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Owen Andersond10fd972007-12-31 06:32:00 +00001796 TII->copyRegToReg(MBB, &MI, DestReg, InReg, RC, RC);
Evan Chengd9c553f2008-09-11 01:02:12 +00001797 MachineOperand *DefMO = MI.findRegisterDefOperand(DestReg);
1798 unsigned SubIdx = DefMO->getSubReg();
Evan Cheng32dfbea2007-10-12 08:50:34 +00001799 // Revisit the copy so we make sure to notice the effects of the
1800 // operation on the destreg (either needing to RA it if it's
1801 // virtual or needing to clobber any values if it's physical).
1802 NextMII = &MI;
1803 --NextMII; // backtrack to the copy.
Evan Chengd9c553f2008-09-11 01:02:12 +00001804 // Propagate the sub-register index over.
1805 if (SubIdx) {
1806 DefMO = NextMII->findRegisterDefOperand(DestReg);
1807 DefMO->setSubReg(SubIdx);
1808 }
Evan Chengbf189392009-02-17 06:41:03 +00001809
1810 // Mark is killed.
1811 MachineOperand *KillOpnd = NextMII->findRegisterUseOperand(InReg);
1812 KillOpnd->setIsKill();
1813
Evan Cheng32dfbea2007-10-12 08:50:34 +00001814 BackTracked = true;
Evan Cheng39c883c2007-12-11 23:36:57 +00001815 } else {
Evan Cheng32dfbea2007-10-12 08:50:34 +00001816 DOUT << "Removing now-noop copy: " << MI;
Evan Cheng39c883c2007-12-11 23:36:57 +00001817 // Unset last kill since it's being reused.
1818 InvalidateKill(InReg, RegKills, KillOps);
1819 }
Evan Chengde4e9422007-02-25 09:51:27 +00001820
Evan Cheng7a0f1852008-05-20 08:13:21 +00001821 InvalidateKills(MI, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001822 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001823 MBB.erase(&MI);
1824 Erased = true;
1825 goto ProcessNextInst;
Chris Lattnercea86882005-09-19 06:56:21 +00001826 }
Evan Cheng7f566252007-10-13 02:50:24 +00001827 } else {
1828 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1829 SmallVector<MachineInstr*, 4> NewMIs;
1830 if (PhysReg &&
Owen Anderson6425f8b2008-01-07 01:35:56 +00001831 TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, false, NewMIs)) {
Evan Cheng7f566252007-10-13 02:50:24 +00001832 MBB.insert(MII, NewMIs[0]);
Evan Cheng7a0f1852008-05-20 08:13:21 +00001833 InvalidateKills(MI, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001834 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng7f566252007-10-13 02:50:24 +00001835 MBB.erase(&MI);
1836 Erased = true;
1837 --NextMII; // backtrack to the unfolded instruction.
1838 BackTracked = true;
1839 goto ProcessNextInst;
1840 }
Chris Lattnercea86882005-09-19 06:56:21 +00001841 }
1842 }
1843
1844 // If this reference is not a use, any previous store is now dead.
1845 // Otherwise, the store to this stack slot is not dead anymore.
Evan Chengfff3e192007-08-14 09:11:18 +00001846 MachineInstr* DeadStore = MaybeDeadStores[SS];
1847 if (DeadStore) {
Evan Cheng66f71632007-10-19 21:23:22 +00001848 bool isDead = !(MR & VirtRegMap::isRef);
Evan Cheng7f566252007-10-13 02:50:24 +00001849 MachineInstr *NewStore = NULL;
Evan Chengcbfb9b22007-10-22 03:01:44 +00001850 if (MR & VirtRegMap::isModRef) {
Evan Cheng7f566252007-10-13 02:50:24 +00001851 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1852 SmallVector<MachineInstr*, 4> NewMIs;
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001853 // We can reuse this physreg as long as we are allowed to clobber
Chris Lattner84bc5422007-12-31 04:13:23 +00001854 // the value and there isn't an earlier def that has already clobbered
1855 // the physreg.
Evan Cheng7f566252007-10-13 02:50:24 +00001856 if (PhysReg &&
Evan Cheng7ebc06b2008-05-07 00:49:28 +00001857 !TII->isStoreToStackSlot(&MI, SS)) { // Not profitable!
1858 MachineOperand *KillOpnd =
1859 DeadStore->findRegisterUseOperand(PhysReg, true);
1860 // Note, if the store is storing a sub-register, it's possible the
1861 // super-register is needed below.
1862 if (KillOpnd && !KillOpnd->getSubReg() &&
1863 TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, true,NewMIs)){
Evan Cheng67845982008-10-17 06:16:07 +00001864 MBB.insert(MII, NewMIs[0]);
Evan Cheng7ebc06b2008-05-07 00:49:28 +00001865 NewStore = NewMIs[1];
1866 MBB.insert(MII, NewStore);
1867 VRM.addSpillSlotUse(SS, NewStore);
Evan Cheng7a0f1852008-05-20 08:13:21 +00001868 InvalidateKills(MI, RegKills, KillOps);
Evan Cheng7ebc06b2008-05-07 00:49:28 +00001869 VRM.RemoveMachineInstrFromMaps(&MI);
1870 MBB.erase(&MI);
1871 Erased = true;
1872 --NextMII;
1873 --NextMII; // backtrack to the unfolded instruction.
1874 BackTracked = true;
1875 isDead = true;
1876 }
Evan Cheng66f71632007-10-19 21:23:22 +00001877 }
Evan Cheng7f566252007-10-13 02:50:24 +00001878 }
1879
1880 if (isDead) { // Previous store is dead.
Chris Lattnercea86882005-09-19 06:56:21 +00001881 // If we get here, the store is dead, nuke it now.
Evan Chengfff3e192007-08-14 09:11:18 +00001882 DOUT << "Removed dead store:\t" << *DeadStore;
1883 InvalidateKills(*DeadStore, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001884 VRM.RemoveMachineInstrFromMaps(DeadStore);
Evan Cheng7f566252007-10-13 02:50:24 +00001885 MBB.erase(DeadStore);
1886 if (!NewStore)
1887 ++NumDSE;
Chris Lattnercea86882005-09-19 06:56:21 +00001888 }
Evan Cheng7f566252007-10-13 02:50:24 +00001889
Evan Chengfff3e192007-08-14 09:11:18 +00001890 MaybeDeadStores[SS] = NULL;
Evan Cheng7f566252007-10-13 02:50:24 +00001891 if (NewStore) {
1892 // Treat this store as a spill merged into a copy. That makes the
1893 // stack slot value available.
1894 VRM.virtFolded(VirtReg, NewStore, VirtRegMap::isMod);
1895 goto ProcessNextInst;
1896 }
Chris Lattnercea86882005-09-19 06:56:21 +00001897 }
1898
1899 // If the spill slot value is available, and this is a new definition of
1900 // the value, the value is not available anymore.
1901 if (MR & VirtRegMap::isMod) {
Chris Lattner07cf1412006-02-03 00:36:31 +00001902 // Notice that the value in this stack slot has been modified.
Evan Cheng549f27d32007-08-13 23:45:17 +00001903 Spills.ModifyStackSlotOrReMat(SS);
Chris Lattnercd816392006-02-02 23:29:36 +00001904
1905 // If this is *just* a mod of the value, check to see if this is just a
1906 // store to the spill slot (i.e. the spill got merged into the copy). If
1907 // so, realize that the vreg is available now, and add the store to the
1908 // MaybeDeadStore info.
1909 int StackSlot;
1910 if (!(MR & VirtRegMap::isRef)) {
1911 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001912 assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
Chris Lattnercd816392006-02-02 23:29:36 +00001913 "Src hasn't been allocated yet?");
Evan Cheng87bb9912008-06-13 23:58:02 +00001914
1915 if (CommuteToFoldReload(MBB, MII, VirtReg, SrcReg, StackSlot,
Evan Chengf7923522009-02-26 02:30:42 +00001916 Spills, RegKills, KillOps, TRI, VRM)) {
Evan Cheng87bb9912008-06-13 23:58:02 +00001917 NextMII = next(MII);
1918 BackTracked = true;
1919 goto ProcessNextInst;
1920 }
1921
Chris Lattner07cf1412006-02-03 00:36:31 +00001922 // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark
Chris Lattnercd816392006-02-02 23:29:36 +00001923 // this as a potentially dead store in case there is a subsequent
1924 // store into the stack slot without a read from it.
1925 MaybeDeadStores[StackSlot] = &MI;
1926
Chris Lattnercd816392006-02-02 23:29:36 +00001927 // If the stack slot value was previously available in some other
Evan Cheng87bb9912008-06-13 23:58:02 +00001928 // register, change it now. Otherwise, make the register
1929 // available in PhysReg.
Evan Cheng752272a2009-02-11 08:24:21 +00001930 Spills.addAvailable(StackSlot, SrcReg, false/*!clobber*/);
Chris Lattnercd816392006-02-02 23:29:36 +00001931 }
1932 }
Chris Lattner7fb64342004-10-01 19:04:51 +00001933 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001934 }
1935
Chris Lattner7fb64342004-10-01 19:04:51 +00001936 // Process all of the spilled defs.
1937 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1938 MachineOperand &MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001939 if (!(MO.isReg() && MO.getReg() && MO.isDef()))
Evan Cheng66f71632007-10-19 21:23:22 +00001940 continue;
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001941
Evan Cheng66f71632007-10-19 21:23:22 +00001942 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001943 if (!TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Evan Cheng66f71632007-10-19 21:23:22 +00001944 // Check to see if this is a noop copy. If so, eliminate the
1945 // instruction before considering the dest reg to be changed.
Evan Cheng04ee5a12009-01-20 19:12:24 +00001946 unsigned Src, Dst, SrcSR, DstSR;
1947 if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) {
Evan Cheng66f71632007-10-19 21:23:22 +00001948 ++NumDCE;
1949 DOUT << "Removing now-noop copy: " << MI;
Evan Cheng7a0f1852008-05-20 08:13:21 +00001950 SmallVector<unsigned, 2> KillRegs;
1951 InvalidateKills(MI, RegKills, KillOps, &KillRegs);
1952 if (MO.isDead() && !KillRegs.empty()) {
Evan Chengbbe4105cd2008-12-02 02:15:36 +00001953 // Source register or an implicit super/sub-register use is killed.
1954 assert(KillRegs[0] == Dst ||
1955 TRI->isSubRegister(KillRegs[0], Dst) ||
1956 TRI->isSuperRegister(KillRegs[0], Dst));
Evan Cheng7a0f1852008-05-20 08:13:21 +00001957 // Last def is now dead.
1958 TransferDeadness(&MBB, Dist, Src, RegKills, KillOps);
1959 }
Evan Chengd3653122008-02-27 03:04:06 +00001960 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng66f71632007-10-19 21:23:22 +00001961 MBB.erase(&MI);
1962 Erased = true;
Evan Cheng66f71632007-10-19 21:23:22 +00001963 Spills.disallowClobberPhysReg(VirtReg);
1964 goto ProcessNextInst;
1965 }
1966
1967 // If it's not a no-op copy, it clobbers the value in the destreg.
1968 Spills.ClobberPhysReg(VirtReg);
1969 ReusedOperands.markClobbered(VirtReg);
1970
1971 // Check to see if this instruction is a load from a stack slot into
1972 // a register. If so, this provides the stack slot value in the reg.
1973 int FrameIdx;
1974 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
1975 assert(DestReg == VirtReg && "Unknown load situation!");
1976
1977 // If it is a folded reference, then it's not safe to clobber.
1978 bool Folded = FoldedSS.count(FrameIdx);
1979 // Otherwise, if it wasn't available, remember that it is now!
Evan Cheng752272a2009-02-11 08:24:21 +00001980 Spills.addAvailable(FrameIdx, DestReg, !Folded);
Evan Cheng66f71632007-10-19 21:23:22 +00001981 goto ProcessNextInst;
1982 }
1983
1984 continue;
1985 }
1986
Evan Chengc498b022007-11-14 07:59:08 +00001987 unsigned SubIdx = MO.getSubReg();
Evan Cheng66f71632007-10-19 21:23:22 +00001988 bool DoReMat = VRM.isReMaterialized(VirtReg);
1989 if (DoReMat)
1990 ReMatDefs.insert(&MI);
1991
1992 // The only vregs left are stack slot definitions.
1993 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001994 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Evan Cheng66f71632007-10-19 21:23:22 +00001995
1996 // If this def is part of a two-address operand, make sure to execute
1997 // the store from the correct physical register.
1998 unsigned PhysReg;
Chris Lattner749c6f62008-01-07 07:27:27 +00001999 int TiedOp = MI.getDesc().findTiedToSrcOperand(i);
Evan Cheng7277a7d2007-11-02 17:35:08 +00002000 if (TiedOp != -1) {
Evan Cheng66f71632007-10-19 21:23:22 +00002001 PhysReg = MI.getOperand(TiedOp).getReg();
Evan Chengc498b022007-11-14 07:59:08 +00002002 if (SubIdx) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00002003 unsigned SuperReg = findSuperReg(RC, PhysReg, SubIdx, TRI);
2004 assert(SuperReg && TRI->getSubReg(SuperReg, SubIdx) == PhysReg &&
Evan Cheng7277a7d2007-11-02 17:35:08 +00002005 "Can't find corresponding super-register!");
2006 PhysReg = SuperReg;
2007 }
2008 } else {
Evan Cheng66f71632007-10-19 21:23:22 +00002009 PhysReg = VRM.getPhys(VirtReg);
2010 if (ReusedOperands.isClobbered(PhysReg)) {
2011 // Another def has taken the assigned physreg. It must have been a
2012 // use&def which got it due to reuse. Undo the reuse!
2013 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
2014 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
2015 }
2016 }
2017
Evan Chenged70cbb32008-03-26 19:03:01 +00002018 assert(PhysReg && "VR not assigned a physical register?");
Chris Lattner84bc5422007-12-31 04:13:23 +00002019 RegInfo->setPhysRegUsed(PhysReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +00002020 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng7277a7d2007-11-02 17:35:08 +00002021 ReusedOperands.markClobbered(RReg);
2022 MI.getOperand(i).setReg(RReg);
2023
Evan Cheng66f71632007-10-19 21:23:22 +00002024 if (!MO.isDead()) {
Evan Cheng66f71632007-10-19 21:23:22 +00002025 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
Evan Cheng35a3e4a2007-12-04 19:19:45 +00002026 SpillRegToStackSlot(MBB, MII, -1, PhysReg, StackSlot, RC, true,
2027 LastStore, Spills, ReMatDefs, RegKills, KillOps, VRM);
Evan Chenge4b39002007-12-03 21:31:55 +00002028 NextMII = next(MII);
Evan Cheng66f71632007-10-19 21:23:22 +00002029
2030 // Check to see if this is a noop copy. If so, eliminate the
2031 // instruction before considering the dest reg to be changed.
2032 {
Evan Cheng04ee5a12009-01-20 19:12:24 +00002033 unsigned Src, Dst, SrcSR, DstSR;
2034 if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) {
Chris Lattner29268692006-09-05 02:12:02 +00002035 ++NumDCE;
Bill Wendlingb2b9c202006-11-17 02:09:07 +00002036 DOUT << "Removing now-noop copy: " << MI;
Evan Cheng7a0f1852008-05-20 08:13:21 +00002037 InvalidateKills(MI, RegKills, KillOps);
Evan Chengd3653122008-02-27 03:04:06 +00002038 VRM.RemoveMachineInstrFromMaps(&MI);
Chris Lattner29268692006-09-05 02:12:02 +00002039 MBB.erase(&MI);
Evan Cheng0c40d722007-07-11 05:28:39 +00002040 Erased = true;
Evan Cheng67845982008-10-17 06:16:07 +00002041 UpdateKills(*LastStore, RegKills, KillOps, TRI);
Chris Lattner29268692006-09-05 02:12:02 +00002042 goto ProcessNextInst;
Chris Lattner7fb64342004-10-01 19:04:51 +00002043 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002044 }
Evan Cheng66f71632007-10-19 21:23:22 +00002045 }
Chris Lattner7fb64342004-10-01 19:04:51 +00002046 }
Chris Lattnercea86882005-09-19 06:56:21 +00002047 ProcessNextInst:
Evan Cheng7a0f1852008-05-20 08:13:21 +00002048 DistanceMap.insert(std::make_pair(&MI, Dist++));
Evan Cheng35a3e4a2007-12-04 19:19:45 +00002049 if (!Erased && !BackTracked) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002050 for (MachineBasicBlock::iterator II = &MI; II != NextMII; ++II)
Evan Cheng67845982008-10-17 06:16:07 +00002051 UpdateKills(*II, RegKills, KillOps, TRI);
Evan Cheng35a3e4a2007-12-04 19:19:45 +00002052 }
Chris Lattner7fb64342004-10-01 19:04:51 +00002053 MII = NextMII;
2054 }
Evan Cheng752272a2009-02-11 08:24:21 +00002055
Chris Lattner8c4d88d2004-09-30 01:54:45 +00002056}
2057
Chris Lattner8c4d88d2004-09-30 01:54:45 +00002058llvm::Spiller* llvm::createSpiller() {
2059 switch (SpillerOpt) {
2060 default: assert(0 && "Unreachable!");
2061 case local:
2062 return new LocalSpiller();
2063 case simple:
2064 return new SimpleSpiller();
2065 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +00002066}