xref: /llvm-project/llvm/lib/IR/Function.cpp (revision 1a5239251ead73ee57f4e2f7fc93433ac7cf18b1)
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/DenseSet.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.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/IntrinsicsAArch64.h"
36 #include "llvm/IR/IntrinsicsAMDGPU.h"
37 #include "llvm/IR/IntrinsicsARM.h"
38 #include "llvm/IR/IntrinsicsBPF.h"
39 #include "llvm/IR/IntrinsicsDirectX.h"
40 #include "llvm/IR/IntrinsicsHexagon.h"
41 #include "llvm/IR/IntrinsicsLoongArch.h"
42 #include "llvm/IR/IntrinsicsMips.h"
43 #include "llvm/IR/IntrinsicsNVPTX.h"
44 #include "llvm/IR/IntrinsicsPowerPC.h"
45 #include "llvm/IR/IntrinsicsR600.h"
46 #include "llvm/IR/IntrinsicsRISCV.h"
47 #include "llvm/IR/IntrinsicsS390.h"
48 #include "llvm/IR/IntrinsicsSPIRV.h"
49 #include "llvm/IR/IntrinsicsVE.h"
50 #include "llvm/IR/IntrinsicsWebAssembly.h"
51 #include "llvm/IR/IntrinsicsX86.h"
52 #include "llvm/IR/IntrinsicsXCore.h"
53 #include "llvm/IR/LLVMContext.h"
54 #include "llvm/IR/MDBuilder.h"
55 #include "llvm/IR/Metadata.h"
56 #include "llvm/IR/Module.h"
57 #include "llvm/IR/Operator.h"
58 #include "llvm/IR/SymbolTableListTraits.h"
59 #include "llvm/IR/Type.h"
60 #include "llvm/IR/Use.h"
61 #include "llvm/IR/User.h"
62 #include "llvm/IR/Value.h"
63 #include "llvm/IR/ValueSymbolTable.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/CommandLine.h"
66 #include "llvm/Support/Compiler.h"
67 #include "llvm/Support/ErrorHandling.h"
68 #include "llvm/Support/ModRef.h"
69 #include <cassert>
70 #include <cstddef>
71 #include <cstdint>
72 #include <cstring>
73 #include <string>
74 
75 using namespace llvm;
76 using ProfileCount = Function::ProfileCount;
77 
78 // Explicit instantiations of SymbolTableListTraits since some of the methods
79 // are not in the public header file...
80 template class llvm::SymbolTableListTraits<BasicBlock>;
81 
82 static cl::opt<int> NonGlobalValueMaxNameSize(
83     "non-global-value-max-name-size", cl::Hidden, cl::init(1024),
84     cl::desc("Maximum size for the name of non-global values."));
85 
86 void Function::convertToNewDbgValues() {
87   IsNewDbgInfoFormat = true;
88   for (auto &BB : *this) {
89     BB.convertToNewDbgValues();
90   }
91 }
92 
93 void Function::convertFromNewDbgValues() {
94   IsNewDbgInfoFormat = false;
95   for (auto &BB : *this) {
96     BB.convertFromNewDbgValues();
97   }
98 }
99 
100 void Function::setIsNewDbgInfoFormat(bool NewFlag) {
101   if (NewFlag && !IsNewDbgInfoFormat)
102     convertToNewDbgValues();
103   else if (!NewFlag && IsNewDbgInfoFormat)
104     convertFromNewDbgValues();
105 }
106 void Function::setNewDbgInfoFormatFlag(bool NewFlag) {
107   for (auto &BB : *this) {
108     BB.setNewDbgInfoFormatFlag(NewFlag);
109   }
110   IsNewDbgInfoFormat = NewFlag;
111 }
112 
113 //===----------------------------------------------------------------------===//
114 // Argument Implementation
115 //===----------------------------------------------------------------------===//
116 
117 Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
118     : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
119   setName(Name);
120 }
121 
122 void Argument::setParent(Function *parent) {
123   Parent = parent;
124 }
125 
126 bool Argument::hasNonNullAttr(bool AllowUndefOrPoison) const {
127   if (!getType()->isPointerTy()) return false;
128   if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull) &&
129       (AllowUndefOrPoison ||
130        getParent()->hasParamAttribute(getArgNo(), Attribute::NoUndef)))
131     return true;
132   else if (getDereferenceableBytes() > 0 &&
133            !NullPointerIsDefined(getParent(),
134                                  getType()->getPointerAddressSpace()))
135     return true;
136   return false;
137 }
138 
139 bool Argument::hasByValAttr() const {
140   if (!getType()->isPointerTy()) return false;
141   return hasAttribute(Attribute::ByVal);
142 }
143 
144 bool Argument::hasByRefAttr() const {
145   if (!getType()->isPointerTy())
146     return false;
147   return hasAttribute(Attribute::ByRef);
148 }
149 
150 bool Argument::hasSwiftSelfAttr() const {
151   return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
152 }
153 
154 bool Argument::hasSwiftErrorAttr() const {
155   return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
156 }
157 
158 bool Argument::hasInAllocaAttr() const {
159   if (!getType()->isPointerTy()) return false;
160   return hasAttribute(Attribute::InAlloca);
161 }
162 
163 bool Argument::hasPreallocatedAttr() const {
164   if (!getType()->isPointerTy())
165     return false;
166   return hasAttribute(Attribute::Preallocated);
167 }
168 
169 bool Argument::hasPassPointeeByValueCopyAttr() const {
170   if (!getType()->isPointerTy()) return false;
171   AttributeList Attrs = getParent()->getAttributes();
172   return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) ||
173          Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) ||
174          Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated);
175 }
176 
177 bool Argument::hasPointeeInMemoryValueAttr() const {
178   if (!getType()->isPointerTy())
179     return false;
180   AttributeList Attrs = getParent()->getAttributes();
181   return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) ||
182          Attrs.hasParamAttr(getArgNo(), Attribute::StructRet) ||
183          Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) ||
184          Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated) ||
185          Attrs.hasParamAttr(getArgNo(), Attribute::ByRef);
186 }
187 
188 /// For a byval, sret, inalloca, or preallocated parameter, get the in-memory
189 /// parameter type.
190 static Type *getMemoryParamAllocType(AttributeSet ParamAttrs) {
191   // FIXME: All the type carrying attributes are mutually exclusive, so there
192   // should be a single query to get the stored type that handles any of them.
193   if (Type *ByValTy = ParamAttrs.getByValType())
194     return ByValTy;
195   if (Type *ByRefTy = ParamAttrs.getByRefType())
196     return ByRefTy;
197   if (Type *PreAllocTy = ParamAttrs.getPreallocatedType())
198     return PreAllocTy;
199   if (Type *InAllocaTy = ParamAttrs.getInAllocaType())
200     return InAllocaTy;
201   if (Type *SRetTy = ParamAttrs.getStructRetType())
202     return SRetTy;
203 
204   return nullptr;
205 }
206 
207 uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout &DL) const {
208   AttributeSet ParamAttrs =
209       getParent()->getAttributes().getParamAttrs(getArgNo());
210   if (Type *MemTy = getMemoryParamAllocType(ParamAttrs))
211     return DL.getTypeAllocSize(MemTy);
212   return 0;
213 }
214 
215 Type *Argument::getPointeeInMemoryValueType() const {
216   AttributeSet ParamAttrs =
217       getParent()->getAttributes().getParamAttrs(getArgNo());
218   return getMemoryParamAllocType(ParamAttrs);
219 }
220 
221 MaybeAlign Argument::getParamAlign() const {
222   assert(getType()->isPointerTy() && "Only pointers have alignments");
223   return getParent()->getParamAlign(getArgNo());
224 }
225 
226 MaybeAlign Argument::getParamStackAlign() const {
227   return getParent()->getParamStackAlign(getArgNo());
228 }
229 
230 Type *Argument::getParamByValType() const {
231   assert(getType()->isPointerTy() && "Only pointers have byval types");
232   return getParent()->getParamByValType(getArgNo());
233 }
234 
235 Type *Argument::getParamStructRetType() const {
236   assert(getType()->isPointerTy() && "Only pointers have sret types");
237   return getParent()->getParamStructRetType(getArgNo());
238 }
239 
240 Type *Argument::getParamByRefType() const {
241   assert(getType()->isPointerTy() && "Only pointers have byref types");
242   return getParent()->getParamByRefType(getArgNo());
243 }
244 
245 Type *Argument::getParamInAllocaType() const {
246   assert(getType()->isPointerTy() && "Only pointers have inalloca types");
247   return getParent()->getParamInAllocaType(getArgNo());
248 }
249 
250 uint64_t Argument::getDereferenceableBytes() const {
251   assert(getType()->isPointerTy() &&
252          "Only pointers have dereferenceable bytes");
253   return getParent()->getParamDereferenceableBytes(getArgNo());
254 }
255 
256 uint64_t Argument::getDereferenceableOrNullBytes() const {
257   assert(getType()->isPointerTy() &&
258          "Only pointers have dereferenceable bytes");
259   return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
260 }
261 
262 FPClassTest Argument::getNoFPClass() const {
263   return getParent()->getParamNoFPClass(getArgNo());
264 }
265 
266 std::optional<ConstantRange> Argument::getRange() const {
267   const Attribute RangeAttr = getAttribute(llvm::Attribute::Range);
268   if (RangeAttr.isValid())
269     return RangeAttr.getRange();
270   return std::nullopt;
271 }
272 
273 bool Argument::hasNestAttr() const {
274   if (!getType()->isPointerTy()) return false;
275   return hasAttribute(Attribute::Nest);
276 }
277 
278 bool Argument::hasNoAliasAttr() const {
279   if (!getType()->isPointerTy()) return false;
280   return hasAttribute(Attribute::NoAlias);
281 }
282 
283 bool Argument::hasNoCaptureAttr() const {
284   if (!getType()->isPointerTy()) return false;
285   return hasAttribute(Attribute::NoCapture);
286 }
287 
288 bool Argument::hasNoFreeAttr() const {
289   if (!getType()->isPointerTy()) return false;
290   return hasAttribute(Attribute::NoFree);
291 }
292 
293 bool Argument::hasStructRetAttr() const {
294   if (!getType()->isPointerTy()) return false;
295   return hasAttribute(Attribute::StructRet);
296 }
297 
298 bool Argument::hasInRegAttr() const {
299   return hasAttribute(Attribute::InReg);
300 }
301 
302 bool Argument::hasReturnedAttr() const {
303   return hasAttribute(Attribute::Returned);
304 }
305 
306 bool Argument::hasZExtAttr() const {
307   return hasAttribute(Attribute::ZExt);
308 }
309 
310 bool Argument::hasSExtAttr() const {
311   return hasAttribute(Attribute::SExt);
312 }
313 
314 bool Argument::onlyReadsMemory() const {
315   AttributeList Attrs = getParent()->getAttributes();
316   return Attrs.hasParamAttr(getArgNo(), Attribute::ReadOnly) ||
317          Attrs.hasParamAttr(getArgNo(), Attribute::ReadNone);
318 }
319 
320 void Argument::addAttrs(AttrBuilder &B) {
321   AttributeList AL = getParent()->getAttributes();
322   AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
323   getParent()->setAttributes(AL);
324 }
325 
326 void Argument::addAttr(Attribute::AttrKind Kind) {
327   getParent()->addParamAttr(getArgNo(), Kind);
328 }
329 
330 void Argument::addAttr(Attribute Attr) {
331   getParent()->addParamAttr(getArgNo(), Attr);
332 }
333 
334 void Argument::removeAttr(Attribute::AttrKind Kind) {
335   getParent()->removeParamAttr(getArgNo(), Kind);
336 }
337 
338 void Argument::removeAttrs(const AttributeMask &AM) {
339   AttributeList AL = getParent()->getAttributes();
340   AL = AL.removeParamAttributes(Parent->getContext(), getArgNo(), AM);
341   getParent()->setAttributes(AL);
342 }
343 
344 bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
345   return getParent()->hasParamAttribute(getArgNo(), Kind);
346 }
347 
348 Attribute Argument::getAttribute(Attribute::AttrKind Kind) const {
349   return getParent()->getParamAttribute(getArgNo(), Kind);
350 }
351 
352 //===----------------------------------------------------------------------===//
353 // Helper Methods in Function
354 //===----------------------------------------------------------------------===//
355 
356 LLVMContext &Function::getContext() const {
357   return getType()->getContext();
358 }
359 
360 unsigned Function::getInstructionCount() const {
361   unsigned NumInstrs = 0;
362   for (const BasicBlock &BB : BasicBlocks)
363     NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(),
364                                BB.instructionsWithoutDebug().end());
365   return NumInstrs;
366 }
367 
368 Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
369                            const Twine &N, Module &M) {
370   return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
371 }
372 
373 Function *Function::createWithDefaultAttr(FunctionType *Ty,
374                                           LinkageTypes Linkage,
375                                           unsigned AddrSpace, const Twine &N,
376                                           Module *M) {
377   auto *F = new Function(Ty, Linkage, AddrSpace, N, M);
378   AttrBuilder B(F->getContext());
379   UWTableKind UWTable = M->getUwtable();
380   if (UWTable != UWTableKind::None)
381     B.addUWTableAttr(UWTable);
382   switch (M->getFramePointer()) {
383   case FramePointerKind::None:
384     // 0 ("none") is the default.
385     break;
386   case FramePointerKind::Reserved:
387     B.addAttribute("frame-pointer", "reserved");
388     break;
389   case FramePointerKind::NonLeaf:
390     B.addAttribute("frame-pointer", "non-leaf");
391     break;
392   case FramePointerKind::All:
393     B.addAttribute("frame-pointer", "all");
394     break;
395   }
396   if (M->getModuleFlag("function_return_thunk_extern"))
397     B.addAttribute(Attribute::FnRetThunkExtern);
398   F->addFnAttrs(B);
399   return F;
400 }
401 
402 void Function::removeFromParent() {
403   getParent()->getFunctionList().remove(getIterator());
404 }
405 
406 void Function::eraseFromParent() {
407   getParent()->getFunctionList().erase(getIterator());
408 }
409 
410 void Function::splice(Function::iterator ToIt, Function *FromF,
411                       Function::iterator FromBeginIt,
412                       Function::iterator FromEndIt) {
413 #ifdef EXPENSIVE_CHECKS
414   // Check that FromBeginIt is before FromEndIt.
415   auto FromFEnd = FromF->end();
416   for (auto It = FromBeginIt; It != FromEndIt; ++It)
417     assert(It != FromFEnd && "FromBeginIt not before FromEndIt!");
418 #endif // EXPENSIVE_CHECKS
419   BasicBlocks.splice(ToIt, FromF->BasicBlocks, FromBeginIt, FromEndIt);
420 }
421 
422 Function::iterator Function::erase(Function::iterator FromIt,
423                                    Function::iterator ToIt) {
424   return BasicBlocks.erase(FromIt, ToIt);
425 }
426 
427 //===----------------------------------------------------------------------===//
428 // Function Implementation
429 //===----------------------------------------------------------------------===//
430 
431 static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) {
432   // If AS == -1 and we are passed a valid module pointer we place the function
433   // in the program address space. Otherwise we default to AS0.
434   if (AddrSpace == static_cast<unsigned>(-1))
435     return M ? M->getDataLayout().getProgramAddressSpace() : 0;
436   return AddrSpace;
437 }
438 
439 Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
440                    const Twine &name, Module *ParentModule)
441     : GlobalObject(Ty, Value::FunctionVal,
442                    OperandTraits<Function>::op_begin(this), 0, Linkage, name,
443                    computeAddrSpace(AddrSpace, ParentModule)),
444       NumArgs(Ty->getNumParams()), IsNewDbgInfoFormat(false) {
445   assert(FunctionType::isValidReturnType(getReturnType()) &&
446          "invalid return type");
447   setGlobalObjectSubClassData(0);
448 
449   // We only need a symbol table for a function if the context keeps value names
450   if (!getContext().shouldDiscardValueNames())
451     SymTab = std::make_unique<ValueSymbolTable>(NonGlobalValueMaxNameSize);
452 
453   // If the function has arguments, mark them as lazily built.
454   if (Ty->getNumParams())
455     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
456 
457   if (ParentModule) {
458     ParentModule->getFunctionList().push_back(this);
459     IsNewDbgInfoFormat = ParentModule->IsNewDbgInfoFormat;
460   }
461 
462   HasLLVMReservedName = getName().starts_with("llvm.");
463   // Ensure intrinsics have the right parameter attributes.
464   // Note, the IntID field will have been set in Value::setName if this function
465   // name is a valid intrinsic ID.
466   if (IntID)
467     setAttributes(Intrinsic::getAttributes(getContext(), IntID));
468 }
469 
470 Function::~Function() {
471   dropAllReferences();    // After this it is safe to delete instructions.
472 
473   // Delete all of the method arguments and unlink from symbol table...
474   if (Arguments)
475     clearArguments();
476 
477   // Remove the function from the on-the-side GC table.
478   clearGC();
479 }
480 
481 void Function::BuildLazyArguments() const {
482   // Create the arguments vector, all arguments start out unnamed.
483   auto *FT = getFunctionType();
484   if (NumArgs > 0) {
485     Arguments = std::allocator<Argument>().allocate(NumArgs);
486     for (unsigned i = 0, e = NumArgs; i != e; ++i) {
487       Type *ArgTy = FT->getParamType(i);
488       assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
489       new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
490     }
491   }
492 
493   // Clear the lazy arguments bit.
494   unsigned SDC = getSubclassDataFromValue();
495   SDC &= ~(1 << 0);
496   const_cast<Function*>(this)->setValueSubclassData(SDC);
497   assert(!hasLazyArguments());
498 }
499 
500 static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
501   return MutableArrayRef<Argument>(Args, Count);
502 }
503 
504 bool Function::isConstrainedFPIntrinsic() const {
505   return Intrinsic::isConstrainedFPIntrinsic(getIntrinsicID());
506 }
507 
508 void Function::clearArguments() {
509   for (Argument &A : makeArgArray(Arguments, NumArgs)) {
510     A.setName("");
511     A.~Argument();
512   }
513   std::allocator<Argument>().deallocate(Arguments, NumArgs);
514   Arguments = nullptr;
515 }
516 
517 void Function::stealArgumentListFrom(Function &Src) {
518   assert(isDeclaration() && "Expected no references to current arguments");
519 
520   // Drop the current arguments, if any, and set the lazy argument bit.
521   if (!hasLazyArguments()) {
522     assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
523                         [](const Argument &A) { return A.use_empty(); }) &&
524            "Expected arguments to be unused in declaration");
525     clearArguments();
526     setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
527   }
528 
529   // Nothing to steal if Src has lazy arguments.
530   if (Src.hasLazyArguments())
531     return;
532 
533   // Steal arguments from Src, and fix the lazy argument bits.
534   assert(arg_size() == Src.arg_size());
535   Arguments = Src.Arguments;
536   Src.Arguments = nullptr;
537   for (Argument &A : makeArgArray(Arguments, NumArgs)) {
538     // FIXME: This does the work of transferNodesFromList inefficiently.
539     SmallString<128> Name;
540     if (A.hasName())
541       Name = A.getName();
542     if (!Name.empty())
543       A.setName("");
544     A.setParent(this);
545     if (!Name.empty())
546       A.setName(Name);
547   }
548 
549   setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
550   assert(!hasLazyArguments());
551   Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
552 }
553 
554 void Function::deleteBodyImpl(bool ShouldDrop) {
555   setIsMaterializable(false);
556 
557   for (BasicBlock &BB : *this)
558     BB.dropAllReferences();
559 
560   // Delete all basic blocks. They are now unused, except possibly by
561   // blockaddresses, but BasicBlock's destructor takes care of those.
562   while (!BasicBlocks.empty())
563     BasicBlocks.begin()->eraseFromParent();
564 
565   if (getNumOperands()) {
566     if (ShouldDrop) {
567       // Drop uses of any optional data (real or placeholder).
568       User::dropAllReferences();
569       setNumHungOffUseOperands(0);
570     } else {
571       // The code needs to match Function::allocHungoffUselist().
572       auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0));
573       Op<0>().set(CPN);
574       Op<1>().set(CPN);
575       Op<2>().set(CPN);
576     }
577     setValueSubclassData(getSubclassDataFromValue() & ~0xe);
578   }
579 
580   // Metadata is stored in a side-table.
581   clearMetadata();
582 }
583 
584 void Function::addAttributeAtIndex(unsigned i, Attribute Attr) {
585   AttributeSets = AttributeSets.addAttributeAtIndex(getContext(), i, Attr);
586 }
587 
588 void Function::addFnAttr(Attribute::AttrKind Kind) {
589   AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind);
590 }
591 
592 void Function::addFnAttr(StringRef Kind, StringRef Val) {
593   AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind, Val);
594 }
595 
596 void Function::addFnAttr(Attribute Attr) {
597   AttributeSets = AttributeSets.addFnAttribute(getContext(), Attr);
598 }
599 
600 void Function::addFnAttrs(const AttrBuilder &Attrs) {
601   AttributeSets = AttributeSets.addFnAttributes(getContext(), Attrs);
602 }
603 
604 void Function::addRetAttr(Attribute::AttrKind Kind) {
605   AttributeSets = AttributeSets.addRetAttribute(getContext(), Kind);
606 }
607 
608 void Function::addRetAttr(Attribute Attr) {
609   AttributeSets = AttributeSets.addRetAttribute(getContext(), Attr);
610 }
611 
612 void Function::addRetAttrs(const AttrBuilder &Attrs) {
613   AttributeSets = AttributeSets.addRetAttributes(getContext(), Attrs);
614 }
615 
616 void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
617   AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Kind);
618 }
619 
620 void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
621   AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Attr);
622 }
623 
624 void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
625   AttributeSets = AttributeSets.addParamAttributes(getContext(), ArgNo, Attrs);
626 }
627 
628 void Function::removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) {
629   AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind);
630 }
631 
632 void Function::removeAttributeAtIndex(unsigned i, StringRef Kind) {
633   AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind);
634 }
635 
636 void Function::removeFnAttr(Attribute::AttrKind Kind) {
637   AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind);
638 }
639 
640 void Function::removeFnAttr(StringRef Kind) {
641   AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind);
642 }
643 
644 void Function::removeFnAttrs(const AttributeMask &AM) {
645   AttributeSets = AttributeSets.removeFnAttributes(getContext(), AM);
646 }
647 
648 void Function::removeRetAttr(Attribute::AttrKind Kind) {
649   AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind);
650 }
651 
652 void Function::removeRetAttr(StringRef Kind) {
653   AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind);
654 }
655 
656 void Function::removeRetAttrs(const AttributeMask &Attrs) {
657   AttributeSets = AttributeSets.removeRetAttributes(getContext(), Attrs);
658 }
659 
660 void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
661   AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind);
662 }
663 
664 void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
665   AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind);
666 }
667 
668 void Function::removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs) {
669   AttributeSets =
670       AttributeSets.removeParamAttributes(getContext(), ArgNo, Attrs);
671 }
672 
673 void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
674   AttributeSets =
675       AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
676 }
677 
678 bool Function::hasFnAttribute(Attribute::AttrKind Kind) const {
679   return AttributeSets.hasFnAttr(Kind);
680 }
681 
682 bool Function::hasFnAttribute(StringRef Kind) const {
683   return AttributeSets.hasFnAttr(Kind);
684 }
685 
686 bool Function::hasRetAttribute(Attribute::AttrKind Kind) const {
687   return AttributeSets.hasRetAttr(Kind);
688 }
689 
690 bool Function::hasParamAttribute(unsigned ArgNo,
691                                  Attribute::AttrKind Kind) const {
692   return AttributeSets.hasParamAttr(ArgNo, Kind);
693 }
694 
695 Attribute Function::getAttributeAtIndex(unsigned i,
696                                         Attribute::AttrKind Kind) const {
697   return AttributeSets.getAttributeAtIndex(i, Kind);
698 }
699 
700 Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const {
701   return AttributeSets.getAttributeAtIndex(i, Kind);
702 }
703 
704 Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const {
705   return AttributeSets.getFnAttr(Kind);
706 }
707 
708 Attribute Function::getFnAttribute(StringRef Kind) const {
709   return AttributeSets.getFnAttr(Kind);
710 }
711 
712 Attribute Function::getRetAttribute(Attribute::AttrKind Kind) const {
713   return AttributeSets.getRetAttr(Kind);
714 }
715 
716 uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name,
717                                                  uint64_t Default) const {
718   Attribute A = getFnAttribute(Name);
719   uint64_t Result = Default;
720   if (A.isStringAttribute()) {
721     StringRef Str = A.getValueAsString();
722     if (Str.getAsInteger(0, Result))
723       getContext().emitError("cannot parse integer attribute " + Name);
724   }
725 
726   return Result;
727 }
728 
729 /// gets the specified attribute from the list of attributes.
730 Attribute Function::getParamAttribute(unsigned ArgNo,
731                                       Attribute::AttrKind Kind) const {
732   return AttributeSets.getParamAttr(ArgNo, Kind);
733 }
734 
735 void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
736                                                  uint64_t Bytes) {
737   AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(getContext(),
738                                                                   ArgNo, Bytes);
739 }
740 
741 void Function::addRangeRetAttr(const ConstantRange &CR) {
742   AttributeSets = AttributeSets.addRangeRetAttr(getContext(), CR);
743 }
744 
745 DenormalMode Function::getDenormalMode(const fltSemantics &FPType) const {
746   if (&FPType == &APFloat::IEEEsingle()) {
747     DenormalMode Mode = getDenormalModeF32Raw();
748     // If the f32 variant of the attribute isn't specified, try to use the
749     // generic one.
750     if (Mode.isValid())
751       return Mode;
752   }
753 
754   return getDenormalModeRaw();
755 }
756 
757 DenormalMode Function::getDenormalModeRaw() const {
758   Attribute Attr = getFnAttribute("denormal-fp-math");
759   StringRef Val = Attr.getValueAsString();
760   return parseDenormalFPAttribute(Val);
761 }
762 
763 DenormalMode Function::getDenormalModeF32Raw() const {
764   Attribute Attr = getFnAttribute("denormal-fp-math-f32");
765   if (Attr.isValid()) {
766     StringRef Val = Attr.getValueAsString();
767     return parseDenormalFPAttribute(Val);
768   }
769 
770   return DenormalMode::getInvalid();
771 }
772 
773 const std::string &Function::getGC() const {
774   assert(hasGC() && "Function has no collector");
775   return getContext().getGC(*this);
776 }
777 
778 void Function::setGC(std::string Str) {
779   setValueSubclassDataBit(14, !Str.empty());
780   getContext().setGC(*this, std::move(Str));
781 }
782 
783 void Function::clearGC() {
784   if (!hasGC())
785     return;
786   getContext().deleteGC(*this);
787   setValueSubclassDataBit(14, false);
788 }
789 
790 bool Function::hasStackProtectorFnAttr() const {
791   return hasFnAttribute(Attribute::StackProtect) ||
792          hasFnAttribute(Attribute::StackProtectStrong) ||
793          hasFnAttribute(Attribute::StackProtectReq);
794 }
795 
796 /// Copy all additional attributes (those not needed to create a Function) from
797 /// the Function Src to this one.
798 void Function::copyAttributesFrom(const Function *Src) {
799   GlobalObject::copyAttributesFrom(Src);
800   setCallingConv(Src->getCallingConv());
801   setAttributes(Src->getAttributes());
802   if (Src->hasGC())
803     setGC(Src->getGC());
804   else
805     clearGC();
806   if (Src->hasPersonalityFn())
807     setPersonalityFn(Src->getPersonalityFn());
808   if (Src->hasPrefixData())
809     setPrefixData(Src->getPrefixData());
810   if (Src->hasPrologueData())
811     setPrologueData(Src->getPrologueData());
812 }
813 
814 MemoryEffects Function::getMemoryEffects() const {
815   return getAttributes().getMemoryEffects();
816 }
817 void Function::setMemoryEffects(MemoryEffects ME) {
818   addFnAttr(Attribute::getWithMemoryEffects(getContext(), ME));
819 }
820 
821 /// Determine if the function does not access memory.
822 bool Function::doesNotAccessMemory() const {
823   return getMemoryEffects().doesNotAccessMemory();
824 }
825 void Function::setDoesNotAccessMemory() {
826   setMemoryEffects(MemoryEffects::none());
827 }
828 
829 /// Determine if the function does not access or only reads memory.
830 bool Function::onlyReadsMemory() const {
831   return getMemoryEffects().onlyReadsMemory();
832 }
833 void Function::setOnlyReadsMemory() {
834   setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly());
835 }
836 
837 /// Determine if the function does not access or only writes memory.
838 bool Function::onlyWritesMemory() const {
839   return getMemoryEffects().onlyWritesMemory();
840 }
841 void Function::setOnlyWritesMemory() {
842   setMemoryEffects(getMemoryEffects() & MemoryEffects::writeOnly());
843 }
844 
845 /// Determine if the call can access memmory only using pointers based
846 /// on its arguments.
847 bool Function::onlyAccessesArgMemory() const {
848   return getMemoryEffects().onlyAccessesArgPointees();
849 }
850 void Function::setOnlyAccessesArgMemory() {
851   setMemoryEffects(getMemoryEffects() & MemoryEffects::argMemOnly());
852 }
853 
854 /// Determine if the function may only access memory that is
855 ///  inaccessible from the IR.
856 bool Function::onlyAccessesInaccessibleMemory() const {
857   return getMemoryEffects().onlyAccessesInaccessibleMem();
858 }
859 void Function::setOnlyAccessesInaccessibleMemory() {
860   setMemoryEffects(getMemoryEffects() & MemoryEffects::inaccessibleMemOnly());
861 }
862 
863 /// Determine if the function may only access memory that is
864 ///  either inaccessible from the IR or pointed to by its arguments.
865 bool Function::onlyAccessesInaccessibleMemOrArgMem() const {
866   return getMemoryEffects().onlyAccessesInaccessibleOrArgMem();
867 }
868 void Function::setOnlyAccessesInaccessibleMemOrArgMem() {
869   setMemoryEffects(getMemoryEffects() &
870                    MemoryEffects::inaccessibleOrArgMemOnly());
871 }
872 
873 /// Table of string intrinsic names indexed by enum value.
874 static const char * const IntrinsicNameTable[] = {
875   "not_intrinsic",
876 #define GET_INTRINSIC_NAME_TABLE
877 #include "llvm/IR/IntrinsicImpl.inc"
878 #undef GET_INTRINSIC_NAME_TABLE
879 };
880 
881 /// Table of per-target intrinsic name tables.
882 #define GET_INTRINSIC_TARGET_DATA
883 #include "llvm/IR/IntrinsicImpl.inc"
884 #undef GET_INTRINSIC_TARGET_DATA
885 
886 bool Function::isTargetIntrinsic(Intrinsic::ID IID) {
887   return IID > TargetInfos[0].Count;
888 }
889 
890 bool Function::isTargetIntrinsic() const {
891   return isTargetIntrinsic(IntID);
892 }
893 
894 /// Find the segment of \c IntrinsicNameTable for intrinsics with the same
895 /// target as \c Name, or the generic table if \c Name is not target specific.
896 ///
897 /// Returns the relevant slice of \c IntrinsicNameTable
898 static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
899   assert(Name.starts_with("llvm."));
900 
901   ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
902   // Drop "llvm." and take the first dotted component. That will be the target
903   // if this is target specific.
904   StringRef Target = Name.drop_front(5).split('.').first;
905   auto It = partition_point(
906       Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; });
907   // We've either found the target or just fall back to the generic set, which
908   // is always first.
909   const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
910   return ArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
911 }
912 
913 /// This does the actual lookup of an intrinsic ID which
914 /// matches the given function name.
915 Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
916   ArrayRef<const char *> NameTable = findTargetSubtable(Name);
917   int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
918   if (Idx == -1)
919     return Intrinsic::not_intrinsic;
920 
921   // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
922   // an index into a sub-table.
923   int Adjust = NameTable.data() - IntrinsicNameTable;
924   Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
925 
926   // If the intrinsic is not overloaded, require an exact match. If it is
927   // overloaded, require either exact or prefix match.
928   const auto MatchSize = strlen(NameTable[Idx]);
929   assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
930   bool IsExactMatch = Name.size() == MatchSize;
931   return IsExactMatch || Intrinsic::isOverloaded(ID) ? ID
932                                                      : Intrinsic::not_intrinsic;
933 }
934 
935 void Function::updateAfterNameChange() {
936   LibFuncCache = UnknownLibFunc;
937   StringRef Name = getName();
938   if (!Name.starts_with("llvm.")) {
939     HasLLVMReservedName = false;
940     IntID = Intrinsic::not_intrinsic;
941     return;
942   }
943   HasLLVMReservedName = true;
944   IntID = lookupIntrinsicID(Name);
945 }
946 
947 /// Returns a stable mangling for the type specified for use in the name
948 /// mangling scheme used by 'any' types in intrinsic signatures.  The mangling
949 /// of named types is simply their name.  Manglings for unnamed types consist
950 /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
951 /// combined with the mangling of their component types.  A vararg function
952 /// type will have a suffix of 'vararg'.  Since function types can contain
953 /// other function types, we close a function type mangling with suffix 'f'
954 /// which can't be confused with it's prefix.  This ensures we don't have
955 /// collisions between two unrelated function types. Otherwise, you might
956 /// parse ffXX as f(fXX) or f(fX)X.  (X is a placeholder for any other type.)
957 /// The HasUnnamedType boolean is set if an unnamed type was encountered,
958 /// indicating that extra care must be taken to ensure a unique name.
959 static std::string getMangledTypeStr(Type *Ty, bool &HasUnnamedType) {
960   std::string Result;
961   if (PointerType *PTyp = dyn_cast<PointerType>(Ty)) {
962     Result += "p" + utostr(PTyp->getAddressSpace());
963   } else if (ArrayType *ATyp = dyn_cast<ArrayType>(Ty)) {
964     Result += "a" + utostr(ATyp->getNumElements()) +
965               getMangledTypeStr(ATyp->getElementType(), HasUnnamedType);
966   } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
967     if (!STyp->isLiteral()) {
968       Result += "s_";
969       if (STyp->hasName())
970         Result += STyp->getName();
971       else
972         HasUnnamedType = true;
973     } else {
974       Result += "sl_";
975       for (auto *Elem : STyp->elements())
976         Result += getMangledTypeStr(Elem, HasUnnamedType);
977     }
978     // Ensure nested structs are distinguishable.
979     Result += "s";
980   } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
981     Result += "f_" + getMangledTypeStr(FT->getReturnType(), HasUnnamedType);
982     for (size_t i = 0; i < FT->getNumParams(); i++)
983       Result += getMangledTypeStr(FT->getParamType(i), HasUnnamedType);
984     if (FT->isVarArg())
985       Result += "vararg";
986     // Ensure nested function types are distinguishable.
987     Result += "f";
988   } else if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
989     ElementCount EC = VTy->getElementCount();
990     if (EC.isScalable())
991       Result += "nx";
992     Result += "v" + utostr(EC.getKnownMinValue()) +
993               getMangledTypeStr(VTy->getElementType(), HasUnnamedType);
994   } else if (TargetExtType *TETy = dyn_cast<TargetExtType>(Ty)) {
995     Result += "t";
996     Result += TETy->getName();
997     for (Type *ParamTy : TETy->type_params())
998       Result += "_" + getMangledTypeStr(ParamTy, HasUnnamedType);
999     for (unsigned IntParam : TETy->int_params())
1000       Result += "_" + utostr(IntParam);
1001     // Ensure nested target extension types are distinguishable.
1002     Result += "t";
1003   } else if (Ty) {
1004     switch (Ty->getTypeID()) {
1005     default: llvm_unreachable("Unhandled type");
1006     case Type::VoidTyID:      Result += "isVoid";   break;
1007     case Type::MetadataTyID:  Result += "Metadata"; break;
1008     case Type::HalfTyID:      Result += "f16";      break;
1009     case Type::BFloatTyID:    Result += "bf16";     break;
1010     case Type::FloatTyID:     Result += "f32";      break;
1011     case Type::DoubleTyID:    Result += "f64";      break;
1012     case Type::X86_FP80TyID:  Result += "f80";      break;
1013     case Type::FP128TyID:     Result += "f128";     break;
1014     case Type::PPC_FP128TyID: Result += "ppcf128";  break;
1015     case Type::X86_MMXTyID:   Result += "x86mmx";   break;
1016     case Type::X86_AMXTyID:   Result += "x86amx";   break;
1017     case Type::IntegerTyID:
1018       Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
1019       break;
1020     }
1021   }
1022   return Result;
1023 }
1024 
1025 StringRef Intrinsic::getBaseName(ID id) {
1026   assert(id < num_intrinsics && "Invalid intrinsic ID!");
1027   return IntrinsicNameTable[id];
1028 }
1029 
1030 StringRef Intrinsic::getName(ID id) {
1031   assert(id < num_intrinsics && "Invalid intrinsic ID!");
1032   assert(!Intrinsic::isOverloaded(id) &&
1033          "This version of getName does not support overloading");
1034   return getBaseName(id);
1035 }
1036 
1037 static std::string getIntrinsicNameImpl(Intrinsic::ID Id, ArrayRef<Type *> Tys,
1038                                         Module *M, FunctionType *FT,
1039                                         bool EarlyModuleCheck) {
1040 
1041   assert(Id < Intrinsic::num_intrinsics && "Invalid intrinsic ID!");
1042   assert((Tys.empty() || Intrinsic::isOverloaded(Id)) &&
1043          "This version of getName is for overloaded intrinsics only");
1044   (void)EarlyModuleCheck;
1045   assert((!EarlyModuleCheck || M ||
1046           !any_of(Tys, [](Type *T) { return isa<PointerType>(T); })) &&
1047          "Intrinsic overloading on pointer types need to provide a Module");
1048   bool HasUnnamedType = false;
1049   std::string Result(Intrinsic::getBaseName(Id));
1050   for (Type *Ty : Tys)
1051     Result += "." + getMangledTypeStr(Ty, HasUnnamedType);
1052   if (HasUnnamedType) {
1053     assert(M && "unnamed types need a module");
1054     if (!FT)
1055       FT = Intrinsic::getType(M->getContext(), Id, Tys);
1056     else
1057       assert((FT == Intrinsic::getType(M->getContext(), Id, Tys)) &&
1058              "Provided FunctionType must match arguments");
1059     return M->getUniqueIntrinsicName(Result, Id, FT);
1060   }
1061   return Result;
1062 }
1063 
1064 std::string Intrinsic::getName(ID Id, ArrayRef<Type *> Tys, Module *M,
1065                                FunctionType *FT) {
1066   assert(M && "We need to have a Module");
1067   return getIntrinsicNameImpl(Id, Tys, M, FT, true);
1068 }
1069 
1070 std::string Intrinsic::getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> Tys) {
1071   return getIntrinsicNameImpl(Id, Tys, nullptr, nullptr, false);
1072 }
1073 
1074 /// IIT_Info - These are enumerators that describe the entries returned by the
1075 /// getIntrinsicInfoTableEntries function.
1076 ///
1077 /// Defined in Intrinsics.td.
1078 enum IIT_Info {
1079 #define GET_INTRINSIC_IITINFO
1080 #include "llvm/IR/IntrinsicImpl.inc"
1081 #undef GET_INTRINSIC_IITINFO
1082 };
1083 
1084 static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
1085                       IIT_Info LastInfo,
1086                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
1087   using namespace Intrinsic;
1088 
1089   bool IsScalableVector = (LastInfo == IIT_SCALABLE_VEC);
1090 
1091   IIT_Info Info = IIT_Info(Infos[NextElt++]);
1092   unsigned StructElts = 2;
1093 
1094   switch (Info) {
1095   case IIT_Done:
1096     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
1097     return;
1098   case IIT_VARARG:
1099     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
1100     return;
1101   case IIT_MMX:
1102     OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
1103     return;
1104   case IIT_AMX:
1105     OutputTable.push_back(IITDescriptor::get(IITDescriptor::AMX, 0));
1106     return;
1107   case IIT_TOKEN:
1108     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
1109     return;
1110   case IIT_METADATA:
1111     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
1112     return;
1113   case IIT_F16:
1114     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
1115     return;
1116   case IIT_BF16:
1117     OutputTable.push_back(IITDescriptor::get(IITDescriptor::BFloat, 0));
1118     return;
1119   case IIT_F32:
1120     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
1121     return;
1122   case IIT_F64:
1123     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
1124     return;
1125   case IIT_F128:
1126     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0));
1127     return;
1128   case IIT_PPCF128:
1129     OutputTable.push_back(IITDescriptor::get(IITDescriptor::PPCQuad, 0));
1130     return;
1131   case IIT_I1:
1132     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
1133     return;
1134   case IIT_I2:
1135     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 2));
1136     return;
1137   case IIT_I4:
1138     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 4));
1139     return;
1140   case IIT_AARCH64_SVCOUNT:
1141     OutputTable.push_back(IITDescriptor::get(IITDescriptor::AArch64Svcount, 0));
1142     return;
1143   case IIT_I8:
1144     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
1145     return;
1146   case IIT_I16:
1147     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
1148     return;
1149   case IIT_I32:
1150     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
1151     return;
1152   case IIT_I64:
1153     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
1154     return;
1155   case IIT_I128:
1156     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
1157     return;
1158   case IIT_V1:
1159     OutputTable.push_back(IITDescriptor::getVector(1, IsScalableVector));
1160     DecodeIITType(NextElt, Infos, Info, OutputTable);
1161     return;
1162   case IIT_V2:
1163     OutputTable.push_back(IITDescriptor::getVector(2, IsScalableVector));
1164     DecodeIITType(NextElt, Infos, Info, OutputTable);
1165     return;
1166   case IIT_V3:
1167     OutputTable.push_back(IITDescriptor::getVector(3, IsScalableVector));
1168     DecodeIITType(NextElt, Infos, Info, OutputTable);
1169     return;
1170   case IIT_V4:
1171     OutputTable.push_back(IITDescriptor::getVector(4, IsScalableVector));
1172     DecodeIITType(NextElt, Infos, Info, OutputTable);
1173     return;
1174   case IIT_V6:
1175     OutputTable.push_back(IITDescriptor::getVector(6, IsScalableVector));
1176     DecodeIITType(NextElt, Infos, Info, OutputTable);
1177     return;
1178   case IIT_V8:
1179     OutputTable.push_back(IITDescriptor::getVector(8, IsScalableVector));
1180     DecodeIITType(NextElt, Infos, Info, OutputTable);
1181     return;
1182   case IIT_V10:
1183     OutputTable.push_back(IITDescriptor::getVector(10, IsScalableVector));
1184     DecodeIITType(NextElt, Infos, Info, OutputTable);
1185     return;
1186   case IIT_V16:
1187     OutputTable.push_back(IITDescriptor::getVector(16, IsScalableVector));
1188     DecodeIITType(NextElt, Infos, Info, OutputTable);
1189     return;
1190   case IIT_V32:
1191     OutputTable.push_back(IITDescriptor::getVector(32, IsScalableVector));
1192     DecodeIITType(NextElt, Infos, Info, OutputTable);
1193     return;
1194   case IIT_V64:
1195     OutputTable.push_back(IITDescriptor::getVector(64, IsScalableVector));
1196     DecodeIITType(NextElt, Infos, Info, OutputTable);
1197     return;
1198   case IIT_V128:
1199     OutputTable.push_back(IITDescriptor::getVector(128, IsScalableVector));
1200     DecodeIITType(NextElt, Infos, Info, OutputTable);
1201     return;
1202   case IIT_V256:
1203     OutputTable.push_back(IITDescriptor::getVector(256, IsScalableVector));
1204     DecodeIITType(NextElt, Infos, Info, OutputTable);
1205     return;
1206   case IIT_V512:
1207     OutputTable.push_back(IITDescriptor::getVector(512, IsScalableVector));
1208     DecodeIITType(NextElt, Infos, Info, OutputTable);
1209     return;
1210   case IIT_V1024:
1211     OutputTable.push_back(IITDescriptor::getVector(1024, IsScalableVector));
1212     DecodeIITType(NextElt, Infos, Info, OutputTable);
1213     return;
1214   case IIT_EXTERNREF:
1215     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 10));
1216     return;
1217   case IIT_FUNCREF:
1218     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 20));
1219     return;
1220   case IIT_PTR:
1221     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
1222     return;
1223   case IIT_ANYPTR: // [ANYPTR addrspace]
1224     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
1225                                              Infos[NextElt++]));
1226     return;
1227   case IIT_ARG: {
1228     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1229     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
1230     return;
1231   }
1232   case IIT_EXTEND_ARG: {
1233     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1234     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
1235                                              ArgInfo));
1236     return;
1237   }
1238   case IIT_TRUNC_ARG: {
1239     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1240     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
1241                                              ArgInfo));
1242     return;
1243   }
1244   case IIT_HALF_VEC_ARG: {
1245     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1246     OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
1247                                              ArgInfo));
1248     return;
1249   }
1250   case IIT_SAME_VEC_WIDTH_ARG: {
1251     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1252     OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
1253                                              ArgInfo));
1254     return;
1255   }
1256   case IIT_VEC_OF_ANYPTRS_TO_ELT: {
1257     unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1258     unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1259     OutputTable.push_back(
1260         IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo));
1261     return;
1262   }
1263   case IIT_EMPTYSTRUCT:
1264     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
1265     return;
1266   case IIT_STRUCT9: ++StructElts; [[fallthrough]];
1267   case IIT_STRUCT8: ++StructElts; [[fallthrough]];
1268   case IIT_STRUCT7: ++StructElts; [[fallthrough]];
1269   case IIT_STRUCT6: ++StructElts; [[fallthrough]];
1270   case IIT_STRUCT5: ++StructElts; [[fallthrough]];
1271   case IIT_STRUCT4: ++StructElts; [[fallthrough]];
1272   case IIT_STRUCT3: ++StructElts; [[fallthrough]];
1273   case IIT_STRUCT2: {
1274     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
1275 
1276     for (unsigned i = 0; i != StructElts; ++i)
1277       DecodeIITType(NextElt, Infos, Info, OutputTable);
1278     return;
1279   }
1280   case IIT_SUBDIVIDE2_ARG: {
1281     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1282     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide2Argument,
1283                                              ArgInfo));
1284     return;
1285   }
1286   case IIT_SUBDIVIDE4_ARG: {
1287     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1288     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide4Argument,
1289                                              ArgInfo));
1290     return;
1291   }
1292   case IIT_VEC_ELEMENT: {
1293     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1294     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecElementArgument,
1295                                              ArgInfo));
1296     return;
1297   }
1298   case IIT_SCALABLE_VEC: {
1299     DecodeIITType(NextElt, Infos, Info, OutputTable);
1300     return;
1301   }
1302   case IIT_VEC_OF_BITCASTS_TO_INT: {
1303     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1304     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfBitcastsToInt,
1305                                              ArgInfo));
1306     return;
1307   }
1308   }
1309   llvm_unreachable("unhandled");
1310 }
1311 
1312 #define GET_INTRINSIC_GENERATOR_GLOBAL
1313 #include "llvm/IR/IntrinsicImpl.inc"
1314 #undef GET_INTRINSIC_GENERATOR_GLOBAL
1315 
1316 void Intrinsic::getIntrinsicInfoTableEntries(ID id,
1317                                              SmallVectorImpl<IITDescriptor> &T){
1318   // Check to see if the intrinsic's type was expressible by the table.
1319   unsigned TableVal = IIT_Table[id-1];
1320 
1321   // Decode the TableVal into an array of IITValues.
1322   SmallVector<unsigned char, 8> IITValues;
1323   ArrayRef<unsigned char> IITEntries;
1324   unsigned NextElt = 0;
1325   if ((TableVal >> 31) != 0) {
1326     // This is an offset into the IIT_LongEncodingTable.
1327     IITEntries = IIT_LongEncodingTable;
1328 
1329     // Strip sentinel bit.
1330     NextElt = (TableVal << 1) >> 1;
1331   } else {
1332     // Decode the TableVal into an array of IITValues.  If the entry was encoded
1333     // into a single word in the table itself, decode it now.
1334     do {
1335       IITValues.push_back(TableVal & 0xF);
1336       TableVal >>= 4;
1337     } while (TableVal);
1338 
1339     IITEntries = IITValues;
1340     NextElt = 0;
1341   }
1342 
1343   // Okay, decode the table into the output vector of IITDescriptors.
1344   DecodeIITType(NextElt, IITEntries, IIT_Done, T);
1345   while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
1346     DecodeIITType(NextElt, IITEntries, IIT_Done, T);
1347 }
1348 
1349 static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
1350                              ArrayRef<Type*> Tys, LLVMContext &Context) {
1351   using namespace Intrinsic;
1352 
1353   IITDescriptor D = Infos.front();
1354   Infos = Infos.slice(1);
1355 
1356   switch (D.Kind) {
1357   case IITDescriptor::Void: return Type::getVoidTy(Context);
1358   case IITDescriptor::VarArg: return Type::getVoidTy(Context);
1359   case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
1360   case IITDescriptor::AMX: return Type::getX86_AMXTy(Context);
1361   case IITDescriptor::Token: return Type::getTokenTy(Context);
1362   case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
1363   case IITDescriptor::Half: return Type::getHalfTy(Context);
1364   case IITDescriptor::BFloat: return Type::getBFloatTy(Context);
1365   case IITDescriptor::Float: return Type::getFloatTy(Context);
1366   case IITDescriptor::Double: return Type::getDoubleTy(Context);
1367   case IITDescriptor::Quad: return Type::getFP128Ty(Context);
1368   case IITDescriptor::PPCQuad: return Type::getPPC_FP128Ty(Context);
1369   case IITDescriptor::AArch64Svcount:
1370     return TargetExtType::get(Context, "aarch64.svcount");
1371 
1372   case IITDescriptor::Integer:
1373     return IntegerType::get(Context, D.Integer_Width);
1374   case IITDescriptor::Vector:
1375     return VectorType::get(DecodeFixedType(Infos, Tys, Context),
1376                            D.Vector_Width);
1377   case IITDescriptor::Pointer:
1378     return PointerType::get(Context, D.Pointer_AddressSpace);
1379   case IITDescriptor::Struct: {
1380     SmallVector<Type *, 8> Elts;
1381     for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1382       Elts.push_back(DecodeFixedType(Infos, Tys, Context));
1383     return StructType::get(Context, Elts);
1384   }
1385   case IITDescriptor::Argument:
1386     return Tys[D.getArgumentNumber()];
1387   case IITDescriptor::ExtendArgument: {
1388     Type *Ty = Tys[D.getArgumentNumber()];
1389     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1390       return VectorType::getExtendedElementVectorType(VTy);
1391 
1392     return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
1393   }
1394   case IITDescriptor::TruncArgument: {
1395     Type *Ty = Tys[D.getArgumentNumber()];
1396     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1397       return VectorType::getTruncatedElementVectorType(VTy);
1398 
1399     IntegerType *ITy = cast<IntegerType>(Ty);
1400     assert(ITy->getBitWidth() % 2 == 0);
1401     return IntegerType::get(Context, ITy->getBitWidth() / 2);
1402   }
1403   case IITDescriptor::Subdivide2Argument:
1404   case IITDescriptor::Subdivide4Argument: {
1405     Type *Ty = Tys[D.getArgumentNumber()];
1406     VectorType *VTy = dyn_cast<VectorType>(Ty);
1407     assert(VTy && "Expected an argument of Vector Type");
1408     int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
1409     return VectorType::getSubdividedVectorType(VTy, SubDivs);
1410   }
1411   case IITDescriptor::HalfVecArgument:
1412     return VectorType::getHalfElementsVectorType(cast<VectorType>(
1413                                                   Tys[D.getArgumentNumber()]));
1414   case IITDescriptor::SameVecWidthArgument: {
1415     Type *EltTy = DecodeFixedType(Infos, Tys, Context);
1416     Type *Ty = Tys[D.getArgumentNumber()];
1417     if (auto *VTy = dyn_cast<VectorType>(Ty))
1418       return VectorType::get(EltTy, VTy->getElementCount());
1419     return EltTy;
1420   }
1421   case IITDescriptor::VecElementArgument: {
1422     Type *Ty = Tys[D.getArgumentNumber()];
1423     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1424       return VTy->getElementType();
1425     llvm_unreachable("Expected an argument of Vector Type");
1426   }
1427   case IITDescriptor::VecOfBitcastsToInt: {
1428     Type *Ty = Tys[D.getArgumentNumber()];
1429     VectorType *VTy = dyn_cast<VectorType>(Ty);
1430     assert(VTy && "Expected an argument of Vector Type");
1431     return VectorType::getInteger(VTy);
1432   }
1433   case IITDescriptor::VecOfAnyPtrsToElt:
1434     // Return the overloaded type (which determines the pointers address space)
1435     return Tys[D.getOverloadArgNumber()];
1436   }
1437   llvm_unreachable("unhandled");
1438 }
1439 
1440 FunctionType *Intrinsic::getType(LLVMContext &Context,
1441                                  ID id, ArrayRef<Type*> Tys) {
1442   SmallVector<IITDescriptor, 8> Table;
1443   getIntrinsicInfoTableEntries(id, Table);
1444 
1445   ArrayRef<IITDescriptor> TableRef = Table;
1446   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
1447 
1448   SmallVector<Type*, 8> ArgTys;
1449   while (!TableRef.empty())
1450     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
1451 
1452   // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
1453   // If we see void type as the type of the last argument, it is vararg intrinsic
1454   if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
1455     ArgTys.pop_back();
1456     return FunctionType::get(ResultTy, ArgTys, true);
1457   }
1458   return FunctionType::get(ResultTy, ArgTys, false);
1459 }
1460 
1461 bool Intrinsic::isOverloaded(ID id) {
1462 #define GET_INTRINSIC_OVERLOAD_TABLE
1463 #include "llvm/IR/IntrinsicImpl.inc"
1464 #undef GET_INTRINSIC_OVERLOAD_TABLE
1465 }
1466 
1467 /// This defines the "Intrinsic::getAttributes(ID id)" method.
1468 #define GET_INTRINSIC_ATTRIBUTES
1469 #include "llvm/IR/IntrinsicImpl.inc"
1470 #undef GET_INTRINSIC_ATTRIBUTES
1471 
1472 Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
1473   // There can never be multiple globals with the same name of different types,
1474   // because intrinsics must be a specific type.
1475   auto *FT = getType(M->getContext(), id, Tys);
1476   return cast<Function>(
1477       M->getOrInsertFunction(
1478            Tys.empty() ? getName(id) : getName(id, Tys, M, FT), FT)
1479           .getCallee());
1480 }
1481 
1482 // This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method.
1483 #define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
1484 #include "llvm/IR/IntrinsicImpl.inc"
1485 #undef GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
1486 
1487 // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
1488 #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
1489 #include "llvm/IR/IntrinsicImpl.inc"
1490 #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
1491 
1492 bool Intrinsic::isConstrainedFPIntrinsic(ID QID) {
1493   switch (QID) {
1494 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
1495   case Intrinsic::INTRINSIC:
1496 #include "llvm/IR/ConstrainedOps.def"
1497 #undef INSTRUCTION
1498     return true;
1499   default:
1500     return false;
1501   }
1502 }
1503 
1504 bool Intrinsic::hasConstrainedFPRoundingModeOperand(Intrinsic::ID QID) {
1505   switch (QID) {
1506 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
1507   case Intrinsic::INTRINSIC:                                                   \
1508     return ROUND_MODE == 1;
1509 #include "llvm/IR/ConstrainedOps.def"
1510 #undef INSTRUCTION
1511   default:
1512     return false;
1513   }
1514 }
1515 
1516 using DeferredIntrinsicMatchPair =
1517     std::pair<Type *, ArrayRef<Intrinsic::IITDescriptor>>;
1518 
1519 static bool matchIntrinsicType(
1520     Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
1521     SmallVectorImpl<Type *> &ArgTys,
1522     SmallVectorImpl<DeferredIntrinsicMatchPair> &DeferredChecks,
1523     bool IsDeferredCheck) {
1524   using namespace Intrinsic;
1525 
1526   // If we ran out of descriptors, there are too many arguments.
1527   if (Infos.empty()) return true;
1528 
1529   // Do this before slicing off the 'front' part
1530   auto InfosRef = Infos;
1531   auto DeferCheck = [&DeferredChecks, &InfosRef](Type *T) {
1532     DeferredChecks.emplace_back(T, InfosRef);
1533     return false;
1534   };
1535 
1536   IITDescriptor D = Infos.front();
1537   Infos = Infos.slice(1);
1538 
1539   switch (D.Kind) {
1540     case IITDescriptor::Void: return !Ty->isVoidTy();
1541     case IITDescriptor::VarArg: return true;
1542     case IITDescriptor::MMX:  return !Ty->isX86_MMXTy();
1543     case IITDescriptor::AMX:  return !Ty->isX86_AMXTy();
1544     case IITDescriptor::Token: return !Ty->isTokenTy();
1545     case IITDescriptor::Metadata: return !Ty->isMetadataTy();
1546     case IITDescriptor::Half: return !Ty->isHalfTy();
1547     case IITDescriptor::BFloat: return !Ty->isBFloatTy();
1548     case IITDescriptor::Float: return !Ty->isFloatTy();
1549     case IITDescriptor::Double: return !Ty->isDoubleTy();
1550     case IITDescriptor::Quad: return !Ty->isFP128Ty();
1551     case IITDescriptor::PPCQuad: return !Ty->isPPC_FP128Ty();
1552     case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1553     case IITDescriptor::AArch64Svcount:
1554       return !isa<TargetExtType>(Ty) ||
1555              cast<TargetExtType>(Ty)->getName() != "aarch64.svcount";
1556     case IITDescriptor::Vector: {
1557       VectorType *VT = dyn_cast<VectorType>(Ty);
1558       return !VT || VT->getElementCount() != D.Vector_Width ||
1559              matchIntrinsicType(VT->getElementType(), Infos, ArgTys,
1560                                 DeferredChecks, IsDeferredCheck);
1561     }
1562     case IITDescriptor::Pointer: {
1563       PointerType *PT = dyn_cast<PointerType>(Ty);
1564       return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace;
1565     }
1566 
1567     case IITDescriptor::Struct: {
1568       StructType *ST = dyn_cast<StructType>(Ty);
1569       if (!ST || !ST->isLiteral() || ST->isPacked() ||
1570           ST->getNumElements() != D.Struct_NumElements)
1571         return true;
1572 
1573       for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1574         if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys,
1575                                DeferredChecks, IsDeferredCheck))
1576           return true;
1577       return false;
1578     }
1579 
1580     case IITDescriptor::Argument:
1581       // If this is the second occurrence of an argument,
1582       // verify that the later instance matches the previous instance.
1583       if (D.getArgumentNumber() < ArgTys.size())
1584         return Ty != ArgTys[D.getArgumentNumber()];
1585 
1586       if (D.getArgumentNumber() > ArgTys.size() ||
1587           D.getArgumentKind() == IITDescriptor::AK_MatchType)
1588         return IsDeferredCheck || DeferCheck(Ty);
1589 
1590       assert(D.getArgumentNumber() == ArgTys.size() && !IsDeferredCheck &&
1591              "Table consistency error");
1592       ArgTys.push_back(Ty);
1593 
1594       switch (D.getArgumentKind()) {
1595         case IITDescriptor::AK_Any:        return false; // Success
1596         case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
1597         case IITDescriptor::AK_AnyFloat:   return !Ty->isFPOrFPVectorTy();
1598         case IITDescriptor::AK_AnyVector:  return !isa<VectorType>(Ty);
1599         case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
1600         default:                           break;
1601       }
1602       llvm_unreachable("all argument kinds not covered");
1603 
1604     case IITDescriptor::ExtendArgument: {
1605       // If this is a forward reference, defer the check for later.
1606       if (D.getArgumentNumber() >= ArgTys.size())
1607         return IsDeferredCheck || DeferCheck(Ty);
1608 
1609       Type *NewTy = ArgTys[D.getArgumentNumber()];
1610       if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1611         NewTy = VectorType::getExtendedElementVectorType(VTy);
1612       else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1613         NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
1614       else
1615         return true;
1616 
1617       return Ty != NewTy;
1618     }
1619     case IITDescriptor::TruncArgument: {
1620       // If this is a forward reference, defer the check for later.
1621       if (D.getArgumentNumber() >= ArgTys.size())
1622         return IsDeferredCheck || DeferCheck(Ty);
1623 
1624       Type *NewTy = ArgTys[D.getArgumentNumber()];
1625       if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1626         NewTy = VectorType::getTruncatedElementVectorType(VTy);
1627       else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1628         NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
1629       else
1630         return true;
1631 
1632       return Ty != NewTy;
1633     }
1634     case IITDescriptor::HalfVecArgument:
1635       // If this is a forward reference, defer the check for later.
1636       if (D.getArgumentNumber() >= ArgTys.size())
1637         return IsDeferredCheck || DeferCheck(Ty);
1638       return !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1639              VectorType::getHalfElementsVectorType(
1640                      cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1641     case IITDescriptor::SameVecWidthArgument: {
1642       if (D.getArgumentNumber() >= ArgTys.size()) {
1643         // Defer check and subsequent check for the vector element type.
1644         Infos = Infos.slice(1);
1645         return IsDeferredCheck || DeferCheck(Ty);
1646       }
1647       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
1648       auto *ThisArgType = dyn_cast<VectorType>(Ty);
1649       // Both must be vectors of the same number of elements or neither.
1650       if ((ReferenceType != nullptr) != (ThisArgType != nullptr))
1651         return true;
1652       Type *EltTy = Ty;
1653       if (ThisArgType) {
1654         if (ReferenceType->getElementCount() !=
1655             ThisArgType->getElementCount())
1656           return true;
1657         EltTy = ThisArgType->getElementType();
1658       }
1659       return matchIntrinsicType(EltTy, Infos, ArgTys, DeferredChecks,
1660                                 IsDeferredCheck);
1661     }
1662     case IITDescriptor::VecOfAnyPtrsToElt: {
1663       unsigned RefArgNumber = D.getRefArgNumber();
1664       if (RefArgNumber >= ArgTys.size()) {
1665         if (IsDeferredCheck)
1666           return true;
1667         // If forward referencing, already add the pointer-vector type and
1668         // defer the checks for later.
1669         ArgTys.push_back(Ty);
1670         return DeferCheck(Ty);
1671       }
1672 
1673       if (!IsDeferredCheck){
1674         assert(D.getOverloadArgNumber() == ArgTys.size() &&
1675                "Table consistency error");
1676         ArgTys.push_back(Ty);
1677       }
1678 
1679       // Verify the overloaded type "matches" the Ref type.
1680       // i.e. Ty is a vector with the same width as Ref.
1681       // Composed of pointers to the same element type as Ref.
1682       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
1683       auto *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1684       if (!ThisArgVecTy || !ReferenceType ||
1685           (ReferenceType->getElementCount() != ThisArgVecTy->getElementCount()))
1686         return true;
1687       return !ThisArgVecTy->getElementType()->isPointerTy();
1688     }
1689     case IITDescriptor::VecElementArgument: {
1690       if (D.getArgumentNumber() >= ArgTys.size())
1691         return IsDeferredCheck ? true : DeferCheck(Ty);
1692       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
1693       return !ReferenceType || Ty != ReferenceType->getElementType();
1694     }
1695     case IITDescriptor::Subdivide2Argument:
1696     case IITDescriptor::Subdivide4Argument: {
1697       // If this is a forward reference, defer the check for later.
1698       if (D.getArgumentNumber() >= ArgTys.size())
1699         return IsDeferredCheck || DeferCheck(Ty);
1700 
1701       Type *NewTy = ArgTys[D.getArgumentNumber()];
1702       if (auto *VTy = dyn_cast<VectorType>(NewTy)) {
1703         int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
1704         NewTy = VectorType::getSubdividedVectorType(VTy, SubDivs);
1705         return Ty != NewTy;
1706       }
1707       return true;
1708     }
1709     case IITDescriptor::VecOfBitcastsToInt: {
1710       if (D.getArgumentNumber() >= ArgTys.size())
1711         return IsDeferredCheck || DeferCheck(Ty);
1712       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
1713       auto *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1714       if (!ThisArgVecTy || !ReferenceType)
1715         return true;
1716       return ThisArgVecTy != VectorType::getInteger(ReferenceType);
1717     }
1718   }
1719   llvm_unreachable("unhandled");
1720 }
1721 
1722 Intrinsic::MatchIntrinsicTypesResult
1723 Intrinsic::matchIntrinsicSignature(FunctionType *FTy,
1724                                    ArrayRef<Intrinsic::IITDescriptor> &Infos,
1725                                    SmallVectorImpl<Type *> &ArgTys) {
1726   SmallVector<DeferredIntrinsicMatchPair, 2> DeferredChecks;
1727   if (matchIntrinsicType(FTy->getReturnType(), Infos, ArgTys, DeferredChecks,
1728                          false))
1729     return MatchIntrinsicTypes_NoMatchRet;
1730 
1731   unsigned NumDeferredReturnChecks = DeferredChecks.size();
1732 
1733   for (auto *Ty : FTy->params())
1734     if (matchIntrinsicType(Ty, Infos, ArgTys, DeferredChecks, false))
1735       return MatchIntrinsicTypes_NoMatchArg;
1736 
1737   for (unsigned I = 0, E = DeferredChecks.size(); I != E; ++I) {
1738     DeferredIntrinsicMatchPair &Check = DeferredChecks[I];
1739     if (matchIntrinsicType(Check.first, Check.second, ArgTys, DeferredChecks,
1740                            true))
1741       return I < NumDeferredReturnChecks ? MatchIntrinsicTypes_NoMatchRet
1742                                          : MatchIntrinsicTypes_NoMatchArg;
1743   }
1744 
1745   return MatchIntrinsicTypes_Match;
1746 }
1747 
1748 bool
1749 Intrinsic::matchIntrinsicVarArg(bool isVarArg,
1750                                 ArrayRef<Intrinsic::IITDescriptor> &Infos) {
1751   // If there are no descriptors left, then it can't be a vararg.
1752   if (Infos.empty())
1753     return isVarArg;
1754 
1755   // There should be only one descriptor remaining at this point.
1756   if (Infos.size() != 1)
1757     return true;
1758 
1759   // Check and verify the descriptor.
1760   IITDescriptor D = Infos.front();
1761   Infos = Infos.slice(1);
1762   if (D.Kind == IITDescriptor::VarArg)
1763     return !isVarArg;
1764 
1765   return true;
1766 }
1767 
1768 bool Intrinsic::getIntrinsicSignature(Intrinsic::ID ID, FunctionType *FT,
1769                                       SmallVectorImpl<Type *> &ArgTys) {
1770   if (!ID)
1771     return false;
1772 
1773   SmallVector<Intrinsic::IITDescriptor, 8> Table;
1774   getIntrinsicInfoTableEntries(ID, Table);
1775   ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
1776 
1777   if (Intrinsic::matchIntrinsicSignature(FT, TableRef, ArgTys) !=
1778       Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
1779     return false;
1780   }
1781   if (Intrinsic::matchIntrinsicVarArg(FT->isVarArg(), TableRef))
1782     return false;
1783   return true;
1784 }
1785 
1786 bool Intrinsic::getIntrinsicSignature(Function *F,
1787                                       SmallVectorImpl<Type *> &ArgTys) {
1788   return getIntrinsicSignature(F->getIntrinsicID(), F->getFunctionType(),
1789                                ArgTys);
1790 }
1791 
1792 std::optional<Function *> Intrinsic::remangleIntrinsicFunction(Function *F) {
1793   SmallVector<Type *, 4> ArgTys;
1794   if (!getIntrinsicSignature(F, ArgTys))
1795     return std::nullopt;
1796 
1797   Intrinsic::ID ID = F->getIntrinsicID();
1798   StringRef Name = F->getName();
1799   std::string WantedName =
1800       Intrinsic::getName(ID, ArgTys, F->getParent(), F->getFunctionType());
1801   if (Name == WantedName)
1802     return std::nullopt;
1803 
1804   Function *NewDecl = [&] {
1805     if (auto *ExistingGV = F->getParent()->getNamedValue(WantedName)) {
1806       if (auto *ExistingF = dyn_cast<Function>(ExistingGV))
1807         if (ExistingF->getFunctionType() == F->getFunctionType())
1808           return ExistingF;
1809 
1810       // The name already exists, but is not a function or has the wrong
1811       // prototype. Make place for the new one by renaming the old version.
1812       // Either this old version will be removed later on or the module is
1813       // invalid and we'll get an error.
1814       ExistingGV->setName(WantedName + ".renamed");
1815     }
1816     return Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1817   }();
1818 
1819   NewDecl->setCallingConv(F->getCallingConv());
1820   assert(NewDecl->getFunctionType() == F->getFunctionType() &&
1821          "Shouldn't change the signature");
1822   return NewDecl;
1823 }
1824 
1825 /// hasAddressTaken - returns true if there are any uses of this function
1826 /// other than direct calls or invokes to it. Optionally ignores callback
1827 /// uses, assume like pointer annotation calls, and references in llvm.used
1828 /// and llvm.compiler.used variables.
1829 bool Function::hasAddressTaken(const User **PutOffender,
1830                                bool IgnoreCallbackUses,
1831                                bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed,
1832                                bool IgnoreARCAttachedCall,
1833                                bool IgnoreCastedDirectCall) const {
1834   for (const Use &U : uses()) {
1835     const User *FU = U.getUser();
1836     if (isa<BlockAddress>(FU))
1837       continue;
1838 
1839     if (IgnoreCallbackUses) {
1840       AbstractCallSite ACS(&U);
1841       if (ACS && ACS.isCallbackCall())
1842         continue;
1843     }
1844 
1845     const auto *Call = dyn_cast<CallBase>(FU);
1846     if (!Call) {
1847       if (IgnoreAssumeLikeCalls &&
1848           isa<BitCastOperator, AddrSpaceCastOperator>(FU) &&
1849           all_of(FU->users(), [](const User *U) {
1850             if (const auto *I = dyn_cast<IntrinsicInst>(U))
1851               return I->isAssumeLikeIntrinsic();
1852             return false;
1853           })) {
1854         continue;
1855       }
1856 
1857       if (IgnoreLLVMUsed && !FU->user_empty()) {
1858         const User *FUU = FU;
1859         if (isa<BitCastOperator, AddrSpaceCastOperator>(FU) &&
1860             FU->hasOneUse() && !FU->user_begin()->user_empty())
1861           FUU = *FU->user_begin();
1862         if (llvm::all_of(FUU->users(), [](const User *U) {
1863               if (const auto *GV = dyn_cast<GlobalVariable>(U))
1864                 return GV->hasName() &&
1865                        (GV->getName() == "llvm.compiler.used" ||
1866                         GV->getName() == "llvm.used");
1867               return false;
1868             }))
1869           continue;
1870       }
1871       if (PutOffender)
1872         *PutOffender = FU;
1873       return true;
1874     }
1875 
1876     if (IgnoreAssumeLikeCalls) {
1877       if (const auto *I = dyn_cast<IntrinsicInst>(Call))
1878         if (I->isAssumeLikeIntrinsic())
1879           continue;
1880     }
1881 
1882     if (!Call->isCallee(&U) || (!IgnoreCastedDirectCall &&
1883                                 Call->getFunctionType() != getFunctionType())) {
1884       if (IgnoreARCAttachedCall &&
1885           Call->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall,
1886                                       U.getOperandNo()))
1887         continue;
1888 
1889       if (PutOffender)
1890         *PutOffender = FU;
1891       return true;
1892     }
1893   }
1894   return false;
1895 }
1896 
1897 bool Function::isDefTriviallyDead() const {
1898   // Check the linkage
1899   if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
1900       !hasAvailableExternallyLinkage())
1901     return false;
1902 
1903   // Check if the function is used by anything other than a blockaddress.
1904   for (const User *U : users())
1905     if (!isa<BlockAddress>(U))
1906       return false;
1907 
1908   return true;
1909 }
1910 
1911 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
1912 /// setjmp or other function that gcc recognizes as "returning twice".
1913 bool Function::callsFunctionThatReturnsTwice() const {
1914   for (const Instruction &I : instructions(this))
1915     if (const auto *Call = dyn_cast<CallBase>(&I))
1916       if (Call->hasFnAttr(Attribute::ReturnsTwice))
1917         return true;
1918 
1919   return false;
1920 }
1921 
1922 Constant *Function::getPersonalityFn() const {
1923   assert(hasPersonalityFn() && getNumOperands());
1924   return cast<Constant>(Op<0>());
1925 }
1926 
1927 void Function::setPersonalityFn(Constant *Fn) {
1928   setHungoffOperand<0>(Fn);
1929   setValueSubclassDataBit(3, Fn != nullptr);
1930 }
1931 
1932 Constant *Function::getPrefixData() const {
1933   assert(hasPrefixData() && getNumOperands());
1934   return cast<Constant>(Op<1>());
1935 }
1936 
1937 void Function::setPrefixData(Constant *PrefixData) {
1938   setHungoffOperand<1>(PrefixData);
1939   setValueSubclassDataBit(1, PrefixData != nullptr);
1940 }
1941 
1942 Constant *Function::getPrologueData() const {
1943   assert(hasPrologueData() && getNumOperands());
1944   return cast<Constant>(Op<2>());
1945 }
1946 
1947 void Function::setPrologueData(Constant *PrologueData) {
1948   setHungoffOperand<2>(PrologueData);
1949   setValueSubclassDataBit(2, PrologueData != nullptr);
1950 }
1951 
1952 void Function::allocHungoffUselist() {
1953   // If we've already allocated a uselist, stop here.
1954   if (getNumOperands())
1955     return;
1956 
1957   allocHungoffUses(3, /*IsPhi=*/ false);
1958   setNumHungOffUseOperands(3);
1959 
1960   // Initialize the uselist with placeholder operands to allow traversal.
1961   auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0));
1962   Op<0>().set(CPN);
1963   Op<1>().set(CPN);
1964   Op<2>().set(CPN);
1965 }
1966 
1967 template <int Idx>
1968 void Function::setHungoffOperand(Constant *C) {
1969   if (C) {
1970     allocHungoffUselist();
1971     Op<Idx>().set(C);
1972   } else if (getNumOperands()) {
1973     Op<Idx>().set(ConstantPointerNull::get(PointerType::get(getContext(), 0)));
1974   }
1975 }
1976 
1977 void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
1978   assert(Bit < 16 && "SubclassData contains only 16 bits");
1979   if (On)
1980     setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
1981   else
1982     setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
1983 }
1984 
1985 void Function::setEntryCount(ProfileCount Count,
1986                              const DenseSet<GlobalValue::GUID> *S) {
1987 #if !defined(NDEBUG)
1988   auto PrevCount = getEntryCount();
1989   assert(!PrevCount || PrevCount->getType() == Count.getType());
1990 #endif
1991 
1992   auto ImportGUIDs = getImportGUIDs();
1993   if (S == nullptr && ImportGUIDs.size())
1994     S = &ImportGUIDs;
1995 
1996   MDBuilder MDB(getContext());
1997   setMetadata(
1998       LLVMContext::MD_prof,
1999       MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S));
2000 }
2001 
2002 void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type,
2003                              const DenseSet<GlobalValue::GUID> *Imports) {
2004   setEntryCount(ProfileCount(Count, Type), Imports);
2005 }
2006 
2007 std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
2008   MDNode *MD = getMetadata(LLVMContext::MD_prof);
2009   if (MD && MD->getOperand(0))
2010     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
2011       if (MDS->getString() == "function_entry_count") {
2012         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
2013         uint64_t Count = CI->getValue().getZExtValue();
2014         // A value of -1 is used for SamplePGO when there were no samples.
2015         // Treat this the same as unknown.
2016         if (Count == (uint64_t)-1)
2017           return std::nullopt;
2018         return ProfileCount(Count, PCT_Real);
2019       } else if (AllowSynthetic &&
2020                  MDS->getString() == "synthetic_function_entry_count") {
2021         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
2022         uint64_t Count = CI->getValue().getZExtValue();
2023         return ProfileCount(Count, PCT_Synthetic);
2024       }
2025     }
2026   return std::nullopt;
2027 }
2028 
2029 DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
2030   DenseSet<GlobalValue::GUID> R;
2031   if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
2032     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
2033       if (MDS->getString() == "function_entry_count")
2034         for (unsigned i = 2; i < MD->getNumOperands(); i++)
2035           R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
2036                        ->getValue()
2037                        .getZExtValue());
2038   return R;
2039 }
2040 
2041 void Function::setSectionPrefix(StringRef Prefix) {
2042   MDBuilder MDB(getContext());
2043   setMetadata(LLVMContext::MD_section_prefix,
2044               MDB.createFunctionSectionPrefix(Prefix));
2045 }
2046 
2047 std::optional<StringRef> Function::getSectionPrefix() const {
2048   if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
2049     assert(cast<MDString>(MD->getOperand(0))->getString() ==
2050                "function_section_prefix" &&
2051            "Metadata not match");
2052     return cast<MDString>(MD->getOperand(1))->getString();
2053   }
2054   return std::nullopt;
2055 }
2056 
2057 bool Function::nullPointerIsDefined() const {
2058   return hasFnAttribute(Attribute::NullPointerIsValid);
2059 }
2060 
2061 bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
2062   if (F && F->nullPointerIsDefined())
2063     return true;
2064 
2065   if (AS != 0)
2066     return true;
2067 
2068   return false;
2069 }
2070