blob: fe767b7671e11b5851caff4f011e05aaa29ea6b2 [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"
Dan Gohman1e57df32008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng7b88cbc2008-04-11 17:53:36 +000022#include "llvm/ADT/BitVector.h"
Evan Cheng97c5f1f2009-05-03 18:32:42 +000023#include "llvm/ADT/DenseMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/ADT/IndexedMap.h"
Evan Chengda872532008-02-27 03:04:06 +000025#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanc24a3f82009-01-05 17:59:02 +000026#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/Streams.h"
28#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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
Owen Andersondd56ab72009-03-13 05:55:11 +000038 class VirtRegMap : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 public:
40 enum {
41 NO_PHYS_REG = 0,
42 NO_STACK_SLOT = (1L << 30)-1,
43 MAX_STACK_SLOT = (1L << 18)-1
44 };
45
46 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
47 typedef std::multimap<MachineInstr*,
48 std::pair<unsigned, ModRef> > MI2VirtMapTy;
49
50 private:
Evan Chengd78907d2009-06-14 20:22:55 +000051 MachineRegisterInfo *MRI;
Owen Andersondd56ab72009-03-13 05:55:11 +000052 const TargetInstrInfo *TII;
Evan Cheng5277da72009-05-04 03:30:11 +000053 const TargetRegisterInfo *TRI;
Owen Andersondd56ab72009-03-13 05:55:11 +000054 MachineFunction *MF;
Evan Cheng5277da72009-05-04 03:30:11 +000055
56 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
57
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 /// Virt2PhysMap - This is a virtual to physical register
59 /// mapping. Each virtual register is required to have an entry in
60 /// it; even spilled virtual registers (the register mapped to a
61 /// spilled register is the temporary used to load it from the
62 /// stack).
63 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Chengcecc8222007-11-17 00:40:40 +000064
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 /// Virt2StackSlotMap - This is virtual register to stack slot
66 /// mapping. Each spilled virtual register has an entry in it
67 /// which corresponds to the stack slot this register is spilled
68 /// at.
69 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Chengcecc8222007-11-17 00:40:40 +000070
Dan Gohman7d45f4d2008-03-12 20:50:04 +000071 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
Evan Chengcecc8222007-11-17 00:40:40 +000072 /// mapping. Each spilled virtual register that should be remat'd has an
73 /// entry in it which corresponds to the remat id.
Evan Cheng1204d172007-08-13 23:45:17 +000074 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Chengcecc8222007-11-17 00:40:40 +000075
76 /// Virt2SplitMap - This is virtual register to splitted virtual register
77 /// mapping.
78 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
79
Evan Cheng6f522672007-12-05 09:51:10 +000080 /// Virt2SplitKillMap - This is splitted virtual register to its last use
Evan Chengd9731042007-12-05 10:24:35 +000081 /// (kill) index mapping.
82 IndexedMap<unsigned> Virt2SplitKillMap;
Evan Cheng6f522672007-12-05 09:51:10 +000083
Evan Chengcecc8222007-11-17 00:40:40 +000084 /// ReMatMap - This is virtual register to re-materialized instruction
85 /// mapping. Each virtual register whose definition is going to be
86 /// re-materialized has an entry in it.
87 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 /// MI2VirtMap - This is MachineInstr to virtual register
90 /// mapping. In the case of memory spill code being folded into
91 /// instructions, we need to know which virtual register was
92 /// read/written by this instruction.
93 MI2VirtMapTy MI2VirtMap;
94
Evan Chengcecc8222007-11-17 00:40:40 +000095 /// SpillPt2VirtMap - This records the virtual registers which should
96 /// be spilled right after the MachineInstr due to live interval
97 /// splitting.
Evan Chenged17a892007-12-05 08:16:32 +000098 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
99 SpillPt2VirtMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
Evan Cheng96c61312007-11-29 01:06:25 +0000101 /// RestorePt2VirtMap - This records the virtual registers which should
102 /// be restored right before the MachineInstr due to live interval
103 /// splitting.
104 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
105
Evan Cheng14cc83f2008-03-11 07:19:34 +0000106 /// EmergencySpillMap - This records the physical registers that should
107 /// be spilled / restored around the MachineInstr since the register
108 /// allocator has run out of registers.
109 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
110
111 /// EmergencySpillSlots - This records emergency spill slots used to
112 /// spill physical registers when the register allocator runs out of
113 /// registers. Ideally only one stack slot is used per function per
114 /// register class.
115 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
118 /// virtual register, an unique id is being assigned. This keeps track of
119 /// the highest id used so far. Note, this starts at (1<<18) to avoid
120 /// conflicts with stack slot numbers.
121 int ReMatId;
122
Evan Chengda872532008-02-27 03:04:06 +0000123 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
124 int LowSpillSlot, HighSpillSlot;
125
126 /// SpillSlotToUsesMap - Records uses for each register spill slot.
127 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
128
Evan Cheng7b88cbc2008-04-11 17:53:36 +0000129 /// ImplicitDefed - One bit for each virtual register. If set it indicates
130 /// the register is implicitly defined.
131 BitVector ImplicitDefed;
132
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000133 /// UnusedRegs - A list of physical registers that have not been used.
134 BitVector UnusedRegs;
135
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
137 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
138
139 public:
Owen Andersondd56ab72009-03-13 05:55:11 +0000140 static char ID;
141 VirtRegMap() : MachineFunctionPass(&ID), Virt2PhysMap(NO_PHYS_REG),
142 Virt2StackSlotMap(NO_STACK_SLOT),
143 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
144 Virt2SplitKillMap(0), ReMatMap(NULL),
145 ReMatId(MAX_STACK_SLOT+1),
146 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
147 virtual bool runOnMachineFunction(MachineFunction &MF);
148
149 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
150 AU.setPreservesAll();
151 MachineFunctionPass::getAnalysisUsage(AU);
152 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
154 void grow();
155
156 /// @brief returns true if the specified virtual register is
157 /// mapped to a physical register
158 bool hasPhys(unsigned virtReg) const {
159 return getPhys(virtReg) != NO_PHYS_REG;
160 }
161
162 /// @brief returns the physical register mapped to the specified
163 /// virtual register
164 unsigned getPhys(unsigned virtReg) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000165 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 return Virt2PhysMap[virtReg];
167 }
168
169 /// @brief creates a mapping for the specified virtual register to
170 /// the specified physical register
171 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000172 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
173 TargetRegisterInfo::isPhysicalRegister(physReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
175 "attempt to assign physical register to already mapped "
176 "virtual register");
177 Virt2PhysMap[virtReg] = physReg;
178 }
179
180 /// @brief clears the specified virtual register's, physical
181 /// register mapping
182 void clearVirt(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000183 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
185 "attempt to clear a not assigned virtual register");
186 Virt2PhysMap[virtReg] = NO_PHYS_REG;
187 }
188
189 /// @brief clears all virtual to physical register mappings
190 void clearAllVirt() {
191 Virt2PhysMap.clear();
192 grow();
193 }
194
Evan Chengd78907d2009-06-14 20:22:55 +0000195 /// @brief returns the register allocation preference.
196 unsigned getRegAllocPref(unsigned virtReg);
197
Evan Chengcecc8222007-11-17 00:40:40 +0000198 /// @brief records virtReg is a split live interval from SReg.
199 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
200 Virt2SplitMap[virtReg] = SReg;
201 }
202
203 /// @brief returns the live interval virtReg is split from.
204 unsigned getPreSplitReg(unsigned virtReg) {
205 return Virt2SplitMap[virtReg];
206 }
207
Dan Gohman7d45f4d2008-03-12 20:50:04 +0000208 /// @brief returns true if the specified virtual register is not
Evan Cheng1204d172007-08-13 23:45:17 +0000209 /// mapped to a stack slot or rematerialized.
210 bool isAssignedReg(unsigned virtReg) const {
Evan Chengcecc8222007-11-17 00:40:40 +0000211 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
212 getReMatId(virtReg) == NO_STACK_SLOT)
213 return true;
214 // Split register can be assigned a physical register as well as a
215 // stack slot or remat id.
216 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 }
218
219 /// @brief returns the stack slot mapped to the specified virtual
220 /// register
221 int getStackSlot(unsigned virtReg) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000222 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 return Virt2StackSlotMap[virtReg];
224 }
225
Evan Cheng1204d172007-08-13 23:45:17 +0000226 /// @brief returns the rematerialization id mapped to the specified virtual
227 /// register
228 int getReMatId(unsigned virtReg) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000229 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000230 return Virt2ReMatIdMap[virtReg];
231 }
232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 /// @brief create a mapping for the specifed virtual register to
234 /// the next available stack slot
235 int assignVirt2StackSlot(unsigned virtReg);
236 /// @brief create a mapping for the specified virtual register to
237 /// the specified stack slot
238 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
239
240 /// @brief assign an unique re-materialization id to the specified
241 /// virtual register.
242 int assignVirtReMatId(unsigned virtReg);
Evan Cheng1204d172007-08-13 23:45:17 +0000243 /// @brief assign an unique re-materialization id to the specified
244 /// virtual register.
245 void assignVirtReMatId(unsigned virtReg, int id);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246
247 /// @brief returns true if the specified virtual register is being
248 /// re-materialized.
249 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng1204d172007-08-13 23:45:17 +0000250 return ReMatMap[virtReg] != NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 }
252
253 /// @brief returns the original machine instruction being re-issued
254 /// to re-materialize the specified virtual register.
Evan Cheng1204d172007-08-13 23:45:17 +0000255 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 return ReMatMap[virtReg];
257 }
258
259 /// @brief records the specified virtual register will be
260 /// re-materialized and the original instruction which will be re-issed
Evan Cheng1204d172007-08-13 23:45:17 +0000261 /// for this purpose. If parameter all is true, then all uses of the
262 /// registers are rematerialized and it's safe to delete the definition.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
264 ReMatMap[virtReg] = def;
265 }
266
Evan Cheng6f522672007-12-05 09:51:10 +0000267 /// @brief record the last use (kill) of a split virtual register.
Evan Chengd9731042007-12-05 10:24:35 +0000268 void addKillPoint(unsigned virtReg, unsigned index) {
269 Virt2SplitKillMap[virtReg] = index;
Evan Cheng6f522672007-12-05 09:51:10 +0000270 }
271
Evan Chengd9731042007-12-05 10:24:35 +0000272 unsigned getKillPoint(unsigned virtReg) const {
273 return Virt2SplitKillMap[virtReg];
274 }
275
276 /// @brief remove the last use (kill) of a split virtual register.
Evan Cheng6f522672007-12-05 09:51:10 +0000277 void removeKillPoint(unsigned virtReg) {
Evan Chengd9731042007-12-05 10:24:35 +0000278 Virt2SplitKillMap[virtReg] = 0;
Evan Cheng6f522672007-12-05 09:51:10 +0000279 }
280
Evan Cheng91e32d02007-11-28 01:28:46 +0000281 /// @brief returns true if the specified MachineInstr is a spill point.
282 bool isSpillPt(MachineInstr *Pt) const {
283 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
284 }
285
Evan Chengcecc8222007-11-17 00:40:40 +0000286 /// @brief returns the virtual registers that should be spilled due to
287 /// splitting right after the specified MachineInstr.
Evan Chenged17a892007-12-05 08:16:32 +0000288 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Chengcecc8222007-11-17 00:40:40 +0000289 return SpillPt2VirtMap[Pt];
290 }
291
292 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chenged17a892007-12-05 08:16:32 +0000293 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000294 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
295 I = SpillPt2VirtMap.find(Pt);
296 if (I != SpillPt2VirtMap.end())
297 I->second.push_back(std::make_pair(virtReg, isKill));
Evan Cheng91e32d02007-11-28 01:28:46 +0000298 else {
Evan Chenged17a892007-12-05 08:16:32 +0000299 std::vector<std::pair<unsigned,bool> > Virts;
300 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Cheng91e32d02007-11-28 01:28:46 +0000301 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
302 }
Evan Chengcecc8222007-11-17 00:40:40 +0000303 }
304
Evan Cheng1eeb2ef2008-03-11 21:34:46 +0000305 /// @brief - transfer spill point information from one instruction to
306 /// another.
Evan Chengcecc8222007-11-17 00:40:40 +0000307 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000308 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
Evan Chenged17a892007-12-05 08:16:32 +0000309 I = SpillPt2VirtMap.find(Old);
Evan Cheng91e32d02007-11-28 01:28:46 +0000310 if (I == SpillPt2VirtMap.end())
311 return;
312 while (!I->second.empty()) {
Evan Chenged17a892007-12-05 08:16:32 +0000313 unsigned virtReg = I->second.back().first;
314 bool isKill = I->second.back().second;
Evan Cheng91e32d02007-11-28 01:28:46 +0000315 I->second.pop_back();
Evan Chenged17a892007-12-05 08:16:32 +0000316 addSpillPoint(virtReg, isKill, New);
Evan Chengcecc8222007-11-17 00:40:40 +0000317 }
Evan Cheng91e32d02007-11-28 01:28:46 +0000318 SpillPt2VirtMap.erase(I);
Evan Chengcecc8222007-11-17 00:40:40 +0000319 }
320
Evan Cheng96c61312007-11-29 01:06:25 +0000321 /// @brief returns true if the specified MachineInstr is a restore point.
322 bool isRestorePt(MachineInstr *Pt) const {
323 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
324 }
325
326 /// @brief returns the virtual registers that should be restoreed due to
327 /// splitting right after the specified MachineInstr.
328 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
329 return RestorePt2VirtMap[Pt];
330 }
331
332 /// @brief records the specified MachineInstr as a restore point for virtReg.
333 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000334 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
335 RestorePt2VirtMap.find(Pt);
336 if (I != RestorePt2VirtMap.end())
337 I->second.push_back(virtReg);
Evan Cheng96c61312007-11-29 01:06:25 +0000338 else {
339 std::vector<unsigned> Virts;
340 Virts.push_back(virtReg);
341 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
342 }
343 }
344
Evan Cheng14cc83f2008-03-11 07:19:34 +0000345 /// @brief - transfer restore point information from one instruction to
346 /// another.
Evan Cheng96c61312007-11-29 01:06:25 +0000347 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000348 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
Evan Cheng96c61312007-11-29 01:06:25 +0000349 RestorePt2VirtMap.find(Old);
350 if (I == RestorePt2VirtMap.end())
351 return;
352 while (!I->second.empty()) {
353 unsigned virtReg = I->second.back();
354 I->second.pop_back();
355 addRestorePoint(virtReg, New);
356 }
357 RestorePt2VirtMap.erase(I);
358 }
359
Evan Cheng14cc83f2008-03-11 07:19:34 +0000360 /// @brief records that the specified physical register must be spilled
361 /// around the specified machine instr.
362 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
363 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
364 EmergencySpillMap[MI].push_back(PhysReg);
365 else {
366 std::vector<unsigned> PhysRegs;
367 PhysRegs.push_back(PhysReg);
368 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
369 }
370 }
371
372 /// @brief returns true if one or more physical registers must be spilled
373 /// around the specified instruction.
374 bool hasEmergencySpills(MachineInstr *MI) const {
375 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
376 }
377
378 /// @brief returns the physical registers to be spilled and restored around
379 /// the instruction.
380 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
381 return EmergencySpillMap[MI];
382 }
383
Evan Cheng1eeb2ef2008-03-11 21:34:46 +0000384 /// @brief - transfer emergency spill information from one instruction to
385 /// another.
386 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
387 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
388 EmergencySpillMap.find(Old);
389 if (I == EmergencySpillMap.end())
390 return;
391 while (!I->second.empty()) {
392 unsigned virtReg = I->second.back();
393 I->second.pop_back();
394 addEmergencySpill(virtReg, New);
395 }
396 EmergencySpillMap.erase(I);
397 }
398
Evan Cheng14cc83f2008-03-11 07:19:34 +0000399 /// @brief return or get a emergency spill slot for the register class.
400 int getEmergencySpillSlot(const TargetRegisterClass *RC);
401
Evan Chengda872532008-02-27 03:04:06 +0000402 /// @brief Return lowest spill slot index.
403 int getLowSpillSlot() const {
404 return LowSpillSlot;
405 }
406
407 /// @brief Return highest spill slot index.
408 int getHighSpillSlot() const {
409 return HighSpillSlot;
410 }
411
412 /// @brief Records a spill slot use.
413 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
414
415 /// @brief Returns true if spill slot has been used.
416 bool isSpillSlotUsed(int FrameIndex) const {
417 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
418 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
419 }
420
Evan Cheng7b88cbc2008-04-11 17:53:36 +0000421 /// @brief Mark the specified register as being implicitly defined.
422 void setIsImplicitlyDefined(unsigned VirtReg) {
423 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
424 }
425
426 /// @brief Returns true if the virtual register is implicitly defined.
427 bool isImplicitlyDefined(unsigned VirtReg) const {
428 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
429 }
430
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 /// @brief Updates information about the specified virtual register's value
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000432 /// folded into newMI machine instruction.
433 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
434 ModRef MRInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435
Evan Chengf3255842007-10-13 02:50:24 +0000436 /// @brief Updates information about the specified virtual register's value
437 /// folded into the specified machine instruction.
438 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 /// @brief returns the virtual registers' values folded in memory
441 /// operands of this instruction
442 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
443 getFoldedVirts(MachineInstr* MI) const {
444 return MI2VirtMap.equal_range(MI);
445 }
446
Evan Cheng91e32d02007-11-28 01:28:46 +0000447 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
448 /// the folded instruction map and spill point map.
Evan Chengda872532008-02-27 03:04:06 +0000449 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000451 /// FindUnusedRegisters - Gather a list of allocatable registers that
452 /// have not been allocated to any virtual register.
Evan Chengd78907d2009-06-14 20:22:55 +0000453 bool FindUnusedRegisters(LiveIntervals* LIs);
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000454
455 /// HasUnusedRegisters - Return true if there are any allocatable registers
456 /// that have not been allocated to any virtual register.
457 bool HasUnusedRegisters() const {
458 return !UnusedRegs.none();
459 }
460
461 /// setRegisterUsed - Remember the physical register is now used.
462 void setRegisterUsed(unsigned Reg) {
463 UnusedRegs.reset(Reg);
464 }
465
466 /// isRegisterUnused - Return true if the physical register has not been
467 /// used.
468 bool isRegisterUnused(unsigned Reg) const {
469 return UnusedRegs[Reg];
470 }
471
472 /// getFirstUnusedRegister - Return the first physical register that has not
473 /// been used.
474 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
475 int Reg = UnusedRegs.find_first();
476 while (Reg != -1) {
Evan Cheng5277da72009-05-04 03:30:11 +0000477 if (allocatableRCRegs[RC][Reg])
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000478 return (unsigned)Reg;
479 Reg = UnusedRegs.find_next(Reg);
480 }
481 return 0;
482 }
483
Owen Andersondd56ab72009-03-13 05:55:11 +0000484 void print(std::ostream &OS, const Module* M = 0) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 void print(std::ostream *OS) const { if (OS) print(*OS); }
486 void dump() const;
487 };
488
489 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
490 VRM.print(OS);
491 return OS;
492 }
493 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
494 VRM.print(OS);
495 return OS;
496 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497} // End llvm namespace
498
499#endif