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/DenseMap.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/Support/Streams.h"
29 class MachineFunction;
30 class TargetInstrInfo;
36 NO_STACK_SLOT = (1L << 30)-1,
37 MAX_STACK_SLOT = (1L << 18)-1
40 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
41 typedef std::multimap<MachineInstr*,
42 std::pair<unsigned, ModRef> > MI2VirtMapTy;
45 const TargetInstrInfo &TII;
48 /// Virt2PhysMap - This is a virtual to physical register
49 /// mapping. Each virtual register is required to have an entry in
50 /// it; even spilled virtual registers (the register mapped to a
51 /// spilled register is the temporary used to load it from the
53 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
55 /// Virt2StackSlotMap - This is virtual register to stack slot
56 /// mapping. Each spilled virtual register has an entry in it
57 /// which corresponds to the stack slot this register is spilled
59 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
61 /// Virt2StackSlotMap - This is virtual register to rematerialization id
62 /// mapping. Each spilled virtual register that should be remat'd has an
63 /// entry in it which corresponds to the remat id.
64 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
66 /// Virt2SplitMap - This is virtual register to splitted virtual register
68 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
70 /// Virt2SplitKillMap - This is splitted virtual register to its last use
71 /// (kill) index mapping.
72 IndexedMap<unsigned> Virt2SplitKillMap;
74 /// ReMatMap - This is virtual register to re-materialized instruction
75 /// mapping. Each virtual register whose definition is going to be
76 /// re-materialized has an entry in it.
77 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
79 /// MI2VirtMap - This is MachineInstr to virtual register
80 /// mapping. In the case of memory spill code being folded into
81 /// instructions, we need to know which virtual register was
82 /// read/written by this instruction.
83 MI2VirtMapTy MI2VirtMap;
85 /// SpillPt2VirtMap - This records the virtual registers which should
86 /// be spilled right after the MachineInstr due to live interval
88 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
91 /// RestorePt2VirtMap - This records the virtual registers which should
92 /// be restored right before the MachineInstr due to live interval
94 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
96 /// EmergencySpillMap - This records the physical registers that should
97 /// be spilled / restored around the MachineInstr since the register
98 /// allocator has run out of registers.
99 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
101 /// EmergencySpillSlots - This records emergency spill slots used to
102 /// spill physical registers when the register allocator runs out of
103 /// registers. Ideally only one stack slot is used per function per
105 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
107 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
108 /// virtual register, an unique id is being assigned. This keeps track of
109 /// the highest id used so far. Note, this starts at (1<<18) to avoid
110 /// conflicts with stack slot numbers.
113 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
114 int LowSpillSlot, HighSpillSlot;
116 /// SpillSlotToUsesMap - Records uses for each register spill slot.
117 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
119 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
120 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
123 explicit VirtRegMap(MachineFunction &mf);
127 /// @brief returns true if the specified virtual register is
128 /// mapped to a physical register
129 bool hasPhys(unsigned virtReg) const {
130 return getPhys(virtReg) != NO_PHYS_REG;
133 /// @brief returns the physical register mapped to the specified
135 unsigned getPhys(unsigned virtReg) const {
136 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
137 return Virt2PhysMap[virtReg];
140 /// @brief creates a mapping for the specified virtual register to
141 /// the specified physical register
142 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
143 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
144 TargetRegisterInfo::isPhysicalRegister(physReg));
145 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
146 "attempt to assign physical register to already mapped "
148 Virt2PhysMap[virtReg] = physReg;
151 /// @brief clears the specified virtual register's, physical
153 void clearVirt(unsigned virtReg) {
154 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
155 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
156 "attempt to clear a not assigned virtual register");
157 Virt2PhysMap[virtReg] = NO_PHYS_REG;
160 /// @brief clears all virtual to physical register mappings
161 void clearAllVirt() {
162 Virt2PhysMap.clear();
166 /// @brief records virtReg is a split live interval from SReg.
167 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
168 Virt2SplitMap[virtReg] = SReg;
171 /// @brief returns the live interval virtReg is split from.
172 unsigned getPreSplitReg(unsigned virtReg) {
173 return Virt2SplitMap[virtReg];
176 /// @brief returns true is the specified virtual register is not
177 /// mapped to a stack slot or rematerialized.
178 bool isAssignedReg(unsigned virtReg) const {
179 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
180 getReMatId(virtReg) == NO_STACK_SLOT)
182 // Split register can be assigned a physical register as well as a
183 // stack slot or remat id.
184 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
187 /// @brief returns the stack slot mapped to the specified virtual
189 int getStackSlot(unsigned virtReg) const {
190 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
191 return Virt2StackSlotMap[virtReg];
194 /// @brief returns the rematerialization id mapped to the specified virtual
196 int getReMatId(unsigned virtReg) const {
197 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
198 return Virt2ReMatIdMap[virtReg];
201 /// @brief create a mapping for the specifed virtual register to
202 /// the next available stack slot
203 int assignVirt2StackSlot(unsigned virtReg);
204 /// @brief create a mapping for the specified virtual register to
205 /// the specified stack slot
206 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
208 /// @brief assign an unique re-materialization id to the specified
209 /// virtual register.
210 int assignVirtReMatId(unsigned virtReg);
211 /// @brief assign an unique re-materialization id to the specified
212 /// virtual register.
213 void assignVirtReMatId(unsigned virtReg, int id);
215 /// @brief returns true if the specified virtual register is being
217 bool isReMaterialized(unsigned virtReg) const {
218 return ReMatMap[virtReg] != NULL;
221 /// @brief returns the original machine instruction being re-issued
222 /// to re-materialize the specified virtual register.
223 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
224 return ReMatMap[virtReg];
227 /// @brief records the specified virtual register will be
228 /// re-materialized and the original instruction which will be re-issed
229 /// for this purpose. If parameter all is true, then all uses of the
230 /// registers are rematerialized and it's safe to delete the definition.
231 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
232 ReMatMap[virtReg] = def;
235 /// @brief record the last use (kill) of a split virtual register.
236 void addKillPoint(unsigned virtReg, unsigned index) {
237 Virt2SplitKillMap[virtReg] = index;
240 unsigned getKillPoint(unsigned virtReg) const {
241 return Virt2SplitKillMap[virtReg];
244 /// @brief remove the last use (kill) of a split virtual register.
245 void removeKillPoint(unsigned virtReg) {
246 Virt2SplitKillMap[virtReg] = 0;
249 /// @brief returns true if the specified MachineInstr is a spill point.
250 bool isSpillPt(MachineInstr *Pt) const {
251 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
254 /// @brief returns the virtual registers that should be spilled due to
255 /// splitting right after the specified MachineInstr.
256 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
257 return SpillPt2VirtMap[Pt];
260 /// @brief records the specified MachineInstr as a spill point for virtReg.
261 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
262 if (SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end())
263 SpillPt2VirtMap[Pt].push_back(std::make_pair(virtReg, isKill));
265 std::vector<std::pair<unsigned,bool> > Virts;
266 Virts.push_back(std::make_pair(virtReg, isKill));
267 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
271 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
272 std::map<MachineInstr*,std::vector<std::pair<unsigned,bool> > >::iterator
273 I = SpillPt2VirtMap.find(Old);
274 if (I == SpillPt2VirtMap.end())
276 while (!I->second.empty()) {
277 unsigned virtReg = I->second.back().first;
278 bool isKill = I->second.back().second;
279 I->second.pop_back();
280 addSpillPoint(virtReg, isKill, New);
282 SpillPt2VirtMap.erase(I);
285 /// @brief returns true if the specified MachineInstr is a restore point.
286 bool isRestorePt(MachineInstr *Pt) const {
287 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
290 /// @brief returns the virtual registers that should be restoreed due to
291 /// splitting right after the specified MachineInstr.
292 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
293 return RestorePt2VirtMap[Pt];
296 /// @brief records the specified MachineInstr as a restore point for virtReg.
297 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
298 if (RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end())
299 RestorePt2VirtMap[Pt].push_back(virtReg);
301 std::vector<unsigned> Virts;
302 Virts.push_back(virtReg);
303 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
307 /// @brief - transfer restore point information from one instruction to
309 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
310 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
311 RestorePt2VirtMap.find(Old);
312 if (I == RestorePt2VirtMap.end())
314 while (!I->second.empty()) {
315 unsigned virtReg = I->second.back();
316 I->second.pop_back();
317 addRestorePoint(virtReg, New);
319 RestorePt2VirtMap.erase(I);
322 /// @brief records that the specified physical register must be spilled
323 /// around the specified machine instr.
324 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
325 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
326 EmergencySpillMap[MI].push_back(PhysReg);
328 std::vector<unsigned> PhysRegs;
329 PhysRegs.push_back(PhysReg);
330 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
334 /// @brief returns true if one or more physical registers must be spilled
335 /// around the specified instruction.
336 bool hasEmergencySpills(MachineInstr *MI) const {
337 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
340 /// @brief returns the physical registers to be spilled and restored around
342 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
343 return EmergencySpillMap[MI];
346 /// @brief return or get a emergency spill slot for the register class.
347 int getEmergencySpillSlot(const TargetRegisterClass *RC);
349 /// @brief Return lowest spill slot index.
350 int getLowSpillSlot() const {
354 /// @brief Return highest spill slot index.
355 int getHighSpillSlot() const {
356 return HighSpillSlot;
359 /// @brief Records a spill slot use.
360 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
362 /// @brief Returns true if spill slot has been used.
363 bool isSpillSlotUsed(int FrameIndex) const {
364 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
365 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
368 /// @brief Updates information about the specified virtual register's value
369 /// folded into newMI machine instruction.
370 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
373 /// @brief Updates information about the specified virtual register's value
374 /// folded into the specified machine instruction.
375 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
377 /// @brief returns the virtual registers' values folded in memory
378 /// operands of this instruction
379 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
380 getFoldedVirts(MachineInstr* MI) const {
381 return MI2VirtMap.equal_range(MI);
384 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
385 /// the folded instruction map and spill point map.
386 void RemoveMachineInstrFromMaps(MachineInstr *MI);
388 void print(std::ostream &OS) const;
389 void print(std::ostream *OS) const { if (OS) print(*OS); }
393 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
397 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
402 /// Spiller interface: Implementations of this interface assign spilled
403 /// virtual registers to stack slots, rewriting the code.
406 virtual bool runOnMachineFunction(MachineFunction &MF,
407 VirtRegMap &VRM) = 0;
410 /// createSpiller - Create an return a spiller object, as specified on the
412 Spiller* createSpiller();
414 } // End llvm namespace