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/BitVector.h" 17 #include "llvm/ADT/DenseSet.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/ConstantRange.h" 29 #include "llvm/IR/Constants.h" 30 #include "llvm/IR/DerivedTypes.h" 31 #include "llvm/IR/GlobalValue.h" 32 #include "llvm/IR/InstIterator.h" 33 #include "llvm/IR/Instruction.h" 34 #include "llvm/IR/IntrinsicInst.h" 35 #include "llvm/IR/Intrinsics.h" 36 #include "llvm/IR/IntrinsicsAArch64.h" 37 #include "llvm/IR/IntrinsicsAMDGPU.h" 38 #include "llvm/IR/IntrinsicsARM.h" 39 #include "llvm/IR/IntrinsicsBPF.h" 40 #include "llvm/IR/IntrinsicsDirectX.h" 41 #include "llvm/IR/IntrinsicsHexagon.h" 42 #include "llvm/IR/IntrinsicsLoongArch.h" 43 #include "llvm/IR/IntrinsicsMips.h" 44 #include "llvm/IR/IntrinsicsNVPTX.h" 45 #include "llvm/IR/IntrinsicsPowerPC.h" 46 #include "llvm/IR/IntrinsicsR600.h" 47 #include "llvm/IR/IntrinsicsRISCV.h" 48 #include "llvm/IR/IntrinsicsS390.h" 49 #include "llvm/IR/IntrinsicsSPIRV.h" 50 #include "llvm/IR/IntrinsicsVE.h" 51 #include "llvm/IR/IntrinsicsWebAssembly.h" 52 #include "llvm/IR/IntrinsicsX86.h" 53 #include "llvm/IR/IntrinsicsXCore.h" 54 #include "llvm/IR/LLVMContext.h" 55 #include "llvm/IR/MDBuilder.h" 56 #include "llvm/IR/Metadata.h" 57 #include "llvm/IR/Module.h" 58 #include "llvm/IR/Operator.h" 59 #include "llvm/IR/SymbolTableListTraits.h" 60 #include "llvm/IR/Type.h" 61 #include "llvm/IR/Use.h" 62 #include "llvm/IR/User.h" 63 #include "llvm/IR/Value.h" 64 #include "llvm/IR/ValueSymbolTable.h" 65 #include "llvm/Support/Casting.h" 66 #include "llvm/Support/CommandLine.h" 67 #include "llvm/Support/Compiler.h" 68 #include "llvm/Support/ErrorHandling.h" 69 #include "llvm/Support/ModRef.h" 70 #include <cassert> 71 #include <cstddef> 72 #include <cstdint> 73 #include <cstring> 74 #include <string> 75 76 using namespace llvm; 77 using ProfileCount = Function::ProfileCount; 78 79 // Explicit instantiations of SymbolTableListTraits since some of the methods 80 // are not in the public header file... 81 template class llvm::SymbolTableListTraits<BasicBlock>; 82 83 static cl::opt<int> NonGlobalValueMaxNameSize( 84 "non-global-value-max-name-size", cl::Hidden, cl::init(1024), 85 cl::desc("Maximum size for the name of non-global values.")); 86 87 extern cl::opt<bool> UseNewDbgInfoFormat; 88 89 void Function::renumberBlocks() { 90 validateBlockNumbers(); 91 92 NextBlockNum = 0; 93 for (auto &BB : *this) 94 BB.Number = NextBlockNum++; 95 BlockNumEpoch++; 96 } 97 98 void Function::validateBlockNumbers() const { 99 #ifndef NDEBUG 100 BitVector Numbers(NextBlockNum); 101 for (const auto &BB : *this) { 102 unsigned Num = BB.getNumber(); 103 assert(Num < NextBlockNum && "out of range block number"); 104 assert(!Numbers[Num] && "duplicate block numbers"); 105 Numbers.set(Num); 106 } 107 #endif 108 } 109 110 void Function::convertToNewDbgValues() { 111 IsNewDbgInfoFormat = true; 112 for (auto &BB : *this) { 113 BB.convertToNewDbgValues(); 114 } 115 } 116 117 void Function::convertFromNewDbgValues() { 118 IsNewDbgInfoFormat = false; 119 for (auto &BB : *this) { 120 BB.convertFromNewDbgValues(); 121 } 122 } 123 124 void Function::setIsNewDbgInfoFormat(bool NewFlag) { 125 if (NewFlag && !IsNewDbgInfoFormat) 126 convertToNewDbgValues(); 127 else if (!NewFlag && IsNewDbgInfoFormat) 128 convertFromNewDbgValues(); 129 } 130 void Function::setNewDbgInfoFormatFlag(bool NewFlag) { 131 for (auto &BB : *this) { 132 BB.setNewDbgInfoFormatFlag(NewFlag); 133 } 134 IsNewDbgInfoFormat = NewFlag; 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Argument Implementation 139 //===----------------------------------------------------------------------===// 140 141 Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo) 142 : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) { 143 setName(Name); 144 } 145 146 void Argument::setParent(Function *parent) { 147 Parent = parent; 148 } 149 150 bool Argument::hasNonNullAttr(bool AllowUndefOrPoison) const { 151 if (!getType()->isPointerTy()) return false; 152 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull) && 153 (AllowUndefOrPoison || 154 getParent()->hasParamAttribute(getArgNo(), Attribute::NoUndef))) 155 return true; 156 else if (getDereferenceableBytes() > 0 && 157 !NullPointerIsDefined(getParent(), 158 getType()->getPointerAddressSpace())) 159 return true; 160 return false; 161 } 162 163 bool Argument::hasByValAttr() const { 164 if (!getType()->isPointerTy()) return false; 165 return hasAttribute(Attribute::ByVal); 166 } 167 168 bool Argument::hasByRefAttr() const { 169 if (!getType()->isPointerTy()) 170 return false; 171 return hasAttribute(Attribute::ByRef); 172 } 173 174 bool Argument::hasSwiftSelfAttr() const { 175 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf); 176 } 177 178 bool Argument::hasSwiftErrorAttr() const { 179 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError); 180 } 181 182 bool Argument::hasInAllocaAttr() const { 183 if (!getType()->isPointerTy()) return false; 184 return hasAttribute(Attribute::InAlloca); 185 } 186 187 bool Argument::hasPreallocatedAttr() const { 188 if (!getType()->isPointerTy()) 189 return false; 190 return hasAttribute(Attribute::Preallocated); 191 } 192 193 bool Argument::hasPassPointeeByValueCopyAttr() const { 194 if (!getType()->isPointerTy()) return false; 195 AttributeList Attrs = getParent()->getAttributes(); 196 return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) || 197 Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) || 198 Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated); 199 } 200 201 bool Argument::hasPointeeInMemoryValueAttr() const { 202 if (!getType()->isPointerTy()) 203 return false; 204 AttributeList Attrs = getParent()->getAttributes(); 205 return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) || 206 Attrs.hasParamAttr(getArgNo(), Attribute::StructRet) || 207 Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) || 208 Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated) || 209 Attrs.hasParamAttr(getArgNo(), Attribute::ByRef); 210 } 211 212 /// For a byval, sret, inalloca, or preallocated parameter, get the in-memory 213 /// parameter type. 214 static Type *getMemoryParamAllocType(AttributeSet ParamAttrs) { 215 // FIXME: All the type carrying attributes are mutually exclusive, so there 216 // should be a single query to get the stored type that handles any of them. 217 if (Type *ByValTy = ParamAttrs.getByValType()) 218 return ByValTy; 219 if (Type *ByRefTy = ParamAttrs.getByRefType()) 220 return ByRefTy; 221 if (Type *PreAllocTy = ParamAttrs.getPreallocatedType()) 222 return PreAllocTy; 223 if (Type *InAllocaTy = ParamAttrs.getInAllocaType()) 224 return InAllocaTy; 225 if (Type *SRetTy = ParamAttrs.getStructRetType()) 226 return SRetTy; 227 228 return nullptr; 229 } 230 231 uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout &DL) const { 232 AttributeSet ParamAttrs = 233 getParent()->getAttributes().getParamAttrs(getArgNo()); 234 if (Type *MemTy = getMemoryParamAllocType(ParamAttrs)) 235 return DL.getTypeAllocSize(MemTy); 236 return 0; 237 } 238 239 Type *Argument::getPointeeInMemoryValueType() const { 240 AttributeSet ParamAttrs = 241 getParent()->getAttributes().getParamAttrs(getArgNo()); 242 return getMemoryParamAllocType(ParamAttrs); 243 } 244 245 MaybeAlign Argument::getParamAlign() const { 246 assert(getType()->isPointerTy() && "Only pointers have alignments"); 247 return getParent()->getParamAlign(getArgNo()); 248 } 249 250 MaybeAlign Argument::getParamStackAlign() const { 251 return getParent()->getParamStackAlign(getArgNo()); 252 } 253 254 Type *Argument::getParamByValType() const { 255 assert(getType()->isPointerTy() && "Only pointers have byval types"); 256 return getParent()->getParamByValType(getArgNo()); 257 } 258 259 Type *Argument::getParamStructRetType() const { 260 assert(getType()->isPointerTy() && "Only pointers have sret types"); 261 return getParent()->getParamStructRetType(getArgNo()); 262 } 263 264 Type *Argument::getParamByRefType() const { 265 assert(getType()->isPointerTy() && "Only pointers have byref types"); 266 return getParent()->getParamByRefType(getArgNo()); 267 } 268 269 Type *Argument::getParamInAllocaType() const { 270 assert(getType()->isPointerTy() && "Only pointers have inalloca types"); 271 return getParent()->getParamInAllocaType(getArgNo()); 272 } 273 274 uint64_t Argument::getDereferenceableBytes() const { 275 assert(getType()->isPointerTy() && 276 "Only pointers have dereferenceable bytes"); 277 return getParent()->getParamDereferenceableBytes(getArgNo()); 278 } 279 280 uint64_t Argument::getDereferenceableOrNullBytes() const { 281 assert(getType()->isPointerTy() && 282 "Only pointers have dereferenceable bytes"); 283 return getParent()->getParamDereferenceableOrNullBytes(getArgNo()); 284 } 285 286 FPClassTest Argument::getNoFPClass() const { 287 return getParent()->getParamNoFPClass(getArgNo()); 288 } 289 290 std::optional<ConstantRange> Argument::getRange() const { 291 const Attribute RangeAttr = getAttribute(llvm::Attribute::Range); 292 if (RangeAttr.isValid()) 293 return RangeAttr.getRange(); 294 return std::nullopt; 295 } 296 297 bool Argument::hasNestAttr() const { 298 if (!getType()->isPointerTy()) return false; 299 return hasAttribute(Attribute::Nest); 300 } 301 302 bool Argument::hasNoAliasAttr() const { 303 if (!getType()->isPointerTy()) return false; 304 return hasAttribute(Attribute::NoAlias); 305 } 306 307 bool Argument::hasNoCaptureAttr() const { 308 if (!getType()->isPointerTy()) return false; 309 return hasAttribute(Attribute::NoCapture); 310 } 311 312 bool Argument::hasNoFreeAttr() const { 313 if (!getType()->isPointerTy()) return false; 314 return hasAttribute(Attribute::NoFree); 315 } 316 317 bool Argument::hasStructRetAttr() const { 318 if (!getType()->isPointerTy()) return false; 319 return hasAttribute(Attribute::StructRet); 320 } 321 322 bool Argument::hasInRegAttr() const { 323 return hasAttribute(Attribute::InReg); 324 } 325 326 bool Argument::hasReturnedAttr() const { 327 return hasAttribute(Attribute::Returned); 328 } 329 330 bool Argument::hasZExtAttr() const { 331 return hasAttribute(Attribute::ZExt); 332 } 333 334 bool Argument::hasSExtAttr() const { 335 return hasAttribute(Attribute::SExt); 336 } 337 338 bool Argument::onlyReadsMemory() const { 339 AttributeList Attrs = getParent()->getAttributes(); 340 return Attrs.hasParamAttr(getArgNo(), Attribute::ReadOnly) || 341 Attrs.hasParamAttr(getArgNo(), Attribute::ReadNone); 342 } 343 344 void Argument::addAttrs(AttrBuilder &B) { 345 AttributeList AL = getParent()->getAttributes(); 346 AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B); 347 getParent()->setAttributes(AL); 348 } 349 350 void Argument::addAttr(Attribute::AttrKind Kind) { 351 getParent()->addParamAttr(getArgNo(), Kind); 352 } 353 354 void Argument::addAttr(Attribute Attr) { 355 getParent()->addParamAttr(getArgNo(), Attr); 356 } 357 358 void Argument::removeAttr(Attribute::AttrKind Kind) { 359 getParent()->removeParamAttr(getArgNo(), Kind); 360 } 361 362 void Argument::removeAttrs(const AttributeMask &AM) { 363 AttributeList AL = getParent()->getAttributes(); 364 AL = AL.removeParamAttributes(Parent->getContext(), getArgNo(), AM); 365 getParent()->setAttributes(AL); 366 } 367 368 bool Argument::hasAttribute(Attribute::AttrKind Kind) const { 369 return getParent()->hasParamAttribute(getArgNo(), Kind); 370 } 371 372 Attribute Argument::getAttribute(Attribute::AttrKind Kind) const { 373 return getParent()->getParamAttribute(getArgNo(), Kind); 374 } 375 376 //===----------------------------------------------------------------------===// 377 // Helper Methods in Function 378 //===----------------------------------------------------------------------===// 379 380 LLVMContext &Function::getContext() const { 381 return getType()->getContext(); 382 } 383 384 const DataLayout &Function::getDataLayout() const { 385 return getParent()->getDataLayout(); 386 } 387 388 unsigned Function::getInstructionCount() const { 389 unsigned NumInstrs = 0; 390 for (const BasicBlock &BB : BasicBlocks) 391 NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(), 392 BB.instructionsWithoutDebug().end()); 393 return NumInstrs; 394 } 395 396 Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage, 397 const Twine &N, Module &M) { 398 return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M); 399 } 400 401 Function *Function::createWithDefaultAttr(FunctionType *Ty, 402 LinkageTypes Linkage, 403 unsigned AddrSpace, const Twine &N, 404 Module *M) { 405 auto *F = new Function(Ty, Linkage, AddrSpace, N, M); 406 AttrBuilder B(F->getContext()); 407 UWTableKind UWTable = M->getUwtable(); 408 if (UWTable != UWTableKind::None) 409 B.addUWTableAttr(UWTable); 410 switch (M->getFramePointer()) { 411 case FramePointerKind::None: 412 // 0 ("none") is the default. 413 break; 414 case FramePointerKind::Reserved: 415 B.addAttribute("frame-pointer", "reserved"); 416 break; 417 case FramePointerKind::NonLeaf: 418 B.addAttribute("frame-pointer", "non-leaf"); 419 break; 420 case FramePointerKind::All: 421 B.addAttribute("frame-pointer", "all"); 422 break; 423 } 424 if (M->getModuleFlag("function_return_thunk_extern")) 425 B.addAttribute(Attribute::FnRetThunkExtern); 426 StringRef DefaultCPU = F->getContext().getDefaultTargetCPU(); 427 if (!DefaultCPU.empty()) 428 B.addAttribute("target-cpu", DefaultCPU); 429 StringRef DefaultFeatures = F->getContext().getDefaultTargetFeatures(); 430 if (!DefaultFeatures.empty()) 431 B.addAttribute("target-features", DefaultFeatures); 432 433 // Check if the module attribute is present and not zero. 434 auto isModuleAttributeSet = [&](const StringRef &ModAttr) -> bool { 435 const auto *Attr = 436 mdconst::extract_or_null<ConstantInt>(M->getModuleFlag(ModAttr)); 437 return Attr && !Attr->isZero(); 438 }; 439 440 auto AddAttributeIfSet = [&](const StringRef &ModAttr) { 441 if (isModuleAttributeSet(ModAttr)) 442 B.addAttribute(ModAttr); 443 }; 444 445 StringRef SignType = "none"; 446 if (isModuleAttributeSet("sign-return-address")) 447 SignType = "non-leaf"; 448 if (isModuleAttributeSet("sign-return-address-all")) 449 SignType = "all"; 450 if (SignType != "none") { 451 B.addAttribute("sign-return-address", SignType); 452 B.addAttribute("sign-return-address-key", 453 isModuleAttributeSet("sign-return-address-with-bkey") 454 ? "b_key" 455 : "a_key"); 456 } 457 AddAttributeIfSet("branch-target-enforcement"); 458 AddAttributeIfSet("branch-protection-pauth-lr"); 459 AddAttributeIfSet("guarded-control-stack"); 460 461 F->addFnAttrs(B); 462 return F; 463 } 464 465 void Function::removeFromParent() { 466 getParent()->getFunctionList().remove(getIterator()); 467 } 468 469 void Function::eraseFromParent() { 470 getParent()->getFunctionList().erase(getIterator()); 471 } 472 473 void Function::splice(Function::iterator ToIt, Function *FromF, 474 Function::iterator FromBeginIt, 475 Function::iterator FromEndIt) { 476 #ifdef EXPENSIVE_CHECKS 477 // Check that FromBeginIt is before FromEndIt. 478 auto FromFEnd = FromF->end(); 479 for (auto It = FromBeginIt; It != FromEndIt; ++It) 480 assert(It != FromFEnd && "FromBeginIt not before FromEndIt!"); 481 #endif // EXPENSIVE_CHECKS 482 BasicBlocks.splice(ToIt, FromF->BasicBlocks, FromBeginIt, FromEndIt); 483 } 484 485 Function::iterator Function::erase(Function::iterator FromIt, 486 Function::iterator ToIt) { 487 return BasicBlocks.erase(FromIt, ToIt); 488 } 489 490 //===----------------------------------------------------------------------===// 491 // Function Implementation 492 //===----------------------------------------------------------------------===// 493 494 static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) { 495 // If AS == -1 and we are passed a valid module pointer we place the function 496 // in the program address space. Otherwise we default to AS0. 497 if (AddrSpace == static_cast<unsigned>(-1)) 498 return M ? M->getDataLayout().getProgramAddressSpace() : 0; 499 return AddrSpace; 500 } 501 502 Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, 503 const Twine &name, Module *ParentModule) 504 : GlobalObject(Ty, Value::FunctionVal, 505 OperandTraits<Function>::op_begin(this), 0, Linkage, name, 506 computeAddrSpace(AddrSpace, ParentModule)), 507 NumArgs(Ty->getNumParams()), IsNewDbgInfoFormat(UseNewDbgInfoFormat) { 508 assert(FunctionType::isValidReturnType(getReturnType()) && 509 "invalid return type"); 510 setGlobalObjectSubClassData(0); 511 512 // We only need a symbol table for a function if the context keeps value names 513 if (!getContext().shouldDiscardValueNames()) 514 SymTab = std::make_unique<ValueSymbolTable>(NonGlobalValueMaxNameSize); 515 516 // If the function has arguments, mark them as lazily built. 517 if (Ty->getNumParams()) 518 setValueSubclassData(1); // Set the "has lazy arguments" bit. 519 520 if (ParentModule) { 521 ParentModule->getFunctionList().push_back(this); 522 IsNewDbgInfoFormat = ParentModule->IsNewDbgInfoFormat; 523 } 524 525 HasLLVMReservedName = getName().starts_with("llvm."); 526 // Ensure intrinsics have the right parameter attributes. 527 // Note, the IntID field will have been set in Value::setName if this function 528 // name is a valid intrinsic ID. 529 if (IntID) 530 setAttributes(Intrinsic::getAttributes(getContext(), IntID)); 531 } 532 533 Function::~Function() { 534 validateBlockNumbers(); 535 536 dropAllReferences(); // After this it is safe to delete instructions. 537 538 // Delete all of the method arguments and unlink from symbol table... 539 if (Arguments) 540 clearArguments(); 541 542 // Remove the function from the on-the-side GC table. 543 clearGC(); 544 } 545 546 void Function::BuildLazyArguments() const { 547 // Create the arguments vector, all arguments start out unnamed. 548 auto *FT = getFunctionType(); 549 if (NumArgs > 0) { 550 Arguments = std::allocator<Argument>().allocate(NumArgs); 551 for (unsigned i = 0, e = NumArgs; i != e; ++i) { 552 Type *ArgTy = FT->getParamType(i); 553 assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!"); 554 new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i); 555 } 556 } 557 558 // Clear the lazy arguments bit. 559 unsigned SDC = getSubclassDataFromValue(); 560 SDC &= ~(1 << 0); 561 const_cast<Function*>(this)->setValueSubclassData(SDC); 562 assert(!hasLazyArguments()); 563 } 564 565 static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) { 566 return MutableArrayRef<Argument>(Args, Count); 567 } 568 569 bool Function::isConstrainedFPIntrinsic() const { 570 return Intrinsic::isConstrainedFPIntrinsic(getIntrinsicID()); 571 } 572 573 void Function::clearArguments() { 574 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 575 A.setName(""); 576 A.~Argument(); 577 } 578 std::allocator<Argument>().deallocate(Arguments, NumArgs); 579 Arguments = nullptr; 580 } 581 582 void Function::stealArgumentListFrom(Function &Src) { 583 assert(isDeclaration() && "Expected no references to current arguments"); 584 585 // Drop the current arguments, if any, and set the lazy argument bit. 586 if (!hasLazyArguments()) { 587 assert(llvm::all_of(makeArgArray(Arguments, NumArgs), 588 [](const Argument &A) { return A.use_empty(); }) && 589 "Expected arguments to be unused in declaration"); 590 clearArguments(); 591 setValueSubclassData(getSubclassDataFromValue() | (1 << 0)); 592 } 593 594 // Nothing to steal if Src has lazy arguments. 595 if (Src.hasLazyArguments()) 596 return; 597 598 // Steal arguments from Src, and fix the lazy argument bits. 599 assert(arg_size() == Src.arg_size()); 600 Arguments = Src.Arguments; 601 Src.Arguments = nullptr; 602 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 603 // FIXME: This does the work of transferNodesFromList inefficiently. 604 SmallString<128> Name; 605 if (A.hasName()) 606 Name = A.getName(); 607 if (!Name.empty()) 608 A.setName(""); 609 A.setParent(this); 610 if (!Name.empty()) 611 A.setName(Name); 612 } 613 614 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0)); 615 assert(!hasLazyArguments()); 616 Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0)); 617 } 618 619 void Function::deleteBodyImpl(bool ShouldDrop) { 620 setIsMaterializable(false); 621 622 for (BasicBlock &BB : *this) 623 BB.dropAllReferences(); 624 625 // Delete all basic blocks. They are now unused, except possibly by 626 // blockaddresses, but BasicBlock's destructor takes care of those. 627 while (!BasicBlocks.empty()) 628 BasicBlocks.begin()->eraseFromParent(); 629 630 if (getNumOperands()) { 631 if (ShouldDrop) { 632 // Drop uses of any optional data (real or placeholder). 633 User::dropAllReferences(); 634 setNumHungOffUseOperands(0); 635 } else { 636 // The code needs to match Function::allocHungoffUselist(). 637 auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0)); 638 Op<0>().set(CPN); 639 Op<1>().set(CPN); 640 Op<2>().set(CPN); 641 } 642 setValueSubclassData(getSubclassDataFromValue() & ~0xe); 643 } 644 645 // Metadata is stored in a side-table. 646 clearMetadata(); 647 } 648 649 void Function::addAttributeAtIndex(unsigned i, Attribute Attr) { 650 AttributeSets = AttributeSets.addAttributeAtIndex(getContext(), i, Attr); 651 } 652 653 void Function::addFnAttr(Attribute::AttrKind Kind) { 654 AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind); 655 } 656 657 void Function::addFnAttr(StringRef Kind, StringRef Val) { 658 AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind, Val); 659 } 660 661 void Function::addFnAttr(Attribute Attr) { 662 AttributeSets = AttributeSets.addFnAttribute(getContext(), Attr); 663 } 664 665 void Function::addFnAttrs(const AttrBuilder &Attrs) { 666 AttributeSets = AttributeSets.addFnAttributes(getContext(), Attrs); 667 } 668 669 void Function::addRetAttr(Attribute::AttrKind Kind) { 670 AttributeSets = AttributeSets.addRetAttribute(getContext(), Kind); 671 } 672 673 void Function::addRetAttr(Attribute Attr) { 674 AttributeSets = AttributeSets.addRetAttribute(getContext(), Attr); 675 } 676 677 void Function::addRetAttrs(const AttrBuilder &Attrs) { 678 AttributeSets = AttributeSets.addRetAttributes(getContext(), Attrs); 679 } 680 681 void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 682 AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Kind); 683 } 684 685 void Function::addParamAttr(unsigned ArgNo, Attribute Attr) { 686 AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Attr); 687 } 688 689 void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { 690 AttributeSets = AttributeSets.addParamAttributes(getContext(), ArgNo, Attrs); 691 } 692 693 void Function::removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { 694 AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind); 695 } 696 697 void Function::removeAttributeAtIndex(unsigned i, StringRef Kind) { 698 AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind); 699 } 700 701 void Function::removeFnAttr(Attribute::AttrKind Kind) { 702 AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); 703 } 704 705 void Function::removeFnAttr(StringRef Kind) { 706 AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); 707 } 708 709 void Function::removeFnAttrs(const AttributeMask &AM) { 710 AttributeSets = AttributeSets.removeFnAttributes(getContext(), AM); 711 } 712 713 void Function::removeRetAttr(Attribute::AttrKind Kind) { 714 AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); 715 } 716 717 void Function::removeRetAttr(StringRef Kind) { 718 AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); 719 } 720 721 void Function::removeRetAttrs(const AttributeMask &Attrs) { 722 AttributeSets = AttributeSets.removeRetAttributes(getContext(), Attrs); 723 } 724 725 void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 726 AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); 727 } 728 729 void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) { 730 AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); 731 } 732 733 void Function::removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs) { 734 AttributeSets = 735 AttributeSets.removeParamAttributes(getContext(), ArgNo, Attrs); 736 } 737 738 void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) { 739 AttributeSets = 740 AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes); 741 } 742 743 bool Function::hasFnAttribute(Attribute::AttrKind Kind) const { 744 return AttributeSets.hasFnAttr(Kind); 745 } 746 747 bool Function::hasFnAttribute(StringRef Kind) const { 748 return AttributeSets.hasFnAttr(Kind); 749 } 750 751 bool Function::hasRetAttribute(Attribute::AttrKind Kind) const { 752 return AttributeSets.hasRetAttr(Kind); 753 } 754 755 bool Function::hasParamAttribute(unsigned ArgNo, 756 Attribute::AttrKind Kind) const { 757 return AttributeSets.hasParamAttr(ArgNo, Kind); 758 } 759 760 Attribute Function::getAttributeAtIndex(unsigned i, 761 Attribute::AttrKind Kind) const { 762 return AttributeSets.getAttributeAtIndex(i, Kind); 763 } 764 765 Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const { 766 return AttributeSets.getAttributeAtIndex(i, Kind); 767 } 768 769 Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const { 770 return AttributeSets.getFnAttr(Kind); 771 } 772 773 Attribute Function::getFnAttribute(StringRef Kind) const { 774 return AttributeSets.getFnAttr(Kind); 775 } 776 777 Attribute Function::getRetAttribute(Attribute::AttrKind Kind) const { 778 return AttributeSets.getRetAttr(Kind); 779 } 780 781 uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name, 782 uint64_t Default) const { 783 Attribute A = getFnAttribute(Name); 784 uint64_t Result = Default; 785 if (A.isStringAttribute()) { 786 StringRef Str = A.getValueAsString(); 787 if (Str.getAsInteger(0, Result)) 788 getContext().emitError("cannot parse integer attribute " + Name); 789 } 790 791 return Result; 792 } 793 794 /// gets the specified attribute from the list of attributes. 795 Attribute Function::getParamAttribute(unsigned ArgNo, 796 Attribute::AttrKind Kind) const { 797 return AttributeSets.getParamAttr(ArgNo, Kind); 798 } 799 800 void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo, 801 uint64_t Bytes) { 802 AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(getContext(), 803 ArgNo, Bytes); 804 } 805 806 void Function::addRangeRetAttr(const ConstantRange &CR) { 807 AttributeSets = AttributeSets.addRangeRetAttr(getContext(), CR); 808 } 809 810 DenormalMode Function::getDenormalMode(const fltSemantics &FPType) const { 811 if (&FPType == &APFloat::IEEEsingle()) { 812 DenormalMode Mode = getDenormalModeF32Raw(); 813 // If the f32 variant of the attribute isn't specified, try to use the 814 // generic one. 815 if (Mode.isValid()) 816 return Mode; 817 } 818 819 return getDenormalModeRaw(); 820 } 821 822 DenormalMode Function::getDenormalModeRaw() const { 823 Attribute Attr = getFnAttribute("denormal-fp-math"); 824 StringRef Val = Attr.getValueAsString(); 825 return parseDenormalFPAttribute(Val); 826 } 827 828 DenormalMode Function::getDenormalModeF32Raw() const { 829 Attribute Attr = getFnAttribute("denormal-fp-math-f32"); 830 if (Attr.isValid()) { 831 StringRef Val = Attr.getValueAsString(); 832 return parseDenormalFPAttribute(Val); 833 } 834 835 return DenormalMode::getInvalid(); 836 } 837 838 const std::string &Function::getGC() const { 839 assert(hasGC() && "Function has no collector"); 840 return getContext().getGC(*this); 841 } 842 843 void Function::setGC(std::string Str) { 844 setValueSubclassDataBit(14, !Str.empty()); 845 getContext().setGC(*this, std::move(Str)); 846 } 847 848 void Function::clearGC() { 849 if (!hasGC()) 850 return; 851 getContext().deleteGC(*this); 852 setValueSubclassDataBit(14, false); 853 } 854 855 bool Function::hasStackProtectorFnAttr() const { 856 return hasFnAttribute(Attribute::StackProtect) || 857 hasFnAttribute(Attribute::StackProtectStrong) || 858 hasFnAttribute(Attribute::StackProtectReq); 859 } 860 861 /// Copy all additional attributes (those not needed to create a Function) from 862 /// the Function Src to this one. 863 void Function::copyAttributesFrom(const Function *Src) { 864 GlobalObject::copyAttributesFrom(Src); 865 setCallingConv(Src->getCallingConv()); 866 setAttributes(Src->getAttributes()); 867 if (Src->hasGC()) 868 setGC(Src->getGC()); 869 else 870 clearGC(); 871 if (Src->hasPersonalityFn()) 872 setPersonalityFn(Src->getPersonalityFn()); 873 if (Src->hasPrefixData()) 874 setPrefixData(Src->getPrefixData()); 875 if (Src->hasPrologueData()) 876 setPrologueData(Src->getPrologueData()); 877 } 878 879 MemoryEffects Function::getMemoryEffects() const { 880 return getAttributes().getMemoryEffects(); 881 } 882 void Function::setMemoryEffects(MemoryEffects ME) { 883 addFnAttr(Attribute::getWithMemoryEffects(getContext(), ME)); 884 } 885 886 /// Determine if the function does not access memory. 887 bool Function::doesNotAccessMemory() const { 888 return getMemoryEffects().doesNotAccessMemory(); 889 } 890 void Function::setDoesNotAccessMemory() { 891 setMemoryEffects(MemoryEffects::none()); 892 } 893 894 /// Determine if the function does not access or only reads memory. 895 bool Function::onlyReadsMemory() const { 896 return getMemoryEffects().onlyReadsMemory(); 897 } 898 void Function::setOnlyReadsMemory() { 899 setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly()); 900 } 901 902 /// Determine if the function does not access or only writes memory. 903 bool Function::onlyWritesMemory() const { 904 return getMemoryEffects().onlyWritesMemory(); 905 } 906 void Function::setOnlyWritesMemory() { 907 setMemoryEffects(getMemoryEffects() & MemoryEffects::writeOnly()); 908 } 909 910 /// Determine if the call can access memmory only using pointers based 911 /// on its arguments. 912 bool Function::onlyAccessesArgMemory() const { 913 return getMemoryEffects().onlyAccessesArgPointees(); 914 } 915 void Function::setOnlyAccessesArgMemory() { 916 setMemoryEffects(getMemoryEffects() & MemoryEffects::argMemOnly()); 917 } 918 919 /// Determine if the function may only access memory that is 920 /// inaccessible from the IR. 921 bool Function::onlyAccessesInaccessibleMemory() const { 922 return getMemoryEffects().onlyAccessesInaccessibleMem(); 923 } 924 void Function::setOnlyAccessesInaccessibleMemory() { 925 setMemoryEffects(getMemoryEffects() & MemoryEffects::inaccessibleMemOnly()); 926 } 927 928 /// Determine if the function may only access memory that is 929 /// either inaccessible from the IR or pointed to by its arguments. 930 bool Function::onlyAccessesInaccessibleMemOrArgMem() const { 931 return getMemoryEffects().onlyAccessesInaccessibleOrArgMem(); 932 } 933 void Function::setOnlyAccessesInaccessibleMemOrArgMem() { 934 setMemoryEffects(getMemoryEffects() & 935 MemoryEffects::inaccessibleOrArgMemOnly()); 936 } 937 938 /// Table of string intrinsic names indexed by enum value. 939 static const char * const IntrinsicNameTable[] = { 940 "not_intrinsic", 941 #define GET_INTRINSIC_NAME_TABLE 942 #include "llvm/IR/IntrinsicImpl.inc" 943 #undef GET_INTRINSIC_NAME_TABLE 944 }; 945 946 /// Table of per-target intrinsic name tables. 947 #define GET_INTRINSIC_TARGET_DATA 948 #include "llvm/IR/IntrinsicImpl.inc" 949 #undef GET_INTRINSIC_TARGET_DATA 950 951 bool Function::isTargetIntrinsic(Intrinsic::ID IID) { 952 return IID > TargetInfos[0].Count; 953 } 954 955 bool Function::isTargetIntrinsic() const { 956 return isTargetIntrinsic(IntID); 957 } 958 959 /// Find the segment of \c IntrinsicNameTable for intrinsics with the same 960 /// target as \c Name, or the generic table if \c Name is not target specific. 961 /// 962 /// Returns the relevant slice of \c IntrinsicNameTable 963 static ArrayRef<const char *> findTargetSubtable(StringRef Name) { 964 assert(Name.starts_with("llvm.")); 965 966 ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos); 967 // Drop "llvm." and take the first dotted component. That will be the target 968 // if this is target specific. 969 StringRef Target = Name.drop_front(5).split('.').first; 970 auto It = partition_point( 971 Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; }); 972 // We've either found the target or just fall back to the generic set, which 973 // is always first. 974 const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0]; 975 return ArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count); 976 } 977 978 /// This does the actual lookup of an intrinsic ID which 979 /// matches the given function name. 980 Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) { 981 ArrayRef<const char *> NameTable = findTargetSubtable(Name); 982 int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name); 983 if (Idx == -1) 984 return Intrinsic::not_intrinsic; 985 986 // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have 987 // an index into a sub-table. 988 int Adjust = NameTable.data() - IntrinsicNameTable; 989 Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust); 990 991 // If the intrinsic is not overloaded, require an exact match. If it is 992 // overloaded, require either exact or prefix match. 993 const auto MatchSize = strlen(NameTable[Idx]); 994 assert(Name.size() >= MatchSize && "Expected either exact or prefix match"); 995 bool IsExactMatch = Name.size() == MatchSize; 996 return IsExactMatch || Intrinsic::isOverloaded(ID) ? ID 997 : Intrinsic::not_intrinsic; 998 } 999 1000 void Function::updateAfterNameChange() { 1001 LibFuncCache = UnknownLibFunc; 1002 StringRef Name = getName(); 1003 if (!Name.starts_with("llvm.")) { 1004 HasLLVMReservedName = false; 1005 IntID = Intrinsic::not_intrinsic; 1006 return; 1007 } 1008 HasLLVMReservedName = true; 1009 IntID = lookupIntrinsicID(Name); 1010 } 1011 1012 /// Returns a stable mangling for the type specified for use in the name 1013 /// mangling scheme used by 'any' types in intrinsic signatures. The mangling 1014 /// of named types is simply their name. Manglings for unnamed types consist 1015 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) 1016 /// combined with the mangling of their component types. A vararg function 1017 /// type will have a suffix of 'vararg'. Since function types can contain 1018 /// other function types, we close a function type mangling with suffix 'f' 1019 /// which can't be confused with it's prefix. This ensures we don't have 1020 /// collisions between two unrelated function types. Otherwise, you might 1021 /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) 1022 /// The HasUnnamedType boolean is set if an unnamed type was encountered, 1023 /// indicating that extra care must be taken to ensure a unique name. 1024 static std::string getMangledTypeStr(Type *Ty, bool &HasUnnamedType) { 1025 std::string Result; 1026 if (PointerType *PTyp = dyn_cast<PointerType>(Ty)) { 1027 Result += "p" + utostr(PTyp->getAddressSpace()); 1028 } else if (ArrayType *ATyp = dyn_cast<ArrayType>(Ty)) { 1029 Result += "a" + utostr(ATyp->getNumElements()) + 1030 getMangledTypeStr(ATyp->getElementType(), HasUnnamedType); 1031 } else if (StructType *STyp = dyn_cast<StructType>(Ty)) { 1032 if (!STyp->isLiteral()) { 1033 Result += "s_"; 1034 if (STyp->hasName()) 1035 Result += STyp->getName(); 1036 else 1037 HasUnnamedType = true; 1038 } else { 1039 Result += "sl_"; 1040 for (auto *Elem : STyp->elements()) 1041 Result += getMangledTypeStr(Elem, HasUnnamedType); 1042 } 1043 // Ensure nested structs are distinguishable. 1044 Result += "s"; 1045 } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) { 1046 Result += "f_" + getMangledTypeStr(FT->getReturnType(), HasUnnamedType); 1047 for (size_t i = 0; i < FT->getNumParams(); i++) 1048 Result += getMangledTypeStr(FT->getParamType(i), HasUnnamedType); 1049 if (FT->isVarArg()) 1050 Result += "vararg"; 1051 // Ensure nested function types are distinguishable. 1052 Result += "f"; 1053 } else if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { 1054 ElementCount EC = VTy->getElementCount(); 1055 if (EC.isScalable()) 1056 Result += "nx"; 1057 Result += "v" + utostr(EC.getKnownMinValue()) + 1058 getMangledTypeStr(VTy->getElementType(), HasUnnamedType); 1059 } else if (TargetExtType *TETy = dyn_cast<TargetExtType>(Ty)) { 1060 Result += "t"; 1061 Result += TETy->getName(); 1062 for (Type *ParamTy : TETy->type_params()) 1063 Result += "_" + getMangledTypeStr(ParamTy, HasUnnamedType); 1064 for (unsigned IntParam : TETy->int_params()) 1065 Result += "_" + utostr(IntParam); 1066 // Ensure nested target extension types are distinguishable. 1067 Result += "t"; 1068 } else if (Ty) { 1069 switch (Ty->getTypeID()) { 1070 default: llvm_unreachable("Unhandled type"); 1071 case Type::VoidTyID: Result += "isVoid"; break; 1072 case Type::MetadataTyID: Result += "Metadata"; break; 1073 case Type::HalfTyID: Result += "f16"; break; 1074 case Type::BFloatTyID: Result += "bf16"; break; 1075 case Type::FloatTyID: Result += "f32"; break; 1076 case Type::DoubleTyID: Result += "f64"; break; 1077 case Type::X86_FP80TyID: Result += "f80"; break; 1078 case Type::FP128TyID: Result += "f128"; break; 1079 case Type::PPC_FP128TyID: 1080 Result += "ppcf128"; 1081 break; 1082 case Type::X86_AMXTyID: Result += "x86amx"; break; 1083 case Type::IntegerTyID: 1084 Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth()); 1085 break; 1086 } 1087 } 1088 return Result; 1089 } 1090 1091 StringRef Intrinsic::getBaseName(ID id) { 1092 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 1093 return IntrinsicNameTable[id]; 1094 } 1095 1096 StringRef Intrinsic::getName(ID id) { 1097 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 1098 assert(!Intrinsic::isOverloaded(id) && 1099 "This version of getName does not support overloading"); 1100 return getBaseName(id); 1101 } 1102 1103 static std::string getIntrinsicNameImpl(Intrinsic::ID Id, ArrayRef<Type *> Tys, 1104 Module *M, FunctionType *FT, 1105 bool EarlyModuleCheck) { 1106 1107 assert(Id < Intrinsic::num_intrinsics && "Invalid intrinsic ID!"); 1108 assert((Tys.empty() || Intrinsic::isOverloaded(Id)) && 1109 "This version of getName is for overloaded intrinsics only"); 1110 (void)EarlyModuleCheck; 1111 assert((!EarlyModuleCheck || M || 1112 !any_of(Tys, [](Type *T) { return isa<PointerType>(T); })) && 1113 "Intrinsic overloading on pointer types need to provide a Module"); 1114 bool HasUnnamedType = false; 1115 std::string Result(Intrinsic::getBaseName(Id)); 1116 for (Type *Ty : Tys) 1117 Result += "." + getMangledTypeStr(Ty, HasUnnamedType); 1118 if (HasUnnamedType) { 1119 assert(M && "unnamed types need a module"); 1120 if (!FT) 1121 FT = Intrinsic::getType(M->getContext(), Id, Tys); 1122 else 1123 assert((FT == Intrinsic::getType(M->getContext(), Id, Tys)) && 1124 "Provided FunctionType must match arguments"); 1125 return M->getUniqueIntrinsicName(Result, Id, FT); 1126 } 1127 return Result; 1128 } 1129 1130 std::string Intrinsic::getName(ID Id, ArrayRef<Type *> Tys, Module *M, 1131 FunctionType *FT) { 1132 assert(M && "We need to have a Module"); 1133 return getIntrinsicNameImpl(Id, Tys, M, FT, true); 1134 } 1135 1136 std::string Intrinsic::getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> Tys) { 1137 return getIntrinsicNameImpl(Id, Tys, nullptr, nullptr, false); 1138 } 1139 1140 /// IIT_Info - These are enumerators that describe the entries returned by the 1141 /// getIntrinsicInfoTableEntries function. 1142 /// 1143 /// Defined in Intrinsics.td. 1144 enum IIT_Info { 1145 #define GET_INTRINSIC_IITINFO 1146 #include "llvm/IR/IntrinsicImpl.inc" 1147 #undef GET_INTRINSIC_IITINFO 1148 }; 1149 1150 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, 1151 IIT_Info LastInfo, 1152 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { 1153 using namespace Intrinsic; 1154 1155 bool IsScalableVector = (LastInfo == IIT_SCALABLE_VEC); 1156 1157 IIT_Info Info = IIT_Info(Infos[NextElt++]); 1158 unsigned StructElts = 2; 1159 1160 switch (Info) { 1161 case IIT_Done: 1162 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); 1163 return; 1164 case IIT_VARARG: 1165 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0)); 1166 return; 1167 case IIT_MMX: 1168 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); 1169 return; 1170 case IIT_AMX: 1171 OutputTable.push_back(IITDescriptor::get(IITDescriptor::AMX, 0)); 1172 return; 1173 case IIT_TOKEN: 1174 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0)); 1175 return; 1176 case IIT_METADATA: 1177 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); 1178 return; 1179 case IIT_F16: 1180 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); 1181 return; 1182 case IIT_BF16: 1183 OutputTable.push_back(IITDescriptor::get(IITDescriptor::BFloat, 0)); 1184 return; 1185 case IIT_F32: 1186 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); 1187 return; 1188 case IIT_F64: 1189 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); 1190 return; 1191 case IIT_F128: 1192 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0)); 1193 return; 1194 case IIT_PPCF128: 1195 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PPCQuad, 0)); 1196 return; 1197 case IIT_I1: 1198 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); 1199 return; 1200 case IIT_I2: 1201 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 2)); 1202 return; 1203 case IIT_I4: 1204 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 4)); 1205 return; 1206 case IIT_AARCH64_SVCOUNT: 1207 OutputTable.push_back(IITDescriptor::get(IITDescriptor::AArch64Svcount, 0)); 1208 return; 1209 case IIT_I8: 1210 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); 1211 return; 1212 case IIT_I16: 1213 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16)); 1214 return; 1215 case IIT_I32: 1216 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); 1217 return; 1218 case IIT_I64: 1219 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); 1220 return; 1221 case IIT_I128: 1222 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128)); 1223 return; 1224 case IIT_V1: 1225 OutputTable.push_back(IITDescriptor::getVector(1, IsScalableVector)); 1226 DecodeIITType(NextElt, Infos, Info, OutputTable); 1227 return; 1228 case IIT_V2: 1229 OutputTable.push_back(IITDescriptor::getVector(2, IsScalableVector)); 1230 DecodeIITType(NextElt, Infos, Info, OutputTable); 1231 return; 1232 case IIT_V3: 1233 OutputTable.push_back(IITDescriptor::getVector(3, IsScalableVector)); 1234 DecodeIITType(NextElt, Infos, Info, OutputTable); 1235 return; 1236 case IIT_V4: 1237 OutputTable.push_back(IITDescriptor::getVector(4, IsScalableVector)); 1238 DecodeIITType(NextElt, Infos, Info, OutputTable); 1239 return; 1240 case IIT_V6: 1241 OutputTable.push_back(IITDescriptor::getVector(6, IsScalableVector)); 1242 DecodeIITType(NextElt, Infos, Info, OutputTable); 1243 return; 1244 case IIT_V8: 1245 OutputTable.push_back(IITDescriptor::getVector(8, IsScalableVector)); 1246 DecodeIITType(NextElt, Infos, Info, OutputTable); 1247 return; 1248 case IIT_V10: 1249 OutputTable.push_back(IITDescriptor::getVector(10, IsScalableVector)); 1250 DecodeIITType(NextElt, Infos, Info, OutputTable); 1251 return; 1252 case IIT_V16: 1253 OutputTable.push_back(IITDescriptor::getVector(16, IsScalableVector)); 1254 DecodeIITType(NextElt, Infos, Info, OutputTable); 1255 return; 1256 case IIT_V32: 1257 OutputTable.push_back(IITDescriptor::getVector(32, IsScalableVector)); 1258 DecodeIITType(NextElt, Infos, Info, OutputTable); 1259 return; 1260 case IIT_V64: 1261 OutputTable.push_back(IITDescriptor::getVector(64, IsScalableVector)); 1262 DecodeIITType(NextElt, Infos, Info, OutputTable); 1263 return; 1264 case IIT_V128: 1265 OutputTable.push_back(IITDescriptor::getVector(128, IsScalableVector)); 1266 DecodeIITType(NextElt, Infos, Info, OutputTable); 1267 return; 1268 case IIT_V256: 1269 OutputTable.push_back(IITDescriptor::getVector(256, IsScalableVector)); 1270 DecodeIITType(NextElt, Infos, Info, OutputTable); 1271 return; 1272 case IIT_V512: 1273 OutputTable.push_back(IITDescriptor::getVector(512, IsScalableVector)); 1274 DecodeIITType(NextElt, Infos, Info, OutputTable); 1275 return; 1276 case IIT_V1024: 1277 OutputTable.push_back(IITDescriptor::getVector(1024, IsScalableVector)); 1278 DecodeIITType(NextElt, Infos, Info, OutputTable); 1279 return; 1280 case IIT_EXTERNREF: 1281 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 10)); 1282 return; 1283 case IIT_FUNCREF: 1284 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 20)); 1285 return; 1286 case IIT_PTR: 1287 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); 1288 return; 1289 case IIT_ANYPTR: // [ANYPTR addrspace] 1290 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 1291 Infos[NextElt++])); 1292 return; 1293 case IIT_ARG: { 1294 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1295 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); 1296 return; 1297 } 1298 case IIT_EXTEND_ARG: { 1299 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1300 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument, 1301 ArgInfo)); 1302 return; 1303 } 1304 case IIT_TRUNC_ARG: { 1305 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1306 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument, 1307 ArgInfo)); 1308 return; 1309 } 1310 case IIT_HALF_VEC_ARG: { 1311 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1312 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument, 1313 ArgInfo)); 1314 return; 1315 } 1316 case IIT_SAME_VEC_WIDTH_ARG: { 1317 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1318 OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument, 1319 ArgInfo)); 1320 return; 1321 } 1322 case IIT_VEC_OF_ANYPTRS_TO_ELT: { 1323 unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1324 unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1325 OutputTable.push_back( 1326 IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo)); 1327 return; 1328 } 1329 case IIT_EMPTYSTRUCT: 1330 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); 1331 return; 1332 case IIT_STRUCT9: ++StructElts; [[fallthrough]]; 1333 case IIT_STRUCT8: ++StructElts; [[fallthrough]]; 1334 case IIT_STRUCT7: ++StructElts; [[fallthrough]]; 1335 case IIT_STRUCT6: ++StructElts; [[fallthrough]]; 1336 case IIT_STRUCT5: ++StructElts; [[fallthrough]]; 1337 case IIT_STRUCT4: ++StructElts; [[fallthrough]]; 1338 case IIT_STRUCT3: ++StructElts; [[fallthrough]]; 1339 case IIT_STRUCT2: { 1340 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts)); 1341 1342 for (unsigned i = 0; i != StructElts; ++i) 1343 DecodeIITType(NextElt, Infos, Info, OutputTable); 1344 return; 1345 } 1346 case IIT_SUBDIVIDE2_ARG: { 1347 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1348 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide2Argument, 1349 ArgInfo)); 1350 return; 1351 } 1352 case IIT_SUBDIVIDE4_ARG: { 1353 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1354 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide4Argument, 1355 ArgInfo)); 1356 return; 1357 } 1358 case IIT_VEC_ELEMENT: { 1359 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1360 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecElementArgument, 1361 ArgInfo)); 1362 return; 1363 } 1364 case IIT_SCALABLE_VEC: { 1365 DecodeIITType(NextElt, Infos, Info, OutputTable); 1366 return; 1367 } 1368 case IIT_VEC_OF_BITCASTS_TO_INT: { 1369 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 1370 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfBitcastsToInt, 1371 ArgInfo)); 1372 return; 1373 } 1374 } 1375 llvm_unreachable("unhandled"); 1376 } 1377 1378 #define GET_INTRINSIC_GENERATOR_GLOBAL 1379 #include "llvm/IR/IntrinsicImpl.inc" 1380 #undef GET_INTRINSIC_GENERATOR_GLOBAL 1381 1382 void Intrinsic::getIntrinsicInfoTableEntries(ID id, 1383 SmallVectorImpl<IITDescriptor> &T){ 1384 static_assert(sizeof(IIT_Table[0]) == 2, 1385 "Expect 16-bit entries in IIT_Table"); 1386 // Check to see if the intrinsic's type was expressible by the table. 1387 uint16_t TableVal = IIT_Table[id - 1]; 1388 1389 // Decode the TableVal into an array of IITValues. 1390 SmallVector<unsigned char> IITValues; 1391 ArrayRef<unsigned char> IITEntries; 1392 unsigned NextElt = 0; 1393 if (TableVal >> 15) { 1394 // This is an offset into the IIT_LongEncodingTable. 1395 IITEntries = IIT_LongEncodingTable; 1396 1397 // Strip sentinel bit. 1398 NextElt = TableVal & 0x7fff; 1399 } else { 1400 // If the entry was encoded into a single word in the table itself, decode 1401 // it from an array of nibbles to an array of bytes. 1402 do { 1403 IITValues.push_back(TableVal & 0xF); 1404 TableVal >>= 4; 1405 } while (TableVal); 1406 1407 IITEntries = IITValues; 1408 NextElt = 0; 1409 } 1410 1411 // Okay, decode the table into the output vector of IITDescriptors. 1412 DecodeIITType(NextElt, IITEntries, IIT_Done, T); 1413 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) 1414 DecodeIITType(NextElt, IITEntries, IIT_Done, T); 1415 } 1416 1417 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, 1418 ArrayRef<Type*> Tys, LLVMContext &Context) { 1419 using namespace Intrinsic; 1420 1421 IITDescriptor D = Infos.front(); 1422 Infos = Infos.slice(1); 1423 1424 switch (D.Kind) { 1425 case IITDescriptor::Void: return Type::getVoidTy(Context); 1426 case IITDescriptor::VarArg: return Type::getVoidTy(Context); 1427 case IITDescriptor::MMX: 1428 return llvm::FixedVectorType::get(llvm::IntegerType::get(Context, 64), 1); 1429 case IITDescriptor::AMX: return Type::getX86_AMXTy(Context); 1430 case IITDescriptor::Token: return Type::getTokenTy(Context); 1431 case IITDescriptor::Metadata: return Type::getMetadataTy(Context); 1432 case IITDescriptor::Half: return Type::getHalfTy(Context); 1433 case IITDescriptor::BFloat: return Type::getBFloatTy(Context); 1434 case IITDescriptor::Float: return Type::getFloatTy(Context); 1435 case IITDescriptor::Double: return Type::getDoubleTy(Context); 1436 case IITDescriptor::Quad: return Type::getFP128Ty(Context); 1437 case IITDescriptor::PPCQuad: return Type::getPPC_FP128Ty(Context); 1438 case IITDescriptor::AArch64Svcount: 1439 return TargetExtType::get(Context, "aarch64.svcount"); 1440 1441 case IITDescriptor::Integer: 1442 return IntegerType::get(Context, D.Integer_Width); 1443 case IITDescriptor::Vector: 1444 return VectorType::get(DecodeFixedType(Infos, Tys, Context), 1445 D.Vector_Width); 1446 case IITDescriptor::Pointer: 1447 return PointerType::get(Context, D.Pointer_AddressSpace); 1448 case IITDescriptor::Struct: { 1449 SmallVector<Type *, 8> Elts; 1450 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 1451 Elts.push_back(DecodeFixedType(Infos, Tys, Context)); 1452 return StructType::get(Context, Elts); 1453 } 1454 case IITDescriptor::Argument: 1455 return Tys[D.getArgumentNumber()]; 1456 case IITDescriptor::ExtendArgument: { 1457 Type *Ty = Tys[D.getArgumentNumber()]; 1458 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 1459 return VectorType::getExtendedElementVectorType(VTy); 1460 1461 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth()); 1462 } 1463 case IITDescriptor::TruncArgument: { 1464 Type *Ty = Tys[D.getArgumentNumber()]; 1465 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 1466 return VectorType::getTruncatedElementVectorType(VTy); 1467 1468 IntegerType *ITy = cast<IntegerType>(Ty); 1469 assert(ITy->getBitWidth() % 2 == 0); 1470 return IntegerType::get(Context, ITy->getBitWidth() / 2); 1471 } 1472 case IITDescriptor::Subdivide2Argument: 1473 case IITDescriptor::Subdivide4Argument: { 1474 Type *Ty = Tys[D.getArgumentNumber()]; 1475 VectorType *VTy = dyn_cast<VectorType>(Ty); 1476 assert(VTy && "Expected an argument of Vector Type"); 1477 int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2; 1478 return VectorType::getSubdividedVectorType(VTy, SubDivs); 1479 } 1480 case IITDescriptor::HalfVecArgument: 1481 return VectorType::getHalfElementsVectorType(cast<VectorType>( 1482 Tys[D.getArgumentNumber()])); 1483 case IITDescriptor::SameVecWidthArgument: { 1484 Type *EltTy = DecodeFixedType(Infos, Tys, Context); 1485 Type *Ty = Tys[D.getArgumentNumber()]; 1486 if (auto *VTy = dyn_cast<VectorType>(Ty)) 1487 return VectorType::get(EltTy, VTy->getElementCount()); 1488 return EltTy; 1489 } 1490 case IITDescriptor::VecElementArgument: { 1491 Type *Ty = Tys[D.getArgumentNumber()]; 1492 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 1493 return VTy->getElementType(); 1494 llvm_unreachable("Expected an argument of Vector Type"); 1495 } 1496 case IITDescriptor::VecOfBitcastsToInt: { 1497 Type *Ty = Tys[D.getArgumentNumber()]; 1498 VectorType *VTy = dyn_cast<VectorType>(Ty); 1499 assert(VTy && "Expected an argument of Vector Type"); 1500 return VectorType::getInteger(VTy); 1501 } 1502 case IITDescriptor::VecOfAnyPtrsToElt: 1503 // Return the overloaded type (which determines the pointers address space) 1504 return Tys[D.getOverloadArgNumber()]; 1505 } 1506 llvm_unreachable("unhandled"); 1507 } 1508 1509 FunctionType *Intrinsic::getType(LLVMContext &Context, 1510 ID id, ArrayRef<Type*> Tys) { 1511 SmallVector<IITDescriptor, 8> Table; 1512 getIntrinsicInfoTableEntries(id, Table); 1513 1514 ArrayRef<IITDescriptor> TableRef = Table; 1515 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); 1516 1517 SmallVector<Type*, 8> ArgTys; 1518 while (!TableRef.empty()) 1519 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); 1520 1521 // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg 1522 // If we see void type as the type of the last argument, it is vararg intrinsic 1523 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) { 1524 ArgTys.pop_back(); 1525 return FunctionType::get(ResultTy, ArgTys, true); 1526 } 1527 return FunctionType::get(ResultTy, ArgTys, false); 1528 } 1529 1530 bool Intrinsic::isOverloaded(ID id) { 1531 #define GET_INTRINSIC_OVERLOAD_TABLE 1532 #include "llvm/IR/IntrinsicImpl.inc" 1533 #undef GET_INTRINSIC_OVERLOAD_TABLE 1534 } 1535 1536 /// This defines the "Intrinsic::getAttributes(ID id)" method. 1537 #define GET_INTRINSIC_ATTRIBUTES 1538 #include "llvm/IR/IntrinsicImpl.inc" 1539 #undef GET_INTRINSIC_ATTRIBUTES 1540 1541 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { 1542 // There can never be multiple globals with the same name of different types, 1543 // because intrinsics must be a specific type. 1544 auto *FT = getType(M->getContext(), id, Tys); 1545 return cast<Function>( 1546 M->getOrInsertFunction( 1547 Tys.empty() ? getName(id) : getName(id, Tys, M, FT), FT) 1548 .getCallee()); 1549 } 1550 1551 // This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method. 1552 #define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN 1553 #include "llvm/IR/IntrinsicImpl.inc" 1554 #undef GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN 1555 1556 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method. 1557 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 1558 #include "llvm/IR/IntrinsicImpl.inc" 1559 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN 1560 1561 bool Intrinsic::isConstrainedFPIntrinsic(ID QID) { 1562 switch (QID) { 1563 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ 1564 case Intrinsic::INTRINSIC: 1565 #include "llvm/IR/ConstrainedOps.def" 1566 #undef INSTRUCTION 1567 return true; 1568 default: 1569 return false; 1570 } 1571 } 1572 1573 bool Intrinsic::hasConstrainedFPRoundingModeOperand(Intrinsic::ID QID) { 1574 switch (QID) { 1575 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ 1576 case Intrinsic::INTRINSIC: \ 1577 return ROUND_MODE == 1; 1578 #include "llvm/IR/ConstrainedOps.def" 1579 #undef INSTRUCTION 1580 default: 1581 return false; 1582 } 1583 } 1584 1585 using DeferredIntrinsicMatchPair = 1586 std::pair<Type *, ArrayRef<Intrinsic::IITDescriptor>>; 1587 1588 static bool matchIntrinsicType( 1589 Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos, 1590 SmallVectorImpl<Type *> &ArgTys, 1591 SmallVectorImpl<DeferredIntrinsicMatchPair> &DeferredChecks, 1592 bool IsDeferredCheck) { 1593 using namespace Intrinsic; 1594 1595 // If we ran out of descriptors, there are too many arguments. 1596 if (Infos.empty()) return true; 1597 1598 // Do this before slicing off the 'front' part 1599 auto InfosRef = Infos; 1600 auto DeferCheck = [&DeferredChecks, &InfosRef](Type *T) { 1601 DeferredChecks.emplace_back(T, InfosRef); 1602 return false; 1603 }; 1604 1605 IITDescriptor D = Infos.front(); 1606 Infos = Infos.slice(1); 1607 1608 switch (D.Kind) { 1609 case IITDescriptor::Void: return !Ty->isVoidTy(); 1610 case IITDescriptor::VarArg: return true; 1611 case IITDescriptor::MMX: { 1612 FixedVectorType *VT = dyn_cast<FixedVectorType>(Ty); 1613 return !VT || VT->getNumElements() != 1 || 1614 !VT->getElementType()->isIntegerTy(64); 1615 } 1616 case IITDescriptor::AMX: return !Ty->isX86_AMXTy(); 1617 case IITDescriptor::Token: return !Ty->isTokenTy(); 1618 case IITDescriptor::Metadata: return !Ty->isMetadataTy(); 1619 case IITDescriptor::Half: return !Ty->isHalfTy(); 1620 case IITDescriptor::BFloat: return !Ty->isBFloatTy(); 1621 case IITDescriptor::Float: return !Ty->isFloatTy(); 1622 case IITDescriptor::Double: return !Ty->isDoubleTy(); 1623 case IITDescriptor::Quad: return !Ty->isFP128Ty(); 1624 case IITDescriptor::PPCQuad: return !Ty->isPPC_FP128Ty(); 1625 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width); 1626 case IITDescriptor::AArch64Svcount: 1627 return !isa<TargetExtType>(Ty) || 1628 cast<TargetExtType>(Ty)->getName() != "aarch64.svcount"; 1629 case IITDescriptor::Vector: { 1630 VectorType *VT = dyn_cast<VectorType>(Ty); 1631 return !VT || VT->getElementCount() != D.Vector_Width || 1632 matchIntrinsicType(VT->getElementType(), Infos, ArgTys, 1633 DeferredChecks, IsDeferredCheck); 1634 } 1635 case IITDescriptor::Pointer: { 1636 PointerType *PT = dyn_cast<PointerType>(Ty); 1637 return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace; 1638 } 1639 1640 case IITDescriptor::Struct: { 1641 StructType *ST = dyn_cast<StructType>(Ty); 1642 if (!ST || !ST->isLiteral() || ST->isPacked() || 1643 ST->getNumElements() != D.Struct_NumElements) 1644 return true; 1645 1646 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 1647 if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys, 1648 DeferredChecks, IsDeferredCheck)) 1649 return true; 1650 return false; 1651 } 1652 1653 case IITDescriptor::Argument: 1654 // If this is the second occurrence of an argument, 1655 // verify that the later instance matches the previous instance. 1656 if (D.getArgumentNumber() < ArgTys.size()) 1657 return Ty != ArgTys[D.getArgumentNumber()]; 1658 1659 if (D.getArgumentNumber() > ArgTys.size() || 1660 D.getArgumentKind() == IITDescriptor::AK_MatchType) 1661 return IsDeferredCheck || DeferCheck(Ty); 1662 1663 assert(D.getArgumentNumber() == ArgTys.size() && !IsDeferredCheck && 1664 "Table consistency error"); 1665 ArgTys.push_back(Ty); 1666 1667 switch (D.getArgumentKind()) { 1668 case IITDescriptor::AK_Any: return false; // Success 1669 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy(); 1670 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy(); 1671 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty); 1672 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty); 1673 default: break; 1674 } 1675 llvm_unreachable("all argument kinds not covered"); 1676 1677 case IITDescriptor::ExtendArgument: { 1678 // If this is a forward reference, defer the check for later. 1679 if (D.getArgumentNumber() >= ArgTys.size()) 1680 return IsDeferredCheck || DeferCheck(Ty); 1681 1682 Type *NewTy = ArgTys[D.getArgumentNumber()]; 1683 if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) 1684 NewTy = VectorType::getExtendedElementVectorType(VTy); 1685 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) 1686 NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth()); 1687 else 1688 return true; 1689 1690 return Ty != NewTy; 1691 } 1692 case IITDescriptor::TruncArgument: { 1693 // If this is a forward reference, defer the check for later. 1694 if (D.getArgumentNumber() >= ArgTys.size()) 1695 return IsDeferredCheck || DeferCheck(Ty); 1696 1697 Type *NewTy = ArgTys[D.getArgumentNumber()]; 1698 if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) 1699 NewTy = VectorType::getTruncatedElementVectorType(VTy); 1700 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) 1701 NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2); 1702 else 1703 return true; 1704 1705 return Ty != NewTy; 1706 } 1707 case IITDescriptor::HalfVecArgument: 1708 // If this is a forward reference, defer the check for later. 1709 if (D.getArgumentNumber() >= ArgTys.size()) 1710 return IsDeferredCheck || DeferCheck(Ty); 1711 return !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || 1712 VectorType::getHalfElementsVectorType( 1713 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; 1714 case IITDescriptor::SameVecWidthArgument: { 1715 if (D.getArgumentNumber() >= ArgTys.size()) { 1716 // Defer check and subsequent check for the vector element type. 1717 Infos = Infos.slice(1); 1718 return IsDeferredCheck || DeferCheck(Ty); 1719 } 1720 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); 1721 auto *ThisArgType = dyn_cast<VectorType>(Ty); 1722 // Both must be vectors of the same number of elements or neither. 1723 if ((ReferenceType != nullptr) != (ThisArgType != nullptr)) 1724 return true; 1725 Type *EltTy = Ty; 1726 if (ThisArgType) { 1727 if (ReferenceType->getElementCount() != 1728 ThisArgType->getElementCount()) 1729 return true; 1730 EltTy = ThisArgType->getElementType(); 1731 } 1732 return matchIntrinsicType(EltTy, Infos, ArgTys, DeferredChecks, 1733 IsDeferredCheck); 1734 } 1735 case IITDescriptor::VecOfAnyPtrsToElt: { 1736 unsigned RefArgNumber = D.getRefArgNumber(); 1737 if (RefArgNumber >= ArgTys.size()) { 1738 if (IsDeferredCheck) 1739 return true; 1740 // If forward referencing, already add the pointer-vector type and 1741 // defer the checks for later. 1742 ArgTys.push_back(Ty); 1743 return DeferCheck(Ty); 1744 } 1745 1746 if (!IsDeferredCheck){ 1747 assert(D.getOverloadArgNumber() == ArgTys.size() && 1748 "Table consistency error"); 1749 ArgTys.push_back(Ty); 1750 } 1751 1752 // Verify the overloaded type "matches" the Ref type. 1753 // i.e. Ty is a vector with the same width as Ref. 1754 // Composed of pointers to the same element type as Ref. 1755 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]); 1756 auto *ThisArgVecTy = dyn_cast<VectorType>(Ty); 1757 if (!ThisArgVecTy || !ReferenceType || 1758 (ReferenceType->getElementCount() != ThisArgVecTy->getElementCount())) 1759 return true; 1760 return !ThisArgVecTy->getElementType()->isPointerTy(); 1761 } 1762 case IITDescriptor::VecElementArgument: { 1763 if (D.getArgumentNumber() >= ArgTys.size()) 1764 return IsDeferredCheck ? true : DeferCheck(Ty); 1765 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); 1766 return !ReferenceType || Ty != ReferenceType->getElementType(); 1767 } 1768 case IITDescriptor::Subdivide2Argument: 1769 case IITDescriptor::Subdivide4Argument: { 1770 // If this is a forward reference, defer the check for later. 1771 if (D.getArgumentNumber() >= ArgTys.size()) 1772 return IsDeferredCheck || DeferCheck(Ty); 1773 1774 Type *NewTy = ArgTys[D.getArgumentNumber()]; 1775 if (auto *VTy = dyn_cast<VectorType>(NewTy)) { 1776 int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2; 1777 NewTy = VectorType::getSubdividedVectorType(VTy, SubDivs); 1778 return Ty != NewTy; 1779 } 1780 return true; 1781 } 1782 case IITDescriptor::VecOfBitcastsToInt: { 1783 if (D.getArgumentNumber() >= ArgTys.size()) 1784 return IsDeferredCheck || DeferCheck(Ty); 1785 auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); 1786 auto *ThisArgVecTy = dyn_cast<VectorType>(Ty); 1787 if (!ThisArgVecTy || !ReferenceType) 1788 return true; 1789 return ThisArgVecTy != VectorType::getInteger(ReferenceType); 1790 } 1791 } 1792 llvm_unreachable("unhandled"); 1793 } 1794 1795 Intrinsic::MatchIntrinsicTypesResult 1796 Intrinsic::matchIntrinsicSignature(FunctionType *FTy, 1797 ArrayRef<Intrinsic::IITDescriptor> &Infos, 1798 SmallVectorImpl<Type *> &ArgTys) { 1799 SmallVector<DeferredIntrinsicMatchPair, 2> DeferredChecks; 1800 if (matchIntrinsicType(FTy->getReturnType(), Infos, ArgTys, DeferredChecks, 1801 false)) 1802 return MatchIntrinsicTypes_NoMatchRet; 1803 1804 unsigned NumDeferredReturnChecks = DeferredChecks.size(); 1805 1806 for (auto *Ty : FTy->params()) 1807 if (matchIntrinsicType(Ty, Infos, ArgTys, DeferredChecks, false)) 1808 return MatchIntrinsicTypes_NoMatchArg; 1809 1810 for (unsigned I = 0, E = DeferredChecks.size(); I != E; ++I) { 1811 DeferredIntrinsicMatchPair &Check = DeferredChecks[I]; 1812 if (matchIntrinsicType(Check.first, Check.second, ArgTys, DeferredChecks, 1813 true)) 1814 return I < NumDeferredReturnChecks ? MatchIntrinsicTypes_NoMatchRet 1815 : MatchIntrinsicTypes_NoMatchArg; 1816 } 1817 1818 return MatchIntrinsicTypes_Match; 1819 } 1820 1821 bool 1822 Intrinsic::matchIntrinsicVarArg(bool isVarArg, 1823 ArrayRef<Intrinsic::IITDescriptor> &Infos) { 1824 // If there are no descriptors left, then it can't be a vararg. 1825 if (Infos.empty()) 1826 return isVarArg; 1827 1828 // There should be only one descriptor remaining at this point. 1829 if (Infos.size() != 1) 1830 return true; 1831 1832 // Check and verify the descriptor. 1833 IITDescriptor D = Infos.front(); 1834 Infos = Infos.slice(1); 1835 if (D.Kind == IITDescriptor::VarArg) 1836 return !isVarArg; 1837 1838 return true; 1839 } 1840 1841 bool Intrinsic::getIntrinsicSignature(Intrinsic::ID ID, FunctionType *FT, 1842 SmallVectorImpl<Type *> &ArgTys) { 1843 if (!ID) 1844 return false; 1845 1846 SmallVector<Intrinsic::IITDescriptor, 8> Table; 1847 getIntrinsicInfoTableEntries(ID, Table); 1848 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table; 1849 1850 if (Intrinsic::matchIntrinsicSignature(FT, TableRef, ArgTys) != 1851 Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) { 1852 return false; 1853 } 1854 if (Intrinsic::matchIntrinsicVarArg(FT->isVarArg(), TableRef)) 1855 return false; 1856 return true; 1857 } 1858 1859 bool Intrinsic::getIntrinsicSignature(Function *F, 1860 SmallVectorImpl<Type *> &ArgTys) { 1861 return getIntrinsicSignature(F->getIntrinsicID(), F->getFunctionType(), 1862 ArgTys); 1863 } 1864 1865 std::optional<Function *> Intrinsic::remangleIntrinsicFunction(Function *F) { 1866 SmallVector<Type *, 4> ArgTys; 1867 if (!getIntrinsicSignature(F, ArgTys)) 1868 return std::nullopt; 1869 1870 Intrinsic::ID ID = F->getIntrinsicID(); 1871 StringRef Name = F->getName(); 1872 std::string WantedName = 1873 Intrinsic::getName(ID, ArgTys, F->getParent(), F->getFunctionType()); 1874 if (Name == WantedName) 1875 return std::nullopt; 1876 1877 Function *NewDecl = [&] { 1878 if (auto *ExistingGV = F->getParent()->getNamedValue(WantedName)) { 1879 if (auto *ExistingF = dyn_cast<Function>(ExistingGV)) 1880 if (ExistingF->getFunctionType() == F->getFunctionType()) 1881 return ExistingF; 1882 1883 // The name already exists, but is not a function or has the wrong 1884 // prototype. Make place for the new one by renaming the old version. 1885 // Either this old version will be removed later on or the module is 1886 // invalid and we'll get an error. 1887 ExistingGV->setName(WantedName + ".renamed"); 1888 } 1889 return Intrinsic::getDeclaration(F->getParent(), ID, ArgTys); 1890 }(); 1891 1892 NewDecl->setCallingConv(F->getCallingConv()); 1893 assert(NewDecl->getFunctionType() == F->getFunctionType() && 1894 "Shouldn't change the signature"); 1895 return NewDecl; 1896 } 1897 1898 /// hasAddressTaken - returns true if there are any uses of this function 1899 /// other than direct calls or invokes to it. Optionally ignores callback 1900 /// uses, assume like pointer annotation calls, and references in llvm.used 1901 /// and llvm.compiler.used variables. 1902 bool Function::hasAddressTaken(const User **PutOffender, 1903 bool IgnoreCallbackUses, 1904 bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed, 1905 bool IgnoreARCAttachedCall, 1906 bool IgnoreCastedDirectCall) const { 1907 for (const Use &U : uses()) { 1908 const User *FU = U.getUser(); 1909 if (isa<BlockAddress>(FU)) 1910 continue; 1911 1912 if (IgnoreCallbackUses) { 1913 AbstractCallSite ACS(&U); 1914 if (ACS && ACS.isCallbackCall()) 1915 continue; 1916 } 1917 1918 const auto *Call = dyn_cast<CallBase>(FU); 1919 if (!Call) { 1920 if (IgnoreAssumeLikeCalls && 1921 isa<BitCastOperator, AddrSpaceCastOperator>(FU) && 1922 all_of(FU->users(), [](const User *U) { 1923 if (const auto *I = dyn_cast<IntrinsicInst>(U)) 1924 return I->isAssumeLikeIntrinsic(); 1925 return false; 1926 })) { 1927 continue; 1928 } 1929 1930 if (IgnoreLLVMUsed && !FU->user_empty()) { 1931 const User *FUU = FU; 1932 if (isa<BitCastOperator, AddrSpaceCastOperator>(FU) && 1933 FU->hasOneUse() && !FU->user_begin()->user_empty()) 1934 FUU = *FU->user_begin(); 1935 if (llvm::all_of(FUU->users(), [](const User *U) { 1936 if (const auto *GV = dyn_cast<GlobalVariable>(U)) 1937 return GV->hasName() && 1938 (GV->getName() == "llvm.compiler.used" || 1939 GV->getName() == "llvm.used"); 1940 return false; 1941 })) 1942 continue; 1943 } 1944 if (PutOffender) 1945 *PutOffender = FU; 1946 return true; 1947 } 1948 1949 if (IgnoreAssumeLikeCalls) { 1950 if (const auto *I = dyn_cast<IntrinsicInst>(Call)) 1951 if (I->isAssumeLikeIntrinsic()) 1952 continue; 1953 } 1954 1955 if (!Call->isCallee(&U) || (!IgnoreCastedDirectCall && 1956 Call->getFunctionType() != getFunctionType())) { 1957 if (IgnoreARCAttachedCall && 1958 Call->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall, 1959 U.getOperandNo())) 1960 continue; 1961 1962 if (PutOffender) 1963 *PutOffender = FU; 1964 return true; 1965 } 1966 } 1967 return false; 1968 } 1969 1970 bool Function::isDefTriviallyDead() const { 1971 // Check the linkage 1972 if (!hasLinkOnceLinkage() && !hasLocalLinkage() && 1973 !hasAvailableExternallyLinkage()) 1974 return false; 1975 1976 // Check if the function is used by anything other than a blockaddress. 1977 for (const User *U : users()) 1978 if (!isa<BlockAddress>(U)) 1979 return false; 1980 1981 return true; 1982 } 1983 1984 /// callsFunctionThatReturnsTwice - Return true if the function has a call to 1985 /// setjmp or other function that gcc recognizes as "returning twice". 1986 bool Function::callsFunctionThatReturnsTwice() const { 1987 for (const Instruction &I : instructions(this)) 1988 if (const auto *Call = dyn_cast<CallBase>(&I)) 1989 if (Call->hasFnAttr(Attribute::ReturnsTwice)) 1990 return true; 1991 1992 return false; 1993 } 1994 1995 Constant *Function::getPersonalityFn() const { 1996 assert(hasPersonalityFn() && getNumOperands()); 1997 return cast<Constant>(Op<0>()); 1998 } 1999 2000 void Function::setPersonalityFn(Constant *Fn) { 2001 setHungoffOperand<0>(Fn); 2002 setValueSubclassDataBit(3, Fn != nullptr); 2003 } 2004 2005 Constant *Function::getPrefixData() const { 2006 assert(hasPrefixData() && getNumOperands()); 2007 return cast<Constant>(Op<1>()); 2008 } 2009 2010 void Function::setPrefixData(Constant *PrefixData) { 2011 setHungoffOperand<1>(PrefixData); 2012 setValueSubclassDataBit(1, PrefixData != nullptr); 2013 } 2014 2015 Constant *Function::getPrologueData() const { 2016 assert(hasPrologueData() && getNumOperands()); 2017 return cast<Constant>(Op<2>()); 2018 } 2019 2020 void Function::setPrologueData(Constant *PrologueData) { 2021 setHungoffOperand<2>(PrologueData); 2022 setValueSubclassDataBit(2, PrologueData != nullptr); 2023 } 2024 2025 void Function::allocHungoffUselist() { 2026 // If we've already allocated a uselist, stop here. 2027 if (getNumOperands()) 2028 return; 2029 2030 allocHungoffUses(3, /*IsPhi=*/ false); 2031 setNumHungOffUseOperands(3); 2032 2033 // Initialize the uselist with placeholder operands to allow traversal. 2034 auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0)); 2035 Op<0>().set(CPN); 2036 Op<1>().set(CPN); 2037 Op<2>().set(CPN); 2038 } 2039 2040 template <int Idx> 2041 void Function::setHungoffOperand(Constant *C) { 2042 if (C) { 2043 allocHungoffUselist(); 2044 Op<Idx>().set(C); 2045 } else if (getNumOperands()) { 2046 Op<Idx>().set(ConstantPointerNull::get(PointerType::get(getContext(), 0))); 2047 } 2048 } 2049 2050 void Function::setValueSubclassDataBit(unsigned Bit, bool On) { 2051 assert(Bit < 16 && "SubclassData contains only 16 bits"); 2052 if (On) 2053 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit)); 2054 else 2055 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit)); 2056 } 2057 2058 void Function::setEntryCount(ProfileCount Count, 2059 const DenseSet<GlobalValue::GUID> *S) { 2060 #if !defined(NDEBUG) 2061 auto PrevCount = getEntryCount(); 2062 assert(!PrevCount || PrevCount->getType() == Count.getType()); 2063 #endif 2064 2065 auto ImportGUIDs = getImportGUIDs(); 2066 if (S == nullptr && ImportGUIDs.size()) 2067 S = &ImportGUIDs; 2068 2069 MDBuilder MDB(getContext()); 2070 setMetadata( 2071 LLVMContext::MD_prof, 2072 MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S)); 2073 } 2074 2075 void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type, 2076 const DenseSet<GlobalValue::GUID> *Imports) { 2077 setEntryCount(ProfileCount(Count, Type), Imports); 2078 } 2079 2080 std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const { 2081 MDNode *MD = getMetadata(LLVMContext::MD_prof); 2082 if (MD && MD->getOperand(0)) 2083 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) { 2084 if (MDS->getString() == "function_entry_count") { 2085 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 2086 uint64_t Count = CI->getValue().getZExtValue(); 2087 // A value of -1 is used for SamplePGO when there were no samples. 2088 // Treat this the same as unknown. 2089 if (Count == (uint64_t)-1) 2090 return std::nullopt; 2091 return ProfileCount(Count, PCT_Real); 2092 } else if (AllowSynthetic && 2093 MDS->getString() == "synthetic_function_entry_count") { 2094 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 2095 uint64_t Count = CI->getValue().getZExtValue(); 2096 return ProfileCount(Count, PCT_Synthetic); 2097 } 2098 } 2099 return std::nullopt; 2100 } 2101 2102 DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const { 2103 DenseSet<GlobalValue::GUID> R; 2104 if (MDNode *MD = getMetadata(LLVMContext::MD_prof)) 2105 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) 2106 if (MDS->getString() == "function_entry_count") 2107 for (unsigned i = 2; i < MD->getNumOperands(); i++) 2108 R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i)) 2109 ->getValue() 2110 .getZExtValue()); 2111 return R; 2112 } 2113 2114 void Function::setSectionPrefix(StringRef Prefix) { 2115 MDBuilder MDB(getContext()); 2116 setMetadata(LLVMContext::MD_section_prefix, 2117 MDB.createFunctionSectionPrefix(Prefix)); 2118 } 2119 2120 std::optional<StringRef> Function::getSectionPrefix() const { 2121 if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) { 2122 assert(cast<MDString>(MD->getOperand(0))->getString() == 2123 "function_section_prefix" && 2124 "Metadata not match"); 2125 return cast<MDString>(MD->getOperand(1))->getString(); 2126 } 2127 return std::nullopt; 2128 } 2129 2130 bool Function::nullPointerIsDefined() const { 2131 return hasFnAttribute(Attribute::NullPointerIsValid); 2132 } 2133 2134 bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) { 2135 if (F && F->nullPointerIsDefined()) 2136 return true; 2137 2138 if (AS != 0) 2139 return true; 2140 2141 return false; 2142 } 2143