blob: ba50f4e42302d473fb3cfb96427405d64db58fc6 [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//
Chris Lattnerf3ebc3f2007-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 Evlogimenosc794a902004-02-23 23:08:11 +00007//
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
Owen Andersond37ddf52009-03-13 05:55:11 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hames3fffe622009-09-04 20:41:11 +000021#include "llvm/CodeGen/LiveInterval.h"
Dan Gohman3a4be0f2008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng499ffa92008-04-11 17:53:36 +000023#include "llvm/ADT/BitVector.h"
Evan Cheng210fc622009-05-03 18:32:42 +000024#include "llvm/ADT/DenseMap.h"
Chris Lattner1003dc72007-02-01 05:32:05 +000025#include "llvm/ADT/IndexedMap.h"
Evan Cheng6d563682008-02-27 03:04:06 +000026#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman906152a2009-01-05 17:59:02 +000027#include "llvm/ADT/SmallVector.h"
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000028#include <map>
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000029
30namespace llvm {
Evan Cheng210fc622009-05-03 18:32:42 +000031 class LiveIntervals;
Chris Lattnere2b77d52004-09-30 01:54:45 +000032 class MachineInstr;
David Greene99905f12007-08-07 16:34:05 +000033 class MachineFunction;
Evan Cheng085caf12009-06-14 20:22:55 +000034 class MachineRegisterInfo;
Chris Lattner13a5dcd2006-09-05 02:12:02 +000035 class TargetInstrInfo;
Evan Cheng3f778052009-05-04 03:30:11 +000036 class TargetRegisterInfo;
Daniel Dunbar796e43e2009-07-24 10:36:58 +000037 class raw_ostream;
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +000038 class SlotIndexes;
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000039
Owen Andersond37ddf52009-03-13 05:55:11 +000040 class VirtRegMap : public MachineFunctionPass {
Chris Lattnere2b77d52004-09-30 01:54:45 +000041 public:
Evan Cheng0e3278e2007-03-20 08:13:50 +000042 enum {
43 NO_PHYS_REG = 0,
Evan Cheng8be98c12007-04-04 07:40:01 +000044 NO_STACK_SLOT = (1L << 30)-1,
45 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng0e3278e2007-03-20 08:13:50 +000046 };
47
Chris Lattner4dee67c2006-05-01 21:16:03 +000048 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattner1905ae62004-10-01 23:15:36 +000049 typedef std::multimap<MachineInstr*,
50 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000051
Chris Lattnere2b77d52004-09-30 01:54:45 +000052 private:
Evan Cheng085caf12009-06-14 20:22:55 +000053 MachineRegisterInfo *MRI;
Owen Andersond37ddf52009-03-13 05:55:11 +000054 const TargetInstrInfo *TII;
Evan Cheng3f778052009-05-04 03:30:11 +000055 const TargetRegisterInfo *TRI;
Owen Andersond37ddf52009-03-13 05:55:11 +000056 MachineFunction *MF;
Evan Cheng3f778052009-05-04 03:30:11 +000057
58 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
59
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000060 /// Virt2PhysMap - This is a virtual to physical register
61 /// mapping. Each virtual register is required to have an entry in
62 /// it; even spilled virtual registers (the register mapped to a
63 /// spilled register is the temporary used to load it from the
64 /// stack).
Chris Lattner1003dc72007-02-01 05:32:05 +000065 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng8e223792007-11-17 00:40:40 +000066
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000067 /// Virt2StackSlotMap - This is virtual register to stack slot
68 /// mapping. Each spilled virtual register has an entry in it
69 /// which corresponds to the stack slot this register is spilled
70 /// at.
Chris Lattner1003dc72007-02-01 05:32:05 +000071 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng8e223792007-11-17 00:40:40 +000072
Dan Gohmanbf68f9f2008-03-12 20:50:04 +000073 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
Evan Cheng8e223792007-11-17 00:40:40 +000074 /// mapping. Each spilled virtual register that should be remat'd has an
75 /// entry in it which corresponds to the remat id.
Evan Cheng33820da2007-08-13 23:45:17 +000076 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Cheng8e223792007-11-17 00:40:40 +000077
78 /// Virt2SplitMap - This is virtual register to splitted virtual register
79 /// mapping.
80 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
81
Evan Cheng06353b42007-12-05 09:51:10 +000082 /// Virt2SplitKillMap - This is splitted virtual register to its last use
Evan Cheng678b86d2007-12-05 10:24:35 +000083 /// (kill) index mapping.
Jakob Stoklund Olesend65524d2011-01-09 18:58:33 +000084 IndexedMap<SlotIndex, VirtReg2IndexFunctor> Virt2SplitKillMap;
Evan Cheng06353b42007-12-05 09:51:10 +000085
Evan Cheng8e223792007-11-17 00:40:40 +000086 /// ReMatMap - This is virtual register to re-materialized instruction
87 /// mapping. Each virtual register whose definition is going to be
88 /// re-materialized has an entry in it.
89 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
90
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000091 /// MI2VirtMap - This is MachineInstr to virtual register
92 /// mapping. In the case of memory spill code being folded into
93 /// instructions, we need to know which virtual register was
94 /// read/written by this instruction.
Chris Lattner39fef8d2004-09-30 02:15:18 +000095 MI2VirtMapTy MI2VirtMap;
Misha Brukman835702a2005-04-21 22:36:52 +000096
Evan Cheng8e223792007-11-17 00:40:40 +000097 /// SpillPt2VirtMap - This records the virtual registers which should
98 /// be spilled right after the MachineInstr due to live interval
99 /// splitting.
Evan Chengd7de56a2007-12-05 08:16:32 +0000100 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
101 SpillPt2VirtMap;
Evan Cheng0e3278e2007-03-20 08:13:50 +0000102
Evan Chengbe255b02007-11-29 01:06:25 +0000103 /// RestorePt2VirtMap - This records the virtual registers which should
104 /// be restored right before the MachineInstr due to live interval
105 /// splitting.
106 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
107
Evan Chenge88a6252008-03-11 07:19:34 +0000108 /// EmergencySpillMap - This records the physical registers that should
109 /// be spilled / restored around the MachineInstr since the register
110 /// allocator has run out of registers.
111 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
112
113 /// EmergencySpillSlots - This records emergency spill slots used to
114 /// spill physical registers when the register allocator runs out of
115 /// registers. Ideally only one stack slot is used per function per
116 /// register class.
117 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
118
Evan Cheng0e3278e2007-03-20 08:13:50 +0000119 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng8be98c12007-04-04 07:40:01 +0000120 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng0e3278e2007-03-20 08:13:50 +0000121 /// the highest id used so far. Note, this starts at (1<<18) to avoid
122 /// conflicts with stack slot numbers.
123 int ReMatId;
124
Evan Cheng6d563682008-02-27 03:04:06 +0000125 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
126 int LowSpillSlot, HighSpillSlot;
127
128 /// SpillSlotToUsesMap - Records uses for each register spill slot.
129 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
130
Evan Cheng499ffa92008-04-11 17:53:36 +0000131 /// ImplicitDefed - One bit for each virtual register. If set it indicates
132 /// the register is implicitly defined.
133 BitVector ImplicitDefed;
134
Evan Cheng210fc622009-05-03 18:32:42 +0000135 /// UnusedRegs - A list of physical registers that have not been used.
136 BitVector UnusedRegs;
137
Jakob Stoklund Olesen39aed732010-11-16 00:41:01 +0000138 /// createSpillSlot - Allocate a spill slot for RC from MFI.
139 unsigned createSpillSlot(const TargetRegisterClass *RC);
140
Chris Lattnere2b77d52004-09-30 01:54:45 +0000141 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
142 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenosab77b052004-02-23 23:47:10 +0000143
Chris Lattnere2b77d52004-09-30 01:54:45 +0000144 public:
Owen Andersond37ddf52009-03-13 05:55:11 +0000145 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +0000146 VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
Owen Andersond37ddf52009-03-13 05:55:11 +0000147 Virt2StackSlotMap(NO_STACK_SLOT),
148 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
Lang Hames05fb9632009-11-03 23:52:08 +0000149 Virt2SplitKillMap(SlotIndex()), ReMatMap(NULL),
Owen Andersond37ddf52009-03-13 05:55:11 +0000150 ReMatId(MAX_STACK_SLOT+1),
151 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
152 virtual bool runOnMachineFunction(MachineFunction &MF);
153
154 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
155 AU.setPreservesAll();
156 MachineFunctionPass::getAnalysisUsage(AU);
157 }
Alkis Evlogimenosc794a902004-02-23 23:08:11 +0000158
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000159 MachineFunction &getMachineFunction() const {
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +0000160 assert(MF && "getMachineFunction called before runOnMachineFunction");
Jakob Stoklund Olesenc6984172010-07-26 23:44:11 +0000161 return *MF;
162 }
163
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +0000164 MachineRegisterInfo &getRegInfo() const { return *MRI; }
165 const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
166
Chris Lattnere2b77d52004-09-30 01:54:45 +0000167 void grow();
Alkis Evlogimenos31953c72004-03-01 23:18:15 +0000168
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000169 /// @brief returns true if the specified virtual register is
170 /// mapped to a physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000171 bool hasPhys(unsigned virtReg) const {
172 return getPhys(virtReg) != NO_PHYS_REG;
173 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +0000174
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000175 /// @brief returns the physical register mapped to the specified
176 /// virtual register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000177 unsigned getPhys(unsigned virtReg) const {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000178 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000179 return Virt2PhysMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +0000180 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +0000181
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000182 /// @brief creates a mapping for the specified virtual register to
183 /// the specified physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000184 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000185 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
186 TargetRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000187 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000188 "attempt to assign physical register to already mapped "
189 "virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +0000190 Virt2PhysMap[virtReg] = physReg;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000191 }
192
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000193 /// @brief clears the specified virtual register's, physical
194 /// register mapping
Chris Lattnere2b77d52004-09-30 01:54:45 +0000195 void clearVirt(unsigned virtReg) {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000196 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000197 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000198 "attempt to clear a not assigned virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +0000199 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000200 }
201
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000202 /// @brief clears all virtual to physical register mappings
Chris Lattnere2b77d52004-09-30 01:54:45 +0000203 void clearAllVirt() {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000204 Virt2PhysMap.clear();
Chris Lattnere2b77d52004-09-30 01:54:45 +0000205 grow();
206 }
207
Evan Cheng085caf12009-06-14 20:22:55 +0000208 /// @brief returns the register allocation preference.
209 unsigned getRegAllocPref(unsigned virtReg);
210
Evan Cheng8e223792007-11-17 00:40:40 +0000211 /// @brief records virtReg is a split live interval from SReg.
212 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
213 Virt2SplitMap[virtReg] = SReg;
214 }
215
216 /// @brief returns the live interval virtReg is split from.
Jakob Stoklund Olesen4376d672011-02-18 22:35:20 +0000217 unsigned getPreSplitReg(unsigned virtReg) const {
Evan Cheng8e223792007-11-17 00:40:40 +0000218 return Virt2SplitMap[virtReg];
219 }
220
Jakob Stoklund Olesen13eb3652011-02-19 00:38:43 +0000221 /// getOriginal - Return the original virtual register that VirtReg descends
222 /// from through splitting.
223 /// A register that was not created by splitting is its own original.
224 /// This operation is idempotent.
225 unsigned getOriginal(unsigned VirtReg) const {
226 unsigned Orig = getPreSplitReg(VirtReg);
227 return Orig ? Orig : VirtReg;
228 }
229
Dan Gohmanbf68f9f2008-03-12 20:50:04 +0000230 /// @brief returns true if the specified virtual register is not
Evan Cheng33820da2007-08-13 23:45:17 +0000231 /// mapped to a stack slot or rematerialized.
232 bool isAssignedReg(unsigned virtReg) const {
Evan Cheng8e223792007-11-17 00:40:40 +0000233 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
234 getReMatId(virtReg) == NO_STACK_SLOT)
235 return true;
236 // Split register can be assigned a physical register as well as a
237 // stack slot or remat id.
238 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000239 }
240
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000241 /// @brief returns the stack slot mapped to the specified virtual
242 /// register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000243 int getStackSlot(unsigned virtReg) const {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000244 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000245 return Virt2StackSlotMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +0000246 }
247
Evan Cheng33820da2007-08-13 23:45:17 +0000248 /// @brief returns the rematerialization id mapped to the specified virtual
249 /// register
250 int getReMatId(unsigned virtReg) const {
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000251 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng33820da2007-08-13 23:45:17 +0000252 return Virt2ReMatIdMap[virtReg];
253 }
254
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000255 /// @brief create a mapping for the specifed virtual register to
256 /// the next available stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000257 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000258 /// @brief create a mapping for the specified virtual register to
259 /// the specified stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000260 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
261
Evan Cheng0e3278e2007-03-20 08:13:50 +0000262 /// @brief assign an unique re-materialization id to the specified
263 /// virtual register.
264 int assignVirtReMatId(unsigned virtReg);
Evan Cheng33820da2007-08-13 23:45:17 +0000265 /// @brief assign an unique re-materialization id to the specified
266 /// virtual register.
267 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng0e3278e2007-03-20 08:13:50 +0000268
269 /// @brief returns true if the specified virtual register is being
270 /// re-materialized.
271 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng33820da2007-08-13 23:45:17 +0000272 return ReMatMap[virtReg] != NULL;
Evan Cheng0e3278e2007-03-20 08:13:50 +0000273 }
274
275 /// @brief returns the original machine instruction being re-issued
276 /// to re-materialize the specified virtual register.
Evan Cheng33820da2007-08-13 23:45:17 +0000277 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng0e3278e2007-03-20 08:13:50 +0000278 return ReMatMap[virtReg];
279 }
280
281 /// @brief records the specified virtual register will be
282 /// re-materialized and the original instruction which will be re-issed
Evan Cheng33820da2007-08-13 23:45:17 +0000283 /// for this purpose. If parameter all is true, then all uses of the
284 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng0e3278e2007-03-20 08:13:50 +0000285 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
286 ReMatMap[virtReg] = def;
287 }
288
Evan Cheng06353b42007-12-05 09:51:10 +0000289 /// @brief record the last use (kill) of a split virtual register.
Lang Hames05fb9632009-11-03 23:52:08 +0000290 void addKillPoint(unsigned virtReg, SlotIndex index) {
Evan Cheng678b86d2007-12-05 10:24:35 +0000291 Virt2SplitKillMap[virtReg] = index;
Evan Cheng06353b42007-12-05 09:51:10 +0000292 }
293
Lang Hames05fb9632009-11-03 23:52:08 +0000294 SlotIndex getKillPoint(unsigned virtReg) const {
Evan Cheng678b86d2007-12-05 10:24:35 +0000295 return Virt2SplitKillMap[virtReg];
296 }
297
298 /// @brief remove the last use (kill) of a split virtual register.
Evan Cheng06353b42007-12-05 09:51:10 +0000299 void removeKillPoint(unsigned virtReg) {
Lang Hames05fb9632009-11-03 23:52:08 +0000300 Virt2SplitKillMap[virtReg] = SlotIndex();
Evan Cheng06353b42007-12-05 09:51:10 +0000301 }
302
Evan Chengc1648b62007-11-28 01:28:46 +0000303 /// @brief returns true if the specified MachineInstr is a spill point.
304 bool isSpillPt(MachineInstr *Pt) const {
305 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
306 }
307
Evan Cheng8e223792007-11-17 00:40:40 +0000308 /// @brief returns the virtual registers that should be spilled due to
309 /// splitting right after the specified MachineInstr.
Evan Chengd7de56a2007-12-05 08:16:32 +0000310 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Cheng8e223792007-11-17 00:40:40 +0000311 return SpillPt2VirtMap[Pt];
312 }
313
314 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chengd7de56a2007-12-05 08:16:32 +0000315 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Cheng210fc622009-05-03 18:32:42 +0000316 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
317 I = SpillPt2VirtMap.find(Pt);
318 if (I != SpillPt2VirtMap.end())
319 I->second.push_back(std::make_pair(virtReg, isKill));
Evan Chengc1648b62007-11-28 01:28:46 +0000320 else {
Evan Chengd7de56a2007-12-05 08:16:32 +0000321 std::vector<std::pair<unsigned,bool> > Virts;
322 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Chengc1648b62007-11-28 01:28:46 +0000323 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
324 }
Evan Cheng8e223792007-11-17 00:40:40 +0000325 }
326
Evan Chenga3891362008-03-11 21:34:46 +0000327 /// @brief - transfer spill point information from one instruction to
328 /// another.
Evan Cheng8e223792007-11-17 00:40:40 +0000329 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Cheng210fc622009-05-03 18:32:42 +0000330 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
Evan Chengd7de56a2007-12-05 08:16:32 +0000331 I = SpillPt2VirtMap.find(Old);
Evan Chengc1648b62007-11-28 01:28:46 +0000332 if (I == SpillPt2VirtMap.end())
333 return;
334 while (!I->second.empty()) {
Evan Chengd7de56a2007-12-05 08:16:32 +0000335 unsigned virtReg = I->second.back().first;
336 bool isKill = I->second.back().second;
Evan Chengc1648b62007-11-28 01:28:46 +0000337 I->second.pop_back();
Evan Chengd7de56a2007-12-05 08:16:32 +0000338 addSpillPoint(virtReg, isKill, New);
Evan Cheng8e223792007-11-17 00:40:40 +0000339 }
Evan Chengc1648b62007-11-28 01:28:46 +0000340 SpillPt2VirtMap.erase(I);
Evan Cheng8e223792007-11-17 00:40:40 +0000341 }
342
Evan Chengbe255b02007-11-29 01:06:25 +0000343 /// @brief returns true if the specified MachineInstr is a restore point.
344 bool isRestorePt(MachineInstr *Pt) const {
345 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
346 }
347
348 /// @brief returns the virtual registers that should be restoreed due to
349 /// splitting right after the specified MachineInstr.
350 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
351 return RestorePt2VirtMap[Pt];
352 }
353
354 /// @brief records the specified MachineInstr as a restore point for virtReg.
355 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
Evan Cheng210fc622009-05-03 18:32:42 +0000356 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
357 RestorePt2VirtMap.find(Pt);
358 if (I != RestorePt2VirtMap.end())
359 I->second.push_back(virtReg);
Evan Chengbe255b02007-11-29 01:06:25 +0000360 else {
361 std::vector<unsigned> Virts;
362 Virts.push_back(virtReg);
363 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
364 }
365 }
366
Evan Chenge88a6252008-03-11 07:19:34 +0000367 /// @brief - transfer restore point information from one instruction to
368 /// another.
Evan Chengbe255b02007-11-29 01:06:25 +0000369 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
Evan Cheng210fc622009-05-03 18:32:42 +0000370 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
Evan Chengbe255b02007-11-29 01:06:25 +0000371 RestorePt2VirtMap.find(Old);
372 if (I == RestorePt2VirtMap.end())
373 return;
374 while (!I->second.empty()) {
375 unsigned virtReg = I->second.back();
376 I->second.pop_back();
377 addRestorePoint(virtReg, New);
378 }
379 RestorePt2VirtMap.erase(I);
380 }
381
Evan Chenge88a6252008-03-11 07:19:34 +0000382 /// @brief records that the specified physical register must be spilled
383 /// around the specified machine instr.
384 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
385 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
386 EmergencySpillMap[MI].push_back(PhysReg);
387 else {
388 std::vector<unsigned> PhysRegs;
389 PhysRegs.push_back(PhysReg);
390 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
391 }
392 }
393
394 /// @brief returns true if one or more physical registers must be spilled
395 /// around the specified instruction.
396 bool hasEmergencySpills(MachineInstr *MI) const {
397 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
398 }
399
400 /// @brief returns the physical registers to be spilled and restored around
401 /// the instruction.
402 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
403 return EmergencySpillMap[MI];
404 }
405
Evan Chenga3891362008-03-11 21:34:46 +0000406 /// @brief - transfer emergency spill information from one instruction to
407 /// another.
408 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
409 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
410 EmergencySpillMap.find(Old);
411 if (I == EmergencySpillMap.end())
412 return;
413 while (!I->second.empty()) {
414 unsigned virtReg = I->second.back();
415 I->second.pop_back();
416 addEmergencySpill(virtReg, New);
417 }
418 EmergencySpillMap.erase(I);
419 }
420
Evan Chenge88a6252008-03-11 07:19:34 +0000421 /// @brief return or get a emergency spill slot for the register class.
422 int getEmergencySpillSlot(const TargetRegisterClass *RC);
423
Evan Cheng6d563682008-02-27 03:04:06 +0000424 /// @brief Return lowest spill slot index.
425 int getLowSpillSlot() const {
426 return LowSpillSlot;
427 }
428
429 /// @brief Return highest spill slot index.
430 int getHighSpillSlot() const {
431 return HighSpillSlot;
432 }
433
434 /// @brief Records a spill slot use.
435 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
436
437 /// @brief Returns true if spill slot has been used.
438 bool isSpillSlotUsed(int FrameIndex) const {
439 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
440 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
441 }
442
Evan Cheng499ffa92008-04-11 17:53:36 +0000443 /// @brief Mark the specified register as being implicitly defined.
444 void setIsImplicitlyDefined(unsigned VirtReg) {
Jakob Stoklund Olesencf4d5ce2011-01-08 23:11:07 +0000445 ImplicitDefed.set(TargetRegisterInfo::virtReg2Index(VirtReg));
Evan Cheng499ffa92008-04-11 17:53:36 +0000446 }
447
448 /// @brief Returns true if the virtual register is implicitly defined.
449 bool isImplicitlyDefined(unsigned VirtReg) const {
Jakob Stoklund Olesencf4d5ce2011-01-08 23:11:07 +0000450 return ImplicitDefed[TargetRegisterInfo::virtReg2Index(VirtReg)];
Evan Cheng499ffa92008-04-11 17:53:36 +0000451 }
452
Chris Lattner1905ae62004-10-01 23:15:36 +0000453 /// @brief Updates information about the specified virtual register's value
Evan Chengf45a1d62007-12-02 08:30:39 +0000454 /// folded into newMI machine instruction.
455 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
456 ModRef MRInfo);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000457
Evan Chengb6307652007-10-13 02:50:24 +0000458 /// @brief Updates information about the specified virtual register's value
459 /// folded into the specified machine instruction.
460 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
461
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000462 /// @brief returns the virtual registers' values folded in memory
463 /// operands of this instruction
Chris Lattner39fef8d2004-09-30 02:15:18 +0000464 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattnere2b77d52004-09-30 01:54:45 +0000465 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000466 return MI2VirtMap.equal_range(MI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000467 }
Chris Lattner4dee67c2006-05-01 21:16:03 +0000468
Evan Chengc1648b62007-11-28 01:28:46 +0000469 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
470 /// the folded instruction map and spill point map.
Evan Cheng6d563682008-02-27 03:04:06 +0000471 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000472
Evan Cheng210fc622009-05-03 18:32:42 +0000473 /// FindUnusedRegisters - Gather a list of allocatable registers that
474 /// have not been allocated to any virtual register.
Evan Cheng085caf12009-06-14 20:22:55 +0000475 bool FindUnusedRegisters(LiveIntervals* LIs);
Evan Cheng210fc622009-05-03 18:32:42 +0000476
477 /// HasUnusedRegisters - Return true if there are any allocatable registers
478 /// that have not been allocated to any virtual register.
479 bool HasUnusedRegisters() const {
480 return !UnusedRegs.none();
481 }
482
483 /// setRegisterUsed - Remember the physical register is now used.
484 void setRegisterUsed(unsigned Reg) {
485 UnusedRegs.reset(Reg);
486 }
487
488 /// isRegisterUnused - Return true if the physical register has not been
489 /// used.
490 bool isRegisterUnused(unsigned Reg) const {
491 return UnusedRegs[Reg];
492 }
493
494 /// getFirstUnusedRegister - Return the first physical register that has not
495 /// been used.
496 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
497 int Reg = UnusedRegs.find_first();
498 while (Reg != -1) {
Evan Cheng3f778052009-05-04 03:30:11 +0000499 if (allocatableRCRegs[RC][Reg])
Evan Cheng210fc622009-05-03 18:32:42 +0000500 return (unsigned)Reg;
501 Reg = UnusedRegs.find_next(Reg);
502 }
503 return 0;
504 }
505
Jakob Stoklund Olesen5bfec692011-02-18 22:03:18 +0000506 /// rewrite - Rewrite all instructions in MF to use only physical registers
507 /// by mapping all virtual register operands to their assigned physical
508 /// registers.
509 ///
510 /// @param Indexes Optionally remove deleted instructions from indexes.
511 void rewrite(SlotIndexes *Indexes);
512
Daniel Dunbar796e43e2009-07-24 10:36:58 +0000513 void print(raw_ostream &OS, const Module* M = 0) const;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000514 void dump() const;
515 };
516
Daniel Dunbar796e43e2009-07-24 10:36:58 +0000517 inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
518 VRM.print(OS);
519 return OS;
520 }
Alkis Evlogimenosc794a902004-02-23 23:08:11 +0000521} // End llvm namespace
522
523#endif