blob: 46f177d4f5c67f36c396dd3d3e0d6946f7cd5c59 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00007//
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
Dan Gohman6f0d0242008-02-10 18:45:23 +000020#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng4cce6b42008-04-11 17:53:36 +000021#include "llvm/ADT/BitVector.h"
Evan Cheng81a03822007-11-17 00:40:40 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000023#include "llvm/ADT/IndexedMap.h"
Evan Chengd3653122008-02-27 03:04:06 +000024#include "llvm/ADT/SmallPtrSet.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000025#include "llvm/Support/Streams.h"
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000026#include <map>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000027
28namespace llvm {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000029 class MachineInstr;
David Greene7e231462007-08-07 16:34:05 +000030 class MachineFunction;
Chris Lattner29268692006-09-05 02:12:02 +000031 class TargetInstrInfo;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000032
Chris Lattner8c4d88d2004-09-30 01:54:45 +000033 class VirtRegMap {
34 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000035 enum {
36 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000037 NO_STACK_SLOT = (1L << 30)-1,
38 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000039 };
40
Chris Lattner35f27052006-05-01 21:16:03 +000041 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000042 typedef std::multimap<MachineInstr*,
43 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000044
Chris Lattner8c4d88d2004-09-30 01:54:45 +000045 private:
Chris Lattner29268692006-09-05 02:12:02 +000046 const TargetInstrInfo &TII;
47
Chris Lattner7f690e62004-09-30 02:15:18 +000048 MachineFunction &MF;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000049 /// Virt2PhysMap - This is a virtual to physical register
50 /// mapping. Each virtual register is required to have an entry in
51 /// it; even spilled virtual registers (the register mapped to a
52 /// spilled register is the temporary used to load it from the
53 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000054 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng81a03822007-11-17 00:40:40 +000055
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000056 /// Virt2StackSlotMap - This is virtual register to stack slot
57 /// mapping. Each spilled virtual register has an entry in it
58 /// which corresponds to the stack slot this register is spilled
59 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000060 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng81a03822007-11-17 00:40:40 +000061
Dan Gohman39e33ac2008-03-12 20:50:04 +000062 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
Evan Cheng81a03822007-11-17 00:40:40 +000063 /// mapping. Each spilled virtual register that should be remat'd has an
64 /// entry in it which corresponds to the remat id.
Evan Cheng549f27d32007-08-13 23:45:17 +000065 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Cheng81a03822007-11-17 00:40:40 +000066
67 /// Virt2SplitMap - This is virtual register to splitted virtual register
68 /// mapping.
69 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
70
Evan Chengadf85902007-12-05 09:51:10 +000071 /// Virt2SplitKillMap - This is splitted virtual register to its last use
Evan Chengd120ffd2007-12-05 10:24:35 +000072 /// (kill) index mapping.
73 IndexedMap<unsigned> Virt2SplitKillMap;
Evan Chengadf85902007-12-05 09:51:10 +000074
Evan Cheng81a03822007-11-17 00:40:40 +000075 /// ReMatMap - This is virtual register to re-materialized instruction
76 /// mapping. Each virtual register whose definition is going to be
77 /// re-materialized has an entry in it.
78 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
79
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000080 /// MI2VirtMap - This is MachineInstr to virtual register
81 /// mapping. In the case of memory spill code being folded into
82 /// instructions, we need to know which virtual register was
83 /// read/written by this instruction.
Chris Lattner7f690e62004-09-30 02:15:18 +000084 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000085
Evan Cheng81a03822007-11-17 00:40:40 +000086 /// SpillPt2VirtMap - This records the virtual registers which should
87 /// be spilled right after the MachineInstr due to live interval
88 /// splitting.
Evan Chengb50bb8c2007-12-05 08:16:32 +000089 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
90 SpillPt2VirtMap;
Evan Cheng2638e1a2007-03-20 08:13:50 +000091
Evan Cheng0cbb1162007-11-29 01:06:25 +000092 /// RestorePt2VirtMap - This records the virtual registers which should
93 /// be restored right before the MachineInstr due to live interval
94 /// splitting.
95 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
96
Evan Cheng676dd7c2008-03-11 07:19:34 +000097 /// EmergencySpillMap - This records the physical registers that should
98 /// be spilled / restored around the MachineInstr since the register
99 /// allocator has run out of registers.
100 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
101
102 /// EmergencySpillSlots - This records emergency spill slots used to
103 /// spill physical registers when the register allocator runs out of
104 /// registers. Ideally only one stack slot is used per function per
105 /// register class.
106 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
107
Evan Cheng2638e1a2007-03-20 08:13:50 +0000108 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng91935142007-04-04 07:40:01 +0000109 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng2638e1a2007-03-20 08:13:50 +0000110 /// the highest id used so far. Note, this starts at (1<<18) to avoid
111 /// conflicts with stack slot numbers.
112 int ReMatId;
113
Evan Chengd3653122008-02-27 03:04:06 +0000114 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
115 int LowSpillSlot, HighSpillSlot;
116
117 /// SpillSlotToUsesMap - Records uses for each register spill slot.
118 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
119
Evan Cheng4cce6b42008-04-11 17:53:36 +0000120 /// ImplicitDefed - One bit for each virtual register. If set it indicates
121 /// the register is implicitly defined.
122 BitVector ImplicitDefed;
123
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000124 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
125 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +0000126
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000127 public:
Dan Gohman61e729e2007-08-02 21:21:54 +0000128 explicit VirtRegMap(MachineFunction &mf);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000129
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000130 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000131
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000132 /// @brief returns true if the specified virtual register is
133 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000134 bool hasPhys(unsigned virtReg) const {
135 return getPhys(virtReg) != NO_PHYS_REG;
136 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000137
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000138 /// @brief returns the physical register mapped to the specified
139 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000140 unsigned getPhys(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000141 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000142 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000143 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000144
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000145 /// @brief creates a mapping for the specified virtual register to
146 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000147 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000148 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
149 TargetRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000150 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000151 "attempt to assign physical register to already mapped "
152 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000153 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000154 }
155
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000156 /// @brief clears the specified virtual register's, physical
157 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000158 void clearVirt(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000159 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000160 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000161 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000162 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000163 }
164
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000165 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000166 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000167 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000168 grow();
169 }
170
Evan Cheng81a03822007-11-17 00:40:40 +0000171 /// @brief records virtReg is a split live interval from SReg.
172 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
173 Virt2SplitMap[virtReg] = SReg;
174 }
175
176 /// @brief returns the live interval virtReg is split from.
177 unsigned getPreSplitReg(unsigned virtReg) {
178 return Virt2SplitMap[virtReg];
179 }
180
Dan Gohman39e33ac2008-03-12 20:50:04 +0000181 /// @brief returns true if the specified virtual register is not
Evan Cheng549f27d32007-08-13 23:45:17 +0000182 /// mapped to a stack slot or rematerialized.
183 bool isAssignedReg(unsigned virtReg) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000184 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
185 getReMatId(virtReg) == NO_STACK_SLOT)
186 return true;
187 // Split register can be assigned a physical register as well as a
188 // stack slot or remat id.
189 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000190 }
191
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000192 /// @brief returns the stack slot mapped to the specified virtual
193 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000194 int getStackSlot(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000195 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000196 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000197 }
198
Evan Cheng549f27d32007-08-13 23:45:17 +0000199 /// @brief returns the rematerialization id mapped to the specified virtual
200 /// register
201 int getReMatId(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000202 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000203 return Virt2ReMatIdMap[virtReg];
204 }
205
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000206 /// @brief create a mapping for the specifed virtual register to
207 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000208 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000209 /// @brief create a mapping for the specified virtual register to
210 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000211 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
212
Evan Cheng2638e1a2007-03-20 08:13:50 +0000213 /// @brief assign an unique re-materialization id to the specified
214 /// virtual register.
215 int assignVirtReMatId(unsigned virtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000216 /// @brief assign an unique re-materialization id to the specified
217 /// virtual register.
218 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng2638e1a2007-03-20 08:13:50 +0000219
220 /// @brief returns true if the specified virtual register is being
221 /// re-materialized.
222 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng549f27d32007-08-13 23:45:17 +0000223 return ReMatMap[virtReg] != NULL;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000224 }
225
226 /// @brief returns the original machine instruction being re-issued
227 /// to re-materialize the specified virtual register.
Evan Cheng549f27d32007-08-13 23:45:17 +0000228 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng2638e1a2007-03-20 08:13:50 +0000229 return ReMatMap[virtReg];
230 }
231
232 /// @brief records the specified virtual register will be
233 /// re-materialized and the original instruction which will be re-issed
Evan Cheng549f27d32007-08-13 23:45:17 +0000234 /// for this purpose. If parameter all is true, then all uses of the
235 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng2638e1a2007-03-20 08:13:50 +0000236 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
237 ReMatMap[virtReg] = def;
238 }
239
Evan Chengadf85902007-12-05 09:51:10 +0000240 /// @brief record the last use (kill) of a split virtual register.
Evan Chengd120ffd2007-12-05 10:24:35 +0000241 void addKillPoint(unsigned virtReg, unsigned index) {
242 Virt2SplitKillMap[virtReg] = index;
Evan Chengadf85902007-12-05 09:51:10 +0000243 }
244
Evan Chengd120ffd2007-12-05 10:24:35 +0000245 unsigned getKillPoint(unsigned virtReg) const {
246 return Virt2SplitKillMap[virtReg];
247 }
248
249 /// @brief remove the last use (kill) of a split virtual register.
Evan Chengadf85902007-12-05 09:51:10 +0000250 void removeKillPoint(unsigned virtReg) {
Evan Chengd120ffd2007-12-05 10:24:35 +0000251 Virt2SplitKillMap[virtReg] = 0;
Evan Chengadf85902007-12-05 09:51:10 +0000252 }
253
Evan Chengcada2452007-11-28 01:28:46 +0000254 /// @brief returns true if the specified MachineInstr is a spill point.
255 bool isSpillPt(MachineInstr *Pt) const {
256 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
257 }
258
Evan Cheng81a03822007-11-17 00:40:40 +0000259 /// @brief returns the virtual registers that should be spilled due to
260 /// splitting right after the specified MachineInstr.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000261 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Cheng81a03822007-11-17 00:40:40 +0000262 return SpillPt2VirtMap[Pt];
263 }
264
265 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000266 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Chengcada2452007-11-28 01:28:46 +0000267 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
Evan Chengb50bb8c2007-12-05 08:16:32 +0000268 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000269 else {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000270 std::vector<std::pair<unsigned,bool> > Virts;
271 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000272 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
273 }
Evan Cheng81a03822007-11-17 00:40:40 +0000274 }
275
Evan Chengc1f53c72008-03-11 21:34:46 +0000276 /// @brief - transfer spill point information from one instruction to
277 /// another.
Evan Cheng81a03822007-11-17 00:40:40 +0000278 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000279 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
280 I = SpillPt2VirtMap.find(Old);
Evan Chengcada2452007-11-28 01:28:46 +0000281 if (I == SpillPt2VirtMap.end())
282 return;
283 while (!I->second.empty()) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000284 unsigned virtReg = I->second.back().first;
285 bool isKill = I->second.back().second;
Evan Chengcada2452007-11-28 01:28:46 +0000286 I->second.pop_back();
Evan Chengb50bb8c2007-12-05 08:16:32 +0000287 addSpillPoint(virtReg, isKill, New);
Evan Cheng81a03822007-11-17 00:40:40 +0000288 }
Evan Chengcada2452007-11-28 01:28:46 +0000289 SpillPt2VirtMap.erase(I);
Evan Cheng81a03822007-11-17 00:40:40 +0000290 }
291
Evan Cheng0cbb1162007-11-29 01:06:25 +0000292 /// @brief returns true if the specified MachineInstr is a restore point.
293 bool isRestorePt(MachineInstr *Pt) const {
294 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
295 }
296
297 /// @brief returns the virtual registers that should be restoreed due to
298 /// splitting right after the specified MachineInstr.
299 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
300 return RestorePt2VirtMap[Pt];
301 }
302
303 /// @brief records the specified MachineInstr as a restore point for virtReg.
304 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
305 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
306 RestorePt2VirtMap[Pt].push_back(virtReg);
307 else {
308 std::vector<unsigned> Virts;
309 Virts.push_back(virtReg);
310 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
311 }
312 }
313
Evan Cheng676dd7c2008-03-11 07:19:34 +0000314 /// @brief - transfer restore point information from one instruction to
315 /// another.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000316 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
317 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
318 RestorePt2VirtMap.find(Old);
319 if (I == RestorePt2VirtMap.end())
320 return;
321 while (!I->second.empty()) {
322 unsigned virtReg = I->second.back();
323 I->second.pop_back();
324 addRestorePoint(virtReg, New);
325 }
326 RestorePt2VirtMap.erase(I);
327 }
328
Evan Cheng676dd7c2008-03-11 07:19:34 +0000329 /// @brief records that the specified physical register must be spilled
330 /// around the specified machine instr.
331 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
332 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
333 EmergencySpillMap[MI].push_back(PhysReg);
334 else {
335 std::vector<unsigned> PhysRegs;
336 PhysRegs.push_back(PhysReg);
337 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
338 }
339 }
340
341 /// @brief returns true if one or more physical registers must be spilled
342 /// around the specified instruction.
343 bool hasEmergencySpills(MachineInstr *MI) const {
344 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
345 }
346
347 /// @brief returns the physical registers to be spilled and restored around
348 /// the instruction.
349 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
350 return EmergencySpillMap[MI];
351 }
352
Evan Chengc1f53c72008-03-11 21:34:46 +0000353 /// @brief - transfer emergency spill information from one instruction to
354 /// another.
355 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
356 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
357 EmergencySpillMap.find(Old);
358 if (I == EmergencySpillMap.end())
359 return;
360 while (!I->second.empty()) {
361 unsigned virtReg = I->second.back();
362 I->second.pop_back();
363 addEmergencySpill(virtReg, New);
364 }
365 EmergencySpillMap.erase(I);
366 }
367
Evan Cheng676dd7c2008-03-11 07:19:34 +0000368 /// @brief return or get a emergency spill slot for the register class.
369 int getEmergencySpillSlot(const TargetRegisterClass *RC);
370
Evan Chengd3653122008-02-27 03:04:06 +0000371 /// @brief Return lowest spill slot index.
372 int getLowSpillSlot() const {
373 return LowSpillSlot;
374 }
375
376 /// @brief Return highest spill slot index.
377 int getHighSpillSlot() const {
378 return HighSpillSlot;
379 }
380
381 /// @brief Records a spill slot use.
382 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
383
384 /// @brief Returns true if spill slot has been used.
385 bool isSpillSlotUsed(int FrameIndex) const {
386 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
387 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
388 }
389
Evan Cheng4cce6b42008-04-11 17:53:36 +0000390 /// @brief Mark the specified register as being implicitly defined.
391 void setIsImplicitlyDefined(unsigned VirtReg) {
392 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
393 }
394
395 /// @brief Returns true if the virtual register is implicitly defined.
396 bool isImplicitlyDefined(unsigned VirtReg) const {
397 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
398 }
399
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000400 /// @brief Updates information about the specified virtual register's value
Evan Chengaee4af62007-12-02 08:30:39 +0000401 /// folded into newMI machine instruction.
402 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
403 ModRef MRInfo);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000404
Evan Cheng7f566252007-10-13 02:50:24 +0000405 /// @brief Updates information about the specified virtual register's value
406 /// folded into the specified machine instruction.
407 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
408
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000409 /// @brief returns the virtual registers' values folded in memory
410 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000411 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000412 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000413 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000414 }
Chris Lattner35f27052006-05-01 21:16:03 +0000415
Evan Chengcada2452007-11-28 01:28:46 +0000416 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
417 /// the folded instruction map and spill point map.
Evan Chengd3653122008-02-27 03:04:06 +0000418 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000419
420 void print(std::ostream &OS) const;
Bill Wendling5c7e3262006-12-17 05:15:13 +0000421 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000422 void dump() const;
423 };
424
Bill Wendling5c7e3262006-12-17 05:15:13 +0000425 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
426 VRM.print(OS);
427 return OS;
428 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000429 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
430 VRM.print(OS);
431 return OS;
432 }
433
434 /// Spiller interface: Implementations of this interface assign spilled
435 /// virtual registers to stack slots, rewriting the code.
436 struct Spiller {
437 virtual ~Spiller();
438 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner35f27052006-05-01 21:16:03 +0000439 VirtRegMap &VRM) = 0;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000440 };
441
442 /// createSpiller - Create an return a spiller object, as specified on the
443 /// command line.
444 Spiller* createSpiller();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000445
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000446} // End llvm namespace
447
448#endif