blob: 8b6082d18193285a244f99ba3898d88d931c7dd5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a virtual register map. This maps virtual registers to
11// physical registers and virtual registers to stack slots. It is created and
12// updated by a register allocator and then used by a machine code rewriter that
13// adds spill code and rewrites virtual into physical register references.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_VIRTREGMAP_H
18#define LLVM_CODEGEN_VIRTREGMAP_H
19
Owen Andersondd56ab72009-03-13 05:55:11 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hamesd8f30992009-09-04 20:41:11 +000021#include "llvm/CodeGen/LiveInterval.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng7b88cbc2008-04-11 17:53:36 +000023#include "llvm/ADT/BitVector.h"
Evan Cheng97c5f1f2009-05-03 18:32:42 +000024#include "llvm/ADT/DenseMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/ADT/IndexedMap.h"
Evan Chengda872532008-02-27 03:04:06 +000026#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanc24a3f82009-01-05 17:59:02 +000027#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include <map>
29
30namespace llvm {
Evan Cheng97c5f1f2009-05-03 18:32:42 +000031 class LiveIntervals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 class MachineInstr;
David Greene44a3bfb2007-08-07 16:34:05 +000033 class MachineFunction;
Evan Chengd78907d2009-06-14 20:22:55 +000034 class MachineRegisterInfo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 class TargetInstrInfo;
Evan Cheng5277da72009-05-04 03:30:11 +000036 class TargetRegisterInfo;
Daniel Dunbarf55f61f2009-07-24 10:36:58 +000037 class raw_ostream;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
Owen Andersondd56ab72009-03-13 05:55:11 +000039 class VirtRegMap : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 public:
41 enum {
42 NO_PHYS_REG = 0,
43 NO_STACK_SLOT = (1L << 30)-1,
44 MAX_STACK_SLOT = (1L << 18)-1
45 };
46
47 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
48 typedef std::multimap<MachineInstr*,
49 std::pair<unsigned, ModRef> > MI2VirtMapTy;
50
51 private:
Evan Chengd78907d2009-06-14 20:22:55 +000052 MachineRegisterInfo *MRI;
Owen Andersondd56ab72009-03-13 05:55:11 +000053 const TargetInstrInfo *TII;
Evan Cheng5277da72009-05-04 03:30:11 +000054 const TargetRegisterInfo *TRI;
Owen Andersondd56ab72009-03-13 05:55:11 +000055 MachineFunction *MF;
Evan Cheng5277da72009-05-04 03:30:11 +000056
57 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
58
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 /// Virt2PhysMap - This is a virtual to physical register
60 /// mapping. Each virtual register is required to have an entry in
61 /// it; even spilled virtual registers (the register mapped to a
62 /// spilled register is the temporary used to load it from the
63 /// stack).
64 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Chengcecc8222007-11-17 00:40:40 +000065
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 /// Virt2StackSlotMap - This is virtual register to stack slot
67 /// mapping. Each spilled virtual register has an entry in it
68 /// which corresponds to the stack slot this register is spilled
69 /// at.
70 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Chengcecc8222007-11-17 00:40:40 +000071
Dan Gohman7d45f4d2008-03-12 20:50:04 +000072 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
Evan Chengcecc8222007-11-17 00:40:40 +000073 /// mapping. Each spilled virtual register that should be remat'd has an
74 /// entry in it which corresponds to the remat id.
Evan Cheng1204d172007-08-13 23:45:17 +000075 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Chengcecc8222007-11-17 00:40:40 +000076
77 /// Virt2SplitMap - This is virtual register to splitted virtual register
78 /// mapping.
79 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
80
Evan Cheng6f522672007-12-05 09:51:10 +000081 /// Virt2SplitKillMap - This is splitted virtual register to its last use
Evan Chengd9731042007-12-05 10:24:35 +000082 /// (kill) index mapping.
Lang Hamesd6a717c2009-11-03 23:52:08 +000083 IndexedMap<SlotIndex> Virt2SplitKillMap;
Evan Cheng6f522672007-12-05 09:51:10 +000084
Evan Chengcecc8222007-11-17 00:40:40 +000085 /// ReMatMap - This is virtual register to re-materialized instruction
86 /// mapping. Each virtual register whose definition is going to be
87 /// re-materialized has an entry in it.
88 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
89
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 /// MI2VirtMap - This is MachineInstr to virtual register
91 /// mapping. In the case of memory spill code being folded into
92 /// instructions, we need to know which virtual register was
93 /// read/written by this instruction.
94 MI2VirtMapTy MI2VirtMap;
95
Evan Chengcecc8222007-11-17 00:40:40 +000096 /// SpillPt2VirtMap - This records the virtual registers which should
97 /// be spilled right after the MachineInstr due to live interval
98 /// splitting.
Evan Chenged17a892007-12-05 08:16:32 +000099 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
100 SpillPt2VirtMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101
Evan Cheng96c61312007-11-29 01:06:25 +0000102 /// RestorePt2VirtMap - This records the virtual registers which should
103 /// be restored right before the MachineInstr due to live interval
104 /// splitting.
105 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
106
Evan Cheng14cc83f2008-03-11 07:19:34 +0000107 /// EmergencySpillMap - This records the physical registers that should
108 /// be spilled / restored around the MachineInstr since the register
109 /// allocator has run out of registers.
110 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
111
112 /// EmergencySpillSlots - This records emergency spill slots used to
113 /// spill physical registers when the register allocator runs out of
114 /// registers. Ideally only one stack slot is used per function per
115 /// register class.
116 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
119 /// virtual register, an unique id is being assigned. This keeps track of
120 /// the highest id used so far. Note, this starts at (1<<18) to avoid
121 /// conflicts with stack slot numbers.
122 int ReMatId;
123
Evan Chengda872532008-02-27 03:04:06 +0000124 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
125 int LowSpillSlot, HighSpillSlot;
126
127 /// SpillSlotToUsesMap - Records uses for each register spill slot.
128 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
129
Evan Cheng7b88cbc2008-04-11 17:53:36 +0000130 /// ImplicitDefed - One bit for each virtual register. If set it indicates
131 /// the register is implicitly defined.
132 BitVector ImplicitDefed;
133
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000134 /// UnusedRegs - A list of physical registers that have not been used.
135 BitVector UnusedRegs;
136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
138 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
139
140 public:
Owen Andersondd56ab72009-03-13 05:55:11 +0000141 static char ID;
Owen Anderson75693222010-08-06 18:33:48 +0000142 VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
Owen Andersondd56ab72009-03-13 05:55:11 +0000143 Virt2StackSlotMap(NO_STACK_SLOT),
144 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
Lang Hamesd6a717c2009-11-03 23:52:08 +0000145 Virt2SplitKillMap(SlotIndex()), ReMatMap(NULL),
Owen Andersondd56ab72009-03-13 05:55:11 +0000146 ReMatId(MAX_STACK_SLOT+1),
147 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
148 virtual bool runOnMachineFunction(MachineFunction &MF);
149
150 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
151 AU.setPreservesAll();
152 MachineFunctionPass::getAnalysisUsage(AU);
153 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
Jakob Stoklund Olesenca2b7202010-07-26 23:44:11 +0000155 MachineFunction &getMachineFunction() const {
156 assert(MF && "getMachineFunction called before runOnMAchineFunction");
157 return *MF;
158 }
159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 void grow();
161
162 /// @brief returns true if the specified virtual register is
163 /// mapped to a physical register
164 bool hasPhys(unsigned virtReg) const {
165 return getPhys(virtReg) != NO_PHYS_REG;
166 }
167
168 /// @brief returns the physical register mapped to the specified
169 /// virtual register
170 unsigned getPhys(unsigned virtReg) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000171 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 return Virt2PhysMap[virtReg];
173 }
174
175 /// @brief creates a mapping for the specified virtual register to
176 /// the specified physical register
177 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000178 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
179 TargetRegisterInfo::isPhysicalRegister(physReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
181 "attempt to assign physical register to already mapped "
182 "virtual register");
183 Virt2PhysMap[virtReg] = physReg;
184 }
185
186 /// @brief clears the specified virtual register's, physical
187 /// register mapping
188 void clearVirt(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000189 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
191 "attempt to clear a not assigned virtual register");
192 Virt2PhysMap[virtReg] = NO_PHYS_REG;
193 }
194
195 /// @brief clears all virtual to physical register mappings
196 void clearAllVirt() {
197 Virt2PhysMap.clear();
198 grow();
199 }
200
Evan Chengd78907d2009-06-14 20:22:55 +0000201 /// @brief returns the register allocation preference.
202 unsigned getRegAllocPref(unsigned virtReg);
203
Evan Chengcecc8222007-11-17 00:40:40 +0000204 /// @brief records virtReg is a split live interval from SReg.
205 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
206 Virt2SplitMap[virtReg] = SReg;
207 }
208
209 /// @brief returns the live interval virtReg is split from.
210 unsigned getPreSplitReg(unsigned virtReg) {
211 return Virt2SplitMap[virtReg];
212 }
213
Dan Gohman7d45f4d2008-03-12 20:50:04 +0000214 /// @brief returns true if the specified virtual register is not
Evan Cheng1204d172007-08-13 23:45:17 +0000215 /// mapped to a stack slot or rematerialized.
216 bool isAssignedReg(unsigned virtReg) const {
Evan Chengcecc8222007-11-17 00:40:40 +0000217 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
218 getReMatId(virtReg) == NO_STACK_SLOT)
219 return true;
220 // Split register can be assigned a physical register as well as a
221 // stack slot or remat id.
222 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 }
224
225 /// @brief returns the stack slot mapped to the specified virtual
226 /// register
227 int getStackSlot(unsigned virtReg) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000228 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 return Virt2StackSlotMap[virtReg];
230 }
231
Evan Cheng1204d172007-08-13 23:45:17 +0000232 /// @brief returns the rematerialization id mapped to the specified virtual
233 /// register
234 int getReMatId(unsigned virtReg) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000235 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000236 return Virt2ReMatIdMap[virtReg];
237 }
238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 /// @brief create a mapping for the specifed virtual register to
240 /// the next available stack slot
241 int assignVirt2StackSlot(unsigned virtReg);
242 /// @brief create a mapping for the specified virtual register to
243 /// the specified stack slot
244 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
245
246 /// @brief assign an unique re-materialization id to the specified
247 /// virtual register.
248 int assignVirtReMatId(unsigned virtReg);
Evan Cheng1204d172007-08-13 23:45:17 +0000249 /// @brief assign an unique re-materialization id to the specified
250 /// virtual register.
251 void assignVirtReMatId(unsigned virtReg, int id);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252
253 /// @brief returns true if the specified virtual register is being
254 /// re-materialized.
255 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng1204d172007-08-13 23:45:17 +0000256 return ReMatMap[virtReg] != NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 }
258
259 /// @brief returns the original machine instruction being re-issued
260 /// to re-materialize the specified virtual register.
Evan Cheng1204d172007-08-13 23:45:17 +0000261 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 return ReMatMap[virtReg];
263 }
264
265 /// @brief records the specified virtual register will be
266 /// re-materialized and the original instruction which will be re-issed
Evan Cheng1204d172007-08-13 23:45:17 +0000267 /// for this purpose. If parameter all is true, then all uses of the
268 /// registers are rematerialized and it's safe to delete the definition.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
270 ReMatMap[virtReg] = def;
271 }
272
Evan Cheng6f522672007-12-05 09:51:10 +0000273 /// @brief record the last use (kill) of a split virtual register.
Lang Hamesd6a717c2009-11-03 23:52:08 +0000274 void addKillPoint(unsigned virtReg, SlotIndex index) {
Evan Chengd9731042007-12-05 10:24:35 +0000275 Virt2SplitKillMap[virtReg] = index;
Evan Cheng6f522672007-12-05 09:51:10 +0000276 }
277
Lang Hamesd6a717c2009-11-03 23:52:08 +0000278 SlotIndex getKillPoint(unsigned virtReg) const {
Evan Chengd9731042007-12-05 10:24:35 +0000279 return Virt2SplitKillMap[virtReg];
280 }
281
282 /// @brief remove the last use (kill) of a split virtual register.
Evan Cheng6f522672007-12-05 09:51:10 +0000283 void removeKillPoint(unsigned virtReg) {
Lang Hamesd6a717c2009-11-03 23:52:08 +0000284 Virt2SplitKillMap[virtReg] = SlotIndex();
Evan Cheng6f522672007-12-05 09:51:10 +0000285 }
286
Evan Cheng91e32d02007-11-28 01:28:46 +0000287 /// @brief returns true if the specified MachineInstr is a spill point.
288 bool isSpillPt(MachineInstr *Pt) const {
289 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
290 }
291
Evan Chengcecc8222007-11-17 00:40:40 +0000292 /// @brief returns the virtual registers that should be spilled due to
293 /// splitting right after the specified MachineInstr.
Evan Chenged17a892007-12-05 08:16:32 +0000294 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Chengcecc8222007-11-17 00:40:40 +0000295 return SpillPt2VirtMap[Pt];
296 }
297
298 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chenged17a892007-12-05 08:16:32 +0000299 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000300 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
301 I = SpillPt2VirtMap.find(Pt);
302 if (I != SpillPt2VirtMap.end())
303 I->second.push_back(std::make_pair(virtReg, isKill));
Evan Cheng91e32d02007-11-28 01:28:46 +0000304 else {
Evan Chenged17a892007-12-05 08:16:32 +0000305 std::vector<std::pair<unsigned,bool> > Virts;
306 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Cheng91e32d02007-11-28 01:28:46 +0000307 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
308 }
Evan Chengcecc8222007-11-17 00:40:40 +0000309 }
310
Evan Cheng1eeb2ef2008-03-11 21:34:46 +0000311 /// @brief - transfer spill point information from one instruction to
312 /// another.
Evan Chengcecc8222007-11-17 00:40:40 +0000313 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000314 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
Evan Chenged17a892007-12-05 08:16:32 +0000315 I = SpillPt2VirtMap.find(Old);
Evan Cheng91e32d02007-11-28 01:28:46 +0000316 if (I == SpillPt2VirtMap.end())
317 return;
318 while (!I->second.empty()) {
Evan Chenged17a892007-12-05 08:16:32 +0000319 unsigned virtReg = I->second.back().first;
320 bool isKill = I->second.back().second;
Evan Cheng91e32d02007-11-28 01:28:46 +0000321 I->second.pop_back();
Evan Chenged17a892007-12-05 08:16:32 +0000322 addSpillPoint(virtReg, isKill, New);
Evan Chengcecc8222007-11-17 00:40:40 +0000323 }
Evan Cheng91e32d02007-11-28 01:28:46 +0000324 SpillPt2VirtMap.erase(I);
Evan Chengcecc8222007-11-17 00:40:40 +0000325 }
326
Evan Cheng96c61312007-11-29 01:06:25 +0000327 /// @brief returns true if the specified MachineInstr is a restore point.
328 bool isRestorePt(MachineInstr *Pt) const {
329 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
330 }
331
332 /// @brief returns the virtual registers that should be restoreed due to
333 /// splitting right after the specified MachineInstr.
334 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
335 return RestorePt2VirtMap[Pt];
336 }
337
338 /// @brief records the specified MachineInstr as a restore point for virtReg.
339 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000340 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
341 RestorePt2VirtMap.find(Pt);
342 if (I != RestorePt2VirtMap.end())
343 I->second.push_back(virtReg);
Evan Cheng96c61312007-11-29 01:06:25 +0000344 else {
345 std::vector<unsigned> Virts;
346 Virts.push_back(virtReg);
347 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
348 }
349 }
350
Evan Cheng14cc83f2008-03-11 07:19:34 +0000351 /// @brief - transfer restore point information from one instruction to
352 /// another.
Evan Cheng96c61312007-11-29 01:06:25 +0000353 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000354 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
Evan Cheng96c61312007-11-29 01:06:25 +0000355 RestorePt2VirtMap.find(Old);
356 if (I == RestorePt2VirtMap.end())
357 return;
358 while (!I->second.empty()) {
359 unsigned virtReg = I->second.back();
360 I->second.pop_back();
361 addRestorePoint(virtReg, New);
362 }
363 RestorePt2VirtMap.erase(I);
364 }
365
Evan Cheng14cc83f2008-03-11 07:19:34 +0000366 /// @brief records that the specified physical register must be spilled
367 /// around the specified machine instr.
368 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
369 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
370 EmergencySpillMap[MI].push_back(PhysReg);
371 else {
372 std::vector<unsigned> PhysRegs;
373 PhysRegs.push_back(PhysReg);
374 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
375 }
376 }
377
378 /// @brief returns true if one or more physical registers must be spilled
379 /// around the specified instruction.
380 bool hasEmergencySpills(MachineInstr *MI) const {
381 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
382 }
383
384 /// @brief returns the physical registers to be spilled and restored around
385 /// the instruction.
386 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
387 return EmergencySpillMap[MI];
388 }
389
Evan Cheng1eeb2ef2008-03-11 21:34:46 +0000390 /// @brief - transfer emergency spill information from one instruction to
391 /// another.
392 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
393 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
394 EmergencySpillMap.find(Old);
395 if (I == EmergencySpillMap.end())
396 return;
397 while (!I->second.empty()) {
398 unsigned virtReg = I->second.back();
399 I->second.pop_back();
400 addEmergencySpill(virtReg, New);
401 }
402 EmergencySpillMap.erase(I);
403 }
404
Evan Cheng14cc83f2008-03-11 07:19:34 +0000405 /// @brief return or get a emergency spill slot for the register class.
406 int getEmergencySpillSlot(const TargetRegisterClass *RC);
407
Evan Chengda872532008-02-27 03:04:06 +0000408 /// @brief Return lowest spill slot index.
409 int getLowSpillSlot() const {
410 return LowSpillSlot;
411 }
412
413 /// @brief Return highest spill slot index.
414 int getHighSpillSlot() const {
415 return HighSpillSlot;
416 }
417
418 /// @brief Records a spill slot use.
419 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
420
421 /// @brief Returns true if spill slot has been used.
422 bool isSpillSlotUsed(int FrameIndex) const {
423 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
424 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
425 }
426
Evan Cheng7b88cbc2008-04-11 17:53:36 +0000427 /// @brief Mark the specified register as being implicitly defined.
428 void setIsImplicitlyDefined(unsigned VirtReg) {
429 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
430 }
431
432 /// @brief Returns true if the virtual register is implicitly defined.
433 bool isImplicitlyDefined(unsigned VirtReg) const {
434 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
435 }
436
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 /// @brief Updates information about the specified virtual register's value
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000438 /// folded into newMI machine instruction.
439 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
440 ModRef MRInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
Evan Chengf3255842007-10-13 02:50:24 +0000442 /// @brief Updates information about the specified virtual register's value
443 /// folded into the specified machine instruction.
444 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
445
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 /// @brief returns the virtual registers' values folded in memory
447 /// operands of this instruction
448 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
449 getFoldedVirts(MachineInstr* MI) const {
450 return MI2VirtMap.equal_range(MI);
451 }
452
Evan Cheng91e32d02007-11-28 01:28:46 +0000453 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
454 /// the folded instruction map and spill point map.
Evan Chengda872532008-02-27 03:04:06 +0000455 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000457 /// FindUnusedRegisters - Gather a list of allocatable registers that
458 /// have not been allocated to any virtual register.
Evan Chengd78907d2009-06-14 20:22:55 +0000459 bool FindUnusedRegisters(LiveIntervals* LIs);
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000460
461 /// HasUnusedRegisters - Return true if there are any allocatable registers
462 /// that have not been allocated to any virtual register.
463 bool HasUnusedRegisters() const {
464 return !UnusedRegs.none();
465 }
466
467 /// setRegisterUsed - Remember the physical register is now used.
468 void setRegisterUsed(unsigned Reg) {
469 UnusedRegs.reset(Reg);
470 }
471
472 /// isRegisterUnused - Return true if the physical register has not been
473 /// used.
474 bool isRegisterUnused(unsigned Reg) const {
475 return UnusedRegs[Reg];
476 }
477
478 /// getFirstUnusedRegister - Return the first physical register that has not
479 /// been used.
480 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
481 int Reg = UnusedRegs.find_first();
482 while (Reg != -1) {
Evan Cheng5277da72009-05-04 03:30:11 +0000483 if (allocatableRCRegs[RC][Reg])
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000484 return (unsigned)Reg;
485 Reg = UnusedRegs.find_next(Reg);
486 }
487 return 0;
488 }
489
Daniel Dunbarf55f61f2009-07-24 10:36:58 +0000490 void print(raw_ostream &OS, const Module* M = 0) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 void dump() const;
492 };
493
Daniel Dunbarf55f61f2009-07-24 10:36:58 +0000494 inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
495 VRM.print(OS);
496 return OS;
497 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498} // End llvm namespace
499
500#endif