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