1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/MRegisterInfo.h"
21 #include "llvm/ADT/DenseMap.h"
29 typedef std::multimap<MachineInstr*, unsigned> MI2VirtMapTy;
33 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
34 DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
35 MI2VirtMapTy MI2VirtMap;
37 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
38 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
42 NO_STACK_SLOT = ~0 >> 1
46 VirtRegMap(MachineFunction &mf)
47 : MF(mf), Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) {
53 bool hasPhys(unsigned virtReg) const {
54 return getPhys(virtReg) != NO_PHYS_REG;
57 unsigned getPhys(unsigned virtReg) const {
58 assert(MRegisterInfo::isVirtualRegister(virtReg));
59 return Virt2PhysMap[virtReg];
62 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
63 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
64 MRegisterInfo::isPhysicalRegister(physReg));
65 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
66 "attempt to assign physical register to already mapped "
68 Virt2PhysMap[virtReg] = physReg;
71 void clearVirt(unsigned virtReg) {
72 assert(MRegisterInfo::isVirtualRegister(virtReg));
73 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
74 "attempt to clear a not assigned virtual register");
75 Virt2PhysMap[virtReg] = NO_PHYS_REG;
83 bool hasStackSlot(unsigned virtReg) const {
84 return getStackSlot(virtReg) != NO_STACK_SLOT;
87 int getStackSlot(unsigned virtReg) const {
88 assert(MRegisterInfo::isVirtualRegister(virtReg));
89 return Virt2StackSlotMap[virtReg];
92 int assignVirt2StackSlot(unsigned virtReg);
93 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
95 void virtFolded(unsigned virtReg, MachineInstr* oldMI,
98 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
99 getFoldedVirts(MachineInstr* MI) const {
100 return MI2VirtMap.equal_range(MI);
103 void print(std::ostream &OS) const;
107 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
112 /// Spiller interface: Implementations of this interface assign spilled
113 /// virtual registers to stack slots, rewriting the code.
116 virtual bool runOnMachineFunction(MachineFunction &MF,
117 const VirtRegMap &VRM) = 0;
120 /// createSpiller - Create an return a spiller object, as specified on the
122 Spiller* createSpiller();
124 } // End llvm namespace