TargetRegisterInfo.h revision 95923d70d90e0b9901d63ec3e35bf94be260e4f0
1d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//=== Target/TargetRegisterInfo.h - Target Register Information -*- C++ -*-===//
2d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//
3d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//                     The LLVM Compiler Infrastructure
4d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//
5d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen// This file is distributed under the University of Illinois Open Source
6d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen// License. See LICENSE.TXT for details.
7d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//
8d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//===----------------------------------------------------------------------===//
9d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//
10d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen// This file describes an abstract interface used to get information about a
11d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen// target machines register file.  This information is used for a variety of
12d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen// purposed, especially register allocation.
13d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//
14d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen//===----------------------------------------------------------------------===//
15d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
16d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen#ifndef LLVM_TARGET_TARGETREGISTERINFO_H
17d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen#define LLVM_TARGET_TARGETREGISTERINFO_H
18d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
19d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen#include "llvm/CodeGen/MachineBasicBlock.h"
20d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen#include "llvm/CodeGen/ValueTypes.h"
21d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen#include "llvm/ADT/DenseSet.h"
22d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen#include <cassert>
23d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen#include <functional>
24d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
25d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chennamespace llvm {
26d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
27d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenclass BitVector;
28d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenclass MachineFunction;
29d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenclass MachineMove;
30d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenclass RegScavenger;
31d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
32d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// TargetRegisterDesc - This record contains all of the information known about
33d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// a particular register.  The AliasSet field (if not null) contains a pointer
34d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// to a Zero terminated array of registers that this register aliases.  This is
35d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// needed for architectures like X86 which have AL alias AX alias EAX.
36d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// Registers that this does not apply to simply should set this to null.
37d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// The SubRegs field is a zero terminated array of registers that are
38d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// sub-registers of the specific register, e.g. AL, AH are sub-registers of AX.
39d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// The SuperRegs field is a zero terminated array of registers that are
40d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// super-registers of the specific register, e.g. RAX, EAX, are super-registers
41d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen/// of AX.
42d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen///
43d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenstruct TargetRegisterDesc {
44d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const char     *Name;         // Printable name for the reg (for debugging)
45d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const unsigned *AliasSet;     // Register Alias Set, described above
46d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const unsigned *SubRegs;      // Sub-register set, described above
47d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const unsigned *SuperRegs;    // Super-register set, described above
48d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen};
49d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
50d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenclass TargetRegisterClass {
51d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic:
52d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  typedef const unsigned* iterator;
53d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  typedef const unsigned* const_iterator;
54d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
55d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  typedef const EVT* vt_iterator;
56d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  typedef const TargetRegisterClass* const * sc_iterator;
57d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenprivate:
58d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  unsigned ID;
59d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const char *Name;
60d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const vt_iterator VTs;
61d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const sc_iterator SubClasses;
62d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const sc_iterator SuperClasses;
63d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const sc_iterator SubRegClasses;
64d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const sc_iterator SuperRegClasses;
65d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const unsigned RegSize, Alignment;    // Size & Alignment of register in bytes
66d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const int CopyCost;
67d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const iterator RegsBegin, RegsEnd;
68d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  DenseSet<unsigned> RegSet;
69d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chenpublic:
70d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  TargetRegisterClass(unsigned id,
71d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      const char *name,
72d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      const EVT *vts,
73d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      const TargetRegisterClass * const *subcs,
74d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      const TargetRegisterClass * const *supcs,
75d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      const TargetRegisterClass * const *subregcs,
76d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      const TargetRegisterClass * const *superregcs,
77d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      unsigned RS, unsigned Al, int CC,
78d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen                      iterator RB, iterator RE)
79d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    : ID(id), Name(name), VTs(vts), SubClasses(subcs), SuperClasses(supcs),
80d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    SubRegClasses(subregcs), SuperRegClasses(superregcs),
81d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    RegSize(RS), Alignment(Al), CopyCost(CC), RegsBegin(RB), RegsEnd(RE) {
82d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen      for (iterator I = RegsBegin, E = RegsEnd; I != E; ++I)
83d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        RegSet.insert(*I);
84d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    }
85d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  virtual ~TargetRegisterClass() {}     // Allow subclasses
86d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
87d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// getID() - Return the register class ID number.
88d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  ///
89d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  unsigned getID() const { return ID; }
90d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
91d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// getName() - Return the register class name for debugging.
92d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  ///
93d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const char *getName() const { return Name; }
94d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
95d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// begin/end - Return all of the registers in this class.
96d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  ///
97d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  iterator       begin() const { return RegsBegin; }
98d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  iterator         end() const { return RegsEnd; }
99d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
100d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// getNumRegs - Return the number of registers in this class.
101d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  ///
102d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  unsigned getNumRegs() const { return (unsigned)(RegsEnd-RegsBegin); }
103d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
104d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// getRegister - Return the specified register in the class.
105d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  ///
106d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  unsigned getRegister(unsigned i) const {
107d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    assert(i < getNumRegs() && "Register number out of range!");
108d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    return RegsBegin[i];
109d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  }
110d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
111d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// contains - Return true if the specified register is included in this
112d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// register class.
113d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  bool contains(unsigned Reg) const {
114d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    return RegSet.count(Reg);
115d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  }
116d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
117d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// hasType - return true if this TargetRegisterClass has the ValueType vt.
118d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  ///
119d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  bool hasType(EVT vt) const {
120d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    for(int i = 0; VTs[i].getSimpleVT().SimpleTy != MVT::Other; ++i)
121d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen      if (VTs[i] == vt)
122d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen        return true;
123d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    return false;
124d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  }
125d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
126d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// vt_begin / vt_end - Loop over all of the value types that can be
127d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// represented by values in this register class.
128d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  vt_iterator vt_begin() const {
129d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    return VTs;
130d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  }
131d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
132d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  vt_iterator vt_end() const {
133d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    vt_iterator I = VTs;
134d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    while (I->getSimpleVT().SimpleTy != MVT::Other) ++I;
135d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    return I;
136d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  }
137d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
138d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// subregclasses_begin / subregclasses_end - Loop over all of
139d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// the subreg register classes of this register class.
140d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  sc_iterator subregclasses_begin() const {
141d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    return SubRegClasses;
142d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  }
143d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
144d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  sc_iterator subregclasses_end() const {
145d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    sc_iterator I = SubRegClasses;
146d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    while (*I != NULL) ++I;
147d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    return I;
148d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  }
149d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen
150d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// getSubRegisterRegClass - Return the register class of subregisters with
151d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  /// index SubIdx, or NULL if no such class exists.
152d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen  const TargetRegisterClass* getSubRegisterRegClass(unsigned SubIdx) const {
153d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    assert(SubIdx>0 && "Invalid subregister index");
154d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen    for (unsigned s = 0; s != SubIdx-1; ++s)
155d7955ce24d294fb2014c59d11fca184471056f44Shuyi Chen      if (!SubRegClasses[s])
156        return NULL;
157    return SubRegClasses[SubIdx-1];
158  }
159
160  /// superregclasses_begin / superregclasses_end - Loop over all of
161  /// the superreg register classes of this register class.
162  sc_iterator superregclasses_begin() const {
163    return SuperRegClasses;
164  }
165
166  sc_iterator superregclasses_end() const {
167    sc_iterator I = SuperRegClasses;
168    while (*I != NULL) ++I;
169    return I;
170  }
171
172  /// hasSubClass - return true if the the specified TargetRegisterClass
173  /// is a proper subset of this TargetRegisterClass.
174  bool hasSubClass(const TargetRegisterClass *cs) const {
175    for (int i = 0; SubClasses[i] != NULL; ++i)
176      if (SubClasses[i] == cs)
177        return true;
178    return false;
179  }
180
181  /// subclasses_begin / subclasses_end - Loop over all of the classes
182  /// that are proper subsets of this register class.
183  sc_iterator subclasses_begin() const {
184    return SubClasses;
185  }
186
187  sc_iterator subclasses_end() const {
188    sc_iterator I = SubClasses;
189    while (*I != NULL) ++I;
190    return I;
191  }
192
193  /// hasSuperClass - return true if the specified TargetRegisterClass is a
194  /// proper superset of this TargetRegisterClass.
195  bool hasSuperClass(const TargetRegisterClass *cs) const {
196    for (int i = 0; SuperClasses[i] != NULL; ++i)
197      if (SuperClasses[i] == cs)
198        return true;
199    return false;
200  }
201
202  /// superclasses_begin / superclasses_end - Loop over all of the classes
203  /// that are proper supersets of this register class.
204  sc_iterator superclasses_begin() const {
205    return SuperClasses;
206  }
207
208  sc_iterator superclasses_end() const {
209    sc_iterator I = SuperClasses;
210    while (*I != NULL) ++I;
211    return I;
212  }
213
214  /// isASubClass - return true if this TargetRegisterClass is a subset
215  /// class of at least one other TargetRegisterClass.
216  bool isASubClass() const {
217    return SuperClasses[0] != 0;
218  }
219
220  /// allocation_order_begin/end - These methods define a range of registers
221  /// which specify the registers in this class that are valid to register
222  /// allocate, and the preferred order to allocate them in.  For example,
223  /// callee saved registers should be at the end of the list, because it is
224  /// cheaper to allocate caller saved registers.
225  ///
226  /// These methods take a MachineFunction argument, which can be used to tune
227  /// the allocatable registers based on the characteristics of the function.
228  /// One simple example is that the frame pointer register can be used if
229  /// frame-pointer-elimination is performed.
230  ///
231  /// By default, these methods return all registers in the class.
232  ///
233  virtual iterator allocation_order_begin(const MachineFunction &MF) const {
234    return begin();
235  }
236  virtual iterator allocation_order_end(const MachineFunction &MF)   const {
237    return end();
238  }
239
240  /// getSize - Return the size of the register in bytes, which is also the size
241  /// of a stack slot allocated to hold a spilled copy of this register.
242  unsigned getSize() const { return RegSize; }
243
244  /// getAlignment - Return the minimum required alignment for a register of
245  /// this class.
246  unsigned getAlignment() const { return Alignment; }
247
248  /// getCopyCost - Return the cost of copying a value between two registers in
249  /// this class. A negative number means the register class is very expensive
250  /// to copy e.g. status flag register classes.
251  int getCopyCost() const { return CopyCost; }
252};
253
254
255/// TargetRegisterInfo base class - We assume that the target defines a static
256/// array of TargetRegisterDesc objects that represent all of the machine
257/// registers that the target has.  As such, we simply have to track a pointer
258/// to this array so that we can turn register number into a register
259/// descriptor.
260///
261class TargetRegisterInfo {
262protected:
263  const unsigned* SubregHash;
264  const unsigned SubregHashSize;
265  const unsigned* SuperregHash;
266  const unsigned SuperregHashSize;
267  const unsigned* AliasesHash;
268  const unsigned AliasesHashSize;
269public:
270  typedef const TargetRegisterClass * const * regclass_iterator;
271private:
272  const TargetRegisterDesc *Desc;             // Pointer to the descriptor array
273  unsigned NumRegs;                           // Number of entries in the array
274
275  regclass_iterator RegClassBegin, RegClassEnd;   // List of regclasses
276
277  int CallFrameSetupOpcode, CallFrameDestroyOpcode;
278protected:
279  TargetRegisterInfo(const TargetRegisterDesc *D, unsigned NR,
280                     regclass_iterator RegClassBegin,
281                     regclass_iterator RegClassEnd,
282                     int CallFrameSetupOpcode = -1,
283                     int CallFrameDestroyOpcode = -1,
284                     const unsigned* subregs = 0,
285                     const unsigned subregsize = 0,
286                     const unsigned* superregs = 0,
287                     const unsigned superregsize = 0,
288                     const unsigned* aliases = 0,
289                     const unsigned aliasessize = 0);
290  virtual ~TargetRegisterInfo();
291public:
292
293  enum {                        // Define some target independent constants
294    /// NoRegister - This physical register is not a real target register.  It
295    /// is useful as a sentinal.
296    NoRegister = 0,
297
298    /// FirstVirtualRegister - This is the first register number that is
299    /// considered to be a 'virtual' register, which is part of the SSA
300    /// namespace.  This must be the same for all targets, which means that each
301    /// target is limited to 1024 registers.
302    FirstVirtualRegister = 1024
303  };
304
305  /// isPhysicalRegister - Return true if the specified register number is in
306  /// the physical register namespace.
307  static bool isPhysicalRegister(unsigned Reg) {
308    assert(Reg && "this is not a register!");
309    return Reg < FirstVirtualRegister;
310  }
311
312  /// isVirtualRegister - Return true if the specified register number is in
313  /// the virtual register namespace.
314  static bool isVirtualRegister(unsigned Reg) {
315    assert(Reg && "this is not a register!");
316    return Reg >= FirstVirtualRegister;
317  }
318
319  /// getPhysicalRegisterRegClass - Returns the Register Class of a physical
320  /// register of the given type. If type is EVT::Other, then just return any
321  /// register class the register belongs to.
322  virtual const TargetRegisterClass *
323    getPhysicalRegisterRegClass(unsigned Reg, EVT VT = MVT::Other) const;
324
325  /// getAllocatableSet - Returns a bitset indexed by register number
326  /// indicating if a register is allocatable or not. If a register class is
327  /// specified, returns the subset for the class.
328  BitVector getAllocatableSet(MachineFunction &MF,
329                              const TargetRegisterClass *RC = NULL) const;
330
331  const TargetRegisterDesc &operator[](unsigned RegNo) const {
332    assert(RegNo < NumRegs &&
333           "Attempting to access record for invalid register number!");
334    return Desc[RegNo];
335  }
336
337  /// Provide a get method, equivalent to [], but more useful if we have a
338  /// pointer to this object.
339  ///
340  const TargetRegisterDesc &get(unsigned RegNo) const {
341    return operator[](RegNo);
342  }
343
344  /// getAliasSet - Return the set of registers aliased by the specified
345  /// register, or a null list of there are none.  The list returned is zero
346  /// terminated.
347  ///
348  const unsigned *getAliasSet(unsigned RegNo) const {
349    return get(RegNo).AliasSet;
350  }
351
352  /// getSubRegisters - Return the list of registers that are sub-registers of
353  /// the specified register, or a null list of there are none. The list
354  /// returned is zero terminated and sorted according to super-sub register
355  /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
356  ///
357  const unsigned *getSubRegisters(unsigned RegNo) const {
358    return get(RegNo).SubRegs;
359  }
360
361  /// getSuperRegisters - Return the list of registers that are super-registers
362  /// of the specified register, or a null list of there are none. The list
363  /// returned is zero terminated and sorted according to super-sub register
364  /// relations. e.g. X86::AL's super-register list is RAX, EAX, AX.
365  ///
366  const unsigned *getSuperRegisters(unsigned RegNo) const {
367    return get(RegNo).SuperRegs;
368  }
369
370  /// getName - Return the human-readable symbolic target-specific name for the
371  /// specified physical register.
372  const char *getName(unsigned RegNo) const {
373    return get(RegNo).Name;
374  }
375
376  /// getNumRegs - Return the number of registers this target has (useful for
377  /// sizing arrays holding per register information)
378  unsigned getNumRegs() const {
379    return NumRegs;
380  }
381
382  /// regsOverlap - Returns true if the two registers are equal or alias each
383  /// other. The registers may be virtual register.
384  bool regsOverlap(unsigned regA, unsigned regB) const {
385    if (regA == regB)
386      return true;
387
388    if (isVirtualRegister(regA) || isVirtualRegister(regB))
389      return false;
390
391    // regA and regB are distinct physical registers. Do they alias?
392    size_t index = (regA + regB * 37) & (AliasesHashSize-1);
393    unsigned ProbeAmt = 0;
394    while (AliasesHash[index*2] != 0 &&
395           AliasesHash[index*2+1] != 0) {
396      if (AliasesHash[index*2] == regA && AliasesHash[index*2+1] == regB)
397        return true;
398
399      index = (index + ProbeAmt) & (AliasesHashSize-1);
400      ProbeAmt += 2;
401    }
402
403    return false;
404  }
405
406  /// isSubRegister - Returns true if regB is a sub-register of regA.
407  ///
408  bool isSubRegister(unsigned regA, unsigned regB) const {
409    // SubregHash is a simple quadratically probed hash table.
410    size_t index = (regA + regB * 37) & (SubregHashSize-1);
411    unsigned ProbeAmt = 2;
412    while (SubregHash[index*2] != 0 &&
413           SubregHash[index*2+1] != 0) {
414      if (SubregHash[index*2] == regA && SubregHash[index*2+1] == regB)
415        return true;
416
417      index = (index + ProbeAmt) & (SubregHashSize-1);
418      ProbeAmt += 2;
419    }
420
421    return false;
422  }
423
424  /// isSuperRegister - Returns true if regB is a super-register of regA.
425  ///
426  bool isSuperRegister(unsigned regA, unsigned regB) const {
427    // SuperregHash is a simple quadratically probed hash table.
428    size_t index = (regA + regB * 37) & (SuperregHashSize-1);
429    unsigned ProbeAmt = 2;
430    while (SuperregHash[index*2] != 0 &&
431           SuperregHash[index*2+1] != 0) {
432      if (SuperregHash[index*2] == regA && SuperregHash[index*2+1] == regB)
433        return true;
434
435      index = (index + ProbeAmt) & (SuperregHashSize-1);
436      ProbeAmt += 2;
437    }
438
439    return false;
440  }
441
442  /// getCalleeSavedRegs - Return a null-terminated list of all of the
443  /// callee saved registers on this target. The register should be in the
444  /// order of desired callee-save stack frame offset. The first register is
445  /// closed to the incoming stack pointer if stack grows down, and vice versa.
446  virtual const unsigned* getCalleeSavedRegs(const MachineFunction *MF = 0)
447                                                                      const = 0;
448
449  /// getCalleeSavedRegClasses - Return a null-terminated list of the preferred
450  /// register classes to spill each callee saved register with.  The order and
451  /// length of this list match the getCalleeSaveRegs() list.
452  virtual const TargetRegisterClass* const *getCalleeSavedRegClasses(
453                                            const MachineFunction *MF) const =0;
454
455  /// getReservedRegs - Returns a bitset indexed by physical register number
456  /// indicating if a register is a special register that has particular uses
457  /// and should be considered unavailable at all times, e.g. SP, RA. This is
458  /// used by register scavenger to determine what registers are free.
459  virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
460
461  /// getSubReg - Returns the physical register number of sub-register "Index"
462  /// for physical register RegNo. Return zero if the sub-register does not
463  /// exist.
464  virtual unsigned getSubReg(unsigned RegNo, unsigned Index) const = 0;
465
466  /// getMatchingSuperReg - Return a super-register of the specified register
467  /// Reg so its sub-register of index SubIdx is Reg.
468  unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
469                               const TargetRegisterClass *RC) const {
470    for (const unsigned *SRs = getSuperRegisters(Reg); unsigned SR = *SRs;++SRs)
471      if (Reg == getSubReg(SR, SubIdx) && RC->contains(SR))
472        return SR;
473    return 0;
474  }
475
476  /// getMatchingSuperRegClass - Return a subclass of the specified register
477  /// class A so that each register in it has a sub-register of the
478  /// specified sub-register index which is in the specified register class B.
479  virtual const TargetRegisterClass *
480  getMatchingSuperRegClass(const TargetRegisterClass *A,
481                           const TargetRegisterClass *B, unsigned Idx) const {
482    return 0;
483  }
484
485  //===--------------------------------------------------------------------===//
486  // Register Class Information
487  //
488
489  /// Register class iterators
490  ///
491  regclass_iterator regclass_begin() const { return RegClassBegin; }
492  regclass_iterator regclass_end() const { return RegClassEnd; }
493
494  unsigned getNumRegClasses() const {
495    return (unsigned)(regclass_end()-regclass_begin());
496  }
497
498  /// getRegClass - Returns the register class associated with the enumeration
499  /// value.  See class TargetOperandInfo.
500  const TargetRegisterClass *getRegClass(unsigned i) const {
501    assert(i <= getNumRegClasses() && "Register Class ID out of range");
502    return i ? RegClassBegin[i - 1] : NULL;
503  }
504
505  /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
506  /// values.  If a target supports multiple different pointer register classes,
507  /// kind specifies which one is indicated.
508  virtual const TargetRegisterClass *getPointerRegClass(unsigned Kind=0) const {
509    assert(0 && "Target didn't implement getPointerRegClass!");
510    return 0; // Must return a value in order to compile with VS 2005
511  }
512
513  /// getCrossCopyRegClass - Returns a legal register class to copy a register
514  /// in the specified class to or from. Returns NULL if it is possible to copy
515  /// between a two registers of the specified class.
516  virtual const TargetRegisterClass *
517  getCrossCopyRegClass(const TargetRegisterClass *RC) const {
518    return NULL;
519  }
520
521  /// getAllocationOrder - Returns the register allocation order for a specified
522  /// register class in the form of a pair of TargetRegisterClass iterators.
523  virtual std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
524  getAllocationOrder(const TargetRegisterClass *RC,
525                     unsigned HintType, unsigned HintReg,
526                     const MachineFunction &MF) const {
527    return std::make_pair(RC->allocation_order_begin(MF),
528                          RC->allocation_order_end(MF));
529  }
530
531  /// ResolveRegAllocHint - Resolves the specified register allocation hint
532  /// to a physical register. Returns the physical register if it is successful.
533  virtual unsigned ResolveRegAllocHint(unsigned Type, unsigned Reg,
534                                       const MachineFunction &MF) const {
535    if (Type == 0 && Reg && isPhysicalRegister(Reg))
536      return Reg;
537    return 0;
538  }
539
540  /// UpdateRegAllocHint - A callback to allow target a chance to update
541  /// register allocation hints when a register is "changed" (e.g. coalesced)
542  /// to another register. e.g. On ARM, some virtual registers should target
543  /// register pairs, if one of pair is coalesced to another register, the
544  /// allocation hint of the other half of the pair should be changed to point
545  /// to the new register.
546  virtual void UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
547                                  MachineFunction &MF) const {
548    // Do nothing.
549  }
550
551  /// targetHandlesStackFrameRounding - Returns true if the target is
552  /// responsible for rounding up the stack frame (probably at emitPrologue
553  /// time).
554  virtual bool targetHandlesStackFrameRounding() const {
555    return false;
556  }
557
558  /// requiresRegisterScavenging - returns true if the target requires (and can
559  /// make use of) the register scavenger.
560  virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
561    return false;
562  }
563
564  /// hasFP - Return true if the specified function should have a dedicated
565  /// frame pointer register. For most targets this is true only if the function
566  /// has variable sized allocas or if frame pointer elimination is disabled.
567  virtual bool hasFP(const MachineFunction &MF) const = 0;
568
569  /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
570  /// not required, we reserve argument space for call sites in the function
571  /// immediately on entry to the current function. This eliminates the need for
572  /// add/sub sp brackets around call sites. Returns true if the call frame is
573  /// included as part of the stack frame.
574  virtual bool hasReservedCallFrame(MachineFunction &MF) const {
575    return !hasFP(MF);
576  }
577
578  /// hasReservedSpillSlot - Return true if target has reserved a spill slot in
579  /// the stack frame of the given function for the specified register. e.g. On
580  /// x86, if the frame register is required, the first fixed stack object is
581  /// reserved as its spill slot. This tells PEI not to create a new stack frame
582  /// object for the given register. It should be called only after
583  /// processFunctionBeforeCalleeSavedScan().
584  virtual bool hasReservedSpillSlot(MachineFunction &MF, unsigned Reg,
585                                    int &FrameIdx) const {
586    return false;
587  }
588
589  /// needsStackRealignment - true if storage within the function requires the
590  /// stack pointer to be aligned more than the normal calling convention calls
591  /// for.
592  virtual bool needsStackRealignment(const MachineFunction &MF) const {
593    return false;
594  }
595
596  /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
597  /// frame setup/destroy instructions if they exist (-1 otherwise).  Some
598  /// targets use pseudo instructions in order to abstract away the difference
599  /// between operating with a frame pointer and operating without, through the
600  /// use of these two instructions.
601  ///
602  int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
603  int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
604
605  /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
606  /// code insertion to eliminate call frame setup and destroy pseudo
607  /// instructions (but only if the Target is using them).  It is responsible
608  /// for eliminating these instructions, replacing them with concrete
609  /// instructions.  This method need only be implemented if using call frame
610  /// setup/destroy pseudo instructions.
611  ///
612  virtual void
613  eliminateCallFramePseudoInstr(MachineFunction &MF,
614                                MachineBasicBlock &MBB,
615                                MachineBasicBlock::iterator MI) const {
616    assert(getCallFrameSetupOpcode()== -1 && getCallFrameDestroyOpcode()== -1 &&
617           "eliminateCallFramePseudoInstr must be implemented if using"
618           " call frame setup/destroy pseudo instructions!");
619    assert(0 && "Call Frame Pseudo Instructions do not exist on this target!");
620  }
621
622  /// processFunctionBeforeCalleeSavedScan - This method is called immediately
623  /// before PrologEpilogInserter scans the physical registers used to determine
624  /// what callee saved registers should be spilled. This method is optional.
625  virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
626                                                RegScavenger *RS = NULL) const {
627
628  }
629
630  /// processFunctionBeforeFrameFinalized - This method is called immediately
631  /// before the specified functions frame layout (MF.getFrameInfo()) is
632  /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
633  /// replaced with direct constants.  This method is optional.
634  ///
635  virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
636  }
637
638  /// eliminateFrameIndex - This method must be overriden to eliminate abstract
639  /// frame indices from instructions which may use them.  The instruction
640  /// referenced by the iterator contains an MO_FrameIndex operand which must be
641  /// eliminated by this method.  This method may modify or replace the
642  /// specified instruction, as long as it keeps the iterator pointing the the
643  /// finished product. SPAdj is the SP adjustment due to call frame setup
644  /// instruction.
645  virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI,
646                                   int SPAdj, RegScavenger *RS=NULL) const = 0;
647
648  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
649  /// the function.
650  virtual void emitPrologue(MachineFunction &MF) const = 0;
651  virtual void emitEpilogue(MachineFunction &MF,
652                            MachineBasicBlock &MBB) const = 0;
653
654  //===--------------------------------------------------------------------===//
655  /// Debug information queries.
656
657  /// getDwarfRegNum - Map a target register to an equivalent dwarf register
658  /// number.  Returns -1 if there is no equivalent value.  The second
659  /// parameter allows targets to use different numberings for EH info and
660  /// debugging info.
661  virtual int getDwarfRegNum(unsigned RegNum, bool isEH) const = 0;
662
663  /// getFrameRegister - This method should return the register used as a base
664  /// for values allocated in the current stack frame.
665  virtual unsigned getFrameRegister(MachineFunction &MF) const = 0;
666
667  /// getFrameIndexOffset - Returns the displacement from the frame register to
668  /// the stack frame of the specified index.
669  virtual int getFrameIndexOffset(MachineFunction &MF, int FI) const;
670
671  /// getRARegister - This method should return the register where the return
672  /// address can be found.
673  virtual unsigned getRARegister() const = 0;
674
675  /// getInitialFrameState - Returns a list of machine moves that are assumed
676  /// on entry to all functions.  Note that LabelID is ignored (assumed to be
677  /// the beginning of the function.)
678  virtual void getInitialFrameState(std::vector<MachineMove> &Moves) const;
679};
680
681
682// This is useful when building IndexedMaps keyed on virtual registers
683struct VirtReg2IndexFunctor : public std::unary_function<unsigned, unsigned> {
684  unsigned operator()(unsigned Reg) const {
685    return Reg - TargetRegisterInfo::FirstVirtualRegister;
686  }
687};
688
689/// getCommonSubClass - find the largest common subclass of A and B. Return NULL
690/// if there is no common subclass.
691const TargetRegisterClass *getCommonSubClass(const TargetRegisterClass *A,
692                                             const TargetRegisterClass *B);
693
694} // End llvm namespace
695
696#endif
697