Function.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. //===-- Function.cpp - Implement the Global object classes ----------------===//
  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 file implements the Function class for the IR library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/Function.h"
  14. #include "LLVMContextImpl.h"
  15. #include "SymbolTableListTraitsImpl.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include "llvm/CodeGen/ValueTypes.h"
  20. #include "llvm/IR/CallSite.h"
  21. #include "llvm/IR/DerivedTypes.h"
  22. #include "llvm/IR/InstIterator.h"
  23. #include "llvm/IR/IntrinsicInst.h"
  24. #include "llvm/IR/LLVMContext.h"
  25. #include "llvm/IR/LeakDetector.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/Support/ManagedStatic.h"
  28. #include "llvm/Support/RWMutex.h"
  29. #include "llvm/Support/StringPool.h"
  30. #include "llvm/Support/Threading.h"
  31. using namespace llvm;
  32. // Explicit instantiations of SymbolTableListTraits since some of the methods
  33. // are not in the public header file...
  34. template class llvm::SymbolTableListTraits<Argument, Function>;
  35. template class llvm::SymbolTableListTraits<BasicBlock, Function>;
  36. //===----------------------------------------------------------------------===//
  37. // Argument Implementation
  38. //===----------------------------------------------------------------------===//
  39. void Argument::anchor() { }
  40. Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
  41. : Value(Ty, Value::ArgumentVal) {
  42. Parent = nullptr;
  43. // Make sure that we get added to a function
  44. LeakDetector::addGarbageObject(this);
  45. if (Par)
  46. Par->getArgumentList().push_back(this);
  47. setName(Name);
  48. }
  49. void Argument::setParent(Function *parent) {
  50. if (getParent())
  51. LeakDetector::addGarbageObject(this);
  52. Parent = parent;
  53. if (getParent())
  54. LeakDetector::removeGarbageObject(this);
  55. }
  56. /// getArgNo - Return the index of this formal argument in its containing
  57. /// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
  58. unsigned Argument::getArgNo() const {
  59. const Function *F = getParent();
  60. assert(F && "Argument is not in a function");
  61. Function::const_arg_iterator AI = F->arg_begin();
  62. unsigned ArgIdx = 0;
  63. for (; &*AI != this; ++AI)
  64. ++ArgIdx;
  65. return ArgIdx;
  66. }
  67. /// hasByValAttr - Return true if this argument has the byval attribute on it
  68. /// in its containing function.
  69. bool Argument::hasByValAttr() const {
  70. if (!getType()->isPointerTy()) return false;
  71. return getParent()->getAttributes().
  72. hasAttribute(getArgNo()+1, Attribute::ByVal);
  73. }
  74. /// \brief Return true if this argument has the inalloca attribute on it in
  75. /// its containing function.
  76. bool Argument::hasInAllocaAttr() const {
  77. if (!getType()->isPointerTy()) return false;
  78. return getParent()->getAttributes().
  79. hasAttribute(getArgNo()+1, Attribute::InAlloca);
  80. }
  81. bool Argument::hasByValOrInAllocaAttr() const {
  82. if (!getType()->isPointerTy()) return false;
  83. AttributeSet Attrs = getParent()->getAttributes();
  84. return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) ||
  85. Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca);
  86. }
  87. unsigned Argument::getParamAlignment() const {
  88. assert(getType()->isPointerTy() && "Only pointers have alignments");
  89. return getParent()->getParamAlignment(getArgNo()+1);
  90. }
  91. /// hasNestAttr - Return true if this argument has the nest attribute on
  92. /// it in its containing function.
  93. bool Argument::hasNestAttr() const {
  94. if (!getType()->isPointerTy()) return false;
  95. return getParent()->getAttributes().
  96. hasAttribute(getArgNo()+1, Attribute::Nest);
  97. }
  98. /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
  99. /// it in its containing function.
  100. bool Argument::hasNoAliasAttr() const {
  101. if (!getType()->isPointerTy()) return false;
  102. return getParent()->getAttributes().
  103. hasAttribute(getArgNo()+1, Attribute::NoAlias);
  104. }
  105. /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
  106. /// on it in its containing function.
  107. bool Argument::hasNoCaptureAttr() const {
  108. if (!getType()->isPointerTy()) return false;
  109. return getParent()->getAttributes().
  110. hasAttribute(getArgNo()+1, Attribute::NoCapture);
  111. }
  112. /// hasSRetAttr - Return true if this argument has the sret attribute on
  113. /// it in its containing function.
  114. bool Argument::hasStructRetAttr() const {
  115. if (!getType()->isPointerTy()) return false;
  116. if (this != getParent()->arg_begin())
  117. return false; // StructRet param must be first param
  118. return getParent()->getAttributes().
  119. hasAttribute(1, Attribute::StructRet);
  120. }
  121. /// hasReturnedAttr - Return true if this argument has the returned attribute on
  122. /// it in its containing function.
  123. bool Argument::hasReturnedAttr() const {
  124. return getParent()->getAttributes().
  125. hasAttribute(getArgNo()+1, Attribute::Returned);
  126. }
  127. /// Return true if this argument has the readonly or readnone attribute on it
  128. /// in its containing function.
  129. bool Argument::onlyReadsMemory() const {
  130. return getParent()->getAttributes().
  131. hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
  132. getParent()->getAttributes().
  133. hasAttribute(getArgNo()+1, Attribute::ReadNone);
  134. }
  135. /// addAttr - Add attributes to an argument.
  136. void Argument::addAttr(AttributeSet AS) {
  137. assert(AS.getNumSlots() <= 1 &&
  138. "Trying to add more than one attribute set to an argument!");
  139. AttrBuilder B(AS, AS.getSlotIndex(0));
  140. getParent()->addAttributes(getArgNo() + 1,
  141. AttributeSet::get(Parent->getContext(),
  142. getArgNo() + 1, B));
  143. }
  144. /// removeAttr - Remove attributes from an argument.
  145. void Argument::removeAttr(AttributeSet AS) {
  146. assert(AS.getNumSlots() <= 1 &&
  147. "Trying to remove more than one attribute set from an argument!");
  148. AttrBuilder B(AS, AS.getSlotIndex(0));
  149. getParent()->removeAttributes(getArgNo() + 1,
  150. AttributeSet::get(Parent->getContext(),
  151. getArgNo() + 1, B));
  152. }
  153. //===----------------------------------------------------------------------===//
  154. // Helper Methods in Function
  155. //===----------------------------------------------------------------------===//
  156. LLVMContext &Function::getContext() const {
  157. return getType()->getContext();
  158. }
  159. FunctionType *Function::getFunctionType() const {
  160. return cast<FunctionType>(getType()->getElementType());
  161. }
  162. bool Function::isVarArg() const {
  163. return getFunctionType()->isVarArg();
  164. }
  165. Type *Function::getReturnType() const {
  166. return getFunctionType()->getReturnType();
  167. }
  168. void Function::removeFromParent() {
  169. getParent()->getFunctionList().remove(this);
  170. }
  171. void Function::eraseFromParent() {
  172. getParent()->getFunctionList().erase(this);
  173. }
  174. //===----------------------------------------------------------------------===//
  175. // Function Implementation
  176. //===----------------------------------------------------------------------===//
  177. Function::Function(FunctionType *Ty, LinkageTypes Linkage,
  178. const Twine &name, Module *ParentModule)
  179. : GlobalValue(PointerType::getUnqual(Ty),
  180. Value::FunctionVal, nullptr, 0, Linkage, name) {
  181. assert(FunctionType::isValidReturnType(getReturnType()) &&
  182. "invalid return type");
  183. SymTab = new ValueSymbolTable();
  184. // If the function has arguments, mark them as lazily built.
  185. if (Ty->getNumParams())
  186. setValueSubclassData(1); // Set the "has lazy arguments" bit.
  187. // Make sure that we get added to a function
  188. LeakDetector::addGarbageObject(this);
  189. if (ParentModule)
  190. ParentModule->getFunctionList().push_back(this);
  191. // Ensure intrinsics have the right parameter attributes.
  192. if (unsigned IID = getIntrinsicID())
  193. setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
  194. }
  195. Function::~Function() {
  196. dropAllReferences(); // After this it is safe to delete instructions.
  197. // Delete all of the method arguments and unlink from symbol table...
  198. ArgumentList.clear();
  199. delete SymTab;
  200. // Remove the function from the on-the-side GC table.
  201. clearGC();
  202. // Remove the intrinsicID from the Cache.
  203. if (getValueName() && isIntrinsic())
  204. getContext().pImpl->IntrinsicIDCache.erase(this);
  205. }
  206. void Function::BuildLazyArguments() const {
  207. // Create the arguments vector, all arguments start out unnamed.
  208. FunctionType *FT = getFunctionType();
  209. for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
  210. assert(!FT->getParamType(i)->isVoidTy() &&
  211. "Cannot have void typed arguments!");
  212. ArgumentList.push_back(new Argument(FT->getParamType(i)));
  213. }
  214. // Clear the lazy arguments bit.
  215. unsigned SDC = getSubclassDataFromValue();
  216. const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
  217. }
  218. size_t Function::arg_size() const {
  219. return getFunctionType()->getNumParams();
  220. }
  221. bool Function::arg_empty() const {
  222. return getFunctionType()->getNumParams() == 0;
  223. }
  224. void Function::setParent(Module *parent) {
  225. if (getParent())
  226. LeakDetector::addGarbageObject(this);
  227. Parent = parent;
  228. if (getParent())
  229. LeakDetector::removeGarbageObject(this);
  230. }
  231. // dropAllReferences() - This function causes all the subinstructions to "let
  232. // go" of all references that they are maintaining. This allows one to
  233. // 'delete' a whole class at a time, even though there may be circular
  234. // references... first all references are dropped, and all use counts go to
  235. // zero. Then everything is deleted for real. Note that no operations are
  236. // valid on an object that has "dropped all references", except operator
  237. // delete.
  238. //
  239. void Function::dropAllReferences() {
  240. for (iterator I = begin(), E = end(); I != E; ++I)
  241. I->dropAllReferences();
  242. // Delete all basic blocks. They are now unused, except possibly by
  243. // blockaddresses, but BasicBlock's destructor takes care of those.
  244. while (!BasicBlocks.empty())
  245. BasicBlocks.begin()->eraseFromParent();
  246. // Prefix data is stored in a side table.
  247. setPrefixData(nullptr);
  248. }
  249. void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
  250. AttributeSet PAL = getAttributes();
  251. PAL = PAL.addAttribute(getContext(), i, attr);
  252. setAttributes(PAL);
  253. }
  254. void Function::addAttributes(unsigned i, AttributeSet attrs) {
  255. AttributeSet PAL = getAttributes();
  256. PAL = PAL.addAttributes(getContext(), i, attrs);
  257. setAttributes(PAL);
  258. }
  259. void Function::removeAttributes(unsigned i, AttributeSet attrs) {
  260. AttributeSet PAL = getAttributes();
  261. PAL = PAL.removeAttributes(getContext(), i, attrs);
  262. setAttributes(PAL);
  263. }
  264. // Maintain the GC name for each function in an on-the-side table. This saves
  265. // allocating an additional word in Function for programs which do not use GC
  266. // (i.e., most programs) at the cost of increased overhead for clients which do
  267. // use GC.
  268. static DenseMap<const Function*,PooledStringPtr> *GCNames;
  269. static StringPool *GCNamePool;
  270. static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
  271. bool Function::hasGC() const {
  272. sys::SmartScopedReader<true> Reader(*GCLock);
  273. return GCNames && GCNames->count(this);
  274. }
  275. const char *Function::getGC() const {
  276. assert(hasGC() && "Function has no collector");
  277. sys::SmartScopedReader<true> Reader(*GCLock);
  278. return *(*GCNames)[this];
  279. }
  280. void Function::setGC(const char *Str) {
  281. sys::SmartScopedWriter<true> Writer(*GCLock);
  282. if (!GCNamePool)
  283. GCNamePool = new StringPool();
  284. if (!GCNames)
  285. GCNames = new DenseMap<const Function*,PooledStringPtr>();
  286. (*GCNames)[this] = GCNamePool->intern(Str);
  287. }
  288. void Function::clearGC() {
  289. sys::SmartScopedWriter<true> Writer(*GCLock);
  290. if (GCNames) {
  291. GCNames->erase(this);
  292. if (GCNames->empty()) {
  293. delete GCNames;
  294. GCNames = nullptr;
  295. if (GCNamePool->empty()) {
  296. delete GCNamePool;
  297. GCNamePool = nullptr;
  298. }
  299. }
  300. }
  301. }
  302. /// copyAttributesFrom - copy all additional attributes (those not needed to
  303. /// create a Function) from the Function Src to this one.
  304. void Function::copyAttributesFrom(const GlobalValue *Src) {
  305. assert(isa<Function>(Src) && "Expected a Function!");
  306. GlobalValue::copyAttributesFrom(Src);
  307. const Function *SrcF = cast<Function>(Src);
  308. setCallingConv(SrcF->getCallingConv());
  309. setAttributes(SrcF->getAttributes());
  310. if (SrcF->hasGC())
  311. setGC(SrcF->getGC());
  312. else
  313. clearGC();
  314. if (SrcF->hasPrefixData())
  315. setPrefixData(SrcF->getPrefixData());
  316. else
  317. setPrefixData(nullptr);
  318. }
  319. /// getIntrinsicID - This method returns the ID number of the specified
  320. /// function, or Intrinsic::not_intrinsic if the function is not an
  321. /// intrinsic, or if the pointer is null. This value is always defined to be
  322. /// zero to allow easy checking for whether a function is intrinsic or not. The
  323. /// particular intrinsic functions which correspond to this value are defined in
  324. /// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent
  325. /// requests for the same ID return results much faster from the cache.
  326. ///
  327. unsigned Function::getIntrinsicID() const {
  328. const ValueName *ValName = this->getValueName();
  329. if (!ValName || !isIntrinsic())
  330. return 0;
  331. LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
  332. getContext().pImpl->IntrinsicIDCache;
  333. if (!IntrinsicIDCache.count(this)) {
  334. unsigned Id = lookupIntrinsicID();
  335. IntrinsicIDCache[this]=Id;
  336. return Id;
  337. }
  338. return IntrinsicIDCache[this];
  339. }
  340. /// This private method does the actual lookup of an intrinsic ID when the query
  341. /// could not be answered from the cache.
  342. unsigned Function::lookupIntrinsicID() const {
  343. const ValueName *ValName = this->getValueName();
  344. unsigned Len = ValName->getKeyLength();
  345. const char *Name = ValName->getKeyData();
  346. #define GET_FUNCTION_RECOGNIZER
  347. #include "llvm/IR/Intrinsics.gen"
  348. #undef GET_FUNCTION_RECOGNIZER
  349. return 0;
  350. }
  351. std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
  352. assert(id < num_intrinsics && "Invalid intrinsic ID!");
  353. static const char * const Table[] = {
  354. "not_intrinsic",
  355. #define GET_INTRINSIC_NAME_TABLE
  356. #include "llvm/IR/Intrinsics.gen"
  357. #undef GET_INTRINSIC_NAME_TABLE
  358. };
  359. if (Tys.empty())
  360. return Table[id];
  361. std::string Result(Table[id]);
  362. for (unsigned i = 0; i < Tys.size(); ++i) {
  363. if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
  364. Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
  365. EVT::getEVT(PTyp->getElementType()).getEVTString();
  366. }
  367. else if (Tys[i])
  368. Result += "." + EVT::getEVT(Tys[i]).getEVTString();
  369. }
  370. return Result;
  371. }
  372. /// IIT_Info - These are enumerators that describe the entries returned by the
  373. /// getIntrinsicInfoTableEntries function.
  374. ///
  375. /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
  376. enum IIT_Info {
  377. // Common values should be encoded with 0-15.
  378. IIT_Done = 0,
  379. IIT_I1 = 1,
  380. IIT_I8 = 2,
  381. IIT_I16 = 3,
  382. IIT_I32 = 4,
  383. IIT_I64 = 5,
  384. IIT_F16 = 6,
  385. IIT_F32 = 7,
  386. IIT_F64 = 8,
  387. IIT_V2 = 9,
  388. IIT_V4 = 10,
  389. IIT_V8 = 11,
  390. IIT_V16 = 12,
  391. IIT_V32 = 13,
  392. IIT_PTR = 14,
  393. IIT_ARG = 15,
  394. // Values from 16+ are only encodable with the inefficient encoding.
  395. IIT_MMX = 16,
  396. IIT_METADATA = 17,
  397. IIT_EMPTYSTRUCT = 18,
  398. IIT_STRUCT2 = 19,
  399. IIT_STRUCT3 = 20,
  400. IIT_STRUCT4 = 21,
  401. IIT_STRUCT5 = 22,
  402. IIT_EXTEND_ARG = 23,
  403. IIT_TRUNC_ARG = 24,
  404. IIT_ANYPTR = 25,
  405. IIT_V1 = 26,
  406. IIT_VARARG = 27,
  407. IIT_HALF_VEC_ARG = 28
  408. };
  409. static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
  410. SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
  411. IIT_Info Info = IIT_Info(Infos[NextElt++]);
  412. unsigned StructElts = 2;
  413. using namespace Intrinsic;
  414. switch (Info) {
  415. case IIT_Done:
  416. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
  417. return;
  418. case IIT_VARARG:
  419. OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
  420. return;
  421. case IIT_MMX:
  422. OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
  423. return;
  424. case IIT_METADATA:
  425. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
  426. return;
  427. case IIT_F16:
  428. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
  429. return;
  430. case IIT_F32:
  431. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
  432. return;
  433. case IIT_F64:
  434. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
  435. return;
  436. case IIT_I1:
  437. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
  438. return;
  439. case IIT_I8:
  440. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
  441. return;
  442. case IIT_I16:
  443. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
  444. return;
  445. case IIT_I32:
  446. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
  447. return;
  448. case IIT_I64:
  449. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
  450. return;
  451. case IIT_V1:
  452. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
  453. DecodeIITType(NextElt, Infos, OutputTable);
  454. return;
  455. case IIT_V2:
  456. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
  457. DecodeIITType(NextElt, Infos, OutputTable);
  458. return;
  459. case IIT_V4:
  460. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
  461. DecodeIITType(NextElt, Infos, OutputTable);
  462. return;
  463. case IIT_V8:
  464. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
  465. DecodeIITType(NextElt, Infos, OutputTable);
  466. return;
  467. case IIT_V16:
  468. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
  469. DecodeIITType(NextElt, Infos, OutputTable);
  470. return;
  471. case IIT_V32:
  472. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
  473. DecodeIITType(NextElt, Infos, OutputTable);
  474. return;
  475. case IIT_PTR:
  476. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
  477. DecodeIITType(NextElt, Infos, OutputTable);
  478. return;
  479. case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
  480. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
  481. Infos[NextElt++]));
  482. DecodeIITType(NextElt, Infos, OutputTable);
  483. return;
  484. }
  485. case IIT_ARG: {
  486. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  487. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
  488. return;
  489. }
  490. case IIT_EXTEND_ARG: {
  491. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  492. OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
  493. ArgInfo));
  494. return;
  495. }
  496. case IIT_TRUNC_ARG: {
  497. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  498. OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
  499. ArgInfo));
  500. return;
  501. }
  502. case IIT_HALF_VEC_ARG: {
  503. unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
  504. OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
  505. ArgInfo));
  506. return;
  507. }
  508. case IIT_EMPTYSTRUCT:
  509. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
  510. return;
  511. case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
  512. case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
  513. case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
  514. case IIT_STRUCT2: {
  515. OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
  516. for (unsigned i = 0; i != StructElts; ++i)
  517. DecodeIITType(NextElt, Infos, OutputTable);
  518. return;
  519. }
  520. }
  521. llvm_unreachable("unhandled");
  522. }
  523. #define GET_INTRINSIC_GENERATOR_GLOBAL
  524. #include "llvm/IR/Intrinsics.gen"
  525. #undef GET_INTRINSIC_GENERATOR_GLOBAL
  526. void Intrinsic::getIntrinsicInfoTableEntries(ID id,
  527. SmallVectorImpl<IITDescriptor> &T){
  528. // Check to see if the intrinsic's type was expressible by the table.
  529. unsigned TableVal = IIT_Table[id-1];
  530. // Decode the TableVal into an array of IITValues.
  531. SmallVector<unsigned char, 8> IITValues;
  532. ArrayRef<unsigned char> IITEntries;
  533. unsigned NextElt = 0;
  534. if ((TableVal >> 31) != 0) {
  535. // This is an offset into the IIT_LongEncodingTable.
  536. IITEntries = IIT_LongEncodingTable;
  537. // Strip sentinel bit.
  538. NextElt = (TableVal << 1) >> 1;
  539. } else {
  540. // Decode the TableVal into an array of IITValues. If the entry was encoded
  541. // into a single word in the table itself, decode it now.
  542. do {
  543. IITValues.push_back(TableVal & 0xF);
  544. TableVal >>= 4;
  545. } while (TableVal);
  546. IITEntries = IITValues;
  547. NextElt = 0;
  548. }
  549. // Okay, decode the table into the output vector of IITDescriptors.
  550. DecodeIITType(NextElt, IITEntries, T);
  551. while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
  552. DecodeIITType(NextElt, IITEntries, T);
  553. }
  554. static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
  555. ArrayRef<Type*> Tys, LLVMContext &Context) {
  556. using namespace Intrinsic;
  557. IITDescriptor D = Infos.front();
  558. Infos = Infos.slice(1);
  559. switch (D.Kind) {
  560. case IITDescriptor::Void: return Type::getVoidTy(Context);
  561. case IITDescriptor::VarArg: return Type::getVoidTy(Context);
  562. case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
  563. case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
  564. case IITDescriptor::Half: return Type::getHalfTy(Context);
  565. case IITDescriptor::Float: return Type::getFloatTy(Context);
  566. case IITDescriptor::Double: return Type::getDoubleTy(Context);
  567. case IITDescriptor::Integer:
  568. return IntegerType::get(Context, D.Integer_Width);
  569. case IITDescriptor::Vector:
  570. return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
  571. case IITDescriptor::Pointer:
  572. return PointerType::get(DecodeFixedType(Infos, Tys, Context),
  573. D.Pointer_AddressSpace);
  574. case IITDescriptor::Struct: {
  575. Type *Elts[5];
  576. assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
  577. for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
  578. Elts[i] = DecodeFixedType(Infos, Tys, Context);
  579. return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
  580. }
  581. case IITDescriptor::Argument:
  582. return Tys[D.getArgumentNumber()];
  583. case IITDescriptor::ExtendArgument: {
  584. Type *Ty = Tys[D.getArgumentNumber()];
  585. if (VectorType *VTy = dyn_cast<VectorType>(Ty))
  586. return VectorType::getExtendedElementVectorType(VTy);
  587. return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
  588. }
  589. case IITDescriptor::TruncArgument: {
  590. Type *Ty = Tys[D.getArgumentNumber()];
  591. if (VectorType *VTy = dyn_cast<VectorType>(Ty))
  592. return VectorType::getTruncatedElementVectorType(VTy);
  593. IntegerType *ITy = cast<IntegerType>(Ty);
  594. assert(ITy->getBitWidth() % 2 == 0);
  595. return IntegerType::get(Context, ITy->getBitWidth() / 2);
  596. }
  597. case IITDescriptor::HalfVecArgument:
  598. return VectorType::getHalfElementsVectorType(cast<VectorType>(
  599. Tys[D.getArgumentNumber()]));
  600. }
  601. llvm_unreachable("unhandled");
  602. }
  603. FunctionType *Intrinsic::getType(LLVMContext &Context,
  604. ID id, ArrayRef<Type*> Tys) {
  605. SmallVector<IITDescriptor, 8> Table;
  606. getIntrinsicInfoTableEntries(id, Table);
  607. ArrayRef<IITDescriptor> TableRef = Table;
  608. Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
  609. SmallVector<Type*, 8> ArgTys;
  610. while (!TableRef.empty())
  611. ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
  612. return FunctionType::get(ResultTy, ArgTys, false);
  613. }
  614. bool Intrinsic::isOverloaded(ID id) {
  615. #define GET_INTRINSIC_OVERLOAD_TABLE
  616. #include "llvm/IR/Intrinsics.gen"
  617. #undef GET_INTRINSIC_OVERLOAD_TABLE
  618. }
  619. /// This defines the "Intrinsic::getAttributes(ID id)" method.
  620. #define GET_INTRINSIC_ATTRIBUTES
  621. #include "llvm/IR/Intrinsics.gen"
  622. #undef GET_INTRINSIC_ATTRIBUTES
  623. Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
  624. // There can never be multiple globals with the same name of different types,
  625. // because intrinsics must be a specific type.
  626. return
  627. cast<Function>(M->getOrInsertFunction(getName(id, Tys),
  628. getType(M->getContext(), id, Tys)));
  629. }
  630. // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
  631. #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
  632. #include "llvm/IR/Intrinsics.gen"
  633. #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
  634. /// hasAddressTaken - returns true if there are any uses of this function
  635. /// other than direct calls or invokes to it.
  636. bool Function::hasAddressTaken(const User* *PutOffender) const {
  637. for (const Use &U : uses()) {
  638. const User *FU = U.getUser();
  639. if (isa<BlockAddress>(FU))
  640. continue;
  641. if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU))
  642. return PutOffender ? (*PutOffender = FU, true) : true;
  643. ImmutableCallSite CS(cast<Instruction>(FU));
  644. if (!CS.isCallee(&U))
  645. return PutOffender ? (*PutOffender = FU, true) : true;
  646. }
  647. return false;
  648. }
  649. bool Function::isDefTriviallyDead() const {
  650. // Check the linkage
  651. if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
  652. !hasAvailableExternallyLinkage())
  653. return false;
  654. // Check if the function is used by anything other than a blockaddress.
  655. for (const User *U : users())
  656. if (!isa<BlockAddress>(U))
  657. return false;
  658. return true;
  659. }
  660. /// callsFunctionThatReturnsTwice - Return true if the function has a call to
  661. /// setjmp or other function that gcc recognizes as "returning twice".
  662. bool Function::callsFunctionThatReturnsTwice() const {
  663. for (const_inst_iterator
  664. I = inst_begin(this), E = inst_end(this); I != E; ++I) {
  665. ImmutableCallSite CS(&*I);
  666. if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
  667. return true;
  668. }
  669. return false;
  670. }
  671. Constant *Function::getPrefixData() const {
  672. assert(hasPrefixData());
  673. const LLVMContextImpl::PrefixDataMapTy &PDMap =
  674. getContext().pImpl->PrefixDataMap;
  675. assert(PDMap.find(this) != PDMap.end());
  676. return cast<Constant>(PDMap.find(this)->second->getReturnValue());
  677. }
  678. void Function::setPrefixData(Constant *PrefixData) {
  679. if (!PrefixData && !hasPrefixData())
  680. return;
  681. unsigned SCData = getSubclassDataFromValue();
  682. LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap;
  683. ReturnInst *&PDHolder = PDMap[this];
  684. if (PrefixData) {
  685. if (PDHolder)
  686. PDHolder->setOperand(0, PrefixData);
  687. else
  688. PDHolder = ReturnInst::Create(getContext(), PrefixData);
  689. SCData |= 2;
  690. } else {
  691. delete PDHolder;
  692. PDMap.erase(this);
  693. SCData &= ~2;
  694. }
  695. setValueSubclassData(SCData);
  696. }