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 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
59 /// MI2VirtMap - This is MachineInstr to virtual register
60 /// mapping. In the case of memory spill code being folded into
61 /// instructions, we need to know which virtual register was
62 /// read/written by this instruction.
63 MI2VirtMapTy MI2VirtMap;
65 /// ReMatMap - This is virtual register to re-materialized instruction
66 /// mapping. Each virtual register whose definition is going to be
67 /// re-materialized has an entry in it.
68 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
70 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
71 /// virtual register, an unique id is being assigned. This keeps track of
72 /// the highest id used so far. Note, this starts at (1<<18) to avoid
73 /// conflicts with stack slot numbers.
76 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
77 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
80 explicit VirtRegMap(MachineFunction &mf);
84 /// @brief returns true if the specified virtual register is
85 /// mapped to a physical register
86 bool hasPhys(unsigned virtReg) const {
87 return getPhys(virtReg) != NO_PHYS_REG;
90 /// @brief returns the physical register mapped to the specified
92 unsigned getPhys(unsigned virtReg) const {
93 assert(MRegisterInfo::isVirtualRegister(virtReg));
94 return Virt2PhysMap[virtReg];
97 /// @brief creates a mapping for the specified virtual register to
98 /// the specified physical register
99 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
100 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
101 MRegisterInfo::isPhysicalRegister(physReg));
102 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
103 "attempt to assign physical register to already mapped "
105 Virt2PhysMap[virtReg] = physReg;
108 /// @brief clears the specified virtual register's, physical
110 void clearVirt(unsigned virtReg) {
111 assert(MRegisterInfo::isVirtualRegister(virtReg));
112 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
113 "attempt to clear a not assigned virtual register");
114 Virt2PhysMap[virtReg] = NO_PHYS_REG;
117 /// @brief clears all virtual to physical register mappings
118 void clearAllVirt() {
119 Virt2PhysMap.clear();
123 /// @brief returns true is the specified virtual register is not
124 /// mapped to a stack slot or rematerialized.
125 bool isAssignedReg(unsigned virtReg) const {
126 return getStackSlot(virtReg) == NO_STACK_SLOT &&
127 getReMatId(virtReg) == NO_STACK_SLOT;
130 /// @brief returns the stack slot mapped to the specified virtual
132 int getStackSlot(unsigned virtReg) const {
133 assert(MRegisterInfo::isVirtualRegister(virtReg));
134 return Virt2StackSlotMap[virtReg];
137 /// @brief returns the rematerialization id mapped to the specified virtual
139 int getReMatId(unsigned virtReg) const {
140 assert(MRegisterInfo::isVirtualRegister(virtReg));
141 return Virt2ReMatIdMap[virtReg];
144 /// @brief create a mapping for the specifed virtual register to
145 /// the next available stack slot
146 int assignVirt2StackSlot(unsigned virtReg);
147 /// @brief create a mapping for the specified virtual register to
148 /// the specified stack slot
149 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
151 /// @brief assign an unique re-materialization id to the specified
152 /// virtual register.
153 int assignVirtReMatId(unsigned virtReg);
154 /// @brief assign an unique re-materialization id to the specified
155 /// virtual register.
156 void assignVirtReMatId(unsigned virtReg, int id);
158 /// @brief returns true if the specified virtual register is being
160 bool isReMaterialized(unsigned virtReg) const {
161 return ReMatMap[virtReg] != NULL;
164 /// @brief returns the original machine instruction being re-issued
165 /// to re-materialize the specified virtual register.
166 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
167 return ReMatMap[virtReg];
170 /// @brief records the specified virtual register will be
171 /// re-materialized and the original instruction which will be re-issed
172 /// for this purpose. If parameter all is true, then all uses of the
173 /// registers are rematerialized and it's safe to delete the definition.
174 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
175 ReMatMap[virtReg] = def;
178 /// @brief Updates information about the specified virtual register's value
179 /// folded into newMI machine instruction. The OpNum argument indicates the
180 /// operand number of OldMI that is folded.
181 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
182 MachineInstr *NewMI);
184 /// @brief returns the virtual registers' values folded in memory
185 /// operands of this instruction
186 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
187 getFoldedVirts(MachineInstr* MI) const {
188 return MI2VirtMap.equal_range(MI);
191 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
192 /// the folded instruction map, remove its entry from the map.
193 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
194 MI2VirtMap.erase(MI);
197 void print(std::ostream &OS) const;
198 void print(std::ostream *OS) const { if (OS) print(*OS); }
202 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
206 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
211 /// Spiller interface: Implementations of this interface assign spilled
212 /// virtual registers to stack slots, rewriting the code.
215 virtual bool runOnMachineFunction(MachineFunction &MF,
216 VirtRegMap &VRM) = 0;
219 /// createSpiller - Create an return a spiller object, as specified on the
221 Spiller* createSpiller();
223 } // End llvm namespace