TableGen: Store all allocation orders together.
[oota-llvm.git] / utils / TableGen / CodeGenRegisters.h
1 //===- CodeGenRegisters.h - Register and RegisterClass Info -----*- 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 structures to encapsulate information gleaned from the
11 // target register and register class definitions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef CODEGEN_REGISTERS_H
16 #define CODEGEN_REGISTERS_H
17
18 #include "SetTheory.h"
19 #include "llvm/TableGen/Record.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/BitVector.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/SetVector.h"
25 #include <cstdlib>
26 #include <map>
27 #include <string>
28 #include <set>
29 #include <vector>
30
31 namespace llvm {
32   class CodeGenRegBank;
33
34   /// CodeGenRegister - Represents a register definition.
35   struct CodeGenRegister {
36     Record *TheDef;
37     unsigned EnumValue;
38     unsigned CostPerUse;
39
40     // Map SubRegIndex -> Register.
41     typedef std::map<Record*, CodeGenRegister*, LessRecord> SubRegMap;
42
43     CodeGenRegister(Record *R, unsigned Enum);
44
45     const std::string &getName() const;
46
47     // Get a map of sub-registers computed lazily.
48     // This includes unique entries for all sub-sub-registers.
49     const SubRegMap &getSubRegs(CodeGenRegBank&);
50
51     const SubRegMap &getSubRegs() const {
52       assert(SubRegsComplete && "Must precompute sub-registers");
53       return SubRegs;
54     }
55
56     // Add sub-registers to OSet following a pre-order defined by the .td file.
57     void addSubRegsPreOrder(SetVector<CodeGenRegister*> &OSet) const;
58
59     // List of super-registers in topological order, small to large.
60     typedef std::vector<CodeGenRegister*> SuperRegList;
61
62     // Get the list of super-registers.
63     // This is only valid after computeDerivedInfo has visited all registers.
64     const SuperRegList &getSuperRegs() const {
65       assert(SubRegsComplete && "Must precompute sub-registers");
66       return SuperRegs;
67     }
68
69     // Order CodeGenRegister pointers by EnumValue.
70     struct Less {
71       bool operator()(const CodeGenRegister *A,
72                       const CodeGenRegister *B) const {
73         return A->EnumValue < B->EnumValue;
74       }
75     };
76
77     // Canonically ordered set.
78     typedef std::set<const CodeGenRegister*, Less> Set;
79
80   private:
81     bool SubRegsComplete;
82     SubRegMap SubRegs;
83     SuperRegList SuperRegs;
84   };
85
86
87   class CodeGenRegisterClass {
88     CodeGenRegister::Set Members;
89     // Allocation orders. Order[0] always contains all registers in Members.
90     std::vector<SmallVector<Record*, 16> > Orders;
91     // Bit mask of sub-classes including this, indexed by their EnumValue.
92     BitVector SubClasses;
93     // List of super-classes, topologocally ordered to have the larger classes
94     // first.  This is the same as sorting by EnumValue.
95     SmallVector<CodeGenRegisterClass*, 4> SuperClasses;
96     Record *TheDef;
97     std::string Name;
98   public:
99     unsigned EnumValue;
100     std::string Namespace;
101     std::vector<MVT::SimpleValueType> VTs;
102     unsigned SpillSize;
103     unsigned SpillAlignment;
104     int CopyCost;
105     bool Allocatable;
106     // Map SubRegIndex -> RegisterClass
107     DenseMap<Record*,Record*> SubRegClasses;
108     std::string AltOrderSelect;
109
110     // Return the Record that defined this class, or NULL if the class was
111     // created by TableGen.
112     Record *getDef() const { return TheDef; }
113
114     const std::string &getName() const { return Name; }
115     std::string getQualifiedName() const;
116     const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
117     unsigned getNumValueTypes() const { return VTs.size(); }
118
119     MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
120       if (VTNum < VTs.size())
121         return VTs[VTNum];
122       assert(0 && "VTNum greater than number of ValueTypes in RegClass!");
123       abort();
124     }
125
126     // Return true if this this class contains the register.
127     bool contains(const CodeGenRegister*) const;
128
129     // Returns true if RC is a subclass.
130     // RC is a sub-class of this class if it is a valid replacement for any
131     // instruction operand where a register of this classis required. It must
132     // satisfy these conditions:
133     //
134     // 1. All RC registers are also in this.
135     // 2. The RC spill size must not be smaller than our spill size.
136     // 3. RC spill alignment must be compatible with ours.
137     //
138     bool hasSubClass(const CodeGenRegisterClass *RC) const {
139       return SubClasses.test(RC->EnumValue);
140     }
141
142     // getSubClasses - Returns a constant BitVector of subclasses indexed by
143     // EnumValue.
144     // The SubClasses vector includs an entry for this class.
145     const BitVector &getSubClasses() const { return SubClasses; };
146
147     // getSuperClasses - Returns a list of super classes ordered by EnumValue.
148     // The array does not include an entry for this class.
149     ArrayRef<CodeGenRegisterClass*> getSuperClasses() const {
150       return SuperClasses;
151     }
152
153     // Returns an ordered list of class members.
154     // The order of registers is the same as in the .td file.
155     // No = 0 is the default allocation order, No = 1 is the first alternative.
156     ArrayRef<Record*> getOrder(unsigned No = 0) const {
157         return Orders[No];
158     }
159
160     // Return the total number of allocation orders available.
161     unsigned getNumOrders() const { return Orders.size(); }
162
163     // Get the set of registers.  This set contains the same registers as
164     // getOrder(0).
165     const CodeGenRegister::Set &getMembers() const { return Members; }
166
167     CodeGenRegisterClass(CodeGenRegBank&, Record *R);
168
169     // Called by CodeGenRegBank::CodeGenRegBank().
170     static void computeSubClasses(ArrayRef<CodeGenRegisterClass*>);
171   };
172
173   // CodeGenRegBank - Represent a target's registers and the relations between
174   // them.
175   class CodeGenRegBank {
176     RecordKeeper &Records;
177     SetTheory Sets;
178
179     std::vector<Record*> SubRegIndices;
180     unsigned NumNamedIndices;
181     std::vector<CodeGenRegister*> Registers;
182     DenseMap<Record*, CodeGenRegister*> Def2Reg;
183
184     std::vector<CodeGenRegisterClass*> RegClasses;
185     DenseMap<Record*, CodeGenRegisterClass*> Def2RC;
186
187     // Composite SubRegIndex instances.
188     // Map (SubRegIndex, SubRegIndex) -> SubRegIndex.
189     typedef DenseMap<std::pair<Record*, Record*>, Record*> CompositeMap;
190     CompositeMap Composite;
191
192     // Populate the Composite map from sub-register relationships.
193     void computeComposites();
194
195   public:
196     CodeGenRegBank(RecordKeeper&);
197
198     SetTheory &getSets() { return Sets; }
199
200     // Sub-register indices. The first NumNamedIndices are defined by the user
201     // in the .td files. The rest are synthesized such that all sub-registers
202     // have a unique name.
203     const std::vector<Record*> &getSubRegIndices() { return SubRegIndices; }
204     unsigned getNumNamedIndices() { return NumNamedIndices; }
205
206     // Map a SubRegIndex Record to its enum value.
207     unsigned getSubRegIndexNo(Record *idx);
208
209     // Find or create a sub-register index representing the A+B composition.
210     Record *getCompositeSubRegIndex(Record *A, Record *B, bool create = false);
211
212     const std::vector<CodeGenRegister*> &getRegisters() { return Registers; }
213
214     // Find a register from its Record def.
215     CodeGenRegister *getReg(Record*);
216
217     ArrayRef<CodeGenRegisterClass*> getRegClasses() const {
218       return RegClasses;
219     }
220
221     // Find a register class from its def.
222     CodeGenRegisterClass *getRegClass(Record*);
223
224     /// getRegisterClassForRegister - Find the register class that contains the
225     /// specified physical register.  If the register is not in a register
226     /// class, return null. If the register is in multiple classes, and the
227     /// classes have a superset-subset relationship and the same set of types,
228     /// return the superclass.  Otherwise return null.
229     const CodeGenRegisterClass* getRegClassForRegister(Record *R);
230
231     // Computed derived records such as missing sub-register indices.
232     void computeDerivedInfo();
233
234     // Compute full overlap sets for every register. These sets include the
235     // rarely used aliases that are neither sub nor super-registers.
236     //
237     // Map[R1].count(R2) is reflexive and symmetric, but not transitive.
238     //
239     // If R1 is a sub-register of R2, Map[R1] is a subset of Map[R2].
240     void computeOverlaps(std::map<const CodeGenRegister*,
241                                   CodeGenRegister::Set> &Map);
242   };
243 }
244
245 #endif