1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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/Target/MRegisterInfo.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include "llvm/Support/Streams.h"
28 class MachineFunction;
29 class TargetInstrInfo;
35 NO_STACK_SLOT = (1L << 30)-1,
36 MAX_STACK_SLOT = (1L << 18)-1
39 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
40 typedef std::multimap<MachineInstr*,
41 std::pair<unsigned, ModRef> > MI2VirtMapTy;
44 const TargetInstrInfo &TII;
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
52 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
54 /// Virt2StackSlotMap - This is virtual register to stack slot
55 /// mapping. Each spilled virtual register has an entry in it
56 /// which corresponds to the stack slot this register is spilled
58 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
60 /// Virt2StackSlotMap - This is virtual register to rematerialization id
61 /// mapping. Each spilled virtual register that should be remat'd has an
62 /// entry in it which corresponds to the remat id.
63 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
65 /// Virt2SplitMap - This is virtual register to splitted virtual register
67 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
69 /// ReMatMap - This is virtual register to re-materialized instruction
70 /// mapping. Each virtual register whose definition is going to be
71 /// re-materialized has an entry in it.
72 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
74 /// MI2VirtMap - This is MachineInstr to virtual register
75 /// mapping. In the case of memory spill code being folded into
76 /// instructions, we need to know which virtual register was
77 /// read/written by this instruction.
78 MI2VirtMapTy MI2VirtMap;
80 /// SpillPt2VirtMap - This records the virtual registers which should
81 /// be spilled right after the MachineInstr due to live interval
83 DenseMap<MachineInstr*, std::vector<unsigned> > SpillPt2VirtMap;
85 /// Virt2SplitMap - This records the MachineInstrs where a virtual
86 /// register should be spilled due to live interval splitting.
87 IndexedMap<std::vector<MachineInstr*>, VirtReg2IndexFunctor>
90 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
91 /// virtual register, an unique id is being assigned. This keeps track of
92 /// the highest id used so far. Note, this starts at (1<<18) to avoid
93 /// conflicts with stack slot numbers.
96 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
97 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
100 explicit VirtRegMap(MachineFunction &mf);
104 /// @brief returns true if the specified virtual register is
105 /// mapped to a physical register
106 bool hasPhys(unsigned virtReg) const {
107 return getPhys(virtReg) != NO_PHYS_REG;
110 /// @brief returns the physical register mapped to the specified
112 unsigned getPhys(unsigned virtReg) const {
113 assert(MRegisterInfo::isVirtualRegister(virtReg));
114 return Virt2PhysMap[virtReg];
117 /// @brief creates a mapping for the specified virtual register to
118 /// the specified physical register
119 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
120 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
121 MRegisterInfo::isPhysicalRegister(physReg));
122 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
123 "attempt to assign physical register to already mapped "
125 Virt2PhysMap[virtReg] = physReg;
128 /// @brief clears the specified virtual register's, physical
130 void clearVirt(unsigned virtReg) {
131 assert(MRegisterInfo::isVirtualRegister(virtReg));
132 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
133 "attempt to clear a not assigned virtual register");
134 Virt2PhysMap[virtReg] = NO_PHYS_REG;
137 /// @brief clears all virtual to physical register mappings
138 void clearAllVirt() {
139 Virt2PhysMap.clear();
143 /// @brief records virtReg is a split live interval from SReg.
144 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
145 Virt2SplitMap[virtReg] = SReg;
148 /// @brief returns the live interval virtReg is split from.
149 unsigned getPreSplitReg(unsigned virtReg) {
150 return Virt2SplitMap[virtReg];
153 /// @brief returns true is the specified virtual register is not
154 /// mapped to a stack slot or rematerialized.
155 bool isAssignedReg(unsigned virtReg) const {
156 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
157 getReMatId(virtReg) == NO_STACK_SLOT)
159 // Split register can be assigned a physical register as well as a
160 // stack slot or remat id.
161 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
164 /// @brief returns the stack slot mapped to the specified virtual
166 int getStackSlot(unsigned virtReg) const {
167 assert(MRegisterInfo::isVirtualRegister(virtReg));
168 return Virt2StackSlotMap[virtReg];
171 /// @brief returns the rematerialization id mapped to the specified virtual
173 int getReMatId(unsigned virtReg) const {
174 assert(MRegisterInfo::isVirtualRegister(virtReg));
175 return Virt2ReMatIdMap[virtReg];
178 /// @brief create a mapping for the specifed virtual register to
179 /// the next available stack slot
180 int assignVirt2StackSlot(unsigned virtReg);
181 /// @brief create a mapping for the specified virtual register to
182 /// the specified stack slot
183 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
185 /// @brief assign an unique re-materialization id to the specified
186 /// virtual register.
187 int assignVirtReMatId(unsigned virtReg);
188 /// @brief assign an unique re-materialization id to the specified
189 /// virtual register.
190 void assignVirtReMatId(unsigned virtReg, int id);
192 /// @brief returns true if the specified virtual register is being
194 bool isReMaterialized(unsigned virtReg) const {
195 return ReMatMap[virtReg] != NULL;
198 /// @brief returns the original machine instruction being re-issued
199 /// to re-materialize the specified virtual register.
200 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
201 return ReMatMap[virtReg];
204 /// @brief records the specified virtual register will be
205 /// re-materialized and the original instruction which will be re-issed
206 /// for this purpose. If parameter all is true, then all uses of the
207 /// registers are rematerialized and it's safe to delete the definition.
208 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
209 ReMatMap[virtReg] = def;
212 /// @brief returns the virtual registers that should be spilled due to
213 /// splitting right after the specified MachineInstr.
214 std::vector<unsigned> &getSpillPtSpills(MachineInstr *Pt) {
215 return SpillPt2VirtMap[Pt];
218 /// @brief records the specified MachineInstr as a spill point for virtReg.
219 void addSpillPoint(unsigned virtReg, MachineInstr *Pt) {
220 SpillPt2VirtMap[Pt].push_back(virtReg);
221 Virt2SpillPtsMap[virtReg].push_back(Pt);
224 /// @brief remove the virtReg from the list of registers that should be
225 /// spilled (due to splitting) right after the specified MachineInstr.
226 void removeRegFromSpillPt(MachineInstr *Pt, unsigned virtReg) {
227 std::vector<unsigned> &Regs = SpillPt2VirtMap[Pt];
228 if (Regs.back() == virtReg) // Most common case.
230 for (unsigned i = 0, e = Regs.size(); i != e; ++i)
231 if (Regs[i] == virtReg) {
232 Regs.erase(Regs.begin()+i-1);
237 /// @brief specify virtReg is no longer being spilled due to splitting.
238 void removeAllSpillPtsForReg(unsigned virtReg) {
239 std::vector<MachineInstr*> &SpillPts = Virt2SpillPtsMap[virtReg];
240 for (unsigned i = 0, e = SpillPts.size(); i != e; ++i)
241 removeRegFromSpillPt(SpillPts[i], virtReg);
242 Virt2SpillPtsMap[virtReg].clear();
245 /// @brief remove the specified MachineInstr as a spill point for the
246 /// specified register.
247 void removeRegSpillPt(unsigned virtReg, MachineInstr *Pt) {
248 std::vector<MachineInstr*> &SpillPts = Virt2SpillPtsMap[virtReg];
249 if (SpillPts.back() == Pt) // Most common case.
251 for (unsigned i = 0, e = SpillPts.size(); i != e; ++i)
252 if (SpillPts[i] == Pt) {
253 SpillPts.erase(SpillPts.begin()+i-1);
258 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
259 std::vector<unsigned> &OldRegs = SpillPt2VirtMap[Old];
260 while (!OldRegs.empty()) {
261 unsigned virtReg = OldRegs.back();
263 removeRegSpillPt(virtReg, Old);
264 addSpillPoint(virtReg, New);
268 /// @brief Updates information about the specified virtual register's value
269 /// folded into newMI machine instruction. The OpNum argument indicates the
270 /// operand number of OldMI that is folded.
271 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
272 MachineInstr *NewMI);
274 /// @brief Updates information about the specified virtual register's value
275 /// folded into the specified machine instruction.
276 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
278 /// @brief returns the virtual registers' values folded in memory
279 /// operands of this instruction
280 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
281 getFoldedVirts(MachineInstr* MI) const {
282 return MI2VirtMap.equal_range(MI);
285 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
286 /// the folded instruction map, remove its entry from the map.
287 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
288 MI2VirtMap.erase(MI);
291 void print(std::ostream &OS) const;
292 void print(std::ostream *OS) const { if (OS) print(*OS); }
296 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
300 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
305 /// Spiller interface: Implementations of this interface assign spilled
306 /// virtual registers to stack slots, rewriting the code.
309 virtual bool runOnMachineFunction(MachineFunction &MF,
310 VirtRegMap &VRM) = 0;
313 /// createSpiller - Create an return a spiller object, as specified on the
315 Spiller* createSpiller();
317 } // End llvm namespace