1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/TargetRegisterInfo.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/Support/Streams.h"
29 class MachineFunction;
30 class TargetInstrInfo;
36 NO_STACK_SLOT = (1L << 30)-1,
37 MAX_STACK_SLOT = (1L << 18)-1
40 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
41 typedef std::multimap<MachineInstr*,
42 std::pair<unsigned, ModRef> > MI2VirtMapTy;
45 const TargetInstrInfo &TII;
48 /// Virt2PhysMap - This is a virtual to physical register
49 /// mapping. Each virtual register is required to have an entry in
50 /// it; even spilled virtual registers (the register mapped to a
51 /// spilled register is the temporary used to load it from the
53 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
55 /// Virt2StackSlotMap - This is virtual register to stack slot
56 /// mapping. Each spilled virtual register has an entry in it
57 /// which corresponds to the stack slot this register is spilled
59 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
61 /// Virt2StackSlotMap - This is virtual register to rematerialization id
62 /// mapping. Each spilled virtual register that should be remat'd has an
63 /// entry in it which corresponds to the remat id.
64 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
66 /// Virt2SplitMap - This is virtual register to splitted virtual register
68 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
70 /// Virt2SplitKillMap - This is splitted virtual register to its last use
71 /// (kill) index mapping.
72 IndexedMap<unsigned> Virt2SplitKillMap;
74 /// ReMatMap - This is virtual register to re-materialized instruction
75 /// mapping. Each virtual register whose definition is going to be
76 /// re-materialized has an entry in it.
77 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
79 /// MI2VirtMap - This is MachineInstr to virtual register
80 /// mapping. In the case of memory spill code being folded into
81 /// instructions, we need to know which virtual register was
82 /// read/written by this instruction.
83 MI2VirtMapTy MI2VirtMap;
85 /// SpillPt2VirtMap - This records the virtual registers which should
86 /// be spilled right after the MachineInstr due to live interval
88 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
91 /// RestorePt2VirtMap - This records the virtual registers which should
92 /// be restored right before the MachineInstr due to live interval
94 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
96 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
97 /// virtual register, an unique id is being assigned. This keeps track of
98 /// the highest id used so far. Note, this starts at (1<<18) to avoid
99 /// conflicts with stack slot numbers.
102 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
103 int LowSpillSlot, HighSpillSlot;
105 /// SpillSlotToUsesMap - Records uses for each register spill slot.
106 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
108 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
109 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
112 explicit VirtRegMap(MachineFunction &mf);
116 /// @brief returns true if the specified virtual register is
117 /// mapped to a physical register
118 bool hasPhys(unsigned virtReg) const {
119 return getPhys(virtReg) != NO_PHYS_REG;
122 /// @brief returns the physical register mapped to the specified
124 unsigned getPhys(unsigned virtReg) const {
125 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
126 return Virt2PhysMap[virtReg];
129 /// @brief creates a mapping for the specified virtual register to
130 /// the specified physical register
131 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
132 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
133 TargetRegisterInfo::isPhysicalRegister(physReg));
134 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
135 "attempt to assign physical register to already mapped "
137 Virt2PhysMap[virtReg] = physReg;
140 /// @brief clears the specified virtual register's, physical
142 void clearVirt(unsigned virtReg) {
143 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
144 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
145 "attempt to clear a not assigned virtual register");
146 Virt2PhysMap[virtReg] = NO_PHYS_REG;
149 /// @brief clears all virtual to physical register mappings
150 void clearAllVirt() {
151 Virt2PhysMap.clear();
155 /// @brief records virtReg is a split live interval from SReg.
156 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
157 Virt2SplitMap[virtReg] = SReg;
160 /// @brief returns the live interval virtReg is split from.
161 unsigned getPreSplitReg(unsigned virtReg) {
162 return Virt2SplitMap[virtReg];
165 /// @brief returns true is the specified virtual register is not
166 /// mapped to a stack slot or rematerialized.
167 bool isAssignedReg(unsigned virtReg) const {
168 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
169 getReMatId(virtReg) == NO_STACK_SLOT)
171 // Split register can be assigned a physical register as well as a
172 // stack slot or remat id.
173 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
176 /// @brief returns the stack slot mapped to the specified virtual
178 int getStackSlot(unsigned virtReg) const {
179 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
180 return Virt2StackSlotMap[virtReg];
183 /// @brief returns the rematerialization id mapped to the specified virtual
185 int getReMatId(unsigned virtReg) const {
186 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
187 return Virt2ReMatIdMap[virtReg];
190 /// @brief create a mapping for the specifed virtual register to
191 /// the next available stack slot
192 int assignVirt2StackSlot(unsigned virtReg);
193 /// @brief create a mapping for the specified virtual register to
194 /// the specified stack slot
195 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
197 /// @brief assign an unique re-materialization id to the specified
198 /// virtual register.
199 int assignVirtReMatId(unsigned virtReg);
200 /// @brief assign an unique re-materialization id to the specified
201 /// virtual register.
202 void assignVirtReMatId(unsigned virtReg, int id);
204 /// @brief returns true if the specified virtual register is being
206 bool isReMaterialized(unsigned virtReg) const {
207 return ReMatMap[virtReg] != NULL;
210 /// @brief returns the original machine instruction being re-issued
211 /// to re-materialize the specified virtual register.
212 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
213 return ReMatMap[virtReg];
216 /// @brief records the specified virtual register will be
217 /// re-materialized and the original instruction which will be re-issed
218 /// for this purpose. If parameter all is true, then all uses of the
219 /// registers are rematerialized and it's safe to delete the definition.
220 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
221 ReMatMap[virtReg] = def;
224 /// @brief record the last use (kill) of a split virtual register.
225 void addKillPoint(unsigned virtReg, unsigned index) {
226 Virt2SplitKillMap[virtReg] = index;
229 unsigned getKillPoint(unsigned virtReg) const {
230 return Virt2SplitKillMap[virtReg];
233 /// @brief remove the last use (kill) of a split virtual register.
234 void removeKillPoint(unsigned virtReg) {
235 Virt2SplitKillMap[virtReg] = 0;
238 /// @brief returns true if the specified MachineInstr is a spill point.
239 bool isSpillPt(MachineInstr *Pt) const {
240 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
243 /// @brief returns the virtual registers that should be spilled due to
244 /// splitting right after the specified MachineInstr.
245 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
246 return SpillPt2VirtMap[Pt];
249 /// @brief records the specified MachineInstr as a spill point for virtReg.
250 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
251 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
252 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
254 std::vector<std::pair<unsigned,bool> > Virts;
255 Virts.push_back(std::make_pair(virtReg, isKill));
256 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
260 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
261 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
262 I = SpillPt2VirtMap.find(Old);
263 if (I == SpillPt2VirtMap.end())
265 while (!I->second.empty()) {
266 unsigned virtReg = I->second.back().first;
267 bool isKill = I->second.back().second;
268 I->second.pop_back();
269 addSpillPoint(virtReg, isKill, New);
271 SpillPt2VirtMap.erase(I);
274 /// @brief returns true if the specified MachineInstr is a restore point.
275 bool isRestorePt(MachineInstr *Pt) const {
276 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
279 /// @brief returns the virtual registers that should be restoreed due to
280 /// splitting right after the specified MachineInstr.
281 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
282 return RestorePt2VirtMap[Pt];
285 /// @brief records the specified MachineInstr as a restore point for virtReg.
286 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
287 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
288 RestorePt2VirtMap[Pt].push_back(virtReg);
290 std::vector<unsigned> Virts;
291 Virts.push_back(virtReg);
292 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
296 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
297 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
298 RestorePt2VirtMap.find(Old);
299 if (I == RestorePt2VirtMap.end())
301 while (!I->second.empty()) {
302 unsigned virtReg = I->second.back();
303 I->second.pop_back();
304 addRestorePoint(virtReg, New);
306 RestorePt2VirtMap.erase(I);
309 /// @brief Return lowest spill slot index.
310 int getLowSpillSlot() const {
314 /// @brief Return highest spill slot index.
315 int getHighSpillSlot() const {
316 return HighSpillSlot;
319 /// @brief Records a spill slot use.
320 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
322 /// @brief Returns true if spill slot has been used.
323 bool isSpillSlotUsed(int FrameIndex) const {
324 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
325 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
328 /// @brief Updates information about the specified virtual register's value
329 /// folded into newMI machine instruction.
330 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
333 /// @brief Updates information about the specified virtual register's value
334 /// folded into the specified machine instruction.
335 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
337 /// @brief returns the virtual registers' values folded in memory
338 /// operands of this instruction
339 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
340 getFoldedVirts(MachineInstr* MI) const {
341 return MI2VirtMap.equal_range(MI);
344 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
345 /// the folded instruction map and spill point map.
346 void RemoveMachineInstrFromMaps(MachineInstr *MI);
348 void print(std::ostream &OS) const;
349 void print(std::ostream *OS) const { if (OS) print(*OS); }
353 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
357 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
362 /// Spiller interface: Implementations of this interface assign spilled
363 /// virtual registers to stack slots, rewriting the code.
366 virtual bool runOnMachineFunction(MachineFunction &MF,
367 VirtRegMap &VRM) = 0;
370 /// createSpiller - Create an return a spiller object, as specified on the
372 Spiller* createSpiller();
374 } // End llvm namespace