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