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