blob: 7740740548b88ed29578a90f0d9ac1106d4e09ef [file] [log] [blame]
Alkis Evlogimenosc794a902004-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 Lattnere2b77d52004-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 Evlogimenosc794a902004-02-23 23:08:11 +000014//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_VIRTREGMAP_H
18#define LLVM_CODEGEN_VIRTREGMAP_H
19
Chris Lattnere2b77d52004-09-30 01:54:45 +000020#include "llvm/Target/MRegisterInfo.h"
Evan Cheng8e223792007-11-17 00:40:40 +000021#include "llvm/ADT/DenseMap.h"
Chris Lattner1003dc72007-02-01 05:32:05 +000022#include "llvm/ADT/IndexedMap.h"
Bill Wendlingf3baad32006-12-07 01:30:32 +000023#include "llvm/Support/Streams.h"
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000024#include <map>
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000025
26namespace llvm {
Chris Lattnere2b77d52004-09-30 01:54:45 +000027 class MachineInstr;
David Greene99905f12007-08-07 16:34:05 +000028 class MachineFunction;
Chris Lattner13a5dcd2006-09-05 02:12:02 +000029 class TargetInstrInfo;
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000030
Chris Lattnere2b77d52004-09-30 01:54:45 +000031 class VirtRegMap {
32 public:
Evan Cheng0e3278e2007-03-20 08:13:50 +000033 enum {
34 NO_PHYS_REG = 0,
Evan Cheng8be98c12007-04-04 07:40:01 +000035 NO_STACK_SLOT = (1L << 30)-1,
36 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng0e3278e2007-03-20 08:13:50 +000037 };
38
Chris Lattner4dee67c2006-05-01 21:16:03 +000039 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattner1905ae62004-10-01 23:15:36 +000040 typedef std::multimap<MachineInstr*,
41 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000042
Chris Lattnere2b77d52004-09-30 01:54:45 +000043 private:
Chris Lattner13a5dcd2006-09-05 02:12:02 +000044 const TargetInstrInfo &TII;
45
Chris Lattner39fef8d2004-09-30 02:15:18 +000046 MachineFunction &MF;
Alkis Evlogimenoscc37da12004-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 Lattner1003dc72007-02-01 05:32:05 +000052 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng8e223792007-11-17 00:40:40 +000053
Alkis Evlogimenoscc37da12004-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 Lattner1003dc72007-02-01 05:32:05 +000058 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng8e223792007-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 Cheng33820da2007-08-13 23:45:17 +000063 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Cheng8e223792007-11-17 00:40:40 +000064
65 /// Virt2SplitMap - This is virtual register to splitted virtual register
66 /// mapping.
67 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
68
Evan Cheng06353b42007-12-05 09:51:10 +000069 /// Virt2SplitKillMap - This is splitted virtual register to its last use
70 /// (kill) mapping.
71 IndexedMap<MachineOperand*> Virt2SplitKillMap;
72
Evan Cheng8e223792007-11-17 00:40:40 +000073 /// ReMatMap - This is virtual register to re-materialized instruction
74 /// mapping. Each virtual register whose definition is going to be
75 /// re-materialized has an entry in it.
76 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
77
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000078 /// MI2VirtMap - This is MachineInstr to virtual register
79 /// mapping. In the case of memory spill code being folded into
80 /// instructions, we need to know which virtual register was
81 /// read/written by this instruction.
Chris Lattner39fef8d2004-09-30 02:15:18 +000082 MI2VirtMapTy MI2VirtMap;
Misha Brukman835702a2005-04-21 22:36:52 +000083
Evan Cheng8e223792007-11-17 00:40:40 +000084 /// SpillPt2VirtMap - This records the virtual registers which should
85 /// be spilled right after the MachineInstr due to live interval
86 /// splitting.
Evan Chengd7de56a2007-12-05 08:16:32 +000087 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
88 SpillPt2VirtMap;
Evan Cheng0e3278e2007-03-20 08:13:50 +000089
Evan Chengbe255b02007-11-29 01:06:25 +000090 /// RestorePt2VirtMap - This records the virtual registers which should
91 /// be restored right before the MachineInstr due to live interval
92 /// splitting.
93 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
94
Evan Cheng0e3278e2007-03-20 08:13:50 +000095 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng8be98c12007-04-04 07:40:01 +000096 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng0e3278e2007-03-20 08:13:50 +000097 /// the highest id used so far. Note, this starts at (1<<18) to avoid
98 /// conflicts with stack slot numbers.
99 int ReMatId;
100
Chris Lattnere2b77d52004-09-30 01:54:45 +0000101 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
102 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenosab77b052004-02-23 23:47:10 +0000103
Chris Lattnere2b77d52004-09-30 01:54:45 +0000104 public:
Dan Gohman5f6a9da52007-08-02 21:21:54 +0000105 explicit VirtRegMap(MachineFunction &mf);
Alkis Evlogimenosc794a902004-02-23 23:08:11 +0000106
Chris Lattnere2b77d52004-09-30 01:54:45 +0000107 void grow();
Alkis Evlogimenos31953c72004-03-01 23:18:15 +0000108
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000109 /// @brief returns true if the specified virtual register is
110 /// mapped to a physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000111 bool hasPhys(unsigned virtReg) const {
112 return getPhys(virtReg) != NO_PHYS_REG;
113 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +0000114
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000115 /// @brief returns the physical register mapped to the specified
116 /// virtual register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000117 unsigned getPhys(unsigned virtReg) const {
118 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000119 return Virt2PhysMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +0000120 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +0000121
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000122 /// @brief creates a mapping for the specified virtual register to
123 /// the specified physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000124 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
125 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
126 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000127 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000128 "attempt to assign physical register to already mapped "
129 "virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +0000130 Virt2PhysMap[virtReg] = physReg;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000131 }
132
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000133 /// @brief clears the specified virtual register's, physical
134 /// register mapping
Chris Lattnere2b77d52004-09-30 01:54:45 +0000135 void clearVirt(unsigned virtReg) {
136 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000137 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000138 "attempt to clear a not assigned virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +0000139 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000140 }
141
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000142 /// @brief clears all virtual to physical register mappings
Chris Lattnere2b77d52004-09-30 01:54:45 +0000143 void clearAllVirt() {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000144 Virt2PhysMap.clear();
Chris Lattnere2b77d52004-09-30 01:54:45 +0000145 grow();
146 }
147
Evan Cheng8e223792007-11-17 00:40:40 +0000148 /// @brief records virtReg is a split live interval from SReg.
149 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
150 Virt2SplitMap[virtReg] = SReg;
151 }
152
153 /// @brief returns the live interval virtReg is split from.
154 unsigned getPreSplitReg(unsigned virtReg) {
155 return Virt2SplitMap[virtReg];
156 }
157
Evan Cheng33820da2007-08-13 23:45:17 +0000158 /// @brief returns true is the specified virtual register is not
159 /// mapped to a stack slot or rematerialized.
160 bool isAssignedReg(unsigned virtReg) const {
Evan Cheng8e223792007-11-17 00:40:40 +0000161 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
162 getReMatId(virtReg) == NO_STACK_SLOT)
163 return true;
164 // Split register can be assigned a physical register as well as a
165 // stack slot or remat id.
166 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000167 }
168
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000169 /// @brief returns the stack slot mapped to the specified virtual
170 /// register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000171 int getStackSlot(unsigned virtReg) const {
172 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000173 return Virt2StackSlotMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +0000174 }
175
Evan Cheng33820da2007-08-13 23:45:17 +0000176 /// @brief returns the rematerialization id mapped to the specified virtual
177 /// register
178 int getReMatId(unsigned virtReg) const {
179 assert(MRegisterInfo::isVirtualRegister(virtReg));
180 return Virt2ReMatIdMap[virtReg];
181 }
182
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000183 /// @brief create a mapping for the specifed virtual register to
184 /// the next available stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000185 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000186 /// @brief create a mapping for the specified virtual register to
187 /// the specified stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000188 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
189
Evan Cheng0e3278e2007-03-20 08:13:50 +0000190 /// @brief assign an unique re-materialization id to the specified
191 /// virtual register.
192 int assignVirtReMatId(unsigned virtReg);
Evan Cheng33820da2007-08-13 23:45:17 +0000193 /// @brief assign an unique re-materialization id to the specified
194 /// virtual register.
195 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng0e3278e2007-03-20 08:13:50 +0000196
197 /// @brief returns true if the specified virtual register is being
198 /// re-materialized.
199 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng33820da2007-08-13 23:45:17 +0000200 return ReMatMap[virtReg] != NULL;
Evan Cheng0e3278e2007-03-20 08:13:50 +0000201 }
202
203 /// @brief returns the original machine instruction being re-issued
204 /// to re-materialize the specified virtual register.
Evan Cheng33820da2007-08-13 23:45:17 +0000205 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng0e3278e2007-03-20 08:13:50 +0000206 return ReMatMap[virtReg];
207 }
208
209 /// @brief records the specified virtual register will be
210 /// re-materialized and the original instruction which will be re-issed
Evan Cheng33820da2007-08-13 23:45:17 +0000211 /// for this purpose. If parameter all is true, then all uses of the
212 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng0e3278e2007-03-20 08:13:50 +0000213 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
214 ReMatMap[virtReg] = def;
215 }
216
Evan Cheng06353b42007-12-05 09:51:10 +0000217 /// @brief record the last use (kill) of a split virtual register.
218 void addKillPoint(unsigned virtReg, MachineOperand *Op) {
219 Virt2SplitKillMap[virtReg] = Op;
220 }
221
222 /// @brief reset and remove the last use (kill) of a split virtual register.
223 void removeKillPoint(unsigned virtReg) {
224 MachineOperand *MO = Virt2SplitKillMap[virtReg];
225 if (MO) {
226 assert(MO->isKill() && "Split last use is not marked kill?");
227 MO->unsetIsKill();
228 Virt2SplitKillMap[virtReg] = NULL;
229 }
230 }
231
Evan Chengc1648b62007-11-28 01:28:46 +0000232 /// @brief returns true if the specified MachineInstr is a spill point.
233 bool isSpillPt(MachineInstr *Pt) const {
234 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
235 }
236
Evan Cheng8e223792007-11-17 00:40:40 +0000237 /// @brief returns the virtual registers that should be spilled due to
238 /// splitting right after the specified MachineInstr.
Evan Chengd7de56a2007-12-05 08:16:32 +0000239 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Cheng8e223792007-11-17 00:40:40 +0000240 return SpillPt2VirtMap[Pt];
241 }
242
243 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chengd7de56a2007-12-05 08:16:32 +0000244 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Chengc1648b62007-11-28 01:28:46 +0000245 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
Evan Chengd7de56a2007-12-05 08:16:32 +0000246 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
Evan Chengc1648b62007-11-28 01:28:46 +0000247 else {
Evan Chengd7de56a2007-12-05 08:16:32 +0000248 std::vector<std::pair<unsigned,bool> > Virts;
249 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Chengc1648b62007-11-28 01:28:46 +0000250 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
251 }
Evan Cheng8e223792007-11-17 00:40:40 +0000252 }
253
254 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Chengd7de56a2007-12-05 08:16:32 +0000255 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
256 I = SpillPt2VirtMap.find(Old);
Evan Chengc1648b62007-11-28 01:28:46 +0000257 if (I == SpillPt2VirtMap.end())
258 return;
259 while (!I->second.empty()) {
Evan Chengd7de56a2007-12-05 08:16:32 +0000260 unsigned virtReg = I->second.back().first;
261 bool isKill = I->second.back().second;
Evan Chengc1648b62007-11-28 01:28:46 +0000262 I->second.pop_back();
Evan Chengd7de56a2007-12-05 08:16:32 +0000263 addSpillPoint(virtReg, isKill, New);
Evan Cheng8e223792007-11-17 00:40:40 +0000264 }
Evan Chengc1648b62007-11-28 01:28:46 +0000265 SpillPt2VirtMap.erase(I);
Evan Cheng8e223792007-11-17 00:40:40 +0000266 }
267
Evan Chengbe255b02007-11-29 01:06:25 +0000268 /// @brief returns true if the specified MachineInstr is a restore point.
269 bool isRestorePt(MachineInstr *Pt) const {
270 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
271 }
272
273 /// @brief returns the virtual registers that should be restoreed due to
274 /// splitting right after the specified MachineInstr.
275 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
276 return RestorePt2VirtMap[Pt];
277 }
278
279 /// @brief records the specified MachineInstr as a restore point for virtReg.
280 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
281 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
282 RestorePt2VirtMap[Pt].push_back(virtReg);
283 else {
284 std::vector<unsigned> Virts;
285 Virts.push_back(virtReg);
286 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
287 }
288 }
289
290 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
291 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
292 RestorePt2VirtMap.find(Old);
293 if (I == RestorePt2VirtMap.end())
294 return;
295 while (!I->second.empty()) {
296 unsigned virtReg = I->second.back();
297 I->second.pop_back();
298 addRestorePoint(virtReg, New);
299 }
300 RestorePt2VirtMap.erase(I);
301 }
302
Chris Lattner1905ae62004-10-01 23:15:36 +0000303 /// @brief Updates information about the specified virtual register's value
Evan Chengf45a1d62007-12-02 08:30:39 +0000304 /// folded into newMI machine instruction.
305 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
306 ModRef MRInfo);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000307
Evan Chengb6307652007-10-13 02:50:24 +0000308 /// @brief Updates information about the specified virtual register's value
309 /// folded into the specified machine instruction.
310 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
311
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000312 /// @brief returns the virtual registers' values folded in memory
313 /// operands of this instruction
Chris Lattner39fef8d2004-09-30 02:15:18 +0000314 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattnere2b77d52004-09-30 01:54:45 +0000315 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000316 return MI2VirtMap.equal_range(MI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000317 }
Chris Lattner4dee67c2006-05-01 21:16:03 +0000318
Evan Chengc1648b62007-11-28 01:28:46 +0000319 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
320 /// the folded instruction map and spill point map.
321 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
Chris Lattnerfd0a5472006-05-01 22:03:24 +0000322 MI2VirtMap.erase(MI);
Evan Chengc1648b62007-11-28 01:28:46 +0000323 SpillPt2VirtMap.erase(MI);
Evan Chengbe255b02007-11-29 01:06:25 +0000324 RestorePt2VirtMap.erase(MI);
Chris Lattner4dee67c2006-05-01 21:16:03 +0000325 }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000326
327 void print(std::ostream &OS) const;
Bill Wendlinga77f1422006-12-17 05:15:13 +0000328 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000329 void dump() const;
330 };
331
Bill Wendlinga77f1422006-12-17 05:15:13 +0000332 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
333 VRM.print(OS);
334 return OS;
335 }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000336 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
337 VRM.print(OS);
338 return OS;
339 }
340
341 /// Spiller interface: Implementations of this interface assign spilled
342 /// virtual registers to stack slots, rewriting the code.
343 struct Spiller {
344 virtual ~Spiller();
345 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner4dee67c2006-05-01 21:16:03 +0000346 VirtRegMap &VRM) = 0;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000347 };
348
349 /// createSpiller - Create an return a spiller object, as specified on the
350 /// command line.
351 Spiller* createSpiller();
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000352
Alkis Evlogimenosc794a902004-02-23 23:08:11 +0000353} // End llvm namespace
354
355#endif