Be a bit more efficient when processing the active and inactive
[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 #include <map>
26
27 namespace llvm {
28
29     class MachineInstr;
30
31     class VirtRegMap {
32     public:
33         typedef DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
34         typedef DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
35         typedef std::multimap<MachineInstr*, unsigned> MI2VirtMap;
36
37     private:
38         MachineFunction* mf_;
39         Virt2PhysMap v2pMap_;
40         Virt2StackSlotMap v2ssMap_;
41         MI2VirtMap mi2vMap_;
42
43         // do not implement
44         VirtRegMap(const VirtRegMap& rhs);
45         const VirtRegMap& operator=(const VirtRegMap& rhs);
46
47         enum {
48             NO_PHYS_REG   = 0,
49             NO_STACK_SLOT = INT_MAX
50         };
51
52     public:
53         VirtRegMap(MachineFunction& mf)
54             : mf_(&mf),
55               v2pMap_(NO_PHYS_REG),
56               v2ssMap_(NO_STACK_SLOT) {
57             grow();
58         }
59
60         void grow() {
61             v2pMap_.grow(mf_->getSSARegMap()->getLastVirtReg());
62             v2ssMap_.grow(mf_->getSSARegMap()->getLastVirtReg());
63         }
64
65         bool hasPhys(unsigned virtReg) const {
66             return getPhys(virtReg) != NO_PHYS_REG;
67         }
68
69         unsigned getPhys(unsigned virtReg) const {
70             assert(MRegisterInfo::isVirtualRegister(virtReg));
71             return v2pMap_[virtReg];
72         }
73
74         void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
75             assert(MRegisterInfo::isVirtualRegister(virtReg) &&
76                    MRegisterInfo::isPhysicalRegister(physReg));
77             assert(v2pMap_[virtReg] == NO_PHYS_REG &&
78                    "attempt to assign physical register to already mapped "
79                    "virtual register");
80             v2pMap_[virtReg] = physReg;
81         }
82
83         void clearVirt(unsigned virtReg) {
84             assert(MRegisterInfo::isVirtualRegister(virtReg));
85             assert(v2pMap_[virtReg] != NO_PHYS_REG &&
86                    "attempt to clear a not assigned virtual register");
87             v2pMap_[virtReg] = NO_PHYS_REG;
88         }
89
90         void clearAllVirt() {
91             v2pMap_.clear();
92             grow();
93         }
94
95         bool hasStackSlot(unsigned virtReg) const {
96             return getStackSlot(virtReg) != NO_STACK_SLOT;
97         }
98
99         int getStackSlot(unsigned virtReg) const {
100             assert(MRegisterInfo::isVirtualRegister(virtReg));
101             return v2ssMap_[virtReg];
102         }
103
104         int assignVirt2StackSlot(unsigned virtReg);
105         void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
106
107         void virtFolded(unsigned virtReg,
108                         MachineInstr* oldMI,
109                         MachineInstr* newMI);
110
111         std::pair<MI2VirtMap::const_iterator, MI2VirtMap::const_iterator>
112         getFoldedVirts(MachineInstr* MI) const {
113             return mi2vMap_.equal_range(MI);
114         }
115
116         friend std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
117     };
118
119     std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
120
121     struct Spiller {
122         virtual ~Spiller();
123
124         virtual bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap& vrm) = 0;
125
126     };
127
128     Spiller* createSpiller();
129
130 } // End llvm namespace
131
132 #endif