1f4a2713aSLionel Sambuc //===- RegisterInfoEmitter.cpp - Generate a Register File Desc. -*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This tablegen backend is responsible for emitting a description of a target
11f4a2713aSLionel Sambuc // register file for a code generator. It uses instances of the Register,
12f4a2713aSLionel Sambuc // RegisterAliases, and RegisterClass classes to gather this information.
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
15f4a2713aSLionel Sambuc
16f4a2713aSLionel Sambuc #include "CodeGenRegisters.h"
17f4a2713aSLionel Sambuc #include "CodeGenTarget.h"
18f4a2713aSLionel Sambuc #include "SequenceToOffsetTable.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/BitVector.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/Twine.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/Format.h"
24f4a2713aSLionel Sambuc #include "llvm/TableGen/Error.h"
25f4a2713aSLionel Sambuc #include "llvm/TableGen/Record.h"
26f4a2713aSLionel Sambuc #include "llvm/TableGen/TableGenBackend.h"
27f4a2713aSLionel Sambuc #include <algorithm>
28f4a2713aSLionel Sambuc #include <set>
29f4a2713aSLionel Sambuc #include <vector>
30f4a2713aSLionel Sambuc using namespace llvm;
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc namespace {
33f4a2713aSLionel Sambuc class RegisterInfoEmitter {
34f4a2713aSLionel Sambuc RecordKeeper &Records;
35f4a2713aSLionel Sambuc public:
RegisterInfoEmitter(RecordKeeper & R)36f4a2713aSLionel Sambuc RegisterInfoEmitter(RecordKeeper &R) : Records(R) {}
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc // runEnums - Print out enum values for all of the registers.
39f4a2713aSLionel Sambuc void runEnums(raw_ostream &o, CodeGenTarget &Target, CodeGenRegBank &Bank);
40f4a2713aSLionel Sambuc
41f4a2713aSLionel Sambuc // runMCDesc - Print out MC register descriptions.
42f4a2713aSLionel Sambuc void runMCDesc(raw_ostream &o, CodeGenTarget &Target, CodeGenRegBank &Bank);
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc // runTargetHeader - Emit a header fragment for the register info emitter.
45f4a2713aSLionel Sambuc void runTargetHeader(raw_ostream &o, CodeGenTarget &Target,
46f4a2713aSLionel Sambuc CodeGenRegBank &Bank);
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc // runTargetDesc - Output the target register and register file descriptions.
49f4a2713aSLionel Sambuc void runTargetDesc(raw_ostream &o, CodeGenTarget &Target,
50f4a2713aSLionel Sambuc CodeGenRegBank &Bank);
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc // run - Output the register file description.
53f4a2713aSLionel Sambuc void run(raw_ostream &o);
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc private:
56*0a6a1f1dSLionel Sambuc void EmitRegMapping(raw_ostream &o, const std::deque<CodeGenRegister> &Regs,
57*0a6a1f1dSLionel Sambuc bool isCtor);
58f4a2713aSLionel Sambuc void EmitRegMappingTables(raw_ostream &o,
59*0a6a1f1dSLionel Sambuc const std::deque<CodeGenRegister> &Regs,
60f4a2713aSLionel Sambuc bool isCtor);
61f4a2713aSLionel Sambuc void EmitRegUnitPressure(raw_ostream &OS, const CodeGenRegBank &RegBank,
62f4a2713aSLionel Sambuc const std::string &ClassName);
63f4a2713aSLionel Sambuc void emitComposeSubRegIndices(raw_ostream &OS, CodeGenRegBank &RegBank,
64f4a2713aSLionel Sambuc const std::string &ClassName);
65*0a6a1f1dSLionel Sambuc void emitComposeSubRegIndexLaneMask(raw_ostream &OS, CodeGenRegBank &RegBank,
66*0a6a1f1dSLionel Sambuc const std::string &ClassName);
67f4a2713aSLionel Sambuc };
68f4a2713aSLionel Sambuc } // End anonymous namespace
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc // runEnums - Print out enum values for all of the registers.
runEnums(raw_ostream & OS,CodeGenTarget & Target,CodeGenRegBank & Bank)71f4a2713aSLionel Sambuc void RegisterInfoEmitter::runEnums(raw_ostream &OS,
72f4a2713aSLionel Sambuc CodeGenTarget &Target, CodeGenRegBank &Bank) {
73*0a6a1f1dSLionel Sambuc const auto &Registers = Bank.getRegisters();
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc // Register enums are stored as uint16_t in the tables. Make sure we'll fit.
76f4a2713aSLionel Sambuc assert(Registers.size() <= 0xffff && "Too many regs to fit in tables");
77f4a2713aSLionel Sambuc
78*0a6a1f1dSLionel Sambuc std::string Namespace =
79*0a6a1f1dSLionel Sambuc Registers.front().TheDef->getValueAsString("Namespace");
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc emitSourceFileHeader("Target Register Enum Values", OS);
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc OS << "\n#ifdef GET_REGINFO_ENUM\n";
84f4a2713aSLionel Sambuc OS << "#undef GET_REGINFO_ENUM\n";
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc OS << "namespace llvm {\n\n";
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc OS << "class MCRegisterClass;\n"
89f4a2713aSLionel Sambuc << "extern const MCRegisterClass " << Namespace
90f4a2713aSLionel Sambuc << "MCRegisterClasses[];\n\n";
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc if (!Namespace.empty())
93f4a2713aSLionel Sambuc OS << "namespace " << Namespace << " {\n";
94f4a2713aSLionel Sambuc OS << "enum {\n NoRegister,\n";
95f4a2713aSLionel Sambuc
96*0a6a1f1dSLionel Sambuc for (const auto &Reg : Registers)
97*0a6a1f1dSLionel Sambuc OS << " " << Reg.getName() << " = " << Reg.EnumValue << ",\n";
98*0a6a1f1dSLionel Sambuc assert(Registers.size() == Registers.back().EnumValue &&
99f4a2713aSLionel Sambuc "Register enum value mismatch!");
100f4a2713aSLionel Sambuc OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
101f4a2713aSLionel Sambuc OS << "};\n";
102f4a2713aSLionel Sambuc if (!Namespace.empty())
103f4a2713aSLionel Sambuc OS << "}\n";
104f4a2713aSLionel Sambuc
105*0a6a1f1dSLionel Sambuc const auto &RegisterClasses = Bank.getRegClasses();
106f4a2713aSLionel Sambuc if (!RegisterClasses.empty()) {
107f4a2713aSLionel Sambuc
108f4a2713aSLionel Sambuc // RegisterClass enums are stored as uint16_t in the tables.
109f4a2713aSLionel Sambuc assert(RegisterClasses.size() <= 0xffff &&
110f4a2713aSLionel Sambuc "Too many register classes to fit in tables");
111f4a2713aSLionel Sambuc
112f4a2713aSLionel Sambuc OS << "\n// Register classes\n";
113f4a2713aSLionel Sambuc if (!Namespace.empty())
114f4a2713aSLionel Sambuc OS << "namespace " << Namespace << " {\n";
115f4a2713aSLionel Sambuc OS << "enum {\n";
116*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses)
117*0a6a1f1dSLionel Sambuc OS << " " << RC.getName() << "RegClassID"
118*0a6a1f1dSLionel Sambuc << " = " << RC.EnumValue << ",\n";
119f4a2713aSLionel Sambuc OS << "\n };\n";
120f4a2713aSLionel Sambuc if (!Namespace.empty())
121f4a2713aSLionel Sambuc OS << "}\n";
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc
124f4a2713aSLionel Sambuc const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
125f4a2713aSLionel Sambuc // If the only definition is the default NoRegAltName, we don't need to
126f4a2713aSLionel Sambuc // emit anything.
127f4a2713aSLionel Sambuc if (RegAltNameIndices.size() > 1) {
128f4a2713aSLionel Sambuc OS << "\n// Register alternate name indices\n";
129f4a2713aSLionel Sambuc if (!Namespace.empty())
130f4a2713aSLionel Sambuc OS << "namespace " << Namespace << " {\n";
131f4a2713aSLionel Sambuc OS << "enum {\n";
132f4a2713aSLionel Sambuc for (unsigned i = 0, e = RegAltNameIndices.size(); i != e; ++i)
133f4a2713aSLionel Sambuc OS << " " << RegAltNameIndices[i]->getName() << ",\t// " << i << "\n";
134f4a2713aSLionel Sambuc OS << " NUM_TARGET_REG_ALT_NAMES = " << RegAltNameIndices.size() << "\n";
135f4a2713aSLionel Sambuc OS << "};\n";
136f4a2713aSLionel Sambuc if (!Namespace.empty())
137f4a2713aSLionel Sambuc OS << "}\n";
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc
140*0a6a1f1dSLionel Sambuc auto &SubRegIndices = Bank.getSubRegIndices();
141f4a2713aSLionel Sambuc if (!SubRegIndices.empty()) {
142f4a2713aSLionel Sambuc OS << "\n// Subregister indices\n";
143*0a6a1f1dSLionel Sambuc std::string Namespace = SubRegIndices.front().getNamespace();
144f4a2713aSLionel Sambuc if (!Namespace.empty())
145f4a2713aSLionel Sambuc OS << "namespace " << Namespace << " {\n";
146f4a2713aSLionel Sambuc OS << "enum {\n NoSubRegister,\n";
147*0a6a1f1dSLionel Sambuc unsigned i = 0;
148*0a6a1f1dSLionel Sambuc for (const auto &Idx : SubRegIndices)
149*0a6a1f1dSLionel Sambuc OS << " " << Idx.getName() << ",\t// " << ++i << "\n";
150f4a2713aSLionel Sambuc OS << " NUM_TARGET_SUBREGS\n};\n";
151f4a2713aSLionel Sambuc if (!Namespace.empty())
152f4a2713aSLionel Sambuc OS << "}\n";
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc OS << "} // End llvm namespace\n";
156f4a2713aSLionel Sambuc OS << "#endif // GET_REGINFO_ENUM\n\n";
157f4a2713aSLionel Sambuc }
158f4a2713aSLionel Sambuc
printInt(raw_ostream & OS,int Val)159*0a6a1f1dSLionel Sambuc static void printInt(raw_ostream &OS, int Val) {
160*0a6a1f1dSLionel Sambuc OS << Val;
161*0a6a1f1dSLionel Sambuc }
162*0a6a1f1dSLionel Sambuc
getMinimalTypeForRange(uint64_t Range)163*0a6a1f1dSLionel Sambuc static const char *getMinimalTypeForRange(uint64_t Range) {
164*0a6a1f1dSLionel Sambuc assert(Range < 0xFFFFFFFFULL && "Enum too large");
165*0a6a1f1dSLionel Sambuc if (Range > 0xFFFF)
166*0a6a1f1dSLionel Sambuc return "uint32_t";
167*0a6a1f1dSLionel Sambuc if (Range > 0xFF)
168*0a6a1f1dSLionel Sambuc return "uint16_t";
169*0a6a1f1dSLionel Sambuc return "uint8_t";
170*0a6a1f1dSLionel Sambuc }
171*0a6a1f1dSLionel Sambuc
172f4a2713aSLionel Sambuc void RegisterInfoEmitter::
EmitRegUnitPressure(raw_ostream & OS,const CodeGenRegBank & RegBank,const std::string & ClassName)173f4a2713aSLionel Sambuc EmitRegUnitPressure(raw_ostream &OS, const CodeGenRegBank &RegBank,
174f4a2713aSLionel Sambuc const std::string &ClassName) {
175f4a2713aSLionel Sambuc unsigned NumRCs = RegBank.getRegClasses().size();
176f4a2713aSLionel Sambuc unsigned NumSets = RegBank.getNumRegPressureSets();
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc OS << "/// Get the weight in units of pressure for this register class.\n"
179f4a2713aSLionel Sambuc << "const RegClassWeight &" << ClassName << "::\n"
180f4a2713aSLionel Sambuc << "getRegClassWeight(const TargetRegisterClass *RC) const {\n"
181f4a2713aSLionel Sambuc << " static const RegClassWeight RCWeightTable[] = {\n";
182*0a6a1f1dSLionel Sambuc for (const auto &RC : RegBank.getRegClasses()) {
183f4a2713aSLionel Sambuc const CodeGenRegister::Set &Regs = RC.getMembers();
184f4a2713aSLionel Sambuc if (Regs.empty())
185f4a2713aSLionel Sambuc OS << " {0, 0";
186f4a2713aSLionel Sambuc else {
187f4a2713aSLionel Sambuc std::vector<unsigned> RegUnits;
188f4a2713aSLionel Sambuc RC.buildRegUnitSet(RegUnits);
189f4a2713aSLionel Sambuc OS << " {" << (*Regs.begin())->getWeight(RegBank)
190f4a2713aSLionel Sambuc << ", " << RegBank.getRegUnitSetWeight(RegUnits);
191f4a2713aSLionel Sambuc }
192f4a2713aSLionel Sambuc OS << "}, \t// " << RC.getName() << "\n";
193f4a2713aSLionel Sambuc }
194*0a6a1f1dSLionel Sambuc OS << " };\n"
195f4a2713aSLionel Sambuc << " return RCWeightTable[RC->getID()];\n"
196f4a2713aSLionel Sambuc << "}\n\n";
197f4a2713aSLionel Sambuc
198f4a2713aSLionel Sambuc // Reasonable targets (not ARMv7) have unit weight for all units, so don't
199f4a2713aSLionel Sambuc // bother generating a table.
200f4a2713aSLionel Sambuc bool RegUnitsHaveUnitWeight = true;
201f4a2713aSLionel Sambuc for (unsigned UnitIdx = 0, UnitEnd = RegBank.getNumNativeRegUnits();
202f4a2713aSLionel Sambuc UnitIdx < UnitEnd; ++UnitIdx) {
203f4a2713aSLionel Sambuc if (RegBank.getRegUnit(UnitIdx).Weight > 1)
204f4a2713aSLionel Sambuc RegUnitsHaveUnitWeight = false;
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc OS << "/// Get the weight in units of pressure for this register unit.\n"
207f4a2713aSLionel Sambuc << "unsigned " << ClassName << "::\n"
208f4a2713aSLionel Sambuc << "getRegUnitWeight(unsigned RegUnit) const {\n"
209f4a2713aSLionel Sambuc << " assert(RegUnit < " << RegBank.getNumNativeRegUnits()
210f4a2713aSLionel Sambuc << " && \"invalid register unit\");\n";
211f4a2713aSLionel Sambuc if (!RegUnitsHaveUnitWeight) {
212f4a2713aSLionel Sambuc OS << " static const uint8_t RUWeightTable[] = {\n ";
213f4a2713aSLionel Sambuc for (unsigned UnitIdx = 0, UnitEnd = RegBank.getNumNativeRegUnits();
214f4a2713aSLionel Sambuc UnitIdx < UnitEnd; ++UnitIdx) {
215f4a2713aSLionel Sambuc const RegUnit &RU = RegBank.getRegUnit(UnitIdx);
216f4a2713aSLionel Sambuc assert(RU.Weight < 256 && "RegUnit too heavy");
217f4a2713aSLionel Sambuc OS << RU.Weight << ", ";
218f4a2713aSLionel Sambuc }
219*0a6a1f1dSLionel Sambuc OS << "};\n"
220f4a2713aSLionel Sambuc << " return RUWeightTable[RegUnit];\n";
221f4a2713aSLionel Sambuc }
222f4a2713aSLionel Sambuc else {
223f4a2713aSLionel Sambuc OS << " // All register units have unit weight.\n"
224f4a2713aSLionel Sambuc << " return 1;\n";
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc OS << "}\n\n";
227f4a2713aSLionel Sambuc
228f4a2713aSLionel Sambuc OS << "\n"
229f4a2713aSLionel Sambuc << "// Get the number of dimensions of register pressure.\n"
230f4a2713aSLionel Sambuc << "unsigned " << ClassName << "::getNumRegPressureSets() const {\n"
231f4a2713aSLionel Sambuc << " return " << NumSets << ";\n}\n\n";
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc OS << "// Get the name of this register unit pressure set.\n"
234f4a2713aSLionel Sambuc << "const char *" << ClassName << "::\n"
235f4a2713aSLionel Sambuc << "getRegPressureSetName(unsigned Idx) const {\n"
236f4a2713aSLionel Sambuc << " static const char *PressureNameTable[] = {\n";
237*0a6a1f1dSLionel Sambuc unsigned MaxRegUnitWeight = 0;
238f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumSets; ++i ) {
239*0a6a1f1dSLionel Sambuc const RegUnitSet &RegUnits = RegBank.getRegSetAt(i);
240*0a6a1f1dSLionel Sambuc MaxRegUnitWeight = std::max(MaxRegUnitWeight, RegUnits.Weight);
241*0a6a1f1dSLionel Sambuc OS << " \"" << RegUnits.Name << "\",\n";
242f4a2713aSLionel Sambuc }
243*0a6a1f1dSLionel Sambuc OS << " nullptr };\n"
244f4a2713aSLionel Sambuc << " return PressureNameTable[Idx];\n"
245f4a2713aSLionel Sambuc << "}\n\n";
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc OS << "// Get the register unit pressure limit for this dimension.\n"
248f4a2713aSLionel Sambuc << "// This limit must be adjusted dynamically for reserved registers.\n"
249f4a2713aSLionel Sambuc << "unsigned " << ClassName << "::\n"
250f4a2713aSLionel Sambuc << "getRegPressureSetLimit(unsigned Idx) const {\n"
251*0a6a1f1dSLionel Sambuc << " static const " << getMinimalTypeForRange(MaxRegUnitWeight)
252*0a6a1f1dSLionel Sambuc << " PressureLimitTable[] = {\n";
253f4a2713aSLionel Sambuc for (unsigned i = 0; i < NumSets; ++i ) {
254f4a2713aSLionel Sambuc const RegUnitSet &RegUnits = RegBank.getRegSetAt(i);
255f4a2713aSLionel Sambuc OS << " " << RegUnits.Weight << ", \t// " << i << ": "
256f4a2713aSLionel Sambuc << RegUnits.Name << "\n";
257f4a2713aSLionel Sambuc }
258*0a6a1f1dSLionel Sambuc OS << " };\n"
259f4a2713aSLionel Sambuc << " return PressureLimitTable[Idx];\n"
260f4a2713aSLionel Sambuc << "}\n\n";
261f4a2713aSLionel Sambuc
262*0a6a1f1dSLionel Sambuc SequenceToOffsetTable<std::vector<int>> PSetsSeqs;
263*0a6a1f1dSLionel Sambuc
264f4a2713aSLionel Sambuc // This table may be larger than NumRCs if some register units needed a list
265f4a2713aSLionel Sambuc // of unit sets that did not correspond to a register class.
266f4a2713aSLionel Sambuc unsigned NumRCUnitSets = RegBank.getNumRegClassPressureSetLists();
267*0a6a1f1dSLionel Sambuc std::vector<std::vector<int>> PSets(NumRCUnitSets);
268*0a6a1f1dSLionel Sambuc
269*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = NumRCUnitSets; i != e; ++i) {
270*0a6a1f1dSLionel Sambuc ArrayRef<unsigned> PSetIDs = RegBank.getRCPressureSetIDs(i);
271*0a6a1f1dSLionel Sambuc PSets[i].reserve(PSetIDs.size());
272*0a6a1f1dSLionel Sambuc for (ArrayRef<unsigned>::iterator PSetI = PSetIDs.begin(),
273*0a6a1f1dSLionel Sambuc PSetE = PSetIDs.end(); PSetI != PSetE; ++PSetI) {
274*0a6a1f1dSLionel Sambuc PSets[i].push_back(RegBank.getRegPressureSet(*PSetI).Order);
275*0a6a1f1dSLionel Sambuc }
276*0a6a1f1dSLionel Sambuc std::sort(PSets[i].begin(), PSets[i].end());
277*0a6a1f1dSLionel Sambuc PSetsSeqs.add(PSets[i]);
278*0a6a1f1dSLionel Sambuc }
279*0a6a1f1dSLionel Sambuc
280*0a6a1f1dSLionel Sambuc PSetsSeqs.layout();
281*0a6a1f1dSLionel Sambuc
282f4a2713aSLionel Sambuc OS << "/// Table of pressure sets per register class or unit.\n"
283f4a2713aSLionel Sambuc << "static const int RCSetsTable[] = {\n";
284*0a6a1f1dSLionel Sambuc PSetsSeqs.emit(OS, printInt, "-1");
285*0a6a1f1dSLionel Sambuc OS << "};\n\n";
286f4a2713aSLionel Sambuc
287f4a2713aSLionel Sambuc OS << "/// Get the dimensions of register pressure impacted by this "
288f4a2713aSLionel Sambuc << "register class.\n"
289f4a2713aSLionel Sambuc << "/// Returns a -1 terminated array of pressure set IDs\n"
290f4a2713aSLionel Sambuc << "const int* " << ClassName << "::\n"
291f4a2713aSLionel Sambuc << "getRegClassPressureSets(const TargetRegisterClass *RC) const {\n";
292*0a6a1f1dSLionel Sambuc OS << " static const " << getMinimalTypeForRange(PSetsSeqs.size()-1)
293*0a6a1f1dSLionel Sambuc << " RCSetStartTable[] = {\n ";
294f4a2713aSLionel Sambuc for (unsigned i = 0, e = NumRCs; i != e; ++i) {
295*0a6a1f1dSLionel Sambuc OS << PSetsSeqs.get(PSets[i]) << ",";
296f4a2713aSLionel Sambuc }
297*0a6a1f1dSLionel Sambuc OS << "};\n"
298*0a6a1f1dSLionel Sambuc << " return &RCSetsTable[RCSetStartTable[RC->getID()]];\n"
299f4a2713aSLionel Sambuc << "}\n\n";
300f4a2713aSLionel Sambuc
301f4a2713aSLionel Sambuc OS << "/// Get the dimensions of register pressure impacted by this "
302f4a2713aSLionel Sambuc << "register unit.\n"
303f4a2713aSLionel Sambuc << "/// Returns a -1 terminated array of pressure set IDs\n"
304f4a2713aSLionel Sambuc << "const int* " << ClassName << "::\n"
305f4a2713aSLionel Sambuc << "getRegUnitPressureSets(unsigned RegUnit) const {\n"
306f4a2713aSLionel Sambuc << " assert(RegUnit < " << RegBank.getNumNativeRegUnits()
307f4a2713aSLionel Sambuc << " && \"invalid register unit\");\n";
308*0a6a1f1dSLionel Sambuc OS << " static const " << getMinimalTypeForRange(PSetsSeqs.size()-1)
309*0a6a1f1dSLionel Sambuc << " RUSetStartTable[] = {\n ";
310f4a2713aSLionel Sambuc for (unsigned UnitIdx = 0, UnitEnd = RegBank.getNumNativeRegUnits();
311f4a2713aSLionel Sambuc UnitIdx < UnitEnd; ++UnitIdx) {
312*0a6a1f1dSLionel Sambuc OS << PSetsSeqs.get(PSets[RegBank.getRegUnit(UnitIdx).RegClassUnitSetsIdx])
313*0a6a1f1dSLionel Sambuc << ",";
314f4a2713aSLionel Sambuc }
315*0a6a1f1dSLionel Sambuc OS << "};\n"
316*0a6a1f1dSLionel Sambuc << " return &RCSetsTable[RUSetStartTable[RegUnit]];\n"
317f4a2713aSLionel Sambuc << "}\n\n";
318f4a2713aSLionel Sambuc }
319f4a2713aSLionel Sambuc
EmitRegMappingTables(raw_ostream & OS,const std::deque<CodeGenRegister> & Regs,bool isCtor)320*0a6a1f1dSLionel Sambuc void RegisterInfoEmitter::EmitRegMappingTables(
321*0a6a1f1dSLionel Sambuc raw_ostream &OS, const std::deque<CodeGenRegister> &Regs, bool isCtor) {
322f4a2713aSLionel Sambuc // Collect all information about dwarf register numbers
323f4a2713aSLionel Sambuc typedef std::map<Record*, std::vector<int64_t>, LessRecordRegister> DwarfRegNumsMapTy;
324f4a2713aSLionel Sambuc DwarfRegNumsMapTy DwarfRegNums;
325f4a2713aSLionel Sambuc
326f4a2713aSLionel Sambuc // First, just pull all provided information to the map
327f4a2713aSLionel Sambuc unsigned maxLength = 0;
328*0a6a1f1dSLionel Sambuc for (auto &RE : Regs) {
329*0a6a1f1dSLionel Sambuc Record *Reg = RE.TheDef;
330f4a2713aSLionel Sambuc std::vector<int64_t> RegNums = Reg->getValueAsListOfInts("DwarfNumbers");
331f4a2713aSLionel Sambuc maxLength = std::max((size_t)maxLength, RegNums.size());
332f4a2713aSLionel Sambuc if (DwarfRegNums.count(Reg))
333f4a2713aSLionel Sambuc PrintWarning(Reg->getLoc(), Twine("DWARF numbers for register ") +
334f4a2713aSLionel Sambuc getQualifiedName(Reg) + "specified multiple times");
335f4a2713aSLionel Sambuc DwarfRegNums[Reg] = RegNums;
336f4a2713aSLionel Sambuc }
337f4a2713aSLionel Sambuc
338f4a2713aSLionel Sambuc if (!maxLength)
339f4a2713aSLionel Sambuc return;
340f4a2713aSLionel Sambuc
341f4a2713aSLionel Sambuc // Now we know maximal length of number list. Append -1's, where needed
342f4a2713aSLionel Sambuc for (DwarfRegNumsMapTy::iterator
343f4a2713aSLionel Sambuc I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I)
344f4a2713aSLionel Sambuc for (unsigned i = I->second.size(), e = maxLength; i != e; ++i)
345f4a2713aSLionel Sambuc I->second.push_back(-1);
346f4a2713aSLionel Sambuc
347*0a6a1f1dSLionel Sambuc std::string Namespace = Regs.front().TheDef->getValueAsString("Namespace");
348f4a2713aSLionel Sambuc
349f4a2713aSLionel Sambuc OS << "// " << Namespace << " Dwarf<->LLVM register mappings.\n";
350f4a2713aSLionel Sambuc
351f4a2713aSLionel Sambuc // Emit reverse information about the dwarf register numbers.
352f4a2713aSLionel Sambuc for (unsigned j = 0; j < 2; ++j) {
353f4a2713aSLionel Sambuc for (unsigned i = 0, e = maxLength; i != e; ++i) {
354f4a2713aSLionel Sambuc OS << "extern const MCRegisterInfo::DwarfLLVMRegPair " << Namespace;
355f4a2713aSLionel Sambuc OS << (j == 0 ? "DwarfFlavour" : "EHFlavour");
356f4a2713aSLionel Sambuc OS << i << "Dwarf2L[]";
357f4a2713aSLionel Sambuc
358f4a2713aSLionel Sambuc if (!isCtor) {
359f4a2713aSLionel Sambuc OS << " = {\n";
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc // Store the mapping sorted by the LLVM reg num so lookup can be done
362f4a2713aSLionel Sambuc // with a binary search.
363f4a2713aSLionel Sambuc std::map<uint64_t, Record*> Dwarf2LMap;
364f4a2713aSLionel Sambuc for (DwarfRegNumsMapTy::iterator
365f4a2713aSLionel Sambuc I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I) {
366f4a2713aSLionel Sambuc int DwarfRegNo = I->second[i];
367f4a2713aSLionel Sambuc if (DwarfRegNo < 0)
368f4a2713aSLionel Sambuc continue;
369f4a2713aSLionel Sambuc Dwarf2LMap[DwarfRegNo] = I->first;
370f4a2713aSLionel Sambuc }
371f4a2713aSLionel Sambuc
372f4a2713aSLionel Sambuc for (std::map<uint64_t, Record*>::iterator
373f4a2713aSLionel Sambuc I = Dwarf2LMap.begin(), E = Dwarf2LMap.end(); I != E; ++I)
374f4a2713aSLionel Sambuc OS << " { " << I->first << "U, " << getQualifiedName(I->second)
375f4a2713aSLionel Sambuc << " },\n";
376f4a2713aSLionel Sambuc
377f4a2713aSLionel Sambuc OS << "};\n";
378f4a2713aSLionel Sambuc } else {
379f4a2713aSLionel Sambuc OS << ";\n";
380f4a2713aSLionel Sambuc }
381f4a2713aSLionel Sambuc
382f4a2713aSLionel Sambuc // We have to store the size in a const global, it's used in multiple
383f4a2713aSLionel Sambuc // places.
384f4a2713aSLionel Sambuc OS << "extern const unsigned " << Namespace
385f4a2713aSLionel Sambuc << (j == 0 ? "DwarfFlavour" : "EHFlavour") << i << "Dwarf2LSize";
386f4a2713aSLionel Sambuc if (!isCtor)
387*0a6a1f1dSLionel Sambuc OS << " = array_lengthof(" << Namespace
388f4a2713aSLionel Sambuc << (j == 0 ? "DwarfFlavour" : "EHFlavour") << i
389*0a6a1f1dSLionel Sambuc << "Dwarf2L);\n\n";
390f4a2713aSLionel Sambuc else
391f4a2713aSLionel Sambuc OS << ";\n\n";
392f4a2713aSLionel Sambuc }
393f4a2713aSLionel Sambuc }
394f4a2713aSLionel Sambuc
395*0a6a1f1dSLionel Sambuc for (auto &RE : Regs) {
396*0a6a1f1dSLionel Sambuc Record *Reg = RE.TheDef;
397f4a2713aSLionel Sambuc const RecordVal *V = Reg->getValue("DwarfAlias");
398f4a2713aSLionel Sambuc if (!V || !V->getValue())
399f4a2713aSLionel Sambuc continue;
400f4a2713aSLionel Sambuc
401f4a2713aSLionel Sambuc DefInit *DI = cast<DefInit>(V->getValue());
402f4a2713aSLionel Sambuc Record *Alias = DI->getDef();
403f4a2713aSLionel Sambuc DwarfRegNums[Reg] = DwarfRegNums[Alias];
404f4a2713aSLionel Sambuc }
405f4a2713aSLionel Sambuc
406f4a2713aSLionel Sambuc // Emit information about the dwarf register numbers.
407f4a2713aSLionel Sambuc for (unsigned j = 0; j < 2; ++j) {
408f4a2713aSLionel Sambuc for (unsigned i = 0, e = maxLength; i != e; ++i) {
409f4a2713aSLionel Sambuc OS << "extern const MCRegisterInfo::DwarfLLVMRegPair " << Namespace;
410f4a2713aSLionel Sambuc OS << (j == 0 ? "DwarfFlavour" : "EHFlavour");
411f4a2713aSLionel Sambuc OS << i << "L2Dwarf[]";
412f4a2713aSLionel Sambuc if (!isCtor) {
413f4a2713aSLionel Sambuc OS << " = {\n";
414f4a2713aSLionel Sambuc // Store the mapping sorted by the Dwarf reg num so lookup can be done
415f4a2713aSLionel Sambuc // with a binary search.
416f4a2713aSLionel Sambuc for (DwarfRegNumsMapTy::iterator
417f4a2713aSLionel Sambuc I = DwarfRegNums.begin(), E = DwarfRegNums.end(); I != E; ++I) {
418f4a2713aSLionel Sambuc int RegNo = I->second[i];
419f4a2713aSLionel Sambuc if (RegNo == -1) // -1 is the default value, don't emit a mapping.
420f4a2713aSLionel Sambuc continue;
421f4a2713aSLionel Sambuc
422f4a2713aSLionel Sambuc OS << " { " << getQualifiedName(I->first) << ", " << RegNo
423f4a2713aSLionel Sambuc << "U },\n";
424f4a2713aSLionel Sambuc }
425f4a2713aSLionel Sambuc OS << "};\n";
426f4a2713aSLionel Sambuc } else {
427f4a2713aSLionel Sambuc OS << ";\n";
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc
430f4a2713aSLionel Sambuc // We have to store the size in a const global, it's used in multiple
431f4a2713aSLionel Sambuc // places.
432f4a2713aSLionel Sambuc OS << "extern const unsigned " << Namespace
433f4a2713aSLionel Sambuc << (j == 0 ? "DwarfFlavour" : "EHFlavour") << i << "L2DwarfSize";
434f4a2713aSLionel Sambuc if (!isCtor)
435*0a6a1f1dSLionel Sambuc OS << " = array_lengthof(" << Namespace
436*0a6a1f1dSLionel Sambuc << (j == 0 ? "DwarfFlavour" : "EHFlavour") << i << "L2Dwarf);\n\n";
437f4a2713aSLionel Sambuc else
438f4a2713aSLionel Sambuc OS << ";\n\n";
439f4a2713aSLionel Sambuc }
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc }
442f4a2713aSLionel Sambuc
EmitRegMapping(raw_ostream & OS,const std::deque<CodeGenRegister> & Regs,bool isCtor)443*0a6a1f1dSLionel Sambuc void RegisterInfoEmitter::EmitRegMapping(
444*0a6a1f1dSLionel Sambuc raw_ostream &OS, const std::deque<CodeGenRegister> &Regs, bool isCtor) {
445f4a2713aSLionel Sambuc // Emit the initializer so the tables from EmitRegMappingTables get wired up
446f4a2713aSLionel Sambuc // to the MCRegisterInfo object.
447f4a2713aSLionel Sambuc unsigned maxLength = 0;
448*0a6a1f1dSLionel Sambuc for (auto &RE : Regs) {
449*0a6a1f1dSLionel Sambuc Record *Reg = RE.TheDef;
450f4a2713aSLionel Sambuc maxLength = std::max((size_t)maxLength,
451f4a2713aSLionel Sambuc Reg->getValueAsListOfInts("DwarfNumbers").size());
452f4a2713aSLionel Sambuc }
453f4a2713aSLionel Sambuc
454f4a2713aSLionel Sambuc if (!maxLength)
455f4a2713aSLionel Sambuc return;
456f4a2713aSLionel Sambuc
457*0a6a1f1dSLionel Sambuc std::string Namespace = Regs.front().TheDef->getValueAsString("Namespace");
458f4a2713aSLionel Sambuc
459f4a2713aSLionel Sambuc // Emit reverse information about the dwarf register numbers.
460f4a2713aSLionel Sambuc for (unsigned j = 0; j < 2; ++j) {
461f4a2713aSLionel Sambuc OS << " switch (";
462f4a2713aSLionel Sambuc if (j == 0)
463f4a2713aSLionel Sambuc OS << "DwarfFlavour";
464f4a2713aSLionel Sambuc else
465f4a2713aSLionel Sambuc OS << "EHFlavour";
466f4a2713aSLionel Sambuc OS << ") {\n"
467f4a2713aSLionel Sambuc << " default:\n"
468f4a2713aSLionel Sambuc << " llvm_unreachable(\"Unknown DWARF flavour\");\n";
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc for (unsigned i = 0, e = maxLength; i != e; ++i) {
471f4a2713aSLionel Sambuc OS << " case " << i << ":\n";
472f4a2713aSLionel Sambuc OS << " ";
473f4a2713aSLionel Sambuc if (!isCtor)
474f4a2713aSLionel Sambuc OS << "RI->";
475f4a2713aSLionel Sambuc std::string Tmp;
476f4a2713aSLionel Sambuc raw_string_ostream(Tmp) << Namespace
477f4a2713aSLionel Sambuc << (j == 0 ? "DwarfFlavour" : "EHFlavour") << i
478f4a2713aSLionel Sambuc << "Dwarf2L";
479f4a2713aSLionel Sambuc OS << "mapDwarfRegsToLLVMRegs(" << Tmp << ", " << Tmp << "Size, ";
480f4a2713aSLionel Sambuc if (j == 0)
481f4a2713aSLionel Sambuc OS << "false";
482f4a2713aSLionel Sambuc else
483f4a2713aSLionel Sambuc OS << "true";
484f4a2713aSLionel Sambuc OS << ");\n";
485f4a2713aSLionel Sambuc OS << " break;\n";
486f4a2713aSLionel Sambuc }
487f4a2713aSLionel Sambuc OS << " }\n";
488f4a2713aSLionel Sambuc }
489f4a2713aSLionel Sambuc
490f4a2713aSLionel Sambuc // Emit information about the dwarf register numbers.
491f4a2713aSLionel Sambuc for (unsigned j = 0; j < 2; ++j) {
492f4a2713aSLionel Sambuc OS << " switch (";
493f4a2713aSLionel Sambuc if (j == 0)
494f4a2713aSLionel Sambuc OS << "DwarfFlavour";
495f4a2713aSLionel Sambuc else
496f4a2713aSLionel Sambuc OS << "EHFlavour";
497f4a2713aSLionel Sambuc OS << ") {\n"
498f4a2713aSLionel Sambuc << " default:\n"
499f4a2713aSLionel Sambuc << " llvm_unreachable(\"Unknown DWARF flavour\");\n";
500f4a2713aSLionel Sambuc
501f4a2713aSLionel Sambuc for (unsigned i = 0, e = maxLength; i != e; ++i) {
502f4a2713aSLionel Sambuc OS << " case " << i << ":\n";
503f4a2713aSLionel Sambuc OS << " ";
504f4a2713aSLionel Sambuc if (!isCtor)
505f4a2713aSLionel Sambuc OS << "RI->";
506f4a2713aSLionel Sambuc std::string Tmp;
507f4a2713aSLionel Sambuc raw_string_ostream(Tmp) << Namespace
508f4a2713aSLionel Sambuc << (j == 0 ? "DwarfFlavour" : "EHFlavour") << i
509f4a2713aSLionel Sambuc << "L2Dwarf";
510f4a2713aSLionel Sambuc OS << "mapLLVMRegsToDwarfRegs(" << Tmp << ", " << Tmp << "Size, ";
511f4a2713aSLionel Sambuc if (j == 0)
512f4a2713aSLionel Sambuc OS << "false";
513f4a2713aSLionel Sambuc else
514f4a2713aSLionel Sambuc OS << "true";
515f4a2713aSLionel Sambuc OS << ");\n";
516f4a2713aSLionel Sambuc OS << " break;\n";
517f4a2713aSLionel Sambuc }
518f4a2713aSLionel Sambuc OS << " }\n";
519f4a2713aSLionel Sambuc }
520f4a2713aSLionel Sambuc }
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc // Print a BitVector as a sequence of hex numbers using a little-endian mapping.
523f4a2713aSLionel Sambuc // Width is the number of bits per hex number.
printBitVectorAsHex(raw_ostream & OS,const BitVector & Bits,unsigned Width)524f4a2713aSLionel Sambuc static void printBitVectorAsHex(raw_ostream &OS,
525f4a2713aSLionel Sambuc const BitVector &Bits,
526f4a2713aSLionel Sambuc unsigned Width) {
527f4a2713aSLionel Sambuc assert(Width <= 32 && "Width too large");
528f4a2713aSLionel Sambuc unsigned Digits = (Width + 3) / 4;
529f4a2713aSLionel Sambuc for (unsigned i = 0, e = Bits.size(); i < e; i += Width) {
530f4a2713aSLionel Sambuc unsigned Value = 0;
531f4a2713aSLionel Sambuc for (unsigned j = 0; j != Width && i + j != e; ++j)
532f4a2713aSLionel Sambuc Value |= Bits.test(i + j) << j;
533f4a2713aSLionel Sambuc OS << format("0x%0*x, ", Digits, Value);
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc }
536f4a2713aSLionel Sambuc
537f4a2713aSLionel Sambuc // Helper to emit a set of bits into a constant byte array.
538f4a2713aSLionel Sambuc class BitVectorEmitter {
539f4a2713aSLionel Sambuc BitVector Values;
540f4a2713aSLionel Sambuc public:
add(unsigned v)541f4a2713aSLionel Sambuc void add(unsigned v) {
542f4a2713aSLionel Sambuc if (v >= Values.size())
543f4a2713aSLionel Sambuc Values.resize(((v/8)+1)*8); // Round up to the next byte.
544f4a2713aSLionel Sambuc Values[v] = true;
545f4a2713aSLionel Sambuc }
546f4a2713aSLionel Sambuc
print(raw_ostream & OS)547f4a2713aSLionel Sambuc void print(raw_ostream &OS) {
548f4a2713aSLionel Sambuc printBitVectorAsHex(OS, Values, 8);
549f4a2713aSLionel Sambuc }
550f4a2713aSLionel Sambuc };
551f4a2713aSLionel Sambuc
printSimpleValueType(raw_ostream & OS,MVT::SimpleValueType VT)552f4a2713aSLionel Sambuc static void printSimpleValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
553f4a2713aSLionel Sambuc OS << getEnumName(VT);
554f4a2713aSLionel Sambuc }
555f4a2713aSLionel Sambuc
printSubRegIndex(raw_ostream & OS,const CodeGenSubRegIndex * Idx)556f4a2713aSLionel Sambuc static void printSubRegIndex(raw_ostream &OS, const CodeGenSubRegIndex *Idx) {
557f4a2713aSLionel Sambuc OS << Idx->EnumValue;
558f4a2713aSLionel Sambuc }
559f4a2713aSLionel Sambuc
560f4a2713aSLionel Sambuc // Differentially encoded register and regunit lists allow for better
561f4a2713aSLionel Sambuc // compression on regular register banks. The sequence is computed from the
562f4a2713aSLionel Sambuc // differential list as:
563f4a2713aSLionel Sambuc //
564f4a2713aSLionel Sambuc // out[0] = InitVal;
565f4a2713aSLionel Sambuc // out[n+1] = out[n] + diff[n]; // n = 0, 1, ...
566f4a2713aSLionel Sambuc //
567f4a2713aSLionel Sambuc // The initial value depends on the specific list. The list is terminated by a
568f4a2713aSLionel Sambuc // 0 differential which means we can't encode repeated elements.
569f4a2713aSLionel Sambuc
570f4a2713aSLionel Sambuc typedef SmallVector<uint16_t, 4> DiffVec;
571*0a6a1f1dSLionel Sambuc typedef SmallVector<unsigned, 4> MaskVec;
572f4a2713aSLionel Sambuc
573f4a2713aSLionel Sambuc // Differentially encode a sequence of numbers into V. The starting value and
574f4a2713aSLionel Sambuc // terminating 0 are not added to V, so it will have the same size as List.
575f4a2713aSLionel Sambuc static
diffEncode(DiffVec & V,unsigned InitVal,ArrayRef<unsigned> List)576f4a2713aSLionel Sambuc DiffVec &diffEncode(DiffVec &V, unsigned InitVal, ArrayRef<unsigned> List) {
577f4a2713aSLionel Sambuc assert(V.empty() && "Clear DiffVec before diffEncode.");
578f4a2713aSLionel Sambuc uint16_t Val = uint16_t(InitVal);
579f4a2713aSLionel Sambuc for (unsigned i = 0; i != List.size(); ++i) {
580f4a2713aSLionel Sambuc uint16_t Cur = List[i];
581f4a2713aSLionel Sambuc V.push_back(Cur - Val);
582f4a2713aSLionel Sambuc Val = Cur;
583f4a2713aSLionel Sambuc }
584f4a2713aSLionel Sambuc return V;
585f4a2713aSLionel Sambuc }
586f4a2713aSLionel Sambuc
587f4a2713aSLionel Sambuc template<typename Iter>
588f4a2713aSLionel Sambuc static
diffEncode(DiffVec & V,unsigned InitVal,Iter Begin,Iter End)589f4a2713aSLionel Sambuc DiffVec &diffEncode(DiffVec &V, unsigned InitVal, Iter Begin, Iter End) {
590f4a2713aSLionel Sambuc assert(V.empty() && "Clear DiffVec before diffEncode.");
591f4a2713aSLionel Sambuc uint16_t Val = uint16_t(InitVal);
592f4a2713aSLionel Sambuc for (Iter I = Begin; I != End; ++I) {
593f4a2713aSLionel Sambuc uint16_t Cur = (*I)->EnumValue;
594f4a2713aSLionel Sambuc V.push_back(Cur - Val);
595f4a2713aSLionel Sambuc Val = Cur;
596f4a2713aSLionel Sambuc }
597f4a2713aSLionel Sambuc return V;
598f4a2713aSLionel Sambuc }
599f4a2713aSLionel Sambuc
printDiff16(raw_ostream & OS,uint16_t Val)600f4a2713aSLionel Sambuc static void printDiff16(raw_ostream &OS, uint16_t Val) {
601f4a2713aSLionel Sambuc OS << Val;
602f4a2713aSLionel Sambuc }
603f4a2713aSLionel Sambuc
printMask(raw_ostream & OS,unsigned Val)604*0a6a1f1dSLionel Sambuc static void printMask(raw_ostream &OS, unsigned Val) {
605*0a6a1f1dSLionel Sambuc OS << format("0x%08X", Val);
606*0a6a1f1dSLionel Sambuc }
607*0a6a1f1dSLionel Sambuc
608f4a2713aSLionel Sambuc // Try to combine Idx's compose map into Vec if it is compatible.
609f4a2713aSLionel Sambuc // Return false if it's not possible.
combine(const CodeGenSubRegIndex * Idx,SmallVectorImpl<CodeGenSubRegIndex * > & Vec)610f4a2713aSLionel Sambuc static bool combine(const CodeGenSubRegIndex *Idx,
611f4a2713aSLionel Sambuc SmallVectorImpl<CodeGenSubRegIndex*> &Vec) {
612f4a2713aSLionel Sambuc const CodeGenSubRegIndex::CompMap &Map = Idx->getComposites();
613f4a2713aSLionel Sambuc for (CodeGenSubRegIndex::CompMap::const_iterator
614f4a2713aSLionel Sambuc I = Map.begin(), E = Map.end(); I != E; ++I) {
615f4a2713aSLionel Sambuc CodeGenSubRegIndex *&Entry = Vec[I->first->EnumValue - 1];
616f4a2713aSLionel Sambuc if (Entry && Entry != I->second)
617f4a2713aSLionel Sambuc return false;
618f4a2713aSLionel Sambuc }
619f4a2713aSLionel Sambuc
620f4a2713aSLionel Sambuc // All entries are compatible. Make it so.
621f4a2713aSLionel Sambuc for (CodeGenSubRegIndex::CompMap::const_iterator
622f4a2713aSLionel Sambuc I = Map.begin(), E = Map.end(); I != E; ++I)
623f4a2713aSLionel Sambuc Vec[I->first->EnumValue - 1] = I->second;
624f4a2713aSLionel Sambuc return true;
625f4a2713aSLionel Sambuc }
626f4a2713aSLionel Sambuc
627f4a2713aSLionel Sambuc void
emitComposeSubRegIndices(raw_ostream & OS,CodeGenRegBank & RegBank,const std::string & ClName)628f4a2713aSLionel Sambuc RegisterInfoEmitter::emitComposeSubRegIndices(raw_ostream &OS,
629f4a2713aSLionel Sambuc CodeGenRegBank &RegBank,
630f4a2713aSLionel Sambuc const std::string &ClName) {
631*0a6a1f1dSLionel Sambuc const auto &SubRegIndices = RegBank.getSubRegIndices();
632f4a2713aSLionel Sambuc OS << "unsigned " << ClName
633f4a2713aSLionel Sambuc << "::composeSubRegIndicesImpl(unsigned IdxA, unsigned IdxB) const {\n";
634f4a2713aSLionel Sambuc
635f4a2713aSLionel Sambuc // Many sub-register indexes are composition-compatible, meaning that
636f4a2713aSLionel Sambuc //
637f4a2713aSLionel Sambuc // compose(IdxA, IdxB) == compose(IdxA', IdxB)
638f4a2713aSLionel Sambuc //
639f4a2713aSLionel Sambuc // for many IdxA, IdxA' pairs. Not all sub-register indexes can be composed.
640f4a2713aSLionel Sambuc // The illegal entries can be use as wildcards to compress the table further.
641f4a2713aSLionel Sambuc
642f4a2713aSLionel Sambuc // Map each Sub-register index to a compatible table row.
643f4a2713aSLionel Sambuc SmallVector<unsigned, 4> RowMap;
644f4a2713aSLionel Sambuc SmallVector<SmallVector<CodeGenSubRegIndex*, 4>, 4> Rows;
645f4a2713aSLionel Sambuc
646*0a6a1f1dSLionel Sambuc auto SubRegIndicesSize =
647*0a6a1f1dSLionel Sambuc std::distance(SubRegIndices.begin(), SubRegIndices.end());
648*0a6a1f1dSLionel Sambuc for (const auto &Idx : SubRegIndices) {
649f4a2713aSLionel Sambuc unsigned Found = ~0u;
650f4a2713aSLionel Sambuc for (unsigned r = 0, re = Rows.size(); r != re; ++r) {
651*0a6a1f1dSLionel Sambuc if (combine(&Idx, Rows[r])) {
652f4a2713aSLionel Sambuc Found = r;
653f4a2713aSLionel Sambuc break;
654f4a2713aSLionel Sambuc }
655f4a2713aSLionel Sambuc }
656f4a2713aSLionel Sambuc if (Found == ~0u) {
657f4a2713aSLionel Sambuc Found = Rows.size();
658f4a2713aSLionel Sambuc Rows.resize(Found + 1);
659*0a6a1f1dSLionel Sambuc Rows.back().resize(SubRegIndicesSize);
660*0a6a1f1dSLionel Sambuc combine(&Idx, Rows.back());
661f4a2713aSLionel Sambuc }
662f4a2713aSLionel Sambuc RowMap.push_back(Found);
663f4a2713aSLionel Sambuc }
664f4a2713aSLionel Sambuc
665f4a2713aSLionel Sambuc // Output the row map if there is multiple rows.
666f4a2713aSLionel Sambuc if (Rows.size() > 1) {
667*0a6a1f1dSLionel Sambuc OS << " static const " << getMinimalTypeForRange(Rows.size()) << " RowMap["
668*0a6a1f1dSLionel Sambuc << SubRegIndicesSize << "] = {\n ";
669*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = SubRegIndicesSize; i != e; ++i)
670f4a2713aSLionel Sambuc OS << RowMap[i] << ", ";
671f4a2713aSLionel Sambuc OS << "\n };\n";
672f4a2713aSLionel Sambuc }
673f4a2713aSLionel Sambuc
674f4a2713aSLionel Sambuc // Output the rows.
675*0a6a1f1dSLionel Sambuc OS << " static const " << getMinimalTypeForRange(SubRegIndicesSize + 1)
676*0a6a1f1dSLionel Sambuc << " Rows[" << Rows.size() << "][" << SubRegIndicesSize << "] = {\n";
677f4a2713aSLionel Sambuc for (unsigned r = 0, re = Rows.size(); r != re; ++r) {
678f4a2713aSLionel Sambuc OS << " { ";
679*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = SubRegIndicesSize; i != e; ++i)
680f4a2713aSLionel Sambuc if (Rows[r][i])
681f4a2713aSLionel Sambuc OS << Rows[r][i]->EnumValue << ", ";
682f4a2713aSLionel Sambuc else
683f4a2713aSLionel Sambuc OS << "0, ";
684f4a2713aSLionel Sambuc OS << "},\n";
685f4a2713aSLionel Sambuc }
686f4a2713aSLionel Sambuc OS << " };\n\n";
687f4a2713aSLionel Sambuc
688*0a6a1f1dSLionel Sambuc OS << " --IdxA; assert(IdxA < " << SubRegIndicesSize << ");\n"
689*0a6a1f1dSLionel Sambuc << " --IdxB; assert(IdxB < " << SubRegIndicesSize << ");\n";
690f4a2713aSLionel Sambuc if (Rows.size() > 1)
691f4a2713aSLionel Sambuc OS << " return Rows[RowMap[IdxA]][IdxB];\n";
692f4a2713aSLionel Sambuc else
693f4a2713aSLionel Sambuc OS << " return Rows[0][IdxB];\n";
694f4a2713aSLionel Sambuc OS << "}\n\n";
695f4a2713aSLionel Sambuc }
696f4a2713aSLionel Sambuc
697*0a6a1f1dSLionel Sambuc void
emitComposeSubRegIndexLaneMask(raw_ostream & OS,CodeGenRegBank & RegBank,const std::string & ClName)698*0a6a1f1dSLionel Sambuc RegisterInfoEmitter::emitComposeSubRegIndexLaneMask(raw_ostream &OS,
699*0a6a1f1dSLionel Sambuc CodeGenRegBank &RegBank,
700*0a6a1f1dSLionel Sambuc const std::string &ClName) {
701*0a6a1f1dSLionel Sambuc // See the comments in computeSubRegLaneMasks() for our goal here.
702*0a6a1f1dSLionel Sambuc const auto &SubRegIndices = RegBank.getSubRegIndices();
703*0a6a1f1dSLionel Sambuc
704*0a6a1f1dSLionel Sambuc // Create a list of Mask+Rotate operations, with equivalent entries merged.
705*0a6a1f1dSLionel Sambuc SmallVector<unsigned, 4> SubReg2SequenceIndexMap;
706*0a6a1f1dSLionel Sambuc SmallVector<SmallVector<MaskRolPair, 1>, 4> Sequences;
707*0a6a1f1dSLionel Sambuc for (const auto &Idx : SubRegIndices) {
708*0a6a1f1dSLionel Sambuc const SmallVector<MaskRolPair, 1> &IdxSequence
709*0a6a1f1dSLionel Sambuc = Idx.CompositionLaneMaskTransform;
710*0a6a1f1dSLionel Sambuc
711*0a6a1f1dSLionel Sambuc unsigned Found = ~0u;
712*0a6a1f1dSLionel Sambuc unsigned SIdx = 0;
713*0a6a1f1dSLionel Sambuc unsigned NextSIdx;
714*0a6a1f1dSLionel Sambuc for (size_t s = 0, se = Sequences.size(); s != se; ++s, SIdx = NextSIdx) {
715*0a6a1f1dSLionel Sambuc SmallVectorImpl<MaskRolPair> &Sequence = Sequences[s];
716*0a6a1f1dSLionel Sambuc NextSIdx = SIdx + Sequence.size() + 1;
717*0a6a1f1dSLionel Sambuc if (Sequence.size() != IdxSequence.size())
718*0a6a1f1dSLionel Sambuc continue;
719*0a6a1f1dSLionel Sambuc bool Identical = true;
720*0a6a1f1dSLionel Sambuc for (size_t o = 0, oe = Sequence.size(); o != oe; ++o) {
721*0a6a1f1dSLionel Sambuc if (Sequence[o] != IdxSequence[o]) {
722*0a6a1f1dSLionel Sambuc Identical = false;
723*0a6a1f1dSLionel Sambuc break;
724*0a6a1f1dSLionel Sambuc }
725*0a6a1f1dSLionel Sambuc }
726*0a6a1f1dSLionel Sambuc if (Identical) {
727*0a6a1f1dSLionel Sambuc Found = SIdx;
728*0a6a1f1dSLionel Sambuc break;
729*0a6a1f1dSLionel Sambuc }
730*0a6a1f1dSLionel Sambuc }
731*0a6a1f1dSLionel Sambuc if (Found == ~0u) {
732*0a6a1f1dSLionel Sambuc Sequences.push_back(IdxSequence);
733*0a6a1f1dSLionel Sambuc Found = SIdx;
734*0a6a1f1dSLionel Sambuc }
735*0a6a1f1dSLionel Sambuc SubReg2SequenceIndexMap.push_back(Found);
736*0a6a1f1dSLionel Sambuc }
737*0a6a1f1dSLionel Sambuc
738*0a6a1f1dSLionel Sambuc OS << "unsigned " << ClName
739*0a6a1f1dSLionel Sambuc << "::composeSubRegIndexLaneMaskImpl(unsigned IdxA, unsigned LaneMask)"
740*0a6a1f1dSLionel Sambuc " const {\n";
741*0a6a1f1dSLionel Sambuc
742*0a6a1f1dSLionel Sambuc OS << " struct MaskRolOp {\n"
743*0a6a1f1dSLionel Sambuc " unsigned Mask;\n"
744*0a6a1f1dSLionel Sambuc " uint8_t RotateLeft;\n"
745*0a6a1f1dSLionel Sambuc " };\n"
746*0a6a1f1dSLionel Sambuc " static const MaskRolOp Seqs[] = {\n";
747*0a6a1f1dSLionel Sambuc unsigned Idx = 0;
748*0a6a1f1dSLionel Sambuc for (size_t s = 0, se = Sequences.size(); s != se; ++s) {
749*0a6a1f1dSLionel Sambuc OS << " ";
750*0a6a1f1dSLionel Sambuc const SmallVectorImpl<MaskRolPair> &Sequence = Sequences[s];
751*0a6a1f1dSLionel Sambuc for (size_t p = 0, pe = Sequence.size(); p != pe; ++p) {
752*0a6a1f1dSLionel Sambuc const MaskRolPair &P = Sequence[p];
753*0a6a1f1dSLionel Sambuc OS << format("{ 0x%08X, %2u }, ", P.Mask, P.RotateLeft);
754*0a6a1f1dSLionel Sambuc }
755*0a6a1f1dSLionel Sambuc OS << "{ 0, 0 }";
756*0a6a1f1dSLionel Sambuc if (s+1 != se)
757*0a6a1f1dSLionel Sambuc OS << ", ";
758*0a6a1f1dSLionel Sambuc OS << " // Sequence " << Idx << "\n";
759*0a6a1f1dSLionel Sambuc Idx += Sequence.size() + 1;
760*0a6a1f1dSLionel Sambuc }
761*0a6a1f1dSLionel Sambuc OS << " };\n"
762*0a6a1f1dSLionel Sambuc " static const MaskRolOp *CompositeSequences[] = {\n";
763*0a6a1f1dSLionel Sambuc for (size_t i = 0, e = SubRegIndices.size(); i != e; ++i) {
764*0a6a1f1dSLionel Sambuc OS << " ";
765*0a6a1f1dSLionel Sambuc unsigned Idx = SubReg2SequenceIndexMap[i];
766*0a6a1f1dSLionel Sambuc OS << format("&Seqs[%u]", Idx);
767*0a6a1f1dSLionel Sambuc if (i+1 != e)
768*0a6a1f1dSLionel Sambuc OS << ",";
769*0a6a1f1dSLionel Sambuc OS << " // to " << SubRegIndices[i].getName() << "\n";
770*0a6a1f1dSLionel Sambuc }
771*0a6a1f1dSLionel Sambuc OS << " };\n\n";
772*0a6a1f1dSLionel Sambuc
773*0a6a1f1dSLionel Sambuc OS << " --IdxA; assert(IdxA < " << SubRegIndices.size()
774*0a6a1f1dSLionel Sambuc << " && \"Subregister index out of bounds\");\n"
775*0a6a1f1dSLionel Sambuc " unsigned Result = 0;\n"
776*0a6a1f1dSLionel Sambuc " for (const MaskRolOp *Ops = CompositeSequences[IdxA]; Ops->Mask != 0; ++Ops)"
777*0a6a1f1dSLionel Sambuc " {\n"
778*0a6a1f1dSLionel Sambuc " unsigned Masked = LaneMask & Ops->Mask;\n"
779*0a6a1f1dSLionel Sambuc " Result |= (Masked << Ops->RotateLeft) & 0xFFFFFFFF;\n"
780*0a6a1f1dSLionel Sambuc " Result |= (Masked >> ((32 - Ops->RotateLeft) & 0x1F));\n"
781*0a6a1f1dSLionel Sambuc " }\n"
782*0a6a1f1dSLionel Sambuc " return Result;\n"
783*0a6a1f1dSLionel Sambuc "}\n";
784*0a6a1f1dSLionel Sambuc }
785*0a6a1f1dSLionel Sambuc
786f4a2713aSLionel Sambuc //
787f4a2713aSLionel Sambuc // runMCDesc - Print out MC register descriptions.
788f4a2713aSLionel Sambuc //
789f4a2713aSLionel Sambuc void
runMCDesc(raw_ostream & OS,CodeGenTarget & Target,CodeGenRegBank & RegBank)790f4a2713aSLionel Sambuc RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
791f4a2713aSLionel Sambuc CodeGenRegBank &RegBank) {
792f4a2713aSLionel Sambuc emitSourceFileHeader("MC Register Information", OS);
793f4a2713aSLionel Sambuc
794f4a2713aSLionel Sambuc OS << "\n#ifdef GET_REGINFO_MC_DESC\n";
795f4a2713aSLionel Sambuc OS << "#undef GET_REGINFO_MC_DESC\n";
796f4a2713aSLionel Sambuc
797*0a6a1f1dSLionel Sambuc const auto &Regs = RegBank.getRegisters();
798f4a2713aSLionel Sambuc
799*0a6a1f1dSLionel Sambuc auto &SubRegIndices = RegBank.getSubRegIndices();
800f4a2713aSLionel Sambuc // The lists of sub-registers and super-registers go in the same array. That
801f4a2713aSLionel Sambuc // allows us to share suffixes.
802f4a2713aSLionel Sambuc typedef std::vector<const CodeGenRegister*> RegVec;
803f4a2713aSLionel Sambuc
804f4a2713aSLionel Sambuc // Differentially encoded lists.
805f4a2713aSLionel Sambuc SequenceToOffsetTable<DiffVec> DiffSeqs;
806f4a2713aSLionel Sambuc SmallVector<DiffVec, 4> SubRegLists(Regs.size());
807f4a2713aSLionel Sambuc SmallVector<DiffVec, 4> SuperRegLists(Regs.size());
808f4a2713aSLionel Sambuc SmallVector<DiffVec, 4> RegUnitLists(Regs.size());
809f4a2713aSLionel Sambuc SmallVector<unsigned, 4> RegUnitInitScale(Regs.size());
810f4a2713aSLionel Sambuc
811*0a6a1f1dSLionel Sambuc // List of lane masks accompanying register unit sequences.
812*0a6a1f1dSLionel Sambuc SequenceToOffsetTable<MaskVec> LaneMaskSeqs;
813*0a6a1f1dSLionel Sambuc SmallVector<MaskVec, 4> RegUnitLaneMasks(Regs.size());
814*0a6a1f1dSLionel Sambuc
815f4a2713aSLionel Sambuc // Keep track of sub-register names as well. These are not differentially
816f4a2713aSLionel Sambuc // encoded.
817f4a2713aSLionel Sambuc typedef SmallVector<const CodeGenSubRegIndex*, 4> SubRegIdxVec;
818f4a2713aSLionel Sambuc SequenceToOffsetTable<SubRegIdxVec, CodeGenSubRegIndex::Less> SubRegIdxSeqs;
819f4a2713aSLionel Sambuc SmallVector<SubRegIdxVec, 4> SubRegIdxLists(Regs.size());
820f4a2713aSLionel Sambuc
821f4a2713aSLionel Sambuc SequenceToOffsetTable<std::string> RegStrings;
822f4a2713aSLionel Sambuc
823f4a2713aSLionel Sambuc // Precompute register lists for the SequenceToOffsetTable.
824*0a6a1f1dSLionel Sambuc unsigned i = 0;
825*0a6a1f1dSLionel Sambuc for (auto I = Regs.begin(), E = Regs.end(); I != E; ++I, ++i) {
826*0a6a1f1dSLionel Sambuc const auto &Reg = *I;
827*0a6a1f1dSLionel Sambuc RegStrings.add(Reg.getName());
828f4a2713aSLionel Sambuc
829f4a2713aSLionel Sambuc // Compute the ordered sub-register list.
830f4a2713aSLionel Sambuc SetVector<const CodeGenRegister*> SR;
831*0a6a1f1dSLionel Sambuc Reg.addSubRegsPreOrder(SR, RegBank);
832*0a6a1f1dSLionel Sambuc diffEncode(SubRegLists[i], Reg.EnumValue, SR.begin(), SR.end());
833f4a2713aSLionel Sambuc DiffSeqs.add(SubRegLists[i]);
834f4a2713aSLionel Sambuc
835f4a2713aSLionel Sambuc // Compute the corresponding sub-register indexes.
836f4a2713aSLionel Sambuc SubRegIdxVec &SRIs = SubRegIdxLists[i];
837f4a2713aSLionel Sambuc for (unsigned j = 0, je = SR.size(); j != je; ++j)
838*0a6a1f1dSLionel Sambuc SRIs.push_back(Reg.getSubRegIndex(SR[j]));
839f4a2713aSLionel Sambuc SubRegIdxSeqs.add(SRIs);
840f4a2713aSLionel Sambuc
841f4a2713aSLionel Sambuc // Super-registers are already computed.
842*0a6a1f1dSLionel Sambuc const RegVec &SuperRegList = Reg.getSuperRegs();
843*0a6a1f1dSLionel Sambuc diffEncode(SuperRegLists[i], Reg.EnumValue, SuperRegList.begin(),
844*0a6a1f1dSLionel Sambuc SuperRegList.end());
845f4a2713aSLionel Sambuc DiffSeqs.add(SuperRegLists[i]);
846f4a2713aSLionel Sambuc
847f4a2713aSLionel Sambuc // Differentially encode the register unit list, seeded by register number.
848f4a2713aSLionel Sambuc // First compute a scale factor that allows more diff-lists to be reused:
849f4a2713aSLionel Sambuc //
850f4a2713aSLionel Sambuc // D0 -> (S0, S1)
851f4a2713aSLionel Sambuc // D1 -> (S2, S3)
852f4a2713aSLionel Sambuc //
853f4a2713aSLionel Sambuc // A scale factor of 2 allows D0 and D1 to share a diff-list. The initial
854f4a2713aSLionel Sambuc // value for the differential decoder is the register number multiplied by
855f4a2713aSLionel Sambuc // the scale.
856f4a2713aSLionel Sambuc //
857f4a2713aSLionel Sambuc // Check the neighboring registers for arithmetic progressions.
858f4a2713aSLionel Sambuc unsigned ScaleA = ~0u, ScaleB = ~0u;
859*0a6a1f1dSLionel Sambuc ArrayRef<unsigned> RUs = Reg.getNativeRegUnits();
860*0a6a1f1dSLionel Sambuc if (I != Regs.begin() &&
861*0a6a1f1dSLionel Sambuc std::prev(I)->getNativeRegUnits().size() == RUs.size())
862*0a6a1f1dSLionel Sambuc ScaleB = RUs.front() - std::prev(I)->getNativeRegUnits().front();
863*0a6a1f1dSLionel Sambuc if (std::next(I) != Regs.end() &&
864*0a6a1f1dSLionel Sambuc std::next(I)->getNativeRegUnits().size() == RUs.size())
865*0a6a1f1dSLionel Sambuc ScaleA = std::next(I)->getNativeRegUnits().front() - RUs.front();
866f4a2713aSLionel Sambuc unsigned Scale = std::min(ScaleB, ScaleA);
867f4a2713aSLionel Sambuc // Default the scale to 0 if it can't be encoded in 4 bits.
868f4a2713aSLionel Sambuc if (Scale >= 16)
869f4a2713aSLionel Sambuc Scale = 0;
870f4a2713aSLionel Sambuc RegUnitInitScale[i] = Scale;
871*0a6a1f1dSLionel Sambuc DiffSeqs.add(diffEncode(RegUnitLists[i], Scale * Reg.EnumValue, RUs));
872*0a6a1f1dSLionel Sambuc
873*0a6a1f1dSLionel Sambuc const auto &RUMasks = Reg.getRegUnitLaneMasks();
874*0a6a1f1dSLionel Sambuc MaskVec &LaneMaskVec = RegUnitLaneMasks[i];
875*0a6a1f1dSLionel Sambuc assert(LaneMaskVec.empty());
876*0a6a1f1dSLionel Sambuc LaneMaskVec.insert(LaneMaskVec.begin(), RUMasks.begin(), RUMasks.end());
877*0a6a1f1dSLionel Sambuc // Terminator mask should not be used inside of the list.
878*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
879*0a6a1f1dSLionel Sambuc for (unsigned M : LaneMaskVec) {
880*0a6a1f1dSLionel Sambuc assert(M != ~0u && "terminator mask should not be part of the list");
881*0a6a1f1dSLionel Sambuc }
882*0a6a1f1dSLionel Sambuc #endif
883*0a6a1f1dSLionel Sambuc LaneMaskSeqs.add(LaneMaskVec);
884f4a2713aSLionel Sambuc }
885f4a2713aSLionel Sambuc
886f4a2713aSLionel Sambuc // Compute the final layout of the sequence table.
887f4a2713aSLionel Sambuc DiffSeqs.layout();
888*0a6a1f1dSLionel Sambuc LaneMaskSeqs.layout();
889f4a2713aSLionel Sambuc SubRegIdxSeqs.layout();
890f4a2713aSLionel Sambuc
891f4a2713aSLionel Sambuc OS << "namespace llvm {\n\n";
892f4a2713aSLionel Sambuc
893f4a2713aSLionel Sambuc const std::string &TargetName = Target.getName();
894f4a2713aSLionel Sambuc
895f4a2713aSLionel Sambuc // Emit the shared table of differential lists.
896f4a2713aSLionel Sambuc OS << "extern const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
897f4a2713aSLionel Sambuc DiffSeqs.emit(OS, printDiff16);
898f4a2713aSLionel Sambuc OS << "};\n\n";
899f4a2713aSLionel Sambuc
900*0a6a1f1dSLionel Sambuc // Emit the shared table of regunit lane mask sequences.
901*0a6a1f1dSLionel Sambuc OS << "extern const unsigned " << TargetName << "LaneMaskLists[] = {\n";
902*0a6a1f1dSLionel Sambuc LaneMaskSeqs.emit(OS, printMask, "~0u");
903*0a6a1f1dSLionel Sambuc OS << "};\n\n";
904*0a6a1f1dSLionel Sambuc
905f4a2713aSLionel Sambuc // Emit the table of sub-register indexes.
906f4a2713aSLionel Sambuc OS << "extern const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
907f4a2713aSLionel Sambuc SubRegIdxSeqs.emit(OS, printSubRegIndex);
908f4a2713aSLionel Sambuc OS << "};\n\n";
909f4a2713aSLionel Sambuc
910f4a2713aSLionel Sambuc // Emit the table of sub-register index sizes.
911f4a2713aSLionel Sambuc OS << "extern const MCRegisterInfo::SubRegCoveredBits "
912f4a2713aSLionel Sambuc << TargetName << "SubRegIdxRanges[] = {\n";
913f4a2713aSLionel Sambuc OS << " { " << (uint16_t)-1 << ", " << (uint16_t)-1 << " },\n";
914*0a6a1f1dSLionel Sambuc for (const auto &Idx : SubRegIndices) {
915*0a6a1f1dSLionel Sambuc OS << " { " << Idx.Offset << ", " << Idx.Size << " },\t// "
916*0a6a1f1dSLionel Sambuc << Idx.getName() << "\n";
917f4a2713aSLionel Sambuc }
918f4a2713aSLionel Sambuc OS << "};\n\n";
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc // Emit the string table.
921f4a2713aSLionel Sambuc RegStrings.layout();
922f4a2713aSLionel Sambuc OS << "extern const char " << TargetName << "RegStrings[] = {\n";
923f4a2713aSLionel Sambuc RegStrings.emit(OS, printChar);
924f4a2713aSLionel Sambuc OS << "};\n\n";
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc OS << "extern const MCRegisterDesc " << TargetName
927f4a2713aSLionel Sambuc << "RegDesc[] = { // Descriptors\n";
928*0a6a1f1dSLionel Sambuc OS << " { " << RegStrings.get("") << ", 0, 0, 0, 0, 0 },\n";
929f4a2713aSLionel Sambuc
930f4a2713aSLionel Sambuc // Emit the register descriptors now.
931*0a6a1f1dSLionel Sambuc i = 0;
932*0a6a1f1dSLionel Sambuc for (const auto &Reg : Regs) {
933*0a6a1f1dSLionel Sambuc OS << " { " << RegStrings.get(Reg.getName()) << ", "
934*0a6a1f1dSLionel Sambuc << DiffSeqs.get(SubRegLists[i]) << ", " << DiffSeqs.get(SuperRegLists[i])
935*0a6a1f1dSLionel Sambuc << ", " << SubRegIdxSeqs.get(SubRegIdxLists[i]) << ", "
936*0a6a1f1dSLionel Sambuc << (DiffSeqs.get(RegUnitLists[i]) * 16 + RegUnitInitScale[i]) << ", "
937*0a6a1f1dSLionel Sambuc << LaneMaskSeqs.get(RegUnitLaneMasks[i]) << " },\n";
938*0a6a1f1dSLionel Sambuc ++i;
939f4a2713aSLionel Sambuc }
940f4a2713aSLionel Sambuc OS << "};\n\n"; // End of register descriptors...
941f4a2713aSLionel Sambuc
942f4a2713aSLionel Sambuc // Emit the table of register unit roots. Each regunit has one or two root
943f4a2713aSLionel Sambuc // registers.
944*0a6a1f1dSLionel Sambuc OS << "extern const MCPhysReg " << TargetName << "RegUnitRoots[][2] = {\n";
945f4a2713aSLionel Sambuc for (unsigned i = 0, e = RegBank.getNumNativeRegUnits(); i != e; ++i) {
946f4a2713aSLionel Sambuc ArrayRef<const CodeGenRegister*> Roots = RegBank.getRegUnit(i).getRoots();
947f4a2713aSLionel Sambuc assert(!Roots.empty() && "All regunits must have a root register.");
948f4a2713aSLionel Sambuc assert(Roots.size() <= 2 && "More than two roots not supported yet.");
949f4a2713aSLionel Sambuc OS << " { " << getQualifiedName(Roots.front()->TheDef);
950f4a2713aSLionel Sambuc for (unsigned r = 1; r != Roots.size(); ++r)
951f4a2713aSLionel Sambuc OS << ", " << getQualifiedName(Roots[r]->TheDef);
952f4a2713aSLionel Sambuc OS << " },\n";
953f4a2713aSLionel Sambuc }
954f4a2713aSLionel Sambuc OS << "};\n\n";
955f4a2713aSLionel Sambuc
956*0a6a1f1dSLionel Sambuc const auto &RegisterClasses = RegBank.getRegClasses();
957f4a2713aSLionel Sambuc
958f4a2713aSLionel Sambuc // Loop over all of the register classes... emitting each one.
959f4a2713aSLionel Sambuc OS << "namespace { // Register classes...\n";
960f4a2713aSLionel Sambuc
961*0a6a1f1dSLionel Sambuc SequenceToOffsetTable<std::string> RegClassStrings;
962*0a6a1f1dSLionel Sambuc
963f4a2713aSLionel Sambuc // Emit the register enum value arrays for each RegisterClass
964*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
965f4a2713aSLionel Sambuc ArrayRef<Record*> Order = RC.getOrder();
966f4a2713aSLionel Sambuc
967f4a2713aSLionel Sambuc // Give the register class a legal C name if it's anonymous.
968f4a2713aSLionel Sambuc std::string Name = RC.getName();
969f4a2713aSLionel Sambuc
970*0a6a1f1dSLionel Sambuc RegClassStrings.add(Name);
971*0a6a1f1dSLionel Sambuc
972f4a2713aSLionel Sambuc // Emit the register list now.
973f4a2713aSLionel Sambuc OS << " // " << Name << " Register Class...\n"
974*0a6a1f1dSLionel Sambuc << " const MCPhysReg " << Name
975f4a2713aSLionel Sambuc << "[] = {\n ";
976f4a2713aSLionel Sambuc for (unsigned i = 0, e = Order.size(); i != e; ++i) {
977f4a2713aSLionel Sambuc Record *Reg = Order[i];
978f4a2713aSLionel Sambuc OS << getQualifiedName(Reg) << ", ";
979f4a2713aSLionel Sambuc }
980f4a2713aSLionel Sambuc OS << "\n };\n\n";
981f4a2713aSLionel Sambuc
982f4a2713aSLionel Sambuc OS << " // " << Name << " Bit set.\n"
983f4a2713aSLionel Sambuc << " const uint8_t " << Name
984f4a2713aSLionel Sambuc << "Bits[] = {\n ";
985f4a2713aSLionel Sambuc BitVectorEmitter BVE;
986f4a2713aSLionel Sambuc for (unsigned i = 0, e = Order.size(); i != e; ++i) {
987f4a2713aSLionel Sambuc Record *Reg = Order[i];
988f4a2713aSLionel Sambuc BVE.add(Target.getRegBank().getReg(Reg)->EnumValue);
989f4a2713aSLionel Sambuc }
990f4a2713aSLionel Sambuc BVE.print(OS);
991f4a2713aSLionel Sambuc OS << "\n };\n\n";
992f4a2713aSLionel Sambuc
993f4a2713aSLionel Sambuc }
994f4a2713aSLionel Sambuc OS << "}\n\n";
995f4a2713aSLionel Sambuc
996*0a6a1f1dSLionel Sambuc RegClassStrings.layout();
997*0a6a1f1dSLionel Sambuc OS << "extern const char " << TargetName << "RegClassStrings[] = {\n";
998*0a6a1f1dSLionel Sambuc RegClassStrings.emit(OS, printChar);
999*0a6a1f1dSLionel Sambuc OS << "};\n\n";
1000*0a6a1f1dSLionel Sambuc
1001f4a2713aSLionel Sambuc OS << "extern const MCRegisterClass " << TargetName
1002f4a2713aSLionel Sambuc << "MCRegisterClasses[] = {\n";
1003f4a2713aSLionel Sambuc
1004*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1005f4a2713aSLionel Sambuc // Asserts to make sure values will fit in table assuming types from
1006f4a2713aSLionel Sambuc // MCRegisterInfo.h
1007f4a2713aSLionel Sambuc assert((RC.SpillSize/8) <= 0xffff && "SpillSize too large.");
1008f4a2713aSLionel Sambuc assert((RC.SpillAlignment/8) <= 0xffff && "SpillAlignment too large.");
1009f4a2713aSLionel Sambuc assert(RC.CopyCost >= -128 && RC.CopyCost <= 127 && "Copy cost too large.");
1010f4a2713aSLionel Sambuc
1011*0a6a1f1dSLionel Sambuc OS << " { " << RC.getName() << ", " << RC.getName() << "Bits, "
1012*0a6a1f1dSLionel Sambuc << RegClassStrings.get(RC.getName()) << ", "
1013f4a2713aSLionel Sambuc << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
1014f4a2713aSLionel Sambuc << RC.getQualifiedName() + "RegClassID" << ", "
1015f4a2713aSLionel Sambuc << RC.SpillSize/8 << ", "
1016f4a2713aSLionel Sambuc << RC.SpillAlignment/8 << ", "
1017f4a2713aSLionel Sambuc << RC.CopyCost << ", "
1018f4a2713aSLionel Sambuc << RC.Allocatable << " },\n";
1019f4a2713aSLionel Sambuc }
1020f4a2713aSLionel Sambuc
1021f4a2713aSLionel Sambuc OS << "};\n\n";
1022f4a2713aSLionel Sambuc
1023f4a2713aSLionel Sambuc EmitRegMappingTables(OS, Regs, false);
1024f4a2713aSLionel Sambuc
1025f4a2713aSLionel Sambuc // Emit Reg encoding table
1026f4a2713aSLionel Sambuc OS << "extern const uint16_t " << TargetName;
1027f4a2713aSLionel Sambuc OS << "RegEncodingTable[] = {\n";
1028f4a2713aSLionel Sambuc // Add entry for NoRegister
1029f4a2713aSLionel Sambuc OS << " 0,\n";
1030*0a6a1f1dSLionel Sambuc for (const auto &RE : Regs) {
1031*0a6a1f1dSLionel Sambuc Record *Reg = RE.TheDef;
1032f4a2713aSLionel Sambuc BitsInit *BI = Reg->getValueAsBitsInit("HWEncoding");
1033f4a2713aSLionel Sambuc uint64_t Value = 0;
1034f4a2713aSLionel Sambuc for (unsigned b = 0, be = BI->getNumBits(); b != be; ++b) {
1035f4a2713aSLionel Sambuc if (BitInit *B = dyn_cast<BitInit>(BI->getBit(b)))
1036f4a2713aSLionel Sambuc Value |= (uint64_t)B->getValue() << b;
1037f4a2713aSLionel Sambuc }
1038f4a2713aSLionel Sambuc OS << " " << Value << ",\n";
1039f4a2713aSLionel Sambuc }
1040f4a2713aSLionel Sambuc OS << "};\n"; // End of HW encoding table
1041f4a2713aSLionel Sambuc
1042f4a2713aSLionel Sambuc // MCRegisterInfo initialization routine.
1043f4a2713aSLionel Sambuc OS << "static inline void Init" << TargetName
1044f4a2713aSLionel Sambuc << "MCRegisterInfo(MCRegisterInfo *RI, unsigned RA, "
1045*0a6a1f1dSLionel Sambuc << "unsigned DwarfFlavour = 0, unsigned EHFlavour = 0, unsigned PC = 0) "
1046*0a6a1f1dSLionel Sambuc "{\n"
1047f4a2713aSLionel Sambuc << " RI->InitMCRegisterInfo(" << TargetName << "RegDesc, "
1048f4a2713aSLionel Sambuc << Regs.size() + 1 << ", RA, PC, " << TargetName << "MCRegisterClasses, "
1049*0a6a1f1dSLionel Sambuc << RegisterClasses.size() << ", " << TargetName << "RegUnitRoots, "
1050*0a6a1f1dSLionel Sambuc << RegBank.getNumNativeRegUnits() << ", " << TargetName << "RegDiffLists, "
1051*0a6a1f1dSLionel Sambuc << TargetName << "LaneMaskLists, " << TargetName << "RegStrings, "
1052*0a6a1f1dSLionel Sambuc << TargetName << "RegClassStrings, " << TargetName << "SubRegIdxLists, "
1053*0a6a1f1dSLionel Sambuc << (std::distance(SubRegIndices.begin(), SubRegIndices.end()) + 1) << ",\n"
1054*0a6a1f1dSLionel Sambuc << TargetName << "SubRegIdxRanges, " << TargetName
1055*0a6a1f1dSLionel Sambuc << "RegEncodingTable);\n\n";
1056f4a2713aSLionel Sambuc
1057f4a2713aSLionel Sambuc EmitRegMapping(OS, Regs, false);
1058f4a2713aSLionel Sambuc
1059f4a2713aSLionel Sambuc OS << "}\n\n";
1060f4a2713aSLionel Sambuc
1061f4a2713aSLionel Sambuc OS << "} // End llvm namespace\n";
1062f4a2713aSLionel Sambuc OS << "#endif // GET_REGINFO_MC_DESC\n\n";
1063f4a2713aSLionel Sambuc }
1064f4a2713aSLionel Sambuc
1065f4a2713aSLionel Sambuc void
runTargetHeader(raw_ostream & OS,CodeGenTarget & Target,CodeGenRegBank & RegBank)1066f4a2713aSLionel Sambuc RegisterInfoEmitter::runTargetHeader(raw_ostream &OS, CodeGenTarget &Target,
1067f4a2713aSLionel Sambuc CodeGenRegBank &RegBank) {
1068f4a2713aSLionel Sambuc emitSourceFileHeader("Register Information Header Fragment", OS);
1069f4a2713aSLionel Sambuc
1070f4a2713aSLionel Sambuc OS << "\n#ifdef GET_REGINFO_HEADER\n";
1071f4a2713aSLionel Sambuc OS << "#undef GET_REGINFO_HEADER\n";
1072f4a2713aSLionel Sambuc
1073f4a2713aSLionel Sambuc const std::string &TargetName = Target.getName();
1074f4a2713aSLionel Sambuc std::string ClassName = TargetName + "GenRegisterInfo";
1075f4a2713aSLionel Sambuc
1076f4a2713aSLionel Sambuc OS << "#include \"llvm/Target/TargetRegisterInfo.h\"\n\n";
1077f4a2713aSLionel Sambuc
1078f4a2713aSLionel Sambuc OS << "namespace llvm {\n\n";
1079f4a2713aSLionel Sambuc
1080f4a2713aSLionel Sambuc OS << "struct " << ClassName << " : public TargetRegisterInfo {\n"
1081f4a2713aSLionel Sambuc << " explicit " << ClassName
1082f4a2713aSLionel Sambuc << "(unsigned RA, unsigned D = 0, unsigned E = 0, unsigned PC = 0);\n"
1083*0a6a1f1dSLionel Sambuc << " bool needsStackRealignment(const MachineFunction &) const override\n"
1084f4a2713aSLionel Sambuc << " { return false; }\n";
1085f4a2713aSLionel Sambuc if (!RegBank.getSubRegIndices().empty()) {
1086*0a6a1f1dSLionel Sambuc OS << " unsigned composeSubRegIndicesImpl"
1087*0a6a1f1dSLionel Sambuc << "(unsigned, unsigned) const override;\n"
1088*0a6a1f1dSLionel Sambuc << " unsigned composeSubRegIndexLaneMaskImpl"
1089*0a6a1f1dSLionel Sambuc << "(unsigned, unsigned) const override;\n"
1090*0a6a1f1dSLionel Sambuc << " const TargetRegisterClass *getSubClassWithSubReg"
1091*0a6a1f1dSLionel Sambuc << "(const TargetRegisterClass*, unsigned) const override;\n";
1092f4a2713aSLionel Sambuc }
1093*0a6a1f1dSLionel Sambuc OS << " const RegClassWeight &getRegClassWeight("
1094*0a6a1f1dSLionel Sambuc << "const TargetRegisterClass *RC) const override;\n"
1095*0a6a1f1dSLionel Sambuc << " unsigned getRegUnitWeight(unsigned RegUnit) const override;\n"
1096*0a6a1f1dSLionel Sambuc << " unsigned getNumRegPressureSets() const override;\n"
1097*0a6a1f1dSLionel Sambuc << " const char *getRegPressureSetName(unsigned Idx) const override;\n"
1098*0a6a1f1dSLionel Sambuc << " unsigned getRegPressureSetLimit(unsigned Idx) const override;\n"
1099*0a6a1f1dSLionel Sambuc << " const int *getRegClassPressureSets("
1100*0a6a1f1dSLionel Sambuc << "const TargetRegisterClass *RC) const override;\n"
1101*0a6a1f1dSLionel Sambuc << " const int *getRegUnitPressureSets("
1102*0a6a1f1dSLionel Sambuc << "unsigned RegUnit) const override;\n"
1103f4a2713aSLionel Sambuc << "};\n\n";
1104f4a2713aSLionel Sambuc
1105*0a6a1f1dSLionel Sambuc const auto &RegisterClasses = RegBank.getRegClasses();
1106f4a2713aSLionel Sambuc
1107f4a2713aSLionel Sambuc if (!RegisterClasses.empty()) {
1108*0a6a1f1dSLionel Sambuc OS << "namespace " << RegisterClasses.front().Namespace
1109f4a2713aSLionel Sambuc << " { // Register classes\n";
1110f4a2713aSLionel Sambuc
1111*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1112f4a2713aSLionel Sambuc const std::string &Name = RC.getName();
1113f4a2713aSLionel Sambuc
1114f4a2713aSLionel Sambuc // Output the extern for the instance.
1115f4a2713aSLionel Sambuc OS << " extern const TargetRegisterClass " << Name << "RegClass;\n";
1116f4a2713aSLionel Sambuc }
1117f4a2713aSLionel Sambuc OS << "} // end of namespace " << TargetName << "\n\n";
1118f4a2713aSLionel Sambuc }
1119f4a2713aSLionel Sambuc OS << "} // End llvm namespace\n";
1120f4a2713aSLionel Sambuc OS << "#endif // GET_REGINFO_HEADER\n\n";
1121f4a2713aSLionel Sambuc }
1122f4a2713aSLionel Sambuc
1123f4a2713aSLionel Sambuc //
1124f4a2713aSLionel Sambuc // runTargetDesc - Output the target register and register file descriptions.
1125f4a2713aSLionel Sambuc //
1126f4a2713aSLionel Sambuc void
runTargetDesc(raw_ostream & OS,CodeGenTarget & Target,CodeGenRegBank & RegBank)1127f4a2713aSLionel Sambuc RegisterInfoEmitter::runTargetDesc(raw_ostream &OS, CodeGenTarget &Target,
1128f4a2713aSLionel Sambuc CodeGenRegBank &RegBank){
1129f4a2713aSLionel Sambuc emitSourceFileHeader("Target Register and Register Classes Information", OS);
1130f4a2713aSLionel Sambuc
1131f4a2713aSLionel Sambuc OS << "\n#ifdef GET_REGINFO_TARGET_DESC\n";
1132f4a2713aSLionel Sambuc OS << "#undef GET_REGINFO_TARGET_DESC\n";
1133f4a2713aSLionel Sambuc
1134f4a2713aSLionel Sambuc OS << "namespace llvm {\n\n";
1135f4a2713aSLionel Sambuc
1136f4a2713aSLionel Sambuc // Get access to MCRegisterClass data.
1137f4a2713aSLionel Sambuc OS << "extern const MCRegisterClass " << Target.getName()
1138f4a2713aSLionel Sambuc << "MCRegisterClasses[];\n";
1139f4a2713aSLionel Sambuc
1140f4a2713aSLionel Sambuc // Start out by emitting each of the register classes.
1141*0a6a1f1dSLionel Sambuc const auto &RegisterClasses = RegBank.getRegClasses();
1142*0a6a1f1dSLionel Sambuc const auto &SubRegIndices = RegBank.getSubRegIndices();
1143f4a2713aSLionel Sambuc
1144f4a2713aSLionel Sambuc // Collect all registers belonging to any allocatable class.
1145f4a2713aSLionel Sambuc std::set<Record*> AllocatableRegs;
1146f4a2713aSLionel Sambuc
1147f4a2713aSLionel Sambuc // Collect allocatable registers.
1148*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1149f4a2713aSLionel Sambuc ArrayRef<Record*> Order = RC.getOrder();
1150f4a2713aSLionel Sambuc
1151f4a2713aSLionel Sambuc if (RC.Allocatable)
1152f4a2713aSLionel Sambuc AllocatableRegs.insert(Order.begin(), Order.end());
1153f4a2713aSLionel Sambuc }
1154f4a2713aSLionel Sambuc
1155f4a2713aSLionel Sambuc // Build a shared array of value types.
1156f4a2713aSLionel Sambuc SequenceToOffsetTable<SmallVector<MVT::SimpleValueType, 4> > VTSeqs;
1157*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses)
1158*0a6a1f1dSLionel Sambuc VTSeqs.add(RC.VTs);
1159f4a2713aSLionel Sambuc VTSeqs.layout();
1160f4a2713aSLionel Sambuc OS << "\nstatic const MVT::SimpleValueType VTLists[] = {\n";
1161f4a2713aSLionel Sambuc VTSeqs.emit(OS, printSimpleValueType, "MVT::Other");
1162f4a2713aSLionel Sambuc OS << "};\n";
1163f4a2713aSLionel Sambuc
1164f4a2713aSLionel Sambuc // Emit SubRegIndex names, skipping 0.
1165f4a2713aSLionel Sambuc OS << "\nstatic const char *const SubRegIndexNameTable[] = { \"";
1166*0a6a1f1dSLionel Sambuc
1167*0a6a1f1dSLionel Sambuc for (const auto &Idx : SubRegIndices) {
1168*0a6a1f1dSLionel Sambuc OS << Idx.getName();
1169f4a2713aSLionel Sambuc OS << "\", \"";
1170f4a2713aSLionel Sambuc }
1171f4a2713aSLionel Sambuc OS << "\" };\n\n";
1172f4a2713aSLionel Sambuc
1173f4a2713aSLionel Sambuc // Emit SubRegIndex lane masks, including 0.
1174f4a2713aSLionel Sambuc OS << "\nstatic const unsigned SubRegIndexLaneMaskTable[] = {\n ~0u,\n";
1175*0a6a1f1dSLionel Sambuc for (const auto &Idx : SubRegIndices) {
1176*0a6a1f1dSLionel Sambuc OS << format(" 0x%08x, // ", Idx.LaneMask) << Idx.getName() << '\n';
1177f4a2713aSLionel Sambuc }
1178f4a2713aSLionel Sambuc OS << " };\n\n";
1179f4a2713aSLionel Sambuc
1180f4a2713aSLionel Sambuc OS << "\n";
1181f4a2713aSLionel Sambuc
1182f4a2713aSLionel Sambuc // Now that all of the structs have been emitted, emit the instances.
1183f4a2713aSLionel Sambuc if (!RegisterClasses.empty()) {
1184f4a2713aSLionel Sambuc OS << "\nstatic const TargetRegisterClass *const "
1185*0a6a1f1dSLionel Sambuc << "NullRegClasses[] = { nullptr };\n\n";
1186f4a2713aSLionel Sambuc
1187f4a2713aSLionel Sambuc // Emit register class bit mask tables. The first bit mask emitted for a
1188f4a2713aSLionel Sambuc // register class, RC, is the set of sub-classes, including RC itself.
1189f4a2713aSLionel Sambuc //
1190f4a2713aSLionel Sambuc // If RC has super-registers, also create a list of subreg indices and bit
1191f4a2713aSLionel Sambuc // masks, (Idx, Mask). The bit mask has a bit for every superreg regclass,
1192f4a2713aSLionel Sambuc // SuperRC, that satisfies:
1193f4a2713aSLionel Sambuc //
1194f4a2713aSLionel Sambuc // For all SuperReg in SuperRC: SuperReg:Idx in RC
1195f4a2713aSLionel Sambuc //
1196f4a2713aSLionel Sambuc // The 0-terminated list of subreg indices starts at:
1197f4a2713aSLionel Sambuc //
1198f4a2713aSLionel Sambuc // RC->getSuperRegIndices() = SuperRegIdxSeqs + ...
1199f4a2713aSLionel Sambuc //
1200f4a2713aSLionel Sambuc // The corresponding bitmasks follow the sub-class mask in memory. Each
1201f4a2713aSLionel Sambuc // mask has RCMaskWords uint32_t entries.
1202f4a2713aSLionel Sambuc //
1203f4a2713aSLionel Sambuc // Every bit mask present in the list has at least one bit set.
1204f4a2713aSLionel Sambuc
1205f4a2713aSLionel Sambuc // Compress the sub-reg index lists.
1206f4a2713aSLionel Sambuc typedef std::vector<const CodeGenSubRegIndex*> IdxList;
1207f4a2713aSLionel Sambuc SmallVector<IdxList, 8> SuperRegIdxLists(RegisterClasses.size());
1208f4a2713aSLionel Sambuc SequenceToOffsetTable<IdxList, CodeGenSubRegIndex::Less> SuperRegIdxSeqs;
1209f4a2713aSLionel Sambuc BitVector MaskBV(RegisterClasses.size());
1210f4a2713aSLionel Sambuc
1211*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1212f4a2713aSLionel Sambuc OS << "static const uint32_t " << RC.getName() << "SubClassMask[] = {\n ";
1213f4a2713aSLionel Sambuc printBitVectorAsHex(OS, RC.getSubClasses(), 32);
1214f4a2713aSLionel Sambuc
1215f4a2713aSLionel Sambuc // Emit super-reg class masks for any relevant SubRegIndices that can
1216f4a2713aSLionel Sambuc // project into RC.
1217*0a6a1f1dSLionel Sambuc IdxList &SRIList = SuperRegIdxLists[RC.EnumValue];
1218*0a6a1f1dSLionel Sambuc for (auto &Idx : SubRegIndices) {
1219f4a2713aSLionel Sambuc MaskBV.reset();
1220*0a6a1f1dSLionel Sambuc RC.getSuperRegClasses(&Idx, MaskBV);
1221f4a2713aSLionel Sambuc if (MaskBV.none())
1222f4a2713aSLionel Sambuc continue;
1223*0a6a1f1dSLionel Sambuc SRIList.push_back(&Idx);
1224f4a2713aSLionel Sambuc OS << "\n ";
1225f4a2713aSLionel Sambuc printBitVectorAsHex(OS, MaskBV, 32);
1226*0a6a1f1dSLionel Sambuc OS << "// " << Idx.getName();
1227f4a2713aSLionel Sambuc }
1228f4a2713aSLionel Sambuc SuperRegIdxSeqs.add(SRIList);
1229f4a2713aSLionel Sambuc OS << "\n};\n\n";
1230f4a2713aSLionel Sambuc }
1231f4a2713aSLionel Sambuc
1232f4a2713aSLionel Sambuc OS << "static const uint16_t SuperRegIdxSeqs[] = {\n";
1233f4a2713aSLionel Sambuc SuperRegIdxSeqs.layout();
1234f4a2713aSLionel Sambuc SuperRegIdxSeqs.emit(OS, printSubRegIndex);
1235f4a2713aSLionel Sambuc OS << "};\n\n";
1236f4a2713aSLionel Sambuc
1237f4a2713aSLionel Sambuc // Emit NULL terminated super-class lists.
1238*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1239f4a2713aSLionel Sambuc ArrayRef<CodeGenRegisterClass*> Supers = RC.getSuperClasses();
1240f4a2713aSLionel Sambuc
1241f4a2713aSLionel Sambuc // Skip classes without supers. We can reuse NullRegClasses.
1242f4a2713aSLionel Sambuc if (Supers.empty())
1243f4a2713aSLionel Sambuc continue;
1244f4a2713aSLionel Sambuc
1245f4a2713aSLionel Sambuc OS << "static const TargetRegisterClass *const "
1246f4a2713aSLionel Sambuc << RC.getName() << "Superclasses[] = {\n";
1247*0a6a1f1dSLionel Sambuc for (const auto *Super : Supers)
1248*0a6a1f1dSLionel Sambuc OS << " &" << Super->getQualifiedName() << "RegClass,\n";
1249*0a6a1f1dSLionel Sambuc OS << " nullptr\n};\n\n";
1250f4a2713aSLionel Sambuc }
1251f4a2713aSLionel Sambuc
1252f4a2713aSLionel Sambuc // Emit methods.
1253*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1254f4a2713aSLionel Sambuc if (!RC.AltOrderSelect.empty()) {
1255f4a2713aSLionel Sambuc OS << "\nstatic inline unsigned " << RC.getName()
1256f4a2713aSLionel Sambuc << "AltOrderSelect(const MachineFunction &MF) {"
1257f4a2713aSLionel Sambuc << RC.AltOrderSelect << "}\n\n"
1258f4a2713aSLionel Sambuc << "static ArrayRef<MCPhysReg> " << RC.getName()
1259f4a2713aSLionel Sambuc << "GetRawAllocationOrder(const MachineFunction &MF) {\n";
1260f4a2713aSLionel Sambuc for (unsigned oi = 1 , oe = RC.getNumOrders(); oi != oe; ++oi) {
1261f4a2713aSLionel Sambuc ArrayRef<Record*> Elems = RC.getOrder(oi);
1262f4a2713aSLionel Sambuc if (!Elems.empty()) {
1263f4a2713aSLionel Sambuc OS << " static const MCPhysReg AltOrder" << oi << "[] = {";
1264f4a2713aSLionel Sambuc for (unsigned elem = 0; elem != Elems.size(); ++elem)
1265f4a2713aSLionel Sambuc OS << (elem ? ", " : " ") << getQualifiedName(Elems[elem]);
1266f4a2713aSLionel Sambuc OS << " };\n";
1267f4a2713aSLionel Sambuc }
1268f4a2713aSLionel Sambuc }
1269f4a2713aSLionel Sambuc OS << " const MCRegisterClass &MCR = " << Target.getName()
1270f4a2713aSLionel Sambuc << "MCRegisterClasses[" << RC.getQualifiedName() + "RegClassID];\n"
1271f4a2713aSLionel Sambuc << " const ArrayRef<MCPhysReg> Order[] = {\n"
1272f4a2713aSLionel Sambuc << " makeArrayRef(MCR.begin(), MCR.getNumRegs()";
1273f4a2713aSLionel Sambuc for (unsigned oi = 1, oe = RC.getNumOrders(); oi != oe; ++oi)
1274f4a2713aSLionel Sambuc if (RC.getOrder(oi).empty())
1275f4a2713aSLionel Sambuc OS << "),\n ArrayRef<MCPhysReg>(";
1276f4a2713aSLionel Sambuc else
1277f4a2713aSLionel Sambuc OS << "),\n makeArrayRef(AltOrder" << oi;
1278f4a2713aSLionel Sambuc OS << ")\n };\n const unsigned Select = " << RC.getName()
1279f4a2713aSLionel Sambuc << "AltOrderSelect(MF);\n assert(Select < " << RC.getNumOrders()
1280f4a2713aSLionel Sambuc << ");\n return Order[Select];\n}\n";
1281f4a2713aSLionel Sambuc }
1282f4a2713aSLionel Sambuc }
1283f4a2713aSLionel Sambuc
1284f4a2713aSLionel Sambuc // Now emit the actual value-initialized register class instances.
1285*0a6a1f1dSLionel Sambuc OS << "\nnamespace " << RegisterClasses.front().Namespace
1286f4a2713aSLionel Sambuc << " { // Register class instances\n";
1287f4a2713aSLionel Sambuc
1288*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1289*0a6a1f1dSLionel Sambuc OS << " extern const TargetRegisterClass " << RC.getName()
1290*0a6a1f1dSLionel Sambuc << "RegClass = {\n " << '&' << Target.getName()
1291*0a6a1f1dSLionel Sambuc << "MCRegisterClasses[" << RC.getName() << "RegClassID],\n "
1292*0a6a1f1dSLionel Sambuc << "VTLists + " << VTSeqs.get(RC.VTs) << ",\n " << RC.getName()
1293*0a6a1f1dSLionel Sambuc << "SubClassMask,\n SuperRegIdxSeqs + "
1294*0a6a1f1dSLionel Sambuc << SuperRegIdxSeqs.get(SuperRegIdxLists[RC.EnumValue]) << ",\n "
1295*0a6a1f1dSLionel Sambuc << format("0x%08x,\n ", RC.LaneMask);
1296f4a2713aSLionel Sambuc if (RC.getSuperClasses().empty())
1297f4a2713aSLionel Sambuc OS << "NullRegClasses,\n ";
1298f4a2713aSLionel Sambuc else
1299f4a2713aSLionel Sambuc OS << RC.getName() << "Superclasses,\n ";
1300f4a2713aSLionel Sambuc if (RC.AltOrderSelect.empty())
1301*0a6a1f1dSLionel Sambuc OS << "nullptr\n";
1302f4a2713aSLionel Sambuc else
1303f4a2713aSLionel Sambuc OS << RC.getName() << "GetRawAllocationOrder\n";
1304f4a2713aSLionel Sambuc OS << " };\n\n";
1305f4a2713aSLionel Sambuc }
1306f4a2713aSLionel Sambuc
1307f4a2713aSLionel Sambuc OS << "}\n";
1308f4a2713aSLionel Sambuc }
1309f4a2713aSLionel Sambuc
1310f4a2713aSLionel Sambuc OS << "\nnamespace {\n";
1311f4a2713aSLionel Sambuc OS << " const TargetRegisterClass* const RegisterClasses[] = {\n";
1312*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses)
1313*0a6a1f1dSLionel Sambuc OS << " &" << RC.getQualifiedName() << "RegClass,\n";
1314f4a2713aSLionel Sambuc OS << " };\n";
1315f4a2713aSLionel Sambuc OS << "}\n"; // End of anonymous namespace...
1316f4a2713aSLionel Sambuc
1317f4a2713aSLionel Sambuc // Emit extra information about registers.
1318f4a2713aSLionel Sambuc const std::string &TargetName = Target.getName();
1319f4a2713aSLionel Sambuc OS << "\nstatic const TargetRegisterInfoDesc "
1320f4a2713aSLionel Sambuc << TargetName << "RegInfoDesc[] = { // Extra Descriptors\n";
1321f4a2713aSLionel Sambuc OS << " { 0, 0 },\n";
1322f4a2713aSLionel Sambuc
1323*0a6a1f1dSLionel Sambuc const auto &Regs = RegBank.getRegisters();
1324*0a6a1f1dSLionel Sambuc for (const auto &Reg : Regs) {
1325f4a2713aSLionel Sambuc OS << " { ";
1326f4a2713aSLionel Sambuc OS << Reg.CostPerUse << ", "
1327f4a2713aSLionel Sambuc << int(AllocatableRegs.count(Reg.TheDef)) << " },\n";
1328f4a2713aSLionel Sambuc }
1329f4a2713aSLionel Sambuc OS << "};\n"; // End of register descriptors...
1330f4a2713aSLionel Sambuc
1331f4a2713aSLionel Sambuc
1332f4a2713aSLionel Sambuc std::string ClassName = Target.getName() + "GenRegisterInfo";
1333f4a2713aSLionel Sambuc
1334*0a6a1f1dSLionel Sambuc auto SubRegIndicesSize =
1335*0a6a1f1dSLionel Sambuc std::distance(SubRegIndices.begin(), SubRegIndices.end());
1336*0a6a1f1dSLionel Sambuc
1337*0a6a1f1dSLionel Sambuc if (!SubRegIndices.empty()) {
1338f4a2713aSLionel Sambuc emitComposeSubRegIndices(OS, RegBank, ClassName);
1339*0a6a1f1dSLionel Sambuc emitComposeSubRegIndexLaneMask(OS, RegBank, ClassName);
1340*0a6a1f1dSLionel Sambuc }
1341f4a2713aSLionel Sambuc
1342f4a2713aSLionel Sambuc // Emit getSubClassWithSubReg.
1343f4a2713aSLionel Sambuc if (!SubRegIndices.empty()) {
1344f4a2713aSLionel Sambuc OS << "const TargetRegisterClass *" << ClassName
1345f4a2713aSLionel Sambuc << "::getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx)"
1346f4a2713aSLionel Sambuc << " const {\n";
1347f4a2713aSLionel Sambuc // Use the smallest type that can hold a regclass ID with room for a
1348f4a2713aSLionel Sambuc // sentinel.
1349f4a2713aSLionel Sambuc if (RegisterClasses.size() < UINT8_MAX)
1350f4a2713aSLionel Sambuc OS << " static const uint8_t Table[";
1351f4a2713aSLionel Sambuc else if (RegisterClasses.size() < UINT16_MAX)
1352f4a2713aSLionel Sambuc OS << " static const uint16_t Table[";
1353f4a2713aSLionel Sambuc else
1354f4a2713aSLionel Sambuc PrintFatalError("Too many register classes.");
1355*0a6a1f1dSLionel Sambuc OS << RegisterClasses.size() << "][" << SubRegIndicesSize << "] = {\n";
1356*0a6a1f1dSLionel Sambuc for (const auto &RC : RegisterClasses) {
1357f4a2713aSLionel Sambuc OS << " {\t// " << RC.getName() << "\n";
1358*0a6a1f1dSLionel Sambuc for (auto &Idx : SubRegIndices) {
1359*0a6a1f1dSLionel Sambuc if (CodeGenRegisterClass *SRC = RC.getSubClassWithSubReg(&Idx))
1360*0a6a1f1dSLionel Sambuc OS << " " << SRC->EnumValue + 1 << ",\t// " << Idx.getName()
1361f4a2713aSLionel Sambuc << " -> " << SRC->getName() << "\n";
1362f4a2713aSLionel Sambuc else
1363*0a6a1f1dSLionel Sambuc OS << " 0,\t// " << Idx.getName() << "\n";
1364f4a2713aSLionel Sambuc }
1365f4a2713aSLionel Sambuc OS << " },\n";
1366f4a2713aSLionel Sambuc }
1367f4a2713aSLionel Sambuc OS << " };\n assert(RC && \"Missing regclass\");\n"
1368f4a2713aSLionel Sambuc << " if (!Idx) return RC;\n --Idx;\n"
1369*0a6a1f1dSLionel Sambuc << " assert(Idx < " << SubRegIndicesSize << " && \"Bad subreg\");\n"
1370f4a2713aSLionel Sambuc << " unsigned TV = Table[RC->getID()][Idx];\n"
1371*0a6a1f1dSLionel Sambuc << " return TV ? getRegClass(TV - 1) : nullptr;\n}\n\n";
1372f4a2713aSLionel Sambuc }
1373f4a2713aSLionel Sambuc
1374f4a2713aSLionel Sambuc EmitRegUnitPressure(OS, RegBank, ClassName);
1375f4a2713aSLionel Sambuc
1376f4a2713aSLionel Sambuc // Emit the constructor of the class...
1377f4a2713aSLionel Sambuc OS << "extern const MCRegisterDesc " << TargetName << "RegDesc[];\n";
1378f4a2713aSLionel Sambuc OS << "extern const MCPhysReg " << TargetName << "RegDiffLists[];\n";
1379*0a6a1f1dSLionel Sambuc OS << "extern const unsigned " << TargetName << "LaneMaskLists[];\n";
1380f4a2713aSLionel Sambuc OS << "extern const char " << TargetName << "RegStrings[];\n";
1381*0a6a1f1dSLionel Sambuc OS << "extern const char " << TargetName << "RegClassStrings[];\n";
1382*0a6a1f1dSLionel Sambuc OS << "extern const MCPhysReg " << TargetName << "RegUnitRoots[][2];\n";
1383f4a2713aSLionel Sambuc OS << "extern const uint16_t " << TargetName << "SubRegIdxLists[];\n";
1384f4a2713aSLionel Sambuc OS << "extern const MCRegisterInfo::SubRegCoveredBits "
1385f4a2713aSLionel Sambuc << TargetName << "SubRegIdxRanges[];\n";
1386f4a2713aSLionel Sambuc OS << "extern const uint16_t " << TargetName << "RegEncodingTable[];\n";
1387f4a2713aSLionel Sambuc
1388f4a2713aSLionel Sambuc EmitRegMappingTables(OS, Regs, true);
1389f4a2713aSLionel Sambuc
1390f4a2713aSLionel Sambuc OS << ClassName << "::\n" << ClassName
1391f4a2713aSLionel Sambuc << "(unsigned RA, unsigned DwarfFlavour, unsigned EHFlavour, unsigned PC)\n"
1392f4a2713aSLionel Sambuc << " : TargetRegisterInfo(" << TargetName << "RegInfoDesc"
1393f4a2713aSLionel Sambuc << ", RegisterClasses, RegisterClasses+" << RegisterClasses.size() <<",\n"
1394f4a2713aSLionel Sambuc << " SubRegIndexNameTable, SubRegIndexLaneMaskTable, 0x";
1395f4a2713aSLionel Sambuc OS.write_hex(RegBank.CoveringLanes);
1396f4a2713aSLionel Sambuc OS << ") {\n"
1397*0a6a1f1dSLionel Sambuc << " InitMCRegisterInfo(" << TargetName << "RegDesc, " << Regs.size() + 1
1398*0a6a1f1dSLionel Sambuc << ", RA, PC,\n " << TargetName
1399f4a2713aSLionel Sambuc << "MCRegisterClasses, " << RegisterClasses.size() << ",\n"
1400f4a2713aSLionel Sambuc << " " << TargetName << "RegUnitRoots,\n"
1401f4a2713aSLionel Sambuc << " " << RegBank.getNumNativeRegUnits() << ",\n"
1402f4a2713aSLionel Sambuc << " " << TargetName << "RegDiffLists,\n"
1403*0a6a1f1dSLionel Sambuc << " " << TargetName << "LaneMaskLists,\n"
1404f4a2713aSLionel Sambuc << " " << TargetName << "RegStrings,\n"
1405*0a6a1f1dSLionel Sambuc << " " << TargetName << "RegClassStrings,\n"
1406f4a2713aSLionel Sambuc << " " << TargetName << "SubRegIdxLists,\n"
1407*0a6a1f1dSLionel Sambuc << " " << SubRegIndicesSize + 1 << ",\n"
1408f4a2713aSLionel Sambuc << " " << TargetName << "SubRegIdxRanges,\n"
1409f4a2713aSLionel Sambuc << " " << TargetName << "RegEncodingTable);\n\n";
1410f4a2713aSLionel Sambuc
1411f4a2713aSLionel Sambuc EmitRegMapping(OS, Regs, true);
1412f4a2713aSLionel Sambuc
1413f4a2713aSLionel Sambuc OS << "}\n\n";
1414f4a2713aSLionel Sambuc
1415f4a2713aSLionel Sambuc
1416f4a2713aSLionel Sambuc // Emit CalleeSavedRegs information.
1417f4a2713aSLionel Sambuc std::vector<Record*> CSRSets =
1418f4a2713aSLionel Sambuc Records.getAllDerivedDefinitions("CalleeSavedRegs");
1419f4a2713aSLionel Sambuc for (unsigned i = 0, e = CSRSets.size(); i != e; ++i) {
1420f4a2713aSLionel Sambuc Record *CSRSet = CSRSets[i];
1421f4a2713aSLionel Sambuc const SetTheory::RecVec *Regs = RegBank.getSets().expand(CSRSet);
1422f4a2713aSLionel Sambuc assert(Regs && "Cannot expand CalleeSavedRegs instance");
1423f4a2713aSLionel Sambuc
1424f4a2713aSLionel Sambuc // Emit the *_SaveList list of callee-saved registers.
1425f4a2713aSLionel Sambuc OS << "static const MCPhysReg " << CSRSet->getName()
1426f4a2713aSLionel Sambuc << "_SaveList[] = { ";
1427f4a2713aSLionel Sambuc for (unsigned r = 0, re = Regs->size(); r != re; ++r)
1428f4a2713aSLionel Sambuc OS << getQualifiedName((*Regs)[r]) << ", ";
1429f4a2713aSLionel Sambuc OS << "0 };\n";
1430f4a2713aSLionel Sambuc
1431f4a2713aSLionel Sambuc // Emit the *_RegMask bit mask of call-preserved registers.
1432f4a2713aSLionel Sambuc BitVector Covered = RegBank.computeCoveredRegisters(*Regs);
1433f4a2713aSLionel Sambuc
1434f4a2713aSLionel Sambuc // Check for an optional OtherPreserved set.
1435f4a2713aSLionel Sambuc // Add those registers to RegMask, but not to SaveList.
1436f4a2713aSLionel Sambuc if (DagInit *OPDag =
1437f4a2713aSLionel Sambuc dyn_cast<DagInit>(CSRSet->getValueInit("OtherPreserved"))) {
1438f4a2713aSLionel Sambuc SetTheory::RecSet OPSet;
1439f4a2713aSLionel Sambuc RegBank.getSets().evaluate(OPDag, OPSet, CSRSet->getLoc());
1440f4a2713aSLionel Sambuc Covered |= RegBank.computeCoveredRegisters(
1441f4a2713aSLionel Sambuc ArrayRef<Record*>(OPSet.begin(), OPSet.end()));
1442f4a2713aSLionel Sambuc }
1443f4a2713aSLionel Sambuc
1444f4a2713aSLionel Sambuc OS << "static const uint32_t " << CSRSet->getName()
1445f4a2713aSLionel Sambuc << "_RegMask[] = { ";
1446f4a2713aSLionel Sambuc printBitVectorAsHex(OS, Covered, 32);
1447f4a2713aSLionel Sambuc OS << "};\n";
1448f4a2713aSLionel Sambuc }
1449f4a2713aSLionel Sambuc OS << "\n\n";
1450f4a2713aSLionel Sambuc
1451f4a2713aSLionel Sambuc OS << "} // End llvm namespace\n";
1452f4a2713aSLionel Sambuc OS << "#endif // GET_REGINFO_TARGET_DESC\n\n";
1453f4a2713aSLionel Sambuc }
1454f4a2713aSLionel Sambuc
run(raw_ostream & OS)1455f4a2713aSLionel Sambuc void RegisterInfoEmitter::run(raw_ostream &OS) {
1456f4a2713aSLionel Sambuc CodeGenTarget Target(Records);
1457f4a2713aSLionel Sambuc CodeGenRegBank &RegBank = Target.getRegBank();
1458f4a2713aSLionel Sambuc RegBank.computeDerivedInfo();
1459f4a2713aSLionel Sambuc
1460f4a2713aSLionel Sambuc runEnums(OS, Target, RegBank);
1461f4a2713aSLionel Sambuc runMCDesc(OS, Target, RegBank);
1462f4a2713aSLionel Sambuc runTargetHeader(OS, Target, RegBank);
1463f4a2713aSLionel Sambuc runTargetDesc(OS, Target, RegBank);
1464f4a2713aSLionel Sambuc }
1465f4a2713aSLionel Sambuc
1466f4a2713aSLionel Sambuc namespace llvm {
1467f4a2713aSLionel Sambuc
EmitRegisterInfo(RecordKeeper & RK,raw_ostream & OS)1468f4a2713aSLionel Sambuc void EmitRegisterInfo(RecordKeeper &RK, raw_ostream &OS) {
1469f4a2713aSLionel Sambuc RegisterInfoEmitter(RK).run(OS);
1470f4a2713aSLionel Sambuc }
1471f4a2713aSLionel Sambuc
1472f4a2713aSLionel Sambuc } // End llvm namespace
1473