blob: 91c8322a75a9021fdad81bc6ad42f8ff395f02ab [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;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000035
Owen Anderson49c8aa02009-03-13 05:55:11 +000036 class VirtRegMap : public MachineFunctionPass {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000037 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000038 enum {
39 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000040 NO_STACK_SLOT = (1L << 30)-1,
41 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000042 };
43
Chris Lattner35f27052006-05-01 21:16:03 +000044 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000045 typedef std::multimap<MachineInstr*,
46 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000047
Chris Lattner8c4d88d2004-09-30 01:54:45 +000048 private:
Owen Anderson49c8aa02009-03-13 05:55:11 +000049 const TargetInstrInfo *TII;
Chris Lattner29268692006-09-05 02:12:02 +000050
Owen Anderson49c8aa02009-03-13 05:55:11 +000051 MachineFunction *MF;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000052 /// Virt2PhysMap - This is a virtual to physical register
53 /// mapping. Each virtual register is required to have an entry in
54 /// it; even spilled virtual registers (the register mapped to a
55 /// spilled register is the temporary used to load it from the
56 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000057 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng81a03822007-11-17 00:40:40 +000058
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000059 /// Virt2StackSlotMap - This is virtual register to stack slot
60 /// mapping. Each spilled virtual register has an entry in it
61 /// which corresponds to the stack slot this register is spilled
62 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000063 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng81a03822007-11-17 00:40:40 +000064
Dan Gohman39e33ac2008-03-12 20:50:04 +000065 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
Evan Cheng81a03822007-11-17 00:40:40 +000066 /// mapping. Each spilled virtual register that should be remat'd has an
67 /// entry in it which corresponds to the remat id.
Evan Cheng549f27d32007-08-13 23:45:17 +000068 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Evan Cheng81a03822007-11-17 00:40:40 +000069
70 /// Virt2SplitMap - This is virtual register to splitted virtual register
71 /// mapping.
72 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
73
Evan Chengadf85902007-12-05 09:51:10 +000074 /// Virt2SplitKillMap - This is splitted virtual register to its last use
Evan Chengd120ffd2007-12-05 10:24:35 +000075 /// (kill) index mapping.
76 IndexedMap<unsigned> Virt2SplitKillMap;
Evan Chengadf85902007-12-05 09:51:10 +000077
Evan Cheng81a03822007-11-17 00:40:40 +000078 /// ReMatMap - This is virtual register to re-materialized instruction
79 /// mapping. Each virtual register whose definition is going to be
80 /// re-materialized has an entry in it.
81 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
82
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000083 /// MI2VirtMap - This is MachineInstr to virtual register
84 /// mapping. In the case of memory spill code being folded into
85 /// instructions, we need to know which virtual register was
86 /// read/written by this instruction.
Chris Lattner7f690e62004-09-30 02:15:18 +000087 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000088
Evan Cheng81a03822007-11-17 00:40:40 +000089 /// SpillPt2VirtMap - This records the virtual registers which should
90 /// be spilled right after the MachineInstr due to live interval
91 /// splitting.
Evan Chengb50bb8c2007-12-05 08:16:32 +000092 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
93 SpillPt2VirtMap;
Evan Cheng2638e1a2007-03-20 08:13:50 +000094
Evan Cheng0cbb1162007-11-29 01:06:25 +000095 /// RestorePt2VirtMap - This records the virtual registers which should
96 /// be restored right before the MachineInstr due to live interval
97 /// splitting.
98 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
99
Evan Cheng676dd7c2008-03-11 07:19:34 +0000100 /// EmergencySpillMap - This records the physical registers that should
101 /// be spilled / restored around the MachineInstr since the register
102 /// allocator has run out of registers.
103 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
104
105 /// EmergencySpillSlots - This records emergency spill slots used to
106 /// spill physical registers when the register allocator runs out of
107 /// registers. Ideally only one stack slot is used per function per
108 /// register class.
109 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
110
Evan Cheng2638e1a2007-03-20 08:13:50 +0000111 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng91935142007-04-04 07:40:01 +0000112 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng2638e1a2007-03-20 08:13:50 +0000113 /// the highest id used so far. Note, this starts at (1<<18) to avoid
114 /// conflicts with stack slot numbers.
115 int ReMatId;
116
Evan Chengd3653122008-02-27 03:04:06 +0000117 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
118 int LowSpillSlot, HighSpillSlot;
119
120 /// SpillSlotToUsesMap - Records uses for each register spill slot.
121 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
122
Evan Cheng4cce6b42008-04-11 17:53:36 +0000123 /// ImplicitDefed - One bit for each virtual register. If set it indicates
124 /// the register is implicitly defined.
125 BitVector ImplicitDefed;
126
Evan Chengc781a242009-05-03 18:32:42 +0000127 /// UnusedRegs - A list of physical registers that have not been used.
128 BitVector UnusedRegs;
129
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000130 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
131 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +0000132
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000133 public:
Owen Anderson49c8aa02009-03-13 05:55:11 +0000134 static char ID;
135 VirtRegMap() : MachineFunctionPass(&ID), Virt2PhysMap(NO_PHYS_REG),
136 Virt2StackSlotMap(NO_STACK_SLOT),
137 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
138 Virt2SplitKillMap(0), ReMatMap(NULL),
139 ReMatId(MAX_STACK_SLOT+1),
140 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
141 virtual bool runOnMachineFunction(MachineFunction &MF);
142
143 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
144 AU.setPreservesAll();
145 MachineFunctionPass::getAnalysisUsage(AU);
146 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000147
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000148 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000149
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000150 /// @brief returns true if the specified virtual register is
151 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000152 bool hasPhys(unsigned virtReg) const {
153 return getPhys(virtReg) != NO_PHYS_REG;
154 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000155
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000156 /// @brief returns the physical register mapped to the specified
157 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000158 unsigned getPhys(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000159 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000160 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000161 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000162
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000163 /// @brief creates a mapping for the specified virtual register to
164 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000165 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000166 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
167 TargetRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000168 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000169 "attempt to assign physical register to already mapped "
170 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000171 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000172 }
173
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000174 /// @brief clears the specified virtual register's, physical
175 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000176 void clearVirt(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000177 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000178 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000179 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000180 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000181 }
182
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000183 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000184 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000185 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000186 grow();
187 }
188
Evan Cheng81a03822007-11-17 00:40:40 +0000189 /// @brief records virtReg is a split live interval from SReg.
190 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
191 Virt2SplitMap[virtReg] = SReg;
192 }
193
194 /// @brief returns the live interval virtReg is split from.
195 unsigned getPreSplitReg(unsigned virtReg) {
196 return Virt2SplitMap[virtReg];
197 }
198
Dan Gohman39e33ac2008-03-12 20:50:04 +0000199 /// @brief returns true if the specified virtual register is not
Evan Cheng549f27d32007-08-13 23:45:17 +0000200 /// mapped to a stack slot or rematerialized.
201 bool isAssignedReg(unsigned virtReg) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000202 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
203 getReMatId(virtReg) == NO_STACK_SLOT)
204 return true;
205 // Split register can be assigned a physical register as well as a
206 // stack slot or remat id.
207 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000208 }
209
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000210 /// @brief returns the stack slot mapped to the specified virtual
211 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000212 int getStackSlot(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000213 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000214 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000215 }
216
Evan Cheng549f27d32007-08-13 23:45:17 +0000217 /// @brief returns the rematerialization id mapped to the specified virtual
218 /// register
219 int getReMatId(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000220 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng549f27d32007-08-13 23:45:17 +0000221 return Virt2ReMatIdMap[virtReg];
222 }
223
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000224 /// @brief create a mapping for the specifed virtual register to
225 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000226 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000227 /// @brief create a mapping for the specified virtual register to
228 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000229 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
230
Evan Cheng2638e1a2007-03-20 08:13:50 +0000231 /// @brief assign an unique re-materialization id to the specified
232 /// virtual register.
233 int assignVirtReMatId(unsigned virtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000234 /// @brief assign an unique re-materialization id to the specified
235 /// virtual register.
236 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng2638e1a2007-03-20 08:13:50 +0000237
238 /// @brief returns true if the specified virtual register is being
239 /// re-materialized.
240 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng549f27d32007-08-13 23:45:17 +0000241 return ReMatMap[virtReg] != NULL;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000242 }
243
244 /// @brief returns the original machine instruction being re-issued
245 /// to re-materialize the specified virtual register.
Evan Cheng549f27d32007-08-13 23:45:17 +0000246 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng2638e1a2007-03-20 08:13:50 +0000247 return ReMatMap[virtReg];
248 }
249
250 /// @brief records the specified virtual register will be
251 /// re-materialized and the original instruction which will be re-issed
Evan Cheng549f27d32007-08-13 23:45:17 +0000252 /// for this purpose. If parameter all is true, then all uses of the
253 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng2638e1a2007-03-20 08:13:50 +0000254 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
255 ReMatMap[virtReg] = def;
256 }
257
Evan Chengadf85902007-12-05 09:51:10 +0000258 /// @brief record the last use (kill) of a split virtual register.
Evan Chengd120ffd2007-12-05 10:24:35 +0000259 void addKillPoint(unsigned virtReg, unsigned index) {
260 Virt2SplitKillMap[virtReg] = index;
Evan Chengadf85902007-12-05 09:51:10 +0000261 }
262
Evan Chengd120ffd2007-12-05 10:24:35 +0000263 unsigned getKillPoint(unsigned virtReg) const {
264 return Virt2SplitKillMap[virtReg];
265 }
266
267 /// @brief remove the last use (kill) of a split virtual register.
Evan Chengadf85902007-12-05 09:51:10 +0000268 void removeKillPoint(unsigned virtReg) {
Evan Chengd120ffd2007-12-05 10:24:35 +0000269 Virt2SplitKillMap[virtReg] = 0;
Evan Chengadf85902007-12-05 09:51:10 +0000270 }
271
Evan Chengcada2452007-11-28 01:28:46 +0000272 /// @brief returns true if the specified MachineInstr is a spill point.
273 bool isSpillPt(MachineInstr *Pt) const {
274 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
275 }
276
Evan Cheng81a03822007-11-17 00:40:40 +0000277 /// @brief returns the virtual registers that should be spilled due to
278 /// splitting right after the specified MachineInstr.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000279 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
Evan Cheng81a03822007-11-17 00:40:40 +0000280 return SpillPt2VirtMap[Pt];
281 }
282
283 /// @brief records the specified MachineInstr as a spill point for virtReg.
Evan Chengb50bb8c2007-12-05 08:16:32 +0000284 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
Evan Chengc781a242009-05-03 18:32:42 +0000285 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
286 I = SpillPt2VirtMap.find(Pt);
287 if (I != SpillPt2VirtMap.end())
288 I->second.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000289 else {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000290 std::vector<std::pair<unsigned,bool> > Virts;
291 Virts.push_back(std::make_pair(virtReg, isKill));
Evan Chengcada2452007-11-28 01:28:46 +0000292 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
293 }
Evan Cheng81a03822007-11-17 00:40:40 +0000294 }
295
Evan Chengc1f53c72008-03-11 21:34:46 +0000296 /// @brief - transfer spill point information from one instruction to
297 /// another.
Evan Cheng81a03822007-11-17 00:40:40 +0000298 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
Evan Chengc781a242009-05-03 18:32:42 +0000299 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
Evan Chengb50bb8c2007-12-05 08:16:32 +0000300 I = SpillPt2VirtMap.find(Old);
Evan Chengcada2452007-11-28 01:28:46 +0000301 if (I == SpillPt2VirtMap.end())
302 return;
303 while (!I->second.empty()) {
Evan Chengb50bb8c2007-12-05 08:16:32 +0000304 unsigned virtReg = I->second.back().first;
305 bool isKill = I->second.back().second;
Evan Chengcada2452007-11-28 01:28:46 +0000306 I->second.pop_back();
Evan Chengb50bb8c2007-12-05 08:16:32 +0000307 addSpillPoint(virtReg, isKill, New);
Evan Cheng81a03822007-11-17 00:40:40 +0000308 }
Evan Chengcada2452007-11-28 01:28:46 +0000309 SpillPt2VirtMap.erase(I);
Evan Cheng81a03822007-11-17 00:40:40 +0000310 }
311
Evan Cheng0cbb1162007-11-29 01:06:25 +0000312 /// @brief returns true if the specified MachineInstr is a restore point.
313 bool isRestorePt(MachineInstr *Pt) const {
314 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
315 }
316
317 /// @brief returns the virtual registers that should be restoreed due to
318 /// splitting right after the specified MachineInstr.
319 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
320 return RestorePt2VirtMap[Pt];
321 }
322
323 /// @brief records the specified MachineInstr as a restore point for virtReg.
324 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
Evan Chengc781a242009-05-03 18:32:42 +0000325 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
326 RestorePt2VirtMap.find(Pt);
327 if (I != RestorePt2VirtMap.end())
328 I->second.push_back(virtReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000329 else {
330 std::vector<unsigned> Virts;
331 Virts.push_back(virtReg);
332 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
333 }
334 }
335
Evan Cheng676dd7c2008-03-11 07:19:34 +0000336 /// @brief - transfer restore point information from one instruction to
337 /// another.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000338 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
Evan Chengc781a242009-05-03 18:32:42 +0000339 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
Evan Cheng0cbb1162007-11-29 01:06:25 +0000340 RestorePt2VirtMap.find(Old);
341 if (I == RestorePt2VirtMap.end())
342 return;
343 while (!I->second.empty()) {
344 unsigned virtReg = I->second.back();
345 I->second.pop_back();
346 addRestorePoint(virtReg, New);
347 }
348 RestorePt2VirtMap.erase(I);
349 }
350
Evan Cheng676dd7c2008-03-11 07:19:34 +0000351 /// @brief records that the specified physical register must be spilled
352 /// around the specified machine instr.
353 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
354 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
355 EmergencySpillMap[MI].push_back(PhysReg);
356 else {
357 std::vector<unsigned> PhysRegs;
358 PhysRegs.push_back(PhysReg);
359 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
360 }
361 }
362
363 /// @brief returns true if one or more physical registers must be spilled
364 /// around the specified instruction.
365 bool hasEmergencySpills(MachineInstr *MI) const {
366 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
367 }
368
369 /// @brief returns the physical registers to be spilled and restored around
370 /// the instruction.
371 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
372 return EmergencySpillMap[MI];
373 }
374
Evan Chengc1f53c72008-03-11 21:34:46 +0000375 /// @brief - transfer emergency spill information from one instruction to
376 /// another.
377 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
378 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
379 EmergencySpillMap.find(Old);
380 if (I == EmergencySpillMap.end())
381 return;
382 while (!I->second.empty()) {
383 unsigned virtReg = I->second.back();
384 I->second.pop_back();
385 addEmergencySpill(virtReg, New);
386 }
387 EmergencySpillMap.erase(I);
388 }
389
Evan Cheng676dd7c2008-03-11 07:19:34 +0000390 /// @brief return or get a emergency spill slot for the register class.
391 int getEmergencySpillSlot(const TargetRegisterClass *RC);
392
Evan Chengd3653122008-02-27 03:04:06 +0000393 /// @brief Return lowest spill slot index.
394 int getLowSpillSlot() const {
395 return LowSpillSlot;
396 }
397
398 /// @brief Return highest spill slot index.
399 int getHighSpillSlot() const {
400 return HighSpillSlot;
401 }
402
403 /// @brief Records a spill slot use.
404 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
405
406 /// @brief Returns true if spill slot has been used.
407 bool isSpillSlotUsed(int FrameIndex) const {
408 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
409 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
410 }
411
Evan Cheng4cce6b42008-04-11 17:53:36 +0000412 /// @brief Mark the specified register as being implicitly defined.
413 void setIsImplicitlyDefined(unsigned VirtReg) {
414 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
415 }
416
417 /// @brief Returns true if the virtual register is implicitly defined.
418 bool isImplicitlyDefined(unsigned VirtReg) const {
419 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
420 }
421
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000422 /// @brief Updates information about the specified virtual register's value
Evan Chengaee4af62007-12-02 08:30:39 +0000423 /// folded into newMI machine instruction.
424 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
425 ModRef MRInfo);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000426
Evan Cheng7f566252007-10-13 02:50:24 +0000427 /// @brief Updates information about the specified virtual register's value
428 /// folded into the specified machine instruction.
429 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
430
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000431 /// @brief returns the virtual registers' values folded in memory
432 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000433 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000434 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000435 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000436 }
Chris Lattner35f27052006-05-01 21:16:03 +0000437
Evan Chengcada2452007-11-28 01:28:46 +0000438 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
439 /// the folded instruction map and spill point map.
Evan Chengd3653122008-02-27 03:04:06 +0000440 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000441
Evan Chengc781a242009-05-03 18:32:42 +0000442 /// FindUnusedRegisters - Gather a list of allocatable registers that
443 /// have not been allocated to any virtual register.
444 bool FindUnusedRegisters(const TargetRegisterInfo *TRI,
445 LiveIntervals* LIs);
446
447 /// HasUnusedRegisters - Return true if there are any allocatable registers
448 /// that have not been allocated to any virtual register.
449 bool HasUnusedRegisters() const {
450 return !UnusedRegs.none();
451 }
452
453 /// setRegisterUsed - Remember the physical register is now used.
454 void setRegisterUsed(unsigned Reg) {
455 UnusedRegs.reset(Reg);
456 }
457
458 /// isRegisterUnused - Return true if the physical register has not been
459 /// used.
460 bool isRegisterUnused(unsigned Reg) const {
461 return UnusedRegs[Reg];
462 }
463
464 /// getFirstUnusedRegister - Return the first physical register that has not
465 /// been used.
466 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
467 int Reg = UnusedRegs.find_first();
468 while (Reg != -1) {
469 if (RC->contains(Reg))
470 return (unsigned)Reg;
471 Reg = UnusedRegs.find_next(Reg);
472 }
473 return 0;
474 }
475
Owen Anderson49c8aa02009-03-13 05:55:11 +0000476 void print(std::ostream &OS, const Module* M = 0) const;
Bill Wendling5c7e3262006-12-17 05:15:13 +0000477 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000478 void dump() const;
479 };
480
Bill Wendling5c7e3262006-12-17 05:15:13 +0000481 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
482 VRM.print(OS);
483 return OS;
484 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000485 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
486 VRM.print(OS);
487 return OS;
488 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000489} // End llvm namespace
490
491#endif