blob: f6d305e494f4773acb562d7a4bee7a85fd1364d7 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_VIRTREGMAP_H
18#define LLVM_CODEGEN_VIRTREGMAP_H
19
20#include "llvm/Target/MRegisterInfo.h"
21#include "llvm/ADT/BitVector.h"
22#include "llvm/ADT/IndexedMap.h"
23#include "llvm/Support/Streams.h"
24#include <map>
25
26namespace llvm {
27 class MachineInstr;
David Greene44a3bfb2007-08-07 16:34:05 +000028 class MachineFunction;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029 class TargetInstrInfo;
30
31 class VirtRegMap {
32 public:
33 enum {
34 NO_PHYS_REG = 0,
35 NO_STACK_SLOT = (1L << 30)-1,
36 MAX_STACK_SLOT = (1L << 18)-1
37 };
38
39 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
40 typedef std::multimap<MachineInstr*,
41 std::pair<unsigned, ModRef> > MI2VirtMapTy;
42
43 private:
44 const TargetInstrInfo &TII;
45
46 MachineFunction &MF;
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
51 /// stack).
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
56 /// at.
57 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng1204d172007-08-13 23:45:17 +000058 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 /// 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;
64
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.
Evan Cheng1204d172007-08-13 23:45:17 +000068 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
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.
74 int ReMatId;
75
76 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
77 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
78
79 public:
Dan Gohman3a78bbf2007-08-02 21:21:54 +000080 explicit VirtRegMap(MachineFunction &mf);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
82 void grow();
83
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;
88 }
89
90 /// @brief returns the physical register mapped to the specified
91 /// virtual register
92 unsigned getPhys(unsigned virtReg) const {
93 assert(MRegisterInfo::isVirtualRegister(virtReg));
94 return Virt2PhysMap[virtReg];
95 }
96
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 "
104 "virtual register");
105 Virt2PhysMap[virtReg] = physReg;
106 }
107
108 /// @brief clears the specified virtual register's, physical
109 /// register mapping
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;
115 }
116
117 /// @brief clears all virtual to physical register mappings
118 void clearAllVirt() {
119 Virt2PhysMap.clear();
120 grow();
121 }
122
Evan Cheng1204d172007-08-13 23:45:17 +0000123 /// @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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 }
129
130 /// @brief returns the stack slot mapped to the specified virtual
131 /// register
132 int getStackSlot(unsigned virtReg) const {
133 assert(MRegisterInfo::isVirtualRegister(virtReg));
134 return Virt2StackSlotMap[virtReg];
135 }
136
Evan Cheng1204d172007-08-13 23:45:17 +0000137 /// @brief returns the rematerialization id mapped to the specified virtual
138 /// register
139 int getReMatId(unsigned virtReg) const {
140 assert(MRegisterInfo::isVirtualRegister(virtReg));
141 return Virt2ReMatIdMap[virtReg];
142 }
143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 /// @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);
150
151 /// @brief assign an unique re-materialization id to the specified
152 /// virtual register.
153 int assignVirtReMatId(unsigned virtReg);
Evan Cheng1204d172007-08-13 23:45:17 +0000154 /// @brief assign an unique re-materialization id to the specified
155 /// virtual register.
156 void assignVirtReMatId(unsigned virtReg, int id);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
158 /// @brief returns true if the specified virtual register is being
159 /// re-materialized.
160 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng1204d172007-08-13 23:45:17 +0000161 return ReMatMap[virtReg] != NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 }
163
164 /// @brief returns the original machine instruction being re-issued
165 /// to re-materialize the specified virtual register.
Evan Cheng1204d172007-08-13 23:45:17 +0000166 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 return ReMatMap[virtReg];
168 }
169
170 /// @brief records the specified virtual register will be
171 /// re-materialized and the original instruction which will be re-issed
Evan Cheng1204d172007-08-13 23:45:17 +0000172 /// 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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
175 ReMatMap[virtReg] = def;
176 }
177
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);
183
Evan Chengf3255842007-10-13 02:50:24 +0000184 /// @brief Updates information about the specified virtual register's value
185 /// folded into the specified machine instruction.
186 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 /// @brief returns the virtual registers' values folded in memory
189 /// operands of this instruction
190 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
191 getFoldedVirts(MachineInstr* MI) const {
192 return MI2VirtMap.equal_range(MI);
193 }
194
195 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
196 /// the folded instruction map, remove its entry from the map.
197 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
198 MI2VirtMap.erase(MI);
199 }
200
201 void print(std::ostream &OS) const;
202 void print(std::ostream *OS) const { if (OS) print(*OS); }
203 void dump() const;
204 };
205
206 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
207 VRM.print(OS);
208 return OS;
209 }
210 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
211 VRM.print(OS);
212 return OS;
213 }
214
215 /// Spiller interface: Implementations of this interface assign spilled
216 /// virtual registers to stack slots, rewriting the code.
217 struct Spiller {
218 virtual ~Spiller();
219 virtual bool runOnMachineFunction(MachineFunction &MF,
220 VirtRegMap &VRM) = 0;
221 };
222
223 /// createSpiller - Create an return a spiller object, as specified on the
224 /// command line.
225 Spiller* createSpiller();
226
227} // End llvm namespace
228
229#endif