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 TargetInstrInfo
;
34 NO_STACK_SLOT
= (1L << 30)-1,
35 MAX_STACK_SLOT
= (1L << 18)-1
38 enum ModRef
{ isRef
= 1, isMod
= 2, isModRef
= 3 };
39 typedef std::multimap
<MachineInstr
*,
40 std::pair
<unsigned, ModRef
> > MI2VirtMapTy
;
43 const TargetInstrInfo
&TII
;
46 /// Virt2PhysMap - This is a virtual to physical register
47 /// mapping. Each virtual register is required to have an entry in
48 /// it; even spilled virtual registers (the register mapped to a
49 /// spilled register is the temporary used to load it from the
51 IndexedMap
<unsigned, VirtReg2IndexFunctor
> Virt2PhysMap
;
52 /// Virt2StackSlotMap - This is virtual register to stack slot
53 /// mapping. Each spilled virtual register has an entry in it
54 /// which corresponds to the stack slot this register is spilled
56 IndexedMap
<int, VirtReg2IndexFunctor
> Virt2StackSlotMap
;
57 /// MI2VirtMap - This is MachineInstr to virtual register
58 /// mapping. In the case of memory spill code being folded into
59 /// instructions, we need to know which virtual register was
60 /// read/written by this instruction.
61 MI2VirtMapTy MI2VirtMap
;
63 /// ReMatMap - This is virtual register to re-materialized instruction
64 /// mapping. Each virtual register whose definition is going to be
65 /// re-materialized has an entry in it.
66 std::map
<unsigned, const MachineInstr
*> ReMatMap
;
68 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
69 /// virtual register, an unique id is being assigned. This keeps track of
70 /// the highest id used so far. Note, this starts at (1<<18) to avoid
71 /// conflicts with stack slot numbers.
74 VirtRegMap(const VirtRegMap
&); // DO NOT IMPLEMENT
75 void operator=(const VirtRegMap
&); // DO NOT IMPLEMENT
78 VirtRegMap(MachineFunction
&mf
);
82 /// @brief returns true if the specified virtual register is
83 /// mapped to a physical register
84 bool hasPhys(unsigned virtReg
) const {
85 return getPhys(virtReg
) != NO_PHYS_REG
;
88 /// @brief returns the physical register mapped to the specified
90 unsigned getPhys(unsigned virtReg
) const {
91 assert(MRegisterInfo::isVirtualRegister(virtReg
));
92 return Virt2PhysMap
[virtReg
];
95 /// @brief creates a mapping for the specified virtual register to
96 /// the specified physical register
97 void assignVirt2Phys(unsigned virtReg
, unsigned physReg
) {
98 assert(MRegisterInfo::isVirtualRegister(virtReg
) &&
99 MRegisterInfo::isPhysicalRegister(physReg
));
100 assert(Virt2PhysMap
[virtReg
] == NO_PHYS_REG
&&
101 "attempt to assign physical register to already mapped "
103 Virt2PhysMap
[virtReg
] = physReg
;
106 /// @brief clears the specified virtual register's, physical
108 void clearVirt(unsigned virtReg
) {
109 assert(MRegisterInfo::isVirtualRegister(virtReg
));
110 assert(Virt2PhysMap
[virtReg
] != NO_PHYS_REG
&&
111 "attempt to clear a not assigned virtual register");
112 Virt2PhysMap
[virtReg
] = NO_PHYS_REG
;
115 /// @brief clears all virtual to physical register mappings
116 void clearAllVirt() {
117 Virt2PhysMap
.clear();
121 /// @brief returns true is the specified virtual register is
122 /// mapped to a stack slot
123 bool hasStackSlot(unsigned virtReg
) const {
124 return getStackSlot(virtReg
) != NO_STACK_SLOT
;
127 /// @brief returns the stack slot mapped to the specified virtual
129 int getStackSlot(unsigned virtReg
) const {
130 assert(MRegisterInfo::isVirtualRegister(virtReg
));
131 return Virt2StackSlotMap
[virtReg
];
134 /// @brief create a mapping for the specifed virtual register to
135 /// the next available stack slot
136 int assignVirt2StackSlot(unsigned virtReg
);
137 /// @brief create a mapping for the specified virtual register to
138 /// the specified stack slot
139 void assignVirt2StackSlot(unsigned virtReg
, int frameIndex
);
141 /// @brief assign an unique re-materialization id to the specified
142 /// virtual register.
143 int assignVirtReMatId(unsigned virtReg
);
145 /// @brief returns true if the specified virtual register is being
147 bool isReMaterialized(unsigned virtReg
) const {
148 return ReMatMap
.count(virtReg
) != 0;
151 /// @brief returns the original machine instruction being re-issued
152 /// to re-materialize the specified virtual register.
153 const MachineInstr
*getReMaterializedMI(unsigned virtReg
) {
154 return ReMatMap
[virtReg
];
157 /// @brief records the specified virtual register will be
158 /// re-materialized and the original instruction which will be re-issed
159 /// for this purpose.
160 void setVirtIsReMaterialized(unsigned virtReg
, MachineInstr
*def
) {
161 ReMatMap
[virtReg
] = def
;
164 /// @brief Updates information about the specified virtual register's value
165 /// folded into newMI machine instruction. The OpNum argument indicates the
166 /// operand number of OldMI that is folded.
167 void virtFolded(unsigned VirtReg
, MachineInstr
*OldMI
, unsigned OpNum
,
168 MachineInstr
*NewMI
);
170 /// @brief returns the virtual registers' values folded in memory
171 /// operands of this instruction
172 std::pair
<MI2VirtMapTy::const_iterator
, MI2VirtMapTy::const_iterator
>
173 getFoldedVirts(MachineInstr
* MI
) const {
174 return MI2VirtMap
.equal_range(MI
);
177 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
178 /// the folded instruction map, remove its entry from the map.
179 void RemoveFromFoldedVirtMap(MachineInstr
*MI
) {
180 MI2VirtMap
.erase(MI
);
183 void print(std::ostream
&OS
) const;
184 void print(std::ostream
*OS
) const { if (OS
) print(*OS
); }
188 inline std::ostream
*operator<<(std::ostream
*OS
, const VirtRegMap
&VRM
) {
192 inline std::ostream
&operator<<(std::ostream
&OS
, const VirtRegMap
&VRM
) {
197 /// Spiller interface: Implementations of this interface assign spilled
198 /// virtual registers to stack slots, rewriting the code.
201 virtual bool runOnMachineFunction(MachineFunction
&MF
,
202 VirtRegMap
&VRM
) = 0;
205 /// createSpiller - Create an return a spiller object, as specified on the
207 Spiller
* createSpiller();
209 } // End llvm namespace