Function.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. //===- Function.cpp - Implement the Global object classes -----------------===//
  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 file implements the Function class for the IR library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/IR/Function.h"
  13. #include "SymbolTableListTraitsImpl.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/ADT/None.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringExtras.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/IR/Argument.h"
  23. #include "llvm/IR/Attributes.h"
  24. #include "llvm/IR/BasicBlock.h"
  25. #include "llvm/IR/Constant.h"
  26. #include "llvm/IR/Constants.h"
  27. #include "llvm/IR/DerivedTypes.h"
  28. #include "llvm/IR/GlobalValue.h"
  29. #include "llvm/IR/InstIterator.h"
  30. #include "llvm/IR/Instruction.h"
  31. #include "llvm/IR/Instructions.h"
  32. #include "llvm/IR/IntrinsicInst.h"
  33. #include "llvm/IR/Intrinsics.h"
  34. #include "llvm/IR/LLVMContext.h"
  35. #include "llvm/IR/MDBuilder.h"
  36. #include "llvm/IR/Metadata.h"
  37. #include "llvm/IR/Module.h"
  38. #include "llvm/IR/SymbolTableListTraits.h"
  39. #include "llvm/IR/Type.h"
  40. #include "llvm/IR/Use.h"
  41. #include "llvm/IR/User.h"
  42. #include "llvm/IR/Value.h"
  43. #include "llvm/IR/ValueSymbolTable.h"
  44. #include "llvm/Support/Casting.h"
  45. #include "llvm/Support/Compiler.h"
  46. #include "llvm/Support/ErrorHandling.h"
  47. #include <algorithm>
  48. #include <cassert>
  49. #include <cstddef>
  50. #include <cstdint>
  51. #include <cstring>
  52. #include <string>
  53. using namespace llvm;
  54. using ProfileCount = Function::ProfileCount;
  55. // Explicit instantiations of SymbolTableListTraits since some of the methods
  56. // are not in the public header file...
  57. template class llvm::SymbolTableListTraits<BasicBlock>;
  58. //===----------------------------------------------------------------------===//
  59. // Argument Implementation
  60. //===----------------------------------------------------------------------===//
  61. Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
  62. : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
  63. setName(Name);
  64. }
  65. void Argument::setParent(Function *parent) {
  66. Parent = parent;
  67. }
  68. bool Argument::hasNonNullAttr() const {
  69. if (!getType()->isPointerTy()) return false;
  70. if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull))
  71. return true;
  72. else if (getDereferenceableBytes() > 0 &&
  73. !NullPointerIsDefined(getParent(),
  74. getType()->getPointerAddressSpace()))
  75. return true;
  76. return false;
  77. }
  78. bool Argument::hasByValAttr() const {
  79. if (!getType()->isPointerTy()) return false;
  80. return hasAttribute(Attribute::ByVal);
  81. }
  82. bool Argument::hasSwiftSelfAttr() const {
  83. return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
  84. }
  85. bool Argument::hasSwiftErrorAttr() const {
  86. return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
  87. }
  88. bool Argument::hasInAllocaAttr() const {
  89. if (!getType()->isPointerTy()) return false;
  90. return hasAttribute(Attribute::InAlloca);
  91. }
  92. bool Argument::hasByValOrInAllocaAttr() const {
  93. if (!getType()->isPointerTy()) return false;
  94. AttributeList Attrs = getParent()->getAttributes();
  95. return Attrs.hasParamAttribute(getArgNo(), Attribute::ByVal) ||
  96. Attrs.hasParamAttribute(getArgNo(), Attribute::InAlloca);
  97. }
  98. unsigned Argument::getParamAlignment() const {
  99. assert(getType()->isPointerTy() && "Only pointers have alignments");
  100. return getParent()->getParamAlignment(getArgNo());
  101. }
  102. Type *Argument::getParamByValType() const {
  103. assert(getType()->isPointerTy() && "Only pointers have byval types");
  104. return getParent()->getParamByValType(getArgNo());
  105. }
  106. uint64_t Argument::getDereferenceableBytes() const {
  107. assert(getType()->isPointerTy() &&
  108. "Only pointers have dereferenceable bytes");
  109. return getParent()->getParamDereferenceableBytes(getArgNo());
  110. }
  111. uint64_t Argument::getDereferenceableOrNullBytes() const {
  112. assert(getType()->isPointerTy() &&
  113. "Only pointers have dereferenceable bytes");
  114. return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
  115. }
  116. bool Argument::hasNestAttr() const {
  117. if (!getType()->isPointerTy()) return false;
  118. return hasAttribute(Attribute::Nest);
  119. }
  120. bool Argument::hasNoAliasAttr() const {
  121. if (!getType()->isPointerTy()) return false;
  122. return hasAttribute(Attribute::NoAlias);
  123. }
  124. bool Argument::hasNoCaptureAttr() const {
  125. if (!getType()->isPointerTy()) return false;
  126. return hasAttribute(Attribute::NoCapture);
  127. }
  128. bool Argument::hasStructRetAttr() const {
  129. if (!getType()->isPointerTy()) return false;
  130. return hasAttribute(Attribute::StructRet);
  131. }
  132. bool Argument::hasInRegAttr() const {
  133. return hasAttribute(Attribute::InReg);
  134. }
  135. bool Argument::hasReturnedAttr() const {
  136. return hasAttribute(Attribute::Returned);
  137. }
  138. bool Argument::hasZExtAttr() const {
  139. return hasAttribute(Attribute::ZExt);
  140. }
  141. bool Argument::hasSExtAttr() const {
  142. return hasAttribute(Attribute::SExt);
  143. }
  144. bool Argument::onlyReadsMemory() const {
  145. AttributeList Attrs = getParent()->getAttributes();
  146. return Attrs.hasParamAttribute(getArgNo(), Attribute::ReadOnly) ||
  147. Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone);
  148. }
  149. void Argument::addAttrs(AttrBuilder &B) {
  150. AttributeList AL = getParent()->getAttributes();
  151. AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
  152. getParent()->setAttributes(AL);
  153. }
  154. void Argument::addAttr(Attribute::AttrKind Kind) {
  155. getParent()->addParamAttr(getArgNo(), Kind);
  156. }
  157. void Argument::addAttr(Attribute Attr) {
  158. getParent()->addParamAttr(getArgNo(), Attr);
  159. }
  160. void Argument::removeAttr(Attribute::AttrKind Kind) {
  161. getParent()->removeParamAttr(getArgNo(), Kind);
  162. }
  163. bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
  164. return getParent()->hasParamAttribute(getArgNo(), Kind);
  165. }
  166. //===----------------------------------------------------------------------===//
  167. // Helper Methods in Function
  168. //===----------------------------------------------------------------------===//
  169. LLVMContext &Function::getContext() const {
  170. return getType()->getContext();
  171. }
  172. unsigned Function::getInstructionCount() const {
  173. unsigned NumInstrs = 0;
  174. for (const BasicBlock &BB : BasicBlocks)
  175. NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(),
  176. BB.instructionsWithoutDebug().end());
  177. return NumInstrs;
  178. }
  179. Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
  180. const Twine &N, Module &M) {
  181. return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
  182. }
  183. void Function::removeFromParent() {
  184. getParent()->getFunctionList().remove(getIterator());
  185. }
  186. void Function::eraseFromParent() {
  187. getParent()->getFunctionList().erase(getIterator());
  188. }
  189. //===----------------------------------------------------------------------===//
  190. // Function Implementation
  191. //===----------------------------------------------------------------------===//
  192. static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) {
  193. // If AS == -1 and we are passed a valid module pointer we place the function
  194. // in the program address space. Otherwise we default to AS0.
  195. if (AddrSpace == static_cast<unsigned>(-1))
  196. return M ? M->getDataLayout().getProgramAddressSpace() : 0;
  197. return AddrSpace;
  198. }
  199. Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
  200. const Twine &name, Module *ParentModule)
  201. : GlobalObject(Ty, Value::FunctionVal,
  202. OperandTraits<Function>::op_begin(this), 0, Linkage, name,
  203. computeAddrSpace(AddrSpace, ParentModule)),
  204. NumArgs(Ty->getNumParams()) {
  205. assert(FunctionType::isValidReturnType(getReturnType()) &&
  206. "invalid return type");
  207. setGlobalObjectSubClassData(0);
  208. // We only need a symbol table for a function if the context keeps value names
  209. if (!getContext().shouldDiscardValueNames())
  210. SymTab = make_unique<ValueSymbolTable>();
  211. // If the function has arguments, mark them as lazily built.
  212. if (Ty->getNumParams())
  213. setValueSubclassData(1); // Set the "has lazy arguments" bit.
  214. if (ParentModule)
  215. ParentModule->getFunctionList().push_back(this);
  216. HasLLVMReservedName = getName().startswith("llvm.");
  217. // Ensure intrinsics have the right parameter attributes.
  218. // Note, the IntID field will have been set in Value::setName if this function
  219. // name is a valid intrinsic ID.
  220. if (IntID)
  221. setAttributes(Intrinsic::getAttributes(getContext(), IntID));
  222. }
  223. Function::~Function() {
  224. dropAllReferences(); // After this it is safe to delete instructions.
  225. // Delete all of the method arguments and unlink from symbol table...
  226. if (Arguments)
  227. clearArguments();
  228. // Remove the function from the on-the-side GC table.
  229. clearGC();
  230. }
  231. void Function::BuildLazyArguments() const {
  232. // Create the arguments vector, all arguments start out unnamed.
  233. auto *FT = getFunctionType();
  234. if (NumArgs > 0) {
  235. Arguments = std::allocator<Argument>().allocate(NumArgs);
  236. for (unsigned i = 0, e = NumArgs; i != e; ++i) {
  237. Type *ArgTy = FT->getParamType(i);
  238. assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
  239. new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
  240. }
  241. }
  242. // Clear the lazy arguments bit.
  243. unsigned SDC = getSubclassDataFromValue();
  244. const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0));
  245. assert(!hasLazyArguments());
  246. }
  247. static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
  248. return MutableArrayRef<Argument>(Args, Count);
  249. }
  250. void Function::clearArguments() {
  251. for (Argument &A : makeArgArray(Arguments, NumArgs)) {
  252. A.setName("");
  253. A.~Argument();
  254. }
  255. std::allocator<Argument>().deallocate(Arguments, NumArgs);
  256. Arguments = nullptr;
  257. }
  258. void Function::stealArgumentListFrom(Function &Src) {
  259. assert(isDeclaration() && "Expected no references to current arguments");
  260. // Drop the current arguments, if any, and set the lazy argument bit.
  261. if (!hasLazyArguments()) {
  262. assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
  263. [](const Argument &A) { return A.use_empty(); }) &&
  264. "Expected arguments to be unused in declaration");
  265. clearArguments();
  266. setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
  267. }
  268. // Nothing to steal if Src has lazy arguments.
  269. if (Src.hasLazyArguments())
  270. return;
  271. // Steal arguments from Src, and fix the lazy argument bits.
  272. assert(arg_size() == Src.arg_size());
  273. Arguments = Src.Arguments;
  274. Src.Arguments = nullptr;
  275. for (Argument &A : makeArgArray(Arguments, NumArgs)) {
  276. // FIXME: This does the work of transferNodesFromList inefficiently.
  277. SmallString<128> Name;
  278. if (A.hasName())
  279. Name = A.getName();
  280. if (!Name.empty())
  281. A.setName("");
  282. A.setParent(this);
  283. if (!Name.empty())
  284. A.setName(Name);
  285. }
  286. setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
  287. assert(!hasLazyArguments());
  288. Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
  289. }
  290. // dropAllReferences() - This function causes all the subinstructions to "let
  291. // go" of all references that they are maintaining. This allows one to
  292. // 'delete' a whole class at a time, even though there may be circular
  293. // references... first all references are dropped, and all use counts go to
  294. // zero. Then everything is deleted for real. Note that no operations are
  295. // valid on an object that has "dropped all references", except operator
  296. // delete.
  297. //
  298. void Function::dropAllReferences() {
  299. setIsMaterializable(false);
  300. for (BasicBlock &BB : *this)
  301. BB.dropAllReferences();
  302. // Delete all basic blocks. They are now unused, except possibly by
  303. // blockaddresses, but BasicBlock's destructor takes care of those.
  304. while (!BasicBlocks.empty())
  305. BasicBlocks.begin()->eraseFromParent();
  306. // Drop uses of any optional data (real or placeholder).
  307. if (getNumOperands()) {
  308. User::dropAllReferences();
  309. setNumHungOffUseOperands(0);
  310. setValueSubclassData(getSubclassDataFromValue() & ~0xe);
  311. }
  312. // Metadata is stored in a side-table.
  313. clearMetadata();
  314. }
  315. void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) {
  316. AttributeList PAL = getAttributes();
  317. PAL = PAL.addAttribute(getContext(), i, Kind);
  318. setAttributes(PAL);
  319. }
  320. void Function::addAttribute(unsigned i, Attribute Attr) {
  321. AttributeList PAL = getAttributes();
  322. PAL = PAL.addAttribute(getContext(), i, Attr);
  323. setAttributes(PAL);
  324. }
  325. void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) {
  326. AttributeList PAL = getAttributes();
  327. PAL = PAL.addAttributes(getContext(), i, Attrs);
  328. setAttributes(PAL);
  329. }
  330. void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
  331. AttributeList PAL = getAttributes();
  332. PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
  333. setAttributes(PAL);
  334. }
  335. void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
  336. AttributeList PAL = getAttributes();
  337. PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
  338. setAttributes(PAL);
  339. }
  340. void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
  341. AttributeList PAL = getAttributes();
  342. PAL = PAL.addParamAttributes(getContext(), ArgNo, Attrs);
  343. setAttributes(PAL);
  344. }
  345. void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
  346. AttributeList PAL = getAttributes();
  347. PAL = PAL.removeAttribute(getContext(), i, Kind);
  348. setAttributes(PAL);
  349. }
  350. void Function::removeAttribute(unsigned i, StringRef Kind) {
  351. AttributeList PAL = getAttributes();
  352. PAL = PAL.removeAttribute(getContext(), i, Kind);
  353. setAttributes(PAL);
  354. }
  355. void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
  356. AttributeList PAL = getAttributes();
  357. PAL = PAL.removeAttributes(getContext(), i, Attrs);
  358. setAttributes(PAL);
  359. }
  360. void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
  361. AttributeList PAL = getAttributes();
  362. PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
  363. setAttributes(PAL);
  364. }
  365. void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
  366. AttributeList PAL = getAttributes();
  367. PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
  368. setAttributes(PAL);
  369. }
  370. void Function::removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
  371. AttributeList PAL = getAttributes();
  372. PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs);
  373. setAttributes(PAL);
  374. }
  375. void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
  376. AttributeList PAL = getAttributes();
  377. PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
  378. setAttributes(PAL);
  379. }
  380. void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
  381. AttributeList PAL = getAttributes();
  382. PAL = PAL.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
  383. setAttributes(PAL);
  384. }
  385. void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
  386. AttributeList PAL = getAttributes();
  387. PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
  388. setAttributes(PAL);
  389. }
  390. void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
  391. uint64_t Bytes) {
  392. AttributeList PAL = getAttributes();
  393. PAL = PAL.addDereferenceableOrNullParamAttr(getContext(), ArgNo, Bytes);
  394. setAttributes(PAL);
  395. }
  396. const std::string &Function::getGC() const {
  397. assert(hasGC() && "Function has no collector");
  398. return getContext().getGC(*this);
  399. }
  400. void Function::setGC(std::string Str) {
  401. setValueSubclassDataBit(14, !Str.empty());
  402. getContext().setGC(*this, std::move(Str));
  403. }
  404. void Function::clearGC() {
  405. if (!hasGC())
  406. return;
  407. getContext().deleteGC(*this);
  408. setValueSubclassDataBit(14, false);
  409. }
  410. /// Copy all additional attributes (those not needed to create a Function) from
  411. /// the Function Src to this one.
  412. void Function::copyAttributesFrom(const Function *Src) {
  413. GlobalObject::copyAttributesFrom(Src);
  414. setCallingConv(Src->getCallingConv());
  415. setAttributes(Src->getAttributes());
  416. if (Src->hasGC())
  417. setGC(Src->getGC());
  418. else
  419. clearGC();
  420. if (Src->hasPersonalityFn())
  421. setPersonalityFn(Src->getPersonalityFn());
  422. if (Src->hasPrefixData())
  423. setPrefixData(Src->getPrefixData());
  424. if (Src->hasPrologueData())
  425. setPrologueData(Src->getPrologueData());
  426. }
  427. /// Table of string intrinsic names indexed by enum value.
  428. static const char * const IntrinsicNameTable[] = {
  429. "not_intrinsic",
  430. #define GET_INTRINSIC_NAME_TABLE
  431. #include "llvm/IR/IntrinsicImpl.inc"
  432. #undef GET_INTRINSIC_NAME_TABLE
  433. };
  434. /// Table of per-target intrinsic name tables.
  435. #define GET_INTRINSIC_TARGET_DATA
  436. #include "llvm/IR/IntrinsicImpl.inc"
  437. #undef GET_INTRINSIC_TARGET_DATA
  438. /// Find the segment of \c IntrinsicNameTable for intrinsics with the same
  439. /// target as \c Name, or the generic table if \c Name is not target specific.
  440. ///
  441. /// Returns the relevant slice of \c IntrinsicNameTable
  442. static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
  443. assert(Name.startswith("llvm."));
  444. ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
  445. // Drop "llvm." and take the first dotted component. That will be the target
  446. // if this is target specific.
  447. StringRef Target = Name.drop_front(5).split('.').first;
  448. auto It = std::lower_bound(Targets.begin(), Targets.end(), Target,
  449. [](const IntrinsicTargetInfo &TI,
  450. StringRef Target) { return TI.Name < Target; });
  451. // We've either found the target or just fall back to the generic set, which
  452. // is always first.
  453. const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
  454. return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
  455. }
  456. /// This does the actual lookup of an intrinsic ID which
  457. /// matches the given function name.
  458. Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
  459. ArrayRef<const char *> NameTable = findTargetSubtable(Name);
  460. int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
  461. if (Idx == -1)
  462. return Intrinsic::not_intrinsic;
  463. // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
  464. // an index into a sub-table.
  465. int Adjust = NameTable.data() - IntrinsicNameTable;
  466. Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
  467. // If the intrinsic is not overloaded, require an exact match. If it is
  468. // overloaded, require either exact or prefix match.
  469. const auto MatchSize = strlen(NameTable[Idx]);
  470. assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
  471. bool IsExactMatch = Name.size() == MatchSize;
  472. return IsExactMatch || isOverloaded(ID) ? ID : Intrinsic::not_intrinsic;
  473. }
  474. void Function::recalculateIntrinsicID() {
  475. StringRef Name = getName();
  476. if (!Name.startswith("llvm.")) {
  477. HasLLVMReservedName = false;
  478. IntID = Intrinsic::not_intrinsic;
  479. return;
  480. }
  481. HasLLVMReservedName = true;
  482. IntID = lookupIntrinsicID(Name);
  483. }
  484. /// Returns a stable mangling for the type specified for use in the name
  485. /// mangling scheme used by 'any' types in intrinsic signatures. The mangling
  486. /// of named types is simply their name. Manglings for unnamed types consist
  487. /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
  488. /// combined with the mangling of their component types. A vararg function
  489. /// type will have a suffix of 'vararg'. Since function types can contain
  490. /// other function types, we close a function type mangling with suffix 'f'
  491. /// which can't be confused with it's prefix. This ensures we don't have
  492. /// collisions between two unrelated function types. Otherwise, you might
  493. /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.)
  494. ///
  495. static std::string getMangledTypeStr(Type* Ty) {
  496. std::string Result;
  497. if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
  498. Result += "p" + utostr(PTyp->getAddressSpace()) +
  499. getMangledTypeStr(PTyp->getElementType());
  500. } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) {
  501. Result += "a" + utostr(ATyp->getNumElements()) +
  502. getMangledTypeStr(ATyp->getElementType());
  503. } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
  504. if (!STyp->isLiteral()) {
  505. Result += "s_";
  506. Result += STyp->getName();
  507. } else {
  508. Result += "sl_";
  509. for (auto Elem : STyp->elements())
  510. Result += getMangledTypeStr(Elem);
  511. }
  512. // Ensure nested structs are distinguishable.
  513. Result += "s";
  514. } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
  515. Result += "f_" + getMangledTypeStr(FT->getReturnType());
  516. for (size_t i = 0; i < FT->getNumParams(); i++)
  517. Result += getMangledTypeStr(FT->getParamType(i));
  518. if (FT->isVarArg())
  519. Result += "vararg";
  520. // Ensure nested function types are distinguishable.
  521. Result += "f";
  522. } else if (isa<VectorType>(Ty)) {
  523. Result += "v" + utostr(Ty->getVectorNumElements()) +
  524. getMangledTypeStr(Ty->getVectorElementType());
  525. } else if (Ty) {
  526. switch (Ty->getTypeID()) {
  527. default: llvm_unreachable("Unhandled type");
  528. case Type::VoidTyID: Result += "isVoid"; break;
  529. case Type::MetadataTyID: Result += "Metadata"; break;
  530. case Type::HalfTyID: Result += "f16"; break;
  531. case Type::FloatTyID: Result += "f32"; break;
  532. case Type::DoubleTyID: Result += "f64"; break;
  533. case Type::X86_FP80TyID: Result += "f80"; break;
  534. case Type::FP128TyID: Result += "f128"; break;
  535. case Type::PPC_FP128TyID: Result += "ppcf128"; break;
  536. case Type::X86_MMXTyID: Result += "x86mmx"; break;
  537. case Type::IntegerTyID:
  538. Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
  539. break;
  540. }
  541. }
  542. return Result;
  543. }
  544. StringRef Intrinsic::getName(ID id) {
  545. assert(id < num_intrinsics && "Invalid intrinsic ID!");
  546. assert(!isOverloaded(id) &&
  547. "This version of getName does not support overloading");
  548. return IntrinsicNameTable[id];
  549. }
  550. std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
  551. assert(id < num_intrinsics && "Invalid intrinsic ID!");
  552. std::string Result(IntrinsicNameTable[id]);
  553. for (Type *Ty : Tys) {
  554. Result += "." + getMangledTypeStr(Ty);
  555. }
  556. return Result;
  557. }
  558. /// IIT_Info - These are enumerators that describe the entries returned by the
  559. /// getIntrinsicInfoTableEntries function.
  560. ///
  561. /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
  562. enum IIT_Info {
  563. // Common values should be encoded with 0-15.
  564. IIT_Done = 0,
  565. IIT_I1 = 1,
  566. IIT_I8 = 2,
  567. IIT_I16 = 3,
  568. IIT_I32 = 4,
  569. IIT_I64 = 5,
  570. IIT_F16 = 6,
  571. IIT_F32 = 7,
  572. IIT_F64 = 8,
  573. IIT_V2 = 9,
  574. IIT_V4 = 10,
  575. IIT_V8 = 11,
  576. IIT_V16 = 12,
  577. IIT_V32 = 13,
  578. IIT_PTR = 14,
  579. IIT_ARG = 15,
  580. // Values from 16+ are only encodable with the inefficient encoding.
  581. IIT_V64 = 16,
  582. IIT_MMX = 17,
  583. IIT_TOKEN = 18,
  584. IIT_METADATA = 19,
  585. IIT_EMPTYSTRUCT = 20,
  586. IIT_STRUCT2 = 21,
  587. IIT_STRUCT3 = 22,
  588. IIT_STRUCT4 = 23,
  589. IIT_STRUCT5 = 24,
  590. IIT_EXTEND_ARG = 25,
  591. IIT_TRUNC_ARG = 26,
  592. IIT_ANYPTR = 27,
  593. IIT_V1 = 28,
  594. IIT_VARARG = 29,
  595. IIT_HALF_VEC_ARG = 30,
  596. IIT_SAME_VEC_WIDTH_ARG = 31,
  597. IIT_PTR_TO_ARG = 32,
  598. IIT_PTR_TO_ELT = 33,
  599. IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
  600. IIT_I128 = 35,
  601. IIT_V512 = 36,
  602. IIT_V1024 = 37,
  603. IIT_STRUCT6 = 38,
  604. IIT_STRUCT7 = 39,
  605. IIT_STRUCT8 = 40,
  606. IIT_F128 = 41
  607. };
  608. static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
  609. SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
  610. using namespace Intrinsic;
  611. IIT_Info Info = IIT_Info(Infos[NextElt++]);
  612. unsigned StructElts = 2;
  613. switch (Info) {
  614. case IIT_Done:
  615. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
  616. return;
  617. case IIT_VARARG:
  618. OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
  619. return;
  620. case IIT_MMX:
  621. OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
  622. return;
  623. case IIT_TOKEN:
  624. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
  625. return;
  626. case IIT_METADATA:
  627. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
  628. return;
  629. case IIT_F16:
  630. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
  631. return;
  632. case IIT_F32:
  633. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
  634. return;
  635. case IIT_F64:
  636. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
  637. return;
  638. case IIT_F128:
  639. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0));
  640. return;
  641. case IIT_I1:
  642. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
  643. return;
  644. case IIT_I8:
  645. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
  646. return;
  647. case IIT_I16:
  648. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
  649. return;
  650. case IIT_I32:
  651. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
  652. return;
  653. case IIT_I64:
  654. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
  655. return;
  656. case IIT_I128:
  657. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
  658. return;
  659. case IIT_V1:
  660. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
  661. DecodeIITType(NextElt, Infos, OutputTable);
  662. return;
  663. case IIT_V2:
  664. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
  665. DecodeIITType(NextElt, Infos, OutputTable);
  666. return;
  667. case IIT_V4:
  668. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
  669. DecodeIITType(NextElt, Infos, OutputTable);
  670. return;
  671. case IIT_V8:
  672. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
  673. DecodeIITType(NextElt, Infos, OutputTable);
  674. return;
  675. case IIT_V16:
  676. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
  677. DecodeIITType(NextElt, Infos, OutputTable);
  678. return;
  679. case IIT_V32:
  680. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
  681. DecodeIITType(NextElt, Infos, OutputTable);
  682. return;
  683. case IIT_V64:
  684. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
  685. DecodeIITType(NextElt, Infos, OutputTable);
  686. return;
  687. case IIT_V512:
  688. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512));
  689. DecodeIITType(NextElt, Infos, OutputTable);
  690. return;
  691. case IIT_V1024:
  692. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024));
  693. DecodeIITType(NextElt, Infos, OutputTable);
  694. return;
  695. case IIT_PTR:
  696. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
  697. DecodeIITType(NextElt, Infos, OutputTable);
  698. return;
  699. case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
  700. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
  701. Infos[NextElt++]));
  702. DecodeIITType(NextElt, Infos, OutputTable);
  703. return;
  704. }
  705. case IIT_ARG: {
  706. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  707. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
  708. return;
  709. }
  710. case IIT_EXTEND_ARG: {
  711. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  712. OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
  713. ArgInfo));
  714. return;
  715. }
  716. case IIT_TRUNC_ARG: {
  717. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  718. OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
  719. ArgInfo));
  720. return;
  721. }
  722. case IIT_HALF_VEC_ARG: {
  723. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  724. OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
  725. ArgInfo));
  726. return;
  727. }
  728. case IIT_SAME_VEC_WIDTH_ARG: {
  729. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  730. OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
  731. ArgInfo));
  732. return;
  733. }
  734. case IIT_PTR_TO_ARG: {
  735. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  736. OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
  737. ArgInfo));
  738. return;
  739. }
  740. case IIT_PTR_TO_ELT: {
  741. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  742. OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToElt, ArgInfo));
  743. return;
  744. }
  745. case IIT_VEC_OF_ANYPTRS_TO_ELT: {
  746. unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  747. unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  748. OutputTable.push_back(
  749. IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo));
  750. return;
  751. }
  752. case IIT_EMPTYSTRUCT:
  753. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
  754. return;
  755. case IIT_STRUCT8: ++StructElts; LLVM_FALLTHROUGH;
  756. case IIT_STRUCT7: ++StructElts; LLVM_FALLTHROUGH;
  757. case IIT_STRUCT6: ++StructElts; LLVM_FALLTHROUGH;
  758. case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH;
  759. case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH;
  760. case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH;
  761. case IIT_STRUCT2: {
  762. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
  763. for (unsigned i = 0; i != StructElts; ++i)
  764. DecodeIITType(NextElt, Infos, OutputTable);
  765. return;
  766. }
  767. }
  768. llvm_unreachable("unhandled");
  769. }
  770. #define GET_INTRINSIC_GENERATOR_GLOBAL
  771. #include "llvm/IR/IntrinsicImpl.inc"
  772. #undef GET_INTRINSIC_GENERATOR_GLOBAL
  773. void Intrinsic::getIntrinsicInfoTableEntries(ID id,
  774. SmallVectorImpl<IITDescriptor> &T){
  775. // Check to see if the intrinsic's type was expressible by the table.
  776. unsigned TableVal = IIT_Table[id-1];
  777. // Decode the TableVal into an array of IITValues.
  778. SmallVector<unsigned char, 8> IITValues;
  779. ArrayRef<unsigned char> IITEntries;
  780. unsigned NextElt = 0;
  781. if ((TableVal >> 31) != 0) {
  782. // This is an offset into the IIT_LongEncodingTable.
  783. IITEntries = IIT_LongEncodingTable;
  784. // Strip sentinel bit.
  785. NextElt = (TableVal << 1) >> 1;
  786. } else {
  787. // Decode the TableVal into an array of IITValues. If the entry was encoded
  788. // into a single word in the table itself, decode it now.
  789. do {
  790. IITValues.push_back(TableVal & 0xF);
  791. TableVal >>= 4;
  792. } while (TableVal);
  793. IITEntries = IITValues;
  794. NextElt = 0;
  795. }
  796. // Okay, decode the table into the output vector of IITDescriptors.
  797. DecodeIITType(NextElt, IITEntries, T);
  798. while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
  799. DecodeIITType(NextElt, IITEntries, T);
  800. }
  801. static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
  802. ArrayRef<Type*> Tys, LLVMContext &Context) {
  803. using namespace Intrinsic;
  804. IITDescriptor D = Infos.front();
  805. Infos = Infos.slice(1);
  806. switch (D.Kind) {
  807. case IITDescriptor::Void: return Type::getVoidTy(Context);
  808. case IITDescriptor::VarArg: return Type::getVoidTy(Context);
  809. case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
  810. case IITDescriptor::Token: return Type::getTokenTy(Context);
  811. case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
  812. case IITDescriptor::Half: return Type::getHalfTy(Context);
  813. case IITDescriptor::Float: return Type::getFloatTy(Context);
  814. case IITDescriptor::Double: return Type::getDoubleTy(Context);
  815. case IITDescriptor::Quad: return Type::getFP128Ty(Context);
  816. case IITDescriptor::Integer:
  817. return IntegerType::get(Context, D.Integer_Width);
  818. case IITDescriptor::Vector:
  819. return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
  820. case IITDescriptor::Pointer:
  821. return PointerType::get(DecodeFixedType(Infos, Tys, Context),
  822. D.Pointer_AddressSpace);
  823. case IITDescriptor::Struct: {
  824. SmallVector<Type *, 8> Elts;
  825. for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
  826. Elts.push_back(DecodeFixedType(Infos, Tys, Context));
  827. return StructType::get(Context, Elts);
  828. }
  829. case IITDescriptor::Argument:
  830. return Tys[D.getArgumentNumber()];
  831. case IITDescriptor::ExtendArgument: {
  832. Type *Ty = Tys[D.getArgumentNumber()];
  833. if (VectorType *VTy = dyn_cast<VectorType>(Ty))
  834. return VectorType::getExtendedElementVectorType(VTy);
  835. return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
  836. }
  837. case IITDescriptor::TruncArgument: {
  838. Type *Ty = Tys[D.getArgumentNumber()];
  839. if (VectorType *VTy = dyn_cast<VectorType>(Ty))
  840. return VectorType::getTruncatedElementVectorType(VTy);
  841. IntegerType *ITy = cast<IntegerType>(Ty);
  842. assert(ITy->getBitWidth() % 2 == 0);
  843. return IntegerType::get(Context, ITy->getBitWidth() / 2);
  844. }
  845. case IITDescriptor::HalfVecArgument:
  846. return VectorType::getHalfElementsVectorType(cast<VectorType>(
  847. Tys[D.getArgumentNumber()]));
  848. case IITDescriptor::SameVecWidthArgument: {
  849. Type *EltTy = DecodeFixedType(Infos, Tys, Context);
  850. Type *Ty = Tys[D.getArgumentNumber()];
  851. if (auto *VTy = dyn_cast<VectorType>(Ty))
  852. return VectorType::get(EltTy, VTy->getNumElements());
  853. return EltTy;
  854. }
  855. case IITDescriptor::PtrToArgument: {
  856. Type *Ty = Tys[D.getArgumentNumber()];
  857. return PointerType::getUnqual(Ty);
  858. }
  859. case IITDescriptor::PtrToElt: {
  860. Type *Ty = Tys[D.getArgumentNumber()];
  861. VectorType *VTy = dyn_cast<VectorType>(Ty);
  862. if (!VTy)
  863. llvm_unreachable("Expected an argument of Vector Type");
  864. Type *EltTy = VTy->getVectorElementType();
  865. return PointerType::getUnqual(EltTy);
  866. }
  867. case IITDescriptor::VecOfAnyPtrsToElt:
  868. // Return the overloaded type (which determines the pointers address space)
  869. return Tys[D.getOverloadArgNumber()];
  870. }
  871. llvm_unreachable("unhandled");
  872. }
  873. FunctionType *Intrinsic::getType(LLVMContext &Context,
  874. ID id, ArrayRef<Type*> Tys) {
  875. SmallVector<IITDescriptor, 8> Table;
  876. getIntrinsicInfoTableEntries(id, Table);
  877. ArrayRef<IITDescriptor> TableRef = Table;
  878. Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
  879. SmallVector<Type*, 8> ArgTys;
  880. while (!TableRef.empty())
  881. ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
  882. // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
  883. // If we see void type as the type of the last argument, it is vararg intrinsic
  884. if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
  885. ArgTys.pop_back();
  886. return FunctionType::get(ResultTy, ArgTys, true);
  887. }
  888. return FunctionType::get(ResultTy, ArgTys, false);
  889. }
  890. bool Intrinsic::isOverloaded(ID id) {
  891. #define GET_INTRINSIC_OVERLOAD_TABLE
  892. #include "llvm/IR/IntrinsicImpl.inc"
  893. #undef GET_INTRINSIC_OVERLOAD_TABLE
  894. }
  895. bool Intrinsic::isLeaf(ID id) {
  896. switch (id) {
  897. default:
  898. return true;
  899. case Intrinsic::experimental_gc_statepoint:
  900. case Intrinsic::experimental_patchpoint_void:
  901. case Intrinsic::experimental_patchpoint_i64:
  902. return false;
  903. }
  904. }
  905. /// This defines the "Intrinsic::getAttributes(ID id)" method.
  906. #define GET_INTRINSIC_ATTRIBUTES
  907. #include "llvm/IR/IntrinsicImpl.inc"
  908. #undef GET_INTRINSIC_ATTRIBUTES
  909. Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
  910. // There can never be multiple globals with the same name of different types,
  911. // because intrinsics must be a specific type.
  912. return cast<Function>(
  913. M->getOrInsertFunction(getName(id, Tys),
  914. getType(M->getContext(), id, Tys))
  915. .getCallee());
  916. }
  917. // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
  918. #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
  919. #include "llvm/IR/IntrinsicImpl.inc"
  920. #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
  921. // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
  922. #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
  923. #include "llvm/IR/IntrinsicImpl.inc"
  924. #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
  925. bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
  926. SmallVectorImpl<Type*> &ArgTys) {
  927. using namespace Intrinsic;
  928. // If we ran out of descriptors, there are too many arguments.
  929. if (Infos.empty()) return true;
  930. IITDescriptor D = Infos.front();
  931. Infos = Infos.slice(1);
  932. switch (D.Kind) {
  933. case IITDescriptor::Void: return !Ty->isVoidTy();
  934. case IITDescriptor::VarArg: return true;
  935. case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
  936. case IITDescriptor::Token: return !Ty->isTokenTy();
  937. case IITDescriptor::Metadata: return !Ty->isMetadataTy();
  938. case IITDescriptor::Half: return !Ty->isHalfTy();
  939. case IITDescriptor::Float: return !Ty->isFloatTy();
  940. case IITDescriptor::Double: return !Ty->isDoubleTy();
  941. case IITDescriptor::Quad: return !Ty->isFP128Ty();
  942. case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
  943. case IITDescriptor::Vector: {
  944. VectorType *VT = dyn_cast<VectorType>(Ty);
  945. return !VT || VT->getNumElements() != D.Vector_Width ||
  946. matchIntrinsicType(VT->getElementType(), Infos, ArgTys);
  947. }
  948. case IITDescriptor::Pointer: {
  949. PointerType *PT = dyn_cast<PointerType>(Ty);
  950. return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace ||
  951. matchIntrinsicType(PT->getElementType(), Infos, ArgTys);
  952. }
  953. case IITDescriptor::Struct: {
  954. StructType *ST = dyn_cast<StructType>(Ty);
  955. if (!ST || ST->getNumElements() != D.Struct_NumElements)
  956. return true;
  957. for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
  958. if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys))
  959. return true;
  960. return false;
  961. }
  962. case IITDescriptor::Argument:
  963. // Two cases here - If this is the second occurrence of an argument, verify
  964. // that the later instance matches the previous instance.
  965. if (D.getArgumentNumber() < ArgTys.size())
  966. return Ty != ArgTys[D.getArgumentNumber()];
  967. // Otherwise, if this is the first instance of an argument, record it and
  968. // verify the "Any" kind.
  969. assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
  970. ArgTys.push_back(Ty);
  971. switch (D.getArgumentKind()) {
  972. case IITDescriptor::AK_Any: return false; // Success
  973. case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
  974. case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
  975. case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
  976. case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
  977. }
  978. llvm_unreachable("all argument kinds not covered");
  979. case IITDescriptor::ExtendArgument: {
  980. // This may only be used when referring to a previous vector argument.
  981. if (D.getArgumentNumber() >= ArgTys.size())
  982. return true;
  983. Type *NewTy = ArgTys[D.getArgumentNumber()];
  984. if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
  985. NewTy = VectorType::getExtendedElementVectorType(VTy);
  986. else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
  987. NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
  988. else
  989. return true;
  990. return Ty != NewTy;
  991. }
  992. case IITDescriptor::TruncArgument: {
  993. // This may only be used when referring to a previous vector argument.
  994. if (D.getArgumentNumber() >= ArgTys.size())
  995. return true;
  996. Type *NewTy = ArgTys[D.getArgumentNumber()];
  997. if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
  998. NewTy = VectorType::getTruncatedElementVectorType(VTy);
  999. else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
  1000. NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
  1001. else
  1002. return true;
  1003. return Ty != NewTy;
  1004. }
  1005. case IITDescriptor::HalfVecArgument:
  1006. // This may only be used when referring to a previous vector argument.
  1007. return D.getArgumentNumber() >= ArgTys.size() ||
  1008. !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
  1009. VectorType::getHalfElementsVectorType(
  1010. cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
  1011. case IITDescriptor::SameVecWidthArgument: {
  1012. if (D.getArgumentNumber() >= ArgTys.size())
  1013. return true;
  1014. auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
  1015. auto *ThisArgType = dyn_cast<VectorType>(Ty);
  1016. // Both must be vectors of the same number of elements or neither.
  1017. if ((ReferenceType != nullptr) != (ThisArgType != nullptr))
  1018. return true;
  1019. Type *EltTy = Ty;
  1020. if (ThisArgType) {
  1021. if (ReferenceType->getVectorNumElements() !=
  1022. ThisArgType->getVectorNumElements())
  1023. return true;
  1024. EltTy = ThisArgType->getVectorElementType();
  1025. }
  1026. return matchIntrinsicType(EltTy, Infos, ArgTys);
  1027. }
  1028. case IITDescriptor::PtrToArgument: {
  1029. if (D.getArgumentNumber() >= ArgTys.size())
  1030. return true;
  1031. Type * ReferenceType = ArgTys[D.getArgumentNumber()];
  1032. PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
  1033. return (!ThisArgType || ThisArgType->getElementType() != ReferenceType);
  1034. }
  1035. case IITDescriptor::PtrToElt: {
  1036. if (D.getArgumentNumber() >= ArgTys.size())
  1037. return true;
  1038. VectorType * ReferenceType =
  1039. dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
  1040. PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
  1041. return (!ThisArgType || !ReferenceType ||
  1042. ThisArgType->getElementType() != ReferenceType->getElementType());
  1043. }
  1044. case IITDescriptor::VecOfAnyPtrsToElt: {
  1045. unsigned RefArgNumber = D.getRefArgNumber();
  1046. // This may only be used when referring to a previous argument.
  1047. if (RefArgNumber >= ArgTys.size())
  1048. return true;
  1049. // Record the overloaded type
  1050. assert(D.getOverloadArgNumber() == ArgTys.size() &&
  1051. "Table consistency error");
  1052. ArgTys.push_back(Ty);
  1053. // Verify the overloaded type "matches" the Ref type.
  1054. // i.e. Ty is a vector with the same width as Ref.
  1055. // Composed of pointers to the same element type as Ref.
  1056. VectorType *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
  1057. VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty);
  1058. if (!ThisArgVecTy || !ReferenceType ||
  1059. (ReferenceType->getVectorNumElements() !=
  1060. ThisArgVecTy->getVectorNumElements()))
  1061. return true;
  1062. PointerType *ThisArgEltTy =
  1063. dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType());
  1064. if (!ThisArgEltTy)
  1065. return true;
  1066. return ThisArgEltTy->getElementType() !=
  1067. ReferenceType->getVectorElementType();
  1068. }
  1069. }
  1070. llvm_unreachable("unhandled");
  1071. }
  1072. bool
  1073. Intrinsic::matchIntrinsicVarArg(bool isVarArg,
  1074. ArrayRef<Intrinsic::IITDescriptor> &Infos) {
  1075. // If there are no descriptors left, then it can't be a vararg.
  1076. if (Infos.empty())
  1077. return isVarArg;
  1078. // There should be only one descriptor remaining at this point.
  1079. if (Infos.size() != 1)
  1080. return true;
  1081. // Check and verify the descriptor.
  1082. IITDescriptor D = Infos.front();
  1083. Infos = Infos.slice(1);
  1084. if (D.Kind == IITDescriptor::VarArg)
  1085. return !isVarArg;
  1086. return true;
  1087. }
  1088. Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) {
  1089. Intrinsic::ID ID = F->getIntrinsicID();
  1090. if (!ID)
  1091. return None;
  1092. FunctionType *FTy = F->getFunctionType();
  1093. // Accumulate an array of overloaded types for the given intrinsic
  1094. SmallVector<Type *, 4> ArgTys;
  1095. {
  1096. SmallVector<Intrinsic::IITDescriptor, 8> Table;
  1097. getIntrinsicInfoTableEntries(ID, Table);
  1098. ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
  1099. // If we encounter any problems matching the signature with the descriptor
  1100. // just give up remangling. It's up to verifier to report the discrepancy.
  1101. if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
  1102. return None;
  1103. for (auto Ty : FTy->params())
  1104. if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
  1105. return None;
  1106. if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
  1107. return None;
  1108. }
  1109. StringRef Name = F->getName();
  1110. if (Name == Intrinsic::getName(ID, ArgTys))
  1111. return None;
  1112. auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
  1113. NewDecl->setCallingConv(F->getCallingConv());
  1114. assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
  1115. return NewDecl;
  1116. }
  1117. /// hasAddressTaken - returns true if there are any uses of this function
  1118. /// other than direct calls or invokes to it.
  1119. bool Function::hasAddressTaken(const User* *PutOffender) const {
  1120. for (const Use &U : uses()) {
  1121. const User *FU = U.getUser();
  1122. if (isa<BlockAddress>(FU))
  1123. continue;
  1124. const auto *Call = dyn_cast<CallBase>(FU);
  1125. if (!Call) {
  1126. if (PutOffender)
  1127. *PutOffender = FU;
  1128. return true;
  1129. }
  1130. if (!Call->isCallee(&U)) {
  1131. if (PutOffender)
  1132. *PutOffender = FU;
  1133. return true;
  1134. }
  1135. }
  1136. return false;
  1137. }
  1138. bool Function::isDefTriviallyDead() const {
  1139. // Check the linkage
  1140. if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
  1141. !hasAvailableExternallyLinkage())
  1142. return false;
  1143. // Check if the function is used by anything other than a blockaddress.
  1144. for (const User *U : users())
  1145. if (!isa<BlockAddress>(U))
  1146. return false;
  1147. return true;
  1148. }
  1149. /// callsFunctionThatReturnsTwice - Return true if the function has a call to
  1150. /// setjmp or other function that gcc recognizes as "returning twice".
  1151. bool Function::callsFunctionThatReturnsTwice() const {
  1152. for (const Instruction &I : instructions(this))
  1153. if (const auto *Call = dyn_cast<CallBase>(&I))
  1154. if (Call->hasFnAttr(Attribute::ReturnsTwice))
  1155. return true;
  1156. return false;
  1157. }
  1158. Constant *Function::getPersonalityFn() const {
  1159. assert(hasPersonalityFn() && getNumOperands());
  1160. return cast<Constant>(Op<0>());
  1161. }
  1162. void Function::setPersonalityFn(Constant *Fn) {
  1163. setHungoffOperand<0>(Fn);
  1164. setValueSubclassDataBit(3, Fn != nullptr);
  1165. }
  1166. Constant *Function::getPrefixData() const {
  1167. assert(hasPrefixData() && getNumOperands());
  1168. return cast<Constant>(Op<1>());
  1169. }
  1170. void Function::setPrefixData(Constant *PrefixData) {
  1171. setHungoffOperand<1>(PrefixData);
  1172. setValueSubclassDataBit(1, PrefixData != nullptr);
  1173. }
  1174. Constant *Function::getPrologueData() const {
  1175. assert(hasPrologueData() && getNumOperands());
  1176. return cast<Constant>(Op<2>());
  1177. }
  1178. void Function::setPrologueData(Constant *PrologueData) {
  1179. setHungoffOperand<2>(PrologueData);
  1180. setValueSubclassDataBit(2, PrologueData != nullptr);
  1181. }
  1182. void Function::allocHungoffUselist() {
  1183. // If we've already allocated a uselist, stop here.
  1184. if (getNumOperands())
  1185. return;
  1186. allocHungoffUses(3, /*IsPhi=*/ false);
  1187. setNumHungOffUseOperands(3);
  1188. // Initialize the uselist with placeholder operands to allow traversal.
  1189. auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
  1190. Op<0>().set(CPN);
  1191. Op<1>().set(CPN);
  1192. Op<2>().set(CPN);
  1193. }
  1194. template <int Idx>
  1195. void Function::setHungoffOperand(Constant *C) {
  1196. if (C) {
  1197. allocHungoffUselist();
  1198. Op<Idx>().set(C);
  1199. } else if (getNumOperands()) {
  1200. Op<Idx>().set(
  1201. ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
  1202. }
  1203. }
  1204. void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
  1205. assert(Bit < 16 && "SubclassData contains only 16 bits");
  1206. if (On)
  1207. setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
  1208. else
  1209. setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
  1210. }
  1211. void Function::setEntryCount(ProfileCount Count,
  1212. const DenseSet<GlobalValue::GUID> *S) {
  1213. assert(Count.hasValue());
  1214. #if !defined(NDEBUG)
  1215. auto PrevCount = getEntryCount();
  1216. assert(!PrevCount.hasValue() || PrevCount.getType() == Count.getType());
  1217. #endif
  1218. MDBuilder MDB(getContext());
  1219. setMetadata(
  1220. LLVMContext::MD_prof,
  1221. MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S));
  1222. }
  1223. void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type,
  1224. const DenseSet<GlobalValue::GUID> *Imports) {
  1225. setEntryCount(ProfileCount(Count, Type), Imports);
  1226. }
  1227. ProfileCount Function::getEntryCount(bool AllowSynthetic) const {
  1228. MDNode *MD = getMetadata(LLVMContext::MD_prof);
  1229. if (MD && MD->getOperand(0))
  1230. if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
  1231. if (MDS->getString().equals("function_entry_count")) {
  1232. ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
  1233. uint64_t Count = CI->getValue().getZExtValue();
  1234. // A value of -1 is used for SamplePGO when there were no samples.
  1235. // Treat this the same as unknown.
  1236. if (Count == (uint64_t)-1)
  1237. return ProfileCount::getInvalid();
  1238. return ProfileCount(Count, PCT_Real);
  1239. } else if (AllowSynthetic &&
  1240. MDS->getString().equals("synthetic_function_entry_count")) {
  1241. ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
  1242. uint64_t Count = CI->getValue().getZExtValue();
  1243. return ProfileCount(Count, PCT_Synthetic);
  1244. }
  1245. }
  1246. return ProfileCount::getInvalid();
  1247. }
  1248. DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
  1249. DenseSet<GlobalValue::GUID> R;
  1250. if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
  1251. if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
  1252. if (MDS->getString().equals("function_entry_count"))
  1253. for (unsigned i = 2; i < MD->getNumOperands(); i++)
  1254. R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
  1255. ->getValue()
  1256. .getZExtValue());
  1257. return R;
  1258. }
  1259. void Function::setSectionPrefix(StringRef Prefix) {
  1260. MDBuilder MDB(getContext());
  1261. setMetadata(LLVMContext::MD_section_prefix,
  1262. MDB.createFunctionSectionPrefix(Prefix));
  1263. }
  1264. Optional<StringRef> Function::getSectionPrefix() const {
  1265. if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
  1266. assert(cast<MDString>(MD->getOperand(0))
  1267. ->getString()
  1268. .equals("function_section_prefix") &&
  1269. "Metadata not match");
  1270. return cast<MDString>(MD->getOperand(1))->getString();
  1271. }
  1272. return None;
  1273. }
  1274. bool Function::nullPointerIsDefined() const {
  1275. return getFnAttribute("null-pointer-is-valid")
  1276. .getValueAsString()
  1277. .equals("true");
  1278. }
  1279. bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
  1280. if (F && F->nullPointerIsDefined())
  1281. return true;
  1282. if (AS != 0)
  1283. return true;
  1284. return false;
  1285. }