Quotes should be printed before private prefix; some code clean up.
[llvm/msp430.git] / lib / CodeGen / VirtRegMap.h
blob507557d24c08085625d79273a1a467c23bc54b8e
1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
18 #define LLVM_CODEGEN_VIRTREGMAP_H
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include "llvm/ADT/BitVector.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/IndexedMap.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/Streams.h"
28 #include <map>
30 namespace llvm {
31 class LiveIntervals;
32 class MachineInstr;
33 class MachineFunction;
34 class TargetInstrInfo;
35 class TargetRegisterInfo;
37 class VirtRegMap : public MachineFunctionPass {
38 public:
39 enum {
40 NO_PHYS_REG = 0,
41 NO_STACK_SLOT = (1L << 30)-1,
42 MAX_STACK_SLOT = (1L << 18)-1
45 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
46 typedef std::multimap<MachineInstr*,
47 std::pair<unsigned, ModRef> > MI2VirtMapTy;
49 private:
50 const TargetInstrInfo *TII;
51 const TargetRegisterInfo *TRI;
52 MachineFunction *MF;
54 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
56 /// Virt2PhysMap - This is a virtual to physical register
57 /// mapping. Each virtual register is required to have an entry in
58 /// it; even spilled virtual registers (the register mapped to a
59 /// spilled register is the temporary used to load it from the
60 /// stack).
61 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
63 /// Virt2StackSlotMap - This is virtual register to stack slot
64 /// mapping. Each spilled virtual register has an entry in it
65 /// which corresponds to the stack slot this register is spilled
66 /// at.
67 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
69 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
70 /// mapping. Each spilled virtual register that should be remat'd has an
71 /// entry in it which corresponds to the remat id.
72 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
74 /// Virt2SplitMap - This is virtual register to splitted virtual register
75 /// mapping.
76 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
78 /// Virt2SplitKillMap - This is splitted virtual register to its last use
79 /// (kill) index mapping.
80 IndexedMap<unsigned> Virt2SplitKillMap;
82 /// ReMatMap - This is virtual register to re-materialized instruction
83 /// mapping. Each virtual register whose definition is going to be
84 /// re-materialized has an entry in it.
85 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
87 /// MI2VirtMap - This is MachineInstr to virtual register
88 /// mapping. In the case of memory spill code being folded into
89 /// instructions, we need to know which virtual register was
90 /// read/written by this instruction.
91 MI2VirtMapTy MI2VirtMap;
93 /// SpillPt2VirtMap - This records the virtual registers which should
94 /// be spilled right after the MachineInstr due to live interval
95 /// splitting.
96 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
97 SpillPt2VirtMap;
99 /// RestorePt2VirtMap - This records the virtual registers which should
100 /// be restored right before the MachineInstr due to live interval
101 /// splitting.
102 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
104 /// EmergencySpillMap - This records the physical registers that should
105 /// be spilled / restored around the MachineInstr since the register
106 /// allocator has run out of registers.
107 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
109 /// EmergencySpillSlots - This records emergency spill slots used to
110 /// spill physical registers when the register allocator runs out of
111 /// registers. Ideally only one stack slot is used per function per
112 /// register class.
113 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
115 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
116 /// virtual register, an unique id is being assigned. This keeps track of
117 /// the highest id used so far. Note, this starts at (1<<18) to avoid
118 /// conflicts with stack slot numbers.
119 int ReMatId;
121 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
122 int LowSpillSlot, HighSpillSlot;
124 /// SpillSlotToUsesMap - Records uses for each register spill slot.
125 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
127 /// ImplicitDefed - One bit for each virtual register. If set it indicates
128 /// the register is implicitly defined.
129 BitVector ImplicitDefed;
131 /// UnusedRegs - A list of physical registers that have not been used.
132 BitVector UnusedRegs;
134 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
135 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
137 public:
138 static char ID;
139 VirtRegMap() : MachineFunctionPass(&ID), Virt2PhysMap(NO_PHYS_REG),
140 Virt2StackSlotMap(NO_STACK_SLOT),
141 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
142 Virt2SplitKillMap(0), ReMatMap(NULL),
143 ReMatId(MAX_STACK_SLOT+1),
144 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
145 virtual bool runOnMachineFunction(MachineFunction &MF);
147 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
148 AU.setPreservesAll();
149 MachineFunctionPass::getAnalysisUsage(AU);
152 void grow();
154 /// @brief returns true if the specified virtual register is
155 /// mapped to a physical register
156 bool hasPhys(unsigned virtReg) const {
157 return getPhys(virtReg) != NO_PHYS_REG;
160 /// @brief returns the physical register mapped to the specified
161 /// virtual register
162 unsigned getPhys(unsigned virtReg) const {
163 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
164 return Virt2PhysMap[virtReg];
167 /// @brief creates a mapping for the specified virtual register to
168 /// the specified physical register
169 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
170 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
171 TargetRegisterInfo::isPhysicalRegister(physReg));
172 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
173 "attempt to assign physical register to already mapped "
174 "virtual register");
175 Virt2PhysMap[virtReg] = physReg;
178 /// @brief clears the specified virtual register's, physical
179 /// register mapping
180 void clearVirt(unsigned virtReg) {
181 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
182 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
183 "attempt to clear a not assigned virtual register");
184 Virt2PhysMap[virtReg] = NO_PHYS_REG;
187 /// @brief clears all virtual to physical register mappings
188 void clearAllVirt() {
189 Virt2PhysMap.clear();
190 grow();
193 /// @brief records virtReg is a split live interval from SReg.
194 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
195 Virt2SplitMap[virtReg] = SReg;
198 /// @brief returns the live interval virtReg is split from.
199 unsigned getPreSplitReg(unsigned virtReg) {
200 return Virt2SplitMap[virtReg];
203 /// @brief returns true if the specified virtual register is not
204 /// mapped to a stack slot or rematerialized.
205 bool isAssignedReg(unsigned virtReg) const {
206 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
207 getReMatId(virtReg) == NO_STACK_SLOT)
208 return true;
209 // Split register can be assigned a physical register as well as a
210 // stack slot or remat id.
211 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
214 /// @brief returns the stack slot mapped to the specified virtual
215 /// register
216 int getStackSlot(unsigned virtReg) const {
217 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
218 return Virt2StackSlotMap[virtReg];
221 /// @brief returns the rematerialization id mapped to the specified virtual
222 /// register
223 int getReMatId(unsigned virtReg) const {
224 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
225 return Virt2ReMatIdMap[virtReg];
228 /// @brief create a mapping for the specifed virtual register to
229 /// the next available stack slot
230 int assignVirt2StackSlot(unsigned virtReg);
231 /// @brief create a mapping for the specified virtual register to
232 /// the specified stack slot
233 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
235 /// @brief assign an unique re-materialization id to the specified
236 /// virtual register.
237 int assignVirtReMatId(unsigned virtReg);
238 /// @brief assign an unique re-materialization id to the specified
239 /// virtual register.
240 void assignVirtReMatId(unsigned virtReg, int id);
242 /// @brief returns true if the specified virtual register is being
243 /// re-materialized.
244 bool isReMaterialized(unsigned virtReg) const {
245 return ReMatMap[virtReg] != NULL;
248 /// @brief returns the original machine instruction being re-issued
249 /// to re-materialize the specified virtual register.
250 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
251 return ReMatMap[virtReg];
254 /// @brief records the specified virtual register will be
255 /// re-materialized and the original instruction which will be re-issed
256 /// for this purpose. If parameter all is true, then all uses of the
257 /// registers are rematerialized and it's safe to delete the definition.
258 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
259 ReMatMap[virtReg] = def;
262 /// @brief record the last use (kill) of a split virtual register.
263 void addKillPoint(unsigned virtReg, unsigned index) {
264 Virt2SplitKillMap[virtReg] = index;
267 unsigned getKillPoint(unsigned virtReg) const {
268 return Virt2SplitKillMap[virtReg];
271 /// @brief remove the last use (kill) of a split virtual register.
272 void removeKillPoint(unsigned virtReg) {
273 Virt2SplitKillMap[virtReg] = 0;
276 /// @brief returns true if the specified MachineInstr is a spill point.
277 bool isSpillPt(MachineInstr *Pt) const {
278 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
281 /// @brief returns the virtual registers that should be spilled due to
282 /// splitting right after the specified MachineInstr.
283 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
284 return SpillPt2VirtMap[Pt];
287 /// @brief records the specified MachineInstr as a spill point for virtReg.
288 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
289 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
290 I = SpillPt2VirtMap.find(Pt);
291 if (I != SpillPt2VirtMap.end())
292 I->second.push_back(std::make_pair(virtReg, isKill));
293 else {
294 std::vector<std::pair<unsigned,bool> > Virts;
295 Virts.push_back(std::make_pair(virtReg, isKill));
296 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
300 /// @brief - transfer spill point information from one instruction to
301 /// another.
302 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
303 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
304 I = SpillPt2VirtMap.find(Old);
305 if (I == SpillPt2VirtMap.end())
306 return;
307 while (!I->second.empty()) {
308 unsigned virtReg = I->second.back().first;
309 bool isKill = I->second.back().second;
310 I->second.pop_back();
311 addSpillPoint(virtReg, isKill, New);
313 SpillPt2VirtMap.erase(I);
316 /// @brief returns true if the specified MachineInstr is a restore point.
317 bool isRestorePt(MachineInstr *Pt) const {
318 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
321 /// @brief returns the virtual registers that should be restoreed due to
322 /// splitting right after the specified MachineInstr.
323 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
324 return RestorePt2VirtMap[Pt];
327 /// @brief records the specified MachineInstr as a restore point for virtReg.
328 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
329 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
330 RestorePt2VirtMap.find(Pt);
331 if (I != RestorePt2VirtMap.end())
332 I->second.push_back(virtReg);
333 else {
334 std::vector<unsigned> Virts;
335 Virts.push_back(virtReg);
336 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
340 /// @brief - transfer restore point information from one instruction to
341 /// another.
342 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
343 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
344 RestorePt2VirtMap.find(Old);
345 if (I == RestorePt2VirtMap.end())
346 return;
347 while (!I->second.empty()) {
348 unsigned virtReg = I->second.back();
349 I->second.pop_back();
350 addRestorePoint(virtReg, New);
352 RestorePt2VirtMap.erase(I);
355 /// @brief records that the specified physical register must be spilled
356 /// around the specified machine instr.
357 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
358 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
359 EmergencySpillMap[MI].push_back(PhysReg);
360 else {
361 std::vector<unsigned> PhysRegs;
362 PhysRegs.push_back(PhysReg);
363 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
367 /// @brief returns true if one or more physical registers must be spilled
368 /// around the specified instruction.
369 bool hasEmergencySpills(MachineInstr *MI) const {
370 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
373 /// @brief returns the physical registers to be spilled and restored around
374 /// the instruction.
375 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
376 return EmergencySpillMap[MI];
379 /// @brief - transfer emergency spill information from one instruction to
380 /// another.
381 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
382 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
383 EmergencySpillMap.find(Old);
384 if (I == EmergencySpillMap.end())
385 return;
386 while (!I->second.empty()) {
387 unsigned virtReg = I->second.back();
388 I->second.pop_back();
389 addEmergencySpill(virtReg, New);
391 EmergencySpillMap.erase(I);
394 /// @brief return or get a emergency spill slot for the register class.
395 int getEmergencySpillSlot(const TargetRegisterClass *RC);
397 /// @brief Return lowest spill slot index.
398 int getLowSpillSlot() const {
399 return LowSpillSlot;
402 /// @brief Return highest spill slot index.
403 int getHighSpillSlot() const {
404 return HighSpillSlot;
407 /// @brief Records a spill slot use.
408 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
410 /// @brief Returns true if spill slot has been used.
411 bool isSpillSlotUsed(int FrameIndex) const {
412 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
413 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
416 /// @brief Mark the specified register as being implicitly defined.
417 void setIsImplicitlyDefined(unsigned VirtReg) {
418 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
421 /// @brief Returns true if the virtual register is implicitly defined.
422 bool isImplicitlyDefined(unsigned VirtReg) const {
423 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
426 /// @brief Updates information about the specified virtual register's value
427 /// folded into newMI machine instruction.
428 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
429 ModRef MRInfo);
431 /// @brief Updates information about the specified virtual register's value
432 /// folded into the specified machine instruction.
433 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
435 /// @brief returns the virtual registers' values folded in memory
436 /// operands of this instruction
437 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
438 getFoldedVirts(MachineInstr* MI) const {
439 return MI2VirtMap.equal_range(MI);
442 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
443 /// the folded instruction map and spill point map.
444 void RemoveMachineInstrFromMaps(MachineInstr *MI);
446 /// FindUnusedRegisters - Gather a list of allocatable registers that
447 /// have not been allocated to any virtual register.
448 bool FindUnusedRegisters(const TargetRegisterInfo *TRI,
449 LiveIntervals* LIs);
451 /// HasUnusedRegisters - Return true if there are any allocatable registers
452 /// that have not been allocated to any virtual register.
453 bool HasUnusedRegisters() const {
454 return !UnusedRegs.none();
457 /// setRegisterUsed - Remember the physical register is now used.
458 void setRegisterUsed(unsigned Reg) {
459 UnusedRegs.reset(Reg);
462 /// isRegisterUnused - Return true if the physical register has not been
463 /// used.
464 bool isRegisterUnused(unsigned Reg) const {
465 return UnusedRegs[Reg];
468 /// getFirstUnusedRegister - Return the first physical register that has not
469 /// been used.
470 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
471 int Reg = UnusedRegs.find_first();
472 while (Reg != -1) {
473 if (allocatableRCRegs[RC][Reg])
474 return (unsigned)Reg;
475 Reg = UnusedRegs.find_next(Reg);
477 return 0;
480 void print(std::ostream &OS, const Module* M = 0) const;
481 void print(std::ostream *OS) const { if (OS) print(*OS); }
482 void dump() const;
485 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
486 VRM.print(OS);
487 return OS;
489 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
490 VRM.print(OS);
491 return OS;
493 } // End llvm namespace
495 #endif