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
11 // registers to physical registers and virtual registers to stack
12 // slots. It is created and updated by a register allocator and then
13 // used by a machine code rewriter that adds spill code and rewrites
14 // virtual into physical register references.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
19 #define LLVM_CODEGEN_VIRTREGMAP_H
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/SSARegMap.h"
29 typedef std::vector<unsigned> Virt2PhysMap;
30 typedef std::vector<int> Virt2StackSlotMap;
34 NO_STACK_SLOT = INT_MAX
40 Virt2StackSlotMap v2ssMap_;
43 VirtRegMap(const VirtRegMap& rhs);
44 const VirtRegMap& operator=(const VirtRegMap& rhs);
46 static unsigned toIndex(unsigned virtReg) {
47 return virtReg - MRegisterInfo::FirstVirtualRegister;
49 static unsigned fromIndex(unsigned index) {
50 return index + MRegisterInfo::FirstVirtualRegister;
54 VirtRegMap(MachineFunction& mf)
56 v2pMap_(mf.getSSARegMap()->getNumVirtualRegs(), NO_PHYS_REG),
57 v2ssMap_(mf.getSSARegMap()->getNumVirtualRegs(), NO_STACK_SLOT) {
60 unsigned getPhys(unsigned virtReg) const {
61 assert(MRegisterInfo::isVirtualRegister(virtReg));
62 return v2pMap_[toIndex(virtReg)];
65 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
66 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
67 MRegisterInfo::isPhysicalRegister(physReg));
68 assert(v2pMap_[toIndex(virtReg)] == NO_PHYS_REG &&
69 "attempt to assign physical register to already mapped "
71 v2pMap_[toIndex(virtReg)] = physReg;
74 void clearVirtReg(unsigned virtReg) {
75 assert(MRegisterInfo::isVirtualRegister(virtReg));
76 assert(v2pMap_[toIndex(virtReg)] != NO_PHYS_REG &&
77 "attempt to clear a not assigned virtual register");
78 v2pMap_[toIndex(virtReg)] = NO_PHYS_REG;
81 int getStackSlot(unsigned virtReg) const {
82 assert(MRegisterInfo::isVirtualRegister(virtReg));
83 return v2ssMap_[toIndex(virtReg)];
86 int assignVirt2StackSlot(unsigned virtReg);
88 friend std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
91 std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
93 } // End llvm namespace