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