1 //===-- Function.cpp - Implement the Global object classes ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Function class for the IR library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/Function.h"
15 #include "LLVMContextImpl.h"
16 #include "SymbolTableListTraitsImpl.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/IR/CallSite.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/InstIterator.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/LeakDetector.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/RWMutex.h"
30 #include "llvm/Support/StringPool.h"
31 #include "llvm/Support/Threading.h"
34 // Explicit instantiations of SymbolTableListTraits since some of the methods
35 // are not in the public header file...
36 template class llvm::SymbolTableListTraits<Argument, Function>;
37 template class llvm::SymbolTableListTraits<BasicBlock, Function>;
39 //===----------------------------------------------------------------------===//
40 // Argument Implementation
41 //===----------------------------------------------------------------------===//
43 void Argument::anchor() { }
45 Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
46 : Value(Ty, Value::ArgumentVal) {
49 // Make sure that we get added to a function
50 LeakDetector::addGarbageObject(this);
53 Par->getArgumentList().push_back(this);
57 void Argument::setParent(Function *parent) {
59 LeakDetector::addGarbageObject(this);
62 LeakDetector::removeGarbageObject(this);
65 /// getArgNo - Return the index of this formal argument in its containing
66 /// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
67 unsigned Argument::getArgNo() const {
68 const Function *F = getParent();
69 assert(F && "Argument is not in a function");
71 Function::const_arg_iterator AI = F->arg_begin();
73 for (; &*AI != this; ++AI)
79 /// hasNonNullAttr - Return true if this argument has the nonnull attribute on
80 /// it in its containing function.
81 bool Argument::hasNonNullAttr() const {
82 if (!getType()->isPointerTy()) return false;
83 return getParent()->getAttributes().
84 hasAttribute(getArgNo()+1, Attribute::NonNull);
87 /// hasByValAttr - Return true if this argument has the byval attribute on it
88 /// in its containing function.
89 bool Argument::hasByValAttr() const {
90 if (!getType()->isPointerTy()) return false;
91 return getParent()->getAttributes().
92 hasAttribute(getArgNo()+1, Attribute::ByVal);
95 /// \brief Return true if this argument has the inalloca attribute on it in
96 /// its containing function.
97 bool Argument::hasInAllocaAttr() const {
98 if (!getType()->isPointerTy()) return false;
99 return getParent()->getAttributes().
100 hasAttribute(getArgNo()+1, Attribute::InAlloca);
103 bool Argument::hasByValOrInAllocaAttr() const {
104 if (!getType()->isPointerTy()) return false;
105 AttributeSet Attrs = getParent()->getAttributes();
106 return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) ||
107 Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca);
110 unsigned Argument::getParamAlignment() const {
111 assert(getType()->isPointerTy() && "Only pointers have alignments");
112 return getParent()->getParamAlignment(getArgNo()+1);
116 /// hasNestAttr - Return true if this argument has the nest attribute on
117 /// it in its containing function.
118 bool Argument::hasNestAttr() const {
119 if (!getType()->isPointerTy()) return false;
120 return getParent()->getAttributes().
121 hasAttribute(getArgNo()+1, Attribute::Nest);
124 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
125 /// it in its containing function.
126 bool Argument::hasNoAliasAttr() const {
127 if (!getType()->isPointerTy()) return false;
128 return getParent()->getAttributes().
129 hasAttribute(getArgNo()+1, Attribute::NoAlias);
132 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
133 /// on it in its containing function.
134 bool Argument::hasNoCaptureAttr() const {
135 if (!getType()->isPointerTy()) return false;
136 return getParent()->getAttributes().
137 hasAttribute(getArgNo()+1, Attribute::NoCapture);
140 /// hasSRetAttr - Return true if this argument has the sret attribute on
141 /// it in its containing function.
142 bool Argument::hasStructRetAttr() const {
143 if (!getType()->isPointerTy()) return false;
144 if (this != getParent()->arg_begin())
145 return false; // StructRet param must be first param
146 return getParent()->getAttributes().
147 hasAttribute(1, Attribute::StructRet);
150 /// hasReturnedAttr - Return true if this argument has the returned attribute on
151 /// it in its containing function.
152 bool Argument::hasReturnedAttr() const {
153 return getParent()->getAttributes().
154 hasAttribute(getArgNo()+1, Attribute::Returned);
157 /// Return true if this argument has the readonly or readnone attribute on it
158 /// in its containing function.
159 bool Argument::onlyReadsMemory() const {
160 return getParent()->getAttributes().
161 hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
162 getParent()->getAttributes().
163 hasAttribute(getArgNo()+1, Attribute::ReadNone);
166 /// addAttr - Add attributes to an argument.
167 void Argument::addAttr(AttributeSet AS) {
168 assert(AS.getNumSlots() <= 1 &&
169 "Trying to add more than one attribute set to an argument!");
170 AttrBuilder B(AS, AS.getSlotIndex(0));
171 getParent()->addAttributes(getArgNo() + 1,
172 AttributeSet::get(Parent->getContext(),
176 /// removeAttr - Remove attributes from an argument.
177 void Argument::removeAttr(AttributeSet AS) {
178 assert(AS.getNumSlots() <= 1 &&
179 "Trying to remove more than one attribute set from an argument!");
180 AttrBuilder B(AS, AS.getSlotIndex(0));
181 getParent()->removeAttributes(getArgNo() + 1,
182 AttributeSet::get(Parent->getContext(),
186 //===----------------------------------------------------------------------===//
187 // Helper Methods in Function
188 //===----------------------------------------------------------------------===//
190 LLVMContext &Function::getContext() const {
191 return getType()->getContext();
194 FunctionType *Function::getFunctionType() const {
195 return cast<FunctionType>(getType()->getElementType());
198 bool Function::isVarArg() const {
199 return getFunctionType()->isVarArg();
202 Type *Function::getReturnType() const {
203 return getFunctionType()->getReturnType();
206 void Function::removeFromParent() {
207 getParent()->getFunctionList().remove(this);
210 void Function::eraseFromParent() {
211 getParent()->getFunctionList().erase(this);
214 //===----------------------------------------------------------------------===//
215 // Function Implementation
216 //===----------------------------------------------------------------------===//
218 Function::Function(FunctionType *Ty, LinkageTypes Linkage,
219 const Twine &name, Module *ParentModule)
220 : GlobalObject(PointerType::getUnqual(Ty),
221 Value::FunctionVal, nullptr, 0, Linkage, name) {
222 assert(FunctionType::isValidReturnType(getReturnType()) &&
223 "invalid return type");
224 SymTab = new ValueSymbolTable();
226 // If the function has arguments, mark them as lazily built.
227 if (Ty->getNumParams())
228 setValueSubclassData(1); // Set the "has lazy arguments" bit.
230 // Make sure that we get added to a function
231 LeakDetector::addGarbageObject(this);
234 ParentModule->getFunctionList().push_back(this);
236 // Ensure intrinsics have the right parameter attributes.
237 if (unsigned IID = getIntrinsicID())
238 setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
242 Function::~Function() {
243 dropAllReferences(); // After this it is safe to delete instructions.
245 // Delete all of the method arguments and unlink from symbol table...
246 ArgumentList.clear();
249 // Remove the function from the on-the-side GC table.
252 // Remove the intrinsicID from the Cache.
253 if (getValueName() && isIntrinsic())
254 getContext().pImpl->IntrinsicIDCache.erase(this);
257 void Function::BuildLazyArguments() const {
258 // Create the arguments vector, all arguments start out unnamed.
259 FunctionType *FT = getFunctionType();
260 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
261 assert(!FT->getParamType(i)->isVoidTy() &&
262 "Cannot have void typed arguments!");
263 ArgumentList.push_back(new Argument(FT->getParamType(i)));
266 // Clear the lazy arguments bit.
267 unsigned SDC = getSubclassDataFromValue();
268 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
271 size_t Function::arg_size() const {
272 return getFunctionType()->getNumParams();
274 bool Function::arg_empty() const {
275 return getFunctionType()->getNumParams() == 0;
278 void Function::setParent(Module *parent) {
280 LeakDetector::addGarbageObject(this);
283 LeakDetector::removeGarbageObject(this);
286 // dropAllReferences() - This function causes all the subinstructions to "let
287 // go" of all references that they are maintaining. This allows one to
288 // 'delete' a whole class at a time, even though there may be circular
289 // references... first all references are dropped, and all use counts go to
290 // zero. Then everything is deleted for real. Note that no operations are
291 // valid on an object that has "dropped all references", except operator
294 void Function::dropAllReferences() {
295 for (iterator I = begin(), E = end(); I != E; ++I)
296 I->dropAllReferences();
298 // Delete all basic blocks. They are now unused, except possibly by
299 // blockaddresses, but BasicBlock's destructor takes care of those.
300 while (!BasicBlocks.empty())
301 BasicBlocks.begin()->eraseFromParent();
303 // Prefix data is stored in a side table.
304 setPrefixData(nullptr);
307 void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
308 AttributeSet PAL = getAttributes();
309 PAL = PAL.addAttribute(getContext(), i, attr);
313 void Function::addAttributes(unsigned i, AttributeSet attrs) {
314 AttributeSet PAL = getAttributes();
315 PAL = PAL.addAttributes(getContext(), i, attrs);
319 void Function::removeAttributes(unsigned i, AttributeSet attrs) {
320 AttributeSet PAL = getAttributes();
321 PAL = PAL.removeAttributes(getContext(), i, attrs);
325 // Maintain the GC name for each function in an on-the-side table. This saves
326 // allocating an additional word in Function for programs which do not use GC
327 // (i.e., most programs) at the cost of increased overhead for clients which do
329 static DenseMap<const Function*,PooledStringPtr> *GCNames;
330 static StringPool *GCNamePool;
331 static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
333 bool Function::hasGC() const {
334 sys::SmartScopedReader<true> Reader(*GCLock);
335 return GCNames && GCNames->count(this);
338 const char *Function::getGC() const {
339 assert(hasGC() && "Function has no collector");
340 sys::SmartScopedReader<true> Reader(*GCLock);
341 return *(*GCNames)[this];
344 void Function::setGC(const char *Str) {
345 sys::SmartScopedWriter<true> Writer(*GCLock);
347 GCNamePool = new StringPool();
349 GCNames = new DenseMap<const Function*,PooledStringPtr>();
350 (*GCNames)[this] = GCNamePool->intern(Str);
353 void Function::clearGC() {
354 sys::SmartScopedWriter<true> Writer(*GCLock);
356 GCNames->erase(this);
357 if (GCNames->empty()) {
360 if (GCNamePool->empty()) {
362 GCNamePool = nullptr;
368 /// copyAttributesFrom - copy all additional attributes (those not needed to
369 /// create a Function) from the Function Src to this one.
370 void Function::copyAttributesFrom(const GlobalValue *Src) {
371 assert(isa<Function>(Src) && "Expected a Function!");
372 GlobalObject::copyAttributesFrom(Src);
373 const Function *SrcF = cast<Function>(Src);
374 setCallingConv(SrcF->getCallingConv());
375 setAttributes(SrcF->getAttributes());
377 setGC(SrcF->getGC());
380 if (SrcF->hasPrefixData())
381 setPrefixData(SrcF->getPrefixData());
383 setPrefixData(nullptr);
386 /// getIntrinsicID - This method returns the ID number of the specified
387 /// function, or Intrinsic::not_intrinsic if the function is not an
388 /// intrinsic, or if the pointer is null. This value is always defined to be
389 /// zero to allow easy checking for whether a function is intrinsic or not. The
390 /// particular intrinsic functions which correspond to this value are defined in
391 /// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent
392 /// requests for the same ID return results much faster from the cache.
394 unsigned Function::getIntrinsicID() const {
395 const ValueName *ValName = this->getValueName();
396 if (!ValName || !isIntrinsic())
399 LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
400 getContext().pImpl->IntrinsicIDCache;
401 if (!IntrinsicIDCache.count(this)) {
402 unsigned Id = lookupIntrinsicID();
403 IntrinsicIDCache[this]=Id;
406 return IntrinsicIDCache[this];
409 /// This private method does the actual lookup of an intrinsic ID when the query
410 /// could not be answered from the cache.
411 unsigned Function::lookupIntrinsicID() const {
412 const ValueName *ValName = this->getValueName();
413 unsigned Len = ValName->getKeyLength();
414 const char *Name = ValName->getKeyData();
416 #define GET_FUNCTION_RECOGNIZER
417 #include "llvm/IR/Intrinsics.gen"
418 #undef GET_FUNCTION_RECOGNIZER
423 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
424 assert(id < num_intrinsics && "Invalid intrinsic ID!");
425 static const char * const Table[] = {
427 #define GET_INTRINSIC_NAME_TABLE
428 #include "llvm/IR/Intrinsics.gen"
429 #undef GET_INTRINSIC_NAME_TABLE
433 std::string Result(Table[id]);
434 for (unsigned i = 0; i < Tys.size(); ++i) {
435 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
436 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
437 EVT::getEVT(PTyp->getElementType()).getEVTString();
440 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
446 /// IIT_Info - These are enumerators that describe the entries returned by the
447 /// getIntrinsicInfoTableEntries function.
449 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
451 // Common values should be encoded with 0-15.
469 // Values from 16+ are only encodable with the inefficient encoding.
472 IIT_EMPTYSTRUCT = 18,
482 IIT_HALF_VEC_ARG = 28
486 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
487 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
488 IIT_Info Info = IIT_Info(Infos[NextElt++]);
489 unsigned StructElts = 2;
490 using namespace Intrinsic;
494 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
497 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
500 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
503 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
506 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
509 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
512 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
515 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
518 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
521 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
524 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
527 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
530 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
531 DecodeIITType(NextElt, Infos, OutputTable);
534 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
535 DecodeIITType(NextElt, Infos, OutputTable);
538 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
539 DecodeIITType(NextElt, Infos, OutputTable);
542 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
543 DecodeIITType(NextElt, Infos, OutputTable);
546 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
547 DecodeIITType(NextElt, Infos, OutputTable);
550 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
551 DecodeIITType(NextElt, Infos, OutputTable);
554 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
555 DecodeIITType(NextElt, Infos, OutputTable);
557 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
558 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
560 DecodeIITType(NextElt, Infos, OutputTable);
564 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
565 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
568 case IIT_EXTEND_ARG: {
569 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
570 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
574 case IIT_TRUNC_ARG: {
575 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
576 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
580 case IIT_HALF_VEC_ARG: {
581 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
582 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
586 case IIT_EMPTYSTRUCT:
587 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
589 case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
590 case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
591 case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
593 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
595 for (unsigned i = 0; i != StructElts; ++i)
596 DecodeIITType(NextElt, Infos, OutputTable);
600 llvm_unreachable("unhandled");
604 #define GET_INTRINSIC_GENERATOR_GLOBAL
605 #include "llvm/IR/Intrinsics.gen"
606 #undef GET_INTRINSIC_GENERATOR_GLOBAL
608 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
609 SmallVectorImpl<IITDescriptor> &T){
610 // Check to see if the intrinsic's type was expressible by the table.
611 unsigned TableVal = IIT_Table[id-1];
613 // Decode the TableVal into an array of IITValues.
614 SmallVector<unsigned char, 8> IITValues;
615 ArrayRef<unsigned char> IITEntries;
616 unsigned NextElt = 0;
617 if ((TableVal >> 31) != 0) {
618 // This is an offset into the IIT_LongEncodingTable.
619 IITEntries = IIT_LongEncodingTable;
621 // Strip sentinel bit.
622 NextElt = (TableVal << 1) >> 1;
624 // Decode the TableVal into an array of IITValues. If the entry was encoded
625 // into a single word in the table itself, decode it now.
627 IITValues.push_back(TableVal & 0xF);
631 IITEntries = IITValues;
635 // Okay, decode the table into the output vector of IITDescriptors.
636 DecodeIITType(NextElt, IITEntries, T);
637 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
638 DecodeIITType(NextElt, IITEntries, T);
642 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
643 ArrayRef<Type*> Tys, LLVMContext &Context) {
644 using namespace Intrinsic;
645 IITDescriptor D = Infos.front();
646 Infos = Infos.slice(1);
649 case IITDescriptor::Void: return Type::getVoidTy(Context);
650 case IITDescriptor::VarArg: return Type::getVoidTy(Context);
651 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
652 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
653 case IITDescriptor::Half: return Type::getHalfTy(Context);
654 case IITDescriptor::Float: return Type::getFloatTy(Context);
655 case IITDescriptor::Double: return Type::getDoubleTy(Context);
657 case IITDescriptor::Integer:
658 return IntegerType::get(Context, D.Integer_Width);
659 case IITDescriptor::Vector:
660 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
661 case IITDescriptor::Pointer:
662 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
663 D.Pointer_AddressSpace);
664 case IITDescriptor::Struct: {
666 assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
667 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
668 Elts[i] = DecodeFixedType(Infos, Tys, Context);
669 return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
672 case IITDescriptor::Argument:
673 return Tys[D.getArgumentNumber()];
674 case IITDescriptor::ExtendArgument: {
675 Type *Ty = Tys[D.getArgumentNumber()];
676 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
677 return VectorType::getExtendedElementVectorType(VTy);
679 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
681 case IITDescriptor::TruncArgument: {
682 Type *Ty = Tys[D.getArgumentNumber()];
683 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
684 return VectorType::getTruncatedElementVectorType(VTy);
686 IntegerType *ITy = cast<IntegerType>(Ty);
687 assert(ITy->getBitWidth() % 2 == 0);
688 return IntegerType::get(Context, ITy->getBitWidth() / 2);
690 case IITDescriptor::HalfVecArgument:
691 return VectorType::getHalfElementsVectorType(cast<VectorType>(
692 Tys[D.getArgumentNumber()]));
694 llvm_unreachable("unhandled");
699 FunctionType *Intrinsic::getType(LLVMContext &Context,
700 ID id, ArrayRef<Type*> Tys) {
701 SmallVector<IITDescriptor, 8> Table;
702 getIntrinsicInfoTableEntries(id, Table);
704 ArrayRef<IITDescriptor> TableRef = Table;
705 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
707 SmallVector<Type*, 8> ArgTys;
708 while (!TableRef.empty())
709 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
711 return FunctionType::get(ResultTy, ArgTys, false);
714 bool Intrinsic::isOverloaded(ID id) {
715 #define GET_INTRINSIC_OVERLOAD_TABLE
716 #include "llvm/IR/Intrinsics.gen"
717 #undef GET_INTRINSIC_OVERLOAD_TABLE
720 /// This defines the "Intrinsic::getAttributes(ID id)" method.
721 #define GET_INTRINSIC_ATTRIBUTES
722 #include "llvm/IR/Intrinsics.gen"
723 #undef GET_INTRINSIC_ATTRIBUTES
725 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
726 // There can never be multiple globals with the same name of different types,
727 // because intrinsics must be a specific type.
729 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
730 getType(M->getContext(), id, Tys)));
733 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
734 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
735 #include "llvm/IR/Intrinsics.gen"
736 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
738 /// hasAddressTaken - returns true if there are any uses of this function
739 /// other than direct calls or invokes to it.
740 bool Function::hasAddressTaken(const User* *PutOffender) const {
741 for (const Use &U : uses()) {
742 const User *FU = U.getUser();
743 if (isa<BlockAddress>(FU))
745 if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU))
746 return PutOffender ? (*PutOffender = FU, true) : true;
747 ImmutableCallSite CS(cast<Instruction>(FU));
748 if (!CS.isCallee(&U))
749 return PutOffender ? (*PutOffender = FU, true) : true;
754 bool Function::isDefTriviallyDead() const {
756 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
757 !hasAvailableExternallyLinkage())
760 // Check if the function is used by anything other than a blockaddress.
761 for (const User *U : users())
762 if (!isa<BlockAddress>(U))
768 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
769 /// setjmp or other function that gcc recognizes as "returning twice".
770 bool Function::callsFunctionThatReturnsTwice() const {
771 for (const_inst_iterator
772 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
773 ImmutableCallSite CS(&*I);
774 if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
781 Constant *Function::getPrefixData() const {
782 assert(hasPrefixData());
783 const LLVMContextImpl::PrefixDataMapTy &PDMap =
784 getContext().pImpl->PrefixDataMap;
785 assert(PDMap.find(this) != PDMap.end());
786 return cast<Constant>(PDMap.find(this)->second->getReturnValue());
789 void Function::setPrefixData(Constant *PrefixData) {
790 if (!PrefixData && !hasPrefixData())
793 unsigned SCData = getSubclassDataFromValue();
794 LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap;
795 ReturnInst *&PDHolder = PDMap[this];
798 PDHolder->setOperand(0, PrefixData);
800 PDHolder = ReturnInst::Create(getContext(), PrefixData);
807 setValueSubclassData(SCData);