When the allocator rewrite a spill register with new virtual register, it replaces...
[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/BitVector.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include "llvm/Support/Streams.h"
24 #include <map>
25
26 namespace llvm {
27   class MachineInstr;
28   class MachineFunction;
29   class TargetInstrInfo;
30
31   class VirtRegMap {
32   public:
33     enum {
34       NO_PHYS_REG = 0,
35       NO_STACK_SLOT = (1L << 30)-1,
36       MAX_STACK_SLOT = (1L << 18)-1
37     };
38
39     enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
40     typedef std::multimap<MachineInstr*,
41                           std::pair<unsigned, ModRef> > MI2VirtMapTy;
42
43   private:
44     const TargetInstrInfo &TII;
45
46     MachineFunction &MF;
47     /// Virt2PhysMap - This is a virtual to physical register
48     /// mapping. Each virtual register is required to have an entry in
49     /// it; even spilled virtual registers (the register mapped to a
50     /// spilled register is the temporary used to load it from the
51     /// stack).
52     IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
53     /// Virt2StackSlotMap - This is virtual register to stack slot
54     /// mapping. Each spilled virtual register has an entry in it
55     /// which corresponds to the stack slot this register is spilled
56     /// at.
57     IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
58     IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
59     /// MI2VirtMap - This is MachineInstr to virtual register
60     /// mapping. In the case of memory spill code being folded into
61     /// instructions, we need to know which virtual register was
62     /// read/written by this instruction.
63     MI2VirtMapTy MI2VirtMap;
64
65     /// ReMatMap - This is virtual register to re-materialized instruction
66     /// mapping. Each virtual register whose definition is going to be
67     /// re-materialized has an entry in it.
68     IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
69
70     /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
71     /// virtual register, an unique id is being assigned. This keeps track of
72     /// the highest id used so far. Note, this starts at (1<<18) to avoid
73     /// conflicts with stack slot numbers.
74     int ReMatId;
75
76     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
77     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
78
79   public:
80     explicit VirtRegMap(MachineFunction &mf);
81
82     void grow();
83
84     /// @brief returns true if the specified virtual register is
85     /// mapped to a physical register
86     bool hasPhys(unsigned virtReg) const {
87       return getPhys(virtReg) != NO_PHYS_REG;
88     }
89
90     /// @brief returns the physical register mapped to the specified
91     /// virtual register
92     unsigned getPhys(unsigned virtReg) const {
93       assert(MRegisterInfo::isVirtualRegister(virtReg));
94       return Virt2PhysMap[virtReg];
95     }
96
97     /// @brief creates a mapping for the specified virtual register to
98     /// the specified physical register
99     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
100       assert(MRegisterInfo::isVirtualRegister(virtReg) &&
101              MRegisterInfo::isPhysicalRegister(physReg));
102       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
103              "attempt to assign physical register to already mapped "
104              "virtual register");
105       Virt2PhysMap[virtReg] = physReg;
106     }
107
108     /// @brief clears the specified virtual register's, physical
109     /// register mapping
110     void clearVirt(unsigned virtReg) {
111       assert(MRegisterInfo::isVirtualRegister(virtReg));
112       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
113              "attempt to clear a not assigned virtual register");
114       Virt2PhysMap[virtReg] = NO_PHYS_REG;
115     }
116
117     /// @brief clears all virtual to physical register mappings
118     void clearAllVirt() {
119       Virt2PhysMap.clear();
120       grow();
121     }
122
123     /// @brief returns true is the specified virtual register is not
124     /// mapped to a stack slot or rematerialized.
125     bool isAssignedReg(unsigned virtReg) const {
126       return getStackSlot(virtReg) == NO_STACK_SLOT &&
127         getReMatId(virtReg) == NO_STACK_SLOT;
128     }
129
130     /// @brief returns the stack slot mapped to the specified virtual
131     /// register
132     int getStackSlot(unsigned virtReg) const {
133       assert(MRegisterInfo::isVirtualRegister(virtReg));
134       return Virt2StackSlotMap[virtReg];
135     }
136
137     /// @brief returns the rematerialization id mapped to the specified virtual
138     /// register
139     int getReMatId(unsigned virtReg) const {
140       assert(MRegisterInfo::isVirtualRegister(virtReg));
141       return Virt2ReMatIdMap[virtReg];
142     }
143
144     /// @brief create a mapping for the specifed virtual register to
145     /// the next available stack slot
146     int assignVirt2StackSlot(unsigned virtReg);
147     /// @brief create a mapping for the specified virtual register to
148     /// the specified stack slot
149     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
150
151     /// @brief assign an unique re-materialization id to the specified
152     /// virtual register.
153     int assignVirtReMatId(unsigned virtReg);
154     /// @brief assign an unique re-materialization id to the specified
155     /// virtual register.
156     void assignVirtReMatId(unsigned virtReg, int id);
157
158     /// @brief returns true if the specified virtual register is being
159     /// re-materialized.
160     bool isReMaterialized(unsigned virtReg) const {
161       return ReMatMap[virtReg] != NULL;
162     }
163
164     /// @brief returns the original machine instruction being re-issued
165     /// to re-materialize the specified virtual register.
166     MachineInstr *getReMaterializedMI(unsigned virtReg) const {
167       return ReMatMap[virtReg];
168     }
169
170     /// @brief records the specified virtual register will be
171     /// re-materialized and the original instruction which will be re-issed
172     /// for this purpose.  If parameter all is true, then all uses of the
173     /// registers are rematerialized and it's safe to delete the definition.
174     void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
175       ReMatMap[virtReg] = def;
176     }
177
178     /// @brief Updates information about the specified virtual register's value
179     /// folded into newMI machine instruction.  The OpNum argument indicates the
180     /// operand number of OldMI that is folded.
181     void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
182                     MachineInstr *NewMI);
183
184     /// @brief Updates information about the specified virtual register's value
185     /// folded into the specified machine instruction.
186     void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
187
188     /// @brief returns the virtual registers' values folded in memory
189     /// operands of this instruction
190     std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
191     getFoldedVirts(MachineInstr* MI) const {
192       return MI2VirtMap.equal_range(MI);
193     }
194     
195     /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
196     /// the folded instruction map, remove its entry from the map.
197     void RemoveFromFoldedVirtMap(MachineInstr *MI) {
198       MI2VirtMap.erase(MI);
199     }
200
201     void print(std::ostream &OS) const;
202     void print(std::ostream *OS) const { if (OS) print(*OS); }
203     void dump() const;
204   };
205
206   inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
207     VRM.print(OS);
208     return OS;
209   }
210   inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
211     VRM.print(OS);
212     return OS;
213   }
214
215   /// Spiller interface: Implementations of this interface assign spilled
216   /// virtual registers to stack slots, rewriting the code.
217   struct Spiller {
218     virtual ~Spiller();
219     virtual bool runOnMachineFunction(MachineFunction &MF,
220                                       VirtRegMap &VRM) = 0;
221   };
222
223   /// createSpiller - Create an return a spiller object, as specified on the
224   /// command line.
225   Spiller* createSpiller();
226
227 } // End llvm namespace
228
229 #endif