Function.cpp 21 KB

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