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 /// Virt2PhysMap - This is a virtual to physical register
34 /// mapping. Each virtual register is required to have an entry in
35 /// it; even spilled virtual registers (the register mapped to a
36 /// spilled register is the temporary used to load it from the
38 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
39 /// Virt2StackSlotMap - This is virtual register to stack slot
40 /// mapping. Each spilled virtual register has an entry in it
41 /// which corresponds to the stack slot this register is spilled
43 DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
44 /// MI2VirtMap - This is MachineInstr to virtual register
45 /// mapping. In the case of memory spill code being folded into
46 /// instructions, we need to know which virtual register was
47 /// read/written by this instruction.
48 MI2VirtMapTy MI2VirtMap;
50 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
51 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
55 NO_STACK_SLOT = ~0 >> 1
59 VirtRegMap(MachineFunction &mf)
60 : MF(mf), Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) {
66 /// @brief returns true if the specified virtual register is
67 /// mapped to a physical register
68 bool hasPhys(unsigned virtReg) const {
69 return getPhys(virtReg) != NO_PHYS_REG;
72 /// @brief returns the physical register mapped to the specified
74 unsigned getPhys(unsigned virtReg) const {
75 assert(MRegisterInfo::isVirtualRegister(virtReg));
76 return Virt2PhysMap[virtReg];
79 /// @brief creates a mapping for the specified virtual register to
80 /// the specified physical register
81 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
82 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
83 MRegisterInfo::isPhysicalRegister(physReg));
84 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
85 "attempt to assign physical register to already mapped "
87 Virt2PhysMap[virtReg] = physReg;
90 /// @brief clears the specified virtual register's, physical
92 void clearVirt(unsigned virtReg) {
93 assert(MRegisterInfo::isVirtualRegister(virtReg));
94 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
95 "attempt to clear a not assigned virtual register");
96 Virt2PhysMap[virtReg] = NO_PHYS_REG;
99 /// @brief clears all virtual to physical register mappings
100 void clearAllVirt() {
101 Virt2PhysMap.clear();
105 /// @brief returns true is the specified virtual register is
106 /// mapped to a stack slot
107 bool hasStackSlot(unsigned virtReg) const {
108 return getStackSlot(virtReg) != NO_STACK_SLOT;
111 /// @brief returns the stack slot mapped to the specified virtual
113 int getStackSlot(unsigned virtReg) const {
114 assert(MRegisterInfo::isVirtualRegister(virtReg));
115 return Virt2StackSlotMap[virtReg];
118 /// @brief create a mapping for the specifed virtual register to
119 /// the next available stack slot
120 int assignVirt2StackSlot(unsigned virtReg);
121 /// @brief create a mapping for the specified virtual register to
122 /// the specified stack slot
123 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
125 /// @brief updates information about the specified virtual
126 /// register's value folded into newMI machine instruction
127 void virtFolded(unsigned virtReg, MachineInstr* oldMI,
128 MachineInstr* newMI);
130 /// @brief returns the virtual registers' values folded in memory
131 /// operands of this instruction
132 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
133 getFoldedVirts(MachineInstr* MI) const {
134 return MI2VirtMap.equal_range(MI);
137 void print(std::ostream &OS) const;
141 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
146 /// Spiller interface: Implementations of this interface assign spilled
147 /// virtual registers to stack slots, rewriting the code.
150 virtual bool runOnMachineFunction(MachineFunction &MF,
151 const VirtRegMap &VRM) = 0;
154 /// createSpiller - Create an return a spiller object, as specified on the
156 Spiller* createSpiller();
158 } // End llvm namespace