blob: 507557d24c08085625d79273a1a467c23bc54b8e [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;
Chris Lattner29268692006-09-05 02:12:02 +000034 class TargetInstrInfo;
Mike Stumpfe095f32009-05-04 18:40:41 +000035 class TargetRegisterInfo;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000036
Owen Anderson49c8aa02009-03-13 05:55:11 +000037 class VirtRegMap : public MachineFunctionPass {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000038 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000039 enum {
40 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000041 NO_STACK_SLOT = (1L << 30)-1,
42 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000043 };
44
Chris Lattner35f27052006-05-01 21:16:03 +000045 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000046 typedef std::multimap<MachineInstr*,
47 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000048
Chris Lattner8c4d88d2004-09-30 01:54:45 +000049 private:
Owen Anderson49c8aa02009-03-13 05:55:11 +000050 const TargetInstrInfo *TII;
Mike Stumpfe095f32009-05-04 18:40:41 +000051 const TargetRegisterInfo *TRI;
Owen Anderson49c8aa02009-03-13 05:55:11 +000052 MachineFunction *MF;
Mike Stumpfe095f32009-05-04 18:40:41 +000053
54 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
55
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000056 /// Virt2PhysMap - This is a virtual to physical register
57 /// mapping. Each virtual register is required to have an entry in
58 /// it; even spilled virtual registers (the register mapped to a
59 /// spilled register is the temporary used to load it from the
60 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000061 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng81a03822007-11-17 00:40:40 +000062
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000063 /// Virt2StackSlotMap - This is virtual register to stack slot
64 /// mapping. Each spilled virtual register has an entry in it
65 /// which corresponds to the stack slot this register is spilled
66 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000067 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng81a03822007-11-17 00:40:40 +000068
Dan Gohman39e33ac2008-03-12 20:50:04 +000069 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
Evan Cheng81a03822007-11-17 00:40:40 +000070 /// mapping. Each spilled virtual register that should be remat'd has an
71 /// entry in it which corresponds to the remat id.
Evan Cheng549f27d32007-08-13 23:45:17 +000072 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Cheng81a03822007-11-17 00:40:40 +000073
74 /// Virt2SplitMap - This is virtual register to splitted virtual register
75 /// mapping.
76 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
77
Evan Chengadf85902007-12-05 09:51:10 +000078 /// Virt2SplitKillMap - This is splitted virtual register to its last use
Evan Chengd120ffd2007-12-05 10:24:35 +000079 /// (kill) index mapping.
80 IndexedMap<unsigned> Virt2SplitKillMap;
Evan Chengadf85902007-12-05 09:51:10 +000081
Evan Cheng81a03822007-11-17 00:40:40 +000082 /// ReMatMap - This is virtual register to re-materialized instruction
83 /// mapping. Each virtual register whose definition is going to be
84 /// re-materialized has an entry in it.
85 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
86
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000087 /// MI2VirtMap - This is MachineInstr to virtual register
88 /// mapping. In the case of memory spill code being folded into
89 /// instructions, we need to know which virtual register was
90 /// read/written by this instruction.
Chris Lattner7f690e62004-09-30 02:15:18 +000091 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000092
Evan Cheng81a03822007-11-17 00:40:40 +000093 /// SpillPt2VirtMap - This records the virtual registers which should
94 /// be spilled right after the MachineInstr due to live interval
95 /// splitting.
Evan Chengb50bb8c2007-12-05 08:16:32 +000096 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
97 SpillPt2VirtMap;
Evan Cheng2638e1a2007-03-20 08:13:50 +000098
Evan Cheng0cbb1162007-11-29 01:06:25 +000099 /// RestorePt2VirtMap - This records the virtual registers which should
100 /// be restored right before the MachineInstr due to live interval
101 /// splitting.
102 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
103
Evan Cheng676dd7c2008-03-11 07:19:34 +0000104 /// EmergencySpillMap - This records the physical registers that should
105 /// be spilled / restored around the MachineInstr since the register
106 /// allocator has run out of registers.
107 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
108
109 /// EmergencySpillSlots - This records emergency spill slots used to
110 /// spill physical registers when the register allocator runs out of
111 /// registers. Ideally only one stack slot is used per function per
112 /// register class.
113 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
114
Evan Cheng2638e1a2007-03-20 08:13:50 +0000115 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng91935142007-04-04 07:40:01 +0000116 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng2638e1a2007-03-20 08:13:50 +0000117 /// the highest id used so far. Note, this starts at (1<<18) to avoid
118 /// conflicts with stack slot numbers.
119 int ReMatId;
120
Evan Chengd3653122008-02-27 03:04:06 +0000121 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
122 int LowSpillSlot, HighSpillSlot;
123
124 /// SpillSlotToUsesMap - Records uses for each register spill slot.
125 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
126
Evan Cheng4cce6b42008-04-11 17:53:36 +0000127 /// ImplicitDefed - One bit for each virtual register. If set it indicates
128 /// the register is implicitly defined.
129 BitVector ImplicitDefed;
130
Evan Chengc781a242009-05-03 18:32:42 +0000131 /// UnusedRegs - A list of physical registers that have not been used.
132 BitVector UnusedRegs;
133
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000134 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
135 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +0000136
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000137 public:
Owen Anderson49c8aa02009-03-13 05:55:11 +0000138 static char ID;
139 VirtRegMap() : MachineFunctionPass(&ID), Virt2PhysMap(NO_PHYS_REG),
140 Virt2StackSlotMap(NO_STACK_SLOT),
141 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
142 Virt2SplitKillMap(0), ReMatMap(NULL),
143 ReMatId(MAX_STACK_SLOT+1),
144 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
145 virtual bool runOnMachineFunction(MachineFunction &MF);
146
147 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
148 AU.setPreservesAll();
149 MachineFunctionPass::getAnalysisUsage(AU);
150 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000151
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000152 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000153
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000154 /// @brief returns true if the specified virtual register is
155 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000156 bool hasPhys(unsigned virtReg) const {
157 return getPhys(virtReg) != NO_PHYS_REG;
158 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000159
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000160 /// @brief returns the physical register mapped to the specified
161 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000162 unsigned getPhys(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000163 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000164 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000165 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000166
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000167 /// @brief creates a mapping for the specified virtual register to
168 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000169 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000170 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
171 TargetRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000172 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000173 "attempt to assign physical register to already mapped "
174 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000175 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000176 }
177
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000178 /// @brief clears the specified virtual register's, physical
179 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000180 void clearVirt(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000181 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000182 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000183 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000184 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000185 }
186
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000187 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000188 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000189 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000190 grow();
191 }
192
Evan Cheng81a03822007-11-17 00:40:40 +0000193 /// @brief records virtReg is a split live interval from SReg.
194 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
195 Virt2SplitMap[virtReg] = SReg;
196 }
197
198 /// @brief returns the live interval virtReg is split from.
199 unsigned getPreSplitReg(unsigned virtReg) {
200 return Virt2SplitMap[virtReg];
201 }
202
Dan Gohman39e33ac2008-03-12 20:50:04 +0000203 /// @brief returns true if the specified virtual register is not
Evan Cheng549f27d32007-08-13 23:45:17 +0000204 /// mapped to a stack slot or rematerialized.
205 bool isAssignedReg(unsigned virtReg) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000206 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
207 getReMatId(virtReg) == NO_STACK_SLOT)
208 return true;
209 // Split register can be assigned a physical register as well as a
210 // stack slot or remat id.
211 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000212 }
213
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000214 /// @brief returns the stack slot mapped to the specified virtual
215 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000216 int getStackSlot(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000217 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000218 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000219 }
220
Evan Cheng549f27d32007-08-13 23:45:17 +0000221 /// @brief returns the rematerialization id mapped to the specified virtual
222 /// register
223 int getReMatId(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000224 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000225 return Virt2ReMatIdMap[virtReg];
226 }
227
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000228 /// @brief create a mapping for the specifed virtual register to
229 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000230 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000231 /// @brief create a mapping for the specified virtual register to
232 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000233 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
234
Evan Cheng2638e1a2007-03-20 08:13:50 +0000235 /// @brief assign an unique re-materialization id to the specified
236 /// virtual register.
237 int assignVirtReMatId(unsigned virtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000238 /// @brief assign an unique re-materialization id to the specified
239 /// virtual register.
240 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng2638e1a2007-03-20 08:13:50 +0000241
242 /// @brief returns true if the specified virtual register is being
243 /// re-materialized.
244 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng549f27d32007-08-13 23:45:17 +0000245 return ReMatMap[virtReg] != NULL;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000246 }
247
248 /// @brief returns the original machine instruction being re-issued
249 /// to re-materialize the specified virtual register.
Evan Cheng549f27d32007-08-13 23:45:17 +0000250 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng2638e1a2007-03-20 08:13:50 +0000251 return ReMatMap[virtReg];
252 }
253
254 /// @brief records the specified virtual register will be
255 /// re-materialized and the original instruction which will be re-issed
Evan Cheng549f27d32007-08-13 23:45:17 +0000256 /// for this purpose. If parameter all is true, then all uses of the
257 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng2638e1a2007-03-20 08:13:50 +0000258 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
259 ReMatMap[virtReg] = def;
260 }
261
Evan Chengadf85902007-12-05 09:51:10 +0000262 /// @brief record the last use (kill) of a split virtual register.
Evan Chengd120ffd2007-12-05 10:24:35 +0000263 void addKillPoint(unsigned virtReg, unsigned index) {
264 Virt2SplitKillMap[virtReg] = index;
Evan Chengadf85902007-12-05 09:51:10 +0000265 }
266
Evan Chengd120ffd2007-12-05 10:24:35 +0000267 unsigned getKillPoint(unsigned virtReg) const {
268 return Virt2SplitKillMap[virtReg];
269 }
270
271 /// @brief remove the last use (kill) of a split virtual register.
Evan Chengadf85902007-12-05 09:51:10 +0000272 void removeKillPoint(unsigned virtReg) {
Evan Chengd120ffd2007-12-05 10:24:35 +0000273 Virt2SplitKillMap[virtReg] = 0;
Evan Chengadf85902007-12-05 09:51:10 +0000274 }
275
Evan Chengcada2452007-11-28 01:28:46 +0000276 /// @brief returns true if the specified MachineInstr is a spill point.
277 bool isSpillPt(MachineInstr *Pt) const {
278 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
279 }
280
Evan Cheng81a03822007-11-17 00:40:40 +0000281 /// @brief returns the virtual registers that should be spilled due to
282 /// splitting right after the specified MachineInstr.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000283 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Cheng81a03822007-11-17 00:40:40 +0000284 return SpillPt2VirtMap[Pt];
285 }
286
287 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000288 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Chengc781a242009-05-03 18:32:42 +0000289 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
290 I = SpillPt2VirtMap.find(Pt);
291 if (I != SpillPt2VirtMap.end())
292 I->second.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000293 else {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000294 std::vector<std::pair<unsigned,bool> > Virts;
295 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000296 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
297 }
Evan Cheng81a03822007-11-17 00:40:40 +0000298 }
299
Evan Chengc1f53c72008-03-11 21:34:46 +0000300 /// @brief - transfer spill point information from one instruction to
301 /// another.
Evan Cheng81a03822007-11-17 00:40:40 +0000302 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Chengc781a242009-05-03 18:32:42 +0000303 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
Evan Chengb50bb8c2007-12-05 08:16:32 +0000304 I = SpillPt2VirtMap.find(Old);
Evan Chengcada2452007-11-28 01:28:46 +0000305 if (I == SpillPt2VirtMap.end())
306 return;
307 while (!I->second.empty()) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000308 unsigned virtReg = I->second.back().first;
309 bool isKill = I->second.back().second;
Evan Chengcada2452007-11-28 01:28:46 +0000310 I->second.pop_back();
Evan Chengb50bb8c2007-12-05 08:16:32 +0000311 addSpillPoint(virtReg, isKill, New);
Evan Cheng81a03822007-11-17 00:40:40 +0000312 }
Evan Chengcada2452007-11-28 01:28:46 +0000313 SpillPt2VirtMap.erase(I);
Evan Cheng81a03822007-11-17 00:40:40 +0000314 }
315
Evan Cheng0cbb1162007-11-29 01:06:25 +0000316 /// @brief returns true if the specified MachineInstr is a restore point.
317 bool isRestorePt(MachineInstr *Pt) const {
318 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
319 }
320
321 /// @brief returns the virtual registers that should be restoreed due to
322 /// splitting right after the specified MachineInstr.
323 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
324 return RestorePt2VirtMap[Pt];
325 }
326
327 /// @brief records the specified MachineInstr as a restore point for virtReg.
328 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
Evan Chengc781a242009-05-03 18:32:42 +0000329 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
330 RestorePt2VirtMap.find(Pt);
331 if (I != RestorePt2VirtMap.end())
332 I->second.push_back(virtReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000333 else {
334 std::vector<unsigned> Virts;
335 Virts.push_back(virtReg);
336 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
337 }
338 }
339
Evan Cheng676dd7c2008-03-11 07:19:34 +0000340 /// @brief - transfer restore point information from one instruction to
341 /// another.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000342 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
Evan Chengc781a242009-05-03 18:32:42 +0000343 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
Evan Cheng0cbb1162007-11-29 01:06:25 +0000344 RestorePt2VirtMap.find(Old);
345 if (I == RestorePt2VirtMap.end())
346 return;
347 while (!I->second.empty()) {
348 unsigned virtReg = I->second.back();
349 I->second.pop_back();
350 addRestorePoint(virtReg, New);
351 }
352 RestorePt2VirtMap.erase(I);
353 }
354
Evan Cheng676dd7c2008-03-11 07:19:34 +0000355 /// @brief records that the specified physical register must be spilled
356 /// around the specified machine instr.
357 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
358 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
359 EmergencySpillMap[MI].push_back(PhysReg);
360 else {
361 std::vector<unsigned> PhysRegs;
362 PhysRegs.push_back(PhysReg);
363 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
364 }
365 }
366
367 /// @brief returns true if one or more physical registers must be spilled
368 /// around the specified instruction.
369 bool hasEmergencySpills(MachineInstr *MI) const {
370 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
371 }
372
373 /// @brief returns the physical registers to be spilled and restored around
374 /// the instruction.
375 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
376 return EmergencySpillMap[MI];
377 }
378
Evan Chengc1f53c72008-03-11 21:34:46 +0000379 /// @brief - transfer emergency spill information from one instruction to
380 /// another.
381 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
382 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
383 EmergencySpillMap.find(Old);
384 if (I == EmergencySpillMap.end())
385 return;
386 while (!I->second.empty()) {
387 unsigned virtReg = I->second.back();
388 I->second.pop_back();
389 addEmergencySpill(virtReg, New);
390 }
391 EmergencySpillMap.erase(I);
392 }
393
Evan Cheng676dd7c2008-03-11 07:19:34 +0000394 /// @brief return or get a emergency spill slot for the register class.
395 int getEmergencySpillSlot(const TargetRegisterClass *RC);
396
Evan Chengd3653122008-02-27 03:04:06 +0000397 /// @brief Return lowest spill slot index.
398 int getLowSpillSlot() const {
399 return LowSpillSlot;
400 }
401
402 /// @brief Return highest spill slot index.
403 int getHighSpillSlot() const {
404 return HighSpillSlot;
405 }
406
407 /// @brief Records a spill slot use.
408 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
409
410 /// @brief Returns true if spill slot has been used.
411 bool isSpillSlotUsed(int FrameIndex) const {
412 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
413 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
414 }
415
Evan Cheng4cce6b42008-04-11 17:53:36 +0000416 /// @brief Mark the specified register as being implicitly defined.
417 void setIsImplicitlyDefined(unsigned VirtReg) {
418 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
419 }
420
421 /// @brief Returns true if the virtual register is implicitly defined.
422 bool isImplicitlyDefined(unsigned VirtReg) const {
423 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
424 }
425
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000426 /// @brief Updates information about the specified virtual register's value
Evan Chengaee4af62007-12-02 08:30:39 +0000427 /// folded into newMI machine instruction.
428 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
429 ModRef MRInfo);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000430
Evan Cheng7f566252007-10-13 02:50:24 +0000431 /// @brief Updates information about the specified virtual register's value
432 /// folded into the specified machine instruction.
433 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
434
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000435 /// @brief returns the virtual registers' values folded in memory
436 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000437 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000438 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000439 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000440 }
Chris Lattner35f27052006-05-01 21:16:03 +0000441
Evan Chengcada2452007-11-28 01:28:46 +0000442 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
443 /// the folded instruction map and spill point map.
Evan Chengd3653122008-02-27 03:04:06 +0000444 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000445
Evan Chengc781a242009-05-03 18:32:42 +0000446 /// FindUnusedRegisters - Gather a list of allocatable registers that
447 /// have not been allocated to any virtual register.
448 bool FindUnusedRegisters(const TargetRegisterInfo *TRI,
449 LiveIntervals* LIs);
450
451 /// HasUnusedRegisters - Return true if there are any allocatable registers
452 /// that have not been allocated to any virtual register.
453 bool HasUnusedRegisters() const {
454 return !UnusedRegs.none();
455 }
456
457 /// setRegisterUsed - Remember the physical register is now used.
458 void setRegisterUsed(unsigned Reg) {
459 UnusedRegs.reset(Reg);
460 }
461
462 /// isRegisterUnused - Return true if the physical register has not been
463 /// used.
464 bool isRegisterUnused(unsigned Reg) const {
465 return UnusedRegs[Reg];
466 }
467
468 /// getFirstUnusedRegister - Return the first physical register that has not
469 /// been used.
470 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
471 int Reg = UnusedRegs.find_first();
472 while (Reg != -1) {
Mike Stumpfe095f32009-05-04 18:40:41 +0000473 if (allocatableRCRegs[RC][Reg])
Evan Chengc781a242009-05-03 18:32:42 +0000474 return (unsigned)Reg;
475 Reg = UnusedRegs.find_next(Reg);
476 }
477 return 0;
478 }
479
Owen Anderson49c8aa02009-03-13 05:55:11 +0000480 void print(std::ostream &OS, const Module* M = 0) const;
Bill Wendling5c7e3262006-12-17 05:15:13 +0000481 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000482 void dump() const;
483 };
484
Bill Wendling5c7e3262006-12-17 05:15:13 +0000485 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
486 VRM.print(OS);
487 return OS;
488 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000489 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
490 VRM.print(OS);
491 return OS;
492 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000493} // End llvm namespace
494
495#endif