Use longer and more explicit names for instance vars (particularly important
[oota-llvm.git] / lib / CodeGen / VirtRegMap.h
1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
18 #define LLVM_CODEGEN_VIRTREGMAP_H
19
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include <map>
23
24 namespace llvm {
25   class MachineInstr;
26
27   class VirtRegMap {
28   public:
29     typedef std::multimap<MachineInstr*, unsigned> MI2VirtMapTy;
30
31   private:
32     MachineFunction &MF;
33     DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
34     DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
35     MI2VirtMapTy MI2VirtMap;
36     
37     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
38     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
39
40     enum {
41       NO_PHYS_REG = 0,
42       NO_STACK_SLOT = ~0 >> 1
43     };
44
45   public:
46     VirtRegMap(MachineFunction &mf)
47       : MF(mf), Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) {
48       grow();
49     }
50
51     void grow();
52
53     bool hasPhys(unsigned virtReg) const {
54       return getPhys(virtReg) != NO_PHYS_REG;
55     }
56
57     unsigned getPhys(unsigned virtReg) const {
58       assert(MRegisterInfo::isVirtualRegister(virtReg));
59       return Virt2PhysMap[virtReg];
60     }
61
62     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
63       assert(MRegisterInfo::isVirtualRegister(virtReg) &&
64              MRegisterInfo::isPhysicalRegister(physReg));
65       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
66              "attempt to assign physical register to already mapped "
67              "virtual register");
68       Virt2PhysMap[virtReg] = physReg;
69     }
70
71     void clearVirt(unsigned virtReg) {
72       assert(MRegisterInfo::isVirtualRegister(virtReg));
73       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
74              "attempt to clear a not assigned virtual register");
75       Virt2PhysMap[virtReg] = NO_PHYS_REG;
76     }
77
78     void clearAllVirt() {
79       Virt2PhysMap.clear();
80       grow();
81     }
82
83     bool hasStackSlot(unsigned virtReg) const {
84       return getStackSlot(virtReg) != NO_STACK_SLOT;
85     }
86
87     int getStackSlot(unsigned virtReg) const {
88       assert(MRegisterInfo::isVirtualRegister(virtReg));
89       return Virt2StackSlotMap[virtReg];
90     }
91
92     int assignVirt2StackSlot(unsigned virtReg);
93     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
94
95     void virtFolded(unsigned virtReg, MachineInstr* oldMI,
96                     MachineInstr* newMI);
97
98     std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
99     getFoldedVirts(MachineInstr* MI) const {
100       return MI2VirtMap.equal_range(MI);
101     }
102
103     void print(std::ostream &OS) const;
104     void dump() const;
105   };
106
107   inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
108     VRM.print(OS);
109     return OS;
110   }
111
112   /// Spiller interface: Implementations of this interface assign spilled
113   /// virtual registers to stack slots, rewriting the code.
114   struct Spiller {
115     virtual ~Spiller();
116     virtual bool runOnMachineFunction(MachineFunction &MF,
117                                       const VirtRegMap &VRM) = 0;
118   };
119
120   /// createSpiller - Create an return a spiller object, as specified on the
121   /// command line.
122   Spiller* createSpiller();
123
124 } // End llvm namespace
125
126 #endif