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 /// Virt2SplitKillMap - This is splitted virtual register to its last use
70 /// (kill) index mapping.
71 IndexedMap<unsigned> Virt2SplitKillMap;
73 /// ReMatMap - This is virtual register to re-materialized instruction
74 /// mapping. Each virtual register whose definition is going to be
75 /// re-materialized has an entry in it.
76 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
78 /// MI2VirtMap - This is MachineInstr to virtual register
79 /// mapping. In the case of memory spill code being folded into
80 /// instructions, we need to know which virtual register was
81 /// read/written by this instruction.
82 MI2VirtMapTy MI2VirtMap;
84 /// SpillPt2VirtMap - This records the virtual registers which should
85 /// be spilled right after the MachineInstr due to live interval
87 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
90 /// RestorePt2VirtMap - This records the virtual registers which should
91 /// be restored right before the MachineInstr due to live interval
93 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
95 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
96 /// virtual register, an unique id is being assigned. This keeps track of
97 /// the highest id used so far. Note, this starts at (1<<18) to avoid
98 /// conflicts with stack slot numbers.
101 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
102 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
105 explicit VirtRegMap(MachineFunction &mf);
109 /// @brief returns true if the specified virtual register is
110 /// mapped to a physical register
111 bool hasPhys(unsigned virtReg) const {
112 return getPhys(virtReg) != NO_PHYS_REG;
115 /// @brief returns the physical register mapped to the specified
117 unsigned getPhys(unsigned virtReg) const {
118 assert(MRegisterInfo::isVirtualRegister(virtReg));
119 return Virt2PhysMap[virtReg];
122 /// @brief creates a mapping for the specified virtual register to
123 /// the specified physical register
124 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
125 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
126 MRegisterInfo::isPhysicalRegister(physReg));
127 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
128 "attempt to assign physical register to already mapped "
130 Virt2PhysMap[virtReg] = physReg;
133 /// @brief clears the specified virtual register's, physical
135 void clearVirt(unsigned virtReg) {
136 assert(MRegisterInfo::isVirtualRegister(virtReg));
137 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
138 "attempt to clear a not assigned virtual register");
139 Virt2PhysMap[virtReg] = NO_PHYS_REG;
142 /// @brief clears all virtual to physical register mappings
143 void clearAllVirt() {
144 Virt2PhysMap.clear();
148 /// @brief records virtReg is a split live interval from SReg.
149 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
150 Virt2SplitMap[virtReg] = SReg;
153 /// @brief returns the live interval virtReg is split from.
154 unsigned getPreSplitReg(unsigned virtReg) {
155 return Virt2SplitMap[virtReg];
158 /// @brief returns true is the specified virtual register is not
159 /// mapped to a stack slot or rematerialized.
160 bool isAssignedReg(unsigned virtReg) const {
161 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
162 getReMatId(virtReg) == NO_STACK_SLOT)
164 // Split register can be assigned a physical register as well as a
165 // stack slot or remat id.
166 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
169 /// @brief returns the stack slot mapped to the specified virtual
171 int getStackSlot(unsigned virtReg) const {
172 assert(MRegisterInfo::isVirtualRegister(virtReg));
173 return Virt2StackSlotMap[virtReg];
176 /// @brief returns the rematerialization id mapped to the specified virtual
178 int getReMatId(unsigned virtReg) const {
179 assert(MRegisterInfo::isVirtualRegister(virtReg));
180 return Virt2ReMatIdMap[virtReg];
183 /// @brief create a mapping for the specifed virtual register to
184 /// the next available stack slot
185 int assignVirt2StackSlot(unsigned virtReg);
186 /// @brief create a mapping for the specified virtual register to
187 /// the specified stack slot
188 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
190 /// @brief assign an unique re-materialization id to the specified
191 /// virtual register.
192 int assignVirtReMatId(unsigned virtReg);
193 /// @brief assign an unique re-materialization id to the specified
194 /// virtual register.
195 void assignVirtReMatId(unsigned virtReg, int id);
197 /// @brief returns true if the specified virtual register is being
199 bool isReMaterialized(unsigned virtReg) const {
200 return ReMatMap[virtReg] != NULL;
203 /// @brief returns the original machine instruction being re-issued
204 /// to re-materialize the specified virtual register.
205 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
206 return ReMatMap[virtReg];
209 /// @brief records the specified virtual register will be
210 /// re-materialized and the original instruction which will be re-issed
211 /// for this purpose. If parameter all is true, then all uses of the
212 /// registers are rematerialized and it's safe to delete the definition.
213 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
214 ReMatMap[virtReg] = def;
217 /// @brief record the last use (kill) of a split virtual register.
218 void addKillPoint(unsigned virtReg, unsigned index) {
219 Virt2SplitKillMap[virtReg] = index;
222 unsigned getKillPoint(unsigned virtReg) const {
223 return Virt2SplitKillMap[virtReg];
226 /// @brief remove the last use (kill) of a split virtual register.
227 void removeKillPoint(unsigned virtReg) {
228 Virt2SplitKillMap[virtReg] = 0;
231 /// @brief returns true if the specified MachineInstr is a spill point.
232 bool isSpillPt(MachineInstr *Pt) const {
233 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
236 /// @brief returns the virtual registers that should be spilled due to
237 /// splitting right after the specified MachineInstr.
238 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
239 return SpillPt2VirtMap[Pt];
242 /// @brief records the specified MachineInstr as a spill point for virtReg.
243 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
244 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
245 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
247 std::vector<std::pair<unsigned,bool> > Virts;
248 Virts.push_back(std::make_pair(virtReg, isKill));
249 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
253 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
254 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
255 I = SpillPt2VirtMap.find(Old);
256 if (I == SpillPt2VirtMap.end())
258 while (!I->second.empty()) {
259 unsigned virtReg = I->second.back().first;
260 bool isKill = I->second.back().second;
261 I->second.pop_back();
262 addSpillPoint(virtReg, isKill, New);
264 SpillPt2VirtMap.erase(I);
267 /// @brief returns true if the specified MachineInstr is a restore point.
268 bool isRestorePt(MachineInstr *Pt) const {
269 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
272 /// @brief returns the virtual registers that should be restoreed due to
273 /// splitting right after the specified MachineInstr.
274 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
275 return RestorePt2VirtMap[Pt];
278 /// @brief records the specified MachineInstr as a restore point for virtReg.
279 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
280 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
281 RestorePt2VirtMap[Pt].push_back(virtReg);
283 std::vector<unsigned> Virts;
284 Virts.push_back(virtReg);
285 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
289 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
290 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
291 RestorePt2VirtMap.find(Old);
292 if (I == RestorePt2VirtMap.end())
294 while (!I->second.empty()) {
295 unsigned virtReg = I->second.back();
296 I->second.pop_back();
297 addRestorePoint(virtReg, New);
299 RestorePt2VirtMap.erase(I);
302 /// @brief Updates information about the specified virtual register's value
303 /// folded into newMI machine instruction.
304 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
307 /// @brief Updates information about the specified virtual register's value
308 /// folded into the specified machine instruction.
309 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
311 /// @brief returns the virtual registers' values folded in memory
312 /// operands of this instruction
313 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
314 getFoldedVirts(MachineInstr* MI) const {
315 return MI2VirtMap.equal_range(MI);
318 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
319 /// the folded instruction map and spill point map.
320 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
321 MI2VirtMap.erase(MI);
322 SpillPt2VirtMap.erase(MI);
323 RestorePt2VirtMap.erase(MI);
326 void print(std::ostream &OS) const;
327 void print(std::ostream *OS) const { if (OS) print(*OS); }
331 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
335 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
340 /// Spiller interface: Implementations of this interface assign spilled
341 /// virtual registers to stack slots, rewriting the code.
344 virtual bool runOnMachineFunction(MachineFunction &MF,
345 VirtRegMap &VRM) = 0;
348 /// createSpiller - Create an return a spiller object, as specified on the
350 Spiller* createSpiller();
352 } // End llvm namespace