blob: df32a6566a701158df3e9699f2d46ba2fc112500 [file] [log] [blame]
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +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//
Chris Lattner8c4d88d2004-09-30 01:54:45 +000010// 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.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000014//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_VIRTREGMAP_H
18#define LLVM_CODEGEN_VIRTREGMAP_H
19
Chris Lattner8c4d88d2004-09-30 01:54:45 +000020#include "llvm/Target/MRegisterInfo.h"
Evan Cheng81a03822007-11-17 00:40:40 +000021#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000022#include "llvm/ADT/IndexedMap.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000023#include "llvm/Support/Streams.h"
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000024#include <map>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000025
26namespace llvm {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000027 class MachineInstr;
David Greene7e231462007-08-07 16:34:05 +000028 class MachineFunction;
Chris Lattner29268692006-09-05 02:12:02 +000029 class TargetInstrInfo;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000030
Chris Lattner8c4d88d2004-09-30 01:54:45 +000031 class VirtRegMap {
32 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000033 enum {
34 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000035 NO_STACK_SLOT = (1L << 30)-1,
36 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000037 };
38
Chris Lattner35f27052006-05-01 21:16:03 +000039 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000040 typedef std::multimap<MachineInstr*,
41 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000042
Chris Lattner8c4d88d2004-09-30 01:54:45 +000043 private:
Chris Lattner29268692006-09-05 02:12:02 +000044 const TargetInstrInfo &TII;
45
Chris Lattner7f690e62004-09-30 02:15:18 +000046 MachineFunction &MF;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000047 /// 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).
Chris Lattner94c002a2007-02-01 05:32:05 +000052 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng81a03822007-11-17 00:40:40 +000053
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +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.
Chris Lattner94c002a2007-02-01 05:32:05 +000058 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng81a03822007-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 Cheng549f27d32007-08-13 23:45:17 +000063 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Cheng81a03822007-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
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +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.
Chris Lattner7f690e62004-09-30 02:15:18 +000078 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000079
Evan Cheng81a03822007-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 Chengb50bb8c2007-12-05 08:16:32 +000083 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
84 SpillPt2VirtMap;
Evan Cheng2638e1a2007-03-20 08:13:50 +000085
Evan Cheng0cbb1162007-11-29 01:06:25 +000086 /// RestorePt2VirtMap - This records the virtual registers which should
87 /// be restored right before the MachineInstr due to live interval
88 /// splitting.
89 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
90
Evan Cheng2638e1a2007-03-20 08:13:50 +000091 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng91935142007-04-04 07:40:01 +000092 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng2638e1a2007-03-20 08:13:50 +000093 /// the highest id used so far. Note, this starts at (1<<18) to avoid
94 /// conflicts with stack slot numbers.
95 int ReMatId;
96
Chris Lattner8c4d88d2004-09-30 01:54:45 +000097 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
98 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +000099
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000100 public:
Dan Gohman61e729e2007-08-02 21:21:54 +0000101 explicit VirtRegMap(MachineFunction &mf);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000102
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000103 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000104
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000105 /// @brief returns true if the specified virtual register is
106 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000107 bool hasPhys(unsigned virtReg) const {
108 return getPhys(virtReg) != NO_PHYS_REG;
109 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000110
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000111 /// @brief returns the physical register mapped to the specified
112 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000113 unsigned getPhys(unsigned virtReg) const {
114 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000115 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000116 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000117
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000118 /// @brief creates a mapping for the specified virtual register to
119 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000120 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
121 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
122 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000123 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000124 "attempt to assign physical register to already mapped "
125 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000126 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000127 }
128
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000129 /// @brief clears the specified virtual register's, physical
130 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000131 void clearVirt(unsigned virtReg) {
132 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000133 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000134 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000135 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000136 }
137
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000138 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000139 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000140 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000141 grow();
142 }
143
Evan Cheng81a03822007-11-17 00:40:40 +0000144 /// @brief records virtReg is a split live interval from SReg.
145 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
146 Virt2SplitMap[virtReg] = SReg;
147 }
148
149 /// @brief returns the live interval virtReg is split from.
150 unsigned getPreSplitReg(unsigned virtReg) {
151 return Virt2SplitMap[virtReg];
152 }
153
Evan Cheng549f27d32007-08-13 23:45:17 +0000154 /// @brief returns true is the specified virtual register is not
155 /// mapped to a stack slot or rematerialized.
156 bool isAssignedReg(unsigned virtReg) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000157 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
158 getReMatId(virtReg) == NO_STACK_SLOT)
159 return true;
160 // Split register can be assigned a physical register as well as a
161 // stack slot or remat id.
162 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000163 }
164
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000165 /// @brief returns the stack slot mapped to the specified virtual
166 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000167 int getStackSlot(unsigned virtReg) const {
168 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000169 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000170 }
171
Evan Cheng549f27d32007-08-13 23:45:17 +0000172 /// @brief returns the rematerialization id mapped to the specified virtual
173 /// register
174 int getReMatId(unsigned virtReg) const {
175 assert(MRegisterInfo::isVirtualRegister(virtReg));
176 return Virt2ReMatIdMap[virtReg];
177 }
178
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000179 /// @brief create a mapping for the specifed virtual register to
180 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000181 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000182 /// @brief create a mapping for the specified virtual register to
183 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000184 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
185
Evan Cheng2638e1a2007-03-20 08:13:50 +0000186 /// @brief assign an unique re-materialization id to the specified
187 /// virtual register.
188 int assignVirtReMatId(unsigned virtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000189 /// @brief assign an unique re-materialization id to the specified
190 /// virtual register.
191 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng2638e1a2007-03-20 08:13:50 +0000192
193 /// @brief returns true if the specified virtual register is being
194 /// re-materialized.
195 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng549f27d32007-08-13 23:45:17 +0000196 return ReMatMap[virtReg] != NULL;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000197 }
198
199 /// @brief returns the original machine instruction being re-issued
200 /// to re-materialize the specified virtual register.
Evan Cheng549f27d32007-08-13 23:45:17 +0000201 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng2638e1a2007-03-20 08:13:50 +0000202 return ReMatMap[virtReg];
203 }
204
205 /// @brief records the specified virtual register will be
206 /// re-materialized and the original instruction which will be re-issed
Evan Cheng549f27d32007-08-13 23:45:17 +0000207 /// for this purpose. If parameter all is true, then all uses of the
208 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng2638e1a2007-03-20 08:13:50 +0000209 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
210 ReMatMap[virtReg] = def;
211 }
212
Evan Chengcada2452007-11-28 01:28:46 +0000213 /// @brief returns true if the specified MachineInstr is a spill point.
214 bool isSpillPt(MachineInstr *Pt) const {
215 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
216 }
217
Evan Cheng81a03822007-11-17 00:40:40 +0000218 /// @brief returns the virtual registers that should be spilled due to
219 /// splitting right after the specified MachineInstr.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000220 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Cheng81a03822007-11-17 00:40:40 +0000221 return SpillPt2VirtMap[Pt];
222 }
223
224 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000225 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Chengcada2452007-11-28 01:28:46 +0000226 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
Evan Chengb50bb8c2007-12-05 08:16:32 +0000227 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000228 else {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000229 std::vector<std::pair<unsigned,bool> > Virts;
230 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000231 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
232 }
Evan Cheng81a03822007-11-17 00:40:40 +0000233 }
234
235 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000236 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
237 I = SpillPt2VirtMap.find(Old);
Evan Chengcada2452007-11-28 01:28:46 +0000238 if (I == SpillPt2VirtMap.end())
239 return;
240 while (!I->second.empty()) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000241 unsigned virtReg = I->second.back().first;
242 bool isKill = I->second.back().second;
Evan Chengcada2452007-11-28 01:28:46 +0000243 I->second.pop_back();
Evan Chengb50bb8c2007-12-05 08:16:32 +0000244 addSpillPoint(virtReg, isKill, New);
Evan Cheng81a03822007-11-17 00:40:40 +0000245 }
Evan Chengcada2452007-11-28 01:28:46 +0000246 SpillPt2VirtMap.erase(I);
Evan Cheng81a03822007-11-17 00:40:40 +0000247 }
248
Evan Cheng0cbb1162007-11-29 01:06:25 +0000249 /// @brief returns true if the specified MachineInstr is a restore point.
250 bool isRestorePt(MachineInstr *Pt) const {
251 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
252 }
253
254 /// @brief returns the virtual registers that should be restoreed due to
255 /// splitting right after the specified MachineInstr.
256 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
257 return RestorePt2VirtMap[Pt];
258 }
259
260 /// @brief records the specified MachineInstr as a restore point for virtReg.
261 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
262 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
263 RestorePt2VirtMap[Pt].push_back(virtReg);
264 else {
265 std::vector<unsigned> Virts;
266 Virts.push_back(virtReg);
267 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
268 }
269 }
270
271 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
272 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
273 RestorePt2VirtMap.find(Old);
274 if (I == RestorePt2VirtMap.end())
275 return;
276 while (!I->second.empty()) {
277 unsigned virtReg = I->second.back();
278 I->second.pop_back();
279 addRestorePoint(virtReg, New);
280 }
281 RestorePt2VirtMap.erase(I);
282 }
283
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000284 /// @brief Updates information about the specified virtual register's value
Evan Chengaee4af62007-12-02 08:30:39 +0000285 /// folded into newMI machine instruction.
286 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
287 ModRef MRInfo);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000288
Evan Cheng7f566252007-10-13 02:50:24 +0000289 /// @brief Updates information about the specified virtual register's value
290 /// folded into the specified machine instruction.
291 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
292
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000293 /// @brief returns the virtual registers' values folded in memory
294 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000295 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000296 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000297 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000298 }
Chris Lattner35f27052006-05-01 21:16:03 +0000299
Evan Chengcada2452007-11-28 01:28:46 +0000300 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
301 /// the folded instruction map and spill point map.
302 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
Chris Lattner229924a2006-05-01 22:03:24 +0000303 MI2VirtMap.erase(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000304 SpillPt2VirtMap.erase(MI);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000305 RestorePt2VirtMap.erase(MI);
Chris Lattner35f27052006-05-01 21:16:03 +0000306 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000307
308 void print(std::ostream &OS) const;
Bill Wendling5c7e3262006-12-17 05:15:13 +0000309 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000310 void dump() const;
311 };
312
Bill Wendling5c7e3262006-12-17 05:15:13 +0000313 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
314 VRM.print(OS);
315 return OS;
316 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000317 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
318 VRM.print(OS);
319 return OS;
320 }
321
322 /// Spiller interface: Implementations of this interface assign spilled
323 /// virtual registers to stack slots, rewriting the code.
324 struct Spiller {
325 virtual ~Spiller();
326 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner35f27052006-05-01 21:16:03 +0000327 VirtRegMap &VRM) = 0;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000328 };
329
330 /// createSpiller - Create an return a spiller object, as specified on the
331 /// command line.
332 Spiller* createSpiller();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000333
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000334} // End llvm namespace
335
336#endif