1 //===- Function.cpp - Implement the Global object classes -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the Function class for the IR library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/IR/Function.h" 14 #include "SymbolTableListTraitsImpl.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/ADT/None.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/IR/Argument.h" 24 #include "llvm/IR/Attributes.h" 25 #include "llvm/IR/BasicBlock.h" 26 #include "llvm/IR/Constant.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/GlobalValue.h" 30 #include "llvm/IR/InstIterator.h" 31 #include "llvm/IR/Instruction.h" 32 #include "llvm/IR/Instructions.h" 33 #include "llvm/IR/IntrinsicInst.h" 34 #include "llvm/IR/Intrinsics.h" 35 #include "llvm/IR/LLVMContext.h" 36 #include "llvm/IR/MDBuilder.h" 37 #include "llvm/IR/Metadata.h" 38 #include "llvm/IR/Module.h" 39 #include "llvm/IR/SymbolTableListTraits.h" 40 #include "llvm/IR/Type.h" 41 #include "llvm/IR/Use.h" 42 #include "llvm/IR/User.h" 43 #include "llvm/IR/Value.h" 44 #include "llvm/IR/ValueSymbolTable.h" 45 #include "llvm/Support/Casting.h" 46 #include "llvm/Support/Compiler.h" 47 #include "llvm/Support/ErrorHandling.h" 48 #include <algorithm> 49 #include <cassert> 50 #include <cstddef> 51 #include <cstdint> 52 #include <cstring> 53 #include <string> 54 55 using namespace llvm; 56 using ProfileCount = Function::ProfileCount; 57 58 // Explicit instantiations of SymbolTableListTraits since some of the methods 59 // are not in the public header file... 60 template class llvm::SymbolTableListTraits<BasicBlock>; 61 62 //===----------------------------------------------------------------------===// 63 // Argument Implementation 64 //===----------------------------------------------------------------------===// 65 66 Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo) 67 : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) { 68 setName(Name); 69 } 70 71 void Argument::setParent(Function *parent) { 72 Parent = parent; 73 } 74 75 bool Argument::hasNonNullAttr() const { 76 if (!getType()->isPointerTy()) return false; 77 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull)) 78 return true; 79 else if (getDereferenceableBytes() > 0 && 80 !NullPointerIsDefined(getParent(), 81 getType()->getPointerAddressSpace())) 82 return true; 83 return false; 84 } 85 86 bool Argument::hasByValAttr() const { 87 if (!getType()->isPointerTy()) return false; 88 return hasAttribute(Attribute::ByVal); 89 } 90 91 bool Argument::hasSwiftSelfAttr() const { 92 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf); 93 } 94 95 bool Argument::hasSwiftErrorAttr() const { 96 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError); 97 } 98 99 bool Argument::hasInAllocaAttr() const { 100 if (!getType()->isPointerTy()) return false; 101 return hasAttribute(Attribute::InAlloca); 102 } 103 104 bool Argument::hasByValOrInAllocaAttr() const { 105 if (!getType()->isPointerTy()) return false; 106 AttributeList Attrs = getParent()->getAttributes(); 107 return Attrs.hasParamAttribute(getArgNo(), Attribute::ByVal) || 108 Attrs.hasParamAttribute(getArgNo(), Attribute::InAlloca); 109 } 110 111 unsigned Argument::getParamAlignment() const { 112 assert(getType()->isPointerTy() && "Only pointers have alignments"); 113 return getParent()->getParamAlignment(getArgNo()); 114 } 115 116 Type *Argument::getParamByValType() const { 117 assert(getType()->isPointerTy() && "Only pointers have byval types"); 118 return getParent()->getParamByValType(getArgNo()); 119 } 120 121 uint64_t Argument::getDereferenceableBytes() const { 122 assert(getType()->isPointerTy() && 123 "Only pointers have dereferenceable bytes"); 124 return getParent()->getParamDereferenceableBytes(getArgNo()); 125 } 126 127 uint64_t Argument::getDereferenceableOrNullBytes() const { 128 assert(getType()->isPointerTy() && 129 "Only pointers have dereferenceable bytes"); 130 return getParent()->getParamDereferenceableOrNullBytes(getArgNo()); 131 } 132 133 bool Argument::hasNestAttr() const { 134 if (!getType()->isPointerTy()) return false; 135 return hasAttribute(Attribute::Nest); 136 } 137 138 bool Argument::hasNoAliasAttr() const { 139 if (!getType()->isPointerTy()) return false; 140 return hasAttribute(Attribute::NoAlias); 141 } 142 143 bool Argument::hasNoCaptureAttr() const { 144 if (!getType()->isPointerTy()) return false; 145 return hasAttribute(Attribute::NoCapture); 146 } 147 148 bool Argument::hasStructRetAttr() const { 149 if (!getType()->isPointerTy()) return false; 150 return hasAttribute(Attribute::StructRet); 151 } 152 153 bool Argument::hasInRegAttr() const { 154 return hasAttribute(Attribute::InReg); 155 } 156 157 bool Argument::hasReturnedAttr() const { 158 return hasAttribute(Attribute::Returned); 159 } 160 161 bool Argument::hasZExtAttr() const { 162 return hasAttribute(Attribute::ZExt); 163 } 164 165 bool Argument::hasSExtAttr() const { 166 return hasAttribute(Attribute::SExt); 167 } 168 169 bool Argument::onlyReadsMemory() const { 170 AttributeList Attrs = getParent()->getAttributes(); 171 return Attrs.hasParamAttribute(getArgNo(), Attribute::ReadOnly) || 172 Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone); 173 } 174 175 void Argument::addAttrs(AttrBuilder &B) { 176 AttributeList AL = getParent()->getAttributes(); 177 AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B); 178 getParent()->setAttributes(AL); 179 } 180 181 void Argument::addAttr(Attribute::AttrKind Kind) { 182 getParent()->addParamAttr(getArgNo(), Kind); 183 } 184 185 void Argument::addAttr(Attribute Attr) { 186 getParent()->addParamAttr(getArgNo(), Attr); 187 } 188 189 void Argument::removeAttr(Attribute::AttrKind Kind) { 190 getParent()->removeParamAttr(getArgNo(), Kind); 191 } 192 193 bool Argument::hasAttribute(Attribute::AttrKind Kind) const { 194 return getParent()->hasParamAttribute(getArgNo(), Kind); 195 } 196 197 //===----------------------------------------------------------------------===// 198 // Helper Methods in Function 199 //===----------------------------------------------------------------------===// 200 201 LLVMContext &Function::getContext() const { 202 return getType()->getContext(); 203 } 204 205 unsigned Function::getInstructionCount() const { 206 unsigned NumInstrs = 0; 207 for (const BasicBlock &BB : BasicBlocks) 208 NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(), 209 BB.instructionsWithoutDebug().end()); 210 return NumInstrs; 211 } 212 213 Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage, 214 const Twine &N, Module &M) { 215 return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M); 216 } 217 218 void Function::removeFromParent() { 219 getParent()->getFunctionList().remove(getIterator()); 220 } 221 222 void Function::eraseFromParent() { 223 getParent()->getFunctionList().erase(getIterator()); 224 } 225 226 //===----------------------------------------------------------------------===// 227 // Function Implementation 228 //===----------------------------------------------------------------------===// 229 230 static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) { 231 // If AS == -1 and we are passed a valid module pointer we place the function 232 // in the program address space. Otherwise we default to AS0. 233 if (AddrSpace == static_cast<unsigned>(-1)) 234 return M ? M->getDataLayout().getProgramAddressSpace() : 0; 235 return AddrSpace; 236 } 237 238 Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, 239 const Twine &name, Module *ParentModule) 240 : GlobalObject(Ty, Value::FunctionVal, 241 OperandTraits<Function>::op_begin(this), 0, Linkage, name, 242 computeAddrSpace(AddrSpace, ParentModule)), 243 NumArgs(Ty->getNumParams()) { 244 assert(FunctionType::isValidReturnType(getReturnType()) && 245 "invalid return type"); 246 setGlobalObjectSubClassData(0); 247 248 // We only need a symbol table for a function if the context keeps value names 249 if (!getContext().shouldDiscardValueNames()) 250 SymTab = make_unique<ValueSymbolTable>(); 251 252 // If the function has arguments, mark them as lazily built. 253 if (Ty->getNumParams()) 254 setValueSubclassData(1); // Set the "has lazy arguments" bit. 255 256 if (ParentModule) 257 ParentModule->getFunctionList().push_back(this); 258 259 HasLLVMReservedName = getName().startswith("llvm."); 260 // Ensure intrinsics have the right parameter attributes. 261 // Note, the IntID field will have been set in Value::setName if this function 262 // name is a valid intrinsic ID. 263 if (IntID) 264 setAttributes(Intrinsic::getAttributes(getContext(), IntID)); 265 } 266 267 Function::~Function() { 268 dropAllReferences(); // After this it is safe to delete instructions. 269 270 // Delete all of the method arguments and unlink from symbol table... 271 if (Arguments) 272 clearArguments(); 273 274 // Remove the function from the on-the-side GC table. 275 clearGC(); 276 } 277 278 void Function::BuildLazyArguments() const { 279 // Create the arguments vector, all arguments start out unnamed. 280 auto *FT = getFunctionType(); 281 if (NumArgs > 0) { 282 Arguments = std::allocator<Argument>().allocate(NumArgs); 283 for (unsigned i = 0, e = NumArgs; i != e; ++i) { 284 Type *ArgTy = FT->getParamType(i); 285 assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!"); 286 new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i); 287 } 288 } 289 290 // Clear the lazy arguments bit. 291 unsigned SDC = getSubclassDataFromValue(); 292 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0)); 293 assert(!hasLazyArguments()); 294 } 295 296 static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) { 297 return MutableArrayRef<Argument>(Args, Count); 298 } 299 300 void Function::clearArguments() { 301 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 302 A.setName(""); 303 A.~Argument(); 304 } 305 std::allocator<Argument>().deallocate(Arguments, NumArgs); 306 Arguments = nullptr; 307 } 308 309 void Function::stealArgumentListFrom(Function &Src) { 310 assert(isDeclaration() && "Expected no references to current arguments"); 311 312 // Drop the current arguments, if any, and set the lazy argument bit. 313 if (!hasLazyArguments()) { 314 assert(llvm::all_of(makeArgArray(Arguments, NumArgs), 315 [](const Argument &A) { return A.use_empty(); }) && 316 "Expected arguments to be unused in declaration"); 317 clearArguments(); 318 setValueSubclassData(getSubclassDataFromValue() | (1 << 0)); 319 } 320 321 // Nothing to steal if Src has lazy arguments. 322 if (Src.hasLazyArguments()) 323 return; 324 325 // Steal arguments from Src, and fix the lazy argument bits. 326 assert(arg_size() == Src.arg_size()); 327 Arguments = Src.Arguments; 328 Src.Arguments = nullptr; 329 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 330 // FIXME: This does the work of transferNodesFromList inefficiently. 331 SmallString<128> Name; 332 if (A.hasName()) 333 Name = A.getName(); 334 if (!Name.empty()) 335 A.setName(""); 336 A.setParent(this); 337 if (!Name.empty()) 338 A.setName(Name); 339 } 340 341 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0)); 342 assert(!hasLazyArguments()); 343 Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0)); 344 } 345 346 // dropAllReferences() - This function causes all the subinstructions to "let 347 // go" of all references that they are maintaining. This allows one to 348 // 'delete' a whole class at a time, even though there may be circular 349 // references... first all references are dropped, and all use counts go to 350 // zero. Then everything is deleted for real. Note that no operations are 351 // valid on an object that has "dropped all references", except operator 352 // delete. 353 // 354 void Function::dropAllReferences() { 355 setIsMaterializable(false); 356 357 for (BasicBlock &BB : *this) 358 BB.dropAllReferences(); 359 360 // Delete all basic blocks. They are now unused, except possibly by 361 // blockaddresses, but BasicBlock's destructor takes care of those. 362 while (!BasicBlocks.empty()) 363 BasicBlocks.begin()->eraseFromParent(); 364 365 // Drop uses of any optional data (real or placeholder). 366 if (getNumOperands()) { 367 User::dropAllReferences(); 368 setNumHungOffUseOperands(0); 369 setValueSubclassData(getSubclassDataFromValue() & ~0xe); 370 } 371 372 // Metadata is stored in a side-table. 373 clearMetadata(); 374 } 375 376 void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) { 377 AttributeList PAL = getAttributes(); 378 PAL = PAL.addAttribute(getContext(), i, Kind); 379 setAttributes(PAL); 380 } 381 382 void Function::addAttribute(unsigned i, Attribute Attr) { 383 AttributeList PAL = getAttributes(); 384 PAL = PAL.addAttribute(getContext(), i, Attr); 385 setAttributes(PAL); 386 } 387 388 void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) { 389 AttributeList PAL = getAttributes(); 390 PAL = PAL.addAttributes(getContext(), i, Attrs); 391 setAttributes(PAL); 392 } 393 394 void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 395 AttributeList PAL = getAttributes(); 396 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind); 397 setAttributes(PAL); 398 } 399 400 void Function::addParamAttr(unsigned ArgNo, Attribute Attr) { 401 AttributeList PAL = getAttributes(); 402 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr); 403 setAttributes(PAL); 404 } 405 406 void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { 407 AttributeList PAL = getAttributes(); 408 PAL = PAL.addParamAttributes(getContext(), ArgNo, Attrs); 409 setAttributes(PAL); 410 } 411 412 void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) { 413 AttributeList PAL = getAttributes(); 414 PAL = PAL.removeAttribute(getContext(), i, Kind); 415 setAttributes(PAL); 416 } 417 418 void Function::removeAttribute(unsigned i, StringRef Kind) { 419 AttributeList PAL = getAttributes(); 420 PAL = PAL.removeAttribute(getContext(), i, Kind); 421 setAttributes(PAL); 422 } 423 424 void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) { 425 AttributeList PAL = getAttributes(); 426 PAL = PAL.removeAttributes(getContext(), i, Attrs); 427 setAttributes(PAL); 428 } 429 430 void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 431 AttributeList PAL = getAttributes(); 432 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); 433 setAttributes(PAL); 434 } 435 436 void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) { 437 AttributeList PAL = getAttributes(); 438 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind); 439 setAttributes(PAL); 440 } 441 442 void Function::removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { 443 AttributeList PAL = getAttributes(); 444 PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs); 445 setAttributes(PAL); 446 } 447 448 void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) { 449 AttributeList PAL = getAttributes(); 450 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); 451 setAttributes(PAL); 452 } 453 454 void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) { 455 AttributeList PAL = getAttributes(); 456 PAL = PAL.addDereferenceableParamAttr(getContext(), ArgNo, Bytes); 457 setAttributes(PAL); 458 } 459 460 void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { 461 AttributeList PAL = getAttributes(); 462 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); 463 setAttributes(PAL); 464 } 465 466 void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo, 467 uint64_t Bytes) { 468 AttributeList PAL = getAttributes(); 469 PAL = PAL.addDereferenceableOrNullParamAttr(getContext(), ArgNo, Bytes); 470 setAttributes(PAL); 471 } 472 473 const std::string &Function::getGC() const { 474 assert(hasGC() && "Function has no collector"); 475 return getContext().getGC(*this); 476 } 477 478 void Function::setGC(std::string Str) { 479 setValueSubclassDataBit(14, !Str.empty()); 480 getContext().setGC(*this, std::move(Str)); 481 } 482 483 void Function::clearGC() { 484 if (!hasGC()) 485 return; 486 getContext().deleteGC(*this); 487 setValueSubclassDataBit(14, false); 488 } 489 490 /// Copy all additional attributes (those not needed to create a Function) from 491 /// the Function Src to this one. 492 void Function::copyAttributesFrom(const Function *Src) { 493 GlobalObject::copyAttributesFrom(Src); 494 setCallingConv(Src->getCallingConv()); 495 setAttributes(Src->getAttributes()); 496 if (Src->hasGC()) 497 setGC(Src->getGC()); 498 else 499 clearGC(); 500 if (Src->hasPersonalityFn()) 501 setPersonalityFn(Src->getPersonalityFn()); 502 if (Src->hasPrefixData()) 503 setPrefixData(Src->getPrefixData()); 504 if (Src->hasPrologueData()) 505 setPrologueData(Src->getPrologueData()); 506 } 507 508 /// Table of string intrinsic names indexed by enum value. 509 static const char * const IntrinsicNameTable[] = { 510 "not_intrinsic", 511 #define GET_INTRINSIC_NAME_TABLE 512 #include "llvm/IR/IntrinsicImpl.inc" 513 #undef GET_INTRINSIC_NAME_TABLE 514 }; 515 516 /// Table of per-target intrinsic name tables. 517 #define GET_INTRINSIC_TARGET_DATA 518 #include "llvm/IR/IntrinsicImpl.inc" 519 #undef GET_INTRINSIC_TARGET_DATA 520 521 /// Find the segment of \c IntrinsicNameTable for intrinsics with the same 522 /// target as \c Name, or the generic table if \c Name is not target specific. 523 /// 524 /// Returns the relevant slice of \c IntrinsicNameTable 525 static ArrayRef<const char *> findTargetSubtable(StringRef Name) { 526 assert(Name.startswith("llvm.")); 527 528 ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos); 529 // Drop "llvm." and take the first dotted component. That will be the target 530 // if this is target specific. 531 StringRef Target = Name.drop_front(5).split('.').first; 532 auto It = std::lower_bound(Targets.begin(), Targets.end(), Target, 533 [](const IntrinsicTargetInfo &TI, 534 StringRef Target) { return TI.Name < Target; }); 535 // We've either found the target or just fall back to the generic set, which 536 // is always first. 537 const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0]; 538 return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count); 539 } 540 541 /// This does the actual lookup of an intrinsic ID which 542 /// matches the given function name. 543 Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) { 544 ArrayRef<const char *> NameTable = findTargetSubtable(Name); 545 int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name); 546 if (Idx == -1) 547 return Intrinsic::not_intrinsic; 548 549 // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have 550 // an index into a sub-table. 551 int Adjust = NameTable.data() - IntrinsicNameTable; 552 Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust); 553 554 // If the intrinsic is not overloaded, require an exact match. If it is 555 // overloaded, require either exact or prefix match. 556 const auto MatchSize = strlen(NameTable[Idx]); 557 assert(Name.size() >= MatchSize && "Expected either exact or prefix match"); 558 bool IsExactMatch = Name.size() == MatchSize; 559 return IsExactMatch || isOverloaded(ID) ? ID : Intrinsic::not_intrinsic; 560 } 561 562 void Function::recalculateIntrinsicID() { 563 StringRef Name = getName(); 564 if (!Name.startswith("llvm.")) { 565 HasLLVMReservedName = false; 566 IntID = Intrinsic::not_intrinsic; 567 return; 568 } 569 HasLLVMReservedName = true; 570 IntID = lookupIntrinsicID(Name); 571 } 572 573 /// Returns a stable mangling for the type specified for use in the name 574 /// mangling scheme used by 'any' types in intrinsic signatures. The mangling 575 /// of named types is simply their name. Manglings for unnamed types consist 576 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) 577 /// combined with the mangling of their component types. A vararg function 578 /// type will have a suffix of 'vararg'. Since function types can contain 579 /// other function types, we close a function type mangling with suffix 'f' 580 /// which can't be confused with it's prefix. This ensures we don't have 581 /// collisions between two unrelated function types. Otherwise, you might 582 /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) 583 /// 584 static std::string getMangledTypeStr(Type* Ty) { 585 std::string Result; 586 if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) { 587 Result += "p" + utostr(PTyp->getAddressSpace()) + 588 getMangledTypeStr(PTyp->getElementType()); 589 } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) { 590 Result += "a" + utostr(ATyp->getNumElements()) + 591 getMangledTypeStr(ATyp->getElementType()); 592 } else if (StructType *STyp = dyn_cast<StructType>(Ty)) { 593 if (!STyp->isLiteral()) { 594 Result += "s_"; 595 Result += STyp->getName(); 596 } else { 597 Result += "sl_"; 598 for (auto Elem : STyp->elements()) 599 Result += getMangledTypeStr(Elem); 600 } 601 // Ensure nested structs are distinguishable. 602 Result += "s"; 603 } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) { 604 Result += "f_" + getMangledTypeStr(FT->getReturnType()); 605 for (size_t i = 0; i < FT->getNumParams(); i++) 606 Result += getMangledTypeStr(FT->getParamType(i)); 607 if (FT->isVarArg()) 608 Result += "vararg"; 609 // Ensure nested function types are distinguishable. 610 Result += "f"; 611 } else if (isa<VectorType>(Ty)) { 612 Result += "v" + utostr(Ty->getVectorNumElements()) + 613 getMangledTypeStr(Ty->getVectorElementType()); 614 } else if (Ty) { 615 switch (Ty->getTypeID()) { 616 default: llvm_unreachable("Unhandled type"); 617 case Type::VoidTyID: Result += "isVoid"; break; 618 case Type::MetadataTyID: Result += "Metadata"; break; 619 case Type::HalfTyID: Result += "f16"; break; 620 case Type::FloatTyID: Result += "f32"; break; 621 case Type::DoubleTyID: Result += "f64"; break; 622 case Type::X86_FP80TyID: Result += "f80"; break; 623 case Type::FP128TyID: Result += "f128"; break; 624 case Type::PPC_FP128TyID: Result += "ppcf128"; break; 625 case Type::X86_MMXTyID: Result += "x86mmx"; break; 626 case Type::IntegerTyID: 627 Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth()); 628 break; 629 } 630 } 631 return Result; 632 } 633 634 StringRef Intrinsic::getName(ID id) { 635 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 636 assert(!isOverloaded(id) && 637 "This version of getName does not support overloading"); 638 return IntrinsicNameTable[id]; 639 } 640 641 std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { 642 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 643 std::string Result(IntrinsicNameTable[id]); 644 for (Type *Ty : Tys) { 645 Result += "." + getMangledTypeStr(Ty); 646 } 647 return Result; 648 } 649 650 /// IIT_Info - These are enumerators that describe the entries returned by the 651 /// getIntrinsicInfoTableEntries function. 652 /// 653 /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter! 654 enum IIT_Info { 655 // Common values should be encoded with 0-15. 656 IIT_Done = 0, 657 IIT_I1 = 1, 658 IIT_I8 = 2, 659 IIT_I16 = 3, 660 IIT_I32 = 4, 661 IIT_I64 = 5, 662 IIT_F16 = 6, 663 IIT_F32 = 7, 664 IIT_F64 = 8, 665 IIT_V2 = 9, 666 IIT_V4 = 10, 667 IIT_V8 = 11, 668 IIT_V16 = 12, 669 IIT_V32 = 13, 670 IIT_PTR = 14, 671 IIT_ARG = 15, 672 673 // Values from 16+ are only encodable with the inefficient encoding. 674 IIT_V64 = 16, 675 IIT_MMX = 17, 676 IIT_TOKEN = 18, 677 IIT_METADATA = 19, 678 IIT_EMPTYSTRUCT = 20, 679 IIT_STRUCT2 = 21, 680 IIT_STRUCT3 = 22, 681 IIT_STRUCT4 = 23, 682 IIT_STRUCT5 = 24, 683 IIT_EXTEND_ARG = 25, 684 IIT_TRUNC_ARG = 26, 685 IIT_ANYPTR = 27, 686 IIT_V1 = 28, 687 IIT_VARARG = 29, 688 IIT_HALF_VEC_ARG = 30, 689 IIT_SAME_VEC_WIDTH_ARG = 31, 690 IIT_PTR_TO_ARG = 32, 691 IIT_PTR_TO_ELT = 33, 692 IIT_VEC_OF_ANYPTRS_TO_ELT = 34, 693 IIT_I128 = 35, 694 IIT_V512 = 36, 695 IIT_V1024 = 37, 696 IIT_STRUCT6 = 38, 697 IIT_STRUCT7 = 39, 698 IIT_STRUCT8 = 40, 699 IIT_F128 = 41 700 }; 701 702 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, 703 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { 704 using namespace Intrinsic; 705 706 IIT_Info Info = IIT_Info(Infos[NextElt++]); 707 unsigned StructElts = 2; 708 709 switch (Info) { 710 case IIT_Done: 711 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); 712 return; 713 case IIT_VARARG: 714 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0)); 715 return; 716 case IIT_MMX: 717 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); 718 return; 719 case IIT_TOKEN: 720 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0)); 721 return; 722 case IIT_METADATA: 723 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); 724 return; 725 case IIT_F16: 726 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); 727 return; 728 case IIT_F32: 729 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); 730 return; 731 case IIT_F64: 732 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); 733 return; 734 case IIT_F128: 735 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0)); 736 return; 737 case IIT_I1: 738 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); 739 return; 740 case IIT_I8: 741 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); 742 return; 743 case IIT_I16: 744 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16)); 745 return; 746 case IIT_I32: 747 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); 748 return; 749 case IIT_I64: 750 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); 751 return; 752 case IIT_I128: 753 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128)); 754 return; 755 case IIT_V1: 756 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1)); 757 DecodeIITType(NextElt, Infos, OutputTable); 758 return; 759 case IIT_V2: 760 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2)); 761 DecodeIITType(NextElt, Infos, OutputTable); 762 return; 763 case IIT_V4: 764 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4)); 765 DecodeIITType(NextElt, Infos, OutputTable); 766 return; 767 case IIT_V8: 768 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8)); 769 DecodeIITType(NextElt, Infos, OutputTable); 770 return; 771 case IIT_V16: 772 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16)); 773 DecodeIITType(NextElt, Infos, OutputTable); 774 return; 775 case IIT_V32: 776 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32)); 777 DecodeIITType(NextElt, Infos, OutputTable); 778 return; 779 case IIT_V64: 780 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64)); 781 DecodeIITType(NextElt, Infos, OutputTable); 782 return; 783 case IIT_V512: 784 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512)); 785 DecodeIITType(NextElt, Infos, OutputTable); 786 return; 787 case IIT_V1024: 788 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024)); 789 DecodeIITType(NextElt, Infos, OutputTable); 790 return; 791 case IIT_PTR: 792 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); 793 DecodeIITType(NextElt, Infos, OutputTable); 794 return; 795 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype] 796 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 797 Infos[NextElt++])); 798 DecodeIITType(NextElt, Infos, OutputTable); 799 return; 800 } 801 case IIT_ARG: { 802 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 803 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); 804 return; 805 } 806 case IIT_EXTEND_ARG: { 807 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 808 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument, 809 ArgInfo)); 810 return; 811 } 812 case IIT_TRUNC_ARG: { 813 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 814 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument, 815 ArgInfo)); 816 return; 817 } 818 case IIT_HALF_VEC_ARG: { 819 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 820 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument, 821 ArgInfo)); 822 return; 823 } 824 case IIT_SAME_VEC_WIDTH_ARG: { 825 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 826 OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument, 827 ArgInfo)); 828 return; 829 } 830 case IIT_PTR_TO_ARG: { 831 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 832 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument, 833 ArgInfo)); 834 return; 835 } 836 case IIT_PTR_TO_ELT: { 837 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 838 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToElt, ArgInfo)); 839 return; 840 } 841 case IIT_VEC_OF_ANYPTRS_TO_ELT: { 842 unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 843 unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 844 OutputTable.push_back( 845 IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo)); 846 return; 847 } 848 case IIT_EMPTYSTRUCT: 849 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); 850 return; 851 case IIT_STRUCT8: ++StructElts; LLVM_FALLTHROUGH; 852 case IIT_STRUCT7: ++StructElts; LLVM_FALLTHROUGH; 853 case IIT_STRUCT6: ++StructElts; LLVM_FALLTHROUGH; 854 case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH; 855 case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH; 856 case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH; 857 case IIT_STRUCT2: { 858 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts)); 859 860 for (unsigned i = 0; i != StructElts; ++i) 861 DecodeIITType(NextElt, Infos, OutputTable); 862 return; 863 } 864 } 865 llvm_unreachable("unhandled"); 866 } 867 868 #define GET_INTRINSIC_GENERATOR_GLOBAL 869 #include "llvm/IR/IntrinsicImpl.inc" 870 #undef GET_INTRINSIC_GENERATOR_GLOBAL 871 872 void Intrinsic::getIntrinsicInfoTableEntries(ID id, 873 SmallVectorImpl<IITDescriptor> &T){ 874 // Check to see if the intrinsic's type was expressible by the table. 875 unsigned TableVal = IIT_Table[id-1]; 876 877 // Decode the TableVal into an array of IITValues. 878 SmallVector<unsigned char, 8> IITValues; 879 ArrayRef<unsigned char> IITEntries; 880 unsigned NextElt = 0; 881 if ((TableVal >> 31) != 0) { 882 // This is an offset into the IIT_LongEncodingTable. 883 IITEntries = IIT_LongEncodingTable; 884 885 // Strip sentinel bit. 886 NextElt = (TableVal << 1) >> 1; 887 } else { 888 // Decode the TableVal into an array of IITValues. If the entry was encoded 889 // into a single word in the table itself, decode it now. 890 do { 891 IITValues.push_back(TableVal & 0xF); 892 TableVal >>= 4; 893 } while (TableVal); 894 895 IITEntries = IITValues; 896 NextElt = 0; 897 } 898 899 // Okay, decode the table into the output vector of IITDescriptors. 900 DecodeIITType(NextElt, IITEntries, T); 901 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) 902 DecodeIITType(NextElt, IITEntries, T); 903 } 904 905 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, 906 ArrayRef<Type*> Tys, LLVMContext &Context) { 907 using namespace Intrinsic; 908 909 IITDescriptor D = Infos.front(); 910 Infos = Infos.slice(1); 911 912 switch (D.Kind) { 913 case IITDescriptor::Void: return Type::getVoidTy(Context); 914 case IITDescriptor::VarArg: return Type::getVoidTy(Context); 915 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context); 916 case IITDescriptor::Token: return Type::getTokenTy(Context); 917 case IITDescriptor::Metadata: return Type::getMetadataTy(Context); 918 case IITDescriptor::Half: return Type::getHalfTy(Context); 919 case IITDescriptor::Float: return Type::getFloatTy(Context); 920 case IITDescriptor::Double: return Type::getDoubleTy(Context); 921 case IITDescriptor::Quad: return Type::getFP128Ty(Context); 922 923 case IITDescriptor::Integer: 924 return IntegerType::get(Context, D.Integer_Width); 925 case IITDescriptor::Vector: 926 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width); 927 case IITDescriptor::Pointer: 928 return PointerType::get(DecodeFixedType(Infos, Tys, Context), 929 D.Pointer_AddressSpace); 930 case IITDescriptor::Struct: { 931 SmallVector<Type *, 8> Elts; 932 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 933 Elts.push_back(DecodeFixedType(Infos, Tys, Context)); 934 return StructType::get(Context, Elts); 935 } 936 case IITDescriptor::Argument: 937 return Tys[D.getArgumentNumber()]; 938 case IITDescriptor::ExtendArgument: { 939 Type *Ty = Tys[D.getArgumentNumber()]; 940 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 941 return VectorType::getExtendedElementVectorType(VTy); 942 943 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth()); 944 } 945 case IITDescriptor::TruncArgument: { 946 Type *Ty = Tys[D.getArgumentNumber()]; 947 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 948 return VectorType::getTruncatedElementVectorType(VTy); 949 950 IntegerType *ITy = cast<IntegerType>(Ty); 951 assert(ITy->getBitWidth() % 2 == 0); 952 return IntegerType::get(Context, ITy->getBitWidth() / 2); 953 } 954 case IITDescriptor::HalfVecArgument: 955 return VectorType::getHalfElementsVectorType(cast<VectorType>( 956 Tys[D.getArgumentNumber()])); 957 case IITDescriptor::SameVecWidthArgument: { 958 Type *EltTy = DecodeFixedType(Infos, Tys, Context); 959 Type *Ty = Tys[D.getArgumentNumber()]; 960 if (auto *VTy = dyn_cast<VectorType>(Ty)) 961 return VectorType::get(EltTy, VTy->getNumElements()); 962 return EltTy; 963 } 964 case IITDescriptor::PtrToArgument: { 965 Type *Ty = Tys[D.getArgumentNumber()]; 966 return PointerType::getUnqual(Ty); 967 } 968 case IITDescriptor::PtrToElt: { 969 Type *Ty = Tys[D.getArgumentNumber()]; 970 VectorType *VTy = dyn_cast<VectorType>(Ty); 971 if (!VTy) 972 llvm_unreachable("Expected an argument of Vector Type"); 973 Type *EltTy = VTy->getVectorElementType(); 974 return PointerType::getUnqual(EltTy); 975 } 976 case IITDescriptor::VecOfAnyPtrsToElt: 977 // Return the overloaded type (which determines the pointers address space) 978 return Tys[D.getOverloadArgNumber()]; 979 } 980 llvm_unreachable("unhandled"); 981 } 982 983 FunctionType *Intrinsic::getType(LLVMContext &Context, 984 ID id, ArrayRef<Type*> Tys) { 985 SmallVector<IITDescriptor, 8> Table; 986 getIntrinsicInfoTableEntries(id, Table); 987 988 ArrayRef<IITDescriptor> TableRef = Table; 989 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); 990 991 SmallVector<Type*, 8> ArgTys; 992 while (!TableRef.empty()) 993 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); 994 995 // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg 996 // If we see void type as the type of the last argument, it is vararg intrinsic 997 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) { 998 ArgTys.pop_back(); 999 return FunctionType::get(ResultTy, ArgTys, true); 1000 } 1001 return FunctionType::get(ResultTy, ArgTys, false); 1002 } 1003 1004 bool Intrinsic::isOverloaded(ID id) { 1005 #define GET_INTRINSIC_OVERLOAD_TABLE 1006 #include "llvm/IR/IntrinsicImpl.inc" 1007 #undef GET_INTRINSIC_OVERLOAD_TABLE 1008 } 1009 1010 bool Intrinsic::isLeaf(ID id) { 1011 switch (id) { 1012 default: 1013 return true; 1014 1015 case Intrinsic::experimental_gc_statepoint: 1016 case Intrinsic::experimental_patchpoint_void: 1017 case Intrinsic::experimental_patchpoint_i64: 1018 return false; 1019 } 1020 } 1021 1022 /// This defines the "Intrinsic::getAttributes(ID id)" method. 1023 #define GET_INTRINSIC_ATTRIBUTES 1024 #include "llvm/IR/IntrinsicImpl.inc" 1025 #undef GET_INTRINSIC_ATTRIBUTES 1026 1027 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { 1028 // There can never be multiple globals with the same name of different types, 1029 // because intrinsics must be a specific type. 1030 return cast<Function>( 1031 M->getOrInsertFunction(getName(id, Tys), 1032 getType(M->getContext(), id, Tys)) 1033 .getCallee()); 1034 } 1035 1036 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. 1037 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 1038 #include "llvm/IR/IntrinsicImpl.inc" 1039 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 1040 1041 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method. 1042 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 1043 #include "llvm/IR/IntrinsicImpl.inc" 1044 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 1045 1046 bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos, 1047 SmallVectorImpl<Type*> &ArgTys) { 1048 using namespace Intrinsic; 1049 1050 // If we ran out of descriptors, there are too many arguments. 1051 if (Infos.empty()) return true; 1052 IITDescriptor D = Infos.front(); 1053 Infos = Infos.slice(1); 1054 1055 switch (D.Kind) { 1056 case IITDescriptor::Void: return !Ty->isVoidTy(); 1057 case IITDescriptor::VarArg: return true; 1058 case IITDescriptor::MMX: return !Ty->isX86_MMXTy(); 1059 case IITDescriptor::Token: return !Ty->isTokenTy(); 1060 case IITDescriptor::Metadata: return !Ty->isMetadataTy(); 1061 case IITDescriptor::Half: return !Ty->isHalfTy(); 1062 case IITDescriptor::Float: return !Ty->isFloatTy(); 1063 case IITDescriptor::Double: return !Ty->isDoubleTy(); 1064 case IITDescriptor::Quad: return !Ty->isFP128Ty(); 1065 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width); 1066 case IITDescriptor::Vector: { 1067 VectorType *VT = dyn_cast<VectorType>(Ty); 1068 return !VT || VT->getNumElements() != D.Vector_Width || 1069 matchIntrinsicType(VT->getElementType(), Infos, ArgTys); 1070 } 1071 case IITDescriptor::Pointer: { 1072 PointerType *PT = dyn_cast<PointerType>(Ty); 1073 return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace || 1074 matchIntrinsicType(PT->getElementType(), Infos, ArgTys); 1075 } 1076 1077 case IITDescriptor::Struct: { 1078 StructType *ST = dyn_cast<StructType>(Ty); 1079 if (!ST || ST->getNumElements() != D.Struct_NumElements) 1080 return true; 1081 1082 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 1083 if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys)) 1084 return true; 1085 return false; 1086 } 1087 1088 case IITDescriptor::Argument: 1089 // Two cases here - If this is the second occurrence of an argument, verify 1090 // that the later instance matches the previous instance. 1091 if (D.getArgumentNumber() < ArgTys.size()) 1092 return Ty != ArgTys[D.getArgumentNumber()]; 1093 1094 // Otherwise, if this is the first instance of an argument, record it and 1095 // verify the "Any" kind. 1096 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error"); 1097 ArgTys.push_back(Ty); 1098 1099 switch (D.getArgumentKind()) { 1100 case IITDescriptor::AK_Any: return false; // Success 1101 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy(); 1102 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy(); 1103 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty); 1104 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty); 1105 } 1106 llvm_unreachable("all argument kinds not covered"); 1107 1108 case IITDescriptor::ExtendArgument: { 1109 // This may only be used when referring to a previous vector argument. 1110 if (D.getArgumentNumber() >= ArgTys.size()) 1111 return true; 1112 1113 Type *NewTy = ArgTys[D.getArgumentNumber()]; 1114 if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) 1115 NewTy = VectorType::getExtendedElementVectorType(VTy); 1116 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) 1117 NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth()); 1118 else 1119 return true; 1120 1121 return Ty != NewTy; 1122 } 1123 case IITDescriptor::TruncArgument: { 1124 // This may only be used when referring to a previous vector argument. 1125 if (D.getArgumentNumber() >= ArgTys.size()) 1126 return true; 1127 1128 Type *NewTy = ArgTys[D.getArgumentNumber()]; 1129 if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) 1130 NewTy = VectorType::getTruncatedElementVectorType(VTy); 1131 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) 1132 NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2); 1133 else 1134 return true; 1135 1136 return Ty != NewTy; 1137 } 1138 case IITDescriptor::HalfVecArgument: 1139 // This may only be used when referring to a previous vector argument. 1140 return D.getArgumentNumber() >= ArgTys.size() || 1141 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || 1142 VectorType::getHalfElementsVectorType( 1143 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; 1144 case IITDescriptor::SameVecWidthArgument: { 1145 if (D.getArgumentNumber() >= ArgTys.size()) 1146 return true; 1147 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); 1148 auto *ThisArgType = dyn_cast<VectorType>(Ty); 1149 // Both must be vectors of the same number of elements or neither. 1150 if ((ReferenceType != nullptr) != (ThisArgType != nullptr)) 1151 return true; 1152 Type *EltTy = Ty; 1153 if (ThisArgType) { 1154 if (ReferenceType->getVectorNumElements() != 1155 ThisArgType->getVectorNumElements()) 1156 return true; 1157 EltTy = ThisArgType->getVectorElementType(); 1158 } 1159 return matchIntrinsicType(EltTy, Infos, ArgTys); 1160 } 1161 case IITDescriptor::PtrToArgument: { 1162 if (D.getArgumentNumber() >= ArgTys.size()) 1163 return true; 1164 Type * ReferenceType = ArgTys[D.getArgumentNumber()]; 1165 PointerType *ThisArgType = dyn_cast<PointerType>(Ty); 1166 return (!ThisArgType || ThisArgType->getElementType() != ReferenceType); 1167 } 1168 case IITDescriptor::PtrToElt: { 1169 if (D.getArgumentNumber() >= ArgTys.size()) 1170 return true; 1171 VectorType * ReferenceType = 1172 dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]); 1173 PointerType *ThisArgType = dyn_cast<PointerType>(Ty); 1174 1175 return (!ThisArgType || !ReferenceType || 1176 ThisArgType->getElementType() != ReferenceType->getElementType()); 1177 } 1178 case IITDescriptor::VecOfAnyPtrsToElt: { 1179 unsigned RefArgNumber = D.getRefArgNumber(); 1180 1181 // This may only be used when referring to a previous argument. 1182 if (RefArgNumber >= ArgTys.size()) 1183 return true; 1184 1185 // Record the overloaded type 1186 assert(D.getOverloadArgNumber() == ArgTys.size() && 1187 "Table consistency error"); 1188 ArgTys.push_back(Ty); 1189 1190 // Verify the overloaded type "matches" the Ref type. 1191 // i.e. Ty is a vector with the same width as Ref. 1192 // Composed of pointers to the same element type as Ref. 1193 VectorType *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]); 1194 VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty); 1195 if (!ThisArgVecTy || !ReferenceType || 1196 (ReferenceType->getVectorNumElements() != 1197 ThisArgVecTy->getVectorNumElements())) 1198 return true; 1199 PointerType *ThisArgEltTy = 1200 dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType()); 1201 if (!ThisArgEltTy) 1202 return true; 1203 return ThisArgEltTy->getElementType() != 1204 ReferenceType->getVectorElementType(); 1205 } 1206 } 1207 llvm_unreachable("unhandled"); 1208 } 1209 1210 bool 1211 Intrinsic::matchIntrinsicVarArg(bool isVarArg, 1212 ArrayRef<Intrinsic::IITDescriptor> &Infos) { 1213 // If there are no descriptors left, then it can't be a vararg. 1214 if (Infos.empty()) 1215 return isVarArg; 1216 1217 // There should be only one descriptor remaining at this point. 1218 if (Infos.size() != 1) 1219 return true; 1220 1221 // Check and verify the descriptor. 1222 IITDescriptor D = Infos.front(); 1223 Infos = Infos.slice(1); 1224 if (D.Kind == IITDescriptor::VarArg) 1225 return !isVarArg; 1226 1227 return true; 1228 } 1229 1230 Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) { 1231 Intrinsic::ID ID = F->getIntrinsicID(); 1232 if (!ID) 1233 return None; 1234 1235 FunctionType *FTy = F->getFunctionType(); 1236 // Accumulate an array of overloaded types for the given intrinsic 1237 SmallVector<Type *, 4> ArgTys; 1238 { 1239 SmallVector<Intrinsic::IITDescriptor, 8> Table; 1240 getIntrinsicInfoTableEntries(ID, Table); 1241 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table; 1242 1243 // If we encounter any problems matching the signature with the descriptor 1244 // just give up remangling. It's up to verifier to report the discrepancy. 1245 if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys)) 1246 return None; 1247 for (auto Ty : FTy->params()) 1248 if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys)) 1249 return None; 1250 if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef)) 1251 return None; 1252 } 1253 1254 StringRef Name = F->getName(); 1255 if (Name == Intrinsic::getName(ID, ArgTys)) 1256 return None; 1257 1258 auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys); 1259 NewDecl->setCallingConv(F->getCallingConv()); 1260 assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature"); 1261 return NewDecl; 1262 } 1263 1264 /// hasAddressTaken - returns true if there are any uses of this function 1265 /// other than direct calls or invokes to it. 1266 bool Function::hasAddressTaken(const User* *PutOffender) const { 1267 for (const Use &U : uses()) { 1268 const User *FU = U.getUser(); 1269 if (isa<BlockAddress>(FU)) 1270 continue; 1271 const auto *Call = dyn_cast<CallBase>(FU); 1272 if (!Call) { 1273 if (PutOffender) 1274 *PutOffender = FU; 1275 return true; 1276 } 1277 if (!Call->isCallee(&U)) { 1278 if (PutOffender) 1279 *PutOffender = FU; 1280 return true; 1281 } 1282 } 1283 return false; 1284 } 1285 1286 bool Function::isDefTriviallyDead() const { 1287 // Check the linkage 1288 if (!hasLinkOnceLinkage() && !hasLocalLinkage() && 1289 !hasAvailableExternallyLinkage()) 1290 return false; 1291 1292 // Check if the function is used by anything other than a blockaddress. 1293 for (const User *U : users()) 1294 if (!isa<BlockAddress>(U)) 1295 return false; 1296 1297 return true; 1298 } 1299 1300 /// callsFunctionThatReturnsTwice - Return true if the function has a call to 1301 /// setjmp or other function that gcc recognizes as "returning twice". 1302 bool Function::callsFunctionThatReturnsTwice() const { 1303 for (const Instruction &I : instructions(this)) 1304 if (const auto *Call = dyn_cast<CallBase>(&I)) 1305 if (Call->hasFnAttr(Attribute::ReturnsTwice)) 1306 return true; 1307 1308 return false; 1309 } 1310 1311 Constant *Function::getPersonalityFn() const { 1312 assert(hasPersonalityFn() && getNumOperands()); 1313 return cast<Constant>(Op<0>()); 1314 } 1315 1316 void Function::setPersonalityFn(Constant *Fn) { 1317 setHungoffOperand<0>(Fn); 1318 setValueSubclassDataBit(3, Fn != nullptr); 1319 } 1320 1321 Constant *Function::getPrefixData() const { 1322 assert(hasPrefixData() && getNumOperands()); 1323 return cast<Constant>(Op<1>()); 1324 } 1325 1326 void Function::setPrefixData(Constant *PrefixData) { 1327 setHungoffOperand<1>(PrefixData); 1328 setValueSubclassDataBit(1, PrefixData != nullptr); 1329 } 1330 1331 Constant *Function::getPrologueData() const { 1332 assert(hasPrologueData() && getNumOperands()); 1333 return cast<Constant>(Op<2>()); 1334 } 1335 1336 void Function::setPrologueData(Constant *PrologueData) { 1337 setHungoffOperand<2>(PrologueData); 1338 setValueSubclassDataBit(2, PrologueData != nullptr); 1339 } 1340 1341 void Function::allocHungoffUselist() { 1342 // If we've already allocated a uselist, stop here. 1343 if (getNumOperands()) 1344 return; 1345 1346 allocHungoffUses(3, /*IsPhi=*/ false); 1347 setNumHungOffUseOperands(3); 1348 1349 // Initialize the uselist with placeholder operands to allow traversal. 1350 auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)); 1351 Op<0>().set(CPN); 1352 Op<1>().set(CPN); 1353 Op<2>().set(CPN); 1354 } 1355 1356 template <int Idx> 1357 void Function::setHungoffOperand(Constant *C) { 1358 if (C) { 1359 allocHungoffUselist(); 1360 Op<Idx>().set(C); 1361 } else if (getNumOperands()) { 1362 Op<Idx>().set( 1363 ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0))); 1364 } 1365 } 1366 1367 void Function::setValueSubclassDataBit(unsigned Bit, bool On) { 1368 assert(Bit < 16 && "SubclassData contains only 16 bits"); 1369 if (On) 1370 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit)); 1371 else 1372 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit)); 1373 } 1374 1375 void Function::setEntryCount(ProfileCount Count, 1376 const DenseSet<GlobalValue::GUID> *S) { 1377 assert(Count.hasValue()); 1378 #if !defined(NDEBUG) 1379 auto PrevCount = getEntryCount(); 1380 assert(!PrevCount.hasValue() || PrevCount.getType() == Count.getType()); 1381 #endif 1382 MDBuilder MDB(getContext()); 1383 setMetadata( 1384 LLVMContext::MD_prof, 1385 MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S)); 1386 } 1387 1388 void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type, 1389 const DenseSet<GlobalValue::GUID> *Imports) { 1390 setEntryCount(ProfileCount(Count, Type), Imports); 1391 } 1392 1393 ProfileCount Function::getEntryCount(bool AllowSynthetic) const { 1394 MDNode *MD = getMetadata(LLVMContext::MD_prof); 1395 if (MD && MD->getOperand(0)) 1396 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) { 1397 if (MDS->getString().equals("function_entry_count")) { 1398 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 1399 uint64_t Count = CI->getValue().getZExtValue(); 1400 // A value of -1 is used for SamplePGO when there were no samples. 1401 // Treat this the same as unknown. 1402 if (Count == (uint64_t)-1) 1403 return ProfileCount::getInvalid(); 1404 return ProfileCount(Count, PCT_Real); 1405 } else if (AllowSynthetic && 1406 MDS->getString().equals("synthetic_function_entry_count")) { 1407 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 1408 uint64_t Count = CI->getValue().getZExtValue(); 1409 return ProfileCount(Count, PCT_Synthetic); 1410 } 1411 } 1412 return ProfileCount::getInvalid(); 1413 } 1414 1415 DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const { 1416 DenseSet<GlobalValue::GUID> R; 1417 if (MDNode *MD = getMetadata(LLVMContext::MD_prof)) 1418 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) 1419 if (MDS->getString().equals("function_entry_count")) 1420 for (unsigned i = 2; i < MD->getNumOperands(); i++) 1421 R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i)) 1422 ->getValue() 1423 .getZExtValue()); 1424 return R; 1425 } 1426 1427 void Function::setSectionPrefix(StringRef Prefix) { 1428 MDBuilder MDB(getContext()); 1429 setMetadata(LLVMContext::MD_section_prefix, 1430 MDB.createFunctionSectionPrefix(Prefix)); 1431 } 1432 1433 Optional<StringRef> Function::getSectionPrefix() const { 1434 if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) { 1435 assert(cast<MDString>(MD->getOperand(0)) 1436 ->getString() 1437 .equals("function_section_prefix") && 1438 "Metadata not match"); 1439 return cast<MDString>(MD->getOperand(1))->getString(); 1440 } 1441 return None; 1442 } 1443 1444 bool Function::nullPointerIsDefined() const { 1445 return getFnAttribute("null-pointer-is-valid") 1446 .getValueAsString() 1447 .equals("true"); 1448 } 1449 1450 bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) { 1451 if (F && F->nullPointerIsDefined()) 1452 return true; 1453 1454 if (AS != 0) 1455 return true; 1456 1457 return false; 1458 } 1459