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