Fix comment for consistency sake.
[llvm/avr.git] / lib / CodeGen / VirtRegMap.h
blob482ba1bc29e1436f291f74fde041bac9b3283bb5
1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/CodeGen/MachineFunctionPass.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include "llvm/ADT/BitVector.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/IndexedMap.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include <map>
29 namespace llvm {
30 class LiveIntervals;
31 class MachineInstr;
32 class MachineFunction;
33 class MachineRegisterInfo;
34 class TargetInstrInfo;
35 class TargetRegisterInfo;
36 class raw_ostream;
38 class VirtRegMap : public MachineFunctionPass {
39 public:
40 enum {
41 NO_PHYS_REG = 0,
42 NO_STACK_SLOT = (1L << 30)-1,
43 MAX_STACK_SLOT = (1L << 18)-1
46 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
47 typedef std::multimap<MachineInstr*,
48 std::pair<unsigned, ModRef> > MI2VirtMapTy;
50 private:
51 MachineRegisterInfo *MRI;
52 const TargetInstrInfo *TII;
53 const TargetRegisterInfo *TRI;
54 MachineFunction *MF;
56 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
58 /// Virt2PhysMap - This is a virtual to physical register
59 /// mapping. Each virtual register is required to have an entry in
60 /// it; even spilled virtual registers (the register mapped to a
61 /// spilled register is the temporary used to load it from the
62 /// stack).
63 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
65 /// Virt2StackSlotMap - This is virtual register to stack slot
66 /// mapping. Each spilled virtual register has an entry in it
67 /// which corresponds to the stack slot this register is spilled
68 /// at.
69 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
71 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
72 /// mapping. Each spilled virtual register that should be remat'd has an
73 /// entry in it which corresponds to the remat id.
74 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
76 /// Virt2SplitMap - This is virtual register to splitted virtual register
77 /// mapping.
78 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
80 /// Virt2SplitKillMap - This is splitted virtual register to its last use
81 /// (kill) index mapping.
82 IndexedMap<unsigned> Virt2SplitKillMap;
84 /// ReMatMap - This is virtual register to re-materialized instruction
85 /// mapping. Each virtual register whose definition is going to be
86 /// re-materialized has an entry in it.
87 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
89 /// MI2VirtMap - This is MachineInstr to virtual register
90 /// mapping. In the case of memory spill code being folded into
91 /// instructions, we need to know which virtual register was
92 /// read/written by this instruction.
93 MI2VirtMapTy MI2VirtMap;
95 /// SpillPt2VirtMap - This records the virtual registers which should
96 /// be spilled right after the MachineInstr due to live interval
97 /// splitting.
98 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
99 SpillPt2VirtMap;
101 /// RestorePt2VirtMap - This records the virtual registers which should
102 /// be restored right before the MachineInstr due to live interval
103 /// splitting.
104 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
106 /// EmergencySpillMap - This records the physical registers that should
107 /// be spilled / restored around the MachineInstr since the register
108 /// allocator has run out of registers.
109 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
111 /// EmergencySpillSlots - This records emergency spill slots used to
112 /// spill physical registers when the register allocator runs out of
113 /// registers. Ideally only one stack slot is used per function per
114 /// register class.
115 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
117 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
118 /// virtual register, an unique id is being assigned. This keeps track of
119 /// the highest id used so far. Note, this starts at (1<<18) to avoid
120 /// conflicts with stack slot numbers.
121 int ReMatId;
123 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
124 int LowSpillSlot, HighSpillSlot;
126 /// SpillSlotToUsesMap - Records uses for each register spill slot.
127 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
129 /// ImplicitDefed - One bit for each virtual register. If set it indicates
130 /// the register is implicitly defined.
131 BitVector ImplicitDefed;
133 /// UnusedRegs - A list of physical registers that have not been used.
134 BitVector UnusedRegs;
136 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
137 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
139 public:
140 static char ID;
141 VirtRegMap() : MachineFunctionPass(&ID), Virt2PhysMap(NO_PHYS_REG),
142 Virt2StackSlotMap(NO_STACK_SLOT),
143 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
144 Virt2SplitKillMap(0), ReMatMap(NULL),
145 ReMatId(MAX_STACK_SLOT+1),
146 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
147 virtual bool runOnMachineFunction(MachineFunction &MF);
149 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
150 AU.setPreservesAll();
151 MachineFunctionPass::getAnalysisUsage(AU);
154 void grow();
156 /// @brief returns true if the specified virtual register is
157 /// mapped to a physical register
158 bool hasPhys(unsigned virtReg) const {
159 return getPhys(virtReg) != NO_PHYS_REG;
162 /// @brief returns the physical register mapped to the specified
163 /// virtual register
164 unsigned getPhys(unsigned virtReg) const {
165 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
166 return Virt2PhysMap[virtReg];
169 /// @brief creates a mapping for the specified virtual register to
170 /// the specified physical register
171 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
172 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
173 TargetRegisterInfo::isPhysicalRegister(physReg));
174 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
175 "attempt to assign physical register to already mapped "
176 "virtual register");
177 Virt2PhysMap[virtReg] = physReg;
180 /// @brief clears the specified virtual register's, physical
181 /// register mapping
182 void clearVirt(unsigned virtReg) {
183 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
184 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
185 "attempt to clear a not assigned virtual register");
186 Virt2PhysMap[virtReg] = NO_PHYS_REG;
189 /// @brief clears all virtual to physical register mappings
190 void clearAllVirt() {
191 Virt2PhysMap.clear();
192 grow();
195 /// @brief returns the register allocation preference.
196 unsigned getRegAllocPref(unsigned virtReg);
198 /// @brief records virtReg is a split live interval from SReg.
199 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
200 Virt2SplitMap[virtReg] = SReg;
203 /// @brief returns the live interval virtReg is split from.
204 unsigned getPreSplitReg(unsigned virtReg) {
205 return Virt2SplitMap[virtReg];
208 /// @brief returns true if the specified virtual register is not
209 /// mapped to a stack slot or rematerialized.
210 bool isAssignedReg(unsigned virtReg) const {
211 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
212 getReMatId(virtReg) == NO_STACK_SLOT)
213 return true;
214 // Split register can be assigned a physical register as well as a
215 // stack slot or remat id.
216 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
219 /// @brief returns the stack slot mapped to the specified virtual
220 /// register
221 int getStackSlot(unsigned virtReg) const {
222 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
223 return Virt2StackSlotMap[virtReg];
226 /// @brief returns the rematerialization id mapped to the specified virtual
227 /// register
228 int getReMatId(unsigned virtReg) const {
229 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
230 return Virt2ReMatIdMap[virtReg];
233 /// @brief create a mapping for the specifed virtual register to
234 /// the next available stack slot
235 int assignVirt2StackSlot(unsigned virtReg);
236 /// @brief create a mapping for the specified virtual register to
237 /// the specified stack slot
238 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
240 /// @brief assign an unique re-materialization id to the specified
241 /// virtual register.
242 int assignVirtReMatId(unsigned virtReg);
243 /// @brief assign an unique re-materialization id to the specified
244 /// virtual register.
245 void assignVirtReMatId(unsigned virtReg, int id);
247 /// @brief returns true if the specified virtual register is being
248 /// re-materialized.
249 bool isReMaterialized(unsigned virtReg) const {
250 return ReMatMap[virtReg] != NULL;
253 /// @brief returns the original machine instruction being re-issued
254 /// to re-materialize the specified virtual register.
255 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
256 return ReMatMap[virtReg];
259 /// @brief records the specified virtual register will be
260 /// re-materialized and the original instruction which will be re-issed
261 /// for this purpose. If parameter all is true, then all uses of the
262 /// registers are rematerialized and it's safe to delete the definition.
263 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
264 ReMatMap[virtReg] = def;
267 /// @brief record the last use (kill) of a split virtual register.
268 void addKillPoint(unsigned virtReg, unsigned index) {
269 Virt2SplitKillMap[virtReg] = index;
272 unsigned getKillPoint(unsigned virtReg) const {
273 return Virt2SplitKillMap[virtReg];
276 /// @brief remove the last use (kill) of a split virtual register.
277 void removeKillPoint(unsigned virtReg) {
278 Virt2SplitKillMap[virtReg] = 0;
281 /// @brief returns true if the specified MachineInstr is a spill point.
282 bool isSpillPt(MachineInstr *Pt) const {
283 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
286 /// @brief returns the virtual registers that should be spilled due to
287 /// splitting right after the specified MachineInstr.
288 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
289 return SpillPt2VirtMap[Pt];
292 /// @brief records the specified MachineInstr as a spill point for virtReg.
293 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
294 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
295 I = SpillPt2VirtMap.find(Pt);
296 if (I != SpillPt2VirtMap.end())
297 I->second.push_back(std::make_pair(virtReg, isKill));
298 else {
299 std::vector<std::pair<unsigned,bool> > Virts;
300 Virts.push_back(std::make_pair(virtReg, isKill));
301 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
305 /// @brief - transfer spill point information from one instruction to
306 /// another.
307 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
308 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
309 I = SpillPt2VirtMap.find(Old);
310 if (I == SpillPt2VirtMap.end())
311 return;
312 while (!I->second.empty()) {
313 unsigned virtReg = I->second.back().first;
314 bool isKill = I->second.back().second;
315 I->second.pop_back();
316 addSpillPoint(virtReg, isKill, New);
318 SpillPt2VirtMap.erase(I);
321 /// @brief returns true if the specified MachineInstr is a restore point.
322 bool isRestorePt(MachineInstr *Pt) const {
323 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
326 /// @brief returns the virtual registers that should be restoreed due to
327 /// splitting right after the specified MachineInstr.
328 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
329 return RestorePt2VirtMap[Pt];
332 /// @brief records the specified MachineInstr as a restore point for virtReg.
333 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
334 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
335 RestorePt2VirtMap.find(Pt);
336 if (I != RestorePt2VirtMap.end())
337 I->second.push_back(virtReg);
338 else {
339 std::vector<unsigned> Virts;
340 Virts.push_back(virtReg);
341 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
345 /// @brief - transfer restore point information from one instruction to
346 /// another.
347 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
348 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
349 RestorePt2VirtMap.find(Old);
350 if (I == RestorePt2VirtMap.end())
351 return;
352 while (!I->second.empty()) {
353 unsigned virtReg = I->second.back();
354 I->second.pop_back();
355 addRestorePoint(virtReg, New);
357 RestorePt2VirtMap.erase(I);
360 /// @brief records that the specified physical register must be spilled
361 /// around the specified machine instr.
362 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
363 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
364 EmergencySpillMap[MI].push_back(PhysReg);
365 else {
366 std::vector<unsigned> PhysRegs;
367 PhysRegs.push_back(PhysReg);
368 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
372 /// @brief returns true if one or more physical registers must be spilled
373 /// around the specified instruction.
374 bool hasEmergencySpills(MachineInstr *MI) const {
375 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
378 /// @brief returns the physical registers to be spilled and restored around
379 /// the instruction.
380 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
381 return EmergencySpillMap[MI];
384 /// @brief - transfer emergency spill information from one instruction to
385 /// another.
386 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
387 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
388 EmergencySpillMap.find(Old);
389 if (I == EmergencySpillMap.end())
390 return;
391 while (!I->second.empty()) {
392 unsigned virtReg = I->second.back();
393 I->second.pop_back();
394 addEmergencySpill(virtReg, New);
396 EmergencySpillMap.erase(I);
399 /// @brief return or get a emergency spill slot for the register class.
400 int getEmergencySpillSlot(const TargetRegisterClass *RC);
402 /// @brief Return lowest spill slot index.
403 int getLowSpillSlot() const {
404 return LowSpillSlot;
407 /// @brief Return highest spill slot index.
408 int getHighSpillSlot() const {
409 return HighSpillSlot;
412 /// @brief Records a spill slot use.
413 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
415 /// @brief Returns true if spill slot has been used.
416 bool isSpillSlotUsed(int FrameIndex) const {
417 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
418 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
421 /// @brief Mark the specified register as being implicitly defined.
422 void setIsImplicitlyDefined(unsigned VirtReg) {
423 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
426 /// @brief Returns true if the virtual register is implicitly defined.
427 bool isImplicitlyDefined(unsigned VirtReg) const {
428 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
431 /// @brief Updates information about the specified virtual register's value
432 /// folded into newMI machine instruction.
433 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
434 ModRef MRInfo);
436 /// @brief Updates information about the specified virtual register's value
437 /// folded into the specified machine instruction.
438 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
440 /// @brief returns the virtual registers' values folded in memory
441 /// operands of this instruction
442 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
443 getFoldedVirts(MachineInstr* MI) const {
444 return MI2VirtMap.equal_range(MI);
447 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
448 /// the folded instruction map and spill point map.
449 void RemoveMachineInstrFromMaps(MachineInstr *MI);
451 /// FindUnusedRegisters - Gather a list of allocatable registers that
452 /// have not been allocated to any virtual register.
453 bool FindUnusedRegisters(LiveIntervals* LIs);
455 /// HasUnusedRegisters - Return true if there are any allocatable registers
456 /// that have not been allocated to any virtual register.
457 bool HasUnusedRegisters() const {
458 return !UnusedRegs.none();
461 /// setRegisterUsed - Remember the physical register is now used.
462 void setRegisterUsed(unsigned Reg) {
463 UnusedRegs.reset(Reg);
466 /// isRegisterUnused - Return true if the physical register has not been
467 /// used.
468 bool isRegisterUnused(unsigned Reg) const {
469 return UnusedRegs[Reg];
472 /// getFirstUnusedRegister - Return the first physical register that has not
473 /// been used.
474 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
475 int Reg = UnusedRegs.find_first();
476 while (Reg != -1) {
477 if (allocatableRCRegs[RC][Reg])
478 return (unsigned)Reg;
479 Reg = UnusedRegs.find_next(Reg);
481 return 0;
484 void print(raw_ostream &OS, const Module* M = 0) const;
485 void dump() const;
488 inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
489 VRM.print(OS);
490 return OS;
492 } // End llvm namespace
494 #endif