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 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
30 typedef std::multimap<MachineInstr*,
31 std::pair<unsigned, ModRef> > MI2VirtMapTy;
35 /// Virt2PhysMap - This is a virtual to physical register
36 /// mapping. Each virtual register is required to have an entry in
37 /// it; even spilled virtual registers (the register mapped to a
38 /// spilled register is the temporary used to load it from the
40 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
41 /// Virt2StackSlotMap - This is virtual register to stack slot
42 /// mapping. Each spilled virtual register has an entry in it
43 /// which corresponds to the stack slot this register is spilled
45 DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
46 /// MI2VirtMap - This is MachineInstr to virtual register
47 /// mapping. In the case of memory spill code being folded into
48 /// instructions, we need to know which virtual register was
49 /// read/written by this instruction.
50 MI2VirtMapTy MI2VirtMap;
52 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
53 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
57 NO_STACK_SLOT = ~0 >> 1
61 VirtRegMap(MachineFunction &mf)
62 : MF(mf), Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) {
68 /// @brief returns true if the specified virtual register is
69 /// mapped to a physical register
70 bool hasPhys(unsigned virtReg) const {
71 return getPhys(virtReg) != NO_PHYS_REG;
74 /// @brief returns the physical register mapped to the specified
76 unsigned getPhys(unsigned virtReg) const {
77 assert(MRegisterInfo::isVirtualRegister(virtReg));
78 return Virt2PhysMap[virtReg];
81 /// @brief creates a mapping for the specified virtual register to
82 /// the specified physical register
83 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
84 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
85 MRegisterInfo::isPhysicalRegister(physReg));
86 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
87 "attempt to assign physical register to already mapped "
89 Virt2PhysMap[virtReg] = physReg;
92 /// @brief clears the specified virtual register's, physical
94 void clearVirt(unsigned virtReg) {
95 assert(MRegisterInfo::isVirtualRegister(virtReg));
96 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
97 "attempt to clear a not assigned virtual register");
98 Virt2PhysMap[virtReg] = NO_PHYS_REG;
101 /// @brief clears all virtual to physical register mappings
102 void clearAllVirt() {
103 Virt2PhysMap.clear();
107 /// @brief returns true is the specified virtual register is
108 /// mapped to a stack slot
109 bool hasStackSlot(unsigned virtReg) const {
110 return getStackSlot(virtReg) != NO_STACK_SLOT;
113 /// @brief returns the stack slot mapped to the specified virtual
115 int getStackSlot(unsigned virtReg) const {
116 assert(MRegisterInfo::isVirtualRegister(virtReg));
117 return Virt2StackSlotMap[virtReg];
120 /// @brief create a mapping for the specifed virtual register to
121 /// the next available stack slot
122 int assignVirt2StackSlot(unsigned virtReg);
123 /// @brief create a mapping for the specified virtual register to
124 /// the specified stack slot
125 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
127 /// @brief Updates information about the specified virtual register's value
128 /// folded into newMI machine instruction. The OpNum argument indicates the
129 /// operand number of OldMI that is folded.
130 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
131 MachineInstr *NewMI);
133 /// @brief returns the virtual registers' values folded in memory
134 /// operands of this instruction
135 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
136 getFoldedVirts(MachineInstr* MI) const {
137 return MI2VirtMap.equal_range(MI);
140 void print(std::ostream &OS) const;
144 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
149 /// Spiller interface: Implementations of this interface assign spilled
150 /// virtual registers to stack slots, rewriting the code.
153 virtual bool runOnMachineFunction(MachineFunction &MF,
154 const VirtRegMap &VRM) = 0;
157 /// createSpiller - Create an return a spiller object, as specified on the
159 Spiller* createSpiller();
161 } // End llvm namespace