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