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