1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include "llvm/ADT/BitVector.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/IndexedMap.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
33 class MachineFunction;
34 class MachineRegisterInfo;
35 class TargetInstrInfo;
36 class TargetRegisterInfo;
40 class VirtRegMap : public MachineFunctionPass {
44 NO_STACK_SLOT = (1L << 30)-1,
45 MAX_STACK_SLOT = (1L << 18)-1
48 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
49 typedef std::multimap<MachineInstr*,
50 std::pair<unsigned, ModRef> > MI2VirtMapTy;
53 MachineRegisterInfo *MRI;
54 const TargetInstrInfo *TII;
55 const TargetRegisterInfo *TRI;
58 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
60 /// Virt2PhysMap - This is a virtual to physical register
61 /// mapping. Each virtual register is required to have an entry in
62 /// it; even spilled virtual registers (the register mapped to a
63 /// spilled register is the temporary used to load it from the
65 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
67 /// Virt2StackSlotMap - This is virtual register to stack slot
68 /// mapping. Each spilled virtual register has an entry in it
69 /// which corresponds to the stack slot this register is spilled
71 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
73 /// Virt2SplitMap - This is virtual register to splitted virtual register
75 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
77 /// createSpillSlot - Allocate a spill slot for RC from MFI.
78 unsigned createSpillSlot(const TargetRegisterClass *RC);
80 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
81 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
85 VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
86 Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0) { }
87 virtual bool runOnMachineFunction(MachineFunction &MF);
89 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
91 MachineFunctionPass::getAnalysisUsage(AU);
94 MachineFunction &getMachineFunction() const {
95 assert(MF && "getMachineFunction called before runOnMachineFunction");
99 MachineRegisterInfo &getRegInfo() const { return *MRI; }
100 const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
104 /// @brief returns true if the specified virtual register is
105 /// mapped to a physical register
106 bool hasPhys(unsigned virtReg) const {
107 return getPhys(virtReg) != NO_PHYS_REG;
110 /// @brief returns the physical register mapped to the specified
112 unsigned getPhys(unsigned virtReg) const {
113 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
114 return Virt2PhysMap[virtReg];
117 /// @brief creates a mapping for the specified virtual register to
118 /// the specified physical register
119 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
120 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
121 TargetRegisterInfo::isPhysicalRegister(physReg));
122 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
123 "attempt to assign physical register to already mapped "
125 Virt2PhysMap[virtReg] = physReg;
128 /// @brief clears the specified virtual register's, physical
130 void clearVirt(unsigned virtReg) {
131 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
132 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
133 "attempt to clear a not assigned virtual register");
134 Virt2PhysMap[virtReg] = NO_PHYS_REG;
137 /// @brief clears all virtual to physical register mappings
138 void clearAllVirt() {
139 Virt2PhysMap.clear();
143 /// @brief returns the register allocation preference.
144 unsigned getRegAllocPref(unsigned virtReg);
146 /// @brief returns true if VirtReg is assigned to its preferred physreg.
147 bool hasPreferredPhys(unsigned VirtReg) {
148 return getPhys(VirtReg) == getRegAllocPref(VirtReg);
151 /// @brief records virtReg is a split live interval from SReg.
152 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
153 Virt2SplitMap[virtReg] = SReg;
156 /// @brief returns the live interval virtReg is split from.
157 unsigned getPreSplitReg(unsigned virtReg) const {
158 return Virt2SplitMap[virtReg];
161 /// getOriginal - Return the original virtual register that VirtReg descends
162 /// from through splitting.
163 /// A register that was not created by splitting is its own original.
164 /// This operation is idempotent.
165 unsigned getOriginal(unsigned VirtReg) const {
166 unsigned Orig = getPreSplitReg(VirtReg);
167 return Orig ? Orig : VirtReg;
170 /// @brief returns true if the specified virtual register is not
171 /// mapped to a stack slot or rematerialized.
172 bool isAssignedReg(unsigned virtReg) const {
173 if (getStackSlot(virtReg) == NO_STACK_SLOT)
175 // Split register can be assigned a physical register as well as a
176 // stack slot or remat id.
177 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
180 /// @brief returns the stack slot mapped to the specified virtual
182 int getStackSlot(unsigned virtReg) const {
183 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
184 return Virt2StackSlotMap[virtReg];
187 /// @brief create a mapping for the specifed virtual register to
188 /// the next available stack slot
189 int assignVirt2StackSlot(unsigned virtReg);
190 /// @brief create a mapping for the specified virtual register to
191 /// the specified stack slot
192 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
194 /// rewrite - Rewrite all instructions in MF to use only physical registers
195 /// by mapping all virtual register operands to their assigned physical
198 /// @param Indexes Optionally remove deleted instructions from indexes.
199 void rewrite(SlotIndexes *Indexes);
201 void print(raw_ostream &OS, const Module* M = 0) const;
205 inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
209 } // End llvm namespace