LinearScanner hotspot.
[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/DenseMap.h"
22 #include <map>
23
24 namespace llvm {
25   class MachineInstr;
26   class TargetInstrInfo;
27
28   class VirtRegMap {
29   public:
30     enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
31     typedef std::multimap<MachineInstr*,
32                           std::pair<unsigned, ModRef> > MI2VirtMapTy;
33
34   private:
35     const TargetInstrInfo &TII;
36
37     MachineFunction &MF;
38     /// Virt2PhysMap - This is a virtual to physical register
39     /// mapping. Each virtual register is required to have an entry in
40     /// it; even spilled virtual registers (the register mapped to a
41     /// spilled register is the temporary used to load it from the
42     /// stack).
43     DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
44     /// Virt2StackSlotMap - This is virtual register to stack slot
45     /// mapping. Each spilled virtual register has an entry in it
46     /// which corresponds to the stack slot this register is spilled
47     /// at.
48     DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
49     /// MI2VirtMap - This is MachineInstr to virtual register
50     /// mapping. In the case of memory spill code being folded into
51     /// instructions, we need to know which virtual register was
52     /// read/written by this instruction.
53     MI2VirtMapTy MI2VirtMap;
54
55     VirtRegMap(const VirtRegMap&);     // DO NOT IMPLEMENT
56     void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
57
58     enum {
59       NO_PHYS_REG = 0,
60       NO_STACK_SLOT = ~0 >> 1
61     };
62
63   public:
64     VirtRegMap(MachineFunction &mf);
65
66     void grow();
67
68     /// @brief returns true if the specified virtual register is
69     /// mapped to a physical register
70     bool hasPhys(unsigned virtReg) const {
71       return getPhys(virtReg) != NO_PHYS_REG;
72     }
73
74     /// @brief returns the physical register mapped to the specified
75     /// virtual register
76     unsigned getPhys(unsigned virtReg) const {
77       assert(MRegisterInfo::isVirtualRegister(virtReg));
78       return Virt2PhysMap[virtReg];
79     }
80
81     /// @brief creates a mapping for the specified virtual register to
82     /// the specified physical register
83     void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
84       assert(MRegisterInfo::isVirtualRegister(virtReg) &&
85              MRegisterInfo::isPhysicalRegister(physReg));
86       assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
87              "attempt to assign physical register to already mapped "
88              "virtual register");
89       Virt2PhysMap[virtReg] = physReg;
90     }
91
92     /// @brief clears the specified virtual register's, physical
93     /// register mapping
94     void clearVirt(unsigned virtReg) {
95       assert(MRegisterInfo::isVirtualRegister(virtReg));
96       assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
97              "attempt to clear a not assigned virtual register");
98       Virt2PhysMap[virtReg] = NO_PHYS_REG;
99     }
100
101     /// @brief clears all virtual to physical register mappings
102     void clearAllVirt() {
103       Virt2PhysMap.clear();
104       grow();
105     }
106
107     /// @brief returns true is the specified virtual register is
108     /// mapped to a stack slot
109     bool hasStackSlot(unsigned virtReg) const {
110       return getStackSlot(virtReg) != NO_STACK_SLOT;
111     }
112
113     /// @brief returns the stack slot mapped to the specified virtual
114     /// register
115     int getStackSlot(unsigned virtReg) const {
116       assert(MRegisterInfo::isVirtualRegister(virtReg));
117       return Virt2StackSlotMap[virtReg];
118     }
119
120     /// @brief create a mapping for the specifed virtual register to
121     /// the next available stack slot
122     int assignVirt2StackSlot(unsigned virtReg);
123     /// @brief create a mapping for the specified virtual register to
124     /// the specified stack slot
125     void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
126
127     /// @brief Updates information about the specified virtual register's value
128     /// folded into newMI machine instruction.  The OpNum argument indicates the
129     /// operand number of OldMI that is folded.
130     void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
131                     MachineInstr *NewMI);
132
133     /// @brief returns the virtual registers' values folded in memory
134     /// operands of this instruction
135     std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
136     getFoldedVirts(MachineInstr* MI) const {
137       return MI2VirtMap.equal_range(MI);
138     }
139     
140     /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
141     /// the folded instruction map, remove its entry from the map.
142     void RemoveFromFoldedVirtMap(MachineInstr *MI) {
143       MI2VirtMap.erase(MI);
144     }
145
146     void print(std::ostream &OS) const;
147     void dump() const;
148   };
149
150   inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
151     VRM.print(OS);
152     return OS;
153   }
154
155   /// Spiller interface: Implementations of this interface assign spilled
156   /// virtual registers to stack slots, rewriting the code.
157   struct Spiller {
158     virtual ~Spiller();
159     virtual bool runOnMachineFunction(MachineFunction &MF,
160                                       VirtRegMap &VRM) = 0;
161   };
162
163   /// createSpiller - Create an return a spiller object, as specified on the
164   /// command line.
165   Spiller* createSpiller();
166
167 } // End llvm namespace
168
169 #endif