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/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/Module.h" 27 #include "llvm/Support/ManagedStatic.h" 28 #include "llvm/Support/RWMutex.h" 29 #include "llvm/Support/StringPool.h" 30 #include "llvm/Support/Threading.h" 31 using namespace llvm; 32 33 // Explicit instantiations of SymbolTableListTraits since some of the methods 34 // are not in the public header file... 35 template class llvm::SymbolTableListTraits<Argument, Function>; 36 template class llvm::SymbolTableListTraits<BasicBlock, Function>; 37 38 //===----------------------------------------------------------------------===// 39 // Argument Implementation 40 //===----------------------------------------------------------------------===// 41 42 void Argument::anchor() { } 43 44 Argument::Argument(Type *Ty, const Twine &Name, Function *Par) 45 : Value(Ty, Value::ArgumentVal) { 46 Parent = nullptr; 47 48 if (Par) 49 Par->getArgumentList().push_back(this); 50 setName(Name); 51 } 52 53 void Argument::setParent(Function *parent) { 54 Parent = parent; 55 } 56 57 /// getArgNo - Return the index of this formal argument in its containing 58 /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. 59 unsigned Argument::getArgNo() const { 60 const Function *F = getParent(); 61 assert(F && "Argument is not in a function"); 62 63 Function::const_arg_iterator AI = F->arg_begin(); 64 unsigned ArgIdx = 0; 65 for (; &*AI != this; ++AI) 66 ++ArgIdx; 67 68 return ArgIdx; 69 } 70 71 /// hasNonNullAttr - Return true if this argument has the nonnull attribute on 72 /// it in its containing function. Also returns true if at least one byte is 73 /// known to be dereferenceable and the pointer is in addrspace(0). 74 bool Argument::hasNonNullAttr() const { 75 if (!getType()->isPointerTy()) return false; 76 if (getParent()->getAttributes(). 77 hasAttribute(getArgNo()+1, Attribute::NonNull)) 78 return true; 79 else if (getDereferenceableBytes() > 0 && 80 getType()->getPointerAddressSpace() == 0) 81 return true; 82 return false; 83 } 84 85 /// hasByValAttr - Return true if this argument has the byval attribute on it 86 /// in its containing function. 87 bool Argument::hasByValAttr() const { 88 if (!getType()->isPointerTy()) return false; 89 return getParent()->getAttributes(). 90 hasAttribute(getArgNo()+1, Attribute::ByVal); 91 } 92 93 /// \brief Return true if this argument has the inalloca attribute on it in 94 /// its containing function. 95 bool Argument::hasInAllocaAttr() const { 96 if (!getType()->isPointerTy()) return false; 97 return getParent()->getAttributes(). 98 hasAttribute(getArgNo()+1, Attribute::InAlloca); 99 } 100 101 bool Argument::hasByValOrInAllocaAttr() const { 102 if (!getType()->isPointerTy()) return false; 103 AttributeSet Attrs = getParent()->getAttributes(); 104 return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) || 105 Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca); 106 } 107 108 unsigned Argument::getParamAlignment() const { 109 assert(getType()->isPointerTy() && "Only pointers have alignments"); 110 return getParent()->getParamAlignment(getArgNo()+1); 111 112 } 113 114 uint64_t Argument::getDereferenceableBytes() const { 115 assert(getType()->isPointerTy() && 116 "Only pointers have dereferenceable bytes"); 117 return getParent()->getDereferenceableBytes(getArgNo()+1); 118 } 119 120 /// hasNestAttr - Return true if this argument has the nest attribute on 121 /// it in its containing function. 122 bool Argument::hasNestAttr() const { 123 if (!getType()->isPointerTy()) return false; 124 return getParent()->getAttributes(). 125 hasAttribute(getArgNo()+1, Attribute::Nest); 126 } 127 128 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on 129 /// it in its containing function. 130 bool Argument::hasNoAliasAttr() const { 131 if (!getType()->isPointerTy()) return false; 132 return getParent()->getAttributes(). 133 hasAttribute(getArgNo()+1, Attribute::NoAlias); 134 } 135 136 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute 137 /// on it in its containing function. 138 bool Argument::hasNoCaptureAttr() const { 139 if (!getType()->isPointerTy()) return false; 140 return getParent()->getAttributes(). 141 hasAttribute(getArgNo()+1, Attribute::NoCapture); 142 } 143 144 /// hasSRetAttr - Return true if this argument has the sret attribute on 145 /// it in its containing function. 146 bool Argument::hasStructRetAttr() const { 147 if (!getType()->isPointerTy()) return false; 148 if (this != getParent()->arg_begin()) 149 return false; // StructRet param must be first param 150 return getParent()->getAttributes(). 151 hasAttribute(1, Attribute::StructRet); 152 } 153 154 /// hasReturnedAttr - Return true if this argument has the returned attribute on 155 /// it in its containing function. 156 bool Argument::hasReturnedAttr() const { 157 return getParent()->getAttributes(). 158 hasAttribute(getArgNo()+1, Attribute::Returned); 159 } 160 161 /// hasZExtAttr - Return true if this argument has the zext attribute on it in 162 /// its containing function. 163 bool Argument::hasZExtAttr() const { 164 return getParent()->getAttributes(). 165 hasAttribute(getArgNo()+1, Attribute::ZExt); 166 } 167 168 /// hasSExtAttr Return true if this argument has the sext attribute on it in its 169 /// containing function. 170 bool Argument::hasSExtAttr() const { 171 return getParent()->getAttributes(). 172 hasAttribute(getArgNo()+1, Attribute::SExt); 173 } 174 175 /// Return true if this argument has the readonly or readnone attribute on it 176 /// in its containing function. 177 bool Argument::onlyReadsMemory() const { 178 return getParent()->getAttributes(). 179 hasAttribute(getArgNo()+1, Attribute::ReadOnly) || 180 getParent()->getAttributes(). 181 hasAttribute(getArgNo()+1, Attribute::ReadNone); 182 } 183 184 /// addAttr - Add attributes to an argument. 185 void Argument::addAttr(AttributeSet AS) { 186 assert(AS.getNumSlots() <= 1 && 187 "Trying to add more than one attribute set to an argument!"); 188 AttrBuilder B(AS, AS.getSlotIndex(0)); 189 getParent()->addAttributes(getArgNo() + 1, 190 AttributeSet::get(Parent->getContext(), 191 getArgNo() + 1, B)); 192 } 193 194 /// removeAttr - Remove attributes from an argument. 195 void Argument::removeAttr(AttributeSet AS) { 196 assert(AS.getNumSlots() <= 1 && 197 "Trying to remove more than one attribute set from an argument!"); 198 AttrBuilder B(AS, AS.getSlotIndex(0)); 199 getParent()->removeAttributes(getArgNo() + 1, 200 AttributeSet::get(Parent->getContext(), 201 getArgNo() + 1, B)); 202 } 203 204 //===----------------------------------------------------------------------===// 205 // Helper Methods in Function 206 //===----------------------------------------------------------------------===// 207 208 bool Function::isMaterializable() const { 209 return getGlobalObjectSubClassData(); 210 } 211 212 void Function::setIsMaterializable(bool V) { setGlobalObjectSubClassData(V); } 213 214 LLVMContext &Function::getContext() const { 215 return getType()->getContext(); 216 } 217 218 FunctionType *Function::getFunctionType() const { return Ty; } 219 220 bool Function::isVarArg() const { 221 return getFunctionType()->isVarArg(); 222 } 223 224 Type *Function::getReturnType() const { 225 return getFunctionType()->getReturnType(); 226 } 227 228 void Function::removeFromParent() { 229 getParent()->getFunctionList().remove(this); 230 } 231 232 void Function::eraseFromParent() { 233 getParent()->getFunctionList().erase(this); 234 } 235 236 //===----------------------------------------------------------------------===// 237 // Function Implementation 238 //===----------------------------------------------------------------------===// 239 240 Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name, 241 Module *ParentModule) 242 : GlobalObject(PointerType::getUnqual(Ty), Value::FunctionVal, nullptr, 0, 243 Linkage, name), 244 Ty(Ty) { 245 assert(FunctionType::isValidReturnType(getReturnType()) && 246 "invalid return type"); 247 setIsMaterializable(false); 248 SymTab = new ValueSymbolTable(); 249 250 // If the function has arguments, mark them as lazily built. 251 if (Ty->getNumParams()) 252 setValueSubclassData(1); // Set the "has lazy arguments" bit. 253 254 if (ParentModule) 255 ParentModule->getFunctionList().push_back(this); 256 257 // Ensure intrinsics have the right parameter attributes. 258 if (unsigned IID = getIntrinsicID()) 259 setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID))); 260 261 } 262 263 Function::~Function() { 264 dropAllReferences(); // After this it is safe to delete instructions. 265 266 // Delete all of the method arguments and unlink from symbol table... 267 ArgumentList.clear(); 268 delete SymTab; 269 270 // Remove the function from the on-the-side GC table. 271 clearGC(); 272 273 // Remove the intrinsicID from the Cache. 274 if (getValueName() && isIntrinsic()) 275 getContext().pImpl->IntrinsicIDCache.erase(this); 276 } 277 278 void Function::BuildLazyArguments() const { 279 // Create the arguments vector, all arguments start out unnamed. 280 FunctionType *FT = getFunctionType(); 281 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { 282 assert(!FT->getParamType(i)->isVoidTy() && 283 "Cannot have void typed arguments!"); 284 ArgumentList.push_back(new Argument(FT->getParamType(i))); 285 } 286 287 // Clear the lazy arguments bit. 288 unsigned SDC = getSubclassDataFromValue(); 289 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0)); 290 } 291 292 size_t Function::arg_size() const { 293 return getFunctionType()->getNumParams(); 294 } 295 bool Function::arg_empty() const { 296 return getFunctionType()->getNumParams() == 0; 297 } 298 299 void Function::setParent(Module *parent) { 300 Parent = parent; 301 } 302 303 // dropAllReferences() - This function causes all the subinstructions to "let 304 // go" of all references that they are maintaining. This allows one to 305 // 'delete' a whole class at a time, even though there may be circular 306 // references... first all references are dropped, and all use counts go to 307 // zero. Then everything is deleted for real. Note that no operations are 308 // valid on an object that has "dropped all references", except operator 309 // delete. 310 // 311 void Function::dropAllReferences() { 312 setIsMaterializable(false); 313 314 for (iterator I = begin(), E = end(); I != E; ++I) 315 I->dropAllReferences(); 316 317 // Delete all basic blocks. They are now unused, except possibly by 318 // blockaddresses, but BasicBlock's destructor takes care of those. 319 while (!BasicBlocks.empty()) 320 BasicBlocks.begin()->eraseFromParent(); 321 322 // Prefix and prologue data are stored in a side table. 323 setPrefixData(nullptr); 324 setPrologueData(nullptr); 325 } 326 327 void Function::addAttribute(unsigned i, Attribute::AttrKind attr) { 328 AttributeSet PAL = getAttributes(); 329 PAL = PAL.addAttribute(getContext(), i, attr); 330 setAttributes(PAL); 331 } 332 333 void Function::addAttributes(unsigned i, AttributeSet attrs) { 334 AttributeSet PAL = getAttributes(); 335 PAL = PAL.addAttributes(getContext(), i, attrs); 336 setAttributes(PAL); 337 } 338 339 void Function::removeAttributes(unsigned i, AttributeSet attrs) { 340 AttributeSet PAL = getAttributes(); 341 PAL = PAL.removeAttributes(getContext(), i, attrs); 342 setAttributes(PAL); 343 } 344 345 void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) { 346 AttributeSet PAL = getAttributes(); 347 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); 348 setAttributes(PAL); 349 } 350 351 void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { 352 AttributeSet PAL = getAttributes(); 353 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); 354 setAttributes(PAL); 355 } 356 357 // Maintain the GC name for each function in an on-the-side table. This saves 358 // allocating an additional word in Function for programs which do not use GC 359 // (i.e., most programs) at the cost of increased overhead for clients which do 360 // use GC. 361 static DenseMap<const Function*,PooledStringPtr> *GCNames; 362 static StringPool *GCNamePool; 363 static ManagedStatic<sys::SmartRWMutex<true> > GCLock; 364 365 bool Function::hasGC() const { 366 sys::SmartScopedReader<true> Reader(*GCLock); 367 return GCNames && GCNames->count(this); 368 } 369 370 const char *Function::getGC() const { 371 assert(hasGC() && "Function has no collector"); 372 sys::SmartScopedReader<true> Reader(*GCLock); 373 return *(*GCNames)[this]; 374 } 375 376 void Function::setGC(const char *Str) { 377 sys::SmartScopedWriter<true> Writer(*GCLock); 378 if (!GCNamePool) 379 GCNamePool = new StringPool(); 380 if (!GCNames) 381 GCNames = new DenseMap<const Function*,PooledStringPtr>(); 382 (*GCNames)[this] = GCNamePool->intern(Str); 383 } 384 385 void Function::clearGC() { 386 sys::SmartScopedWriter<true> Writer(*GCLock); 387 if (GCNames) { 388 GCNames->erase(this); 389 if (GCNames->empty()) { 390 delete GCNames; 391 GCNames = nullptr; 392 if (GCNamePool->empty()) { 393 delete GCNamePool; 394 GCNamePool = nullptr; 395 } 396 } 397 } 398 } 399 400 /// copyAttributesFrom - copy all additional attributes (those not needed to 401 /// create a Function) from the Function Src to this one. 402 void Function::copyAttributesFrom(const GlobalValue *Src) { 403 assert(isa<Function>(Src) && "Expected a Function!"); 404 GlobalObject::copyAttributesFrom(Src); 405 const Function *SrcF = cast<Function>(Src); 406 setCallingConv(SrcF->getCallingConv()); 407 setAttributes(SrcF->getAttributes()); 408 if (SrcF->hasGC()) 409 setGC(SrcF->getGC()); 410 else 411 clearGC(); 412 if (SrcF->hasPrefixData()) 413 setPrefixData(SrcF->getPrefixData()); 414 else 415 setPrefixData(nullptr); 416 if (SrcF->hasPrologueData()) 417 setPrologueData(SrcF->getPrologueData()); 418 else 419 setPrologueData(nullptr); 420 } 421 422 /// getIntrinsicID - This method returns the ID number of the specified 423 /// function, or Intrinsic::not_intrinsic if the function is not an 424 /// intrinsic, or if the pointer is null. This value is always defined to be 425 /// zero to allow easy checking for whether a function is intrinsic or not. The 426 /// particular intrinsic functions which correspond to this value are defined in 427 /// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent 428 /// requests for the same ID return results much faster from the cache. 429 /// 430 unsigned Function::getIntrinsicID() const { 431 const ValueName *ValName = this->getValueName(); 432 if (!ValName || !isIntrinsic()) 433 return 0; 434 435 LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache = 436 getContext().pImpl->IntrinsicIDCache; 437 if (!IntrinsicIDCache.count(this)) { 438 unsigned Id = lookupIntrinsicID(); 439 IntrinsicIDCache[this]=Id; 440 return Id; 441 } 442 return IntrinsicIDCache[this]; 443 } 444 445 /// This private method does the actual lookup of an intrinsic ID when the query 446 /// could not be answered from the cache. 447 unsigned Function::lookupIntrinsicID() const { 448 const ValueName *ValName = this->getValueName(); 449 unsigned Len = ValName->getKeyLength(); 450 const char *Name = ValName->getKeyData(); 451 452 #define GET_FUNCTION_RECOGNIZER 453 #include "llvm/IR/Intrinsics.gen" 454 #undef GET_FUNCTION_RECOGNIZER 455 456 return 0; 457 } 458 459 /// Returns a stable mangling for the type specified for use in the name 460 /// mangling scheme used by 'any' types in intrinsic signatures. The mangling 461 /// of named types is simply their name. Manglings for unnamed types consist 462 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) 463 /// combined with the mangling of their component types. A vararg function 464 /// type will have a suffix of 'vararg'. Since function types can contain 465 /// other function types, we close a function type mangling with suffix 'f' 466 /// which can't be confused with it's prefix. This ensures we don't have 467 /// collisions between two unrelated function types. Otherwise, you might 468 /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) 469 /// Manglings of integers, floats, and vectors ('i', 'f', and 'v' prefix in most 470 /// cases) fall back to the MVT codepath, where they could be mangled to 471 /// 'x86mmx', for example; matching on derived types is not sufficient to mangle 472 /// everything. 473 static std::string getMangledTypeStr(Type* Ty) { 474 std::string Result; 475 if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) { 476 Result += "p" + llvm::utostr(PTyp->getAddressSpace()) + 477 getMangledTypeStr(PTyp->getElementType()); 478 } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) { 479 Result += "a" + llvm::utostr(ATyp->getNumElements()) + 480 getMangledTypeStr(ATyp->getElementType()); 481 } else if (StructType* STyp = dyn_cast<StructType>(Ty)) { 482 if (!STyp->isLiteral()) 483 Result += STyp->getName(); 484 else 485 llvm_unreachable("TODO: implement literal types"); 486 } else if (FunctionType* FT = dyn_cast<FunctionType>(Ty)) { 487 Result += "f_" + getMangledTypeStr(FT->getReturnType()); 488 for (size_t i = 0; i < FT->getNumParams(); i++) 489 Result += getMangledTypeStr(FT->getParamType(i)); 490 if (FT->isVarArg()) 491 Result += "vararg"; 492 // Ensure nested function types are distinguishable. 493 Result += "f"; 494 } else if (Ty) 495 Result += EVT::getEVT(Ty).getEVTString(); 496 return Result; 497 } 498 499 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { 500 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 501 static const char * const Table[] = { 502 "not_intrinsic", 503 #define GET_INTRINSIC_NAME_TABLE 504 #include "llvm/IR/Intrinsics.gen" 505 #undef GET_INTRINSIC_NAME_TABLE 506 }; 507 if (Tys.empty()) 508 return Table[id]; 509 std::string Result(Table[id]); 510 for (unsigned i = 0; i < Tys.size(); ++i) { 511 Result += "." + getMangledTypeStr(Tys[i]); 512 } 513 return Result; 514 } 515 516 517 /// IIT_Info - These are enumerators that describe the entries returned by the 518 /// getIntrinsicInfoTableEntries function. 519 /// 520 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter! 521 enum IIT_Info { 522 // Common values should be encoded with 0-15. 523 IIT_Done = 0, 524 IIT_I1 = 1, 525 IIT_I8 = 2, 526 IIT_I16 = 3, 527 IIT_I32 = 4, 528 IIT_I64 = 5, 529 IIT_F16 = 6, 530 IIT_F32 = 7, 531 IIT_F64 = 8, 532 IIT_V2 = 9, 533 IIT_V4 = 10, 534 IIT_V8 = 11, 535 IIT_V16 = 12, 536 IIT_V32 = 13, 537 IIT_PTR = 14, 538 IIT_ARG = 15, 539 540 // Values from 16+ are only encodable with the inefficient encoding. 541 IIT_V64 = 16, 542 IIT_MMX = 17, 543 IIT_METADATA = 18, 544 IIT_EMPTYSTRUCT = 19, 545 IIT_STRUCT2 = 20, 546 IIT_STRUCT3 = 21, 547 IIT_STRUCT4 = 22, 548 IIT_STRUCT5 = 23, 549 IIT_EXTEND_ARG = 24, 550 IIT_TRUNC_ARG = 25, 551 IIT_ANYPTR = 26, 552 IIT_V1 = 27, 553 IIT_VARARG = 28, 554 IIT_HALF_VEC_ARG = 29, 555 IIT_SAME_VEC_WIDTH_ARG = 30, 556 IIT_PTR_TO_ARG = 31, 557 IIT_VEC_OF_PTRS_TO_ELT = 32 558 }; 559 560 561 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, 562 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { 563 IIT_Info Info = IIT_Info(Infos[NextElt++]); 564 unsigned StructElts = 2; 565 using namespace Intrinsic; 566 567 switch (Info) { 568 case IIT_Done: 569 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); 570 return; 571 case IIT_VARARG: 572 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0)); 573 return; 574 case IIT_MMX: 575 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); 576 return; 577 case IIT_METADATA: 578 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); 579 return; 580 case IIT_F16: 581 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); 582 return; 583 case IIT_F32: 584 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); 585 return; 586 case IIT_F64: 587 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); 588 return; 589 case IIT_I1: 590 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); 591 return; 592 case IIT_I8: 593 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); 594 return; 595 case IIT_I16: 596 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16)); 597 return; 598 case IIT_I32: 599 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); 600 return; 601 case IIT_I64: 602 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); 603 return; 604 case IIT_V1: 605 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1)); 606 DecodeIITType(NextElt, Infos, OutputTable); 607 return; 608 case IIT_V2: 609 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2)); 610 DecodeIITType(NextElt, Infos, OutputTable); 611 return; 612 case IIT_V4: 613 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4)); 614 DecodeIITType(NextElt, Infos, OutputTable); 615 return; 616 case IIT_V8: 617 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8)); 618 DecodeIITType(NextElt, Infos, OutputTable); 619 return; 620 case IIT_V16: 621 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16)); 622 DecodeIITType(NextElt, Infos, OutputTable); 623 return; 624 case IIT_V32: 625 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32)); 626 DecodeIITType(NextElt, Infos, OutputTable); 627 return; 628 case IIT_V64: 629 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64)); 630 DecodeIITType(NextElt, Infos, OutputTable); 631 return; 632 case IIT_PTR: 633 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); 634 DecodeIITType(NextElt, Infos, OutputTable); 635 return; 636 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype] 637 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 638 Infos[NextElt++])); 639 DecodeIITType(NextElt, Infos, OutputTable); 640 return; 641 } 642 case IIT_ARG: { 643 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 644 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); 645 return; 646 } 647 case IIT_EXTEND_ARG: { 648 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 649 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument, 650 ArgInfo)); 651 return; 652 } 653 case IIT_TRUNC_ARG: { 654 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 655 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument, 656 ArgInfo)); 657 return; 658 } 659 case IIT_HALF_VEC_ARG: { 660 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 661 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument, 662 ArgInfo)); 663 return; 664 } 665 case IIT_SAME_VEC_WIDTH_ARG: { 666 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 667 OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument, 668 ArgInfo)); 669 return; 670 } 671 case IIT_PTR_TO_ARG: { 672 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 673 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument, 674 ArgInfo)); 675 return; 676 } 677 case IIT_VEC_OF_PTRS_TO_ELT: { 678 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 679 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfPtrsToElt, 680 ArgInfo)); 681 return; 682 } 683 case IIT_EMPTYSTRUCT: 684 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); 685 return; 686 case IIT_STRUCT5: ++StructElts; // FALL THROUGH. 687 case IIT_STRUCT4: ++StructElts; // FALL THROUGH. 688 case IIT_STRUCT3: ++StructElts; // FALL THROUGH. 689 case IIT_STRUCT2: { 690 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts)); 691 692 for (unsigned i = 0; i != StructElts; ++i) 693 DecodeIITType(NextElt, Infos, OutputTable); 694 return; 695 } 696 } 697 llvm_unreachable("unhandled"); 698 } 699 700 701 #define GET_INTRINSIC_GENERATOR_GLOBAL 702 #include "llvm/IR/Intrinsics.gen" 703 #undef GET_INTRINSIC_GENERATOR_GLOBAL 704 705 void Intrinsic::getIntrinsicInfoTableEntries(ID id, 706 SmallVectorImpl<IITDescriptor> &T){ 707 // Check to see if the intrinsic's type was expressible by the table. 708 unsigned TableVal = IIT_Table[id-1]; 709 710 // Decode the TableVal into an array of IITValues. 711 SmallVector<unsigned char, 8> IITValues; 712 ArrayRef<unsigned char> IITEntries; 713 unsigned NextElt = 0; 714 if ((TableVal >> 31) != 0) { 715 // This is an offset into the IIT_LongEncodingTable. 716 IITEntries = IIT_LongEncodingTable; 717 718 // Strip sentinel bit. 719 NextElt = (TableVal << 1) >> 1; 720 } else { 721 // Decode the TableVal into an array of IITValues. If the entry was encoded 722 // into a single word in the table itself, decode it now. 723 do { 724 IITValues.push_back(TableVal & 0xF); 725 TableVal >>= 4; 726 } while (TableVal); 727 728 IITEntries = IITValues; 729 NextElt = 0; 730 } 731 732 // Okay, decode the table into the output vector of IITDescriptors. 733 DecodeIITType(NextElt, IITEntries, T); 734 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) 735 DecodeIITType(NextElt, IITEntries, T); 736 } 737 738 739 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, 740 ArrayRef<Type*> Tys, LLVMContext &Context) { 741 using namespace Intrinsic; 742 IITDescriptor D = Infos.front(); 743 Infos = Infos.slice(1); 744 745 switch (D.Kind) { 746 case IITDescriptor::Void: return Type::getVoidTy(Context); 747 case IITDescriptor::VarArg: return Type::getVoidTy(Context); 748 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context); 749 case IITDescriptor::Metadata: return Type::getMetadataTy(Context); 750 case IITDescriptor::Half: return Type::getHalfTy(Context); 751 case IITDescriptor::Float: return Type::getFloatTy(Context); 752 case IITDescriptor::Double: return Type::getDoubleTy(Context); 753 754 case IITDescriptor::Integer: 755 return IntegerType::get(Context, D.Integer_Width); 756 case IITDescriptor::Vector: 757 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width); 758 case IITDescriptor::Pointer: 759 return PointerType::get(DecodeFixedType(Infos, Tys, Context), 760 D.Pointer_AddressSpace); 761 case IITDescriptor::Struct: { 762 Type *Elts[5]; 763 assert(D.Struct_NumElements <= 5 && "Can't handle this yet"); 764 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 765 Elts[i] = DecodeFixedType(Infos, Tys, Context); 766 return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements)); 767 } 768 769 case IITDescriptor::Argument: 770 return Tys[D.getArgumentNumber()]; 771 case IITDescriptor::ExtendArgument: { 772 Type *Ty = Tys[D.getArgumentNumber()]; 773 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 774 return VectorType::getExtendedElementVectorType(VTy); 775 776 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth()); 777 } 778 case IITDescriptor::TruncArgument: { 779 Type *Ty = Tys[D.getArgumentNumber()]; 780 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 781 return VectorType::getTruncatedElementVectorType(VTy); 782 783 IntegerType *ITy = cast<IntegerType>(Ty); 784 assert(ITy->getBitWidth() % 2 == 0); 785 return IntegerType::get(Context, ITy->getBitWidth() / 2); 786 } 787 case IITDescriptor::HalfVecArgument: 788 return VectorType::getHalfElementsVectorType(cast<VectorType>( 789 Tys[D.getArgumentNumber()])); 790 case IITDescriptor::SameVecWidthArgument: { 791 Type *EltTy = DecodeFixedType(Infos, Tys, Context); 792 Type *Ty = Tys[D.getArgumentNumber()]; 793 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { 794 return VectorType::get(EltTy, VTy->getNumElements()); 795 } 796 llvm_unreachable("unhandled"); 797 } 798 case IITDescriptor::PtrToArgument: { 799 Type *Ty = Tys[D.getArgumentNumber()]; 800 return PointerType::getUnqual(Ty); 801 } 802 case IITDescriptor::VecOfPtrsToElt: { 803 Type *Ty = Tys[D.getArgumentNumber()]; 804 VectorType *VTy = dyn_cast<VectorType>(Ty); 805 if (!VTy) 806 llvm_unreachable("Expected an argument of Vector Type"); 807 Type *EltTy = VTy->getVectorElementType(); 808 return VectorType::get(PointerType::getUnqual(EltTy), 809 VTy->getNumElements()); 810 } 811 } 812 llvm_unreachable("unhandled"); 813 } 814 815 816 817 FunctionType *Intrinsic::getType(LLVMContext &Context, 818 ID id, ArrayRef<Type*> Tys) { 819 SmallVector<IITDescriptor, 8> Table; 820 getIntrinsicInfoTableEntries(id, Table); 821 822 ArrayRef<IITDescriptor> TableRef = Table; 823 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); 824 825 SmallVector<Type*, 8> ArgTys; 826 while (!TableRef.empty()) 827 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); 828 829 // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg 830 // If we see void type as the type of the last argument, it is vararg intrinsic 831 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) { 832 ArgTys.pop_back(); 833 return FunctionType::get(ResultTy, ArgTys, true); 834 } 835 return FunctionType::get(ResultTy, ArgTys, false); 836 } 837 838 bool Intrinsic::isOverloaded(ID id) { 839 #define GET_INTRINSIC_OVERLOAD_TABLE 840 #include "llvm/IR/Intrinsics.gen" 841 #undef GET_INTRINSIC_OVERLOAD_TABLE 842 } 843 844 /// This defines the "Intrinsic::getAttributes(ID id)" method. 845 #define GET_INTRINSIC_ATTRIBUTES 846 #include "llvm/IR/Intrinsics.gen" 847 #undef GET_INTRINSIC_ATTRIBUTES 848 849 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { 850 // There can never be multiple globals with the same name of different types, 851 // because intrinsics must be a specific type. 852 return 853 cast<Function>(M->getOrInsertFunction(getName(id, Tys), 854 getType(M->getContext(), id, Tys))); 855 } 856 857 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. 858 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 859 #include "llvm/IR/Intrinsics.gen" 860 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 861 862 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method. 863 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 864 #include "llvm/IR/Intrinsics.gen" 865 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 866 867 /// hasAddressTaken - returns true if there are any uses of this function 868 /// other than direct calls or invokes to it. 869 bool Function::hasAddressTaken(const User* *PutOffender) const { 870 for (const Use &U : uses()) { 871 const User *FU = U.getUser(); 872 if (isa<BlockAddress>(FU)) 873 continue; 874 if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU)) 875 return PutOffender ? (*PutOffender = FU, true) : true; 876 ImmutableCallSite CS(cast<Instruction>(FU)); 877 if (!CS.isCallee(&U)) 878 return PutOffender ? (*PutOffender = FU, true) : true; 879 } 880 return false; 881 } 882 883 bool Function::isDefTriviallyDead() const { 884 // Check the linkage 885 if (!hasLinkOnceLinkage() && !hasLocalLinkage() && 886 !hasAvailableExternallyLinkage()) 887 return false; 888 889 // Check if the function is used by anything other than a blockaddress. 890 for (const User *U : users()) 891 if (!isa<BlockAddress>(U)) 892 return false; 893 894 return true; 895 } 896 897 /// callsFunctionThatReturnsTwice - Return true if the function has a call to 898 /// setjmp or other function that gcc recognizes as "returning twice". 899 bool Function::callsFunctionThatReturnsTwice() const { 900 for (const_inst_iterator 901 I = inst_begin(this), E = inst_end(this); I != E; ++I) { 902 ImmutableCallSite CS(&*I); 903 if (CS && CS.hasFnAttr(Attribute::ReturnsTwice)) 904 return true; 905 } 906 907 return false; 908 } 909 910 Constant *Function::getPrefixData() const { 911 assert(hasPrefixData()); 912 const LLVMContextImpl::PrefixDataMapTy &PDMap = 913 getContext().pImpl->PrefixDataMap; 914 assert(PDMap.find(this) != PDMap.end()); 915 return cast<Constant>(PDMap.find(this)->second->getReturnValue()); 916 } 917 918 void Function::setPrefixData(Constant *PrefixData) { 919 if (!PrefixData && !hasPrefixData()) 920 return; 921 922 unsigned SCData = getSubclassDataFromValue(); 923 LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap; 924 ReturnInst *&PDHolder = PDMap[this]; 925 if (PrefixData) { 926 if (PDHolder) 927 PDHolder->setOperand(0, PrefixData); 928 else 929 PDHolder = ReturnInst::Create(getContext(), PrefixData); 930 SCData |= (1<<1); 931 } else { 932 delete PDHolder; 933 PDMap.erase(this); 934 SCData &= ~(1<<1); 935 } 936 setValueSubclassData(SCData); 937 } 938 939 Constant *Function::getPrologueData() const { 940 assert(hasPrologueData()); 941 const LLVMContextImpl::PrologueDataMapTy &SOMap = 942 getContext().pImpl->PrologueDataMap; 943 assert(SOMap.find(this) != SOMap.end()); 944 return cast<Constant>(SOMap.find(this)->second->getReturnValue()); 945 } 946 947 void Function::setPrologueData(Constant *PrologueData) { 948 if (!PrologueData && !hasPrologueData()) 949 return; 950 951 unsigned PDData = getSubclassDataFromValue(); 952 LLVMContextImpl::PrologueDataMapTy &PDMap = getContext().pImpl->PrologueDataMap; 953 ReturnInst *&PDHolder = PDMap[this]; 954 if (PrologueData) { 955 if (PDHolder) 956 PDHolder->setOperand(0, PrologueData); 957 else 958 PDHolder = ReturnInst::Create(getContext(), PrologueData); 959 PDData |= (1<<2); 960 } else { 961 delete PDHolder; 962 PDMap.erase(this); 963 PDData &= ~(1<<2); 964 } 965 setValueSubclassData(PDData); 966 } 967