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