1eba7e4ecSEugene Zelenko //===- Function.cpp - Implement the Global object classes -----------------===//
2ef860a24SChandler Carruth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ef860a24SChandler Carruth //
7ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
8ef860a24SChandler Carruth //
9ef860a24SChandler Carruth // This file implements the Function class for the IR library.
10ef860a24SChandler Carruth //
11ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
12ef860a24SChandler Carruth
136bda14b3SChandler Carruth #include "llvm/IR/Function.h"
14ef860a24SChandler Carruth #include "SymbolTableListTraitsImpl.h"
15eba7e4ecSEugene Zelenko #include "llvm/ADT/ArrayRef.h"
16eba7e4ecSEugene Zelenko #include "llvm/ADT/DenseSet.h"
17eba7e4ecSEugene Zelenko #include "llvm/ADT/None.h"
186bda14b3SChandler Carruth #include "llvm/ADT/STLExtras.h"
19eba7e4ecSEugene Zelenko #include "llvm/ADT/SmallString.h"
20eba7e4ecSEugene Zelenko #include "llvm/ADT/SmallVector.h"
21ef860a24SChandler Carruth #include "llvm/ADT/StringExtras.h"
22eba7e4ecSEugene Zelenko #include "llvm/ADT/StringRef.h"
23aef60af3SGiorgis Georgakoudis #include "llvm/IR/AbstractCallSite.h"
24eba7e4ecSEugene Zelenko #include "llvm/IR/Argument.h"
25eba7e4ecSEugene Zelenko #include "llvm/IR/Attributes.h"
26eba7e4ecSEugene Zelenko #include "llvm/IR/BasicBlock.h"
27eba7e4ecSEugene Zelenko #include "llvm/IR/Constant.h"
282567f3d0SDiego Novillo #include "llvm/IR/Constants.h"
299fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h"
30eba7e4ecSEugene Zelenko #include "llvm/IR/GlobalValue.h"
318394857fSChandler Carruth #include "llvm/IR/InstIterator.h"
32eba7e4ecSEugene Zelenko #include "llvm/IR/Instruction.h"
3329e2d946SStanislav Mekhanoshin #include "llvm/IR/IntrinsicInst.h"
34eba7e4ecSEugene Zelenko #include "llvm/IR/Intrinsics.h"
355d986953SReid Kleckner #include "llvm/IR/IntrinsicsAArch64.h"
365d986953SReid Kleckner #include "llvm/IR/IntrinsicsAMDGPU.h"
375d986953SReid Kleckner #include "llvm/IR/IntrinsicsARM.h"
385d986953SReid Kleckner #include "llvm/IR/IntrinsicsBPF.h"
3943dc3190SXiang Li #include "llvm/IR/IntrinsicsDirectX.h"
405d986953SReid Kleckner #include "llvm/IR/IntrinsicsHexagon.h"
415d986953SReid Kleckner #include "llvm/IR/IntrinsicsMips.h"
425d986953SReid Kleckner #include "llvm/IR/IntrinsicsNVPTX.h"
435d986953SReid Kleckner #include "llvm/IR/IntrinsicsPowerPC.h"
445d986953SReid Kleckner #include "llvm/IR/IntrinsicsR600.h"
455d986953SReid Kleckner #include "llvm/IR/IntrinsicsRISCV.h"
465d986953SReid Kleckner #include "llvm/IR/IntrinsicsS390.h"
47410626c9SKazushi (Jam) Marukawa #include "llvm/IR/IntrinsicsVE.h"
485d986953SReid Kleckner #include "llvm/IR/IntrinsicsWebAssembly.h"
495d986953SReid Kleckner #include "llvm/IR/IntrinsicsX86.h"
505d986953SReid Kleckner #include "llvm/IR/IntrinsicsXCore.h"
519fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h"
522567f3d0SDiego Novillo #include "llvm/IR/MDBuilder.h"
532567f3d0SDiego Novillo #include "llvm/IR/Metadata.h"
549fb823bbSChandler Carruth #include "llvm/IR/Module.h"
55d9c99043SStanislav Mekhanoshin #include "llvm/IR/Operator.h"
56eba7e4ecSEugene Zelenko #include "llvm/IR/SymbolTableListTraits.h"
57eba7e4ecSEugene Zelenko #include "llvm/IR/Type.h"
58eba7e4ecSEugene Zelenko #include "llvm/IR/Use.h"
59eba7e4ecSEugene Zelenko #include "llvm/IR/User.h"
60eba7e4ecSEugene Zelenko #include "llvm/IR/Value.h"
61eba7e4ecSEugene Zelenko #include "llvm/IR/ValueSymbolTable.h"
62eba7e4ecSEugene Zelenko #include "llvm/Support/Casting.h"
638d257627SHasyimi Bahrudin #include "llvm/Support/CommandLine.h"
64eba7e4ecSEugene Zelenko #include "llvm/Support/Compiler.h"
65eba7e4ecSEugene Zelenko #include "llvm/Support/ErrorHandling.h"
66eba7e4ecSEugene Zelenko #include <cassert>
67eba7e4ecSEugene Zelenko #include <cstddef>
68eba7e4ecSEugene Zelenko #include <cstdint>
69eba7e4ecSEugene Zelenko #include <cstring>
70eba7e4ecSEugene Zelenko #include <string>
71eba7e4ecSEugene Zelenko
72ef860a24SChandler Carruth using namespace llvm;
73e5b8de2fSEaswaran Raman using ProfileCount = Function::ProfileCount;
74ef860a24SChandler Carruth
75ef860a24SChandler Carruth // Explicit instantiations of SymbolTableListTraits since some of the methods
76ef860a24SChandler Carruth // are not in the public header file...
7737bf678aSDuncan P. N. Exon Smith template class llvm::SymbolTableListTraits<BasicBlock>;
78ef860a24SChandler Carruth
798d257627SHasyimi Bahrudin static cl::opt<unsigned> NonGlobalValueMaxNameSize(
808d257627SHasyimi Bahrudin "non-global-value-max-name-size", cl::Hidden, cl::init(1024),
818d257627SHasyimi Bahrudin cl::desc("Maximum size for the name of non-global values."));
828d257627SHasyimi Bahrudin
83ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
84ef860a24SChandler Carruth // Argument Implementation
85ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
86ef860a24SChandler Carruth
Argument(Type * Ty,const Twine & Name,Function * Par,unsigned ArgNo)8756d028d9SReid Kleckner Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
8856d028d9SReid Kleckner : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
89ef860a24SChandler Carruth setName(Name);
90ef860a24SChandler Carruth }
91ef860a24SChandler Carruth
setParent(Function * parent)92ef860a24SChandler Carruth void Argument::setParent(Function *parent) {
93ef860a24SChandler Carruth Parent = parent;
94ef860a24SChandler Carruth }
95ef860a24SChandler Carruth
hasNonNullAttr(bool AllowUndefOrPoison) const964479c0c2SJuneyoung Lee bool Argument::hasNonNullAttr(bool AllowUndefOrPoison) const {
97d52b1528SNick Lewycky if (!getType()->isPointerTy()) return false;
984479c0c2SJuneyoung Lee if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull) &&
994479c0c2SJuneyoung Lee (AllowUndefOrPoison ||
1004479c0c2SJuneyoung Lee getParent()->hasParamAttribute(getArgNo(), Attribute::NoUndef)))
101b0407ba0SHal Finkel return true;
102b0407ba0SHal Finkel else if (getDereferenceableBytes() > 0 &&
10377eeac3dSManoj Gupta !NullPointerIsDefined(getParent(),
10477eeac3dSManoj Gupta getType()->getPointerAddressSpace()))
105b0407ba0SHal Finkel return true;
106b0407ba0SHal Finkel return false;
107d52b1528SNick Lewycky }
108d52b1528SNick Lewycky
hasByValAttr() const109ef860a24SChandler Carruth bool Argument::hasByValAttr() const {
110ef860a24SChandler Carruth if (!getType()->isPointerTy()) return false;
1117b05a4c2SAmaury Sechet return hasAttribute(Attribute::ByVal);
112ef860a24SChandler Carruth }
113ef860a24SChandler Carruth
hasByRefAttr() const1145e999cbeSMatt Arsenault bool Argument::hasByRefAttr() const {
1155e999cbeSMatt Arsenault if (!getType()->isPointerTy())
1165e999cbeSMatt Arsenault return false;
1175e999cbeSMatt Arsenault return hasAttribute(Attribute::ByRef);
1185e999cbeSMatt Arsenault }
1195e999cbeSMatt Arsenault
hasSwiftSelfAttr() const120f46262e0SManman Ren bool Argument::hasSwiftSelfAttr() const {
121f021fab2SReid Kleckner return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
122f46262e0SManman Ren }
123f46262e0SManman Ren
hasSwiftErrorAttr() const1249bfd0d03SManman Ren bool Argument::hasSwiftErrorAttr() const {
125f021fab2SReid Kleckner return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
1269bfd0d03SManman Ren }
1279bfd0d03SManman Ren
hasInAllocaAttr() const128a534a381SReid Kleckner bool Argument::hasInAllocaAttr() const {
129a534a381SReid Kleckner if (!getType()->isPointerTy()) return false;
1307b05a4c2SAmaury Sechet return hasAttribute(Attribute::InAlloca);
131a534a381SReid Kleckner }
132a534a381SReid Kleckner
hasPreallocatedAttr() const1338a887556SArthur Eubanks bool Argument::hasPreallocatedAttr() const {
1348a887556SArthur Eubanks if (!getType()->isPointerTy())
1358a887556SArthur Eubanks return false;
1368a887556SArthur Eubanks return hasAttribute(Attribute::Preallocated);
1378a887556SArthur Eubanks }
1388a887556SArthur Eubanks
hasPassPointeeByValueCopyAttr() const139023883a8SMatt Arsenault bool Argument::hasPassPointeeByValueCopyAttr() const {
140436c42ecSReid Kleckner if (!getType()->isPointerTy()) return false;
141b518054bSReid Kleckner AttributeList Attrs = getParent()->getAttributes();
142a0c42ca5SArthur Eubanks return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) ||
143a0c42ca5SArthur Eubanks Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) ||
144a0c42ca5SArthur Eubanks Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated);
145436c42ecSReid Kleckner }
146436c42ecSReid Kleckner
hasPointeeInMemoryValueAttr() const1475e999cbeSMatt Arsenault bool Argument::hasPointeeInMemoryValueAttr() const {
1485e999cbeSMatt Arsenault if (!getType()->isPointerTy())
1495e999cbeSMatt Arsenault return false;
1505e999cbeSMatt Arsenault AttributeList Attrs = getParent()->getAttributes();
151a0c42ca5SArthur Eubanks return Attrs.hasParamAttr(getArgNo(), Attribute::ByVal) ||
152a0c42ca5SArthur Eubanks Attrs.hasParamAttr(getArgNo(), Attribute::StructRet) ||
153a0c42ca5SArthur Eubanks Attrs.hasParamAttr(getArgNo(), Attribute::InAlloca) ||
154a0c42ca5SArthur Eubanks Attrs.hasParamAttr(getArgNo(), Attribute::Preallocated) ||
155a0c42ca5SArthur Eubanks Attrs.hasParamAttr(getArgNo(), Attribute::ByRef);
1565e999cbeSMatt Arsenault }
1576f5d9136SMatt Arsenault
158d65a7003SMatt Arsenault /// For a byval, sret, inalloca, or preallocated parameter, get the in-memory
1595e999cbeSMatt Arsenault /// parameter type.
getMemoryParamAllocType(AttributeSet ParamAttrs)160d34d2bbeSNikita Popov static Type *getMemoryParamAllocType(AttributeSet ParamAttrs) {
1616f5d9136SMatt Arsenault // FIXME: All the type carrying attributes are mutually exclusive, so there
1626f5d9136SMatt Arsenault // should be a single query to get the stored type that handles any of them.
1636f5d9136SMatt Arsenault if (Type *ByValTy = ParamAttrs.getByValType())
1645e999cbeSMatt Arsenault return ByValTy;
1655e999cbeSMatt Arsenault if (Type *ByRefTy = ParamAttrs.getByRefType())
1665e999cbeSMatt Arsenault return ByRefTy;
1676f5d9136SMatt Arsenault if (Type *PreAllocTy = ParamAttrs.getPreallocatedType())
1685e999cbeSMatt Arsenault return PreAllocTy;
1699a0c9402SMatt Arsenault if (Type *InAllocaTy = ParamAttrs.getInAllocaType())
1709a0c9402SMatt Arsenault return InAllocaTy;
17137a2c451SArthur Eubanks if (Type *SRetTy = ParamAttrs.getStructRetType())
17237a2c451SArthur Eubanks return SRetTy;
1736f5d9136SMatt Arsenault
1745e999cbeSMatt Arsenault return nullptr;
1755e999cbeSMatt Arsenault }
1765e999cbeSMatt Arsenault
getPassPointeeByValueCopySize(const DataLayout & DL) const1775e999cbeSMatt Arsenault uint64_t Argument::getPassPointeeByValueCopySize(const DataLayout &DL) const {
1785e999cbeSMatt Arsenault AttributeSet ParamAttrs =
17980ea2bb5SArthur Eubanks getParent()->getAttributes().getParamAttrs(getArgNo());
180d34d2bbeSNikita Popov if (Type *MemTy = getMemoryParamAllocType(ParamAttrs))
1815e999cbeSMatt Arsenault return DL.getTypeAllocSize(MemTy);
1826f5d9136SMatt Arsenault return 0;
1836f5d9136SMatt Arsenault }
1846f5d9136SMatt Arsenault
getPointeeInMemoryValueType() const1855e999cbeSMatt Arsenault Type *Argument::getPointeeInMemoryValueType() const {
1865e999cbeSMatt Arsenault AttributeSet ParamAttrs =
18780ea2bb5SArthur Eubanks getParent()->getAttributes().getParamAttrs(getArgNo());
188d34d2bbeSNikita Popov return getMemoryParamAllocType(ParamAttrs);
1895e999cbeSMatt Arsenault }
1905e999cbeSMatt Arsenault
getParamAlignment() const19105392466SArthur Eubanks uint64_t Argument::getParamAlignment() const {
192ef860a24SChandler Carruth assert(getType()->isPointerTy() && "Only pointers have alignments");
193859f8b54SReid Kleckner return getParent()->getParamAlignment(getArgNo());
194ef860a24SChandler Carruth }
195ef860a24SChandler Carruth
getParamAlign() const196531c1161SGuillaume Chatelet MaybeAlign Argument::getParamAlign() const {
197531c1161SGuillaume Chatelet assert(getType()->isPointerTy() && "Only pointers have alignments");
198531c1161SGuillaume Chatelet return getParent()->getParamAlign(getArgNo());
199531c1161SGuillaume Chatelet }
200531c1161SGuillaume Chatelet
getParamStackAlign() const201f9d932e6SMomchil Velikov MaybeAlign Argument::getParamStackAlign() const {
202f9d932e6SMomchil Velikov return getParent()->getParamStackAlign(getArgNo());
203f9d932e6SMomchil Velikov }
204f9d932e6SMomchil Velikov
getParamByValType() const205b7141207STim Northover Type *Argument::getParamByValType() const {
206b7141207STim Northover assert(getType()->isPointerTy() && "Only pointers have byval types");
207b7141207STim Northover return getParent()->getParamByValType(getArgNo());
208b7141207STim Northover }
209b7141207STim Northover
getParamStructRetType() const210d65a7003SMatt Arsenault Type *Argument::getParamStructRetType() const {
211d65a7003SMatt Arsenault assert(getType()->isPointerTy() && "Only pointers have sret types");
212d65a7003SMatt Arsenault return getParent()->getParamStructRetType(getArgNo());
213d65a7003SMatt Arsenault }
214d65a7003SMatt Arsenault
getParamByRefType() const2155e999cbeSMatt Arsenault Type *Argument::getParamByRefType() const {
216e867951dSMatt Arsenault assert(getType()->isPointerTy() && "Only pointers have byref types");
2175e999cbeSMatt Arsenault return getParent()->getParamByRefType(getArgNo());
2185e999cbeSMatt Arsenault }
2195e999cbeSMatt Arsenault
getParamInAllocaType() const2209a0c9402SMatt Arsenault Type *Argument::getParamInAllocaType() const {
2219a0c9402SMatt Arsenault assert(getType()->isPointerTy() && "Only pointers have inalloca types");
2229a0c9402SMatt Arsenault return getParent()->getParamInAllocaType(getArgNo());
2239a0c9402SMatt Arsenault }
2249a0c9402SMatt Arsenault
getDereferenceableBytes() const225b0407ba0SHal Finkel uint64_t Argument::getDereferenceableBytes() const {
226b0407ba0SHal Finkel assert(getType()->isPointerTy() &&
227b0407ba0SHal Finkel "Only pointers have dereferenceable bytes");
2285fbdd177SReid Kleckner return getParent()->getParamDereferenceableBytes(getArgNo());
229b0407ba0SHal Finkel }
230b0407ba0SHal Finkel
getDereferenceableOrNullBytes() const23106cf33fbSSanjoy Das uint64_t Argument::getDereferenceableOrNullBytes() const {
23206cf33fbSSanjoy Das assert(getType()->isPointerTy() &&
23306cf33fbSSanjoy Das "Only pointers have dereferenceable bytes");
2345fbdd177SReid Kleckner return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
23506cf33fbSSanjoy Das }
23606cf33fbSSanjoy Das
hasNestAttr() const237ef860a24SChandler Carruth bool Argument::hasNestAttr() const {
238ef860a24SChandler Carruth if (!getType()->isPointerTy()) return false;
2397b05a4c2SAmaury Sechet return hasAttribute(Attribute::Nest);
240ef860a24SChandler Carruth }
241ef860a24SChandler Carruth
hasNoAliasAttr() const242ef860a24SChandler Carruth bool Argument::hasNoAliasAttr() const {
243ef860a24SChandler Carruth if (!getType()->isPointerTy()) return false;
2447b05a4c2SAmaury Sechet return hasAttribute(Attribute::NoAlias);
245ef860a24SChandler Carruth }
246ef860a24SChandler Carruth
hasNoCaptureAttr() const247ef860a24SChandler Carruth bool Argument::hasNoCaptureAttr() const {
248ef860a24SChandler Carruth if (!getType()->isPointerTy()) return false;
2497b05a4c2SAmaury Sechet return hasAttribute(Attribute::NoCapture);
250ef860a24SChandler Carruth }
251ef860a24SChandler Carruth
hasNoFreeAttr() const252fa26da05SPhilip Reames bool Argument::hasNoFreeAttr() const {
253fa26da05SPhilip Reames if (!getType()->isPointerTy()) return false;
254fa26da05SPhilip Reames return hasAttribute(Attribute::NoFree);
255fa26da05SPhilip Reames }
256fa26da05SPhilip Reames
hasStructRetAttr() const257ef860a24SChandler Carruth bool Argument::hasStructRetAttr() const {
258ef860a24SChandler Carruth if (!getType()->isPointerTy()) return false;
2597b05a4c2SAmaury Sechet return hasAttribute(Attribute::StructRet);
260ef860a24SChandler Carruth }
261ef860a24SChandler Carruth
hasInRegAttr() const2625dc8aeb2SMandeep Singh Grang bool Argument::hasInRegAttr() const {
2635dc8aeb2SMandeep Singh Grang return hasAttribute(Attribute::InReg);
2645dc8aeb2SMandeep Singh Grang }
2655dc8aeb2SMandeep Singh Grang
hasReturnedAttr() const266b8bd232aSStephen Lin bool Argument::hasReturnedAttr() const {
2677b05a4c2SAmaury Sechet return hasAttribute(Attribute::Returned);
268b8bd232aSStephen Lin }
269b8bd232aSStephen Lin
hasZExtAttr() const270384c3b5cSJuergen Ributzka bool Argument::hasZExtAttr() const {
2717b05a4c2SAmaury Sechet return hasAttribute(Attribute::ZExt);
272384c3b5cSJuergen Ributzka }
273384c3b5cSJuergen Ributzka
hasSExtAttr() const274384c3b5cSJuergen Ributzka bool Argument::hasSExtAttr() const {
2757b05a4c2SAmaury Sechet return hasAttribute(Attribute::SExt);
276384c3b5cSJuergen Ributzka }
277384c3b5cSJuergen Ributzka
onlyReadsMemory() const278c2ec0725SNick Lewycky bool Argument::onlyReadsMemory() const {
279f021fab2SReid Kleckner AttributeList Attrs = getParent()->getAttributes();
280a0c42ca5SArthur Eubanks return Attrs.hasParamAttr(getArgNo(), Attribute::ReadOnly) ||
281a0c42ca5SArthur Eubanks Attrs.hasParamAttr(getArgNo(), Attribute::ReadNone);
282c2ec0725SNick Lewycky }
283c2ec0725SNick Lewycky
addAttrs(AttrBuilder & B)2849d16fa09SReid Kleckner void Argument::addAttrs(AttrBuilder &B) {
2859d16fa09SReid Kleckner AttributeList AL = getParent()->getAttributes();
2865fbdd177SReid Kleckner AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
2879d16fa09SReid Kleckner getParent()->setAttributes(AL);
2889d16fa09SReid Kleckner }
2899d16fa09SReid Kleckner
addAttr(Attribute::AttrKind Kind)2909d16fa09SReid Kleckner void Argument::addAttr(Attribute::AttrKind Kind) {
2915fbdd177SReid Kleckner getParent()->addParamAttr(getArgNo(), Kind);
2929d16fa09SReid Kleckner }
2939d16fa09SReid Kleckner
addAttr(Attribute Attr)2949d16fa09SReid Kleckner void Argument::addAttr(Attribute Attr) {
2955fbdd177SReid Kleckner getParent()->addParamAttr(getArgNo(), Attr);
296ef860a24SChandler Carruth }
297ef860a24SChandler Carruth
removeAttr(Attribute::AttrKind Kind)2989d16fa09SReid Kleckner void Argument::removeAttr(Attribute::AttrKind Kind) {
2995fbdd177SReid Kleckner getParent()->removeParamAttr(getArgNo(), Kind);
3009d16fa09SReid Kleckner }
3019d16fa09SReid Kleckner
removeAttrs(const AttributeMask & AM)3029290ccc3Sserge-sans-paille void Argument::removeAttrs(const AttributeMask &AM) {
3035b6cae55SSteven Wu AttributeList AL = getParent()->getAttributes();
3049290ccc3Sserge-sans-paille AL = AL.removeParamAttributes(Parent->getContext(), getArgNo(), AM);
3055b6cae55SSteven Wu getParent()->setAttributes(AL);
3065b6cae55SSteven Wu }
3075b6cae55SSteven Wu
hasAttribute(Attribute::AttrKind Kind) const3087b05a4c2SAmaury Sechet bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
309f021fab2SReid Kleckner return getParent()->hasParamAttribute(getArgNo(), Kind);
3107b05a4c2SAmaury Sechet }
3117b05a4c2SAmaury Sechet
getAttribute(Attribute::AttrKind Kind) const312607c8a9dSTim Northover Attribute Argument::getAttribute(Attribute::AttrKind Kind) const {
313607c8a9dSTim Northover return getParent()->getParamAttribute(getArgNo(), Kind);
314607c8a9dSTim Northover }
315607c8a9dSTim Northover
316ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
317ef860a24SChandler Carruth // Helper Methods in Function
318ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
319ef860a24SChandler Carruth
getContext() const320ef860a24SChandler Carruth LLVMContext &Function::getContext() const {
321ef860a24SChandler Carruth return getType()->getContext();
322ef860a24SChandler Carruth }
323ef860a24SChandler Carruth
getInstructionCount() const32487b6725cSMircea Trofin unsigned Function::getInstructionCount() const {
325e49374d0SJessica Paquette unsigned NumInstrs = 0;
32687b6725cSMircea Trofin for (const BasicBlock &BB : BasicBlocks)
327e49374d0SJessica Paquette NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(),
328e49374d0SJessica Paquette BB.instructionsWithoutDebug().end());
329e49374d0SJessica Paquette return NumInstrs;
330e49374d0SJessica Paquette }
331e49374d0SJessica Paquette
Create(FunctionType * Ty,LinkageTypes Linkage,const Twine & N,Module & M)3326bcf2ba2SAlexander Richardson Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
3336bcf2ba2SAlexander Richardson const Twine &N, Module &M) {
3346bcf2ba2SAlexander Richardson return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
3356bcf2ba2SAlexander Richardson }
3366bcf2ba2SAlexander Richardson
createWithDefaultAttr(FunctionType * Ty,LinkageTypes Linkage,unsigned AddrSpace,const Twine & N,Module * M)337775a9483SFangrui Song Function *Function::createWithDefaultAttr(FunctionType *Ty,
338775a9483SFangrui Song LinkageTypes Linkage,
339775a9483SFangrui Song unsigned AddrSpace, const Twine &N,
340775a9483SFangrui Song Module *M) {
341775a9483SFangrui Song auto *F = new Function(Ty, Linkage, AddrSpace, N, M);
342d2cc6c2dSSerge Guelton AttrBuilder B(F->getContext());
3436398903aSMomchil Velikov UWTableKind UWTable = M->getUwtable();
3446398903aSMomchil Velikov if (UWTable != UWTableKind::None)
3456398903aSMomchil Velikov B.addUWTableAttr(UWTable);
3462786e673SFangrui Song switch (M->getFramePointer()) {
3472786e673SFangrui Song case FramePointerKind::None:
3482786e673SFangrui Song // 0 ("none") is the default.
3492786e673SFangrui Song break;
3502786e673SFangrui Song case FramePointerKind::NonLeaf:
3512786e673SFangrui Song B.addAttribute("frame-pointer", "non-leaf");
3522786e673SFangrui Song break;
3532786e673SFangrui Song case FramePointerKind::All:
3542786e673SFangrui Song B.addAttribute("frame-pointer", "all");
3552786e673SFangrui Song break;
3562786e673SFangrui Song }
357*140bfdcaSNick Desaulniers if (M->getModuleFlag("function_return_thunk_extern"))
358*140bfdcaSNick Desaulniers B.addAttribute(Attribute::FnRetThunkExtern);
359ad727ab7SArthur Eubanks F->addFnAttrs(B);
360775a9483SFangrui Song return F;
361775a9483SFangrui Song }
362775a9483SFangrui Song
removeFromParent()363ef860a24SChandler Carruth void Function::removeFromParent() {
36452888a67SDuncan P. N. Exon Smith getParent()->getFunctionList().remove(getIterator());
365ef860a24SChandler Carruth }
366ef860a24SChandler Carruth
eraseFromParent()367ef860a24SChandler Carruth void Function::eraseFromParent() {
36852888a67SDuncan P. N. Exon Smith getParent()->getFunctionList().erase(getIterator());
369ef860a24SChandler Carruth }
370ef860a24SChandler Carruth
371ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
372ef860a24SChandler Carruth // Function Implementation
373ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
374ef860a24SChandler Carruth
computeAddrSpace(unsigned AddrSpace,Module * M)3756bcf2ba2SAlexander Richardson static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) {
3766bcf2ba2SAlexander Richardson // If AS == -1 and we are passed a valid module pointer we place the function
3776bcf2ba2SAlexander Richardson // in the program address space. Otherwise we default to AS0.
3786bcf2ba2SAlexander Richardson if (AddrSpace == static_cast<unsigned>(-1))
3796bcf2ba2SAlexander Richardson return M ? M->getDataLayout().getProgramAddressSpace() : 0;
3806bcf2ba2SAlexander Richardson return AddrSpace;
3816bcf2ba2SAlexander Richardson }
3826bcf2ba2SAlexander Richardson
Function(FunctionType * Ty,LinkageTypes Linkage,unsigned AddrSpace,const Twine & name,Module * ParentModule)3836bcf2ba2SAlexander Richardson Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
3846bcf2ba2SAlexander Richardson const Twine &name, Module *ParentModule)
385d583b195SDavid Blaikie : GlobalObject(Ty, Value::FunctionVal,
3866bcf2ba2SAlexander Richardson OperandTraits<Function>::op_begin(this), 0, Linkage, name,
3876bcf2ba2SAlexander Richardson computeAddrSpace(AddrSpace, ParentModule)),
388eba7e4ecSEugene Zelenko NumArgs(Ty->getNumParams()) {
389ef860a24SChandler Carruth assert(FunctionType::isValidReturnType(getReturnType()) &&
390ef860a24SChandler Carruth "invalid return type");
3914472b776SDuncan P. N. Exon Smith setGlobalObjectSubClassData(0);
392a53d49e1SMehdi Amini
393a53d49e1SMehdi Amini // We only need a symbol table for a function if the context keeps value names
394a53d49e1SMehdi Amini if (!getContext().shouldDiscardValueNames())
3958d257627SHasyimi Bahrudin SymTab = std::make_unique<ValueSymbolTable>(NonGlobalValueMaxNameSize);
396ef860a24SChandler Carruth
397ef860a24SChandler Carruth // If the function has arguments, mark them as lazily built.
398ef860a24SChandler Carruth if (Ty->getNumParams())
399ef860a24SChandler Carruth setValueSubclassData(1); // Set the "has lazy arguments" bit.
400ef860a24SChandler Carruth
401ef860a24SChandler Carruth if (ParentModule)
402ef860a24SChandler Carruth ParentModule->getFunctionList().push_back(this);
403ef860a24SChandler Carruth
404291abd3eSJustin Lebar HasLLVMReservedName = getName().startswith("llvm.");
405ef860a24SChandler Carruth // Ensure intrinsics have the right parameter attributes.
406a7c0c18cSPete Cooper // Note, the IntID field will have been set in Value::setName if this function
407a7c0c18cSPete Cooper // name is a valid intrinsic ID.
408a7c0c18cSPete Cooper if (IntID)
409a7c0c18cSPete Cooper setAttributes(Intrinsic::getAttributes(getContext(), IntID));
410ef860a24SChandler Carruth }
411ef860a24SChandler Carruth
~Function()412ef860a24SChandler Carruth Function::~Function() {
413ef860a24SChandler Carruth dropAllReferences(); // After this it is safe to delete instructions.
414ef860a24SChandler Carruth
415ef860a24SChandler Carruth // Delete all of the method arguments and unlink from symbol table...
41656d028d9SReid Kleckner if (Arguments)
41756d028d9SReid Kleckner clearArguments();
418ef860a24SChandler Carruth
419ef860a24SChandler Carruth // Remove the function from the on-the-side GC table.
420ef860a24SChandler Carruth clearGC();
421ef860a24SChandler Carruth }
422ef860a24SChandler Carruth
BuildLazyArguments() const423ef860a24SChandler Carruth void Function::BuildLazyArguments() const {
424ef860a24SChandler Carruth // Create the arguments vector, all arguments start out unnamed.
42556d028d9SReid Kleckner auto *FT = getFunctionType();
42656d028d9SReid Kleckner if (NumArgs > 0) {
42756d028d9SReid Kleckner Arguments = std::allocator<Argument>().allocate(NumArgs);
42856d028d9SReid Kleckner for (unsigned i = 0, e = NumArgs; i != e; ++i) {
42956d028d9SReid Kleckner Type *ArgTy = FT->getParamType(i);
43056d028d9SReid Kleckner assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
43156d028d9SReid Kleckner new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
43256d028d9SReid Kleckner }
433ef860a24SChandler Carruth }
434ef860a24SChandler Carruth
435ef860a24SChandler Carruth // Clear the lazy arguments bit.
436ef860a24SChandler Carruth unsigned SDC = getSubclassDataFromValue();
43708609342SSimon Pilgrim SDC &= ~(1 << 0);
43808609342SSimon Pilgrim const_cast<Function*>(this)->setValueSubclassData(SDC);
43956d028d9SReid Kleckner assert(!hasLazyArguments());
44056d028d9SReid Kleckner }
44156d028d9SReid Kleckner
makeArgArray(Argument * Args,size_t Count)44256d028d9SReid Kleckner static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
44356d028d9SReid Kleckner return MutableArrayRef<Argument>(Args, Count);
44456d028d9SReid Kleckner }
44556d028d9SReid Kleckner
isConstrainedFPIntrinsic() const4467f38812dSKevin P. Neal bool Function::isConstrainedFPIntrinsic() const {
4477f38812dSKevin P. Neal switch (getIntrinsicID()) {
4487f38812dSKevin P. Neal #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
4497f38812dSKevin P. Neal case Intrinsic::INTRINSIC:
4507f38812dSKevin P. Neal #include "llvm/IR/ConstrainedOps.def"
4517f38812dSKevin P. Neal return true;
4527f38812dSKevin P. Neal #undef INSTRUCTION
4537f38812dSKevin P. Neal default:
4547f38812dSKevin P. Neal return false;
4557f38812dSKevin P. Neal }
4567f38812dSKevin P. Neal }
4577f38812dSKevin P. Neal
clearArguments()45856d028d9SReid Kleckner void Function::clearArguments() {
45956d028d9SReid Kleckner for (Argument &A : makeArgArray(Arguments, NumArgs)) {
46056d028d9SReid Kleckner A.setName("");
46156d028d9SReid Kleckner A.~Argument();
46256d028d9SReid Kleckner }
46356d028d9SReid Kleckner std::allocator<Argument>().deallocate(Arguments, NumArgs);
46456d028d9SReid Kleckner Arguments = nullptr;
465ef860a24SChandler Carruth }
466ef860a24SChandler Carruth
stealArgumentListFrom(Function & Src)467bdfc9846SDuncan P. N. Exon Smith void Function::stealArgumentListFrom(Function &Src) {
468bdfc9846SDuncan P. N. Exon Smith assert(isDeclaration() && "Expected no references to current arguments");
469bdfc9846SDuncan P. N. Exon Smith
470bdfc9846SDuncan P. N. Exon Smith // Drop the current arguments, if any, and set the lazy argument bit.
471bdfc9846SDuncan P. N. Exon Smith if (!hasLazyArguments()) {
47256d028d9SReid Kleckner assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
473bdfc9846SDuncan P. N. Exon Smith [](const Argument &A) { return A.use_empty(); }) &&
474bdfc9846SDuncan P. N. Exon Smith "Expected arguments to be unused in declaration");
47556d028d9SReid Kleckner clearArguments();
476bdfc9846SDuncan P. N. Exon Smith setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
477bdfc9846SDuncan P. N. Exon Smith }
478bdfc9846SDuncan P. N. Exon Smith
479bdfc9846SDuncan P. N. Exon Smith // Nothing to steal if Src has lazy arguments.
480bdfc9846SDuncan P. N. Exon Smith if (Src.hasLazyArguments())
481bdfc9846SDuncan P. N. Exon Smith return;
482bdfc9846SDuncan P. N. Exon Smith
483bdfc9846SDuncan P. N. Exon Smith // Steal arguments from Src, and fix the lazy argument bits.
48456d028d9SReid Kleckner assert(arg_size() == Src.arg_size());
48556d028d9SReid Kleckner Arguments = Src.Arguments;
48656d028d9SReid Kleckner Src.Arguments = nullptr;
48756d028d9SReid Kleckner for (Argument &A : makeArgArray(Arguments, NumArgs)) {
48856d028d9SReid Kleckner // FIXME: This does the work of transferNodesFromList inefficiently.
48956d028d9SReid Kleckner SmallString<128> Name;
49056d028d9SReid Kleckner if (A.hasName())
49156d028d9SReid Kleckner Name = A.getName();
49256d028d9SReid Kleckner if (!Name.empty())
49356d028d9SReid Kleckner A.setName("");
49456d028d9SReid Kleckner A.setParent(this);
49556d028d9SReid Kleckner if (!Name.empty())
49656d028d9SReid Kleckner A.setName(Name);
49756d028d9SReid Kleckner }
49856d028d9SReid Kleckner
499bdfc9846SDuncan P. N. Exon Smith setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
50056d028d9SReid Kleckner assert(!hasLazyArguments());
501bdfc9846SDuncan P. N. Exon Smith Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
502bdfc9846SDuncan P. N. Exon Smith }
503bdfc9846SDuncan P. N. Exon Smith
504ef860a24SChandler Carruth // dropAllReferences() - This function causes all the subinstructions to "let
505ef860a24SChandler Carruth // go" of all references that they are maintaining. This allows one to
506ef860a24SChandler Carruth // 'delete' a whole class at a time, even though there may be circular
507ef860a24SChandler Carruth // references... first all references are dropped, and all use counts go to
508ef860a24SChandler Carruth // zero. Then everything is deleted for real. Note that no operations are
509ef860a24SChandler Carruth // valid on an object that has "dropped all references", except operator
510ef860a24SChandler Carruth // delete.
511ef860a24SChandler Carruth //
dropAllReferences()512ef860a24SChandler Carruth void Function::dropAllReferences() {
513d4bcefc7SRafael Espindola setIsMaterializable(false);
514d4bcefc7SRafael Espindola
515af28e7d6SBenjamin Kramer for (BasicBlock &BB : *this)
516af28e7d6SBenjamin Kramer BB.dropAllReferences();
517ef860a24SChandler Carruth
518ef860a24SChandler Carruth // Delete all basic blocks. They are now unused, except possibly by
519ef860a24SChandler Carruth // blockaddresses, but BasicBlock's destructor takes care of those.
520ef860a24SChandler Carruth while (!BasicBlocks.empty())
521ef860a24SChandler Carruth BasicBlocks.begin()->eraseFromParent();
5223fa50f9bSPeter Collingbourne
5233a63fb31SVedant Kumar // Drop uses of any optional data (real or placeholder).
5243a63fb31SVedant Kumar if (getNumOperands()) {
5253a63fb31SVedant Kumar User::dropAllReferences();
5263a63fb31SVedant Kumar setNumHungOffUseOperands(0);
5273a63fb31SVedant Kumar setValueSubclassData(getSubclassDataFromValue() & ~0xe);
5283a63fb31SVedant Kumar }
529e2510cdfSDuncan P. N. Exon Smith
530e2510cdfSDuncan P. N. Exon Smith // Metadata is stored in a side-table.
531e2510cdfSDuncan P. N. Exon Smith clearMetadata();
532ef860a24SChandler Carruth }
533ef860a24SChandler Carruth
addAttributeAtIndex(unsigned i,Attribute Attr)534c9693492SArthur Eubanks void Function::addAttributeAtIndex(unsigned i, Attribute Attr) {
535c9693492SArthur Eubanks AttributeSets = AttributeSets.addAttributeAtIndex(getContext(), i, Attr);
5365db224e1SAmaury Sechet }
5375db224e1SAmaury Sechet
addFnAttr(Attribute::AttrKind Kind)538cc327bd5SArthur Eubanks void Function::addFnAttr(Attribute::AttrKind Kind) {
539cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind);
540cc327bd5SArthur Eubanks }
541cc327bd5SArthur Eubanks
addFnAttr(StringRef Kind,StringRef Val)542cc327bd5SArthur Eubanks void Function::addFnAttr(StringRef Kind, StringRef Val) {
543cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addFnAttribute(getContext(), Kind, Val);
544cc327bd5SArthur Eubanks }
545cc327bd5SArthur Eubanks
addFnAttr(Attribute Attr)546cc327bd5SArthur Eubanks void Function::addFnAttr(Attribute Attr) {
547cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addFnAttribute(getContext(), Attr);
548cc327bd5SArthur Eubanks }
549cc327bd5SArthur Eubanks
addFnAttrs(const AttrBuilder & Attrs)550cc327bd5SArthur Eubanks void Function::addFnAttrs(const AttrBuilder &Attrs) {
551cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addFnAttributes(getContext(), Attrs);
552cc327bd5SArthur Eubanks }
553cc327bd5SArthur Eubanks
addRetAttr(Attribute::AttrKind Kind)554cc327bd5SArthur Eubanks void Function::addRetAttr(Attribute::AttrKind Kind) {
555cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addRetAttribute(getContext(), Kind);
556c0e2a1f4SBill Wendling }
557c0e2a1f4SBill Wendling
addRetAttr(Attribute Attr)5587f54009aSArthur Eubanks void Function::addRetAttr(Attribute Attr) {
5597f54009aSArthur Eubanks AttributeSets = AttributeSets.addRetAttribute(getContext(), Attr);
5607f54009aSArthur Eubanks }
5617f54009aSArthur Eubanks
addRetAttrs(const AttrBuilder & Attrs)5627f54009aSArthur Eubanks void Function::addRetAttrs(const AttrBuilder &Attrs) {
5637f54009aSArthur Eubanks AttributeSets = AttributeSets.addRetAttributes(getContext(), Attrs);
5647f54009aSArthur Eubanks }
5657f54009aSArthur Eubanks
addParamAttr(unsigned ArgNo,Attribute::AttrKind Kind)5665fbdd177SReid Kleckner void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
567cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Kind);
5685fbdd177SReid Kleckner }
5695fbdd177SReid Kleckner
addParamAttr(unsigned ArgNo,Attribute Attr)5705fbdd177SReid Kleckner void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
571cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addParamAttribute(getContext(), ArgNo, Attr);
5725fbdd177SReid Kleckner }
5735fbdd177SReid Kleckner
addParamAttrs(unsigned ArgNo,const AttrBuilder & Attrs)5745fbdd177SReid Kleckner void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
575cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addParamAttributes(getContext(), ArgNo, Attrs);
5765fbdd177SReid Kleckner }
5775fbdd177SReid Kleckner
removeAttributeAtIndex(unsigned i,Attribute::AttrKind Kind)578c9693492SArthur Eubanks void Function::removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind) {
579c9693492SArthur Eubanks AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind);
5807b05a4c2SAmaury Sechet }
5817b05a4c2SAmaury Sechet
removeAttributeAtIndex(unsigned i,StringRef Kind)582c9693492SArthur Eubanks void Function::removeAttributeAtIndex(unsigned i, StringRef Kind) {
583c9693492SArthur Eubanks AttributeSets = AttributeSets.removeAttributeAtIndex(getContext(), i, Kind);
584cc327bd5SArthur Eubanks }
585cc327bd5SArthur Eubanks
removeFnAttr(Attribute::AttrKind Kind)586cc327bd5SArthur Eubanks void Function::removeFnAttr(Attribute::AttrKind Kind) {
587cc327bd5SArthur Eubanks AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind);
588cc327bd5SArthur Eubanks }
589cc327bd5SArthur Eubanks
removeFnAttr(StringRef Kind)590cc327bd5SArthur Eubanks void Function::removeFnAttr(StringRef Kind) {
591cc327bd5SArthur Eubanks AttributeSets = AttributeSets.removeFnAttribute(getContext(), Kind);
592cc327bd5SArthur Eubanks }
593cc327bd5SArthur Eubanks
removeFnAttrs(const AttributeMask & AM)5949290ccc3Sserge-sans-paille void Function::removeFnAttrs(const AttributeMask &AM) {
5959290ccc3Sserge-sans-paille AttributeSets = AttributeSets.removeFnAttributes(getContext(), AM);
5966100adfeSAmaury Sechet }
5976100adfeSAmaury Sechet
removeRetAttr(Attribute::AttrKind Kind)598ad727ab7SArthur Eubanks void Function::removeRetAttr(Attribute::AttrKind Kind) {
599cc327bd5SArthur Eubanks AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind);
600ad727ab7SArthur Eubanks }
601ad727ab7SArthur Eubanks
removeRetAttr(StringRef Kind)602ad727ab7SArthur Eubanks void Function::removeRetAttr(StringRef Kind) {
603cc327bd5SArthur Eubanks AttributeSets = AttributeSets.removeRetAttribute(getContext(), Kind);
604ad727ab7SArthur Eubanks }
605ad727ab7SArthur Eubanks
removeRetAttrs(const AttributeMask & Attrs)6069290ccc3Sserge-sans-paille void Function::removeRetAttrs(const AttributeMask &Attrs) {
607cc327bd5SArthur Eubanks AttributeSets = AttributeSets.removeRetAttributes(getContext(), Attrs);
608ef860a24SChandler Carruth }
609ef860a24SChandler Carruth
removeParamAttr(unsigned ArgNo,Attribute::AttrKind Kind)6105fbdd177SReid Kleckner void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
611cc327bd5SArthur Eubanks AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind);
6125fbdd177SReid Kleckner }
6135fbdd177SReid Kleckner
removeParamAttr(unsigned ArgNo,StringRef Kind)6145fbdd177SReid Kleckner void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
615cc327bd5SArthur Eubanks AttributeSets = AttributeSets.removeParamAttribute(getContext(), ArgNo, Kind);
6165fbdd177SReid Kleckner }
6175fbdd177SReid Kleckner
removeParamAttrs(unsigned ArgNo,const AttributeMask & Attrs)6189290ccc3Sserge-sans-paille void Function::removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs) {
619cc327bd5SArthur Eubanks AttributeSets =
620cc327bd5SArthur Eubanks AttributeSets.removeParamAttributes(getContext(), ArgNo, Attrs);
6215fbdd177SReid Kleckner }
6225fbdd177SReid Kleckner
addDereferenceableParamAttr(unsigned ArgNo,uint64_t Bytes)6235fbdd177SReid Kleckner void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
624cc327bd5SArthur Eubanks AttributeSets =
625cc327bd5SArthur Eubanks AttributeSets.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
626cc327bd5SArthur Eubanks }
627cc327bd5SArthur Eubanks
hasFnAttribute(Attribute::AttrKind Kind) const628cc327bd5SArthur Eubanks bool Function::hasFnAttribute(Attribute::AttrKind Kind) const {
629cc327bd5SArthur Eubanks return AttributeSets.hasFnAttr(Kind);
630cc327bd5SArthur Eubanks }
631cc327bd5SArthur Eubanks
hasFnAttribute(StringRef Kind) const632cc327bd5SArthur Eubanks bool Function::hasFnAttribute(StringRef Kind) const {
633cc327bd5SArthur Eubanks return AttributeSets.hasFnAttr(Kind);
634cc327bd5SArthur Eubanks }
635cc327bd5SArthur Eubanks
hasRetAttribute(Attribute::AttrKind Kind) const636cc327bd5SArthur Eubanks bool Function::hasRetAttribute(Attribute::AttrKind Kind) const {
637cc327bd5SArthur Eubanks return AttributeSets.hasRetAttr(Kind);
638cc327bd5SArthur Eubanks }
639cc327bd5SArthur Eubanks
hasParamAttribute(unsigned ArgNo,Attribute::AttrKind Kind) const640cc327bd5SArthur Eubanks bool Function::hasParamAttribute(unsigned ArgNo,
641cc327bd5SArthur Eubanks Attribute::AttrKind Kind) const {
642cc327bd5SArthur Eubanks return AttributeSets.hasParamAttr(ArgNo, Kind);
643cc327bd5SArthur Eubanks }
644cc327bd5SArthur Eubanks
getAttributeAtIndex(unsigned i,Attribute::AttrKind Kind) const645c9693492SArthur Eubanks Attribute Function::getAttributeAtIndex(unsigned i,
646c9693492SArthur Eubanks Attribute::AttrKind Kind) const {
647c9693492SArthur Eubanks return AttributeSets.getAttributeAtIndex(i, Kind);
648cc327bd5SArthur Eubanks }
649cc327bd5SArthur Eubanks
getAttributeAtIndex(unsigned i,StringRef Kind) const650c9693492SArthur Eubanks Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const {
651c9693492SArthur Eubanks return AttributeSets.getAttributeAtIndex(i, Kind);
652cc327bd5SArthur Eubanks }
653cc327bd5SArthur Eubanks
getFnAttribute(Attribute::AttrKind Kind) const654cc327bd5SArthur Eubanks Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const {
655cc327bd5SArthur Eubanks return AttributeSets.getFnAttr(Kind);
656cc327bd5SArthur Eubanks }
657cc327bd5SArthur Eubanks
getFnAttribute(StringRef Kind) const658cc327bd5SArthur Eubanks Attribute Function::getFnAttribute(StringRef Kind) const {
659cc327bd5SArthur Eubanks return AttributeSets.getFnAttr(Kind);
660cc327bd5SArthur Eubanks }
661cc327bd5SArthur Eubanks
662cc327bd5SArthur Eubanks /// gets the specified attribute from the list of attributes.
getParamAttribute(unsigned ArgNo,Attribute::AttrKind Kind) const663cc327bd5SArthur Eubanks Attribute Function::getParamAttribute(unsigned ArgNo,
664cc327bd5SArthur Eubanks Attribute::AttrKind Kind) const {
665cc327bd5SArthur Eubanks return AttributeSets.getParamAttr(ArgNo, Kind);
6665fbdd177SReid Kleckner }
6675fbdd177SReid Kleckner
addDereferenceableOrNullParamAttr(unsigned ArgNo,uint64_t Bytes)6685fbdd177SReid Kleckner void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
6695fbdd177SReid Kleckner uint64_t Bytes) {
670cc327bd5SArthur Eubanks AttributeSets = AttributeSets.addDereferenceableOrNullParamAttr(getContext(),
671cc327bd5SArthur Eubanks ArgNo, Bytes);
6725fbdd177SReid Kleckner }
6735fbdd177SReid Kleckner
getDenormalMode(const fltSemantics & FPType) const674751a6c57SMatt Arsenault DenormalMode Function::getDenormalMode(const fltSemantics &FPType) const {
675751a6c57SMatt Arsenault if (&FPType == &APFloat::IEEEsingle()) {
676751a6c57SMatt Arsenault Attribute Attr = getFnAttribute("denormal-fp-math-f32");
677751a6c57SMatt Arsenault StringRef Val = Attr.getValueAsString();
678751a6c57SMatt Arsenault if (!Val.empty())
679751a6c57SMatt Arsenault return parseDenormalFPAttribute(Val);
680751a6c57SMatt Arsenault
681751a6c57SMatt Arsenault // If the f32 variant of the attribute isn't specified, try to use the
682751a6c57SMatt Arsenault // generic one.
683751a6c57SMatt Arsenault }
684751a6c57SMatt Arsenault
685751a6c57SMatt Arsenault Attribute Attr = getFnAttribute("denormal-fp-math");
686751a6c57SMatt Arsenault return parseDenormalFPAttribute(Attr.getValueAsString());
687751a6c57SMatt Arsenault }
688751a6c57SMatt Arsenault
getGC() const689599ebf27SMehdi Amini const std::string &Function::getGC() const {
690ef860a24SChandler Carruth assert(hasGC() && "Function has no collector");
691599ebf27SMehdi Amini return getContext().getGC(*this);
692ef860a24SChandler Carruth }
693ef860a24SChandler Carruth
setGC(std::string Str)694728f4448SBenjamin Kramer void Function::setGC(std::string Str) {
695599ebf27SMehdi Amini setValueSubclassDataBit(14, !Str.empty());
696599ebf27SMehdi Amini getContext().setGC(*this, std::move(Str));
697ef860a24SChandler Carruth }
698ef860a24SChandler Carruth
clearGC()699ef860a24SChandler Carruth void Function::clearGC() {
700599ebf27SMehdi Amini if (!hasGC())
701599ebf27SMehdi Amini return;
702599ebf27SMehdi Amini getContext().deleteGC(*this);
703599ebf27SMehdi Amini setValueSubclassDataBit(14, false);
704ef860a24SChandler Carruth }
705ef860a24SChandler Carruth
hasStackProtectorFnAttr() const706bc044a88SNick Desaulniers bool Function::hasStackProtectorFnAttr() const {
707bc044a88SNick Desaulniers return hasFnAttribute(Attribute::StackProtect) ||
708bc044a88SNick Desaulniers hasFnAttribute(Attribute::StackProtectStrong) ||
709bc044a88SNick Desaulniers hasFnAttribute(Attribute::StackProtectReq);
710bc044a88SNick Desaulniers }
711bc044a88SNick Desaulniers
712769efe62SRafael Espindola /// Copy all additional attributes (those not needed to create a Function) from
713769efe62SRafael Espindola /// the Function Src to this one.
copyAttributesFrom(const Function * Src)714e7c7854cSReid Kleckner void Function::copyAttributesFrom(const Function *Src) {
71599e05cf1SRafael Espindola GlobalObject::copyAttributesFrom(Src);
716e7c7854cSReid Kleckner setCallingConv(Src->getCallingConv());
717e7c7854cSReid Kleckner setAttributes(Src->getAttributes());
718e7c7854cSReid Kleckner if (Src->hasGC())
719e7c7854cSReid Kleckner setGC(Src->getGC());
720ef860a24SChandler Carruth else
721ef860a24SChandler Carruth clearGC();
722e7c7854cSReid Kleckner if (Src->hasPersonalityFn())
723e7c7854cSReid Kleckner setPersonalityFn(Src->getPersonalityFn());
724e7c7854cSReid Kleckner if (Src->hasPrefixData())
725e7c7854cSReid Kleckner setPrefixData(Src->getPrefixData());
726e7c7854cSReid Kleckner if (Src->hasPrologueData())
727e7c7854cSReid Kleckner setPrologueData(Src->getPrologueData());
728ef860a24SChandler Carruth }
729ef860a24SChandler Carruth
7300b5220d0SReid Kleckner /// Table of string intrinsic names indexed by enum value.
7310b5220d0SReid Kleckner static const char * const IntrinsicNameTable[] = {
7320b5220d0SReid Kleckner "not_intrinsic",
7330b5220d0SReid Kleckner #define GET_INTRINSIC_NAME_TABLE
734f5890e4eSReid Kleckner #include "llvm/IR/IntrinsicImpl.inc"
7350b5220d0SReid Kleckner #undef GET_INTRINSIC_NAME_TABLE
7360b5220d0SReid Kleckner };
7370b5220d0SReid Kleckner
73892a8c611SJustin Bogner /// Table of per-target intrinsic name tables.
73992a8c611SJustin Bogner #define GET_INTRINSIC_TARGET_DATA
740f5890e4eSReid Kleckner #include "llvm/IR/IntrinsicImpl.inc"
74192a8c611SJustin Bogner #undef GET_INTRINSIC_TARGET_DATA
74292a8c611SJustin Bogner
isTargetIntrinsic(Intrinsic::ID IID)74390131e3eSDavid Green bool Function::isTargetIntrinsic(Intrinsic::ID IID) {
74490131e3eSDavid Green return IID > TargetInfos[0].Count;
74590131e3eSDavid Green }
74690131e3eSDavid Green
isTargetIntrinsic() const7472a6c8715SSebastian Neubauer bool Function::isTargetIntrinsic() const {
74890131e3eSDavid Green return isTargetIntrinsic(IntID);
7492a6c8715SSebastian Neubauer }
7502a6c8715SSebastian Neubauer
75192a8c611SJustin Bogner /// Find the segment of \c IntrinsicNameTable for intrinsics with the same
75292a8c611SJustin Bogner /// target as \c Name, or the generic table if \c Name is not target specific.
75392a8c611SJustin Bogner ///
75492a8c611SJustin Bogner /// Returns the relevant slice of \c IntrinsicNameTable
findTargetSubtable(StringRef Name)75592a8c611SJustin Bogner static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
75692a8c611SJustin Bogner assert(Name.startswith("llvm."));
75792a8c611SJustin Bogner
75892a8c611SJustin Bogner ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
75992a8c611SJustin Bogner // Drop "llvm." and take the first dotted component. That will be the target
76092a8c611SJustin Bogner // if this is target specific.
76192a8c611SJustin Bogner StringRef Target = Name.drop_front(5).split('.').first;
76278ee2fbfSFangrui Song auto It = partition_point(
76378ee2fbfSFangrui Song Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; });
76492a8c611SJustin Bogner // We've either found the target or just fall back to the generic set, which
76592a8c611SJustin Bogner // is always first.
76692a8c611SJustin Bogner const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
76792a8c611SJustin Bogner return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
76892a8c611SJustin Bogner }
76992a8c611SJustin Bogner
7705f8f34e4SAdrian Prantl /// This does the actual lookup of an intrinsic ID which
77121241135SPete Cooper /// matches the given function name.
lookupIntrinsicID(StringRef Name)7726b3bd612STim Northover Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
77392a8c611SJustin Bogner ArrayRef<const char *> NameTable = findTargetSubtable(Name);
774c2752daaSReid Kleckner int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
77592a8c611SJustin Bogner if (Idx == -1)
77692a8c611SJustin Bogner return Intrinsic::not_intrinsic;
77792a8c611SJustin Bogner
77892a8c611SJustin Bogner // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
77992a8c611SJustin Bogner // an index into a sub-table.
78092a8c611SJustin Bogner int Adjust = NameTable.data() - IntrinsicNameTable;
78192a8c611SJustin Bogner Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
78221241135SPete Cooper
7830b5220d0SReid Kleckner // If the intrinsic is not overloaded, require an exact match. If it is
78412a4dc4cSRoman Tereshin // overloaded, require either exact or prefix match.
78512a4dc4cSRoman Tereshin const auto MatchSize = strlen(NameTable[Idx]);
78612a4dc4cSRoman Tereshin assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
78712a4dc4cSRoman Tereshin bool IsExactMatch = Name.size() == MatchSize;
7885d986953SReid Kleckner return IsExactMatch || Intrinsic::isOverloaded(ID) ? ID
7895d986953SReid Kleckner : Intrinsic::not_intrinsic;
79021241135SPete Cooper }
79121241135SPete Cooper
recalculateIntrinsicID()792a7c0c18cSPete Cooper void Function::recalculateIntrinsicID() {
793291abd3eSJustin Lebar StringRef Name = getName();
794291abd3eSJustin Lebar if (!Name.startswith("llvm.")) {
795291abd3eSJustin Lebar HasLLVMReservedName = false;
796a7c0c18cSPete Cooper IntID = Intrinsic::not_intrinsic;
797a7c0c18cSPete Cooper return;
798516d7039SMichael Ilseman }
799291abd3eSJustin Lebar HasLLVMReservedName = true;
800291abd3eSJustin Lebar IntID = lookupIntrinsicID(Name);
801516d7039SMichael Ilseman }
802516d7039SMichael Ilseman
803319c48ebSPhilip Reames /// Returns a stable mangling for the type specified for use in the name
804059ecbfbSPhilip Reames /// mangling scheme used by 'any' types in intrinsic signatures. The mangling
805059ecbfbSPhilip Reames /// of named types is simply their name. Manglings for unnamed types consist
806059ecbfbSPhilip Reames /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
807059ecbfbSPhilip Reames /// combined with the mangling of their component types. A vararg function
808059ecbfbSPhilip Reames /// type will have a suffix of 'vararg'. Since function types can contain
809059ecbfbSPhilip Reames /// other function types, we close a function type mangling with suffix 'f'
810059ecbfbSPhilip Reames /// which can't be confused with it's prefix. This ensures we don't have
811059ecbfbSPhilip Reames /// collisions between two unrelated function types. Otherwise, you might
812059ecbfbSPhilip Reames /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.)
81304790d9cSJeroen Dobbelaere /// The HasUnnamedType boolean is set if an unnamed type was encountered,
81404790d9cSJeroen Dobbelaere /// indicating that extra care must be taken to ensure a unique name.
getMangledTypeStr(Type * Ty,bool & HasUnnamedType)81504790d9cSJeroen Dobbelaere static std::string getMangledTypeStr(Type *Ty, bool &HasUnnamedType) {
816319c48ebSPhilip Reames std::string Result;
817319c48ebSPhilip Reames if (PointerType *PTyp = dyn_cast<PointerType>(Ty)) {
818ec08f03bSZequan Wu Result += "p" + utostr(PTyp->getAddressSpace());
819ec08f03bSZequan Wu // Opaque pointer doesn't have pointee type information, so we just mangle
820ec08f03bSZequan Wu // address space for opaque pointer.
821ec08f03bSZequan Wu if (!PTyp->isOpaque())
822aa97bc11SNikita Popov Result += getMangledTypeStr(PTyp->getNonOpaquePointerElementType(),
823aa97bc11SNikita Popov HasUnnamedType);
824319c48ebSPhilip Reames } else if (ArrayType *ATyp = dyn_cast<ArrayType>(Ty)) {
825eba7e4ecSEugene Zelenko Result += "a" + utostr(ATyp->getNumElements()) +
82604790d9cSJeroen Dobbelaere getMangledTypeStr(ATyp->getElementType(), HasUnnamedType);
827319c48ebSPhilip Reames } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
8283c1432feSDaniel Berlin if (!STyp->isLiteral()) {
8293c1432feSDaniel Berlin Result += "s_";
83004790d9cSJeroen Dobbelaere if (STyp->hasName())
831319c48ebSPhilip Reames Result += STyp->getName();
83204790d9cSJeroen Dobbelaere else
83304790d9cSJeroen Dobbelaere HasUnnamedType = true;
8343c1432feSDaniel Berlin } else {
8353c1432feSDaniel Berlin Result += "sl_";
8363c1432feSDaniel Berlin for (auto Elem : STyp->elements())
83704790d9cSJeroen Dobbelaere Result += getMangledTypeStr(Elem, HasUnnamedType);
8383c1432feSDaniel Berlin }
8393c1432feSDaniel Berlin // Ensure nested structs are distinguishable.
8403c1432feSDaniel Berlin Result += "s";
841319c48ebSPhilip Reames } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
84204790d9cSJeroen Dobbelaere Result += "f_" + getMangledTypeStr(FT->getReturnType(), HasUnnamedType);
843319c48ebSPhilip Reames for (size_t i = 0; i < FT->getNumParams(); i++)
84404790d9cSJeroen Dobbelaere Result += getMangledTypeStr(FT->getParamType(i), HasUnnamedType);
845319c48ebSPhilip Reames if (FT->isVarArg())
846319c48ebSPhilip Reames Result += "vararg";
847059ecbfbSPhilip Reames // Ensure nested function types are distinguishable.
848059ecbfbSPhilip Reames Result += "f";
8492ba5d64aSCullen Rhodes } else if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
8503d178581SChristopher Tetreault ElementCount EC = VTy->getElementCount();
851f4257c58SDavid Sherwood if (EC.isScalable())
8522ba5d64aSCullen Rhodes Result += "nx";
853f4257c58SDavid Sherwood Result += "v" + utostr(EC.getKnownMinValue()) +
85404790d9cSJeroen Dobbelaere getMangledTypeStr(VTy->getElementType(), HasUnnamedType);
8552fa14362SCraig Topper } else if (Ty) {
8562fa14362SCraig Topper switch (Ty->getTypeID()) {
8572fa14362SCraig Topper default: llvm_unreachable("Unhandled type");
8582fa14362SCraig Topper case Type::VoidTyID: Result += "isVoid"; break;
8592fa14362SCraig Topper case Type::MetadataTyID: Result += "Metadata"; break;
8602fa14362SCraig Topper case Type::HalfTyID: Result += "f16"; break;
8618c24f331STies Stuij case Type::BFloatTyID: Result += "bf16"; break;
8622fa14362SCraig Topper case Type::FloatTyID: Result += "f32"; break;
8632fa14362SCraig Topper case Type::DoubleTyID: Result += "f64"; break;
8642fa14362SCraig Topper case Type::X86_FP80TyID: Result += "f80"; break;
8652fa14362SCraig Topper case Type::FP128TyID: Result += "f128"; break;
8662fa14362SCraig Topper case Type::PPC_FP128TyID: Result += "ppcf128"; break;
8672fa14362SCraig Topper case Type::X86_MMXTyID: Result += "x86mmx"; break;
868981a0bd8SLuo, Yuanke case Type::X86_AMXTyID: Result += "x86amx"; break;
8692fa14362SCraig Topper case Type::IntegerTyID:
8702fa14362SCraig Topper Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
8712fa14362SCraig Topper break;
8722fa14362SCraig Topper }
8732fa14362SCraig Topper }
874319c48ebSPhilip Reames return Result;
875319c48ebSPhilip Reames }
876319c48ebSPhilip Reames
getBaseName(ID id)877bb8ce25eSJeroen Dobbelaere StringRef Intrinsic::getBaseName(ID id) {
878bb8ce25eSJeroen Dobbelaere assert(id < num_intrinsics && "Invalid intrinsic ID!");
879bb8ce25eSJeroen Dobbelaere return IntrinsicNameTable[id];
880bb8ce25eSJeroen Dobbelaere }
881bb8ce25eSJeroen Dobbelaere
getName(ID id)882a8db71e8SPete Cooper StringRef Intrinsic::getName(ID id) {
883a8db71e8SPete Cooper assert(id < num_intrinsics && "Invalid intrinsic ID!");
8845d986953SReid Kleckner assert(!Intrinsic::isOverloaded(id) &&
885a5f8c722SPete Cooper "This version of getName does not support overloading");
886bb8ce25eSJeroen Dobbelaere return getBaseName(id);
887a8db71e8SPete Cooper }
888a8db71e8SPete Cooper
getIntrinsicNameImpl(Intrinsic::ID Id,ArrayRef<Type * > Tys,Module * M,FunctionType * FT,bool EarlyModuleCheck)889bb8ce25eSJeroen Dobbelaere static std::string getIntrinsicNameImpl(Intrinsic::ID Id, ArrayRef<Type *> Tys,
890bb8ce25eSJeroen Dobbelaere Module *M, FunctionType *FT,
891bb8ce25eSJeroen Dobbelaere bool EarlyModuleCheck) {
892bb8ce25eSJeroen Dobbelaere
893bb8ce25eSJeroen Dobbelaere assert(Id < Intrinsic::num_intrinsics && "Invalid intrinsic ID!");
89404790d9cSJeroen Dobbelaere assert((Tys.empty() || Intrinsic::isOverloaded(Id)) &&
89580b0f74cSXun Li "This version of getName is for overloaded intrinsics only");
896bb8ce25eSJeroen Dobbelaere (void)EarlyModuleCheck;
897bb8ce25eSJeroen Dobbelaere assert((!EarlyModuleCheck || M ||
898bb8ce25eSJeroen Dobbelaere !any_of(Tys, [](Type *T) { return isa<PointerType>(T); })) &&
899bb8ce25eSJeroen Dobbelaere "Intrinsic overloading on pointer types need to provide a Module");
90004790d9cSJeroen Dobbelaere bool HasUnnamedType = false;
901bb8ce25eSJeroen Dobbelaere std::string Result(Intrinsic::getBaseName(Id));
902bb8ce25eSJeroen Dobbelaere for (Type *Ty : Tys)
90304790d9cSJeroen Dobbelaere Result += "." + getMangledTypeStr(Ty, HasUnnamedType);
904bb8ce25eSJeroen Dobbelaere if (HasUnnamedType) {
905bb8ce25eSJeroen Dobbelaere assert(M && "unnamed types need a module");
90604790d9cSJeroen Dobbelaere if (!FT)
907bb8ce25eSJeroen Dobbelaere FT = Intrinsic::getType(M->getContext(), Id, Tys);
90804790d9cSJeroen Dobbelaere else
909bb8ce25eSJeroen Dobbelaere assert((FT == Intrinsic::getType(M->getContext(), Id, Tys)) &&
91004790d9cSJeroen Dobbelaere "Provided FunctionType must match arguments");
91104790d9cSJeroen Dobbelaere return M->getUniqueIntrinsicName(Result, Id, FT);
912ef860a24SChandler Carruth }
913ef860a24SChandler Carruth return Result;
914ef860a24SChandler Carruth }
915ef860a24SChandler Carruth
getName(ID Id,ArrayRef<Type * > Tys,Module * M,FunctionType * FT)916bb8ce25eSJeroen Dobbelaere std::string Intrinsic::getName(ID Id, ArrayRef<Type *> Tys, Module *M,
917bb8ce25eSJeroen Dobbelaere FunctionType *FT) {
918bb8ce25eSJeroen Dobbelaere assert(M && "We need to have a Module");
919bb8ce25eSJeroen Dobbelaere return getIntrinsicNameImpl(Id, Tys, M, FT, true);
920bb8ce25eSJeroen Dobbelaere }
921bb8ce25eSJeroen Dobbelaere
getNameNoUnnamedTypes(ID Id,ArrayRef<Type * > Tys)922bb8ce25eSJeroen Dobbelaere std::string Intrinsic::getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> Tys) {
923bb8ce25eSJeroen Dobbelaere return getIntrinsicNameImpl(Id, Tys, nullptr, nullptr, false);
92404790d9cSJeroen Dobbelaere }
92504790d9cSJeroen Dobbelaere
926ef860a24SChandler Carruth /// IIT_Info - These are enumerators that describe the entries returned by the
927ef860a24SChandler Carruth /// getIntrinsicInfoTableEntries function.
928ef860a24SChandler Carruth ///
929ef860a24SChandler Carruth /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
930ef860a24SChandler Carruth enum IIT_Info {
93165c27568SRobert Khasanov // Common values should be encoded with 0-15.
932ef860a24SChandler Carruth IIT_Done = 0,
933ef860a24SChandler Carruth IIT_I1 = 1,
934ef860a24SChandler Carruth IIT_I8 = 2,
935ef860a24SChandler Carruth IIT_I16 = 3,
936ef860a24SChandler Carruth IIT_I32 = 4,
937ef860a24SChandler Carruth IIT_I64 = 5,
9386c6d715cSMichael Ilseman IIT_F16 = 6,
9396c6d715cSMichael Ilseman IIT_F32 = 7,
9406c6d715cSMichael Ilseman IIT_F64 = 8,
9416c6d715cSMichael Ilseman IIT_V2 = 9,
9426c6d715cSMichael Ilseman IIT_V4 = 10,
9436c6d715cSMichael Ilseman IIT_V8 = 11,
9446c6d715cSMichael Ilseman IIT_V16 = 12,
9456c6d715cSMichael Ilseman IIT_V32 = 13,
94665c27568SRobert Khasanov IIT_PTR = 14,
94765c27568SRobert Khasanov IIT_ARG = 15,
948ef860a24SChandler Carruth
94965c27568SRobert Khasanov // Values from 16+ are only encodable with the inefficient encoding.
95065c27568SRobert Khasanov IIT_V64 = 16,
951b25e562dSRobert Khasanov IIT_MMX = 17,
952917c7382SJoseph Tremoulet IIT_TOKEN = 18,
953917c7382SJoseph Tremoulet IIT_METADATA = 19,
954917c7382SJoseph Tremoulet IIT_EMPTYSTRUCT = 20,
955917c7382SJoseph Tremoulet IIT_STRUCT2 = 21,
956917c7382SJoseph Tremoulet IIT_STRUCT3 = 22,
957917c7382SJoseph Tremoulet IIT_STRUCT4 = 23,
958917c7382SJoseph Tremoulet IIT_STRUCT5 = 24,
959917c7382SJoseph Tremoulet IIT_EXTEND_ARG = 25,
960917c7382SJoseph Tremoulet IIT_TRUNC_ARG = 26,
961917c7382SJoseph Tremoulet IIT_ANYPTR = 27,
962917c7382SJoseph Tremoulet IIT_V1 = 28,
963917c7382SJoseph Tremoulet IIT_VARARG = 29,
964917c7382SJoseph Tremoulet IIT_HALF_VEC_ARG = 30,
965917c7382SJoseph Tremoulet IIT_SAME_VEC_WIDTH_ARG = 31,
966917c7382SJoseph Tremoulet IIT_PTR_TO_ARG = 32,
967caaceef4SElena Demikhovsky IIT_PTR_TO_ELT = 33,
968ef5798acSElad Cohen IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
969caaceef4SElena Demikhovsky IIT_I128 = 35,
970caaceef4SElena Demikhovsky IIT_V512 = 36,
971786ca6a1SArtem Belevich IIT_V1024 = 37,
972786ca6a1SArtem Belevich IIT_STRUCT6 = 38,
973786ca6a1SArtem Belevich IIT_STRUCT7 = 39,
97483a5fe14SStefan Pintilie IIT_STRUCT8 = 40,
97551c2fa0eSSander de Smalen IIT_F128 = 41,
9762ba5d64aSCullen Rhodes IIT_VEC_ELEMENT = 42,
97722a8f35cSKerry McLaughlin IIT_SCALABLE_VEC = 43,
97822a8f35cSKerry McLaughlin IIT_SUBDIVIDE2_ARG = 44,
97976365b3bSKerry McLaughlin IIT_SUBDIVIDE4_ARG = 45,
980bc23e83eSKrzysztof Parzyszek IIT_VEC_OF_BITCASTS_TO_INT = 46,
981ad5d319eSTies Stuij IIT_V128 = 47,
982413577a8SXiang1 Zhang IIT_BF16 = 48,
983f3202b30SAhsan Saghir IIT_STRUCT9 = 49,
984981a0bd8SLuo, Yuanke IIT_V256 = 50,
985741aeda9SQiu Chaofan IIT_AMX = 51,
986c8e84c7aSJay Foad IIT_PPCF128 = 52,
987c8e84c7aSJay Foad IIT_V3 = 53,
988a96d8285SPaulo Matos IIT_EXTERNREF = 54,
98949c2206bSCraig Topper IIT_FUNCREF = 55,
99049c2206bSCraig Topper IIT_ANYPTR_TO_ELT = 56,
991a92ed167SHendrik Greving IIT_I2 = 57,
992a92ed167SHendrik Greving IIT_I4 = 58,
993ef860a24SChandler Carruth };
994ef860a24SChandler Carruth
DecodeIITType(unsigned & NextElt,ArrayRef<unsigned char> Infos,IIT_Info LastInfo,SmallVectorImpl<Intrinsic::IITDescriptor> & OutputTable)995ef860a24SChandler Carruth static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
996a3e3986bSDavid Sherwood IIT_Info LastInfo,
997ef860a24SChandler Carruth SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
998eba7e4ecSEugene Zelenko using namespace Intrinsic;
999eba7e4ecSEugene Zelenko
1000a3e3986bSDavid Sherwood bool IsScalableVector = (LastInfo == IIT_SCALABLE_VEC);
10011c3d9c2fSDavid Sherwood
1002ef860a24SChandler Carruth IIT_Info Info = IIT_Info(Infos[NextElt++]);
1003ef860a24SChandler Carruth unsigned StructElts = 2;
1004ef860a24SChandler Carruth
1005ef860a24SChandler Carruth switch (Info) {
1006ef860a24SChandler Carruth case IIT_Done:
1007ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
1008ef860a24SChandler Carruth return;
1009a2efd99bSAndrew Trick case IIT_VARARG:
1010a2efd99bSAndrew Trick OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
1011a2efd99bSAndrew Trick return;
1012ef860a24SChandler Carruth case IIT_MMX:
1013ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
1014ef860a24SChandler Carruth return;
1015981a0bd8SLuo, Yuanke case IIT_AMX:
1016981a0bd8SLuo, Yuanke OutputTable.push_back(IITDescriptor::get(IITDescriptor::AMX, 0));
1017981a0bd8SLuo, Yuanke return;
1018917c7382SJoseph Tremoulet case IIT_TOKEN:
1019917c7382SJoseph Tremoulet OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
1020917c7382SJoseph Tremoulet return;
1021ef860a24SChandler Carruth case IIT_METADATA:
1022ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
1023ef860a24SChandler Carruth return;
10246c6d715cSMichael Ilseman case IIT_F16:
10256c6d715cSMichael Ilseman OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
10266c6d715cSMichael Ilseman return;
1027ad5d319eSTies Stuij case IIT_BF16:
1028ad5d319eSTies Stuij OutputTable.push_back(IITDescriptor::get(IITDescriptor::BFloat, 0));
1029ad5d319eSTies Stuij return;
1030ef860a24SChandler Carruth case IIT_F32:
1031ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
1032ef860a24SChandler Carruth return;
1033ef860a24SChandler Carruth case IIT_F64:
1034ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
1035ef860a24SChandler Carruth return;
103683a5fe14SStefan Pintilie case IIT_F128:
103783a5fe14SStefan Pintilie OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0));
103883a5fe14SStefan Pintilie return;
1039741aeda9SQiu Chaofan case IIT_PPCF128:
1040741aeda9SQiu Chaofan OutputTable.push_back(IITDescriptor::get(IITDescriptor::PPCQuad, 0));
1041741aeda9SQiu Chaofan return;
1042ef860a24SChandler Carruth case IIT_I1:
1043ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
1044ef860a24SChandler Carruth return;
1045a92ed167SHendrik Greving case IIT_I2:
1046a92ed167SHendrik Greving OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 2));
1047a92ed167SHendrik Greving return;
1048a92ed167SHendrik Greving case IIT_I4:
1049a92ed167SHendrik Greving OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 4));
1050a92ed167SHendrik Greving return;
1051ef860a24SChandler Carruth case IIT_I8:
1052ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
1053ef860a24SChandler Carruth return;
1054ef860a24SChandler Carruth case IIT_I16:
1055ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
1056ef860a24SChandler Carruth return;
1057ef860a24SChandler Carruth case IIT_I32:
1058ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
1059ef860a24SChandler Carruth return;
1060ef860a24SChandler Carruth case IIT_I64:
1061ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
1062ef860a24SChandler Carruth return;
10636646033eSKit Barton case IIT_I128:
10646646033eSKit Barton OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
10656646033eSKit Barton return;
106663dc840fSJiangning Liu case IIT_V1:
10671c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(1, IsScalableVector));
1068a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
106963dc840fSJiangning Liu return;
1070ef860a24SChandler Carruth case IIT_V2:
10711c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(2, IsScalableVector));
1072a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1073ef860a24SChandler Carruth return;
1074c8e84c7aSJay Foad case IIT_V3:
1075c8e84c7aSJay Foad OutputTable.push_back(IITDescriptor::getVector(3, IsScalableVector));
1076c8e84c7aSJay Foad DecodeIITType(NextElt, Infos, Info, OutputTable);
1077c8e84c7aSJay Foad return;
1078ef860a24SChandler Carruth case IIT_V4:
10791c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(4, IsScalableVector));
1080a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1081ef860a24SChandler Carruth return;
1082ef860a24SChandler Carruth case IIT_V8:
10831c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(8, IsScalableVector));
1084a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1085ef860a24SChandler Carruth return;
1086ef860a24SChandler Carruth case IIT_V16:
10871c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(16, IsScalableVector));
1088a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1089ef860a24SChandler Carruth return;
1090ef860a24SChandler Carruth case IIT_V32:
10911c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(32, IsScalableVector));
1092a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1093ef860a24SChandler Carruth return;
1094b25e562dSRobert Khasanov case IIT_V64:
10951c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(64, IsScalableVector));
1096a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1097b25e562dSRobert Khasanov return;
1098bc23e83eSKrzysztof Parzyszek case IIT_V128:
10991c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(128, IsScalableVector));
1100a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1101bc23e83eSKrzysztof Parzyszek return;
1102f3202b30SAhsan Saghir case IIT_V256:
1103f3202b30SAhsan Saghir OutputTable.push_back(IITDescriptor::getVector(256, IsScalableVector));
1104f3202b30SAhsan Saghir DecodeIITType(NextElt, Infos, Info, OutputTable);
1105f3202b30SAhsan Saghir return;
1106b8bb90b7SKrzysztof Parzyszek case IIT_V512:
11071c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(512, IsScalableVector));
1108a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1109b8bb90b7SKrzysztof Parzyszek return;
1110b8bb90b7SKrzysztof Parzyszek case IIT_V1024:
11111c3d9c2fSDavid Sherwood OutputTable.push_back(IITDescriptor::getVector(1024, IsScalableVector));
1112a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1113b8bb90b7SKrzysztof Parzyszek return;
1114a96d8285SPaulo Matos case IIT_EXTERNREF:
1115a96d8285SPaulo Matos OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 10));
1116a96d8285SPaulo Matos OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
1117a96d8285SPaulo Matos return;
1118a96d8285SPaulo Matos case IIT_FUNCREF:
1119a96d8285SPaulo Matos OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 20));
1120a96d8285SPaulo Matos OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
1121a96d8285SPaulo Matos return;
1122ef860a24SChandler Carruth case IIT_PTR:
1123ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
1124a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1125ef860a24SChandler Carruth return;
1126ef860a24SChandler Carruth case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
1127ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
1128ef860a24SChandler Carruth Infos[NextElt++]));
1129a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1130ef860a24SChandler Carruth return;
1131ef860a24SChandler Carruth }
1132ef860a24SChandler Carruth case IIT_ARG: {
1133ef860a24SChandler Carruth unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1134ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
1135ef860a24SChandler Carruth return;
1136ef860a24SChandler Carruth }
1137aa3cf1e6STim Northover case IIT_EXTEND_ARG: {
1138ef860a24SChandler Carruth unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1139aa3cf1e6STim Northover OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
1140ef860a24SChandler Carruth ArgInfo));
1141ef860a24SChandler Carruth return;
1142ef860a24SChandler Carruth }
1143aa3cf1e6STim Northover case IIT_TRUNC_ARG: {
1144ef860a24SChandler Carruth unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1145aa3cf1e6STim Northover OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
1146ef860a24SChandler Carruth ArgInfo));
1147ef860a24SChandler Carruth return;
1148ef860a24SChandler Carruth }
11494516de34STim Northover case IIT_HALF_VEC_ARG: {
11504516de34STim Northover unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
11514516de34STim Northover OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
11524516de34STim Northover ArgInfo));
11534516de34STim Northover return;
11544516de34STim Northover }
1155f1de34b8SElena Demikhovsky case IIT_SAME_VEC_WIDTH_ARG: {
1156f1de34b8SElena Demikhovsky unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1157f1de34b8SElena Demikhovsky OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
1158f1de34b8SElena Demikhovsky ArgInfo));
1159f1de34b8SElena Demikhovsky return;
1160f1de34b8SElena Demikhovsky }
1161fb81b93eSElena Demikhovsky case IIT_PTR_TO_ARG: {
1162fb81b93eSElena Demikhovsky unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1163fb81b93eSElena Demikhovsky OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
1164fb81b93eSElena Demikhovsky ArgInfo));
1165fb81b93eSElena Demikhovsky return;
1166fb81b93eSElena Demikhovsky }
1167caaceef4SElena Demikhovsky case IIT_PTR_TO_ELT: {
1168caaceef4SElena Demikhovsky unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1169caaceef4SElena Demikhovsky OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToElt, ArgInfo));
1170caaceef4SElena Demikhovsky return;
1171caaceef4SElena Demikhovsky }
117249c2206bSCraig Topper case IIT_ANYPTR_TO_ELT: {
117349c2206bSCraig Topper unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
117449c2206bSCraig Topper unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
117549c2206bSCraig Topper OutputTable.push_back(
117649c2206bSCraig Topper IITDescriptor::get(IITDescriptor::AnyPtrToElt, ArgNo, RefNo));
117749c2206bSCraig Topper return;
117849c2206bSCraig Topper }
1179ef5798acSElad Cohen case IIT_VEC_OF_ANYPTRS_TO_ELT: {
1180ef5798acSElad Cohen unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1181ef5798acSElad Cohen unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
1182ef5798acSElad Cohen OutputTable.push_back(
1183ef5798acSElad Cohen IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo));
118423a485a4SElena Demikhovsky return;
118523a485a4SElena Demikhovsky }
1186ef860a24SChandler Carruth case IIT_EMPTYSTRUCT:
1187ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
1188ef860a24SChandler Carruth return;
1189413577a8SXiang1 Zhang case IIT_STRUCT9: ++StructElts; LLVM_FALLTHROUGH;
1190786ca6a1SArtem Belevich case IIT_STRUCT8: ++StructElts; LLVM_FALLTHROUGH;
1191786ca6a1SArtem Belevich case IIT_STRUCT7: ++StructElts; LLVM_FALLTHROUGH;
1192786ca6a1SArtem Belevich case IIT_STRUCT6: ++StructElts; LLVM_FALLTHROUGH;
1193cd1d5aafSJustin Bogner case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH;
1194cd1d5aafSJustin Bogner case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH;
1195cd1d5aafSJustin Bogner case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH;
1196ef860a24SChandler Carruth case IIT_STRUCT2: {
1197ef860a24SChandler Carruth OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
1198ef860a24SChandler Carruth
1199ef860a24SChandler Carruth for (unsigned i = 0; i != StructElts; ++i)
1200a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
1201ef860a24SChandler Carruth return;
1202ef860a24SChandler Carruth }
120322a8f35cSKerry McLaughlin case IIT_SUBDIVIDE2_ARG: {
120422a8f35cSKerry McLaughlin unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
120522a8f35cSKerry McLaughlin OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide2Argument,
120622a8f35cSKerry McLaughlin ArgInfo));
120722a8f35cSKerry McLaughlin return;
120822a8f35cSKerry McLaughlin }
120922a8f35cSKerry McLaughlin case IIT_SUBDIVIDE4_ARG: {
121022a8f35cSKerry McLaughlin unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
121122a8f35cSKerry McLaughlin OutputTable.push_back(IITDescriptor::get(IITDescriptor::Subdivide4Argument,
121222a8f35cSKerry McLaughlin ArgInfo));
121322a8f35cSKerry McLaughlin return;
121422a8f35cSKerry McLaughlin }
121551c2fa0eSSander de Smalen case IIT_VEC_ELEMENT: {
121651c2fa0eSSander de Smalen unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
121751c2fa0eSSander de Smalen OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecElementArgument,
121851c2fa0eSSander de Smalen ArgInfo));
121951c2fa0eSSander de Smalen return;
122051c2fa0eSSander de Smalen }
12212ba5d64aSCullen Rhodes case IIT_SCALABLE_VEC: {
1222a3e3986bSDavid Sherwood DecodeIITType(NextElt, Infos, Info, OutputTable);
12232ba5d64aSCullen Rhodes return;
12242ba5d64aSCullen Rhodes }
122576365b3bSKerry McLaughlin case IIT_VEC_OF_BITCASTS_TO_INT: {
122676365b3bSKerry McLaughlin unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
122776365b3bSKerry McLaughlin OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfBitcastsToInt,
122876365b3bSKerry McLaughlin ArgInfo));
122976365b3bSKerry McLaughlin return;
123076365b3bSKerry McLaughlin }
1231ef860a24SChandler Carruth }
1232ef860a24SChandler Carruth llvm_unreachable("unhandled");
1233ef860a24SChandler Carruth }
1234ef860a24SChandler Carruth
1235ef860a24SChandler Carruth #define GET_INTRINSIC_GENERATOR_GLOBAL
1236f5890e4eSReid Kleckner #include "llvm/IR/IntrinsicImpl.inc"
1237ef860a24SChandler Carruth #undef GET_INTRINSIC_GENERATOR_GLOBAL
1238ef860a24SChandler Carruth
getIntrinsicInfoTableEntries(ID id,SmallVectorImpl<IITDescriptor> & T)1239ef860a24SChandler Carruth void Intrinsic::getIntrinsicInfoTableEntries(ID id,
1240ef860a24SChandler Carruth SmallVectorImpl<IITDescriptor> &T){
1241ef860a24SChandler Carruth // Check to see if the intrinsic's type was expressible by the table.
1242ef860a24SChandler Carruth unsigned TableVal = IIT_Table[id-1];
1243ef860a24SChandler Carruth
1244ef860a24SChandler Carruth // Decode the TableVal into an array of IITValues.
1245ef860a24SChandler Carruth SmallVector<unsigned char, 8> IITValues;
1246ef860a24SChandler Carruth ArrayRef<unsigned char> IITEntries;
1247ef860a24SChandler Carruth unsigned NextElt = 0;
1248ef860a24SChandler Carruth if ((TableVal >> 31) != 0) {
1249ef860a24SChandler Carruth // This is an offset into the IIT_LongEncodingTable.
1250ef860a24SChandler Carruth IITEntries = IIT_LongEncodingTable;
1251ef860a24SChandler Carruth
1252ef860a24SChandler Carruth // Strip sentinel bit.
1253ef860a24SChandler Carruth NextElt = (TableVal << 1) >> 1;
1254ef860a24SChandler Carruth } else {
1255ef860a24SChandler Carruth // Decode the TableVal into an array of IITValues. If the entry was encoded
1256ef860a24SChandler Carruth // into a single word in the table itself, decode it now.
1257ef860a24SChandler Carruth do {
1258ef860a24SChandler Carruth IITValues.push_back(TableVal & 0xF);
1259ef860a24SChandler Carruth TableVal >>= 4;
1260ef860a24SChandler Carruth } while (TableVal);
1261ef860a24SChandler Carruth
1262ef860a24SChandler Carruth IITEntries = IITValues;
1263ef860a24SChandler Carruth NextElt = 0;
1264ef860a24SChandler Carruth }
1265ef860a24SChandler Carruth
1266ef860a24SChandler Carruth // Okay, decode the table into the output vector of IITDescriptors.
1267a3e3986bSDavid Sherwood DecodeIITType(NextElt, IITEntries, IIT_Done, T);
1268ef860a24SChandler Carruth while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
1269a3e3986bSDavid Sherwood DecodeIITType(NextElt, IITEntries, IIT_Done, T);
1270ef860a24SChandler Carruth }
1271ef860a24SChandler Carruth
DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> & Infos,ArrayRef<Type * > Tys,LLVMContext & Context)1272ef860a24SChandler Carruth static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
1273ef860a24SChandler Carruth ArrayRef<Type*> Tys, LLVMContext &Context) {
1274ef860a24SChandler Carruth using namespace Intrinsic;
1275eba7e4ecSEugene Zelenko
1276ef860a24SChandler Carruth IITDescriptor D = Infos.front();
1277ef860a24SChandler Carruth Infos = Infos.slice(1);
1278ef860a24SChandler Carruth
1279ef860a24SChandler Carruth switch (D.Kind) {
1280ef860a24SChandler Carruth case IITDescriptor::Void: return Type::getVoidTy(Context);
1281a2efd99bSAndrew Trick case IITDescriptor::VarArg: return Type::getVoidTy(Context);
1282ef860a24SChandler Carruth case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
1283981a0bd8SLuo, Yuanke case IITDescriptor::AMX: return Type::getX86_AMXTy(Context);
1284917c7382SJoseph Tremoulet case IITDescriptor::Token: return Type::getTokenTy(Context);
1285ef860a24SChandler Carruth case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
12866c6d715cSMichael Ilseman case IITDescriptor::Half: return Type::getHalfTy(Context);
1287ad5d319eSTies Stuij case IITDescriptor::BFloat: return Type::getBFloatTy(Context);
1288ef860a24SChandler Carruth case IITDescriptor::Float: return Type::getFloatTy(Context);
1289ef860a24SChandler Carruth case IITDescriptor::Double: return Type::getDoubleTy(Context);
129083a5fe14SStefan Pintilie case IITDescriptor::Quad: return Type::getFP128Ty(Context);
1291741aeda9SQiu Chaofan case IITDescriptor::PPCQuad: return Type::getPPC_FP128Ty(Context);
1292ef860a24SChandler Carruth
1293ef860a24SChandler Carruth case IITDescriptor::Integer:
1294ef860a24SChandler Carruth return IntegerType::get(Context, D.Integer_Width);
1295ef860a24SChandler Carruth case IITDescriptor::Vector:
12961c3d9c2fSDavid Sherwood return VectorType::get(DecodeFixedType(Infos, Tys, Context),
12971c3d9c2fSDavid Sherwood D.Vector_Width);
1298ef860a24SChandler Carruth case IITDescriptor::Pointer:
1299ef860a24SChandler Carruth return PointerType::get(DecodeFixedType(Infos, Tys, Context),
1300ef860a24SChandler Carruth D.Pointer_AddressSpace);
1301ef860a24SChandler Carruth case IITDescriptor::Struct: {
1302786ca6a1SArtem Belevich SmallVector<Type *, 8> Elts;
1303ef860a24SChandler Carruth for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1304786ca6a1SArtem Belevich Elts.push_back(DecodeFixedType(Infos, Tys, Context));
1305786ca6a1SArtem Belevich return StructType::get(Context, Elts);
1306ef860a24SChandler Carruth }
1307ef860a24SChandler Carruth case IITDescriptor::Argument:
1308ef860a24SChandler Carruth return Tys[D.getArgumentNumber()];
1309aa3cf1e6STim Northover case IITDescriptor::ExtendArgument: {
1310aa3cf1e6STim Northover Type *Ty = Tys[D.getArgumentNumber()];
1311aa3cf1e6STim Northover if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1312aa3cf1e6STim Northover return VectorType::getExtendedElementVectorType(VTy);
1313ef860a24SChandler Carruth
1314aa3cf1e6STim Northover return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
1315aa3cf1e6STim Northover }
1316aa3cf1e6STim Northover case IITDescriptor::TruncArgument: {
1317aa3cf1e6STim Northover Type *Ty = Tys[D.getArgumentNumber()];
1318aa3cf1e6STim Northover if (VectorType *VTy = dyn_cast<VectorType>(Ty))
1319aa3cf1e6STim Northover return VectorType::getTruncatedElementVectorType(VTy);
1320aa3cf1e6STim Northover
1321aa3cf1e6STim Northover IntegerType *ITy = cast<IntegerType>(Ty);
1322aa3cf1e6STim Northover assert(ITy->getBitWidth() % 2 == 0);
1323aa3cf1e6STim Northover return IntegerType::get(Context, ITy->getBitWidth() / 2);
1324aa3cf1e6STim Northover }
132522a8f35cSKerry McLaughlin case IITDescriptor::Subdivide2Argument:
132622a8f35cSKerry McLaughlin case IITDescriptor::Subdivide4Argument: {
132722a8f35cSKerry McLaughlin Type *Ty = Tys[D.getArgumentNumber()];
132822a8f35cSKerry McLaughlin VectorType *VTy = dyn_cast<VectorType>(Ty);
132922a8f35cSKerry McLaughlin assert(VTy && "Expected an argument of Vector Type");
133022a8f35cSKerry McLaughlin int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
133122a8f35cSKerry McLaughlin return VectorType::getSubdividedVectorType(VTy, SubDivs);
133222a8f35cSKerry McLaughlin }
13334516de34STim Northover case IITDescriptor::HalfVecArgument:
13344516de34STim Northover return VectorType::getHalfElementsVectorType(cast<VectorType>(
13354516de34STim Northover Tys[D.getArgumentNumber()]));
1336fb81b93eSElena Demikhovsky case IITDescriptor::SameVecWidthArgument: {
1337f1de34b8SElena Demikhovsky Type *EltTy = DecodeFixedType(Infos, Tys, Context);
1338f1de34b8SElena Demikhovsky Type *Ty = Tys[D.getArgumentNumber()];
1339f87226ebSSimon Pilgrim if (auto *VTy = dyn_cast<VectorType>(Ty))
13402ba5d64aSCullen Rhodes return VectorType::get(EltTy, VTy->getElementCount());
1341f87226ebSSimon Pilgrim return EltTy;
1342ef860a24SChandler Carruth }
1343fb81b93eSElena Demikhovsky case IITDescriptor::PtrToArgument: {
1344fb81b93eSElena Demikhovsky Type *Ty = Tys[D.getArgumentNumber()];
1345fb81b93eSElena Demikhovsky return PointerType::getUnqual(Ty);
1346fb81b93eSElena Demikhovsky }
1347caaceef4SElena Demikhovsky case IITDescriptor::PtrToElt: {
1348caaceef4SElena Demikhovsky Type *Ty = Tys[D.getArgumentNumber()];
1349caaceef4SElena Demikhovsky VectorType *VTy = dyn_cast<VectorType>(Ty);
1350caaceef4SElena Demikhovsky if (!VTy)
1351caaceef4SElena Demikhovsky llvm_unreachable("Expected an argument of Vector Type");
135240ed21bbSChristopher Tetreault Type *EltTy = VTy->getElementType();
1353caaceef4SElena Demikhovsky return PointerType::getUnqual(EltTy);
1354caaceef4SElena Demikhovsky }
135551c2fa0eSSander de Smalen case IITDescriptor::VecElementArgument: {
135651c2fa0eSSander de Smalen Type *Ty = Tys[D.getArgumentNumber()];
135751c2fa0eSSander de Smalen if (VectorType *VTy = dyn_cast<VectorType>(Ty))
135851c2fa0eSSander de Smalen return VTy->getElementType();
135951c2fa0eSSander de Smalen llvm_unreachable("Expected an argument of Vector Type");
136051c2fa0eSSander de Smalen }
136176365b3bSKerry McLaughlin case IITDescriptor::VecOfBitcastsToInt: {
136276365b3bSKerry McLaughlin Type *Ty = Tys[D.getArgumentNumber()];
136376365b3bSKerry McLaughlin VectorType *VTy = dyn_cast<VectorType>(Ty);
136476365b3bSKerry McLaughlin assert(VTy && "Expected an argument of Vector Type");
136576365b3bSKerry McLaughlin return VectorType::getInteger(VTy);
136676365b3bSKerry McLaughlin }
1367ef5798acSElad Cohen case IITDescriptor::VecOfAnyPtrsToElt:
1368ef5798acSElad Cohen // Return the overloaded type (which determines the pointers address space)
1369ef5798acSElad Cohen return Tys[D.getOverloadArgNumber()];
137049c2206bSCraig Topper case IITDescriptor::AnyPtrToElt:
137149c2206bSCraig Topper // Return the overloaded type (which determines the pointers address space)
137249c2206bSCraig Topper return Tys[D.getOverloadArgNumber()];
1373fb81b93eSElena Demikhovsky }
1374ef860a24SChandler Carruth llvm_unreachable("unhandled");
1375ef860a24SChandler Carruth }
1376ef860a24SChandler Carruth
getType(LLVMContext & Context,ID id,ArrayRef<Type * > Tys)1377ef860a24SChandler Carruth FunctionType *Intrinsic::getType(LLVMContext &Context,
1378ef860a24SChandler Carruth ID id, ArrayRef<Type*> Tys) {
1379ef860a24SChandler Carruth SmallVector<IITDescriptor, 8> Table;
1380ef860a24SChandler Carruth getIntrinsicInfoTableEntries(id, Table);
1381ef860a24SChandler Carruth
1382ef860a24SChandler Carruth ArrayRef<IITDescriptor> TableRef = Table;
1383ef860a24SChandler Carruth Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
1384ef860a24SChandler Carruth
1385ef860a24SChandler Carruth SmallVector<Type*, 8> ArgTys;
1386ef860a24SChandler Carruth while (!TableRef.empty())
1387ef860a24SChandler Carruth ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
1388ef860a24SChandler Carruth
1389d05affccSSteven Wu // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
1390d05affccSSteven Wu // If we see void type as the type of the last argument, it is vararg intrinsic
1391d05affccSSteven Wu if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
1392d05affccSSteven Wu ArgTys.pop_back();
1393d05affccSSteven Wu return FunctionType::get(ResultTy, ArgTys, true);
1394d05affccSSteven Wu }
1395ef860a24SChandler Carruth return FunctionType::get(ResultTy, ArgTys, false);
1396ef860a24SChandler Carruth }
1397ef860a24SChandler Carruth
isOverloaded(ID id)1398ef860a24SChandler Carruth bool Intrinsic::isOverloaded(ID id) {
1399ef860a24SChandler Carruth #define GET_INTRINSIC_OVERLOAD_TABLE
1400f5890e4eSReid Kleckner #include "llvm/IR/IntrinsicImpl.inc"
1401ef860a24SChandler Carruth #undef GET_INTRINSIC_OVERLOAD_TABLE
1402ef860a24SChandler Carruth }
1403ef860a24SChandler Carruth
isLeaf(ID id)1404c65d43e6SSanjoy Das bool Intrinsic::isLeaf(ID id) {
1405c65d43e6SSanjoy Das switch (id) {
1406c65d43e6SSanjoy Das default:
1407c65d43e6SSanjoy Das return true;
1408c65d43e6SSanjoy Das
1409c65d43e6SSanjoy Das case Intrinsic::experimental_gc_statepoint:
1410c65d43e6SSanjoy Das case Intrinsic::experimental_patchpoint_void:
1411c65d43e6SSanjoy Das case Intrinsic::experimental_patchpoint_i64:
1412c65d43e6SSanjoy Das return false;
1413c65d43e6SSanjoy Das }
1414c65d43e6SSanjoy Das }
1415c65d43e6SSanjoy Das
1416ef860a24SChandler Carruth /// This defines the "Intrinsic::getAttributes(ID id)" method.
1417ef860a24SChandler Carruth #define GET_INTRINSIC_ATTRIBUTES
1418f5890e4eSReid Kleckner #include "llvm/IR/IntrinsicImpl.inc"
1419ef860a24SChandler Carruth #undef GET_INTRINSIC_ATTRIBUTES
1420ef860a24SChandler Carruth
getDeclaration(Module * M,ID id,ArrayRef<Type * > Tys)1421ef860a24SChandler Carruth Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
1422ef860a24SChandler Carruth // There can never be multiple globals with the same name of different types,
1423ef860a24SChandler Carruth // because intrinsics must be a specific type.
142404790d9cSJeroen Dobbelaere auto *FT = getType(M->getContext(), id, Tys);
142513680223SJames Y Knight return cast<Function>(
142604790d9cSJeroen Dobbelaere M->getOrInsertFunction(Tys.empty() ? getName(id)
142704790d9cSJeroen Dobbelaere : getName(id, Tys, M, FT),
142813680223SJames Y Knight getType(M->getContext(), id, Tys))
142913680223SJames Y Knight .getCallee());
1430ef860a24SChandler Carruth }
1431ef860a24SChandler Carruth
1432d0a4450eSGuillaume Gomez // This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method.
1433d0a4450eSGuillaume Gomez #define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
1434f5890e4eSReid Kleckner #include "llvm/IR/IntrinsicImpl.inc"
1435d0a4450eSGuillaume Gomez #undef GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
1436ef860a24SChandler Carruth
14374e63fc49SSaleem Abdulrasool // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
14384e63fc49SSaleem Abdulrasool #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
1439f5890e4eSReid Kleckner #include "llvm/IR/IntrinsicImpl.inc"
14404e63fc49SSaleem Abdulrasool #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
14414e63fc49SSaleem Abdulrasool
14427957fc65SSander de Smalen using DeferredIntrinsicMatchPair =
14437957fc65SSander de Smalen std::pair<Type *, ArrayRef<Intrinsic::IITDescriptor>>;
14447957fc65SSander de Smalen
matchIntrinsicType(Type * Ty,ArrayRef<Intrinsic::IITDescriptor> & Infos,SmallVectorImpl<Type * > & ArgTys,SmallVectorImpl<DeferredIntrinsicMatchPair> & DeferredChecks,bool IsDeferredCheck)14457957fc65SSander de Smalen static bool matchIntrinsicType(
14467957fc65SSander de Smalen Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
14477957fc65SSander de Smalen SmallVectorImpl<Type *> &ArgTys,
14487957fc65SSander de Smalen SmallVectorImpl<DeferredIntrinsicMatchPair> &DeferredChecks,
14497957fc65SSander de Smalen bool IsDeferredCheck) {
1450bc552275SArtur Pilipenko using namespace Intrinsic;
1451bc552275SArtur Pilipenko
1452bc552275SArtur Pilipenko // If we ran out of descriptors, there are too many arguments.
1453bc552275SArtur Pilipenko if (Infos.empty()) return true;
14547957fc65SSander de Smalen
14557957fc65SSander de Smalen // Do this before slicing off the 'front' part
14567957fc65SSander de Smalen auto InfosRef = Infos;
14577957fc65SSander de Smalen auto DeferCheck = [&DeferredChecks, &InfosRef](Type *T) {
14587957fc65SSander de Smalen DeferredChecks.emplace_back(T, InfosRef);
14597957fc65SSander de Smalen return false;
14607957fc65SSander de Smalen };
14617957fc65SSander de Smalen
1462bc552275SArtur Pilipenko IITDescriptor D = Infos.front();
1463bc552275SArtur Pilipenko Infos = Infos.slice(1);
1464bc552275SArtur Pilipenko
1465bc552275SArtur Pilipenko switch (D.Kind) {
1466bc552275SArtur Pilipenko case IITDescriptor::Void: return !Ty->isVoidTy();
1467bc552275SArtur Pilipenko case IITDescriptor::VarArg: return true;
1468bc552275SArtur Pilipenko case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
1469981a0bd8SLuo, Yuanke case IITDescriptor::AMX: return !Ty->isX86_AMXTy();
1470bc552275SArtur Pilipenko case IITDescriptor::Token: return !Ty->isTokenTy();
1471bc552275SArtur Pilipenko case IITDescriptor::Metadata: return !Ty->isMetadataTy();
1472bc552275SArtur Pilipenko case IITDescriptor::Half: return !Ty->isHalfTy();
1473ad5d319eSTies Stuij case IITDescriptor::BFloat: return !Ty->isBFloatTy();
1474bc552275SArtur Pilipenko case IITDescriptor::Float: return !Ty->isFloatTy();
1475bc552275SArtur Pilipenko case IITDescriptor::Double: return !Ty->isDoubleTy();
147683a5fe14SStefan Pintilie case IITDescriptor::Quad: return !Ty->isFP128Ty();
1477741aeda9SQiu Chaofan case IITDescriptor::PPCQuad: return !Ty->isPPC_FP128Ty();
1478bc552275SArtur Pilipenko case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1479bc552275SArtur Pilipenko case IITDescriptor::Vector: {
14801c3d9c2fSDavid Sherwood VectorType *VT = dyn_cast<VectorType>(Ty);
14811c3d9c2fSDavid Sherwood return !VT || VT->getElementCount() != D.Vector_Width ||
14827957fc65SSander de Smalen matchIntrinsicType(VT->getElementType(), Infos, ArgTys,
14837957fc65SSander de Smalen DeferredChecks, IsDeferredCheck);
1484bc552275SArtur Pilipenko }
1485bc552275SArtur Pilipenko case IITDescriptor::Pointer: {
1486bc552275SArtur Pilipenko PointerType *PT = dyn_cast<PointerType>(Ty);
14878c7349b3SNikita Popov if (!PT || PT->getAddressSpace() != D.Pointer_AddressSpace)
14888c7349b3SNikita Popov return true;
14898a72391fSNikita Popov if (!PT->isOpaque()) {
14908a72391fSNikita Popov /* Manually consume a pointer to empty struct descriptor, which is
14918a72391fSNikita Popov * used for externref. We don't want to enforce that the struct is
14928a72391fSNikita Popov * anonymous in this case. (This renders externref intrinsics
14938a72391fSNikita Popov * non-unique, but this will go away with opaque pointers anyway.) */
14948a72391fSNikita Popov if (Infos.front().Kind == IITDescriptor::Struct &&
14958a72391fSNikita Popov Infos.front().Struct_NumElements == 0) {
14968a72391fSNikita Popov Infos = Infos.slice(1);
14978a72391fSNikita Popov return false;
14988a72391fSNikita Popov }
1499aa97bc11SNikita Popov return matchIntrinsicType(PT->getNonOpaquePointerElementType(), Infos,
1500aa97bc11SNikita Popov ArgTys, DeferredChecks, IsDeferredCheck);
15018a72391fSNikita Popov }
15028c7349b3SNikita Popov // Consume IIT descriptors relating to the pointer element type.
1503510a2bbdSHendrik Greving // FIXME: Intrinsic type matching of nested single value types or even
1504510a2bbdSHendrik Greving // aggregates doesn't work properly with opaque pointers but hopefully
1505510a2bbdSHendrik Greving // doesn't happen in practice.
1506510a2bbdSHendrik Greving while (Infos.front().Kind == IITDescriptor::Pointer ||
1507510a2bbdSHendrik Greving Infos.front().Kind == IITDescriptor::Vector)
15088c7349b3SNikita Popov Infos = Infos.slice(1);
15098c975eacSThomas Preud'homme assert((Infos.front().Kind != IITDescriptor::Argument ||
15108c975eacSThomas Preud'homme Infos.front().getArgumentKind() == IITDescriptor::AK_MatchType) &&
15118c975eacSThomas Preud'homme "Unsupported polymorphic pointer type with opaque pointer");
15128c7349b3SNikita Popov Infos = Infos.slice(1);
15138c7349b3SNikita Popov return false;
1514bc552275SArtur Pilipenko }
1515bc552275SArtur Pilipenko
1516bc552275SArtur Pilipenko case IITDescriptor::Struct: {
1517bc552275SArtur Pilipenko StructType *ST = dyn_cast<StructType>(Ty);
15188a72391fSNikita Popov if (!ST || !ST->isLiteral() || ST->isPacked() ||
15198a72391fSNikita Popov ST->getNumElements() != D.Struct_NumElements)
1520bc552275SArtur Pilipenko return true;
1521bc552275SArtur Pilipenko
1522bc552275SArtur Pilipenko for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
15237957fc65SSander de Smalen if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys,
15247957fc65SSander de Smalen DeferredChecks, IsDeferredCheck))
1525bc552275SArtur Pilipenko return true;
1526bc552275SArtur Pilipenko return false;
1527bc552275SArtur Pilipenko }
1528bc552275SArtur Pilipenko
1529bc552275SArtur Pilipenko case IITDescriptor::Argument:
15307957fc65SSander de Smalen // If this is the second occurrence of an argument,
15317957fc65SSander de Smalen // verify that the later instance matches the previous instance.
1532bc552275SArtur Pilipenko if (D.getArgumentNumber() < ArgTys.size())
1533bc552275SArtur Pilipenko return Ty != ArgTys[D.getArgumentNumber()];
1534bc552275SArtur Pilipenko
15357957fc65SSander de Smalen if (D.getArgumentNumber() > ArgTys.size() ||
15367957fc65SSander de Smalen D.getArgumentKind() == IITDescriptor::AK_MatchType)
15377957fc65SSander de Smalen return IsDeferredCheck || DeferCheck(Ty);
15387957fc65SSander de Smalen
15397957fc65SSander de Smalen assert(D.getArgumentNumber() == ArgTys.size() && !IsDeferredCheck &&
15407957fc65SSander de Smalen "Table consistency error");
1541bc552275SArtur Pilipenko ArgTys.push_back(Ty);
1542bc552275SArtur Pilipenko
1543bc552275SArtur Pilipenko switch (D.getArgumentKind()) {
1544bc552275SArtur Pilipenko case IITDescriptor::AK_Any: return false; // Success
1545bc552275SArtur Pilipenko case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
1546bc552275SArtur Pilipenko case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
1547bc552275SArtur Pilipenko case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
1548bc552275SArtur Pilipenko case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
15497957fc65SSander de Smalen default: break;
1550bc552275SArtur Pilipenko }
1551bc552275SArtur Pilipenko llvm_unreachable("all argument kinds not covered");
1552bc552275SArtur Pilipenko
1553bc552275SArtur Pilipenko case IITDescriptor::ExtendArgument: {
15547957fc65SSander de Smalen // If this is a forward reference, defer the check for later.
1555bc552275SArtur Pilipenko if (D.getArgumentNumber() >= ArgTys.size())
15567957fc65SSander de Smalen return IsDeferredCheck || DeferCheck(Ty);
1557bc552275SArtur Pilipenko
1558bc552275SArtur Pilipenko Type *NewTy = ArgTys[D.getArgumentNumber()];
1559bc552275SArtur Pilipenko if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1560bc552275SArtur Pilipenko NewTy = VectorType::getExtendedElementVectorType(VTy);
1561bc552275SArtur Pilipenko else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1562bc552275SArtur Pilipenko NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
1563bc552275SArtur Pilipenko else
1564bc552275SArtur Pilipenko return true;
1565bc552275SArtur Pilipenko
1566bc552275SArtur Pilipenko return Ty != NewTy;
1567bc552275SArtur Pilipenko }
1568bc552275SArtur Pilipenko case IITDescriptor::TruncArgument: {
15697957fc65SSander de Smalen // If this is a forward reference, defer the check for later.
1570bc552275SArtur Pilipenko if (D.getArgumentNumber() >= ArgTys.size())
15717957fc65SSander de Smalen return IsDeferredCheck || DeferCheck(Ty);
1572bc552275SArtur Pilipenko
1573bc552275SArtur Pilipenko Type *NewTy = ArgTys[D.getArgumentNumber()];
1574bc552275SArtur Pilipenko if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1575bc552275SArtur Pilipenko NewTy = VectorType::getTruncatedElementVectorType(VTy);
1576bc552275SArtur Pilipenko else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1577bc552275SArtur Pilipenko NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
1578bc552275SArtur Pilipenko else
1579bc552275SArtur Pilipenko return true;
1580bc552275SArtur Pilipenko
1581bc552275SArtur Pilipenko return Ty != NewTy;
1582bc552275SArtur Pilipenko }
1583bc552275SArtur Pilipenko case IITDescriptor::HalfVecArgument:
15847957fc65SSander de Smalen // If this is a forward reference, defer the check for later.
158501b84e17SKerry McLaughlin if (D.getArgumentNumber() >= ArgTys.size())
158601b84e17SKerry McLaughlin return IsDeferredCheck || DeferCheck(Ty);
158701b84e17SKerry McLaughlin return !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1588bc552275SArtur Pilipenko VectorType::getHalfElementsVectorType(
1589bc552275SArtur Pilipenko cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1590bc552275SArtur Pilipenko case IITDescriptor::SameVecWidthArgument: {
15917957fc65SSander de Smalen if (D.getArgumentNumber() >= ArgTys.size()) {
15927957fc65SSander de Smalen // Defer check and subsequent check for the vector element type.
15937957fc65SSander de Smalen Infos = Infos.slice(1);
15947957fc65SSander de Smalen return IsDeferredCheck || DeferCheck(Ty);
15957957fc65SSander de Smalen }
1596f87226ebSSimon Pilgrim auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
1597f87226ebSSimon Pilgrim auto *ThisArgType = dyn_cast<VectorType>(Ty);
1598f87226ebSSimon Pilgrim // Both must be vectors of the same number of elements or neither.
1599f87226ebSSimon Pilgrim if ((ReferenceType != nullptr) != (ThisArgType != nullptr))
1600bc552275SArtur Pilipenko return true;
1601f87226ebSSimon Pilgrim Type *EltTy = Ty;
1602f87226ebSSimon Pilgrim if (ThisArgType) {
16032ba5d64aSCullen Rhodes if (ReferenceType->getElementCount() !=
16042ba5d64aSCullen Rhodes ThisArgType->getElementCount())
1605f87226ebSSimon Pilgrim return true;
160640ed21bbSChristopher Tetreault EltTy = ThisArgType->getElementType();
1607f87226ebSSimon Pilgrim }
16087957fc65SSander de Smalen return matchIntrinsicType(EltTy, Infos, ArgTys, DeferredChecks,
16097957fc65SSander de Smalen IsDeferredCheck);
1610bc552275SArtur Pilipenko }
1611bc552275SArtur Pilipenko case IITDescriptor::PtrToArgument: {
1612bc552275SArtur Pilipenko if (D.getArgumentNumber() >= ArgTys.size())
16137957fc65SSander de Smalen return IsDeferredCheck || DeferCheck(Ty);
1614bc552275SArtur Pilipenko Type * ReferenceType = ArgTys[D.getArgumentNumber()];
1615bc552275SArtur Pilipenko PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1616aa97bc11SNikita Popov return (!ThisArgType ||
16174d9f6ab3SNikita Popov !ThisArgType->isOpaqueOrPointeeTypeMatches(ReferenceType));
1618bc552275SArtur Pilipenko }
1619caaceef4SElena Demikhovsky case IITDescriptor::PtrToElt: {
1620caaceef4SElena Demikhovsky if (D.getArgumentNumber() >= ArgTys.size())
16217957fc65SSander de Smalen return IsDeferredCheck || DeferCheck(Ty);
1622caaceef4SElena Demikhovsky VectorType * ReferenceType =
1623caaceef4SElena Demikhovsky dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
1624caaceef4SElena Demikhovsky PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1625caaceef4SElena Demikhovsky
16268c7349b3SNikita Popov if (!ThisArgType || !ReferenceType)
16278c7349b3SNikita Popov return true;
162890ec6dffSNikita Popov return !ThisArgType->isOpaqueOrPointeeTypeMatches(
162990ec6dffSNikita Popov ReferenceType->getElementType());
1630caaceef4SElena Demikhovsky }
163149c2206bSCraig Topper case IITDescriptor::AnyPtrToElt: {
163249c2206bSCraig Topper unsigned RefArgNumber = D.getRefArgNumber();
163349c2206bSCraig Topper if (RefArgNumber >= ArgTys.size()) {
163449c2206bSCraig Topper if (IsDeferredCheck)
163549c2206bSCraig Topper return true;
163649c2206bSCraig Topper // If forward referencing, already add the pointer type and
163749c2206bSCraig Topper // defer the checks for later.
163849c2206bSCraig Topper ArgTys.push_back(Ty);
163949c2206bSCraig Topper return DeferCheck(Ty);
164049c2206bSCraig Topper }
164149c2206bSCraig Topper
164249c2206bSCraig Topper if (!IsDeferredCheck) {
164349c2206bSCraig Topper assert(D.getOverloadArgNumber() == ArgTys.size() &&
164449c2206bSCraig Topper "Table consistency error");
164549c2206bSCraig Topper ArgTys.push_back(Ty);
164649c2206bSCraig Topper }
164749c2206bSCraig Topper
164849c2206bSCraig Topper auto *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
164949c2206bSCraig Topper auto *ThisArgType = dyn_cast<PointerType>(Ty);
165049c2206bSCraig Topper if (!ThisArgType || !ReferenceType)
165149c2206bSCraig Topper return true;
165249c2206bSCraig Topper return !ThisArgType->isOpaqueOrPointeeTypeMatches(
165349c2206bSCraig Topper ReferenceType->getElementType());
165449c2206bSCraig Topper }
1655ef5798acSElad Cohen case IITDescriptor::VecOfAnyPtrsToElt: {
1656ef5798acSElad Cohen unsigned RefArgNumber = D.getRefArgNumber();
16577957fc65SSander de Smalen if (RefArgNumber >= ArgTys.size()) {
16587957fc65SSander de Smalen if (IsDeferredCheck)
1659bc552275SArtur Pilipenko return true;
16607957fc65SSander de Smalen // If forward referencing, already add the pointer-vector type and
16617957fc65SSander de Smalen // defer the checks for later.
16627957fc65SSander de Smalen ArgTys.push_back(Ty);
16637957fc65SSander de Smalen return DeferCheck(Ty);
16647957fc65SSander de Smalen }
1665ef5798acSElad Cohen
16667957fc65SSander de Smalen if (!IsDeferredCheck){
1667ef5798acSElad Cohen assert(D.getOverloadArgNumber() == ArgTys.size() &&
1668ef5798acSElad Cohen "Table consistency error");
1669ef5798acSElad Cohen ArgTys.push_back(Ty);
16707957fc65SSander de Smalen }
1671ef5798acSElad Cohen
1672ef5798acSElad Cohen // Verify the overloaded type "matches" the Ref type.
1673ef5798acSElad Cohen // i.e. Ty is a vector with the same width as Ref.
1674ef5798acSElad Cohen // Composed of pointers to the same element type as Ref.
16755a55e278SChristopher Tetreault auto *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
16765a55e278SChristopher Tetreault auto *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1677bc552275SArtur Pilipenko if (!ThisArgVecTy || !ReferenceType ||
167815e9a6c2SFrancesco Petrogalli (ReferenceType->getElementCount() != ThisArgVecTy->getElementCount()))
1679bc552275SArtur Pilipenko return true;
1680bc552275SArtur Pilipenko PointerType *ThisArgEltTy =
168140ed21bbSChristopher Tetreault dyn_cast<PointerType>(ThisArgVecTy->getElementType());
1682bc552275SArtur Pilipenko if (!ThisArgEltTy)
1683bc552275SArtur Pilipenko return true;
1684460dfbd9SNikita Popov return !ThisArgEltTy->isOpaqueOrPointeeTypeMatches(
1685460dfbd9SNikita Popov ReferenceType->getElementType());
1686bc552275SArtur Pilipenko }
168751c2fa0eSSander de Smalen case IITDescriptor::VecElementArgument: {
168851c2fa0eSSander de Smalen if (D.getArgumentNumber() >= ArgTys.size())
168951c2fa0eSSander de Smalen return IsDeferredCheck ? true : DeferCheck(Ty);
169051c2fa0eSSander de Smalen auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
169151c2fa0eSSander de Smalen return !ReferenceType || Ty != ReferenceType->getElementType();
169251c2fa0eSSander de Smalen }
169322a8f35cSKerry McLaughlin case IITDescriptor::Subdivide2Argument:
169422a8f35cSKerry McLaughlin case IITDescriptor::Subdivide4Argument: {
169522a8f35cSKerry McLaughlin // If this is a forward reference, defer the check for later.
169622a8f35cSKerry McLaughlin if (D.getArgumentNumber() >= ArgTys.size())
169722a8f35cSKerry McLaughlin return IsDeferredCheck || DeferCheck(Ty);
169822a8f35cSKerry McLaughlin
169922a8f35cSKerry McLaughlin Type *NewTy = ArgTys[D.getArgumentNumber()];
170022a8f35cSKerry McLaughlin if (auto *VTy = dyn_cast<VectorType>(NewTy)) {
170122a8f35cSKerry McLaughlin int SubDivs = D.Kind == IITDescriptor::Subdivide2Argument ? 1 : 2;
170222a8f35cSKerry McLaughlin NewTy = VectorType::getSubdividedVectorType(VTy, SubDivs);
170322a8f35cSKerry McLaughlin return Ty != NewTy;
170422a8f35cSKerry McLaughlin }
170522a8f35cSKerry McLaughlin return true;
170622a8f35cSKerry McLaughlin }
170776365b3bSKerry McLaughlin case IITDescriptor::VecOfBitcastsToInt: {
170876365b3bSKerry McLaughlin if (D.getArgumentNumber() >= ArgTys.size())
170976365b3bSKerry McLaughlin return IsDeferredCheck || DeferCheck(Ty);
171076365b3bSKerry McLaughlin auto *ReferenceType = dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
171176365b3bSKerry McLaughlin auto *ThisArgVecTy = dyn_cast<VectorType>(Ty);
171276365b3bSKerry McLaughlin if (!ThisArgVecTy || !ReferenceType)
171376365b3bSKerry McLaughlin return true;
171476365b3bSKerry McLaughlin return ThisArgVecTy != VectorType::getInteger(ReferenceType);
171576365b3bSKerry McLaughlin }
1716bc552275SArtur Pilipenko }
1717bc552275SArtur Pilipenko llvm_unreachable("unhandled");
1718bc552275SArtur Pilipenko }
1719bc552275SArtur Pilipenko
17207957fc65SSander de Smalen Intrinsic::MatchIntrinsicTypesResult
matchIntrinsicSignature(FunctionType * FTy,ArrayRef<Intrinsic::IITDescriptor> & Infos,SmallVectorImpl<Type * > & ArgTys)17217957fc65SSander de Smalen Intrinsic::matchIntrinsicSignature(FunctionType *FTy,
17227957fc65SSander de Smalen ArrayRef<Intrinsic::IITDescriptor> &Infos,
17237957fc65SSander de Smalen SmallVectorImpl<Type *> &ArgTys) {
17247957fc65SSander de Smalen SmallVector<DeferredIntrinsicMatchPair, 2> DeferredChecks;
17257957fc65SSander de Smalen if (matchIntrinsicType(FTy->getReturnType(), Infos, ArgTys, DeferredChecks,
17267957fc65SSander de Smalen false))
17277957fc65SSander de Smalen return MatchIntrinsicTypes_NoMatchRet;
17287957fc65SSander de Smalen
17297957fc65SSander de Smalen unsigned NumDeferredReturnChecks = DeferredChecks.size();
17307957fc65SSander de Smalen
17317957fc65SSander de Smalen for (auto Ty : FTy->params())
17327957fc65SSander de Smalen if (matchIntrinsicType(Ty, Infos, ArgTys, DeferredChecks, false))
17337957fc65SSander de Smalen return MatchIntrinsicTypes_NoMatchArg;
17347957fc65SSander de Smalen
17357957fc65SSander de Smalen for (unsigned I = 0, E = DeferredChecks.size(); I != E; ++I) {
17367957fc65SSander de Smalen DeferredIntrinsicMatchPair &Check = DeferredChecks[I];
17377957fc65SSander de Smalen if (matchIntrinsicType(Check.first, Check.second, ArgTys, DeferredChecks,
17387957fc65SSander de Smalen true))
17397957fc65SSander de Smalen return I < NumDeferredReturnChecks ? MatchIntrinsicTypes_NoMatchRet
17407957fc65SSander de Smalen : MatchIntrinsicTypes_NoMatchArg;
17417957fc65SSander de Smalen }
17427957fc65SSander de Smalen
17437957fc65SSander de Smalen return MatchIntrinsicTypes_Match;
17447957fc65SSander de Smalen }
17457957fc65SSander de Smalen
1746b68b8211SArtur Pilipenko bool
matchIntrinsicVarArg(bool isVarArg,ArrayRef<Intrinsic::IITDescriptor> & Infos)1747b68b8211SArtur Pilipenko Intrinsic::matchIntrinsicVarArg(bool isVarArg,
1748b68b8211SArtur Pilipenko ArrayRef<Intrinsic::IITDescriptor> &Infos) {
1749b68b8211SArtur Pilipenko // If there are no descriptors left, then it can't be a vararg.
1750b68b8211SArtur Pilipenko if (Infos.empty())
1751b68b8211SArtur Pilipenko return isVarArg;
1752b68b8211SArtur Pilipenko
1753b68b8211SArtur Pilipenko // There should be only one descriptor remaining at this point.
1754b68b8211SArtur Pilipenko if (Infos.size() != 1)
1755b68b8211SArtur Pilipenko return true;
1756b68b8211SArtur Pilipenko
1757b68b8211SArtur Pilipenko // Check and verify the descriptor.
1758b68b8211SArtur Pilipenko IITDescriptor D = Infos.front();
1759b68b8211SArtur Pilipenko Infos = Infos.slice(1);
1760b68b8211SArtur Pilipenko if (D.Kind == IITDescriptor::VarArg)
1761b68b8211SArtur Pilipenko return !isVarArg;
1762b68b8211SArtur Pilipenko
1763b68b8211SArtur Pilipenko return true;
1764b68b8211SArtur Pilipenko }
1765b68b8211SArtur Pilipenko
getIntrinsicSignature(Function * F,SmallVectorImpl<Type * > & ArgTys)1766874fcd4eSSebastian Neubauer bool Intrinsic::getIntrinsicSignature(Function *F,
1767874fcd4eSSebastian Neubauer SmallVectorImpl<Type *> &ArgTys) {
17686c7a8abfSArtur Pilipenko Intrinsic::ID ID = F->getIntrinsicID();
17696c7a8abfSArtur Pilipenko if (!ID)
1770874fcd4eSSebastian Neubauer return false;
17716c7a8abfSArtur Pilipenko
17726c7a8abfSArtur Pilipenko SmallVector<Intrinsic::IITDescriptor, 8> Table;
17736c7a8abfSArtur Pilipenko getIntrinsicInfoTableEntries(ID, Table);
17746c7a8abfSArtur Pilipenko ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
17756c7a8abfSArtur Pilipenko
1776874fcd4eSSebastian Neubauer if (Intrinsic::matchIntrinsicSignature(F->getFunctionType(), TableRef,
1777874fcd4eSSebastian Neubauer ArgTys) !=
1778874fcd4eSSebastian Neubauer Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
1779874fcd4eSSebastian Neubauer return false;
1780874fcd4eSSebastian Neubauer }
1781874fcd4eSSebastian Neubauer if (Intrinsic::matchIntrinsicVarArg(F->getFunctionType()->isVarArg(),
1782874fcd4eSSebastian Neubauer TableRef))
1783874fcd4eSSebastian Neubauer return false;
1784874fcd4eSSebastian Neubauer return true;
17856c7a8abfSArtur Pilipenko }
17866c7a8abfSArtur Pilipenko
remangleIntrinsicFunction(Function * F)1787874fcd4eSSebastian Neubauer Optional<Function *> Intrinsic::remangleIntrinsicFunction(Function *F) {
1788874fcd4eSSebastian Neubauer SmallVector<Type *, 4> ArgTys;
1789874fcd4eSSebastian Neubauer if (!getIntrinsicSignature(F, ArgTys))
1790874fcd4eSSebastian Neubauer return None;
1791874fcd4eSSebastian Neubauer
1792874fcd4eSSebastian Neubauer Intrinsic::ID ID = F->getIntrinsicID();
17936c7a8abfSArtur Pilipenko StringRef Name = F->getName();
179490a6bb30SJeroen Dobbelaere std::string WantedName =
179590a6bb30SJeroen Dobbelaere Intrinsic::getName(ID, ArgTys, F->getParent(), F->getFunctionType());
179690a6bb30SJeroen Dobbelaere if (Name == WantedName)
17976c7a8abfSArtur Pilipenko return None;
17986c7a8abfSArtur Pilipenko
179990a6bb30SJeroen Dobbelaere Function *NewDecl = [&] {
180090a6bb30SJeroen Dobbelaere if (auto *ExistingGV = F->getParent()->getNamedValue(WantedName)) {
180190a6bb30SJeroen Dobbelaere if (auto *ExistingF = dyn_cast<Function>(ExistingGV))
180290a6bb30SJeroen Dobbelaere if (ExistingF->getFunctionType() == F->getFunctionType())
180390a6bb30SJeroen Dobbelaere return ExistingF;
180490a6bb30SJeroen Dobbelaere
180590a6bb30SJeroen Dobbelaere // The name already exists, but is not a function or has the wrong
180690a6bb30SJeroen Dobbelaere // prototype. Make place for the new one by renaming the old version.
180790a6bb30SJeroen Dobbelaere // Either this old version will be removed later on or the module is
180890a6bb30SJeroen Dobbelaere // invalid and we'll get an error.
180990a6bb30SJeroen Dobbelaere ExistingGV->setName(WantedName + ".renamed");
181090a6bb30SJeroen Dobbelaere }
181190a6bb30SJeroen Dobbelaere return Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
181290a6bb30SJeroen Dobbelaere }();
181390a6bb30SJeroen Dobbelaere
18146c7a8abfSArtur Pilipenko NewDecl->setCallingConv(F->getCallingConv());
18156d01a941SReid Kleckner assert(NewDecl->getFunctionType() == F->getFunctionType() &&
18166d01a941SReid Kleckner "Shouldn't change the signature");
18176c7a8abfSArtur Pilipenko return NewDecl;
18186c7a8abfSArtur Pilipenko }
18196c7a8abfSArtur Pilipenko
1820ef860a24SChandler Carruth /// hasAddressTaken - returns true if there are any uses of this function
1821aef60af3SGiorgis Georgakoudis /// other than direct calls or invokes to it. Optionally ignores callback
1822d9c99043SStanislav Mekhanoshin /// uses, assume like pointer annotation calls, and references in llvm.used
1823d9c99043SStanislav Mekhanoshin /// and llvm.compiler.used variables.
hasAddressTaken(const User ** PutOffender,bool IgnoreCallbackUses,bool IgnoreAssumeLikeCalls,bool IgnoreLLVMUsed,bool IgnoreARCAttachedCall) const1824aef60af3SGiorgis Georgakoudis bool Function::hasAddressTaken(const User **PutOffender,
182529e2d946SStanislav Mekhanoshin bool IgnoreCallbackUses,
1826dea6f71aSAkira Hatanaka bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed,
1827dea6f71aSAkira Hatanaka bool IgnoreARCAttachedCall) const {
1828cdf47884SChandler Carruth for (const Use &U : uses()) {
1829cdf47884SChandler Carruth const User *FU = U.getUser();
1830cdf47884SChandler Carruth if (isa<BlockAddress>(FU))
1831ef860a24SChandler Carruth continue;
1832aef60af3SGiorgis Georgakoudis
1833aef60af3SGiorgis Georgakoudis if (IgnoreCallbackUses) {
1834aef60af3SGiorgis Georgakoudis AbstractCallSite ACS(&U);
1835aef60af3SGiorgis Georgakoudis if (ACS && ACS.isCallbackCall())
1836aef60af3SGiorgis Georgakoudis continue;
1837aef60af3SGiorgis Georgakoudis }
1838aef60af3SGiorgis Georgakoudis
183990c09232SChandler Carruth const auto *Call = dyn_cast<CallBase>(FU);
184090c09232SChandler Carruth if (!Call) {
184129e2d946SStanislav Mekhanoshin if (IgnoreAssumeLikeCalls) {
184229e2d946SStanislav Mekhanoshin if (const auto *FI = dyn_cast<Instruction>(FU)) {
184329e2d946SStanislav Mekhanoshin if (FI->isCast() && !FI->user_empty() &&
184429e2d946SStanislav Mekhanoshin llvm::all_of(FU->users(), [](const User *U) {
184529e2d946SStanislav Mekhanoshin if (const auto *I = dyn_cast<IntrinsicInst>(U))
184629e2d946SStanislav Mekhanoshin return I->isAssumeLikeIntrinsic();
184729e2d946SStanislav Mekhanoshin return false;
184829e2d946SStanislav Mekhanoshin }))
184929e2d946SStanislav Mekhanoshin continue;
185029e2d946SStanislav Mekhanoshin }
185129e2d946SStanislav Mekhanoshin }
1852d9c99043SStanislav Mekhanoshin if (IgnoreLLVMUsed && !FU->user_empty()) {
1853d9c99043SStanislav Mekhanoshin const User *FUU = FU;
1854d9c99043SStanislav Mekhanoshin if (isa<BitCastOperator>(FU) && FU->hasOneUse() &&
1855d9c99043SStanislav Mekhanoshin !FU->user_begin()->user_empty())
1856d9c99043SStanislav Mekhanoshin FUU = *FU->user_begin();
1857d9c99043SStanislav Mekhanoshin if (llvm::all_of(FUU->users(), [](const User *U) {
1858d9c99043SStanislav Mekhanoshin if (const auto *GV = dyn_cast<GlobalVariable>(U))
1859d9c99043SStanislav Mekhanoshin return GV->hasName() &&
1860d9c99043SStanislav Mekhanoshin (GV->getName().equals("llvm.compiler.used") ||
1861d9c99043SStanislav Mekhanoshin GV->getName().equals("llvm.used"));
1862d9c99043SStanislav Mekhanoshin return false;
1863d9c99043SStanislav Mekhanoshin }))
1864d9c99043SStanislav Mekhanoshin continue;
1865d9c99043SStanislav Mekhanoshin }
18667a083814SRichard Trieu if (PutOffender)
18677a083814SRichard Trieu *PutOffender = FU;
18687a083814SRichard Trieu return true;
18697a083814SRichard Trieu }
1870e3d87fd6SNikita Popov if (!Call->isCallee(&U) || Call->getFunctionType() != getFunctionType()) {
1871dea6f71aSAkira Hatanaka if (IgnoreARCAttachedCall &&
1872dea6f71aSAkira Hatanaka Call->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall,
1873dea6f71aSAkira Hatanaka U.getOperandNo()))
1874dea6f71aSAkira Hatanaka continue;
1875dea6f71aSAkira Hatanaka
18767a083814SRichard Trieu if (PutOffender)
18777a083814SRichard Trieu *PutOffender = FU;
18787a083814SRichard Trieu return true;
18797a083814SRichard Trieu }
1880ef860a24SChandler Carruth }
1881ef860a24SChandler Carruth return false;
1882ef860a24SChandler Carruth }
1883ef860a24SChandler Carruth
isDefTriviallyDead() const1884ef860a24SChandler Carruth bool Function::isDefTriviallyDead() const {
1885ef860a24SChandler Carruth // Check the linkage
1886ef860a24SChandler Carruth if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
1887ef860a24SChandler Carruth !hasAvailableExternallyLinkage())
1888ef860a24SChandler Carruth return false;
1889ef860a24SChandler Carruth
1890ef860a24SChandler Carruth // Check if the function is used by anything other than a blockaddress.
1891cdf47884SChandler Carruth for (const User *U : users())
1892cdf47884SChandler Carruth if (!isa<BlockAddress>(U))
1893ef860a24SChandler Carruth return false;
1894ef860a24SChandler Carruth
1895ef860a24SChandler Carruth return true;
1896ef860a24SChandler Carruth }
1897ef860a24SChandler Carruth
1898ef860a24SChandler Carruth /// callsFunctionThatReturnsTwice - Return true if the function has a call to
1899ef860a24SChandler Carruth /// setjmp or other function that gcc recognizes as "returning twice".
callsFunctionThatReturnsTwice() const1900ef860a24SChandler Carruth bool Function::callsFunctionThatReturnsTwice() const {
190190c09232SChandler Carruth for (const Instruction &I : instructions(this))
190290c09232SChandler Carruth if (const auto *Call = dyn_cast<CallBase>(&I))
190390c09232SChandler Carruth if (Call->hasFnAttr(Attribute::ReturnsTwice))
1904ef860a24SChandler Carruth return true;
1905ef860a24SChandler Carruth
1906ef860a24SChandler Carruth return false;
1907ef860a24SChandler Carruth }
19083fa50f9bSPeter Collingbourne
getPersonalityFn() const19093a63fb31SVedant Kumar Constant *Function::getPersonalityFn() const {
19103a63fb31SVedant Kumar assert(hasPersonalityFn() && getNumOperands());
19113a63fb31SVedant Kumar return cast<Constant>(Op<0>());
19121ab5ea56SVedant Kumar }
19131ab5ea56SVedant Kumar
setPersonalityFn(Constant * Fn)19143a63fb31SVedant Kumar void Function::setPersonalityFn(Constant *Fn) {
19153a63fb31SVedant Kumar setHungoffOperand<0>(Fn);
19163a63fb31SVedant Kumar setValueSubclassDataBit(3, Fn != nullptr);
19171ab5ea56SVedant Kumar }
19181ab5ea56SVedant Kumar
getPrefixData() const19193fa50f9bSPeter Collingbourne Constant *Function::getPrefixData() const {
19203a63fb31SVedant Kumar assert(hasPrefixData() && getNumOperands());
19213a63fb31SVedant Kumar return cast<Constant>(Op<1>());
19223fa50f9bSPeter Collingbourne }
19233fa50f9bSPeter Collingbourne
setPrefixData(Constant * PrefixData)19243fa50f9bSPeter Collingbourne void Function::setPrefixData(Constant *PrefixData) {
19253a63fb31SVedant Kumar setHungoffOperand<1>(PrefixData);
19263a63fb31SVedant Kumar setValueSubclassDataBit(1, PrefixData != nullptr);
19273fa50f9bSPeter Collingbourne }
192851d2de7bSPeter Collingbourne
getPrologueData() const192951d2de7bSPeter Collingbourne Constant *Function::getPrologueData() const {
19303a63fb31SVedant Kumar assert(hasPrologueData() && getNumOperands());
19313a63fb31SVedant Kumar return cast<Constant>(Op<2>());
193251d2de7bSPeter Collingbourne }
193351d2de7bSPeter Collingbourne
setPrologueData(Constant * PrologueData)193451d2de7bSPeter Collingbourne void Function::setPrologueData(Constant *PrologueData) {
19353a63fb31SVedant Kumar setHungoffOperand<2>(PrologueData);
19363a63fb31SVedant Kumar setValueSubclassDataBit(2, PrologueData != nullptr);
19373a63fb31SVedant Kumar }
19383a63fb31SVedant Kumar
allocHungoffUselist()19393a63fb31SVedant Kumar void Function::allocHungoffUselist() {
19403a63fb31SVedant Kumar // If we've already allocated a uselist, stop here.
19413a63fb31SVedant Kumar if (getNumOperands())
194251d2de7bSPeter Collingbourne return;
194351d2de7bSPeter Collingbourne
19443a63fb31SVedant Kumar allocHungoffUses(3, /*IsPhi=*/ false);
19453a63fb31SVedant Kumar setNumHungOffUseOperands(3);
19463a63fb31SVedant Kumar
19473a63fb31SVedant Kumar // Initialize the uselist with placeholder operands to allow traversal.
19483a63fb31SVedant Kumar auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
19493a63fb31SVedant Kumar Op<0>().set(CPN);
19503a63fb31SVedant Kumar Op<1>().set(CPN);
19513a63fb31SVedant Kumar Op<2>().set(CPN);
19523a63fb31SVedant Kumar }
19533a63fb31SVedant Kumar
19543a63fb31SVedant Kumar template <int Idx>
setHungoffOperand(Constant * C)19553a63fb31SVedant Kumar void Function::setHungoffOperand(Constant *C) {
19569bc46b11SKeno Fischer if (C) {
19573a63fb31SVedant Kumar allocHungoffUselist();
19583a63fb31SVedant Kumar Op<Idx>().set(C);
19599bc46b11SKeno Fischer } else if (getNumOperands()) {
19609bc46b11SKeno Fischer Op<Idx>().set(
19619bc46b11SKeno Fischer ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
19629bc46b11SKeno Fischer }
19633a63fb31SVedant Kumar }
19643a63fb31SVedant Kumar
setValueSubclassDataBit(unsigned Bit,bool On)19653a63fb31SVedant Kumar void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
19663a63fb31SVedant Kumar assert(Bit < 16 && "SubclassData contains only 16 bits");
19673a63fb31SVedant Kumar if (On)
19683a63fb31SVedant Kumar setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
19693a63fb31SVedant Kumar else
19703a63fb31SVedant Kumar setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
197151d2de7bSPeter Collingbourne }
19723058d0f0SAkira Hatanaka
setEntryCount(ProfileCount Count,const DenseSet<GlobalValue::GUID> * S)1973e5b8de2fSEaswaran Raman void Function::setEntryCount(ProfileCount Count,
1974a60cdd38SDehao Chen const DenseSet<GlobalValue::GUID> *S) {
1975e5b8de2fSEaswaran Raman #if !defined(NDEBUG)
1976e5b8de2fSEaswaran Raman auto PrevCount = getEntryCount();
19775413bf1bSKazu Hirata assert(!PrevCount || PrevCount->getType() == Count.getType());
1978e5b8de2fSEaswaran Raman #endif
1979ba1dfae0SWenlei He
1980ba1dfae0SWenlei He auto ImportGUIDs = getImportGUIDs();
1981ba1dfae0SWenlei He if (S == nullptr && ImportGUIDs.size())
1982ba1dfae0SWenlei He S = &ImportGUIDs;
1983ba1dfae0SWenlei He
19842567f3d0SDiego Novillo MDBuilder MDB(getContext());
1985e5b8de2fSEaswaran Raman setMetadata(
1986e5b8de2fSEaswaran Raman LLVMContext::MD_prof,
1987e5b8de2fSEaswaran Raman MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S));
19882567f3d0SDiego Novillo }
19892567f3d0SDiego Novillo
setEntryCount(uint64_t Count,Function::ProfileCountType Type,const DenseSet<GlobalValue::GUID> * Imports)1990e5b8de2fSEaswaran Raman void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type,
1991e5b8de2fSEaswaran Raman const DenseSet<GlobalValue::GUID> *Imports) {
1992e5b8de2fSEaswaran Raman setEntryCount(ProfileCount(Count, Type), Imports);
1993e5b8de2fSEaswaran Raman }
1994e5b8de2fSEaswaran Raman
getEntryCount(bool AllowSynthetic) const1995a32c2c38SMircea Trofin Optional<ProfileCount> Function::getEntryCount(bool AllowSynthetic) const {
19962567f3d0SDiego Novillo MDNode *MD = getMetadata(LLVMContext::MD_prof);
19972567f3d0SDiego Novillo if (MD && MD->getOperand(0))
1998e5b8de2fSEaswaran Raman if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
19992567f3d0SDiego Novillo if (MDS->getString().equals("function_entry_count")) {
20002567f3d0SDiego Novillo ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
2001c1c810deSDavid Callahan uint64_t Count = CI->getValue().getZExtValue();
2002915897e2STeresa Johnson // A value of -1 is used for SamplePGO when there were no samples.
2003915897e2STeresa Johnson // Treat this the same as unknown.
2004915897e2STeresa Johnson if (Count == (uint64_t)-1)
2005a32c2c38SMircea Trofin return None;
2006e5b8de2fSEaswaran Raman return ProfileCount(Count, PCT_Real);
2007499c80b8SXinliang David Li } else if (AllowSynthetic &&
2008499c80b8SXinliang David Li MDS->getString().equals("synthetic_function_entry_count")) {
2009e5b8de2fSEaswaran Raman ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
2010e5b8de2fSEaswaran Raman uint64_t Count = CI->getValue().getZExtValue();
2011e5b8de2fSEaswaran Raman return ProfileCount(Count, PCT_Synthetic);
20122567f3d0SDiego Novillo }
2013e5b8de2fSEaswaran Raman }
2014a32c2c38SMircea Trofin return None;
20152567f3d0SDiego Novillo }
2016302b69c9SDehao Chen
getImportGUIDs() const2017a60cdd38SDehao Chen DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
2018a60cdd38SDehao Chen DenseSet<GlobalValue::GUID> R;
2019a60cdd38SDehao Chen if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
2020a60cdd38SDehao Chen if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
2021a60cdd38SDehao Chen if (MDS->getString().equals("function_entry_count"))
2022a60cdd38SDehao Chen for (unsigned i = 2; i < MD->getNumOperands(); i++)
2023a60cdd38SDehao Chen R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
2024a60cdd38SDehao Chen ->getValue()
2025a60cdd38SDehao Chen .getZExtValue());
2026a60cdd38SDehao Chen return R;
2027a60cdd38SDehao Chen }
2028a60cdd38SDehao Chen
setSectionPrefix(StringRef Prefix)2029302b69c9SDehao Chen void Function::setSectionPrefix(StringRef Prefix) {
2030302b69c9SDehao Chen MDBuilder MDB(getContext());
2031302b69c9SDehao Chen setMetadata(LLVMContext::MD_section_prefix,
2032302b69c9SDehao Chen MDB.createFunctionSectionPrefix(Prefix));
2033302b69c9SDehao Chen }
2034302b69c9SDehao Chen
getSectionPrefix() const2035302b69c9SDehao Chen Optional<StringRef> Function::getSectionPrefix() const {
2036302b69c9SDehao Chen if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
2037781aa181SCraig Topper assert(cast<MDString>(MD->getOperand(0))
2038302b69c9SDehao Chen ->getString()
2039302b69c9SDehao Chen .equals("function_section_prefix") &&
2040302b69c9SDehao Chen "Metadata not match");
2041781aa181SCraig Topper return cast<MDString>(MD->getOperand(1))->getString();
2042302b69c9SDehao Chen }
2043302b69c9SDehao Chen return None;
2044302b69c9SDehao Chen }
204577eeac3dSManoj Gupta
nullPointerIsDefined() const204677eeac3dSManoj Gupta bool Function::nullPointerIsDefined() const {
2047f89f7da9SNikita Popov return hasFnAttribute(Attribute::NullPointerIsValid);
204877eeac3dSManoj Gupta }
204977eeac3dSManoj Gupta
NullPointerIsDefined(const Function * F,unsigned AS)205077eeac3dSManoj Gupta bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
205177eeac3dSManoj Gupta if (F && F->nullPointerIsDefined())
205277eeac3dSManoj Gupta return true;
205377eeac3dSManoj Gupta
205477eeac3dSManoj Gupta if (AS != 0)
205577eeac3dSManoj Gupta return true;
205677eeac3dSManoj Gupta
205777eeac3dSManoj Gupta return false;
205877eeac3dSManoj Gupta }
2059