1 //===- Function.cpp - Implement the Global object classes -----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the Function class for the IR library. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/IR/Function.h" 14 #include "SymbolTableListTraitsImpl.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/BitVector.h" 17 #include "llvm/ADT/DenseSet.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/IR/AbstractCallSite.h" 24 #include "llvm/IR/Argument.h" 25 #include "llvm/IR/Attributes.h" 26 #include "llvm/IR/BasicBlock.h" 27 #include "llvm/IR/Constant.h" 28 #include "llvm/IR/ConstantRange.h" 29 #include "llvm/IR/Constants.h" 30 #include "llvm/IR/DerivedTypes.h" 31 #include "llvm/IR/GlobalValue.h" 32 #include "llvm/IR/InstIterator.h" 33 #include "llvm/IR/Instruction.h" 34 #include "llvm/IR/IntrinsicInst.h" 35 #include "llvm/IR/Intrinsics.h" 36 #include "llvm/IR/LLVMContext.h" 37 #include "llvm/IR/MDBuilder.h" 38 #include "llvm/IR/Metadata.h" 39 #include "llvm/IR/Module.h" 40 #include "llvm/IR/Operator.h" 41 #include "llvm/IR/SymbolTableListTraits.h" 42 #include "llvm/IR/Type.h" 43 #include "llvm/IR/Use.h" 44 #include "llvm/IR/User.h" 45 #include "llvm/IR/Value.h" 46 #include "llvm/IR/ValueSymbolTable.h" 47 #include "llvm/Support/Casting.h" 48 #include "llvm/Support/CommandLine.h" 49 #include "llvm/Support/Compiler.h" 50 #include "llvm/Support/ErrorHandling.h" 51 #include "llvm/Support/ModRef.h" 52 #include <cassert> 53 #include <cstddef> 54 #include <cstdint> 55 #include <cstring> 56 #include <string> 57 58 using namespace llvm; 59 using ProfileCount = Function::ProfileCount; 60 61 // Explicit instantiations of SymbolTableListTraits since some of the methods 62 // are not in the public header file... 63 template class llvm::SymbolTableListTraits<BasicBlock>; 64 65 static cl::opt<int> NonGlobalValueMaxNameSize( 66 "non-global-value-max-name-size", cl::Hidden, cl::init(1024), 67 cl::desc("Maximum size for the name of non-global values.")); 68 69 extern cl::opt<bool> UseNewDbgInfoFormat; 70 71 void Function::renumberBlocks() { 72 validateBlockNumbers(); 73 74 NextBlockNum = 0; 75 for (auto &BB : *this) 76 BB.Number = NextBlockNum++; 77 BlockNumEpoch++; 78 } 79 80 void Function::validateBlockNumbers() const { 81 #ifndef NDEBUG 82 BitVector Numbers(NextBlockNum); 83 for (const auto &BB : *this) { 84 unsigned Num = BB.getNumber(); 85 assert(Num < NextBlockNum && "out of range block number"); 86 assert(!Numbers[Num] && "duplicate block numbers"); 87 Numbers.set(Num); 88 } 89 #endif 90 } 91 92 void Function::convertToNewDbgValues() { 93 IsNewDbgInfoFormat = true; 94 for (auto &BB : *this) { 95 BB.convertToNewDbgValues(); 96 } 97 } 98 99 void Function::convertFromNewDbgValues() { 100 IsNewDbgInfoFormat = false; 101 for (auto &BB : *this) { 102 BB.convertFromNewDbgValues(); 103 } 104 } 105 106 void Function::setIsNewDbgInfoFormat(bool NewFlag) { 107 if (NewFlag && !IsNewDbgInfoFormat) 108 convertToNewDbgValues(); 109 else if (!NewFlag && IsNewDbgInfoFormat) 110 convertFromNewDbgValues(); 111 } 112 void Function::setNewDbgInfoFormatFlag(bool NewFlag) { 113 for (auto &BB : *this) { 114 BB.setNewDbgInfoFormatFlag(NewFlag); 115 } 116 IsNewDbgInfoFormat = NewFlag; 117 } 118 119 //===----------------------------------------------------------------------===// 120 // Argument Implementation 121 //===----------------------------------------------------------------------===// 122 123 Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo) 124 : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) { 125 setName(Name); 126 } 127 128 void Argument::setParent(Function *parent) { 129 Parent = parent; 130 } 131 132 bool Argument::hasNonNullAttr(bool AllowUndefOrPoison) const { 133 if (!getType()->isPointerTy()) return false; 134 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull) && 135 (AllowUndefOrPoison || 136 getParent()->hasParamAttribute(getArgNo(), Attribute::NoUndef))) 137 return true; 138 else if (getDereferenceableBytes() > 0 && 139 !NullPointerIsDefined(getParent(), 140 getType()->getPointerAddressSpace())) 141 return true; 142 return false; 143 } 144 145 bool Argument::hasByValAttr() const { 146 if (!getType()->isPointerTy()) return false; 147 return hasAttribute(Attribute::ByVal); 148 } 149 150 bool Argument::hasByRefAttr() const { 151 if (!getType()->isPointerTy()) 152 return false; 153 return hasAttribute(Attribute::ByRef); 154 } 155 156 bool Argument::hasSwiftSelfAttr() const { 157 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf); 158 } 159 160 bool Argument::hasSwiftErrorAttr() const { 161 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError); 162 } 163 164 bool Argument::hasInAllocaAttr() const { 165 if (!getType()->isPointerTy()) return false; 166 return hasAttribute(Attribute::InAlloca); 167 } 168 169 bool Argument::hasPreallocatedAttr() const { 170 if (!getType()->isPointerTy()) 171 return false; 172 return hasAttribute(Attribute::Preallocated); 173 } 174 175 bool Argument::hasPassPointeeByValueCopyAttr() const { 176 if (!getType()->isPointerTy()) return false; 177 AttributeList Attrs = getParent()->getAttributes(); 178 return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) || 179 Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) || 180 Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated); 181 } 182 183 bool Argument::hasPointeeInMemoryValueAttr() const { 184 if (!getType()->isPointerTy()) 185 return false; 186 AttributeList Attrs = getParent()->getAttributes(); 187 return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) || 188 Attrs.hasParamAttr(getArgNo(), Attribute::StructRet) || 189 Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) || 190 Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated) || 191 Attrs.hasParamAttr(getArgNo(), Attribute::ByRef); 192 } 193 194 /// For a byval, sret, inalloca, or preallocated parameter, get the in-memory 195 /// parameter type. 196 static Type *getMemoryParamAllocType(AttributeSet ParamAttrs) { 197 // FIXME: All the type carrying attributes are mutually exclusive, so there 198 // should be a single query to get the stored type that handles any of them. 199 if (Type *ByValTy = ParamAttrs.getByValType()) 200 return ByValTy; 201 if (Type *ByRefTy = ParamAttrs.getByRefType()) 202 return ByRefTy; 203 if (Type *PreAllocTy = ParamAttrs.getPreallocatedType()) 204 return PreAllocTy; 205 if (Type *InAllocaTy = ParamAttrs.getInAllocaType()) 206 return InAllocaTy; 207 if (Type *SRetTy = ParamAttrs.getStructRetType()) 208 return SRetTy; 209 210 return nullptr; 211 } 212 213 uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout &DL) const { 214 AttributeSet ParamAttrs = 215 getParent()->getAttributes().getParamAttrs(getArgNo()); 216 if (Type *MemTy = getMemoryParamAllocType(ParamAttrs)) 217 return DL.getTypeAllocSize(MemTy); 218 return 0; 219 } 220 221 Type *Argument::getPointeeInMemoryValueType() const { 222 AttributeSet ParamAttrs = 223 getParent()->getAttributes().getParamAttrs(getArgNo()); 224 return getMemoryParamAllocType(ParamAttrs); 225 } 226 227 MaybeAlign Argument::getParamAlign() const { 228 assert(getType()->isPointerTy() && "Only pointers have alignments"); 229 return getParent()->getParamAlign(getArgNo()); 230 } 231 232 MaybeAlign Argument::getParamStackAlign() const { 233 return getParent()->getParamStackAlign(getArgNo()); 234 } 235 236 Type *Argument::getParamByValType() const { 237 assert(getType()->isPointerTy() && "Only pointers have byval types"); 238 return getParent()->getParamByValType(getArgNo()); 239 } 240 241 Type *Argument::getParamStructRetType() const { 242 assert(getType()->isPointerTy() && "Only pointers have sret types"); 243 return getParent()->getParamStructRetType(getArgNo()); 244 } 245 246 Type *Argument::getParamByRefType() const { 247 assert(getType()->isPointerTy() && "Only pointers have byref types"); 248 return getParent()->getParamByRefType(getArgNo()); 249 } 250 251 Type *Argument::getParamInAllocaType() const { 252 assert(getType()->isPointerTy() && "Only pointers have inalloca types"); 253 return getParent()->getParamInAllocaType(getArgNo()); 254 } 255 256 uint64_t Argument::getDereferenceableBytes() const { 257 assert(getType()->isPointerTy() && 258 "Only pointers have dereferenceable bytes"); 259 return getParent()->getParamDereferenceableBytes(getArgNo()); 260 } 261 262 uint64_t Argument::getDereferenceableOrNullBytes() const { 263 assert(getType()->isPointerTy() && 264 "Only pointers have dereferenceable bytes"); 265 return getParent()->getParamDereferenceableOrNullBytes(getArgNo()); 266 } 267 268 FPClassTest Argument::getNoFPClass() const { 269 return getParent()->getParamNoFPClass(getArgNo()); 270 } 271 272 std::optional<ConstantRange> Argument::getRange() const { 273 const Attribute RangeAttr = getAttribute(llvm::Attribute::Range); 274 if (RangeAttr.isValid()) 275 return RangeAttr.getRange(); 276 return std::nullopt; 277 } 278 279 bool Argument::hasNestAttr() const { 280 if (!getType()->isPointerTy()) return false; 281 return hasAttribute(Attribute::Nest); 282 } 283 284 bool Argument::hasNoAliasAttr() const { 285 if (!getType()->isPointerTy()) return false; 286 return hasAttribute(Attribute::NoAlias); 287 } 288 289 bool Argument::hasNoCaptureAttr() const { 290 if (!getType()->isPointerTy()) return false; 291 return hasAttribute(Attribute::NoCapture); 292 } 293 294 bool Argument::hasNoFreeAttr() const { 295 if (!getType()->isPointerTy()) return false; 296 return hasAttribute(Attribute::NoFree); 297 } 298 299 bool Argument::hasStructRetAttr() const { 300 if (!getType()->isPointerTy()) return false; 301 return hasAttribute(Attribute::StructRet); 302 } 303 304 bool Argument::hasInRegAttr() const { 305 return hasAttribute(Attribute::InReg); 306 } 307 308 bool Argument::hasReturnedAttr() const { 309 return hasAttribute(Attribute::Returned); 310 } 311 312 bool Argument::hasZExtAttr() const { 313 return hasAttribute(Attribute::ZExt); 314 } 315 316 bool Argument::hasSExtAttr() const { 317 return hasAttribute(Attribute::SExt); 318 } 319 320 bool Argument::onlyReadsMemory() const { 321 AttributeList Attrs = getParent()->getAttributes(); 322 return Attrs.hasParamAttr(getArgNo(), Attribute::ReadOnly) || 323 Attrs.hasParamAttr(getArgNo(), Attribute::ReadNone); 324 } 325 326 void Argument::addAttrs(AttrBuilder &B) { 327 AttributeList AL = getParent()->getAttributes(); 328 AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B); 329 getParent()->setAttributes(AL); 330 } 331 332 void Argument::addAttr(Attribute::AttrKind Kind) { 333 getParent()->addParamAttr(getArgNo(), Kind); 334 } 335 336 void Argument::addAttr(Attribute Attr) { 337 getParent()->addParamAttr(getArgNo(), Attr); 338 } 339 340 void Argument::removeAttr(Attribute::AttrKind Kind) { 341 getParent()->removeParamAttr(getArgNo(), Kind); 342 } 343 344 void Argument::removeAttrs(const AttributeMask &AM) { 345 AttributeList AL = getParent()->getAttributes(); 346 AL = AL.removeParamAttributes(Parent->getContext(), getArgNo(), AM); 347 getParent()->setAttributes(AL); 348 } 349 350 bool Argument::hasAttribute(Attribute::AttrKind Kind) const { 351 return getParent()->hasParamAttribute(getArgNo(), Kind); 352 } 353 354 bool Argument::hasAttribute(StringRef Kind) const { 355 return getParent()->hasParamAttribute(getArgNo(), Kind); 356 } 357 358 Attribute Argument::getAttribute(Attribute::AttrKind Kind) const { 359 return getParent()->getParamAttribute(getArgNo(), Kind); 360 } 361 362 //===----------------------------------------------------------------------===// 363 // Helper Methods in Function 364 //===----------------------------------------------------------------------===// 365 366 LLVMContext &Function::getContext() const { 367 return getType()->getContext(); 368 } 369 370 const DataLayout &Function::getDataLayout() const { 371 return getParent()->getDataLayout(); 372 } 373 374 unsigned Function::getInstructionCount() const { 375 unsigned NumInstrs = 0; 376 for (const BasicBlock &BB : BasicBlocks) 377 NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(), 378 BB.instructionsWithoutDebug().end()); 379 return NumInstrs; 380 } 381 382 Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage, 383 const Twine &N, Module &M) { 384 return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M); 385 } 386 387 Function *Function::createWithDefaultAttr(FunctionType *Ty, 388 LinkageTypes Linkage, 389 unsigned AddrSpace, const Twine &N, 390 Module *M) { 391 auto *F = new (AllocMarker) Function(Ty, Linkage, AddrSpace, N, M); 392 AttrBuilder B(F->getContext()); 393 UWTableKind UWTable = M->getUwtable(); 394 if (UWTable != UWTableKind::None) 395 B.addUWTableAttr(UWTable); 396 switch (M->getFramePointer()) { 397 case FramePointerKind::None: 398 // 0 ("none") is the default. 399 break; 400 case FramePointerKind::Reserved: 401 B.addAttribute("frame-pointer", "reserved"); 402 break; 403 case FramePointerKind::NonLeaf: 404 B.addAttribute("frame-pointer", "non-leaf"); 405 break; 406 case FramePointerKind::All: 407 B.addAttribute("frame-pointer", "all"); 408 break; 409 } 410 if (M->getModuleFlag("function_return_thunk_extern")) 411 B.addAttribute(Attribute::FnRetThunkExtern); 412 StringRef DefaultCPU = F->getContext().getDefaultTargetCPU(); 413 if (!DefaultCPU.empty()) 414 B.addAttribute("target-cpu", DefaultCPU); 415 StringRef DefaultFeatures = F->getContext().getDefaultTargetFeatures(); 416 if (!DefaultFeatures.empty()) 417 B.addAttribute("target-features", DefaultFeatures); 418 419 // Check if the module attribute is present and not zero. 420 auto isModuleAttributeSet = [&](const StringRef &ModAttr) -> bool { 421 const auto *Attr = 422 mdconst::extract_or_null<ConstantInt>(M->getModuleFlag(ModAttr)); 423 return Attr && !Attr->isZero(); 424 }; 425 426 auto AddAttributeIfSet = [&](const StringRef &ModAttr) { 427 if (isModuleAttributeSet(ModAttr)) 428 B.addAttribute(ModAttr); 429 }; 430 431 StringRef SignType = "none"; 432 if (isModuleAttributeSet("sign-return-address")) 433 SignType = "non-leaf"; 434 if (isModuleAttributeSet("sign-return-address-all")) 435 SignType = "all"; 436 if (SignType != "none") { 437 B.addAttribute("sign-return-address", SignType); 438 B.addAttribute("sign-return-address-key", 439 isModuleAttributeSet("sign-return-address-with-bkey") 440 ? "b_key" 441 : "a_key"); 442 } 443 AddAttributeIfSet("branch-target-enforcement"); 444 AddAttributeIfSet("branch-protection-pauth-lr"); 445 AddAttributeIfSet("guarded-control-stack"); 446 447 F->addFnAttrs(B); 448 return F; 449 } 450 451 void Function::removeFromParent() { 452 getParent()->getFunctionList().remove(getIterator()); 453 } 454 455 void Function::eraseFromParent() { 456 getParent()->getFunctionList().erase(getIterator()); 457 } 458 459 void Function::splice(Function::iterator ToIt, Function *FromF, 460 Function::iterator FromBeginIt, 461 Function::iterator FromEndIt) { 462 #ifdef EXPENSIVE_CHECKS 463 // Check that FromBeginIt is before FromEndIt. 464 auto FromFEnd = FromF->end(); 465 for (auto It = FromBeginIt; It != FromEndIt; ++It) 466 assert(It != FromFEnd && "FromBeginIt not before FromEndIt!"); 467 #endif // EXPENSIVE_CHECKS 468 BasicBlocks.splice(ToIt, FromF->BasicBlocks, FromBeginIt, FromEndIt); 469 } 470 471 Function::iterator Function::erase(Function::iterator FromIt, 472 Function::iterator ToIt) { 473 return BasicBlocks.erase(FromIt, ToIt); 474 } 475 476 //===----------------------------------------------------------------------===// 477 // Function Implementation 478 //===----------------------------------------------------------------------===// 479 480 static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) { 481 // If AS == -1 and we are passed a valid module pointer we place the function 482 // in the program address space. Otherwise we default to AS0. 483 if (AddrSpace == static_cast<unsigned>(-1)) 484 return M ? M->getDataLayout().getProgramAddressSpace() : 0; 485 return AddrSpace; 486 } 487 488 Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, 489 const Twine &name, Module *ParentModule) 490 : GlobalObject(Ty, Value::FunctionVal, AllocMarker, Linkage, name, 491 computeAddrSpace(AddrSpace, ParentModule)), 492 NumArgs(Ty->getNumParams()), IsNewDbgInfoFormat(UseNewDbgInfoFormat) { 493 assert(FunctionType::isValidReturnType(getReturnType()) && 494 "invalid return type"); 495 setGlobalObjectSubClassData(0); 496 497 // We only need a symbol table for a function if the context keeps value names 498 if (!getContext().shouldDiscardValueNames()) 499 SymTab = std::make_unique<ValueSymbolTable>(NonGlobalValueMaxNameSize); 500 501 // If the function has arguments, mark them as lazily built. 502 if (Ty->getNumParams()) 503 setValueSubclassData(1); // Set the "has lazy arguments" bit. 504 505 if (ParentModule) { 506 ParentModule->getFunctionList().push_back(this); 507 IsNewDbgInfoFormat = ParentModule->IsNewDbgInfoFormat; 508 } 509 510 HasLLVMReservedName = getName().starts_with("llvm."); 511 // Ensure intrinsics have the right parameter attributes. 512 // Note, the IntID field will have been set in Value::setName if this function 513 // name is a valid intrinsic ID. 514 if (IntID) 515 setAttributes(Intrinsic::getAttributes(getContext(), IntID)); 516 } 517 518 Function::~Function() { 519 validateBlockNumbers(); 520 521 dropAllReferences(); // After this it is safe to delete instructions. 522 523 // Delete all of the method arguments and unlink from symbol table... 524 if (Arguments) 525 clearArguments(); 526 527 // Remove the function from the on-the-side GC table. 528 clearGC(); 529 } 530 531 void Function::BuildLazyArguments() const { 532 // Create the arguments vector, all arguments start out unnamed. 533 auto *FT = getFunctionType(); 534 if (NumArgs > 0) { 535 Arguments = std::allocator<Argument>().allocate(NumArgs); 536 for (unsigned i = 0, e = NumArgs; i != e; ++i) { 537 Type *ArgTy = FT->getParamType(i); 538 assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!"); 539 new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i); 540 } 541 } 542 543 // Clear the lazy arguments bit. 544 unsigned SDC = getSubclassDataFromValue(); 545 SDC &= ~(1 << 0); 546 const_cast<Function*>(this)->setValueSubclassData(SDC); 547 assert(!hasLazyArguments()); 548 } 549 550 static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) { 551 return MutableArrayRef<Argument>(Args, Count); 552 } 553 554 bool Function::isConstrainedFPIntrinsic() const { 555 return Intrinsic::isConstrainedFPIntrinsic(getIntrinsicID()); 556 } 557 558 void Function::clearArguments() { 559 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 560 A.setName(""); 561 A.~Argument(); 562 } 563 std::allocator<Argument>().deallocate(Arguments, NumArgs); 564 Arguments = nullptr; 565 } 566 567 void Function::stealArgumentListFrom(Function &Src) { 568 assert(isDeclaration() && "Expected no references to current arguments"); 569 570 // Drop the current arguments, if any, and set the lazy argument bit. 571 if (!hasLazyArguments()) { 572 assert(llvm::all_of(makeArgArray(Arguments, NumArgs), 573 [](const Argument &A) { return A.use_empty(); }) && 574 "Expected arguments to be unused in declaration"); 575 clearArguments(); 576 setValueSubclassData(getSubclassDataFromValue() | (1 << 0)); 577 } 578 579 // Nothing to steal if Src has lazy arguments. 580 if (Src.hasLazyArguments()) 581 return; 582 583 // Steal arguments from Src, and fix the lazy argument bits. 584 assert(arg_size() == Src.arg_size()); 585 Arguments = Src.Arguments; 586 Src.Arguments = nullptr; 587 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 588 // FIXME: This does the work of transferNodesFromList inefficiently. 589 SmallString<128> Name; 590 if (A.hasName()) 591 Name = A.getName(); 592 if (!Name.empty()) 593 A.setName(""); 594 A.setParent(this); 595 if (!Name.empty()) 596 A.setName(Name); 597 } 598 599 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0)); 600 assert(!hasLazyArguments()); 601 Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0)); 602 } 603 604 void Function::deleteBodyImpl(bool ShouldDrop) { 605 setIsMaterializable(false); 606 607 for (BasicBlock &BB : *this) 608 BB.dropAllReferences(); 609 610 // Delete all basic blocks. They are now unused, except possibly by 611 // blockaddresses, but BasicBlock's destructor takes care of those. 612 while (!BasicBlocks.empty()) 613 BasicBlocks.begin()->eraseFromParent(); 614 615 if (getNumOperands()) { 616 if (ShouldDrop) { 617 // Drop uses of any optional data (real or placeholder). 618 User::dropAllReferences(); 619 setNumHungOffUseOperands(0); 620 } else { 621 // The code needs to match Function::allocHungoffUselist(). 622 auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0)); 623 Op<0>().set(CPN); 624 Op<1>().set(CPN); 625 Op<2>().set(CPN); 626 } 627 setValueSubclassData(getSubclassDataFromValue() & ~0xe); 628 } 629 630 // Metadata is stored in a side-table. 631 clearMetadata(); 632 } 633 634 void Function::addAttributeAtIndex(unsigned i, Attribute Attr) { 635 AttributeSets = AttributeSets.addAttributeAtIndex(getContext(), i, Attr); 636 } 637 638 void Function::addFnAttr(Attribute::AttrKind Kind) { 639 AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind); 640 } 641 642 void Function::addFnAttr(StringRef Kind, StringRef Val) { 643 AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind, Val); 644 } 645 646 void Function::addFnAttr(Attribute Attr) { 647 AttributeSets = AttributeSets.addFnAttribute(getContext(), Attr); 648 } 649 650 void Function::addFnAttrs(const AttrBuilder &Attrs) { 651 AttributeSets = AttributeSets.addFnAttributes(getContext(), Attrs); 652 } 653 654 void Function::addRetAttr(Attribute::AttrKind Kind) { 655 AttributeSets = AttributeSets.addRetAttribute(getContext(), Kind); 656 } 657 658 void Function::addRetAttr(Attribute Attr) { 659 AttributeSets = AttributeSets.addRetAttribute(getContext(), Attr); 660 } 661 662 void Function::addRetAttrs(const AttrBuilder &Attrs) { 663 AttributeSets = AttributeSets.addRetAttributes(getContext(), Attrs); 664 } 665 666 void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 667 AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Kind); 668 } 669 670 void Function::addParamAttr(unsigned ArgNo, Attribute Attr) { 671 AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Attr); 672 } 673 674 void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { 675 AttributeSets = AttributeSets.addParamAttributes(getContext(), ArgNo, Attrs); 676 } 677 678 void Function::removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { 679 AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind); 680 } 681 682 void Function::removeAttributeAtIndex(unsigned i, StringRef Kind) { 683 AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind); 684 } 685 686 void Function::removeFnAttr(Attribute::AttrKind Kind) { 687 AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); 688 } 689 690 void Function::removeFnAttr(StringRef Kind) { 691 AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); 692 } 693 694 void Function::removeFnAttrs(const AttributeMask &AM) { 695 AttributeSets = AttributeSets.removeFnAttributes(getContext(), AM); 696 } 697 698 void Function::removeRetAttr(Attribute::AttrKind Kind) { 699 AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); 700 } 701 702 void Function::removeRetAttr(StringRef Kind) { 703 AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); 704 } 705 706 void Function::removeRetAttrs(const AttributeMask &Attrs) { 707 AttributeSets = AttributeSets.removeRetAttributes(getContext(), Attrs); 708 } 709 710 void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 711 AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); 712 } 713 714 void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) { 715 AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); 716 } 717 718 void Function::removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs) { 719 AttributeSets = 720 AttributeSets.removeParamAttributes(getContext(), ArgNo, Attrs); 721 } 722 723 void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) { 724 AttributeSets = 725 AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes); 726 } 727 728 bool Function::hasFnAttribute(Attribute::AttrKind Kind) const { 729 return AttributeSets.hasFnAttr(Kind); 730 } 731 732 bool Function::hasFnAttribute(StringRef Kind) const { 733 return AttributeSets.hasFnAttr(Kind); 734 } 735 736 bool Function::hasRetAttribute(Attribute::AttrKind Kind) const { 737 return AttributeSets.hasRetAttr(Kind); 738 } 739 740 bool Function::hasParamAttribute(unsigned ArgNo, 741 Attribute::AttrKind Kind) const { 742 return AttributeSets.hasParamAttr(ArgNo, Kind); 743 } 744 745 bool Function::hasParamAttribute(unsigned ArgNo, StringRef Kind) const { 746 return AttributeSets.hasParamAttr(ArgNo, Kind); 747 } 748 749 Attribute Function::getAttributeAtIndex(unsigned i, 750 Attribute::AttrKind Kind) const { 751 return AttributeSets.getAttributeAtIndex(i, Kind); 752 } 753 754 Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const { 755 return AttributeSets.getAttributeAtIndex(i, Kind); 756 } 757 758 bool Function::hasAttributeAtIndex(unsigned Idx, 759 Attribute::AttrKind Kind) const { 760 return AttributeSets.hasAttributeAtIndex(Idx, Kind); 761 } 762 763 Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const { 764 return AttributeSets.getFnAttr(Kind); 765 } 766 767 Attribute Function::getFnAttribute(StringRef Kind) const { 768 return AttributeSets.getFnAttr(Kind); 769 } 770 771 Attribute Function::getRetAttribute(Attribute::AttrKind Kind) const { 772 return AttributeSets.getRetAttr(Kind); 773 } 774 775 uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name, 776 uint64_t Default) const { 777 Attribute A = getFnAttribute(Name); 778 uint64_t Result = Default; 779 if (A.isStringAttribute()) { 780 StringRef Str = A.getValueAsString(); 781 if (Str.getAsInteger(0, Result)) 782 getContext().emitError("cannot parse integer attribute " + Name); 783 } 784 785 return Result; 786 } 787 788 /// gets the specified attribute from the list of attributes. 789 Attribute Function::getParamAttribute(unsigned ArgNo, 790 Attribute::AttrKind Kind) const { 791 return AttributeSets.getParamAttr(ArgNo, Kind); 792 } 793 794 void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo, 795 uint64_t Bytes) { 796 AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(getContext(), 797 ArgNo, Bytes); 798 } 799 800 void Function::addRangeRetAttr(const ConstantRange &CR) { 801 AttributeSets = AttributeSets.addRangeRetAttr(getContext(), CR); 802 } 803 804 DenormalMode Function::getDenormalMode(const fltSemantics &FPType) const { 805 if (&FPType == &APFloat::IEEEsingle()) { 806 DenormalMode Mode = getDenormalModeF32Raw(); 807 // If the f32 variant of the attribute isn't specified, try to use the 808 // generic one. 809 if (Mode.isValid()) 810 return Mode; 811 } 812 813 return getDenormalModeRaw(); 814 } 815 816 DenormalMode Function::getDenormalModeRaw() const { 817 Attribute Attr = getFnAttribute("denormal-fp-math"); 818 StringRef Val = Attr.getValueAsString(); 819 return parseDenormalFPAttribute(Val); 820 } 821 822 DenormalMode Function::getDenormalModeF32Raw() const { 823 Attribute Attr = getFnAttribute("denormal-fp-math-f32"); 824 if (Attr.isValid()) { 825 StringRef Val = Attr.getValueAsString(); 826 return parseDenormalFPAttribute(Val); 827 } 828 829 return DenormalMode::getInvalid(); 830 } 831 832 const std::string &Function::getGC() const { 833 assert(hasGC() && "Function has no collector"); 834 return getContext().getGC(*this); 835 } 836 837 void Function::setGC(std::string Str) { 838 setValueSubclassDataBit(14, !Str.empty()); 839 getContext().setGC(*this, std::move(Str)); 840 } 841 842 void Function::clearGC() { 843 if (!hasGC()) 844 return; 845 getContext().deleteGC(*this); 846 setValueSubclassDataBit(14, false); 847 } 848 849 bool Function::hasStackProtectorFnAttr() const { 850 return hasFnAttribute(Attribute::StackProtect) || 851 hasFnAttribute(Attribute::StackProtectStrong) || 852 hasFnAttribute(Attribute::StackProtectReq); 853 } 854 855 /// Copy all additional attributes (those not needed to create a Function) from 856 /// the Function Src to this one. 857 void Function::copyAttributesFrom(const Function *Src) { 858 GlobalObject::copyAttributesFrom(Src); 859 setCallingConv(Src->getCallingConv()); 860 setAttributes(Src->getAttributes()); 861 if (Src->hasGC()) 862 setGC(Src->getGC()); 863 else 864 clearGC(); 865 if (Src->hasPersonalityFn()) 866 setPersonalityFn(Src->getPersonalityFn()); 867 if (Src->hasPrefixData()) 868 setPrefixData(Src->getPrefixData()); 869 if (Src->hasPrologueData()) 870 setPrologueData(Src->getPrologueData()); 871 } 872 873 MemoryEffects Function::getMemoryEffects() const { 874 return getAttributes().getMemoryEffects(); 875 } 876 void Function::setMemoryEffects(MemoryEffects ME) { 877 addFnAttr(Attribute::getWithMemoryEffects(getContext(), ME)); 878 } 879 880 /// Determine if the function does not access memory. 881 bool Function::doesNotAccessMemory() const { 882 return getMemoryEffects().doesNotAccessMemory(); 883 } 884 void Function::setDoesNotAccessMemory() { 885 setMemoryEffects(MemoryEffects::none()); 886 } 887 888 /// Determine if the function does not access or only reads memory. 889 bool Function::onlyReadsMemory() const { 890 return getMemoryEffects().onlyReadsMemory(); 891 } 892 void Function::setOnlyReadsMemory() { 893 setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly()); 894 } 895 896 /// Determine if the function does not access or only writes memory. 897 bool Function::onlyWritesMemory() const { 898 return getMemoryEffects().onlyWritesMemory(); 899 } 900 void Function::setOnlyWritesMemory() { 901 setMemoryEffects(getMemoryEffects() & MemoryEffects::writeOnly()); 902 } 903 904 /// Determine if the call can access memmory only using pointers based 905 /// on its arguments. 906 bool Function::onlyAccessesArgMemory() const { 907 return getMemoryEffects().onlyAccessesArgPointees(); 908 } 909 void Function::setOnlyAccessesArgMemory() { 910 setMemoryEffects(getMemoryEffects() & MemoryEffects::argMemOnly()); 911 } 912 913 /// Determine if the function may only access memory that is 914 /// inaccessible from the IR. 915 bool Function::onlyAccessesInaccessibleMemory() const { 916 return getMemoryEffects().onlyAccessesInaccessibleMem(); 917 } 918 void Function::setOnlyAccessesInaccessibleMemory() { 919 setMemoryEffects(getMemoryEffects() & MemoryEffects::inaccessibleMemOnly()); 920 } 921 922 /// Determine if the function may only access memory that is 923 /// either inaccessible from the IR or pointed to by its arguments. 924 bool Function::onlyAccessesInaccessibleMemOrArgMem() const { 925 return getMemoryEffects().onlyAccessesInaccessibleOrArgMem(); 926 } 927 void Function::setOnlyAccessesInaccessibleMemOrArgMem() { 928 setMemoryEffects(getMemoryEffects() & 929 MemoryEffects::inaccessibleOrArgMemOnly()); 930 } 931 932 bool Function::isTargetIntrinsic() const { 933 return Intrinsic::isTargetIntrinsic(IntID); 934 } 935 936 void Function::updateAfterNameChange() { 937 LibFuncCache = UnknownLibFunc; 938 StringRef Name = getName(); 939 if (!Name.starts_with("llvm.")) { 940 HasLLVMReservedName = false; 941 IntID = Intrinsic::not_intrinsic; 942 return; 943 } 944 HasLLVMReservedName = true; 945 IntID = Intrinsic::lookupIntrinsicID(Name); 946 } 947 948 /// hasAddressTaken - returns true if there are any uses of this function 949 /// other than direct calls or invokes to it. Optionally ignores callback 950 /// uses, assume like pointer annotation calls, and references in llvm.used 951 /// and llvm.compiler.used variables. 952 bool Function::hasAddressTaken(const User **PutOffender, 953 bool IgnoreCallbackUses, 954 bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed, 955 bool IgnoreARCAttachedCall, 956 bool IgnoreCastedDirectCall) const { 957 for (const Use &U : uses()) { 958 const User *FU = U.getUser(); 959 if (isa<BlockAddress>(FU)) 960 continue; 961 962 if (IgnoreCallbackUses) { 963 AbstractCallSite ACS(&U); 964 if (ACS && ACS.isCallbackCall()) 965 continue; 966 } 967 968 const auto *Call = dyn_cast<CallBase>(FU); 969 if (!Call) { 970 if (IgnoreAssumeLikeCalls && 971 isa<BitCastOperator, AddrSpaceCastOperator>(FU) && 972 all_of(FU->users(), [](const User *U) { 973 if (const auto *I = dyn_cast<IntrinsicInst>(U)) 974 return I->isAssumeLikeIntrinsic(); 975 return false; 976 })) { 977 continue; 978 } 979 980 if (IgnoreLLVMUsed && !FU->user_empty()) { 981 const User *FUU = FU; 982 if (isa<BitCastOperator, AddrSpaceCastOperator>(FU) && 983 FU->hasOneUse() && !FU->user_begin()->user_empty()) 984 FUU = *FU->user_begin(); 985 if (llvm::all_of(FUU->users(), [](const User *U) { 986 if (const auto *GV = dyn_cast<GlobalVariable>(U)) 987 return GV->hasName() && 988 (GV->getName() == "llvm.compiler.used" || 989 GV->getName() == "llvm.used"); 990 return false; 991 })) 992 continue; 993 } 994 if (PutOffender) 995 *PutOffender = FU; 996 return true; 997 } 998 999 if (IgnoreAssumeLikeCalls) { 1000 if (const auto *I = dyn_cast<IntrinsicInst>(Call)) 1001 if (I->isAssumeLikeIntrinsic()) 1002 continue; 1003 } 1004 1005 if (!Call->isCallee(&U) || (!IgnoreCastedDirectCall && 1006 Call->getFunctionType() != getFunctionType())) { 1007 if (IgnoreARCAttachedCall && 1008 Call->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall, 1009 U.getOperandNo())) 1010 continue; 1011 1012 if (PutOffender) 1013 *PutOffender = FU; 1014 return true; 1015 } 1016 } 1017 return false; 1018 } 1019 1020 bool Function::isDefTriviallyDead() const { 1021 // Check the linkage 1022 if (!hasLinkOnceLinkage() && !hasLocalLinkage() && 1023 !hasAvailableExternallyLinkage()) 1024 return false; 1025 1026 // Check if the function is used by anything other than a blockaddress. 1027 for (const User *U : users()) 1028 if (!isa<BlockAddress>(U)) 1029 return false; 1030 1031 return true; 1032 } 1033 1034 /// callsFunctionThatReturnsTwice - Return true if the function has a call to 1035 /// setjmp or other function that gcc recognizes as "returning twice". 1036 bool Function::callsFunctionThatReturnsTwice() const { 1037 for (const Instruction &I : instructions(this)) 1038 if (const auto *Call = dyn_cast<CallBase>(&I)) 1039 if (Call->hasFnAttr(Attribute::ReturnsTwice)) 1040 return true; 1041 1042 return false; 1043 } 1044 1045 Constant *Function::getPersonalityFn() const { 1046 assert(hasPersonalityFn() && getNumOperands()); 1047 return cast<Constant>(Op<0>()); 1048 } 1049 1050 void Function::setPersonalityFn(Constant *Fn) { 1051 setHungoffOperand<0>(Fn); 1052 setValueSubclassDataBit(3, Fn != nullptr); 1053 } 1054 1055 Constant *Function::getPrefixData() const { 1056 assert(hasPrefixData() && getNumOperands()); 1057 return cast<Constant>(Op<1>()); 1058 } 1059 1060 void Function::setPrefixData(Constant *PrefixData) { 1061 setHungoffOperand<1>(PrefixData); 1062 setValueSubclassDataBit(1, PrefixData != nullptr); 1063 } 1064 1065 Constant *Function::getPrologueData() const { 1066 assert(hasPrologueData() && getNumOperands()); 1067 return cast<Constant>(Op<2>()); 1068 } 1069 1070 void Function::setPrologueData(Constant *PrologueData) { 1071 setHungoffOperand<2>(PrologueData); 1072 setValueSubclassDataBit(2, PrologueData != nullptr); 1073 } 1074 1075 void Function::allocHungoffUselist() { 1076 // If we've already allocated a uselist, stop here. 1077 if (getNumOperands()) 1078 return; 1079 1080 allocHungoffUses(3, /*IsPhi=*/ false); 1081 setNumHungOffUseOperands(3); 1082 1083 // Initialize the uselist with placeholder operands to allow traversal. 1084 auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0)); 1085 Op<0>().set(CPN); 1086 Op<1>().set(CPN); 1087 Op<2>().set(CPN); 1088 } 1089 1090 template <int Idx> 1091 void Function::setHungoffOperand(Constant *C) { 1092 if (C) { 1093 allocHungoffUselist(); 1094 Op<Idx>().set(C); 1095 } else if (getNumOperands()) { 1096 Op<Idx>().set(ConstantPointerNull::get(PointerType::get(getContext(), 0))); 1097 } 1098 } 1099 1100 void Function::setValueSubclassDataBit(unsigned Bit, bool On) { 1101 assert(Bit < 16 && "SubclassData contains only 16 bits"); 1102 if (On) 1103 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit)); 1104 else 1105 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit)); 1106 } 1107 1108 void Function::setEntryCount(ProfileCount Count, 1109 const DenseSet<GlobalValue::GUID> *S) { 1110 #if !defined(NDEBUG) 1111 auto PrevCount = getEntryCount(); 1112 assert(!PrevCount || PrevCount->getType() == Count.getType()); 1113 #endif 1114 1115 auto ImportGUIDs = getImportGUIDs(); 1116 if (S == nullptr && ImportGUIDs.size()) 1117 S = &ImportGUIDs; 1118 1119 MDBuilder MDB(getContext()); 1120 setMetadata( 1121 LLVMContext::MD_prof, 1122 MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S)); 1123 } 1124 1125 void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type, 1126 const DenseSet<GlobalValue::GUID> *Imports) { 1127 setEntryCount(ProfileCount(Count, Type), Imports); 1128 } 1129 1130 std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const { 1131 MDNode *MD = getMetadata(LLVMContext::MD_prof); 1132 if (MD && MD->getOperand(0)) 1133 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) { 1134 if (MDS->getString() == "function_entry_count") { 1135 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 1136 uint64_t Count = CI->getValue().getZExtValue(); 1137 // A value of -1 is used for SamplePGO when there were no samples. 1138 // Treat this the same as unknown. 1139 if (Count == (uint64_t)-1) 1140 return std::nullopt; 1141 return ProfileCount(Count, PCT_Real); 1142 } else if (AllowSynthetic && 1143 MDS->getString() == "synthetic_function_entry_count") { 1144 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 1145 uint64_t Count = CI->getValue().getZExtValue(); 1146 return ProfileCount(Count, PCT_Synthetic); 1147 } 1148 } 1149 return std::nullopt; 1150 } 1151 1152 DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const { 1153 DenseSet<GlobalValue::GUID> R; 1154 if (MDNode *MD = getMetadata(LLVMContext::MD_prof)) 1155 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) 1156 if (MDS->getString() == "function_entry_count") 1157 for (unsigned i = 2; i < MD->getNumOperands(); i++) 1158 R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i)) 1159 ->getValue() 1160 .getZExtValue()); 1161 return R; 1162 } 1163 1164 void Function::setSectionPrefix(StringRef Prefix) { 1165 MDBuilder MDB(getContext()); 1166 setMetadata(LLVMContext::MD_section_prefix, 1167 MDB.createFunctionSectionPrefix(Prefix)); 1168 } 1169 1170 std::optional<StringRef> Function::getSectionPrefix() const { 1171 if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) { 1172 assert(cast<MDString>(MD->getOperand(0))->getString() == 1173 "function_section_prefix" && 1174 "Metadata not match"); 1175 return cast<MDString>(MD->getOperand(1))->getString(); 1176 } 1177 return std::nullopt; 1178 } 1179 1180 bool Function::nullPointerIsDefined() const { 1181 return hasFnAttribute(Attribute::NullPointerIsValid); 1182 } 1183 1184 bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) { 1185 if (F && F->nullPointerIsDefined()) 1186 return true; 1187 1188 if (AS != 0) 1189 return true; 1190 1191 return false; 1192 } 1193