xref: /freebsd/contrib/llvm-project/llvm/lib/IR/Function.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- Function.cpp - Implement the Global object classes -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the Function class for the IR library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/IR/Function.h"
140b57cec5SDimitry Andric #include "SymbolTableListTraitsImpl.h"
150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
160b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
170b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
200b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
210b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
225ffd83dbSDimitry Andric #include "llvm/IR/AbstractCallSite.h"
230b57cec5SDimitry Andric #include "llvm/IR/Argument.h"
240b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
250b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
260b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
270b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
280b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
290b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
300b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h"
310b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
32fe6060f1SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
330b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
34480093f4SDimitry Andric #include "llvm/IR/IntrinsicsAArch64.h"
35480093f4SDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h"
36480093f4SDimitry Andric #include "llvm/IR/IntrinsicsARM.h"
37480093f4SDimitry Andric #include "llvm/IR/IntrinsicsBPF.h"
3881ad6265SDimitry Andric #include "llvm/IR/IntrinsicsDirectX.h"
39480093f4SDimitry Andric #include "llvm/IR/IntrinsicsHexagon.h"
40480093f4SDimitry Andric #include "llvm/IR/IntrinsicsMips.h"
41480093f4SDimitry Andric #include "llvm/IR/IntrinsicsNVPTX.h"
42480093f4SDimitry Andric #include "llvm/IR/IntrinsicsPowerPC.h"
43480093f4SDimitry Andric #include "llvm/IR/IntrinsicsR600.h"
44480093f4SDimitry Andric #include "llvm/IR/IntrinsicsRISCV.h"
45480093f4SDimitry Andric #include "llvm/IR/IntrinsicsS390.h"
46e8d8bef9SDimitry Andric #include "llvm/IR/IntrinsicsVE.h"
47480093f4SDimitry Andric #include "llvm/IR/IntrinsicsWebAssembly.h"
48480093f4SDimitry Andric #include "llvm/IR/IntrinsicsX86.h"
49480093f4SDimitry Andric #include "llvm/IR/IntrinsicsXCore.h"
500b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
510b57cec5SDimitry Andric #include "llvm/IR/MDBuilder.h"
520b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
530b57cec5SDimitry Andric #include "llvm/IR/Module.h"
54fe6060f1SDimitry Andric #include "llvm/IR/Operator.h"
550b57cec5SDimitry Andric #include "llvm/IR/SymbolTableListTraits.h"
560b57cec5SDimitry Andric #include "llvm/IR/Type.h"
570b57cec5SDimitry Andric #include "llvm/IR/Use.h"
580b57cec5SDimitry Andric #include "llvm/IR/User.h"
590b57cec5SDimitry Andric #include "llvm/IR/Value.h"
600b57cec5SDimitry Andric #include "llvm/IR/ValueSymbolTable.h"
610b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
62fe6060f1SDimitry Andric #include "llvm/Support/CommandLine.h"
630b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
640b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
65bdd1243dSDimitry Andric #include "llvm/Support/ModRef.h"
660b57cec5SDimitry Andric #include <cassert>
670b57cec5SDimitry Andric #include <cstddef>
680b57cec5SDimitry Andric #include <cstdint>
690b57cec5SDimitry Andric #include <cstring>
700b57cec5SDimitry Andric #include <string>
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric using namespace llvm;
730b57cec5SDimitry Andric using ProfileCount = Function::ProfileCount;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric // Explicit instantiations of SymbolTableListTraits since some of the methods
760b57cec5SDimitry Andric // are not in the public header file...
770b57cec5SDimitry Andric template class llvm::SymbolTableListTraits<BasicBlock>;
780b57cec5SDimitry Andric 
79fe6060f1SDimitry Andric static cl::opt<unsigned> NonGlobalValueMaxNameSize(
80fe6060f1SDimitry Andric     "non-global-value-max-name-size", cl::Hidden, cl::init(1024),
81fe6060f1SDimitry Andric     cl::desc("Maximum size for the name of non-global values."));
82fe6060f1SDimitry Andric 
830b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
840b57cec5SDimitry Andric // Argument Implementation
850b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
880b57cec5SDimitry Andric     : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
890b57cec5SDimitry Andric   setName(Name);
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric void Argument::setParent(Function *parent) {
930b57cec5SDimitry Andric   Parent = parent;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
96e8d8bef9SDimitry Andric bool Argument::hasNonNullAttr(bool AllowUndefOrPoison) const {
970b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
98e8d8bef9SDimitry Andric   if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull) &&
99e8d8bef9SDimitry Andric       (AllowUndefOrPoison ||
100e8d8bef9SDimitry Andric        getParent()->hasParamAttribute(getArgNo(), Attribute::NoUndef)))
1010b57cec5SDimitry Andric     return true;
1020b57cec5SDimitry Andric   else if (getDereferenceableBytes() > 0 &&
1030b57cec5SDimitry Andric            !NullPointerIsDefined(getParent(),
1040b57cec5SDimitry Andric                                  getType()->getPointerAddressSpace()))
1050b57cec5SDimitry Andric     return true;
1060b57cec5SDimitry Andric   return false;
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric bool Argument::hasByValAttr() const {
1100b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
1110b57cec5SDimitry Andric   return hasAttribute(Attribute::ByVal);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
114e8d8bef9SDimitry Andric bool Argument::hasByRefAttr() const {
115e8d8bef9SDimitry Andric   if (!getType()->isPointerTy())
116e8d8bef9SDimitry Andric     return false;
117e8d8bef9SDimitry Andric   return hasAttribute(Attribute::ByRef);
118e8d8bef9SDimitry Andric }
119e8d8bef9SDimitry Andric 
1200b57cec5SDimitry Andric bool Argument::hasSwiftSelfAttr() const {
1210b57cec5SDimitry Andric   return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric bool Argument::hasSwiftErrorAttr() const {
1250b57cec5SDimitry Andric   return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric bool Argument::hasInAllocaAttr() const {
1290b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
1300b57cec5SDimitry Andric   return hasAttribute(Attribute::InAlloca);
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
1335ffd83dbSDimitry Andric bool Argument::hasPreallocatedAttr() const {
1345ffd83dbSDimitry Andric   if (!getType()->isPointerTy())
1355ffd83dbSDimitry Andric     return false;
1365ffd83dbSDimitry Andric   return hasAttribute(Attribute::Preallocated);
1375ffd83dbSDimitry Andric }
1385ffd83dbSDimitry Andric 
139e8d8bef9SDimitry Andric bool Argument::hasPassPointeeByValueCopyAttr() const {
1400b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
1410b57cec5SDimitry Andric   AttributeList Attrs = getParent()->getAttributes();
142349cc55cSDimitry Andric   return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) ||
143349cc55cSDimitry Andric          Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) ||
144349cc55cSDimitry Andric          Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated);
1455ffd83dbSDimitry Andric }
1465ffd83dbSDimitry Andric 
147e8d8bef9SDimitry Andric bool Argument::hasPointeeInMemoryValueAttr() const {
148e8d8bef9SDimitry Andric   if (!getType()->isPointerTy())
149e8d8bef9SDimitry Andric     return false;
150e8d8bef9SDimitry Andric   AttributeList Attrs = getParent()->getAttributes();
151349cc55cSDimitry Andric   return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) ||
152349cc55cSDimitry Andric          Attrs.hasParamAttr(getArgNo(), Attribute::StructRet) ||
153349cc55cSDimitry Andric          Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) ||
154349cc55cSDimitry Andric          Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated) ||
155349cc55cSDimitry Andric          Attrs.hasParamAttr(getArgNo(), Attribute::ByRef);
156e8d8bef9SDimitry Andric }
1575ffd83dbSDimitry Andric 
158e8d8bef9SDimitry Andric /// For a byval, sret, inalloca, or preallocated parameter, get the in-memory
159e8d8bef9SDimitry Andric /// parameter type.
160349cc55cSDimitry Andric static Type *getMemoryParamAllocType(AttributeSet ParamAttrs) {
1615ffd83dbSDimitry Andric   // FIXME: All the type carrying attributes are mutually exclusive, so there
1625ffd83dbSDimitry Andric   // should be a single query to get the stored type that handles any of them.
1635ffd83dbSDimitry Andric   if (Type *ByValTy = ParamAttrs.getByValType())
164e8d8bef9SDimitry Andric     return ByValTy;
165e8d8bef9SDimitry Andric   if (Type *ByRefTy = ParamAttrs.getByRefType())
166e8d8bef9SDimitry Andric     return ByRefTy;
1675ffd83dbSDimitry Andric   if (Type *PreAllocTy = ParamAttrs.getPreallocatedType())
168e8d8bef9SDimitry Andric     return PreAllocTy;
169fe6060f1SDimitry Andric   if (Type *InAllocaTy = ParamAttrs.getInAllocaType())
170fe6060f1SDimitry Andric     return InAllocaTy;
171fe6060f1SDimitry Andric   if (Type *SRetTy = ParamAttrs.getStructRetType())
172fe6060f1SDimitry Andric     return SRetTy;
1735ffd83dbSDimitry Andric 
174e8d8bef9SDimitry Andric   return nullptr;
175e8d8bef9SDimitry Andric }
176e8d8bef9SDimitry Andric 
177e8d8bef9SDimitry Andric uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout &DL) const {
178e8d8bef9SDimitry Andric   AttributeSet ParamAttrs =
179349cc55cSDimitry Andric       getParent()->getAttributes().getParamAttrs(getArgNo());
180349cc55cSDimitry Andric   if (Type *MemTy = getMemoryParamAllocType(ParamAttrs))
181e8d8bef9SDimitry Andric     return DL.getTypeAllocSize(MemTy);
1825ffd83dbSDimitry Andric   return 0;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
185e8d8bef9SDimitry Andric Type *Argument::getPointeeInMemoryValueType() const {
186e8d8bef9SDimitry Andric   AttributeSet ParamAttrs =
187349cc55cSDimitry Andric       getParent()->getAttributes().getParamAttrs(getArgNo());
188349cc55cSDimitry Andric   return getMemoryParamAllocType(ParamAttrs);
189e8d8bef9SDimitry Andric }
190e8d8bef9SDimitry Andric 
191480093f4SDimitry Andric MaybeAlign Argument::getParamAlign() const {
192480093f4SDimitry Andric   assert(getType()->isPointerTy() && "Only pointers have alignments");
193480093f4SDimitry Andric   return getParent()->getParamAlign(getArgNo());
194480093f4SDimitry Andric }
195480093f4SDimitry Andric 
196fe6060f1SDimitry Andric MaybeAlign Argument::getParamStackAlign() const {
197fe6060f1SDimitry Andric   return getParent()->getParamStackAlign(getArgNo());
198fe6060f1SDimitry Andric }
199fe6060f1SDimitry Andric 
2000b57cec5SDimitry Andric Type *Argument::getParamByValType() const {
2010b57cec5SDimitry Andric   assert(getType()->isPointerTy() && "Only pointers have byval types");
2020b57cec5SDimitry Andric   return getParent()->getParamByValType(getArgNo());
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
205e8d8bef9SDimitry Andric Type *Argument::getParamStructRetType() const {
206e8d8bef9SDimitry Andric   assert(getType()->isPointerTy() && "Only pointers have sret types");
207e8d8bef9SDimitry Andric   return getParent()->getParamStructRetType(getArgNo());
208e8d8bef9SDimitry Andric }
209e8d8bef9SDimitry Andric 
210e8d8bef9SDimitry Andric Type *Argument::getParamByRefType() const {
211fe6060f1SDimitry Andric   assert(getType()->isPointerTy() && "Only pointers have byref types");
212e8d8bef9SDimitry Andric   return getParent()->getParamByRefType(getArgNo());
213e8d8bef9SDimitry Andric }
214e8d8bef9SDimitry Andric 
215fe6060f1SDimitry Andric Type *Argument::getParamInAllocaType() const {
216fe6060f1SDimitry Andric   assert(getType()->isPointerTy() && "Only pointers have inalloca types");
217fe6060f1SDimitry Andric   return getParent()->getParamInAllocaType(getArgNo());
218fe6060f1SDimitry Andric }
219fe6060f1SDimitry Andric 
2200b57cec5SDimitry Andric uint64_t Argument::getDereferenceableBytes() const {
2210b57cec5SDimitry Andric   assert(getType()->isPointerTy() &&
2220b57cec5SDimitry Andric          "Only pointers have dereferenceable bytes");
2230b57cec5SDimitry Andric   return getParent()->getParamDereferenceableBytes(getArgNo());
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric uint64_t Argument::getDereferenceableOrNullBytes() const {
2270b57cec5SDimitry Andric   assert(getType()->isPointerTy() &&
2280b57cec5SDimitry Andric          "Only pointers have dereferenceable bytes");
2290b57cec5SDimitry Andric   return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric 
232*06c3fb27SDimitry Andric FPClassTest Argument::getNoFPClass() const {
233*06c3fb27SDimitry Andric   return getParent()->getParamNoFPClass(getArgNo());
234*06c3fb27SDimitry Andric }
235*06c3fb27SDimitry Andric 
2360b57cec5SDimitry Andric bool Argument::hasNestAttr() const {
2370b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
2380b57cec5SDimitry Andric   return hasAttribute(Attribute::Nest);
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric bool Argument::hasNoAliasAttr() const {
2420b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
2430b57cec5SDimitry Andric   return hasAttribute(Attribute::NoAlias);
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric bool Argument::hasNoCaptureAttr() const {
2470b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
2480b57cec5SDimitry Andric   return hasAttribute(Attribute::NoCapture);
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
251fe6060f1SDimitry Andric bool Argument::hasNoFreeAttr() const {
252fe6060f1SDimitry Andric   if (!getType()->isPointerTy()) return false;
253fe6060f1SDimitry Andric   return hasAttribute(Attribute::NoFree);
254fe6060f1SDimitry Andric }
255fe6060f1SDimitry Andric 
2560b57cec5SDimitry Andric bool Argument::hasStructRetAttr() const {
2570b57cec5SDimitry Andric   if (!getType()->isPointerTy()) return false;
2580b57cec5SDimitry Andric   return hasAttribute(Attribute::StructRet);
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric bool Argument::hasInRegAttr() const {
2620b57cec5SDimitry Andric   return hasAttribute(Attribute::InReg);
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric bool Argument::hasReturnedAttr() const {
2660b57cec5SDimitry Andric   return hasAttribute(Attribute::Returned);
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric bool Argument::hasZExtAttr() const {
2700b57cec5SDimitry Andric   return hasAttribute(Attribute::ZExt);
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric bool Argument::hasSExtAttr() const {
2740b57cec5SDimitry Andric   return hasAttribute(Attribute::SExt);
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric bool Argument::onlyReadsMemory() const {
2780b57cec5SDimitry Andric   AttributeList Attrs = getParent()->getAttributes();
279349cc55cSDimitry Andric   return Attrs.hasParamAttr(getArgNo(), Attribute::ReadOnly) ||
280349cc55cSDimitry Andric          Attrs.hasParamAttr(getArgNo(), Attribute::ReadNone);
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric void Argument::addAttrs(AttrBuilder &B) {
2840b57cec5SDimitry Andric   AttributeList AL = getParent()->getAttributes();
2850b57cec5SDimitry Andric   AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
2860b57cec5SDimitry Andric   getParent()->setAttributes(AL);
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric void Argument::addAttr(Attribute::AttrKind Kind) {
2900b57cec5SDimitry Andric   getParent()->addParamAttr(getArgNo(), Kind);
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric void Argument::addAttr(Attribute Attr) {
2940b57cec5SDimitry Andric   getParent()->addParamAttr(getArgNo(), Attr);
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric void Argument::removeAttr(Attribute::AttrKind Kind) {
2980b57cec5SDimitry Andric   getParent()->removeParamAttr(getArgNo(), Kind);
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
30104eeddc0SDimitry Andric void Argument::removeAttrs(const AttributeMask &AM) {
302fe6060f1SDimitry Andric   AttributeList AL = getParent()->getAttributes();
30304eeddc0SDimitry Andric   AL = AL.removeParamAttributes(Parent->getContext(), getArgNo(), AM);
304fe6060f1SDimitry Andric   getParent()->setAttributes(AL);
305fe6060f1SDimitry Andric }
306fe6060f1SDimitry Andric 
3070b57cec5SDimitry Andric bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
3080b57cec5SDimitry Andric   return getParent()->hasParamAttribute(getArgNo(), Kind);
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric Attribute Argument::getAttribute(Attribute::AttrKind Kind) const {
3120b57cec5SDimitry Andric   return getParent()->getParamAttribute(getArgNo(), Kind);
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3160b57cec5SDimitry Andric // Helper Methods in Function
3170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric LLVMContext &Function::getContext() const {
3200b57cec5SDimitry Andric   return getType()->getContext();
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric unsigned Function::getInstructionCount() const {
3240b57cec5SDimitry Andric   unsigned NumInstrs = 0;
3250b57cec5SDimitry Andric   for (const BasicBlock &BB : BasicBlocks)
3260b57cec5SDimitry Andric     NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(),
3270b57cec5SDimitry Andric                                BB.instructionsWithoutDebug().end());
3280b57cec5SDimitry Andric   return NumInstrs;
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
3320b57cec5SDimitry Andric                            const Twine &N, Module &M) {
3330b57cec5SDimitry Andric   return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
3340b57cec5SDimitry Andric }
3350b57cec5SDimitry Andric 
336fe6060f1SDimitry Andric Function *Function::createWithDefaultAttr(FunctionType *Ty,
337fe6060f1SDimitry Andric                                           LinkageTypes Linkage,
338fe6060f1SDimitry Andric                                           unsigned AddrSpace, const Twine &N,
339fe6060f1SDimitry Andric                                           Module *M) {
340fe6060f1SDimitry Andric   auto *F = new Function(Ty, Linkage, AddrSpace, N, M);
34104eeddc0SDimitry Andric   AttrBuilder B(F->getContext());
34281ad6265SDimitry Andric   UWTableKind UWTable = M->getUwtable();
34381ad6265SDimitry Andric   if (UWTable != UWTableKind::None)
34481ad6265SDimitry Andric     B.addUWTableAttr(UWTable);
345fe6060f1SDimitry Andric   switch (M->getFramePointer()) {
346fe6060f1SDimitry Andric   case FramePointerKind::None:
347fe6060f1SDimitry Andric     // 0 ("none") is the default.
348fe6060f1SDimitry Andric     break;
349fe6060f1SDimitry Andric   case FramePointerKind::NonLeaf:
350fe6060f1SDimitry Andric     B.addAttribute("frame-pointer", "non-leaf");
351fe6060f1SDimitry Andric     break;
352fe6060f1SDimitry Andric   case FramePointerKind::All:
353fe6060f1SDimitry Andric     B.addAttribute("frame-pointer", "all");
354fe6060f1SDimitry Andric     break;
355fe6060f1SDimitry Andric   }
356fcaf7f86SDimitry Andric   if (M->getModuleFlag("function_return_thunk_extern"))
357fcaf7f86SDimitry Andric     B.addAttribute(Attribute::FnRetThunkExtern);
358349cc55cSDimitry Andric   F->addFnAttrs(B);
359fe6060f1SDimitry Andric   return F;
360fe6060f1SDimitry Andric }
361fe6060f1SDimitry Andric 
3620b57cec5SDimitry Andric void Function::removeFromParent() {
3630b57cec5SDimitry Andric   getParent()->getFunctionList().remove(getIterator());
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric void Function::eraseFromParent() {
3670b57cec5SDimitry Andric   getParent()->getFunctionList().erase(getIterator());
3680b57cec5SDimitry Andric }
3690b57cec5SDimitry Andric 
370bdd1243dSDimitry Andric void Function::splice(Function::iterator ToIt, Function *FromF,
371bdd1243dSDimitry Andric                       Function::iterator FromBeginIt,
372bdd1243dSDimitry Andric                       Function::iterator FromEndIt) {
373bdd1243dSDimitry Andric #ifdef EXPENSIVE_CHECKS
374bdd1243dSDimitry Andric   // Check that FromBeginIt is before FromEndIt.
375bdd1243dSDimitry Andric   auto FromFEnd = FromF->end();
376bdd1243dSDimitry Andric   for (auto It = FromBeginIt; It != FromEndIt; ++It)
377bdd1243dSDimitry Andric     assert(It != FromFEnd && "FromBeginIt not before FromEndIt!");
378bdd1243dSDimitry Andric #endif // EXPENSIVE_CHECKS
379bdd1243dSDimitry Andric   BasicBlocks.splice(ToIt, FromF->BasicBlocks, FromBeginIt, FromEndIt);
380bdd1243dSDimitry Andric }
381bdd1243dSDimitry Andric 
382bdd1243dSDimitry Andric Function::iterator Function::erase(Function::iterator FromIt,
383bdd1243dSDimitry Andric                                    Function::iterator ToIt) {
384bdd1243dSDimitry Andric   return BasicBlocks.erase(FromIt, ToIt);
385bdd1243dSDimitry Andric }
386bdd1243dSDimitry Andric 
3870b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3880b57cec5SDimitry Andric // Function Implementation
3890b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) {
3920b57cec5SDimitry Andric   // If AS == -1 and we are passed a valid module pointer we place the function
3930b57cec5SDimitry Andric   // in the program address space. Otherwise we default to AS0.
3940b57cec5SDimitry Andric   if (AddrSpace == static_cast<unsigned>(-1))
3950b57cec5SDimitry Andric     return M ? M->getDataLayout().getProgramAddressSpace() : 0;
3960b57cec5SDimitry Andric   return AddrSpace;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
4000b57cec5SDimitry Andric                    const Twine &name, Module *ParentModule)
4010b57cec5SDimitry Andric     : GlobalObject(Ty, Value::FunctionVal,
4020b57cec5SDimitry Andric                    OperandTraits<Function>::op_begin(this), 0, Linkage, name,
4030b57cec5SDimitry Andric                    computeAddrSpace(AddrSpace, ParentModule)),
4040b57cec5SDimitry Andric       NumArgs(Ty->getNumParams()) {
4050b57cec5SDimitry Andric   assert(FunctionType::isValidReturnType(getReturnType()) &&
4060b57cec5SDimitry Andric          "invalid return type");
4070b57cec5SDimitry Andric   setGlobalObjectSubClassData(0);
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   // We only need a symbol table for a function if the context keeps value names
4100b57cec5SDimitry Andric   if (!getContext().shouldDiscardValueNames())
411fe6060f1SDimitry Andric     SymTab = std::make_unique<ValueSymbolTable>(NonGlobalValueMaxNameSize);
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   // If the function has arguments, mark them as lazily built.
4140b57cec5SDimitry Andric   if (Ty->getNumParams())
4150b57cec5SDimitry Andric     setValueSubclassData(1);   // Set the "has lazy arguments" bit.
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   if (ParentModule)
4180b57cec5SDimitry Andric     ParentModule->getFunctionList().push_back(this);
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   HasLLVMReservedName = getName().startswith("llvm.");
4210b57cec5SDimitry Andric   // Ensure intrinsics have the right parameter attributes.
4220b57cec5SDimitry Andric   // Note, the IntID field will have been set in Value::setName if this function
4230b57cec5SDimitry Andric   // name is a valid intrinsic ID.
4240b57cec5SDimitry Andric   if (IntID)
4250b57cec5SDimitry Andric     setAttributes(Intrinsic::getAttributes(getContext(), IntID));
4260b57cec5SDimitry Andric }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric Function::~Function() {
4290b57cec5SDimitry Andric   dropAllReferences();    // After this it is safe to delete instructions.
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   // Delete all of the method arguments and unlink from symbol table...
4320b57cec5SDimitry Andric   if (Arguments)
4330b57cec5SDimitry Andric     clearArguments();
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   // Remove the function from the on-the-side GC table.
4360b57cec5SDimitry Andric   clearGC();
4370b57cec5SDimitry Andric }
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric void Function::BuildLazyArguments() const {
4400b57cec5SDimitry Andric   // Create the arguments vector, all arguments start out unnamed.
4410b57cec5SDimitry Andric   auto *FT = getFunctionType();
4420b57cec5SDimitry Andric   if (NumArgs > 0) {
4430b57cec5SDimitry Andric     Arguments = std::allocator<Argument>().allocate(NumArgs);
4440b57cec5SDimitry Andric     for (unsigned i = 0, e = NumArgs; i != e; ++i) {
4450b57cec5SDimitry Andric       Type *ArgTy = FT->getParamType(i);
4460b57cec5SDimitry Andric       assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
4470b57cec5SDimitry Andric       new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
4480b57cec5SDimitry Andric     }
4490b57cec5SDimitry Andric   }
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric   // Clear the lazy arguments bit.
4520b57cec5SDimitry Andric   unsigned SDC = getSubclassDataFromValue();
4538bcb0991SDimitry Andric   SDC &= ~(1 << 0);
4548bcb0991SDimitry Andric   const_cast<Function*>(this)->setValueSubclassData(SDC);
4550b57cec5SDimitry Andric   assert(!hasLazyArguments());
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
4590b57cec5SDimitry Andric   return MutableArrayRef<Argument>(Args, Count);
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
4625ffd83dbSDimitry Andric bool Function::isConstrainedFPIntrinsic() const {
4635ffd83dbSDimitry Andric   switch (getIntrinsicID()) {
4645ffd83dbSDimitry Andric #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
4655ffd83dbSDimitry Andric   case Intrinsic::INTRINSIC:
4665ffd83dbSDimitry Andric #include "llvm/IR/ConstrainedOps.def"
4675ffd83dbSDimitry Andric     return true;
4685ffd83dbSDimitry Andric #undef INSTRUCTION
4695ffd83dbSDimitry Andric   default:
4705ffd83dbSDimitry Andric     return false;
4715ffd83dbSDimitry Andric   }
4725ffd83dbSDimitry Andric }
4735ffd83dbSDimitry Andric 
4740b57cec5SDimitry Andric void Function::clearArguments() {
4750b57cec5SDimitry Andric   for (Argument &A : makeArgArray(Arguments, NumArgs)) {
4760b57cec5SDimitry Andric     A.setName("");
4770b57cec5SDimitry Andric     A.~Argument();
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric   std::allocator<Argument>().deallocate(Arguments, NumArgs);
4800b57cec5SDimitry Andric   Arguments = nullptr;
4810b57cec5SDimitry Andric }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric void Function::stealArgumentListFrom(Function &Src) {
4840b57cec5SDimitry Andric   assert(isDeclaration() && "Expected no references to current arguments");
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   // Drop the current arguments, if any, and set the lazy argument bit.
4870b57cec5SDimitry Andric   if (!hasLazyArguments()) {
4880b57cec5SDimitry Andric     assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
4890b57cec5SDimitry Andric                         [](const Argument &A) { return A.use_empty(); }) &&
4900b57cec5SDimitry Andric            "Expected arguments to be unused in declaration");
4910b57cec5SDimitry Andric     clearArguments();
4920b57cec5SDimitry Andric     setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
4930b57cec5SDimitry Andric   }
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   // Nothing to steal if Src has lazy arguments.
4960b57cec5SDimitry Andric   if (Src.hasLazyArguments())
4970b57cec5SDimitry Andric     return;
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   // Steal arguments from Src, and fix the lazy argument bits.
5000b57cec5SDimitry Andric   assert(arg_size() == Src.arg_size());
5010b57cec5SDimitry Andric   Arguments = Src.Arguments;
5020b57cec5SDimitry Andric   Src.Arguments = nullptr;
5030b57cec5SDimitry Andric   for (Argument &A : makeArgArray(Arguments, NumArgs)) {
5040b57cec5SDimitry Andric     // FIXME: This does the work of transferNodesFromList inefficiently.
5050b57cec5SDimitry Andric     SmallString<128> Name;
5060b57cec5SDimitry Andric     if (A.hasName())
5070b57cec5SDimitry Andric       Name = A.getName();
5080b57cec5SDimitry Andric     if (!Name.empty())
5090b57cec5SDimitry Andric       A.setName("");
5100b57cec5SDimitry Andric     A.setParent(this);
5110b57cec5SDimitry Andric     if (!Name.empty())
5120b57cec5SDimitry Andric       A.setName(Name);
5130b57cec5SDimitry Andric   }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
5160b57cec5SDimitry Andric   assert(!hasLazyArguments());
5170b57cec5SDimitry Andric   Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
5180b57cec5SDimitry Andric }
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric // dropAllReferences() - This function causes all the subinstructions to "let
5210b57cec5SDimitry Andric // go" of all references that they are maintaining.  This allows one to
5220b57cec5SDimitry Andric // 'delete' a whole class at a time, even though there may be circular
5230b57cec5SDimitry Andric // references... first all references are dropped, and all use counts go to
5240b57cec5SDimitry Andric // zero.  Then everything is deleted for real.  Note that no operations are
5250b57cec5SDimitry Andric // valid on an object that has "dropped all references", except operator
5260b57cec5SDimitry Andric // delete.
5270b57cec5SDimitry Andric //
5280b57cec5SDimitry Andric void Function::dropAllReferences() {
5290b57cec5SDimitry Andric   setIsMaterializable(false);
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric   for (BasicBlock &BB : *this)
5320b57cec5SDimitry Andric     BB.dropAllReferences();
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   // Delete all basic blocks. They are now unused, except possibly by
5350b57cec5SDimitry Andric   // blockaddresses, but BasicBlock's destructor takes care of those.
5360b57cec5SDimitry Andric   while (!BasicBlocks.empty())
5370b57cec5SDimitry Andric     BasicBlocks.begin()->eraseFromParent();
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric   // Drop uses of any optional data (real or placeholder).
5400b57cec5SDimitry Andric   if (getNumOperands()) {
5410b57cec5SDimitry Andric     User::dropAllReferences();
5420b57cec5SDimitry Andric     setNumHungOffUseOperands(0);
5430b57cec5SDimitry Andric     setValueSubclassData(getSubclassDataFromValue() & ~0xe);
5440b57cec5SDimitry Andric   }
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric   // Metadata is stored in a side-table.
5470b57cec5SDimitry Andric   clearMetadata();
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
550349cc55cSDimitry Andric void Function::addAttributeAtIndex(unsigned i, Attribute Attr) {
551349cc55cSDimitry Andric   AttributeSets = AttributeSets.addAttributeAtIndex(getContext(), i, Attr);
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric 
554349cc55cSDimitry Andric void Function::addFnAttr(Attribute::AttrKind Kind) {
555349cc55cSDimitry Andric   AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind);
5560b57cec5SDimitry Andric }
5570b57cec5SDimitry Andric 
558349cc55cSDimitry Andric void Function::addFnAttr(StringRef Kind, StringRef Val) {
559349cc55cSDimitry Andric   AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind, Val);
560349cc55cSDimitry Andric }
561349cc55cSDimitry Andric 
562349cc55cSDimitry Andric void Function::addFnAttr(Attribute Attr) {
563349cc55cSDimitry Andric   AttributeSets = AttributeSets.addFnAttribute(getContext(), Attr);
564349cc55cSDimitry Andric }
565349cc55cSDimitry Andric 
566349cc55cSDimitry Andric void Function::addFnAttrs(const AttrBuilder &Attrs) {
567349cc55cSDimitry Andric   AttributeSets = AttributeSets.addFnAttributes(getContext(), Attrs);
568349cc55cSDimitry Andric }
569349cc55cSDimitry Andric 
570349cc55cSDimitry Andric void Function::addRetAttr(Attribute::AttrKind Kind) {
571349cc55cSDimitry Andric   AttributeSets = AttributeSets.addRetAttribute(getContext(), Kind);
572349cc55cSDimitry Andric }
573349cc55cSDimitry Andric 
574349cc55cSDimitry Andric void Function::addRetAttr(Attribute Attr) {
575349cc55cSDimitry Andric   AttributeSets = AttributeSets.addRetAttribute(getContext(), Attr);
576349cc55cSDimitry Andric }
577349cc55cSDimitry Andric 
578349cc55cSDimitry Andric void Function::addRetAttrs(const AttrBuilder &Attrs) {
579349cc55cSDimitry Andric   AttributeSets = AttributeSets.addRetAttributes(getContext(), Attrs);
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
583349cc55cSDimitry Andric   AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Kind);
5840b57cec5SDimitry Andric }
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
587349cc55cSDimitry Andric   AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Attr);
5880b57cec5SDimitry Andric }
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
591349cc55cSDimitry Andric   AttributeSets = AttributeSets.addParamAttributes(getContext(), ArgNo, Attrs);
5920b57cec5SDimitry Andric }
5930b57cec5SDimitry Andric 
594349cc55cSDimitry Andric void Function::removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) {
595349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind);
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric 
598349cc55cSDimitry Andric void Function::removeAttributeAtIndex(unsigned i, StringRef Kind) {
599349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind);
6000b57cec5SDimitry Andric }
6010b57cec5SDimitry Andric 
602349cc55cSDimitry Andric void Function::removeFnAttr(Attribute::AttrKind Kind) {
603349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind);
604349cc55cSDimitry Andric }
605349cc55cSDimitry Andric 
606349cc55cSDimitry Andric void Function::removeFnAttr(StringRef Kind) {
607349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind);
608349cc55cSDimitry Andric }
609349cc55cSDimitry Andric 
61004eeddc0SDimitry Andric void Function::removeFnAttrs(const AttributeMask &AM) {
61104eeddc0SDimitry Andric   AttributeSets = AttributeSets.removeFnAttributes(getContext(), AM);
612349cc55cSDimitry Andric }
613349cc55cSDimitry Andric 
614349cc55cSDimitry Andric void Function::removeRetAttr(Attribute::AttrKind Kind) {
615349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind);
616349cc55cSDimitry Andric }
617349cc55cSDimitry Andric 
618349cc55cSDimitry Andric void Function::removeRetAttr(StringRef Kind) {
619349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind);
620349cc55cSDimitry Andric }
621349cc55cSDimitry Andric 
62204eeddc0SDimitry Andric void Function::removeRetAttrs(const AttributeMask &Attrs) {
623349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeRetAttributes(getContext(), Attrs);
6240b57cec5SDimitry Andric }
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
627349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind);
6280b57cec5SDimitry Andric }
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
631349cc55cSDimitry Andric   AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind);
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
63404eeddc0SDimitry Andric void Function::removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs) {
635349cc55cSDimitry Andric   AttributeSets =
636349cc55cSDimitry Andric       AttributeSets.removeParamAttributes(getContext(), ArgNo, Attrs);
6370b57cec5SDimitry Andric }
6380b57cec5SDimitry Andric 
6390b57cec5SDimitry Andric void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
640349cc55cSDimitry Andric   AttributeSets =
641349cc55cSDimitry Andric       AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric 
644349cc55cSDimitry Andric bool Function::hasFnAttribute(Attribute::AttrKind Kind) const {
645349cc55cSDimitry Andric   return AttributeSets.hasFnAttr(Kind);
646349cc55cSDimitry Andric }
647349cc55cSDimitry Andric 
648349cc55cSDimitry Andric bool Function::hasFnAttribute(StringRef Kind) const {
649349cc55cSDimitry Andric   return AttributeSets.hasFnAttr(Kind);
650349cc55cSDimitry Andric }
651349cc55cSDimitry Andric 
652349cc55cSDimitry Andric bool Function::hasRetAttribute(Attribute::AttrKind Kind) const {
653349cc55cSDimitry Andric   return AttributeSets.hasRetAttr(Kind);
654349cc55cSDimitry Andric }
655349cc55cSDimitry Andric 
656349cc55cSDimitry Andric bool Function::hasParamAttribute(unsigned ArgNo,
657349cc55cSDimitry Andric                                  Attribute::AttrKind Kind) const {
658349cc55cSDimitry Andric   return AttributeSets.hasParamAttr(ArgNo, Kind);
659349cc55cSDimitry Andric }
660349cc55cSDimitry Andric 
661349cc55cSDimitry Andric Attribute Function::getAttributeAtIndex(unsigned i,
662349cc55cSDimitry Andric                                         Attribute::AttrKind Kind) const {
663349cc55cSDimitry Andric   return AttributeSets.getAttributeAtIndex(i, Kind);
664349cc55cSDimitry Andric }
665349cc55cSDimitry Andric 
666349cc55cSDimitry Andric Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const {
667349cc55cSDimitry Andric   return AttributeSets.getAttributeAtIndex(i, Kind);
668349cc55cSDimitry Andric }
669349cc55cSDimitry Andric 
670349cc55cSDimitry Andric Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const {
671349cc55cSDimitry Andric   return AttributeSets.getFnAttr(Kind);
672349cc55cSDimitry Andric }
673349cc55cSDimitry Andric 
674349cc55cSDimitry Andric Attribute Function::getFnAttribute(StringRef Kind) const {
675349cc55cSDimitry Andric   return AttributeSets.getFnAttr(Kind);
676349cc55cSDimitry Andric }
677349cc55cSDimitry Andric 
678bdd1243dSDimitry Andric uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name,
679bdd1243dSDimitry Andric                                                  uint64_t Default) const {
680bdd1243dSDimitry Andric   Attribute A = getFnAttribute(Name);
681bdd1243dSDimitry Andric   uint64_t Result = Default;
682bdd1243dSDimitry Andric   if (A.isStringAttribute()) {
683bdd1243dSDimitry Andric     StringRef Str = A.getValueAsString();
684bdd1243dSDimitry Andric     if (Str.getAsInteger(0, Result))
685bdd1243dSDimitry Andric       getContext().emitError("cannot parse integer attribute " + Name);
686bdd1243dSDimitry Andric   }
687bdd1243dSDimitry Andric 
688bdd1243dSDimitry Andric   return Result;
689bdd1243dSDimitry Andric }
690bdd1243dSDimitry Andric 
691349cc55cSDimitry Andric /// gets the specified attribute from the list of attributes.
692349cc55cSDimitry Andric Attribute Function::getParamAttribute(unsigned ArgNo,
693349cc55cSDimitry Andric                                       Attribute::AttrKind Kind) const {
694349cc55cSDimitry Andric   return AttributeSets.getParamAttr(ArgNo, Kind);
6950b57cec5SDimitry Andric }
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
6980b57cec5SDimitry Andric                                                  uint64_t Bytes) {
699349cc55cSDimitry Andric   AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(getContext(),
700349cc55cSDimitry Andric                                                                   ArgNo, Bytes);
7010b57cec5SDimitry Andric }
7020b57cec5SDimitry Andric 
703e8d8bef9SDimitry Andric DenormalMode Function::getDenormalMode(const fltSemantics &FPType) const {
704e8d8bef9SDimitry Andric   if (&FPType == &APFloat::IEEEsingle()) {
705*06c3fb27SDimitry Andric     DenormalMode Mode = getDenormalModeF32Raw();
706e8d8bef9SDimitry Andric     // If the f32 variant of the attribute isn't specified, try to use the
707e8d8bef9SDimitry Andric     // generic one.
708*06c3fb27SDimitry Andric     if (Mode.isValid())
709*06c3fb27SDimitry Andric       return Mode;
710e8d8bef9SDimitry Andric   }
711e8d8bef9SDimitry Andric 
712*06c3fb27SDimitry Andric   return getDenormalModeRaw();
713*06c3fb27SDimitry Andric }
714*06c3fb27SDimitry Andric 
715*06c3fb27SDimitry Andric DenormalMode Function::getDenormalModeRaw() const {
716e8d8bef9SDimitry Andric   Attribute Attr = getFnAttribute("denormal-fp-math");
717*06c3fb27SDimitry Andric   StringRef Val = Attr.getValueAsString();
718*06c3fb27SDimitry Andric   return parseDenormalFPAttribute(Val);
719*06c3fb27SDimitry Andric }
720*06c3fb27SDimitry Andric 
721*06c3fb27SDimitry Andric DenormalMode Function::getDenormalModeF32Raw() const {
722*06c3fb27SDimitry Andric   Attribute Attr = getFnAttribute("denormal-fp-math-f32");
723*06c3fb27SDimitry Andric   if (Attr.isValid()) {
724*06c3fb27SDimitry Andric     StringRef Val = Attr.getValueAsString();
725*06c3fb27SDimitry Andric     return parseDenormalFPAttribute(Val);
726*06c3fb27SDimitry Andric   }
727*06c3fb27SDimitry Andric 
728*06c3fb27SDimitry Andric   return DenormalMode::getInvalid();
729e8d8bef9SDimitry Andric }
730e8d8bef9SDimitry Andric 
7310b57cec5SDimitry Andric const std::string &Function::getGC() const {
7320b57cec5SDimitry Andric   assert(hasGC() && "Function has no collector");
7330b57cec5SDimitry Andric   return getContext().getGC(*this);
7340b57cec5SDimitry Andric }
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric void Function::setGC(std::string Str) {
7370b57cec5SDimitry Andric   setValueSubclassDataBit(14, !Str.empty());
7380b57cec5SDimitry Andric   getContext().setGC(*this, std::move(Str));
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric void Function::clearGC() {
7420b57cec5SDimitry Andric   if (!hasGC())
7430b57cec5SDimitry Andric     return;
7440b57cec5SDimitry Andric   getContext().deleteGC(*this);
7450b57cec5SDimitry Andric   setValueSubclassDataBit(14, false);
7460b57cec5SDimitry Andric }
7470b57cec5SDimitry Andric 
748e8d8bef9SDimitry Andric bool Function::hasStackProtectorFnAttr() const {
749e8d8bef9SDimitry Andric   return hasFnAttribute(Attribute::StackProtect) ||
750e8d8bef9SDimitry Andric          hasFnAttribute(Attribute::StackProtectStrong) ||
751e8d8bef9SDimitry Andric          hasFnAttribute(Attribute::StackProtectReq);
752e8d8bef9SDimitry Andric }
753e8d8bef9SDimitry Andric 
7540b57cec5SDimitry Andric /// Copy all additional attributes (those not needed to create a Function) from
7550b57cec5SDimitry Andric /// the Function Src to this one.
7560b57cec5SDimitry Andric void Function::copyAttributesFrom(const Function *Src) {
7570b57cec5SDimitry Andric   GlobalObject::copyAttributesFrom(Src);
7580b57cec5SDimitry Andric   setCallingConv(Src->getCallingConv());
7590b57cec5SDimitry Andric   setAttributes(Src->getAttributes());
7600b57cec5SDimitry Andric   if (Src->hasGC())
7610b57cec5SDimitry Andric     setGC(Src->getGC());
7620b57cec5SDimitry Andric   else
7630b57cec5SDimitry Andric     clearGC();
7640b57cec5SDimitry Andric   if (Src->hasPersonalityFn())
7650b57cec5SDimitry Andric     setPersonalityFn(Src->getPersonalityFn());
7660b57cec5SDimitry Andric   if (Src->hasPrefixData())
7670b57cec5SDimitry Andric     setPrefixData(Src->getPrefixData());
7680b57cec5SDimitry Andric   if (Src->hasPrologueData())
7690b57cec5SDimitry Andric     setPrologueData(Src->getPrologueData());
7700b57cec5SDimitry Andric }
7710b57cec5SDimitry Andric 
772bdd1243dSDimitry Andric MemoryEffects Function::getMemoryEffects() const {
773bdd1243dSDimitry Andric   return getAttributes().getMemoryEffects();
774bdd1243dSDimitry Andric }
775bdd1243dSDimitry Andric void Function::setMemoryEffects(MemoryEffects ME) {
776bdd1243dSDimitry Andric   addFnAttr(Attribute::getWithMemoryEffects(getContext(), ME));
777bdd1243dSDimitry Andric }
778bdd1243dSDimitry Andric 
779bdd1243dSDimitry Andric /// Determine if the function does not access memory.
780bdd1243dSDimitry Andric bool Function::doesNotAccessMemory() const {
781bdd1243dSDimitry Andric   return getMemoryEffects().doesNotAccessMemory();
782bdd1243dSDimitry Andric }
783bdd1243dSDimitry Andric void Function::setDoesNotAccessMemory() {
784bdd1243dSDimitry Andric   setMemoryEffects(MemoryEffects::none());
785bdd1243dSDimitry Andric }
786bdd1243dSDimitry Andric 
787bdd1243dSDimitry Andric /// Determine if the function does not access or only reads memory.
788bdd1243dSDimitry Andric bool Function::onlyReadsMemory() const {
789bdd1243dSDimitry Andric   return getMemoryEffects().onlyReadsMemory();
790bdd1243dSDimitry Andric }
791bdd1243dSDimitry Andric void Function::setOnlyReadsMemory() {
792bdd1243dSDimitry Andric   setMemoryEffects(getMemoryEffects() & MemoryEffects::readOnly());
793bdd1243dSDimitry Andric }
794bdd1243dSDimitry Andric 
795bdd1243dSDimitry Andric /// Determine if the function does not access or only writes memory.
796bdd1243dSDimitry Andric bool Function::onlyWritesMemory() const {
797bdd1243dSDimitry Andric   return getMemoryEffects().onlyWritesMemory();
798bdd1243dSDimitry Andric }
799bdd1243dSDimitry Andric void Function::setOnlyWritesMemory() {
800bdd1243dSDimitry Andric   setMemoryEffects(getMemoryEffects() & MemoryEffects::writeOnly());
801bdd1243dSDimitry Andric }
802bdd1243dSDimitry Andric 
803bdd1243dSDimitry Andric /// Determine if the call can access memmory only using pointers based
804bdd1243dSDimitry Andric /// on its arguments.
805bdd1243dSDimitry Andric bool Function::onlyAccessesArgMemory() const {
806bdd1243dSDimitry Andric   return getMemoryEffects().onlyAccessesArgPointees();
807bdd1243dSDimitry Andric }
808bdd1243dSDimitry Andric void Function::setOnlyAccessesArgMemory() {
809bdd1243dSDimitry Andric   setMemoryEffects(getMemoryEffects() & MemoryEffects::argMemOnly());
810bdd1243dSDimitry Andric }
811bdd1243dSDimitry Andric 
812bdd1243dSDimitry Andric /// Determine if the function may only access memory that is
813bdd1243dSDimitry Andric ///  inaccessible from the IR.
814bdd1243dSDimitry Andric bool Function::onlyAccessesInaccessibleMemory() const {
815bdd1243dSDimitry Andric   return getMemoryEffects().onlyAccessesInaccessibleMem();
816bdd1243dSDimitry Andric }
817bdd1243dSDimitry Andric void Function::setOnlyAccessesInaccessibleMemory() {
818bdd1243dSDimitry Andric   setMemoryEffects(getMemoryEffects() & MemoryEffects::inaccessibleMemOnly());
819bdd1243dSDimitry Andric }
820bdd1243dSDimitry Andric 
821bdd1243dSDimitry Andric /// Determine if the function may only access memory that is
822bdd1243dSDimitry Andric ///  either inaccessible from the IR or pointed to by its arguments.
823bdd1243dSDimitry Andric bool Function::onlyAccessesInaccessibleMemOrArgMem() const {
824bdd1243dSDimitry Andric   return getMemoryEffects().onlyAccessesInaccessibleOrArgMem();
825bdd1243dSDimitry Andric }
826bdd1243dSDimitry Andric void Function::setOnlyAccessesInaccessibleMemOrArgMem() {
827bdd1243dSDimitry Andric   setMemoryEffects(getMemoryEffects() &
828bdd1243dSDimitry Andric                    MemoryEffects::inaccessibleOrArgMemOnly());
829bdd1243dSDimitry Andric }
830bdd1243dSDimitry Andric 
8310b57cec5SDimitry Andric /// Table of string intrinsic names indexed by enum value.
8320b57cec5SDimitry Andric static const char * const IntrinsicNameTable[] = {
8330b57cec5SDimitry Andric   "not_intrinsic",
8340b57cec5SDimitry Andric #define GET_INTRINSIC_NAME_TABLE
8350b57cec5SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
8360b57cec5SDimitry Andric #undef GET_INTRINSIC_NAME_TABLE
8370b57cec5SDimitry Andric };
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric /// Table of per-target intrinsic name tables.
8400b57cec5SDimitry Andric #define GET_INTRINSIC_TARGET_DATA
8410b57cec5SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
8420b57cec5SDimitry Andric #undef GET_INTRINSIC_TARGET_DATA
8430b57cec5SDimitry Andric 
844e8d8bef9SDimitry Andric bool Function::isTargetIntrinsic(Intrinsic::ID IID) {
845e8d8bef9SDimitry Andric   return IID > TargetInfos[0].Count;
846e8d8bef9SDimitry Andric }
847e8d8bef9SDimitry Andric 
848e8d8bef9SDimitry Andric bool Function::isTargetIntrinsic() const {
849e8d8bef9SDimitry Andric   return isTargetIntrinsic(IntID);
850e8d8bef9SDimitry Andric }
851e8d8bef9SDimitry Andric 
8520b57cec5SDimitry Andric /// Find the segment of \c IntrinsicNameTable for intrinsics with the same
8530b57cec5SDimitry Andric /// target as \c Name, or the generic table if \c Name is not target specific.
8540b57cec5SDimitry Andric ///
8550b57cec5SDimitry Andric /// Returns the relevant slice of \c IntrinsicNameTable
8560b57cec5SDimitry Andric static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
8570b57cec5SDimitry Andric   assert(Name.startswith("llvm."));
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric   ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
8600b57cec5SDimitry Andric   // Drop "llvm." and take the first dotted component. That will be the target
8610b57cec5SDimitry Andric   // if this is target specific.
8620b57cec5SDimitry Andric   StringRef Target = Name.drop_front(5).split('.').first;
8630b57cec5SDimitry Andric   auto It = partition_point(
8640b57cec5SDimitry Andric       Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; });
8650b57cec5SDimitry Andric   // We've either found the target or just fall back to the generic set, which
8660b57cec5SDimitry Andric   // is always first.
8670b57cec5SDimitry Andric   const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
868bdd1243dSDimitry Andric   return ArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
8690b57cec5SDimitry Andric }
8700b57cec5SDimitry Andric 
8710b57cec5SDimitry Andric /// This does the actual lookup of an intrinsic ID which
8720b57cec5SDimitry Andric /// matches the given function name.
8730b57cec5SDimitry Andric Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
8740b57cec5SDimitry Andric   ArrayRef<const char *> NameTable = findTargetSubtable(Name);
8750b57cec5SDimitry Andric   int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
8760b57cec5SDimitry Andric   if (Idx == -1)
8770b57cec5SDimitry Andric     return Intrinsic::not_intrinsic;
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
8800b57cec5SDimitry Andric   // an index into a sub-table.
8810b57cec5SDimitry Andric   int Adjust = NameTable.data() - IntrinsicNameTable;
8820b57cec5SDimitry Andric   Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   // If the intrinsic is not overloaded, require an exact match. If it is
8850b57cec5SDimitry Andric   // overloaded, require either exact or prefix match.
8860b57cec5SDimitry Andric   const auto MatchSize = strlen(NameTable[Idx]);
8870b57cec5SDimitry Andric   assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
8880b57cec5SDimitry Andric   bool IsExactMatch = Name.size() == MatchSize;
889480093f4SDimitry Andric   return IsExactMatch || Intrinsic::isOverloaded(ID) ? ID
890480093f4SDimitry Andric                                                      : Intrinsic::not_intrinsic;
8910b57cec5SDimitry Andric }
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric void Function::recalculateIntrinsicID() {
8940b57cec5SDimitry Andric   StringRef Name = getName();
8950b57cec5SDimitry Andric   if (!Name.startswith("llvm.")) {
8960b57cec5SDimitry Andric     HasLLVMReservedName = false;
8970b57cec5SDimitry Andric     IntID = Intrinsic::not_intrinsic;
8980b57cec5SDimitry Andric     return;
8990b57cec5SDimitry Andric   }
9000b57cec5SDimitry Andric   HasLLVMReservedName = true;
9010b57cec5SDimitry Andric   IntID = lookupIntrinsicID(Name);
9020b57cec5SDimitry Andric }
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric /// Returns a stable mangling for the type specified for use in the name
9050b57cec5SDimitry Andric /// mangling scheme used by 'any' types in intrinsic signatures.  The mangling
9060b57cec5SDimitry Andric /// of named types is simply their name.  Manglings for unnamed types consist
9070b57cec5SDimitry Andric /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
9080b57cec5SDimitry Andric /// combined with the mangling of their component types.  A vararg function
9090b57cec5SDimitry Andric /// type will have a suffix of 'vararg'.  Since function types can contain
9100b57cec5SDimitry Andric /// other function types, we close a function type mangling with suffix 'f'
9110b57cec5SDimitry Andric /// which can't be confused with it's prefix.  This ensures we don't have
9120b57cec5SDimitry Andric /// collisions between two unrelated function types. Otherwise, you might
9130b57cec5SDimitry Andric /// parse ffXX as f(fXX) or f(fX)X.  (X is a placeholder for any other type.)
914fe6060f1SDimitry Andric /// The HasUnnamedType boolean is set if an unnamed type was encountered,
915fe6060f1SDimitry Andric /// indicating that extra care must be taken to ensure a unique name.
916fe6060f1SDimitry Andric static std::string getMangledTypeStr(Type *Ty, bool &HasUnnamedType) {
9170b57cec5SDimitry Andric   std::string Result;
9180b57cec5SDimitry Andric   if (PointerType *PTyp = dyn_cast<PointerType>(Ty)) {
919fe6060f1SDimitry Andric     Result += "p" + utostr(PTyp->getAddressSpace());
9200b57cec5SDimitry Andric   } else if (ArrayType *ATyp = dyn_cast<ArrayType>(Ty)) {
9210b57cec5SDimitry Andric     Result += "a" + utostr(ATyp->getNumElements()) +
922fe6060f1SDimitry Andric               getMangledTypeStr(ATyp->getElementType(), HasUnnamedType);
9230b57cec5SDimitry Andric   } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
9240b57cec5SDimitry Andric     if (!STyp->isLiteral()) {
9250b57cec5SDimitry Andric       Result += "s_";
926fe6060f1SDimitry Andric       if (STyp->hasName())
9270b57cec5SDimitry Andric         Result += STyp->getName();
928fe6060f1SDimitry Andric       else
929fe6060f1SDimitry Andric         HasUnnamedType = true;
9300b57cec5SDimitry Andric     } else {
9310b57cec5SDimitry Andric       Result += "sl_";
932bdd1243dSDimitry Andric       for (auto *Elem : STyp->elements())
933fe6060f1SDimitry Andric         Result += getMangledTypeStr(Elem, HasUnnamedType);
9340b57cec5SDimitry Andric     }
9350b57cec5SDimitry Andric     // Ensure nested structs are distinguishable.
9360b57cec5SDimitry Andric     Result += "s";
9370b57cec5SDimitry Andric   } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
938fe6060f1SDimitry Andric     Result += "f_" + getMangledTypeStr(FT->getReturnType(), HasUnnamedType);
9390b57cec5SDimitry Andric     for (size_t i = 0; i < FT->getNumParams(); i++)
940fe6060f1SDimitry Andric       Result += getMangledTypeStr(FT->getParamType(i), HasUnnamedType);
9410b57cec5SDimitry Andric     if (FT->isVarArg())
9420b57cec5SDimitry Andric       Result += "vararg";
9430b57cec5SDimitry Andric     // Ensure nested function types are distinguishable.
9440b57cec5SDimitry Andric     Result += "f";
9458bcb0991SDimitry Andric   } else if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
9465ffd83dbSDimitry Andric     ElementCount EC = VTy->getElementCount();
947e8d8bef9SDimitry Andric     if (EC.isScalable())
9488bcb0991SDimitry Andric       Result += "nx";
949e8d8bef9SDimitry Andric     Result += "v" + utostr(EC.getKnownMinValue()) +
950fe6060f1SDimitry Andric               getMangledTypeStr(VTy->getElementType(), HasUnnamedType);
951bdd1243dSDimitry Andric   } else if (TargetExtType *TETy = dyn_cast<TargetExtType>(Ty)) {
952bdd1243dSDimitry Andric     Result += "t";
953bdd1243dSDimitry Andric     Result += TETy->getName();
954bdd1243dSDimitry Andric     for (Type *ParamTy : TETy->type_params())
955bdd1243dSDimitry Andric       Result += "_" + getMangledTypeStr(ParamTy, HasUnnamedType);
956bdd1243dSDimitry Andric     for (unsigned IntParam : TETy->int_params())
957bdd1243dSDimitry Andric       Result += "_" + utostr(IntParam);
958bdd1243dSDimitry Andric     // Ensure nested target extension types are distinguishable.
959bdd1243dSDimitry Andric     Result += "t";
9600b57cec5SDimitry Andric   } else if (Ty) {
9610b57cec5SDimitry Andric     switch (Ty->getTypeID()) {
9620b57cec5SDimitry Andric     default: llvm_unreachable("Unhandled type");
9630b57cec5SDimitry Andric     case Type::VoidTyID:      Result += "isVoid";   break;
9640b57cec5SDimitry Andric     case Type::MetadataTyID:  Result += "Metadata"; break;
9650b57cec5SDimitry Andric     case Type::HalfTyID:      Result += "f16";      break;
9665ffd83dbSDimitry Andric     case Type::BFloatTyID:    Result += "bf16";     break;
9670b57cec5SDimitry Andric     case Type::FloatTyID:     Result += "f32";      break;
9680b57cec5SDimitry Andric     case Type::DoubleTyID:    Result += "f64";      break;
9690b57cec5SDimitry Andric     case Type::X86_FP80TyID:  Result += "f80";      break;
9700b57cec5SDimitry Andric     case Type::FP128TyID:     Result += "f128";     break;
9710b57cec5SDimitry Andric     case Type::PPC_FP128TyID: Result += "ppcf128";  break;
9720b57cec5SDimitry Andric     case Type::X86_MMXTyID:   Result += "x86mmx";   break;
973e8d8bef9SDimitry Andric     case Type::X86_AMXTyID:   Result += "x86amx";   break;
9740b57cec5SDimitry Andric     case Type::IntegerTyID:
9750b57cec5SDimitry Andric       Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
9760b57cec5SDimitry Andric       break;
9770b57cec5SDimitry Andric     }
9780b57cec5SDimitry Andric   }
9790b57cec5SDimitry Andric   return Result;
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric 
982fe6060f1SDimitry Andric StringRef Intrinsic::getBaseName(ID id) {
983fe6060f1SDimitry Andric   assert(id < num_intrinsics && "Invalid intrinsic ID!");
984fe6060f1SDimitry Andric   return IntrinsicNameTable[id];
985fe6060f1SDimitry Andric }
986fe6060f1SDimitry Andric 
9870b57cec5SDimitry Andric StringRef Intrinsic::getName(ID id) {
9880b57cec5SDimitry Andric   assert(id < num_intrinsics && "Invalid intrinsic ID!");
989480093f4SDimitry Andric   assert(!Intrinsic::isOverloaded(id) &&
9900b57cec5SDimitry Andric          "This version of getName does not support overloading");
991fe6060f1SDimitry Andric   return getBaseName(id);
9920b57cec5SDimitry Andric }
9930b57cec5SDimitry Andric 
994fe6060f1SDimitry Andric static std::string getIntrinsicNameImpl(Intrinsic::ID Id, ArrayRef<Type *> Tys,
995fe6060f1SDimitry Andric                                         Module *M, FunctionType *FT,
996fe6060f1SDimitry Andric                                         bool EarlyModuleCheck) {
997fe6060f1SDimitry Andric 
998fe6060f1SDimitry Andric   assert(Id < Intrinsic::num_intrinsics && "Invalid intrinsic ID!");
999fe6060f1SDimitry Andric   assert((Tys.empty() || Intrinsic::isOverloaded(Id)) &&
1000e8d8bef9SDimitry Andric          "This version of getName is for overloaded intrinsics only");
1001fe6060f1SDimitry Andric   (void)EarlyModuleCheck;
1002fe6060f1SDimitry Andric   assert((!EarlyModuleCheck || M ||
1003fe6060f1SDimitry Andric           !any_of(Tys, [](Type *T) { return isa<PointerType>(T); })) &&
1004fe6060f1SDimitry Andric          "Intrinsic overloading on pointer types need to provide a Module");
1005fe6060f1SDimitry Andric   bool HasUnnamedType = false;
1006fe6060f1SDimitry Andric   std::string Result(Intrinsic::getBaseName(Id));
1007fe6060f1SDimitry Andric   for (Type *Ty : Tys)
1008fe6060f1SDimitry Andric     Result += "." + getMangledTypeStr(Ty, HasUnnamedType);
1009fe6060f1SDimitry Andric   if (HasUnnamedType) {
1010fe6060f1SDimitry Andric     assert(M && "unnamed types need a module");
1011fe6060f1SDimitry Andric     if (!FT)
1012fe6060f1SDimitry Andric       FT = Intrinsic::getType(M->getContext(), Id, Tys);
1013fe6060f1SDimitry Andric     else
1014fe6060f1SDimitry Andric       assert((FT == Intrinsic::getType(M->getContext(), Id, Tys)) &&
1015fe6060f1SDimitry Andric              "Provided FunctionType must match arguments");
1016fe6060f1SDimitry Andric     return M->getUniqueIntrinsicName(Result, Id, FT);
10170b57cec5SDimitry Andric   }
10180b57cec5SDimitry Andric   return Result;
10190b57cec5SDimitry Andric }
10200b57cec5SDimitry Andric 
1021fe6060f1SDimitry Andric std::string Intrinsic::getName(ID Id, ArrayRef<Type *> Tys, Module *M,
1022fe6060f1SDimitry Andric                                FunctionType *FT) {
1023fe6060f1SDimitry Andric   assert(M && "We need to have a Module");
1024fe6060f1SDimitry Andric   return getIntrinsicNameImpl(Id, Tys, M, FT, true);
1025fe6060f1SDimitry Andric }
1026fe6060f1SDimitry Andric 
1027fe6060f1SDimitry Andric std::string Intrinsic::getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> Tys) {
1028fe6060f1SDimitry Andric   return getIntrinsicNameImpl(Id, Tys, nullptr, nullptr, false);
1029fe6060f1SDimitry Andric }
1030fe6060f1SDimitry Andric 
10310b57cec5SDimitry Andric /// IIT_Info - These are enumerators that describe the entries returned by the
10320b57cec5SDimitry Andric /// getIntrinsicInfoTableEntries function.
10330b57cec5SDimitry Andric ///
1034*06c3fb27SDimitry Andric /// Defined in Intrinsics.td.
10350b57cec5SDimitry Andric enum IIT_Info {
1036*06c3fb27SDimitry Andric #define GET_INTRINSIC_IITINFO
1037*06c3fb27SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
1038*06c3fb27SDimitry Andric #undef GET_INTRINSIC_IITINFO
10390b57cec5SDimitry Andric };
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
10425ffd83dbSDimitry Andric                       IIT_Info LastInfo,
10430b57cec5SDimitry Andric                       SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
10440b57cec5SDimitry Andric   using namespace Intrinsic;
10450b57cec5SDimitry Andric 
10465ffd83dbSDimitry Andric   bool IsScalableVector = (LastInfo == IIT_SCALABLE_VEC);
10475ffd83dbSDimitry Andric 
10480b57cec5SDimitry Andric   IIT_Info Info = IIT_Info(Infos[NextElt++]);
10490b57cec5SDimitry Andric   unsigned StructElts = 2;
10500b57cec5SDimitry Andric 
10510b57cec5SDimitry Andric   switch (Info) {
10520b57cec5SDimitry Andric   case IIT_Done:
10530b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
10540b57cec5SDimitry Andric     return;
10550b57cec5SDimitry Andric   case IIT_VARARG:
10560b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
10570b57cec5SDimitry Andric     return;
10580b57cec5SDimitry Andric   case IIT_MMX:
10590b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
10600b57cec5SDimitry Andric     return;
1061e8d8bef9SDimitry Andric   case IIT_AMX:
1062e8d8bef9SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::AMX, 0));
1063e8d8bef9SDimitry Andric     return;
10640b57cec5SDimitry Andric   case IIT_TOKEN:
10650b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
10660b57cec5SDimitry Andric     return;
10670b57cec5SDimitry Andric   case IIT_METADATA:
10680b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
10690b57cec5SDimitry Andric     return;
10700b57cec5SDimitry Andric   case IIT_F16:
10710b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
10720b57cec5SDimitry Andric     return;
10735ffd83dbSDimitry Andric   case IIT_BF16:
10745ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::BFloat, 0));
10755ffd83dbSDimitry Andric     return;
10760b57cec5SDimitry Andric   case IIT_F32:
10770b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
10780b57cec5SDimitry Andric     return;
10790b57cec5SDimitry Andric   case IIT_F64:
10800b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
10810b57cec5SDimitry Andric     return;
10820b57cec5SDimitry Andric   case IIT_F128:
10830b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0));
10840b57cec5SDimitry Andric     return;
1085349cc55cSDimitry Andric   case IIT_PPCF128:
1086349cc55cSDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::PPCQuad, 0));
1087349cc55cSDimitry Andric     return;
10880b57cec5SDimitry Andric   case IIT_I1:
10890b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
10900b57cec5SDimitry Andric     return;
109181ad6265SDimitry Andric   case IIT_I2:
109281ad6265SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 2));
109381ad6265SDimitry Andric     return;
109481ad6265SDimitry Andric   case IIT_I4:
109581ad6265SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 4));
109681ad6265SDimitry Andric     return;
1097*06c3fb27SDimitry Andric   case IIT_AARCH64_SVCOUNT:
1098*06c3fb27SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::AArch64Svcount, 0));
1099*06c3fb27SDimitry Andric     return;
11000b57cec5SDimitry Andric   case IIT_I8:
11010b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
11020b57cec5SDimitry Andric     return;
11030b57cec5SDimitry Andric   case IIT_I16:
11040b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
11050b57cec5SDimitry Andric     return;
11060b57cec5SDimitry Andric   case IIT_I32:
11070b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
11080b57cec5SDimitry Andric     return;
11090b57cec5SDimitry Andric   case IIT_I64:
11100b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
11110b57cec5SDimitry Andric     return;
11120b57cec5SDimitry Andric   case IIT_I128:
11130b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
11140b57cec5SDimitry Andric     return;
11150b57cec5SDimitry Andric   case IIT_V1:
11165ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(1, IsScalableVector));
11175ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11180b57cec5SDimitry Andric     return;
11190b57cec5SDimitry Andric   case IIT_V2:
11205ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(2, IsScalableVector));
11215ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11220b57cec5SDimitry Andric     return;
11230eae32dcSDimitry Andric   case IIT_V3:
11240eae32dcSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(3, IsScalableVector));
11250eae32dcSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11260eae32dcSDimitry Andric     return;
11270b57cec5SDimitry Andric   case IIT_V4:
11285ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(4, IsScalableVector));
11295ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11300b57cec5SDimitry Andric     return;
11310b57cec5SDimitry Andric   case IIT_V8:
11325ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(8, IsScalableVector));
11335ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11340b57cec5SDimitry Andric     return;
11350b57cec5SDimitry Andric   case IIT_V16:
11365ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(16, IsScalableVector));
11375ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11380b57cec5SDimitry Andric     return;
11390b57cec5SDimitry Andric   case IIT_V32:
11405ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(32, IsScalableVector));
11415ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11420b57cec5SDimitry Andric     return;
11430b57cec5SDimitry Andric   case IIT_V64:
11445ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(64, IsScalableVector));
11455ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11465ffd83dbSDimitry Andric     return;
11475ffd83dbSDimitry Andric   case IIT_V128:
11485ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(128, IsScalableVector));
11495ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11500b57cec5SDimitry Andric     return;
1151e8d8bef9SDimitry Andric   case IIT_V256:
1152e8d8bef9SDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(256, IsScalableVector));
1153e8d8bef9SDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
1154e8d8bef9SDimitry Andric     return;
11550b57cec5SDimitry Andric   case IIT_V512:
11565ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(512, IsScalableVector));
11575ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11580b57cec5SDimitry Andric     return;
11590b57cec5SDimitry Andric   case IIT_V1024:
11605ffd83dbSDimitry Andric     OutputTable.push_back(IITDescriptor::getVector(1024, IsScalableVector));
11615ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
11620b57cec5SDimitry Andric     return;
11630eae32dcSDimitry Andric   case IIT_EXTERNREF:
11640eae32dcSDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 10));
11650eae32dcSDimitry Andric     return;
11660eae32dcSDimitry Andric   case IIT_FUNCREF:
11670eae32dcSDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 20));
11680eae32dcSDimitry Andric     return;
11690b57cec5SDimitry Andric   case IIT_PTR:
11700b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
11710b57cec5SDimitry Andric     return;
1172*06c3fb27SDimitry Andric   case IIT_ANYPTR: // [ANYPTR addrspace]
11730b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
11740b57cec5SDimitry Andric                                              Infos[NextElt++]));
11750b57cec5SDimitry Andric     return;
11760b57cec5SDimitry Andric   case IIT_ARG: {
11770b57cec5SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
11780b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
11790b57cec5SDimitry Andric     return;
11800b57cec5SDimitry Andric   }
11810b57cec5SDimitry Andric   case IIT_EXTEND_ARG: {
11820b57cec5SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
11830b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
11840b57cec5SDimitry Andric                                              ArgInfo));
11850b57cec5SDimitry Andric     return;
11860b57cec5SDimitry Andric   }
11870b57cec5SDimitry Andric   case IIT_TRUNC_ARG: {
11880b57cec5SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
11890b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
11900b57cec5SDimitry Andric                                              ArgInfo));
11910b57cec5SDimitry Andric     return;
11920b57cec5SDimitry Andric   }
11930b57cec5SDimitry Andric   case IIT_HALF_VEC_ARG: {
11940b57cec5SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
11950b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
11960b57cec5SDimitry Andric                                              ArgInfo));
11970b57cec5SDimitry Andric     return;
11980b57cec5SDimitry Andric   }
11990b57cec5SDimitry Andric   case IIT_SAME_VEC_WIDTH_ARG: {
12000b57cec5SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
12010b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
12020b57cec5SDimitry Andric                                              ArgInfo));
12030b57cec5SDimitry Andric     return;
12040b57cec5SDimitry Andric   }
12050b57cec5SDimitry Andric   case IIT_VEC_OF_ANYPTRS_TO_ELT: {
12060b57cec5SDimitry Andric     unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
12070b57cec5SDimitry Andric     unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
12080b57cec5SDimitry Andric     OutputTable.push_back(
12090b57cec5SDimitry Andric         IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo));
12100b57cec5SDimitry Andric     return;
12110b57cec5SDimitry Andric   }
12120b57cec5SDimitry Andric   case IIT_EMPTYSTRUCT:
12130b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
12140b57cec5SDimitry Andric     return;
1215bdd1243dSDimitry Andric   case IIT_STRUCT9: ++StructElts; [[fallthrough]];
1216bdd1243dSDimitry Andric   case IIT_STRUCT8: ++StructElts; [[fallthrough]];
1217bdd1243dSDimitry Andric   case IIT_STRUCT7: ++StructElts; [[fallthrough]];
1218bdd1243dSDimitry Andric   case IIT_STRUCT6: ++StructElts; [[fallthrough]];
1219bdd1243dSDimitry Andric   case IIT_STRUCT5: ++StructElts; [[fallthrough]];
1220bdd1243dSDimitry Andric   case IIT_STRUCT4: ++StructElts; [[fallthrough]];
1221bdd1243dSDimitry Andric   case IIT_STRUCT3: ++StructElts; [[fallthrough]];
12220b57cec5SDimitry Andric   case IIT_STRUCT2: {
12230b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric     for (unsigned i = 0; i != StructElts; ++i)
12265ffd83dbSDimitry Andric       DecodeIITType(NextElt, Infos, Info, OutputTable);
12270b57cec5SDimitry Andric     return;
12280b57cec5SDimitry Andric   }
12298bcb0991SDimitry Andric   case IIT_SUBDIVIDE2_ARG: {
12308bcb0991SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
12318bcb0991SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide2Argument,
12328bcb0991SDimitry Andric                                              ArgInfo));
12338bcb0991SDimitry Andric     return;
12348bcb0991SDimitry Andric   }
12358bcb0991SDimitry Andric   case IIT_SUBDIVIDE4_ARG: {
12368bcb0991SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
12378bcb0991SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide4Argument,
12388bcb0991SDimitry Andric                                              ArgInfo));
12398bcb0991SDimitry Andric     return;
12408bcb0991SDimitry Andric   }
12410b57cec5SDimitry Andric   case IIT_VEC_ELEMENT: {
12420b57cec5SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
12430b57cec5SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecElementArgument,
12440b57cec5SDimitry Andric                                              ArgInfo));
12450b57cec5SDimitry Andric     return;
12460b57cec5SDimitry Andric   }
12478bcb0991SDimitry Andric   case IIT_SCALABLE_VEC: {
12485ffd83dbSDimitry Andric     DecodeIITType(NextElt, Infos, Info, OutputTable);
12498bcb0991SDimitry Andric     return;
12508bcb0991SDimitry Andric   }
12518bcb0991SDimitry Andric   case IIT_VEC_OF_BITCASTS_TO_INT: {
12528bcb0991SDimitry Andric     unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
12538bcb0991SDimitry Andric     OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfBitcastsToInt,
12548bcb0991SDimitry Andric                                              ArgInfo));
12558bcb0991SDimitry Andric     return;
12568bcb0991SDimitry Andric   }
12570b57cec5SDimitry Andric   }
12580b57cec5SDimitry Andric   llvm_unreachable("unhandled");
12590b57cec5SDimitry Andric }
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric #define GET_INTRINSIC_GENERATOR_GLOBAL
12620b57cec5SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
12630b57cec5SDimitry Andric #undef GET_INTRINSIC_GENERATOR_GLOBAL
12640b57cec5SDimitry Andric 
12650b57cec5SDimitry Andric void Intrinsic::getIntrinsicInfoTableEntries(ID id,
12660b57cec5SDimitry Andric                                              SmallVectorImpl<IITDescriptor> &T){
12670b57cec5SDimitry Andric   // Check to see if the intrinsic's type was expressible by the table.
12680b57cec5SDimitry Andric   unsigned TableVal = IIT_Table[id-1];
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric   // Decode the TableVal into an array of IITValues.
12710b57cec5SDimitry Andric   SmallVector<unsigned char, 8> IITValues;
12720b57cec5SDimitry Andric   ArrayRef<unsigned char> IITEntries;
12730b57cec5SDimitry Andric   unsigned NextElt = 0;
12740b57cec5SDimitry Andric   if ((TableVal >> 31) != 0) {
12750b57cec5SDimitry Andric     // This is an offset into the IIT_LongEncodingTable.
12760b57cec5SDimitry Andric     IITEntries = IIT_LongEncodingTable;
12770b57cec5SDimitry Andric 
12780b57cec5SDimitry Andric     // Strip sentinel bit.
12790b57cec5SDimitry Andric     NextElt = (TableVal << 1) >> 1;
12800b57cec5SDimitry Andric   } else {
12810b57cec5SDimitry Andric     // Decode the TableVal into an array of IITValues.  If the entry was encoded
12820b57cec5SDimitry Andric     // into a single word in the table itself, decode it now.
12830b57cec5SDimitry Andric     do {
12840b57cec5SDimitry Andric       IITValues.push_back(TableVal & 0xF);
12850b57cec5SDimitry Andric       TableVal >>= 4;
12860b57cec5SDimitry Andric     } while (TableVal);
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric     IITEntries = IITValues;
12890b57cec5SDimitry Andric     NextElt = 0;
12900b57cec5SDimitry Andric   }
12910b57cec5SDimitry Andric 
12920b57cec5SDimitry Andric   // Okay, decode the table into the output vector of IITDescriptors.
12935ffd83dbSDimitry Andric   DecodeIITType(NextElt, IITEntries, IIT_Done, T);
12940b57cec5SDimitry Andric   while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
12955ffd83dbSDimitry Andric     DecodeIITType(NextElt, IITEntries, IIT_Done, T);
12960b57cec5SDimitry Andric }
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
12990b57cec5SDimitry Andric                              ArrayRef<Type*> Tys, LLVMContext &Context) {
13000b57cec5SDimitry Andric   using namespace Intrinsic;
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric   IITDescriptor D = Infos.front();
13030b57cec5SDimitry Andric   Infos = Infos.slice(1);
13040b57cec5SDimitry Andric 
13050b57cec5SDimitry Andric   switch (D.Kind) {
13060b57cec5SDimitry Andric   case IITDescriptor::Void: return Type::getVoidTy(Context);
13070b57cec5SDimitry Andric   case IITDescriptor::VarArg: return Type::getVoidTy(Context);
13080b57cec5SDimitry Andric   case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
1309e8d8bef9SDimitry Andric   case IITDescriptor::AMX: return Type::getX86_AMXTy(Context);
13100b57cec5SDimitry Andric   case IITDescriptor::Token: return Type::getTokenTy(Context);
13110b57cec5SDimitry Andric   case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
13120b57cec5SDimitry Andric   case IITDescriptor::Half: return Type::getHalfTy(Context);
13135ffd83dbSDimitry Andric   case IITDescriptor::BFloat: return Type::getBFloatTy(Context);
13140b57cec5SDimitry Andric   case IITDescriptor::Float: return Type::getFloatTy(Context);
13150b57cec5SDimitry Andric   case IITDescriptor::Double: return Type::getDoubleTy(Context);
13160b57cec5SDimitry Andric   case IITDescriptor::Quad: return Type::getFP128Ty(Context);
1317349cc55cSDimitry Andric   case IITDescriptor::PPCQuad: return Type::getPPC_FP128Ty(Context);
1318*06c3fb27SDimitry Andric   case IITDescriptor::AArch64Svcount:
1319*06c3fb27SDimitry Andric     return TargetExtType::get(Context, "aarch64.svcount");
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric   case IITDescriptor::Integer:
13220b57cec5SDimitry Andric     return IntegerType::get(Context, D.Integer_Width);
13230b57cec5SDimitry Andric   case IITDescriptor::Vector:
13245ffd83dbSDimitry Andric     return VectorType::get(DecodeFixedType(Infos, Tys, Context),
13255ffd83dbSDimitry Andric                            D.Vector_Width);
13260b57cec5SDimitry Andric   case IITDescriptor::Pointer:
1327*06c3fb27SDimitry Andric     return PointerType::get(Context, D.Pointer_AddressSpace);
13280b57cec5SDimitry Andric   case IITDescriptor::Struct: {
13290b57cec5SDimitry Andric     SmallVector<Type *, 8> Elts;
13300b57cec5SDimitry Andric     for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
13310b57cec5SDimitry Andric       Elts.push_back(DecodeFixedType(Infos, Tys, Context));
13320b57cec5SDimitry Andric     return StructType::get(Context, Elts);
13330b57cec5SDimitry Andric   }
13340b57cec5SDimitry Andric   case IITDescriptor::Argument:
13350b57cec5SDimitry Andric     return Tys[D.getArgumentNumber()];
13360b57cec5SDimitry Andric   case IITDescriptor::ExtendArgument: {
13370b57cec5SDimitry Andric     Type *Ty = Tys[D.getArgumentNumber()];
13380b57cec5SDimitry Andric     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
13390b57cec5SDimitry Andric       return VectorType::getExtendedElementVectorType(VTy);
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric     return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
13420b57cec5SDimitry Andric   }
13430b57cec5SDimitry Andric   case IITDescriptor::TruncArgument: {
13440b57cec5SDimitry Andric     Type *Ty = Tys[D.getArgumentNumber()];
13450b57cec5SDimitry Andric     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
13460b57cec5SDimitry Andric       return VectorType::getTruncatedElementVectorType(VTy);
13470b57cec5SDimitry Andric 
13480b57cec5SDimitry Andric     IntegerType *ITy = cast<IntegerType>(Ty);
13490b57cec5SDimitry Andric     assert(ITy->getBitWidth() % 2 == 0);
13500b57cec5SDimitry Andric     return IntegerType::get(Context, ITy->getBitWidth() / 2);
13510b57cec5SDimitry Andric   }
13528bcb0991SDimitry Andric   case IITDescriptor::Subdivide2Argument:
13538bcb0991SDimitry Andric   case IITDescriptor::Subdivide4Argument: {
13548bcb0991SDimitry Andric     Type *Ty = Tys[D.getArgumentNumber()];
13558bcb0991SDimitry Andric     VectorType *VTy = dyn_cast<VectorType>(Ty);
13568bcb0991SDimitry Andric     assert(VTy && "Expected an argument of Vector Type");
13578bcb0991SDimitry Andric     int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
13588bcb0991SDimitry Andric     return VectorType::getSubdividedVectorType(VTy, SubDivs);
13598bcb0991SDimitry Andric   }
13600b57cec5SDimitry Andric   case IITDescriptor::HalfVecArgument:
13610b57cec5SDimitry Andric     return VectorType::getHalfElementsVectorType(cast<VectorType>(
13620b57cec5SDimitry Andric                                                   Tys[D.getArgumentNumber()]));
13630b57cec5SDimitry Andric   case IITDescriptor::SameVecWidthArgument: {
13640b57cec5SDimitry Andric     Type *EltTy = DecodeFixedType(Infos, Tys, Context);
13650b57cec5SDimitry Andric     Type *Ty = Tys[D.getArgumentNumber()];
13660b57cec5SDimitry Andric     if (auto *VTy = dyn_cast<VectorType>(Ty))
13678bcb0991SDimitry Andric       return VectorType::get(EltTy, VTy->getElementCount());
13680b57cec5SDimitry Andric     return EltTy;
13690b57cec5SDimitry Andric   }
13700b57cec5SDimitry Andric   case IITDescriptor::VecElementArgument: {
13710b57cec5SDimitry Andric     Type *Ty = Tys[D.getArgumentNumber()];
13720b57cec5SDimitry Andric     if (VectorType *VTy = dyn_cast<VectorType>(Ty))
13730b57cec5SDimitry Andric       return VTy->getElementType();
13740b57cec5SDimitry Andric     llvm_unreachable("Expected an argument of Vector Type");
13750b57cec5SDimitry Andric   }
13768bcb0991SDimitry Andric   case IITDescriptor::VecOfBitcastsToInt: {
13778bcb0991SDimitry Andric     Type *Ty = Tys[D.getArgumentNumber()];
13788bcb0991SDimitry Andric     VectorType *VTy = dyn_cast<VectorType>(Ty);
13798bcb0991SDimitry Andric     assert(VTy && "Expected an argument of Vector Type");
13808bcb0991SDimitry Andric     return VectorType::getInteger(VTy);
13818bcb0991SDimitry Andric   }
13820b57cec5SDimitry Andric   case IITDescriptor::VecOfAnyPtrsToElt:
13830b57cec5SDimitry Andric     // Return the overloaded type (which determines the pointers address space)
13840b57cec5SDimitry Andric     return Tys[D.getOverloadArgNumber()];
13850b57cec5SDimitry Andric   }
13860b57cec5SDimitry Andric   llvm_unreachable("unhandled");
13870b57cec5SDimitry Andric }
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric FunctionType *Intrinsic::getType(LLVMContext &Context,
13900b57cec5SDimitry Andric                                  ID id, ArrayRef<Type*> Tys) {
13910b57cec5SDimitry Andric   SmallVector<IITDescriptor, 8> Table;
13920b57cec5SDimitry Andric   getIntrinsicInfoTableEntries(id, Table);
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric   ArrayRef<IITDescriptor> TableRef = Table;
13950b57cec5SDimitry Andric   Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   SmallVector<Type*, 8> ArgTys;
13980b57cec5SDimitry Andric   while (!TableRef.empty())
13990b57cec5SDimitry Andric     ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric   // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
14020b57cec5SDimitry Andric   // If we see void type as the type of the last argument, it is vararg intrinsic
14030b57cec5SDimitry Andric   if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
14040b57cec5SDimitry Andric     ArgTys.pop_back();
14050b57cec5SDimitry Andric     return FunctionType::get(ResultTy, ArgTys, true);
14060b57cec5SDimitry Andric   }
14070b57cec5SDimitry Andric   return FunctionType::get(ResultTy, ArgTys, false);
14080b57cec5SDimitry Andric }
14090b57cec5SDimitry Andric 
14100b57cec5SDimitry Andric bool Intrinsic::isOverloaded(ID id) {
14110b57cec5SDimitry Andric #define GET_INTRINSIC_OVERLOAD_TABLE
14120b57cec5SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
14130b57cec5SDimitry Andric #undef GET_INTRINSIC_OVERLOAD_TABLE
14140b57cec5SDimitry Andric }
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric /// This defines the "Intrinsic::getAttributes(ID id)" method.
14170b57cec5SDimitry Andric #define GET_INTRINSIC_ATTRIBUTES
14180b57cec5SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
14190b57cec5SDimitry Andric #undef GET_INTRINSIC_ATTRIBUTES
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
14220b57cec5SDimitry Andric   // There can never be multiple globals with the same name of different types,
14230b57cec5SDimitry Andric   // because intrinsics must be a specific type.
1424fe6060f1SDimitry Andric   auto *FT = getType(M->getContext(), id, Tys);
14250b57cec5SDimitry Andric   return cast<Function>(
1426bdd1243dSDimitry Andric       M->getOrInsertFunction(
1427bdd1243dSDimitry Andric            Tys.empty() ? getName(id) : getName(id, Tys, M, FT), FT)
14280b57cec5SDimitry Andric           .getCallee());
14290b57cec5SDimitry Andric }
14300b57cec5SDimitry Andric 
143181ad6265SDimitry Andric // This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method.
143281ad6265SDimitry Andric #define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
14330b57cec5SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
143481ad6265SDimitry Andric #undef GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
14350b57cec5SDimitry Andric 
14360b57cec5SDimitry Andric // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
14370b57cec5SDimitry Andric #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
14380b57cec5SDimitry Andric #include "llvm/IR/IntrinsicImpl.inc"
14390b57cec5SDimitry Andric #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
14400b57cec5SDimitry Andric 
14410b57cec5SDimitry Andric using DeferredIntrinsicMatchPair =
14420b57cec5SDimitry Andric     std::pair<Type *, ArrayRef<Intrinsic::IITDescriptor>>;
14430b57cec5SDimitry Andric 
14440b57cec5SDimitry Andric static bool matchIntrinsicType(
14450b57cec5SDimitry Andric     Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
14460b57cec5SDimitry Andric     SmallVectorImpl<Type *> &ArgTys,
14470b57cec5SDimitry Andric     SmallVectorImpl<DeferredIntrinsicMatchPair> &DeferredChecks,
14480b57cec5SDimitry Andric     bool IsDeferredCheck) {
14490b57cec5SDimitry Andric   using namespace Intrinsic;
14500b57cec5SDimitry Andric 
14510b57cec5SDimitry Andric   // If we ran out of descriptors, there are too many arguments.
14520b57cec5SDimitry Andric   if (Infos.empty()) return true;
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric   // Do this before slicing off the 'front' part
14550b57cec5SDimitry Andric   auto InfosRef = Infos;
14560b57cec5SDimitry Andric   auto DeferCheck = [&DeferredChecks, &InfosRef](Type *T) {
14570b57cec5SDimitry Andric     DeferredChecks.emplace_back(T, InfosRef);
14580b57cec5SDimitry Andric     return false;
14590b57cec5SDimitry Andric   };
14600b57cec5SDimitry Andric 
14610b57cec5SDimitry Andric   IITDescriptor D = Infos.front();
14620b57cec5SDimitry Andric   Infos = Infos.slice(1);
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric   switch (D.Kind) {
14650b57cec5SDimitry Andric     case IITDescriptor::Void: return !Ty->isVoidTy();
14660b57cec5SDimitry Andric     case IITDescriptor::VarArg: return true;
14670b57cec5SDimitry Andric     case IITDescriptor::MMX:  return !Ty->isX86_MMXTy();
1468e8d8bef9SDimitry Andric     case IITDescriptor::AMX:  return !Ty->isX86_AMXTy();
14690b57cec5SDimitry Andric     case IITDescriptor::Token: return !Ty->isTokenTy();
14700b57cec5SDimitry Andric     case IITDescriptor::Metadata: return !Ty->isMetadataTy();
14710b57cec5SDimitry Andric     case IITDescriptor::Half: return !Ty->isHalfTy();
14725ffd83dbSDimitry Andric     case IITDescriptor::BFloat: return !Ty->isBFloatTy();
14730b57cec5SDimitry Andric     case IITDescriptor::Float: return !Ty->isFloatTy();
14740b57cec5SDimitry Andric     case IITDescriptor::Double: return !Ty->isDoubleTy();
14750b57cec5SDimitry Andric     case IITDescriptor::Quad: return !Ty->isFP128Ty();
1476349cc55cSDimitry Andric     case IITDescriptor::PPCQuad: return !Ty->isPPC_FP128Ty();
14770b57cec5SDimitry Andric     case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1478*06c3fb27SDimitry Andric     case IITDescriptor::AArch64Svcount:
1479*06c3fb27SDimitry Andric       return !isa<TargetExtType>(Ty) ||
1480*06c3fb27SDimitry Andric              cast<TargetExtType>(Ty)->getName() != "aarch64.svcount";
14810b57cec5SDimitry Andric     case IITDescriptor::Vector: {
14820b57cec5SDimitry Andric       VectorType *VT = dyn_cast<VectorType>(Ty);
14835ffd83dbSDimitry Andric       return !VT || VT->getElementCount() != D.Vector_Width ||
14840b57cec5SDimitry Andric              matchIntrinsicType(VT->getElementType(), Infos, ArgTys,
14850b57cec5SDimitry Andric                                 DeferredChecks, IsDeferredCheck);
14860b57cec5SDimitry Andric     }
14870b57cec5SDimitry Andric     case IITDescriptor::Pointer: {
14880b57cec5SDimitry Andric       PointerType *PT = dyn_cast<PointerType>(Ty);
1489*06c3fb27SDimitry Andric       return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace;
14900b57cec5SDimitry Andric     }
14910b57cec5SDimitry Andric 
14920b57cec5SDimitry Andric     case IITDescriptor::Struct: {
14930b57cec5SDimitry Andric       StructType *ST = dyn_cast<StructType>(Ty);
149481ad6265SDimitry Andric       if (!ST || !ST->isLiteral() || ST->isPacked() ||
149581ad6265SDimitry Andric           ST->getNumElements() != D.Struct_NumElements)
14960b57cec5SDimitry Andric         return true;
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric       for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
14990b57cec5SDimitry Andric         if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys,
15000b57cec5SDimitry Andric                                DeferredChecks, IsDeferredCheck))
15010b57cec5SDimitry Andric           return true;
15020b57cec5SDimitry Andric       return false;
15030b57cec5SDimitry Andric     }
15040b57cec5SDimitry Andric 
15050b57cec5SDimitry Andric     case IITDescriptor::Argument:
15060b57cec5SDimitry Andric       // If this is the second occurrence of an argument,
15070b57cec5SDimitry Andric       // verify that the later instance matches the previous instance.
15080b57cec5SDimitry Andric       if (D.getArgumentNumber() < ArgTys.size())
15090b57cec5SDimitry Andric         return Ty != ArgTys[D.getArgumentNumber()];
15100b57cec5SDimitry Andric 
15110b57cec5SDimitry Andric       if (D.getArgumentNumber() > ArgTys.size() ||
15120b57cec5SDimitry Andric           D.getArgumentKind() == IITDescriptor::AK_MatchType)
15130b57cec5SDimitry Andric         return IsDeferredCheck || DeferCheck(Ty);
15140b57cec5SDimitry Andric 
15150b57cec5SDimitry Andric       assert(D.getArgumentNumber() == ArgTys.size() && !IsDeferredCheck &&
15160b57cec5SDimitry Andric              "Table consistency error");
15170b57cec5SDimitry Andric       ArgTys.push_back(Ty);
15180b57cec5SDimitry Andric 
15190b57cec5SDimitry Andric       switch (D.getArgumentKind()) {
15200b57cec5SDimitry Andric         case IITDescriptor::AK_Any:        return false; // Success
15210b57cec5SDimitry Andric         case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
15220b57cec5SDimitry Andric         case IITDescriptor::AK_AnyFloat:   return !Ty->isFPOrFPVectorTy();
15230b57cec5SDimitry Andric         case IITDescriptor::AK_AnyVector:  return !isa<VectorType>(Ty);
15240b57cec5SDimitry Andric         case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
15250b57cec5SDimitry Andric         default:                           break;
15260b57cec5SDimitry Andric       }
15270b57cec5SDimitry Andric       llvm_unreachable("all argument kinds not covered");
15280b57cec5SDimitry Andric 
15290b57cec5SDimitry Andric     case IITDescriptor::ExtendArgument: {
15300b57cec5SDimitry Andric       // If this is a forward reference, defer the check for later.
15310b57cec5SDimitry Andric       if (D.getArgumentNumber() >= ArgTys.size())
15320b57cec5SDimitry Andric         return IsDeferredCheck || DeferCheck(Ty);
15330b57cec5SDimitry Andric 
15340b57cec5SDimitry Andric       Type *NewTy = ArgTys[D.getArgumentNumber()];
15350b57cec5SDimitry Andric       if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
15360b57cec5SDimitry Andric         NewTy = VectorType::getExtendedElementVectorType(VTy);
15370b57cec5SDimitry Andric       else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
15380b57cec5SDimitry Andric         NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
15390b57cec5SDimitry Andric       else
15400b57cec5SDimitry Andric         return true;
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric       return Ty != NewTy;
15430b57cec5SDimitry Andric     }
15440b57cec5SDimitry Andric     case IITDescriptor::TruncArgument: {
15450b57cec5SDimitry Andric       // If this is a forward reference, defer the check for later.
15460b57cec5SDimitry Andric       if (D.getArgumentNumber() >= ArgTys.size())
15470b57cec5SDimitry Andric         return IsDeferredCheck || DeferCheck(Ty);
15480b57cec5SDimitry Andric 
15490b57cec5SDimitry Andric       Type *NewTy = ArgTys[D.getArgumentNumber()];
15500b57cec5SDimitry Andric       if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
15510b57cec5SDimitry Andric         NewTy = VectorType::getTruncatedElementVectorType(VTy);
15520b57cec5SDimitry Andric       else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
15530b57cec5SDimitry Andric         NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
15540b57cec5SDimitry Andric       else
15550b57cec5SDimitry Andric         return true;
15560b57cec5SDimitry Andric 
15570b57cec5SDimitry Andric       return Ty != NewTy;
15580b57cec5SDimitry Andric     }
15590b57cec5SDimitry Andric     case IITDescriptor::HalfVecArgument:
15600b57cec5SDimitry Andric       // If this is a forward reference, defer the check for later.
15618bcb0991SDimitry Andric       if (D.getArgumentNumber() >= ArgTys.size())
15628bcb0991SDimitry Andric         return IsDeferredCheck || DeferCheck(Ty);
15638bcb0991SDimitry Andric       return !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
15640b57cec5SDimitry Andric              VectorType::getHalfElementsVectorType(
15650b57cec5SDimitry Andric                      cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
15660b57cec5SDimitry Andric     case IITDescriptor::SameVecWidthArgument: {
15670b57cec5SDimitry Andric       if (D.getArgumentNumber() >= ArgTys.size()) {
15680b57cec5SDimitry Andric         // Defer check and subsequent check for the vector element type.
15690b57cec5SDimitry Andric         Infos = Infos.slice(1);
15700b57cec5SDimitry Andric         return IsDeferredCheck || DeferCheck(Ty);
15710b57cec5SDimitry Andric       }
15720b57cec5SDimitry Andric       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
15730b57cec5SDimitry Andric       auto *ThisArgType = dyn_cast<VectorType>(Ty);
15740b57cec5SDimitry Andric       // Both must be vectors of the same number of elements or neither.
15750b57cec5SDimitry Andric       if ((ReferenceType != nullptr) != (ThisArgType != nullptr))
15760b57cec5SDimitry Andric         return true;
15770b57cec5SDimitry Andric       Type *EltTy = Ty;
15780b57cec5SDimitry Andric       if (ThisArgType) {
15798bcb0991SDimitry Andric         if (ReferenceType->getElementCount() !=
15808bcb0991SDimitry Andric             ThisArgType->getElementCount())
15810b57cec5SDimitry Andric           return true;
15825ffd83dbSDimitry Andric         EltTy = ThisArgType->getElementType();
15830b57cec5SDimitry Andric       }
15840b57cec5SDimitry Andric       return matchIntrinsicType(EltTy, Infos, ArgTys, DeferredChecks,
15850b57cec5SDimitry Andric                                 IsDeferredCheck);
15860b57cec5SDimitry Andric     }
15870b57cec5SDimitry Andric     case IITDescriptor::VecOfAnyPtrsToElt: {
15880b57cec5SDimitry Andric       unsigned RefArgNumber = D.getRefArgNumber();
15890b57cec5SDimitry Andric       if (RefArgNumber >= ArgTys.size()) {
15900b57cec5SDimitry Andric         if (IsDeferredCheck)
15910b57cec5SDimitry Andric           return true;
15920b57cec5SDimitry Andric         // If forward referencing, already add the pointer-vector type and
15930b57cec5SDimitry Andric         // defer the checks for later.
15940b57cec5SDimitry Andric         ArgTys.push_back(Ty);
15950b57cec5SDimitry Andric         return DeferCheck(Ty);
15960b57cec5SDimitry Andric       }
15970b57cec5SDimitry Andric 
15980b57cec5SDimitry Andric       if (!IsDeferredCheck){
15990b57cec5SDimitry Andric         assert(D.getOverloadArgNumber() == ArgTys.size() &&
16000b57cec5SDimitry Andric                "Table consistency error");
16010b57cec5SDimitry Andric         ArgTys.push_back(Ty);
16020b57cec5SDimitry Andric       }
16030b57cec5SDimitry Andric 
16040b57cec5SDimitry Andric       // Verify the overloaded type "matches" the Ref type.
16050b57cec5SDimitry Andric       // i.e. Ty is a vector with the same width as Ref.
16060b57cec5SDimitry Andric       // Composed of pointers to the same element type as Ref.
1607e8d8bef9SDimitry Andric       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
1608e8d8bef9SDimitry Andric       auto *ThisArgVecTy = dyn_cast<VectorType>(Ty);
16090b57cec5SDimitry Andric       if (!ThisArgVecTy || !ReferenceType ||
1610e8d8bef9SDimitry Andric           (ReferenceType->getElementCount() != ThisArgVecTy->getElementCount()))
16110b57cec5SDimitry Andric         return true;
1612*06c3fb27SDimitry Andric       return !ThisArgVecTy->getElementType()->isPointerTy();
16130b57cec5SDimitry Andric     }
16140b57cec5SDimitry Andric     case IITDescriptor::VecElementArgument: {
16150b57cec5SDimitry Andric       if (D.getArgumentNumber() >= ArgTys.size())
16160b57cec5SDimitry Andric         return IsDeferredCheck ? true : DeferCheck(Ty);
16170b57cec5SDimitry Andric       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
16180b57cec5SDimitry Andric       return !ReferenceType || Ty != ReferenceType->getElementType();
16190b57cec5SDimitry Andric     }
16208bcb0991SDimitry Andric     case IITDescriptor::Subdivide2Argument:
16218bcb0991SDimitry Andric     case IITDescriptor::Subdivide4Argument: {
16228bcb0991SDimitry Andric       // If this is a forward reference, defer the check for later.
16238bcb0991SDimitry Andric       if (D.getArgumentNumber() >= ArgTys.size())
16248bcb0991SDimitry Andric         return IsDeferredCheck || DeferCheck(Ty);
16258bcb0991SDimitry Andric 
16268bcb0991SDimitry Andric       Type *NewTy = ArgTys[D.getArgumentNumber()];
16278bcb0991SDimitry Andric       if (auto *VTy = dyn_cast<VectorType>(NewTy)) {
16288bcb0991SDimitry Andric         int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
16298bcb0991SDimitry Andric         NewTy = VectorType::getSubdividedVectorType(VTy, SubDivs);
16308bcb0991SDimitry Andric         return Ty != NewTy;
16318bcb0991SDimitry Andric       }
16328bcb0991SDimitry Andric       return true;
16338bcb0991SDimitry Andric     }
16348bcb0991SDimitry Andric     case IITDescriptor::VecOfBitcastsToInt: {
16358bcb0991SDimitry Andric       if (D.getArgumentNumber() >= ArgTys.size())
16368bcb0991SDimitry Andric         return IsDeferredCheck || DeferCheck(Ty);
16378bcb0991SDimitry Andric       auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
16388bcb0991SDimitry Andric       auto *ThisArgVecTy = dyn_cast<VectorType>(Ty);
16398bcb0991SDimitry Andric       if (!ThisArgVecTy || !ReferenceType)
16408bcb0991SDimitry Andric         return true;
16418bcb0991SDimitry Andric       return ThisArgVecTy != VectorType::getInteger(ReferenceType);
16428bcb0991SDimitry Andric     }
16430b57cec5SDimitry Andric   }
16440b57cec5SDimitry Andric   llvm_unreachable("unhandled");
16450b57cec5SDimitry Andric }
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric Intrinsic::MatchIntrinsicTypesResult
16480b57cec5SDimitry Andric Intrinsic::matchIntrinsicSignature(FunctionType *FTy,
16490b57cec5SDimitry Andric                                    ArrayRef<Intrinsic::IITDescriptor> &Infos,
16500b57cec5SDimitry Andric                                    SmallVectorImpl<Type *> &ArgTys) {
16510b57cec5SDimitry Andric   SmallVector<DeferredIntrinsicMatchPair, 2> DeferredChecks;
16520b57cec5SDimitry Andric   if (matchIntrinsicType(FTy->getReturnType(), Infos, ArgTys, DeferredChecks,
16530b57cec5SDimitry Andric                          false))
16540b57cec5SDimitry Andric     return MatchIntrinsicTypes_NoMatchRet;
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric   unsigned NumDeferredReturnChecks = DeferredChecks.size();
16570b57cec5SDimitry Andric 
1658bdd1243dSDimitry Andric   for (auto *Ty : FTy->params())
16590b57cec5SDimitry Andric     if (matchIntrinsicType(Ty, Infos, ArgTys, DeferredChecks, false))
16600b57cec5SDimitry Andric       return MatchIntrinsicTypes_NoMatchArg;
16610b57cec5SDimitry Andric 
16620b57cec5SDimitry Andric   for (unsigned I = 0, E = DeferredChecks.size(); I != E; ++I) {
16630b57cec5SDimitry Andric     DeferredIntrinsicMatchPair &Check = DeferredChecks[I];
16640b57cec5SDimitry Andric     if (matchIntrinsicType(Check.first, Check.second, ArgTys, DeferredChecks,
16650b57cec5SDimitry Andric                            true))
16660b57cec5SDimitry Andric       return I < NumDeferredReturnChecks ? MatchIntrinsicTypes_NoMatchRet
16670b57cec5SDimitry Andric                                          : MatchIntrinsicTypes_NoMatchArg;
16680b57cec5SDimitry Andric   }
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   return MatchIntrinsicTypes_Match;
16710b57cec5SDimitry Andric }
16720b57cec5SDimitry Andric 
16730b57cec5SDimitry Andric bool
16740b57cec5SDimitry Andric Intrinsic::matchIntrinsicVarArg(bool isVarArg,
16750b57cec5SDimitry Andric                                 ArrayRef<Intrinsic::IITDescriptor> &Infos) {
16760b57cec5SDimitry Andric   // If there are no descriptors left, then it can't be a vararg.
16770b57cec5SDimitry Andric   if (Infos.empty())
16780b57cec5SDimitry Andric     return isVarArg;
16790b57cec5SDimitry Andric 
16800b57cec5SDimitry Andric   // There should be only one descriptor remaining at this point.
16810b57cec5SDimitry Andric   if (Infos.size() != 1)
16820b57cec5SDimitry Andric     return true;
16830b57cec5SDimitry Andric 
16840b57cec5SDimitry Andric   // Check and verify the descriptor.
16850b57cec5SDimitry Andric   IITDescriptor D = Infos.front();
16860b57cec5SDimitry Andric   Infos = Infos.slice(1);
16870b57cec5SDimitry Andric   if (D.Kind == IITDescriptor::VarArg)
16880b57cec5SDimitry Andric     return !isVarArg;
16890b57cec5SDimitry Andric 
16900b57cec5SDimitry Andric   return true;
16910b57cec5SDimitry Andric }
16920b57cec5SDimitry Andric 
16935ffd83dbSDimitry Andric bool Intrinsic::getIntrinsicSignature(Function *F,
16945ffd83dbSDimitry Andric                                       SmallVectorImpl<Type *> &ArgTys) {
16950b57cec5SDimitry Andric   Intrinsic::ID ID = F->getIntrinsicID();
16960b57cec5SDimitry Andric   if (!ID)
16975ffd83dbSDimitry Andric     return false;
16980b57cec5SDimitry Andric 
16990b57cec5SDimitry Andric   SmallVector<Intrinsic::IITDescriptor, 8> Table;
17000b57cec5SDimitry Andric   getIntrinsicInfoTableEntries(ID, Table);
17010b57cec5SDimitry Andric   ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
17020b57cec5SDimitry Andric 
17035ffd83dbSDimitry Andric   if (Intrinsic::matchIntrinsicSignature(F->getFunctionType(), TableRef,
17045ffd83dbSDimitry Andric                                          ArgTys) !=
17055ffd83dbSDimitry Andric       Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
17065ffd83dbSDimitry Andric     return false;
17075ffd83dbSDimitry Andric   }
17085ffd83dbSDimitry Andric   if (Intrinsic::matchIntrinsicVarArg(F->getFunctionType()->isVarArg(),
17095ffd83dbSDimitry Andric                                       TableRef))
17105ffd83dbSDimitry Andric     return false;
17115ffd83dbSDimitry Andric   return true;
17120b57cec5SDimitry Andric }
17130b57cec5SDimitry Andric 
1714bdd1243dSDimitry Andric std::optional<Function *> Intrinsic::remangleIntrinsicFunction(Function *F) {
17155ffd83dbSDimitry Andric   SmallVector<Type *, 4> ArgTys;
17165ffd83dbSDimitry Andric   if (!getIntrinsicSignature(F, ArgTys))
1717bdd1243dSDimitry Andric     return std::nullopt;
17185ffd83dbSDimitry Andric 
17195ffd83dbSDimitry Andric   Intrinsic::ID ID = F->getIntrinsicID();
17200b57cec5SDimitry Andric   StringRef Name = F->getName();
1721fe6060f1SDimitry Andric   std::string WantedName =
1722fe6060f1SDimitry Andric       Intrinsic::getName(ID, ArgTys, F->getParent(), F->getFunctionType());
1723fe6060f1SDimitry Andric   if (Name == WantedName)
1724bdd1243dSDimitry Andric     return std::nullopt;
17250b57cec5SDimitry Andric 
1726fe6060f1SDimitry Andric   Function *NewDecl = [&] {
1727fe6060f1SDimitry Andric     if (auto *ExistingGV = F->getParent()->getNamedValue(WantedName)) {
1728fe6060f1SDimitry Andric       if (auto *ExistingF = dyn_cast<Function>(ExistingGV))
1729fe6060f1SDimitry Andric         if (ExistingF->getFunctionType() == F->getFunctionType())
1730fe6060f1SDimitry Andric           return ExistingF;
1731fe6060f1SDimitry Andric 
1732fe6060f1SDimitry Andric       // The name already exists, but is not a function or has the wrong
1733fe6060f1SDimitry Andric       // prototype. Make place for the new one by renaming the old version.
1734fe6060f1SDimitry Andric       // Either this old version will be removed later on or the module is
1735fe6060f1SDimitry Andric       // invalid and we'll get an error.
1736fe6060f1SDimitry Andric       ExistingGV->setName(WantedName + ".renamed");
1737fe6060f1SDimitry Andric     }
1738fe6060f1SDimitry Andric     return Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1739fe6060f1SDimitry Andric   }();
1740fe6060f1SDimitry Andric 
17410b57cec5SDimitry Andric   NewDecl->setCallingConv(F->getCallingConv());
17425ffd83dbSDimitry Andric   assert(NewDecl->getFunctionType() == F->getFunctionType() &&
17435ffd83dbSDimitry Andric          "Shouldn't change the signature");
17440b57cec5SDimitry Andric   return NewDecl;
17450b57cec5SDimitry Andric }
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric /// hasAddressTaken - returns true if there are any uses of this function
17485ffd83dbSDimitry Andric /// other than direct calls or invokes to it. Optionally ignores callback
1749fe6060f1SDimitry Andric /// uses, assume like pointer annotation calls, and references in llvm.used
1750fe6060f1SDimitry Andric /// and llvm.compiler.used variables.
17515ffd83dbSDimitry Andric bool Function::hasAddressTaken(const User **PutOffender,
1752fe6060f1SDimitry Andric                                bool IgnoreCallbackUses,
1753349cc55cSDimitry Andric                                bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed,
1754349cc55cSDimitry Andric                                bool IgnoreARCAttachedCall) const {
17550b57cec5SDimitry Andric   for (const Use &U : uses()) {
17560b57cec5SDimitry Andric     const User *FU = U.getUser();
17570b57cec5SDimitry Andric     if (isa<BlockAddress>(FU))
17580b57cec5SDimitry Andric       continue;
17595ffd83dbSDimitry Andric 
17605ffd83dbSDimitry Andric     if (IgnoreCallbackUses) {
17615ffd83dbSDimitry Andric       AbstractCallSite ACS(&U);
17625ffd83dbSDimitry Andric       if (ACS && ACS.isCallbackCall())
17635ffd83dbSDimitry Andric         continue;
17645ffd83dbSDimitry Andric     }
17655ffd83dbSDimitry Andric 
17660b57cec5SDimitry Andric     const auto *Call = dyn_cast<CallBase>(FU);
17670b57cec5SDimitry Andric     if (!Call) {
1768bdd1243dSDimitry Andric       if (IgnoreAssumeLikeCalls &&
1769bdd1243dSDimitry Andric           isa<BitCastOperator, AddrSpaceCastOperator>(FU) &&
1770bdd1243dSDimitry Andric           all_of(FU->users(), [](const User *U) {
1771fe6060f1SDimitry Andric             if (const auto *I = dyn_cast<IntrinsicInst>(U))
1772fe6060f1SDimitry Andric               return I->isAssumeLikeIntrinsic();
1773fe6060f1SDimitry Andric             return false;
1774bdd1243dSDimitry Andric           })) {
1775fe6060f1SDimitry Andric         continue;
1776fe6060f1SDimitry Andric       }
1777bdd1243dSDimitry Andric 
1778fe6060f1SDimitry Andric       if (IgnoreLLVMUsed && !FU->user_empty()) {
1779fe6060f1SDimitry Andric         const User *FUU = FU;
1780bdd1243dSDimitry Andric         if (isa<BitCastOperator, AddrSpaceCastOperator>(FU) &&
1781bdd1243dSDimitry Andric             FU->hasOneUse() && !FU->user_begin()->user_empty())
1782fe6060f1SDimitry Andric           FUU = *FU->user_begin();
1783fe6060f1SDimitry Andric         if (llvm::all_of(FUU->users(), [](const User *U) {
1784fe6060f1SDimitry Andric               if (const auto *GV = dyn_cast<GlobalVariable>(U))
1785fe6060f1SDimitry Andric                 return GV->hasName() &&
1786fe6060f1SDimitry Andric                        (GV->getName().equals("llvm.compiler.used") ||
1787fe6060f1SDimitry Andric                         GV->getName().equals("llvm.used"));
1788fe6060f1SDimitry Andric               return false;
1789fe6060f1SDimitry Andric             }))
1790fe6060f1SDimitry Andric           continue;
1791fe6060f1SDimitry Andric       }
17920b57cec5SDimitry Andric       if (PutOffender)
17930b57cec5SDimitry Andric         *PutOffender = FU;
17940b57cec5SDimitry Andric       return true;
17950b57cec5SDimitry Andric     }
1796bdd1243dSDimitry Andric 
1797bdd1243dSDimitry Andric     if (IgnoreAssumeLikeCalls) {
1798bdd1243dSDimitry Andric       if (const auto *I = dyn_cast<IntrinsicInst>(Call))
1799bdd1243dSDimitry Andric         if (I->isAssumeLikeIntrinsic())
1800bdd1243dSDimitry Andric           continue;
1801bdd1243dSDimitry Andric     }
1802bdd1243dSDimitry Andric 
180381ad6265SDimitry Andric     if (!Call->isCallee(&U) || Call->getFunctionType() != getFunctionType()) {
1804349cc55cSDimitry Andric       if (IgnoreARCAttachedCall &&
1805349cc55cSDimitry Andric           Call->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall,
1806349cc55cSDimitry Andric                                       U.getOperandNo()))
1807349cc55cSDimitry Andric         continue;
1808349cc55cSDimitry Andric 
18090b57cec5SDimitry Andric       if (PutOffender)
18100b57cec5SDimitry Andric         *PutOffender = FU;
18110b57cec5SDimitry Andric       return true;
18120b57cec5SDimitry Andric     }
18130b57cec5SDimitry Andric   }
18140b57cec5SDimitry Andric   return false;
18150b57cec5SDimitry Andric }
18160b57cec5SDimitry Andric 
18170b57cec5SDimitry Andric bool Function::isDefTriviallyDead() const {
18180b57cec5SDimitry Andric   // Check the linkage
18190b57cec5SDimitry Andric   if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
18200b57cec5SDimitry Andric       !hasAvailableExternallyLinkage())
18210b57cec5SDimitry Andric     return false;
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric   // Check if the function is used by anything other than a blockaddress.
18240b57cec5SDimitry Andric   for (const User *U : users())
18250b57cec5SDimitry Andric     if (!isa<BlockAddress>(U))
18260b57cec5SDimitry Andric       return false;
18270b57cec5SDimitry Andric 
18280b57cec5SDimitry Andric   return true;
18290b57cec5SDimitry Andric }
18300b57cec5SDimitry Andric 
18310b57cec5SDimitry Andric /// callsFunctionThatReturnsTwice - Return true if the function has a call to
18320b57cec5SDimitry Andric /// setjmp or other function that gcc recognizes as "returning twice".
18330b57cec5SDimitry Andric bool Function::callsFunctionThatReturnsTwice() const {
18340b57cec5SDimitry Andric   for (const Instruction &I : instructions(this))
18350b57cec5SDimitry Andric     if (const auto *Call = dyn_cast<CallBase>(&I))
18360b57cec5SDimitry Andric       if (Call->hasFnAttr(Attribute::ReturnsTwice))
18370b57cec5SDimitry Andric         return true;
18380b57cec5SDimitry Andric 
18390b57cec5SDimitry Andric   return false;
18400b57cec5SDimitry Andric }
18410b57cec5SDimitry Andric 
18420b57cec5SDimitry Andric Constant *Function::getPersonalityFn() const {
18430b57cec5SDimitry Andric   assert(hasPersonalityFn() && getNumOperands());
18440b57cec5SDimitry Andric   return cast<Constant>(Op<0>());
18450b57cec5SDimitry Andric }
18460b57cec5SDimitry Andric 
18470b57cec5SDimitry Andric void Function::setPersonalityFn(Constant *Fn) {
18480b57cec5SDimitry Andric   setHungoffOperand<0>(Fn);
18490b57cec5SDimitry Andric   setValueSubclassDataBit(3, Fn != nullptr);
18500b57cec5SDimitry Andric }
18510b57cec5SDimitry Andric 
18520b57cec5SDimitry Andric Constant *Function::getPrefixData() const {
18530b57cec5SDimitry Andric   assert(hasPrefixData() && getNumOperands());
18540b57cec5SDimitry Andric   return cast<Constant>(Op<1>());
18550b57cec5SDimitry Andric }
18560b57cec5SDimitry Andric 
18570b57cec5SDimitry Andric void Function::setPrefixData(Constant *PrefixData) {
18580b57cec5SDimitry Andric   setHungoffOperand<1>(PrefixData);
18590b57cec5SDimitry Andric   setValueSubclassDataBit(1, PrefixData != nullptr);
18600b57cec5SDimitry Andric }
18610b57cec5SDimitry Andric 
18620b57cec5SDimitry Andric Constant *Function::getPrologueData() const {
18630b57cec5SDimitry Andric   assert(hasPrologueData() && getNumOperands());
18640b57cec5SDimitry Andric   return cast<Constant>(Op<2>());
18650b57cec5SDimitry Andric }
18660b57cec5SDimitry Andric 
18670b57cec5SDimitry Andric void Function::setPrologueData(Constant *PrologueData) {
18680b57cec5SDimitry Andric   setHungoffOperand<2>(PrologueData);
18690b57cec5SDimitry Andric   setValueSubclassDataBit(2, PrologueData != nullptr);
18700b57cec5SDimitry Andric }
18710b57cec5SDimitry Andric 
18720b57cec5SDimitry Andric void Function::allocHungoffUselist() {
18730b57cec5SDimitry Andric   // If we've already allocated a uselist, stop here.
18740b57cec5SDimitry Andric   if (getNumOperands())
18750b57cec5SDimitry Andric     return;
18760b57cec5SDimitry Andric 
18770b57cec5SDimitry Andric   allocHungoffUses(3, /*IsPhi=*/ false);
18780b57cec5SDimitry Andric   setNumHungOffUseOperands(3);
18790b57cec5SDimitry Andric 
18800b57cec5SDimitry Andric   // Initialize the uselist with placeholder operands to allow traversal.
18810b57cec5SDimitry Andric   auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
18820b57cec5SDimitry Andric   Op<0>().set(CPN);
18830b57cec5SDimitry Andric   Op<1>().set(CPN);
18840b57cec5SDimitry Andric   Op<2>().set(CPN);
18850b57cec5SDimitry Andric }
18860b57cec5SDimitry Andric 
18870b57cec5SDimitry Andric template <int Idx>
18880b57cec5SDimitry Andric void Function::setHungoffOperand(Constant *C) {
18890b57cec5SDimitry Andric   if (C) {
18900b57cec5SDimitry Andric     allocHungoffUselist();
18910b57cec5SDimitry Andric     Op<Idx>().set(C);
18920b57cec5SDimitry Andric   } else if (getNumOperands()) {
18930b57cec5SDimitry Andric     Op<Idx>().set(
18940b57cec5SDimitry Andric         ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
18950b57cec5SDimitry Andric   }
18960b57cec5SDimitry Andric }
18970b57cec5SDimitry Andric 
18980b57cec5SDimitry Andric void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
18990b57cec5SDimitry Andric   assert(Bit < 16 && "SubclassData contains only 16 bits");
19000b57cec5SDimitry Andric   if (On)
19010b57cec5SDimitry Andric     setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
19020b57cec5SDimitry Andric   else
19030b57cec5SDimitry Andric     setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
19040b57cec5SDimitry Andric }
19050b57cec5SDimitry Andric 
19060b57cec5SDimitry Andric void Function::setEntryCount(ProfileCount Count,
19070b57cec5SDimitry Andric                              const DenseSet<GlobalValue::GUID> *S) {
19080b57cec5SDimitry Andric #if !defined(NDEBUG)
19090b57cec5SDimitry Andric   auto PrevCount = getEntryCount();
191081ad6265SDimitry Andric   assert(!PrevCount || PrevCount->getType() == Count.getType());
19110b57cec5SDimitry Andric #endif
1912480093f4SDimitry Andric 
1913480093f4SDimitry Andric   auto ImportGUIDs = getImportGUIDs();
1914480093f4SDimitry Andric   if (S == nullptr && ImportGUIDs.size())
1915480093f4SDimitry Andric     S = &ImportGUIDs;
1916480093f4SDimitry Andric 
19170b57cec5SDimitry Andric   MDBuilder MDB(getContext());
19180b57cec5SDimitry Andric   setMetadata(
19190b57cec5SDimitry Andric       LLVMContext::MD_prof,
19200b57cec5SDimitry Andric       MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S));
19210b57cec5SDimitry Andric }
19220b57cec5SDimitry Andric 
19230b57cec5SDimitry Andric void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type,
19240b57cec5SDimitry Andric                              const DenseSet<GlobalValue::GUID> *Imports) {
19250b57cec5SDimitry Andric   setEntryCount(ProfileCount(Count, Type), Imports);
19260b57cec5SDimitry Andric }
19270b57cec5SDimitry Andric 
1928bdd1243dSDimitry Andric std::optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
19290b57cec5SDimitry Andric   MDNode *MD = getMetadata(LLVMContext::MD_prof);
19300b57cec5SDimitry Andric   if (MD && MD->getOperand(0))
19310b57cec5SDimitry Andric     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
19320b57cec5SDimitry Andric       if (MDS->getString().equals("function_entry_count")) {
19330b57cec5SDimitry Andric         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
19340b57cec5SDimitry Andric         uint64_t Count = CI->getValue().getZExtValue();
19350b57cec5SDimitry Andric         // A value of -1 is used for SamplePGO when there were no samples.
19360b57cec5SDimitry Andric         // Treat this the same as unknown.
19370b57cec5SDimitry Andric         if (Count == (uint64_t)-1)
1938bdd1243dSDimitry Andric           return std::nullopt;
19390b57cec5SDimitry Andric         return ProfileCount(Count, PCT_Real);
19400b57cec5SDimitry Andric       } else if (AllowSynthetic &&
19410b57cec5SDimitry Andric                  MDS->getString().equals("synthetic_function_entry_count")) {
19420b57cec5SDimitry Andric         ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
19430b57cec5SDimitry Andric         uint64_t Count = CI->getValue().getZExtValue();
19440b57cec5SDimitry Andric         return ProfileCount(Count, PCT_Synthetic);
19450b57cec5SDimitry Andric       }
19460b57cec5SDimitry Andric     }
1947bdd1243dSDimitry Andric   return std::nullopt;
19480b57cec5SDimitry Andric }
19490b57cec5SDimitry Andric 
19500b57cec5SDimitry Andric DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
19510b57cec5SDimitry Andric   DenseSet<GlobalValue::GUID> R;
19520b57cec5SDimitry Andric   if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
19530b57cec5SDimitry Andric     if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
19540b57cec5SDimitry Andric       if (MDS->getString().equals("function_entry_count"))
19550b57cec5SDimitry Andric         for (unsigned i = 2; i < MD->getNumOperands(); i++)
19560b57cec5SDimitry Andric           R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
19570b57cec5SDimitry Andric                        ->getValue()
19580b57cec5SDimitry Andric                        .getZExtValue());
19590b57cec5SDimitry Andric   return R;
19600b57cec5SDimitry Andric }
19610b57cec5SDimitry Andric 
19620b57cec5SDimitry Andric void Function::setSectionPrefix(StringRef Prefix) {
19630b57cec5SDimitry Andric   MDBuilder MDB(getContext());
19640b57cec5SDimitry Andric   setMetadata(LLVMContext::MD_section_prefix,
19650b57cec5SDimitry Andric               MDB.createFunctionSectionPrefix(Prefix));
19660b57cec5SDimitry Andric }
19670b57cec5SDimitry Andric 
1968bdd1243dSDimitry Andric std::optional<StringRef> Function::getSectionPrefix() const {
19690b57cec5SDimitry Andric   if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
19700b57cec5SDimitry Andric     assert(cast<MDString>(MD->getOperand(0))
19710b57cec5SDimitry Andric                ->getString()
19720b57cec5SDimitry Andric                .equals("function_section_prefix") &&
19730b57cec5SDimitry Andric            "Metadata not match");
19740b57cec5SDimitry Andric     return cast<MDString>(MD->getOperand(1))->getString();
19750b57cec5SDimitry Andric   }
1976bdd1243dSDimitry Andric   return std::nullopt;
19770b57cec5SDimitry Andric }
19780b57cec5SDimitry Andric 
19790b57cec5SDimitry Andric bool Function::nullPointerIsDefined() const {
19805ffd83dbSDimitry Andric   return hasFnAttribute(Attribute::NullPointerIsValid);
19810b57cec5SDimitry Andric }
19820b57cec5SDimitry Andric 
19830b57cec5SDimitry Andric bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
19840b57cec5SDimitry Andric   if (F && F->nullPointerIsDefined())
19850b57cec5SDimitry Andric     return true;
19860b57cec5SDimitry Andric 
19870b57cec5SDimitry Andric   if (AS != 0)
19880b57cec5SDimitry Andric     return true;
19890b57cec5SDimitry Andric 
19900b57cec5SDimitry Andric   return false;
19910b57cec5SDimitry Andric }
1992