IntrinsicEmitter.cpp 33 KB

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