blob: f7e5c08052a04587fc6f06ec0a142d3c302ba468 [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"
Evan Chengcecc8222007-11-17 00:40:40 +000021#include "llvm/ADT/DenseMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#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;
Evan Chengcecc8222007-11-17 00:40:40 +000053
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 /// Virt2StackSlotMap - This is virtual register to stack slot
55 /// mapping. Each spilled virtual register has an entry in it
56 /// which corresponds to the stack slot this register is spilled
57 /// at.
58 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Chengcecc8222007-11-17 00:40:40 +000059
60 /// Virt2StackSlotMap - This is virtual register to rematerialization id
61 /// mapping. Each spilled virtual register that should be remat'd has an
62 /// entry in it which corresponds to the remat id.
Evan Cheng1204d172007-08-13 23:45:17 +000063 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Chengcecc8222007-11-17 00:40:40 +000064
65 /// Virt2SplitMap - This is virtual register to splitted virtual register
66 /// mapping.
67 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
68
69 /// ReMatMap - This is virtual register to re-materialized instruction
70 /// mapping. Each virtual register whose definition is going to be
71 /// re-materialized has an entry in it.
72 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
73
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 /// MI2VirtMap - This is MachineInstr to virtual register
75 /// mapping. In the case of memory spill code being folded into
76 /// instructions, we need to know which virtual register was
77 /// read/written by this instruction.
78 MI2VirtMapTy MI2VirtMap;
79
Evan Chengcecc8222007-11-17 00:40:40 +000080 /// SpillPt2VirtMap - This records the virtual registers which should
81 /// be spilled right after the MachineInstr due to live interval
82 /// splitting.
Evan Cheng91e32d02007-11-28 01:28:46 +000083 std::map<MachineInstr*, std::vector<unsigned> > SpillPt2VirtMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
85 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
86 /// virtual register, an unique id is being assigned. This keeps track of
87 /// the highest id used so far. Note, this starts at (1<<18) to avoid
88 /// conflicts with stack slot numbers.
89 int ReMatId;
90
91 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
92 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
93
94 public:
Dan Gohman3a78bbf2007-08-02 21:21:54 +000095 explicit VirtRegMap(MachineFunction &mf);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 void grow();
98
99 /// @brief returns true if the specified virtual register is
100 /// mapped to a physical register
101 bool hasPhys(unsigned virtReg) const {
102 return getPhys(virtReg) != NO_PHYS_REG;
103 }
104
105 /// @brief returns the physical register mapped to the specified
106 /// virtual register
107 unsigned getPhys(unsigned virtReg) const {
108 assert(MRegisterInfo::isVirtualRegister(virtReg));
109 return Virt2PhysMap[virtReg];
110 }
111
112 /// @brief creates a mapping for the specified virtual register to
113 /// the specified physical register
114 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
115 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
116 MRegisterInfo::isPhysicalRegister(physReg));
117 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
118 "attempt to assign physical register to already mapped "
119 "virtual register");
120 Virt2PhysMap[virtReg] = physReg;
121 }
122
123 /// @brief clears the specified virtual register's, physical
124 /// register mapping
125 void clearVirt(unsigned virtReg) {
126 assert(MRegisterInfo::isVirtualRegister(virtReg));
127 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
128 "attempt to clear a not assigned virtual register");
129 Virt2PhysMap[virtReg] = NO_PHYS_REG;
130 }
131
132 /// @brief clears all virtual to physical register mappings
133 void clearAllVirt() {
134 Virt2PhysMap.clear();
135 grow();
136 }
137
Evan Chengcecc8222007-11-17 00:40:40 +0000138 /// @brief records virtReg is a split live interval from SReg.
139 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
140 Virt2SplitMap[virtReg] = SReg;
141 }
142
143 /// @brief returns the live interval virtReg is split from.
144 unsigned getPreSplitReg(unsigned virtReg) {
145 return Virt2SplitMap[virtReg];
146 }
147
Evan Cheng1204d172007-08-13 23:45:17 +0000148 /// @brief returns true is the specified virtual register is not
149 /// mapped to a stack slot or rematerialized.
150 bool isAssignedReg(unsigned virtReg) const {
Evan Chengcecc8222007-11-17 00:40:40 +0000151 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
152 getReMatId(virtReg) == NO_STACK_SLOT)
153 return true;
154 // Split register can be assigned a physical register as well as a
155 // stack slot or remat id.
156 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 }
158
159 /// @brief returns the stack slot mapped to the specified virtual
160 /// register
161 int getStackSlot(unsigned virtReg) const {
162 assert(MRegisterInfo::isVirtualRegister(virtReg));
163 return Virt2StackSlotMap[virtReg];
164 }
165
Evan Cheng1204d172007-08-13 23:45:17 +0000166 /// @brief returns the rematerialization id mapped to the specified virtual
167 /// register
168 int getReMatId(unsigned virtReg) const {
169 assert(MRegisterInfo::isVirtualRegister(virtReg));
170 return Virt2ReMatIdMap[virtReg];
171 }
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 /// @brief create a mapping for the specifed virtual register to
174 /// the next available stack slot
175 int assignVirt2StackSlot(unsigned virtReg);
176 /// @brief create a mapping for the specified virtual register to
177 /// the specified stack slot
178 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
179
180 /// @brief assign an unique re-materialization id to the specified
181 /// virtual register.
182 int assignVirtReMatId(unsigned virtReg);
Evan Cheng1204d172007-08-13 23:45:17 +0000183 /// @brief assign an unique re-materialization id to the specified
184 /// virtual register.
185 void assignVirtReMatId(unsigned virtReg, int id);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 /// @brief returns true if the specified virtual register is being
188 /// re-materialized.
189 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng1204d172007-08-13 23:45:17 +0000190 return ReMatMap[virtReg] != NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 }
192
193 /// @brief returns the original machine instruction being re-issued
194 /// to re-materialize the specified virtual register.
Evan Cheng1204d172007-08-13 23:45:17 +0000195 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 return ReMatMap[virtReg];
197 }
198
199 /// @brief records the specified virtual register will be
200 /// re-materialized and the original instruction which will be re-issed
Evan Cheng1204d172007-08-13 23:45:17 +0000201 /// for this purpose. If parameter all is true, then all uses of the
202 /// registers are rematerialized and it's safe to delete the definition.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
204 ReMatMap[virtReg] = def;
205 }
206
Evan Cheng91e32d02007-11-28 01:28:46 +0000207 /// @brief returns true if the specified MachineInstr is a spill point.
208 bool isSpillPt(MachineInstr *Pt) const {
209 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
210 }
211
Evan Chengcecc8222007-11-17 00:40:40 +0000212 /// @brief returns the virtual registers that should be spilled due to
213 /// splitting right after the specified MachineInstr.
214 std::vector<unsigned> &getSpillPtSpills(MachineInstr *Pt) {
215 return SpillPt2VirtMap[Pt];
216 }
217
218 /// @brief records the specified MachineInstr as a spill point for virtReg.
219 void addSpillPoint(unsigned virtReg, MachineInstr *Pt) {
Evan Cheng91e32d02007-11-28 01:28:46 +0000220 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
221 SpillPt2VirtMap[Pt].push_back(virtReg);
222 else {
223 std::vector<unsigned> Virts;
224 Virts.push_back(virtReg);
225 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
226 }
Evan Chengcecc8222007-11-17 00:40:40 +0000227 }
228
229 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Cheng91e32d02007-11-28 01:28:46 +0000230 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
231 SpillPt2VirtMap.find(Old);
232 if (I == SpillPt2VirtMap.end())
233 return;
234 while (!I->second.empty()) {
235 unsigned virtReg = I->second.back();
236 I->second.pop_back();
Evan Chengcecc8222007-11-17 00:40:40 +0000237 addSpillPoint(virtReg, New);
238 }
Evan Cheng91e32d02007-11-28 01:28:46 +0000239 SpillPt2VirtMap.erase(I);
Evan Chengcecc8222007-11-17 00:40:40 +0000240 }
241
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 /// @brief Updates information about the specified virtual register's value
243 /// folded into newMI machine instruction. The OpNum argument indicates the
244 /// operand number of OldMI that is folded.
245 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
246 MachineInstr *NewMI);
247
Evan Chengf3255842007-10-13 02:50:24 +0000248 /// @brief Updates information about the specified virtual register's value
249 /// folded into the specified machine instruction.
250 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
251
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 /// @brief returns the virtual registers' values folded in memory
253 /// operands of this instruction
254 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
255 getFoldedVirts(MachineInstr* MI) const {
256 return MI2VirtMap.equal_range(MI);
257 }
258
Evan Cheng91e32d02007-11-28 01:28:46 +0000259 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
260 /// the folded instruction map and spill point map.
261 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 MI2VirtMap.erase(MI);
Evan Cheng91e32d02007-11-28 01:28:46 +0000263 SpillPt2VirtMap.erase(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 }
265
266 void print(std::ostream &OS) const;
267 void print(std::ostream *OS) const { if (OS) print(*OS); }
268 void dump() const;
269 };
270
271 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
272 VRM.print(OS);
273 return OS;
274 }
275 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
276 VRM.print(OS);
277 return OS;
278 }
279
280 /// Spiller interface: Implementations of this interface assign spilled
281 /// virtual registers to stack slots, rewriting the code.
282 struct Spiller {
283 virtual ~Spiller();
284 virtual bool runOnMachineFunction(MachineFunction &MF,
285 VirtRegMap &VRM) = 0;
286 };
287
288 /// createSpiller - Create an return a spiller object, as specified on the
289 /// command line.
290 Spiller* createSpiller();
291
292} // End llvm namespace
293
294#endif