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