blob: 8bc8b82f1c8938821b440e2a156cba2afa0bfedd [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"
29#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Evan Cheng957840b2007-02-21 02:22:03 +000031#include "llvm/ADT/BitVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/STLExtras.h"
Chris Lattner08a4d5a2007-01-23 00:59:48 +000034#include "llvm/ADT/SmallSet.h"
Chris Lattner27f29162004-10-26 15:35:58 +000035#include <algorithm>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000036using namespace llvm;
37
Chris Lattnercd3245a2006-12-19 22:41:21 +000038STATISTIC(NumSpills, "Number of register spills");
Evan Chengc1f53c72008-03-11 21:34:46 +000039STATISTIC(NumPSpills,"Number of physical register spills");
Evan Cheng2638e1a2007-03-20 08:13:50 +000040STATISTIC(NumReMats, "Number of re-materialization");
Evan Chengb6ca4b32007-08-14 23:25:37 +000041STATISTIC(NumDRM , "Number of re-materializable defs elided");
Chris Lattnercd3245a2006-12-19 22:41:21 +000042STATISTIC(NumStores, "Number of stores added");
43STATISTIC(NumLoads , "Number of loads added");
44STATISTIC(NumReused, "Number of values reused");
45STATISTIC(NumDSE , "Number of dead stores elided");
46STATISTIC(NumDCE , "Number of copies elided");
Evan Chengd3653122008-02-27 03:04:06 +000047STATISTIC(NumDSS , "Number of dead spill slots removed");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000048
Chris Lattnercd3245a2006-12-19 22:41:21 +000049namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000050 enum SpillerName { simple, local };
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000051}
52
Dan Gohman844731a2008-05-13 00:00:25 +000053static cl::opt<SpillerName>
54SpillerOpt("spiller",
55 cl::desc("Spiller to use: (default: local)"),
56 cl::Prefix,
57 cl::values(clEnumVal(simple, " simple spiller"),
58 clEnumVal(local, " local spiller"),
59 clEnumValEnd),
60 cl::init(local));
61
Chris Lattner8c4d88d2004-09-30 01:54:45 +000062//===----------------------------------------------------------------------===//
63// VirtRegMap implementation
64//===----------------------------------------------------------------------===//
65
Chris Lattner29268692006-09-05 02:12:02 +000066VirtRegMap::VirtRegMap(MachineFunction &mf)
67 : TII(*mf.getTarget().getInstrInfo()), MF(mf),
Evan Cheng2638e1a2007-03-20 08:13:50 +000068 Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT),
Evan Cheng81a03822007-11-17 00:40:40 +000069 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
Evan Chengd3653122008-02-27 03:04:06 +000070 Virt2SplitKillMap(0), ReMatMap(NULL), ReMatId(MAX_STACK_SLOT+1),
71 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) {
72 SpillSlotToUsesMap.resize(8);
Evan Cheng4cce6b42008-04-11 17:53:36 +000073 ImplicitDefed.resize(MF.getRegInfo().getLastVirtReg()+1-
74 TargetRegisterInfo::FirstVirtualRegister);
Chris Lattner29268692006-09-05 02:12:02 +000075 grow();
76}
77
Chris Lattner8c4d88d2004-09-30 01:54:45 +000078void VirtRegMap::grow() {
Chris Lattner84bc5422007-12-31 04:13:23 +000079 unsigned LastVirtReg = MF.getRegInfo().getLastVirtReg();
Evan Cheng549f27d32007-08-13 23:45:17 +000080 Virt2PhysMap.grow(LastVirtReg);
81 Virt2StackSlotMap.grow(LastVirtReg);
82 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Cheng81a03822007-11-17 00:40:40 +000083 Virt2SplitMap.grow(LastVirtReg);
Evan Chengadf85902007-12-05 09:51:10 +000084 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +000085 ReMatMap.grow(LastVirtReg);
Evan Cheng4cce6b42008-04-11 17:53:36 +000086 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000087}
88
Chris Lattner8c4d88d2004-09-30 01:54:45 +000089int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000090 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000091 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000092 "attempt to assign stack slot to already spilled register");
Chris Lattner84bc5422007-12-31 04:13:23 +000093 const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(virtReg);
Evan Chengd3653122008-02-27 03:04:06 +000094 int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
95 RC->getAlignment());
96 if (LowSpillSlot == NO_STACK_SLOT)
97 LowSpillSlot = SS;
98 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
99 HighSpillSlot = SS;
100 unsigned Idx = SS-LowSpillSlot;
101 while (Idx >= SpillSlotToUsesMap.size())
102 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
103 Virt2StackSlotMap[virtReg] = SS;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000104 ++NumSpills;
Evan Chengd3653122008-02-27 03:04:06 +0000105 return SS;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000106}
107
Evan Chengd3653122008-02-27 03:04:06 +0000108void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000109 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000110 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000111 "attempt to assign stack slot to already spilled register");
Evan Chengd3653122008-02-27 03:04:06 +0000112 assert((SS >= 0 ||
113 (SS >= MF.getFrameInfo()->getObjectIndexBegin())) &&
Evan Cheng91935142007-04-04 07:40:01 +0000114 "illegal fixed frame index");
Evan Chengd3653122008-02-27 03:04:06 +0000115 Virt2StackSlotMap[virtReg] = SS;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +0000116}
117
Evan Cheng2638e1a2007-03-20 08:13:50 +0000118int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000119 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000120 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Evan Cheng2638e1a2007-03-20 08:13:50 +0000121 "attempt to assign re-mat id to already spilled register");
Evan Cheng549f27d32007-08-13 23:45:17 +0000122 Virt2ReMatIdMap[virtReg] = ReMatId;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000123 return ReMatId++;
124}
125
Evan Cheng549f27d32007-08-13 23:45:17 +0000126void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000127 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000128 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
129 "attempt to assign re-mat id to already spilled register");
130 Virt2ReMatIdMap[virtReg] = id;
131}
132
Evan Cheng676dd7c2008-03-11 07:19:34 +0000133int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
134 std::map<const TargetRegisterClass*, int>::iterator I =
135 EmergencySpillSlots.find(RC);
136 if (I != EmergencySpillSlots.end())
137 return I->second;
138 int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
139 RC->getAlignment());
140 if (LowSpillSlot == NO_STACK_SLOT)
141 LowSpillSlot = SS;
142 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
143 HighSpillSlot = SS;
144 I->second = SS;
145 return SS;
146}
147
Evan Chengd3653122008-02-27 03:04:06 +0000148void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
149 if (!MF.getFrameInfo()->isFixedObjectIndex(FI)) {
David Greenecff86082008-05-22 21:12:21 +0000150 // If FI < LowSpillSlot, this stack reference was produced by
151 // instruction selection and is not a spill
152 if (FI >= LowSpillSlot) {
153 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendlingf3061f82008-05-23 01:29:08 +0000154 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greenecff86082008-05-22 21:12:21 +0000155 && "Invalid spill slot");
156 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
157 }
Evan Chengd3653122008-02-27 03:04:06 +0000158 }
159}
160
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000161void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengaee4af62007-12-02 08:30:39 +0000162 MachineInstr *NewMI, ModRef MRInfo) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000163 // Move previous memory references folded to new instruction.
164 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000165 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000166 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
167 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +0000168 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000169 }
Chris Lattnerdbea9732004-09-30 16:35:08 +0000170
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000171 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000172 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000173}
174
Evan Cheng7f566252007-10-13 02:50:24 +0000175void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
176 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
177 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
178}
179
Evan Chengd3653122008-02-27 03:04:06 +0000180void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
181 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
182 MachineOperand &MO = MI->getOperand(i);
183 if (!MO.isFrameIndex())
184 continue;
185 int FI = MO.getIndex();
186 if (MF.getFrameInfo()->isFixedObjectIndex(FI))
187 continue;
David Greenecff86082008-05-22 21:12:21 +0000188 // This stack reference was produced by instruction selection and
189 // is not a spill
190 if (FI < LowSpillSlot)
191 continue;
Bill Wendlingf3061f82008-05-23 01:29:08 +0000192 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greenecff86082008-05-22 21:12:21 +0000193 && "Invalid spill slot");
Evan Chengd3653122008-02-27 03:04:06 +0000194 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
195 }
196 MI2VirtMap.erase(MI);
197 SpillPt2VirtMap.erase(MI);
198 RestorePt2VirtMap.erase(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +0000199 EmergencySpillMap.erase(MI);
Evan Chengd3653122008-02-27 03:04:06 +0000200}
201
Chris Lattner7f690e62004-09-30 02:15:18 +0000202void VirtRegMap::print(std::ostream &OS) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000203 const TargetRegisterInfo* TRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000204
Chris Lattner7f690e62004-09-30 02:15:18 +0000205 OS << "********** REGISTER MAP **********\n";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000206 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +0000207 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i) {
Chris Lattner7f690e62004-09-30 02:15:18 +0000208 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000209 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling74ab84c2008-02-26 21:11:01 +0000210 << "]\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000211 }
212
Dan Gohman6f0d0242008-02-10 18:45:23 +0000213 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +0000214 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattner7f690e62004-09-30 02:15:18 +0000215 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
216 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
217 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000218}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000219
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000220void VirtRegMap::dump() const {
Dan Gohmanb5769312008-03-12 20:52:10 +0000221 print(cerr);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000222}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000223
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000224
225//===----------------------------------------------------------------------===//
226// Simple Spiller Implementation
227//===----------------------------------------------------------------------===//
228
229Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000230
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000231namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000232 struct VISIBILITY_HIDDEN SimpleSpiller : public Spiller {
Chris Lattner35f27052006-05-01 21:16:03 +0000233 bool runOnMachineFunction(MachineFunction& mf, VirtRegMap &VRM);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000234 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000235}
236
Chris Lattner35f27052006-05-01 21:16:03 +0000237bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000238 DOUT << "********** REWRITE MACHINE CODE **********\n";
239 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000240 const TargetMachine &TM = MF.getTarget();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000241 const TargetInstrInfo &TII = *TM.getInstrInfo();
242
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000243
Chris Lattner4ea1b822004-09-30 02:33:48 +0000244 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
245 // each vreg once (in the case where a spilled vreg is used by multiple
246 // operands). This is always smaller than the number of operands to the
247 // current machine instr, so it should be small.
248 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000249
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000250 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
251 MBBI != E; ++MBBI) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000252 DOUT << MBBI->getBasicBlock()->getName() << ":\n";
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000253 MachineBasicBlock &MBB = *MBBI;
254 for (MachineBasicBlock::iterator MII = MBB.begin(),
255 E = MBB.end(); MII != E; ++MII) {
256 MachineInstr &MI = *MII;
257 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000258 MachineOperand &MO = MI.getOperand(i);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000259 if (MO.isRegister() && MO.getReg()) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000260 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Chris Lattner886dd912005-04-04 21:35:34 +0000261 unsigned VirtReg = MO.getReg();
262 unsigned PhysReg = VRM.getPhys(VirtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000263 if (!VRM.isAssignedReg(VirtReg)) {
Chris Lattner886dd912005-04-04 21:35:34 +0000264 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000265 const TargetRegisterClass* RC =
Chris Lattner84bc5422007-12-31 04:13:23 +0000266 MF.getRegInfo().getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000267
Chris Lattner886dd912005-04-04 21:35:34 +0000268 if (MO.isUse() &&
269 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
270 == LoadedRegs.end()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000271 TII.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Evan Chengd3653122008-02-27 03:04:06 +0000272 MachineInstr *LoadMI = prior(MII);
273 VRM.addSpillSlotUse(StackSlot, LoadMI);
Chris Lattner886dd912005-04-04 21:35:34 +0000274 LoadedRegs.push_back(VirtReg);
275 ++NumLoads;
Evan Chengd3653122008-02-27 03:04:06 +0000276 DOUT << '\t' << *LoadMI;
Chris Lattner886dd912005-04-04 21:35:34 +0000277 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000278
Chris Lattner886dd912005-04-04 21:35:34 +0000279 if (MO.isDef()) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000280 TII.storeRegToStackSlot(MBB, next(MII), PhysReg, true,
Evan Chengd64b5c82007-12-05 03:14:33 +0000281 StackSlot, RC);
Evan Chengd3653122008-02-27 03:04:06 +0000282 MachineInstr *StoreMI = next(MII);
283 VRM.addSpillSlotUse(StackSlot, StoreMI);
Chris Lattner886dd912005-04-04 21:35:34 +0000284 ++NumStores;
285 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000286 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000287 MF.getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000288 MI.getOperand(i).setReg(PhysReg);
Chris Lattner886dd912005-04-04 21:35:34 +0000289 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +0000290 MF.getRegInfo().setPhysRegUsed(MO.getReg());
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000291 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000292 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000293 }
Chris Lattner886dd912005-04-04 21:35:34 +0000294
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000295 DOUT << '\t' << MI;
Chris Lattner4ea1b822004-09-30 02:33:48 +0000296 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000297 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000298 }
299 return true;
300}
301
302//===----------------------------------------------------------------------===//
303// Local Spiller Implementation
304//===----------------------------------------------------------------------===//
305
306namespace {
Evan Cheng66f71632007-10-19 21:23:22 +0000307 class AvailableSpills;
308
Chris Lattner7fb64342004-10-01 19:04:51 +0000309 /// LocalSpiller - This spiller does a simple pass over the machine basic
310 /// block to attempt to keep spills in registers as much as possible for
311 /// blocks that have low register pressure (the vreg may be spilled due to
312 /// register pressure in other blocks).
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000313 class VISIBILITY_HIDDEN LocalSpiller : public Spiller {
Chris Lattner84bc5422007-12-31 04:13:23 +0000314 MachineRegisterInfo *RegInfo;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000315 const TargetRegisterInfo *TRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000316 const TargetInstrInfo *TII;
Evan Cheng7a0f1852008-05-20 08:13:21 +0000317 DenseMap<MachineInstr*, unsigned> DistanceMap;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000318 public:
Chris Lattner35f27052006-05-01 21:16:03 +0000319 bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000320 RegInfo = &MF.getRegInfo();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000321 TRI = MF.getTarget().getRegisterInfo();
Chris Lattner7fb64342004-10-01 19:04:51 +0000322 TII = MF.getTarget().getInstrInfo();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000323 DOUT << "\n**** Local spiller rewriting function '"
324 << MF.getFunction()->getName() << "':\n";
Chris Lattner84bc5422007-12-31 04:13:23 +0000325 DOUT << "**** Machine Instrs (NOTE! Does not include spills and reloads!)"
326 " ****\n";
David Greene04fa32f2007-09-06 16:36:39 +0000327 DEBUG(MF.dump());
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000328
Chris Lattner7fb64342004-10-01 19:04:51 +0000329 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
330 MBB != E; ++MBB)
Evan Cheng549f27d32007-08-13 23:45:17 +0000331 RewriteMBB(*MBB, VRM);
David Greene04fa32f2007-09-06 16:36:39 +0000332
Evan Chengd3653122008-02-27 03:04:06 +0000333 // Mark unused spill slots.
334 MachineFrameInfo *MFI = MF.getFrameInfo();
335 int SS = VRM.getLowSpillSlot();
336 if (SS != VirtRegMap::NO_STACK_SLOT)
337 for (int e = VRM.getHighSpillSlot(); SS <= e; ++SS)
338 if (!VRM.isSpillSlotUsed(SS)) {
339 MFI->RemoveStackObject(SS);
340 ++NumDSS;
341 }
342
David Greene04fa32f2007-09-06 16:36:39 +0000343 DOUT << "**** Post Machine Instrs ****\n";
344 DEBUG(MF.dump());
345
Chris Lattner7fb64342004-10-01 19:04:51 +0000346 return true;
347 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000348 private:
Evan Cheng7a0f1852008-05-20 08:13:21 +0000349 void TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist,
350 unsigned Reg, BitVector &RegKills,
351 std::vector<MachineOperand*> &KillOps);
Evan Cheng66f71632007-10-19 21:23:22 +0000352 bool PrepForUnfoldOpti(MachineBasicBlock &MBB,
353 MachineBasicBlock::iterator &MII,
354 std::vector<MachineInstr*> &MaybeDeadStores,
355 AvailableSpills &Spills, BitVector &RegKills,
356 std::vector<MachineOperand*> &KillOps,
357 VirtRegMap &VRM);
Evan Cheng81a03822007-11-17 00:40:40 +0000358 void SpillRegToStackSlot(MachineBasicBlock &MBB,
359 MachineBasicBlock::iterator &MII,
360 int Idx, unsigned PhysReg, int StackSlot,
361 const TargetRegisterClass *RC,
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000362 bool isAvailable, MachineInstr *&LastStore,
Evan Cheng81a03822007-11-17 00:40:40 +0000363 AvailableSpills &Spills,
364 SmallSet<MachineInstr*, 4> &ReMatDefs,
365 BitVector &RegKills,
366 std::vector<MachineOperand*> &KillOps,
Evan Chenge4b39002007-12-03 21:31:55 +0000367 VirtRegMap &VRM);
Evan Cheng549f27d32007-08-13 23:45:17 +0000368 void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000369 };
370}
371
Chris Lattner66cf80f2006-02-03 23:13:58 +0000372/// AvailableSpills - As the local spiller is scanning and rewriting an MBB from
Evan Cheng549f27d32007-08-13 23:45:17 +0000373/// top down, keep track of which spills slots or remat are available in each
374/// register.
Chris Lattner593c9582006-02-03 23:28:46 +0000375///
376/// Note that not all physregs are created equal here. In particular, some
377/// physregs are reloads that we are allowed to clobber or ignore at any time.
378/// Other physregs are values that the register allocated program is using that
379/// we cannot CHANGE, but we can read if we like. We keep track of this on a
Evan Cheng549f27d32007-08-13 23:45:17 +0000380/// per-stack-slot / remat id basis as the low bit in the value of the
381/// SpillSlotsAvailable entries. The predicate 'canClobberPhysReg()' checks
382/// this bit and addAvailable sets it if.
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000383namespace {
384class VISIBILITY_HIDDEN AvailableSpills {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000385 const TargetRegisterInfo *TRI;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000386 const TargetInstrInfo *TII;
387
Evan Cheng549f27d32007-08-13 23:45:17 +0000388 // SpillSlotsOrReMatsAvailable - This map keeps track of all of the spilled
389 // or remat'ed virtual register values that are still available, due to being
390 // loaded or stored to, but not invalidated yet.
391 std::map<int, unsigned> SpillSlotsOrReMatsAvailable;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000392
Evan Cheng549f27d32007-08-13 23:45:17 +0000393 // PhysRegsAvailable - This is the inverse of SpillSlotsOrReMatsAvailable,
394 // indicating which stack slot values are currently held by a physreg. This
395 // is used to invalidate entries in SpillSlotsOrReMatsAvailable when a
396 // physreg is modified.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000397 std::multimap<unsigned, int> PhysRegsAvailable;
398
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000399 void disallowClobberPhysRegOnly(unsigned PhysReg);
400
Chris Lattner66cf80f2006-02-03 23:13:58 +0000401 void ClobberPhysRegOnly(unsigned PhysReg);
402public:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000403 AvailableSpills(const TargetRegisterInfo *tri, const TargetInstrInfo *tii)
404 : TRI(tri), TII(tii) {
Chris Lattner66cf80f2006-02-03 23:13:58 +0000405 }
406
Dan Gohman6f0d0242008-02-10 18:45:23 +0000407 const TargetRegisterInfo *getRegInfo() const { return TRI; }
Evan Cheng91e23902007-02-23 01:13:26 +0000408
Evan Cheng549f27d32007-08-13 23:45:17 +0000409 /// getSpillSlotOrReMatPhysReg - If the specified stack slot or remat is
410 /// available in a physical register, return that PhysReg, otherwise
411 /// return 0.
412 unsigned getSpillSlotOrReMatPhysReg(int Slot) const {
413 std::map<int, unsigned>::const_iterator I =
414 SpillSlotsOrReMatsAvailable.find(Slot);
415 if (I != SpillSlotsOrReMatsAvailable.end()) {
Evan Chengb9591c62007-07-11 08:47:44 +0000416 return I->second >> 1; // Remove the CanClobber bit.
Evan Cheng91e23902007-02-23 01:13:26 +0000417 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000418 return 0;
419 }
Evan Chengde4e9422007-02-25 09:51:27 +0000420
Evan Cheng549f27d32007-08-13 23:45:17 +0000421 /// addAvailable - Mark that the specified stack slot / remat is available in
422 /// the specified physreg. If CanClobber is true, the physreg can be modified
423 /// at any time without changing the semantics of the program.
424 void addAvailable(int SlotOrReMat, MachineInstr *MI, unsigned Reg,
Evan Cheng91e23902007-02-23 01:13:26 +0000425 bool CanClobber = true) {
Chris Lattner86662492006-02-03 23:50:46 +0000426 // If this stack slot is thought to be available in some other physreg,
427 // remove its record.
Evan Cheng549f27d32007-08-13 23:45:17 +0000428 ModifyStackSlotOrReMat(SlotOrReMat);
Chris Lattner86662492006-02-03 23:50:46 +0000429
Evan Cheng549f27d32007-08-13 23:45:17 +0000430 PhysRegsAvailable.insert(std::make_pair(Reg, SlotOrReMat));
Evan Cheng90a43c32007-08-15 20:20:34 +0000431 SpillSlotsOrReMatsAvailable[SlotOrReMat]= (Reg << 1) | (unsigned)CanClobber;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000432
Evan Cheng549f27d32007-08-13 23:45:17 +0000433 if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
434 DOUT << "Remembering RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000435 else
Evan Cheng549f27d32007-08-13 23:45:17 +0000436 DOUT << "Remembering SS#" << SlotOrReMat;
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000437 DOUT << " in physreg " << TRI->getName(Reg) << "\n";
Chris Lattner66cf80f2006-02-03 23:13:58 +0000438 }
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000439
Chris Lattner593c9582006-02-03 23:28:46 +0000440 /// canClobberPhysReg - Return true if the spiller is allowed to change the
441 /// value of the specified stackslot register if it desires. The specified
442 /// stack slot must be available in a physreg for this query to make sense.
Evan Cheng549f27d32007-08-13 23:45:17 +0000443 bool canClobberPhysReg(int SlotOrReMat) const {
Evan Cheng90a43c32007-08-15 20:20:34 +0000444 assert(SpillSlotsOrReMatsAvailable.count(SlotOrReMat) &&
445 "Value not available!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000446 return SpillSlotsOrReMatsAvailable.find(SlotOrReMat)->second & 1;
Chris Lattner593c9582006-02-03 23:28:46 +0000447 }
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000448
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000449 /// disallowClobberPhysReg - Unset the CanClobber bit of the specified
450 /// stackslot register. The register is still available but is no longer
451 /// allowed to be modifed.
452 void disallowClobberPhysReg(unsigned PhysReg);
453
Chris Lattner66cf80f2006-02-03 23:13:58 +0000454 /// ClobberPhysReg - This is called when the specified physreg changes
Evan Cheng66f71632007-10-19 21:23:22 +0000455 /// value. We use this to invalidate any info about stuff that lives in
Chris Lattner66cf80f2006-02-03 23:13:58 +0000456 /// it and any of its aliases.
457 void ClobberPhysReg(unsigned PhysReg);
458
Evan Cheng90a43c32007-08-15 20:20:34 +0000459 /// ModifyStackSlotOrReMat - This method is called when the value in a stack
460 /// slot changes. This removes information about which register the previous
461 /// value for this slot lives in (as the previous value is dead now).
Evan Cheng549f27d32007-08-13 23:45:17 +0000462 void ModifyStackSlotOrReMat(int SlotOrReMat);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000463};
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000464}
Chris Lattner66cf80f2006-02-03 23:13:58 +0000465
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000466/// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified
467/// stackslot register. The register is still available but is no longer
468/// allowed to be modifed.
469void AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) {
470 std::multimap<unsigned, int>::iterator I =
471 PhysRegsAvailable.lower_bound(PhysReg);
472 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000473 int SlotOrReMat = I->second;
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000474 I++;
Evan Cheng549f27d32007-08-13 23:45:17 +0000475 assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000476 "Bidirectional map mismatch!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000477 SpillSlotsOrReMatsAvailable[SlotOrReMat] &= ~1;
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000478 DOUT << "PhysReg " << TRI->getName(PhysReg)
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000479 << " copied, it is available for use but can no longer be modified\n";
480 }
481}
482
483/// disallowClobberPhysReg - Unset the CanClobber bit of the specified
484/// stackslot register and its aliases. The register and its aliases may
485/// still available but is no longer allowed to be modifed.
486void AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000487 for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS)
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000488 disallowClobberPhysRegOnly(*AS);
489 disallowClobberPhysRegOnly(PhysReg);
490}
491
Chris Lattner66cf80f2006-02-03 23:13:58 +0000492/// ClobberPhysRegOnly - This is called when the specified physreg changes
493/// value. We use this to invalidate any info about stuff we thing lives in it.
494void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
495 std::multimap<unsigned, int>::iterator I =
496 PhysRegsAvailable.lower_bound(PhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000497 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000498 int SlotOrReMat = I->second;
Chris Lattner07cf1412006-02-03 00:36:31 +0000499 PhysRegsAvailable.erase(I++);
Evan Cheng549f27d32007-08-13 23:45:17 +0000500 assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
Chris Lattner66cf80f2006-02-03 23:13:58 +0000501 "Bidirectional map mismatch!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000502 SpillSlotsOrReMatsAvailable.erase(SlotOrReMat);
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000503 DOUT << "PhysReg " << TRI->getName(PhysReg)
Evan Cheng2638e1a2007-03-20 08:13:50 +0000504 << " clobbered, invalidating ";
Evan Cheng549f27d32007-08-13 23:45:17 +0000505 if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
506 DOUT << "RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1 << "\n";
Evan Cheng2638e1a2007-03-20 08:13:50 +0000507 else
Evan Cheng549f27d32007-08-13 23:45:17 +0000508 DOUT << "SS#" << SlotOrReMat << "\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000509 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000510}
511
Chris Lattner66cf80f2006-02-03 23:13:58 +0000512/// ClobberPhysReg - This is called when the specified physreg changes
513/// value. We use this to invalidate any info about stuff we thing lives in
514/// it and any of its aliases.
515void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000516 for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS)
Chris Lattner66cf80f2006-02-03 23:13:58 +0000517 ClobberPhysRegOnly(*AS);
518 ClobberPhysRegOnly(PhysReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000519}
520
Evan Cheng90a43c32007-08-15 20:20:34 +0000521/// ModifyStackSlotOrReMat - This method is called when the value in a stack
522/// slot changes. This removes information about which register the previous
523/// value for this slot lives in (as the previous value is dead now).
Evan Cheng549f27d32007-08-13 23:45:17 +0000524void AvailableSpills::ModifyStackSlotOrReMat(int SlotOrReMat) {
Evan Cheng90a43c32007-08-15 20:20:34 +0000525 std::map<int, unsigned>::iterator It =
526 SpillSlotsOrReMatsAvailable.find(SlotOrReMat);
Evan Cheng549f27d32007-08-13 23:45:17 +0000527 if (It == SpillSlotsOrReMatsAvailable.end()) return;
Evan Chengb9591c62007-07-11 08:47:44 +0000528 unsigned Reg = It->second >> 1;
Evan Cheng549f27d32007-08-13 23:45:17 +0000529 SpillSlotsOrReMatsAvailable.erase(It);
Chris Lattner07cf1412006-02-03 00:36:31 +0000530
531 // This register may hold the value of multiple stack slots, only remove this
532 // stack slot from the set of values the register contains.
533 std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
534 for (; ; ++I) {
535 assert(I != PhysRegsAvailable.end() && I->first == Reg &&
536 "Map inverse broken!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000537 if (I->second == SlotOrReMat) break;
Chris Lattner07cf1412006-02-03 00:36:31 +0000538 }
539 PhysRegsAvailable.erase(I);
540}
541
542
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000543
Evan Cheng28bb4622007-07-11 19:17:18 +0000544/// InvalidateKills - MI is going to be deleted. If any of its operands are
545/// marked kill, then invalidate the information.
546static void InvalidateKills(MachineInstr &MI, BitVector &RegKills,
Evan Chengc91f0b82007-08-14 20:23:13 +0000547 std::vector<MachineOperand*> &KillOps,
Evan Cheng66f71632007-10-19 21:23:22 +0000548 SmallVector<unsigned, 2> *KillRegs = NULL) {
Evan Cheng28bb4622007-07-11 19:17:18 +0000549 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
550 MachineOperand &MO = MI.getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000551 if (!MO.isRegister() || !MO.isUse() || !MO.isKill())
Evan Cheng28bb4622007-07-11 19:17:18 +0000552 continue;
553 unsigned Reg = MO.getReg();
Evan Chengb6ca4b32007-08-14 23:25:37 +0000554 if (KillRegs)
555 KillRegs->push_back(Reg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000556 if (KillOps[Reg] == &MO) {
557 RegKills.reset(Reg);
558 KillOps[Reg] = NULL;
559 }
560 }
561}
562
Evan Cheng39c883c2007-12-11 23:36:57 +0000563/// InvalidateKill - A MI that defines the specified register is being deleted,
564/// invalidate the register kill information.
565static void InvalidateKill(unsigned Reg, BitVector &RegKills,
566 std::vector<MachineOperand*> &KillOps) {
567 if (RegKills[Reg]) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000568 KillOps[Reg]->setIsKill(false);
Evan Cheng39c883c2007-12-11 23:36:57 +0000569 KillOps[Reg] = NULL;
570 RegKills.reset(Reg);
571 }
572}
573
Evan Chengb6ca4b32007-08-14 23:25:37 +0000574/// InvalidateRegDef - If the def operand of the specified def MI is now dead
575/// (since it's spill instruction is removed), mark it isDead. Also checks if
576/// the def MI has other definition operands that are not dead. Returns it by
577/// reference.
578static bool InvalidateRegDef(MachineBasicBlock::iterator I,
579 MachineInstr &NewDef, unsigned Reg,
580 bool &HasLiveDef) {
581 // Due to remat, it's possible this reg isn't being reused. That is,
582 // the def of this reg (by prev MI) is now dead.
583 MachineInstr *DefMI = I;
584 MachineOperand *DefOp = NULL;
585 for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) {
586 MachineOperand &MO = DefMI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000587 if (MO.isRegister() && MO.isDef()) {
Evan Chengb6ca4b32007-08-14 23:25:37 +0000588 if (MO.getReg() == Reg)
589 DefOp = &MO;
590 else if (!MO.isDead())
591 HasLiveDef = true;
592 }
593 }
594 if (!DefOp)
595 return false;
596
597 bool FoundUse = false, Done = false;
598 MachineBasicBlock::iterator E = NewDef;
599 ++I; ++E;
600 for (; !Done && I != E; ++I) {
601 MachineInstr *NMI = I;
602 for (unsigned j = 0, ee = NMI->getNumOperands(); j != ee; ++j) {
603 MachineOperand &MO = NMI->getOperand(j);
Dan Gohman92dfe202007-09-14 20:33:02 +0000604 if (!MO.isRegister() || MO.getReg() != Reg)
Evan Chengb6ca4b32007-08-14 23:25:37 +0000605 continue;
606 if (MO.isUse())
607 FoundUse = true;
608 Done = true; // Stop after scanning all the operands of this MI.
609 }
610 }
611 if (!FoundUse) {
612 // Def is dead!
613 DefOp->setIsDead();
614 return true;
615 }
616 return false;
617}
618
Evan Cheng28bb4622007-07-11 19:17:18 +0000619/// UpdateKills - Track and update kill info. If a MI reads a register that is
620/// marked kill, then it must be due to register reuse. Transfer the kill info
621/// over.
622static void UpdateKills(MachineInstr &MI, BitVector &RegKills,
623 std::vector<MachineOperand*> &KillOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000624 const TargetInstrDesc &TID = MI.getDesc();
Evan Cheng28bb4622007-07-11 19:17:18 +0000625 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
626 MachineOperand &MO = MI.getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000627 if (!MO.isRegister() || !MO.isUse())
Evan Cheng28bb4622007-07-11 19:17:18 +0000628 continue;
629 unsigned Reg = MO.getReg();
630 if (Reg == 0)
631 continue;
632
Evan Cheng70366b92008-03-21 19:09:30 +0000633 if (RegKills[Reg] && KillOps[Reg]->getParent() != &MI) {
Evan Cheng28bb4622007-07-11 19:17:18 +0000634 // That can't be right. Register is killed but not re-defined and it's
635 // being reused. Let's fix that.
Chris Lattnerf7382302007-12-30 21:56:09 +0000636 KillOps[Reg]->setIsKill(false);
Evan Cheng39c883c2007-12-11 23:36:57 +0000637 KillOps[Reg] = NULL;
638 RegKills.reset(Reg);
Chris Lattner749c6f62008-01-07 07:27:27 +0000639 if (i < TID.getNumOperands() &&
640 TID.getOperandConstraint(i, TOI::TIED_TO) == -1)
Evan Cheng28bb4622007-07-11 19:17:18 +0000641 // Unless it's a two-address operand, this is the new kill.
642 MO.setIsKill();
643 }
Evan Cheng28bb4622007-07-11 19:17:18 +0000644 if (MO.isKill()) {
645 RegKills.set(Reg);
646 KillOps[Reg] = &MO;
647 }
648 }
649
650 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
651 const MachineOperand &MO = MI.getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000652 if (!MO.isRegister() || !MO.isDef())
Evan Cheng28bb4622007-07-11 19:17:18 +0000653 continue;
654 unsigned Reg = MO.getReg();
655 RegKills.reset(Reg);
656 KillOps[Reg] = NULL;
657 }
658}
659
Evan Chengd70dbb52008-02-22 09:24:50 +0000660/// ReMaterialize - Re-materialize definition for Reg targetting DestReg.
661///
662static void ReMaterialize(MachineBasicBlock &MBB,
663 MachineBasicBlock::iterator &MII,
664 unsigned DestReg, unsigned Reg,
Evan Chengca1267c2008-03-31 20:40:39 +0000665 const TargetInstrInfo *TII,
Evan Chengd70dbb52008-02-22 09:24:50 +0000666 const TargetRegisterInfo *TRI,
667 VirtRegMap &VRM) {
Evan Chengca1267c2008-03-31 20:40:39 +0000668 TII->reMaterialize(MBB, MII, DestReg, VRM.getReMaterializedMI(Reg));
Evan Chengd70dbb52008-02-22 09:24:50 +0000669 MachineInstr *NewMI = prior(MII);
670 for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
671 MachineOperand &MO = NewMI->getOperand(i);
672 if (!MO.isRegister() || MO.getReg() == 0)
673 continue;
674 unsigned VirtReg = MO.getReg();
675 if (TargetRegisterInfo::isPhysicalRegister(VirtReg))
676 continue;
677 assert(MO.isUse());
678 unsigned SubIdx = MO.getSubReg();
679 unsigned Phys = VRM.getPhys(VirtReg);
680 assert(Phys);
681 unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
682 MO.setReg(RReg);
683 }
684 ++NumReMats;
685}
686
Evan Cheng28bb4622007-07-11 19:17:18 +0000687
Chris Lattner7fb64342004-10-01 19:04:51 +0000688// ReusedOp - For each reused operand, we keep track of a bit of information, in
689// case we need to rollback upon processing a new operand. See comments below.
690namespace {
691 struct ReusedOp {
692 // The MachineInstr operand that reused an available value.
693 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000694
Evan Cheng549f27d32007-08-13 23:45:17 +0000695 // StackSlotOrReMat - The spill slot or remat id of the value being reused.
696 unsigned StackSlotOrReMat;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000697
Chris Lattner7fb64342004-10-01 19:04:51 +0000698 // PhysRegReused - The physical register the value was available in.
699 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000700
Chris Lattner7fb64342004-10-01 19:04:51 +0000701 // AssignedPhysReg - The physreg that was assigned for use by the reload.
702 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000703
704 // VirtReg - The virtual register itself.
705 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000706
Chris Lattner8a61a752005-10-06 17:19:06 +0000707 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
708 unsigned vreg)
Evan Cheng90a43c32007-08-15 20:20:34 +0000709 : Operand(o), StackSlotOrReMat(ss), PhysRegReused(prr),
710 AssignedPhysReg(apr), VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000711 };
Chris Lattner540fec62006-02-25 01:51:33 +0000712
713 /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
714 /// is reused instead of reloaded.
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000715 class VISIBILITY_HIDDEN ReuseInfo {
Chris Lattner540fec62006-02-25 01:51:33 +0000716 MachineInstr &MI;
717 std::vector<ReusedOp> Reuses;
Evan Cheng957840b2007-02-21 02:22:03 +0000718 BitVector PhysRegsClobbered;
Chris Lattner540fec62006-02-25 01:51:33 +0000719 public:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000720 ReuseInfo(MachineInstr &mi, const TargetRegisterInfo *tri) : MI(mi) {
721 PhysRegsClobbered.resize(tri->getNumRegs());
Evan Chenge077ef62006-11-04 00:21:55 +0000722 }
Chris Lattner540fec62006-02-25 01:51:33 +0000723
724 bool hasReuses() const {
725 return !Reuses.empty();
726 }
727
728 /// addReuse - If we choose to reuse a virtual register that is already
729 /// available instead of reloading it, remember that we did so.
Evan Cheng549f27d32007-08-13 23:45:17 +0000730 void addReuse(unsigned OpNo, unsigned StackSlotOrReMat,
Chris Lattner540fec62006-02-25 01:51:33 +0000731 unsigned PhysRegReused, unsigned AssignedPhysReg,
732 unsigned VirtReg) {
733 // If the reload is to the assigned register anyway, no undo will be
734 // required.
735 if (PhysRegReused == AssignedPhysReg) return;
736
737 // Otherwise, remember this.
Evan Cheng549f27d32007-08-13 23:45:17 +0000738 Reuses.push_back(ReusedOp(OpNo, StackSlotOrReMat, PhysRegReused,
Chris Lattner540fec62006-02-25 01:51:33 +0000739 AssignedPhysReg, VirtReg));
740 }
Evan Chenge077ef62006-11-04 00:21:55 +0000741
742 void markClobbered(unsigned PhysReg) {
Evan Cheng957840b2007-02-21 02:22:03 +0000743 PhysRegsClobbered.set(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +0000744 }
745
746 bool isClobbered(unsigned PhysReg) const {
Evan Cheng957840b2007-02-21 02:22:03 +0000747 return PhysRegsClobbered.test(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +0000748 }
Chris Lattner540fec62006-02-25 01:51:33 +0000749
750 /// GetRegForReload - We are about to emit a reload into PhysReg. If there
751 /// is some other operand that is using the specified register, either pick
752 /// a new register to use, or evict the previous reload and use this reg.
753 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
754 AvailableSpills &Spills,
Evan Chengfff3e192007-08-14 09:11:18 +0000755 std::vector<MachineInstr*> &MaybeDeadStores,
Evan Cheng28bb4622007-07-11 19:17:18 +0000756 SmallSet<unsigned, 8> &Rejected,
757 BitVector &RegKills,
Evan Cheng549f27d32007-08-13 23:45:17 +0000758 std::vector<MachineOperand*> &KillOps,
759 VirtRegMap &VRM) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000760 const TargetInstrInfo* TII = MI->getParent()->getParent()->getTarget()
761 .getInstrInfo();
762
Chris Lattner540fec62006-02-25 01:51:33 +0000763 if (Reuses.empty()) return PhysReg; // This is most often empty.
764
765 for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
766 ReusedOp &Op = Reuses[ro];
767 // If we find some other reuse that was supposed to use this register
768 // exactly for its reload, we can change this reload to use ITS reload
Evan Cheng3c82cab2007-01-19 22:40:14 +0000769 // register. That is, unless its reload register has already been
770 // considered and subsequently rejected because it has also been reused
771 // by another operand.
772 if (Op.PhysRegReused == PhysReg &&
773 Rejected.count(Op.AssignedPhysReg) == 0) {
Chris Lattner540fec62006-02-25 01:51:33 +0000774 // Yup, use the reload register that we didn't use before.
Evan Cheng3c82cab2007-01-19 22:40:14 +0000775 unsigned NewReg = Op.AssignedPhysReg;
776 Rejected.insert(PhysReg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000777 return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores, Rejected,
Evan Cheng549f27d32007-08-13 23:45:17 +0000778 RegKills, KillOps, VRM);
Chris Lattner540fec62006-02-25 01:51:33 +0000779 } else {
780 // Otherwise, we might also have a problem if a previously reused
781 // value aliases the new register. If so, codegen the previous reload
782 // and use this one.
783 unsigned PRRU = Op.PhysRegReused;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000784 const TargetRegisterInfo *TRI = Spills.getRegInfo();
785 if (TRI->areAliases(PRRU, PhysReg)) {
Chris Lattner540fec62006-02-25 01:51:33 +0000786 // Okay, we found out that an alias of a reused register
787 // was used. This isn't good because it means we have
788 // to undo a previous reuse.
789 MachineBasicBlock *MBB = MI->getParent();
790 const TargetRegisterClass *AliasRC =
Chris Lattner84bc5422007-12-31 04:13:23 +0000791 MBB->getParent()->getRegInfo().getRegClass(Op.VirtReg);
Chris Lattner28bad082006-02-25 02:17:31 +0000792
793 // Copy Op out of the vector and remove it, we're going to insert an
794 // explicit load for it.
795 ReusedOp NewOp = Op;
796 Reuses.erase(Reuses.begin()+ro);
797
798 // Ok, we're going to try to reload the assigned physreg into the
799 // slot that we were supposed to in the first place. However, that
800 // register could hold a reuse. Check to see if it conflicts or
801 // would prefer us to use a different register.
802 unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg,
Evan Cheng28bb4622007-07-11 19:17:18 +0000803 MI, Spills, MaybeDeadStores,
Evan Cheng549f27d32007-08-13 23:45:17 +0000804 Rejected, RegKills, KillOps, VRM);
Chris Lattner28bad082006-02-25 02:17:31 +0000805
Evan Chengd70dbb52008-02-22 09:24:50 +0000806 MachineBasicBlock::iterator MII = MI;
Evan Cheng549f27d32007-08-13 23:45:17 +0000807 if (NewOp.StackSlotOrReMat > VirtRegMap::MAX_STACK_SLOT) {
Evan Chengca1267c2008-03-31 20:40:39 +0000808 ReMaterialize(*MBB, MII, NewPhysReg, NewOp.VirtReg, TII, TRI,VRM);
Evan Cheng549f27d32007-08-13 23:45:17 +0000809 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +0000810 TII->loadRegFromStackSlot(*MBB, MII, NewPhysReg,
Evan Cheng549f27d32007-08-13 23:45:17 +0000811 NewOp.StackSlotOrReMat, AliasRC);
Evan Chengd3653122008-02-27 03:04:06 +0000812 MachineInstr *LoadMI = prior(MII);
813 VRM.addSpillSlotUse(NewOp.StackSlotOrReMat, LoadMI);
Evan Chengfff3e192007-08-14 09:11:18 +0000814 // Any stores to this stack slot are not dead anymore.
815 MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL;
Evan Cheng549f27d32007-08-13 23:45:17 +0000816 ++NumLoads;
817 }
Chris Lattner28bad082006-02-25 02:17:31 +0000818 Spills.ClobberPhysReg(NewPhysReg);
819 Spills.ClobberPhysReg(NewOp.PhysRegReused);
Chris Lattner540fec62006-02-25 01:51:33 +0000820
Chris Lattnere53f4a02006-05-04 17:52:23 +0000821 MI->getOperand(NewOp.Operand).setReg(NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000822
Evan Cheng549f27d32007-08-13 23:45:17 +0000823 Spills.addAvailable(NewOp.StackSlotOrReMat, MI, NewPhysReg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000824 --MII;
825 UpdateKills(*MII, RegKills, KillOps);
826 DOUT << '\t' << *MII;
Chris Lattner540fec62006-02-25 01:51:33 +0000827
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000828 DOUT << "Reuse undone!\n";
Chris Lattner540fec62006-02-25 01:51:33 +0000829 --NumReused;
Chris Lattner28bad082006-02-25 02:17:31 +0000830
831 // Finally, PhysReg is now available, go ahead and use it.
Chris Lattner540fec62006-02-25 01:51:33 +0000832 return PhysReg;
833 }
834 }
835 }
836 return PhysReg;
837 }
Evan Cheng3c82cab2007-01-19 22:40:14 +0000838
839 /// GetRegForReload - Helper for the above GetRegForReload(). Add a
840 /// 'Rejected' set to remember which registers have been considered and
841 /// rejected for the reload. This avoids infinite looping in case like
842 /// this:
843 /// t1 := op t2, t3
844 /// t2 <- assigned r0 for use by the reload but ended up reuse r1
845 /// t3 <- assigned r1 for use by the reload but ended up reuse r0
846 /// t1 <- desires r1
847 /// sees r1 is taken by t2, tries t2's reload register r0
848 /// sees r0 is taken by t3, tries t3's reload register r1
849 /// sees r1 is taken by t2, tries t2's reload register r0 ...
850 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
851 AvailableSpills &Spills,
Evan Chengfff3e192007-08-14 09:11:18 +0000852 std::vector<MachineInstr*> &MaybeDeadStores,
Evan Cheng28bb4622007-07-11 19:17:18 +0000853 BitVector &RegKills,
Evan Cheng549f27d32007-08-13 23:45:17 +0000854 std::vector<MachineOperand*> &KillOps,
855 VirtRegMap &VRM) {
Chris Lattner08a4d5a2007-01-23 00:59:48 +0000856 SmallSet<unsigned, 8> Rejected;
Evan Cheng28bb4622007-07-11 19:17:18 +0000857 return GetRegForReload(PhysReg, MI, Spills, MaybeDeadStores, Rejected,
Evan Cheng549f27d32007-08-13 23:45:17 +0000858 RegKills, KillOps, VRM);
Evan Cheng3c82cab2007-01-19 22:40:14 +0000859 }
Chris Lattner540fec62006-02-25 01:51:33 +0000860 };
Chris Lattner7fb64342004-10-01 19:04:51 +0000861}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000862
Evan Cheng66f71632007-10-19 21:23:22 +0000863/// PrepForUnfoldOpti - Turn a store folding instruction into a load folding
864/// instruction. e.g.
865/// xorl %edi, %eax
866/// movl %eax, -32(%ebp)
867/// movl -36(%ebp), %eax
Bill Wendlingf059deb2008-02-26 10:51:52 +0000868/// orl %eax, -32(%ebp)
Evan Cheng66f71632007-10-19 21:23:22 +0000869/// ==>
870/// xorl %edi, %eax
871/// orl -36(%ebp), %eax
872/// mov %eax, -32(%ebp)
873/// This enables unfolding optimization for a subsequent instruction which will
874/// also eliminate the newly introduced store instruction.
875bool LocalSpiller::PrepForUnfoldOpti(MachineBasicBlock &MBB,
876 MachineBasicBlock::iterator &MII,
877 std::vector<MachineInstr*> &MaybeDeadStores,
878 AvailableSpills &Spills,
879 BitVector &RegKills,
880 std::vector<MachineOperand*> &KillOps,
881 VirtRegMap &VRM) {
882 MachineFunction &MF = *MBB.getParent();
883 MachineInstr &MI = *MII;
884 unsigned UnfoldedOpc = 0;
885 unsigned UnfoldPR = 0;
886 unsigned UnfoldVR = 0;
887 int FoldedSS = VirtRegMap::NO_STACK_SLOT;
888 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
Evan Chengc17ba8a2008-03-14 20:44:01 +0000889 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) {
Evan Cheng66f71632007-10-19 21:23:22 +0000890 // Only transform a MI that folds a single register.
891 if (UnfoldedOpc)
892 return false;
893 UnfoldVR = I->second.first;
894 VirtRegMap::ModRef MR = I->second.second;
Evan Chengc17ba8a2008-03-14 20:44:01 +0000895 // MI2VirtMap be can updated which invalidate the iterator.
896 // Increment the iterator first.
897 ++I;
Evan Cheng66f71632007-10-19 21:23:22 +0000898 if (VRM.isAssignedReg(UnfoldVR))
899 continue;
900 // If this reference is not a use, any previous store is now dead.
901 // Otherwise, the store to this stack slot is not dead anymore.
902 FoldedSS = VRM.getStackSlot(UnfoldVR);
903 MachineInstr* DeadStore = MaybeDeadStores[FoldedSS];
904 if (DeadStore && (MR & VirtRegMap::isModRef)) {
905 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(FoldedSS);
Evan Cheng6130f662008-03-05 00:59:57 +0000906 if (!PhysReg || !DeadStore->readsRegister(PhysReg))
Evan Cheng66f71632007-10-19 21:23:22 +0000907 continue;
908 UnfoldPR = PhysReg;
Owen Anderson6425f8b2008-01-07 01:35:56 +0000909 UnfoldedOpc = TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(),
Evan Cheng66f71632007-10-19 21:23:22 +0000910 false, true);
911 }
912 }
913
914 if (!UnfoldedOpc)
915 return false;
916
917 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
918 MachineOperand &MO = MI.getOperand(i);
919 if (!MO.isRegister() || MO.getReg() == 0 || !MO.isUse())
920 continue;
921 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000922 if (TargetRegisterInfo::isPhysicalRegister(VirtReg) || MO.getSubReg())
Evan Cheng66f71632007-10-19 21:23:22 +0000923 continue;
924 if (VRM.isAssignedReg(VirtReg)) {
925 unsigned PhysReg = VRM.getPhys(VirtReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000926 if (PhysReg && TRI->regsOverlap(PhysReg, UnfoldPR))
Evan Cheng66f71632007-10-19 21:23:22 +0000927 return false;
928 } else if (VRM.isReMaterialized(VirtReg))
929 continue;
930 int SS = VRM.getStackSlot(VirtReg);
931 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
932 if (PhysReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000933 if (TRI->regsOverlap(PhysReg, UnfoldPR))
Evan Cheng66f71632007-10-19 21:23:22 +0000934 return false;
935 continue;
936 }
937 PhysReg = VRM.getPhys(VirtReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000938 if (!TRI->regsOverlap(PhysReg, UnfoldPR))
Evan Cheng66f71632007-10-19 21:23:22 +0000939 continue;
940
941 // Ok, we'll need to reload the value into a register which makes
942 // it impossible to perform the store unfolding optimization later.
943 // Let's see if it is possible to fold the load if the store is
944 // unfolded. This allows us to perform the store unfolding
945 // optimization.
946 SmallVector<MachineInstr*, 4> NewMIs;
Owen Anderson6425f8b2008-01-07 01:35:56 +0000947 if (TII->unfoldMemoryOperand(MF, &MI, UnfoldVR, false, false, NewMIs)) {
Evan Cheng66f71632007-10-19 21:23:22 +0000948 assert(NewMIs.size() == 1);
949 MachineInstr *NewMI = NewMIs.back();
950 NewMIs.clear();
Evan Cheng6130f662008-03-05 00:59:57 +0000951 int Idx = NewMI->findRegisterUseOperandIdx(VirtReg, false);
Evan Cheng81a03822007-11-17 00:40:40 +0000952 assert(Idx != -1);
Evan Chengaee4af62007-12-02 08:30:39 +0000953 SmallVector<unsigned, 2> Ops;
954 Ops.push_back(Idx);
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000955 MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, NewMI, Ops, SS);
Evan Cheng66f71632007-10-19 21:23:22 +0000956 if (FoldedMI) {
Evan Cheng21b3f312008-02-27 19:57:11 +0000957 VRM.addSpillSlotUse(SS, FoldedMI);
Evan Chengcbfb9b22007-10-22 03:01:44 +0000958 if (!VRM.hasPhys(UnfoldVR))
Evan Cheng66f71632007-10-19 21:23:22 +0000959 VRM.assignVirt2Phys(UnfoldVR, UnfoldPR);
Evan Cheng66f71632007-10-19 21:23:22 +0000960 VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
961 MII = MBB.insert(MII, FoldedMI);
Evan Cheng7a0f1852008-05-20 08:13:21 +0000962 InvalidateKills(MI, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +0000963 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng66f71632007-10-19 21:23:22 +0000964 MBB.erase(&MI);
965 return true;
966 }
967 delete NewMI;
968 }
969 }
970 return false;
971}
Chris Lattner7fb64342004-10-01 19:04:51 +0000972
Evan Cheng7277a7d2007-11-02 17:35:08 +0000973/// findSuperReg - Find the SubReg's super-register of given register class
974/// where its SubIdx sub-register is SubReg.
975static unsigned findSuperReg(const TargetRegisterClass *RC, unsigned SubReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +0000976 unsigned SubIdx, const TargetRegisterInfo *TRI) {
Evan Cheng7277a7d2007-11-02 17:35:08 +0000977 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
978 I != E; ++I) {
979 unsigned Reg = *I;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000980 if (TRI->getSubReg(Reg, SubIdx) == SubReg)
Evan Cheng7277a7d2007-11-02 17:35:08 +0000981 return Reg;
982 }
983 return 0;
984}
985
Evan Cheng81a03822007-11-17 00:40:40 +0000986/// SpillRegToStackSlot - Spill a register to a specified stack slot. Check if
987/// the last store to the same slot is now dead. If so, remove the last store.
988void LocalSpiller::SpillRegToStackSlot(MachineBasicBlock &MBB,
989 MachineBasicBlock::iterator &MII,
990 int Idx, unsigned PhysReg, int StackSlot,
991 const TargetRegisterClass *RC,
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000992 bool isAvailable, MachineInstr *&LastStore,
Evan Cheng81a03822007-11-17 00:40:40 +0000993 AvailableSpills &Spills,
994 SmallSet<MachineInstr*, 4> &ReMatDefs,
995 BitVector &RegKills,
996 std::vector<MachineOperand*> &KillOps,
Evan Chenge4b39002007-12-03 21:31:55 +0000997 VirtRegMap &VRM) {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000998 TII->storeRegToStackSlot(MBB, next(MII), PhysReg, true, StackSlot, RC);
Evan Chengd3653122008-02-27 03:04:06 +0000999 MachineInstr *StoreMI = next(MII);
1000 VRM.addSpillSlotUse(StackSlot, StoreMI);
1001 DOUT << "Store:\t" << *StoreMI;
Evan Cheng81a03822007-11-17 00:40:40 +00001002
1003 // If there is a dead store to this stack slot, nuke it now.
1004 if (LastStore) {
1005 DOUT << "Removed dead store:\t" << *LastStore;
1006 ++NumDSE;
1007 SmallVector<unsigned, 2> KillRegs;
1008 InvalidateKills(*LastStore, RegKills, KillOps, &KillRegs);
1009 MachineBasicBlock::iterator PrevMII = LastStore;
1010 bool CheckDef = PrevMII != MBB.begin();
1011 if (CheckDef)
1012 --PrevMII;
Evan Chengcada2452007-11-28 01:28:46 +00001013 VRM.RemoveMachineInstrFromMaps(LastStore);
Evan Chengd3653122008-02-27 03:04:06 +00001014 MBB.erase(LastStore);
Evan Cheng81a03822007-11-17 00:40:40 +00001015 if (CheckDef) {
1016 // Look at defs of killed registers on the store. Mark the defs
1017 // as dead since the store has been deleted and they aren't
1018 // being reused.
1019 for (unsigned j = 0, ee = KillRegs.size(); j != ee; ++j) {
1020 bool HasOtherDef = false;
1021 if (InvalidateRegDef(PrevMII, *MII, KillRegs[j], HasOtherDef)) {
1022 MachineInstr *DeadDef = PrevMII;
1023 if (ReMatDefs.count(DeadDef) && !HasOtherDef) {
1024 // FIXME: This assumes a remat def does not have side
1025 // effects.
Evan Chengcada2452007-11-28 01:28:46 +00001026 VRM.RemoveMachineInstrFromMaps(DeadDef);
Evan Chengd3653122008-02-27 03:04:06 +00001027 MBB.erase(DeadDef);
Evan Cheng81a03822007-11-17 00:40:40 +00001028 ++NumDRM;
1029 }
1030 }
1031 }
1032 }
1033 }
1034
Evan Chenge4b39002007-12-03 21:31:55 +00001035 LastStore = next(MII);
Evan Cheng81a03822007-11-17 00:40:40 +00001036
1037 // If the stack slot value was previously available in some other
1038 // register, change it now. Otherwise, make the register available,
1039 // in PhysReg.
1040 Spills.ModifyStackSlotOrReMat(StackSlot);
1041 Spills.ClobberPhysReg(PhysReg);
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001042 Spills.addAvailable(StackSlot, LastStore, PhysReg, isAvailable);
Evan Cheng81a03822007-11-17 00:40:40 +00001043 ++NumStores;
1044}
1045
Evan Cheng7a0f1852008-05-20 08:13:21 +00001046/// TransferDeadness - A identity copy definition is dead and it's being
1047/// removed. Find the last def or use and mark it as dead / kill.
1048void LocalSpiller::TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist,
1049 unsigned Reg, BitVector &RegKills,
1050 std::vector<MachineOperand*> &KillOps) {
1051 int LastUDDist = -1;
1052 MachineInstr *LastUDMI = NULL;
1053 for (MachineRegisterInfo::reg_iterator RI = RegInfo->reg_begin(Reg),
1054 RE = RegInfo->reg_end(); RI != RE; ++RI) {
1055 MachineInstr *UDMI = &*RI;
1056 if (UDMI->getParent() != MBB)
1057 continue;
1058 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI);
1059 if (DI == DistanceMap.end() || DI->second > CurDist)
1060 continue;
1061 if ((int)DI->second < LastUDDist)
1062 continue;
1063 LastUDDist = DI->second;
1064 LastUDMI = UDMI;
1065 }
1066
1067 if (LastUDMI) {
1068 const TargetInstrDesc &TID = LastUDMI->getDesc();
1069 MachineOperand *LastUD = NULL;
1070 for (unsigned i = 0, e = LastUDMI->getNumOperands(); i != e; ++i) {
1071 MachineOperand &MO = LastUDMI->getOperand(i);
1072 if (!MO.isRegister() || MO.getReg() != Reg)
1073 continue;
1074 if (!LastUD || (LastUD->isUse() && MO.isDef()))
1075 LastUD = &MO;
1076 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1)
1077 return;
1078 }
1079 if (LastUD->isDef())
1080 LastUD->setIsDead();
1081 else {
1082 LastUD->setIsKill();
1083 RegKills.set(Reg);
1084 KillOps[Reg] = LastUD;
1085 }
1086 }
1087}
1088
Chris Lattner7fb64342004-10-01 19:04:51 +00001089/// rewriteMBB - Keep track of which spills are available even after the
Evan Cheng81a03822007-11-17 00:40:40 +00001090/// register allocator is done with them. If possible, avid reloading vregs.
Evan Cheng549f27d32007-08-13 23:45:17 +00001091void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001092 DOUT << MBB.getBasicBlock()->getName() << ":\n";
Chris Lattner7fb64342004-10-01 19:04:51 +00001093
Evan Chengfff3e192007-08-14 09:11:18 +00001094 MachineFunction &MF = *MBB.getParent();
Owen Andersond10fd972007-12-31 06:32:00 +00001095
Chris Lattner66cf80f2006-02-03 23:13:58 +00001096 // Spills - Keep track of which spilled values are available in physregs so
1097 // that we can choose to reuse the physregs instead of emitting reloads.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001098 AvailableSpills Spills(TRI, TII);
Chris Lattner66cf80f2006-02-03 23:13:58 +00001099
Chris Lattner52b25db2004-10-01 19:47:12 +00001100 // MaybeDeadStores - When we need to write a value back into a stack slot,
1101 // keep track of the inserted store. If the stack slot value is never read
1102 // (because the value was used from some available register, for example), and
1103 // subsequently stored to, the original store is dead. This map keeps track
1104 // of inserted stores that are not used. If we see a subsequent store to the
1105 // same stack slot, the original store is deleted.
Evan Chengfff3e192007-08-14 09:11:18 +00001106 std::vector<MachineInstr*> MaybeDeadStores;
1107 MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL);
Chris Lattner52b25db2004-10-01 19:47:12 +00001108
Evan Chengb6ca4b32007-08-14 23:25:37 +00001109 // ReMatDefs - These are rematerializable def MIs which are not deleted.
1110 SmallSet<MachineInstr*, 4> ReMatDefs;
1111
Evan Cheng0c40d722007-07-11 05:28:39 +00001112 // Keep track of kill information.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001113 BitVector RegKills(TRI->getNumRegs());
Evan Cheng0c40d722007-07-11 05:28:39 +00001114 std::vector<MachineOperand*> KillOps;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001115 KillOps.resize(TRI->getNumRegs(), NULL);
Evan Cheng0c40d722007-07-11 05:28:39 +00001116
Evan Cheng7a0f1852008-05-20 08:13:21 +00001117 unsigned Dist = 0;
1118 DistanceMap.clear();
Chris Lattner7fb64342004-10-01 19:04:51 +00001119 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
1120 MII != E; ) {
Chris Lattner7fb64342004-10-01 19:04:51 +00001121 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
Evan Cheng0c40d722007-07-11 05:28:39 +00001122
Evan Cheng66f71632007-10-19 21:23:22 +00001123 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
Evan Cheng0c40d722007-07-11 05:28:39 +00001124 bool Erased = false;
1125 bool BackTracked = false;
Evan Cheng66f71632007-10-19 21:23:22 +00001126 if (PrepForUnfoldOpti(MBB, MII,
1127 MaybeDeadStores, Spills, RegKills, KillOps, VRM))
1128 NextMII = next(MII);
Chris Lattner7fb64342004-10-01 19:04:51 +00001129
Evan Cheng66f71632007-10-19 21:23:22 +00001130 MachineInstr &MI = *MII;
Chris Lattner749c6f62008-01-07 07:27:27 +00001131 const TargetInstrDesc &TID = MI.getDesc();
Evan Chenge077ef62006-11-04 00:21:55 +00001132
Evan Cheng676dd7c2008-03-11 07:19:34 +00001133 if (VRM.hasEmergencySpills(&MI)) {
1134 // Spill physical register(s) in the rare case the allocator has run out
1135 // of registers to allocate.
1136 SmallSet<int, 4> UsedSS;
1137 std::vector<unsigned> &EmSpills = VRM.getEmergencySpills(&MI);
1138 for (unsigned i = 0, e = EmSpills.size(); i != e; ++i) {
1139 unsigned PhysReg = EmSpills[i];
1140 const TargetRegisterClass *RC =
1141 TRI->getPhysicalRegisterRegClass(PhysReg);
1142 assert(RC && "Unable to determine register class!");
1143 int SS = VRM.getEmergencySpillSlot(RC);
1144 if (UsedSS.count(SS))
1145 assert(0 && "Need to spill more than one physical registers!");
1146 UsedSS.insert(SS);
1147 TII->storeRegToStackSlot(MBB, MII, PhysReg, true, SS, RC);
1148 MachineInstr *StoreMI = prior(MII);
1149 VRM.addSpillSlotUse(SS, StoreMI);
1150 TII->loadRegFromStackSlot(MBB, next(MII), PhysReg, SS, RC);
1151 MachineInstr *LoadMI = next(MII);
1152 VRM.addSpillSlotUse(SS, LoadMI);
Evan Chengc1f53c72008-03-11 21:34:46 +00001153 ++NumPSpills;
Evan Cheng676dd7c2008-03-11 07:19:34 +00001154 }
Evan Cheng17d5f542008-03-12 00:14:07 +00001155 NextMII = next(MII);
Evan Cheng676dd7c2008-03-11 07:19:34 +00001156 }
1157
Evan Cheng0cbb1162007-11-29 01:06:25 +00001158 // Insert restores here if asked to.
1159 if (VRM.isRestorePt(&MI)) {
1160 std::vector<unsigned> &RestoreRegs = VRM.getRestorePtRestores(&MI);
1161 for (unsigned i = 0, e = RestoreRegs.size(); i != e; ++i) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001162 unsigned VirtReg = RestoreRegs[e-i-1]; // Reverse order.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001163 if (!VRM.getPreSplitReg(VirtReg))
1164 continue; // Split interval spilled again.
1165 unsigned Phys = VRM.getPhys(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001166 RegInfo->setPhysRegUsed(Phys);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001167 if (VRM.isReMaterialized(VirtReg)) {
Evan Chengca1267c2008-03-31 20:40:39 +00001168 ReMaterialize(MBB, MII, Phys, VirtReg, TII, TRI, VRM);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001169 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +00001170 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Evan Chengd3653122008-02-27 03:04:06 +00001171 int SS = VRM.getStackSlot(VirtReg);
1172 TII->loadRegFromStackSlot(MBB, &MI, Phys, SS, RC);
1173 MachineInstr *LoadMI = prior(MII);
1174 VRM.addSpillSlotUse(SS, LoadMI);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001175 ++NumLoads;
1176 }
1177 // This invalidates Phys.
1178 Spills.ClobberPhysReg(Phys);
1179 UpdateKills(*prior(MII), RegKills, KillOps);
1180 DOUT << '\t' << *prior(MII);
1181 }
1182 }
1183
Evan Cheng81a03822007-11-17 00:40:40 +00001184 // Insert spills here if asked to.
Evan Chengcada2452007-11-28 01:28:46 +00001185 if (VRM.isSpillPt(&MI)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001186 std::vector<std::pair<unsigned,bool> > &SpillRegs =
1187 VRM.getSpillPtSpills(&MI);
Evan Chengcada2452007-11-28 01:28:46 +00001188 for (unsigned i = 0, e = SpillRegs.size(); i != e; ++i) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001189 unsigned VirtReg = SpillRegs[i].first;
1190 bool isKill = SpillRegs[i].second;
Evan Chengcada2452007-11-28 01:28:46 +00001191 if (!VRM.getPreSplitReg(VirtReg))
1192 continue; // Split interval spilled again.
Chris Lattner84bc5422007-12-31 04:13:23 +00001193 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Evan Chengcada2452007-11-28 01:28:46 +00001194 unsigned Phys = VRM.getPhys(VirtReg);
1195 int StackSlot = VRM.getStackSlot(VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001196 TII->storeRegToStackSlot(MBB, next(MII), Phys, isKill, StackSlot, RC);
Evan Chengd64b5c82007-12-05 03:14:33 +00001197 MachineInstr *StoreMI = next(MII);
Evan Chengd3653122008-02-27 03:04:06 +00001198 VRM.addSpillSlotUse(StackSlot, StoreMI);
Evan Cheng4191b962008-03-12 00:02:46 +00001199 DOUT << "Store:\t" << *StoreMI;
Evan Chengd64b5c82007-12-05 03:14:33 +00001200 VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod);
Evan Chengcada2452007-11-28 01:28:46 +00001201 }
Evan Chenge4b39002007-12-03 21:31:55 +00001202 NextMII = next(MII);
Evan Cheng81a03822007-11-17 00:40:40 +00001203 }
1204
1205 /// ReusedOperands - Keep track of operand reuse in case we need to undo
1206 /// reuse.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001207 ReuseInfo ReusedOperands(MI, TRI);
Evan Chengb2fd65f2008-02-22 19:22:06 +00001208 SmallVector<unsigned, 4> VirtUseOps;
Chris Lattner7fb64342004-10-01 19:04:51 +00001209 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1210 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001211 if (!MO.isRegister() || MO.getReg() == 0)
1212 continue; // Ignore non-register operands.
1213
Evan Cheng32dfbea2007-10-12 08:50:34 +00001214 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001215 if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) {
Chris Lattner50ea01e2005-09-09 20:29:51 +00001216 // Ignore physregs for spilling, but remember that it is used by this
1217 // function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001218 RegInfo->setPhysRegUsed(VirtReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001219 continue;
1220 }
Evan Chengb2fd65f2008-02-22 19:22:06 +00001221
1222 // We want to process implicit virtual register uses first.
1223 if (MO.isImplicit())
Evan Cheng4cce6b42008-04-11 17:53:36 +00001224 // If the virtual register is implicitly defined, emit a implicit_def
1225 // before so scavenger knows it's "defined".
Evan Chengb2fd65f2008-02-22 19:22:06 +00001226 VirtUseOps.insert(VirtUseOps.begin(), i);
1227 else
1228 VirtUseOps.push_back(i);
1229 }
1230
1231 // Process all of the spilled uses and all non spilled reg references.
1232 for (unsigned j = 0, e = VirtUseOps.size(); j != e; ++j) {
1233 unsigned i = VirtUseOps[j];
1234 MachineOperand &MO = MI.getOperand(i);
1235 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001236 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
Evan Chengb2fd65f2008-02-22 19:22:06 +00001237 "Not a virtual register?");
Evan Cheng70306f82007-12-03 09:58:48 +00001238
Evan Chengc498b022007-11-14 07:59:08 +00001239 unsigned SubIdx = MO.getSubReg();
Evan Cheng549f27d32007-08-13 23:45:17 +00001240 if (VRM.isAssignedReg(VirtReg)) {
Chris Lattner50ea01e2005-09-09 20:29:51 +00001241 // This virtual register was assigned a physreg!
1242 unsigned Phys = VRM.getPhys(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001243 RegInfo->setPhysRegUsed(Phys);
Evan Chenge077ef62006-11-04 00:21:55 +00001244 if (MO.isDef())
1245 ReusedOperands.markClobbered(Phys);
Dan Gohman6f0d0242008-02-10 18:45:23 +00001246 unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001247 MI.getOperand(i).setReg(RReg);
Evan Cheng4cce6b42008-04-11 17:53:36 +00001248 if (VRM.isImplicitlyDefined(VirtReg))
1249 BuildMI(MBB, MI, TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001250 continue;
1251 }
1252
1253 // This virtual register is now known to be a spilled value.
1254 if (!MO.isUse())
1255 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +00001256
Evan Cheng549f27d32007-08-13 23:45:17 +00001257 bool DoReMat = VRM.isReMaterialized(VirtReg);
1258 int SSorRMId = DoReMat
1259 ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg);
Evan Chengdc6be192007-08-14 05:42:54 +00001260 int ReuseSlot = SSorRMId;
Chris Lattner7fb64342004-10-01 19:04:51 +00001261
Chris Lattner50ea01e2005-09-09 20:29:51 +00001262 // Check to see if this stack slot is available.
Evan Chengdc6be192007-08-14 05:42:54 +00001263 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001264
1265 // If this is a sub-register use, make sure the reuse register is in the
1266 // right register class. For example, for x86 not all of the 32-bit
1267 // registers have accessible sub-registers.
1268 // Similarly so for EXTRACT_SUBREG. Consider this:
1269 // EDI = op
1270 // MOV32_mr fi#1, EDI
1271 // ...
1272 // = EXTRACT_SUBREG fi#1
1273 // fi#1 is available in EDI, but it cannot be reused because it's not in
1274 // the right register file.
1275 if (PhysReg &&
Evan Chengc498b022007-11-14 07:59:08 +00001276 (SubIdx || MI.getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001277 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001278 if (!RC->contains(PhysReg))
1279 PhysReg = 0;
1280 }
1281
Evan Chengdc6be192007-08-14 05:42:54 +00001282 if (PhysReg) {
Chris Lattner29268692006-09-05 02:12:02 +00001283 // This spilled operand might be part of a two-address operand. If this
1284 // is the case, then changing it will necessarily require changing the
1285 // def part of the instruction as well. However, in some cases, we
1286 // aren't allowed to modify the reused register. If none of these cases
1287 // apply, reuse it.
1288 bool CanReuse = true;
Chris Lattner749c6f62008-01-07 07:27:27 +00001289 int ti = TID.getOperandConstraint(i, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +00001290 if (ti != -1 &&
Dan Gohman92dfe202007-09-14 20:33:02 +00001291 MI.getOperand(ti).isRegister() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +00001292 MI.getOperand(ti).getReg() == VirtReg) {
Chris Lattner29268692006-09-05 02:12:02 +00001293 // Okay, we have a two address operand. We can reuse this physreg as
Evan Cheng3c82cab2007-01-19 22:40:14 +00001294 // long as we are allowed to clobber the value and there isn't an
1295 // earlier def that has already clobbered the physreg.
Evan Chengdc6be192007-08-14 05:42:54 +00001296 CanReuse = Spills.canClobberPhysReg(ReuseSlot) &&
Evan Chenge077ef62006-11-04 00:21:55 +00001297 !ReusedOperands.isClobbered(PhysReg);
Chris Lattner29268692006-09-05 02:12:02 +00001298 }
1299
1300 if (CanReuse) {
Chris Lattneraddc55a2006-04-28 01:46:50 +00001301 // If this stack slot value is already available, reuse it!
Evan Chengdc6be192007-08-14 05:42:54 +00001302 if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1303 DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001304 else
Evan Chengdc6be192007-08-14 05:42:54 +00001305 DOUT << "Reusing SS#" << ReuseSlot;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001306 DOUT << " from physreg "
Bill Wendlinge6d088a2008-02-26 21:47:57 +00001307 << TRI->getName(PhysReg) << " for vreg"
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001308 << VirtReg <<" instead of reloading into physreg "
Bill Wendlinge6d088a2008-02-26 21:47:57 +00001309 << TRI->getName(VRM.getPhys(VirtReg)) << "\n";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001310 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001311 MI.getOperand(i).setReg(RReg);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001312
1313 // The only technical detail we have is that we don't know that
1314 // PhysReg won't be clobbered by a reloaded stack slot that occurs
1315 // later in the instruction. In particular, consider 'op V1, V2'.
1316 // If V1 is available in physreg R0, we would choose to reuse it
1317 // here, instead of reloading it into the register the allocator
1318 // indicated (say R1). However, V2 might have to be reloaded
1319 // later, and it might indicate that it needs to live in R0. When
1320 // this occurs, we need to have information available that
1321 // indicates it is safe to use R1 for the reload instead of R0.
1322 //
1323 // To further complicate matters, we might conflict with an alias,
1324 // or R0 and R1 might not be compatible with each other. In this
1325 // case, we actually insert a reload for V1 in R1, ensuring that
1326 // we can get at R0 or its alias.
Evan Chengdc6be192007-08-14 05:42:54 +00001327 ReusedOperands.addReuse(i, ReuseSlot, PhysReg,
Chris Lattneraddc55a2006-04-28 01:46:50 +00001328 VRM.getPhys(VirtReg), VirtReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001329 if (ti != -1)
1330 // Only mark it clobbered if this is a use&def operand.
1331 ReusedOperands.markClobbered(PhysReg);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001332 ++NumReused;
Evan Chengfff3e192007-08-14 09:11:18 +00001333
1334 if (MI.getOperand(i).isKill() &&
1335 ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) {
1336 // This was the last use and the spilled value is still available
1337 // for reuse. That means the spill was unnecessary!
1338 MachineInstr* DeadStore = MaybeDeadStores[ReuseSlot];
1339 if (DeadStore) {
1340 DOUT << "Removed dead store:\t" << *DeadStore;
1341 InvalidateKills(*DeadStore, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001342 VRM.RemoveMachineInstrFromMaps(DeadStore);
Evan Cheng66f71632007-10-19 21:23:22 +00001343 MBB.erase(DeadStore);
Evan Chengfff3e192007-08-14 09:11:18 +00001344 MaybeDeadStores[ReuseSlot] = NULL;
1345 ++NumDSE;
1346 }
1347 }
Chris Lattneraddc55a2006-04-28 01:46:50 +00001348 continue;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001349 } // CanReuse
Chris Lattneraddc55a2006-04-28 01:46:50 +00001350
1351 // Otherwise we have a situation where we have a two-address instruction
1352 // whose mod/ref operand needs to be reloaded. This reload is already
1353 // available in some register "PhysReg", but if we used PhysReg as the
1354 // operand to our 2-addr instruction, the instruction would modify
1355 // PhysReg. This isn't cool if something later uses PhysReg and expects
1356 // to get its initial value.
Chris Lattner50ea01e2005-09-09 20:29:51 +00001357 //
Chris Lattneraddc55a2006-04-28 01:46:50 +00001358 // To avoid this problem, and to avoid doing a load right after a store,
1359 // we emit a copy from PhysReg into the designated register for this
1360 // operand.
1361 unsigned DesignatedReg = VRM.getPhys(VirtReg);
1362 assert(DesignatedReg && "Must map virtreg to physreg!");
1363
1364 // Note that, if we reused a register for a previous operand, the
1365 // register we want to reload into might not actually be
1366 // available. If this occurs, use the register indicated by the
1367 // reuser.
1368 if (ReusedOperands.hasReuses())
1369 DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI,
Evan Cheng549f27d32007-08-13 23:45:17 +00001370 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001371
Chris Lattnerba1fc3d2006-04-28 04:43:18 +00001372 // If the mapped designated register is actually the physreg we have
1373 // incoming, we don't need to inserted a dead copy.
1374 if (DesignatedReg == PhysReg) {
1375 // If this stack slot value is already available, reuse it!
Evan Chengdc6be192007-08-14 05:42:54 +00001376 if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1377 DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001378 else
Evan Chengdc6be192007-08-14 05:42:54 +00001379 DOUT << "Reusing SS#" << ReuseSlot;
Bill Wendlinge6d088a2008-02-26 21:47:57 +00001380 DOUT << " from physreg " << TRI->getName(PhysReg)
Bill Wendling6ef781f2008-02-27 06:33:05 +00001381 << " for vreg" << VirtReg
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001382 << " instead of reloading into same physreg.\n";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001383 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001384 MI.getOperand(i).setReg(RReg);
Evan Cheng7277a7d2007-11-02 17:35:08 +00001385 ReusedOperands.markClobbered(RReg);
Chris Lattnerba1fc3d2006-04-28 04:43:18 +00001386 ++NumReused;
1387 continue;
1388 }
1389
Chris Lattner84bc5422007-12-31 04:13:23 +00001390 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1391 RegInfo->setPhysRegUsed(DesignatedReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001392 ReusedOperands.markClobbered(DesignatedReg);
Owen Andersond10fd972007-12-31 06:32:00 +00001393 TII->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC, RC);
Evan Chengde4e9422007-02-25 09:51:27 +00001394
Evan Cheng6b448092007-03-02 08:52:00 +00001395 MachineInstr *CopyMI = prior(MII);
Evan Cheng0c40d722007-07-11 05:28:39 +00001396 UpdateKills(*CopyMI, RegKills, KillOps);
Evan Chengde4e9422007-02-25 09:51:27 +00001397
Chris Lattneraddc55a2006-04-28 01:46:50 +00001398 // This invalidates DesignatedReg.
1399 Spills.ClobberPhysReg(DesignatedReg);
1400
Evan Chengdc6be192007-08-14 05:42:54 +00001401 Spills.addAvailable(ReuseSlot, &MI, DesignatedReg);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001402 unsigned RReg =
Dan Gohman6f0d0242008-02-10 18:45:23 +00001403 SubIdx ? TRI->getSubReg(DesignatedReg, SubIdx) : DesignatedReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001404 MI.getOperand(i).setReg(RReg);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001405 DOUT << '\t' << *prior(MII);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001406 ++NumReused;
1407 continue;
Evan Cheng66f71632007-10-19 21:23:22 +00001408 } // if (PhysReg)
Chris Lattner50ea01e2005-09-09 20:29:51 +00001409
1410 // Otherwise, reload it and remember that we have it.
1411 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +00001412 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattner7fb64342004-10-01 19:04:51 +00001413
Chris Lattner50ea01e2005-09-09 20:29:51 +00001414 // Note that, if we reused a register for a previous operand, the
1415 // register we want to reload into might not actually be
1416 // available. If this occurs, use the register indicated by the
1417 // reuser.
Chris Lattner540fec62006-02-25 01:51:33 +00001418 if (ReusedOperands.hasReuses())
1419 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
Evan Cheng549f27d32007-08-13 23:45:17 +00001420 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
Chris Lattner540fec62006-02-25 01:51:33 +00001421
Chris Lattner84bc5422007-12-31 04:13:23 +00001422 RegInfo->setPhysRegUsed(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001423 ReusedOperands.markClobbered(PhysReg);
Evan Cheng549f27d32007-08-13 23:45:17 +00001424 if (DoReMat) {
Evan Chengca1267c2008-03-31 20:40:39 +00001425 ReMaterialize(MBB, MII, PhysReg, VirtReg, TII, TRI, VRM);
Evan Cheng91935142007-04-04 07:40:01 +00001426 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +00001427 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001428 TII->loadRegFromStackSlot(MBB, &MI, PhysReg, SSorRMId, RC);
Evan Chengd3653122008-02-27 03:04:06 +00001429 MachineInstr *LoadMI = prior(MII);
1430 VRM.addSpillSlotUse(SSorRMId, LoadMI);
Evan Cheng91935142007-04-04 07:40:01 +00001431 ++NumLoads;
1432 }
Chris Lattner50ea01e2005-09-09 20:29:51 +00001433 // This invalidates PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +00001434 Spills.ClobberPhysReg(PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001435
1436 // Any stores to this stack slot are not dead anymore.
Evan Cheng549f27d32007-08-13 23:45:17 +00001437 if (!DoReMat)
Evan Chengfff3e192007-08-14 09:11:18 +00001438 MaybeDeadStores[SSorRMId] = NULL;
Evan Cheng549f27d32007-08-13 23:45:17 +00001439 Spills.addAvailable(SSorRMId, &MI, PhysReg);
Evan Chengde4e9422007-02-25 09:51:27 +00001440 // Assumes this is the last use. IsKill will be unset if reg is reused
1441 // unless it's a two-address operand.
Chris Lattner749c6f62008-01-07 07:27:27 +00001442 if (TID.getOperandConstraint(i, TOI::TIED_TO) == -1)
Evan Chengde4e9422007-02-25 09:51:27 +00001443 MI.getOperand(i).setIsKill();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001444 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001445 MI.getOperand(i).setReg(RReg);
Evan Cheng0c40d722007-07-11 05:28:39 +00001446 UpdateKills(*prior(MII), RegKills, KillOps);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001447 DOUT << '\t' << *prior(MII);
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001448 }
1449
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001450 DOUT << '\t' << MI;
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001451
Evan Cheng81a03822007-11-17 00:40:40 +00001452
Chris Lattner7fb64342004-10-01 19:04:51 +00001453 // If we have folded references to memory operands, make sure we clear all
1454 // physical registers that may contain the value of the spilled virtual
1455 // register
Evan Cheng66f71632007-10-19 21:23:22 +00001456 SmallSet<int, 2> FoldedSS;
Evan Chengc17ba8a2008-03-14 20:44:01 +00001457 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +00001458 unsigned VirtReg = I->second.first;
1459 VirtRegMap::ModRef MR = I->second.second;
Evan Cheng66f71632007-10-19 21:23:22 +00001460 DOUT << "Folded vreg: " << VirtReg << " MR: " << MR;
Evan Cheng81a03822007-11-17 00:40:40 +00001461
Evan Chengc17ba8a2008-03-14 20:44:01 +00001462 // MI2VirtMap be can updated which invalidate the iterator.
1463 // Increment the iterator first.
1464 ++I;
Chris Lattnercea86882005-09-19 06:56:21 +00001465 int SS = VRM.getStackSlot(VirtReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001466 if (SS == VirtRegMap::NO_STACK_SLOT)
1467 continue;
Evan Cheng90a43c32007-08-15 20:20:34 +00001468 FoldedSS.insert(SS);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001469 DOUT << " - StackSlot: " << SS << "\n";
Chris Lattnercea86882005-09-19 06:56:21 +00001470
1471 // If this folded instruction is just a use, check to see if it's a
1472 // straight load from the virt reg slot.
1473 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
1474 int FrameIdx;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001475 unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx);
1476 if (DestReg && FrameIdx == SS) {
1477 // If this spill slot is available, turn it into a copy (or nothing)
1478 // instead of leaving it as a load!
1479 if (unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SS)) {
1480 DOUT << "Promoted Load To Copy: " << MI;
1481 if (DestReg != InReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001482 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Owen Andersond10fd972007-12-31 06:32:00 +00001483 TII->copyRegToReg(MBB, &MI, DestReg, InReg, RC, RC);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001484 // Revisit the copy so we make sure to notice the effects of the
1485 // operation on the destreg (either needing to RA it if it's
1486 // virtual or needing to clobber any values if it's physical).
1487 NextMII = &MI;
1488 --NextMII; // backtrack to the copy.
1489 BackTracked = true;
Evan Cheng39c883c2007-12-11 23:36:57 +00001490 } else {
Evan Cheng32dfbea2007-10-12 08:50:34 +00001491 DOUT << "Removing now-noop copy: " << MI;
Evan Cheng39c883c2007-12-11 23:36:57 +00001492 // Unset last kill since it's being reused.
1493 InvalidateKill(InReg, RegKills, KillOps);
1494 }
Evan Chengde4e9422007-02-25 09:51:27 +00001495
Evan Cheng7a0f1852008-05-20 08:13:21 +00001496 InvalidateKills(MI, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001497 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001498 MBB.erase(&MI);
1499 Erased = true;
1500 goto ProcessNextInst;
Chris Lattnercea86882005-09-19 06:56:21 +00001501 }
Evan Cheng7f566252007-10-13 02:50:24 +00001502 } else {
1503 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1504 SmallVector<MachineInstr*, 4> NewMIs;
1505 if (PhysReg &&
Owen Anderson6425f8b2008-01-07 01:35:56 +00001506 TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, false, NewMIs)) {
Evan Cheng7f566252007-10-13 02:50:24 +00001507 MBB.insert(MII, NewMIs[0]);
Evan Cheng7a0f1852008-05-20 08:13:21 +00001508 InvalidateKills(MI, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001509 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng7f566252007-10-13 02:50:24 +00001510 MBB.erase(&MI);
1511 Erased = true;
1512 --NextMII; // backtrack to the unfolded instruction.
1513 BackTracked = true;
1514 goto ProcessNextInst;
1515 }
Chris Lattnercea86882005-09-19 06:56:21 +00001516 }
1517 }
1518
1519 // If this reference is not a use, any previous store is now dead.
1520 // Otherwise, the store to this stack slot is not dead anymore.
Evan Chengfff3e192007-08-14 09:11:18 +00001521 MachineInstr* DeadStore = MaybeDeadStores[SS];
1522 if (DeadStore) {
Evan Cheng66f71632007-10-19 21:23:22 +00001523 bool isDead = !(MR & VirtRegMap::isRef);
Evan Cheng7f566252007-10-13 02:50:24 +00001524 MachineInstr *NewStore = NULL;
Evan Chengcbfb9b22007-10-22 03:01:44 +00001525 if (MR & VirtRegMap::isModRef) {
Evan Cheng7f566252007-10-13 02:50:24 +00001526 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1527 SmallVector<MachineInstr*, 4> NewMIs;
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001528 // We can reuse this physreg as long as we are allowed to clobber
Chris Lattner84bc5422007-12-31 04:13:23 +00001529 // the value and there isn't an earlier def that has already clobbered
1530 // the physreg.
Evan Cheng7f566252007-10-13 02:50:24 +00001531 if (PhysReg &&
Evan Cheng7ebc06b2008-05-07 00:49:28 +00001532 !TII->isStoreToStackSlot(&MI, SS)) { // Not profitable!
1533 MachineOperand *KillOpnd =
1534 DeadStore->findRegisterUseOperand(PhysReg, true);
1535 // Note, if the store is storing a sub-register, it's possible the
1536 // super-register is needed below.
1537 if (KillOpnd && !KillOpnd->getSubReg() &&
1538 TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, true,NewMIs)){
1539 MBB.insert(MII, NewMIs[0]);
1540 NewStore = NewMIs[1];
1541 MBB.insert(MII, NewStore);
1542 VRM.addSpillSlotUse(SS, NewStore);
Evan Cheng7a0f1852008-05-20 08:13:21 +00001543 InvalidateKills(MI, RegKills, KillOps);
Evan Cheng7ebc06b2008-05-07 00:49:28 +00001544 VRM.RemoveMachineInstrFromMaps(&MI);
1545 MBB.erase(&MI);
1546 Erased = true;
1547 --NextMII;
1548 --NextMII; // backtrack to the unfolded instruction.
1549 BackTracked = true;
1550 isDead = true;
1551 }
Evan Cheng66f71632007-10-19 21:23:22 +00001552 }
Evan Cheng7f566252007-10-13 02:50:24 +00001553 }
1554
1555 if (isDead) { // Previous store is dead.
Chris Lattnercea86882005-09-19 06:56:21 +00001556 // If we get here, the store is dead, nuke it now.
Evan Chengfff3e192007-08-14 09:11:18 +00001557 DOUT << "Removed dead store:\t" << *DeadStore;
1558 InvalidateKills(*DeadStore, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001559 VRM.RemoveMachineInstrFromMaps(DeadStore);
Evan Cheng7f566252007-10-13 02:50:24 +00001560 MBB.erase(DeadStore);
1561 if (!NewStore)
1562 ++NumDSE;
Chris Lattnercea86882005-09-19 06:56:21 +00001563 }
Evan Cheng7f566252007-10-13 02:50:24 +00001564
Evan Chengfff3e192007-08-14 09:11:18 +00001565 MaybeDeadStores[SS] = NULL;
Evan Cheng7f566252007-10-13 02:50:24 +00001566 if (NewStore) {
1567 // Treat this store as a spill merged into a copy. That makes the
1568 // stack slot value available.
1569 VRM.virtFolded(VirtReg, NewStore, VirtRegMap::isMod);
1570 goto ProcessNextInst;
1571 }
Chris Lattnercea86882005-09-19 06:56:21 +00001572 }
1573
1574 // If the spill slot value is available, and this is a new definition of
1575 // the value, the value is not available anymore.
1576 if (MR & VirtRegMap::isMod) {
Chris Lattner07cf1412006-02-03 00:36:31 +00001577 // Notice that the value in this stack slot has been modified.
Evan Cheng549f27d32007-08-13 23:45:17 +00001578 Spills.ModifyStackSlotOrReMat(SS);
Chris Lattnercd816392006-02-02 23:29:36 +00001579
1580 // If this is *just* a mod of the value, check to see if this is just a
1581 // store to the spill slot (i.e. the spill got merged into the copy). If
1582 // so, realize that the vreg is available now, and add the store to the
1583 // MaybeDeadStore info.
1584 int StackSlot;
1585 if (!(MR & VirtRegMap::isRef)) {
1586 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001587 assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
Chris Lattnercd816392006-02-02 23:29:36 +00001588 "Src hasn't been allocated yet?");
Chris Lattner07cf1412006-02-03 00:36:31 +00001589 // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark
Chris Lattnercd816392006-02-02 23:29:36 +00001590 // this as a potentially dead store in case there is a subsequent
1591 // store into the stack slot without a read from it.
1592 MaybeDeadStores[StackSlot] = &MI;
1593
Chris Lattnercd816392006-02-02 23:29:36 +00001594 // If the stack slot value was previously available in some other
1595 // register, change it now. Otherwise, make the register available,
1596 // in PhysReg.
Evan Cheng91e23902007-02-23 01:13:26 +00001597 Spills.addAvailable(StackSlot, &MI, SrcReg, false/*don't clobber*/);
Chris Lattnercd816392006-02-02 23:29:36 +00001598 }
1599 }
Chris Lattner7fb64342004-10-01 19:04:51 +00001600 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001601 }
1602
Chris Lattner7fb64342004-10-01 19:04:51 +00001603 // Process all of the spilled defs.
1604 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1605 MachineOperand &MO = MI.getOperand(i);
Evan Cheng66f71632007-10-19 21:23:22 +00001606 if (!(MO.isRegister() && MO.getReg() && MO.isDef()))
1607 continue;
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001608
Evan Cheng66f71632007-10-19 21:23:22 +00001609 unsigned VirtReg = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001610 if (!TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Evan Cheng66f71632007-10-19 21:23:22 +00001611 // Check to see if this is a noop copy. If so, eliminate the
1612 // instruction before considering the dest reg to be changed.
1613 unsigned Src, Dst;
1614 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
1615 ++NumDCE;
1616 DOUT << "Removing now-noop copy: " << MI;
Evan Cheng7a0f1852008-05-20 08:13:21 +00001617 SmallVector<unsigned, 2> KillRegs;
1618 InvalidateKills(MI, RegKills, KillOps, &KillRegs);
1619 if (MO.isDead() && !KillRegs.empty()) {
1620 assert(KillRegs[0] == Dst);
1621 // Last def is now dead.
1622 TransferDeadness(&MBB, Dist, Src, RegKills, KillOps);
1623 }
Evan Chengd3653122008-02-27 03:04:06 +00001624 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng66f71632007-10-19 21:23:22 +00001625 MBB.erase(&MI);
1626 Erased = true;
Evan Cheng66f71632007-10-19 21:23:22 +00001627 Spills.disallowClobberPhysReg(VirtReg);
1628 goto ProcessNextInst;
1629 }
1630
1631 // If it's not a no-op copy, it clobbers the value in the destreg.
1632 Spills.ClobberPhysReg(VirtReg);
1633 ReusedOperands.markClobbered(VirtReg);
1634
1635 // Check to see if this instruction is a load from a stack slot into
1636 // a register. If so, this provides the stack slot value in the reg.
1637 int FrameIdx;
1638 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
1639 assert(DestReg == VirtReg && "Unknown load situation!");
1640
1641 // If it is a folded reference, then it's not safe to clobber.
1642 bool Folded = FoldedSS.count(FrameIdx);
1643 // Otherwise, if it wasn't available, remember that it is now!
1644 Spills.addAvailable(FrameIdx, &MI, DestReg, !Folded);
1645 goto ProcessNextInst;
1646 }
1647
1648 continue;
1649 }
1650
Evan Chengc498b022007-11-14 07:59:08 +00001651 unsigned SubIdx = MO.getSubReg();
Evan Cheng66f71632007-10-19 21:23:22 +00001652 bool DoReMat = VRM.isReMaterialized(VirtReg);
1653 if (DoReMat)
1654 ReMatDefs.insert(&MI);
1655
1656 // The only vregs left are stack slot definitions.
1657 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001658 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Evan Cheng66f71632007-10-19 21:23:22 +00001659
1660 // If this def is part of a two-address operand, make sure to execute
1661 // the store from the correct physical register.
1662 unsigned PhysReg;
Chris Lattner749c6f62008-01-07 07:27:27 +00001663 int TiedOp = MI.getDesc().findTiedToSrcOperand(i);
Evan Cheng7277a7d2007-11-02 17:35:08 +00001664 if (TiedOp != -1) {
Evan Cheng66f71632007-10-19 21:23:22 +00001665 PhysReg = MI.getOperand(TiedOp).getReg();
Evan Chengc498b022007-11-14 07:59:08 +00001666 if (SubIdx) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001667 unsigned SuperReg = findSuperReg(RC, PhysReg, SubIdx, TRI);
1668 assert(SuperReg && TRI->getSubReg(SuperReg, SubIdx) == PhysReg &&
Evan Cheng7277a7d2007-11-02 17:35:08 +00001669 "Can't find corresponding super-register!");
1670 PhysReg = SuperReg;
1671 }
1672 } else {
Evan Cheng66f71632007-10-19 21:23:22 +00001673 PhysReg = VRM.getPhys(VirtReg);
1674 if (ReusedOperands.isClobbered(PhysReg)) {
1675 // Another def has taken the assigned physreg. It must have been a
1676 // use&def which got it due to reuse. Undo the reuse!
1677 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
1678 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
1679 }
1680 }
1681
Evan Chenged70cbb32008-03-26 19:03:01 +00001682 assert(PhysReg && "VR not assigned a physical register?");
Chris Lattner84bc5422007-12-31 04:13:23 +00001683 RegInfo->setPhysRegUsed(PhysReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +00001684 unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng7277a7d2007-11-02 17:35:08 +00001685 ReusedOperands.markClobbered(RReg);
1686 MI.getOperand(i).setReg(RReg);
1687
Evan Cheng66f71632007-10-19 21:23:22 +00001688 if (!MO.isDead()) {
Evan Cheng66f71632007-10-19 21:23:22 +00001689 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001690 SpillRegToStackSlot(MBB, MII, -1, PhysReg, StackSlot, RC, true,
1691 LastStore, Spills, ReMatDefs, RegKills, KillOps, VRM);
Evan Chenge4b39002007-12-03 21:31:55 +00001692 NextMII = next(MII);
Evan Cheng66f71632007-10-19 21:23:22 +00001693
1694 // Check to see if this is a noop copy. If so, eliminate the
1695 // instruction before considering the dest reg to be changed.
1696 {
Chris Lattner29268692006-09-05 02:12:02 +00001697 unsigned Src, Dst;
1698 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
1699 ++NumDCE;
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001700 DOUT << "Removing now-noop copy: " << MI;
Evan Cheng7a0f1852008-05-20 08:13:21 +00001701 InvalidateKills(MI, RegKills, KillOps);
Evan Chengd3653122008-02-27 03:04:06 +00001702 VRM.RemoveMachineInstrFromMaps(&MI);
Chris Lattner29268692006-09-05 02:12:02 +00001703 MBB.erase(&MI);
Evan Cheng0c40d722007-07-11 05:28:39 +00001704 Erased = true;
Evan Cheng66f71632007-10-19 21:23:22 +00001705 UpdateKills(*LastStore, RegKills, KillOps);
Chris Lattner29268692006-09-05 02:12:02 +00001706 goto ProcessNextInst;
Chris Lattner7fb64342004-10-01 19:04:51 +00001707 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001708 }
Evan Cheng66f71632007-10-19 21:23:22 +00001709 }
Chris Lattner7fb64342004-10-01 19:04:51 +00001710 }
Chris Lattnercea86882005-09-19 06:56:21 +00001711 ProcessNextInst:
Evan Cheng7a0f1852008-05-20 08:13:21 +00001712 DistanceMap.insert(std::make_pair(&MI, Dist++));
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001713 if (!Erased && !BackTracked) {
Evan Cheng0c40d722007-07-11 05:28:39 +00001714 for (MachineBasicBlock::iterator II = MI; II != NextMII; ++II)
1715 UpdateKills(*II, RegKills, KillOps);
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001716 }
Chris Lattner7fb64342004-10-01 19:04:51 +00001717 MII = NextMII;
1718 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001719}
1720
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001721llvm::Spiller* llvm::createSpiller() {
1722 switch (SpillerOpt) {
1723 default: assert(0 && "Unreachable!");
1724 case local:
1725 return new LocalSpiller();
1726 case simple:
1727 return new SimpleSpiller();
1728 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +00001729}