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 AttributeSet Argument::getAttributes() const { 363 return getParent()->getAttributes().getParamAttrs(getArgNo()); 364 } 365 366 //===----------------------------------------------------------------------===// 367 // Helper Methods in Function 368 //===----------------------------------------------------------------------===// 369 370 LLVMContext &Function::getContext() const { 371 return getType()->getContext(); 372 } 373 374 const DataLayout &Function::getDataLayout() const { 375 return getParent()->getDataLayout(); 376 } 377 378 unsigned Function::getInstructionCount() const { 379 unsigned NumInstrs = 0; 380 for (const BasicBlock &BB : BasicBlocks) 381 NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(), 382 BB.instructionsWithoutDebug().end()); 383 return NumInstrs; 384 } 385 386 Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage, 387 const Twine &N, Module &M) { 388 return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M); 389 } 390 391 Function *Function::createWithDefaultAttr(FunctionType *Ty, 392 LinkageTypes Linkage, 393 unsigned AddrSpace, const Twine &N, 394 Module *M) { 395 auto *F = new (AllocMarker) Function(Ty, Linkage, AddrSpace, N, M); 396 AttrBuilder B(F->getContext()); 397 UWTableKind UWTable = M->getUwtable(); 398 if (UWTable != UWTableKind::None) 399 B.addUWTableAttr(UWTable); 400 switch (M->getFramePointer()) { 401 case FramePointerKind::None: 402 // 0 ("none") is the default. 403 break; 404 case FramePointerKind::Reserved: 405 B.addAttribute("frame-pointer", "reserved"); 406 break; 407 case FramePointerKind::NonLeaf: 408 B.addAttribute("frame-pointer", "non-leaf"); 409 break; 410 case FramePointerKind::All: 411 B.addAttribute("frame-pointer", "all"); 412 break; 413 } 414 if (M->getModuleFlag("function_return_thunk_extern")) 415 B.addAttribute(Attribute::FnRetThunkExtern); 416 StringRef DefaultCPU = F->getContext().getDefaultTargetCPU(); 417 if (!DefaultCPU.empty()) 418 B.addAttribute("target-cpu", DefaultCPU); 419 StringRef DefaultFeatures = F->getContext().getDefaultTargetFeatures(); 420 if (!DefaultFeatures.empty()) 421 B.addAttribute("target-features", DefaultFeatures); 422 423 // Check if the module attribute is present and not zero. 424 auto isModuleAttributeSet = [&](const StringRef &ModAttr) -> bool { 425 const auto *Attr = 426 mdconst::extract_or_null<ConstantInt>(M->getModuleFlag(ModAttr)); 427 return Attr && !Attr->isZero(); 428 }; 429 430 auto AddAttributeIfSet = [&](const StringRef &ModAttr) { 431 if (isModuleAttributeSet(ModAttr)) 432 B.addAttribute(ModAttr); 433 }; 434 435 StringRef SignType = "none"; 436 if (isModuleAttributeSet("sign-return-address")) 437 SignType = "non-leaf"; 438 if (isModuleAttributeSet("sign-return-address-all")) 439 SignType = "all"; 440 if (SignType != "none") { 441 B.addAttribute("sign-return-address", SignType); 442 B.addAttribute("sign-return-address-key", 443 isModuleAttributeSet("sign-return-address-with-bkey") 444 ? "b_key" 445 : "a_key"); 446 } 447 AddAttributeIfSet("branch-target-enforcement"); 448 AddAttributeIfSet("branch-protection-pauth-lr"); 449 AddAttributeIfSet("guarded-control-stack"); 450 451 F->addFnAttrs(B); 452 return F; 453 } 454 455 void Function::removeFromParent() { 456 getParent()->getFunctionList().remove(getIterator()); 457 } 458 459 void Function::eraseFromParent() { 460 getParent()->getFunctionList().erase(getIterator()); 461 } 462 463 void Function::splice(Function::iterator ToIt, Function *FromF, 464 Function::iterator FromBeginIt, 465 Function::iterator FromEndIt) { 466 #ifdef EXPENSIVE_CHECKS 467 // Check that FromBeginIt is before FromEndIt. 468 auto FromFEnd = FromF->end(); 469 for (auto It = FromBeginIt; It != FromEndIt; ++It) 470 assert(It != FromFEnd && "FromBeginIt not before FromEndIt!"); 471 #endif // EXPENSIVE_CHECKS 472 BasicBlocks.splice(ToIt, FromF->BasicBlocks, FromBeginIt, FromEndIt); 473 } 474 475 Function::iterator Function::erase(Function::iterator FromIt, 476 Function::iterator ToIt) { 477 return BasicBlocks.erase(FromIt, ToIt); 478 } 479 480 //===----------------------------------------------------------------------===// 481 // Function Implementation 482 //===----------------------------------------------------------------------===// 483 484 static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) { 485 // If AS == -1 and we are passed a valid module pointer we place the function 486 // in the program address space. Otherwise we default to AS0. 487 if (AddrSpace == static_cast<unsigned>(-1)) 488 return M ? M->getDataLayout().getProgramAddressSpace() : 0; 489 return AddrSpace; 490 } 491 492 Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, 493 const Twine &name, Module *ParentModule) 494 : GlobalObject(Ty, Value::FunctionVal, AllocMarker, Linkage, name, 495 computeAddrSpace(AddrSpace, ParentModule)), 496 NumArgs(Ty->getNumParams()), IsNewDbgInfoFormat(UseNewDbgInfoFormat) { 497 assert(FunctionType::isValidReturnType(getReturnType()) && 498 "invalid return type"); 499 setGlobalObjectSubClassData(0); 500 501 // We only need a symbol table for a function if the context keeps value names 502 if (!getContext().shouldDiscardValueNames()) 503 SymTab = std::make_unique<ValueSymbolTable>(NonGlobalValueMaxNameSize); 504 505 // If the function has arguments, mark them as lazily built. 506 if (Ty->getNumParams()) 507 setValueSubclassData(1); // Set the "has lazy arguments" bit. 508 509 if (ParentModule) { 510 ParentModule->getFunctionList().push_back(this); 511 IsNewDbgInfoFormat = ParentModule->IsNewDbgInfoFormat; 512 } 513 514 HasLLVMReservedName = getName().starts_with("llvm."); 515 // Ensure intrinsics have the right parameter attributes. 516 // Note, the IntID field will have been set in Value::setName if this function 517 // name is a valid intrinsic ID. 518 if (IntID) 519 setAttributes(Intrinsic::getAttributes(getContext(), IntID)); 520 } 521 522 Function::~Function() { 523 validateBlockNumbers(); 524 525 dropAllReferences(); // After this it is safe to delete instructions. 526 527 // Delete all of the method arguments and unlink from symbol table... 528 if (Arguments) 529 clearArguments(); 530 531 // Remove the function from the on-the-side GC table. 532 clearGC(); 533 } 534 535 void Function::BuildLazyArguments() const { 536 // Create the arguments vector, all arguments start out unnamed. 537 auto *FT = getFunctionType(); 538 if (NumArgs > 0) { 539 Arguments = std::allocator<Argument>().allocate(NumArgs); 540 for (unsigned i = 0, e = NumArgs; i != e; ++i) { 541 Type *ArgTy = FT->getParamType(i); 542 assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!"); 543 new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i); 544 } 545 } 546 547 // Clear the lazy arguments bit. 548 unsigned SDC = getSubclassDataFromValue(); 549 SDC &= ~(1 << 0); 550 const_cast<Function*>(this)->setValueSubclassData(SDC); 551 assert(!hasLazyArguments()); 552 } 553 554 static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) { 555 return MutableArrayRef<Argument>(Args, Count); 556 } 557 558 bool Function::isConstrainedFPIntrinsic() const { 559 return Intrinsic::isConstrainedFPIntrinsic(getIntrinsicID()); 560 } 561 562 void Function::clearArguments() { 563 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 564 A.setName(""); 565 A.~Argument(); 566 } 567 std::allocator<Argument>().deallocate(Arguments, NumArgs); 568 Arguments = nullptr; 569 } 570 571 void Function::stealArgumentListFrom(Function &Src) { 572 assert(isDeclaration() && "Expected no references to current arguments"); 573 574 // Drop the current arguments, if any, and set the lazy argument bit. 575 if (!hasLazyArguments()) { 576 assert(llvm::all_of(makeArgArray(Arguments, NumArgs), 577 [](const Argument &A) { return A.use_empty(); }) && 578 "Expected arguments to be unused in declaration"); 579 clearArguments(); 580 setValueSubclassData(getSubclassDataFromValue() | (1 << 0)); 581 } 582 583 // Nothing to steal if Src has lazy arguments. 584 if (Src.hasLazyArguments()) 585 return; 586 587 // Steal arguments from Src, and fix the lazy argument bits. 588 assert(arg_size() == Src.arg_size()); 589 Arguments = Src.Arguments; 590 Src.Arguments = nullptr; 591 for (Argument &A : makeArgArray(Arguments, NumArgs)) { 592 // FIXME: This does the work of transferNodesFromList inefficiently. 593 SmallString<128> Name; 594 if (A.hasName()) 595 Name = A.getName(); 596 if (!Name.empty()) 597 A.setName(""); 598 A.setParent(this); 599 if (!Name.empty()) 600 A.setName(Name); 601 } 602 603 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0)); 604 assert(!hasLazyArguments()); 605 Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0)); 606 } 607 608 void Function::deleteBodyImpl(bool ShouldDrop) { 609 setIsMaterializable(false); 610 611 for (BasicBlock &BB : *this) 612 BB.dropAllReferences(); 613 614 // Delete all basic blocks. They are now unused, except possibly by 615 // blockaddresses, but BasicBlock's destructor takes care of those. 616 while (!BasicBlocks.empty()) 617 BasicBlocks.begin()->eraseFromParent(); 618 619 if (getNumOperands()) { 620 if (ShouldDrop) { 621 // Drop uses of any optional data (real or placeholder). 622 User::dropAllReferences(); 623 setNumHungOffUseOperands(0); 624 } else { 625 // The code needs to match Function::allocHungoffUselist(). 626 auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0)); 627 Op<0>().set(CPN); 628 Op<1>().set(CPN); 629 Op<2>().set(CPN); 630 } 631 setValueSubclassData(getSubclassDataFromValue() & ~0xe); 632 } 633 634 // Metadata is stored in a side-table. 635 clearMetadata(); 636 } 637 638 void Function::addAttributeAtIndex(unsigned i, Attribute Attr) { 639 AttributeSets = AttributeSets.addAttributeAtIndex(getContext(), i, Attr); 640 } 641 642 void Function::addFnAttr(Attribute::AttrKind Kind) { 643 AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind); 644 } 645 646 void Function::addFnAttr(StringRef Kind, StringRef Val) { 647 AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind, Val); 648 } 649 650 void Function::addFnAttr(Attribute Attr) { 651 AttributeSets = AttributeSets.addFnAttribute(getContext(), Attr); 652 } 653 654 void Function::addFnAttrs(const AttrBuilder &Attrs) { 655 AttributeSets = AttributeSets.addFnAttributes(getContext(), Attrs); 656 } 657 658 void Function::addRetAttr(Attribute::AttrKind Kind) { 659 AttributeSets = AttributeSets.addRetAttribute(getContext(), Kind); 660 } 661 662 void Function::addRetAttr(Attribute Attr) { 663 AttributeSets = AttributeSets.addRetAttribute(getContext(), Attr); 664 } 665 666 void Function::addRetAttrs(const AttrBuilder &Attrs) { 667 AttributeSets = AttributeSets.addRetAttributes(getContext(), Attrs); 668 } 669 670 void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 671 AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Kind); 672 } 673 674 void Function::addParamAttr(unsigned ArgNo, Attribute Attr) { 675 AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Attr); 676 } 677 678 void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { 679 AttributeSets = AttributeSets.addParamAttributes(getContext(), ArgNo, Attrs); 680 } 681 682 void Function::removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { 683 AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind); 684 } 685 686 void Function::removeAttributeAtIndex(unsigned i, StringRef Kind) { 687 AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind); 688 } 689 690 void Function::removeFnAttr(Attribute::AttrKind Kind) { 691 AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); 692 } 693 694 void Function::removeFnAttr(StringRef Kind) { 695 AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind); 696 } 697 698 void Function::removeFnAttrs(const AttributeMask &AM) { 699 AttributeSets = AttributeSets.removeFnAttributes(getContext(), AM); 700 } 701 702 void Function::removeRetAttr(Attribute::AttrKind Kind) { 703 AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); 704 } 705 706 void Function::removeRetAttr(StringRef Kind) { 707 AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind); 708 } 709 710 void Function::removeRetAttrs(const AttributeMask &Attrs) { 711 AttributeSets = AttributeSets.removeRetAttributes(getContext(), Attrs); 712 } 713 714 void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { 715 AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); 716 } 717 718 void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) { 719 AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind); 720 } 721 722 void Function::removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs) { 723 AttributeSets = 724 AttributeSets.removeParamAttributes(getContext(), ArgNo, Attrs); 725 } 726 727 void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) { 728 AttributeSets = 729 AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes); 730 } 731 732 bool Function::hasFnAttribute(Attribute::AttrKind Kind) const { 733 return AttributeSets.hasFnAttr(Kind); 734 } 735 736 bool Function::hasFnAttribute(StringRef Kind) const { 737 return AttributeSets.hasFnAttr(Kind); 738 } 739 740 bool Function::hasRetAttribute(Attribute::AttrKind Kind) const { 741 return AttributeSets.hasRetAttr(Kind); 742 } 743 744 bool Function::hasParamAttribute(unsigned ArgNo, 745 Attribute::AttrKind Kind) const { 746 return AttributeSets.hasParamAttr(ArgNo, Kind); 747 } 748 749 bool Function::hasParamAttribute(unsigned ArgNo, StringRef Kind) const { 750 return AttributeSets.hasParamAttr(ArgNo, Kind); 751 } 752 753 Attribute Function::getAttributeAtIndex(unsigned i, 754 Attribute::AttrKind Kind) const { 755 return AttributeSets.getAttributeAtIndex(i, Kind); 756 } 757 758 Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const { 759 return AttributeSets.getAttributeAtIndex(i, Kind); 760 } 761 762 bool Function::hasAttributeAtIndex(unsigned Idx, 763 Attribute::AttrKind Kind) const { 764 return AttributeSets.hasAttributeAtIndex(Idx, Kind); 765 } 766 767 Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const { 768 return AttributeSets.getFnAttr(Kind); 769 } 770 771 Attribute Function::getFnAttribute(StringRef Kind) const { 772 return AttributeSets.getFnAttr(Kind); 773 } 774 775 Attribute Function::getRetAttribute(Attribute::AttrKind Kind) const { 776 return AttributeSets.getRetAttr(Kind); 777 } 778 779 uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name, 780 uint64_t Default) const { 781 Attribute A = getFnAttribute(Name); 782 uint64_t Result = Default; 783 if (A.isStringAttribute()) { 784 StringRef Str = A.getValueAsString(); 785 if (Str.getAsInteger(0, Result)) 786 getContext().emitError("cannot parse integer attribute " + Name); 787 } 788 789 return Result; 790 } 791 792 /// gets the specified attribute from the list of attributes. 793 Attribute Function::getParamAttribute(unsigned ArgNo, 794 Attribute::AttrKind Kind) const { 795 return AttributeSets.getParamAttr(ArgNo, Kind); 796 } 797 798 void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo, 799 uint64_t Bytes) { 800 AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(getContext(), 801 ArgNo, Bytes); 802 } 803 804 void Function::addRangeRetAttr(const ConstantRange &CR) { 805 AttributeSets = AttributeSets.addRangeRetAttr(getContext(), CR); 806 } 807 808 DenormalMode Function::getDenormalMode(const fltSemantics &FPType) const { 809 if (&FPType == &APFloat::IEEEsingle()) { 810 DenormalMode Mode = getDenormalModeF32Raw(); 811 // If the f32 variant of the attribute isn't specified, try to use the 812 // generic one. 813 if (Mode.isValid()) 814 return Mode; 815 } 816 817 return getDenormalModeRaw(); 818 } 819 820 DenormalMode Function::getDenormalModeRaw() const { 821 Attribute Attr = getFnAttribute("denormal-fp-math"); 822 StringRef Val = Attr.getValueAsString(); 823 return parseDenormalFPAttribute(Val); 824 } 825 826 DenormalMode Function::getDenormalModeF32Raw() const { 827 Attribute Attr = getFnAttribute("denormal-fp-math-f32"); 828 if (Attr.isValid()) { 829 StringRef Val = Attr.getValueAsString(); 830 return parseDenormalFPAttribute(Val); 831 } 832 833 return DenormalMode::getInvalid(); 834 } 835 836 const std::string &Function::getGC() const { 837 assert(hasGC() && "Function has no collector"); 838 return getContext().getGC(*this); 839 } 840 841 void Function::setGC(std::string Str) { 842 setValueSubclassDataBit(14, !Str.empty()); 843 getContext().setGC(*this, std::move(Str)); 844 } 845 846 void Function::clearGC() { 847 if (!hasGC()) 848 return; 849 getContext().deleteGC(*this); 850 setValueSubclassDataBit(14, false); 851 } 852 853 bool Function::hasStackProtectorFnAttr() const { 854 return hasFnAttribute(Attribute::StackProtect) || 855 hasFnAttribute(Attribute::StackProtectStrong) || 856 hasFnAttribute(Attribute::StackProtectReq); 857 } 858 859 /// Copy all additional attributes (those not needed to create a Function) from 860 /// the Function Src to this one. 861 void Function::copyAttributesFrom(const Function *Src) { 862 GlobalObject::copyAttributesFrom(Src); 863 setCallingConv(Src->getCallingConv()); 864 setAttributes(Src->getAttributes()); 865 if (Src->hasGC()) 866 setGC(Src->getGC()); 867 else 868 clearGC(); 869 if (Src->hasPersonalityFn()) 870 setPersonalityFn(Src->getPersonalityFn()); 871 if (Src->hasPrefixData()) 872 setPrefixData(Src->getPrefixData()); 873 if (Src->hasPrologueData()) 874 setPrologueData(Src->getPrologueData()); 875 } 876 877 MemoryEffects Function::getMemoryEffects() const { 878 return getAttributes().getMemoryEffects(); 879 } 880 void Function::setMemoryEffects(MemoryEffects ME) { 881 addFnAttr(Attribute::getWithMemoryEffects(getContext(), ME)); 882 } 883 884 /// Determine if the function does not access memory. 885 bool Function::doesNotAccessMemory() const { 886 return getMemoryEffects().doesNotAccessMemory(); 887 } 888 void Function::setDoesNotAccessMemory() { 889 setMemoryEffects(MemoryEffects::none()); 890 } 891 892 /// Determine if the function does not access or only reads memory. 893 bool Function::onlyReadsMemory() const { 894 return getMemoryEffects().onlyReadsMemory(); 895 } 896 void Function::setOnlyReadsMemory() { 897 setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly()); 898 } 899 900 /// Determine if the function does not access or only writes memory. 901 bool Function::onlyWritesMemory() const { 902 return getMemoryEffects().onlyWritesMemory(); 903 } 904 void Function::setOnlyWritesMemory() { 905 setMemoryEffects(getMemoryEffects() & MemoryEffects::writeOnly()); 906 } 907 908 /// Determine if the call can access memmory only using pointers based 909 /// on its arguments. 910 bool Function::onlyAccessesArgMemory() const { 911 return getMemoryEffects().onlyAccessesArgPointees(); 912 } 913 void Function::setOnlyAccessesArgMemory() { 914 setMemoryEffects(getMemoryEffects() & MemoryEffects::argMemOnly()); 915 } 916 917 /// Determine if the function may only access memory that is 918 /// inaccessible from the IR. 919 bool Function::onlyAccessesInaccessibleMemory() const { 920 return getMemoryEffects().onlyAccessesInaccessibleMem(); 921 } 922 void Function::setOnlyAccessesInaccessibleMemory() { 923 setMemoryEffects(getMemoryEffects() & MemoryEffects::inaccessibleMemOnly()); 924 } 925 926 /// Determine if the function may only access memory that is 927 /// either inaccessible from the IR or pointed to by its arguments. 928 bool Function::onlyAccessesInaccessibleMemOrArgMem() const { 929 return getMemoryEffects().onlyAccessesInaccessibleOrArgMem(); 930 } 931 void Function::setOnlyAccessesInaccessibleMemOrArgMem() { 932 setMemoryEffects(getMemoryEffects() & 933 MemoryEffects::inaccessibleOrArgMemOnly()); 934 } 935 936 bool Function::isTargetIntrinsic() const { 937 return Intrinsic::isTargetIntrinsic(IntID); 938 } 939 940 void Function::updateAfterNameChange() { 941 LibFuncCache = UnknownLibFunc; 942 StringRef Name = getName(); 943 if (!Name.starts_with("llvm.")) { 944 HasLLVMReservedName = false; 945 IntID = Intrinsic::not_intrinsic; 946 return; 947 } 948 HasLLVMReservedName = true; 949 IntID = Intrinsic::lookupIntrinsicID(Name); 950 } 951 952 /// hasAddressTaken - returns true if there are any uses of this function 953 /// other than direct calls or invokes to it. Optionally ignores callback 954 /// uses, assume like pointer annotation calls, and references in llvm.used 955 /// and llvm.compiler.used variables. 956 bool Function::hasAddressTaken(const User **PutOffender, 957 bool IgnoreCallbackUses, 958 bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed, 959 bool IgnoreARCAttachedCall, 960 bool IgnoreCastedDirectCall) const { 961 for (const Use &U : uses()) { 962 const User *FU = U.getUser(); 963 if (isa<BlockAddress>(FU)) 964 continue; 965 966 if (IgnoreCallbackUses) { 967 AbstractCallSite ACS(&U); 968 if (ACS && ACS.isCallbackCall()) 969 continue; 970 } 971 972 const auto *Call = dyn_cast<CallBase>(FU); 973 if (!Call) { 974 if (IgnoreAssumeLikeCalls && 975 isa<BitCastOperator, AddrSpaceCastOperator>(FU) && 976 all_of(FU->users(), [](const User *U) { 977 if (const auto *I = dyn_cast<IntrinsicInst>(U)) 978 return I->isAssumeLikeIntrinsic(); 979 return false; 980 })) { 981 continue; 982 } 983 984 if (IgnoreLLVMUsed && !FU->user_empty()) { 985 const User *FUU = FU; 986 if (isa<BitCastOperator, AddrSpaceCastOperator>(FU) && 987 FU->hasOneUse() && !FU->user_begin()->user_empty()) 988 FUU = *FU->user_begin(); 989 if (llvm::all_of(FUU->users(), [](const User *U) { 990 if (const auto *GV = dyn_cast<GlobalVariable>(U)) 991 return GV->hasName() && 992 (GV->getName() == "llvm.compiler.used" || 993 GV->getName() == "llvm.used"); 994 return false; 995 })) 996 continue; 997 } 998 if (PutOffender) 999 *PutOffender = FU; 1000 return true; 1001 } 1002 1003 if (IgnoreAssumeLikeCalls) { 1004 if (const auto *I = dyn_cast<IntrinsicInst>(Call)) 1005 if (I->isAssumeLikeIntrinsic()) 1006 continue; 1007 } 1008 1009 if (!Call->isCallee(&U) || (!IgnoreCastedDirectCall && 1010 Call->getFunctionType() != getFunctionType())) { 1011 if (IgnoreARCAttachedCall && 1012 Call->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall, 1013 U.getOperandNo())) 1014 continue; 1015 1016 if (PutOffender) 1017 *PutOffender = FU; 1018 return true; 1019 } 1020 } 1021 return false; 1022 } 1023 1024 bool Function::isDefTriviallyDead() const { 1025 // Check the linkage 1026 if (!hasLinkOnceLinkage() && !hasLocalLinkage() && 1027 !hasAvailableExternallyLinkage()) 1028 return false; 1029 1030 // Check if the function is used by anything other than a blockaddress. 1031 for (const User *U : users()) 1032 if (!isa<BlockAddress>(U)) 1033 return false; 1034 1035 return true; 1036 } 1037 1038 /// callsFunctionThatReturnsTwice - Return true if the function has a call to 1039 /// setjmp or other function that gcc recognizes as "returning twice". 1040 bool Function::callsFunctionThatReturnsTwice() const { 1041 for (const Instruction &I : instructions(this)) 1042 if (const auto *Call = dyn_cast<CallBase>(&I)) 1043 if (Call->hasFnAttr(Attribute::ReturnsTwice)) 1044 return true; 1045 1046 return false; 1047 } 1048 1049 Constant *Function::getPersonalityFn() const { 1050 assert(hasPersonalityFn() && getNumOperands()); 1051 return cast<Constant>(Op<0>()); 1052 } 1053 1054 void Function::setPersonalityFn(Constant *Fn) { 1055 setHungoffOperand<0>(Fn); 1056 setValueSubclassDataBit(3, Fn != nullptr); 1057 } 1058 1059 Constant *Function::getPrefixData() const { 1060 assert(hasPrefixData() && getNumOperands()); 1061 return cast<Constant>(Op<1>()); 1062 } 1063 1064 void Function::setPrefixData(Constant *PrefixData) { 1065 setHungoffOperand<1>(PrefixData); 1066 setValueSubclassDataBit(1, PrefixData != nullptr); 1067 } 1068 1069 Constant *Function::getPrologueData() const { 1070 assert(hasPrologueData() && getNumOperands()); 1071 return cast<Constant>(Op<2>()); 1072 } 1073 1074 void Function::setPrologueData(Constant *PrologueData) { 1075 setHungoffOperand<2>(PrologueData); 1076 setValueSubclassDataBit(2, PrologueData != nullptr); 1077 } 1078 1079 void Function::allocHungoffUselist() { 1080 // If we've already allocated a uselist, stop here. 1081 if (getNumOperands()) 1082 return; 1083 1084 allocHungoffUses(3, /*IsPhi=*/ false); 1085 setNumHungOffUseOperands(3); 1086 1087 // Initialize the uselist with placeholder operands to allow traversal. 1088 auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0)); 1089 Op<0>().set(CPN); 1090 Op<1>().set(CPN); 1091 Op<2>().set(CPN); 1092 } 1093 1094 template <int Idx> 1095 void Function::setHungoffOperand(Constant *C) { 1096 if (C) { 1097 allocHungoffUselist(); 1098 Op<Idx>().set(C); 1099 } else if (getNumOperands()) { 1100 Op<Idx>().set(ConstantPointerNull::get(PointerType::get(getContext(), 0))); 1101 } 1102 } 1103 1104 void Function::setValueSubclassDataBit(unsigned Bit, bool On) { 1105 assert(Bit < 16 && "SubclassData contains only 16 bits"); 1106 if (On) 1107 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit)); 1108 else 1109 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit)); 1110 } 1111 1112 void Function::setEntryCount(ProfileCount Count, 1113 const DenseSet<GlobalValue::GUID> *S) { 1114 #if !defined(NDEBUG) 1115 auto PrevCount = getEntryCount(); 1116 assert(!PrevCount || PrevCount->getType() == Count.getType()); 1117 #endif 1118 1119 auto ImportGUIDs = getImportGUIDs(); 1120 if (S == nullptr && ImportGUIDs.size()) 1121 S = &ImportGUIDs; 1122 1123 MDBuilder MDB(getContext()); 1124 setMetadata( 1125 LLVMContext::MD_prof, 1126 MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S)); 1127 } 1128 1129 void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type, 1130 const DenseSet<GlobalValue::GUID> *Imports) { 1131 setEntryCount(ProfileCount(Count, Type), Imports); 1132 } 1133 1134 std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const { 1135 MDNode *MD = getMetadata(LLVMContext::MD_prof); 1136 if (MD && MD->getOperand(0)) 1137 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) { 1138 if (MDS->getString() == "function_entry_count") { 1139 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 1140 uint64_t Count = CI->getValue().getZExtValue(); 1141 // A value of -1 is used for SamplePGO when there were no samples. 1142 // Treat this the same as unknown. 1143 if (Count == (uint64_t)-1) 1144 return std::nullopt; 1145 return ProfileCount(Count, PCT_Real); 1146 } else if (AllowSynthetic && 1147 MDS->getString() == "synthetic_function_entry_count") { 1148 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); 1149 uint64_t Count = CI->getValue().getZExtValue(); 1150 return ProfileCount(Count, PCT_Synthetic); 1151 } 1152 } 1153 return std::nullopt; 1154 } 1155 1156 DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const { 1157 DenseSet<GlobalValue::GUID> R; 1158 if (MDNode *MD = getMetadata(LLVMContext::MD_prof)) 1159 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) 1160 if (MDS->getString() == "function_entry_count") 1161 for (unsigned i = 2; i < MD->getNumOperands(); i++) 1162 R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i)) 1163 ->getValue() 1164 .getZExtValue()); 1165 return R; 1166 } 1167 1168 void Function::setSectionPrefix(StringRef Prefix) { 1169 MDBuilder MDB(getContext()); 1170 setMetadata(LLVMContext::MD_section_prefix, 1171 MDB.createFunctionSectionPrefix(Prefix)); 1172 } 1173 1174 std::optional<StringRef> Function::getSectionPrefix() const { 1175 if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) { 1176 assert(cast<MDString>(MD->getOperand(0))->getString() == 1177 "function_section_prefix" && 1178 "Metadata not match"); 1179 return cast<MDString>(MD->getOperand(1))->getString(); 1180 } 1181 return std::nullopt; 1182 } 1183 1184 bool Function::nullPointerIsDefined() const { 1185 return hasFnAttribute(Attribute::NullPointerIsValid); 1186 } 1187 1188 bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) { 1189 if (F && F->nullPointerIsDefined()) 1190 return true; 1191 1192 if (AS != 0) 1193 return true; 1194 1195 return false; 1196 } 1197