add efficient iteration support for register use/def's
[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/MRegisterInfo.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/ADT/iterator"
20 #include <vector>
21
22 namespace llvm {
23   
24 /// MachineRegisterInfo - Keep track of information for each virtual register,
25 /// including its register class.
26 class MachineRegisterInfo {
27   /// VRegInfo - Information we keep for each virtual register.  The entries in
28   /// this vector are actually converted to vreg numbers by adding the 
29   /// MRegisterInfo::FirstVirtualRegister delta to their index.
30   ///
31   /// Each element in this list contains the register class of the vreg and the
32   /// start of the use/def list for the register.
33   std::vector<std::pair<const TargetRegisterClass*, MachineOperand*> > VRegInfo;
34   
35   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
36   /// physical registers.
37   MachineOperand **PhysRegUseDefLists; 
38   
39   /// UsedPhysRegs - This is a bit vector that is computed and set by the
40   /// register allocator, and must be kept up to date by passes that run after
41   /// register allocation (though most don't modify this).  This is used
42   /// so that the code generator knows which callee save registers to save and
43   /// for other target specific uses.
44   BitVector UsedPhysRegs;
45   
46   /// LiveIns/LiveOuts - Keep track of the physical registers that are
47   /// livein/liveout of the function.  Live in values are typically arguments in
48   /// registers, live out values are typically return values in registers.
49   /// LiveIn values are allowed to have virtual registers associated with them,
50   /// stored in the second element.
51   std::vector<std::pair<unsigned, unsigned> > LiveIns;
52   std::vector<unsigned> LiveOuts;
53   
54   MachineRegisterInfo(const MachineRegisterInfo&); // DO NOT IMPLEMENT
55   void operator=(const MachineRegisterInfo&);      // DO NOT IMPLEMENT
56 public:
57   MachineRegisterInfo(const MRegisterInfo &MRI);
58   ~MachineRegisterInfo();
59   
60   //===--------------------------------------------------------------------===//
61   // Register Info
62   //===--------------------------------------------------------------------===//
63
64   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
65   /// and uses of a register within the MachineFunction that corresponds to this
66   /// MachineRegisterInfo object.
67   class reg_iterator;
68   reg_iterator reg_begin(unsigned RegNo) const {
69     return reg_iterator(getRegUseDefListHead(RegNo));
70   }
71   static reg_iterator reg_end() { return reg_iterator(0); }
72   
73   /// getRegUseDefListHead - Return the head pointer for the register use/def
74   /// list for the specified virtual or physical register.
75   MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
76     if (RegNo < MRegisterInfo::FirstVirtualRegister)
77       return PhysRegUseDefLists[RegNo];
78     RegNo -= MRegisterInfo::FirstVirtualRegister;
79     return VRegInfo[RegNo].second;
80   }
81   
82   MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
83     if (RegNo < MRegisterInfo::FirstVirtualRegister)
84       return PhysRegUseDefLists[RegNo];
85     RegNo -= MRegisterInfo::FirstVirtualRegister;
86     return VRegInfo[RegNo].second;
87   }
88   
89   //===--------------------------------------------------------------------===//
90   // Virtual Register Info
91   //===--------------------------------------------------------------------===//
92   
93   /// getRegClass - Return the register class of the specified virtual register.
94   const TargetRegisterClass *getRegClass(unsigned Reg) {
95     Reg -= MRegisterInfo::FirstVirtualRegister;
96     assert(Reg < VRegInfo.size() && "Invalid vreg!");
97     return VRegInfo[Reg].first;
98   }
99   
100   /// createVirtualRegister - Create and return a new virtual register in the
101   /// function with the specified register class.
102   ///
103   unsigned createVirtualRegister(const TargetRegisterClass *RegClass) {
104     assert(RegClass && "Cannot create register without RegClass!");
105     // Add a reg, but keep track of whether the vector reallocated or not.
106     void *ArrayBase = &VRegInfo[0];
107     VRegInfo.push_back(std::make_pair(RegClass, (MachineOperand*)0));
108     
109     if (&VRegInfo[0] == ArrayBase)
110       return getLastVirtReg();
111
112     // Otherwise, the vector reallocated, handle this now.
113     HandleVRegListReallocation();
114     return getLastVirtReg();
115   }
116
117   /// getLastVirtReg - Return the highest currently assigned virtual register.
118   ///
119   unsigned getLastVirtReg() const {
120     return VRegInfo.size()+MRegisterInfo::FirstVirtualRegister-1;
121   }
122   
123   //===--------------------------------------------------------------------===//
124   // Physical Register Use Info
125   //===--------------------------------------------------------------------===//
126   
127   /// isPhysRegUsed - Return true if the specified register is used in this
128   /// function.  This only works after register allocation.
129   bool isPhysRegUsed(unsigned Reg) const { return UsedPhysRegs[Reg]; }
130   
131   /// setPhysRegUsed - Mark the specified register used in this function.
132   /// This should only be called during and after register allocation.
133   void setPhysRegUsed(unsigned Reg) { UsedPhysRegs[Reg] = true; }
134   
135   /// setPhysRegUnused - Mark the specified register unused in this function.
136   /// This should only be called during and after register allocation.
137   void setPhysRegUnused(unsigned Reg) { UsedPhysRegs[Reg] = false; }
138   
139
140   //===--------------------------------------------------------------------===//
141   // LiveIn/LiveOut Management
142   //===--------------------------------------------------------------------===//
143   
144   /// addLiveIn/Out - Add the specified register as a live in/out.  Note that it
145   /// is an error to add the same register to the same set more than once.
146   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
147     LiveIns.push_back(std::make_pair(Reg, vreg));
148   }
149   void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
150   
151   // Iteration support for live in/out sets.  These sets are kept in sorted
152   // order by their register number.
153   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
154   livein_iterator;
155   typedef std::vector<unsigned>::const_iterator liveout_iterator;
156   livein_iterator livein_begin() const { return LiveIns.begin(); }
157   livein_iterator livein_end()   const { return LiveIns.end(); }
158   bool            livein_empty() const { return LiveIns.empty(); }
159   liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
160   liveout_iterator liveout_end()   const { return LiveOuts.end(); }
161   bool             liveout_empty() const { return LiveOuts.empty(); }
162 private:
163   void HandleVRegListReallocation();
164   
165 public:
166   /// reg_iterator - This class provides iterator support for machine
167   /// operands in the function that use or define a specific register.
168   class reg_iterator : public forward_iterator<MachineOperand, ptrdiff_t> {
169     typedef forward_iterator<MachineOperand, ptrdiff_t> super;
170     
171     MachineOperand *Op;
172     reg_iterator(MachineOperand *op) : Op(op) {}
173     friend class MachineRegisterInfo;
174   public:
175     typedef super::reference reference;
176     typedef super::pointer pointer;
177     
178     reg_iterator(const reg_iterator &I) : Op(I.Op) {}
179     reg_iterator() : Op(0) {}
180     
181     bool operator==(const reg_iterator &x) const {
182       return Op == x.Op;
183     }
184     bool operator!=(const reg_iterator &x) const {
185       return !operator==(x);
186     }
187     
188     /// atEnd - return true if this iterator is equal to reg_end() on the value.
189     bool atEnd() const { return Op == 0; }
190     
191     // Iterator traversal: forward iteration only
192     reg_iterator &operator++() {          // Preincrement
193       assert(Op && "Cannot increment end iterator!");
194       Op = Op->getNextOperandForReg();
195       return *this;
196     }
197     reg_iterator operator++(int) {        // Postincrement
198       reg_iterator tmp = *this; ++*this; return tmp;
199     }
200     
201     // Retrieve a reference to the current operand.
202     MachineOperand &operator*() const {
203       assert(Op && "Cannot dereference end iterator!");
204       return *Op;
205     }
206     
207     MachineOperand *operator->() const { return Op; }
208   };
209   
210 };
211
212 } // End llvm namespace
213
214 #endif