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