InstrInfoEmitter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This tablegen backend is responsible for emitting a description of the target
  11. // instruction set for the code generator.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "CodeGenDAGPatterns.h"
  15. #include "CodeGenSchedule.h"
  16. #include "CodeGenTarget.h"
  17. #include "SequenceToOffsetTable.h"
  18. #include "TableGenBackends.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. #include "llvm/TableGen/Error.h"
  21. #include "llvm/TableGen/Record.h"
  22. #include "llvm/TableGen/TableGenBackend.h"
  23. #include <algorithm>
  24. #include <cstdio>
  25. #include <map>
  26. #include <vector>
  27. using namespace llvm;
  28. namespace {
  29. class InstrInfoEmitter {
  30. RecordKeeper &Records;
  31. CodeGenDAGPatterns CDP;
  32. const CodeGenSchedModels &SchedModels;
  33. public:
  34. InstrInfoEmitter(RecordKeeper &R):
  35. Records(R), CDP(R), SchedModels(CDP.getTargetInfo().getSchedModels()) {}
  36. // run - Output the instruction set description.
  37. void run(raw_ostream &OS);
  38. private:
  39. void emitEnums(raw_ostream &OS);
  40. typedef std::map<std::vector<std::string>, unsigned> OperandInfoMapTy;
  41. /// The keys of this map are maps which have OpName enum values as their keys
  42. /// and instruction operand indices as their values. The values of this map
  43. /// are lists of instruction names.
  44. typedef std::map<std::map<unsigned, unsigned>,
  45. std::vector<std::string> > OpNameMapTy;
  46. typedef std::map<std::string, unsigned>::iterator StrUintMapIter;
  47. void emitRecord(const CodeGenInstruction &Inst, unsigned Num,
  48. Record *InstrInfo,
  49. std::map<std::vector<Record*>, unsigned> &EL,
  50. const OperandInfoMapTy &OpInfo,
  51. raw_ostream &OS);
  52. void emitOperandTypesEnum(raw_ostream &OS, const CodeGenTarget &Target);
  53. void initOperandMapData(
  54. const std::vector<const CodeGenInstruction *> &NumberedInstructions,
  55. const std::string &Namespace,
  56. std::map<std::string, unsigned> &Operands,
  57. OpNameMapTy &OperandMap);
  58. void emitOperandNameMappings(raw_ostream &OS, const CodeGenTarget &Target,
  59. const std::vector<const CodeGenInstruction*> &NumberedInstructions);
  60. // Operand information.
  61. void EmitOperandInfo(raw_ostream &OS, OperandInfoMapTy &OperandInfoIDs);
  62. std::vector<std::string> GetOperandInfo(const CodeGenInstruction &Inst);
  63. };
  64. } // End anonymous namespace
  65. static void PrintDefList(const std::vector<Record*> &Uses,
  66. unsigned Num, raw_ostream &OS) {
  67. OS << "static const uint16_t ImplicitList" << Num << "[] = { ";
  68. for (unsigned i = 0, e = Uses.size(); i != e; ++i)
  69. OS << getQualifiedName(Uses[i]) << ", ";
  70. OS << "0 };\n";
  71. }
  72. //===----------------------------------------------------------------------===//
  73. // Operand Info Emission.
  74. //===----------------------------------------------------------------------===//
  75. std::vector<std::string>
  76. InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
  77. std::vector<std::string> Result;
  78. for (auto &Op : Inst.Operands) {
  79. // Handle aggregate operands and normal operands the same way by expanding
  80. // either case into a list of operands for this op.
  81. std::vector<CGIOperandList::OperandInfo> OperandList;
  82. // This might be a multiple operand thing. Targets like X86 have
  83. // registers in their multi-operand operands. It may also be an anonymous
  84. // operand, which has a single operand, but no declared class for the
  85. // operand.
  86. DagInit *MIOI = Op.MIOperandInfo;
  87. if (!MIOI || MIOI->getNumArgs() == 0) {
  88. // Single, anonymous, operand.
  89. OperandList.push_back(Op);
  90. } else {
  91. for (unsigned j = 0, e = Op.MINumOperands; j != e; ++j) {
  92. OperandList.push_back(Op);
  93. Record *OpR = cast<DefInit>(MIOI->getArg(j))->getDef();
  94. OperandList.back().Rec = OpR;
  95. }
  96. }
  97. for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
  98. Record *OpR = OperandList[j].Rec;
  99. std::string Res;
  100. if (OpR->isSubClassOf("RegisterOperand"))
  101. OpR = OpR->getValueAsDef("RegClass");
  102. if (OpR->isSubClassOf("RegisterClass"))
  103. Res += getQualifiedName(OpR) + "RegClassID, ";
  104. else if (OpR->isSubClassOf("PointerLikeRegClass"))
  105. Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", ";
  106. else
  107. // -1 means the operand does not have a fixed register class.
  108. Res += "-1, ";
  109. // Fill in applicable flags.
  110. Res += "0";
  111. // Ptr value whose register class is resolved via callback.
  112. if (OpR->isSubClassOf("PointerLikeRegClass"))
  113. Res += "|(1<<MCOI::LookupPtrRegClass)";
  114. // Predicate operands. Check to see if the original unexpanded operand
  115. // was of type PredicateOp.
  116. if (Op.Rec->isSubClassOf("PredicateOp"))
  117. Res += "|(1<<MCOI::Predicate)";
  118. // Optional def operands. Check to see if the original unexpanded operand
  119. // was of type OptionalDefOperand.
  120. if (Op.Rec->isSubClassOf("OptionalDefOperand"))
  121. Res += "|(1<<MCOI::OptionalDef)";
  122. // Fill in operand type.
  123. Res += ", ";
  124. assert(!Op.OperandType.empty() && "Invalid operand type.");
  125. Res += Op.OperandType;
  126. // Fill in constraint info.
  127. Res += ", ";
  128. const CGIOperandList::ConstraintInfo &Constraint =
  129. Op.Constraints[j];
  130. if (Constraint.isNone())
  131. Res += "0";
  132. else if (Constraint.isEarlyClobber())
  133. Res += "(1 << MCOI::EARLY_CLOBBER)";
  134. else {
  135. assert(Constraint.isTied());
  136. Res += "((" + utostr(Constraint.getTiedOperand()) +
  137. " << 16) | (1 << MCOI::TIED_TO))";
  138. }
  139. Result.push_back(Res);
  140. }
  141. }
  142. return Result;
  143. }
  144. void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
  145. OperandInfoMapTy &OperandInfoIDs) {
  146. // ID #0 is for no operand info.
  147. unsigned OperandListNum = 0;
  148. OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
  149. OS << "\n";
  150. const CodeGenTarget &Target = CDP.getTargetInfo();
  151. for (const CodeGenInstruction *Inst : Target.instructions()) {
  152. std::vector<std::string> OperandInfo = GetOperandInfo(*Inst);
  153. unsigned &N = OperandInfoIDs[OperandInfo];
  154. if (N != 0) continue;
  155. N = ++OperandListNum;
  156. OS << "static const MCOperandInfo OperandInfo" << N << "[] = { ";
  157. for (const std::string &Info : OperandInfo)
  158. OS << "{ " << Info << " }, ";
  159. OS << "};\n";
  160. }
  161. }
  162. /// Initialize data structures for generating operand name mappings.
  163. ///
  164. /// \param Operands [out] A map used to generate the OpName enum with operand
  165. /// names as its keys and operand enum values as its values.
  166. /// \param OperandMap [out] A map for representing the operand name mappings for
  167. /// each instructions. This is used to generate the OperandMap table as
  168. /// well as the getNamedOperandIdx() function.
  169. void InstrInfoEmitter::initOperandMapData(
  170. const std::vector<const CodeGenInstruction *> &NumberedInstructions,
  171. const std::string &Namespace,
  172. std::map<std::string, unsigned> &Operands,
  173. OpNameMapTy &OperandMap) {
  174. unsigned NumOperands = 0;
  175. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  176. if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable"))
  177. continue;
  178. std::map<unsigned, unsigned> OpList;
  179. for (const auto &Info : Inst->Operands) {
  180. StrUintMapIter I = Operands.find(Info.Name);
  181. if (I == Operands.end()) {
  182. I = Operands.insert(Operands.begin(),
  183. std::pair<std::string, unsigned>(Info.Name, NumOperands++));
  184. }
  185. OpList[I->second] = Info.MIOperandNo;
  186. }
  187. OperandMap[OpList].push_back(Namespace + "::" + Inst->TheDef->getName());
  188. }
  189. }
  190. /// Generate a table and function for looking up the indices of operands by
  191. /// name.
  192. ///
  193. /// This code generates:
  194. /// - An enum in the llvm::TargetNamespace::OpName namespace, with one entry
  195. /// for each operand name.
  196. /// - A 2-dimensional table called OperandMap for mapping OpName enum values to
  197. /// operand indices.
  198. /// - A function called getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
  199. /// for looking up the operand index for an instruction, given a value from
  200. /// OpName enum
  201. void InstrInfoEmitter::emitOperandNameMappings(raw_ostream &OS,
  202. const CodeGenTarget &Target,
  203. const std::vector<const CodeGenInstruction*> &NumberedInstructions) {
  204. const std::string &Namespace = Target.getInstNamespace();
  205. std::string OpNameNS = "OpName";
  206. // Map of operand names to their enumeration value. This will be used to
  207. // generate the OpName enum.
  208. std::map<std::string, unsigned> Operands;
  209. OpNameMapTy OperandMap;
  210. initOperandMapData(NumberedInstructions, Namespace, Operands, OperandMap);
  211. OS << "#ifdef GET_INSTRINFO_OPERAND_ENUM\n";
  212. OS << "#undef GET_INSTRINFO_OPERAND_ENUM\n";
  213. OS << "namespace llvm {\n";
  214. OS << "namespace " << Namespace << " {\n";
  215. OS << "namespace " << OpNameNS << " { \n";
  216. OS << "enum {\n";
  217. for (const auto &Op : Operands)
  218. OS << " " << Op.first << " = " << Op.second << ",\n";
  219. OS << "OPERAND_LAST";
  220. OS << "\n};\n";
  221. OS << "} // End namespace OpName\n";
  222. OS << "} // End namespace " << Namespace << "\n";
  223. OS << "} // End namespace llvm\n";
  224. OS << "#endif //GET_INSTRINFO_OPERAND_ENUM\n";
  225. OS << "#ifdef GET_INSTRINFO_NAMED_OPS\n";
  226. OS << "#undef GET_INSTRINFO_NAMED_OPS\n";
  227. OS << "namespace llvm {\n";
  228. OS << "namespace " << Namespace << " {\n";
  229. OS << "LLVM_READONLY\n";
  230. OS << "int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) {\n";
  231. if (!Operands.empty()) {
  232. OS << " static const int16_t OperandMap [][" << Operands.size()
  233. << "] = {\n";
  234. for (const auto &Entry : OperandMap) {
  235. const std::map<unsigned, unsigned> &OpList = Entry.first;
  236. OS << "{";
  237. // Emit a row of the OperandMap table
  238. for (unsigned i = 0, e = Operands.size(); i != e; ++i)
  239. OS << (OpList.count(i) == 0 ? -1 : (int)OpList.find(i)->second) << ", ";
  240. OS << "},\n";
  241. }
  242. OS << "};\n";
  243. OS << " switch(Opcode) {\n";
  244. unsigned TableIndex = 0;
  245. for (const auto &Entry : OperandMap) {
  246. for (const std::string &Name : Entry.second)
  247. OS << " case " << Name << ":\n";
  248. OS << " return OperandMap[" << TableIndex++ << "][NamedIdx];\n";
  249. }
  250. OS << " default: return -1;\n";
  251. OS << " }\n";
  252. } else {
  253. // There are no operands, so no need to emit anything
  254. OS << " return -1;\n";
  255. }
  256. OS << "}\n";
  257. OS << "} // End namespace " << Namespace << "\n";
  258. OS << "} // End namespace llvm\n";
  259. OS << "#endif //GET_INSTRINFO_NAMED_OPS\n";
  260. }
  261. /// Generate an enum for all the operand types for this target, under the
  262. /// llvm::TargetNamespace::OpTypes namespace.
  263. /// Operand types are all definitions derived of the Operand Target.td class.
  264. void InstrInfoEmitter::emitOperandTypesEnum(raw_ostream &OS,
  265. const CodeGenTarget &Target) {
  266. const std::string &Namespace = Target.getInstNamespace();
  267. std::vector<Record *> Operands = Records.getAllDerivedDefinitions("Operand");
  268. OS << "\n#ifdef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
  269. OS << "#undef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
  270. OS << "namespace llvm {\n";
  271. OS << "namespace " << Namespace << " {\n";
  272. OS << "namespace OpTypes { \n";
  273. OS << "enum OperandType {\n";
  274. unsigned EnumVal = 0;
  275. for (const Record *Op : Operands) {
  276. if (!Op->isAnonymous())
  277. OS << " " << Op->getName() << " = " << EnumVal << ",\n";
  278. ++EnumVal;
  279. }
  280. OS << " OPERAND_TYPE_LIST_END" << "\n};\n";
  281. OS << "} // End namespace OpTypes\n";
  282. OS << "} // End namespace " << Namespace << "\n";
  283. OS << "} // End namespace llvm\n";
  284. OS << "#endif // GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
  285. }
  286. //===----------------------------------------------------------------------===//
  287. // Main Output.
  288. //===----------------------------------------------------------------------===//
  289. // run - Emit the main instruction description records for the target...
  290. void InstrInfoEmitter::run(raw_ostream &OS) {
  291. emitSourceFileHeader("Target Instruction Enum Values", OS);
  292. emitEnums(OS);
  293. emitSourceFileHeader("Target Instruction Descriptors", OS);
  294. OS << "\n#ifdef GET_INSTRINFO_MC_DESC\n";
  295. OS << "#undef GET_INSTRINFO_MC_DESC\n";
  296. OS << "namespace llvm {\n\n";
  297. CodeGenTarget &Target = CDP.getTargetInfo();
  298. const std::string &TargetName = Target.getName();
  299. Record *InstrInfo = Target.getInstructionSet();
  300. // Keep track of all of the def lists we have emitted already.
  301. std::map<std::vector<Record*>, unsigned> EmittedLists;
  302. unsigned ListNumber = 0;
  303. // Emit all of the instruction's implicit uses and defs.
  304. for (const CodeGenInstruction *II : Target.instructions()) {
  305. Record *Inst = II->TheDef;
  306. std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
  307. if (!Uses.empty()) {
  308. unsigned &IL = EmittedLists[Uses];
  309. if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
  310. }
  311. std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
  312. if (!Defs.empty()) {
  313. unsigned &IL = EmittedLists[Defs];
  314. if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
  315. }
  316. }
  317. OperandInfoMapTy OperandInfoIDs;
  318. // Emit all of the operand info records.
  319. EmitOperandInfo(OS, OperandInfoIDs);
  320. // Emit all of the MCInstrDesc records in their ENUM ordering.
  321. //
  322. OS << "\nextern const MCInstrDesc " << TargetName << "Insts[] = {\n";
  323. const std::vector<const CodeGenInstruction*> &NumberedInstructions =
  324. Target.getInstructionsByEnumValue();
  325. SequenceToOffsetTable<std::string> InstrNames;
  326. unsigned Num = 0;
  327. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  328. // Keep a list of the instruction names.
  329. InstrNames.add(Inst->TheDef->getName());
  330. // Emit the record into the table.
  331. emitRecord(*Inst, Num++, InstrInfo, EmittedLists, OperandInfoIDs, OS);
  332. }
  333. OS << "};\n\n";
  334. // Emit the array of instruction names.
  335. InstrNames.layout();
  336. OS << "extern const char " << TargetName << "InstrNameData[] = {\n";
  337. InstrNames.emit(OS, printChar);
  338. OS << "};\n\n";
  339. OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {";
  340. Num = 0;
  341. for (const CodeGenInstruction *Inst : NumberedInstructions) {
  342. // Newline every eight entries.
  343. if (Num % 8 == 0)
  344. OS << "\n ";
  345. OS << InstrNames.get(Inst->TheDef->getName()) << "U, ";
  346. ++Num;
  347. }
  348. OS << "\n};\n\n";
  349. // MCInstrInfo initialization routine.
  350. OS << "static inline void Init" << TargetName
  351. << "MCInstrInfo(MCInstrInfo *II) {\n";
  352. OS << " II->InitMCInstrInfo(" << TargetName << "Insts, "
  353. << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, "
  354. << NumberedInstructions.size() << ");\n}\n\n";
  355. OS << "} // End llvm namespace \n";
  356. OS << "#endif // GET_INSTRINFO_MC_DESC\n\n";
  357. // Create a TargetInstrInfo subclass to hide the MC layer initialization.
  358. OS << "\n#ifdef GET_INSTRINFO_HEADER\n";
  359. OS << "#undef GET_INSTRINFO_HEADER\n";
  360. std::string ClassName = TargetName + "GenInstrInfo";
  361. OS << "namespace llvm {\n";
  362. OS << "struct " << ClassName << " : public TargetInstrInfo {\n"
  363. << " explicit " << ClassName
  364. << "(int CFSetupOpcode = -1, int CFDestroyOpcode = -1);\n"
  365. << " virtual ~" << ClassName << "();\n"
  366. << "};\n";
  367. OS << "} // End llvm namespace \n";
  368. OS << "#endif // GET_INSTRINFO_HEADER\n\n";
  369. OS << "\n#ifdef GET_INSTRINFO_CTOR_DTOR\n";
  370. OS << "#undef GET_INSTRINFO_CTOR_DTOR\n";
  371. OS << "namespace llvm {\n";
  372. OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n";
  373. OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n";
  374. OS << "extern const char " << TargetName << "InstrNameData[];\n";
  375. OS << ClassName << "::" << ClassName
  376. << "(int CFSetupOpcode, int CFDestroyOpcode)\n"
  377. << " : TargetInstrInfo(CFSetupOpcode, CFDestroyOpcode) {\n"
  378. << " InitMCInstrInfo(" << TargetName << "Insts, " << TargetName
  379. << "InstrNameIndices, " << TargetName << "InstrNameData, "
  380. << NumberedInstructions.size() << ");\n}\n"
  381. << ClassName << "::~" << ClassName << "() {}\n";
  382. OS << "} // End llvm namespace \n";
  383. OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n";
  384. emitOperandNameMappings(OS, Target, NumberedInstructions);
  385. emitOperandTypesEnum(OS, Target);
  386. }
  387. void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
  388. Record *InstrInfo,
  389. std::map<std::vector<Record*>, unsigned> &EmittedLists,
  390. const OperandInfoMapTy &OpInfo,
  391. raw_ostream &OS) {
  392. int MinOperands = 0;
  393. if (!Inst.Operands.empty())
  394. // Each logical operand can be multiple MI operands.
  395. MinOperands = Inst.Operands.back().MIOperandNo +
  396. Inst.Operands.back().MINumOperands;
  397. OS << " { ";
  398. OS << Num << ",\t" << MinOperands << ",\t"
  399. << Inst.Operands.NumDefs << ",\t"
  400. << Inst.TheDef->getValueAsInt("Size") << ",\t"
  401. << SchedModels.getSchedClassIdx(Inst) << ",\t0";
  402. // Emit all of the target independent flags...
  403. if (Inst.isPseudo) OS << "|(1ULL<<MCID::Pseudo)";
  404. if (Inst.isReturn) OS << "|(1ULL<<MCID::Return)";
  405. if (Inst.isBranch) OS << "|(1ULL<<MCID::Branch)";
  406. if (Inst.isIndirectBranch) OS << "|(1ULL<<MCID::IndirectBranch)";
  407. if (Inst.isCompare) OS << "|(1ULL<<MCID::Compare)";
  408. if (Inst.isMoveImm) OS << "|(1ULL<<MCID::MoveImm)";
  409. if (Inst.isBitcast) OS << "|(1ULL<<MCID::Bitcast)";
  410. if (Inst.isSelect) OS << "|(1ULL<<MCID::Select)";
  411. if (Inst.isBarrier) OS << "|(1ULL<<MCID::Barrier)";
  412. if (Inst.hasDelaySlot) OS << "|(1ULL<<MCID::DelaySlot)";
  413. if (Inst.isCall) OS << "|(1ULL<<MCID::Call)";
  414. if (Inst.canFoldAsLoad) OS << "|(1ULL<<MCID::FoldableAsLoad)";
  415. if (Inst.mayLoad) OS << "|(1ULL<<MCID::MayLoad)";
  416. if (Inst.mayStore) OS << "|(1ULL<<MCID::MayStore)";
  417. if (Inst.isPredicable) OS << "|(1ULL<<MCID::Predicable)";
  418. if (Inst.isConvertibleToThreeAddress) OS << "|(1ULL<<MCID::ConvertibleTo3Addr)";
  419. if (Inst.isCommutable) OS << "|(1ULL<<MCID::Commutable)";
  420. if (Inst.isTerminator) OS << "|(1ULL<<MCID::Terminator)";
  421. if (Inst.isReMaterializable) OS << "|(1ULL<<MCID::Rematerializable)";
  422. if (Inst.isNotDuplicable) OS << "|(1ULL<<MCID::NotDuplicable)";
  423. if (Inst.Operands.hasOptionalDef) OS << "|(1ULL<<MCID::HasOptionalDef)";
  424. if (Inst.usesCustomInserter) OS << "|(1ULL<<MCID::UsesCustomInserter)";
  425. if (Inst.hasPostISelHook) OS << "|(1ULL<<MCID::HasPostISelHook)";
  426. if (Inst.Operands.isVariadic)OS << "|(1ULL<<MCID::Variadic)";
  427. if (Inst.hasSideEffects) OS << "|(1ULL<<MCID::UnmodeledSideEffects)";
  428. if (Inst.isAsCheapAsAMove) OS << "|(1ULL<<MCID::CheapAsAMove)";
  429. if (Inst.hasExtraSrcRegAllocReq) OS << "|(1ULL<<MCID::ExtraSrcRegAllocReq)";
  430. if (Inst.hasExtraDefRegAllocReq) OS << "|(1ULL<<MCID::ExtraDefRegAllocReq)";
  431. if (Inst.isRegSequence) OS << "|(1ULL<<MCID::RegSequence)";
  432. if (Inst.isExtractSubreg) OS << "|(1ULL<<MCID::ExtractSubreg)";
  433. if (Inst.isInsertSubreg) OS << "|(1ULL<<MCID::InsertSubreg)";
  434. if (Inst.isConvergent) OS << "|(1ULL<<MCID::Convergent)";
  435. // Emit all of the target-specific flags...
  436. BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
  437. if (!TSF)
  438. PrintFatalError("no TSFlags?");
  439. uint64_t Value = 0;
  440. for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
  441. if (BitInit *Bit = dyn_cast<BitInit>(TSF->getBit(i)))
  442. Value |= uint64_t(Bit->getValue()) << i;
  443. else
  444. PrintFatalError("Invalid TSFlags bit in " + Inst.TheDef->getName());
  445. }
  446. OS << ", 0x";
  447. OS.write_hex(Value);
  448. OS << "ULL, ";
  449. // Emit the implicit uses and defs lists...
  450. std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
  451. if (UseList.empty())
  452. OS << "nullptr, ";
  453. else
  454. OS << "ImplicitList" << EmittedLists[UseList] << ", ";
  455. std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
  456. if (DefList.empty())
  457. OS << "nullptr, ";
  458. else
  459. OS << "ImplicitList" << EmittedLists[DefList] << ", ";
  460. // Emit the operand info.
  461. std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
  462. if (OperandInfo.empty())
  463. OS << "nullptr";
  464. else
  465. OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
  466. CodeGenTarget &Target = CDP.getTargetInfo();
  467. if (Inst.HasComplexDeprecationPredicate)
  468. // Emit a function pointer to the complex predicate method.
  469. OS << ", -1 "
  470. << ",&get" << Inst.DeprecatedReason << "DeprecationInfo";
  471. else if (!Inst.DeprecatedReason.empty())
  472. // Emit the Subtarget feature.
  473. OS << ", " << Target.getInstNamespace() << "::" << Inst.DeprecatedReason
  474. << " ,nullptr";
  475. else
  476. // Instruction isn't deprecated.
  477. OS << ", -1 ,nullptr";
  478. OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
  479. }
  480. // emitEnums - Print out enum values for all of the instructions.
  481. void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
  482. OS << "\n#ifdef GET_INSTRINFO_ENUM\n";
  483. OS << "#undef GET_INSTRINFO_ENUM\n";
  484. OS << "namespace llvm {\n\n";
  485. CodeGenTarget Target(Records);
  486. // We must emit the PHI opcode first...
  487. std::string Namespace = Target.getInstNamespace();
  488. if (Namespace.empty())
  489. PrintFatalError("No instructions defined!");
  490. const std::vector<const CodeGenInstruction*> &NumberedInstructions =
  491. Target.getInstructionsByEnumValue();
  492. OS << "namespace " << Namespace << " {\n";
  493. OS << " enum {\n";
  494. unsigned Num = 0;
  495. for (const CodeGenInstruction *Inst : NumberedInstructions)
  496. OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
  497. OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << "\n";
  498. OS << " };\n\n";
  499. OS << "namespace Sched {\n";
  500. OS << " enum {\n";
  501. Num = 0;
  502. for (const auto &Class : SchedModels.explicit_classes())
  503. OS << " " << Class.Name << "\t= " << Num++ << ",\n";
  504. OS << " SCHED_LIST_END = " << SchedModels.numInstrSchedClasses() << "\n";
  505. OS << " };\n";
  506. OS << "} // End Sched namespace\n";
  507. OS << "} // End " << Namespace << " namespace\n";
  508. OS << "} // End llvm namespace \n";
  509. OS << "#endif // GET_INSTRINFO_ENUM\n\n";
  510. }
  511. namespace llvm {
  512. void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
  513. InstrInfoEmitter(RK).run(OS);
  514. EmitMapTable(RK, OS);
  515. }
  516. } // End llvm namespace