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/IndexedMap.h"
22 #include "llvm/Support/Streams.h"
27 class TargetInstrInfo;
31 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
32 typedef std::multimap<MachineInstr*,
33 std::pair<unsigned, ModRef> > MI2VirtMapTy;
36 const TargetInstrInfo &TII;
39 /// Virt2PhysMap - This is a virtual to physical register
40 /// mapping. Each virtual register is required to have an entry in
41 /// it; even spilled virtual registers (the register mapped to a
42 /// spilled register is the temporary used to load it from the
44 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
45 /// Virt2StackSlotMap - This is virtual register to stack slot
46 /// mapping. Each spilled virtual register has an entry in it
47 /// which corresponds to the stack slot this register is spilled
49 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
50 /// MI2VirtMap - This is MachineInstr to virtual register
51 /// mapping. In the case of memory spill code being folded into
52 /// instructions, we need to know which virtual register was
53 /// read/written by this instruction.
54 MI2VirtMapTy MI2VirtMap;
56 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
57 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
61 NO_STACK_SLOT = ~0 >> 1
65 VirtRegMap(MachineFunction &mf);
69 /// @brief returns true if the specified virtual register is
70 /// mapped to a physical register
71 bool hasPhys(unsigned virtReg) const {
72 return getPhys(virtReg) != NO_PHYS_REG;
75 /// @brief returns the physical register mapped to the specified
77 unsigned getPhys(unsigned virtReg) const {
78 assert(MRegisterInfo::isVirtualRegister(virtReg));
79 return Virt2PhysMap[virtReg];
82 /// @brief creates a mapping for the specified virtual register to
83 /// the specified physical register
84 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
85 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
86 MRegisterInfo::isPhysicalRegister(physReg));
87 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
88 "attempt to assign physical register to already mapped "
90 Virt2PhysMap[virtReg] = physReg;
93 /// @brief clears the specified virtual register's, physical
95 void clearVirt(unsigned virtReg) {
96 assert(MRegisterInfo::isVirtualRegister(virtReg));
97 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
98 "attempt to clear a not assigned virtual register");
99 Virt2PhysMap[virtReg] = NO_PHYS_REG;
102 /// @brief clears all virtual to physical register mappings
103 void clearAllVirt() {
104 Virt2PhysMap.clear();
108 /// @brief returns true is the specified virtual register is
109 /// mapped to a stack slot
110 bool hasStackSlot(unsigned virtReg) const {
111 return getStackSlot(virtReg) != NO_STACK_SLOT;
114 /// @brief returns the stack slot mapped to the specified virtual
116 int getStackSlot(unsigned virtReg) const {
117 assert(MRegisterInfo::isVirtualRegister(virtReg));
118 return Virt2StackSlotMap[virtReg];
121 /// @brief create a mapping for the specifed virtual register to
122 /// the next available stack slot
123 int assignVirt2StackSlot(unsigned virtReg);
124 /// @brief create a mapping for the specified virtual register to
125 /// the specified stack slot
126 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
128 /// @brief Updates information about the specified virtual register's value
129 /// folded into newMI machine instruction. The OpNum argument indicates the
130 /// operand number of OldMI that is folded.
131 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
132 MachineInstr *NewMI);
134 /// @brief returns the virtual registers' values folded in memory
135 /// operands of this instruction
136 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
137 getFoldedVirts(MachineInstr* MI) const {
138 return MI2VirtMap.equal_range(MI);
141 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
142 /// the folded instruction map, remove its entry from the map.
143 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
144 MI2VirtMap.erase(MI);
147 void print(std::ostream &OS) const;
148 void print(std::ostream *OS) const { if (OS) print(*OS); }
152 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
156 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
161 /// Spiller interface: Implementations of this interface assign spilled
162 /// virtual registers to stack slots, rewriting the code.
165 virtual bool runOnMachineFunction(MachineFunction &MF,
166 VirtRegMap &VRM) = 0;
169 /// createSpiller - Create an return a spiller object, as specified on the
171 Spiller* createSpiller();
173 } // End llvm namespace