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(Val: 1024), |
67 | cl::desc("Maximum size for the name of non-global values." )); |
68 | |
69 | void Function::renumberBlocks() { |
70 | validateBlockNumbers(); |
71 | |
72 | NextBlockNum = 0; |
73 | for (auto &BB : *this) |
74 | BB.Number = NextBlockNum++; |
75 | BlockNumEpoch++; |
76 | } |
77 | |
78 | 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 | |
90 | void Function::convertToNewDbgValues() { |
91 | for (auto &BB : *this) { |
92 | BB.convertToNewDbgValues(); |
93 | } |
94 | } |
95 | |
96 | void Function::convertFromNewDbgValues() { |
97 | for (auto &BB : *this) { |
98 | BB.convertFromNewDbgValues(); |
99 | } |
100 | } |
101 | |
102 | //===----------------------------------------------------------------------===// |
103 | // Argument Implementation |
104 | //===----------------------------------------------------------------------===// |
105 | |
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 | |
111 | void Argument::setParent(Function *parent) { |
112 | Parent = parent; |
113 | } |
114 | |
115 | bool Argument::hasNonNullAttr(bool AllowUndefOrPoison) const { |
116 | if (!getType()->isPointerTy()) return false; |
117 | if (getParent()->hasParamAttribute(ArgNo: getArgNo(), Kind: Attribute::NonNull) && |
118 | (AllowUndefOrPoison || |
119 | getParent()->hasParamAttribute(ArgNo: getArgNo(), Kind: Attribute::NoUndef))) |
120 | return true; |
121 | else if (getDereferenceableBytes() > 0 && |
122 | !NullPointerIsDefined(F: getParent(), |
123 | AS: getType()->getPointerAddressSpace())) |
124 | return true; |
125 | return false; |
126 | } |
127 | |
128 | bool Argument::hasByValAttr() const { |
129 | if (!getType()->isPointerTy()) return false; |
130 | return hasAttribute(Kind: Attribute::ByVal); |
131 | } |
132 | |
133 | bool Argument::hasDeadOnReturnAttr() const { |
134 | if (!getType()->isPointerTy()) |
135 | return false; |
136 | return hasAttribute(Kind: Attribute::DeadOnReturn); |
137 | } |
138 | |
139 | bool Argument::hasByRefAttr() const { |
140 | if (!getType()->isPointerTy()) |
141 | return false; |
142 | return hasAttribute(Kind: Attribute::ByRef); |
143 | } |
144 | |
145 | bool Argument::hasSwiftSelfAttr() const { |
146 | return getParent()->hasParamAttribute(ArgNo: getArgNo(), Kind: Attribute::SwiftSelf); |
147 | } |
148 | |
149 | bool Argument::hasSwiftErrorAttr() const { |
150 | return getParent()->hasParamAttribute(ArgNo: getArgNo(), Kind: Attribute::SwiftError); |
151 | } |
152 | |
153 | bool Argument::hasInAllocaAttr() const { |
154 | if (!getType()->isPointerTy()) return false; |
155 | return hasAttribute(Kind: Attribute::InAlloca); |
156 | } |
157 | |
158 | bool Argument::hasPreallocatedAttr() const { |
159 | if (!getType()->isPointerTy()) |
160 | return false; |
161 | return hasAttribute(Kind: Attribute::Preallocated); |
162 | } |
163 | |
164 | bool Argument::hasPassPointeeByValueCopyAttr() const { |
165 | if (!getType()->isPointerTy()) return false; |
166 | AttributeList Attrs = getParent()->getAttributes(); |
167 | return Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::ByVal) || |
168 | Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::InAlloca) || |
169 | Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::Preallocated); |
170 | } |
171 | |
172 | bool Argument::hasPointeeInMemoryValueAttr() const { |
173 | if (!getType()->isPointerTy()) |
174 | return false; |
175 | AttributeList Attrs = getParent()->getAttributes(); |
176 | return Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::ByVal) || |
177 | Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::StructRet) || |
178 | Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::InAlloca) || |
179 | Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::Preallocated) || |
180 | Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::ByRef); |
181 | } |
182 | |
183 | /// For a byval, sret, inalloca, or preallocated parameter, get the in-memory |
184 | /// parameter type. |
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 | |
202 | uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout &DL) const { |
203 | AttributeSet ParamAttrs = |
204 | getParent()->getAttributes().getParamAttrs(ArgNo: getArgNo()); |
205 | if (Type *MemTy = getMemoryParamAllocType(ParamAttrs)) |
206 | return DL.getTypeAllocSize(Ty: MemTy); |
207 | return 0; |
208 | } |
209 | |
210 | Type *Argument::getPointeeInMemoryValueType() const { |
211 | AttributeSet ParamAttrs = |
212 | getParent()->getAttributes().getParamAttrs(ArgNo: getArgNo()); |
213 | return getMemoryParamAllocType(ParamAttrs); |
214 | } |
215 | |
216 | MaybeAlign Argument::getParamAlign() const { |
217 | assert(getType()->isPointerTy() && "Only pointers have alignments" ); |
218 | return getParent()->getParamAlign(ArgNo: getArgNo()); |
219 | } |
220 | |
221 | MaybeAlign Argument::getParamStackAlign() const { |
222 | return getParent()->getParamStackAlign(ArgNo: getArgNo()); |
223 | } |
224 | |
225 | Type *Argument::getParamByValType() const { |
226 | assert(getType()->isPointerTy() && "Only pointers have byval types" ); |
227 | return getParent()->getParamByValType(ArgNo: getArgNo()); |
228 | } |
229 | |
230 | Type *Argument::getParamStructRetType() const { |
231 | assert(getType()->isPointerTy() && "Only pointers have sret types" ); |
232 | return getParent()->getParamStructRetType(ArgNo: getArgNo()); |
233 | } |
234 | |
235 | Type *Argument::getParamByRefType() const { |
236 | assert(getType()->isPointerTy() && "Only pointers have byref types" ); |
237 | return getParent()->getParamByRefType(ArgNo: getArgNo()); |
238 | } |
239 | |
240 | Type *Argument::getParamInAllocaType() const { |
241 | assert(getType()->isPointerTy() && "Only pointers have inalloca types" ); |
242 | return getParent()->getParamInAllocaType(ArgNo: getArgNo()); |
243 | } |
244 | |
245 | uint64_t Argument::getDereferenceableBytes() const { |
246 | assert(getType()->isPointerTy() && |
247 | "Only pointers have dereferenceable bytes" ); |
248 | return getParent()->getParamDereferenceableBytes(ArgNo: getArgNo()); |
249 | } |
250 | |
251 | uint64_t Argument::getDereferenceableOrNullBytes() const { |
252 | assert(getType()->isPointerTy() && |
253 | "Only pointers have dereferenceable bytes" ); |
254 | return getParent()->getParamDereferenceableOrNullBytes(ArgNo: getArgNo()); |
255 | } |
256 | |
257 | FPClassTest Argument::getNoFPClass() const { |
258 | return getParent()->getParamNoFPClass(ArgNo: getArgNo()); |
259 | } |
260 | |
261 | std::optional<ConstantRange> Argument::getRange() const { |
262 | const Attribute RangeAttr = getAttribute(Kind: llvm::Attribute::Range); |
263 | if (RangeAttr.isValid()) |
264 | return RangeAttr.getRange(); |
265 | return std::nullopt; |
266 | } |
267 | |
268 | bool Argument::hasNestAttr() const { |
269 | if (!getType()->isPointerTy()) return false; |
270 | return hasAttribute(Kind: Attribute::Nest); |
271 | } |
272 | |
273 | bool Argument::hasNoAliasAttr() const { |
274 | if (!getType()->isPointerTy()) return false; |
275 | return hasAttribute(Kind: Attribute::NoAlias); |
276 | } |
277 | |
278 | bool Argument::hasNoCaptureAttr() const { |
279 | if (!getType()->isPointerTy()) return false; |
280 | return capturesNothing(CC: getAttributes().getCaptureInfo()); |
281 | } |
282 | |
283 | bool Argument::hasNoFreeAttr() const { |
284 | if (!getType()->isPointerTy()) return false; |
285 | return hasAttribute(Kind: Attribute::NoFree); |
286 | } |
287 | |
288 | bool Argument::hasStructRetAttr() const { |
289 | if (!getType()->isPointerTy()) return false; |
290 | return hasAttribute(Kind: Attribute::StructRet); |
291 | } |
292 | |
293 | bool Argument::hasInRegAttr() const { |
294 | return hasAttribute(Kind: Attribute::InReg); |
295 | } |
296 | |
297 | bool Argument::hasReturnedAttr() const { |
298 | return hasAttribute(Kind: Attribute::Returned); |
299 | } |
300 | |
301 | bool Argument::hasZExtAttr() const { |
302 | return hasAttribute(Kind: Attribute::ZExt); |
303 | } |
304 | |
305 | bool Argument::hasSExtAttr() const { |
306 | return hasAttribute(Kind: Attribute::SExt); |
307 | } |
308 | |
309 | bool Argument::onlyReadsMemory() const { |
310 | AttributeList Attrs = getParent()->getAttributes(); |
311 | return Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::ReadOnly) || |
312 | Attrs.hasParamAttr(ArgNo: getArgNo(), Kind: Attribute::ReadNone); |
313 | } |
314 | |
315 | void Argument::addAttrs(AttrBuilder &B) { |
316 | AttributeList AL = getParent()->getAttributes(); |
317 | AL = AL.addParamAttributes(C&: Parent->getContext(), ArgNo: getArgNo(), B); |
318 | getParent()->setAttributes(AL); |
319 | } |
320 | |
321 | void Argument::addAttr(Attribute::AttrKind Kind) { |
322 | getParent()->addParamAttr(ArgNo: getArgNo(), Kind); |
323 | } |
324 | |
325 | void Argument::addAttr(Attribute Attr) { |
326 | getParent()->addParamAttr(ArgNo: getArgNo(), Attr); |
327 | } |
328 | |
329 | void Argument::removeAttr(Attribute::AttrKind Kind) { |
330 | getParent()->removeParamAttr(ArgNo: getArgNo(), Kind); |
331 | } |
332 | |
333 | void Argument::removeAttrs(const AttributeMask &AM) { |
334 | AttributeList AL = getParent()->getAttributes(); |
335 | AL = AL.removeParamAttributes(C&: Parent->getContext(), ArgNo: getArgNo(), AttrsToRemove: AM); |
336 | getParent()->setAttributes(AL); |
337 | } |
338 | |
339 | bool Argument::hasAttribute(Attribute::AttrKind Kind) const { |
340 | return getParent()->hasParamAttribute(ArgNo: getArgNo(), Kind); |
341 | } |
342 | |
343 | bool Argument::hasAttribute(StringRef Kind) const { |
344 | return getParent()->hasParamAttribute(ArgNo: getArgNo(), Kind); |
345 | } |
346 | |
347 | Attribute Argument::getAttribute(Attribute::AttrKind Kind) const { |
348 | return getParent()->getParamAttribute(ArgNo: getArgNo(), Kind); |
349 | } |
350 | |
351 | AttributeSet Argument::getAttributes() const { |
352 | return getParent()->getAttributes().getParamAttrs(ArgNo: getArgNo()); |
353 | } |
354 | |
355 | //===----------------------------------------------------------------------===// |
356 | // Helper Methods in Function |
357 | //===----------------------------------------------------------------------===// |
358 | |
359 | LLVMContext &Function::getContext() const { |
360 | return getType()->getContext(); |
361 | } |
362 | |
363 | const DataLayout &Function::getDataLayout() const { |
364 | return getParent()->getDataLayout(); |
365 | } |
366 | |
367 | unsigned Function::getInstructionCount() const { |
368 | unsigned NumInstrs = 0; |
369 | for (const BasicBlock &BB : BasicBlocks) |
370 | NumInstrs += std::distance(first: BB.instructionsWithoutDebug().begin(), |
371 | last: BB.instructionsWithoutDebug().end()); |
372 | return NumInstrs; |
373 | } |
374 | |
375 | Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage, |
376 | const Twine &N, Module &M) { |
377 | return Create(Ty, Linkage, AddrSpace: M.getDataLayout().getProgramAddressSpace(), N, M: &M); |
378 | } |
379 | |
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(Kind: UWTable); |
389 | switch (M->getFramePointer()) { |
390 | case FramePointerKind::None: |
391 | // 0 ("none") is the default. |
392 | break; |
393 | case FramePointerKind::Reserved: |
394 | B.addAttribute(A: "frame-pointer" , V: "reserved" ); |
395 | break; |
396 | case FramePointerKind::NonLeaf: |
397 | B.addAttribute(A: "frame-pointer" , V: "non-leaf" ); |
398 | break; |
399 | case FramePointerKind::All: |
400 | B.addAttribute(A: "frame-pointer" , V: "all" ); |
401 | break; |
402 | } |
403 | if (M->getModuleFlag(Key: "function_return_thunk_extern" )) |
404 | B.addAttribute(Val: Attribute::FnRetThunkExtern); |
405 | StringRef DefaultCPU = F->getContext().getDefaultTargetCPU(); |
406 | if (!DefaultCPU.empty()) |
407 | B.addAttribute(A: "target-cpu" , V: DefaultCPU); |
408 | StringRef DefaultFeatures = F->getContext().getDefaultTargetFeatures(); |
409 | if (!DefaultFeatures.empty()) |
410 | B.addAttribute(A: "target-features" , V: 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>(MD: M->getModuleFlag(Key: ModAttr)); |
416 | return Attr && !Attr->isZero(); |
417 | }; |
418 | |
419 | auto AddAttributeIfSet = [&](const StringRef &ModAttr) { |
420 | if (isModuleAttributeSet(ModAttr)) |
421 | B.addAttribute(A: 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(A: "sign-return-address" , V: SignType); |
431 | B.addAttribute(A: "sign-return-address-key" , |
432 | V: 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(Attrs: B); |
441 | return F; |
442 | } |
443 | |
444 | void Function::removeFromParent() { |
445 | getParent()->getFunctionList().remove(IT: getIterator()); |
446 | } |
447 | |
448 | void Function::eraseFromParent() { |
449 | getParent()->getFunctionList().erase(where: getIterator()); |
450 | } |
451 | |
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(where: ToIt, L2&: FromF->BasicBlocks, first: FromBeginIt, last: FromEndIt); |
462 | } |
463 | |
464 | Function::iterator Function::erase(Function::iterator FromIt, |
465 | Function::iterator ToIt) { |
466 | return BasicBlocks.erase(first: FromIt, last: ToIt); |
467 | } |
468 | |
469 | //===----------------------------------------------------------------------===// |
470 | // Function Implementation |
471 | //===----------------------------------------------------------------------===// |
472 | |
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 | |
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, M: 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>(args&: 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(val: this); |
500 | } |
501 | |
502 | HasLLVMReservedName = getName().starts_with(Prefix: "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, FT: Ty, ArgTys&: OverloadTys)) |
511 | return; |
512 | |
513 | setAttributes(Intrinsic::getAttributes(C&: getContext(), id: IntID, FT: Ty)); |
514 | } |
515 | } |
516 | |
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 | |
530 | 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(n: 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 | |
549 | static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) { |
550 | return MutableArrayRef<Argument>(Args, Count); |
551 | } |
552 | |
553 | bool Function::isConstrainedFPIntrinsic() const { |
554 | return Intrinsic::isConstrainedFPIntrinsic(QID: getIntrinsicID()); |
555 | } |
556 | |
557 | void Function::clearArguments() { |
558 | for (Argument &A : makeArgArray(Args: Arguments, Count: NumArgs)) { |
559 | A.setName("" ); |
560 | A.~Argument(); |
561 | } |
562 | std::allocator<Argument>().deallocate(p: Arguments, n: NumArgs); |
563 | Arguments = nullptr; |
564 | } |
565 | |
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(Args: Arguments, Count: 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 | |
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(T: PointerType::get(C&: getContext(), AddressSpace: 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 | |
633 | void Function::addAttributeAtIndex(unsigned i, Attribute Attr) { |
634 | AttributeSets = AttributeSets.addAttributeAtIndex(C&: getContext(), Index: i, A: Attr); |
635 | } |
636 | |
637 | void Function::addFnAttr(Attribute::AttrKind Kind) { |
638 | AttributeSets = AttributeSets.addFnAttribute(C&: getContext(), Kind); |
639 | } |
640 | |
641 | void Function::addFnAttr(StringRef Kind, StringRef Val) { |
642 | AttributeSets = AttributeSets.addFnAttribute(C&: getContext(), Kind, Value: Val); |
643 | } |
644 | |
645 | void Function::addFnAttr(Attribute Attr) { |
646 | AttributeSets = AttributeSets.addFnAttribute(C&: getContext(), Attr); |
647 | } |
648 | |
649 | void Function::addFnAttrs(const AttrBuilder &Attrs) { |
650 | AttributeSets = AttributeSets.addFnAttributes(C&: getContext(), B: Attrs); |
651 | } |
652 | |
653 | void Function::addRetAttr(Attribute::AttrKind Kind) { |
654 | AttributeSets = AttributeSets.addRetAttribute(C&: getContext(), Kind); |
655 | } |
656 | |
657 | void Function::addRetAttr(Attribute Attr) { |
658 | AttributeSets = AttributeSets.addRetAttribute(C&: getContext(), Attr); |
659 | } |
660 | |
661 | void Function::addRetAttrs(const AttrBuilder &Attrs) { |
662 | AttributeSets = AttributeSets.addRetAttributes(C&: getContext(), B: Attrs); |
663 | } |
664 | |
665 | void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
666 | AttributeSets = AttributeSets.addParamAttribute(C&: getContext(), ArgNo, Kind); |
667 | } |
668 | |
669 | void Function::addParamAttr(unsigned ArgNo, Attribute Attr) { |
670 | AttributeSets = AttributeSets.addParamAttribute(C&: getContext(), ArgNos: ArgNo, A: Attr); |
671 | } |
672 | |
673 | void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) { |
674 | AttributeSets = AttributeSets.addParamAttributes(C&: getContext(), ArgNo, B: Attrs); |
675 | } |
676 | |
677 | void Function::removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) { |
678 | AttributeSets = AttributeSets.removeAttributeAtIndex(C&: getContext(), Index: i, Kind); |
679 | } |
680 | |
681 | void Function::removeAttributeAtIndex(unsigned i, StringRef Kind) { |
682 | AttributeSets = AttributeSets.removeAttributeAtIndex(C&: getContext(), Index: i, Kind); |
683 | } |
684 | |
685 | void Function::removeFnAttr(Attribute::AttrKind Kind) { |
686 | AttributeSets = AttributeSets.removeFnAttribute(C&: getContext(), Kind); |
687 | } |
688 | |
689 | void Function::removeFnAttr(StringRef Kind) { |
690 | AttributeSets = AttributeSets.removeFnAttribute(C&: getContext(), Kind); |
691 | } |
692 | |
693 | void Function::removeFnAttrs(const AttributeMask &AM) { |
694 | AttributeSets = AttributeSets.removeFnAttributes(C&: getContext(), AttrsToRemove: AM); |
695 | } |
696 | |
697 | void Function::removeRetAttr(Attribute::AttrKind Kind) { |
698 | AttributeSets = AttributeSets.removeRetAttribute(C&: getContext(), Kind); |
699 | } |
700 | |
701 | void Function::removeRetAttr(StringRef Kind) { |
702 | AttributeSets = AttributeSets.removeRetAttribute(C&: getContext(), Kind); |
703 | } |
704 | |
705 | void Function::removeRetAttrs(const AttributeMask &Attrs) { |
706 | AttributeSets = AttributeSets.removeRetAttributes(C&: getContext(), AttrsToRemove: Attrs); |
707 | } |
708 | |
709 | void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) { |
710 | AttributeSets = AttributeSets.removeParamAttribute(C&: getContext(), ArgNo, Kind); |
711 | } |
712 | |
713 | void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) { |
714 | AttributeSets = AttributeSets.removeParamAttribute(C&: getContext(), ArgNo, Kind); |
715 | } |
716 | |
717 | void Function::removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs) { |
718 | AttributeSets = |
719 | AttributeSets.removeParamAttributes(C&: getContext(), ArgNo, AttrsToRemove: Attrs); |
720 | } |
721 | |
722 | void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) { |
723 | AttributeSets = |
724 | AttributeSets.addDereferenceableParamAttr(C&: getContext(), ArgNo, Bytes); |
725 | } |
726 | |
727 | bool Function::hasFnAttribute(Attribute::AttrKind Kind) const { |
728 | return AttributeSets.hasFnAttr(Kind); |
729 | } |
730 | |
731 | bool Function::hasFnAttribute(StringRef Kind) const { |
732 | return AttributeSets.hasFnAttr(Kind); |
733 | } |
734 | |
735 | bool Function::hasRetAttribute(Attribute::AttrKind Kind) const { |
736 | return AttributeSets.hasRetAttr(Kind); |
737 | } |
738 | |
739 | bool Function::hasParamAttribute(unsigned ArgNo, |
740 | Attribute::AttrKind Kind) const { |
741 | return AttributeSets.hasParamAttr(ArgNo, Kind); |
742 | } |
743 | |
744 | bool Function::hasParamAttribute(unsigned ArgNo, StringRef Kind) const { |
745 | return AttributeSets.hasParamAttr(ArgNo, Kind); |
746 | } |
747 | |
748 | Attribute Function::getAttributeAtIndex(unsigned i, |
749 | Attribute::AttrKind Kind) const { |
750 | return AttributeSets.getAttributeAtIndex(Index: i, Kind); |
751 | } |
752 | |
753 | Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const { |
754 | return AttributeSets.getAttributeAtIndex(Index: i, Kind); |
755 | } |
756 | |
757 | bool Function::hasAttributeAtIndex(unsigned Idx, |
758 | Attribute::AttrKind Kind) const { |
759 | return AttributeSets.hasAttributeAtIndex(Index: Idx, Kind); |
760 | } |
761 | |
762 | Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const { |
763 | return AttributeSets.getFnAttr(Kind); |
764 | } |
765 | |
766 | Attribute Function::getFnAttribute(StringRef Kind) const { |
767 | return AttributeSets.getFnAttr(Kind); |
768 | } |
769 | |
770 | Attribute Function::getRetAttribute(Attribute::AttrKind Kind) const { |
771 | return AttributeSets.getRetAttr(Kind); |
772 | } |
773 | |
774 | uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name, |
775 | uint64_t Default) const { |
776 | Attribute A = getFnAttribute(Kind: Name); |
777 | uint64_t Result = Default; |
778 | if (A.isStringAttribute()) { |
779 | StringRef Str = A.getValueAsString(); |
780 | if (Str.getAsInteger(Radix: 0, Result)) |
781 | getContext().emitError(ErrorStr: "cannot parse integer attribute " + Name); |
782 | } |
783 | |
784 | return Result; |
785 | } |
786 | |
787 | /// gets the specified attribute from the list of attributes. |
788 | Attribute Function::getParamAttribute(unsigned ArgNo, |
789 | Attribute::AttrKind Kind) const { |
790 | return AttributeSets.getParamAttr(ArgNo, Kind); |
791 | } |
792 | |
793 | void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo, |
794 | uint64_t Bytes) { |
795 | AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(C&: getContext(), |
796 | ArgNo, Bytes); |
797 | } |
798 | |
799 | void Function::addRangeRetAttr(const ConstantRange &CR) { |
800 | AttributeSets = AttributeSets.addRangeRetAttr(C&: getContext(), CR); |
801 | } |
802 | |
803 | 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 | |
815 | DenormalMode Function::getDenormalModeRaw() const { |
816 | Attribute Attr = getFnAttribute(Kind: "denormal-fp-math" ); |
817 | StringRef Val = Attr.getValueAsString(); |
818 | return parseDenormalFPAttribute(Str: Val); |
819 | } |
820 | |
821 | DenormalMode Function::getDenormalModeF32Raw() const { |
822 | Attribute Attr = getFnAttribute(Kind: "denormal-fp-math-f32" ); |
823 | if (Attr.isValid()) { |
824 | StringRef Val = Attr.getValueAsString(); |
825 | return parseDenormalFPAttribute(Str: Val); |
826 | } |
827 | |
828 | return DenormalMode::getInvalid(); |
829 | } |
830 | |
831 | const std::string &Function::getGC() const { |
832 | assert(hasGC() && "Function has no collector" ); |
833 | return getContext().getGC(Fn: *this); |
834 | } |
835 | |
836 | void Function::setGC(std::string Str) { |
837 | setValueSubclassDataBit(Bit: 14, On: !Str.empty()); |
838 | getContext().setGC(Fn: *this, GCName: std::move(Str)); |
839 | } |
840 | |
841 | void Function::clearGC() { |
842 | if (!hasGC()) |
843 | return; |
844 | getContext().deleteGC(Fn: *this); |
845 | setValueSubclassDataBit(Bit: 14, On: false); |
846 | } |
847 | |
848 | bool Function::hasStackProtectorFnAttr() const { |
849 | return hasFnAttribute(Kind: Attribute::StackProtect) || |
850 | hasFnAttribute(Kind: Attribute::StackProtectStrong) || |
851 | hasFnAttribute(Kind: Attribute::StackProtectReq); |
852 | } |
853 | |
854 | /// Copy all additional attributes (those not needed to create a Function) from |
855 | /// the Function Src to this one. |
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 | |
872 | MemoryEffects Function::getMemoryEffects() const { |
873 | return getAttributes().getMemoryEffects(); |
874 | } |
875 | void Function::setMemoryEffects(MemoryEffects ME) { |
876 | addFnAttr(Attr: Attribute::getWithMemoryEffects(Context&: getContext(), ME)); |
877 | } |
878 | |
879 | /// Determine if the function does not access memory. |
880 | bool Function::doesNotAccessMemory() const { |
881 | return getMemoryEffects().doesNotAccessMemory(); |
882 | } |
883 | void Function::setDoesNotAccessMemory() { |
884 | setMemoryEffects(MemoryEffects::none()); |
885 | } |
886 | |
887 | /// Determine if the function does not access or only reads memory. |
888 | bool Function::onlyReadsMemory() const { |
889 | return getMemoryEffects().onlyReadsMemory(); |
890 | } |
891 | void Function::setOnlyReadsMemory() { |
892 | setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly()); |
893 | } |
894 | |
895 | /// Determine if the function does not access or only writes memory. |
896 | bool Function::onlyWritesMemory() const { |
897 | return getMemoryEffects().onlyWritesMemory(); |
898 | } |
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. |
905 | bool Function::onlyAccessesArgMemory() const { |
906 | return getMemoryEffects().onlyAccessesArgPointees(); |
907 | } |
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. |
914 | bool Function::onlyAccessesInaccessibleMemory() const { |
915 | return getMemoryEffects().onlyAccessesInaccessibleMem(); |
916 | } |
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. |
923 | bool Function::onlyAccessesInaccessibleMemOrArgMem() const { |
924 | return getMemoryEffects().onlyAccessesInaccessibleOrArgMem(); |
925 | } |
926 | void Function::setOnlyAccessesInaccessibleMemOrArgMem() { |
927 | setMemoryEffects(getMemoryEffects() & |
928 | MemoryEffects::inaccessibleOrArgMemOnly()); |
929 | } |
930 | |
931 | bool Function::isTargetIntrinsic() const { |
932 | return Intrinsic::isTargetIntrinsic(IID: IntID); |
933 | } |
934 | |
935 | void Function::updateAfterNameChange() { |
936 | LibFuncCache = UnknownLibFunc; |
937 | StringRef Name = getName(); |
938 | if (!Name.starts_with(Prefix: "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. |
951 | 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>(Val: FU); |
965 | if (!Call) { |
966 | if (IgnoreAssumeLikeCalls && |
967 | isa<BitCastOperator, AddrSpaceCastOperator>(Val: FU) && |
968 | all_of(Range: FU->users(), P: [](const User *U) { |
969 | if (const auto *I = dyn_cast<IntrinsicInst>(Val: 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>(Val: FU) && |
979 | FU->hasOneUse() && !FU->user_begin()->user_empty()) |
980 | FUU = *FU->user_begin(); |
981 | if (llvm::all_of(Range: FUU->users(), P: [](const User *U) { |
982 | if (const auto *GV = dyn_cast<GlobalVariable>(Val: 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>(Val: Call)) |
997 | if (I->isAssumeLikeIntrinsic()) |
998 | continue; |
999 | } |
1000 | |
1001 | if (!Call->isCallee(U: &U) || (!IgnoreCastedDirectCall && |
1002 | Call->getFunctionType() != getFunctionType())) { |
1003 | if (IgnoreARCAttachedCall && |
1004 | Call->isOperandBundleOfType(ID: LLVMContext::OB_clang_arc_attachedcall, |
1005 | Idx: U.getOperandNo())) |
1006 | continue; |
1007 | |
1008 | if (PutOffender) |
1009 | *PutOffender = FU; |
1010 | return true; |
1011 | } |
1012 | } |
1013 | return false; |
1014 | } |
1015 | |
1016 | 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". |
1027 | bool Function::callsFunctionThatReturnsTwice() const { |
1028 | for (const Instruction &I : instructions(F: this)) |
1029 | if (const auto *Call = dyn_cast<CallBase>(Val: &I)) |
1030 | if (Call->hasFnAttr(Kind: Attribute::ReturnsTwice)) |
1031 | return true; |
1032 | |
1033 | return false; |
1034 | } |
1035 | |
1036 | Constant *Function::getPersonalityFn() const { |
1037 | assert(hasPersonalityFn() && getNumOperands()); |
1038 | return cast<Constant>(Val: Op<0>()); |
1039 | } |
1040 | |
1041 | void Function::setPersonalityFn(Constant *Fn) { |
1042 | setHungoffOperand<0>(Fn); |
1043 | setValueSubclassDataBit(Bit: 3, On: Fn != nullptr); |
1044 | } |
1045 | |
1046 | Constant *Function::getPrefixData() const { |
1047 | assert(hasPrefixData() && getNumOperands()); |
1048 | return cast<Constant>(Val: Op<1>()); |
1049 | } |
1050 | |
1051 | void Function::setPrefixData(Constant *PrefixData) { |
1052 | setHungoffOperand<1>(PrefixData); |
1053 | setValueSubclassDataBit(Bit: 1, On: PrefixData != nullptr); |
1054 | } |
1055 | |
1056 | Constant *Function::getPrologueData() const { |
1057 | assert(hasPrologueData() && getNumOperands()); |
1058 | return cast<Constant>(Val: Op<2>()); |
1059 | } |
1060 | |
1061 | void Function::setPrologueData(Constant *PrologueData) { |
1062 | setHungoffOperand<2>(PrologueData); |
1063 | setValueSubclassDataBit(Bit: 2, On: PrologueData != nullptr); |
1064 | } |
1065 | |
1066 | void Function::allocHungoffUselist() { |
1067 | // If we've already allocated a uselist, stop here. |
1068 | if (getNumOperands()) |
1069 | return; |
1070 | |
1071 | allocHungoffUses(N: 3, /*IsPhi=*/ false); |
1072 | setNumHungOffUseOperands(3); |
1073 | |
1074 | // Initialize the uselist with placeholder operands to allow traversal. |
1075 | auto *CPN = ConstantPointerNull::get(T: PointerType::get(C&: getContext(), AddressSpace: 0)); |
1076 | Op<0>().set(CPN); |
1077 | Op<1>().set(CPN); |
1078 | Op<2>().set(CPN); |
1079 | } |
1080 | |
1081 | template <int Idx> |
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(T: PointerType::get(C&: getContext(), AddressSpace: 0))); |
1088 | } |
1089 | } |
1090 | |
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 | |
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 | KindID: LLVMContext::MD_prof, |
1113 | Node: MDB.createFunctionEntryCount(Count: Count.getCount(), Synthetic: Count.isSynthetic(), Imports: S)); |
1114 | } |
1115 | |
1116 | void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type, |
1117 | const DenseSet<GlobalValue::GUID> *Imports) { |
1118 | setEntryCount(Count: ProfileCount(Count, Type), S: Imports); |
1119 | } |
1120 | |
1121 | std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const { |
1122 | MDNode *MD = getMetadata(KindID: LLVMContext::MD_prof); |
1123 | if (MD && MD->getOperand(I: 0)) |
1124 | if (MDString *MDS = dyn_cast<MDString>(Val: MD->getOperand(I: 0))) { |
1125 | if (MDS->getString() == MDProfLabels::FunctionEntryCount) { |
1126 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD: MD->getOperand(I: 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: MD->getOperand(I: 1)); |
1137 | uint64_t Count = CI->getValue().getZExtValue(); |
1138 | return ProfileCount(Count, PCT_Synthetic); |
1139 | } |
1140 | } |
1141 | return std::nullopt; |
1142 | } |
1143 | |
1144 | DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const { |
1145 | DenseSet<GlobalValue::GUID> R; |
1146 | if (MDNode *MD = getMetadata(KindID: LLVMContext::MD_prof)) |
1147 | if (MDString *MDS = dyn_cast<MDString>(Val: MD->getOperand(I: 0))) |
1148 | if (MDS->getString() == MDProfLabels::FunctionEntryCount) |
1149 | for (unsigned i = 2; i < MD->getNumOperands(); i++) |
1150 | R.insert(V: mdconst::extract<ConstantInt>(MD: MD->getOperand(I: i)) |
1151 | ->getValue() |
1152 | .getZExtValue()); |
1153 | return R; |
1154 | } |
1155 | |
1156 | bool Function::nullPointerIsDefined() const { |
1157 | return hasFnAttribute(Kind: Attribute::NullPointerIsValid); |
1158 | } |
1159 | |
1160 | unsigned Function::getVScaleValue() const { |
1161 | Attribute Attr = getFnAttribute(Kind: 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 | |
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 | |
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 | |