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/BitVector.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;
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
57 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
58 /// MI2VirtMap - This is MachineInstr to virtual register
59 /// mapping. In the case of memory spill code being folded into
60 /// instructions, we need to know which virtual register was
61 /// read/written by this instruction.
62 MI2VirtMapTy MI2VirtMap;
64 /// ReMatMap - This is virtual register to re-materialized instruction
65 /// mapping. Each virtual register whose definition is going to be
66 /// re-materialized has an entry in it.
67 std::map<unsigned, const MachineInstr*> ReMatMap;
69 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
70 /// virtual register, an unique id is being assigned. This keeps track of
71 /// the highest id used so far. Note, this starts at (1<<18) to avoid
72 /// conflicts with stack slot numbers.
75 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
76 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
79 explicit VirtRegMap(MachineFunction &mf);
83 /// @brief returns true if the specified virtual register is
84 /// mapped to a physical register
85 bool hasPhys(unsigned virtReg) const {
86 return getPhys(virtReg) != NO_PHYS_REG;
89 /// @brief returns the physical register mapped to the specified
91 unsigned getPhys(unsigned virtReg) const {
92 assert(MRegisterInfo::isVirtualRegister(virtReg));
93 return Virt2PhysMap[virtReg];
96 /// @brief creates a mapping for the specified virtual register to
97 /// the specified physical register
98 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
99 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
100 MRegisterInfo::isPhysicalRegister(physReg));
101 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
102 "attempt to assign physical register to already mapped "
104 Virt2PhysMap[virtReg] = physReg;
107 /// @brief clears the specified virtual register's, physical
109 void clearVirt(unsigned virtReg) {
110 assert(MRegisterInfo::isVirtualRegister(virtReg));
111 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
112 "attempt to clear a not assigned virtual register");
113 Virt2PhysMap[virtReg] = NO_PHYS_REG;
116 /// @brief clears all virtual to physical register mappings
117 void clearAllVirt() {
118 Virt2PhysMap.clear();
122 /// @brief returns true is the specified virtual register is
123 /// mapped to a stack slot
124 bool hasStackSlot(unsigned virtReg) const {
125 return getStackSlot(virtReg) != NO_STACK_SLOT;
128 /// @brief returns the stack slot mapped to the specified virtual
130 int getStackSlot(unsigned virtReg) const {
131 assert(MRegisterInfo::isVirtualRegister(virtReg));
132 return Virt2StackSlotMap[virtReg];
135 /// @brief create a mapping for the specifed virtual register to
136 /// the next available stack slot
137 int assignVirt2StackSlot(unsigned virtReg);
138 /// @brief create a mapping for the specified virtual register to
139 /// the specified stack slot
140 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
142 /// @brief assign an unique re-materialization id to the specified
143 /// virtual register.
144 int assignVirtReMatId(unsigned virtReg);
146 /// @brief returns true if the specified virtual register is being
148 bool isReMaterialized(unsigned virtReg) const {
149 return ReMatMap.count(virtReg) != 0;
152 /// @brief returns the original machine instruction being re-issued
153 /// to re-materialize the specified virtual register.
154 const MachineInstr *getReMaterializedMI(unsigned virtReg) {
155 return ReMatMap[virtReg];
158 /// @brief records the specified virtual register will be
159 /// re-materialized and the original instruction which will be re-issed
160 /// for this purpose.
161 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
162 ReMatMap[virtReg] = def;
165 /// @brief Updates information about the specified virtual register's value
166 /// folded into newMI machine instruction. The OpNum argument indicates the
167 /// operand number of OldMI that is folded.
168 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
169 MachineInstr *NewMI);
171 /// @brief returns the virtual registers' values folded in memory
172 /// operands of this instruction
173 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
174 getFoldedVirts(MachineInstr* MI) const {
175 return MI2VirtMap.equal_range(MI);
178 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
179 /// the folded instruction map, remove its entry from the map.
180 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
181 MI2VirtMap.erase(MI);
184 void print(std::ostream &OS) const;
185 void print(std::ostream *OS) const { if (OS) print(*OS); }
189 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
193 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
198 /// Spiller interface: Implementations of this interface assign spilled
199 /// virtual registers to stack slots, rewriting the code.
202 virtual bool runOnMachineFunction(MachineFunction &MF,
203 VirtRegMap &VRM) = 0;
206 /// createSpiller - Create an return a spiller object, as specified on the
208 Spiller* createSpiller();
210 } // End llvm namespace