Change allowsUnalignedMemoryAccesses to take type argument since some targets
[llvm/avr.git] / lib / CodeGen / VirtRegMap.h
blob7d1cf95fce0e4b2c177b9a40e1c7b373f23c9965
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 "llvm/Support/Streams.h"
28 #include <map>
30 namespace llvm {
31 class LiveIntervals;
32 class MachineInstr;
33 class MachineFunction;
34 class MachineRegisterInfo;
35 class TargetInstrInfo;
36 class TargetRegisterInfo;
37 class raw_ostream;
39 class VirtRegMap : public MachineFunctionPass {
40 public:
41 enum {
42 NO_PHYS_REG = 0,
43 NO_STACK_SLOT = (1L << 30)-1,
44 MAX_STACK_SLOT = (1L << 18)-1
47 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
48 typedef std::multimap<MachineInstr*,
49 std::pair<unsigned, ModRef> > MI2VirtMapTy;
51 private:
52 MachineRegisterInfo *MRI;
53 const TargetInstrInfo *TII;
54 const TargetRegisterInfo *TRI;
55 MachineFunction *MF;
57 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
59 /// Virt2PhysMap - This is a virtual to physical register
60 /// mapping. Each virtual register is required to have an entry in
61 /// it; even spilled virtual registers (the register mapped to a
62 /// spilled register is the temporary used to load it from the
63 /// stack).
64 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
66 /// Virt2StackSlotMap - This is virtual register to stack slot
67 /// mapping. Each spilled virtual register has an entry in it
68 /// which corresponds to the stack slot this register is spilled
69 /// at.
70 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
72 /// Virt2ReMatIdMap - This is virtual register to rematerialization id
73 /// mapping. Each spilled virtual register that should be remat'd has an
74 /// entry in it which corresponds to the remat id.
75 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
77 /// Virt2SplitMap - This is virtual register to splitted virtual register
78 /// mapping.
79 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
81 /// Virt2SplitKillMap - This is splitted virtual register to its last use
82 /// (kill) index mapping.
83 IndexedMap<unsigned> Virt2SplitKillMap;
85 /// ReMatMap - This is virtual register to re-materialized instruction
86 /// mapping. Each virtual register whose definition is going to be
87 /// re-materialized has an entry in it.
88 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
90 /// MI2VirtMap - This is MachineInstr to virtual register
91 /// mapping. In the case of memory spill code being folded into
92 /// instructions, we need to know which virtual register was
93 /// read/written by this instruction.
94 MI2VirtMapTy MI2VirtMap;
96 /// SpillPt2VirtMap - This records the virtual registers which should
97 /// be spilled right after the MachineInstr due to live interval
98 /// splitting.
99 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >
100 SpillPt2VirtMap;
102 /// RestorePt2VirtMap - This records the virtual registers which should
103 /// be restored right before the MachineInstr due to live interval
104 /// splitting.
105 std::map<MachineInstr*, std::vector<unsigned> > RestorePt2VirtMap;
107 /// EmergencySpillMap - This records the physical registers that should
108 /// be spilled / restored around the MachineInstr since the register
109 /// allocator has run out of registers.
110 std::map<MachineInstr*, std::vector<unsigned> > EmergencySpillMap;
112 /// EmergencySpillSlots - This records emergency spill slots used to
113 /// spill physical registers when the register allocator runs out of
114 /// registers. Ideally only one stack slot is used per function per
115 /// register class.
116 std::map<const TargetRegisterClass*, int> EmergencySpillSlots;
118 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
119 /// virtual register, an unique id is being assigned. This keeps track of
120 /// the highest id used so far. Note, this starts at (1<<18) to avoid
121 /// conflicts with stack slot numbers.
122 int ReMatId;
124 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
125 int LowSpillSlot, HighSpillSlot;
127 /// SpillSlotToUsesMap - Records uses for each register spill slot.
128 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
130 /// ImplicitDefed - One bit for each virtual register. If set it indicates
131 /// the register is implicitly defined.
132 BitVector ImplicitDefed;
134 /// UnusedRegs - A list of physical registers that have not been used.
135 BitVector UnusedRegs;
137 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
138 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
140 public:
141 static char ID;
142 VirtRegMap() : MachineFunctionPass(&ID), Virt2PhysMap(NO_PHYS_REG),
143 Virt2StackSlotMap(NO_STACK_SLOT),
144 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
145 Virt2SplitKillMap(0), ReMatMap(NULL),
146 ReMatId(MAX_STACK_SLOT+1),
147 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
148 virtual bool runOnMachineFunction(MachineFunction &MF);
150 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
151 AU.setPreservesAll();
152 MachineFunctionPass::getAnalysisUsage(AU);
155 void grow();
157 /// @brief returns true if the specified virtual register is
158 /// mapped to a physical register
159 bool hasPhys(unsigned virtReg) const {
160 return getPhys(virtReg) != NO_PHYS_REG;
163 /// @brief returns the physical register mapped to the specified
164 /// virtual register
165 unsigned getPhys(unsigned virtReg) const {
166 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
167 return Virt2PhysMap[virtReg];
170 /// @brief creates a mapping for the specified virtual register to
171 /// the specified physical register
172 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
173 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
174 TargetRegisterInfo::isPhysicalRegister(physReg));
175 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
176 "attempt to assign physical register to already mapped "
177 "virtual register");
178 Virt2PhysMap[virtReg] = physReg;
181 /// @brief clears the specified virtual register's, physical
182 /// register mapping
183 void clearVirt(unsigned virtReg) {
184 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
185 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
186 "attempt to clear a not assigned virtual register");
187 Virt2PhysMap[virtReg] = NO_PHYS_REG;
190 /// @brief clears all virtual to physical register mappings
191 void clearAllVirt() {
192 Virt2PhysMap.clear();
193 grow();
196 /// @brief returns the register allocation preference.
197 unsigned getRegAllocPref(unsigned virtReg);
199 /// @brief records virtReg is a split live interval from SReg.
200 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
201 Virt2SplitMap[virtReg] = SReg;
204 /// @brief returns the live interval virtReg is split from.
205 unsigned getPreSplitReg(unsigned virtReg) {
206 return Virt2SplitMap[virtReg];
209 /// @brief returns true if the specified virtual register is not
210 /// mapped to a stack slot or rematerialized.
211 bool isAssignedReg(unsigned virtReg) const {
212 if (getStackSlot(virtReg) == NO_STACK_SLOT &&
213 getReMatId(virtReg) == NO_STACK_SLOT)
214 return true;
215 // Split register can be assigned a physical register as well as a
216 // stack slot or remat id.
217 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
220 /// @brief returns the stack slot mapped to the specified virtual
221 /// register
222 int getStackSlot(unsigned virtReg) const {
223 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
224 return Virt2StackSlotMap[virtReg];
227 /// @brief returns the rematerialization id mapped to the specified virtual
228 /// register
229 int getReMatId(unsigned virtReg) const {
230 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
231 return Virt2ReMatIdMap[virtReg];
234 /// @brief create a mapping for the specifed virtual register to
235 /// the next available stack slot
236 int assignVirt2StackSlot(unsigned virtReg);
237 /// @brief create a mapping for the specified virtual register to
238 /// the specified stack slot
239 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
241 /// @brief assign an unique re-materialization id to the specified
242 /// virtual register.
243 int assignVirtReMatId(unsigned virtReg);
244 /// @brief assign an unique re-materialization id to the specified
245 /// virtual register.
246 void assignVirtReMatId(unsigned virtReg, int id);
248 /// @brief returns true if the specified virtual register is being
249 /// re-materialized.
250 bool isReMaterialized(unsigned virtReg) const {
251 return ReMatMap[virtReg] != NULL;
254 /// @brief returns the original machine instruction being re-issued
255 /// to re-materialize the specified virtual register.
256 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
257 return ReMatMap[virtReg];
260 /// @brief records the specified virtual register will be
261 /// re-materialized and the original instruction which will be re-issed
262 /// for this purpose. If parameter all is true, then all uses of the
263 /// registers are rematerialized and it's safe to delete the definition.
264 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
265 ReMatMap[virtReg] = def;
268 /// @brief record the last use (kill) of a split virtual register.
269 void addKillPoint(unsigned virtReg, unsigned index) {
270 Virt2SplitKillMap[virtReg] = index;
273 unsigned getKillPoint(unsigned virtReg) const {
274 return Virt2SplitKillMap[virtReg];
277 /// @brief remove the last use (kill) of a split virtual register.
278 void removeKillPoint(unsigned virtReg) {
279 Virt2SplitKillMap[virtReg] = 0;
282 /// @brief returns true if the specified MachineInstr is a spill point.
283 bool isSpillPt(MachineInstr *Pt) const {
284 return SpillPt2VirtMap.find(Pt) != SpillPt2VirtMap.end();
287 /// @brief returns the virtual registers that should be spilled due to
288 /// splitting right after the specified MachineInstr.
289 std::vector<std::pair<unsigned,bool> > &getSpillPtSpills(MachineInstr *Pt) {
290 return SpillPt2VirtMap[Pt];
293 /// @brief records the specified MachineInstr as a spill point for virtReg.
294 void addSpillPoint(unsigned virtReg, bool isKill, MachineInstr *Pt) {
295 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
296 I = SpillPt2VirtMap.find(Pt);
297 if (I != SpillPt2VirtMap.end())
298 I->second.push_back(std::make_pair(virtReg, isKill));
299 else {
300 std::vector<std::pair<unsigned,bool> > Virts;
301 Virts.push_back(std::make_pair(virtReg, isKill));
302 SpillPt2VirtMap.insert(std::make_pair(Pt, Virts));
306 /// @brief - transfer spill point information from one instruction to
307 /// another.
308 void transferSpillPts(MachineInstr *Old, MachineInstr *New) {
309 std::map<MachineInstr*, std::vector<std::pair<unsigned,bool> > >::iterator
310 I = SpillPt2VirtMap.find(Old);
311 if (I == SpillPt2VirtMap.end())
312 return;
313 while (!I->second.empty()) {
314 unsigned virtReg = I->second.back().first;
315 bool isKill = I->second.back().second;
316 I->second.pop_back();
317 addSpillPoint(virtReg, isKill, New);
319 SpillPt2VirtMap.erase(I);
322 /// @brief returns true if the specified MachineInstr is a restore point.
323 bool isRestorePt(MachineInstr *Pt) const {
324 return RestorePt2VirtMap.find(Pt) != RestorePt2VirtMap.end();
327 /// @brief returns the virtual registers that should be restoreed due to
328 /// splitting right after the specified MachineInstr.
329 std::vector<unsigned> &getRestorePtRestores(MachineInstr *Pt) {
330 return RestorePt2VirtMap[Pt];
333 /// @brief records the specified MachineInstr as a restore point for virtReg.
334 void addRestorePoint(unsigned virtReg, MachineInstr *Pt) {
335 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
336 RestorePt2VirtMap.find(Pt);
337 if (I != RestorePt2VirtMap.end())
338 I->second.push_back(virtReg);
339 else {
340 std::vector<unsigned> Virts;
341 Virts.push_back(virtReg);
342 RestorePt2VirtMap.insert(std::make_pair(Pt, Virts));
346 /// @brief - transfer restore point information from one instruction to
347 /// another.
348 void transferRestorePts(MachineInstr *Old, MachineInstr *New) {
349 std::map<MachineInstr*, std::vector<unsigned> >::iterator I =
350 RestorePt2VirtMap.find(Old);
351 if (I == RestorePt2VirtMap.end())
352 return;
353 while (!I->second.empty()) {
354 unsigned virtReg = I->second.back();
355 I->second.pop_back();
356 addRestorePoint(virtReg, New);
358 RestorePt2VirtMap.erase(I);
361 /// @brief records that the specified physical register must be spilled
362 /// around the specified machine instr.
363 void addEmergencySpill(unsigned PhysReg, MachineInstr *MI) {
364 if (EmergencySpillMap.find(MI) != EmergencySpillMap.end())
365 EmergencySpillMap[MI].push_back(PhysReg);
366 else {
367 std::vector<unsigned> PhysRegs;
368 PhysRegs.push_back(PhysReg);
369 EmergencySpillMap.insert(std::make_pair(MI, PhysRegs));
373 /// @brief returns true if one or more physical registers must be spilled
374 /// around the specified instruction.
375 bool hasEmergencySpills(MachineInstr *MI) const {
376 return EmergencySpillMap.find(MI) != EmergencySpillMap.end();
379 /// @brief returns the physical registers to be spilled and restored around
380 /// the instruction.
381 std::vector<unsigned> &getEmergencySpills(MachineInstr *MI) {
382 return EmergencySpillMap[MI];
385 /// @brief - transfer emergency spill information from one instruction to
386 /// another.
387 void transferEmergencySpills(MachineInstr *Old, MachineInstr *New) {
388 std::map<MachineInstr*,std::vector<unsigned> >::iterator I =
389 EmergencySpillMap.find(Old);
390 if (I == EmergencySpillMap.end())
391 return;
392 while (!I->second.empty()) {
393 unsigned virtReg = I->second.back();
394 I->second.pop_back();
395 addEmergencySpill(virtReg, New);
397 EmergencySpillMap.erase(I);
400 /// @brief return or get a emergency spill slot for the register class.
401 int getEmergencySpillSlot(const TargetRegisterClass *RC);
403 /// @brief Return lowest spill slot index.
404 int getLowSpillSlot() const {
405 return LowSpillSlot;
408 /// @brief Return highest spill slot index.
409 int getHighSpillSlot() const {
410 return HighSpillSlot;
413 /// @brief Records a spill slot use.
414 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
416 /// @brief Returns true if spill slot has been used.
417 bool isSpillSlotUsed(int FrameIndex) const {
418 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
419 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
422 /// @brief Mark the specified register as being implicitly defined.
423 void setIsImplicitlyDefined(unsigned VirtReg) {
424 ImplicitDefed.set(VirtReg-TargetRegisterInfo::FirstVirtualRegister);
427 /// @brief Returns true if the virtual register is implicitly defined.
428 bool isImplicitlyDefined(unsigned VirtReg) const {
429 return ImplicitDefed[VirtReg-TargetRegisterInfo::FirstVirtualRegister];
432 /// @brief Updates information about the specified virtual register's value
433 /// folded into newMI machine instruction.
434 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, MachineInstr *NewMI,
435 ModRef MRInfo);
437 /// @brief Updates information about the specified virtual register's value
438 /// folded into the specified machine instruction.
439 void virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo);
441 /// @brief returns the virtual registers' values folded in memory
442 /// operands of this instruction
443 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
444 getFoldedVirts(MachineInstr* MI) const {
445 return MI2VirtMap.equal_range(MI);
448 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
449 /// the folded instruction map and spill point map.
450 void RemoveMachineInstrFromMaps(MachineInstr *MI);
452 /// FindUnusedRegisters - Gather a list of allocatable registers that
453 /// have not been allocated to any virtual register.
454 bool FindUnusedRegisters(LiveIntervals* LIs);
456 /// HasUnusedRegisters - Return true if there are any allocatable registers
457 /// that have not been allocated to any virtual register.
458 bool HasUnusedRegisters() const {
459 return !UnusedRegs.none();
462 /// setRegisterUsed - Remember the physical register is now used.
463 void setRegisterUsed(unsigned Reg) {
464 UnusedRegs.reset(Reg);
467 /// isRegisterUnused - Return true if the physical register has not been
468 /// used.
469 bool isRegisterUnused(unsigned Reg) const {
470 return UnusedRegs[Reg];
473 /// getFirstUnusedRegister - Return the first physical register that has not
474 /// been used.
475 unsigned getFirstUnusedRegister(const TargetRegisterClass *RC) {
476 int Reg = UnusedRegs.find_first();
477 while (Reg != -1) {
478 if (allocatableRCRegs[RC][Reg])
479 return (unsigned)Reg;
480 Reg = UnusedRegs.find_next(Reg);
482 return 0;
485 void print(std::ostream &OS, const Module* M = 0) const;
486 void print(std::ostream *OS) const { if (OS) print(*OS); }
487 void print(raw_ostream &OS, const Module* M = 0) const;
488 void print(raw_ostream *OS) const { if (OS) print(*OS); }
489 void dump() const;
492 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
493 VRM.print(OS);
494 return OS;
496 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
497 VRM.print(OS);
498 return OS;
500 inline raw_ostream *operator<<(raw_ostream *OS, const VirtRegMap &VRM) {
501 VRM.print(OS);
502 return OS;
504 inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
505 VRM.print(OS);
506 return OS;
508 } // End llvm namespace
510 #endif