RegisterInfoEmitter.cpp 61 KB

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