1 //===- CodeGenRegisters.h - Register and RegisterClass Info -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines structures to encapsulate information gleaned from the
11 // target register and register class definitions.
13 //===----------------------------------------------------------------------===//
15 #ifndef CODEGEN_REGISTERS_H
16 #define CODEGEN_REGISTERS_H
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"
35 /// CodeGenSubRegIndex - Represents a sub-register index.
36 class CodeGenSubRegIndex {
38 const unsigned EnumValue;
41 CodeGenSubRegIndex(Record *R, unsigned Enum);
43 const std::string &getName() const;
44 std::string getNamespace() const;
45 std::string getQualifiedName() const;
47 // Order CodeGenSubRegIndex pointers by EnumValue.
49 bool operator()(const CodeGenSubRegIndex *A,
50 const CodeGenSubRegIndex *B) const {
52 return A->EnumValue < B->EnumValue;
56 // Map of composite subreg indices.
57 typedef std::map<CodeGenSubRegIndex*, CodeGenSubRegIndex*, Less> CompMap;
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;
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;
75 // Update the composite maps of components specified in 'ComposedOf'.
76 void updateComponents(CodeGenRegBank&);
78 // Clean out redundant composite mappings.
79 void cleanComposites();
81 // Return the map of composites.
82 const CompMap &getComposites() const { return Composed; }
88 /// CodeGenRegister - Represents a register definition.
89 struct CodeGenRegister {
93 bool CoveredBySubRegs;
95 // Map SubRegIndex -> Register.
96 typedef std::map<CodeGenSubRegIndex*, CodeGenRegister*,
97 CodeGenSubRegIndex::Less> SubRegMap;
99 CodeGenRegister(Record *R, unsigned Enum);
101 const std::string &getName() const;
103 // Get a map of sub-registers computed lazily.
104 // This includes unique entries for all sub-sub-registers.
105 const SubRegMap &getSubRegs(CodeGenRegBank&);
107 const SubRegMap &getSubRegs() const {
108 assert(SubRegsComplete && "Must precompute sub-registers");
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;
116 // List of super-registers in topological order, small to large.
117 typedef std::vector<const CodeGenRegister*> SuperRegList;
119 // Get the list of super-registers. This is valid after getSubReg
120 // visits all registers during RegBank construction.
121 const SuperRegList &getSuperRegs() const {
122 assert(SubRegsComplete && "Must precompute sub-registers");
126 // List of register units in ascending order.
127 typedef SmallVector<unsigned, 16> RegUnitList;
129 // Get the list of register units.
130 // This is only valid after getSubRegs() completes.
131 const RegUnitList &getRegUnits() const { return RegUnits; }
133 // Inherit register units from subregisters.
134 // Return true if the RegUnits changed.
135 bool inheritRegUnits(CodeGenRegBank &RegBank);
137 // Adopt a register unit for pressure tracking.
138 // A unit is adopted iff its unit number is >= NumNativeRegUnits.
139 void adoptRegUnit(unsigned RUID) { RegUnits.push_back(RUID); }
141 // Get the sum of this register's register unit weights.
142 unsigned getWeight(const CodeGenRegBank &RegBank) const;
144 // Order CodeGenRegister pointers by EnumValue.
146 bool operator()(const CodeGenRegister *A,
147 const CodeGenRegister *B) const {
149 return A->EnumValue < B->EnumValue;
153 // Canonically ordered set.
154 typedef std::set<const CodeGenRegister*, Less> Set;
157 bool SubRegsComplete;
159 SuperRegList SuperRegs;
160 RegUnitList RegUnits;
164 class CodeGenRegisterClass {
165 CodeGenRegister::Set Members;
166 // Allocation orders. Order[0] always contains all registers in Members.
167 std::vector<SmallVector<Record*, 16> > Orders;
168 // Bit mask of sub-classes including this, indexed by their EnumValue.
169 BitVector SubClasses;
170 // List of super-classes, topologocally ordered to have the larger classes
171 // first. This is the same as sorting by EnumValue.
172 SmallVector<CodeGenRegisterClass*, 4> SuperClasses;
176 // For a synthesized class, inherit missing properties from the nearest
178 void inheritProperties(CodeGenRegBank&);
180 // Map SubRegIndex -> sub-class. This is the largest sub-class where all
181 // registers have a SubRegIndex sub-register.
182 DenseMap<CodeGenSubRegIndex*, CodeGenRegisterClass*> SubClassWithSubReg;
184 // Map SubRegIndex -> set of super-reg classes. This is all register
185 // classes SuperRC such that:
187 // R:SubRegIndex in this RC for all R in SuperRC.
189 DenseMap<CodeGenSubRegIndex*,
190 SmallPtrSet<CodeGenRegisterClass*, 8> > SuperRegClasses;
193 std::string Namespace;
194 std::vector<MVT::SimpleValueType> VTs;
196 unsigned SpillAlignment;
199 // Map SubRegIndex -> RegisterClass
200 DenseMap<Record*,Record*> SubRegClasses;
201 std::string AltOrderSelect;
203 // Return the Record that defined this class, or NULL if the class was
204 // created by TableGen.
205 Record *getDef() const { return TheDef; }
207 const std::string &getName() const { return Name; }
208 std::string getQualifiedName() const;
209 const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
210 unsigned getNumValueTypes() const { return VTs.size(); }
212 MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
213 if (VTNum < VTs.size())
215 llvm_unreachable("VTNum greater than number of ValueTypes in RegClass!");
218 // Return true if this this class contains the register.
219 bool contains(const CodeGenRegister*) const;
221 // Returns true if RC is a subclass.
222 // RC is a sub-class of this class if it is a valid replacement for any
223 // instruction operand where a register of this classis required. It must
224 // satisfy these conditions:
226 // 1. All RC registers are also in this.
227 // 2. The RC spill size must not be smaller than our spill size.
228 // 3. RC spill alignment must be compatible with ours.
230 bool hasSubClass(const CodeGenRegisterClass *RC) const {
231 return SubClasses.test(RC->EnumValue);
234 // getSubClassWithSubReg - Returns the largest sub-class where all
235 // registers have a SubIdx sub-register.
236 CodeGenRegisterClass*
237 getSubClassWithSubReg(CodeGenSubRegIndex *SubIdx) const {
238 return SubClassWithSubReg.lookup(SubIdx);
241 void setSubClassWithSubReg(CodeGenSubRegIndex *SubIdx,
242 CodeGenRegisterClass *SubRC) {
243 SubClassWithSubReg[SubIdx] = SubRC;
246 // getSuperRegClasses - Returns a bit vector of all register classes
247 // containing only SubIdx super-registers of this class.
248 void getSuperRegClasses(CodeGenSubRegIndex *SubIdx, BitVector &Out) const;
250 // addSuperRegClass - Add a class containing only SudIdx super-registers.
251 void addSuperRegClass(CodeGenSubRegIndex *SubIdx,
252 CodeGenRegisterClass *SuperRC) {
253 SuperRegClasses[SubIdx].insert(SuperRC);
256 // getSubClasses - Returns a constant BitVector of subclasses indexed by
258 // The SubClasses vector includs an entry for this class.
259 const BitVector &getSubClasses() const { return SubClasses; }
261 // getSuperClasses - Returns a list of super classes ordered by EnumValue.
262 // The array does not include an entry for this class.
263 ArrayRef<CodeGenRegisterClass*> getSuperClasses() const {
267 // Returns an ordered list of class members.
268 // The order of registers is the same as in the .td file.
269 // No = 0 is the default allocation order, No = 1 is the first alternative.
270 ArrayRef<Record*> getOrder(unsigned No = 0) const {
274 // Return the total number of allocation orders available.
275 unsigned getNumOrders() const { return Orders.size(); }
277 // Get the set of registers. This set contains the same registers as
279 const CodeGenRegister::Set &getMembers() const { return Members; }
281 CodeGenRegisterClass(CodeGenRegBank&, Record *R);
283 // A key representing the parts of a register class used for forming
284 // sub-classes. Note the ordering provided by this key is not the same as
285 // the topological order used for the EnumValues.
287 const CodeGenRegister::Set *Members;
289 unsigned SpillAlignment;
292 : Members(O.Members),
293 SpillSize(O.SpillSize),
294 SpillAlignment(O.SpillAlignment) {}
296 Key(const CodeGenRegister::Set *M, unsigned S = 0, unsigned A = 0)
297 : Members(M), SpillSize(S), SpillAlignment(A) {}
299 Key(const CodeGenRegisterClass &RC)
300 : Members(&RC.getMembers()),
301 SpillSize(RC.SpillSize),
302 SpillAlignment(RC.SpillAlignment) {}
304 // Lexicographical order of (Members, SpillSize, SpillAlignment).
305 bool operator<(const Key&) const;
308 // Create a non-user defined register class.
309 CodeGenRegisterClass(StringRef Name, Key Props);
311 // Called by CodeGenRegBank::CodeGenRegBank().
312 static void computeSubClasses(CodeGenRegBank&);
315 // CodeGenRegBank - Represent a target's registers and the relations between
317 class CodeGenRegBank {
318 RecordKeeper &Records;
322 std::vector<CodeGenSubRegIndex*> SubRegIndices;
323 DenseMap<Record*, CodeGenSubRegIndex*> Def2SubRegIdx;
324 unsigned NumNamedIndices;
327 std::vector<CodeGenRegister*> Registers;
328 DenseMap<Record*, CodeGenRegister*> Def2Reg;
329 unsigned NumNativeRegUnits;
330 unsigned NumRegUnits; // # native + adopted register units.
332 // Map each register unit to a weight (for register pressure).
333 // Includes native and adopted register units.
334 std::vector<unsigned> RegUnitWeights;
337 std::vector<CodeGenRegisterClass*> RegClasses;
338 DenseMap<Record*, CodeGenRegisterClass*> Def2RC;
339 typedef std::map<CodeGenRegisterClass::Key, CodeGenRegisterClass*> RCKeyMap;
342 // Add RC to *2RC maps.
343 void addToMaps(CodeGenRegisterClass*);
345 // Create a synthetic sub-class if it is missing.
346 CodeGenRegisterClass *getOrCreateSubClass(const CodeGenRegisterClass *RC,
347 const CodeGenRegister::Set *Membs,
350 // Infer missing register classes.
351 void computeInferredRegisterClasses();
352 void inferCommonSubClass(CodeGenRegisterClass *RC);
353 void inferSubClassWithSubReg(CodeGenRegisterClass *RC);
354 void inferMatchingSuperRegClass(CodeGenRegisterClass *RC,
355 unsigned FirstSubRegRC = 0);
357 // Compute a weight for each register unit created during getSubRegs.
358 void computeRegUnitWeights();
360 // Populate the Composite map from sub-register relationships.
361 void computeComposites();
364 CodeGenRegBank(RecordKeeper&);
366 SetTheory &getSets() { return Sets; }
368 // Sub-register indices. The first NumNamedIndices are defined by the user
369 // in the .td files. The rest are synthesized such that all sub-registers
370 // have a unique name.
371 ArrayRef<CodeGenSubRegIndex*> getSubRegIndices() { return SubRegIndices; }
372 unsigned getNumNamedIndices() { return NumNamedIndices; }
374 // Find a SubRegIndex form its Record def.
375 CodeGenSubRegIndex *getSubRegIdx(Record*);
377 // Find or create a sub-register index representing the A+B composition.
378 CodeGenSubRegIndex *getCompositeSubRegIndex(CodeGenSubRegIndex *A,
379 CodeGenSubRegIndex *B);
381 const std::vector<CodeGenRegister*> &getRegisters() { return Registers; }
383 // Find a register from its Record def.
384 CodeGenRegister *getReg(Record*);
386 // Get a Register's index into the Registers array.
387 unsigned getRegIndex(const CodeGenRegister *Reg) const {
388 return Reg->EnumValue - 1;
391 // Create a new non-native register unit that can be adopted by a register
392 // to increase its pressure. Note that NumNativeRegUnits is not increased.
393 unsigned newRegUnit(unsigned Weight) {
394 if (!RegUnitWeights.empty()) {
395 RegUnitWeights.resize(NumRegUnits+1);
396 RegUnitWeights[NumRegUnits] = Weight;
398 return NumRegUnits++;
401 bool isNativeUnit(unsigned RUID) {
402 return RUID < NumNativeRegUnits;
405 ArrayRef<CodeGenRegisterClass*> getRegClasses() const {
409 // Find a register class from its def.
410 CodeGenRegisterClass *getRegClass(Record*);
412 /// getRegisterClassForRegister - Find the register class that contains the
413 /// specified physical register. If the register is not in a register
414 /// class, return null. If the register is in multiple classes, and the
415 /// classes have a superset-subset relationship and the same set of types,
416 /// return the superclass. Otherwise return null.
417 const CodeGenRegisterClass* getRegClassForRegister(Record *R);
419 unsigned getRegUnitWeight(unsigned RUID) const {
420 return RegUnitWeights[RUID];
423 void increaseRegUnitWeight(unsigned RUID, unsigned Inc) {
424 RegUnitWeights[RUID] += Inc;
427 // Computed derived records such as missing sub-register indices.
428 void computeDerivedInfo();
430 // Compute full overlap sets for every register. These sets include the
431 // rarely used aliases that are neither sub nor super-registers.
433 // Map[R1].count(R2) is reflexive and symmetric, but not transitive.
435 // If R1 is a sub-register of R2, Map[R1] is a subset of Map[R2].
436 void computeOverlaps(std::map<const CodeGenRegister*,
437 CodeGenRegister::Set> &Map);
439 // Compute the set of registers completely covered by the registers in Regs.
440 // The returned BitVector will have a bit set for each register in Regs,
441 // all sub-registers, and all super-registers that are covered by the
442 // registers in Regs.
444 // This is used to compute the mask of call-preserved registers from a list
446 BitVector computeCoveredRegisters(ArrayRef<Record*> Regs);