Enable rematerialization of constants using AliasAnalysis::pointsToConstantMemory,
[oota-llvm.git] / include / llvm / CodeGen / MachineRegisterInfo.h
1 //===-- llvm/CodeGen/MachineRegisterInfo.h ----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MachineRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
15 #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
16
17 #include "llvm/Target/TargetRegisterInfo.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/iterator.h"
20 #include <vector>
21
22 namespace llvm {
23   
24 /// MachineRegisterInfo - Keep track of information for virtual and physical
25 /// registers, including vreg register classes, use/def chains for registers,
26 /// etc.
27 class MachineRegisterInfo {
28   /// VRegInfo - Information we keep for each virtual register.  The entries in
29   /// this vector are actually converted to vreg numbers by adding the 
30   /// TargetRegisterInfo::FirstVirtualRegister delta to their index.
31   ///
32   /// Each element in this list contains the register class of the vreg and the
33   /// start of the use/def list for the register.
34   std::vector<std::pair<const TargetRegisterClass*, MachineOperand*> > VRegInfo;
35   
36   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
37   /// physical registers.
38   MachineOperand **PhysRegUseDefLists; 
39   
40   /// UsedPhysRegs - This is a bit vector that is computed and set by the
41   /// register allocator, and must be kept up to date by passes that run after
42   /// register allocation (though most don't modify this).  This is used
43   /// so that the code generator knows which callee save registers to save and
44   /// for other target specific uses.
45   BitVector UsedPhysRegs;
46   
47   /// LiveIns/LiveOuts - Keep track of the physical registers that are
48   /// livein/liveout of the function.  Live in values are typically arguments in
49   /// registers, live out values are typically return values in registers.
50   /// LiveIn values are allowed to have virtual registers associated with them,
51   /// stored in the second element.
52   std::vector<std::pair<unsigned, unsigned> > LiveIns;
53   std::vector<unsigned> LiveOuts;
54   
55   MachineRegisterInfo(const MachineRegisterInfo&); // DO NOT IMPLEMENT
56   void operator=(const MachineRegisterInfo&);      // DO NOT IMPLEMENT
57 public:
58   explicit MachineRegisterInfo(const TargetRegisterInfo &TRI);
59   ~MachineRegisterInfo();
60   
61   //===--------------------------------------------------------------------===//
62   // Register Info
63   //===--------------------------------------------------------------------===//
64
65   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
66   /// and uses of a register within the MachineFunction that corresponds to this
67   /// MachineRegisterInfo object.
68   template<bool Uses, bool Defs>
69   class defusechain_iterator;
70
71   /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
72   /// register.
73   typedef defusechain_iterator<true,true> reg_iterator;
74   reg_iterator reg_begin(unsigned RegNo) const {
75     return reg_iterator(getRegUseDefListHead(RegNo));
76   }
77   static reg_iterator reg_end() { return reg_iterator(0); }
78
79   /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
80   typedef defusechain_iterator<false,true> def_iterator;
81   def_iterator def_begin(unsigned RegNo) const {
82     return def_iterator(getRegUseDefListHead(RegNo));
83   }
84   static def_iterator def_end() { return def_iterator(0); }
85
86   /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
87   typedef defusechain_iterator<true,false> use_iterator;
88   use_iterator use_begin(unsigned RegNo) const {
89     return use_iterator(getRegUseDefListHead(RegNo));
90   }
91   static use_iterator use_end() { return use_iterator(0); }
92   
93   /// use_empty - Return true if there are no instructions using the specified
94   /// register.
95   bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
96
97   
98   /// replaceRegWith - Replace all instances of FromReg with ToReg in the
99   /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
100   /// except that it also changes any definitions of the register as well.
101   void replaceRegWith(unsigned FromReg, unsigned ToReg);
102   
103   /// getRegUseDefListHead - Return the head pointer for the register use/def
104   /// list for the specified virtual or physical register.
105   MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
106     if (RegNo < TargetRegisterInfo::FirstVirtualRegister)
107       return PhysRegUseDefLists[RegNo];
108     RegNo -= TargetRegisterInfo::FirstVirtualRegister;
109     return VRegInfo[RegNo].second;
110   }
111   
112   MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
113     if (RegNo < TargetRegisterInfo::FirstVirtualRegister)
114       return PhysRegUseDefLists[RegNo];
115     RegNo -= TargetRegisterInfo::FirstVirtualRegister;
116     return VRegInfo[RegNo].second;
117   }
118
119   /// getVRegDef - Return the machine instr that defines the specified virtual
120   /// register or null if none is found.  This assumes that the code is in SSA
121   /// form, so there should only be one definition.
122   MachineInstr *getVRegDef(unsigned Reg) const;
123   
124 #ifndef NDEBUG
125   void dumpUses(unsigned RegNo) const;
126 #endif
127   
128   //===--------------------------------------------------------------------===//
129   // Virtual Register Info
130   //===--------------------------------------------------------------------===//
131   
132   /// getRegClass - Return the register class of the specified virtual register.
133   const TargetRegisterClass *getRegClass(unsigned Reg) const {
134     Reg -= TargetRegisterInfo::FirstVirtualRegister;
135     assert(Reg < VRegInfo.size() && "Invalid vreg!");
136     return VRegInfo[Reg].first;
137   }
138
139   /// setRegClass - Set the register class of the specified virtual register.
140   void setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
141     Reg -= TargetRegisterInfo::FirstVirtualRegister;
142     assert(Reg < VRegInfo.size() && "Invalid vreg!");
143     VRegInfo[Reg].first = RC;
144   }
145   
146   /// createVirtualRegister - Create and return a new virtual register in the
147   /// function with the specified register class.
148   ///
149   unsigned createVirtualRegister(const TargetRegisterClass *RegClass) {
150     assert(RegClass && "Cannot create register without RegClass!");
151     // Add a reg, but keep track of whether the vector reallocated or not.
152     void *ArrayBase = VRegInfo.empty() ? 0 : &VRegInfo[0];
153     VRegInfo.push_back(std::make_pair(RegClass, (MachineOperand*)0));
154     
155     if (&VRegInfo[0] == ArrayBase || VRegInfo.size() == 1)
156       return getLastVirtReg();
157
158     // Otherwise, the vector reallocated, handle this now.
159     HandleVRegListReallocation();
160     return getLastVirtReg();
161   }
162
163   /// getLastVirtReg - Return the highest currently assigned virtual register.
164   ///
165   unsigned getLastVirtReg() const {
166     return (unsigned)VRegInfo.size()+TargetRegisterInfo::FirstVirtualRegister-1;
167   }
168   
169   
170   //===--------------------------------------------------------------------===//
171   // Physical Register Use Info
172   //===--------------------------------------------------------------------===//
173   
174   /// isPhysRegUsed - Return true if the specified register is used in this
175   /// function.  This only works after register allocation.
176   bool isPhysRegUsed(unsigned Reg) const { return UsedPhysRegs[Reg]; }
177   
178   /// setPhysRegUsed - Mark the specified register used in this function.
179   /// This should only be called during and after register allocation.
180   void setPhysRegUsed(unsigned Reg) { UsedPhysRegs[Reg] = true; }
181   
182   /// setPhysRegUnused - Mark the specified register unused in this function.
183   /// This should only be called during and after register allocation.
184   void setPhysRegUnused(unsigned Reg) { UsedPhysRegs[Reg] = false; }
185   
186
187   //===--------------------------------------------------------------------===//
188   // LiveIn/LiveOut Management
189   //===--------------------------------------------------------------------===//
190   
191   /// addLiveIn/Out - Add the specified register as a live in/out.  Note that it
192   /// is an error to add the same register to the same set more than once.
193   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
194     LiveIns.push_back(std::make_pair(Reg, vreg));
195   }
196   void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
197   
198   // Iteration support for live in/out sets.  These sets are kept in sorted
199   // order by their register number.
200   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
201   livein_iterator;
202   typedef std::vector<unsigned>::const_iterator liveout_iterator;
203   livein_iterator livein_begin() const { return LiveIns.begin(); }
204   livein_iterator livein_end()   const { return LiveIns.end(); }
205   bool            livein_empty() const { return LiveIns.empty(); }
206   liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
207   liveout_iterator liveout_end()   const { return LiveOuts.end(); }
208   bool             liveout_empty() const { return LiveOuts.empty(); }
209
210   bool isLiveIn(unsigned Reg) const {
211     for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
212       if (I->first == Reg || I->second == Reg)
213         return true;
214     return false;
215   }
216
217 private:
218   void HandleVRegListReallocation();
219   
220 public:
221   /// defusechain_iterator - This class provides iterator support for machine
222   /// operands in the function that use or define a specific register.  If
223   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
224   /// returns defs.  If neither are true then you are silly and it always
225   /// returns end().
226   template<bool ReturnUses, bool ReturnDefs>
227   class defusechain_iterator
228     : public forward_iterator<MachineInstr, ptrdiff_t> {
229     MachineOperand *Op;
230     explicit defusechain_iterator(MachineOperand *op) : Op(op) {
231       // If the first node isn't one we're interested in, advance to one that
232       // we are interested in.
233       if (op) {
234         if ((!ReturnUses && op->isUse()) ||
235             (!ReturnDefs && op->isDef()))
236           ++*this;
237       }
238     }
239     friend class MachineRegisterInfo;
240   public:
241     typedef forward_iterator<MachineInstr, ptrdiff_t>::reference reference;
242     typedef forward_iterator<MachineInstr, ptrdiff_t>::pointer pointer;
243     
244     defusechain_iterator(const defusechain_iterator &I) : Op(I.Op) {}
245     defusechain_iterator() : Op(0) {}
246     
247     bool operator==(const defusechain_iterator &x) const {
248       return Op == x.Op;
249     }
250     bool operator!=(const defusechain_iterator &x) const {
251       return !operator==(x);
252     }
253     
254     /// atEnd - return true if this iterator is equal to reg_end() on the value.
255     bool atEnd() const { return Op == 0; }
256     
257     // Iterator traversal: forward iteration only
258     defusechain_iterator &operator++() {          // Preincrement
259       assert(Op && "Cannot increment end iterator!");
260       Op = Op->getNextOperandForReg();
261       
262       // If this is an operand we don't care about, skip it.
263       while (Op && ((!ReturnUses && Op->isUse()) || 
264                     (!ReturnDefs && Op->isDef())))
265         Op = Op->getNextOperandForReg();
266       
267       return *this;
268     }
269     defusechain_iterator operator++(int) {        // Postincrement
270       defusechain_iterator tmp = *this; ++*this; return tmp;
271     }
272     
273     MachineOperand &getOperand() const {
274       assert(Op && "Cannot dereference end iterator!");
275       return *Op;
276     }
277     
278     /// getOperandNo - Return the operand # of this MachineOperand in its
279     /// MachineInstr.
280     unsigned getOperandNo() const {
281       assert(Op && "Cannot dereference end iterator!");
282       return Op - &Op->getParent()->getOperand(0);
283     }
284     
285     // Retrieve a reference to the current operand.
286     MachineInstr &operator*() const {
287       assert(Op && "Cannot dereference end iterator!");
288       return *Op->getParent();
289     }
290     
291     MachineInstr *operator->() const {
292       assert(Op && "Cannot dereference end iterator!");
293       return Op->getParent();
294     }
295   };
296   
297 };
298
299 } // End llvm namespace
300
301 #endif