IntrinsicEmitter.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
  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 emits information about intrinsic functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenIntrinsics.h"
  14. #include "CodeGenTarget.h"
  15. #include "SequenceToOffsetTable.h"
  16. #include "TableGenBackends.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/TableGen/Error.h"
  19. #include "llvm/TableGen/Record.h"
  20. #include "llvm/TableGen/StringMatcher.h"
  21. #include "llvm/TableGen/TableGenBackend.h"
  22. #include <algorithm>
  23. using namespace llvm;
  24. namespace {
  25. class IntrinsicEmitter {
  26. RecordKeeper &Records;
  27. bool TargetOnly;
  28. std::string TargetPrefix;
  29. public:
  30. IntrinsicEmitter(RecordKeeper &R, bool T)
  31. : Records(R), TargetOnly(T) {}
  32. void run(raw_ostream &OS);
  33. void EmitPrefix(raw_ostream &OS);
  34. void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
  35. raw_ostream &OS);
  36. void EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
  37. raw_ostream &OS);
  38. void EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
  39. raw_ostream &OS);
  40. void EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
  41. raw_ostream &OS);
  42. void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
  43. raw_ostream &OS);
  44. void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
  45. raw_ostream &OS);
  46. void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
  47. raw_ostream &OS);
  48. void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
  49. raw_ostream &OS);
  50. void EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
  51. raw_ostream &OS);
  52. void EmitSuffix(raw_ostream &OS);
  53. };
  54. } // End anonymous namespace
  55. //===----------------------------------------------------------------------===//
  56. // IntrinsicEmitter Implementation
  57. //===----------------------------------------------------------------------===//
  58. void IntrinsicEmitter::run(raw_ostream &OS) {
  59. emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
  60. std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
  61. if (TargetOnly && !Ints.empty())
  62. TargetPrefix = Ints[0].TargetPrefix;
  63. EmitPrefix(OS);
  64. // Emit the enum information.
  65. EmitEnumInfo(Ints, OS);
  66. // Emit the intrinsic ID -> name table.
  67. EmitIntrinsicToNameTable(Ints, OS);
  68. // Emit the intrinsic ID -> overload table.
  69. EmitIntrinsicToOverloadTable(Ints, OS);
  70. // Emit the function name recognizer.
  71. EmitFnNameRecognizer(Ints, OS);
  72. // Emit the intrinsic declaration generator.
  73. EmitGenerator(Ints, OS);
  74. // Emit the intrinsic parameter attributes.
  75. EmitAttributes(Ints, OS);
  76. // Emit intrinsic alias analysis mod/ref behavior.
  77. EmitModRefBehavior(Ints, OS);
  78. // Emit code to translate GCC builtins into LLVM intrinsics.
  79. EmitIntrinsicToGCCBuiltinMap(Ints, OS);
  80. // Emit code to translate MS builtins into LLVM intrinsics.
  81. EmitIntrinsicToMSBuiltinMap(Ints, OS);
  82. EmitSuffix(OS);
  83. }
  84. void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
  85. OS << "// VisualStudio defines setjmp as _setjmp\n"
  86. "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
  87. " !defined(setjmp_undefined_for_msvc)\n"
  88. "# pragma push_macro(\"setjmp\")\n"
  89. "# undef setjmp\n"
  90. "# define setjmp_undefined_for_msvc\n"
  91. "#endif\n\n";
  92. }
  93. void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
  94. OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
  95. "// let's return it to _setjmp state\n"
  96. "# pragma pop_macro(\"setjmp\")\n"
  97. "# undef setjmp_undefined_for_msvc\n"
  98. "#endif\n\n";
  99. }
  100. void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
  101. raw_ostream &OS) {
  102. OS << "// Enum values for Intrinsics.h\n";
  103. OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
  104. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  105. OS << " " << Ints[i].EnumName;
  106. OS << ((i != e-1) ? ", " : " ");
  107. if (Ints[i].EnumName.size() < 40)
  108. OS << std::string(40-Ints[i].EnumName.size(), ' ');
  109. OS << " // " << Ints[i].Name << "\n";
  110. }
  111. OS << "#endif\n\n";
  112. }
  113. void IntrinsicEmitter::
  114. EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
  115. raw_ostream &OS) {
  116. // Build a 'first character of function name' -> intrinsic # mapping.
  117. std::map<char, std::vector<unsigned> > IntMapping;
  118. for (unsigned i = 0, e = Ints.size(); i != e; ++i)
  119. IntMapping[Ints[i].Name[5]].push_back(i);
  120. OS << "// Function name -> enum value recognizer code.\n";
  121. OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
  122. OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
  123. OS << " switch (Name[5]) { // Dispatch on first letter.\n";
  124. OS << " default: break;\n";
  125. // Emit the intrinsic matching stuff by first letter.
  126. for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
  127. E = IntMapping.end(); I != E; ++I) {
  128. OS << " case '" << I->first << "':\n";
  129. std::vector<unsigned> &IntList = I->second;
  130. // Sort in reverse order of intrinsic name so "abc.def" appears after
  131. // "abd.def.ghi" in the overridden name matcher
  132. std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
  133. return Ints[i].Name > Ints[j].Name;
  134. });
  135. // Emit all the overloaded intrinsics first, build a table of the
  136. // non-overloaded ones.
  137. std::vector<StringMatcher::StringPair> MatchTable;
  138. for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
  139. unsigned IntNo = IntList[i];
  140. std::string Result = "return " + TargetPrefix + "Intrinsic::" +
  141. Ints[IntNo].EnumName + ";";
  142. if (!Ints[IntNo].isOverloaded) {
  143. MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
  144. continue;
  145. }
  146. // For overloaded intrinsics, only the prefix needs to match
  147. std::string TheStr = Ints[IntNo].Name.substr(6);
  148. TheStr += '.'; // Require "bswap." instead of bswap.
  149. OS << " if (NameR.startswith(\"" << TheStr << "\")) "
  150. << Result << '\n';
  151. }
  152. // Emit the matcher logic for the fixed length strings.
  153. StringMatcher("NameR", MatchTable, OS).Emit(1);
  154. OS << " break; // end of '" << I->first << "' case.\n";
  155. }
  156. OS << " }\n";
  157. OS << "#endif\n\n";
  158. }
  159. void IntrinsicEmitter::
  160. EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
  161. raw_ostream &OS) {
  162. OS << "// Intrinsic ID to name table\n";
  163. OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
  164. OS << " // Note that entry #0 is the invalid intrinsic!\n";
  165. for (unsigned i = 0, e = Ints.size(); i != e; ++i)
  166. OS << " \"" << Ints[i].Name << "\",\n";
  167. OS << "#endif\n\n";
  168. }
  169. void IntrinsicEmitter::
  170. EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
  171. raw_ostream &OS) {
  172. OS << "// Intrinsic ID to overload bitset\n";
  173. OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
  174. OS << "static const uint8_t OTable[] = {\n";
  175. OS << " 0";
  176. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  177. // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
  178. if ((i+1)%8 == 0)
  179. OS << ",\n 0";
  180. if (Ints[i].isOverloaded)
  181. OS << " | (1<<" << (i+1)%8 << ')';
  182. }
  183. OS << "\n};\n\n";
  184. // OTable contains a true bit at the position if the intrinsic is overloaded.
  185. OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
  186. OS << "#endif\n\n";
  187. }
  188. // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
  189. enum IIT_Info {
  190. // Common values should be encoded with 0-15.
  191. IIT_Done = 0,
  192. IIT_I1 = 1,
  193. IIT_I8 = 2,
  194. IIT_I16 = 3,
  195. IIT_I32 = 4,
  196. IIT_I64 = 5,
  197. IIT_F16 = 6,
  198. IIT_F32 = 7,
  199. IIT_F64 = 8,
  200. IIT_V2 = 9,
  201. IIT_V4 = 10,
  202. IIT_V8 = 11,
  203. IIT_V16 = 12,
  204. IIT_V32 = 13,
  205. IIT_PTR = 14,
  206. IIT_ARG = 15,
  207. // Values from 16+ are only encodable with the inefficient encoding.
  208. IIT_V64 = 16,
  209. IIT_MMX = 17,
  210. IIT_METADATA = 18,
  211. IIT_EMPTYSTRUCT = 19,
  212. IIT_STRUCT2 = 20,
  213. IIT_STRUCT3 = 21,
  214. IIT_STRUCT4 = 22,
  215. IIT_STRUCT5 = 23,
  216. IIT_EXTEND_ARG = 24,
  217. IIT_TRUNC_ARG = 25,
  218. IIT_ANYPTR = 26,
  219. IIT_V1 = 27,
  220. IIT_VARARG = 28,
  221. IIT_HALF_VEC_ARG = 29,
  222. IIT_SAME_VEC_WIDTH_ARG = 30,
  223. IIT_PTR_TO_ARG = 31,
  224. IIT_VEC_OF_PTRS_TO_ELT = 32,
  225. IIT_I128 = 33
  226. };
  227. static void EncodeFixedValueType(MVT::SimpleValueType VT,
  228. std::vector<unsigned char> &Sig) {
  229. if (MVT(VT).isInteger()) {
  230. unsigned BitWidth = MVT(VT).getSizeInBits();
  231. switch (BitWidth) {
  232. default: PrintFatalError("unhandled integer type width in intrinsic!");
  233. case 1: return Sig.push_back(IIT_I1);
  234. case 8: return Sig.push_back(IIT_I8);
  235. case 16: return Sig.push_back(IIT_I16);
  236. case 32: return Sig.push_back(IIT_I32);
  237. case 64: return Sig.push_back(IIT_I64);
  238. case 128: return Sig.push_back(IIT_I128);
  239. }
  240. }
  241. switch (VT) {
  242. default: PrintFatalError("unhandled MVT in intrinsic!");
  243. case MVT::f16: return Sig.push_back(IIT_F16);
  244. case MVT::f32: return Sig.push_back(IIT_F32);
  245. case MVT::f64: return Sig.push_back(IIT_F64);
  246. case MVT::Metadata: return Sig.push_back(IIT_METADATA);
  247. case MVT::x86mmx: return Sig.push_back(IIT_MMX);
  248. // MVT::OtherVT is used to mean the empty struct type here.
  249. case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
  250. // MVT::isVoid is used to represent varargs here.
  251. case MVT::isVoid: return Sig.push_back(IIT_VARARG);
  252. }
  253. }
  254. #if defined(_MSC_VER) && !defined(__clang__)
  255. // HLSL Comment - still a problem with more recent versions
  256. #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
  257. #endif
  258. static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
  259. std::vector<unsigned char> &Sig) {
  260. if (R->isSubClassOf("LLVMMatchType")) {
  261. unsigned Number = R->getValueAsInt("Number");
  262. assert(Number < ArgCodes.size() && "Invalid matching number!");
  263. if (R->isSubClassOf("LLVMExtendedType"))
  264. Sig.push_back(IIT_EXTEND_ARG);
  265. else if (R->isSubClassOf("LLVMTruncatedType"))
  266. Sig.push_back(IIT_TRUNC_ARG);
  267. else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
  268. Sig.push_back(IIT_HALF_VEC_ARG);
  269. else if (R->isSubClassOf("LLVMVectorSameWidth")) {
  270. Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
  271. Sig.push_back((Number << 3) | ArgCodes[Number]);
  272. MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
  273. EncodeFixedValueType(VT, Sig);
  274. return;
  275. }
  276. else if (R->isSubClassOf("LLVMPointerTo"))
  277. Sig.push_back(IIT_PTR_TO_ARG);
  278. else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
  279. Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
  280. else
  281. Sig.push_back(IIT_ARG);
  282. return Sig.push_back((Number << 3) | ArgCodes[Number]);
  283. }
  284. MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
  285. unsigned Tmp = 0;
  286. switch (VT) {
  287. default: break;
  288. case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
  289. case MVT::vAny: ++Tmp; // FALL THROUGH.
  290. case MVT::fAny: ++Tmp; // FALL THROUGH.
  291. case MVT::iAny: ++Tmp; // FALL THROUGH.
  292. case MVT::Any: {
  293. // If this is an "any" valuetype, then the type is the type of the next
  294. // type in the list specified to getIntrinsic().
  295. Sig.push_back(IIT_ARG);
  296. // Figure out what arg # this is consuming, and remember what kind it was.
  297. unsigned ArgNo = ArgCodes.size();
  298. ArgCodes.push_back(Tmp);
  299. // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
  300. return Sig.push_back((ArgNo << 3) | Tmp);
  301. }
  302. case MVT::iPTR: {
  303. unsigned AddrSpace = 0;
  304. if (R->isSubClassOf("LLVMQualPointerType")) {
  305. AddrSpace = R->getValueAsInt("AddrSpace");
  306. assert(AddrSpace < 256 && "Address space exceeds 255");
  307. }
  308. if (AddrSpace) {
  309. Sig.push_back(IIT_ANYPTR);
  310. Sig.push_back(AddrSpace);
  311. } else {
  312. Sig.push_back(IIT_PTR);
  313. }
  314. return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
  315. }
  316. }
  317. if (MVT(VT).isVector()) {
  318. MVT VVT = VT;
  319. switch (VVT.getVectorNumElements()) {
  320. default: PrintFatalError("unhandled vector type width in intrinsic!");
  321. case 1: Sig.push_back(IIT_V1); break;
  322. case 2: Sig.push_back(IIT_V2); break;
  323. case 4: Sig.push_back(IIT_V4); break;
  324. case 8: Sig.push_back(IIT_V8); break;
  325. case 16: Sig.push_back(IIT_V16); break;
  326. case 32: Sig.push_back(IIT_V32); break;
  327. case 64: Sig.push_back(IIT_V64); break;
  328. }
  329. return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
  330. }
  331. EncodeFixedValueType(VT, Sig);
  332. }
  333. #if defined(_MSC_VER) && !defined(__clang__)
  334. #pragma optimize("",on)
  335. #endif
  336. /// ComputeFixedEncoding - If we can encode the type signature for this
  337. /// intrinsic into 32 bits, return it. If not, return ~0U.
  338. static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
  339. std::vector<unsigned char> &TypeSig) {
  340. std::vector<unsigned char> ArgCodes;
  341. if (Int.IS.RetVTs.empty())
  342. TypeSig.push_back(IIT_Done);
  343. else if (Int.IS.RetVTs.size() == 1 &&
  344. Int.IS.RetVTs[0] == MVT::isVoid)
  345. TypeSig.push_back(IIT_Done);
  346. else {
  347. switch (Int.IS.RetVTs.size()) {
  348. case 1: break;
  349. case 2: TypeSig.push_back(IIT_STRUCT2); break;
  350. case 3: TypeSig.push_back(IIT_STRUCT3); break;
  351. case 4: TypeSig.push_back(IIT_STRUCT4); break;
  352. case 5: TypeSig.push_back(IIT_STRUCT5); break;
  353. default: llvm_unreachable("Unhandled case in struct");
  354. }
  355. for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
  356. EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
  357. }
  358. for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
  359. EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
  360. }
  361. static void printIITEntry(raw_ostream &OS, unsigned char X) {
  362. OS << (unsigned)X;
  363. }
  364. void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
  365. raw_ostream &OS) {
  366. // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
  367. // capture it in this vector, otherwise store a ~0U.
  368. std::vector<unsigned> FixedEncodings;
  369. SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
  370. std::vector<unsigned char> TypeSig;
  371. // Compute the unique argument type info.
  372. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  373. // Get the signature for the intrinsic.
  374. TypeSig.clear();
  375. ComputeFixedEncoding(Ints[i], TypeSig);
  376. // Check to see if we can encode it into a 32-bit word. We can only encode
  377. // 8 nibbles into a 32-bit word.
  378. if (TypeSig.size() <= 8) {
  379. bool Failed = false;
  380. unsigned Result = 0;
  381. for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
  382. // If we had an unencodable argument, bail out.
  383. if (TypeSig[i] > 15) {
  384. Failed = true;
  385. break;
  386. }
  387. Result = (Result << 4) | TypeSig[e-i-1];
  388. }
  389. // If this could be encoded into a 31-bit word, return it.
  390. if (!Failed && (Result >> 31) == 0) {
  391. FixedEncodings.push_back(Result);
  392. continue;
  393. }
  394. }
  395. // Otherwise, we're going to unique the sequence into the
  396. // LongEncodingTable, and use its offset in the 32-bit table instead.
  397. LongEncodingTable.add(TypeSig);
  398. // This is a placehold that we'll replace after the table is laid out.
  399. FixedEncodings.push_back(~0U);
  400. }
  401. LongEncodingTable.layout();
  402. OS << "// Global intrinsic function declaration type table.\n";
  403. OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
  404. OS << "static const unsigned IIT_Table[] = {\n ";
  405. for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
  406. if ((i & 7) == 7)
  407. OS << "\n ";
  408. // If the entry fit in the table, just emit it.
  409. if (FixedEncodings[i] != ~0U) {
  410. OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
  411. continue;
  412. }
  413. TypeSig.clear();
  414. ComputeFixedEncoding(Ints[i], TypeSig);
  415. // Otherwise, emit the offset into the long encoding table. We emit it this
  416. // way so that it is easier to read the offset in the .def file.
  417. OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
  418. }
  419. OS << "0\n};\n\n";
  420. // Emit the shared table of register lists.
  421. OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
  422. if (!LongEncodingTable.empty())
  423. LongEncodingTable.emit(OS, printIITEntry);
  424. OS << " 255\n};\n\n";
  425. OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
  426. }
  427. namespace {
  428. enum ModRefKind {
  429. MRK_none,
  430. MRK_readonly,
  431. MRK_readnone
  432. };
  433. }
  434. static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
  435. switch (intrinsic.ModRef) {
  436. case CodeGenIntrinsic::NoMem:
  437. return MRK_readnone;
  438. case CodeGenIntrinsic::ReadArgMem:
  439. case CodeGenIntrinsic::ReadMem:
  440. return MRK_readonly;
  441. case CodeGenIntrinsic::ReadWriteArgMem:
  442. case CodeGenIntrinsic::ReadWriteMem:
  443. return MRK_none;
  444. }
  445. llvm_unreachable("bad mod-ref kind");
  446. }
  447. namespace {
  448. struct AttributeComparator {
  449. bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
  450. // Sort throwing intrinsics after non-throwing intrinsics.
  451. if (L->canThrow != R->canThrow)
  452. return R->canThrow;
  453. if (L->isNoDuplicate != R->isNoDuplicate)
  454. return R->isNoDuplicate;
  455. if (L->isNoReturn != R->isNoReturn)
  456. return R->isNoReturn;
  457. if (L->isConvergent != R->isConvergent)
  458. return R->isConvergent;
  459. // Try to order by readonly/readnone attribute.
  460. ModRefKind LK = getModRefKind(*L);
  461. ModRefKind RK = getModRefKind(*R);
  462. if (LK != RK) return (LK > RK);
  463. // Order by argument attributes.
  464. // This is reliable because each side is already sorted internally.
  465. return (L->ArgumentAttributes < R->ArgumentAttributes);
  466. }
  467. };
  468. } // End anonymous namespace
  469. /// EmitAttributes - This emits the Intrinsic::getAttributes method.
  470. void IntrinsicEmitter::
  471. EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
  472. OS << "// Add parameter attributes that are not common to all intrinsics.\n";
  473. OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
  474. if (TargetOnly)
  475. OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
  476. << "Intrinsic::ID id) {\n";
  477. else
  478. OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
  479. // Compute the maximum number of attribute arguments and the map
  480. typedef std::map<const CodeGenIntrinsic*, unsigned,
  481. AttributeComparator> UniqAttrMapTy;
  482. UniqAttrMapTy UniqAttributes;
  483. unsigned maxArgAttrs = 0;
  484. unsigned AttrNum = 0;
  485. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  486. const CodeGenIntrinsic &intrinsic = Ints[i];
  487. maxArgAttrs =
  488. std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
  489. unsigned &N = UniqAttributes[&intrinsic];
  490. if (N) continue;
  491. assert(AttrNum < 256 && "Too many unique attributes for table!");
  492. N = ++AttrNum;
  493. }
  494. // Emit an array of AttributeSet. Most intrinsics will have at least one
  495. // entry, for the function itself (index ~1), which is usually nounwind.
  496. OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
  497. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  498. const CodeGenIntrinsic &intrinsic = Ints[i];
  499. OS << " " << UniqAttributes[&intrinsic] << ", // "
  500. << intrinsic.Name << "\n";
  501. }
  502. OS << " };\n\n";
  503. OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
  504. OS << " unsigned NumAttrs = 0;\n";
  505. OS << " if (id != 0) {\n";
  506. OS << " switch(IntrinsicsToAttributesMap[id - ";
  507. if (TargetOnly)
  508. OS << "Intrinsic::num_intrinsics";
  509. else
  510. OS << "1";
  511. OS << "]) {\n";
  512. OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
  513. for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
  514. E = UniqAttributes.end(); I != E; ++I) {
  515. OS << " case " << I->second << ": {\n";
  516. const CodeGenIntrinsic &intrinsic = *(I->first);
  517. // Keep track of the number of attributes we're writing out.
  518. unsigned numAttrs = 0;
  519. // The argument attributes are alreadys sorted by argument index.
  520. unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
  521. if (ae) {
  522. while (ai != ae) {
  523. unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
  524. OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
  525. bool addComma = false;
  526. do {
  527. switch (intrinsic.ArgumentAttributes[ai].second) {
  528. case CodeGenIntrinsic::NoCapture:
  529. if (addComma)
  530. OS << ",";
  531. OS << "Attribute::NoCapture";
  532. addComma = true;
  533. break;
  534. case CodeGenIntrinsic::ReadOnly:
  535. if (addComma)
  536. OS << ",";
  537. OS << "Attribute::ReadOnly";
  538. addComma = true;
  539. break;
  540. case CodeGenIntrinsic::ReadNone:
  541. if (addComma)
  542. OS << ",";
  543. OS << "Attributes::ReadNone";
  544. addComma = true;
  545. break;
  546. }
  547. ++ai;
  548. } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
  549. OS << "};\n";
  550. OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
  551. << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
  552. }
  553. }
  554. ModRefKind modRef = getModRefKind(intrinsic);
  555. if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
  556. intrinsic.isNoDuplicate || intrinsic.isConvergent) {
  557. OS << " const Attribute::AttrKind Atts[] = {";
  558. bool addComma = false;
  559. if (!intrinsic.canThrow) {
  560. OS << "Attribute::NoUnwind";
  561. addComma = true;
  562. }
  563. if (intrinsic.isNoReturn) {
  564. if (addComma)
  565. OS << ",";
  566. OS << "Attribute::NoReturn";
  567. addComma = true;
  568. }
  569. if (intrinsic.isNoDuplicate) {
  570. if (addComma)
  571. OS << ",";
  572. OS << "Attribute::NoDuplicate";
  573. addComma = true;
  574. }
  575. if (intrinsic.isConvergent) {
  576. if (addComma)
  577. OS << ",";
  578. OS << "Attribute::Convergent";
  579. addComma = true;
  580. }
  581. switch (modRef) {
  582. case MRK_none: break;
  583. case MRK_readonly:
  584. if (addComma)
  585. OS << ",";
  586. OS << "Attribute::ReadOnly";
  587. break;
  588. case MRK_readnone:
  589. if (addComma)
  590. OS << ",";
  591. OS << "Attribute::ReadNone";
  592. break;
  593. }
  594. OS << "};\n";
  595. OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
  596. << "AttributeSet::FunctionIndex, Atts);\n";
  597. }
  598. if (numAttrs) {
  599. OS << " NumAttrs = " << numAttrs << ";\n";
  600. OS << " break;\n";
  601. OS << " }\n";
  602. } else {
  603. OS << " return AttributeSet();\n";
  604. OS << " }\n";
  605. }
  606. }
  607. OS << " }\n";
  608. OS << " }\n";
  609. OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
  610. OS << "}\n";
  611. OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
  612. }
  613. /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
  614. void IntrinsicEmitter::
  615. EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
  616. OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
  617. << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
  618. << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
  619. << "\"Unknown intrinsic.\");\n\n";
  620. OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
  621. << " /* invalid */ UnknownModRefBehavior,\n";
  622. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  623. OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
  624. switch (Ints[i].ModRef) {
  625. case CodeGenIntrinsic::NoMem:
  626. OS << "DoesNotAccessMemory,\n";
  627. break;
  628. case CodeGenIntrinsic::ReadArgMem:
  629. OS << "OnlyReadsArgumentPointees,\n";
  630. break;
  631. case CodeGenIntrinsic::ReadMem:
  632. OS << "OnlyReadsMemory,\n";
  633. break;
  634. case CodeGenIntrinsic::ReadWriteArgMem:
  635. OS << "OnlyAccessesArgumentPointees,\n";
  636. break;
  637. case CodeGenIntrinsic::ReadWriteMem:
  638. OS << "UnknownModRefBehavior,\n";
  639. break;
  640. }
  641. }
  642. OS << "};\n\n"
  643. << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
  644. << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
  645. }
  646. /// EmitTargetBuiltins - All of the builtins in the specified map are for the
  647. /// same target, and we already checked it.
  648. static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
  649. const std::string &TargetPrefix,
  650. raw_ostream &OS) {
  651. std::vector<StringMatcher::StringPair> Results;
  652. for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
  653. E = BIM.end(); I != E; ++I) {
  654. std::string ResultCode =
  655. "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
  656. Results.emplace_back(I->first, ResultCode);
  657. }
  658. StringMatcher("BuiltinName", Results, OS).Emit();
  659. }
  660. void IntrinsicEmitter::
  661. EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
  662. raw_ostream &OS) {
  663. typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
  664. BIMTy BuiltinMap;
  665. for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
  666. if (!Ints[i].GCCBuiltinName.empty()) {
  667. // Get the map for this target prefix.
  668. std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
  669. if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
  670. Ints[i].EnumName)).second)
  671. PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
  672. "': duplicate GCC builtin name!");
  673. }
  674. }
  675. OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
  676. OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
  677. OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
  678. OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
  679. OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
  680. if (TargetOnly) {
  681. OS << "static " << TargetPrefix << "Intrinsic::ID "
  682. << "getIntrinsicForGCCBuiltin(const char "
  683. << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
  684. } else {
  685. OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
  686. << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
  687. }
  688. OS << " StringRef BuiltinName(BuiltinNameStr);\n";
  689. OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
  690. // Note: this could emit significantly better code if we cared.
  691. for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
  692. OS << " ";
  693. if (!I->first.empty())
  694. OS << "if (TargetPrefix == \"" << I->first << "\") ";
  695. else
  696. OS << "/* Target Independent Builtins */ ";
  697. OS << "{\n";
  698. // Emit the comparisons for this target prefix.
  699. EmitTargetBuiltins(I->second, TargetPrefix, OS);
  700. OS << " }\n";
  701. }
  702. OS << " return ";
  703. if (!TargetPrefix.empty())
  704. OS << "(" << TargetPrefix << "Intrinsic::ID)";
  705. OS << "Intrinsic::not_intrinsic;\n";
  706. OS << "}\n";
  707. OS << "#endif\n\n";
  708. }
  709. void IntrinsicEmitter::
  710. EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
  711. raw_ostream &OS) {
  712. std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
  713. for (const auto &Intrinsic : Ints) {
  714. if (Intrinsic.MSBuiltinName.empty())
  715. continue;
  716. auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
  717. if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
  718. Intrinsic.EnumName)).second)
  719. PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
  720. "duplicate MS builtin name!");
  721. }
  722. OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
  723. "// This is used by the C front-end. The MS builtin name is passed\n"
  724. "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
  725. "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
  726. "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
  727. OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
  728. << (TargetOnly ? "" : "Intrinsic::")
  729. << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
  730. OS << " StringRef BuiltinName(BN);\n"
  731. " StringRef TargetPrefix(TP);\n"
  732. "\n";
  733. for (const auto &Builtins : TargetBuiltins) {
  734. OS << " ";
  735. if (Builtins.first.empty())
  736. OS << "/* Target Independent Builtins */ ";
  737. else
  738. OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
  739. OS << "{\n";
  740. EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
  741. OS << "}";
  742. }
  743. OS << " return ";
  744. if (!TargetPrefix.empty())
  745. OS << "(" << TargetPrefix << "Intrinsic::ID)";
  746. OS << "Intrinsic::not_intrinsic;\n";
  747. OS << "}\n";
  748. OS << "#endif\n\n";
  749. }
  750. void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
  751. IntrinsicEmitter(RK, TargetOnly).run(OS);
  752. }