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