da20d4ae114b6a25c2cb1f49faa8b3453268a45b
[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 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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_VIRTREGMAP_H
18 #define LLVM_CODEGEN_VIRTREGMAP_H
19
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/IndexedMap.h"
23 #include "llvm/Support/Streams.h"
24 #include <map>
25
26 namespace llvm {
27   class MachineInstr;
28   class MachineFunction;
29   class TargetInstrInfo;
30
31   class VirtRegMap {
32   public:
33     enum {
34       NO_PHYS_REG = 0,
35       NO_STACK_SLOT = (1L << 30)-1,
36       MAX_STACK_SLOT = (1L << 18)-1
37     };
38
39     enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
40     typedef std::multimap<MachineInstr*,
41                           std::pair<unsigned, ModRef> > MI2VirtMapTy;
42
43   private:
44     const TargetInstrInfo &TII;
45
46     MachineFunction &MF;
47     /// Virt2PhysMap - This is a virtual to physical register
48     /// mapping. Each virtual register is required to have an entry in
49     /// it; even spilled virtual registers (the register mapped to a
50     /// spilled register is the temporary used to load it from the
51     /// stack).
52     IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
53     /// Virt2StackSlotMap - This is virtual register to stack slot
54     /// mapping. Each spilled virtual register has an entry in it
55     /// which corresponds to the stack slot this register is spilled
56     /// at.
57     IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
58     /// MI2VirtMap - This is MachineInstr to virtual register
59     /// mapping. In the case of memory spill code being folded into
60     /// instructions, we need to know which virtual register was
61     /// read/written by this instruction.
62     MI2VirtMapTy MI2VirtMap;
63
64     /// ReMatMap - This is virtual register to re-materialized instruction
65     /// mapping. Each virtual register whose definition is going to be
66     /// re-materialized has an entry in it.
67     std::map<unsigned, const MachineInstr*> ReMatMap;
68
69     /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
70     /// virtual register, an unique id is being assigned. This keeps track of
71     /// the highest id used so far. Note, this starts at (1<<18) to avoid
72     /// conflicts with stack slot numbers.
73     int ReMatId;
74
75     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
76     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
77
78   public:
79     explicit VirtRegMap(MachineFunction &mf);
80
81     void grow();
82
83     /// @brief returns true if the specified virtual register is
84     /// mapped to a physical register
85     bool hasPhys(unsigned virtReg) const {
86       return getPhys(virtReg) != NO_PHYS_REG;
87     }
88
89     /// @brief returns the physical register mapped to the specified
90     /// virtual register
91     unsigned getPhys(unsigned virtReg) const {
92       assert(MRegisterInfo::isVirtualRegister(virtReg));
93       return Virt2PhysMap[virtReg];
94     }
95
96     /// @brief creates a mapping for the specified virtual register to
97     /// the specified physical register
98     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
99       assert(MRegisterInfo::isVirtualRegister(virtReg) &&
100              MRegisterInfo::isPhysicalRegister(physReg));
101       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
102              "attempt to assign physical register to already mapped "
103              "virtual register");
104       Virt2PhysMap[virtReg] = physReg;
105     }
106
107     /// @brief clears the specified virtual register's, physical
108     /// register mapping
109     void clearVirt(unsigned virtReg) {
110       assert(MRegisterInfo::isVirtualRegister(virtReg));
111       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
112              "attempt to clear a not assigned virtual register");
113       Virt2PhysMap[virtReg] = NO_PHYS_REG;
114     }
115
116     /// @brief clears all virtual to physical register mappings
117     void clearAllVirt() {
118       Virt2PhysMap.clear();
119       grow();
120     }
121
122     /// @brief returns true is the specified virtual register is
123     /// mapped to a stack slot
124     bool hasStackSlot(unsigned virtReg) const {
125       return getStackSlot(virtReg) != NO_STACK_SLOT;
126     }
127
128     /// @brief returns the stack slot mapped to the specified virtual
129     /// register
130     int getStackSlot(unsigned virtReg) const {
131       assert(MRegisterInfo::isVirtualRegister(virtReg));
132       return Virt2StackSlotMap[virtReg];
133     }
134
135     /// @brief create a mapping for the specifed virtual register to
136     /// the next available stack slot
137     int assignVirt2StackSlot(unsigned virtReg);
138     /// @brief create a mapping for the specified virtual register to
139     /// the specified stack slot
140     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
141
142     /// @brief assign an unique re-materialization id to the specified
143     /// virtual register.
144     int assignVirtReMatId(unsigned virtReg);
145
146     /// @brief returns true if the specified virtual register is being
147     /// re-materialized.
148     bool isReMaterialized(unsigned virtReg) const {
149       return ReMatMap.count(virtReg) != 0;
150     }
151
152     /// @brief returns the original machine instruction being re-issued
153     /// to re-materialize the specified virtual register.
154     const MachineInstr *getReMaterializedMI(unsigned virtReg) {
155       return ReMatMap[virtReg];
156     }
157
158     /// @brief records the specified virtual register will be
159     /// re-materialized and the original instruction which will be re-issed
160     /// for this purpose.
161     void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
162       ReMatMap[virtReg] = def;
163     }
164
165     /// @brief Updates information about the specified virtual register's value
166     /// folded into newMI machine instruction.  The OpNum argument indicates the
167     /// operand number of OldMI that is folded.
168     void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
169                     MachineInstr *NewMI);
170
171     /// @brief returns the virtual registers' values folded in memory
172     /// operands of this instruction
173     std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
174     getFoldedVirts(MachineInstr* MI) const {
175       return MI2VirtMap.equal_range(MI);
176     }
177     
178     /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
179     /// the folded instruction map, remove its entry from the map.
180     void RemoveFromFoldedVirtMap(MachineInstr *MI) {
181       MI2VirtMap.erase(MI);
182     }
183
184     void print(std::ostream &OS) const;
185     void print(std::ostream *OS) const { if (OS) print(*OS); }
186     void dump() const;
187   };
188
189   inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
190     VRM.print(OS);
191     return OS;
192   }
193   inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
194     VRM.print(OS);
195     return OS;
196   }
197
198   /// Spiller interface: Implementations of this interface assign spilled
199   /// virtual registers to stack slots, rewriting the code.
200   struct Spiller {
201     virtual ~Spiller();
202     virtual bool runOnMachineFunction(MachineFunction &MF,
203                                       VirtRegMap &VRM) = 0;
204   };
205
206   /// createSpiller - Create an return a spiller object, as specified on the
207   /// command line.
208   Spiller* createSpiller();
209
210 } // End llvm namespace
211
212 #endif