blob: 91000541690982aad9b045544b7bea608b4353aa [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"
Chris Lattner84bc5422007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000025#include "llvm/Target/TargetMachine.h"
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000026#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Evan Cheng957840b2007-02-21 02:22:03 +000030#include "llvm/ADT/BitVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/ADT/STLExtras.h"
Chris Lattner08a4d5a2007-01-23 00:59:48 +000033#include "llvm/ADT/SmallSet.h"
Chris Lattner27f29162004-10-26 15:35:58 +000034#include <algorithm>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000035using namespace llvm;
36
Chris Lattnercd3245a2006-12-19 22:41:21 +000037STATISTIC(NumSpills, "Number of register spills");
Evan Cheng2638e1a2007-03-20 08:13:50 +000038STATISTIC(NumReMats, "Number of re-materialization");
Evan Chengb6ca4b32007-08-14 23:25:37 +000039STATISTIC(NumDRM , "Number of re-materializable defs elided");
Chris Lattnercd3245a2006-12-19 22:41:21 +000040STATISTIC(NumStores, "Number of stores added");
41STATISTIC(NumLoads , "Number of loads added");
42STATISTIC(NumReused, "Number of values reused");
43STATISTIC(NumDSE , "Number of dead stores elided");
44STATISTIC(NumDCE , "Number of copies elided");
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +000045
Chris Lattnercd3245a2006-12-19 22:41:21 +000046namespace {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000047 enum SpillerName { simple, local };
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000048
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000049 static cl::opt<SpillerName>
Chris Lattner8c4d88d2004-09-30 01:54:45 +000050 SpillerOpt("spiller",
Chris Lattner7fb64342004-10-01 19:04:51 +000051 cl::desc("Spiller to use: (default: local)"),
Chris Lattner8c4d88d2004-09-30 01:54:45 +000052 cl::Prefix,
53 cl::values(clEnumVal(simple, " simple spiller"),
54 clEnumVal(local, " local spiller"),
55 clEnumValEnd),
Chris Lattner7fb64342004-10-01 19:04:51 +000056 cl::init(local));
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000057}
58
Chris Lattner8c4d88d2004-09-30 01:54:45 +000059//===----------------------------------------------------------------------===//
60// VirtRegMap implementation
61//===----------------------------------------------------------------------===//
62
Chris Lattner29268692006-09-05 02:12:02 +000063VirtRegMap::VirtRegMap(MachineFunction &mf)
64 : TII(*mf.getTarget().getInstrInfo()), MF(mf),
Evan Cheng2638e1a2007-03-20 08:13:50 +000065 Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT),
Evan Cheng81a03822007-11-17 00:40:40 +000066 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
Evan Chengd120ffd2007-12-05 10:24:35 +000067 Virt2SplitKillMap(0), ReMatMap(NULL), ReMatId(MAX_STACK_SLOT+1) {
Chris Lattner29268692006-09-05 02:12:02 +000068 grow();
69}
70
Chris Lattner8c4d88d2004-09-30 01:54:45 +000071void VirtRegMap::grow() {
Chris Lattner84bc5422007-12-31 04:13:23 +000072 unsigned LastVirtReg = MF.getRegInfo().getLastVirtReg();
Evan Cheng549f27d32007-08-13 23:45:17 +000073 Virt2PhysMap.grow(LastVirtReg);
74 Virt2StackSlotMap.grow(LastVirtReg);
75 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Cheng81a03822007-11-17 00:40:40 +000076 Virt2SplitMap.grow(LastVirtReg);
Evan Chengadf85902007-12-05 09:51:10 +000077 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +000078 ReMatMap.grow(LastVirtReg);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000079}
80
Chris Lattner8c4d88d2004-09-30 01:54:45 +000081int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
82 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000083 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000084 "attempt to assign stack slot to already spilled register");
Chris Lattner84bc5422007-12-31 04:13:23 +000085 const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(virtReg);
Chris Lattner7f690e62004-09-30 02:15:18 +000086 int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
87 RC->getAlignment());
88 Virt2StackSlotMap[virtReg] = frameIndex;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000089 ++NumSpills;
90 return frameIndex;
91}
92
93void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
94 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000095 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000096 "attempt to assign stack slot to already spilled register");
Evan Cheng91935142007-04-04 07:40:01 +000097 assert((frameIndex >= 0 ||
98 (frameIndex >= MF.getFrameInfo()->getObjectIndexBegin())) &&
99 "illegal fixed frame index");
Chris Lattner7f690e62004-09-30 02:15:18 +0000100 Virt2StackSlotMap[virtReg] = frameIndex;
Alkis Evlogimenos38af59a2004-05-29 20:38:05 +0000101}
102
Evan Cheng2638e1a2007-03-20 08:13:50 +0000103int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
104 assert(MRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000105 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Evan Cheng2638e1a2007-03-20 08:13:50 +0000106 "attempt to assign re-mat id to already spilled register");
Evan Cheng549f27d32007-08-13 23:45:17 +0000107 Virt2ReMatIdMap[virtReg] = ReMatId;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000108 return ReMatId++;
109}
110
Evan Cheng549f27d32007-08-13 23:45:17 +0000111void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
112 assert(MRegisterInfo::isVirtualRegister(virtReg));
113 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
114 "attempt to assign re-mat id to already spilled register");
115 Virt2ReMatIdMap[virtReg] = id;
116}
117
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000118void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengaee4af62007-12-02 08:30:39 +0000119 MachineInstr *NewMI, ModRef MRInfo) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000120 // Move previous memory references folded to new instruction.
121 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000122 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000123 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
124 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
Chris Lattnerdbea9732004-09-30 16:35:08 +0000125 MI2VirtMap.erase(I++);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000126 }
Chris Lattnerdbea9732004-09-30 16:35:08 +0000127
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000128 // add new memory reference
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000129 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000130}
131
Evan Cheng7f566252007-10-13 02:50:24 +0000132void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
133 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
134 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
135}
136
Chris Lattner7f690e62004-09-30 02:15:18 +0000137void VirtRegMap::print(std::ostream &OS) const {
138 const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo();
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000139
Chris Lattner7f690e62004-09-30 02:15:18 +0000140 OS << "********** REGISTER MAP **********\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000141 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +0000142 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i) {
Chris Lattner7f690e62004-09-30 02:15:18 +0000143 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
144 OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000145
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000146 }
147
148 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +0000149 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattner7f690e62004-09-30 02:15:18 +0000150 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
151 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
152 OS << '\n';
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000153}
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000154
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000155void VirtRegMap::dump() const {
Bill Wendling5c7e3262006-12-17 05:15:13 +0000156 print(DOUT);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000157}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000158
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000159
160//===----------------------------------------------------------------------===//
161// Simple Spiller Implementation
162//===----------------------------------------------------------------------===//
163
164Spiller::~Spiller() {}
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000165
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000166namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000167 struct VISIBILITY_HIDDEN SimpleSpiller : public Spiller {
Chris Lattner35f27052006-05-01 21:16:03 +0000168 bool runOnMachineFunction(MachineFunction& mf, VirtRegMap &VRM);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000169 };
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000170}
171
Chris Lattner35f27052006-05-01 21:16:03 +0000172bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000173 DOUT << "********** REWRITE MACHINE CODE **********\n";
174 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000175 const TargetMachine &TM = MF.getTarget();
176 const MRegisterInfo &MRI = *TM.getRegisterInfo();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000177
Chris Lattner4ea1b822004-09-30 02:33:48 +0000178 // LoadedRegs - Keep track of which vregs are loaded, so that we only load
179 // each vreg once (in the case where a spilled vreg is used by multiple
180 // operands). This is always smaller than the number of operands to the
181 // current machine instr, so it should be small.
182 std::vector<unsigned> LoadedRegs;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000183
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000184 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
185 MBBI != E; ++MBBI) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000186 DOUT << MBBI->getBasicBlock()->getName() << ":\n";
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000187 MachineBasicBlock &MBB = *MBBI;
188 for (MachineBasicBlock::iterator MII = MBB.begin(),
189 E = MBB.end(); MII != E; ++MII) {
190 MachineInstr &MI = *MII;
191 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000192 MachineOperand &MO = MI.getOperand(i);
Chris Lattner886dd912005-04-04 21:35:34 +0000193 if (MO.isRegister() && MO.getReg())
194 if (MRegisterInfo::isVirtualRegister(MO.getReg())) {
195 unsigned VirtReg = MO.getReg();
196 unsigned PhysReg = VRM.getPhys(VirtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000197 if (!VRM.isAssignedReg(VirtReg)) {
Chris Lattner886dd912005-04-04 21:35:34 +0000198 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000199 const TargetRegisterClass* RC =
Chris Lattner84bc5422007-12-31 04:13:23 +0000200 MF.getRegInfo().getRegClass(VirtReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000201
Chris Lattner886dd912005-04-04 21:35:34 +0000202 if (MO.isUse() &&
203 std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg)
204 == LoadedRegs.end()) {
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000205 MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000206 LoadedRegs.push_back(VirtReg);
207 ++NumLoads;
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000208 DOUT << '\t' << *prior(MII);
Chris Lattner886dd912005-04-04 21:35:34 +0000209 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000210
Chris Lattner886dd912005-04-04 21:35:34 +0000211 if (MO.isDef()) {
Evan Chengd64b5c82007-12-05 03:14:33 +0000212 MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, true,
213 StackSlot, RC);
Chris Lattner886dd912005-04-04 21:35:34 +0000214 ++NumStores;
215 }
Chris Lattner0fc27cc2004-09-30 02:59:33 +0000216 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000217 MF.getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000218 MI.getOperand(i).setReg(PhysReg);
Chris Lattner886dd912005-04-04 21:35:34 +0000219 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +0000220 MF.getRegInfo().setPhysRegUsed(MO.getReg());
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000221 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000222 }
Chris Lattner886dd912005-04-04 21:35:34 +0000223
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000224 DOUT << '\t' << MI;
Chris Lattner4ea1b822004-09-30 02:33:48 +0000225 LoadedRegs.clear();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000226 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000227 }
228 return true;
229}
230
231//===----------------------------------------------------------------------===//
232// Local Spiller Implementation
233//===----------------------------------------------------------------------===//
234
235namespace {
Evan Cheng66f71632007-10-19 21:23:22 +0000236 class AvailableSpills;
237
Chris Lattner7fb64342004-10-01 19:04:51 +0000238 /// LocalSpiller - This spiller does a simple pass over the machine basic
239 /// block to attempt to keep spills in registers as much as possible for
240 /// blocks that have low register pressure (the vreg may be spilled due to
241 /// register pressure in other blocks).
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000242 class VISIBILITY_HIDDEN LocalSpiller : public Spiller {
Chris Lattner84bc5422007-12-31 04:13:23 +0000243 MachineRegisterInfo *RegInfo;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000244 const MRegisterInfo *MRI;
Chris Lattner7fb64342004-10-01 19:04:51 +0000245 const TargetInstrInfo *TII;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000246 public:
Chris Lattner35f27052006-05-01 21:16:03 +0000247 bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000248 RegInfo = &MF.getRegInfo();
Chris Lattner7fb64342004-10-01 19:04:51 +0000249 MRI = MF.getTarget().getRegisterInfo();
250 TII = MF.getTarget().getInstrInfo();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000251 DOUT << "\n**** Local spiller rewriting function '"
252 << MF.getFunction()->getName() << "':\n";
Chris Lattner84bc5422007-12-31 04:13:23 +0000253 DOUT << "**** Machine Instrs (NOTE! Does not include spills and reloads!)"
254 " ****\n";
David Greene04fa32f2007-09-06 16:36:39 +0000255 DEBUG(MF.dump());
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000256
Chris Lattner7fb64342004-10-01 19:04:51 +0000257 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
258 MBB != E; ++MBB)
Evan Cheng549f27d32007-08-13 23:45:17 +0000259 RewriteMBB(*MBB, VRM);
David Greene04fa32f2007-09-06 16:36:39 +0000260
261 DOUT << "**** Post Machine Instrs ****\n";
262 DEBUG(MF.dump());
263
Chris Lattner7fb64342004-10-01 19:04:51 +0000264 return true;
265 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000266 private:
Evan Cheng66f71632007-10-19 21:23:22 +0000267 bool PrepForUnfoldOpti(MachineBasicBlock &MBB,
268 MachineBasicBlock::iterator &MII,
269 std::vector<MachineInstr*> &MaybeDeadStores,
270 AvailableSpills &Spills, BitVector &RegKills,
271 std::vector<MachineOperand*> &KillOps,
272 VirtRegMap &VRM);
Evan Cheng81a03822007-11-17 00:40:40 +0000273 void SpillRegToStackSlot(MachineBasicBlock &MBB,
274 MachineBasicBlock::iterator &MII,
275 int Idx, unsigned PhysReg, int StackSlot,
276 const TargetRegisterClass *RC,
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000277 bool isAvailable, MachineInstr *&LastStore,
Evan Cheng81a03822007-11-17 00:40:40 +0000278 AvailableSpills &Spills,
279 SmallSet<MachineInstr*, 4> &ReMatDefs,
280 BitVector &RegKills,
281 std::vector<MachineOperand*> &KillOps,
Evan Chenge4b39002007-12-03 21:31:55 +0000282 VirtRegMap &VRM);
Evan Cheng549f27d32007-08-13 23:45:17 +0000283 void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000284 };
285}
286
Chris Lattner66cf80f2006-02-03 23:13:58 +0000287/// AvailableSpills - As the local spiller is scanning and rewriting an MBB from
Evan Cheng549f27d32007-08-13 23:45:17 +0000288/// top down, keep track of which spills slots or remat are available in each
289/// register.
Chris Lattner593c9582006-02-03 23:28:46 +0000290///
291/// Note that not all physregs are created equal here. In particular, some
292/// physregs are reloads that we are allowed to clobber or ignore at any time.
293/// Other physregs are values that the register allocated program is using that
294/// we cannot CHANGE, but we can read if we like. We keep track of this on a
Evan Cheng549f27d32007-08-13 23:45:17 +0000295/// per-stack-slot / remat id basis as the low bit in the value of the
296/// SpillSlotsAvailable entries. The predicate 'canClobberPhysReg()' checks
297/// this bit and addAvailable sets it if.
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000298namespace {
299class VISIBILITY_HIDDEN AvailableSpills {
Chris Lattner66cf80f2006-02-03 23:13:58 +0000300 const MRegisterInfo *MRI;
301 const TargetInstrInfo *TII;
302
Evan Cheng549f27d32007-08-13 23:45:17 +0000303 // SpillSlotsOrReMatsAvailable - This map keeps track of all of the spilled
304 // or remat'ed virtual register values that are still available, due to being
305 // loaded or stored to, but not invalidated yet.
306 std::map<int, unsigned> SpillSlotsOrReMatsAvailable;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000307
Evan Cheng549f27d32007-08-13 23:45:17 +0000308 // PhysRegsAvailable - This is the inverse of SpillSlotsOrReMatsAvailable,
309 // indicating which stack slot values are currently held by a physreg. This
310 // is used to invalidate entries in SpillSlotsOrReMatsAvailable when a
311 // physreg is modified.
Chris Lattner66cf80f2006-02-03 23:13:58 +0000312 std::multimap<unsigned, int> PhysRegsAvailable;
313
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000314 void disallowClobberPhysRegOnly(unsigned PhysReg);
315
Chris Lattner66cf80f2006-02-03 23:13:58 +0000316 void ClobberPhysRegOnly(unsigned PhysReg);
317public:
318 AvailableSpills(const MRegisterInfo *mri, const TargetInstrInfo *tii)
319 : MRI(mri), TII(tii) {
320 }
321
Evan Cheng91e23902007-02-23 01:13:26 +0000322 const MRegisterInfo *getRegInfo() const { return MRI; }
323
Evan Cheng549f27d32007-08-13 23:45:17 +0000324 /// getSpillSlotOrReMatPhysReg - If the specified stack slot or remat is
325 /// available in a physical register, return that PhysReg, otherwise
326 /// return 0.
327 unsigned getSpillSlotOrReMatPhysReg(int Slot) const {
328 std::map<int, unsigned>::const_iterator I =
329 SpillSlotsOrReMatsAvailable.find(Slot);
330 if (I != SpillSlotsOrReMatsAvailable.end()) {
Evan Chengb9591c62007-07-11 08:47:44 +0000331 return I->second >> 1; // Remove the CanClobber bit.
Evan Cheng91e23902007-02-23 01:13:26 +0000332 }
Chris Lattner66cf80f2006-02-03 23:13:58 +0000333 return 0;
334 }
Evan Chengde4e9422007-02-25 09:51:27 +0000335
Evan Cheng549f27d32007-08-13 23:45:17 +0000336 /// addAvailable - Mark that the specified stack slot / remat is available in
337 /// the specified physreg. If CanClobber is true, the physreg can be modified
338 /// at any time without changing the semantics of the program.
339 void addAvailable(int SlotOrReMat, MachineInstr *MI, unsigned Reg,
Evan Cheng91e23902007-02-23 01:13:26 +0000340 bool CanClobber = true) {
Chris Lattner86662492006-02-03 23:50:46 +0000341 // If this stack slot is thought to be available in some other physreg,
342 // remove its record.
Evan Cheng549f27d32007-08-13 23:45:17 +0000343 ModifyStackSlotOrReMat(SlotOrReMat);
Chris Lattner86662492006-02-03 23:50:46 +0000344
Evan Cheng549f27d32007-08-13 23:45:17 +0000345 PhysRegsAvailable.insert(std::make_pair(Reg, SlotOrReMat));
Evan Cheng90a43c32007-08-15 20:20:34 +0000346 SpillSlotsOrReMatsAvailable[SlotOrReMat]= (Reg << 1) | (unsigned)CanClobber;
Chris Lattner66cf80f2006-02-03 23:13:58 +0000347
Evan Cheng549f27d32007-08-13 23:45:17 +0000348 if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
349 DOUT << "Remembering RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000350 else
Evan Cheng549f27d32007-08-13 23:45:17 +0000351 DOUT << "Remembering SS#" << SlotOrReMat;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000352 DOUT << " in physreg " << MRI->getName(Reg) << "\n";
Chris Lattner66cf80f2006-02-03 23:13:58 +0000353 }
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000354
Chris Lattner593c9582006-02-03 23:28:46 +0000355 /// canClobberPhysReg - Return true if the spiller is allowed to change the
356 /// value of the specified stackslot register if it desires. The specified
357 /// stack slot must be available in a physreg for this query to make sense.
Evan Cheng549f27d32007-08-13 23:45:17 +0000358 bool canClobberPhysReg(int SlotOrReMat) const {
Evan Cheng90a43c32007-08-15 20:20:34 +0000359 assert(SpillSlotsOrReMatsAvailable.count(SlotOrReMat) &&
360 "Value not available!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000361 return SpillSlotsOrReMatsAvailable.find(SlotOrReMat)->second & 1;
Chris Lattner593c9582006-02-03 23:28:46 +0000362 }
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000363
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000364 /// disallowClobberPhysReg - Unset the CanClobber bit of the specified
365 /// stackslot register. The register is still available but is no longer
366 /// allowed to be modifed.
367 void disallowClobberPhysReg(unsigned PhysReg);
368
Chris Lattner66cf80f2006-02-03 23:13:58 +0000369 /// ClobberPhysReg - This is called when the specified physreg changes
Evan Cheng66f71632007-10-19 21:23:22 +0000370 /// value. We use this to invalidate any info about stuff that lives in
Chris Lattner66cf80f2006-02-03 23:13:58 +0000371 /// it and any of its aliases.
372 void ClobberPhysReg(unsigned PhysReg);
373
Evan Cheng90a43c32007-08-15 20:20:34 +0000374 /// ModifyStackSlotOrReMat - This method is called when the value in a stack
375 /// slot changes. This removes information about which register the previous
376 /// value for this slot lives in (as the previous value is dead now).
Evan Cheng549f27d32007-08-13 23:45:17 +0000377 void ModifyStackSlotOrReMat(int SlotOrReMat);
Chris Lattner66cf80f2006-02-03 23:13:58 +0000378};
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000379}
Chris Lattner66cf80f2006-02-03 23:13:58 +0000380
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000381/// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified
382/// stackslot register. The register is still available but is no longer
383/// allowed to be modifed.
384void AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) {
385 std::multimap<unsigned, int>::iterator I =
386 PhysRegsAvailable.lower_bound(PhysReg);
387 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000388 int SlotOrReMat = I->second;
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000389 I++;
Evan Cheng549f27d32007-08-13 23:45:17 +0000390 assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000391 "Bidirectional map mismatch!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000392 SpillSlotsOrReMatsAvailable[SlotOrReMat] &= ~1;
Evan Cheng7a0d51c2006-12-14 07:54:05 +0000393 DOUT << "PhysReg " << MRI->getName(PhysReg)
394 << " copied, it is available for use but can no longer be modified\n";
395 }
396}
397
398/// disallowClobberPhysReg - Unset the CanClobber bit of the specified
399/// stackslot register and its aliases. The register and its aliases may
400/// still available but is no longer allowed to be modifed.
401void AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) {
402 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
403 disallowClobberPhysRegOnly(*AS);
404 disallowClobberPhysRegOnly(PhysReg);
405}
406
Chris Lattner66cf80f2006-02-03 23:13:58 +0000407/// ClobberPhysRegOnly - This is called when the specified physreg changes
408/// value. We use this to invalidate any info about stuff we thing lives in it.
409void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) {
410 std::multimap<unsigned, int>::iterator I =
411 PhysRegsAvailable.lower_bound(PhysReg);
Chris Lattner07cf1412006-02-03 00:36:31 +0000412 while (I != PhysRegsAvailable.end() && I->first == PhysReg) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000413 int SlotOrReMat = I->second;
Chris Lattner07cf1412006-02-03 00:36:31 +0000414 PhysRegsAvailable.erase(I++);
Evan Cheng549f27d32007-08-13 23:45:17 +0000415 assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg &&
Chris Lattner66cf80f2006-02-03 23:13:58 +0000416 "Bidirectional map mismatch!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000417 SpillSlotsOrReMatsAvailable.erase(SlotOrReMat);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000418 DOUT << "PhysReg " << MRI->getName(PhysReg)
Evan Cheng2638e1a2007-03-20 08:13:50 +0000419 << " clobbered, invalidating ";
Evan Cheng549f27d32007-08-13 23:45:17 +0000420 if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT)
421 DOUT << "RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1 << "\n";
Evan Cheng2638e1a2007-03-20 08:13:50 +0000422 else
Evan Cheng549f27d32007-08-13 23:45:17 +0000423 DOUT << "SS#" << SlotOrReMat << "\n";
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000424 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000425}
426
Chris Lattner66cf80f2006-02-03 23:13:58 +0000427/// ClobberPhysReg - This is called when the specified physreg changes
428/// value. We use this to invalidate any info about stuff we thing lives in
429/// it and any of its aliases.
430void AvailableSpills::ClobberPhysReg(unsigned PhysReg) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000431 for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS)
Chris Lattner66cf80f2006-02-03 23:13:58 +0000432 ClobberPhysRegOnly(*AS);
433 ClobberPhysRegOnly(PhysReg);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000434}
435
Evan Cheng90a43c32007-08-15 20:20:34 +0000436/// ModifyStackSlotOrReMat - This method is called when the value in a stack
437/// slot changes. This removes information about which register the previous
438/// value for this slot lives in (as the previous value is dead now).
Evan Cheng549f27d32007-08-13 23:45:17 +0000439void AvailableSpills::ModifyStackSlotOrReMat(int SlotOrReMat) {
Evan Cheng90a43c32007-08-15 20:20:34 +0000440 std::map<int, unsigned>::iterator It =
441 SpillSlotsOrReMatsAvailable.find(SlotOrReMat);
Evan Cheng549f27d32007-08-13 23:45:17 +0000442 if (It == SpillSlotsOrReMatsAvailable.end()) return;
Evan Chengb9591c62007-07-11 08:47:44 +0000443 unsigned Reg = It->second >> 1;
Evan Cheng549f27d32007-08-13 23:45:17 +0000444 SpillSlotsOrReMatsAvailable.erase(It);
Chris Lattner07cf1412006-02-03 00:36:31 +0000445
446 // This register may hold the value of multiple stack slots, only remove this
447 // stack slot from the set of values the register contains.
448 std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg);
449 for (; ; ++I) {
450 assert(I != PhysRegsAvailable.end() && I->first == Reg &&
451 "Map inverse broken!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000452 if (I->second == SlotOrReMat) break;
Chris Lattner07cf1412006-02-03 00:36:31 +0000453 }
454 PhysRegsAvailable.erase(I);
455}
456
457
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000458
Evan Cheng28bb4622007-07-11 19:17:18 +0000459/// InvalidateKills - MI is going to be deleted. If any of its operands are
460/// marked kill, then invalidate the information.
461static void InvalidateKills(MachineInstr &MI, BitVector &RegKills,
Evan Chengc91f0b82007-08-14 20:23:13 +0000462 std::vector<MachineOperand*> &KillOps,
Evan Cheng66f71632007-10-19 21:23:22 +0000463 SmallVector<unsigned, 2> *KillRegs = NULL) {
Evan Cheng28bb4622007-07-11 19:17:18 +0000464 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
465 MachineOperand &MO = MI.getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000466 if (!MO.isRegister() || !MO.isUse() || !MO.isKill())
Evan Cheng28bb4622007-07-11 19:17:18 +0000467 continue;
468 unsigned Reg = MO.getReg();
Evan Chengb6ca4b32007-08-14 23:25:37 +0000469 if (KillRegs)
470 KillRegs->push_back(Reg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000471 if (KillOps[Reg] == &MO) {
472 RegKills.reset(Reg);
473 KillOps[Reg] = NULL;
474 }
475 }
476}
477
Evan Cheng39c883c2007-12-11 23:36:57 +0000478/// InvalidateKill - A MI that defines the specified register is being deleted,
479/// invalidate the register kill information.
480static void InvalidateKill(unsigned Reg, BitVector &RegKills,
481 std::vector<MachineOperand*> &KillOps) {
482 if (RegKills[Reg]) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000483 KillOps[Reg]->setIsKill(false);
Evan Cheng39c883c2007-12-11 23:36:57 +0000484 KillOps[Reg] = NULL;
485 RegKills.reset(Reg);
486 }
487}
488
Evan Chengb6ca4b32007-08-14 23:25:37 +0000489/// InvalidateRegDef - If the def operand of the specified def MI is now dead
490/// (since it's spill instruction is removed), mark it isDead. Also checks if
491/// the def MI has other definition operands that are not dead. Returns it by
492/// reference.
493static bool InvalidateRegDef(MachineBasicBlock::iterator I,
494 MachineInstr &NewDef, unsigned Reg,
495 bool &HasLiveDef) {
496 // Due to remat, it's possible this reg isn't being reused. That is,
497 // the def of this reg (by prev MI) is now dead.
498 MachineInstr *DefMI = I;
499 MachineOperand *DefOp = NULL;
500 for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) {
501 MachineOperand &MO = DefMI->getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000502 if (MO.isRegister() && MO.isDef()) {
Evan Chengb6ca4b32007-08-14 23:25:37 +0000503 if (MO.getReg() == Reg)
504 DefOp = &MO;
505 else if (!MO.isDead())
506 HasLiveDef = true;
507 }
508 }
509 if (!DefOp)
510 return false;
511
512 bool FoundUse = false, Done = false;
513 MachineBasicBlock::iterator E = NewDef;
514 ++I; ++E;
515 for (; !Done && I != E; ++I) {
516 MachineInstr *NMI = I;
517 for (unsigned j = 0, ee = NMI->getNumOperands(); j != ee; ++j) {
518 MachineOperand &MO = NMI->getOperand(j);
Dan Gohman92dfe202007-09-14 20:33:02 +0000519 if (!MO.isRegister() || MO.getReg() != Reg)
Evan Chengb6ca4b32007-08-14 23:25:37 +0000520 continue;
521 if (MO.isUse())
522 FoundUse = true;
523 Done = true; // Stop after scanning all the operands of this MI.
524 }
525 }
526 if (!FoundUse) {
527 // Def is dead!
528 DefOp->setIsDead();
529 return true;
530 }
531 return false;
532}
533
Evan Cheng28bb4622007-07-11 19:17:18 +0000534/// UpdateKills - Track and update kill info. If a MI reads a register that is
535/// marked kill, then it must be due to register reuse. Transfer the kill info
536/// over.
537static void UpdateKills(MachineInstr &MI, BitVector &RegKills,
538 std::vector<MachineOperand*> &KillOps) {
539 const TargetInstrDescriptor *TID = MI.getInstrDescriptor();
540 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
541 MachineOperand &MO = MI.getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000542 if (!MO.isRegister() || !MO.isUse())
Evan Cheng28bb4622007-07-11 19:17:18 +0000543 continue;
544 unsigned Reg = MO.getReg();
545 if (Reg == 0)
546 continue;
547
548 if (RegKills[Reg]) {
549 // That can't be right. Register is killed but not re-defined and it's
550 // being reused. Let's fix that.
Chris Lattnerf7382302007-12-30 21:56:09 +0000551 KillOps[Reg]->setIsKill(false);
Evan Cheng39c883c2007-12-11 23:36:57 +0000552 KillOps[Reg] = NULL;
553 RegKills.reset(Reg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000554 if (i < TID->numOperands &&
555 TID->getOperandConstraint(i, TOI::TIED_TO) == -1)
556 // Unless it's a two-address operand, this is the new kill.
557 MO.setIsKill();
558 }
Evan Cheng28bb4622007-07-11 19:17:18 +0000559 if (MO.isKill()) {
560 RegKills.set(Reg);
561 KillOps[Reg] = &MO;
562 }
563 }
564
565 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
566 const MachineOperand &MO = MI.getOperand(i);
Dan Gohman92dfe202007-09-14 20:33:02 +0000567 if (!MO.isRegister() || !MO.isDef())
Evan Cheng28bb4622007-07-11 19:17:18 +0000568 continue;
569 unsigned Reg = MO.getReg();
570 RegKills.reset(Reg);
571 KillOps[Reg] = NULL;
572 }
573}
574
575
Chris Lattner7fb64342004-10-01 19:04:51 +0000576// ReusedOp - For each reused operand, we keep track of a bit of information, in
577// case we need to rollback upon processing a new operand. See comments below.
578namespace {
579 struct ReusedOp {
580 // The MachineInstr operand that reused an available value.
581 unsigned Operand;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000582
Evan Cheng549f27d32007-08-13 23:45:17 +0000583 // StackSlotOrReMat - The spill slot or remat id of the value being reused.
584 unsigned StackSlotOrReMat;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000585
Chris Lattner7fb64342004-10-01 19:04:51 +0000586 // PhysRegReused - The physical register the value was available in.
587 unsigned PhysRegReused;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000588
Chris Lattner7fb64342004-10-01 19:04:51 +0000589 // AssignedPhysReg - The physreg that was assigned for use by the reload.
590 unsigned AssignedPhysReg;
Chris Lattner8a61a752005-10-06 17:19:06 +0000591
592 // VirtReg - The virtual register itself.
593 unsigned VirtReg;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000594
Chris Lattner8a61a752005-10-06 17:19:06 +0000595 ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr,
596 unsigned vreg)
Evan Cheng90a43c32007-08-15 20:20:34 +0000597 : Operand(o), StackSlotOrReMat(ss), PhysRegReused(prr),
598 AssignedPhysReg(apr), VirtReg(vreg) {}
Chris Lattner7fb64342004-10-01 19:04:51 +0000599 };
Chris Lattner540fec62006-02-25 01:51:33 +0000600
601 /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that
602 /// is reused instead of reloaded.
Chris Lattnerf8c68f62006-06-28 22:17:39 +0000603 class VISIBILITY_HIDDEN ReuseInfo {
Chris Lattner540fec62006-02-25 01:51:33 +0000604 MachineInstr &MI;
605 std::vector<ReusedOp> Reuses;
Evan Cheng957840b2007-02-21 02:22:03 +0000606 BitVector PhysRegsClobbered;
Chris Lattner540fec62006-02-25 01:51:33 +0000607 public:
Evan Chenge077ef62006-11-04 00:21:55 +0000608 ReuseInfo(MachineInstr &mi, const MRegisterInfo *mri) : MI(mi) {
Evan Cheng957840b2007-02-21 02:22:03 +0000609 PhysRegsClobbered.resize(mri->getNumRegs());
Evan Chenge077ef62006-11-04 00:21:55 +0000610 }
Chris Lattner540fec62006-02-25 01:51:33 +0000611
612 bool hasReuses() const {
613 return !Reuses.empty();
614 }
615
616 /// addReuse - If we choose to reuse a virtual register that is already
617 /// available instead of reloading it, remember that we did so.
Evan Cheng549f27d32007-08-13 23:45:17 +0000618 void addReuse(unsigned OpNo, unsigned StackSlotOrReMat,
Chris Lattner540fec62006-02-25 01:51:33 +0000619 unsigned PhysRegReused, unsigned AssignedPhysReg,
620 unsigned VirtReg) {
621 // If the reload is to the assigned register anyway, no undo will be
622 // required.
623 if (PhysRegReused == AssignedPhysReg) return;
624
625 // Otherwise, remember this.
Evan Cheng549f27d32007-08-13 23:45:17 +0000626 Reuses.push_back(ReusedOp(OpNo, StackSlotOrReMat, PhysRegReused,
Chris Lattner540fec62006-02-25 01:51:33 +0000627 AssignedPhysReg, VirtReg));
628 }
Evan Chenge077ef62006-11-04 00:21:55 +0000629
630 void markClobbered(unsigned PhysReg) {
Evan Cheng957840b2007-02-21 02:22:03 +0000631 PhysRegsClobbered.set(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +0000632 }
633
634 bool isClobbered(unsigned PhysReg) const {
Evan Cheng957840b2007-02-21 02:22:03 +0000635 return PhysRegsClobbered.test(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +0000636 }
Chris Lattner540fec62006-02-25 01:51:33 +0000637
638 /// GetRegForReload - We are about to emit a reload into PhysReg. If there
639 /// is some other operand that is using the specified register, either pick
640 /// a new register to use, or evict the previous reload and use this reg.
641 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
642 AvailableSpills &Spills,
Evan Chengfff3e192007-08-14 09:11:18 +0000643 std::vector<MachineInstr*> &MaybeDeadStores,
Evan Cheng28bb4622007-07-11 19:17:18 +0000644 SmallSet<unsigned, 8> &Rejected,
645 BitVector &RegKills,
Evan Cheng549f27d32007-08-13 23:45:17 +0000646 std::vector<MachineOperand*> &KillOps,
647 VirtRegMap &VRM) {
Chris Lattner540fec62006-02-25 01:51:33 +0000648 if (Reuses.empty()) return PhysReg; // This is most often empty.
649
650 for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) {
651 ReusedOp &Op = Reuses[ro];
652 // If we find some other reuse that was supposed to use this register
653 // exactly for its reload, we can change this reload to use ITS reload
Evan Cheng3c82cab2007-01-19 22:40:14 +0000654 // register. That is, unless its reload register has already been
655 // considered and subsequently rejected because it has also been reused
656 // by another operand.
657 if (Op.PhysRegReused == PhysReg &&
658 Rejected.count(Op.AssignedPhysReg) == 0) {
Chris Lattner540fec62006-02-25 01:51:33 +0000659 // Yup, use the reload register that we didn't use before.
Evan Cheng3c82cab2007-01-19 22:40:14 +0000660 unsigned NewReg = Op.AssignedPhysReg;
661 Rejected.insert(PhysReg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000662 return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores, Rejected,
Evan Cheng549f27d32007-08-13 23:45:17 +0000663 RegKills, KillOps, VRM);
Chris Lattner540fec62006-02-25 01:51:33 +0000664 } else {
665 // Otherwise, we might also have a problem if a previously reused
666 // value aliases the new register. If so, codegen the previous reload
667 // and use this one.
668 unsigned PRRU = Op.PhysRegReused;
669 const MRegisterInfo *MRI = Spills.getRegInfo();
670 if (MRI->areAliases(PRRU, PhysReg)) {
671 // Okay, we found out that an alias of a reused register
672 // was used. This isn't good because it means we have
673 // to undo a previous reuse.
674 MachineBasicBlock *MBB = MI->getParent();
675 const TargetRegisterClass *AliasRC =
Chris Lattner84bc5422007-12-31 04:13:23 +0000676 MBB->getParent()->getRegInfo().getRegClass(Op.VirtReg);
Chris Lattner28bad082006-02-25 02:17:31 +0000677
678 // Copy Op out of the vector and remove it, we're going to insert an
679 // explicit load for it.
680 ReusedOp NewOp = Op;
681 Reuses.erase(Reuses.begin()+ro);
682
683 // Ok, we're going to try to reload the assigned physreg into the
684 // slot that we were supposed to in the first place. However, that
685 // register could hold a reuse. Check to see if it conflicts or
686 // would prefer us to use a different register.
687 unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg,
Evan Cheng28bb4622007-07-11 19:17:18 +0000688 MI, Spills, MaybeDeadStores,
Evan Cheng549f27d32007-08-13 23:45:17 +0000689 Rejected, RegKills, KillOps, VRM);
Chris Lattner28bad082006-02-25 02:17:31 +0000690
Evan Cheng549f27d32007-08-13 23:45:17 +0000691 if (NewOp.StackSlotOrReMat > VirtRegMap::MAX_STACK_SLOT) {
692 MRI->reMaterialize(*MBB, MI, NewPhysReg,
693 VRM.getReMaterializedMI(NewOp.VirtReg));
694 ++NumReMats;
695 } else {
696 MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg,
697 NewOp.StackSlotOrReMat, AliasRC);
Evan Chengfff3e192007-08-14 09:11:18 +0000698 // Any stores to this stack slot are not dead anymore.
699 MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL;
Evan Cheng549f27d32007-08-13 23:45:17 +0000700 ++NumLoads;
701 }
Chris Lattner28bad082006-02-25 02:17:31 +0000702 Spills.ClobberPhysReg(NewPhysReg);
703 Spills.ClobberPhysReg(NewOp.PhysRegReused);
Chris Lattner540fec62006-02-25 01:51:33 +0000704
Chris Lattnere53f4a02006-05-04 17:52:23 +0000705 MI->getOperand(NewOp.Operand).setReg(NewPhysReg);
Chris Lattner540fec62006-02-25 01:51:33 +0000706
Evan Cheng549f27d32007-08-13 23:45:17 +0000707 Spills.addAvailable(NewOp.StackSlotOrReMat, MI, NewPhysReg);
Evan Cheng28bb4622007-07-11 19:17:18 +0000708 MachineBasicBlock::iterator MII = MI;
709 --MII;
710 UpdateKills(*MII, RegKills, KillOps);
711 DOUT << '\t' << *MII;
Chris Lattner540fec62006-02-25 01:51:33 +0000712
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000713 DOUT << "Reuse undone!\n";
Chris Lattner540fec62006-02-25 01:51:33 +0000714 --NumReused;
Chris Lattner28bad082006-02-25 02:17:31 +0000715
716 // Finally, PhysReg is now available, go ahead and use it.
Chris Lattner540fec62006-02-25 01:51:33 +0000717 return PhysReg;
718 }
719 }
720 }
721 return PhysReg;
722 }
Evan Cheng3c82cab2007-01-19 22:40:14 +0000723
724 /// GetRegForReload - Helper for the above GetRegForReload(). Add a
725 /// 'Rejected' set to remember which registers have been considered and
726 /// rejected for the reload. This avoids infinite looping in case like
727 /// this:
728 /// t1 := op t2, t3
729 /// t2 <- assigned r0 for use by the reload but ended up reuse r1
730 /// t3 <- assigned r1 for use by the reload but ended up reuse r0
731 /// t1 <- desires r1
732 /// sees r1 is taken by t2, tries t2's reload register r0
733 /// sees r0 is taken by t3, tries t3's reload register r1
734 /// sees r1 is taken by t2, tries t2's reload register r0 ...
735 unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI,
736 AvailableSpills &Spills,
Evan Chengfff3e192007-08-14 09:11:18 +0000737 std::vector<MachineInstr*> &MaybeDeadStores,
Evan Cheng28bb4622007-07-11 19:17:18 +0000738 BitVector &RegKills,
Evan Cheng549f27d32007-08-13 23:45:17 +0000739 std::vector<MachineOperand*> &KillOps,
740 VirtRegMap &VRM) {
Chris Lattner08a4d5a2007-01-23 00:59:48 +0000741 SmallSet<unsigned, 8> Rejected;
Evan Cheng28bb4622007-07-11 19:17:18 +0000742 return GetRegForReload(PhysReg, MI, Spills, MaybeDeadStores, Rejected,
Evan Cheng549f27d32007-08-13 23:45:17 +0000743 RegKills, KillOps, VRM);
Evan Cheng3c82cab2007-01-19 22:40:14 +0000744 }
Chris Lattner540fec62006-02-25 01:51:33 +0000745 };
Chris Lattner7fb64342004-10-01 19:04:51 +0000746}
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000747
Evan Cheng66f71632007-10-19 21:23:22 +0000748/// PrepForUnfoldOpti - Turn a store folding instruction into a load folding
749/// instruction. e.g.
750/// xorl %edi, %eax
751/// movl %eax, -32(%ebp)
752/// movl -36(%ebp), %eax
753/// orl %eax, -32(%ebp)
754/// ==>
755/// xorl %edi, %eax
756/// orl -36(%ebp), %eax
757/// mov %eax, -32(%ebp)
758/// This enables unfolding optimization for a subsequent instruction which will
759/// also eliminate the newly introduced store instruction.
760bool LocalSpiller::PrepForUnfoldOpti(MachineBasicBlock &MBB,
761 MachineBasicBlock::iterator &MII,
762 std::vector<MachineInstr*> &MaybeDeadStores,
763 AvailableSpills &Spills,
764 BitVector &RegKills,
765 std::vector<MachineOperand*> &KillOps,
766 VirtRegMap &VRM) {
767 MachineFunction &MF = *MBB.getParent();
768 MachineInstr &MI = *MII;
769 unsigned UnfoldedOpc = 0;
770 unsigned UnfoldPR = 0;
771 unsigned UnfoldVR = 0;
772 int FoldedSS = VirtRegMap::NO_STACK_SLOT;
773 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
774 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
775 // Only transform a MI that folds a single register.
776 if (UnfoldedOpc)
777 return false;
778 UnfoldVR = I->second.first;
779 VirtRegMap::ModRef MR = I->second.second;
780 if (VRM.isAssignedReg(UnfoldVR))
781 continue;
782 // If this reference is not a use, any previous store is now dead.
783 // Otherwise, the store to this stack slot is not dead anymore.
784 FoldedSS = VRM.getStackSlot(UnfoldVR);
785 MachineInstr* DeadStore = MaybeDeadStores[FoldedSS];
786 if (DeadStore && (MR & VirtRegMap::isModRef)) {
787 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(FoldedSS);
788 if (!PhysReg ||
789 DeadStore->findRegisterUseOperandIdx(PhysReg, true) == -1)
790 continue;
791 UnfoldPR = PhysReg;
792 UnfoldedOpc = MRI->getOpcodeAfterMemoryUnfold(MI.getOpcode(),
793 false, true);
794 }
795 }
796
797 if (!UnfoldedOpc)
798 return false;
799
800 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
801 MachineOperand &MO = MI.getOperand(i);
802 if (!MO.isRegister() || MO.getReg() == 0 || !MO.isUse())
803 continue;
804 unsigned VirtReg = MO.getReg();
Evan Chengc498b022007-11-14 07:59:08 +0000805 if (MRegisterInfo::isPhysicalRegister(VirtReg) || MO.getSubReg())
Evan Cheng66f71632007-10-19 21:23:22 +0000806 continue;
807 if (VRM.isAssignedReg(VirtReg)) {
808 unsigned PhysReg = VRM.getPhys(VirtReg);
809 if (PhysReg && MRI->regsOverlap(PhysReg, UnfoldPR))
810 return false;
811 } else if (VRM.isReMaterialized(VirtReg))
812 continue;
813 int SS = VRM.getStackSlot(VirtReg);
814 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
815 if (PhysReg) {
816 if (MRI->regsOverlap(PhysReg, UnfoldPR))
817 return false;
818 continue;
819 }
820 PhysReg = VRM.getPhys(VirtReg);
821 if (!MRI->regsOverlap(PhysReg, UnfoldPR))
822 continue;
823
824 // Ok, we'll need to reload the value into a register which makes
825 // it impossible to perform the store unfolding optimization later.
826 // Let's see if it is possible to fold the load if the store is
827 // unfolded. This allows us to perform the store unfolding
828 // optimization.
829 SmallVector<MachineInstr*, 4> NewMIs;
830 if (MRI->unfoldMemoryOperand(MF, &MI, UnfoldVR, false, false, NewMIs)) {
831 assert(NewMIs.size() == 1);
832 MachineInstr *NewMI = NewMIs.back();
833 NewMIs.clear();
Evan Cheng81a03822007-11-17 00:40:40 +0000834 int Idx = NewMI->findRegisterUseOperandIdx(VirtReg);
835 assert(Idx != -1);
Evan Chengaee4af62007-12-02 08:30:39 +0000836 SmallVector<unsigned, 2> Ops;
837 Ops.push_back(Idx);
838 MachineInstr *FoldedMI = MRI->foldMemoryOperand(NewMI, Ops, SS);
Evan Cheng66f71632007-10-19 21:23:22 +0000839 if (FoldedMI) {
Evan Chengcbfb9b22007-10-22 03:01:44 +0000840 if (!VRM.hasPhys(UnfoldVR))
Evan Cheng66f71632007-10-19 21:23:22 +0000841 VRM.assignVirt2Phys(UnfoldVR, UnfoldPR);
Evan Cheng66f71632007-10-19 21:23:22 +0000842 VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
843 MII = MBB.insert(MII, FoldedMI);
Evan Chengcada2452007-11-28 01:28:46 +0000844 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng66f71632007-10-19 21:23:22 +0000845 MBB.erase(&MI);
846 return true;
847 }
848 delete NewMI;
849 }
850 }
851 return false;
852}
Chris Lattner7fb64342004-10-01 19:04:51 +0000853
Evan Cheng7277a7d2007-11-02 17:35:08 +0000854/// findSuperReg - Find the SubReg's super-register of given register class
855/// where its SubIdx sub-register is SubReg.
856static unsigned findSuperReg(const TargetRegisterClass *RC, unsigned SubReg,
857 unsigned SubIdx, const MRegisterInfo *MRI) {
858 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
859 I != E; ++I) {
860 unsigned Reg = *I;
861 if (MRI->getSubReg(Reg, SubIdx) == SubReg)
862 return Reg;
863 }
864 return 0;
865}
866
Evan Cheng81a03822007-11-17 00:40:40 +0000867/// SpillRegToStackSlot - Spill a register to a specified stack slot. Check if
868/// the last store to the same slot is now dead. If so, remove the last store.
869void LocalSpiller::SpillRegToStackSlot(MachineBasicBlock &MBB,
870 MachineBasicBlock::iterator &MII,
871 int Idx, unsigned PhysReg, int StackSlot,
872 const TargetRegisterClass *RC,
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000873 bool isAvailable, MachineInstr *&LastStore,
Evan Cheng81a03822007-11-17 00:40:40 +0000874 AvailableSpills &Spills,
875 SmallSet<MachineInstr*, 4> &ReMatDefs,
876 BitVector &RegKills,
877 std::vector<MachineOperand*> &KillOps,
Evan Chenge4b39002007-12-03 21:31:55 +0000878 VirtRegMap &VRM) {
Evan Chengd64b5c82007-12-05 03:14:33 +0000879 MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, true, StackSlot, RC);
Evan Cheng81a03822007-11-17 00:40:40 +0000880 DOUT << "Store:\t" << *next(MII);
881
882 // If there is a dead store to this stack slot, nuke it now.
883 if (LastStore) {
884 DOUT << "Removed dead store:\t" << *LastStore;
885 ++NumDSE;
886 SmallVector<unsigned, 2> KillRegs;
887 InvalidateKills(*LastStore, RegKills, KillOps, &KillRegs);
888 MachineBasicBlock::iterator PrevMII = LastStore;
889 bool CheckDef = PrevMII != MBB.begin();
890 if (CheckDef)
891 --PrevMII;
892 MBB.erase(LastStore);
Evan Chengcada2452007-11-28 01:28:46 +0000893 VRM.RemoveMachineInstrFromMaps(LastStore);
Evan Cheng81a03822007-11-17 00:40:40 +0000894 if (CheckDef) {
895 // Look at defs of killed registers on the store. Mark the defs
896 // as dead since the store has been deleted and they aren't
897 // being reused.
898 for (unsigned j = 0, ee = KillRegs.size(); j != ee; ++j) {
899 bool HasOtherDef = false;
900 if (InvalidateRegDef(PrevMII, *MII, KillRegs[j], HasOtherDef)) {
901 MachineInstr *DeadDef = PrevMII;
902 if (ReMatDefs.count(DeadDef) && !HasOtherDef) {
903 // FIXME: This assumes a remat def does not have side
904 // effects.
905 MBB.erase(DeadDef);
Evan Chengcada2452007-11-28 01:28:46 +0000906 VRM.RemoveMachineInstrFromMaps(DeadDef);
Evan Cheng81a03822007-11-17 00:40:40 +0000907 ++NumDRM;
908 }
909 }
910 }
911 }
912 }
913
Evan Chenge4b39002007-12-03 21:31:55 +0000914 LastStore = next(MII);
Evan Cheng81a03822007-11-17 00:40:40 +0000915
916 // If the stack slot value was previously available in some other
917 // register, change it now. Otherwise, make the register available,
918 // in PhysReg.
919 Spills.ModifyStackSlotOrReMat(StackSlot);
920 Spills.ClobberPhysReg(PhysReg);
Evan Cheng35a3e4a2007-12-04 19:19:45 +0000921 Spills.addAvailable(StackSlot, LastStore, PhysReg, isAvailable);
Evan Cheng81a03822007-11-17 00:40:40 +0000922 ++NumStores;
923}
924
Chris Lattner7fb64342004-10-01 19:04:51 +0000925/// rewriteMBB - Keep track of which spills are available even after the
Evan Cheng81a03822007-11-17 00:40:40 +0000926/// register allocator is done with them. If possible, avid reloading vregs.
Evan Cheng549f27d32007-08-13 23:45:17 +0000927void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000928 DOUT << MBB.getBasicBlock()->getName() << ":\n";
Chris Lattner7fb64342004-10-01 19:04:51 +0000929
Evan Chengfff3e192007-08-14 09:11:18 +0000930 MachineFunction &MF = *MBB.getParent();
931
Chris Lattner66cf80f2006-02-03 23:13:58 +0000932 // Spills - Keep track of which spilled values are available in physregs so
933 // that we can choose to reuse the physregs instead of emitting reloads.
934 AvailableSpills Spills(MRI, TII);
935
Chris Lattner52b25db2004-10-01 19:47:12 +0000936 // MaybeDeadStores - When we need to write a value back into a stack slot,
937 // keep track of the inserted store. If the stack slot value is never read
938 // (because the value was used from some available register, for example), and
939 // subsequently stored to, the original store is dead. This map keeps track
940 // of inserted stores that are not used. If we see a subsequent store to the
941 // same stack slot, the original store is deleted.
Evan Chengfff3e192007-08-14 09:11:18 +0000942 std::vector<MachineInstr*> MaybeDeadStores;
943 MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL);
Chris Lattner52b25db2004-10-01 19:47:12 +0000944
Evan Chengb6ca4b32007-08-14 23:25:37 +0000945 // ReMatDefs - These are rematerializable def MIs which are not deleted.
946 SmallSet<MachineInstr*, 4> ReMatDefs;
947
Evan Cheng0c40d722007-07-11 05:28:39 +0000948 // Keep track of kill information.
949 BitVector RegKills(MRI->getNumRegs());
950 std::vector<MachineOperand*> KillOps;
951 KillOps.resize(MRI->getNumRegs(), NULL);
952
Chris Lattner7fb64342004-10-01 19:04:51 +0000953 for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
954 MII != E; ) {
Chris Lattner7fb64342004-10-01 19:04:51 +0000955 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
Evan Cheng0c40d722007-07-11 05:28:39 +0000956
Evan Cheng66f71632007-10-19 21:23:22 +0000957 VirtRegMap::MI2VirtMapTy::const_iterator I, End;
Evan Cheng0c40d722007-07-11 05:28:39 +0000958 bool Erased = false;
959 bool BackTracked = false;
Evan Cheng66f71632007-10-19 21:23:22 +0000960 if (PrepForUnfoldOpti(MBB, MII,
961 MaybeDeadStores, Spills, RegKills, KillOps, VRM))
962 NextMII = next(MII);
Chris Lattner7fb64342004-10-01 19:04:51 +0000963
Evan Cheng66f71632007-10-19 21:23:22 +0000964 MachineInstr &MI = *MII;
Evan Cheng86facc22006-12-15 06:41:01 +0000965 const TargetInstrDescriptor *TID = MI.getInstrDescriptor();
Evan Chenge077ef62006-11-04 00:21:55 +0000966
Evan Cheng0cbb1162007-11-29 01:06:25 +0000967 // Insert restores here if asked to.
968 if (VRM.isRestorePt(&MI)) {
969 std::vector<unsigned> &RestoreRegs = VRM.getRestorePtRestores(&MI);
970 for (unsigned i = 0, e = RestoreRegs.size(); i != e; ++i) {
971 unsigned VirtReg = RestoreRegs[i];
972 if (!VRM.getPreSplitReg(VirtReg))
973 continue; // Split interval spilled again.
974 unsigned Phys = VRM.getPhys(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +0000975 RegInfo->setPhysRegUsed(Phys);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000976 if (VRM.isReMaterialized(VirtReg)) {
977 MRI->reMaterialize(MBB, &MI, Phys,
978 VRM.getReMaterializedMI(VirtReg));
979 ++NumReMats;
980 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +0000981 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
982 MRI->loadRegFromStackSlot(MBB, &MI, Phys, VRM.getStackSlot(VirtReg),
983 RC);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000984 ++NumLoads;
985 }
986 // This invalidates Phys.
987 Spills.ClobberPhysReg(Phys);
988 UpdateKills(*prior(MII), RegKills, KillOps);
989 DOUT << '\t' << *prior(MII);
990 }
991 }
992
Evan Cheng81a03822007-11-17 00:40:40 +0000993 // Insert spills here if asked to.
Evan Chengcada2452007-11-28 01:28:46 +0000994 if (VRM.isSpillPt(&MI)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000995 std::vector<std::pair<unsigned,bool> > &SpillRegs =
996 VRM.getSpillPtSpills(&MI);
Evan Chengcada2452007-11-28 01:28:46 +0000997 for (unsigned i = 0, e = SpillRegs.size(); i != e; ++i) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000998 unsigned VirtReg = SpillRegs[i].first;
999 bool isKill = SpillRegs[i].second;
Evan Chengcada2452007-11-28 01:28:46 +00001000 if (!VRM.getPreSplitReg(VirtReg))
1001 continue; // Split interval spilled again.
Chris Lattner84bc5422007-12-31 04:13:23 +00001002 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Evan Chengcada2452007-11-28 01:28:46 +00001003 unsigned Phys = VRM.getPhys(VirtReg);
1004 int StackSlot = VRM.getStackSlot(VirtReg);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001005 MRI->storeRegToStackSlot(MBB, next(MII), Phys, isKill, StackSlot, RC);
Evan Chengd64b5c82007-12-05 03:14:33 +00001006 MachineInstr *StoreMI = next(MII);
1007 DOUT << "Store:\t" << StoreMI;
1008 VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod);
Evan Chengcada2452007-11-28 01:28:46 +00001009 }
Evan Chenge4b39002007-12-03 21:31:55 +00001010 NextMII = next(MII);
Evan Cheng81a03822007-11-17 00:40:40 +00001011 }
1012
1013 /// ReusedOperands - Keep track of operand reuse in case we need to undo
1014 /// reuse.
1015 ReuseInfo ReusedOperands(MI, MRI);
Chris Lattner7fb64342004-10-01 19:04:51 +00001016 // Process all of the spilled uses and all non spilled reg references.
1017 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1018 MachineOperand &MO = MI.getOperand(i);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001019 if (!MO.isRegister() || MO.getReg() == 0)
1020 continue; // Ignore non-register operands.
1021
Evan Cheng32dfbea2007-10-12 08:50:34 +00001022 unsigned VirtReg = MO.getReg();
1023 if (MRegisterInfo::isPhysicalRegister(VirtReg)) {
Chris Lattner50ea01e2005-09-09 20:29:51 +00001024 // Ignore physregs for spilling, but remember that it is used by this
1025 // function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001026 RegInfo->setPhysRegUsed(VirtReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001027 continue;
1028 }
1029
Evan Cheng32dfbea2007-10-12 08:50:34 +00001030 assert(MRegisterInfo::isVirtualRegister(VirtReg) &&
Chris Lattner50ea01e2005-09-09 20:29:51 +00001031 "Not a virtual or a physical register?");
Evan Cheng70306f82007-12-03 09:58:48 +00001032
Evan Chengc498b022007-11-14 07:59:08 +00001033 unsigned SubIdx = MO.getSubReg();
Evan Cheng549f27d32007-08-13 23:45:17 +00001034 if (VRM.isAssignedReg(VirtReg)) {
Chris Lattner50ea01e2005-09-09 20:29:51 +00001035 // This virtual register was assigned a physreg!
1036 unsigned Phys = VRM.getPhys(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001037 RegInfo->setPhysRegUsed(Phys);
Evan Chenge077ef62006-11-04 00:21:55 +00001038 if (MO.isDef())
1039 ReusedOperands.markClobbered(Phys);
Evan Chengc498b022007-11-14 07:59:08 +00001040 unsigned RReg = SubIdx ? MRI->getSubReg(Phys, SubIdx) : Phys;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001041 MI.getOperand(i).setReg(RReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001042 continue;
1043 }
1044
1045 // This virtual register is now known to be a spilled value.
1046 if (!MO.isUse())
1047 continue; // Handle defs in the loop below (handle use&def here though)
Chris Lattner7fb64342004-10-01 19:04:51 +00001048
Evan Cheng549f27d32007-08-13 23:45:17 +00001049 bool DoReMat = VRM.isReMaterialized(VirtReg);
1050 int SSorRMId = DoReMat
1051 ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg);
Evan Chengdc6be192007-08-14 05:42:54 +00001052 int ReuseSlot = SSorRMId;
Chris Lattner7fb64342004-10-01 19:04:51 +00001053
Chris Lattner50ea01e2005-09-09 20:29:51 +00001054 // Check to see if this stack slot is available.
Evan Chengdc6be192007-08-14 05:42:54 +00001055 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001056
1057 // If this is a sub-register use, make sure the reuse register is in the
1058 // right register class. For example, for x86 not all of the 32-bit
1059 // registers have accessible sub-registers.
1060 // Similarly so for EXTRACT_SUBREG. Consider this:
1061 // EDI = op
1062 // MOV32_mr fi#1, EDI
1063 // ...
1064 // = EXTRACT_SUBREG fi#1
1065 // fi#1 is available in EDI, but it cannot be reused because it's not in
1066 // the right register file.
1067 if (PhysReg &&
Evan Chengc498b022007-11-14 07:59:08 +00001068 (SubIdx || MI.getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001069 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001070 if (!RC->contains(PhysReg))
1071 PhysReg = 0;
1072 }
1073
Evan Chengdc6be192007-08-14 05:42:54 +00001074 if (PhysReg) {
Chris Lattner29268692006-09-05 02:12:02 +00001075 // This spilled operand might be part of a two-address operand. If this
1076 // is the case, then changing it will necessarily require changing the
1077 // def part of the instruction as well. However, in some cases, we
1078 // aren't allowed to modify the reused register. If none of these cases
1079 // apply, reuse it.
1080 bool CanReuse = true;
Evan Cheng86facc22006-12-15 06:41:01 +00001081 int ti = TID->getOperandConstraint(i, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +00001082 if (ti != -1 &&
Dan Gohman92dfe202007-09-14 20:33:02 +00001083 MI.getOperand(ti).isRegister() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +00001084 MI.getOperand(ti).getReg() == VirtReg) {
Chris Lattner29268692006-09-05 02:12:02 +00001085 // Okay, we have a two address operand. We can reuse this physreg as
Evan Cheng3c82cab2007-01-19 22:40:14 +00001086 // long as we are allowed to clobber the value and there isn't an
1087 // earlier def that has already clobbered the physreg.
Evan Chengdc6be192007-08-14 05:42:54 +00001088 CanReuse = Spills.canClobberPhysReg(ReuseSlot) &&
Evan Chenge077ef62006-11-04 00:21:55 +00001089 !ReusedOperands.isClobbered(PhysReg);
Chris Lattner29268692006-09-05 02:12:02 +00001090 }
1091
1092 if (CanReuse) {
Chris Lattneraddc55a2006-04-28 01:46:50 +00001093 // If this stack slot value is already available, reuse it!
Evan Chengdc6be192007-08-14 05:42:54 +00001094 if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1095 DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001096 else
Evan Chengdc6be192007-08-14 05:42:54 +00001097 DOUT << "Reusing SS#" << ReuseSlot;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001098 DOUT << " from physreg "
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001099 << MRI->getName(PhysReg) << " for vreg"
1100 << VirtReg <<" instead of reloading into physreg "
1101 << MRI->getName(VRM.getPhys(VirtReg)) << "\n";
Evan Chengc498b022007-11-14 07:59:08 +00001102 unsigned RReg = SubIdx ? MRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001103 MI.getOperand(i).setReg(RReg);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001104
1105 // The only technical detail we have is that we don't know that
1106 // PhysReg won't be clobbered by a reloaded stack slot that occurs
1107 // later in the instruction. In particular, consider 'op V1, V2'.
1108 // If V1 is available in physreg R0, we would choose to reuse it
1109 // here, instead of reloading it into the register the allocator
1110 // indicated (say R1). However, V2 might have to be reloaded
1111 // later, and it might indicate that it needs to live in R0. When
1112 // this occurs, we need to have information available that
1113 // indicates it is safe to use R1 for the reload instead of R0.
1114 //
1115 // To further complicate matters, we might conflict with an alias,
1116 // or R0 and R1 might not be compatible with each other. In this
1117 // case, we actually insert a reload for V1 in R1, ensuring that
1118 // we can get at R0 or its alias.
Evan Chengdc6be192007-08-14 05:42:54 +00001119 ReusedOperands.addReuse(i, ReuseSlot, PhysReg,
Chris Lattneraddc55a2006-04-28 01:46:50 +00001120 VRM.getPhys(VirtReg), VirtReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001121 if (ti != -1)
1122 // Only mark it clobbered if this is a use&def operand.
1123 ReusedOperands.markClobbered(PhysReg);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001124 ++NumReused;
Evan Chengfff3e192007-08-14 09:11:18 +00001125
1126 if (MI.getOperand(i).isKill() &&
1127 ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) {
1128 // This was the last use and the spilled value is still available
1129 // for reuse. That means the spill was unnecessary!
1130 MachineInstr* DeadStore = MaybeDeadStores[ReuseSlot];
1131 if (DeadStore) {
1132 DOUT << "Removed dead store:\t" << *DeadStore;
1133 InvalidateKills(*DeadStore, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001134 VRM.RemoveMachineInstrFromMaps(DeadStore);
Evan Cheng66f71632007-10-19 21:23:22 +00001135 MBB.erase(DeadStore);
Evan Chengfff3e192007-08-14 09:11:18 +00001136 MaybeDeadStores[ReuseSlot] = NULL;
1137 ++NumDSE;
1138 }
1139 }
Chris Lattneraddc55a2006-04-28 01:46:50 +00001140 continue;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001141 } // CanReuse
Chris Lattneraddc55a2006-04-28 01:46:50 +00001142
1143 // Otherwise we have a situation where we have a two-address instruction
1144 // whose mod/ref operand needs to be reloaded. This reload is already
1145 // available in some register "PhysReg", but if we used PhysReg as the
1146 // operand to our 2-addr instruction, the instruction would modify
1147 // PhysReg. This isn't cool if something later uses PhysReg and expects
1148 // to get its initial value.
Chris Lattner50ea01e2005-09-09 20:29:51 +00001149 //
Chris Lattneraddc55a2006-04-28 01:46:50 +00001150 // To avoid this problem, and to avoid doing a load right after a store,
1151 // we emit a copy from PhysReg into the designated register for this
1152 // operand.
1153 unsigned DesignatedReg = VRM.getPhys(VirtReg);
1154 assert(DesignatedReg && "Must map virtreg to physreg!");
1155
1156 // Note that, if we reused a register for a previous operand, the
1157 // register we want to reload into might not actually be
1158 // available. If this occurs, use the register indicated by the
1159 // reuser.
1160 if (ReusedOperands.hasReuses())
1161 DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI,
Evan Cheng549f27d32007-08-13 23:45:17 +00001162 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
Chris Lattneraddc55a2006-04-28 01:46:50 +00001163
Chris Lattnerba1fc3d2006-04-28 04:43:18 +00001164 // If the mapped designated register is actually the physreg we have
1165 // incoming, we don't need to inserted a dead copy.
1166 if (DesignatedReg == PhysReg) {
1167 // If this stack slot value is already available, reuse it!
Evan Chengdc6be192007-08-14 05:42:54 +00001168 if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT)
1169 DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001170 else
Evan Chengdc6be192007-08-14 05:42:54 +00001171 DOUT << "Reusing SS#" << ReuseSlot;
Evan Cheng2638e1a2007-03-20 08:13:50 +00001172 DOUT << " from physreg " << MRI->getName(PhysReg) << " for vreg"
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001173 << VirtReg
1174 << " instead of reloading into same physreg.\n";
Evan Chengc498b022007-11-14 07:59:08 +00001175 unsigned RReg = SubIdx ? MRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001176 MI.getOperand(i).setReg(RReg);
Evan Cheng7277a7d2007-11-02 17:35:08 +00001177 ReusedOperands.markClobbered(RReg);
Chris Lattnerba1fc3d2006-04-28 04:43:18 +00001178 ++NumReused;
1179 continue;
1180 }
1181
Chris Lattner84bc5422007-12-31 04:13:23 +00001182 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
1183 RegInfo->setPhysRegUsed(DesignatedReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001184 ReusedOperands.markClobbered(DesignatedReg);
Evan Cheng9efce632007-09-26 06:25:56 +00001185 MRI->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC, RC);
Evan Chengde4e9422007-02-25 09:51:27 +00001186
Evan Cheng6b448092007-03-02 08:52:00 +00001187 MachineInstr *CopyMI = prior(MII);
Evan Cheng0c40d722007-07-11 05:28:39 +00001188 UpdateKills(*CopyMI, RegKills, KillOps);
Evan Chengde4e9422007-02-25 09:51:27 +00001189
Chris Lattneraddc55a2006-04-28 01:46:50 +00001190 // This invalidates DesignatedReg.
1191 Spills.ClobberPhysReg(DesignatedReg);
1192
Evan Chengdc6be192007-08-14 05:42:54 +00001193 Spills.addAvailable(ReuseSlot, &MI, DesignatedReg);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001194 unsigned RReg =
Evan Chengc498b022007-11-14 07:59:08 +00001195 SubIdx ? MRI->getSubReg(DesignatedReg, SubIdx) : DesignatedReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001196 MI.getOperand(i).setReg(RReg);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001197 DOUT << '\t' << *prior(MII);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001198 ++NumReused;
1199 continue;
Evan Cheng66f71632007-10-19 21:23:22 +00001200 } // if (PhysReg)
Chris Lattner50ea01e2005-09-09 20:29:51 +00001201
1202 // Otherwise, reload it and remember that we have it.
1203 PhysReg = VRM.getPhys(VirtReg);
Chris Lattner172c3622006-01-04 06:47:48 +00001204 assert(PhysReg && "Must map virtreg to physreg!");
Chris Lattner7fb64342004-10-01 19:04:51 +00001205
Chris Lattner50ea01e2005-09-09 20:29:51 +00001206 // Note that, if we reused a register for a previous operand, the
1207 // register we want to reload into might not actually be
1208 // available. If this occurs, use the register indicated by the
1209 // reuser.
Chris Lattner540fec62006-02-25 01:51:33 +00001210 if (ReusedOperands.hasReuses())
1211 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
Evan Cheng549f27d32007-08-13 23:45:17 +00001212 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
Chris Lattner540fec62006-02-25 01:51:33 +00001213
Chris Lattner84bc5422007-12-31 04:13:23 +00001214 RegInfo->setPhysRegUsed(PhysReg);
Evan Chenge077ef62006-11-04 00:21:55 +00001215 ReusedOperands.markClobbered(PhysReg);
Evan Cheng549f27d32007-08-13 23:45:17 +00001216 if (DoReMat) {
Evan Cheng2638e1a2007-03-20 08:13:50 +00001217 MRI->reMaterialize(MBB, &MI, PhysReg, VRM.getReMaterializedMI(VirtReg));
Evan Cheng91935142007-04-04 07:40:01 +00001218 ++NumReMats;
1219 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +00001220 const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +00001221 MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, SSorRMId, RC);
Evan Cheng91935142007-04-04 07:40:01 +00001222 ++NumLoads;
1223 }
Chris Lattner50ea01e2005-09-09 20:29:51 +00001224 // This invalidates PhysReg.
Chris Lattner66cf80f2006-02-03 23:13:58 +00001225 Spills.ClobberPhysReg(PhysReg);
Chris Lattner50ea01e2005-09-09 20:29:51 +00001226
1227 // Any stores to this stack slot are not dead anymore.
Evan Cheng549f27d32007-08-13 23:45:17 +00001228 if (!DoReMat)
Evan Chengfff3e192007-08-14 09:11:18 +00001229 MaybeDeadStores[SSorRMId] = NULL;
Evan Cheng549f27d32007-08-13 23:45:17 +00001230 Spills.addAvailable(SSorRMId, &MI, PhysReg);
Evan Chengde4e9422007-02-25 09:51:27 +00001231 // Assumes this is the last use. IsKill will be unset if reg is reused
1232 // unless it's a two-address operand.
1233 if (TID->getOperandConstraint(i, TOI::TIED_TO) == -1)
1234 MI.getOperand(i).setIsKill();
Evan Chengc498b022007-11-14 07:59:08 +00001235 unsigned RReg = SubIdx ? MRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001236 MI.getOperand(i).setReg(RReg);
Evan Cheng0c40d722007-07-11 05:28:39 +00001237 UpdateKills(*prior(MII), RegKills, KillOps);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001238 DOUT << '\t' << *prior(MII);
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001239 }
1240
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001241 DOUT << '\t' << MI;
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001242
Evan Cheng81a03822007-11-17 00:40:40 +00001243
Chris Lattner7fb64342004-10-01 19:04:51 +00001244 // If we have folded references to memory operands, make sure we clear all
1245 // physical registers that may contain the value of the spilled virtual
1246 // register
Evan Cheng66f71632007-10-19 21:23:22 +00001247 SmallSet<int, 2> FoldedSS;
Chris Lattner8f1d6402005-01-14 15:54:24 +00001248 for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) {
Chris Lattnerbec6a9e2004-10-01 23:15:36 +00001249 unsigned VirtReg = I->second.first;
1250 VirtRegMap::ModRef MR = I->second.second;
Evan Cheng66f71632007-10-19 21:23:22 +00001251 DOUT << "Folded vreg: " << VirtReg << " MR: " << MR;
Evan Cheng81a03822007-11-17 00:40:40 +00001252
Chris Lattnercea86882005-09-19 06:56:21 +00001253 int SS = VRM.getStackSlot(VirtReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001254 if (SS == VirtRegMap::NO_STACK_SLOT)
1255 continue;
Evan Cheng90a43c32007-08-15 20:20:34 +00001256 FoldedSS.insert(SS);
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001257 DOUT << " - StackSlot: " << SS << "\n";
Chris Lattnercea86882005-09-19 06:56:21 +00001258
1259 // If this folded instruction is just a use, check to see if it's a
1260 // straight load from the virt reg slot.
1261 if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) {
1262 int FrameIdx;
Evan Cheng32dfbea2007-10-12 08:50:34 +00001263 unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx);
1264 if (DestReg && FrameIdx == SS) {
1265 // If this spill slot is available, turn it into a copy (or nothing)
1266 // instead of leaving it as a load!
1267 if (unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SS)) {
1268 DOUT << "Promoted Load To Copy: " << MI;
1269 if (DestReg != InReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001270 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001271 MRI->copyRegToReg(MBB, &MI, DestReg, InReg, RC, RC);
1272 // Revisit the copy so we make sure to notice the effects of the
1273 // operation on the destreg (either needing to RA it if it's
1274 // virtual or needing to clobber any values if it's physical).
1275 NextMII = &MI;
1276 --NextMII; // backtrack to the copy.
1277 BackTracked = true;
Evan Cheng39c883c2007-12-11 23:36:57 +00001278 } else {
Evan Cheng32dfbea2007-10-12 08:50:34 +00001279 DOUT << "Removing now-noop copy: " << MI;
Evan Cheng39c883c2007-12-11 23:36:57 +00001280 // Unset last kill since it's being reused.
1281 InvalidateKill(InReg, RegKills, KillOps);
1282 }
Evan Chengde4e9422007-02-25 09:51:27 +00001283
Evan Chengcada2452007-11-28 01:28:46 +00001284 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng32dfbea2007-10-12 08:50:34 +00001285 MBB.erase(&MI);
1286 Erased = true;
1287 goto ProcessNextInst;
Chris Lattnercea86882005-09-19 06:56:21 +00001288 }
Evan Cheng7f566252007-10-13 02:50:24 +00001289 } else {
1290 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1291 SmallVector<MachineInstr*, 4> NewMIs;
1292 if (PhysReg &&
1293 MRI->unfoldMemoryOperand(MF, &MI, PhysReg, false, false, NewMIs)) {
1294 MBB.insert(MII, NewMIs[0]);
Evan Chengcada2452007-11-28 01:28:46 +00001295 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng7f566252007-10-13 02:50:24 +00001296 MBB.erase(&MI);
1297 Erased = true;
1298 --NextMII; // backtrack to the unfolded instruction.
1299 BackTracked = true;
1300 goto ProcessNextInst;
1301 }
Chris Lattnercea86882005-09-19 06:56:21 +00001302 }
1303 }
1304
1305 // If this reference is not a use, any previous store is now dead.
1306 // Otherwise, the store to this stack slot is not dead anymore.
Evan Chengfff3e192007-08-14 09:11:18 +00001307 MachineInstr* DeadStore = MaybeDeadStores[SS];
1308 if (DeadStore) {
Evan Cheng66f71632007-10-19 21:23:22 +00001309 bool isDead = !(MR & VirtRegMap::isRef);
Evan Cheng7f566252007-10-13 02:50:24 +00001310 MachineInstr *NewStore = NULL;
Evan Chengcbfb9b22007-10-22 03:01:44 +00001311 if (MR & VirtRegMap::isModRef) {
Evan Cheng7f566252007-10-13 02:50:24 +00001312 unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS);
1313 SmallVector<MachineInstr*, 4> NewMIs;
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001314 // We can reuse this physreg as long as we are allowed to clobber
Chris Lattner84bc5422007-12-31 04:13:23 +00001315 // the value and there isn't an earlier def that has already clobbered
1316 // the physreg.
Evan Cheng7f566252007-10-13 02:50:24 +00001317 if (PhysReg &&
Evan Cheng39c883c2007-12-11 23:36:57 +00001318 !TII->isStoreToStackSlot(&MI, SS) && // Not profitable!
Evan Cheng7f566252007-10-13 02:50:24 +00001319 DeadStore->findRegisterUseOperandIdx(PhysReg, true) != -1 &&
1320 MRI->unfoldMemoryOperand(MF, &MI, PhysReg, false, true, NewMIs)) {
1321 MBB.insert(MII, NewMIs[0]);
1322 NewStore = NewMIs[1];
1323 MBB.insert(MII, NewStore);
Evan Chengcada2452007-11-28 01:28:46 +00001324 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng7f566252007-10-13 02:50:24 +00001325 MBB.erase(&MI);
1326 Erased = true;
1327 --NextMII;
1328 --NextMII; // backtrack to the unfolded instruction.
1329 BackTracked = true;
Evan Cheng66f71632007-10-19 21:23:22 +00001330 isDead = true;
1331 }
Evan Cheng7f566252007-10-13 02:50:24 +00001332 }
1333
1334 if (isDead) { // Previous store is dead.
Chris Lattnercea86882005-09-19 06:56:21 +00001335 // If we get here, the store is dead, nuke it now.
Evan Chengfff3e192007-08-14 09:11:18 +00001336 DOUT << "Removed dead store:\t" << *DeadStore;
1337 InvalidateKills(*DeadStore, RegKills, KillOps);
Evan Chengcada2452007-11-28 01:28:46 +00001338 VRM.RemoveMachineInstrFromMaps(DeadStore);
Evan Cheng7f566252007-10-13 02:50:24 +00001339 MBB.erase(DeadStore);
1340 if (!NewStore)
1341 ++NumDSE;
Chris Lattnercea86882005-09-19 06:56:21 +00001342 }
Evan Cheng7f566252007-10-13 02:50:24 +00001343
Evan Chengfff3e192007-08-14 09:11:18 +00001344 MaybeDeadStores[SS] = NULL;
Evan Cheng7f566252007-10-13 02:50:24 +00001345 if (NewStore) {
1346 // Treat this store as a spill merged into a copy. That makes the
1347 // stack slot value available.
1348 VRM.virtFolded(VirtReg, NewStore, VirtRegMap::isMod);
1349 goto ProcessNextInst;
1350 }
Chris Lattnercea86882005-09-19 06:56:21 +00001351 }
1352
1353 // If the spill slot value is available, and this is a new definition of
1354 // the value, the value is not available anymore.
1355 if (MR & VirtRegMap::isMod) {
Chris Lattner07cf1412006-02-03 00:36:31 +00001356 // Notice that the value in this stack slot has been modified.
Evan Cheng549f27d32007-08-13 23:45:17 +00001357 Spills.ModifyStackSlotOrReMat(SS);
Chris Lattnercd816392006-02-02 23:29:36 +00001358
1359 // If this is *just* a mod of the value, check to see if this is just a
1360 // store to the spill slot (i.e. the spill got merged into the copy). If
1361 // so, realize that the vreg is available now, and add the store to the
1362 // MaybeDeadStore info.
1363 int StackSlot;
1364 if (!(MR & VirtRegMap::isRef)) {
1365 if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) {
1366 assert(MRegisterInfo::isPhysicalRegister(SrcReg) &&
1367 "Src hasn't been allocated yet?");
Chris Lattner07cf1412006-02-03 00:36:31 +00001368 // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark
Chris Lattnercd816392006-02-02 23:29:36 +00001369 // this as a potentially dead store in case there is a subsequent
1370 // store into the stack slot without a read from it.
1371 MaybeDeadStores[StackSlot] = &MI;
1372
Chris Lattnercd816392006-02-02 23:29:36 +00001373 // If the stack slot value was previously available in some other
1374 // register, change it now. Otherwise, make the register available,
1375 // in PhysReg.
Evan Cheng91e23902007-02-23 01:13:26 +00001376 Spills.addAvailable(StackSlot, &MI, SrcReg, false/*don't clobber*/);
Chris Lattnercd816392006-02-02 23:29:36 +00001377 }
1378 }
Chris Lattner7fb64342004-10-01 19:04:51 +00001379 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001380 }
1381
Chris Lattner7fb64342004-10-01 19:04:51 +00001382 // Process all of the spilled defs.
1383 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1384 MachineOperand &MO = MI.getOperand(i);
Evan Cheng66f71632007-10-19 21:23:22 +00001385 if (!(MO.isRegister() && MO.getReg() && MO.isDef()))
1386 continue;
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001387
Evan Cheng66f71632007-10-19 21:23:22 +00001388 unsigned VirtReg = MO.getReg();
1389 if (!MRegisterInfo::isVirtualRegister(VirtReg)) {
1390 // Check to see if this is a noop copy. If so, eliminate the
1391 // instruction before considering the dest reg to be changed.
1392 unsigned Src, Dst;
1393 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
1394 ++NumDCE;
1395 DOUT << "Removing now-noop copy: " << MI;
1396 MBB.erase(&MI);
1397 Erased = true;
Evan Chengcada2452007-11-28 01:28:46 +00001398 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng66f71632007-10-19 21:23:22 +00001399 Spills.disallowClobberPhysReg(VirtReg);
1400 goto ProcessNextInst;
1401 }
1402
1403 // If it's not a no-op copy, it clobbers the value in the destreg.
1404 Spills.ClobberPhysReg(VirtReg);
1405 ReusedOperands.markClobbered(VirtReg);
1406
1407 // Check to see if this instruction is a load from a stack slot into
1408 // a register. If so, this provides the stack slot value in the reg.
1409 int FrameIdx;
1410 if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) {
1411 assert(DestReg == VirtReg && "Unknown load situation!");
1412
1413 // If it is a folded reference, then it's not safe to clobber.
1414 bool Folded = FoldedSS.count(FrameIdx);
1415 // Otherwise, if it wasn't available, remember that it is now!
1416 Spills.addAvailable(FrameIdx, &MI, DestReg, !Folded);
1417 goto ProcessNextInst;
1418 }
1419
1420 continue;
1421 }
1422
Evan Chengc498b022007-11-14 07:59:08 +00001423 unsigned SubIdx = MO.getSubReg();
Evan Cheng66f71632007-10-19 21:23:22 +00001424 bool DoReMat = VRM.isReMaterialized(VirtReg);
1425 if (DoReMat)
1426 ReMatDefs.insert(&MI);
1427
1428 // The only vregs left are stack slot definitions.
1429 int StackSlot = VRM.getStackSlot(VirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +00001430 const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg);
Evan Cheng66f71632007-10-19 21:23:22 +00001431
1432 // If this def is part of a two-address operand, make sure to execute
1433 // the store from the correct physical register.
1434 unsigned PhysReg;
1435 int TiedOp = MI.getInstrDescriptor()->findTiedToSrcOperand(i);
Evan Cheng7277a7d2007-11-02 17:35:08 +00001436 if (TiedOp != -1) {
Evan Cheng66f71632007-10-19 21:23:22 +00001437 PhysReg = MI.getOperand(TiedOp).getReg();
Evan Chengc498b022007-11-14 07:59:08 +00001438 if (SubIdx) {
Evan Cheng7277a7d2007-11-02 17:35:08 +00001439 unsigned SuperReg = findSuperReg(RC, PhysReg, SubIdx, MRI);
1440 assert(SuperReg && MRI->getSubReg(SuperReg, SubIdx) == PhysReg &&
1441 "Can't find corresponding super-register!");
1442 PhysReg = SuperReg;
1443 }
1444 } else {
Evan Cheng66f71632007-10-19 21:23:22 +00001445 PhysReg = VRM.getPhys(VirtReg);
1446 if (ReusedOperands.isClobbered(PhysReg)) {
1447 // Another def has taken the assigned physreg. It must have been a
1448 // use&def which got it due to reuse. Undo the reuse!
1449 PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI,
1450 Spills, MaybeDeadStores, RegKills, KillOps, VRM);
1451 }
1452 }
1453
Chris Lattner84bc5422007-12-31 04:13:23 +00001454 RegInfo->setPhysRegUsed(PhysReg);
Evan Chengc498b022007-11-14 07:59:08 +00001455 unsigned RReg = SubIdx ? MRI->getSubReg(PhysReg, SubIdx) : PhysReg;
Evan Cheng7277a7d2007-11-02 17:35:08 +00001456 ReusedOperands.markClobbered(RReg);
1457 MI.getOperand(i).setReg(RReg);
1458
Evan Cheng66f71632007-10-19 21:23:22 +00001459 if (!MO.isDead()) {
Evan Cheng66f71632007-10-19 21:23:22 +00001460 MachineInstr *&LastStore = MaybeDeadStores[StackSlot];
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001461 SpillRegToStackSlot(MBB, MII, -1, PhysReg, StackSlot, RC, true,
1462 LastStore, Spills, ReMatDefs, RegKills, KillOps, VRM);
Evan Chenge4b39002007-12-03 21:31:55 +00001463 NextMII = next(MII);
Evan Cheng66f71632007-10-19 21:23:22 +00001464
1465 // Check to see if this is a noop copy. If so, eliminate the
1466 // instruction before considering the dest reg to be changed.
1467 {
Chris Lattner29268692006-09-05 02:12:02 +00001468 unsigned Src, Dst;
1469 if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
1470 ++NumDCE;
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001471 DOUT << "Removing now-noop copy: " << MI;
Chris Lattner29268692006-09-05 02:12:02 +00001472 MBB.erase(&MI);
Evan Cheng0c40d722007-07-11 05:28:39 +00001473 Erased = true;
Evan Chengcada2452007-11-28 01:28:46 +00001474 VRM.RemoveMachineInstrFromMaps(&MI);
Evan Cheng66f71632007-10-19 21:23:22 +00001475 UpdateKills(*LastStore, RegKills, KillOps);
Chris Lattner29268692006-09-05 02:12:02 +00001476 goto ProcessNextInst;
Chris Lattner7fb64342004-10-01 19:04:51 +00001477 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001478 }
Evan Cheng66f71632007-10-19 21:23:22 +00001479 }
Chris Lattner7fb64342004-10-01 19:04:51 +00001480 }
Chris Lattnercea86882005-09-19 06:56:21 +00001481 ProcessNextInst:
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001482 if (!Erased && !BackTracked) {
Evan Cheng0c40d722007-07-11 05:28:39 +00001483 for (MachineBasicBlock::iterator II = MI; II != NextMII; ++II)
1484 UpdateKills(*II, RegKills, KillOps);
Evan Cheng35a3e4a2007-12-04 19:19:45 +00001485 }
Chris Lattner7fb64342004-10-01 19:04:51 +00001486 MII = NextMII;
1487 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001488}
1489
Chris Lattner8c4d88d2004-09-30 01:54:45 +00001490llvm::Spiller* llvm::createSpiller() {
1491 switch (SpillerOpt) {
1492 default: assert(0 && "Unreachable!");
1493 case local:
1494 return new LocalSpiller();
1495 case simple:
1496 return new SimpleSpiller();
1497 }
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +00001498}