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