blob: fe767b7671e11b5851caff4f011e05aaa29ea6b2 [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
Owen Anderson49c8aa02009-03-13 05:55:11 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng4cce6b42008-04-11 17:53:36 +000022#include "llvm/ADT/BitVector.h"
Evan Chengc781a242009-05-03 18:32:42 +000023#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000024#include "llvm/ADT/IndexedMap.h"
Evan Chengd3653122008-02-27 03:04:06 +000025#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000026#include "llvm/ADT/SmallVector.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000027#include "llvm/Support/Streams.h"
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000028#include <map>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000029
30namespace llvm {
Evan Chengc781a242009-05-03 18:32:42 +000031 class LiveIntervals;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000032 class MachineInstr;
David Greene7e231462007-08-07 16:34:05 +000033 class MachineFunction;
Evan Cheng90f95f82009-06-14 20:22:55 +000034 class MachineRegisterInfo;
Chris Lattner29268692006-09-05 02:12:02 +000035 class TargetInstrInfo;
Mike Stumpfe095f32009-05-04 18:40:41 +000036 class TargetRegisterInfo;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000037
Owen Anderson49c8aa02009-03-13 05:55:11 +000038 class VirtRegMap : public MachineFunctionPass {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000039 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000040 enum {
41 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000042 NO_STACK_SLOT = (1L << 30)-1,
43 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000044 };
45
Chris Lattner35f27052006-05-01 21:16:03 +000046 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000047 typedef std::multimap<MachineInstr*,
48 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000049
Chris Lattner8c4d88d2004-09-30 01:54:45 +000050 private:
Evan Cheng90f95f82009-06-14 20:22:55 +000051 MachineRegisterInfo *MRI;
Owen Anderson49c8aa02009-03-13 05:55:11 +000052 const TargetInstrInfo *TII;
Mike Stumpfe095f32009-05-04 18:40:41 +000053 const TargetRegisterInfo *TRI;
Owen Anderson49c8aa02009-03-13 05:55:11 +000054 MachineFunction *MF;
Mike Stumpfe095f32009-05-04 18:40:41 +000055
56 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
57
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000058 /// Virt2PhysMap - This is a virtual to physical register
59 /// mapping. Each virtual register is required to have an entry in
60 /// it; even spilled virtual registers (the register mapped to a
61 /// spilled register is the temporary used to load it from the
62 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000063 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng81a03822007-11-17 00:40:40 +000064
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000065 /// Virt2StackSlotMap - This is virtual register to stack slot
66 /// mapping. Each spilled virtual register has an entry in it
67 /// which corresponds to the stack slot this register is spilled
68 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000069 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng81a03822007-11-17 00:40:40 +000070
Dan Gohman39e33ac2008-03-12 20:50:04 +000071 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
Evan Cheng81a03822007-11-17 00:40:40 +000072 /// mapping. Each spilled virtual register that should be remat'd has an
73 /// entry in it which corresponds to the remat id.
Evan Cheng549f27d32007-08-13 23:45:17 +000074 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Cheng81a03822007-11-17 00:40:40 +000075
76 /// Virt2SplitMap - This is virtual register to splitted virtual register
77 /// mapping.
78 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
79
Evan Chengadf85902007-12-05 09:51:10 +000080 /// Virt2SplitKillMap - This is splitted virtual register to its last use
Evan Chengd120ffd2007-12-05 10:24:35 +000081 /// (kill) index mapping.
82 IndexedMap<unsigned> Virt2SplitKillMap;
Evan Chengadf85902007-12-05 09:51:10 +000083
Evan Cheng81a03822007-11-17 00:40:40 +000084 /// ReMatMap - This is virtual register to re-materialized instruction
85 /// mapping. Each virtual register whose definition is going to be
86 /// re-materialized has an entry in it.
87 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
88
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000089 /// MI2VirtMap - This is MachineInstr to virtual register
90 /// mapping. In the case of memory spill code being folded into
91 /// instructions, we need to know which virtual register was
92 /// read/written by this instruction.
Chris Lattner7f690e62004-09-30 02:15:18 +000093 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000094
Evan Cheng81a03822007-11-17 00:40:40 +000095 /// SpillPt2VirtMap - This records the virtual registers which should
96 /// be spilled right after the MachineInstr due to live interval
97 /// splitting.
Evan Chengb50bb8c2007-12-05 08:16:32 +000098 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
99 SpillPt2VirtMap;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000100
Evan Cheng0cbb1162007-11-29 01:06:25 +0000101 /// RestorePt2VirtMap - This records the virtual registers which should
102 /// be restored right before the MachineInstr due to live interval
103 /// splitting.
104 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
105
Evan Cheng676dd7c2008-03-11 07:19:34 +0000106 /// EmergencySpillMap - This records the physical registers that should
107 /// be spilled / restored around the MachineInstr since the register
108 /// allocator has run out of registers.
109 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
110
111 /// EmergencySpillSlots - This records emergency spill slots used to
112 /// spill physical registers when the register allocator runs out of
113 /// registers. Ideally only one stack slot is used per function per
114 /// register class.
115 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
116
Evan Cheng2638e1a2007-03-20 08:13:50 +0000117 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng91935142007-04-04 07:40:01 +0000118 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng2638e1a2007-03-20 08:13:50 +0000119 /// the highest id used so far. Note, this starts at (1<<18) to avoid
120 /// conflicts with stack slot numbers.
121 int ReMatId;
122
Evan Chengd3653122008-02-27 03:04:06 +0000123 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
124 int LowSpillSlot, HighSpillSlot;
125
126 /// SpillSlotToUsesMap - Records uses for each register spill slot.
127 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
128
Evan Cheng4cce6b42008-04-11 17:53:36 +0000129 /// ImplicitDefed - One bit for each virtual register. If set it indicates
130 /// the register is implicitly defined.
131 BitVector ImplicitDefed;
132
Evan Chengc781a242009-05-03 18:32:42 +0000133 /// UnusedRegs - A list of physical registers that have not been used.
134 BitVector UnusedRegs;
135
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000136 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
137 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +0000138
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000139 public:
Owen Anderson49c8aa02009-03-13 05:55:11 +0000140 static char ID;
141 VirtRegMap() : MachineFunctionPass(&ID), Virt2PhysMap(NO_PHYS_REG),
142 Virt2StackSlotMap(NO_STACK_SLOT),
143 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
144 Virt2SplitKillMap(0), ReMatMap(NULL),
145 ReMatId(MAX_STACK_SLOT+1),
146 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
147 virtual bool runOnMachineFunction(MachineFunction &MF);
148
149 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
150 AU.setPreservesAll();
151 MachineFunctionPass::getAnalysisUsage(AU);
152 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000153
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000154 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000155
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000156 /// @brief returns true if the specified virtual register is
157 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000158 bool hasPhys(unsigned virtReg) const {
159 return getPhys(virtReg) != NO_PHYS_REG;
160 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000161
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000162 /// @brief returns the physical register mapped to the specified
163 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000164 unsigned getPhys(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000165 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000166 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000167 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000168
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000169 /// @brief creates a mapping for the specified virtual register to
170 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000171 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000172 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
173 TargetRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000174 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000175 "attempt to assign physical register to already mapped "
176 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000177 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000178 }
179
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000180 /// @brief clears the specified virtual register's, physical
181 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000182 void clearVirt(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000183 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000184 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000185 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000186 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000187 }
188
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000189 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000190 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000191 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000192 grow();
193 }
194
Evan Cheng90f95f82009-06-14 20:22:55 +0000195 /// @brief returns the register allocation preference.
196 unsigned getRegAllocPref(unsigned virtReg);
197
Evan Cheng81a03822007-11-17 00:40:40 +0000198 /// @brief records virtReg is a split live interval from SReg.
199 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
200 Virt2SplitMap[virtReg] = SReg;
201 }
202
203 /// @brief returns the live interval virtReg is split from.
204 unsigned getPreSplitReg(unsigned virtReg) {
205 return Virt2SplitMap[virtReg];
206 }
207
Dan Gohman39e33ac2008-03-12 20:50:04 +0000208 /// @brief returns true if the specified virtual register is not
Evan Cheng549f27d32007-08-13 23:45:17 +0000209 /// mapped to a stack slot or rematerialized.
210 bool isAssignedReg(unsigned virtReg) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000211 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
212 getReMatId(virtReg) == NO_STACK_SLOT)
213 return true;
214 // Split register can be assigned a physical register as well as a
215 // stack slot or remat id.
216 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000217 }
218
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000219 /// @brief returns the stack slot mapped to the specified virtual
220 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000221 int getStackSlot(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000222 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000223 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000224 }
225
Evan Cheng549f27d32007-08-13 23:45:17 +0000226 /// @brief returns the rematerialization id mapped to the specified virtual
227 /// register
228 int getReMatId(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000229 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000230 return Virt2ReMatIdMap[virtReg];
231 }
232
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000233 /// @brief create a mapping for the specifed virtual register to
234 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000235 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000236 /// @brief create a mapping for the specified virtual register to
237 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000238 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
239
Evan Cheng2638e1a2007-03-20 08:13:50 +0000240 /// @brief assign an unique re-materialization id to the specified
241 /// virtual register.
242 int assignVirtReMatId(unsigned virtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000243 /// @brief assign an unique re-materialization id to the specified
244 /// virtual register.
245 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng2638e1a2007-03-20 08:13:50 +0000246
247 /// @brief returns true if the specified virtual register is being
248 /// re-materialized.
249 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng549f27d32007-08-13 23:45:17 +0000250 return ReMatMap[virtReg] != NULL;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000251 }
252
253 /// @brief returns the original machine instruction being re-issued
254 /// to re-materialize the specified virtual register.
Evan Cheng549f27d32007-08-13 23:45:17 +0000255 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng2638e1a2007-03-20 08:13:50 +0000256 return ReMatMap[virtReg];
257 }
258
259 /// @brief records the specified virtual register will be
260 /// re-materialized and the original instruction which will be re-issed
Evan Cheng549f27d32007-08-13 23:45:17 +0000261 /// for this purpose. If parameter all is true, then all uses of the
262 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng2638e1a2007-03-20 08:13:50 +0000263 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
264 ReMatMap[virtReg] = def;
265 }
266
Evan Chengadf85902007-12-05 09:51:10 +0000267 /// @brief record the last use (kill) of a split virtual register.
Evan Chengd120ffd2007-12-05 10:24:35 +0000268 void addKillPoint(unsigned virtReg, unsigned index) {
269 Virt2SplitKillMap[virtReg] = index;
Evan Chengadf85902007-12-05 09:51:10 +0000270 }
271
Evan Chengd120ffd2007-12-05 10:24:35 +0000272 unsigned getKillPoint(unsigned virtReg) const {
273 return Virt2SplitKillMap[virtReg];
274 }
275
276 /// @brief remove the last use (kill) of a split virtual register.
Evan Chengadf85902007-12-05 09:51:10 +0000277 void removeKillPoint(unsigned virtReg) {
Evan Chengd120ffd2007-12-05 10:24:35 +0000278 Virt2SplitKillMap[virtReg] = 0;
Evan Chengadf85902007-12-05 09:51:10 +0000279 }
280
Evan Chengcada2452007-11-28 01:28:46 +0000281 /// @brief returns true if the specified MachineInstr is a spill point.
282 bool isSpillPt(MachineInstr *Pt) const {
283 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
284 }
285
Evan Cheng81a03822007-11-17 00:40:40 +0000286 /// @brief returns the virtual registers that should be spilled due to
287 /// splitting right after the specified MachineInstr.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000288 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Cheng81a03822007-11-17 00:40:40 +0000289 return SpillPt2VirtMap[Pt];
290 }
291
292 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000293 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Chengc781a242009-05-03 18:32:42 +0000294 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
295 I = SpillPt2VirtMap.find(Pt);
296 if (I != SpillPt2VirtMap.end())
297 I->second.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000298 else {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000299 std::vector<std::pair<unsigned,bool> > Virts;
300 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000301 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
302 }
Evan Cheng81a03822007-11-17 00:40:40 +0000303 }
304
Evan Chengc1f53c72008-03-11 21:34:46 +0000305 /// @brief - transfer spill point information from one instruction to
306 /// another.
Evan Cheng81a03822007-11-17 00:40:40 +0000307 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Chengc781a242009-05-03 18:32:42 +0000308 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
Evan Chengb50bb8c2007-12-05 08:16:32 +0000309 I = SpillPt2VirtMap.find(Old);
Evan Chengcada2452007-11-28 01:28:46 +0000310 if (I == SpillPt2VirtMap.end())
311 return;
312 while (!I->second.empty()) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000313 unsigned virtReg = I->second.back().first;
314 bool isKill = I->second.back().second;
Evan Chengcada2452007-11-28 01:28:46 +0000315 I->second.pop_back();
Evan Chengb50bb8c2007-12-05 08:16:32 +0000316 addSpillPoint(virtReg, isKill, New);
Evan Cheng81a03822007-11-17 00:40:40 +0000317 }
Evan Chengcada2452007-11-28 01:28:46 +0000318 SpillPt2VirtMap.erase(I);
Evan Cheng81a03822007-11-17 00:40:40 +0000319 }
320
Evan Cheng0cbb1162007-11-29 01:06:25 +0000321 /// @brief returns true if the specified MachineInstr is a restore point.
322 bool isRestorePt(MachineInstr *Pt) const {
323 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
324 }
325
326 /// @brief returns the virtual registers that should be restoreed due to
327 /// splitting right after the specified MachineInstr.
328 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
329 return RestorePt2VirtMap[Pt];
330 }
331
332 /// @brief records the specified MachineInstr as a restore point for virtReg.
333 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
Evan Chengc781a242009-05-03 18:32:42 +0000334 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
335 RestorePt2VirtMap.find(Pt);
336 if (I != RestorePt2VirtMap.end())
337 I->second.push_back(virtReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000338 else {
339 std::vector<unsigned> Virts;
340 Virts.push_back(virtReg);
341 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
342 }
343 }
344
Evan Cheng676dd7c2008-03-11 07:19:34 +0000345 /// @brief - transfer restore point information from one instruction to
346 /// another.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000347 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
Evan Chengc781a242009-05-03 18:32:42 +0000348 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
Evan Cheng0cbb1162007-11-29 01:06:25 +0000349 RestorePt2VirtMap.find(Old);
350 if (I == RestorePt2VirtMap.end())
351 return;
352 while (!I->second.empty()) {
353 unsigned virtReg = I->second.back();
354 I->second.pop_back();
355 addRestorePoint(virtReg, New);
356 }
357 RestorePt2VirtMap.erase(I);
358 }
359
Evan Cheng676dd7c2008-03-11 07:19:34 +0000360 /// @brief records that the specified physical register must be spilled
361 /// around the specified machine instr.
362 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
363 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
364 EmergencySpillMap[MI].push_back(PhysReg);
365 else {
366 std::vector<unsigned> PhysRegs;
367 PhysRegs.push_back(PhysReg);
368 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
369 }
370 }
371
372 /// @brief returns true if one or more physical registers must be spilled
373 /// around the specified instruction.
374 bool hasEmergencySpills(MachineInstr *MI) const {
375 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
376 }
377
378 /// @brief returns the physical registers to be spilled and restored around
379 /// the instruction.
380 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
381 return EmergencySpillMap[MI];
382 }
383
Evan Chengc1f53c72008-03-11 21:34:46 +0000384 /// @brief - transfer emergency spill information from one instruction to
385 /// another.
386 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
387 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
388 EmergencySpillMap.find(Old);
389 if (I == EmergencySpillMap.end())
390 return;
391 while (!I->second.empty()) {
392 unsigned virtReg = I->second.back();
393 I->second.pop_back();
394 addEmergencySpill(virtReg, New);
395 }
396 EmergencySpillMap.erase(I);
397 }
398
Evan Cheng676dd7c2008-03-11 07:19:34 +0000399 /// @brief return or get a emergency spill slot for the register class.
400 int getEmergencySpillSlot(const TargetRegisterClass *RC);
401
Evan Chengd3653122008-02-27 03:04:06 +0000402 /// @brief Return lowest spill slot index.
403 int getLowSpillSlot() const {
404 return LowSpillSlot;
405 }
406
407 /// @brief Return highest spill slot index.
408 int getHighSpillSlot() const {
409 return HighSpillSlot;
410 }
411
412 /// @brief Records a spill slot use.
413 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
414
415 /// @brief Returns true if spill slot has been used.
416 bool isSpillSlotUsed(int FrameIndex) const {
417 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
418 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
419 }
420
Evan Cheng4cce6b42008-04-11 17:53:36 +0000421 /// @brief Mark the specified register as being implicitly defined.
422 void setIsImplicitlyDefined(unsigned VirtReg) {
423 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
424 }
425
426 /// @brief Returns true if the virtual register is implicitly defined.
427 bool isImplicitlyDefined(unsigned VirtReg) const {
428 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
429 }
430
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000431 /// @brief Updates information about the specified virtual register's value
Evan Chengaee4af62007-12-02 08:30:39 +0000432 /// folded into newMI machine instruction.
433 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
434 ModRef MRInfo);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000435
Evan Cheng7f566252007-10-13 02:50:24 +0000436 /// @brief Updates information about the specified virtual register's value
437 /// folded into the specified machine instruction.
438 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
439
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000440 /// @brief returns the virtual registers' values folded in memory
441 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000442 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000443 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000444 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000445 }
Chris Lattner35f27052006-05-01 21:16:03 +0000446
Evan Chengcada2452007-11-28 01:28:46 +0000447 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
448 /// the folded instruction map and spill point map.
Evan Chengd3653122008-02-27 03:04:06 +0000449 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000450
Evan Chengc781a242009-05-03 18:32:42 +0000451 /// FindUnusedRegisters - Gather a list of allocatable registers that
452 /// have not been allocated to any virtual register.
Evan Cheng90f95f82009-06-14 20:22:55 +0000453 bool FindUnusedRegisters(LiveIntervals* LIs);
Evan Chengc781a242009-05-03 18:32:42 +0000454
455 /// HasUnusedRegisters - Return true if there are any allocatable registers
456 /// that have not been allocated to any virtual register.
457 bool HasUnusedRegisters() const {
458 return !UnusedRegs.none();
459 }
460
461 /// setRegisterUsed - Remember the physical register is now used.
462 void setRegisterUsed(unsigned Reg) {
463 UnusedRegs.reset(Reg);
464 }
465
466 /// isRegisterUnused - Return true if the physical register has not been
467 /// used.
468 bool isRegisterUnused(unsigned Reg) const {
469 return UnusedRegs[Reg];
470 }
471
472 /// getFirstUnusedRegister - Return the first physical register that has not
473 /// been used.
474 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
475 int Reg = UnusedRegs.find_first();
476 while (Reg != -1) {
Mike Stumpfe095f32009-05-04 18:40:41 +0000477 if (allocatableRCRegs[RC][Reg])
Evan Chengc781a242009-05-03 18:32:42 +0000478 return (unsigned)Reg;
479 Reg = UnusedRegs.find_next(Reg);
480 }
481 return 0;
482 }
483
Owen Anderson49c8aa02009-03-13 05:55:11 +0000484 void print(std::ostream &OS, const Module* M = 0) const;
Bill Wendling5c7e3262006-12-17 05:15:13 +0000485 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000486 void dump() const;
487 };
488
Bill Wendling5c7e3262006-12-17 05:15:13 +0000489 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
490 VRM.print(OS);
491 return OS;
492 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000493 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
494 VRM.print(OS);
495 return OS;
496 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000497} // End llvm namespace
498
499#endif