blob: a88478b89bfcffbcb81da6ae948cf7a779a723fa [file] [log] [blame]
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +00001//===- Function.cpp - Implement the Global object classes -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chandler Carruthef860a22013-01-02 09:10:48 +000010// This file implements the Function class for the IR library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/IR/Function.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000015#include "SymbolTableListTraitsImpl.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000016#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/None.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000022#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000023#include "llvm/ADT/StringRef.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000024#include "llvm/IR/Argument.h"
25#include "llvm/IR/Attributes.h"
26#include "llvm/IR/BasicBlock.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000027#include "llvm/IR/Constant.h"
Diego Novillo2567f3d2015-05-13 15:13:45 +000028#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000030#include "llvm/IR/GlobalValue.h"
Chandler Carruth83948572014-03-04 10:30:26 +000031#include "llvm/IR/InstIterator.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000032#include "llvm/IR/Instruction.h"
33#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000035#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/LLVMContext.h"
Diego Novillo2567f3d2015-05-13 15:13:45 +000037#include "llvm/IR/MDBuilder.h"
38#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/Module.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000040#include "llvm/IR/SymbolTableListTraits.h"
41#include "llvm/IR/Type.h"
42#include "llvm/IR/Use.h"
43#include "llvm/IR/User.h"
44#include "llvm/IR/Value.h"
45#include "llvm/IR/ValueSymbolTable.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/Compiler.h"
48#include "llvm/Support/ErrorHandling.h"
49#include <algorithm>
50#include <cassert>
51#include <cstddef>
52#include <cstdint>
53#include <cstring>
54#include <string>
55
Chris Lattner189d19f2003-11-21 20:23:48 +000056using namespace llvm;
Easwaran Ramane5b8de22018-01-17 22:24:23 +000057using ProfileCount = Function::ProfileCount;
Brian Gaeke960707c2003-11-11 22:41:34 +000058
Chris Lattner113f4f42002-06-25 16:13:24 +000059// Explicit instantiations of SymbolTableListTraits since some of the methods
60// are not in the public header file...
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000061template class llvm::SymbolTableListTraits<BasicBlock>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattnerda975502001-09-10 07:58:01 +000063//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000064// Argument Implementation
65//===----------------------------------------------------------------------===//
66
Reid Kleckner56d028d2017-03-17 17:16:39 +000067Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
68 : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
Chris Lattner32ab6432007-02-12 05:18:08 +000069 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000070}
71
Chris Lattner9ed7aef2002-09-06 21:33:15 +000072void Argument::setParent(Function *parent) {
73 Parent = parent;
74}
75
Nick Lewyckyd52b1522014-05-20 01:23:40 +000076bool Argument::hasNonNullAttr() const {
77 if (!getType()->isPointerTy()) return false;
Reid Klecknerf021fab2017-04-13 23:12:13 +000078 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull))
Hal Finkelb0407ba2014-07-18 15:51:28 +000079 return true;
80 else if (getDereferenceableBytes() > 0 &&
Manoj Gupta77eeac32018-07-09 22:27:23 +000081 !NullPointerIsDefined(getParent(),
82 getType()->getPointerAddressSpace()))
Hal Finkelb0407ba2014-07-18 15:51:28 +000083 return true;
84 return false;
Nick Lewyckyd52b1522014-05-20 01:23:40 +000085}
86
Chris Lattnere30f09d2008-01-24 17:47:11 +000087bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000088 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +000089 return hasAttribute(Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000090}
91
Manman Renf46262e2016-03-29 17:37:21 +000092bool Argument::hasSwiftSelfAttr() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +000093 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
Manman Renf46262e2016-03-29 17:37:21 +000094}
95
Manman Ren9bfd0d02016-04-01 21:41:15 +000096bool Argument::hasSwiftErrorAttr() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +000097 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
Manman Ren9bfd0d02016-04-01 21:41:15 +000098}
99
Reid Klecknera534a382013-12-19 02:14:12 +0000100bool Argument::hasInAllocaAttr() const {
101 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000102 return hasAttribute(Attribute::InAlloca);
Reid Klecknera534a382013-12-19 02:14:12 +0000103}
104
Reid Kleckner436c42e2014-01-17 23:58:17 +0000105bool Argument::hasByValOrInAllocaAttr() const {
106 if (!getType()->isPointerTy()) return false;
Reid Klecknerb5180542017-03-21 16:57:19 +0000107 AttributeList Attrs = getParent()->getAttributes();
Reid Klecknerf021fab2017-04-13 23:12:13 +0000108 return Attrs.hasParamAttribute(getArgNo(), Attribute::ByVal) ||
109 Attrs.hasParamAttribute(getArgNo(), Attribute::InAlloca);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000110}
111
Chris Lattner4d37d992011-05-22 23:57:23 +0000112unsigned Argument::getParamAlignment() const {
113 assert(getType()->isPointerTy() && "Only pointers have alignments");
Reid Kleckner859f8b52017-04-28 20:34:27 +0000114 return getParent()->getParamAlignment(getArgNo());
Chris Lattner4d37d992011-05-22 23:57:23 +0000115}
116
Hal Finkelb0407ba2014-07-18 15:51:28 +0000117uint64_t Argument::getDereferenceableBytes() const {
118 assert(getType()->isPointerTy() &&
119 "Only pointers have dereferenceable bytes");
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000120 return getParent()->getParamDereferenceableBytes(getArgNo());
Hal Finkelb0407ba2014-07-18 15:51:28 +0000121}
122
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000123uint64_t Argument::getDereferenceableOrNullBytes() const {
124 assert(getType()->isPointerTy() &&
125 "Only pointers have dereferenceable bytes");
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000126 return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000127}
128
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000129bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000130 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000131 return hasAttribute(Attribute::Nest);
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000132}
133
Chris Lattnere30f09d2008-01-24 17:47:11 +0000134bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000135 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000136 return hasAttribute(Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000137}
138
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000139bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000140 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000141 return hasAttribute(Attribute::NoCapture);
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000142}
143
Owen Andersonc64dfb42008-02-17 23:22:28 +0000144bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000145 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000146 return hasAttribute(Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000147}
148
Stephen Linb8bd2322013-04-20 05:14:40 +0000149bool Argument::hasReturnedAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000150 return hasAttribute(Attribute::Returned);
Stephen Linb8bd2322013-04-20 05:14:40 +0000151}
152
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000153bool Argument::hasZExtAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000154 return hasAttribute(Attribute::ZExt);
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000155}
156
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000157bool Argument::hasSExtAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000158 return hasAttribute(Attribute::SExt);
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000159}
160
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000161bool Argument::onlyReadsMemory() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +0000162 AttributeList Attrs = getParent()->getAttributes();
163 return Attrs.hasParamAttribute(getArgNo(), Attribute::ReadOnly) ||
164 Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000165}
166
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000167void Argument::addAttrs(AttrBuilder &B) {
168 AttributeList AL = getParent()->getAttributes();
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000169 AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000170 getParent()->setAttributes(AL);
171}
172
173void Argument::addAttr(Attribute::AttrKind Kind) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000174 getParent()->addParamAttr(getArgNo(), Kind);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000175}
176
177void Argument::addAttr(Attribute Attr) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000178 getParent()->addParamAttr(getArgNo(), Attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000179}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000180
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000181void Argument::removeAttr(Attribute::AttrKind Kind) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000182 getParent()->removeParamAttr(getArgNo(), Kind);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000183}
184
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000185bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
Reid Klecknerf021fab2017-04-13 23:12:13 +0000186 return getParent()->hasParamAttribute(getArgNo(), Kind);
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000187}
188
Chris Lattnerd255ae22002-04-09 19:39:35 +0000189//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000190// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000191//===----------------------------------------------------------------------===//
192
Owen Anderson47db9412009-07-22 00:24:57 +0000193LLVMContext &Function::getContext() const {
194 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000195}
196
Mircea Trofin87b67252018-10-18 19:49:44 +0000197unsigned Function::getInstructionCount() const {
Jessica Paquettee49374d2018-05-18 17:26:39 +0000198 unsigned NumInstrs = 0;
Mircea Trofin87b67252018-10-18 19:49:44 +0000199 for (const BasicBlock &BB : BasicBlocks)
Jessica Paquettee49374d2018-05-18 17:26:39 +0000200 NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(),
201 BB.instructionsWithoutDebug().end());
202 return NumInstrs;
203}
204
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +0000205Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage,
206 const Twine &N, Module &M) {
207 return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), N, &M);
208}
209
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000210void Function::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000211 getParent()->getFunctionList().remove(getIterator());
Duncan Sands185eeac2007-11-25 14:10:56 +0000212}
213
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000214void Function::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000215 getParent()->getFunctionList().erase(getIterator());
Reid Spencer019c8862007-04-09 15:01:12 +0000216}
217
Reid Spencer019c8862007-04-09 15:01:12 +0000218//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000219// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000220//===----------------------------------------------------------------------===//
221
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +0000222static unsigned computeAddrSpace(unsigned AddrSpace, Module *M) {
223 // If AS == -1 and we are passed a valid module pointer we place the function
224 // in the program address space. Otherwise we default to AS0.
225 if (AddrSpace == static_cast<unsigned>(-1))
226 return M ? M->getDataLayout().getProgramAddressSpace() : 0;
227 return AddrSpace;
228}
229
230Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace,
231 const Twine &name, Module *ParentModule)
David Blaikied583b192015-08-21 21:35:28 +0000232 : GlobalObject(Ty, Value::FunctionVal,
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +0000233 OperandTraits<Function>::op_begin(this), 0, Linkage, name,
234 computeAddrSpace(AddrSpace, ParentModule)),
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000235 NumArgs(Ty->getNumParams()) {
Chris Lattner654695b2009-01-05 07:58:59 +0000236 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000237 "invalid return type");
Duncan P. N. Exon Smith4472b772015-04-24 20:47:23 +0000238 setGlobalObjectSubClassData(0);
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000239
240 // We only need a symbol table for a function if the context keeps value names
241 if (!getContext().shouldDiscardValueNames())
242 SymTab = make_unique<ValueSymbolTable>();
Chris Lattner6213ae02002-09-06 20:46:32 +0000243
Chris Lattnere2de9082007-08-18 06:14:52 +0000244 // If the function has arguments, mark them as lazily built.
245 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000246 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000247
Chris Lattner6213ae02002-09-06 20:46:32 +0000248 if (ParentModule)
249 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000250
Justin Lebar291abd32016-12-28 22:59:45 +0000251 HasLLVMReservedName = getName().startswith("llvm.");
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000252 // Ensure intrinsics have the right parameter attributes.
Pete Coopera7c0c182015-05-19 00:24:26 +0000253 // Note, the IntID field will have been set in Value::setName if this function
254 // name is a valid intrinsic ID.
255 if (IntID)
256 setAttributes(Intrinsic::getAttributes(getContext(), IntID));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257}
258
Gordon Henriksen14a55692007-12-10 02:14:30 +0000259Function::~Function() {
260 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261
Chris Lattner2f7c9632001-06-06 20:29:01 +0000262 // Delete all of the method arguments and unlink from symbol table...
Reid Kleckner56d028d2017-03-17 17:16:39 +0000263 if (Arguments)
264 clearArguments();
Reid Spencerc6a83842007-04-22 17:28:03 +0000265
Gordon Henriksend930f912008-08-17 18:44:35 +0000266 // Remove the function from the on-the-side GC table.
267 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268}
269
Chris Lattnere2de9082007-08-18 06:14:52 +0000270void Function::BuildLazyArguments() const {
271 // Create the arguments vector, all arguments start out unnamed.
Reid Kleckner56d028d2017-03-17 17:16:39 +0000272 auto *FT = getFunctionType();
273 if (NumArgs > 0) {
274 Arguments = std::allocator<Argument>().allocate(NumArgs);
275 for (unsigned i = 0, e = NumArgs; i != e; ++i) {
276 Type *ArgTy = FT->getParamType(i);
277 assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
278 new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
279 }
Chris Lattnere2de9082007-08-18 06:14:52 +0000280 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000281
Chris Lattnere2de9082007-08-18 06:14:52 +0000282 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000283 unsigned SDC = getSubclassDataFromValue();
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000284 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0));
Reid Kleckner56d028d2017-03-17 17:16:39 +0000285 assert(!hasLazyArguments());
286}
287
288static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
289 return MutableArrayRef<Argument>(Args, Count);
290}
291
292void Function::clearArguments() {
293 for (Argument &A : makeArgArray(Arguments, NumArgs)) {
294 A.setName("");
295 A.~Argument();
296 }
297 std::allocator<Argument>().deallocate(Arguments, NumArgs);
298 Arguments = nullptr;
Chris Lattnere2de9082007-08-18 06:14:52 +0000299}
300
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000301void Function::stealArgumentListFrom(Function &Src) {
302 assert(isDeclaration() && "Expected no references to current arguments");
303
304 // Drop the current arguments, if any, and set the lazy argument bit.
305 if (!hasLazyArguments()) {
Reid Kleckner56d028d2017-03-17 17:16:39 +0000306 assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000307 [](const Argument &A) { return A.use_empty(); }) &&
308 "Expected arguments to be unused in declaration");
Reid Kleckner56d028d2017-03-17 17:16:39 +0000309 clearArguments();
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000310 setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
311 }
312
313 // Nothing to steal if Src has lazy arguments.
314 if (Src.hasLazyArguments())
315 return;
316
317 // Steal arguments from Src, and fix the lazy argument bits.
Reid Kleckner56d028d2017-03-17 17:16:39 +0000318 assert(arg_size() == Src.arg_size());
319 Arguments = Src.Arguments;
320 Src.Arguments = nullptr;
321 for (Argument &A : makeArgArray(Arguments, NumArgs)) {
322 // FIXME: This does the work of transferNodesFromList inefficiently.
323 SmallString<128> Name;
324 if (A.hasName())
325 Name = A.getName();
326 if (!Name.empty())
327 A.setName("");
328 A.setParent(this);
329 if (!Name.empty())
330 A.setName(Name);
331 }
332
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000333 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
Reid Kleckner56d028d2017-03-17 17:16:39 +0000334 assert(!hasLazyArguments());
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000335 Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
336}
337
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338// dropAllReferences() - This function causes all the subinstructions to "let
339// go" of all references that they are maintaining. This allows one to
340// 'delete' a whole class at a time, even though there may be circular
341// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000342// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344// delete.
345//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000346void Function::dropAllReferences() {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000347 setIsMaterializable(false);
348
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000349 for (BasicBlock &BB : *this)
350 BB.dropAllReferences();
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000351
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000352 // Delete all basic blocks. They are now unused, except possibly by
353 // blockaddresses, but BasicBlock's destructor takes care of those.
354 while (!BasicBlocks.empty())
355 BasicBlocks.begin()->eraseFromParent();
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000356
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000357 // Drop uses of any optional data (real or placeholder).
358 if (getNumOperands()) {
359 User::dropAllReferences();
360 setNumHungOffUseOperands(0);
361 setValueSubclassData(getSubclassDataFromValue() & ~0xe);
362 }
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +0000363
364 // Metadata is stored in a side-table.
365 clearMetadata();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000366}
Chris Lattnerda975502001-09-10 07:58:01 +0000367
Amaury Sechet392638d2016-06-14 20:27:35 +0000368void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000369 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000370 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000371 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000372}
373
Amaury Sechet5db224e2016-06-12 06:17:24 +0000374void Function::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000375 AttributeList PAL = getAttributes();
Amaury Sechet5db224e2016-06-12 06:17:24 +0000376 PAL = PAL.addAttribute(getContext(), i, Attr);
377 setAttributes(PAL);
378}
379
Reid Kleckneree4930b2017-05-02 22:07:37 +0000380void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000381 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000382 PAL = PAL.addAttributes(getContext(), i, Attrs);
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000383 setAttributes(PAL);
384}
385
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000386void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
387 AttributeList PAL = getAttributes();
388 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
389 setAttributes(PAL);
390}
391
392void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
393 AttributeList PAL = getAttributes();
394 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
395 setAttributes(PAL);
396}
397
398void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
399 AttributeList PAL = getAttributes();
400 PAL = PAL.addParamAttributes(getContext(), ArgNo, Attrs);
401 setAttributes(PAL);
402}
403
Amaury Sechet392638d2016-06-14 20:27:35 +0000404void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000405 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000406 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000407 setAttributes(PAL);
408}
409
Amaury Sechet6100adf2016-06-15 17:50:39 +0000410void Function::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000411 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000412 PAL = PAL.removeAttribute(getContext(), i, Kind);
413 setAttributes(PAL);
414}
415
Reid Kleckneree4930b2017-05-02 22:07:37 +0000416void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000417 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000418 PAL = PAL.removeAttributes(getContext(), i, Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000419 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000420}
421
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000422void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
423 AttributeList PAL = getAttributes();
424 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
425 setAttributes(PAL);
426}
427
428void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
429 AttributeList PAL = getAttributes();
430 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
431 setAttributes(PAL);
432}
433
434void Function::removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
435 AttributeList PAL = getAttributes();
436 PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs);
437 setAttributes(PAL);
438}
439
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000440void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000441 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000442 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
443 setAttributes(PAL);
444}
445
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000446void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
447 AttributeList PAL = getAttributes();
448 PAL = PAL.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
449 setAttributes(PAL);
450}
451
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000452void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000453 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000454 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
455 setAttributes(PAL);
456}
457
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000458void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
459 uint64_t Bytes) {
460 AttributeList PAL = getAttributes();
461 PAL = PAL.addDereferenceableOrNullParamAttr(getContext(), ArgNo, Bytes);
462 setAttributes(PAL);
463}
464
Mehdi Amini599ebf22016-01-08 02:28:20 +0000465const std::string &Function::getGC() const {
Gordon Henriksend930f912008-08-17 18:44:35 +0000466 assert(hasGC() && "Function has no collector");
Mehdi Amini599ebf22016-01-08 02:28:20 +0000467 return getContext().getGC(*this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000468}
469
Benjamin Kramer728f4442016-05-29 10:46:35 +0000470void Function::setGC(std::string Str) {
Mehdi Amini599ebf22016-01-08 02:28:20 +0000471 setValueSubclassDataBit(14, !Str.empty());
472 getContext().setGC(*this, std::move(Str));
Gordon Henriksen71183b62007-12-10 03:18:06 +0000473}
474
Gordon Henriksend930f912008-08-17 18:44:35 +0000475void Function::clearGC() {
Mehdi Amini599ebf22016-01-08 02:28:20 +0000476 if (!hasGC())
477 return;
478 getContext().deleteGC(*this);
479 setValueSubclassDataBit(14, false);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000480}
481
Rafael Espindola769efe62015-12-02 20:03:17 +0000482/// Copy all additional attributes (those not needed to create a Function) from
483/// the Function Src to this one.
Reid Klecknere7c78542017-05-11 21:14:29 +0000484void Function::copyAttributesFrom(const Function *Src) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000485 GlobalObject::copyAttributesFrom(Src);
Reid Klecknere7c78542017-05-11 21:14:29 +0000486 setCallingConv(Src->getCallingConv());
487 setAttributes(Src->getAttributes());
488 if (Src->hasGC())
489 setGC(Src->getGC());
Gordon Henriksend930f912008-08-17 18:44:35 +0000490 else
491 clearGC();
Reid Klecknere7c78542017-05-11 21:14:29 +0000492 if (Src->hasPersonalityFn())
493 setPersonalityFn(Src->getPersonalityFn());
494 if (Src->hasPrefixData())
495 setPrefixData(Src->getPrefixData());
496 if (Src->hasPrologueData())
497 setPrologueData(Src->getPrologueData());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000498}
499
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000500/// Table of string intrinsic names indexed by enum value.
501static const char * const IntrinsicNameTable[] = {
502 "not_intrinsic",
503#define GET_INTRINSIC_NAME_TABLE
Reid Klecknerf5890e42018-06-23 02:02:38 +0000504#include "llvm/IR/IntrinsicImpl.inc"
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000505#undef GET_INTRINSIC_NAME_TABLE
506};
507
Justin Bogner92a8c612016-07-15 16:31:37 +0000508/// Table of per-target intrinsic name tables.
509#define GET_INTRINSIC_TARGET_DATA
Reid Klecknerf5890e42018-06-23 02:02:38 +0000510#include "llvm/IR/IntrinsicImpl.inc"
Justin Bogner92a8c612016-07-15 16:31:37 +0000511#undef GET_INTRINSIC_TARGET_DATA
512
513/// Find the segment of \c IntrinsicNameTable for intrinsics with the same
514/// target as \c Name, or the generic table if \c Name is not target specific.
515///
516/// Returns the relevant slice of \c IntrinsicNameTable
517static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
518 assert(Name.startswith("llvm."));
519
520 ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
521 // Drop "llvm." and take the first dotted component. That will be the target
522 // if this is target specific.
523 StringRef Target = Name.drop_front(5).split('.').first;
524 auto It = std::lower_bound(Targets.begin(), Targets.end(), Target,
525 [](const IntrinsicTargetInfo &TI,
526 StringRef Target) { return TI.Name < Target; });
527 // We've either found the target or just fall back to the generic set, which
528 // is always first.
529 const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
530 return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
531}
532
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000533/// This does the actual lookup of an intrinsic ID which
Pete Cooper21241132015-05-19 00:02:25 +0000534/// matches the given function name.
Tim Northover6b3bd612016-07-29 20:32:59 +0000535Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
Justin Bogner92a8c612016-07-15 16:31:37 +0000536 ArrayRef<const char *> NameTable = findTargetSubtable(Name);
Reid Klecknerc2752da2016-01-26 22:33:19 +0000537 int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
Justin Bogner92a8c612016-07-15 16:31:37 +0000538 if (Idx == -1)
539 return Intrinsic::not_intrinsic;
540
541 // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
542 // an index into a sub-table.
543 int Adjust = NameTable.data() - IntrinsicNameTable;
544 Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
Pete Cooper21241132015-05-19 00:02:25 +0000545
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000546 // If the intrinsic is not overloaded, require an exact match. If it is
Roman Tereshin12a4dc42018-02-28 23:51:49 +0000547 // overloaded, require either exact or prefix match.
548 const auto MatchSize = strlen(NameTable[Idx]);
549 assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
550 bool IsExactMatch = Name.size() == MatchSize;
551 return IsExactMatch || isOverloaded(ID) ? ID : Intrinsic::not_intrinsic;
Pete Cooper21241132015-05-19 00:02:25 +0000552}
553
Pete Coopera7c0c182015-05-19 00:24:26 +0000554void Function::recalculateIntrinsicID() {
Justin Lebar291abd32016-12-28 22:59:45 +0000555 StringRef Name = getName();
556 if (!Name.startswith("llvm.")) {
557 HasLLVMReservedName = false;
Pete Coopera7c0c182015-05-19 00:24:26 +0000558 IntID = Intrinsic::not_intrinsic;
559 return;
Michael Ilseman516d7032013-03-01 18:48:54 +0000560 }
Justin Lebar291abd32016-12-28 22:59:45 +0000561 HasLLVMReservedName = true;
562 IntID = lookupIntrinsicID(Name);
Michael Ilseman516d7032013-03-01 18:48:54 +0000563}
564
Philip Reames319c48e2014-11-12 00:21:51 +0000565/// Returns a stable mangling for the type specified for use in the name
Philip Reames059ecbf2014-11-24 23:24:24 +0000566/// mangling scheme used by 'any' types in intrinsic signatures. The mangling
567/// of named types is simply their name. Manglings for unnamed types consist
568/// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
569/// combined with the mangling of their component types. A vararg function
570/// type will have a suffix of 'vararg'. Since function types can contain
571/// other function types, we close a function type mangling with suffix 'f'
572/// which can't be confused with it's prefix. This ensures we don't have
573/// collisions between two unrelated function types. Otherwise, you might
574/// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.)
Craig Topper2fa14362018-03-29 17:21:10 +0000575///
Philip Reames319c48e2014-11-12 00:21:51 +0000576static std::string getMangledTypeStr(Type* Ty) {
577 std::string Result;
578 if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000579 Result += "p" + utostr(PTyp->getAddressSpace()) +
Philip Reames319c48e2014-11-12 00:21:51 +0000580 getMangledTypeStr(PTyp->getElementType());
581 } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) {
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000582 Result += "a" + utostr(ATyp->getNumElements()) +
Philip Reames319c48e2014-11-12 00:21:51 +0000583 getMangledTypeStr(ATyp->getElementType());
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000584 } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
585 if (!STyp->isLiteral()) {
586 Result += "s_";
587 Result += STyp->getName();
588 } else {
589 Result += "sl_";
590 for (auto Elem : STyp->elements())
591 Result += getMangledTypeStr(Elem);
592 }
593 // Ensure nested structs are distinguishable.
594 Result += "s";
595 } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
Philip Reames319c48e2014-11-12 00:21:51 +0000596 Result += "f_" + getMangledTypeStr(FT->getReturnType());
597 for (size_t i = 0; i < FT->getNumParams(); i++)
598 Result += getMangledTypeStr(FT->getParamType(i));
599 if (FT->isVarArg())
600 Result += "vararg";
Philip Reames059ecbf2014-11-24 23:24:24 +0000601 // Ensure nested function types are distinguishable.
Fangrui Songf78650a2018-07-30 19:41:25 +0000602 Result += "f";
Craig Topper2fa14362018-03-29 17:21:10 +0000603 } else if (isa<VectorType>(Ty)) {
Elena Demikhovsky1ca72e12015-11-19 07:17:16 +0000604 Result += "v" + utostr(Ty->getVectorNumElements()) +
605 getMangledTypeStr(Ty->getVectorElementType());
Craig Topper2fa14362018-03-29 17:21:10 +0000606 } else if (Ty) {
607 switch (Ty->getTypeID()) {
608 default: llvm_unreachable("Unhandled type");
609 case Type::VoidTyID: Result += "isVoid"; break;
610 case Type::MetadataTyID: Result += "Metadata"; break;
611 case Type::HalfTyID: Result += "f16"; break;
612 case Type::FloatTyID: Result += "f32"; break;
613 case Type::DoubleTyID: Result += "f64"; break;
614 case Type::X86_FP80TyID: Result += "f80"; break;
615 case Type::FP128TyID: Result += "f128"; break;
616 case Type::PPC_FP128TyID: Result += "ppcf128"; break;
617 case Type::X86_MMXTyID: Result += "x86mmx"; break;
618 case Type::IntegerTyID:
619 Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
620 break;
621 }
622 }
Philip Reames319c48e2014-11-12 00:21:51 +0000623 return Result;
624}
625
Pete Coopera8db71e2016-08-18 18:30:54 +0000626StringRef Intrinsic::getName(ID id) {
627 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Pete Coopera5f8c722016-08-22 20:18:28 +0000628 assert(!isOverloaded(id) &&
629 "This version of getName does not support overloading");
Pete Coopera8db71e2016-08-18 18:30:54 +0000630 return IntrinsicNameTable[id];
631}
632
Benjamin Kramere6e19332011-07-14 17:45:39 +0000633std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000634 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000635 std::string Result(IntrinsicNameTable[id]);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000636 for (Type *Ty : Tys) {
637 Result += "." + getMangledTypeStr(Ty);
Mon P Wang2c839d42008-07-30 04:36:53 +0000638 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000639 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000640}
641
Chris Lattnerf39c2782012-05-27 18:28:35 +0000642/// IIT_Info - These are enumerators that describe the entries returned by the
643/// getIntrinsicInfoTableEntries function.
644///
645/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
646enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000647 // Common values should be encoded with 0-15.
Chris Lattnerf39c2782012-05-27 18:28:35 +0000648 IIT_Done = 0,
649 IIT_I1 = 1,
650 IIT_I8 = 2,
651 IIT_I16 = 3,
652 IIT_I32 = 4,
653 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000654 IIT_F16 = 6,
655 IIT_F32 = 7,
656 IIT_F64 = 8,
657 IIT_V2 = 9,
658 IIT_V4 = 10,
659 IIT_V8 = 11,
660 IIT_V16 = 12,
661 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000662 IIT_PTR = 14,
663 IIT_ARG = 15,
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000664
Robert Khasanov65c27562014-10-20 19:25:05 +0000665 // Values from 16+ are only encodable with the inefficient encoding.
666 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000667 IIT_MMX = 17,
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000668 IIT_TOKEN = 18,
669 IIT_METADATA = 19,
670 IIT_EMPTYSTRUCT = 20,
671 IIT_STRUCT2 = 21,
672 IIT_STRUCT3 = 22,
673 IIT_STRUCT4 = 23,
674 IIT_STRUCT5 = 24,
675 IIT_EXTEND_ARG = 25,
676 IIT_TRUNC_ARG = 26,
677 IIT_ANYPTR = 27,
678 IIT_V1 = 28,
679 IIT_VARARG = 29,
680 IIT_HALF_VEC_ARG = 30,
681 IIT_SAME_VEC_WIDTH_ARG = 31,
682 IIT_PTR_TO_ARG = 32,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000683 IIT_PTR_TO_ELT = 33,
Elad Cohenef5798a2017-05-03 12:28:54 +0000684 IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000685 IIT_I128 = 35,
686 IIT_V512 = 36,
Artem Belevich786ca6a2017-10-12 17:40:00 +0000687 IIT_V1024 = 37,
688 IIT_STRUCT6 = 38,
689 IIT_STRUCT7 = 39,
Stefan Pintilie83a5fe12018-07-09 18:50:06 +0000690 IIT_STRUCT8 = 40,
691 IIT_F128 = 41
Chris Lattnerf39c2782012-05-27 18:28:35 +0000692};
693
Chris Lattnerf39c2782012-05-27 18:28:35 +0000694static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
695 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000696 using namespace Intrinsic;
697
Chris Lattnera57c7972012-05-17 05:13:57 +0000698 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000699 unsigned StructElts = 2;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000700
Chris Lattnera57c7972012-05-17 05:13:57 +0000701 switch (Info) {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000702 case IIT_Done:
703 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
704 return;
Andrew Tricka2efd992013-10-31 17:18:11 +0000705 case IIT_VARARG:
706 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
707 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000708 case IIT_MMX:
709 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
710 return;
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000711 case IIT_TOKEN:
712 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
713 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000714 case IIT_METADATA:
715 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
716 return;
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000717 case IIT_F16:
718 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
719 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000720 case IIT_F32:
721 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
722 return;
723 case IIT_F64:
724 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
725 return;
Stefan Pintilie83a5fe12018-07-09 18:50:06 +0000726 case IIT_F128:
727 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0));
728 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000729 case IIT_I1:
730 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
731 return;
732 case IIT_I8:
733 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
734 return;
735 case IIT_I16:
736 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
737 return;
738 case IIT_I32:
739 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
740 return;
741 case IIT_I64:
742 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
743 return;
Kit Barton66460332015-05-25 15:49:26 +0000744 case IIT_I128:
745 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
746 return;
Jiangning Liu63dc8402013-09-24 02:47:27 +0000747 case IIT_V1:
748 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
749 DecodeIITType(NextElt, Infos, OutputTable);
750 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000751 case IIT_V2:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000752 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
753 DecodeIITType(NextElt, Infos, OutputTable);
754 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000755 case IIT_V4:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000756 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
757 DecodeIITType(NextElt, Infos, OutputTable);
758 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000759 case IIT_V8:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000760 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
761 DecodeIITType(NextElt, Infos, OutputTable);
762 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000763 case IIT_V16:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000764 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
765 DecodeIITType(NextElt, Infos, OutputTable);
766 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000767 case IIT_V32:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000768 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
769 DecodeIITType(NextElt, Infos, OutputTable);
770 return;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000771 case IIT_V64:
772 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
773 DecodeIITType(NextElt, Infos, OutputTable);
774 return;
Krzysztof Parzyszekb8bb90b2015-11-24 16:28:14 +0000775 case IIT_V512:
776 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512));
777 DecodeIITType(NextElt, Infos, OutputTable);
778 return;
779 case IIT_V1024:
780 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024));
781 DecodeIITType(NextElt, Infos, OutputTable);
782 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000783 case IIT_PTR:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000784 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
785 DecodeIITType(NextElt, Infos, OutputTable);
786 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000787 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000788 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000789 Infos[NextElt++]));
790 DecodeIITType(NextElt, Infos, OutputTable);
791 return;
Pete Cooper243efd72012-05-21 23:21:28 +0000792 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000793 case IIT_ARG: {
794 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
795 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
796 return;
797 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000798 case IIT_EXTEND_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000799 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000800 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000801 ArgInfo));
802 return;
803 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000804 case IIT_TRUNC_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000805 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000806 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000807 ArgInfo));
808 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000809 }
Tim Northover4516de32014-03-29 07:04:54 +0000810 case IIT_HALF_VEC_ARG: {
811 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
812 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
813 ArgInfo));
814 return;
815 }
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000816 case IIT_SAME_VEC_WIDTH_ARG: {
817 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
818 OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
819 ArgInfo));
820 return;
821 }
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000822 case IIT_PTR_TO_ARG: {
823 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
824 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
825 ArgInfo));
826 return;
827 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000828 case IIT_PTR_TO_ELT: {
829 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
830 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToElt, ArgInfo));
831 return;
832 }
Elad Cohenef5798a2017-05-03 12:28:54 +0000833 case IIT_VEC_OF_ANYPTRS_TO_ELT: {
834 unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
835 unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
836 OutputTable.push_back(
837 IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo));
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000838 return;
839 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000840 case IIT_EMPTYSTRUCT:
841 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
842 return;
Artem Belevich786ca6a2017-10-12 17:40:00 +0000843 case IIT_STRUCT8: ++StructElts; LLVM_FALLTHROUGH;
844 case IIT_STRUCT7: ++StructElts; LLVM_FALLTHROUGH;
845 case IIT_STRUCT6: ++StructElts; LLVM_FALLTHROUGH;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000846 case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH;
847 case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH;
848 case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000849 case IIT_STRUCT2: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000850 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
851
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000852 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattnerf39c2782012-05-27 18:28:35 +0000853 DecodeIITType(NextElt, Infos, OutputTable);
854 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000855 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000856 }
857 llvm_unreachable("unhandled");
858}
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000859
Chris Lattnerf39c2782012-05-27 18:28:35 +0000860#define GET_INTRINSIC_GENERATOR_GLOBAL
Reid Klecknerf5890e42018-06-23 02:02:38 +0000861#include "llvm/IR/IntrinsicImpl.inc"
Chris Lattnerf39c2782012-05-27 18:28:35 +0000862#undef GET_INTRINSIC_GENERATOR_GLOBAL
863
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000864void Intrinsic::getIntrinsicInfoTableEntries(ID id,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000865 SmallVectorImpl<IITDescriptor> &T){
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000866 // Check to see if the intrinsic's type was expressible by the table.
867 unsigned TableVal = IIT_Table[id-1];
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000868
Chris Lattnera3b0f522012-05-17 15:55:41 +0000869 // Decode the TableVal into an array of IITValues.
870 SmallVector<unsigned char, 8> IITValues;
871 ArrayRef<unsigned char> IITEntries;
872 unsigned NextElt = 0;
873 if ((TableVal >> 31) != 0) {
874 // This is an offset into the IIT_LongEncodingTable.
875 IITEntries = IIT_LongEncodingTable;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000876
Chris Lattnera3b0f522012-05-17 15:55:41 +0000877 // Strip sentinel bit.
878 NextElt = (TableVal << 1) >> 1;
879 } else {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000880 // Decode the TableVal into an array of IITValues. If the entry was encoded
881 // into a single word in the table itself, decode it now.
Chris Lattnera57c7972012-05-17 05:13:57 +0000882 do {
883 IITValues.push_back(TableVal & 0xF);
884 TableVal >>= 4;
885 } while (TableVal);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000886
Chris Lattnera3b0f522012-05-17 15:55:41 +0000887 IITEntries = IITValues;
888 NextElt = 0;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000889 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000890
891 // Okay, decode the table into the output vector of IITDescriptors.
892 DecodeIITType(NextElt, IITEntries, T);
893 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
894 DecodeIITType(NextElt, IITEntries, T);
895}
896
Chris Lattnerf39c2782012-05-27 18:28:35 +0000897static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
898 ArrayRef<Type*> Tys, LLVMContext &Context) {
899 using namespace Intrinsic;
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000900
Chris Lattnerf39c2782012-05-27 18:28:35 +0000901 IITDescriptor D = Infos.front();
902 Infos = Infos.slice(1);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000903
Chris Lattnerf39c2782012-05-27 18:28:35 +0000904 switch (D.Kind) {
905 case IITDescriptor::Void: return Type::getVoidTy(Context);
Andrew Tricka2efd992013-10-31 17:18:11 +0000906 case IITDescriptor::VarArg: return Type::getVoidTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000907 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000908 case IITDescriptor::Token: return Type::getTokenTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000909 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000910 case IITDescriptor::Half: return Type::getHalfTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000911 case IITDescriptor::Float: return Type::getFloatTy(Context);
912 case IITDescriptor::Double: return Type::getDoubleTy(Context);
Stefan Pintilie83a5fe12018-07-09 18:50:06 +0000913 case IITDescriptor::Quad: return Type::getFP128Ty(Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000914
Chris Lattnerf39c2782012-05-27 18:28:35 +0000915 case IITDescriptor::Integer:
916 return IntegerType::get(Context, D.Integer_Width);
917 case IITDescriptor::Vector:
918 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
919 case IITDescriptor::Pointer:
920 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
921 D.Pointer_AddressSpace);
922 case IITDescriptor::Struct: {
Artem Belevich786ca6a2017-10-12 17:40:00 +0000923 SmallVector<Type *, 8> Elts;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000924 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
Artem Belevich786ca6a2017-10-12 17:40:00 +0000925 Elts.push_back(DecodeFixedType(Infos, Tys, Context));
926 return StructType::get(Context, Elts);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000927 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000928 case IITDescriptor::Argument:
929 return Tys[D.getArgumentNumber()];
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000930 case IITDescriptor::ExtendArgument: {
931 Type *Ty = Tys[D.getArgumentNumber()];
932 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
933 return VectorType::getExtendedElementVectorType(VTy);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000934
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000935 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
936 }
937 case IITDescriptor::TruncArgument: {
938 Type *Ty = Tys[D.getArgumentNumber()];
939 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
940 return VectorType::getTruncatedElementVectorType(VTy);
941
942 IntegerType *ITy = cast<IntegerType>(Ty);
943 assert(ITy->getBitWidth() % 2 == 0);
944 return IntegerType::get(Context, ITy->getBitWidth() / 2);
945 }
Tim Northover4516de32014-03-29 07:04:54 +0000946 case IITDescriptor::HalfVecArgument:
947 return VectorType::getHalfElementsVectorType(cast<VectorType>(
948 Tys[D.getArgumentNumber()]));
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000949 case IITDescriptor::SameVecWidthArgument: {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000950 Type *EltTy = DecodeFixedType(Infos, Tys, Context);
951 Type *Ty = Tys[D.getArgumentNumber()];
952 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
953 return VectorType::get(EltTy, VTy->getNumElements());
954 }
955 llvm_unreachable("unhandled");
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000956 }
957 case IITDescriptor::PtrToArgument: {
958 Type *Ty = Tys[D.getArgumentNumber()];
959 return PointerType::getUnqual(Ty);
960 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000961 case IITDescriptor::PtrToElt: {
962 Type *Ty = Tys[D.getArgumentNumber()];
963 VectorType *VTy = dyn_cast<VectorType>(Ty);
964 if (!VTy)
965 llvm_unreachable("Expected an argument of Vector Type");
966 Type *EltTy = VTy->getVectorElementType();
967 return PointerType::getUnqual(EltTy);
968 }
Elad Cohenef5798a2017-05-03 12:28:54 +0000969 case IITDescriptor::VecOfAnyPtrsToElt:
970 // Return the overloaded type (which determines the pointers address space)
971 return Tys[D.getOverloadArgNumber()];
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000972 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000973 llvm_unreachable("unhandled");
974}
975
Chris Lattnerf39c2782012-05-27 18:28:35 +0000976FunctionType *Intrinsic::getType(LLVMContext &Context,
977 ID id, ArrayRef<Type*> Tys) {
978 SmallVector<IITDescriptor, 8> Table;
979 getIntrinsicInfoTableEntries(id, Table);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000980
Chris Lattnerf39c2782012-05-27 18:28:35 +0000981 ArrayRef<IITDescriptor> TableRef = Table;
982 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000983
Chris Lattnera3b0f522012-05-17 15:55:41 +0000984 SmallVector<Type*, 8> ArgTys;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000985 while (!TableRef.empty())
986 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
Jim Laskey2682ea62007-02-07 20:38:26 +0000987
Steven Wud05affc2014-10-20 15:47:24 +0000988 // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
989 // If we see void type as the type of the last argument, it is vararg intrinsic
990 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
991 ArgTys.pop_back();
992 return FunctionType::get(ResultTy, ArgTys, true);
993 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000994 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey2682ea62007-02-07 20:38:26 +0000995}
996
Mon P Wangb4024932009-02-24 23:17:49 +0000997bool Intrinsic::isOverloaded(ID id) {
Mon P Wangb4024932009-02-24 23:17:49 +0000998#define GET_INTRINSIC_OVERLOAD_TABLE
Reid Klecknerf5890e42018-06-23 02:02:38 +0000999#include "llvm/IR/IntrinsicImpl.inc"
Mon P Wangb4024932009-02-24 23:17:49 +00001000#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wangb4024932009-02-24 23:17:49 +00001001}
1002
Sanjoy Dasc65d43e2015-06-18 19:28:26 +00001003bool Intrinsic::isLeaf(ID id) {
1004 switch (id) {
1005 default:
1006 return true;
1007
1008 case Intrinsic::experimental_gc_statepoint:
1009 case Intrinsic::experimental_patchpoint_void:
1010 case Intrinsic::experimental_patchpoint_i64:
1011 return false;
1012 }
1013}
1014
Chris Lattner49b7ee12009-01-12 01:18:58 +00001015/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +00001016#define GET_INTRINSIC_ATTRIBUTES
Reid Klecknerf5890e42018-06-23 02:02:38 +00001017#include "llvm/IR/IntrinsicImpl.inc"
Duncan Sands38ef3a82007-12-03 20:06:50 +00001018#undef GET_INTRINSIC_ATTRIBUTES
1019
Benjamin Kramere6e19332011-07-14 17:45:39 +00001020Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +00001021 // There can never be multiple globals with the same name of different types,
1022 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +00001023 return
Benjamin Kramere6e19332011-07-14 17:45:39 +00001024 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
1025 getType(M->getContext(), id, Tys)));
Jim Laskey2682ea62007-02-07 20:38:26 +00001026}
1027
Dale Johannesenb842d522009-02-05 01:49:45 +00001028// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
1029#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
Reid Klecknerf5890e42018-06-23 02:02:38 +00001030#include "llvm/IR/IntrinsicImpl.inc"
Dale Johannesenb842d522009-02-05 01:49:45 +00001031#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
1032
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +00001033// This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
1034#define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
Reid Klecknerf5890e42018-06-23 02:02:38 +00001035#include "llvm/IR/IntrinsicImpl.inc"
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +00001036#undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
1037
Artur Pilipenkobc552272016-06-22 14:56:33 +00001038bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
1039 SmallVectorImpl<Type*> &ArgTys) {
1040 using namespace Intrinsic;
1041
1042 // If we ran out of descriptors, there are too many arguments.
1043 if (Infos.empty()) return true;
1044 IITDescriptor D = Infos.front();
1045 Infos = Infos.slice(1);
1046
1047 switch (D.Kind) {
1048 case IITDescriptor::Void: return !Ty->isVoidTy();
1049 case IITDescriptor::VarArg: return true;
1050 case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
1051 case IITDescriptor::Token: return !Ty->isTokenTy();
1052 case IITDescriptor::Metadata: return !Ty->isMetadataTy();
1053 case IITDescriptor::Half: return !Ty->isHalfTy();
1054 case IITDescriptor::Float: return !Ty->isFloatTy();
1055 case IITDescriptor::Double: return !Ty->isDoubleTy();
Stefan Pintilie83a5fe12018-07-09 18:50:06 +00001056 case IITDescriptor::Quad: return !Ty->isFP128Ty();
Artur Pilipenkobc552272016-06-22 14:56:33 +00001057 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1058 case IITDescriptor::Vector: {
1059 VectorType *VT = dyn_cast<VectorType>(Ty);
1060 return !VT || VT->getNumElements() != D.Vector_Width ||
1061 matchIntrinsicType(VT->getElementType(), Infos, ArgTys);
1062 }
1063 case IITDescriptor::Pointer: {
1064 PointerType *PT = dyn_cast<PointerType>(Ty);
1065 return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace ||
1066 matchIntrinsicType(PT->getElementType(), Infos, ArgTys);
1067 }
1068
1069 case IITDescriptor::Struct: {
1070 StructType *ST = dyn_cast<StructType>(Ty);
1071 if (!ST || ST->getNumElements() != D.Struct_NumElements)
1072 return true;
1073
1074 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1075 if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys))
1076 return true;
1077 return false;
1078 }
1079
1080 case IITDescriptor::Argument:
1081 // Two cases here - If this is the second occurrence of an argument, verify
1082 // that the later instance matches the previous instance.
1083 if (D.getArgumentNumber() < ArgTys.size())
1084 return Ty != ArgTys[D.getArgumentNumber()];
1085
1086 // Otherwise, if this is the first instance of an argument, record it and
1087 // verify the "Any" kind.
1088 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
1089 ArgTys.push_back(Ty);
1090
1091 switch (D.getArgumentKind()) {
1092 case IITDescriptor::AK_Any: return false; // Success
1093 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
1094 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
1095 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
1096 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
1097 }
1098 llvm_unreachable("all argument kinds not covered");
1099
1100 case IITDescriptor::ExtendArgument: {
1101 // This may only be used when referring to a previous vector argument.
1102 if (D.getArgumentNumber() >= ArgTys.size())
1103 return true;
1104
1105 Type *NewTy = ArgTys[D.getArgumentNumber()];
1106 if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1107 NewTy = VectorType::getExtendedElementVectorType(VTy);
1108 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1109 NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
1110 else
1111 return true;
1112
1113 return Ty != NewTy;
1114 }
1115 case IITDescriptor::TruncArgument: {
1116 // This may only be used when referring to a previous vector argument.
1117 if (D.getArgumentNumber() >= ArgTys.size())
1118 return true;
1119
1120 Type *NewTy = ArgTys[D.getArgumentNumber()];
1121 if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1122 NewTy = VectorType::getTruncatedElementVectorType(VTy);
1123 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1124 NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
1125 else
1126 return true;
1127
1128 return Ty != NewTy;
1129 }
1130 case IITDescriptor::HalfVecArgument:
1131 // This may only be used when referring to a previous vector argument.
1132 return D.getArgumentNumber() >= ArgTys.size() ||
1133 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1134 VectorType::getHalfElementsVectorType(
1135 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1136 case IITDescriptor::SameVecWidthArgument: {
1137 if (D.getArgumentNumber() >= ArgTys.size())
1138 return true;
1139 VectorType * ReferenceType =
Elena Demikhovskycaaceef2016-11-03 03:23:55 +00001140 dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
Artur Pilipenkobc552272016-06-22 14:56:33 +00001141 VectorType *ThisArgType = dyn_cast<VectorType>(Ty);
1142 if (!ThisArgType || !ReferenceType ||
1143 (ReferenceType->getVectorNumElements() !=
1144 ThisArgType->getVectorNumElements()))
1145 return true;
1146 return matchIntrinsicType(ThisArgType->getVectorElementType(),
1147 Infos, ArgTys);
1148 }
1149 case IITDescriptor::PtrToArgument: {
1150 if (D.getArgumentNumber() >= ArgTys.size())
1151 return true;
1152 Type * ReferenceType = ArgTys[D.getArgumentNumber()];
1153 PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1154 return (!ThisArgType || ThisArgType->getElementType() != ReferenceType);
1155 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +00001156 case IITDescriptor::PtrToElt: {
1157 if (D.getArgumentNumber() >= ArgTys.size())
1158 return true;
1159 VectorType * ReferenceType =
1160 dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
1161 PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1162
1163 return (!ThisArgType || !ReferenceType ||
1164 ThisArgType->getElementType() != ReferenceType->getElementType());
1165 }
Elad Cohenef5798a2017-05-03 12:28:54 +00001166 case IITDescriptor::VecOfAnyPtrsToElt: {
1167 unsigned RefArgNumber = D.getRefArgNumber();
1168
1169 // This may only be used when referring to a previous argument.
1170 if (RefArgNumber >= ArgTys.size())
Artur Pilipenkobc552272016-06-22 14:56:33 +00001171 return true;
Elad Cohenef5798a2017-05-03 12:28:54 +00001172
1173 // Record the overloaded type
1174 assert(D.getOverloadArgNumber() == ArgTys.size() &&
1175 "Table consistency error");
1176 ArgTys.push_back(Ty);
1177
1178 // Verify the overloaded type "matches" the Ref type.
1179 // i.e. Ty is a vector with the same width as Ref.
1180 // Composed of pointers to the same element type as Ref.
1181 VectorType *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
Artur Pilipenkobc552272016-06-22 14:56:33 +00001182 VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1183 if (!ThisArgVecTy || !ReferenceType ||
1184 (ReferenceType->getVectorNumElements() !=
1185 ThisArgVecTy->getVectorNumElements()))
1186 return true;
1187 PointerType *ThisArgEltTy =
1188 dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType());
1189 if (!ThisArgEltTy)
1190 return true;
1191 return ThisArgEltTy->getElementType() !=
1192 ReferenceType->getVectorElementType();
1193 }
1194 }
1195 llvm_unreachable("unhandled");
1196}
1197
Artur Pilipenkob68b8212016-06-24 14:47:27 +00001198bool
1199Intrinsic::matchIntrinsicVarArg(bool isVarArg,
1200 ArrayRef<Intrinsic::IITDescriptor> &Infos) {
1201 // If there are no descriptors left, then it can't be a vararg.
1202 if (Infos.empty())
1203 return isVarArg;
1204
1205 // There should be only one descriptor remaining at this point.
1206 if (Infos.size() != 1)
1207 return true;
1208
1209 // Check and verify the descriptor.
1210 IITDescriptor D = Infos.front();
1211 Infos = Infos.slice(1);
1212 if (D.Kind == IITDescriptor::VarArg)
1213 return !isVarArg;
1214
1215 return true;
1216}
1217
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +00001218Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) {
1219 Intrinsic::ID ID = F->getIntrinsicID();
1220 if (!ID)
1221 return None;
1222
1223 FunctionType *FTy = F->getFunctionType();
1224 // Accumulate an array of overloaded types for the given intrinsic
1225 SmallVector<Type *, 4> ArgTys;
1226 {
1227 SmallVector<Intrinsic::IITDescriptor, 8> Table;
1228 getIntrinsicInfoTableEntries(ID, Table);
1229 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
1230
1231 // If we encounter any problems matching the signature with the descriptor
1232 // just give up remangling. It's up to verifier to report the discrepancy.
1233 if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
1234 return None;
1235 for (auto Ty : FTy->params())
1236 if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
1237 return None;
1238 if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
1239 return None;
1240 }
1241
1242 StringRef Name = F->getName();
1243 if (Name == Intrinsic::getName(ID, ArgTys))
1244 return None;
1245
1246 auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1247 NewDecl->setCallingConv(F->getCallingConv());
1248 assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
1249 return NewDecl;
1250}
1251
Gabor Greif161cb042010-03-23 14:40:20 +00001252/// hasAddressTaken - returns true if there are any uses of this function
1253/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001254bool Function::hasAddressTaken(const User* *PutOffender) const {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001255 for (const Use &U : uses()) {
1256 const User *FU = U.getUser();
1257 if (isa<BlockAddress>(FU))
Jay Foadca0c4992012-05-12 08:30:16 +00001258 continue;
Chandler Carruth90c09232019-01-07 07:31:49 +00001259 const auto *Call = dyn_cast<CallBase>(FU);
1260 if (!Call) {
Richard Trieu7a083812016-02-18 22:09:30 +00001261 if (PutOffender)
1262 *PutOffender = FU;
1263 return true;
1264 }
Chandler Carruth90c09232019-01-07 07:31:49 +00001265 if (!Call->isCallee(&U)) {
Richard Trieu7a083812016-02-18 22:09:30 +00001266 if (PutOffender)
1267 *PutOffender = FU;
1268 return true;
1269 }
Jay Foad557169d2009-06-10 08:41:11 +00001270 }
1271 return false;
1272}
1273
Eli Friedman1923a332011-10-20 05:23:42 +00001274bool Function::isDefTriviallyDead() const {
1275 // Check the linkage
1276 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
1277 !hasAvailableExternallyLinkage())
1278 return false;
1279
1280 // Check if the function is used by anything other than a blockaddress.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001281 for (const User *U : users())
1282 if (!isa<BlockAddress>(U))
Eli Friedman1923a332011-10-20 05:23:42 +00001283 return false;
1284
1285 return true;
1286}
1287
Bill Wendling63a4ea12011-10-17 18:43:40 +00001288/// callsFunctionThatReturnsTwice - Return true if the function has a call to
1289/// setjmp or other function that gcc recognizes as "returning twice".
1290bool Function::callsFunctionThatReturnsTwice() const {
Chandler Carruth90c09232019-01-07 07:31:49 +00001291 for (const Instruction &I : instructions(this))
1292 if (const auto *Call = dyn_cast<CallBase>(&I))
1293 if (Call->hasFnAttr(Attribute::ReturnsTwice))
1294 return true;
Bill Wendling63a4ea12011-10-17 18:43:40 +00001295
1296 return false;
1297}
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001298
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001299Constant *Function::getPersonalityFn() const {
1300 assert(hasPersonalityFn() && getNumOperands());
1301 return cast<Constant>(Op<0>());
Vedant Kumar1ab5ea52015-10-06 20:31:57 +00001302}
1303
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001304void Function::setPersonalityFn(Constant *Fn) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001305 setHungoffOperand<0>(Fn);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001306 setValueSubclassDataBit(3, Fn != nullptr);
Vedant Kumar1ab5ea52015-10-06 20:31:57 +00001307}
1308
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001309Constant *Function::getPrefixData() const {
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001310 assert(hasPrefixData() && getNumOperands());
1311 return cast<Constant>(Op<1>());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001312}
1313
1314void Function::setPrefixData(Constant *PrefixData) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001315 setHungoffOperand<1>(PrefixData);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001316 setValueSubclassDataBit(1, PrefixData != nullptr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001317}
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001318
1319Constant *Function::getPrologueData() const {
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001320 assert(hasPrologueData() && getNumOperands());
1321 return cast<Constant>(Op<2>());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001322}
1323
1324void Function::setPrologueData(Constant *PrologueData) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001325 setHungoffOperand<2>(PrologueData);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001326 setValueSubclassDataBit(2, PrologueData != nullptr);
1327}
1328
1329void Function::allocHungoffUselist() {
1330 // If we've already allocated a uselist, stop here.
1331 if (getNumOperands())
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001332 return;
1333
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001334 allocHungoffUses(3, /*IsPhi=*/ false);
1335 setNumHungOffUseOperands(3);
1336
1337 // Initialize the uselist with placeholder operands to allow traversal.
1338 auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
1339 Op<0>().set(CPN);
1340 Op<1>().set(CPN);
1341 Op<2>().set(CPN);
1342}
1343
1344template <int Idx>
1345void Function::setHungoffOperand(Constant *C) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001346 if (C) {
1347 allocHungoffUselist();
1348 Op<Idx>().set(C);
1349 } else if (getNumOperands()) {
1350 Op<Idx>().set(
1351 ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
1352 }
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001353}
1354
1355void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
1356 assert(Bit < 16 && "SubclassData contains only 16 bits");
1357 if (On)
1358 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
1359 else
1360 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001361}
Akira Hatanaka3058d0f2015-05-06 23:54:14 +00001362
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001363void Function::setEntryCount(ProfileCount Count,
Dehao Chena60cdd32017-02-28 18:09:44 +00001364 const DenseSet<GlobalValue::GUID> *S) {
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001365 assert(Count.hasValue());
1366#if !defined(NDEBUG)
1367 auto PrevCount = getEntryCount();
1368 assert(!PrevCount.hasValue() || PrevCount.getType() == Count.getType());
1369#endif
Diego Novillo2567f3d2015-05-13 15:13:45 +00001370 MDBuilder MDB(getContext());
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001371 setMetadata(
1372 LLVMContext::MD_prof,
1373 MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S));
Diego Novillo2567f3d2015-05-13 15:13:45 +00001374}
1375
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001376void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type,
1377 const DenseSet<GlobalValue::GUID> *Imports) {
1378 setEntryCount(ProfileCount(Count, Type), Imports);
1379}
1380
1381ProfileCount Function::getEntryCount() const {
Diego Novillo2567f3d2015-05-13 15:13:45 +00001382 MDNode *MD = getMetadata(LLVMContext::MD_prof);
1383 if (MD && MD->getOperand(0))
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001384 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
Diego Novillo2567f3d2015-05-13 15:13:45 +00001385 if (MDS->getString().equals("function_entry_count")) {
1386 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
David Callahanc1c810d2016-08-05 18:38:19 +00001387 uint64_t Count = CI->getValue().getZExtValue();
Teresa Johnson915897e2017-12-18 20:02:43 +00001388 // A value of -1 is used for SamplePGO when there were no samples.
1389 // Treat this the same as unknown.
1390 if (Count == (uint64_t)-1)
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001391 return ProfileCount::getInvalid();
1392 return ProfileCount(Count, PCT_Real);
1393 } else if (MDS->getString().equals("synthetic_function_entry_count")) {
1394 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
1395 uint64_t Count = CI->getValue().getZExtValue();
1396 return ProfileCount(Count, PCT_Synthetic);
Diego Novillo2567f3d2015-05-13 15:13:45 +00001397 }
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001398 }
1399 return ProfileCount::getInvalid();
Diego Novillo2567f3d2015-05-13 15:13:45 +00001400}
Dehao Chen302b69c2016-10-18 20:42:47 +00001401
Dehao Chena60cdd32017-02-28 18:09:44 +00001402DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
1403 DenseSet<GlobalValue::GUID> R;
1404 if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
1405 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
1406 if (MDS->getString().equals("function_entry_count"))
1407 for (unsigned i = 2; i < MD->getNumOperands(); i++)
1408 R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
1409 ->getValue()
1410 .getZExtValue());
1411 return R;
1412}
1413
Dehao Chen302b69c2016-10-18 20:42:47 +00001414void Function::setSectionPrefix(StringRef Prefix) {
1415 MDBuilder MDB(getContext());
1416 setMetadata(LLVMContext::MD_section_prefix,
1417 MDB.createFunctionSectionPrefix(Prefix));
1418}
1419
1420Optional<StringRef> Function::getSectionPrefix() const {
1421 if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
Craig Topper781aa182018-05-05 01:57:00 +00001422 assert(cast<MDString>(MD->getOperand(0))
Dehao Chen302b69c2016-10-18 20:42:47 +00001423 ->getString()
1424 .equals("function_section_prefix") &&
1425 "Metadata not match");
Craig Topper781aa182018-05-05 01:57:00 +00001426 return cast<MDString>(MD->getOperand(1))->getString();
Dehao Chen302b69c2016-10-18 20:42:47 +00001427 }
1428 return None;
1429}
Manoj Gupta77eeac32018-07-09 22:27:23 +00001430
1431bool Function::nullPointerIsDefined() const {
1432 return getFnAttribute("null-pointer-is-valid")
1433 .getValueAsString()
1434 .equals("true");
1435}
1436
1437bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
1438 if (F && F->nullPointerIsDefined())
1439 return true;
1440
1441 if (AS != 0)
1442 return true;
1443
1444 return false;
1445}