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 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
86 /// RestorePt2VirtMap - This records the virtual registers which should
87 /// be restored right before the MachineInstr due to live interval
89 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
91 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
92 /// virtual register, an unique id is being assigned. This keeps track of
93 /// the highest id used so far. Note, this starts at (1<<18) to avoid
94 /// conflicts with stack slot numbers.
97 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
98 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
101 explicit VirtRegMap(MachineFunction &mf);
105 /// @brief returns true if the specified virtual register is
106 /// mapped to a physical register
107 bool hasPhys(unsigned virtReg) const {
108 return getPhys(virtReg) != NO_PHYS_REG;
111 /// @brief returns the physical register mapped to the specified
113 unsigned getPhys(unsigned virtReg) const {
114 assert(MRegisterInfo::isVirtualRegister(virtReg));
115 return Virt2PhysMap[virtReg];
118 /// @brief creates a mapping for the specified virtual register to
119 /// the specified physical register
120 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
121 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
122 MRegisterInfo::isPhysicalRegister(physReg));
123 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
124 "attempt to assign physical register to already mapped "
126 Virt2PhysMap[virtReg] = physReg;
129 /// @brief clears the specified virtual register's, physical
131 void clearVirt(unsigned virtReg) {
132 assert(MRegisterInfo::isVirtualRegister(virtReg));
133 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
134 "attempt to clear a not assigned virtual register");
135 Virt2PhysMap[virtReg] = NO_PHYS_REG;
138 /// @brief clears all virtual to physical register mappings
139 void clearAllVirt() {
140 Virt2PhysMap.clear();
144 /// @brief records virtReg is a split live interval from SReg.
145 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
146 Virt2SplitMap[virtReg] = SReg;
149 /// @brief returns the live interval virtReg is split from.
150 unsigned getPreSplitReg(unsigned virtReg) {
151 return Virt2SplitMap[virtReg];
154 /// @brief returns true is the specified virtual register is not
155 /// mapped to a stack slot or rematerialized.
156 bool isAssignedReg(unsigned virtReg) const {
157 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
158 getReMatId(virtReg) == NO_STACK_SLOT)
160 // Split register can be assigned a physical register as well as a
161 // stack slot or remat id.
162 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
165 /// @brief returns the stack slot mapped to the specified virtual
167 int getStackSlot(unsigned virtReg) const {
168 assert(MRegisterInfo::isVirtualRegister(virtReg));
169 return Virt2StackSlotMap[virtReg];
172 /// @brief returns the rematerialization id mapped to the specified virtual
174 int getReMatId(unsigned virtReg) const {
175 assert(MRegisterInfo::isVirtualRegister(virtReg));
176 return Virt2ReMatIdMap[virtReg];
179 /// @brief create a mapping for the specifed virtual register to
180 /// the next available stack slot
181 int assignVirt2StackSlot(unsigned virtReg);
182 /// @brief create a mapping for the specified virtual register to
183 /// the specified stack slot
184 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
186 /// @brief assign an unique re-materialization id to the specified
187 /// virtual register.
188 int assignVirtReMatId(unsigned virtReg);
189 /// @brief assign an unique re-materialization id to the specified
190 /// virtual register.
191 void assignVirtReMatId(unsigned virtReg, int id);
193 /// @brief returns true if the specified virtual register is being
195 bool isReMaterialized(unsigned virtReg) const {
196 return ReMatMap[virtReg] != NULL;
199 /// @brief returns the original machine instruction being re-issued
200 /// to re-materialize the specified virtual register.
201 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
202 return ReMatMap[virtReg];
205 /// @brief records the specified virtual register will be
206 /// re-materialized and the original instruction which will be re-issed
207 /// for this purpose. If parameter all is true, then all uses of the
208 /// registers are rematerialized and it's safe to delete the definition.
209 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
210 ReMatMap[virtReg] = def;
213 /// @brief returns true if the specified MachineInstr is a spill point.
214 bool isSpillPt(MachineInstr *Pt) const {
215 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
218 /// @brief returns the virtual registers that should be spilled due to
219 /// splitting right after the specified MachineInstr.
220 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
221 return SpillPt2VirtMap[Pt];
224 /// @brief records the specified MachineInstr as a spill point for virtReg.
225 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
226 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
227 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
229 std::vector<std::pair<unsigned,bool> > Virts;
230 Virts.push_back(std::make_pair(virtReg, isKill));
231 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
235 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
236 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
237 I = SpillPt2VirtMap.find(Old);
238 if (I == SpillPt2VirtMap.end())
240 while (!I->second.empty()) {
241 unsigned virtReg = I->second.back().first;
242 bool isKill = I->second.back().second;
243 I->second.pop_back();
244 addSpillPoint(virtReg, isKill, New);
246 SpillPt2VirtMap.erase(I);
249 /// @brief returns true if the specified MachineInstr is a restore point.
250 bool isRestorePt(MachineInstr *Pt) const {
251 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
254 /// @brief returns the virtual registers that should be restoreed due to
255 /// splitting right after the specified MachineInstr.
256 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
257 return RestorePt2VirtMap[Pt];
260 /// @brief records the specified MachineInstr as a restore point for virtReg.
261 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
262 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
263 RestorePt2VirtMap[Pt].push_back(virtReg);
265 std::vector<unsigned> Virts;
266 Virts.push_back(virtReg);
267 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
271 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
272 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
273 RestorePt2VirtMap.find(Old);
274 if (I == RestorePt2VirtMap.end())
276 while (!I->second.empty()) {
277 unsigned virtReg = I->second.back();
278 I->second.pop_back();
279 addRestorePoint(virtReg, New);
281 RestorePt2VirtMap.erase(I);
284 /// @brief Updates information about the specified virtual register's value
285 /// folded into newMI machine instruction.
286 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
289 /// @brief Updates information about the specified virtual register's value
290 /// folded into the specified machine instruction.
291 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
293 /// @brief returns the virtual registers' values folded in memory
294 /// operands of this instruction
295 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
296 getFoldedVirts(MachineInstr* MI) const {
297 return MI2VirtMap.equal_range(MI);
300 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
301 /// the folded instruction map and spill point map.
302 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
303 MI2VirtMap.erase(MI);
304 SpillPt2VirtMap.erase(MI);
305 RestorePt2VirtMap.erase(MI);
308 void print(std::ostream &OS) const;
309 void print(std::ostream *OS) const { if (OS) print(*OS); }
313 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
317 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
322 /// Spiller interface: Implementations of this interface assign spilled
323 /// virtual registers to stack slots, rewriting the code.
326 virtual bool runOnMachineFunction(MachineFunction &MF,
327 VirtRegMap &VRM) = 0;
330 /// createSpiller - Create an return a spiller object, as specified on the
332 Spiller* createSpiller();
334 } // End llvm namespace