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