f73519ebbf73521cfbca4fcd63d75ba665f84fec
[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 "llvm/Support/ErrorHandling.h"
26 #include <cstdlib>
27 #include <map>
28 #include <string>
29 #include <set>
30 #include <vector>
31
32 namespace llvm {
33   class CodeGenRegBank;
34
35   /// CodeGenSubRegIndex - Represents a sub-register index.
36   class CodeGenSubRegIndex {
37     Record *const TheDef;
38     const unsigned EnumValue;
39
40   public:
41     CodeGenSubRegIndex(Record *R, unsigned Enum);
42
43     const std::string &getName() const;
44     std::string getNamespace() const;
45     std::string getQualifiedName() const;
46
47     // Order CodeGenSubRegIndex pointers by EnumValue.
48     struct Less {
49       bool operator()(const CodeGenSubRegIndex *A,
50                       const CodeGenSubRegIndex *B) const {
51         assert(A && B);
52         return A->EnumValue < B->EnumValue;
53       }
54     };
55
56     // Map of composite subreg indices.
57     typedef std::map<CodeGenSubRegIndex*, CodeGenSubRegIndex*, Less> CompMap;
58
59     // Returns the subreg index that results from composing this with Idx.
60     // Returns NULL if this and Idx don't compose.
61     CodeGenSubRegIndex *compose(CodeGenSubRegIndex *Idx) const {
62       CompMap::const_iterator I = Composed.find(Idx);
63       return I == Composed.end() ? 0 : I->second;
64     }
65
66     // Add a composite subreg index: this+A = B.
67     // Return a conflicting composite, or NULL
68     CodeGenSubRegIndex *addComposite(CodeGenSubRegIndex *A,
69                                      CodeGenSubRegIndex *B) {
70       std::pair<CompMap::iterator, bool> Ins =
71         Composed.insert(std::make_pair(A, B));
72       return (Ins.second || Ins.first->second == B) ? 0 : Ins.first->second;
73     }
74
75     // Update the composite maps of components specified in 'ComposedOf'.
76     void updateComponents(CodeGenRegBank&);
77
78     // Clean out redundant composite mappings.
79     void cleanComposites();
80
81     // Return the map of composites.
82     const CompMap &getComposites() const { return Composed; }
83
84   private:
85     CompMap Composed;
86   };
87
88   /// CodeGenRegister - Represents a register definition.
89   struct CodeGenRegister {
90     Record *TheDef;
91     unsigned EnumValue;
92     unsigned CostPerUse;
93     bool CoveredBySubRegs;
94
95     // Map SubRegIndex -> Register.
96     typedef std::map<CodeGenSubRegIndex*, CodeGenRegister*,
97                      CodeGenSubRegIndex::Less> SubRegMap;
98
99     CodeGenRegister(Record *R, unsigned Enum);
100
101     const std::string &getName() const;
102
103     // Get a map of sub-registers computed lazily.
104     // This includes unique entries for all sub-sub-registers.
105     const SubRegMap &getSubRegs(CodeGenRegBank&);
106
107     const SubRegMap &getSubRegs() const {
108       assert(SubRegsComplete && "Must precompute sub-registers");
109       return SubRegs;
110     }
111
112     // Add sub-registers to OSet following a pre-order defined by the .td file.
113     void addSubRegsPreOrder(SetVector<const CodeGenRegister*> &OSet,
114                             CodeGenRegBank&) const;
115
116     // List of super-registers in topological order, small to large.
117     typedef std::vector<const CodeGenRegister*> SuperRegList;
118
119     // Get the list of super-registers.
120     // This is only valid after computeDerivedInfo has visited all registers.
121     const SuperRegList &getSuperRegs() const {
122       assert(SubRegsComplete && "Must precompute sub-registers");
123       return SuperRegs;
124     }
125
126     // List of register units in ascending order.
127     typedef SmallVector<unsigned, 16> RegUnitList;
128
129     // Get the list of register units.
130     // This is only valid after getSubRegs() completes.
131     const RegUnitList &getRegUnits() const { return RegUnits; }
132
133     // Order CodeGenRegister pointers by EnumValue.
134     struct Less {
135       bool operator()(const CodeGenRegister *A,
136                       const CodeGenRegister *B) const {
137         assert(A && B);
138         return A->EnumValue < B->EnumValue;
139       }
140     };
141
142     // Canonically ordered set.
143     typedef std::set<const CodeGenRegister*, Less> Set;
144
145   private:
146     bool SubRegsComplete;
147     SubRegMap SubRegs;
148     SuperRegList SuperRegs;
149     RegUnitList RegUnits;
150   };
151
152
153   class CodeGenRegisterClass {
154     CodeGenRegister::Set Members;
155     // Allocation orders. Order[0] always contains all registers in Members.
156     std::vector<SmallVector<Record*, 16> > Orders;
157     // Bit mask of sub-classes including this, indexed by their EnumValue.
158     BitVector SubClasses;
159     // List of super-classes, topologocally ordered to have the larger classes
160     // first.  This is the same as sorting by EnumValue.
161     SmallVector<CodeGenRegisterClass*, 4> SuperClasses;
162     Record *TheDef;
163     std::string Name;
164
165     // For a synthesized class, inherit missing properties from the nearest
166     // super-class.
167     void inheritProperties(CodeGenRegBank&);
168
169     // Map SubRegIndex -> sub-class.  This is the largest sub-class where all
170     // registers have a SubRegIndex sub-register.
171     DenseMap<CodeGenSubRegIndex*, CodeGenRegisterClass*> SubClassWithSubReg;
172
173     // Map SubRegIndex -> set of super-reg classes.  This is all register
174     // classes SuperRC such that:
175     //
176     //   R:SubRegIndex in this RC for all R in SuperRC.
177     //
178     DenseMap<CodeGenSubRegIndex*,
179              SmallPtrSet<CodeGenRegisterClass*, 8> > SuperRegClasses;
180   public:
181     unsigned EnumValue;
182     std::string Namespace;
183     std::vector<MVT::SimpleValueType> VTs;
184     unsigned SpillSize;
185     unsigned SpillAlignment;
186     int CopyCost;
187     bool Allocatable;
188     // Map SubRegIndex -> RegisterClass
189     DenseMap<Record*,Record*> SubRegClasses;
190     std::string AltOrderSelect;
191
192     // Return the Record that defined this class, or NULL if the class was
193     // created by TableGen.
194     Record *getDef() const { return TheDef; }
195
196     const std::string &getName() const { return Name; }
197     std::string getQualifiedName() const;
198     const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
199     unsigned getNumValueTypes() const { return VTs.size(); }
200
201     MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
202       if (VTNum < VTs.size())
203         return VTs[VTNum];
204       llvm_unreachable("VTNum greater than number of ValueTypes in RegClass!");
205     }
206
207     // Return true if this this class contains the register.
208     bool contains(const CodeGenRegister*) const;
209
210     // Returns true if RC is a subclass.
211     // RC is a sub-class of this class if it is a valid replacement for any
212     // instruction operand where a register of this classis required. It must
213     // satisfy these conditions:
214     //
215     // 1. All RC registers are also in this.
216     // 2. The RC spill size must not be smaller than our spill size.
217     // 3. RC spill alignment must be compatible with ours.
218     //
219     bool hasSubClass(const CodeGenRegisterClass *RC) const {
220       return SubClasses.test(RC->EnumValue);
221     }
222
223     // getSubClassWithSubReg - Returns the largest sub-class where all
224     // registers have a SubIdx sub-register.
225     CodeGenRegisterClass*
226     getSubClassWithSubReg(CodeGenSubRegIndex *SubIdx) const {
227       return SubClassWithSubReg.lookup(SubIdx);
228     }
229
230     void setSubClassWithSubReg(CodeGenSubRegIndex *SubIdx,
231                                CodeGenRegisterClass *SubRC) {
232       SubClassWithSubReg[SubIdx] = SubRC;
233     }
234
235     // getSuperRegClasses - Returns a bit vector of all register classes
236     // containing only SubIdx super-registers of this class.
237     void getSuperRegClasses(CodeGenSubRegIndex *SubIdx, BitVector &Out) const;
238
239     // addSuperRegClass - Add a class containing only SudIdx super-registers.
240     void addSuperRegClass(CodeGenSubRegIndex *SubIdx,
241                           CodeGenRegisterClass *SuperRC) {
242       SuperRegClasses[SubIdx].insert(SuperRC);
243     }
244
245     // getSubClasses - Returns a constant BitVector of subclasses indexed by
246     // EnumValue.
247     // The SubClasses vector includs an entry for this class.
248     const BitVector &getSubClasses() const { return SubClasses; }
249
250     // getSuperClasses - Returns a list of super classes ordered by EnumValue.
251     // The array does not include an entry for this class.
252     ArrayRef<CodeGenRegisterClass*> getSuperClasses() const {
253       return SuperClasses;
254     }
255
256     // Returns an ordered list of class members.
257     // The order of registers is the same as in the .td file.
258     // No = 0 is the default allocation order, No = 1 is the first alternative.
259     ArrayRef<Record*> getOrder(unsigned No = 0) const {
260         return Orders[No];
261     }
262
263     // Return the total number of allocation orders available.
264     unsigned getNumOrders() const { return Orders.size(); }
265
266     // Get the set of registers.  This set contains the same registers as
267     // getOrder(0).
268     const CodeGenRegister::Set &getMembers() const { return Members; }
269
270     CodeGenRegisterClass(CodeGenRegBank&, Record *R);
271
272     // A key representing the parts of a register class used for forming
273     // sub-classes.  Note the ordering provided by this key is not the same as
274     // the topological order used for the EnumValues.
275     struct Key {
276       const CodeGenRegister::Set *Members;
277       unsigned SpillSize;
278       unsigned SpillAlignment;
279
280       Key(const Key &O)
281         : Members(O.Members),
282           SpillSize(O.SpillSize),
283           SpillAlignment(O.SpillAlignment) {}
284
285       Key(const CodeGenRegister::Set *M, unsigned S = 0, unsigned A = 0)
286         : Members(M), SpillSize(S), SpillAlignment(A) {}
287
288       Key(const CodeGenRegisterClass &RC)
289         : Members(&RC.getMembers()),
290           SpillSize(RC.SpillSize),
291           SpillAlignment(RC.SpillAlignment) {}
292
293       // Lexicographical order of (Members, SpillSize, SpillAlignment).
294       bool operator<(const Key&) const;
295     };
296
297     // Create a non-user defined register class.
298     CodeGenRegisterClass(StringRef Name, Key Props);
299
300     // Called by CodeGenRegBank::CodeGenRegBank().
301     static void computeSubClasses(CodeGenRegBank&);
302   };
303
304   // CodeGenRegBank - Represent a target's registers and the relations between
305   // them.
306   class CodeGenRegBank {
307     RecordKeeper &Records;
308     SetTheory Sets;
309
310     // SubRegIndices.
311     std::vector<CodeGenSubRegIndex*> SubRegIndices;
312     DenseMap<Record*, CodeGenSubRegIndex*> Def2SubRegIdx;
313     unsigned NumNamedIndices;
314
315     // Registers.
316     std::vector<CodeGenRegister*> Registers;
317     DenseMap<Record*, CodeGenRegister*> Def2Reg;
318     unsigned NumRegUnits;
319
320     // Register classes.
321     std::vector<CodeGenRegisterClass*> RegClasses;
322     DenseMap<Record*, CodeGenRegisterClass*> Def2RC;
323     typedef std::map<CodeGenRegisterClass::Key, CodeGenRegisterClass*> RCKeyMap;
324     RCKeyMap Key2RC;
325
326     // Add RC to *2RC maps.
327     void addToMaps(CodeGenRegisterClass*);
328
329     // Create a synthetic sub-class if it is missing.
330     CodeGenRegisterClass *getOrCreateSubClass(const CodeGenRegisterClass *RC,
331                                               const CodeGenRegister::Set *Membs,
332                                               StringRef Name);
333
334     // Infer missing register classes.
335     void computeInferredRegisterClasses();
336     void inferCommonSubClass(CodeGenRegisterClass *RC);
337     void inferSubClassWithSubReg(CodeGenRegisterClass *RC);
338     void inferMatchingSuperRegClass(CodeGenRegisterClass *RC,
339                                     unsigned FirstSubRegRC = 0);
340
341     // Populate the Composite map from sub-register relationships.
342     void computeComposites();
343
344   public:
345     CodeGenRegBank(RecordKeeper&);
346
347     SetTheory &getSets() { return Sets; }
348
349     // Sub-register indices. The first NumNamedIndices are defined by the user
350     // in the .td files. The rest are synthesized such that all sub-registers
351     // have a unique name.
352     ArrayRef<CodeGenSubRegIndex*> getSubRegIndices() { return SubRegIndices; }
353     unsigned getNumNamedIndices() { return NumNamedIndices; }
354
355     // Find a SubRegIndex form its Record def.
356     CodeGenSubRegIndex *getSubRegIdx(Record*);
357
358     // Find or create a sub-register index representing the A+B composition.
359     CodeGenSubRegIndex *getCompositeSubRegIndex(CodeGenSubRegIndex *A,
360                                                 CodeGenSubRegIndex *B);
361
362     const std::vector<CodeGenRegister*> &getRegisters() { return Registers; }
363
364     // Find a register from its Record def.
365     CodeGenRegister *getReg(Record*);
366
367     unsigned newRegUnit() { return NumRegUnits++; }
368
369     ArrayRef<CodeGenRegisterClass*> getRegClasses() const {
370       return RegClasses;
371     }
372
373     // Find a register class from its def.
374     CodeGenRegisterClass *getRegClass(Record*);
375
376     /// getRegisterClassForRegister - Find the register class that contains the
377     /// specified physical register.  If the register is not in a register
378     /// class, return null. If the register is in multiple classes, and the
379     /// classes have a superset-subset relationship and the same set of types,
380     /// return the superclass.  Otherwise return null.
381     const CodeGenRegisterClass* getRegClassForRegister(Record *R);
382
383     // Computed derived records such as missing sub-register indices.
384     void computeDerivedInfo();
385
386     // Compute full overlap sets for every register. These sets include the
387     // rarely used aliases that are neither sub nor super-registers.
388     //
389     // Map[R1].count(R2) is reflexive and symmetric, but not transitive.
390     //
391     // If R1 is a sub-register of R2, Map[R1] is a subset of Map[R2].
392     void computeOverlaps(std::map<const CodeGenRegister*,
393                                   CodeGenRegister::Set> &Map);
394
395     // Compute the set of registers completely covered by the registers in Regs.
396     // The returned BitVector will have a bit set for each register in Regs,
397     // all sub-registers, and all super-registers that are covered by the
398     // registers in Regs.
399     //
400     // This is used to compute the mask of call-preserved registers from a list
401     // of callee-saves.
402     BitVector computeCoveredRegisters(ArrayRef<Record*> Regs);
403   };
404 }
405
406 #endif