3c991abb62e39104d6d707ec5952357cd754815c
[oota-llvm.git] / lib / CodeGen / VirtRegMap.h
1 //===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
19 #define LLVM_CODEGEN_VIRTREGMAP_H
20
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/SSARegMap.h"
23 #include "Support/DenseMap.h"
24 #include <climits>
25
26 namespace llvm {
27
28     class VirtRegMap {
29     public:
30         typedef DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
31         typedef DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
32
33     private:
34         MachineFunction* mf_;
35         Virt2PhysMap v2pMap_;
36         Virt2StackSlotMap v2ssMap_;
37
38         // do not implement
39         VirtRegMap(const VirtRegMap& rhs);
40         const VirtRegMap& operator=(const VirtRegMap& rhs);
41
42         enum {
43             NO_PHYS_REG   = 0,
44             NO_STACK_SLOT = INT_MAX
45         };
46
47     public:
48         VirtRegMap(MachineFunction& mf)
49             : mf_(&mf),
50               v2pMap_(NO_PHYS_REG),
51               v2ssMap_(NO_STACK_SLOT) {
52             v2pMap_.grow(mf.getSSARegMap()->getLastVirtReg());
53             v2ssMap_.grow(mf.getSSARegMap()->getLastVirtReg());
54         }
55
56         bool hasPhys(unsigned virtReg) const {
57             return getPhys(virtReg) != NO_PHYS_REG;
58         }
59
60         unsigned getPhys(unsigned virtReg) const {
61             assert(MRegisterInfo::isVirtualRegister(virtReg));
62             return v2pMap_[virtReg];
63         }
64
65         void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
66             assert(MRegisterInfo::isVirtualRegister(virtReg) &&
67                    MRegisterInfo::isPhysicalRegister(physReg));
68             assert(v2pMap_[virtReg] == NO_PHYS_REG &&
69                    "attempt to assign physical register to already mapped "
70                    "virtual register");
71             v2pMap_[virtReg] = physReg;
72         }
73
74         void clearVirtReg(unsigned virtReg) {
75             assert(MRegisterInfo::isVirtualRegister(virtReg));
76             assert(v2pMap_[virtReg] != NO_PHYS_REG &&
77                    "attempt to clear a not assigned virtual register");
78             v2pMap_[virtReg] = NO_PHYS_REG;
79         }
80
81         bool hasStackSlot(unsigned virtReg) const {
82             return getStackSlot(virtReg) != NO_STACK_SLOT;
83         }
84
85         int getStackSlot(unsigned virtReg) const {
86             assert(MRegisterInfo::isVirtualRegister(virtReg));
87             return v2ssMap_[virtReg];
88         }
89
90         int assignVirt2StackSlot(unsigned virtReg);
91
92         friend std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
93     };
94
95     std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
96
97     void eliminateVirtRegs(MachineFunction& mf, const VirtRegMap& vrm);
98
99 } // End llvm namespace
100
101 #endif