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