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
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SetVector.h"
31 /// CodeGenRegister - Represents a register definition.
32 struct CodeGenRegister {
37 // Map SubRegIndex -> Register.
38 typedef std::map<Record*, CodeGenRegister*, LessRecord> SubRegMap;
40 CodeGenRegister(Record *R, unsigned Enum);
42 const std::string &getName() const;
44 // Get a map of sub-registers computed lazily.
45 // This includes unique entries for all sub-sub-registers.
46 const SubRegMap &getSubRegs(CodeGenRegBank&);
48 const SubRegMap &getSubRegs() const {
49 assert(SubRegsComplete && "Must precompute sub-registers");
53 // Add sub-registers to OSet following a pre-order defined by the .td file.
54 void addSubRegsPreOrder(SetVector<CodeGenRegister*> &OSet) const;
56 // List of super-registers in topological order, small to large.
57 typedef std::vector<CodeGenRegister*> SuperRegList;
59 // Get the list of super-registers.
60 // This is only valid after computeDerivedInfo has visited all registers.
61 const SuperRegList &getSuperRegs() const {
62 assert(SubRegsComplete && "Must precompute sub-registers");
66 // Order CodeGenRegister pointers by EnumValue.
68 bool operator()(const CodeGenRegister *A, const CodeGenRegister *B) {
69 return A->EnumValue < B->EnumValue;
73 // Canonically ordered set.
74 typedef std::set<CodeGenRegister*, Less> Set;
79 SuperRegList SuperRegs;
83 struct CodeGenRegisterClass {
85 std::string Namespace;
86 std::vector<Record*> Elements;
87 std::vector<MVT::SimpleValueType> VTs;
89 unsigned SpillAlignment;
92 // Map SubRegIndex -> RegisterClass
93 DenseMap<Record*,Record*> SubRegClasses;
94 std::string MethodProtos, MethodBodies;
96 const std::string &getName() const;
97 const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
98 unsigned getNumValueTypes() const { return VTs.size(); }
100 MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
101 if (VTNum < VTs.size())
103 assert(0 && "VTNum greater than number of ValueTypes in RegClass!");
107 bool containsRegister(Record *R) const {
108 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
109 if (Elements[i] == R) return true;
113 // Returns true if RC is a strict subclass.
114 // RC is a sub-class of this class if it is a valid replacement for any
115 // instruction operand where a register of this classis required. It must
116 // satisfy these conditions:
118 // 1. All RC registers are also in this.
119 // 2. The RC spill size must not be smaller than our spill size.
120 // 3. RC spill alignment must be compatible with ours.
122 bool hasSubClass(const CodeGenRegisterClass *RC) const {
124 if (RC->Elements.size() > Elements.size() ||
125 (SpillAlignment && RC->SpillAlignment % SpillAlignment) ||
126 SpillSize > RC->SpillSize)
129 std::set<Record*> RegSet;
130 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
131 Record *Reg = Elements[i];
135 for (unsigned i = 0, e = RC->Elements.size(); i != e; ++i) {
136 Record *Reg = RC->Elements[i];
137 if (!RegSet.count(Reg))
144 CodeGenRegisterClass(Record *R);
147 // CodeGenRegBank - Represent a target's registers and the relations between
149 class CodeGenRegBank {
150 RecordKeeper &Records;
151 std::vector<Record*> SubRegIndices;
152 unsigned NumNamedIndices;
153 std::vector<CodeGenRegister> Registers;
154 DenseMap<Record*, CodeGenRegister*> Def2Reg;
156 // Composite SubRegIndex instances.
157 // Map (SubRegIndex, SubRegIndex) -> SubRegIndex.
158 typedef DenseMap<std::pair<Record*, Record*>, Record*> CompositeMap;
159 CompositeMap Composite;
161 // Populate the Composite map from sub-register relationships.
162 void computeComposites();
165 CodeGenRegBank(RecordKeeper&);
167 // Sub-register indices. The first NumNamedIndices are defined by the user
168 // in the .td files. The rest are synthesized such that all sub-registers
169 // have a unique name.
170 const std::vector<Record*> &getSubRegIndices() { return SubRegIndices; }
171 unsigned getNumNamedIndices() { return NumNamedIndices; }
173 // Map a SubRegIndex Record to its enum value.
174 unsigned getSubRegIndexNo(Record *idx);
176 // Find or create a sub-register index representing the A+B composition.
177 Record *getCompositeSubRegIndex(Record *A, Record *B, bool create = false);
179 const std::vector<CodeGenRegister> &getRegisters() { return Registers; }
181 // Find a register from its Record def.
182 CodeGenRegister *getReg(Record*);
184 // Computed derived records such as missing sub-register indices.
185 void computeDerivedInfo();
187 // Compute full overlap sets for every register. These sets include the
188 // rarely used aliases that are neither sub nor super-registers.
190 // Map[R1].count(R2) is reflexive and symmetric, but not transitive.
192 // If R1 is a sub-register of R2, Map[R1] is a subset of Map[R2].
193 void computeOverlaps(std::map<const CodeGenRegister*,
194 CodeGenRegister::Set> &Map);