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