1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
18 #define LLVM_CODEGEN_VIRTREGMAP_H
20 #include "llvm/Target/TargetRegisterInfo.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/IndexedMap.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/Support/Streams.h"
30 class MachineFunction;
31 class TargetInstrInfo;
37 NO_STACK_SLOT = (1L << 30)-1,
38 MAX_STACK_SLOT = (1L << 18)-1
41 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
42 typedef std::multimap<MachineInstr*,
43 std::pair<unsigned, ModRef> > MI2VirtMapTy;
46 const TargetInstrInfo &TII;
49 /// Virt2PhysMap - This is a virtual to physical register
50 /// mapping. Each virtual register is required to have an entry in
51 /// it; even spilled virtual registers (the register mapped to a
52 /// spilled register is the temporary used to load it from the
54 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
56 /// Virt2StackSlotMap - This is virtual register to stack slot
57 /// mapping. Each spilled virtual register has an entry in it
58 /// which corresponds to the stack slot this register is spilled
60 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
62 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
63 /// mapping. Each spilled virtual register that should be remat'd has an
64 /// entry in it which corresponds to the remat id.
65 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
67 /// Virt2SplitMap - This is virtual register to splitted virtual register
69 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
71 /// Virt2SplitKillMap - This is splitted virtual register to its last use
72 /// (kill) index mapping.
73 IndexedMap<unsigned> Virt2SplitKillMap;
75 /// ReMatMap - This is virtual register to re-materialized instruction
76 /// mapping. Each virtual register whose definition is going to be
77 /// re-materialized has an entry in it.
78 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
80 /// MI2VirtMap - This is MachineInstr to virtual register
81 /// mapping. In the case of memory spill code being folded into
82 /// instructions, we need to know which virtual register was
83 /// read/written by this instruction.
84 MI2VirtMapTy MI2VirtMap;
86 /// SpillPt2VirtMap - This records the virtual registers which should
87 /// be spilled right after the MachineInstr due to live interval
89 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
92 /// RestorePt2VirtMap - This records the virtual registers which should
93 /// be restored right before the MachineInstr due to live interval
95 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
97 /// EmergencySpillMap - This records the physical registers that should
98 /// be spilled / restored around the MachineInstr since the register
99 /// allocator has run out of registers.
100 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
102 /// EmergencySpillSlots - This records emergency spill slots used to
103 /// spill physical registers when the register allocator runs out of
104 /// registers. Ideally only one stack slot is used per function per
106 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
108 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
109 /// virtual register, an unique id is being assigned. This keeps track of
110 /// the highest id used so far. Note, this starts at (1<<18) to avoid
111 /// conflicts with stack slot numbers.
114 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
115 int LowSpillSlot, HighSpillSlot;
117 /// SpillSlotToUsesMap - Records uses for each register spill slot.
118 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
120 /// ImplicitDefed - One bit for each virtual register. If set it indicates
121 /// the register is implicitly defined.
122 BitVector ImplicitDefed;
124 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
125 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
128 explicit VirtRegMap(MachineFunction &mf);
132 /// @brief returns true if the specified virtual register is
133 /// mapped to a physical register
134 bool hasPhys(unsigned virtReg) const {
135 return getPhys(virtReg) != NO_PHYS_REG;
138 /// @brief returns the physical register mapped to the specified
140 unsigned getPhys(unsigned virtReg) const {
141 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
142 return Virt2PhysMap[virtReg];
145 /// @brief creates a mapping for the specified virtual register to
146 /// the specified physical register
147 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
148 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
149 TargetRegisterInfo::isPhysicalRegister(physReg));
150 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
151 "attempt to assign physical register to already mapped "
153 Virt2PhysMap[virtReg] = physReg;
156 /// @brief clears the specified virtual register's, physical
158 void clearVirt(unsigned virtReg) {
159 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
160 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
161 "attempt to clear a not assigned virtual register");
162 Virt2PhysMap[virtReg] = NO_PHYS_REG;
165 /// @brief clears all virtual to physical register mappings
166 void clearAllVirt() {
167 Virt2PhysMap.clear();
171 /// @brief records virtReg is a split live interval from SReg.
172 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
173 Virt2SplitMap[virtReg] = SReg;
176 /// @brief returns the live interval virtReg is split from.
177 unsigned getPreSplitReg(unsigned virtReg) {
178 return Virt2SplitMap[virtReg];
181 /// @brief returns true if the specified virtual register is not
182 /// mapped to a stack slot or rematerialized.
183 bool isAssignedReg(unsigned virtReg) const {
184 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
185 getReMatId(virtReg) == NO_STACK_SLOT)
187 // Split register can be assigned a physical register as well as a
188 // stack slot or remat id.
189 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
192 /// @brief returns the stack slot mapped to the specified virtual
194 int getStackSlot(unsigned virtReg) const {
195 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
196 return Virt2StackSlotMap[virtReg];
199 /// @brief returns the rematerialization id mapped to the specified virtual
201 int getReMatId(unsigned virtReg) const {
202 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
203 return Virt2ReMatIdMap[virtReg];
206 /// @brief create a mapping for the specifed virtual register to
207 /// the next available stack slot
208 int assignVirt2StackSlot(unsigned virtReg);
209 /// @brief create a mapping for the specified virtual register to
210 /// the specified stack slot
211 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
213 /// @brief assign an unique re-materialization id to the specified
214 /// virtual register.
215 int assignVirtReMatId(unsigned virtReg);
216 /// @brief assign an unique re-materialization id to the specified
217 /// virtual register.
218 void assignVirtReMatId(unsigned virtReg, int id);
220 /// @brief returns true if the specified virtual register is being
222 bool isReMaterialized(unsigned virtReg) const {
223 return ReMatMap[virtReg] != NULL;
226 /// @brief returns the original machine instruction being re-issued
227 /// to re-materialize the specified virtual register.
228 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
229 return ReMatMap[virtReg];
232 /// @brief records the specified virtual register will be
233 /// re-materialized and the original instruction which will be re-issed
234 /// for this purpose. If parameter all is true, then all uses of the
235 /// registers are rematerialized and it's safe to delete the definition.
236 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
237 ReMatMap[virtReg] = def;
240 /// @brief record the last use (kill) of a split virtual register.
241 void addKillPoint(unsigned virtReg, unsigned index) {
242 Virt2SplitKillMap[virtReg] = index;
245 unsigned getKillPoint(unsigned virtReg) const {
246 return Virt2SplitKillMap[virtReg];
249 /// @brief remove the last use (kill) of a split virtual register.
250 void removeKillPoint(unsigned virtReg) {
251 Virt2SplitKillMap[virtReg] = 0;
254 /// @brief returns true if the specified MachineInstr is a spill point.
255 bool isSpillPt(MachineInstr *Pt) const {
256 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
259 /// @brief returns the virtual registers that should be spilled due to
260 /// splitting right after the specified MachineInstr.
261 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
262 return SpillPt2VirtMap[Pt];
265 /// @brief records the specified MachineInstr as a spill point for virtReg.
266 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
267 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
268 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
270 std::vector<std::pair<unsigned,bool> > Virts;
271 Virts.push_back(std::make_pair(virtReg, isKill));
272 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
276 /// @brief - transfer spill point information from one instruction to
278 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
279 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
280 I = SpillPt2VirtMap.find(Old);
281 if (I == SpillPt2VirtMap.end())
283 while (!I->second.empty()) {
284 unsigned virtReg = I->second.back().first;
285 bool isKill = I->second.back().second;
286 I->second.pop_back();
287 addSpillPoint(virtReg, isKill, New);
289 SpillPt2VirtMap.erase(I);
292 /// @brief returns true if the specified MachineInstr is a restore point.
293 bool isRestorePt(MachineInstr *Pt) const {
294 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
297 /// @brief returns the virtual registers that should be restoreed due to
298 /// splitting right after the specified MachineInstr.
299 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
300 return RestorePt2VirtMap[Pt];
303 /// @brief records the specified MachineInstr as a restore point for virtReg.
304 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
305 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
306 RestorePt2VirtMap[Pt].push_back(virtReg);
308 std::vector<unsigned> Virts;
309 Virts.push_back(virtReg);
310 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
314 /// @brief - transfer restore point information from one instruction to
316 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
317 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
318 RestorePt2VirtMap.find(Old);
319 if (I == RestorePt2VirtMap.end())
321 while (!I->second.empty()) {
322 unsigned virtReg = I->second.back();
323 I->second.pop_back();
324 addRestorePoint(virtReg, New);
326 RestorePt2VirtMap.erase(I);
329 /// @brief records that the specified physical register must be spilled
330 /// around the specified machine instr.
331 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
332 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
333 EmergencySpillMap[MI].push_back(PhysReg);
335 std::vector<unsigned> PhysRegs;
336 PhysRegs.push_back(PhysReg);
337 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
341 /// @brief returns true if one or more physical registers must be spilled
342 /// around the specified instruction.
343 bool hasEmergencySpills(MachineInstr *MI) const {
344 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
347 /// @brief returns the physical registers to be spilled and restored around
349 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
350 return EmergencySpillMap[MI];
353 /// @brief - transfer emergency spill information from one instruction to
355 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
356 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
357 EmergencySpillMap.find(Old);
358 if (I == EmergencySpillMap.end())
360 while (!I->second.empty()) {
361 unsigned virtReg = I->second.back();
362 I->second.pop_back();
363 addEmergencySpill(virtReg, New);
365 EmergencySpillMap.erase(I);
368 /// @brief return or get a emergency spill slot for the register class.
369 int getEmergencySpillSlot(const TargetRegisterClass *RC);
371 /// @brief Return lowest spill slot index.
372 int getLowSpillSlot() const {
376 /// @brief Return highest spill slot index.
377 int getHighSpillSlot() const {
378 return HighSpillSlot;
381 /// @brief Records a spill slot use.
382 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
384 /// @brief Returns true if spill slot has been used.
385 bool isSpillSlotUsed(int FrameIndex) const {
386 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
387 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
390 /// @brief Mark the specified register as being implicitly defined.
391 void setIsImplicitlyDefined(unsigned VirtReg) {
392 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
395 /// @brief Returns true if the virtual register is implicitly defined.
396 bool isImplicitlyDefined(unsigned VirtReg) const {
397 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
400 /// @brief Updates information about the specified virtual register's value
401 /// folded into newMI machine instruction.
402 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
405 /// @brief Updates information about the specified virtual register's value
406 /// folded into the specified machine instruction.
407 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
409 /// @brief returns the virtual registers' values folded in memory
410 /// operands of this instruction
411 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
412 getFoldedVirts(MachineInstr* MI) const {
413 return MI2VirtMap.equal_range(MI);
416 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
417 /// the folded instruction map and spill point map.
418 void RemoveMachineInstrFromMaps(MachineInstr *MI);
420 void print(std::ostream &OS) const;
421 void print(std::ostream *OS) const { if (OS) print(*OS); }
425 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
429 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
434 /// Spiller interface: Implementations of this interface assign spilled
435 /// virtual registers to stack slots, rewriting the code.
438 virtual bool runOnMachineFunction(MachineFunction &MF,
439 VirtRegMap &VRM) = 0;
442 /// createSpiller - Create an return a spiller object, as specified on the
444 Spiller* createSpiller();
446 } // End llvm namespace