d096ccd3f39d9dfe859b811f1c93daf30208eeae
[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 "Record.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SetVector.h"
23 #include <cstdlib>
24 #include <map>
25 #include <string>
26 #include <set>
27 #include <vector>
28
29 namespace llvm {
30   class CodeGenRegBank;
31
32   /// CodeGenRegister - Represents a register definition.
33   struct CodeGenRegister {
34     Record *TheDef;
35     unsigned EnumValue;
36     unsigned CostPerUse;
37
38     // Map SubRegIndex -> Register.
39     typedef std::map<Record*, CodeGenRegister*, LessRecord> SubRegMap;
40
41     CodeGenRegister(Record *R, unsigned Enum);
42
43     const std::string &getName() const;
44
45     // Get a map of sub-registers computed lazily.
46     // This includes unique entries for all sub-sub-registers.
47     const SubRegMap &getSubRegs(CodeGenRegBank&);
48
49     const SubRegMap &getSubRegs() const {
50       assert(SubRegsComplete && "Must precompute sub-registers");
51       return SubRegs;
52     }
53
54     // Add sub-registers to OSet following a pre-order defined by the .td file.
55     void addSubRegsPreOrder(SetVector<CodeGenRegister*> &OSet) const;
56
57     // List of super-registers in topological order, small to large.
58     typedef std::vector<CodeGenRegister*> SuperRegList;
59
60     // Get the list of super-registers.
61     // This is only valid after computeDerivedInfo has visited all registers.
62     const SuperRegList &getSuperRegs() const {
63       assert(SubRegsComplete && "Must precompute sub-registers");
64       return SuperRegs;
65     }
66
67     // Order CodeGenRegister pointers by EnumValue.
68     struct Less {
69       bool operator()(const CodeGenRegister *A,
70                       const CodeGenRegister *B) const {
71         return A->EnumValue < B->EnumValue;
72       }
73     };
74
75     // Canonically ordered set.
76     typedef std::set<const CodeGenRegister*, Less> Set;
77
78   private:
79     bool SubRegsComplete;
80     SubRegMap SubRegs;
81     SuperRegList SuperRegs;
82   };
83
84
85   class CodeGenRegisterClass {
86     CodeGenRegister::Set Members;
87     std::vector<Record*> Elements;
88   public:
89     Record *TheDef;
90     std::string Namespace;
91     std::vector<MVT::SimpleValueType> VTs;
92     unsigned SpillSize;
93     unsigned SpillAlignment;
94     int CopyCost;
95     bool Allocatable;
96     // Map SubRegIndex -> RegisterClass
97     DenseMap<Record*,Record*> SubRegClasses;
98     std::string MethodProtos, MethodBodies;
99
100     const std::string &getName() const;
101     const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
102     unsigned getNumValueTypes() const { return VTs.size(); }
103
104     MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
105       if (VTNum < VTs.size())
106         return VTs[VTNum];
107       assert(0 && "VTNum greater than number of ValueTypes in RegClass!");
108       abort();
109     }
110
111     // Return true if this this class contains the register.
112     bool contains(const CodeGenRegister*) const;
113
114     // Returns true if RC is a subclass.
115     // RC is a sub-class of this class if it is a valid replacement for any
116     // instruction operand where a register of this classis required. It must
117     // satisfy these conditions:
118     //
119     // 1. All RC registers are also in this.
120     // 2. The RC spill size must not be smaller than our spill size.
121     // 3. RC spill alignment must be compatible with ours.
122     //
123     bool hasSubClass(const CodeGenRegisterClass *RC) const;
124
125     // Returns an ordered list of class members.
126     // The order of registers is the same as in the .td file.
127     ArrayRef<Record*> getOrder() const {
128       return Elements;
129     }
130
131     CodeGenRegisterClass(CodeGenRegBank&, Record *R);
132   };
133
134   // CodeGenRegBank - Represent a target's registers and the relations between
135   // them.
136   class CodeGenRegBank {
137     RecordKeeper &Records;
138     std::vector<Record*> SubRegIndices;
139     unsigned NumNamedIndices;
140     std::vector<CodeGenRegister> Registers;
141     DenseMap<Record*, CodeGenRegister*> Def2Reg;
142
143     std::vector<CodeGenRegisterClass> RegClasses;
144     DenseMap<Record*, CodeGenRegisterClass*> Def2RC;
145
146     // Composite SubRegIndex instances.
147     // Map (SubRegIndex, SubRegIndex) -> SubRegIndex.
148     typedef DenseMap<std::pair<Record*, Record*>, Record*> CompositeMap;
149     CompositeMap Composite;
150
151     // Populate the Composite map from sub-register relationships.
152     void computeComposites();
153
154   public:
155     CodeGenRegBank(RecordKeeper&);
156
157     // Sub-register indices. The first NumNamedIndices are defined by the user
158     // in the .td files. The rest are synthesized such that all sub-registers
159     // have a unique name.
160     const std::vector<Record*> &getSubRegIndices() { return SubRegIndices; }
161     unsigned getNumNamedIndices() { return NumNamedIndices; }
162
163     // Map a SubRegIndex Record to its enum value.
164     unsigned getSubRegIndexNo(Record *idx);
165
166     // Find or create a sub-register index representing the A+B composition.
167     Record *getCompositeSubRegIndex(Record *A, Record *B, bool create = false);
168
169     const std::vector<CodeGenRegister> &getRegisters() { return Registers; }
170
171     // Find a register from its Record def.
172     CodeGenRegister *getReg(Record*);
173
174     const std::vector<CodeGenRegisterClass> &getRegClasses() {
175       return RegClasses;
176     }
177
178     // Find a register class from its def.
179     CodeGenRegisterClass *getRegClass(Record*);
180
181     /// getRegisterClassForRegister - Find the register class that contains the
182     /// specified physical register.  If the register is not in a register
183     /// class, return null. If the register is in multiple classes, and the
184     /// classes have a superset-subset relationship and the same set of types,
185     /// return the superclass.  Otherwise return null.
186     const CodeGenRegisterClass* getRegClassForRegister(Record *R);
187
188     // Computed derived records such as missing sub-register indices.
189     void computeDerivedInfo();
190
191     // Compute full overlap sets for every register. These sets include the
192     // rarely used aliases that are neither sub nor super-registers.
193     //
194     // Map[R1].count(R2) is reflexive and symmetric, but not transitive.
195     //
196     // If R1 is a sub-register of R2, Map[R1] is a subset of Map[R2].
197     void computeOverlaps(std::map<const CodeGenRegister*,
198                                   CodeGenRegister::Set> &Map);
199   };
200 }
201
202 #endif