IntrinsicEmitter.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
  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 emits information about intrinsic functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CodeGenIntrinsics.h"
  13. #include "CodeGenTarget.h"
  14. #include "SequenceToOffsetTable.h"
  15. #include "TableGenBackends.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/TableGen/Error.h"
  19. #include "llvm/TableGen/Record.h"
  20. #include "llvm/TableGen/StringToOffsetTable.h"
  21. #include "llvm/TableGen/TableGenBackend.h"
  22. #include <algorithm>
  23. using namespace llvm;
  24. cl::OptionCategory GenIntrinsicCat("Options for -gen-intrinsic-enums");
  25. cl::opt<std::string>
  26. IntrinsicPrefix("intrinsic-prefix",
  27. cl::desc("Generate intrinsics with this target prefix"),
  28. cl::value_desc("target prefix"), cl::cat(GenIntrinsicCat));
  29. namespace {
  30. class IntrinsicEmitter {
  31. RecordKeeper &Records;
  32. public:
  33. IntrinsicEmitter(RecordKeeper &R) : Records(R) {}
  34. void run(raw_ostream &OS, bool Enums);
  35. void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
  36. void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
  37. void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
  38. raw_ostream &OS);
  39. void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
  40. raw_ostream &OS);
  41. void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
  42. void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
  43. void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsGCC,
  44. raw_ostream &OS);
  45. };
  46. } // End anonymous namespace
  47. //===----------------------------------------------------------------------===//
  48. // IntrinsicEmitter Implementation
  49. //===----------------------------------------------------------------------===//
  50. void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
  51. emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
  52. CodeGenIntrinsicTable Ints(Records);
  53. if (Enums) {
  54. // Emit the enum information.
  55. EmitEnumInfo(Ints, OS);
  56. } else {
  57. // Emit the target metadata.
  58. EmitTargetInfo(Ints, OS);
  59. // Emit the intrinsic ID -> name table.
  60. EmitIntrinsicToNameTable(Ints, OS);
  61. // Emit the intrinsic ID -> overload table.
  62. EmitIntrinsicToOverloadTable(Ints, OS);
  63. // Emit the intrinsic declaration generator.
  64. EmitGenerator(Ints, OS);
  65. // Emit the intrinsic parameter attributes.
  66. EmitAttributes(Ints, OS);
  67. // Emit code to translate GCC builtins into LLVM intrinsics.
  68. EmitIntrinsicToBuiltinMap(Ints, true, OS);
  69. // Emit code to translate MS builtins into LLVM intrinsics.
  70. EmitIntrinsicToBuiltinMap(Ints, false, OS);
  71. }
  72. }
  73. void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
  74. raw_ostream &OS) {
  75. // Find the TargetSet for which to generate enums. There will be an initial
  76. // set with an empty target prefix which will include target independent
  77. // intrinsics like dbg.value.
  78. const CodeGenIntrinsicTable::TargetSet *Set = nullptr;
  79. for (const auto &Target : Ints.Targets) {
  80. if (Target.Name == IntrinsicPrefix) {
  81. Set = &Target;
  82. break;
  83. }
  84. }
  85. if (!Set) {
  86. std::vector<std::string> KnownTargets;
  87. for (const auto &Target : Ints.Targets)
  88. if (!Target.Name.empty())
  89. KnownTargets.push_back(Target.Name);
  90. PrintFatalError("tried to generate intrinsics for unknown target " +
  91. IntrinsicPrefix +
  92. "\nKnown targets are: " + join(KnownTargets, ", ") + "\n");
  93. }
  94. // Generate a complete header for target specific intrinsics.
  95. if (!IntrinsicPrefix.empty()) {
  96. std::string UpperPrefix = StringRef(IntrinsicPrefix).upper();
  97. OS << "#ifndef LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n";
  98. OS << "#define LLVM_IR_INTRINSIC_" << UpperPrefix << "_ENUMS_H\n\n";
  99. OS << "namespace llvm {\n";
  100. OS << "namespace Intrinsic {\n";
  101. OS << "enum " << UpperPrefix << "Intrinsics : unsigned {\n";
  102. }
  103. OS << "// Enum values for intrinsics\n";
  104. for (unsigned i = Set->Offset, e = Set->Offset + Set->Count; i != e; ++i) {
  105. OS << " " << Ints[i].EnumName;
  106. // Assign a value to the first intrinsic in this target set so that all
  107. // intrinsic ids are distinct.
  108. if (i == Set->Offset)
  109. OS << " = " << (Set->Offset + 1);
  110. OS << ", ";
  111. if (Ints[i].EnumName.size() < 40)
  112. OS.indent(40 - Ints[i].EnumName.size());
  113. OS << " // " << Ints[i].Name << "\n";
  114. }
  115. // Emit num_intrinsics into the target neutral enum.
  116. if (IntrinsicPrefix.empty()) {
  117. OS << " num_intrinsics = " << (Ints.size() + 1) << "\n";
  118. } else {
  119. OS << "}; // enum\n";
  120. OS << "} // namespace Intrinsic\n";
  121. OS << "} // namespace llvm\n\n";
  122. OS << "#endif\n";
  123. }
  124. }
  125. void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
  126. raw_ostream &OS) {
  127. OS << "// Target mapping\n";
  128. OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
  129. OS << "struct IntrinsicTargetInfo {\n"
  130. << " llvm::StringLiteral Name;\n"
  131. << " size_t Offset;\n"
  132. << " size_t Count;\n"
  133. << "};\n";
  134. OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
  135. for (auto Target : Ints.Targets)
  136. OS << " {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
  137. << ", " << Target.Count << "},\n";
  138. OS << "};\n";
  139. OS << "#endif\n\n";
  140. }
  141. void IntrinsicEmitter::EmitIntrinsicToNameTable(
  142. const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
  143. OS << "// Intrinsic ID to name table\n";
  144. OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
  145. OS << " // Note that entry #0 is the invalid intrinsic!\n";
  146. for (unsigned i = 0, e = Ints.size(); i != e; ++i)
  147. OS << " \"" << Ints[i].Name << "\",\n";
  148. OS << "#endif\n\n";
  149. }
  150. void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
  151. const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
  152. OS << "// Intrinsic ID to overload bitset\n";
  153. OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
  154. OS << "static const uint8_t OTable[] = {\n";
  155. OS << " 0";
  156. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  157. // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
  158. if ((i+1)%8 == 0)
  159. OS << ",\n 0";
  160. if (Ints[i].isOverloaded)
  161. OS << " | (1<<" << (i+1)%8 << ')';
  162. }
  163. OS << "\n};\n\n";
  164. // OTable contains a true bit at the position if the intrinsic is overloaded.
  165. OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
  166. OS << "#endif\n\n";
  167. }
  168. // NOTE: This must be kept in synch with the copy in lib/IR/Function.cpp!
  169. enum IIT_Info {
  170. // Common values should be encoded with 0-15.
  171. IIT_Done = 0,
  172. IIT_I1 = 1,
  173. IIT_I8 = 2,
  174. IIT_I16 = 3,
  175. IIT_I32 = 4,
  176. IIT_I64 = 5,
  177. IIT_F16 = 6,
  178. IIT_F32 = 7,
  179. IIT_F64 = 8,
  180. IIT_V2 = 9,
  181. IIT_V4 = 10,
  182. IIT_V8 = 11,
  183. IIT_V16 = 12,
  184. IIT_V32 = 13,
  185. IIT_PTR = 14,
  186. IIT_ARG = 15,
  187. // Values from 16+ are only encodable with the inefficient encoding.
  188. IIT_V64 = 16,
  189. IIT_MMX = 17,
  190. IIT_TOKEN = 18,
  191. IIT_METADATA = 19,
  192. IIT_EMPTYSTRUCT = 20,
  193. IIT_STRUCT2 = 21,
  194. IIT_STRUCT3 = 22,
  195. IIT_STRUCT4 = 23,
  196. IIT_STRUCT5 = 24,
  197. IIT_EXTEND_ARG = 25,
  198. IIT_TRUNC_ARG = 26,
  199. IIT_ANYPTR = 27,
  200. IIT_V1 = 28,
  201. IIT_VARARG = 29,
  202. IIT_HALF_VEC_ARG = 30,
  203. IIT_SAME_VEC_WIDTH_ARG = 31,
  204. IIT_PTR_TO_ARG = 32,
  205. IIT_PTR_TO_ELT = 33,
  206. IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
  207. IIT_I128 = 35,
  208. IIT_V512 = 36,
  209. IIT_V1024 = 37,
  210. IIT_STRUCT6 = 38,
  211. IIT_STRUCT7 = 39,
  212. IIT_STRUCT8 = 40,
  213. IIT_F128 = 41,
  214. IIT_VEC_ELEMENT = 42,
  215. IIT_SCALABLE_VEC = 43,
  216. IIT_SUBDIVIDE2_ARG = 44,
  217. IIT_SUBDIVIDE4_ARG = 45,
  218. IIT_VEC_OF_BITCASTS_TO_INT = 46,
  219. IIT_V128 = 47,
  220. IIT_BF16 = 48,
  221. IIT_STRUCT9 = 49,
  222. IIT_V256 = 50,
  223. IIT_AMX = 51,
  224. IIT_PPCF128 = 52,
  225. IIT_V3 = 53,
  226. IIT_EXTERNREF = 54,
  227. IIT_FUNCREF = 55
  228. };
  229. static void EncodeFixedValueType(MVT::SimpleValueType VT,
  230. std::vector<unsigned char> &Sig) {
  231. if (MVT(VT).isInteger()) {
  232. unsigned BitWidth = MVT(VT).getFixedSizeInBits();
  233. switch (BitWidth) {
  234. default: PrintFatalError("unhandled integer type width in intrinsic!");
  235. case 1: return Sig.push_back(IIT_I1);
  236. case 8: return Sig.push_back(IIT_I8);
  237. case 16: return Sig.push_back(IIT_I16);
  238. case 32: return Sig.push_back(IIT_I32);
  239. case 64: return Sig.push_back(IIT_I64);
  240. case 128: return Sig.push_back(IIT_I128);
  241. }
  242. }
  243. switch (VT) {
  244. default: PrintFatalError("unhandled MVT in intrinsic!");
  245. case MVT::f16: return Sig.push_back(IIT_F16);
  246. case MVT::bf16: return Sig.push_back(IIT_BF16);
  247. case MVT::f32: return Sig.push_back(IIT_F32);
  248. case MVT::f64: return Sig.push_back(IIT_F64);
  249. case MVT::f128: return Sig.push_back(IIT_F128);
  250. case MVT::ppcf128: return Sig.push_back(IIT_PPCF128);
  251. case MVT::token: return Sig.push_back(IIT_TOKEN);
  252. case MVT::Metadata: return Sig.push_back(IIT_METADATA);
  253. case MVT::x86mmx: return Sig.push_back(IIT_MMX);
  254. case MVT::x86amx: return Sig.push_back(IIT_AMX);
  255. // MVT::OtherVT is used to mean the empty struct type here.
  256. case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
  257. // MVT::isVoid is used to represent varargs here.
  258. case MVT::isVoid: return Sig.push_back(IIT_VARARG);
  259. case MVT::externref:
  260. return Sig.push_back(IIT_EXTERNREF);
  261. case MVT::funcref:
  262. return Sig.push_back(IIT_FUNCREF);
  263. }
  264. }
  265. #if defined(_MSC_VER) && !defined(__clang__)
  266. #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
  267. #endif
  268. static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
  269. unsigned &NextArgCode,
  270. std::vector<unsigned char> &Sig,
  271. ArrayRef<unsigned char> Mapping) {
  272. if (R->isSubClassOf("LLVMMatchType")) {
  273. unsigned Number = Mapping[R->getValueAsInt("Number")];
  274. assert(Number < ArgCodes.size() && "Invalid matching number!");
  275. if (R->isSubClassOf("LLVMExtendedType"))
  276. Sig.push_back(IIT_EXTEND_ARG);
  277. else if (R->isSubClassOf("LLVMTruncatedType"))
  278. Sig.push_back(IIT_TRUNC_ARG);
  279. else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
  280. Sig.push_back(IIT_HALF_VEC_ARG);
  281. else if (R->isSubClassOf("LLVMScalarOrSameVectorWidth")) {
  282. Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
  283. Sig.push_back((Number << 3) | ArgCodes[Number]);
  284. MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
  285. EncodeFixedValueType(VT, Sig);
  286. return;
  287. }
  288. else if (R->isSubClassOf("LLVMPointerTo"))
  289. Sig.push_back(IIT_PTR_TO_ARG);
  290. else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
  291. Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT);
  292. // Encode overloaded ArgNo
  293. Sig.push_back(NextArgCode++);
  294. // Encode LLVMMatchType<Number> ArgNo
  295. Sig.push_back(Number);
  296. return;
  297. } else if (R->isSubClassOf("LLVMPointerToElt"))
  298. Sig.push_back(IIT_PTR_TO_ELT);
  299. else if (R->isSubClassOf("LLVMVectorElementType"))
  300. Sig.push_back(IIT_VEC_ELEMENT);
  301. else if (R->isSubClassOf("LLVMSubdivide2VectorType"))
  302. Sig.push_back(IIT_SUBDIVIDE2_ARG);
  303. else if (R->isSubClassOf("LLVMSubdivide4VectorType"))
  304. Sig.push_back(IIT_SUBDIVIDE4_ARG);
  305. else if (R->isSubClassOf("LLVMVectorOfBitcastsToInt"))
  306. Sig.push_back(IIT_VEC_OF_BITCASTS_TO_INT);
  307. else
  308. Sig.push_back(IIT_ARG);
  309. return Sig.push_back((Number << 3) | 7 /*IITDescriptor::AK_MatchType*/);
  310. }
  311. MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
  312. unsigned Tmp = 0;
  313. switch (VT) {
  314. default: break;
  315. case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
  316. case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
  317. case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
  318. case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
  319. case MVT::Any: {
  320. // If this is an "any" valuetype, then the type is the type of the next
  321. // type in the list specified to getIntrinsic().
  322. Sig.push_back(IIT_ARG);
  323. // Figure out what arg # this is consuming, and remember what kind it was.
  324. assert(NextArgCode < ArgCodes.size() && ArgCodes[NextArgCode] == Tmp &&
  325. "Invalid or no ArgCode associated with overloaded VT!");
  326. unsigned ArgNo = NextArgCode++;
  327. // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
  328. return Sig.push_back((ArgNo << 3) | Tmp);
  329. }
  330. case MVT::iPTR: {
  331. unsigned AddrSpace = 0;
  332. if (R->isSubClassOf("LLVMQualPointerType")) {
  333. AddrSpace = R->getValueAsInt("AddrSpace");
  334. assert(AddrSpace < 256 && "Address space exceeds 255");
  335. }
  336. if (AddrSpace) {
  337. Sig.push_back(IIT_ANYPTR);
  338. Sig.push_back(AddrSpace);
  339. } else {
  340. Sig.push_back(IIT_PTR);
  341. }
  342. return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, NextArgCode, Sig,
  343. Mapping);
  344. }
  345. }
  346. if (MVT(VT).isVector()) {
  347. MVT VVT = VT;
  348. if (VVT.isScalableVector())
  349. Sig.push_back(IIT_SCALABLE_VEC);
  350. switch (VVT.getVectorMinNumElements()) {
  351. default: PrintFatalError("unhandled vector type width in intrinsic!");
  352. case 1: Sig.push_back(IIT_V1); break;
  353. case 2: Sig.push_back(IIT_V2); break;
  354. case 3: Sig.push_back(IIT_V3); break;
  355. case 4: Sig.push_back(IIT_V4); break;
  356. case 8: Sig.push_back(IIT_V8); break;
  357. case 16: Sig.push_back(IIT_V16); break;
  358. case 32: Sig.push_back(IIT_V32); break;
  359. case 64: Sig.push_back(IIT_V64); break;
  360. case 128: Sig.push_back(IIT_V128); break;
  361. case 256: Sig.push_back(IIT_V256); break;
  362. case 512: Sig.push_back(IIT_V512); break;
  363. case 1024: Sig.push_back(IIT_V1024); break;
  364. }
  365. return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
  366. }
  367. EncodeFixedValueType(VT, Sig);
  368. }
  369. static void UpdateArgCodes(Record *R, std::vector<unsigned char> &ArgCodes,
  370. unsigned int &NumInserted,
  371. SmallVectorImpl<unsigned char> &Mapping) {
  372. if (R->isSubClassOf("LLVMMatchType")) {
  373. if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
  374. ArgCodes.push_back(3 /*vAny*/);
  375. ++NumInserted;
  376. }
  377. return;
  378. }
  379. unsigned Tmp = 0;
  380. switch (getValueType(R->getValueAsDef("VT"))) {
  381. default: break;
  382. case MVT::iPTR:
  383. UpdateArgCodes(R->getValueAsDef("ElTy"), ArgCodes, NumInserted, Mapping);
  384. break;
  385. case MVT::iPTRAny:
  386. ++Tmp;
  387. LLVM_FALLTHROUGH;
  388. case MVT::vAny:
  389. ++Tmp;
  390. LLVM_FALLTHROUGH;
  391. case MVT::fAny:
  392. ++Tmp;
  393. LLVM_FALLTHROUGH;
  394. case MVT::iAny:
  395. ++Tmp;
  396. LLVM_FALLTHROUGH;
  397. case MVT::Any:
  398. unsigned OriginalIdx = ArgCodes.size() - NumInserted;
  399. assert(OriginalIdx >= Mapping.size());
  400. Mapping.resize(OriginalIdx+1);
  401. Mapping[OriginalIdx] = ArgCodes.size();
  402. ArgCodes.push_back(Tmp);
  403. break;
  404. }
  405. }
  406. #if defined(_MSC_VER) && !defined(__clang__)
  407. #pragma optimize("",on)
  408. #endif
  409. /// ComputeFixedEncoding - If we can encode the type signature for this
  410. /// intrinsic into 32 bits, return it. If not, return ~0U.
  411. static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
  412. std::vector<unsigned char> &TypeSig) {
  413. std::vector<unsigned char> ArgCodes;
  414. // Add codes for any overloaded result VTs.
  415. unsigned int NumInserted = 0;
  416. SmallVector<unsigned char, 8> ArgMapping;
  417. for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
  418. UpdateArgCodes(Int.IS.RetTypeDefs[i], ArgCodes, NumInserted, ArgMapping);
  419. // Add codes for any overloaded operand VTs.
  420. for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
  421. UpdateArgCodes(Int.IS.ParamTypeDefs[i], ArgCodes, NumInserted, ArgMapping);
  422. unsigned NextArgCode = 0;
  423. if (Int.IS.RetVTs.empty())
  424. TypeSig.push_back(IIT_Done);
  425. else if (Int.IS.RetVTs.size() == 1 &&
  426. Int.IS.RetVTs[0] == MVT::isVoid)
  427. TypeSig.push_back(IIT_Done);
  428. else {
  429. switch (Int.IS.RetVTs.size()) {
  430. case 1: break;
  431. case 2: TypeSig.push_back(IIT_STRUCT2); break;
  432. case 3: TypeSig.push_back(IIT_STRUCT3); break;
  433. case 4: TypeSig.push_back(IIT_STRUCT4); break;
  434. case 5: TypeSig.push_back(IIT_STRUCT5); break;
  435. case 6: TypeSig.push_back(IIT_STRUCT6); break;
  436. case 7: TypeSig.push_back(IIT_STRUCT7); break;
  437. case 8: TypeSig.push_back(IIT_STRUCT8); break;
  438. case 9: TypeSig.push_back(IIT_STRUCT9); break;
  439. default: llvm_unreachable("Unhandled case in struct");
  440. }
  441. for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
  442. EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, NextArgCode, TypeSig,
  443. ArgMapping);
  444. }
  445. for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
  446. EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, NextArgCode, TypeSig,
  447. ArgMapping);
  448. }
  449. static void printIITEntry(raw_ostream &OS, unsigned char X) {
  450. OS << (unsigned)X;
  451. }
  452. void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
  453. raw_ostream &OS) {
  454. // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
  455. // capture it in this vector, otherwise store a ~0U.
  456. std::vector<unsigned> FixedEncodings;
  457. SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
  458. std::vector<unsigned char> TypeSig;
  459. // Compute the unique argument type info.
  460. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  461. // Get the signature for the intrinsic.
  462. TypeSig.clear();
  463. ComputeFixedEncoding(Ints[i], TypeSig);
  464. // Check to see if we can encode it into a 32-bit word. We can only encode
  465. // 8 nibbles into a 32-bit word.
  466. if (TypeSig.size() <= 8) {
  467. bool Failed = false;
  468. unsigned Result = 0;
  469. for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
  470. // If we had an unencodable argument, bail out.
  471. if (TypeSig[i] > 15) {
  472. Failed = true;
  473. break;
  474. }
  475. Result = (Result << 4) | TypeSig[e-i-1];
  476. }
  477. // If this could be encoded into a 31-bit word, return it.
  478. if (!Failed && (Result >> 31) == 0) {
  479. FixedEncodings.push_back(Result);
  480. continue;
  481. }
  482. }
  483. // Otherwise, we're going to unique the sequence into the
  484. // LongEncodingTable, and use its offset in the 32-bit table instead.
  485. LongEncodingTable.add(TypeSig);
  486. // This is a placehold that we'll replace after the table is laid out.
  487. FixedEncodings.push_back(~0U);
  488. }
  489. LongEncodingTable.layout();
  490. OS << "// Global intrinsic function declaration type table.\n";
  491. OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
  492. OS << "static const unsigned IIT_Table[] = {\n ";
  493. for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
  494. if ((i & 7) == 7)
  495. OS << "\n ";
  496. // If the entry fit in the table, just emit it.
  497. if (FixedEncodings[i] != ~0U) {
  498. OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", ";
  499. continue;
  500. }
  501. TypeSig.clear();
  502. ComputeFixedEncoding(Ints[i], TypeSig);
  503. // Otherwise, emit the offset into the long encoding table. We emit it this
  504. // way so that it is easier to read the offset in the .def file.
  505. OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
  506. }
  507. OS << "0\n};\n\n";
  508. // Emit the shared table of register lists.
  509. OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
  510. if (!LongEncodingTable.empty())
  511. LongEncodingTable.emit(OS, printIITEntry);
  512. OS << " 255\n};\n\n";
  513. OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
  514. }
  515. namespace {
  516. struct AttributeComparator {
  517. bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
  518. // Sort throwing intrinsics after non-throwing intrinsics.
  519. if (L->canThrow != R->canThrow)
  520. return R->canThrow;
  521. if (L->isNoDuplicate != R->isNoDuplicate)
  522. return R->isNoDuplicate;
  523. if (L->isNoMerge != R->isNoMerge)
  524. return R->isNoMerge;
  525. if (L->isNoReturn != R->isNoReturn)
  526. return R->isNoReturn;
  527. if (L->isNoSync != R->isNoSync)
  528. return R->isNoSync;
  529. if (L->isNoFree != R->isNoFree)
  530. return R->isNoFree;
  531. if (L->isWillReturn != R->isWillReturn)
  532. return R->isWillReturn;
  533. if (L->isCold != R->isCold)
  534. return R->isCold;
  535. if (L->isConvergent != R->isConvergent)
  536. return R->isConvergent;
  537. if (L->isSpeculatable != R->isSpeculatable)
  538. return R->isSpeculatable;
  539. if (L->hasSideEffects != R->hasSideEffects)
  540. return R->hasSideEffects;
  541. // Try to order by readonly/readnone attribute.
  542. CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
  543. CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
  544. if (LK != RK) return (LK > RK);
  545. // Order by argument attributes.
  546. // This is reliable because each side is already sorted internally.
  547. return (L->ArgumentAttributes < R->ArgumentAttributes);
  548. }
  549. };
  550. } // End anonymous namespace
  551. /// EmitAttributes - This emits the Intrinsic::getAttributes method.
  552. void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
  553. raw_ostream &OS) {
  554. OS << "// Add parameter attributes that are not common to all intrinsics.\n";
  555. OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
  556. OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
  557. // Compute the maximum number of attribute arguments and the map
  558. typedef std::map<const CodeGenIntrinsic*, unsigned,
  559. AttributeComparator> UniqAttrMapTy;
  560. UniqAttrMapTy UniqAttributes;
  561. unsigned maxArgAttrs = 0;
  562. unsigned AttrNum = 0;
  563. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  564. const CodeGenIntrinsic &intrinsic = Ints[i];
  565. maxArgAttrs =
  566. std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
  567. unsigned &N = UniqAttributes[&intrinsic];
  568. if (N) continue;
  569. N = ++AttrNum;
  570. assert(N < 65536 && "Too many unique attributes for table!");
  571. }
  572. // Emit an array of AttributeList. Most intrinsics will have at least one
  573. // entry, for the function itself (index ~1), which is usually nounwind.
  574. OS << " static const uint16_t IntrinsicsToAttributesMap[] = {\n";
  575. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  576. const CodeGenIntrinsic &intrinsic = Ints[i];
  577. OS << " " << UniqAttributes[&intrinsic] << ", // "
  578. << intrinsic.Name << "\n";
  579. }
  580. OS << " };\n\n";
  581. OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n";
  582. OS << " unsigned NumAttrs = 0;\n";
  583. OS << " if (id != 0) {\n";
  584. OS << " switch(IntrinsicsToAttributesMap[id - 1]) {\n";
  585. OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
  586. for (auto UniqAttribute : UniqAttributes) {
  587. OS << " case " << UniqAttribute.second << ": {\n";
  588. const CodeGenIntrinsic &Intrinsic = *(UniqAttribute.first);
  589. // Keep track of the number of attributes we're writing out.
  590. unsigned numAttrs = 0;
  591. // The argument attributes are alreadys sorted by argument index.
  592. unsigned Ai = 0, Ae = Intrinsic.ArgumentAttributes.size();
  593. if (Ae) {
  594. while (Ai != Ae) {
  595. unsigned AttrIdx = Intrinsic.ArgumentAttributes[Ai].Index;
  596. OS << " const Attribute::AttrKind AttrParam" << AttrIdx << "[]= {";
  597. ListSeparator LS(",");
  598. bool AllValuesAreZero = true;
  599. SmallVector<uint64_t, 8> Values;
  600. do {
  601. switch (Intrinsic.ArgumentAttributes[Ai].Kind) {
  602. case CodeGenIntrinsic::NoCapture:
  603. OS << LS << "Attribute::NoCapture";
  604. break;
  605. case CodeGenIntrinsic::NoAlias:
  606. OS << LS << "Attribute::NoAlias";
  607. break;
  608. case CodeGenIntrinsic::NoUndef:
  609. OS << LS << "Attribute::NoUndef";
  610. break;
  611. case CodeGenIntrinsic::Returned:
  612. OS << LS << "Attribute::Returned";
  613. break;
  614. case CodeGenIntrinsic::ReadOnly:
  615. OS << LS << "Attribute::ReadOnly";
  616. break;
  617. case CodeGenIntrinsic::WriteOnly:
  618. OS << LS << "Attribute::WriteOnly";
  619. break;
  620. case CodeGenIntrinsic::ReadNone:
  621. OS << LS << "Attribute::ReadNone";
  622. break;
  623. case CodeGenIntrinsic::ImmArg:
  624. OS << LS << "Attribute::ImmArg";
  625. break;
  626. case CodeGenIntrinsic::Alignment:
  627. OS << LS << "Attribute::Alignment";
  628. break;
  629. }
  630. uint64_t V = Intrinsic.ArgumentAttributes[Ai].Value;
  631. Values.push_back(V);
  632. AllValuesAreZero &= (V == 0);
  633. ++Ai;
  634. } while (Ai != Ae && Intrinsic.ArgumentAttributes[Ai].Index == AttrIdx);
  635. OS << "};\n";
  636. // Generate attribute value array if not all attribute values are zero.
  637. if (!AllValuesAreZero) {
  638. OS << " const uint64_t AttrValParam" << AttrIdx << "[]= {";
  639. ListSeparator LSV(",");
  640. for (const auto V : Values)
  641. OS << LSV << V;
  642. OS << "};\n";
  643. }
  644. OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
  645. << AttrIdx << ", AttrParam" << AttrIdx;
  646. if (!AllValuesAreZero)
  647. OS << ", AttrValParam" << AttrIdx;
  648. OS << ");\n";
  649. }
  650. }
  651. if (!Intrinsic.canThrow ||
  652. (Intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem &&
  653. !Intrinsic.hasSideEffects) ||
  654. Intrinsic.isNoReturn || Intrinsic.isNoSync || Intrinsic.isNoFree ||
  655. Intrinsic.isWillReturn || Intrinsic.isCold || Intrinsic.isNoDuplicate ||
  656. Intrinsic.isNoMerge || Intrinsic.isConvergent ||
  657. Intrinsic.isSpeculatable) {
  658. OS << " const Attribute::AttrKind Atts[] = {";
  659. ListSeparator LS(",");
  660. if (!Intrinsic.canThrow)
  661. OS << LS << "Attribute::NoUnwind";
  662. if (Intrinsic.isNoReturn)
  663. OS << LS << "Attribute::NoReturn";
  664. if (Intrinsic.isNoSync)
  665. OS << LS << "Attribute::NoSync";
  666. if (Intrinsic.isNoFree)
  667. OS << LS << "Attribute::NoFree";
  668. if (Intrinsic.isWillReturn)
  669. OS << LS << "Attribute::WillReturn";
  670. if (Intrinsic.isCold)
  671. OS << LS << "Attribute::Cold";
  672. if (Intrinsic.isNoDuplicate)
  673. OS << LS << "Attribute::NoDuplicate";
  674. if (Intrinsic.isNoMerge)
  675. OS << LS << "Attribute::NoMerge";
  676. if (Intrinsic.isConvergent)
  677. OS << LS << "Attribute::Convergent";
  678. if (Intrinsic.isSpeculatable)
  679. OS << LS << "Attribute::Speculatable";
  680. switch (Intrinsic.ModRef) {
  681. case CodeGenIntrinsic::NoMem:
  682. if (Intrinsic.hasSideEffects)
  683. break;
  684. OS << LS;
  685. OS << "Attribute::ReadNone";
  686. break;
  687. case CodeGenIntrinsic::ReadArgMem:
  688. OS << LS;
  689. OS << "Attribute::ReadOnly,";
  690. OS << "Attribute::ArgMemOnly";
  691. break;
  692. case CodeGenIntrinsic::ReadMem:
  693. OS << LS;
  694. OS << "Attribute::ReadOnly";
  695. break;
  696. case CodeGenIntrinsic::ReadInaccessibleMem:
  697. OS << LS;
  698. OS << "Attribute::ReadOnly,";
  699. OS << "Attribute::InaccessibleMemOnly";
  700. break;
  701. case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
  702. OS << LS;
  703. OS << "Attribute::ReadOnly,";
  704. OS << "Attribute::InaccessibleMemOrArgMemOnly";
  705. break;
  706. case CodeGenIntrinsic::WriteArgMem:
  707. OS << LS;
  708. OS << "Attribute::WriteOnly,";
  709. OS << "Attribute::ArgMemOnly";
  710. break;
  711. case CodeGenIntrinsic::WriteMem:
  712. OS << LS;
  713. OS << "Attribute::WriteOnly";
  714. break;
  715. case CodeGenIntrinsic::WriteInaccessibleMem:
  716. OS << LS;
  717. OS << "Attribute::WriteOnly,";
  718. OS << "Attribute::InaccessibleMemOnly";
  719. break;
  720. case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
  721. OS << LS;
  722. OS << "Attribute::WriteOnly,";
  723. OS << "Attribute::InaccessibleMemOrArgMemOnly";
  724. break;
  725. case CodeGenIntrinsic::ReadWriteArgMem:
  726. OS << LS;
  727. OS << "Attribute::ArgMemOnly";
  728. break;
  729. case CodeGenIntrinsic::ReadWriteInaccessibleMem:
  730. OS << LS;
  731. OS << "Attribute::InaccessibleMemOnly";
  732. break;
  733. case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
  734. OS << LS;
  735. OS << "Attribute::InaccessibleMemOrArgMemOnly";
  736. break;
  737. case CodeGenIntrinsic::ReadWriteMem:
  738. break;
  739. }
  740. OS << "};\n";
  741. OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
  742. << "AttributeList::FunctionIndex, Atts);\n";
  743. }
  744. if (numAttrs) {
  745. OS << " NumAttrs = " << numAttrs << ";\n";
  746. OS << " break;\n";
  747. OS << " }\n";
  748. } else {
  749. OS << " return AttributeList();\n";
  750. OS << " }\n";
  751. }
  752. }
  753. OS << " }\n";
  754. OS << " }\n";
  755. OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
  756. OS << "}\n";
  757. OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
  758. }
  759. void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
  760. const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
  761. StringRef CompilerName = (IsGCC ? "GCC" : "MS");
  762. typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
  763. BIMTy BuiltinMap;
  764. StringToOffsetTable Table;
  765. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  766. const std::string &BuiltinName =
  767. IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
  768. if (!BuiltinName.empty()) {
  769. // Get the map for this target prefix.
  770. std::map<std::string, std::string> &BIM =
  771. BuiltinMap[Ints[i].TargetPrefix];
  772. if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
  773. PrintFatalError(Ints[i].TheDef->getLoc(),
  774. "Intrinsic '" + Ints[i].TheDef->getName() +
  775. "': duplicate " + CompilerName + " builtin name!");
  776. Table.GetOrAddStringOffset(BuiltinName);
  777. }
  778. }
  779. OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
  780. OS << "// This is used by the C front-end. The builtin name is passed\n";
  781. OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
  782. OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
  783. OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
  784. OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
  785. << "Builtin(const char "
  786. << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
  787. if (Table.Empty()) {
  788. OS << " return Intrinsic::not_intrinsic;\n";
  789. OS << "}\n";
  790. OS << "#endif\n\n";
  791. return;
  792. }
  793. OS << " static const char BuiltinNames[] = {\n";
  794. Table.EmitCharArray(OS);
  795. OS << " };\n\n";
  796. OS << " struct BuiltinEntry {\n";
  797. OS << " Intrinsic::ID IntrinID;\n";
  798. OS << " unsigned StrTabOffset;\n";
  799. OS << " const char *getName() const {\n";
  800. OS << " return &BuiltinNames[StrTabOffset];\n";
  801. OS << " }\n";
  802. OS << " bool operator<(StringRef RHS) const {\n";
  803. OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
  804. OS << " }\n";
  805. OS << " };\n";
  806. OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
  807. // Note: this could emit significantly better code if we cared.
  808. for (auto &I : BuiltinMap) {
  809. OS << " ";
  810. if (!I.first.empty())
  811. OS << "if (TargetPrefix == \"" << I.first << "\") ";
  812. else
  813. OS << "/* Target Independent Builtins */ ";
  814. OS << "{\n";
  815. // Emit the comparisons for this target prefix.
  816. OS << " static const BuiltinEntry " << I.first << "Names[] = {\n";
  817. for (const auto &P : I.second) {
  818. OS << " {Intrinsic::" << P.second << ", "
  819. << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
  820. }
  821. OS << " };\n";
  822. OS << " auto I = std::lower_bound(std::begin(" << I.first << "Names),\n";
  823. OS << " std::end(" << I.first << "Names),\n";
  824. OS << " BuiltinNameStr);\n";
  825. OS << " if (I != std::end(" << I.first << "Names) &&\n";
  826. OS << " I->getName() == BuiltinNameStr)\n";
  827. OS << " return I->IntrinID;\n";
  828. OS << " }\n";
  829. }
  830. OS << " return ";
  831. OS << "Intrinsic::not_intrinsic;\n";
  832. OS << "}\n";
  833. OS << "#endif\n\n";
  834. }
  835. void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS) {
  836. IntrinsicEmitter(RK).run(OS, /*Enums=*/true);
  837. }
  838. void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS) {
  839. IntrinsicEmitter(RK).run(OS, /*Enums=*/false);
  840. }