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. Also returns true if at least one byte is
81 /// known to be dereferenceable and the pointer is in addrspace(0).
82 bool Argument::hasNonNullAttr() const {
83 if (!getType()->isPointerTy()) return false;
84 if (getParent()->getAttributes().
85 hasAttribute(getArgNo()+1, Attribute::NonNull))
87 else if (getDereferenceableBytes() > 0 &&
88 getType()->getPointerAddressSpace() == 0)
93 /// hasByValAttr - Return true if this argument has the byval attribute on it
94 /// in its containing function.
95 bool Argument::hasByValAttr() const {
96 if (!getType()->isPointerTy()) return false;
97 return getParent()->getAttributes().
98 hasAttribute(getArgNo()+1, Attribute::ByVal);
101 /// \brief Return true if this argument has the inalloca attribute on it in
102 /// its containing function.
103 bool Argument::hasInAllocaAttr() const {
104 if (!getType()->isPointerTy()) return false;
105 return getParent()->getAttributes().
106 hasAttribute(getArgNo()+1, Attribute::InAlloca);
109 bool Argument::hasByValOrInAllocaAttr() const {
110 if (!getType()->isPointerTy()) return false;
111 AttributeSet Attrs = getParent()->getAttributes();
112 return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) ||
113 Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca);
116 unsigned Argument::getParamAlignment() const {
117 assert(getType()->isPointerTy() && "Only pointers have alignments");
118 return getParent()->getParamAlignment(getArgNo()+1);
122 uint64_t Argument::getDereferenceableBytes() const {
123 assert(getType()->isPointerTy() &&
124 "Only pointers have dereferenceable bytes");
125 return getParent()->getDereferenceableBytes(getArgNo()+1);
128 /// hasNestAttr - Return true if this argument has the nest attribute on
129 /// it in its containing function.
130 bool Argument::hasNestAttr() const {
131 if (!getType()->isPointerTy()) return false;
132 return getParent()->getAttributes().
133 hasAttribute(getArgNo()+1, Attribute::Nest);
136 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
137 /// it in its containing function.
138 bool Argument::hasNoAliasAttr() const {
139 if (!getType()->isPointerTy()) return false;
140 return getParent()->getAttributes().
141 hasAttribute(getArgNo()+1, Attribute::NoAlias);
144 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
145 /// on it in its containing function.
146 bool Argument::hasNoCaptureAttr() const {
147 if (!getType()->isPointerTy()) return false;
148 return getParent()->getAttributes().
149 hasAttribute(getArgNo()+1, Attribute::NoCapture);
152 /// hasSRetAttr - Return true if this argument has the sret attribute on
153 /// it in its containing function.
154 bool Argument::hasStructRetAttr() const {
155 if (!getType()->isPointerTy()) return false;
156 if (this != getParent()->arg_begin())
157 return false; // StructRet param must be first param
158 return getParent()->getAttributes().
159 hasAttribute(1, Attribute::StructRet);
162 /// hasReturnedAttr - Return true if this argument has the returned attribute on
163 /// it in its containing function.
164 bool Argument::hasReturnedAttr() const {
165 return getParent()->getAttributes().
166 hasAttribute(getArgNo()+1, Attribute::Returned);
169 /// hasZExtAttr - Return true if this argument has the zext attribute on it in
170 /// its containing function.
171 bool Argument::hasZExtAttr() const {
172 return getParent()->getAttributes().
173 hasAttribute(getArgNo()+1, Attribute::ZExt);
176 /// hasSExtAttr Return true if this argument has the sext attribute on it in its
177 /// containing function.
178 bool Argument::hasSExtAttr() const {
179 return getParent()->getAttributes().
180 hasAttribute(getArgNo()+1, Attribute::SExt);
183 /// Return true if this argument has the readonly or readnone attribute on it
184 /// in its containing function.
185 bool Argument::onlyReadsMemory() const {
186 return getParent()->getAttributes().
187 hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
188 getParent()->getAttributes().
189 hasAttribute(getArgNo()+1, Attribute::ReadNone);
192 /// addAttr - Add attributes to an argument.
193 void Argument::addAttr(AttributeSet AS) {
194 assert(AS.getNumSlots() <= 1 &&
195 "Trying to add more than one attribute set to an argument!");
196 AttrBuilder B(AS, AS.getSlotIndex(0));
197 getParent()->addAttributes(getArgNo() + 1,
198 AttributeSet::get(Parent->getContext(),
202 /// removeAttr - Remove attributes from an argument.
203 void Argument::removeAttr(AttributeSet AS) {
204 assert(AS.getNumSlots() <= 1 &&
205 "Trying to remove more than one attribute set from an argument!");
206 AttrBuilder B(AS, AS.getSlotIndex(0));
207 getParent()->removeAttributes(getArgNo() + 1,
208 AttributeSet::get(Parent->getContext(),
212 //===----------------------------------------------------------------------===//
213 // Helper Methods in Function
214 //===----------------------------------------------------------------------===//
216 LLVMContext &Function::getContext() const {
217 return getType()->getContext();
220 FunctionType *Function::getFunctionType() const {
221 return cast<FunctionType>(getType()->getElementType());
224 bool Function::isVarArg() const {
225 return getFunctionType()->isVarArg();
228 Type *Function::getReturnType() const {
229 return getFunctionType()->getReturnType();
232 void Function::removeFromParent() {
233 getParent()->getFunctionList().remove(this);
236 void Function::eraseFromParent() {
237 getParent()->getFunctionList().erase(this);
240 //===----------------------------------------------------------------------===//
241 // Function Implementation
242 //===----------------------------------------------------------------------===//
244 Function::Function(FunctionType *Ty, LinkageTypes Linkage,
245 const Twine &name, Module *ParentModule)
246 : GlobalObject(PointerType::getUnqual(Ty),
247 Value::FunctionVal, nullptr, 0, Linkage, name) {
248 assert(FunctionType::isValidReturnType(getReturnType()) &&
249 "invalid return type");
250 SymTab = new ValueSymbolTable();
252 // If the function has arguments, mark them as lazily built.
253 if (Ty->getNumParams())
254 setValueSubclassData(1); // Set the "has lazy arguments" bit.
256 // Make sure that we get added to a function
257 LeakDetector::addGarbageObject(this);
260 ParentModule->getFunctionList().push_back(this);
262 // Ensure intrinsics have the right parameter attributes.
263 if (unsigned IID = getIntrinsicID())
264 setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
268 Function::~Function() {
269 dropAllReferences(); // After this it is safe to delete instructions.
271 // Delete all of the method arguments and unlink from symbol table...
272 ArgumentList.clear();
275 // Remove the function from the on-the-side GC table.
278 // Remove the intrinsicID from the Cache.
279 if (getValueName() && isIntrinsic())
280 getContext().pImpl->IntrinsicIDCache.erase(this);
283 void Function::BuildLazyArguments() const {
284 // Create the arguments vector, all arguments start out unnamed.
285 FunctionType *FT = getFunctionType();
286 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
287 assert(!FT->getParamType(i)->isVoidTy() &&
288 "Cannot have void typed arguments!");
289 ArgumentList.push_back(new Argument(FT->getParamType(i)));
292 // Clear the lazy arguments bit.
293 unsigned SDC = getSubclassDataFromValue();
294 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
297 size_t Function::arg_size() const {
298 return getFunctionType()->getNumParams();
300 bool Function::arg_empty() const {
301 return getFunctionType()->getNumParams() == 0;
304 void Function::setParent(Module *parent) {
306 LeakDetector::addGarbageObject(this);
309 LeakDetector::removeGarbageObject(this);
312 // dropAllReferences() - This function causes all the subinstructions to "let
313 // go" of all references that they are maintaining. This allows one to
314 // 'delete' a whole class at a time, even though there may be circular
315 // references... first all references are dropped, and all use counts go to
316 // zero. Then everything is deleted for real. Note that no operations are
317 // valid on an object that has "dropped all references", except operator
320 void Function::dropAllReferences() {
321 for (iterator I = begin(), E = end(); I != E; ++I)
322 I->dropAllReferences();
324 // Delete all basic blocks. They are now unused, except possibly by
325 // blockaddresses, but BasicBlock's destructor takes care of those.
326 while (!BasicBlocks.empty())
327 BasicBlocks.begin()->eraseFromParent();
329 // Prefix data is stored in a side table.
330 setPrefixData(nullptr);
333 void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
334 AttributeSet PAL = getAttributes();
335 PAL = PAL.addAttribute(getContext(), i, attr);
339 void Function::addAttributes(unsigned i, AttributeSet attrs) {
340 AttributeSet PAL = getAttributes();
341 PAL = PAL.addAttributes(getContext(), i, attrs);
345 void Function::removeAttributes(unsigned i, AttributeSet attrs) {
346 AttributeSet PAL = getAttributes();
347 PAL = PAL.removeAttributes(getContext(), i, attrs);
351 // Maintain the GC name for each function in an on-the-side table. This saves
352 // allocating an additional word in Function for programs which do not use GC
353 // (i.e., most programs) at the cost of increased overhead for clients which do
355 static DenseMap<const Function*,PooledStringPtr> *GCNames;
356 static StringPool *GCNamePool;
357 static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
359 bool Function::hasGC() const {
360 sys::SmartScopedReader<true> Reader(*GCLock);
361 return GCNames && GCNames->count(this);
364 const char *Function::getGC() const {
365 assert(hasGC() && "Function has no collector");
366 sys::SmartScopedReader<true> Reader(*GCLock);
367 return *(*GCNames)[this];
370 void Function::setGC(const char *Str) {
371 sys::SmartScopedWriter<true> Writer(*GCLock);
373 GCNamePool = new StringPool();
375 GCNames = new DenseMap<const Function*,PooledStringPtr>();
376 (*GCNames)[this] = GCNamePool->intern(Str);
379 void Function::clearGC() {
380 sys::SmartScopedWriter<true> Writer(*GCLock);
382 GCNames->erase(this);
383 if (GCNames->empty()) {
386 if (GCNamePool->empty()) {
388 GCNamePool = nullptr;
394 /// copyAttributesFrom - copy all additional attributes (those not needed to
395 /// create a Function) from the Function Src to this one.
396 void Function::copyAttributesFrom(const GlobalValue *Src) {
397 assert(isa<Function>(Src) && "Expected a Function!");
398 GlobalObject::copyAttributesFrom(Src);
399 const Function *SrcF = cast<Function>(Src);
400 setCallingConv(SrcF->getCallingConv());
401 setAttributes(SrcF->getAttributes());
403 setGC(SrcF->getGC());
406 if (SrcF->hasPrefixData())
407 setPrefixData(SrcF->getPrefixData());
409 setPrefixData(nullptr);
412 /// getIntrinsicID - This method returns the ID number of the specified
413 /// function, or Intrinsic::not_intrinsic if the function is not an
414 /// intrinsic, or if the pointer is null. This value is always defined to be
415 /// zero to allow easy checking for whether a function is intrinsic or not. The
416 /// particular intrinsic functions which correspond to this value are defined in
417 /// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent
418 /// requests for the same ID return results much faster from the cache.
420 unsigned Function::getIntrinsicID() const {
421 const ValueName *ValName = this->getValueName();
422 if (!ValName || !isIntrinsic())
425 LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
426 getContext().pImpl->IntrinsicIDCache;
427 if (!IntrinsicIDCache.count(this)) {
428 unsigned Id = lookupIntrinsicID();
429 IntrinsicIDCache[this]=Id;
432 return IntrinsicIDCache[this];
435 /// This private method does the actual lookup of an intrinsic ID when the query
436 /// could not be answered from the cache.
437 unsigned Function::lookupIntrinsicID() const {
438 const ValueName *ValName = this->getValueName();
439 unsigned Len = ValName->getKeyLength();
440 const char *Name = ValName->getKeyData();
442 #define GET_FUNCTION_RECOGNIZER
443 #include "llvm/IR/Intrinsics.gen"
444 #undef GET_FUNCTION_RECOGNIZER
449 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
450 assert(id < num_intrinsics && "Invalid intrinsic ID!");
451 static const char * const Table[] = {
453 #define GET_INTRINSIC_NAME_TABLE
454 #include "llvm/IR/Intrinsics.gen"
455 #undef GET_INTRINSIC_NAME_TABLE
459 std::string Result(Table[id]);
460 for (unsigned i = 0; i < Tys.size(); ++i) {
461 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
462 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
463 EVT::getEVT(PTyp->getElementType()).getEVTString();
466 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
472 /// IIT_Info - These are enumerators that describe the entries returned by the
473 /// getIntrinsicInfoTableEntries function.
475 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
477 // Common values should be encoded with 0-15.
495 // Values from 16+ are only encodable with the inefficient encoding.
498 IIT_EMPTYSTRUCT = 18,
508 IIT_HALF_VEC_ARG = 28
512 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
513 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
514 IIT_Info Info = IIT_Info(Infos[NextElt++]);
515 unsigned StructElts = 2;
516 using namespace Intrinsic;
520 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
523 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
526 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
529 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
532 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
535 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
538 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
541 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
544 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
547 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
550 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
553 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
556 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
557 DecodeIITType(NextElt, Infos, OutputTable);
560 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
561 DecodeIITType(NextElt, Infos, OutputTable);
564 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
565 DecodeIITType(NextElt, Infos, OutputTable);
568 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
569 DecodeIITType(NextElt, Infos, OutputTable);
572 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
573 DecodeIITType(NextElt, Infos, OutputTable);
576 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
577 DecodeIITType(NextElt, Infos, OutputTable);
580 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
581 DecodeIITType(NextElt, Infos, OutputTable);
583 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
584 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
586 DecodeIITType(NextElt, Infos, OutputTable);
590 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
591 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
594 case IIT_EXTEND_ARG: {
595 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
596 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
600 case IIT_TRUNC_ARG: {
601 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
602 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
606 case IIT_HALF_VEC_ARG: {
607 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
608 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
612 case IIT_EMPTYSTRUCT:
613 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
615 case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
616 case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
617 case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
619 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
621 for (unsigned i = 0; i != StructElts; ++i)
622 DecodeIITType(NextElt, Infos, OutputTable);
626 llvm_unreachable("unhandled");
630 #define GET_INTRINSIC_GENERATOR_GLOBAL
631 #include "llvm/IR/Intrinsics.gen"
632 #undef GET_INTRINSIC_GENERATOR_GLOBAL
634 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
635 SmallVectorImpl<IITDescriptor> &T){
636 // Check to see if the intrinsic's type was expressible by the table.
637 unsigned TableVal = IIT_Table[id-1];
639 // Decode the TableVal into an array of IITValues.
640 SmallVector<unsigned char, 8> IITValues;
641 ArrayRef<unsigned char> IITEntries;
642 unsigned NextElt = 0;
643 if ((TableVal >> 31) != 0) {
644 // This is an offset into the IIT_LongEncodingTable.
645 IITEntries = IIT_LongEncodingTable;
647 // Strip sentinel bit.
648 NextElt = (TableVal << 1) >> 1;
650 // Decode the TableVal into an array of IITValues. If the entry was encoded
651 // into a single word in the table itself, decode it now.
653 IITValues.push_back(TableVal & 0xF);
657 IITEntries = IITValues;
661 // Okay, decode the table into the output vector of IITDescriptors.
662 DecodeIITType(NextElt, IITEntries, T);
663 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
664 DecodeIITType(NextElt, IITEntries, T);
668 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
669 ArrayRef<Type*> Tys, LLVMContext &Context) {
670 using namespace Intrinsic;
671 IITDescriptor D = Infos.front();
672 Infos = Infos.slice(1);
675 case IITDescriptor::Void: return Type::getVoidTy(Context);
676 case IITDescriptor::VarArg: return Type::getVoidTy(Context);
677 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
678 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
679 case IITDescriptor::Half: return Type::getHalfTy(Context);
680 case IITDescriptor::Float: return Type::getFloatTy(Context);
681 case IITDescriptor::Double: return Type::getDoubleTy(Context);
683 case IITDescriptor::Integer:
684 return IntegerType::get(Context, D.Integer_Width);
685 case IITDescriptor::Vector:
686 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
687 case IITDescriptor::Pointer:
688 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
689 D.Pointer_AddressSpace);
690 case IITDescriptor::Struct: {
692 assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
693 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
694 Elts[i] = DecodeFixedType(Infos, Tys, Context);
695 return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements));
698 case IITDescriptor::Argument:
699 return Tys[D.getArgumentNumber()];
700 case IITDescriptor::ExtendArgument: {
701 Type *Ty = Tys[D.getArgumentNumber()];
702 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
703 return VectorType::getExtendedElementVectorType(VTy);
705 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
707 case IITDescriptor::TruncArgument: {
708 Type *Ty = Tys[D.getArgumentNumber()];
709 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
710 return VectorType::getTruncatedElementVectorType(VTy);
712 IntegerType *ITy = cast<IntegerType>(Ty);
713 assert(ITy->getBitWidth() % 2 == 0);
714 return IntegerType::get(Context, ITy->getBitWidth() / 2);
716 case IITDescriptor::HalfVecArgument:
717 return VectorType::getHalfElementsVectorType(cast<VectorType>(
718 Tys[D.getArgumentNumber()]));
720 llvm_unreachable("unhandled");
725 FunctionType *Intrinsic::getType(LLVMContext &Context,
726 ID id, ArrayRef<Type*> Tys) {
727 SmallVector<IITDescriptor, 8> Table;
728 getIntrinsicInfoTableEntries(id, Table);
730 ArrayRef<IITDescriptor> TableRef = Table;
731 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
733 SmallVector<Type*, 8> ArgTys;
734 while (!TableRef.empty())
735 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
737 return FunctionType::get(ResultTy, ArgTys, false);
740 bool Intrinsic::isOverloaded(ID id) {
741 #define GET_INTRINSIC_OVERLOAD_TABLE
742 #include "llvm/IR/Intrinsics.gen"
743 #undef GET_INTRINSIC_OVERLOAD_TABLE
746 /// This defines the "Intrinsic::getAttributes(ID id)" method.
747 #define GET_INTRINSIC_ATTRIBUTES
748 #include "llvm/IR/Intrinsics.gen"
749 #undef GET_INTRINSIC_ATTRIBUTES
751 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
752 // There can never be multiple globals with the same name of different types,
753 // because intrinsics must be a specific type.
755 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
756 getType(M->getContext(), id, Tys)));
759 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
760 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
761 #include "llvm/IR/Intrinsics.gen"
762 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
764 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
765 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
766 #include "llvm/IR/Intrinsics.gen"
767 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
769 /// hasAddressTaken - returns true if there are any uses of this function
770 /// other than direct calls or invokes to it.
771 bool Function::hasAddressTaken(const User* *PutOffender) const {
772 for (const Use &U : uses()) {
773 const User *FU = U.getUser();
774 if (isa<BlockAddress>(FU))
776 if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU))
777 return PutOffender ? (*PutOffender = FU, true) : true;
778 ImmutableCallSite CS(cast<Instruction>(FU));
779 if (!CS.isCallee(&U))
780 return PutOffender ? (*PutOffender = FU, true) : true;
785 bool Function::isDefTriviallyDead() const {
787 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
788 !hasAvailableExternallyLinkage())
791 // Check if the function is used by anything other than a blockaddress.
792 for (const User *U : users())
793 if (!isa<BlockAddress>(U))
799 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
800 /// setjmp or other function that gcc recognizes as "returning twice".
801 bool Function::callsFunctionThatReturnsTwice() const {
802 for (const_inst_iterator
803 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
804 ImmutableCallSite CS(&*I);
805 if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
812 Constant *Function::getPrefixData() const {
813 assert(hasPrefixData());
814 const LLVMContextImpl::PrefixDataMapTy &PDMap =
815 getContext().pImpl->PrefixDataMap;
816 assert(PDMap.find(this) != PDMap.end());
817 return cast<Constant>(PDMap.find(this)->second->getReturnValue());
820 void Function::setPrefixData(Constant *PrefixData) {
821 if (!PrefixData && !hasPrefixData())
824 unsigned SCData = getSubclassDataFromValue();
825 LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap;
826 ReturnInst *&PDHolder = PDMap[this];
829 PDHolder->setOperand(0, PrefixData);
831 PDHolder = ReturnInst::Create(getContext(), PrefixData);
838 setValueSubclassData(SCData);