InstrInfoEmitter.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. //===- InstrInfoEmitter.cpp - Generate a Instruction Set 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 the target
  10. // instruction set for the code generator.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenDAGPatterns.h"
  14. #include "CodeGenInstruction.h"
  15. #include "CodeGenSchedule.h"
  16. #include "CodeGenTarget.h"
  17. #include "PredicateExpander.h"
  18. #include "SequenceToOffsetTable.h"
  19. #include "TableGenBackends.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/StringExtras.h"
  23. #include "llvm/Support/Casting.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/TableGen/Error.h"
  26. #include "llvm/TableGen/Record.h"
  27. #include "llvm/TableGen/TableGenBackend.h"
  28. #include <cassert>
  29. #include <cstdint>
  30. #include <iterator>
  31. #include <map>
  32. #include <string>
  33. #include <utility>
  34. #include <vector>
  35. using namespace llvm;
  36. namespace {
  37. class InstrInfoEmitter {
  38. RecordKeeper &Records;
  39. CodeGenDAGPatterns CDP;
  40. const CodeGenSchedModels &SchedModels;
  41. public:
  42. InstrInfoEmitter(RecordKeeper &R):
  43. Records(R), CDP(R), SchedModels(CDP.getTargetInfo().getSchedModels()) {}
  44. // run - Output the instruction set description.
  45. void run(raw_ostream &OS);
  46. private:
  47. void emitEnums(raw_ostream &OS);
  48. typedef std::map<std::vector<std::string>, unsigned> OperandInfoMapTy;
  49. /// The keys of this map are maps which have OpName enum values as their keys
  50. /// and instruction operand indices as their values. The values of this map
  51. /// are lists of instruction names.
  52. typedef std::map<std::map<unsigned, unsigned>,
  53. std::vector<std::string>> OpNameMapTy;
  54. typedef std::map<std::string, unsigned>::iterator StrUintMapIter;
  55. /// Generate member functions in the target-specific GenInstrInfo class.
  56. ///
  57. /// This method is used to custom expand TIIPredicate definitions.
  58. /// See file llvm/Target/TargetInstPredicates.td for a description of what is
  59. /// a TIIPredicate and how to use it.
  60. void emitTIIHelperMethods(raw_ostream &OS, StringRef TargetName,
  61. bool ExpandDefinition = true);
  62. /// Expand TIIPredicate definitions to functions that accept a const MCInst
  63. /// reference.
  64. void emitMCIIHelperMethods(raw_ostream &OS, StringRef TargetName);
  65. void emitRecord(const CodeGenInstruction &Inst, unsigned Num,
  66. Record *InstrInfo,
  67. std::map<std::vector<Record*>, unsigned> &EL,
  68. const OperandInfoMapTy &OpInfo,
  69. raw_ostream &OS);
  70. void emitOperandTypeMappings(
  71. raw_ostream &OS, const CodeGenTarget &Target,
  72. ArrayRef<const CodeGenInstruction *> NumberedInstructions);
  73. void initOperandMapData(
  74. ArrayRef<const CodeGenInstruction *> NumberedInstructions,
  75. StringRef Namespace,
  76. std::map<std::string, unsigned> &Operands,
  77. OpNameMapTy &OperandMap);
  78. void emitOperandNameMappings(raw_ostream &OS, const CodeGenTarget &Target,
  79. ArrayRef<const CodeGenInstruction*> NumberedInstructions);
  80. void emitLogicalOperandSizeMappings(
  81. raw_ostream &OS, StringRef Namespace,
  82. ArrayRef<const CodeGenInstruction *> NumberedInstructions);
  83. void emitLogicalOperandTypeMappings(
  84. raw_ostream &OS, StringRef Namespace,
  85. ArrayRef<const CodeGenInstruction *> NumberedInstructions);
  86. // Operand information.
  87. void EmitOperandInfo(raw_ostream &OS, OperandInfoMapTy &OperandInfoIDs);
  88. std::vector<std::string> GetOperandInfo(const CodeGenInstruction &Inst);
  89. };
  90. } // end anonymous namespace
  91. static void PrintDefList(const std::vector<Record*> &Uses,
  92. unsigned Num, raw_ostream &OS) {
  93. OS << "static const MCPhysReg ImplicitList" << Num << "[] = { ";
  94. for (Record *U : Uses)
  95. OS << getQualifiedName(U) << ", ";
  96. OS << "0 };\n";
  97. }
  98. //===----------------------------------------------------------------------===//
  99. // Operand Info Emission.
  100. //===----------------------------------------------------------------------===//
  101. std::vector<std::string>
  102. InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
  103. std::vector<std::string> Result;
  104. for (auto &Op : Inst.Operands) {
  105. // Handle aggregate operands and normal operands the same way by expanding
  106. // either case into a list of operands for this op.
  107. std::vector<CGIOperandList::OperandInfo> OperandList;
  108. // This might be a multiple operand thing. Targets like X86 have
  109. // registers in their multi-operand operands. It may also be an anonymous
  110. // operand, which has a single operand, but no declared class for the
  111. // operand.
  112. DagInit *MIOI = Op.MIOperandInfo;
  113. if (!MIOI || MIOI->getNumArgs() == 0) {
  114. // Single, anonymous, operand.
  115. OperandList.push_back(Op);
  116. } else {
  117. for (unsigned j = 0, e = Op.MINumOperands; j != e; ++j) {
  118. OperandList.push_back(Op);
  119. auto *OpR = cast<DefInit>(MIOI->getArg(j))->getDef();
  120. OperandList.back().Rec = OpR;
  121. }
  122. }
  123. for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
  124. Record *OpR = OperandList[j].Rec;
  125. std::string Res;
  126. if (OpR->isSubClassOf("RegisterOperand"))
  127. OpR = OpR->getValueAsDef("RegClass");
  128. if (OpR->isSubClassOf("RegisterClass"))
  129. Res += getQualifiedName(OpR) + "RegClassID, ";
  130. else if (OpR->isSubClassOf("PointerLikeRegClass"))
  131. Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", ";
  132. else
  133. // -1 means the operand does not have a fixed register class.
  134. Res += "-1, ";
  135. // Fill in applicable flags.
  136. Res += "0";
  137. // Ptr value whose register class is resolved via callback.
  138. if (OpR->isSubClassOf("PointerLikeRegClass"))
  139. Res += "|(1<<MCOI::LookupPtrRegClass)";
  140. // Predicate operands. Check to see if the original unexpanded operand
  141. // was of type PredicateOp.
  142. if (Op.Rec->isSubClassOf("PredicateOp"))
  143. Res += "|(1<<MCOI::Predicate)";
  144. // Optional def operands. Check to see if the original unexpanded operand
  145. // was of type OptionalDefOperand.
  146. if (Op.Rec->isSubClassOf("OptionalDefOperand"))
  147. Res += "|(1<<MCOI::OptionalDef)";
  148. // Branch target operands. Check to see if the original unexpanded
  149. // operand was of type BranchTargetOperand.
  150. if (Op.Rec->isSubClassOf("BranchTargetOperand"))
  151. Res += "|(1<<MCOI::BranchTarget)";
  152. // Fill in operand type.
  153. Res += ", ";
  154. assert(!Op.OperandType.empty() && "Invalid operand type.");
  155. Res += Op.OperandType;
  156. // Fill in constraint info.
  157. Res += ", ";
  158. const CGIOperandList::ConstraintInfo &Constraint =
  159. Op.Constraints[j];
  160. if (Constraint.isNone())
  161. Res += "0";
  162. else if (Constraint.isEarlyClobber())
  163. Res += "MCOI_EARLY_CLOBBER";
  164. else {
  165. assert(Constraint.isTied());
  166. Res += "MCOI_TIED_TO(" + utostr(Constraint.getTiedOperand()) + ")";
  167. }
  168. Result.push_back(Res);
  169. }
  170. }
  171. return Result;
  172. }
  173. void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
  174. OperandInfoMapTy &OperandInfoIDs) {
  175. // ID #0 is for no operand info.
  176. unsigned OperandListNum = 0;
  177. OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
  178. OS << "\n";
  179. const CodeGenTarget &Target = CDP.getTargetInfo();
  180. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
  181. std::vector<std::string> OperandInfo = GetOperandInfo(*Inst);
  182. unsigned &N = OperandInfoIDs[OperandInfo];
  183. if (N != 0) continue;
  184. N = ++OperandListNum;
  185. OS << "static const MCOperandInfo OperandInfo" << N << "[] = { ";
  186. for (const std::string &Info : OperandInfo)
  187. OS << "{ " << Info << " }, ";
  188. OS << "};\n";
  189. }
  190. }
  191. /// Initialize data structures for generating operand name mappings.
  192. ///
  193. /// \param Operands [out] A map used to generate the OpName enum with operand
  194. /// names as its keys and operand enum values as its values.
  195. /// \param OperandMap [out] A map for representing the operand name mappings for
  196. /// each instructions. This is used to generate the OperandMap table as
  197. /// well as the getNamedOperandIdx() function.
  198. void InstrInfoEmitter::initOperandMapData(
  199. ArrayRef<const CodeGenInstruction *> NumberedInstructions,
  200. StringRef Namespace,
  201. std::map<std::string, unsigned> &Operands,
  202. OpNameMapTy &OperandMap) {
  203. unsigned NumOperands = 0;
  204. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  205. if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable"))
  206. continue;
  207. std::map<unsigned, unsigned> OpList;
  208. for (const auto &Info : Inst->Operands) {
  209. StrUintMapIter I = Operands.find(Info.Name);
  210. if (I == Operands.end()) {
  211. I = Operands.insert(Operands.begin(),
  212. std::pair<std::string, unsigned>(Info.Name, NumOperands++));
  213. }
  214. OpList[I->second] = Info.MIOperandNo;
  215. }
  216. OperandMap[OpList].push_back(Namespace.str() + "::" +
  217. Inst->TheDef->getName().str());
  218. }
  219. }
  220. /// Generate a table and function for looking up the indices of operands by
  221. /// name.
  222. ///
  223. /// This code generates:
  224. /// - An enum in the llvm::TargetNamespace::OpName namespace, with one entry
  225. /// for each operand name.
  226. /// - A 2-dimensional table called OperandMap for mapping OpName enum values to
  227. /// operand indices.
  228. /// - A function called getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
  229. /// for looking up the operand index for an instruction, given a value from
  230. /// OpName enum
  231. void InstrInfoEmitter::emitOperandNameMappings(raw_ostream &OS,
  232. const CodeGenTarget &Target,
  233. ArrayRef<const CodeGenInstruction*> NumberedInstructions) {
  234. StringRef Namespace = Target.getInstNamespace();
  235. std::string OpNameNS = "OpName";
  236. // Map of operand names to their enumeration value. This will be used to
  237. // generate the OpName enum.
  238. std::map<std::string, unsigned> Operands;
  239. OpNameMapTy OperandMap;
  240. initOperandMapData(NumberedInstructions, Namespace, Operands, OperandMap);
  241. OS << "#ifdef GET_INSTRINFO_OPERAND_ENUM\n";
  242. OS << "#undef GET_INSTRINFO_OPERAND_ENUM\n";
  243. OS << "namespace llvm {\n";
  244. OS << "namespace " << Namespace << " {\n";
  245. OS << "namespace " << OpNameNS << " {\n";
  246. OS << "enum {\n";
  247. for (const auto &Op : Operands)
  248. OS << " " << Op.first << " = " << Op.second << ",\n";
  249. OS << " OPERAND_LAST";
  250. OS << "\n};\n";
  251. OS << "} // end namespace OpName\n";
  252. OS << "} // end namespace " << Namespace << "\n";
  253. OS << "} // end namespace llvm\n";
  254. OS << "#endif //GET_INSTRINFO_OPERAND_ENUM\n\n";
  255. OS << "#ifdef GET_INSTRINFO_NAMED_OPS\n";
  256. OS << "#undef GET_INSTRINFO_NAMED_OPS\n";
  257. OS << "namespace llvm {\n";
  258. OS << "namespace " << Namespace << " {\n";
  259. OS << "LLVM_READONLY\n";
  260. OS << "int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) {\n";
  261. if (!Operands.empty()) {
  262. OS << " static const int16_t OperandMap [][" << Operands.size()
  263. << "] = {\n";
  264. for (const auto &Entry : OperandMap) {
  265. const std::map<unsigned, unsigned> &OpList = Entry.first;
  266. OS << "{";
  267. // Emit a row of the OperandMap table
  268. for (unsigned i = 0, e = Operands.size(); i != e; ++i)
  269. OS << (OpList.count(i) == 0 ? -1 : (int)OpList.find(i)->second) << ", ";
  270. OS << "},\n";
  271. }
  272. OS << "};\n";
  273. OS << " switch(Opcode) {\n";
  274. unsigned TableIndex = 0;
  275. for (const auto &Entry : OperandMap) {
  276. for (const std::string &Name : Entry.second)
  277. OS << " case " << Name << ":\n";
  278. OS << " return OperandMap[" << TableIndex++ << "][NamedIdx];\n";
  279. }
  280. OS << " default: return -1;\n";
  281. OS << " }\n";
  282. } else {
  283. // There are no operands, so no need to emit anything
  284. OS << " return -1;\n";
  285. }
  286. OS << "}\n";
  287. OS << "} // end namespace " << Namespace << "\n";
  288. OS << "} // end namespace llvm\n";
  289. OS << "#endif //GET_INSTRINFO_NAMED_OPS\n\n";
  290. }
  291. /// Generate an enum for all the operand types for this target, under the
  292. /// llvm::TargetNamespace::OpTypes namespace.
  293. /// Operand types are all definitions derived of the Operand Target.td class.
  294. void InstrInfoEmitter::emitOperandTypeMappings(
  295. raw_ostream &OS, const CodeGenTarget &Target,
  296. ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
  297. StringRef Namespace = Target.getInstNamespace();
  298. std::vector<Record *> Operands = Records.getAllDerivedDefinitions("Operand");
  299. std::vector<Record *> RegisterOperands =
  300. Records.getAllDerivedDefinitions("RegisterOperand");
  301. std::vector<Record *> RegisterClasses =
  302. Records.getAllDerivedDefinitions("RegisterClass");
  303. OS << "#ifdef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
  304. OS << "#undef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
  305. OS << "namespace llvm {\n";
  306. OS << "namespace " << Namespace << " {\n";
  307. OS << "namespace OpTypes {\n";
  308. OS << "enum OperandType {\n";
  309. unsigned EnumVal = 0;
  310. for (const std::vector<Record *> *RecordsToAdd :
  311. {&Operands, &RegisterOperands, &RegisterClasses}) {
  312. for (const Record *Op : *RecordsToAdd) {
  313. if (!Op->isAnonymous())
  314. OS << " " << Op->getName() << " = " << EnumVal << ",\n";
  315. ++EnumVal;
  316. }
  317. }
  318. OS << " OPERAND_TYPE_LIST_END" << "\n};\n";
  319. OS << "} // end namespace OpTypes\n";
  320. OS << "} // end namespace " << Namespace << "\n";
  321. OS << "} // end namespace llvm\n";
  322. OS << "#endif // GET_INSTRINFO_OPERAND_TYPES_ENUM\n\n";
  323. OS << "#ifdef GET_INSTRINFO_OPERAND_TYPE\n";
  324. OS << "#undef GET_INSTRINFO_OPERAND_TYPE\n";
  325. OS << "namespace llvm {\n";
  326. OS << "namespace " << Namespace << " {\n";
  327. OS << "LLVM_READONLY\n";
  328. OS << "static int getOperandType(uint16_t Opcode, uint16_t OpIdx) {\n";
  329. // TODO: Factor out duplicate operand lists to compress the tables.
  330. if (!NumberedInstructions.empty()) {
  331. std::vector<int> OperandOffsets;
  332. std::vector<Record *> OperandRecords;
  333. int CurrentOffset = 0;
  334. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  335. OperandOffsets.push_back(CurrentOffset);
  336. for (const auto &Op : Inst->Operands) {
  337. const DagInit *MIOI = Op.MIOperandInfo;
  338. if (!MIOI || MIOI->getNumArgs() == 0) {
  339. // Single, anonymous, operand.
  340. OperandRecords.push_back(Op.Rec);
  341. ++CurrentOffset;
  342. } else {
  343. for (Init *Arg : MIOI->getArgs()) {
  344. OperandRecords.push_back(cast<DefInit>(Arg)->getDef());
  345. ++CurrentOffset;
  346. }
  347. }
  348. }
  349. }
  350. // Emit the table of offsets (indexes) into the operand type table.
  351. // Size the unsigned integer offset to save space.
  352. assert(OperandRecords.size() <= UINT32_MAX &&
  353. "Too many operands for offset table");
  354. OS << ((OperandRecords.size() <= UINT16_MAX) ? " const uint16_t"
  355. : " const uint32_t");
  356. OS << " Offsets[] = {\n";
  357. for (int I = 0, E = OperandOffsets.size(); I != E; ++I)
  358. OS << " " << OperandOffsets[I] << ",\n";
  359. OS << " };\n";
  360. // Add an entry for the end so that we don't need to special case it below.
  361. OperandOffsets.push_back(OperandRecords.size());
  362. // Emit the actual operand types in a flat table.
  363. // Size the signed integer operand type to save space.
  364. assert(EnumVal <= INT16_MAX &&
  365. "Too many operand types for operand types table");
  366. OS << ((EnumVal <= INT8_MAX) ? " const int8_t" : " const int16_t");
  367. OS << " OpcodeOperandTypes[] = {\n ";
  368. for (int I = 0, E = OperandRecords.size(), CurOffset = 1; I != E; ++I) {
  369. // We print each Opcode's operands in its own row.
  370. if (I == OperandOffsets[CurOffset]) {
  371. OS << "\n ";
  372. // If there are empty rows, mark them with an empty comment.
  373. while (OperandOffsets[++CurOffset] == I)
  374. OS << "/**/\n ";
  375. }
  376. Record *OpR = OperandRecords[I];
  377. if ((OpR->isSubClassOf("Operand") ||
  378. OpR->isSubClassOf("RegisterOperand") ||
  379. OpR->isSubClassOf("RegisterClass")) &&
  380. !OpR->isAnonymous())
  381. OS << "OpTypes::" << OpR->getName();
  382. else
  383. OS << -1;
  384. OS << ", ";
  385. }
  386. OS << "\n };\n";
  387. OS << " return OpcodeOperandTypes[Offsets[Opcode] + OpIdx];\n";
  388. } else {
  389. OS << " llvm_unreachable(\"No instructions defined\");\n";
  390. }
  391. OS << "}\n";
  392. OS << "} // end namespace " << Namespace << "\n";
  393. OS << "} // end namespace llvm\n";
  394. OS << "#endif // GET_INSTRINFO_OPERAND_TYPE\n\n";
  395. }
  396. void InstrInfoEmitter::emitLogicalOperandSizeMappings(
  397. raw_ostream &OS, StringRef Namespace,
  398. ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
  399. std::map<std::vector<unsigned>, unsigned> LogicalOpSizeMap;
  400. std::map<unsigned, std::vector<std::string>> InstMap;
  401. size_t LogicalOpListSize = 0U;
  402. std::vector<unsigned> LogicalOpList;
  403. for (const auto *Inst : NumberedInstructions) {
  404. if (!Inst->TheDef->getValueAsBit("UseLogicalOperandMappings"))
  405. continue;
  406. LogicalOpList.clear();
  407. llvm::transform(Inst->Operands, std::back_inserter(LogicalOpList),
  408. [](const CGIOperandList::OperandInfo &Op) -> unsigned {
  409. auto *MIOI = Op.MIOperandInfo;
  410. if (!MIOI || MIOI->getNumArgs() == 0)
  411. return 1;
  412. return MIOI->getNumArgs();
  413. });
  414. LogicalOpListSize = std::max(LogicalOpList.size(), LogicalOpListSize);
  415. auto I =
  416. LogicalOpSizeMap.insert({LogicalOpList, LogicalOpSizeMap.size()}).first;
  417. InstMap[I->second].push_back(
  418. (Namespace + "::" + Inst->TheDef->getName()).str());
  419. }
  420. OS << "#ifdef GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n";
  421. OS << "#undef GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n";
  422. OS << "namespace llvm {\n";
  423. OS << "namespace " << Namespace << " {\n";
  424. OS << "LLVM_READONLY static unsigned\n";
  425. OS << "getLogicalOperandSize(uint16_t Opcode, uint16_t LogicalOpIdx) {\n";
  426. if (!InstMap.empty()) {
  427. std::vector<const std::vector<unsigned> *> LogicalOpSizeList(
  428. LogicalOpSizeMap.size());
  429. for (auto &P : LogicalOpSizeMap) {
  430. LogicalOpSizeList[P.second] = &P.first;
  431. }
  432. OS << " static const unsigned SizeMap[][" << LogicalOpListSize
  433. << "] = {\n";
  434. for (auto &R : LogicalOpSizeList) {
  435. const auto &Row = *R;
  436. OS << " {";
  437. int i;
  438. for (i = 0; i < static_cast<int>(Row.size()); ++i) {
  439. OS << Row[i] << ", ";
  440. }
  441. for (; i < static_cast<int>(LogicalOpListSize); ++i) {
  442. OS << "0, ";
  443. }
  444. OS << "}, ";
  445. OS << "\n";
  446. }
  447. OS << " };\n";
  448. OS << " switch (Opcode) {\n";
  449. OS << " default: return LogicalOpIdx;\n";
  450. for (auto &P : InstMap) {
  451. auto OpMapIdx = P.first;
  452. const auto &Insts = P.second;
  453. for (const auto &Inst : Insts) {
  454. OS << " case " << Inst << ":\n";
  455. }
  456. OS << " return SizeMap[" << OpMapIdx << "][LogicalOpIdx];\n";
  457. }
  458. OS << " }\n";
  459. } else {
  460. OS << " return LogicalOpIdx;\n";
  461. }
  462. OS << "}\n";
  463. OS << "LLVM_READONLY static inline unsigned\n";
  464. OS << "getLogicalOperandIdx(uint16_t Opcode, uint16_t LogicalOpIdx) {\n";
  465. OS << " auto S = 0U;\n";
  466. OS << " for (auto i = 0U; i < LogicalOpIdx; ++i)\n";
  467. OS << " S += getLogicalOperandSize(Opcode, i);\n";
  468. OS << " return S;\n";
  469. OS << "}\n";
  470. OS << "} // end namespace " << Namespace << "\n";
  471. OS << "} // end namespace llvm\n";
  472. OS << "#endif // GET_INSTRINFO_LOGICAL_OPERAND_SIZE_MAP\n\n";
  473. }
  474. void InstrInfoEmitter::emitLogicalOperandTypeMappings(
  475. raw_ostream &OS, StringRef Namespace,
  476. ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
  477. std::map<std::vector<std::string>, unsigned> LogicalOpTypeMap;
  478. std::map<unsigned, std::vector<std::string>> InstMap;
  479. size_t OpTypeListSize = 0U;
  480. std::vector<std::string> LogicalOpTypeList;
  481. for (const auto *Inst : NumberedInstructions) {
  482. if (!Inst->TheDef->getValueAsBit("UseLogicalOperandMappings"))
  483. continue;
  484. LogicalOpTypeList.clear();
  485. for (const auto &Op : Inst->Operands) {
  486. auto *OpR = Op.Rec;
  487. if ((OpR->isSubClassOf("Operand") ||
  488. OpR->isSubClassOf("RegisterOperand") ||
  489. OpR->isSubClassOf("RegisterClass")) &&
  490. !OpR->isAnonymous()) {
  491. LogicalOpTypeList.push_back(
  492. (Namespace + "::OpTypes::" + Op.Rec->getName()).str());
  493. } else {
  494. LogicalOpTypeList.push_back("-1");
  495. }
  496. }
  497. OpTypeListSize = std::max(LogicalOpTypeList.size(), OpTypeListSize);
  498. auto I =
  499. LogicalOpTypeMap.insert({LogicalOpTypeList, LogicalOpTypeMap.size()})
  500. .first;
  501. InstMap[I->second].push_back(
  502. (Namespace + "::" + Inst->TheDef->getName()).str());
  503. }
  504. OS << "#ifdef GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n";
  505. OS << "#undef GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n";
  506. OS << "namespace llvm {\n";
  507. OS << "namespace " << Namespace << " {\n";
  508. OS << "LLVM_READONLY static int\n";
  509. OS << "getLogicalOperandType(uint16_t Opcode, uint16_t LogicalOpIdx) {\n";
  510. if (!InstMap.empty()) {
  511. std::vector<const std::vector<std::string> *> LogicalOpTypeList(
  512. LogicalOpTypeMap.size());
  513. for (auto &P : LogicalOpTypeMap) {
  514. LogicalOpTypeList[P.second] = &P.first;
  515. }
  516. OS << " static const int TypeMap[][" << OpTypeListSize << "] = {\n";
  517. for (int r = 0, rs = LogicalOpTypeList.size(); r < rs; ++r) {
  518. const auto &Row = *LogicalOpTypeList[r];
  519. OS << " {";
  520. int i, s = Row.size();
  521. for (i = 0; i < s; ++i) {
  522. if (i > 0)
  523. OS << ", ";
  524. OS << Row[i];
  525. }
  526. for (; i < static_cast<int>(OpTypeListSize); ++i) {
  527. if (i > 0)
  528. OS << ", ";
  529. OS << "-1";
  530. }
  531. OS << "}";
  532. if (r != rs - 1)
  533. OS << ",";
  534. OS << "\n";
  535. }
  536. OS << " };\n";
  537. OS << " switch (Opcode) {\n";
  538. OS << " default: return -1;\n";
  539. for (auto &P : InstMap) {
  540. auto OpMapIdx = P.first;
  541. const auto &Insts = P.second;
  542. for (const auto &Inst : Insts) {
  543. OS << " case " << Inst << ":\n";
  544. }
  545. OS << " return TypeMap[" << OpMapIdx << "][LogicalOpIdx];\n";
  546. }
  547. OS << " }\n";
  548. } else {
  549. OS << " return -1;\n";
  550. }
  551. OS << "}\n";
  552. OS << "} // end namespace " << Namespace << "\n";
  553. OS << "} // end namespace llvm\n";
  554. OS << "#endif // GET_INSTRINFO_LOGICAL_OPERAND_TYPE_MAP\n\n";
  555. }
  556. void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS,
  557. StringRef TargetName) {
  558. RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
  559. if (TIIPredicates.empty())
  560. return;
  561. OS << "#ifdef GET_INSTRINFO_MC_HELPER_DECLS\n";
  562. OS << "#undef GET_INSTRINFO_MC_HELPER_DECLS\n\n";
  563. OS << "namespace llvm {\n";
  564. OS << "class MCInst;\n\n";
  565. OS << "namespace " << TargetName << "_MC {\n\n";
  566. for (const Record *Rec : TIIPredicates) {
  567. OS << "bool " << Rec->getValueAsString("FunctionName")
  568. << "(const MCInst &MI);\n";
  569. }
  570. OS << "\n} // end namespace " << TargetName << "_MC\n";
  571. OS << "} // end namespace llvm\n\n";
  572. OS << "#endif // GET_INSTRINFO_MC_HELPER_DECLS\n\n";
  573. OS << "#ifdef GET_INSTRINFO_MC_HELPERS\n";
  574. OS << "#undef GET_INSTRINFO_MC_HELPERS\n\n";
  575. OS << "namespace llvm {\n";
  576. OS << "namespace " << TargetName << "_MC {\n\n";
  577. PredicateExpander PE(TargetName);
  578. PE.setExpandForMC(true);
  579. for (const Record *Rec : TIIPredicates) {
  580. OS << "bool " << Rec->getValueAsString("FunctionName");
  581. OS << "(const MCInst &MI) {\n";
  582. OS.indent(PE.getIndentLevel() * 2);
  583. PE.expandStatement(OS, Rec->getValueAsDef("Body"));
  584. OS << "\n}\n\n";
  585. }
  586. OS << "} // end namespace " << TargetName << "_MC\n";
  587. OS << "} // end namespace llvm\n\n";
  588. OS << "#endif // GET_GENISTRINFO_MC_HELPERS\n";
  589. }
  590. void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS,
  591. StringRef TargetName,
  592. bool ExpandDefinition) {
  593. RecVec TIIPredicates = Records.getAllDerivedDefinitions("TIIPredicate");
  594. if (TIIPredicates.empty())
  595. return;
  596. PredicateExpander PE(TargetName);
  597. PE.setExpandForMC(false);
  598. for (const Record *Rec : TIIPredicates) {
  599. OS << (ExpandDefinition ? "" : "static ") << "bool ";
  600. if (ExpandDefinition)
  601. OS << TargetName << "InstrInfo::";
  602. OS << Rec->getValueAsString("FunctionName");
  603. OS << "(const MachineInstr &MI)";
  604. if (!ExpandDefinition) {
  605. OS << ";\n";
  606. continue;
  607. }
  608. OS << " {\n";
  609. OS.indent(PE.getIndentLevel() * 2);
  610. PE.expandStatement(OS, Rec->getValueAsDef("Body"));
  611. OS << "\n}\n\n";
  612. }
  613. }
  614. //===----------------------------------------------------------------------===//
  615. // Main Output.
  616. //===----------------------------------------------------------------------===//
  617. // run - Emit the main instruction description records for the target...
  618. void InstrInfoEmitter::run(raw_ostream &OS) {
  619. emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS);
  620. emitEnums(OS);
  621. OS << "#ifdef GET_INSTRINFO_MC_DESC\n";
  622. OS << "#undef GET_INSTRINFO_MC_DESC\n";
  623. OS << "namespace llvm {\n\n";
  624. CodeGenTarget &Target = CDP.getTargetInfo();
  625. const std::string &TargetName = std::string(Target.getName());
  626. Record *InstrInfo = Target.getInstructionSet();
  627. // Keep track of all of the def lists we have emitted already.
  628. std::map<std::vector<Record*>, unsigned> EmittedLists;
  629. unsigned ListNumber = 0;
  630. // Emit all of the instruction's implicit uses and defs.
  631. Records.startTimer("Emit uses/defs");
  632. for (const CodeGenInstruction *II : Target.getInstructionsByEnumValue()) {
  633. Record *Inst = II->TheDef;
  634. std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
  635. if (!Uses.empty()) {
  636. unsigned &IL = EmittedLists[Uses];
  637. if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
  638. }
  639. std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
  640. if (!Defs.empty()) {
  641. unsigned &IL = EmittedLists[Defs];
  642. if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
  643. }
  644. }
  645. OperandInfoMapTy OperandInfoIDs;
  646. // Emit all of the operand info records.
  647. Records.startTimer("Emit operand info");
  648. EmitOperandInfo(OS, OperandInfoIDs);
  649. // Emit all of the MCInstrDesc records in their ENUM ordering.
  650. //
  651. Records.startTimer("Emit InstrDesc records");
  652. OS << "\nextern const MCInstrDesc " << TargetName << "Insts[] = {\n";
  653. ArrayRef<const CodeGenInstruction*> NumberedInstructions =
  654. Target.getInstructionsByEnumValue();
  655. SequenceToOffsetTable<std::string> InstrNames;
  656. unsigned Num = 0;
  657. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  658. // Keep a list of the instruction names.
  659. InstrNames.add(std::string(Inst->TheDef->getName()));
  660. // Emit the record into the table.
  661. emitRecord(*Inst, Num++, InstrInfo, EmittedLists, OperandInfoIDs, OS);
  662. }
  663. OS << "};\n\n";
  664. // Emit the array of instruction names.
  665. Records.startTimer("Emit instruction names");
  666. InstrNames.layout();
  667. InstrNames.emitStringLiteralDef(OS, Twine("extern const char ") + TargetName +
  668. "InstrNameData[]");
  669. OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {";
  670. Num = 0;
  671. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  672. // Newline every eight entries.
  673. if (Num % 8 == 0)
  674. OS << "\n ";
  675. OS << InstrNames.get(std::string(Inst->TheDef->getName())) << "U, ";
  676. ++Num;
  677. }
  678. OS << "\n};\n\n";
  679. bool HasDeprecationFeatures =
  680. llvm::any_of(NumberedInstructions, [](const CodeGenInstruction *Inst) {
  681. return !Inst->HasComplexDeprecationPredicate &&
  682. !Inst->DeprecatedReason.empty();
  683. });
  684. if (HasDeprecationFeatures) {
  685. OS << "extern const uint8_t " << TargetName
  686. << "InstrDeprecationFeatures[] = {";
  687. Num = 0;
  688. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  689. if (Num % 8 == 0)
  690. OS << "\n ";
  691. if (!Inst->HasComplexDeprecationPredicate &&
  692. !Inst->DeprecatedReason.empty())
  693. OS << Target.getInstNamespace() << "::" << Inst->DeprecatedReason
  694. << ", ";
  695. else
  696. OS << "uint8_t(-1), ";
  697. ++Num;
  698. }
  699. OS << "\n};\n\n";
  700. }
  701. bool HasComplexDeprecationInfos =
  702. llvm::any_of(NumberedInstructions, [](const CodeGenInstruction *Inst) {
  703. return Inst->HasComplexDeprecationPredicate;
  704. });
  705. if (HasComplexDeprecationInfos) {
  706. OS << "extern const MCInstrInfo::ComplexDeprecationPredicate " << TargetName
  707. << "InstrComplexDeprecationInfos[] = {";
  708. Num = 0;
  709. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  710. if (Num % 8 == 0)
  711. OS << "\n ";
  712. if (Inst->HasComplexDeprecationPredicate)
  713. // Emit a function pointer to the complex predicate method.
  714. OS << "&get" << Inst->DeprecatedReason << "DeprecationInfo, ";
  715. else
  716. OS << "nullptr, ";
  717. ++Num;
  718. }
  719. OS << "\n};\n\n";
  720. }
  721. // MCInstrInfo initialization routine.
  722. Records.startTimer("Emit initialization routine");
  723. OS << "static inline void Init" << TargetName
  724. << "MCInstrInfo(MCInstrInfo *II) {\n";
  725. OS << " II->InitMCInstrInfo(" << TargetName << "Insts, " << TargetName
  726. << "InstrNameIndices, " << TargetName << "InstrNameData, ";
  727. if (HasDeprecationFeatures)
  728. OS << TargetName << "InstrDeprecationFeatures, ";
  729. else
  730. OS << "nullptr, ";
  731. if (HasComplexDeprecationInfos)
  732. OS << TargetName << "InstrComplexDeprecationInfos, ";
  733. else
  734. OS << "nullptr, ";
  735. OS << NumberedInstructions.size() << ");\n}\n\n";
  736. OS << "} // end namespace llvm\n";
  737. OS << "#endif // GET_INSTRINFO_MC_DESC\n\n";
  738. // Create a TargetInstrInfo subclass to hide the MC layer initialization.
  739. OS << "#ifdef GET_INSTRINFO_HEADER\n";
  740. OS << "#undef GET_INSTRINFO_HEADER\n";
  741. std::string ClassName = TargetName + "GenInstrInfo";
  742. OS << "namespace llvm {\n";
  743. OS << "struct " << ClassName << " : public TargetInstrInfo {\n"
  744. << " explicit " << ClassName
  745. << "(int CFSetupOpcode = -1, int CFDestroyOpcode = -1, int CatchRetOpcode = -1, int ReturnOpcode = -1);\n"
  746. << " ~" << ClassName << "() override = default;\n";
  747. OS << "\n};\n} // end namespace llvm\n";
  748. OS << "#endif // GET_INSTRINFO_HEADER\n\n";
  749. OS << "#ifdef GET_INSTRINFO_HELPER_DECLS\n";
  750. OS << "#undef GET_INSTRINFO_HELPER_DECLS\n\n";
  751. emitTIIHelperMethods(OS, TargetName, /* ExpandDefinition = */ false);
  752. OS << "\n";
  753. OS << "#endif // GET_INSTRINFO_HELPER_DECLS\n\n";
  754. OS << "#ifdef GET_INSTRINFO_HELPERS\n";
  755. OS << "#undef GET_INSTRINFO_HELPERS\n\n";
  756. emitTIIHelperMethods(OS, TargetName, /* ExpandDefinition = */ true);
  757. OS << "#endif // GET_INSTRINFO_HELPERS\n\n";
  758. OS << "#ifdef GET_INSTRINFO_CTOR_DTOR\n";
  759. OS << "#undef GET_INSTRINFO_CTOR_DTOR\n";
  760. OS << "namespace llvm {\n";
  761. OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n";
  762. OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n";
  763. OS << "extern const char " << TargetName << "InstrNameData[];\n";
  764. if (HasDeprecationFeatures)
  765. OS << "extern const uint8_t " << TargetName
  766. << "InstrDeprecationFeatures[];\n";
  767. if (HasComplexDeprecationInfos)
  768. OS << "extern const MCInstrInfo::ComplexDeprecationPredicate " << TargetName
  769. << "InstrComplexDeprecationInfos[];\n";
  770. OS << ClassName << "::" << ClassName
  771. << "(int CFSetupOpcode, int CFDestroyOpcode, int CatchRetOpcode, int "
  772. "ReturnOpcode)\n"
  773. << " : TargetInstrInfo(CFSetupOpcode, CFDestroyOpcode, CatchRetOpcode, "
  774. "ReturnOpcode) {\n"
  775. << " InitMCInstrInfo(" << TargetName << "Insts, " << TargetName
  776. << "InstrNameIndices, " << TargetName << "InstrNameData, ";
  777. if (HasDeprecationFeatures)
  778. OS << TargetName << "InstrDeprecationFeatures, ";
  779. else
  780. OS << "nullptr, ";
  781. if (HasComplexDeprecationInfos)
  782. OS << TargetName << "InstrComplexDeprecationInfos, ";
  783. else
  784. OS << "nullptr, ";
  785. OS << NumberedInstructions.size() << ");\n}\n";
  786. OS << "} // end namespace llvm\n";
  787. OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n";
  788. Records.startTimer("Emit operand name mappings");
  789. emitOperandNameMappings(OS, Target, NumberedInstructions);
  790. Records.startTimer("Emit operand type mappings");
  791. emitOperandTypeMappings(OS, Target, NumberedInstructions);
  792. Records.startTimer("Emit logical operand size mappings");
  793. emitLogicalOperandSizeMappings(OS, TargetName, NumberedInstructions);
  794. Records.startTimer("Emit logical operand type mappings");
  795. emitLogicalOperandTypeMappings(OS, TargetName, NumberedInstructions);
  796. Records.startTimer("Emit helper methods");
  797. emitMCIIHelperMethods(OS, TargetName);
  798. }
  799. void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
  800. Record *InstrInfo,
  801. std::map<std::vector<Record*>, unsigned> &EmittedLists,
  802. const OperandInfoMapTy &OpInfo,
  803. raw_ostream &OS) {
  804. int MinOperands = 0;
  805. if (!Inst.Operands.empty())
  806. // Each logical operand can be multiple MI operands.
  807. MinOperands = Inst.Operands.back().MIOperandNo +
  808. Inst.Operands.back().MINumOperands;
  809. OS << " { ";
  810. OS << Num << ",\t" << MinOperands << ",\t"
  811. << Inst.Operands.NumDefs << ",\t"
  812. << Inst.TheDef->getValueAsInt("Size") << ",\t"
  813. << SchedModels.getSchedClassIdx(Inst) << ",\t0";
  814. CodeGenTarget &Target = CDP.getTargetInfo();
  815. // Emit all of the target independent flags...
  816. if (Inst.isPreISelOpcode) OS << "|(1ULL<<MCID::PreISelOpcode)";
  817. if (Inst.isPseudo) OS << "|(1ULL<<MCID::Pseudo)";
  818. if (Inst.isReturn) OS << "|(1ULL<<MCID::Return)";
  819. if (Inst.isEHScopeReturn) OS << "|(1ULL<<MCID::EHScopeReturn)";
  820. if (Inst.isBranch) OS << "|(1ULL<<MCID::Branch)";
  821. if (Inst.isIndirectBranch) OS << "|(1ULL<<MCID::IndirectBranch)";
  822. if (Inst.isCompare) OS << "|(1ULL<<MCID::Compare)";
  823. if (Inst.isMoveImm) OS << "|(1ULL<<MCID::MoveImm)";
  824. if (Inst.isMoveReg) OS << "|(1ULL<<MCID::MoveReg)";
  825. if (Inst.isBitcast) OS << "|(1ULL<<MCID::Bitcast)";
  826. if (Inst.isAdd) OS << "|(1ULL<<MCID::Add)";
  827. if (Inst.isTrap) OS << "|(1ULL<<MCID::Trap)";
  828. if (Inst.isSelect) OS << "|(1ULL<<MCID::Select)";
  829. if (Inst.isBarrier) OS << "|(1ULL<<MCID::Barrier)";
  830. if (Inst.hasDelaySlot) OS << "|(1ULL<<MCID::DelaySlot)";
  831. if (Inst.isCall) OS << "|(1ULL<<MCID::Call)";
  832. if (Inst.canFoldAsLoad) OS << "|(1ULL<<MCID::FoldableAsLoad)";
  833. if (Inst.mayLoad) OS << "|(1ULL<<MCID::MayLoad)";
  834. if (Inst.mayStore) OS << "|(1ULL<<MCID::MayStore)";
  835. if (Inst.mayRaiseFPException) OS << "|(1ULL<<MCID::MayRaiseFPException)";
  836. if (Inst.isPredicable) OS << "|(1ULL<<MCID::Predicable)";
  837. if (Inst.isConvertibleToThreeAddress) OS << "|(1ULL<<MCID::ConvertibleTo3Addr)";
  838. if (Inst.isCommutable) OS << "|(1ULL<<MCID::Commutable)";
  839. if (Inst.isTerminator) OS << "|(1ULL<<MCID::Terminator)";
  840. if (Inst.isReMaterializable) OS << "|(1ULL<<MCID::Rematerializable)";
  841. if (Inst.isNotDuplicable) OS << "|(1ULL<<MCID::NotDuplicable)";
  842. if (Inst.Operands.hasOptionalDef) OS << "|(1ULL<<MCID::HasOptionalDef)";
  843. if (Inst.usesCustomInserter) OS << "|(1ULL<<MCID::UsesCustomInserter)";
  844. if (Inst.hasPostISelHook) OS << "|(1ULL<<MCID::HasPostISelHook)";
  845. if (Inst.Operands.isVariadic)OS << "|(1ULL<<MCID::Variadic)";
  846. if (Inst.hasSideEffects) OS << "|(1ULL<<MCID::UnmodeledSideEffects)";
  847. if (Inst.isAsCheapAsAMove) OS << "|(1ULL<<MCID::CheapAsAMove)";
  848. if (!Target.getAllowRegisterRenaming() || Inst.hasExtraSrcRegAllocReq)
  849. OS << "|(1ULL<<MCID::ExtraSrcRegAllocReq)";
  850. if (!Target.getAllowRegisterRenaming() || Inst.hasExtraDefRegAllocReq)
  851. OS << "|(1ULL<<MCID::ExtraDefRegAllocReq)";
  852. if (Inst.isRegSequence) OS << "|(1ULL<<MCID::RegSequence)";
  853. if (Inst.isExtractSubreg) OS << "|(1ULL<<MCID::ExtractSubreg)";
  854. if (Inst.isInsertSubreg) OS << "|(1ULL<<MCID::InsertSubreg)";
  855. if (Inst.isConvergent) OS << "|(1ULL<<MCID::Convergent)";
  856. if (Inst.variadicOpsAreDefs) OS << "|(1ULL<<MCID::VariadicOpsAreDefs)";
  857. if (Inst.isAuthenticated) OS << "|(1ULL<<MCID::Authenticated)";
  858. // Emit all of the target-specific flags...
  859. BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
  860. if (!TSF)
  861. PrintFatalError(Inst.TheDef->getLoc(), "no TSFlags?");
  862. uint64_t Value = 0;
  863. for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
  864. if (const auto *Bit = dyn_cast<BitInit>(TSF->getBit(i)))
  865. Value |= uint64_t(Bit->getValue()) << i;
  866. else
  867. PrintFatalError(Inst.TheDef->getLoc(),
  868. "Invalid TSFlags bit in " + Inst.TheDef->getName());
  869. }
  870. OS << ", 0x";
  871. OS.write_hex(Value);
  872. OS << "ULL, ";
  873. // Emit the implicit uses and defs lists...
  874. std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
  875. if (UseList.empty())
  876. OS << "nullptr, ";
  877. else
  878. OS << "ImplicitList" << EmittedLists[UseList] << ", ";
  879. std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
  880. if (DefList.empty())
  881. OS << "nullptr, ";
  882. else
  883. OS << "ImplicitList" << EmittedLists[DefList] << ", ";
  884. // Emit the operand info.
  885. std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
  886. if (OperandInfo.empty())
  887. OS << "nullptr";
  888. else
  889. OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
  890. OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
  891. }
  892. // emitEnums - Print out enum values for all of the instructions.
  893. void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
  894. OS << "#ifdef GET_INSTRINFO_ENUM\n";
  895. OS << "#undef GET_INSTRINFO_ENUM\n";
  896. OS << "namespace llvm {\n\n";
  897. const CodeGenTarget &Target = CDP.getTargetInfo();
  898. // We must emit the PHI opcode first...
  899. StringRef Namespace = Target.getInstNamespace();
  900. if (Namespace.empty())
  901. PrintFatalError("No instructions defined!");
  902. OS << "namespace " << Namespace << " {\n";
  903. OS << " enum {\n";
  904. unsigned Num = 0;
  905. for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue())
  906. OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
  907. OS << " INSTRUCTION_LIST_END = " << Num << "\n";
  908. OS << " };\n\n";
  909. OS << "} // end namespace " << Namespace << "\n";
  910. OS << "} // end namespace llvm\n";
  911. OS << "#endif // GET_INSTRINFO_ENUM\n\n";
  912. OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
  913. OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
  914. OS << "namespace llvm {\n\n";
  915. OS << "namespace " << Namespace << " {\n";
  916. OS << "namespace Sched {\n";
  917. OS << " enum {\n";
  918. Num = 0;
  919. for (const auto &Class : SchedModels.explicit_classes())
  920. OS << " " << Class.Name << "\t= " << Num++ << ",\n";
  921. OS << " SCHED_LIST_END = " << Num << "\n";
  922. OS << " };\n";
  923. OS << "} // end namespace Sched\n";
  924. OS << "} // end namespace " << Namespace << "\n";
  925. OS << "} // end namespace llvm\n";
  926. OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n";
  927. }
  928. namespace llvm {
  929. void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
  930. RK.startTimer("Analyze DAG patterns");
  931. InstrInfoEmitter(RK).run(OS);
  932. RK.startTimer("Emit map table");
  933. EmitMapTable(RK, OS);
  934. }
  935. } // end namespace llvm