Move register allocation preference (or hint) from LiveInterval to MachineRegisterInf...
[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 public:
29   /// Register allocation hints.
30   enum RegAllocHintType {
31     RA_None,                /// No preference
32     RA_Preference,          /// Prefer a particular register
33     RA_PairEven,            /// Even register of a register pair
34     RA_PairOdd              /// Odd register of a register pair
35   };
36
37 private:
38   /// VRegInfo - Information we keep for each virtual register.  The entries in
39   /// this vector are actually converted to vreg numbers by adding the 
40   /// TargetRegisterInfo::FirstVirtualRegister delta to their index.
41   ///
42   /// Each element in this list contains the register class of the vreg and the
43   /// start of the use/def list for the register.
44   std::vector<std::pair<const TargetRegisterClass*, MachineOperand*> > VRegInfo;
45
46   /// RegClassVRegMap - This vector acts as a map from TargetRegisterClass to
47   /// virtual registers. For each target register class, it keeps a list of
48   /// virtual registers belonging to the class.
49   std::vector<std::vector<unsigned> > RegClass2VRegMap;
50
51   /// RegAllocHints - This vector records register allocation hints for virtual
52   /// registers. For each virtual register, it keeps a register and type enum
53   /// pair making up the allocation hint. For example, if the hint type is
54   /// RA_Specified, it means the virtual register prefers the specified physical
55   /// register of the hint or the physical register allocated to the virtual
56   /// register of the hint.
57   std::vector<std::pair<RegAllocHintType, unsigned> > RegAllocHints;
58   
59   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
60   /// physical registers.
61   MachineOperand **PhysRegUseDefLists; 
62   
63   /// UsedPhysRegs - This is a bit vector that is computed and set by the
64   /// register allocator, and must be kept up to date by passes that run after
65   /// register allocation (though most don't modify this).  This is used
66   /// so that the code generator knows which callee save registers to save and
67   /// for other target specific uses.
68   BitVector UsedPhysRegs;
69   
70   /// LiveIns/LiveOuts - Keep track of the physical registers that are
71   /// livein/liveout of the function.  Live in values are typically arguments in
72   /// registers, live out values are typically return values in registers.
73   /// LiveIn values are allowed to have virtual registers associated with them,
74   /// stored in the second element.
75   std::vector<std::pair<unsigned, unsigned> > LiveIns;
76   std::vector<unsigned> LiveOuts;
77   
78   MachineRegisterInfo(const MachineRegisterInfo&); // DO NOT IMPLEMENT
79   void operator=(const MachineRegisterInfo&);      // DO NOT IMPLEMENT
80 public:
81   explicit MachineRegisterInfo(const TargetRegisterInfo &TRI);
82   ~MachineRegisterInfo();
83   
84   //===--------------------------------------------------------------------===//
85   // Register Info
86   //===--------------------------------------------------------------------===//
87
88   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
89   /// and uses of a register within the MachineFunction that corresponds to this
90   /// MachineRegisterInfo object.
91   template<bool Uses, bool Defs>
92   class defusechain_iterator;
93
94   /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
95   /// register.
96   typedef defusechain_iterator<true,true> reg_iterator;
97   reg_iterator reg_begin(unsigned RegNo) const {
98     return reg_iterator(getRegUseDefListHead(RegNo));
99   }
100   static reg_iterator reg_end() { return reg_iterator(0); }
101
102   /// reg_empty - Return true if there are no instructions using or defining the
103   /// specified register (it may be live-in).
104   bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
105
106   /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
107   typedef defusechain_iterator<false,true> def_iterator;
108   def_iterator def_begin(unsigned RegNo) const {
109     return def_iterator(getRegUseDefListHead(RegNo));
110   }
111   static def_iterator def_end() { return def_iterator(0); }
112
113   /// def_empty - Return true if there are no instructions defining the
114   /// specified register (it may be live-in).
115   bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
116
117   /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
118   typedef defusechain_iterator<true,false> use_iterator;
119   use_iterator use_begin(unsigned RegNo) const {
120     return use_iterator(getRegUseDefListHead(RegNo));
121   }
122   static use_iterator use_end() { return use_iterator(0); }
123   
124   /// use_empty - Return true if there are no instructions using the specified
125   /// register.
126   bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
127
128   
129   /// replaceRegWith - Replace all instances of FromReg with ToReg in the
130   /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
131   /// except that it also changes any definitions of the register as well.
132   void replaceRegWith(unsigned FromReg, unsigned ToReg);
133   
134   /// getRegUseDefListHead - Return the head pointer for the register use/def
135   /// list for the specified virtual or physical register.
136   MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
137     if (RegNo < TargetRegisterInfo::FirstVirtualRegister)
138       return PhysRegUseDefLists[RegNo];
139     RegNo -= TargetRegisterInfo::FirstVirtualRegister;
140     return VRegInfo[RegNo].second;
141   }
142   
143   MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
144     if (RegNo < TargetRegisterInfo::FirstVirtualRegister)
145       return PhysRegUseDefLists[RegNo];
146     RegNo -= TargetRegisterInfo::FirstVirtualRegister;
147     return VRegInfo[RegNo].second;
148   }
149
150   /// getVRegDef - Return the machine instr that defines the specified virtual
151   /// register or null if none is found.  This assumes that the code is in SSA
152   /// form, so there should only be one definition.
153   MachineInstr *getVRegDef(unsigned Reg) const;
154   
155 #ifndef NDEBUG
156   void dumpUses(unsigned RegNo) const;
157 #endif
158   
159   //===--------------------------------------------------------------------===//
160   // Virtual Register Info
161   //===--------------------------------------------------------------------===//
162   
163   /// getRegClass - Return the register class of the specified virtual register.
164   ///
165   const TargetRegisterClass *getRegClass(unsigned Reg) const {
166     Reg -= TargetRegisterInfo::FirstVirtualRegister;
167     assert(Reg < VRegInfo.size() && "Invalid vreg!");
168     return VRegInfo[Reg].first;
169   }
170
171   /// setRegClass - Set the register class of the specified virtual register.
172   ///
173   void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
174
175   /// createVirtualRegister - Create and return a new virtual register in the
176   /// function with the specified register class.
177   ///
178   unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
179
180   /// getLastVirtReg - Return the highest currently assigned virtual register.
181   ///
182   unsigned getLastVirtReg() const {
183     return (unsigned)VRegInfo.size()+TargetRegisterInfo::FirstVirtualRegister-1;
184   }
185
186   /// getRegClassVirtRegs - Return the list of virtual registers of the given
187   /// target register class.
188   std::vector<unsigned> &getRegClassVirtRegs(const TargetRegisterClass *RC) {
189     return RegClass2VRegMap[RC->getID()];
190   }
191
192   /// setRegAllocationHint - Specify a register allocation hint for the
193   /// specified virtual register.
194   void setRegAllocationHint(unsigned Reg,
195                             RegAllocHintType Type, unsigned PrefReg) {
196     Reg -= TargetRegisterInfo::FirstVirtualRegister;
197     assert(Reg < VRegInfo.size() && "Invalid vreg!");
198     RegAllocHints[Reg].first  = Type;
199     RegAllocHints[Reg].second = PrefReg;
200   }
201
202   /// getRegAllocationHint - Return the register allocation hint for the
203   /// specified virtual register.
204   std::pair<RegAllocHintType, unsigned>
205   getRegAllocationHint(unsigned Reg) const {
206     Reg -= TargetRegisterInfo::FirstVirtualRegister;
207     assert(Reg < VRegInfo.size() && "Invalid vreg!");
208     return RegAllocHints[Reg];
209   }
210
211   //===--------------------------------------------------------------------===//
212   // Physical Register Use Info
213   //===--------------------------------------------------------------------===//
214   
215   /// isPhysRegUsed - Return true if the specified register is used in this
216   /// function.  This only works after register allocation.
217   bool isPhysRegUsed(unsigned Reg) const { return UsedPhysRegs[Reg]; }
218   
219   /// setPhysRegUsed - Mark the specified register used in this function.
220   /// This should only be called during and after register allocation.
221   void setPhysRegUsed(unsigned Reg) { UsedPhysRegs[Reg] = true; }
222   
223   /// setPhysRegUnused - Mark the specified register unused in this function.
224   /// This should only be called during and after register allocation.
225   void setPhysRegUnused(unsigned Reg) { UsedPhysRegs[Reg] = false; }
226   
227
228   //===--------------------------------------------------------------------===//
229   // LiveIn/LiveOut Management
230   //===--------------------------------------------------------------------===//
231   
232   /// addLiveIn/Out - Add the specified register as a live in/out.  Note that it
233   /// is an error to add the same register to the same set more than once.
234   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
235     LiveIns.push_back(std::make_pair(Reg, vreg));
236   }
237   void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
238   
239   // Iteration support for live in/out sets.  These sets are kept in sorted
240   // order by their register number.
241   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
242   livein_iterator;
243   typedef std::vector<unsigned>::const_iterator liveout_iterator;
244   livein_iterator livein_begin() const { return LiveIns.begin(); }
245   livein_iterator livein_end()   const { return LiveIns.end(); }
246   bool            livein_empty() const { return LiveIns.empty(); }
247   liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
248   liveout_iterator liveout_end()   const { return LiveOuts.end(); }
249   bool             liveout_empty() const { return LiveOuts.empty(); }
250
251   bool isLiveIn(unsigned Reg) const {
252     for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
253       if (I->first == Reg || I->second == Reg)
254         return true;
255     return false;
256   }
257
258 private:
259   void HandleVRegListReallocation();
260   
261 public:
262   /// defusechain_iterator - This class provides iterator support for machine
263   /// operands in the function that use or define a specific register.  If
264   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
265   /// returns defs.  If neither are true then you are silly and it always
266   /// returns end().
267   template<bool ReturnUses, bool ReturnDefs>
268   class defusechain_iterator
269     : public forward_iterator<MachineInstr, ptrdiff_t> {
270     MachineOperand *Op;
271     explicit defusechain_iterator(MachineOperand *op) : Op(op) {
272       // If the first node isn't one we're interested in, advance to one that
273       // we are interested in.
274       if (op) {
275         if ((!ReturnUses && op->isUse()) ||
276             (!ReturnDefs && op->isDef()))
277           ++*this;
278       }
279     }
280     friend class MachineRegisterInfo;
281   public:
282     typedef forward_iterator<MachineInstr, ptrdiff_t>::reference reference;
283     typedef forward_iterator<MachineInstr, ptrdiff_t>::pointer pointer;
284     
285     defusechain_iterator(const defusechain_iterator &I) : Op(I.Op) {}
286     defusechain_iterator() : Op(0) {}
287     
288     bool operator==(const defusechain_iterator &x) const {
289       return Op == x.Op;
290     }
291     bool operator!=(const defusechain_iterator &x) const {
292       return !operator==(x);
293     }
294     
295     /// atEnd - return true if this iterator is equal to reg_end() on the value.
296     bool atEnd() const { return Op == 0; }
297     
298     // Iterator traversal: forward iteration only
299     defusechain_iterator &operator++() {          // Preincrement
300       assert(Op && "Cannot increment end iterator!");
301       Op = Op->getNextOperandForReg();
302       
303       // If this is an operand we don't care about, skip it.
304       while (Op && ((!ReturnUses && Op->isUse()) || 
305                     (!ReturnDefs && Op->isDef())))
306         Op = Op->getNextOperandForReg();
307       
308       return *this;
309     }
310     defusechain_iterator operator++(int) {        // Postincrement
311       defusechain_iterator tmp = *this; ++*this; return tmp;
312     }
313     
314     MachineOperand &getOperand() const {
315       assert(Op && "Cannot dereference end iterator!");
316       return *Op;
317     }
318     
319     /// getOperandNo - Return the operand # of this MachineOperand in its
320     /// MachineInstr.
321     unsigned getOperandNo() const {
322       assert(Op && "Cannot dereference end iterator!");
323       return Op - &Op->getParent()->getOperand(0);
324     }
325     
326     // Retrieve a reference to the current operand.
327     MachineInstr &operator*() const {
328       assert(Op && "Cannot dereference end iterator!");
329       return *Op->getParent();
330     }
331     
332     MachineInstr *operator->() const {
333       assert(Op && "Cannot dereference end iterator!");
334       return Op->getParent();
335     }
336   };
337   
338 };
339
340 } // End llvm namespace
341
342 #endif