InstrInfoEmitter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "InstrInfoEmitter.h"
  15. #include "CodeGenTarget.h"
  16. #include "StringToOffsetTable.h"
  17. #include "llvm/TableGen/Record.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include <algorithm>
  20. #include <cstdio>
  21. using namespace llvm;
  22. static void PrintDefList(const std::vector<Record*> &Uses,
  23. unsigned Num, raw_ostream &OS) {
  24. OS << "static const uint16_t ImplicitList" << Num << "[] = { ";
  25. for (unsigned i = 0, e = Uses.size(); i != e; ++i)
  26. OS << getQualifiedName(Uses[i]) << ", ";
  27. OS << "0 };\n";
  28. }
  29. //===----------------------------------------------------------------------===//
  30. // Instruction Itinerary Information.
  31. //===----------------------------------------------------------------------===//
  32. void InstrInfoEmitter::GatherItinClasses() {
  33. std::vector<Record*> DefList =
  34. Records.getAllDerivedDefinitions("InstrItinClass");
  35. std::sort(DefList.begin(), DefList.end(), LessRecord());
  36. for (unsigned i = 0, N = DefList.size(); i < N; i++)
  37. ItinClassMap[DefList[i]->getName()] = i;
  38. }
  39. unsigned InstrInfoEmitter::getItinClassNumber(const Record *InstRec) {
  40. return ItinClassMap[InstRec->getValueAsDef("Itinerary")->getName()];
  41. }
  42. //===----------------------------------------------------------------------===//
  43. // Operand Info Emission.
  44. //===----------------------------------------------------------------------===//
  45. std::vector<std::string>
  46. InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
  47. std::vector<std::string> Result;
  48. for (unsigned i = 0, e = Inst.Operands.size(); i != e; ++i) {
  49. // Handle aggregate operands and normal operands the same way by expanding
  50. // either case into a list of operands for this op.
  51. std::vector<CGIOperandList::OperandInfo> OperandList;
  52. // This might be a multiple operand thing. Targets like X86 have
  53. // registers in their multi-operand operands. It may also be an anonymous
  54. // operand, which has a single operand, but no declared class for the
  55. // operand.
  56. DagInit *MIOI = Inst.Operands[i].MIOperandInfo;
  57. if (!MIOI || MIOI->getNumArgs() == 0) {
  58. // Single, anonymous, operand.
  59. OperandList.push_back(Inst.Operands[i]);
  60. } else {
  61. for (unsigned j = 0, e = Inst.Operands[i].MINumOperands; j != e; ++j) {
  62. OperandList.push_back(Inst.Operands[i]);
  63. Record *OpR = dynamic_cast<DefInit*>(MIOI->getArg(j))->getDef();
  64. OperandList.back().Rec = OpR;
  65. }
  66. }
  67. for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
  68. Record *OpR = OperandList[j].Rec;
  69. std::string Res;
  70. if (OpR->isSubClassOf("RegisterOperand"))
  71. OpR = OpR->getValueAsDef("RegClass");
  72. if (OpR->isSubClassOf("RegisterClass"))
  73. Res += getQualifiedName(OpR) + "RegClassID, ";
  74. else if (OpR->isSubClassOf("PointerLikeRegClass"))
  75. Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", ";
  76. else
  77. // -1 means the operand does not have a fixed register class.
  78. Res += "-1, ";
  79. // Fill in applicable flags.
  80. Res += "0";
  81. // Ptr value whose register class is resolved via callback.
  82. if (OpR->isSubClassOf("PointerLikeRegClass"))
  83. Res += "|(1<<MCOI::LookupPtrRegClass)";
  84. // Predicate operands. Check to see if the original unexpanded operand
  85. // was of type PredicateOperand.
  86. if (Inst.Operands[i].Rec->isSubClassOf("PredicateOperand"))
  87. Res += "|(1<<MCOI::Predicate)";
  88. // Optional def operands. Check to see if the original unexpanded operand
  89. // was of type OptionalDefOperand.
  90. if (Inst.Operands[i].Rec->isSubClassOf("OptionalDefOperand"))
  91. Res += "|(1<<MCOI::OptionalDef)";
  92. // Fill in operand type.
  93. Res += ", MCOI::";
  94. assert(!Inst.Operands[i].OperandType.empty() && "Invalid operand type.");
  95. Res += Inst.Operands[i].OperandType;
  96. // Fill in constraint info.
  97. Res += ", ";
  98. const CGIOperandList::ConstraintInfo &Constraint =
  99. Inst.Operands[i].Constraints[j];
  100. if (Constraint.isNone())
  101. Res += "0";
  102. else if (Constraint.isEarlyClobber())
  103. Res += "(1 << MCOI::EARLY_CLOBBER)";
  104. else {
  105. assert(Constraint.isTied());
  106. Res += "((" + utostr(Constraint.getTiedOperand()) +
  107. " << 16) | (1 << MCOI::TIED_TO))";
  108. }
  109. Result.push_back(Res);
  110. }
  111. }
  112. return Result;
  113. }
  114. void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
  115. OperandInfoMapTy &OperandInfoIDs) {
  116. // ID #0 is for no operand info.
  117. unsigned OperandListNum = 0;
  118. OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
  119. OS << "\n";
  120. const CodeGenTarget &Target = CDP.getTargetInfo();
  121. for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
  122. E = Target.inst_end(); II != E; ++II) {
  123. std::vector<std::string> OperandInfo = GetOperandInfo(**II);
  124. unsigned &N = OperandInfoIDs[OperandInfo];
  125. if (N != 0) continue;
  126. N = ++OperandListNum;
  127. OS << "static const MCOperandInfo OperandInfo" << N << "[] = { ";
  128. for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i)
  129. OS << "{ " << OperandInfo[i] << " }, ";
  130. OS << "};\n";
  131. }
  132. }
  133. //===----------------------------------------------------------------------===//
  134. // Main Output.
  135. //===----------------------------------------------------------------------===//
  136. // run - Emit the main instruction description records for the target...
  137. void InstrInfoEmitter::run(raw_ostream &OS) {
  138. emitEnums(OS);
  139. GatherItinClasses();
  140. EmitSourceFileHeader("Target Instruction Descriptors", OS);
  141. OS << "\n#ifdef GET_INSTRINFO_MC_DESC\n";
  142. OS << "#undef GET_INSTRINFO_MC_DESC\n";
  143. OS << "namespace llvm {\n\n";
  144. CodeGenTarget &Target = CDP.getTargetInfo();
  145. const std::string &TargetName = Target.getName();
  146. Record *InstrInfo = Target.getInstructionSet();
  147. // Keep track of all of the def lists we have emitted already.
  148. std::map<std::vector<Record*>, unsigned> EmittedLists;
  149. unsigned ListNumber = 0;
  150. // Emit all of the instruction's implicit uses and defs.
  151. for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
  152. E = Target.inst_end(); II != E; ++II) {
  153. Record *Inst = (*II)->TheDef;
  154. std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
  155. if (!Uses.empty()) {
  156. unsigned &IL = EmittedLists[Uses];
  157. if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
  158. }
  159. std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
  160. if (!Defs.empty()) {
  161. unsigned &IL = EmittedLists[Defs];
  162. if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
  163. }
  164. }
  165. OperandInfoMapTy OperandInfoIDs;
  166. // Emit all of the operand info records.
  167. EmitOperandInfo(OS, OperandInfoIDs);
  168. // Emit all of the MCInstrDesc records in their ENUM ordering.
  169. //
  170. OS << "\nextern const MCInstrDesc " << TargetName << "Insts[] = {\n";
  171. const std::vector<const CodeGenInstruction*> &NumberedInstructions =
  172. Target.getInstructionsByEnumValue();
  173. for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
  174. emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
  175. OperandInfoIDs, OS);
  176. OS << "};\n\n";
  177. OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {\n ";
  178. StringToOffsetTable StringTable;
  179. for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
  180. const CodeGenInstruction *Instr = NumberedInstructions[i];
  181. OS << StringTable.GetOrAddStringOffset(Instr->TheDef->getName()) << "U, ";
  182. if (i % 8 == 0)
  183. OS << "\n ";
  184. }
  185. OS << "\n};\n\n";
  186. OS << "const char *" << TargetName << "InstrNameData =\n";
  187. StringTable.EmitString(OS);
  188. OS << ";\n\n";
  189. // MCInstrInfo initialization routine.
  190. OS << "static inline void Init" << TargetName
  191. << "MCInstrInfo(MCInstrInfo *II) {\n";
  192. OS << " II->InitMCInstrInfo(" << TargetName << "Insts, "
  193. << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, "
  194. << NumberedInstructions.size() << ");\n}\n\n";
  195. OS << "} // End llvm namespace \n";
  196. OS << "#endif // GET_INSTRINFO_MC_DESC\n\n";
  197. // Create a TargetInstrInfo subclass to hide the MC layer initialization.
  198. OS << "\n#ifdef GET_INSTRINFO_HEADER\n";
  199. OS << "#undef GET_INSTRINFO_HEADER\n";
  200. std::string ClassName = TargetName + "GenInstrInfo";
  201. OS << "namespace llvm {\n";
  202. OS << "struct " << ClassName << " : public TargetInstrInfoImpl {\n"
  203. << " explicit " << ClassName << "(int SO = -1, int DO = -1);\n"
  204. << "};\n";
  205. OS << "} // End llvm namespace \n";
  206. OS << "#endif // GET_INSTRINFO_HEADER\n\n";
  207. OS << "\n#ifdef GET_INSTRINFO_CTOR\n";
  208. OS << "#undef GET_INSTRINFO_CTOR\n";
  209. OS << "namespace llvm {\n";
  210. OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n";
  211. OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n";
  212. OS << "extern const char *" << TargetName << "InstrNameData;\n";
  213. OS << ClassName << "::" << ClassName << "(int SO, int DO)\n"
  214. << " : TargetInstrInfoImpl(SO, DO) {\n"
  215. << " InitMCInstrInfo(" << TargetName << "Insts, "
  216. << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, "
  217. << NumberedInstructions.size() << ");\n}\n";
  218. OS << "} // End llvm namespace \n";
  219. OS << "#endif // GET_INSTRINFO_CTOR\n\n";
  220. }
  221. void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
  222. Record *InstrInfo,
  223. std::map<std::vector<Record*>, unsigned> &EmittedLists,
  224. const OperandInfoMapTy &OpInfo,
  225. raw_ostream &OS) {
  226. int MinOperands = 0;
  227. if (!Inst.Operands.size() == 0)
  228. // Each logical operand can be multiple MI operands.
  229. MinOperands = Inst.Operands.back().MIOperandNo +
  230. Inst.Operands.back().MINumOperands;
  231. OS << " { ";
  232. OS << Num << ",\t" << MinOperands << ",\t"
  233. << Inst.Operands.NumDefs << ",\t"
  234. << getItinClassNumber(Inst.TheDef) << ",\t"
  235. << Inst.TheDef->getValueAsInt("Size") << ",\t0";
  236. // Emit all of the target indepedent flags...
  237. if (Inst.isPseudo) OS << "|(1<<MCID::Pseudo)";
  238. if (Inst.isReturn) OS << "|(1<<MCID::Return)";
  239. if (Inst.isBranch) OS << "|(1<<MCID::Branch)";
  240. if (Inst.isIndirectBranch) OS << "|(1<<MCID::IndirectBranch)";
  241. if (Inst.isCompare) OS << "|(1<<MCID::Compare)";
  242. if (Inst.isMoveImm) OS << "|(1<<MCID::MoveImm)";
  243. if (Inst.isBitcast) OS << "|(1<<MCID::Bitcast)";
  244. if (Inst.isBarrier) OS << "|(1<<MCID::Barrier)";
  245. if (Inst.hasDelaySlot) OS << "|(1<<MCID::DelaySlot)";
  246. if (Inst.isCall) OS << "|(1<<MCID::Call)";
  247. if (Inst.canFoldAsLoad) OS << "|(1<<MCID::FoldableAsLoad)";
  248. if (Inst.mayLoad) OS << "|(1<<MCID::MayLoad)";
  249. if (Inst.mayStore) OS << "|(1<<MCID::MayStore)";
  250. if (Inst.isPredicable) OS << "|(1<<MCID::Predicable)";
  251. if (Inst.isConvertibleToThreeAddress) OS << "|(1<<MCID::ConvertibleTo3Addr)";
  252. if (Inst.isCommutable) OS << "|(1<<MCID::Commutable)";
  253. if (Inst.isTerminator) OS << "|(1<<MCID::Terminator)";
  254. if (Inst.isReMaterializable) OS << "|(1<<MCID::Rematerializable)";
  255. if (Inst.isNotDuplicable) OS << "|(1<<MCID::NotDuplicable)";
  256. if (Inst.Operands.hasOptionalDef) OS << "|(1<<MCID::HasOptionalDef)";
  257. if (Inst.usesCustomInserter) OS << "|(1<<MCID::UsesCustomInserter)";
  258. if (Inst.hasPostISelHook) OS << "|(1<<MCID::HasPostISelHook)";
  259. if (Inst.Operands.isVariadic)OS << "|(1<<MCID::Variadic)";
  260. if (Inst.hasSideEffects) OS << "|(1<<MCID::UnmodeledSideEffects)";
  261. if (Inst.isAsCheapAsAMove) OS << "|(1<<MCID::CheapAsAMove)";
  262. if (Inst.hasExtraSrcRegAllocReq) OS << "|(1<<MCID::ExtraSrcRegAllocReq)";
  263. if (Inst.hasExtraDefRegAllocReq) OS << "|(1<<MCID::ExtraDefRegAllocReq)";
  264. // Emit all of the target-specific flags...
  265. BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
  266. if (!TSF) throw "no TSFlags?";
  267. uint64_t Value = 0;
  268. for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
  269. if (BitInit *Bit = dynamic_cast<BitInit*>(TSF->getBit(i)))
  270. Value |= uint64_t(Bit->getValue()) << i;
  271. else
  272. throw "Invalid TSFlags bit in " + Inst.TheDef->getName();
  273. }
  274. OS << ", 0x";
  275. OS.write_hex(Value);
  276. OS << "ULL, ";
  277. // Emit the implicit uses and defs lists...
  278. std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
  279. if (UseList.empty())
  280. OS << "NULL, ";
  281. else
  282. OS << "ImplicitList" << EmittedLists[UseList] << ", ";
  283. std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
  284. if (DefList.empty())
  285. OS << "NULL, ";
  286. else
  287. OS << "ImplicitList" << EmittedLists[DefList] << ", ";
  288. // Emit the operand info.
  289. std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
  290. if (OperandInfo.empty())
  291. OS << "0";
  292. else
  293. OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
  294. OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
  295. }
  296. // emitEnums - Print out enum values for all of the instructions.
  297. void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
  298. EmitSourceFileHeader("Target Instruction Enum Values", OS);
  299. OS << "\n#ifdef GET_INSTRINFO_ENUM\n";
  300. OS << "#undef GET_INSTRINFO_ENUM\n";
  301. OS << "namespace llvm {\n\n";
  302. CodeGenTarget Target(Records);
  303. // We must emit the PHI opcode first...
  304. std::string Namespace = Target.getInstNamespace();
  305. if (Namespace.empty()) {
  306. fprintf(stderr, "No instructions defined!\n");
  307. exit(1);
  308. }
  309. const std::vector<const CodeGenInstruction*> &NumberedInstructions =
  310. Target.getInstructionsByEnumValue();
  311. OS << "namespace " << Namespace << " {\n";
  312. OS << " enum {\n";
  313. for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
  314. OS << " " << NumberedInstructions[i]->TheDef->getName()
  315. << "\t= " << i << ",\n";
  316. }
  317. OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << "\n";
  318. OS << " };\n}\n";
  319. OS << "} // End llvm namespace \n";
  320. OS << "#endif // GET_INSTRINFO_ENUM\n\n";
  321. }