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