blob: 72090f5bac3eece803f081f74c27e7ad7885b0cf [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"
Chandler Carruth219b89b2014-03-04 11:01:28 +000027#include "llvm/IR/CallSite.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000028#include "llvm/IR/Constant.h"
Diego Novillo2567f3d2015-05-13 15:13:45 +000029#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000031#include "llvm/IR/GlobalValue.h"
Chandler Carruth83948572014-03-04 10:30:26 +000032#include "llvm/IR/InstIterator.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000033#include "llvm/IR/Instruction.h"
34#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000036#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/LLVMContext.h"
Diego Novillo2567f3d2015-05-13 15:13:45 +000038#include "llvm/IR/MDBuilder.h"
39#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Module.h"
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +000041#include "llvm/IR/SymbolTableListTraits.h"
42#include "llvm/IR/Type.h"
43#include "llvm/IR/Use.h"
44#include "llvm/IR/User.h"
45#include "llvm/IR/Value.h"
46#include "llvm/IR/ValueSymbolTable.h"
47#include "llvm/Support/Casting.h"
48#include "llvm/Support/Compiler.h"
49#include "llvm/Support/ErrorHandling.h"
50#include <algorithm>
51#include <cassert>
52#include <cstddef>
53#include <cstdint>
54#include <cstring>
55#include <string>
56
Chris Lattner189d19f2003-11-21 20:23:48 +000057using namespace llvm;
Easwaran Ramane5b8de22018-01-17 22:24:23 +000058using ProfileCount = Function::ProfileCount;
Brian Gaeke960707c2003-11-11 22:41:34 +000059
Chris Lattner113f4f42002-06-25 16:13:24 +000060// Explicit instantiations of SymbolTableListTraits since some of the methods
61// are not in the public header file...
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000062template class llvm::SymbolTableListTraits<BasicBlock>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
Chris Lattnerda975502001-09-10 07:58:01 +000064//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000065// Argument Implementation
66//===----------------------------------------------------------------------===//
67
Reid Kleckner56d028d2017-03-17 17:16:39 +000068Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
69 : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
Chris Lattner32ab6432007-02-12 05:18:08 +000070 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000071}
72
Chris Lattner9ed7aef2002-09-06 21:33:15 +000073void Argument::setParent(Function *parent) {
74 Parent = parent;
75}
76
Nick Lewyckyd52b1522014-05-20 01:23:40 +000077bool Argument::hasNonNullAttr() const {
78 if (!getType()->isPointerTy()) return false;
Reid Klecknerf021fab2017-04-13 23:12:13 +000079 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull))
Hal Finkelb0407ba2014-07-18 15:51:28 +000080 return true;
81 else if (getDereferenceableBytes() > 0 &&
Manoj Gupta77eeac32018-07-09 22:27:23 +000082 !NullPointerIsDefined(getParent(),
83 getType()->getPointerAddressSpace()))
Hal Finkelb0407ba2014-07-18 15:51:28 +000084 return true;
85 return false;
Nick Lewyckyd52b1522014-05-20 01:23:40 +000086}
87
Chris Lattnere30f09d2008-01-24 17:47:11 +000088bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000089 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +000090 return hasAttribute(Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000091}
92
Manman Renf46262e2016-03-29 17:37:21 +000093bool Argument::hasSwiftSelfAttr() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +000094 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
Manman Renf46262e2016-03-29 17:37:21 +000095}
96
Manman Ren9bfd0d02016-04-01 21:41:15 +000097bool Argument::hasSwiftErrorAttr() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +000098 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
Manman Ren9bfd0d02016-04-01 21:41:15 +000099}
100
Reid Klecknera534a382013-12-19 02:14:12 +0000101bool Argument::hasInAllocaAttr() const {
102 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000103 return hasAttribute(Attribute::InAlloca);
Reid Klecknera534a382013-12-19 02:14:12 +0000104}
105
Reid Kleckner436c42e2014-01-17 23:58:17 +0000106bool Argument::hasByValOrInAllocaAttr() const {
107 if (!getType()->isPointerTy()) return false;
Reid Klecknerb5180542017-03-21 16:57:19 +0000108 AttributeList Attrs = getParent()->getAttributes();
Reid Klecknerf021fab2017-04-13 23:12:13 +0000109 return Attrs.hasParamAttribute(getArgNo(), Attribute::ByVal) ||
110 Attrs.hasParamAttribute(getArgNo(), Attribute::InAlloca);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000111}
112
Chris Lattner4d37d992011-05-22 23:57:23 +0000113unsigned Argument::getParamAlignment() const {
114 assert(getType()->isPointerTy() && "Only pointers have alignments");
Reid Kleckner859f8b52017-04-28 20:34:27 +0000115 return getParent()->getParamAlignment(getArgNo());
Chris Lattner4d37d992011-05-22 23:57:23 +0000116}
117
Hal Finkelb0407ba2014-07-18 15:51:28 +0000118uint64_t Argument::getDereferenceableBytes() const {
119 assert(getType()->isPointerTy() &&
120 "Only pointers have dereferenceable bytes");
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000121 return getParent()->getParamDereferenceableBytes(getArgNo());
Hal Finkelb0407ba2014-07-18 15:51:28 +0000122}
123
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000124uint64_t Argument::getDereferenceableOrNullBytes() const {
125 assert(getType()->isPointerTy() &&
126 "Only pointers have dereferenceable bytes");
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000127 return getParent()->getParamDereferenceableOrNullBytes(getArgNo());
Sanjoy Das06cf33f2015-05-06 17:41:54 +0000128}
129
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000130bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000131 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000132 return hasAttribute(Attribute::Nest);
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000133}
134
Chris Lattnere30f09d2008-01-24 17:47:11 +0000135bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000136 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000137 return hasAttribute(Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000138}
139
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000140bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000141 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000142 return hasAttribute(Attribute::NoCapture);
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000143}
144
Owen Andersonc64dfb42008-02-17 23:22:28 +0000145bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000146 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000147 return hasAttribute(Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000148}
149
Stephen Linb8bd2322013-04-20 05:14:40 +0000150bool Argument::hasReturnedAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000151 return hasAttribute(Attribute::Returned);
Stephen Linb8bd2322013-04-20 05:14:40 +0000152}
153
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000154bool Argument::hasZExtAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000155 return hasAttribute(Attribute::ZExt);
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000156}
157
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000158bool Argument::hasSExtAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000159 return hasAttribute(Attribute::SExt);
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000160}
161
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000162bool Argument::onlyReadsMemory() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +0000163 AttributeList Attrs = getParent()->getAttributes();
164 return Attrs.hasParamAttribute(getArgNo(), Attribute::ReadOnly) ||
165 Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000166}
167
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000168void Argument::addAttrs(AttrBuilder &B) {
169 AttributeList AL = getParent()->getAttributes();
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000170 AL = AL.addParamAttributes(Parent->getContext(), getArgNo(), B);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000171 getParent()->setAttributes(AL);
172}
173
174void Argument::addAttr(Attribute::AttrKind Kind) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000175 getParent()->addParamAttr(getArgNo(), Kind);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000176}
177
178void Argument::addAttr(Attribute Attr) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000179 getParent()->addParamAttr(getArgNo(), Attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000180}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000181
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000182void Argument::removeAttr(Attribute::AttrKind Kind) {
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000183 getParent()->removeParamAttr(getArgNo(), Kind);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000184}
185
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000186bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
Reid Klecknerf021fab2017-04-13 23:12:13 +0000187 return getParent()->hasParamAttribute(getArgNo(), Kind);
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000188}
189
Chris Lattnerd255ae22002-04-09 19:39:35 +0000190//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000191// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000192//===----------------------------------------------------------------------===//
193
Owen Anderson47db9412009-07-22 00:24:57 +0000194LLVMContext &Function::getContext() const {
195 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000196}
197
Jessica Paquettee49374d2018-05-18 17:26:39 +0000198unsigned Function::getInstructionCount() {
199 unsigned NumInstrs = 0;
200 for (BasicBlock &BB : BasicBlocks)
201 NumInstrs += std::distance(BB.instructionsWithoutDebug().begin(),
202 BB.instructionsWithoutDebug().end());
203 return NumInstrs;
204}
205
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000206void Function::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000207 getParent()->getFunctionList().remove(getIterator());
Duncan Sands185eeac2007-11-25 14:10:56 +0000208}
209
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000210void Function::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000211 getParent()->getFunctionList().erase(getIterator());
Reid Spencer019c8862007-04-09 15:01:12 +0000212}
213
Reid Spencer019c8862007-04-09 15:01:12 +0000214//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000215// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000216//===----------------------------------------------------------------------===//
217
Rafael Espindola1b47a282014-10-23 15:20:05 +0000218Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name,
219 Module *ParentModule)
David Blaikied583b192015-08-21 21:35:28 +0000220 : GlobalObject(Ty, Value::FunctionVal,
Reid Kleckner56d028d2017-03-17 17:16:39 +0000221 OperandTraits<Function>::op_begin(this), 0, Linkage, name),
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000222 NumArgs(Ty->getNumParams()) {
Chris Lattner654695b2009-01-05 07:58:59 +0000223 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000224 "invalid return type");
Duncan P. N. Exon Smith4472b772015-04-24 20:47:23 +0000225 setGlobalObjectSubClassData(0);
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000226
227 // We only need a symbol table for a function if the context keeps value names
228 if (!getContext().shouldDiscardValueNames())
229 SymTab = make_unique<ValueSymbolTable>();
Chris Lattner6213ae02002-09-06 20:46:32 +0000230
Chris Lattnere2de9082007-08-18 06:14:52 +0000231 // If the function has arguments, mark them as lazily built.
232 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000233 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000234
Chris Lattner6213ae02002-09-06 20:46:32 +0000235 if (ParentModule)
236 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000237
Justin Lebar291abd32016-12-28 22:59:45 +0000238 HasLLVMReservedName = getName().startswith("llvm.");
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000239 // Ensure intrinsics have the right parameter attributes.
Pete Coopera7c0c182015-05-19 00:24:26 +0000240 // Note, the IntID field will have been set in Value::setName if this function
241 // name is a valid intrinsic ID.
242 if (IntID)
243 setAttributes(Intrinsic::getAttributes(getContext(), IntID));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244}
245
Gordon Henriksen14a55692007-12-10 02:14:30 +0000246Function::~Function() {
247 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 // Delete all of the method arguments and unlink from symbol table...
Reid Kleckner56d028d2017-03-17 17:16:39 +0000250 if (Arguments)
251 clearArguments();
Reid Spencerc6a83842007-04-22 17:28:03 +0000252
Gordon Henriksend930f912008-08-17 18:44:35 +0000253 // Remove the function from the on-the-side GC table.
254 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255}
256
Chris Lattnere2de9082007-08-18 06:14:52 +0000257void Function::BuildLazyArguments() const {
258 // Create the arguments vector, all arguments start out unnamed.
Reid Kleckner56d028d2017-03-17 17:16:39 +0000259 auto *FT = getFunctionType();
260 if (NumArgs > 0) {
261 Arguments = std::allocator<Argument>().allocate(NumArgs);
262 for (unsigned i = 0, e = NumArgs; i != e; ++i) {
263 Type *ArgTy = FT->getParamType(i);
264 assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
265 new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
266 }
Chris Lattnere2de9082007-08-18 06:14:52 +0000267 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000268
Chris Lattnere2de9082007-08-18 06:14:52 +0000269 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000270 unsigned SDC = getSubclassDataFromValue();
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000271 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0));
Reid Kleckner56d028d2017-03-17 17:16:39 +0000272 assert(!hasLazyArguments());
273}
274
275static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
276 return MutableArrayRef<Argument>(Args, Count);
277}
278
279void Function::clearArguments() {
280 for (Argument &A : makeArgArray(Arguments, NumArgs)) {
281 A.setName("");
282 A.~Argument();
283 }
284 std::allocator<Argument>().deallocate(Arguments, NumArgs);
285 Arguments = nullptr;
Chris Lattnere2de9082007-08-18 06:14:52 +0000286}
287
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000288void Function::stealArgumentListFrom(Function &Src) {
289 assert(isDeclaration() && "Expected no references to current arguments");
290
291 // Drop the current arguments, if any, and set the lazy argument bit.
292 if (!hasLazyArguments()) {
Reid Kleckner56d028d2017-03-17 17:16:39 +0000293 assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000294 [](const Argument &A) { return A.use_empty(); }) &&
295 "Expected arguments to be unused in declaration");
Reid Kleckner56d028d2017-03-17 17:16:39 +0000296 clearArguments();
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000297 setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
298 }
299
300 // Nothing to steal if Src has lazy arguments.
301 if (Src.hasLazyArguments())
302 return;
303
304 // Steal arguments from Src, and fix the lazy argument bits.
Reid Kleckner56d028d2017-03-17 17:16:39 +0000305 assert(arg_size() == Src.arg_size());
306 Arguments = Src.Arguments;
307 Src.Arguments = nullptr;
308 for (Argument &A : makeArgArray(Arguments, NumArgs)) {
309 // FIXME: This does the work of transferNodesFromList inefficiently.
310 SmallString<128> Name;
311 if (A.hasName())
312 Name = A.getName();
313 if (!Name.empty())
314 A.setName("");
315 A.setParent(this);
316 if (!Name.empty())
317 A.setName(Name);
318 }
319
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000320 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
Reid Kleckner56d028d2017-03-17 17:16:39 +0000321 assert(!hasLazyArguments());
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000322 Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
323}
324
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325// dropAllReferences() - This function causes all the subinstructions to "let
326// go" of all references that they are maintaining. This allows one to
327// 'delete' a whole class at a time, even though there may be circular
328// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000329// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000330// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000331// delete.
332//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000333void Function::dropAllReferences() {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000334 setIsMaterializable(false);
335
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000336 for (BasicBlock &BB : *this)
337 BB.dropAllReferences();
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000338
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000339 // Delete all basic blocks. They are now unused, except possibly by
340 // blockaddresses, but BasicBlock's destructor takes care of those.
341 while (!BasicBlocks.empty())
342 BasicBlocks.begin()->eraseFromParent();
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000343
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000344 // Drop uses of any optional data (real or placeholder).
345 if (getNumOperands()) {
346 User::dropAllReferences();
347 setNumHungOffUseOperands(0);
348 setValueSubclassData(getSubclassDataFromValue() & ~0xe);
349 }
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +0000350
351 // Metadata is stored in a side-table.
352 clearMetadata();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000353}
Chris Lattnerda975502001-09-10 07:58:01 +0000354
Amaury Sechet392638d2016-06-14 20:27:35 +0000355void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000356 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000357 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000358 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000359}
360
Amaury Sechet5db224e2016-06-12 06:17:24 +0000361void Function::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000362 AttributeList PAL = getAttributes();
Amaury Sechet5db224e2016-06-12 06:17:24 +0000363 PAL = PAL.addAttribute(getContext(), i, Attr);
364 setAttributes(PAL);
365}
366
Reid Kleckneree4930b2017-05-02 22:07:37 +0000367void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000368 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000369 PAL = PAL.addAttributes(getContext(), i, Attrs);
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000370 setAttributes(PAL);
371}
372
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000373void Function::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
374 AttributeList PAL = getAttributes();
375 PAL = PAL.addParamAttribute(getContext(), ArgNo, Kind);
376 setAttributes(PAL);
377}
378
379void Function::addParamAttr(unsigned ArgNo, Attribute Attr) {
380 AttributeList PAL = getAttributes();
381 PAL = PAL.addParamAttribute(getContext(), ArgNo, Attr);
382 setAttributes(PAL);
383}
384
385void Function::addParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
386 AttributeList PAL = getAttributes();
387 PAL = PAL.addParamAttributes(getContext(), ArgNo, Attrs);
388 setAttributes(PAL);
389}
390
Amaury Sechet392638d2016-06-14 20:27:35 +0000391void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000392 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000393 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000394 setAttributes(PAL);
395}
396
Amaury Sechet6100adf2016-06-15 17:50:39 +0000397void Function::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000398 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000399 PAL = PAL.removeAttribute(getContext(), i, Kind);
400 setAttributes(PAL);
401}
402
Reid Kleckneree4930b2017-05-02 22:07:37 +0000403void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000404 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000405 PAL = PAL.removeAttributes(getContext(), i, Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000406 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000407}
408
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000409void Function::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
410 AttributeList PAL = getAttributes();
411 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
412 setAttributes(PAL);
413}
414
415void Function::removeParamAttr(unsigned ArgNo, StringRef Kind) {
416 AttributeList PAL = getAttributes();
417 PAL = PAL.removeParamAttribute(getContext(), ArgNo, Kind);
418 setAttributes(PAL);
419}
420
421void Function::removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs) {
422 AttributeList PAL = getAttributes();
423 PAL = PAL.removeParamAttributes(getContext(), ArgNo, Attrs);
424 setAttributes(PAL);
425}
426
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000427void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000428 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000429 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
430 setAttributes(PAL);
431}
432
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000433void Function::addDereferenceableParamAttr(unsigned ArgNo, uint64_t Bytes) {
434 AttributeList PAL = getAttributes();
435 PAL = PAL.addDereferenceableParamAttr(getContext(), ArgNo, Bytes);
436 setAttributes(PAL);
437}
438
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000439void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000440 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000441 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
442 setAttributes(PAL);
443}
444
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000445void Function::addDereferenceableOrNullParamAttr(unsigned ArgNo,
446 uint64_t Bytes) {
447 AttributeList PAL = getAttributes();
448 PAL = PAL.addDereferenceableOrNullParamAttr(getContext(), ArgNo, Bytes);
449 setAttributes(PAL);
450}
451
Mehdi Amini599ebf22016-01-08 02:28:20 +0000452const std::string &Function::getGC() const {
Gordon Henriksend930f912008-08-17 18:44:35 +0000453 assert(hasGC() && "Function has no collector");
Mehdi Amini599ebf22016-01-08 02:28:20 +0000454 return getContext().getGC(*this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000455}
456
Benjamin Kramer728f4442016-05-29 10:46:35 +0000457void Function::setGC(std::string Str) {
Mehdi Amini599ebf22016-01-08 02:28:20 +0000458 setValueSubclassDataBit(14, !Str.empty());
459 getContext().setGC(*this, std::move(Str));
Gordon Henriksen71183b62007-12-10 03:18:06 +0000460}
461
Gordon Henriksend930f912008-08-17 18:44:35 +0000462void Function::clearGC() {
Mehdi Amini599ebf22016-01-08 02:28:20 +0000463 if (!hasGC())
464 return;
465 getContext().deleteGC(*this);
466 setValueSubclassDataBit(14, false);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000467}
468
Rafael Espindola769efe62015-12-02 20:03:17 +0000469/// Copy all additional attributes (those not needed to create a Function) from
470/// the Function Src to this one.
Reid Klecknere7c78542017-05-11 21:14:29 +0000471void Function::copyAttributesFrom(const Function *Src) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000472 GlobalObject::copyAttributesFrom(Src);
Reid Klecknere7c78542017-05-11 21:14:29 +0000473 setCallingConv(Src->getCallingConv());
474 setAttributes(Src->getAttributes());
475 if (Src->hasGC())
476 setGC(Src->getGC());
Gordon Henriksend930f912008-08-17 18:44:35 +0000477 else
478 clearGC();
Reid Klecknere7c78542017-05-11 21:14:29 +0000479 if (Src->hasPersonalityFn())
480 setPersonalityFn(Src->getPersonalityFn());
481 if (Src->hasPrefixData())
482 setPrefixData(Src->getPrefixData());
483 if (Src->hasPrologueData())
484 setPrologueData(Src->getPrologueData());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000485}
486
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000487/// Table of string intrinsic names indexed by enum value.
488static const char * const IntrinsicNameTable[] = {
489 "not_intrinsic",
490#define GET_INTRINSIC_NAME_TABLE
Reid Klecknerf5890e42018-06-23 02:02:38 +0000491#include "llvm/IR/IntrinsicImpl.inc"
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000492#undef GET_INTRINSIC_NAME_TABLE
493};
494
Justin Bogner92a8c612016-07-15 16:31:37 +0000495/// Table of per-target intrinsic name tables.
496#define GET_INTRINSIC_TARGET_DATA
Reid Klecknerf5890e42018-06-23 02:02:38 +0000497#include "llvm/IR/IntrinsicImpl.inc"
Justin Bogner92a8c612016-07-15 16:31:37 +0000498#undef GET_INTRINSIC_TARGET_DATA
499
500/// Find the segment of \c IntrinsicNameTable for intrinsics with the same
501/// target as \c Name, or the generic table if \c Name is not target specific.
502///
503/// Returns the relevant slice of \c IntrinsicNameTable
504static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
505 assert(Name.startswith("llvm."));
506
507 ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
508 // Drop "llvm." and take the first dotted component. That will be the target
509 // if this is target specific.
510 StringRef Target = Name.drop_front(5).split('.').first;
511 auto It = std::lower_bound(Targets.begin(), Targets.end(), Target,
512 [](const IntrinsicTargetInfo &TI,
513 StringRef Target) { return TI.Name < Target; });
514 // We've either found the target or just fall back to the generic set, which
515 // is always first.
516 const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
517 return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
518}
519
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000520/// This does the actual lookup of an intrinsic ID which
Pete Cooper21241132015-05-19 00:02:25 +0000521/// matches the given function name.
Tim Northover6b3bd612016-07-29 20:32:59 +0000522Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
Justin Bogner92a8c612016-07-15 16:31:37 +0000523 ArrayRef<const char *> NameTable = findTargetSubtable(Name);
Reid Klecknerc2752da2016-01-26 22:33:19 +0000524 int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
Justin Bogner92a8c612016-07-15 16:31:37 +0000525 if (Idx == -1)
526 return Intrinsic::not_intrinsic;
527
528 // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
529 // an index into a sub-table.
530 int Adjust = NameTable.data() - IntrinsicNameTable;
531 Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
Pete Cooper21241132015-05-19 00:02:25 +0000532
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000533 // If the intrinsic is not overloaded, require an exact match. If it is
Roman Tereshin12a4dc42018-02-28 23:51:49 +0000534 // overloaded, require either exact or prefix match.
535 const auto MatchSize = strlen(NameTable[Idx]);
536 assert(Name.size() >= MatchSize && "Expected either exact or prefix match");
537 bool IsExactMatch = Name.size() == MatchSize;
538 return IsExactMatch || isOverloaded(ID) ? ID : Intrinsic::not_intrinsic;
Pete Cooper21241132015-05-19 00:02:25 +0000539}
540
Pete Coopera7c0c182015-05-19 00:24:26 +0000541void Function::recalculateIntrinsicID() {
Justin Lebar291abd32016-12-28 22:59:45 +0000542 StringRef Name = getName();
543 if (!Name.startswith("llvm.")) {
544 HasLLVMReservedName = false;
Pete Coopera7c0c182015-05-19 00:24:26 +0000545 IntID = Intrinsic::not_intrinsic;
546 return;
Michael Ilseman516d7032013-03-01 18:48:54 +0000547 }
Justin Lebar291abd32016-12-28 22:59:45 +0000548 HasLLVMReservedName = true;
549 IntID = lookupIntrinsicID(Name);
Michael Ilseman516d7032013-03-01 18:48:54 +0000550}
551
Philip Reames319c48e2014-11-12 00:21:51 +0000552/// Returns a stable mangling for the type specified for use in the name
Philip Reames059ecbf2014-11-24 23:24:24 +0000553/// mangling scheme used by 'any' types in intrinsic signatures. The mangling
554/// of named types is simply their name. Manglings for unnamed types consist
555/// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
556/// combined with the mangling of their component types. A vararg function
557/// type will have a suffix of 'vararg'. Since function types can contain
558/// other function types, we close a function type mangling with suffix 'f'
559/// which can't be confused with it's prefix. This ensures we don't have
560/// collisions between two unrelated function types. Otherwise, you might
561/// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.)
Craig Topper2fa14362018-03-29 17:21:10 +0000562///
Philip Reames319c48e2014-11-12 00:21:51 +0000563static std::string getMangledTypeStr(Type* Ty) {
564 std::string Result;
565 if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000566 Result += "p" + utostr(PTyp->getAddressSpace()) +
Philip Reames319c48e2014-11-12 00:21:51 +0000567 getMangledTypeStr(PTyp->getElementType());
568 } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) {
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000569 Result += "a" + utostr(ATyp->getNumElements()) +
Philip Reames319c48e2014-11-12 00:21:51 +0000570 getMangledTypeStr(ATyp->getElementType());
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000571 } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
572 if (!STyp->isLiteral()) {
573 Result += "s_";
574 Result += STyp->getName();
575 } else {
576 Result += "sl_";
577 for (auto Elem : STyp->elements())
578 Result += getMangledTypeStr(Elem);
579 }
580 // Ensure nested structs are distinguishable.
581 Result += "s";
582 } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
Philip Reames319c48e2014-11-12 00:21:51 +0000583 Result += "f_" + getMangledTypeStr(FT->getReturnType());
584 for (size_t i = 0; i < FT->getNumParams(); i++)
585 Result += getMangledTypeStr(FT->getParamType(i));
586 if (FT->isVarArg())
587 Result += "vararg";
Philip Reames059ecbf2014-11-24 23:24:24 +0000588 // Ensure nested function types are distinguishable.
Fangrui Songf78650a2018-07-30 19:41:25 +0000589 Result += "f";
Craig Topper2fa14362018-03-29 17:21:10 +0000590 } else if (isa<VectorType>(Ty)) {
Elena Demikhovsky1ca72e12015-11-19 07:17:16 +0000591 Result += "v" + utostr(Ty->getVectorNumElements()) +
592 getMangledTypeStr(Ty->getVectorElementType());
Craig Topper2fa14362018-03-29 17:21:10 +0000593 } else if (Ty) {
594 switch (Ty->getTypeID()) {
595 default: llvm_unreachable("Unhandled type");
596 case Type::VoidTyID: Result += "isVoid"; break;
597 case Type::MetadataTyID: Result += "Metadata"; break;
598 case Type::HalfTyID: Result += "f16"; break;
599 case Type::FloatTyID: Result += "f32"; break;
600 case Type::DoubleTyID: Result += "f64"; break;
601 case Type::X86_FP80TyID: Result += "f80"; break;
602 case Type::FP128TyID: Result += "f128"; break;
603 case Type::PPC_FP128TyID: Result += "ppcf128"; break;
604 case Type::X86_MMXTyID: Result += "x86mmx"; break;
605 case Type::IntegerTyID:
606 Result += "i" + utostr(cast<IntegerType>(Ty)->getBitWidth());
607 break;
608 }
609 }
Philip Reames319c48e2014-11-12 00:21:51 +0000610 return Result;
611}
612
Pete Coopera8db71e2016-08-18 18:30:54 +0000613StringRef Intrinsic::getName(ID id) {
614 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Pete Coopera5f8c722016-08-22 20:18:28 +0000615 assert(!isOverloaded(id) &&
616 "This version of getName does not support overloading");
Pete Coopera8db71e2016-08-18 18:30:54 +0000617 return IntrinsicNameTable[id];
618}
619
Benjamin Kramere6e19332011-07-14 17:45:39 +0000620std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000621 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000622 std::string Result(IntrinsicNameTable[id]);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000623 for (Type *Ty : Tys) {
624 Result += "." + getMangledTypeStr(Ty);
Mon P Wang2c839d42008-07-30 04:36:53 +0000625 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000626 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000627}
628
Chris Lattnerf39c2782012-05-27 18:28:35 +0000629/// IIT_Info - These are enumerators that describe the entries returned by the
630/// getIntrinsicInfoTableEntries function.
631///
632/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
633enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000634 // Common values should be encoded with 0-15.
Chris Lattnerf39c2782012-05-27 18:28:35 +0000635 IIT_Done = 0,
636 IIT_I1 = 1,
637 IIT_I8 = 2,
638 IIT_I16 = 3,
639 IIT_I32 = 4,
640 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000641 IIT_F16 = 6,
642 IIT_F32 = 7,
643 IIT_F64 = 8,
644 IIT_V2 = 9,
645 IIT_V4 = 10,
646 IIT_V8 = 11,
647 IIT_V16 = 12,
648 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000649 IIT_PTR = 14,
650 IIT_ARG = 15,
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000651
Robert Khasanov65c27562014-10-20 19:25:05 +0000652 // Values from 16+ are only encodable with the inefficient encoding.
653 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000654 IIT_MMX = 17,
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000655 IIT_TOKEN = 18,
656 IIT_METADATA = 19,
657 IIT_EMPTYSTRUCT = 20,
658 IIT_STRUCT2 = 21,
659 IIT_STRUCT3 = 22,
660 IIT_STRUCT4 = 23,
661 IIT_STRUCT5 = 24,
662 IIT_EXTEND_ARG = 25,
663 IIT_TRUNC_ARG = 26,
664 IIT_ANYPTR = 27,
665 IIT_V1 = 28,
666 IIT_VARARG = 29,
667 IIT_HALF_VEC_ARG = 30,
668 IIT_SAME_VEC_WIDTH_ARG = 31,
669 IIT_PTR_TO_ARG = 32,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000670 IIT_PTR_TO_ELT = 33,
Elad Cohenef5798a2017-05-03 12:28:54 +0000671 IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000672 IIT_I128 = 35,
673 IIT_V512 = 36,
Artem Belevich786ca6a2017-10-12 17:40:00 +0000674 IIT_V1024 = 37,
675 IIT_STRUCT6 = 38,
676 IIT_STRUCT7 = 39,
Stefan Pintilie83a5fe12018-07-09 18:50:06 +0000677 IIT_STRUCT8 = 40,
678 IIT_F128 = 41
Chris Lattnerf39c2782012-05-27 18:28:35 +0000679};
680
Chris Lattnerf39c2782012-05-27 18:28:35 +0000681static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
682 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000683 using namespace Intrinsic;
684
Chris Lattnera57c7972012-05-17 05:13:57 +0000685 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000686 unsigned StructElts = 2;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000687
Chris Lattnera57c7972012-05-17 05:13:57 +0000688 switch (Info) {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000689 case IIT_Done:
690 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
691 return;
Andrew Tricka2efd992013-10-31 17:18:11 +0000692 case IIT_VARARG:
693 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
694 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000695 case IIT_MMX:
696 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
697 return;
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000698 case IIT_TOKEN:
699 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
700 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000701 case IIT_METADATA:
702 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
703 return;
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000704 case IIT_F16:
705 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
706 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000707 case IIT_F32:
708 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
709 return;
710 case IIT_F64:
711 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
712 return;
Stefan Pintilie83a5fe12018-07-09 18:50:06 +0000713 case IIT_F128:
714 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Quad, 0));
715 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000716 case IIT_I1:
717 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
718 return;
719 case IIT_I8:
720 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
721 return;
722 case IIT_I16:
723 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
724 return;
725 case IIT_I32:
726 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
727 return;
728 case IIT_I64:
729 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
730 return;
Kit Barton66460332015-05-25 15:49:26 +0000731 case IIT_I128:
732 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
733 return;
Jiangning Liu63dc8402013-09-24 02:47:27 +0000734 case IIT_V1:
735 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
736 DecodeIITType(NextElt, Infos, OutputTable);
737 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000738 case IIT_V2:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000739 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
740 DecodeIITType(NextElt, Infos, OutputTable);
741 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000742 case IIT_V4:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000743 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
744 DecodeIITType(NextElt, Infos, OutputTable);
745 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000746 case IIT_V8:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000747 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
748 DecodeIITType(NextElt, Infos, OutputTable);
749 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000750 case IIT_V16:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000751 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
752 DecodeIITType(NextElt, Infos, OutputTable);
753 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000754 case IIT_V32:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000755 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
756 DecodeIITType(NextElt, Infos, OutputTable);
757 return;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000758 case IIT_V64:
759 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
760 DecodeIITType(NextElt, Infos, OutputTable);
761 return;
Krzysztof Parzyszekb8bb90b2015-11-24 16:28:14 +0000762 case IIT_V512:
763 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512));
764 DecodeIITType(NextElt, Infos, OutputTable);
765 return;
766 case IIT_V1024:
767 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024));
768 DecodeIITType(NextElt, Infos, OutputTable);
769 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000770 case IIT_PTR:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000771 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
772 DecodeIITType(NextElt, Infos, OutputTable);
773 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000774 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000775 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000776 Infos[NextElt++]));
777 DecodeIITType(NextElt, Infos, OutputTable);
778 return;
Pete Cooper243efd72012-05-21 23:21:28 +0000779 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000780 case IIT_ARG: {
781 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
782 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
783 return;
784 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000785 case IIT_EXTEND_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000786 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000787 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000788 ArgInfo));
789 return;
790 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000791 case IIT_TRUNC_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000792 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000793 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000794 ArgInfo));
795 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000796 }
Tim Northover4516de32014-03-29 07:04:54 +0000797 case IIT_HALF_VEC_ARG: {
798 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
799 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
800 ArgInfo));
801 return;
802 }
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000803 case IIT_SAME_VEC_WIDTH_ARG: {
804 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
805 OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
806 ArgInfo));
807 return;
808 }
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000809 case IIT_PTR_TO_ARG: {
810 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
811 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
812 ArgInfo));
813 return;
814 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000815 case IIT_PTR_TO_ELT: {
816 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
817 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToElt, ArgInfo));
818 return;
819 }
Elad Cohenef5798a2017-05-03 12:28:54 +0000820 case IIT_VEC_OF_ANYPTRS_TO_ELT: {
821 unsigned short ArgNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
822 unsigned short RefNo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
823 OutputTable.push_back(
824 IITDescriptor::get(IITDescriptor::VecOfAnyPtrsToElt, ArgNo, RefNo));
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000825 return;
826 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000827 case IIT_EMPTYSTRUCT:
828 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
829 return;
Artem Belevich786ca6a2017-10-12 17:40:00 +0000830 case IIT_STRUCT8: ++StructElts; LLVM_FALLTHROUGH;
831 case IIT_STRUCT7: ++StructElts; LLVM_FALLTHROUGH;
832 case IIT_STRUCT6: ++StructElts; LLVM_FALLTHROUGH;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000833 case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH;
834 case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH;
835 case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000836 case IIT_STRUCT2: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000837 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
838
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000839 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattnerf39c2782012-05-27 18:28:35 +0000840 DecodeIITType(NextElt, Infos, OutputTable);
841 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000842 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000843 }
844 llvm_unreachable("unhandled");
845}
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000846
Chris Lattnerf39c2782012-05-27 18:28:35 +0000847#define GET_INTRINSIC_GENERATOR_GLOBAL
Reid Klecknerf5890e42018-06-23 02:02:38 +0000848#include "llvm/IR/IntrinsicImpl.inc"
Chris Lattnerf39c2782012-05-27 18:28:35 +0000849#undef GET_INTRINSIC_GENERATOR_GLOBAL
850
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000851void Intrinsic::getIntrinsicInfoTableEntries(ID id,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000852 SmallVectorImpl<IITDescriptor> &T){
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000853 // Check to see if the intrinsic's type was expressible by the table.
854 unsigned TableVal = IIT_Table[id-1];
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000855
Chris Lattnera3b0f522012-05-17 15:55:41 +0000856 // Decode the TableVal into an array of IITValues.
857 SmallVector<unsigned char, 8> IITValues;
858 ArrayRef<unsigned char> IITEntries;
859 unsigned NextElt = 0;
860 if ((TableVal >> 31) != 0) {
861 // This is an offset into the IIT_LongEncodingTable.
862 IITEntries = IIT_LongEncodingTable;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000863
Chris Lattnera3b0f522012-05-17 15:55:41 +0000864 // Strip sentinel bit.
865 NextElt = (TableVal << 1) >> 1;
866 } else {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000867 // Decode the TableVal into an array of IITValues. If the entry was encoded
868 // into a single word in the table itself, decode it now.
Chris Lattnera57c7972012-05-17 05:13:57 +0000869 do {
870 IITValues.push_back(TableVal & 0xF);
871 TableVal >>= 4;
872 } while (TableVal);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000873
Chris Lattnera3b0f522012-05-17 15:55:41 +0000874 IITEntries = IITValues;
875 NextElt = 0;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000876 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000877
878 // Okay, decode the table into the output vector of IITDescriptors.
879 DecodeIITType(NextElt, IITEntries, T);
880 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
881 DecodeIITType(NextElt, IITEntries, T);
882}
883
Chris Lattnerf39c2782012-05-27 18:28:35 +0000884static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
885 ArrayRef<Type*> Tys, LLVMContext &Context) {
886 using namespace Intrinsic;
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000887
Chris Lattnerf39c2782012-05-27 18:28:35 +0000888 IITDescriptor D = Infos.front();
889 Infos = Infos.slice(1);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000890
Chris Lattnerf39c2782012-05-27 18:28:35 +0000891 switch (D.Kind) {
892 case IITDescriptor::Void: return Type::getVoidTy(Context);
Andrew Tricka2efd992013-10-31 17:18:11 +0000893 case IITDescriptor::VarArg: return Type::getVoidTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000894 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000895 case IITDescriptor::Token: return Type::getTokenTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000896 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000897 case IITDescriptor::Half: return Type::getHalfTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000898 case IITDescriptor::Float: return Type::getFloatTy(Context);
899 case IITDescriptor::Double: return Type::getDoubleTy(Context);
Stefan Pintilie83a5fe12018-07-09 18:50:06 +0000900 case IITDescriptor::Quad: return Type::getFP128Ty(Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000901
Chris Lattnerf39c2782012-05-27 18:28:35 +0000902 case IITDescriptor::Integer:
903 return IntegerType::get(Context, D.Integer_Width);
904 case IITDescriptor::Vector:
905 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
906 case IITDescriptor::Pointer:
907 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
908 D.Pointer_AddressSpace);
909 case IITDescriptor::Struct: {
Artem Belevich786ca6a2017-10-12 17:40:00 +0000910 SmallVector<Type *, 8> Elts;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000911 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
Artem Belevich786ca6a2017-10-12 17:40:00 +0000912 Elts.push_back(DecodeFixedType(Infos, Tys, Context));
913 return StructType::get(Context, Elts);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000914 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000915 case IITDescriptor::Argument:
916 return Tys[D.getArgumentNumber()];
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000917 case IITDescriptor::ExtendArgument: {
918 Type *Ty = Tys[D.getArgumentNumber()];
919 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
920 return VectorType::getExtendedElementVectorType(VTy);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000921
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000922 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
923 }
924 case IITDescriptor::TruncArgument: {
925 Type *Ty = Tys[D.getArgumentNumber()];
926 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
927 return VectorType::getTruncatedElementVectorType(VTy);
928
929 IntegerType *ITy = cast<IntegerType>(Ty);
930 assert(ITy->getBitWidth() % 2 == 0);
931 return IntegerType::get(Context, ITy->getBitWidth() / 2);
932 }
Tim Northover4516de32014-03-29 07:04:54 +0000933 case IITDescriptor::HalfVecArgument:
934 return VectorType::getHalfElementsVectorType(cast<VectorType>(
935 Tys[D.getArgumentNumber()]));
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000936 case IITDescriptor::SameVecWidthArgument: {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000937 Type *EltTy = DecodeFixedType(Infos, Tys, Context);
938 Type *Ty = Tys[D.getArgumentNumber()];
939 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
940 return VectorType::get(EltTy, VTy->getNumElements());
941 }
942 llvm_unreachable("unhandled");
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000943 }
944 case IITDescriptor::PtrToArgument: {
945 Type *Ty = Tys[D.getArgumentNumber()];
946 return PointerType::getUnqual(Ty);
947 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000948 case IITDescriptor::PtrToElt: {
949 Type *Ty = Tys[D.getArgumentNumber()];
950 VectorType *VTy = dyn_cast<VectorType>(Ty);
951 if (!VTy)
952 llvm_unreachable("Expected an argument of Vector Type");
953 Type *EltTy = VTy->getVectorElementType();
954 return PointerType::getUnqual(EltTy);
955 }
Elad Cohenef5798a2017-05-03 12:28:54 +0000956 case IITDescriptor::VecOfAnyPtrsToElt:
957 // Return the overloaded type (which determines the pointers address space)
958 return Tys[D.getOverloadArgNumber()];
Eugene Zelenkoeba7e4e2017-05-10 23:41:30 +0000959 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000960 llvm_unreachable("unhandled");
961}
962
Chris Lattnerf39c2782012-05-27 18:28:35 +0000963FunctionType *Intrinsic::getType(LLVMContext &Context,
964 ID id, ArrayRef<Type*> Tys) {
965 SmallVector<IITDescriptor, 8> Table;
966 getIntrinsicInfoTableEntries(id, Table);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000967
Chris Lattnerf39c2782012-05-27 18:28:35 +0000968 ArrayRef<IITDescriptor> TableRef = Table;
969 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000970
Chris Lattnera3b0f522012-05-17 15:55:41 +0000971 SmallVector<Type*, 8> ArgTys;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000972 while (!TableRef.empty())
973 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
Jim Laskey2682ea62007-02-07 20:38:26 +0000974
Steven Wud05affc2014-10-20 15:47:24 +0000975 // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
976 // If we see void type as the type of the last argument, it is vararg intrinsic
977 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
978 ArgTys.pop_back();
979 return FunctionType::get(ResultTy, ArgTys, true);
980 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000981 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey2682ea62007-02-07 20:38:26 +0000982}
983
Mon P Wangb4024932009-02-24 23:17:49 +0000984bool Intrinsic::isOverloaded(ID id) {
Mon P Wangb4024932009-02-24 23:17:49 +0000985#define GET_INTRINSIC_OVERLOAD_TABLE
Reid Klecknerf5890e42018-06-23 02:02:38 +0000986#include "llvm/IR/IntrinsicImpl.inc"
Mon P Wangb4024932009-02-24 23:17:49 +0000987#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wangb4024932009-02-24 23:17:49 +0000988}
989
Sanjoy Dasc65d43e2015-06-18 19:28:26 +0000990bool Intrinsic::isLeaf(ID id) {
991 switch (id) {
992 default:
993 return true;
994
995 case Intrinsic::experimental_gc_statepoint:
996 case Intrinsic::experimental_patchpoint_void:
997 case Intrinsic::experimental_patchpoint_i64:
998 return false;
999 }
1000}
1001
Chris Lattner49b7ee12009-01-12 01:18:58 +00001002/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +00001003#define GET_INTRINSIC_ATTRIBUTES
Reid Klecknerf5890e42018-06-23 02:02:38 +00001004#include "llvm/IR/IntrinsicImpl.inc"
Duncan Sands38ef3a82007-12-03 20:06:50 +00001005#undef GET_INTRINSIC_ATTRIBUTES
1006
Benjamin Kramere6e19332011-07-14 17:45:39 +00001007Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +00001008 // There can never be multiple globals with the same name of different types,
1009 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +00001010 return
Benjamin Kramere6e19332011-07-14 17:45:39 +00001011 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
1012 getType(M->getContext(), id, Tys)));
Jim Laskey2682ea62007-02-07 20:38:26 +00001013}
1014
Dale Johannesenb842d522009-02-05 01:49:45 +00001015// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
1016#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
Reid Klecknerf5890e42018-06-23 02:02:38 +00001017#include "llvm/IR/IntrinsicImpl.inc"
Dale Johannesenb842d522009-02-05 01:49:45 +00001018#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
1019
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +00001020// This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
1021#define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
Reid Klecknerf5890e42018-06-23 02:02:38 +00001022#include "llvm/IR/IntrinsicImpl.inc"
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +00001023#undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
1024
Artur Pilipenkobc552272016-06-22 14:56:33 +00001025bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
1026 SmallVectorImpl<Type*> &ArgTys) {
1027 using namespace Intrinsic;
1028
1029 // If we ran out of descriptors, there are too many arguments.
1030 if (Infos.empty()) return true;
1031 IITDescriptor D = Infos.front();
1032 Infos = Infos.slice(1);
1033
1034 switch (D.Kind) {
1035 case IITDescriptor::Void: return !Ty->isVoidTy();
1036 case IITDescriptor::VarArg: return true;
1037 case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
1038 case IITDescriptor::Token: return !Ty->isTokenTy();
1039 case IITDescriptor::Metadata: return !Ty->isMetadataTy();
1040 case IITDescriptor::Half: return !Ty->isHalfTy();
1041 case IITDescriptor::Float: return !Ty->isFloatTy();
1042 case IITDescriptor::Double: return !Ty->isDoubleTy();
Stefan Pintilie83a5fe12018-07-09 18:50:06 +00001043 case IITDescriptor::Quad: return !Ty->isFP128Ty();
Artur Pilipenkobc552272016-06-22 14:56:33 +00001044 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1045 case IITDescriptor::Vector: {
1046 VectorType *VT = dyn_cast<VectorType>(Ty);
1047 return !VT || VT->getNumElements() != D.Vector_Width ||
1048 matchIntrinsicType(VT->getElementType(), Infos, ArgTys);
1049 }
1050 case IITDescriptor::Pointer: {
1051 PointerType *PT = dyn_cast<PointerType>(Ty);
1052 return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace ||
1053 matchIntrinsicType(PT->getElementType(), Infos, ArgTys);
1054 }
1055
1056 case IITDescriptor::Struct: {
1057 StructType *ST = dyn_cast<StructType>(Ty);
1058 if (!ST || ST->getNumElements() != D.Struct_NumElements)
1059 return true;
1060
1061 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1062 if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys))
1063 return true;
1064 return false;
1065 }
1066
1067 case IITDescriptor::Argument:
1068 // Two cases here - If this is the second occurrence of an argument, verify
1069 // that the later instance matches the previous instance.
1070 if (D.getArgumentNumber() < ArgTys.size())
1071 return Ty != ArgTys[D.getArgumentNumber()];
1072
1073 // Otherwise, if this is the first instance of an argument, record it and
1074 // verify the "Any" kind.
1075 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
1076 ArgTys.push_back(Ty);
1077
1078 switch (D.getArgumentKind()) {
1079 case IITDescriptor::AK_Any: return false; // Success
1080 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
1081 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
1082 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
1083 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
1084 }
1085 llvm_unreachable("all argument kinds not covered");
1086
1087 case IITDescriptor::ExtendArgument: {
1088 // This may only be used when referring to a previous vector argument.
1089 if (D.getArgumentNumber() >= ArgTys.size())
1090 return true;
1091
1092 Type *NewTy = ArgTys[D.getArgumentNumber()];
1093 if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1094 NewTy = VectorType::getExtendedElementVectorType(VTy);
1095 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1096 NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
1097 else
1098 return true;
1099
1100 return Ty != NewTy;
1101 }
1102 case IITDescriptor::TruncArgument: {
1103 // This may only be used when referring to a previous vector argument.
1104 if (D.getArgumentNumber() >= ArgTys.size())
1105 return true;
1106
1107 Type *NewTy = ArgTys[D.getArgumentNumber()];
1108 if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1109 NewTy = VectorType::getTruncatedElementVectorType(VTy);
1110 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1111 NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
1112 else
1113 return true;
1114
1115 return Ty != NewTy;
1116 }
1117 case IITDescriptor::HalfVecArgument:
1118 // This may only be used when referring to a previous vector argument.
1119 return D.getArgumentNumber() >= ArgTys.size() ||
1120 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1121 VectorType::getHalfElementsVectorType(
1122 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1123 case IITDescriptor::SameVecWidthArgument: {
1124 if (D.getArgumentNumber() >= ArgTys.size())
1125 return true;
1126 VectorType * ReferenceType =
Elena Demikhovskycaaceef2016-11-03 03:23:55 +00001127 dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
Artur Pilipenkobc552272016-06-22 14:56:33 +00001128 VectorType *ThisArgType = dyn_cast<VectorType>(Ty);
1129 if (!ThisArgType || !ReferenceType ||
1130 (ReferenceType->getVectorNumElements() !=
1131 ThisArgType->getVectorNumElements()))
1132 return true;
1133 return matchIntrinsicType(ThisArgType->getVectorElementType(),
1134 Infos, ArgTys);
1135 }
1136 case IITDescriptor::PtrToArgument: {
1137 if (D.getArgumentNumber() >= ArgTys.size())
1138 return true;
1139 Type * ReferenceType = ArgTys[D.getArgumentNumber()];
1140 PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1141 return (!ThisArgType || ThisArgType->getElementType() != ReferenceType);
1142 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +00001143 case IITDescriptor::PtrToElt: {
1144 if (D.getArgumentNumber() >= ArgTys.size())
1145 return true;
1146 VectorType * ReferenceType =
1147 dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
1148 PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1149
1150 return (!ThisArgType || !ReferenceType ||
1151 ThisArgType->getElementType() != ReferenceType->getElementType());
1152 }
Elad Cohenef5798a2017-05-03 12:28:54 +00001153 case IITDescriptor::VecOfAnyPtrsToElt: {
1154 unsigned RefArgNumber = D.getRefArgNumber();
1155
1156 // This may only be used when referring to a previous argument.
1157 if (RefArgNumber >= ArgTys.size())
Artur Pilipenkobc552272016-06-22 14:56:33 +00001158 return true;
Elad Cohenef5798a2017-05-03 12:28:54 +00001159
1160 // Record the overloaded type
1161 assert(D.getOverloadArgNumber() == ArgTys.size() &&
1162 "Table consistency error");
1163 ArgTys.push_back(Ty);
1164
1165 // Verify the overloaded type "matches" the Ref type.
1166 // i.e. Ty is a vector with the same width as Ref.
1167 // Composed of pointers to the same element type as Ref.
1168 VectorType *ReferenceType = dyn_cast<VectorType>(ArgTys[RefArgNumber]);
Artur Pilipenkobc552272016-06-22 14:56:33 +00001169 VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1170 if (!ThisArgVecTy || !ReferenceType ||
1171 (ReferenceType->getVectorNumElements() !=
1172 ThisArgVecTy->getVectorNumElements()))
1173 return true;
1174 PointerType *ThisArgEltTy =
1175 dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType());
1176 if (!ThisArgEltTy)
1177 return true;
1178 return ThisArgEltTy->getElementType() !=
1179 ReferenceType->getVectorElementType();
1180 }
1181 }
1182 llvm_unreachable("unhandled");
1183}
1184
Artur Pilipenkob68b8212016-06-24 14:47:27 +00001185bool
1186Intrinsic::matchIntrinsicVarArg(bool isVarArg,
1187 ArrayRef<Intrinsic::IITDescriptor> &Infos) {
1188 // If there are no descriptors left, then it can't be a vararg.
1189 if (Infos.empty())
1190 return isVarArg;
1191
1192 // There should be only one descriptor remaining at this point.
1193 if (Infos.size() != 1)
1194 return true;
1195
1196 // Check and verify the descriptor.
1197 IITDescriptor D = Infos.front();
1198 Infos = Infos.slice(1);
1199 if (D.Kind == IITDescriptor::VarArg)
1200 return !isVarArg;
1201
1202 return true;
1203}
1204
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +00001205Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) {
1206 Intrinsic::ID ID = F->getIntrinsicID();
1207 if (!ID)
1208 return None;
1209
1210 FunctionType *FTy = F->getFunctionType();
1211 // Accumulate an array of overloaded types for the given intrinsic
1212 SmallVector<Type *, 4> ArgTys;
1213 {
1214 SmallVector<Intrinsic::IITDescriptor, 8> Table;
1215 getIntrinsicInfoTableEntries(ID, Table);
1216 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
1217
1218 // If we encounter any problems matching the signature with the descriptor
1219 // just give up remangling. It's up to verifier to report the discrepancy.
1220 if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
1221 return None;
1222 for (auto Ty : FTy->params())
1223 if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
1224 return None;
1225 if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
1226 return None;
1227 }
1228
1229 StringRef Name = F->getName();
1230 if (Name == Intrinsic::getName(ID, ArgTys))
1231 return None;
1232
1233 auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1234 NewDecl->setCallingConv(F->getCallingConv());
1235 assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
1236 return NewDecl;
1237}
1238
Gabor Greif161cb042010-03-23 14:40:20 +00001239/// hasAddressTaken - returns true if there are any uses of this function
1240/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001241bool Function::hasAddressTaken(const User* *PutOffender) const {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001242 for (const Use &U : uses()) {
1243 const User *FU = U.getUser();
1244 if (isa<BlockAddress>(FU))
Jay Foadca0c4992012-05-12 08:30:16 +00001245 continue;
Richard Trieu7a083812016-02-18 22:09:30 +00001246 if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU)) {
1247 if (PutOffender)
1248 *PutOffender = FU;
1249 return true;
1250 }
Chandler Carruthcdf47882014-03-09 03:16:01 +00001251 ImmutableCallSite CS(cast<Instruction>(FU));
Richard Trieu7a083812016-02-18 22:09:30 +00001252 if (!CS.isCallee(&U)) {
1253 if (PutOffender)
1254 *PutOffender = FU;
1255 return true;
1256 }
Jay Foad557169d2009-06-10 08:41:11 +00001257 }
1258 return false;
1259}
1260
Eli Friedman1923a332011-10-20 05:23:42 +00001261bool Function::isDefTriviallyDead() const {
1262 // Check the linkage
1263 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
1264 !hasAvailableExternallyLinkage())
1265 return false;
1266
1267 // Check if the function is used by anything other than a blockaddress.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001268 for (const User *U : users())
1269 if (!isa<BlockAddress>(U))
Eli Friedman1923a332011-10-20 05:23:42 +00001270 return false;
1271
1272 return true;
1273}
1274
Bill Wendling63a4ea12011-10-17 18:43:40 +00001275/// callsFunctionThatReturnsTwice - Return true if the function has a call to
1276/// setjmp or other function that gcc recognizes as "returning twice".
1277bool Function::callsFunctionThatReturnsTwice() const {
1278 for (const_inst_iterator
1279 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
Mark Seaborn82711182014-01-14 04:20:01 +00001280 ImmutableCallSite CS(&*I);
1281 if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
Bill Wendling63a4ea12011-10-17 18:43:40 +00001282 return true;
1283 }
1284
1285 return false;
1286}
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001287
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001288Constant *Function::getPersonalityFn() const {
1289 assert(hasPersonalityFn() && getNumOperands());
1290 return cast<Constant>(Op<0>());
Vedant Kumar1ab5ea52015-10-06 20:31:57 +00001291}
1292
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001293void Function::setPersonalityFn(Constant *Fn) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001294 setHungoffOperand<0>(Fn);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001295 setValueSubclassDataBit(3, Fn != nullptr);
Vedant Kumar1ab5ea52015-10-06 20:31:57 +00001296}
1297
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001298Constant *Function::getPrefixData() const {
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001299 assert(hasPrefixData() && getNumOperands());
1300 return cast<Constant>(Op<1>());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001301}
1302
1303void Function::setPrefixData(Constant *PrefixData) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001304 setHungoffOperand<1>(PrefixData);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001305 setValueSubclassDataBit(1, PrefixData != nullptr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001306}
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001307
1308Constant *Function::getPrologueData() const {
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001309 assert(hasPrologueData() && getNumOperands());
1310 return cast<Constant>(Op<2>());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001311}
1312
1313void Function::setPrologueData(Constant *PrologueData) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001314 setHungoffOperand<2>(PrologueData);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001315 setValueSubclassDataBit(2, PrologueData != nullptr);
1316}
1317
1318void Function::allocHungoffUselist() {
1319 // If we've already allocated a uselist, stop here.
1320 if (getNumOperands())
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001321 return;
1322
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001323 allocHungoffUses(3, /*IsPhi=*/ false);
1324 setNumHungOffUseOperands(3);
1325
1326 // Initialize the uselist with placeholder operands to allow traversal.
1327 auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
1328 Op<0>().set(CPN);
1329 Op<1>().set(CPN);
1330 Op<2>().set(CPN);
1331}
1332
1333template <int Idx>
1334void Function::setHungoffOperand(Constant *C) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001335 if (C) {
1336 allocHungoffUselist();
1337 Op<Idx>().set(C);
1338 } else if (getNumOperands()) {
1339 Op<Idx>().set(
1340 ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
1341 }
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001342}
1343
1344void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
1345 assert(Bit < 16 && "SubclassData contains only 16 bits");
1346 if (On)
1347 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
1348 else
1349 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001350}
Akira Hatanaka3058d0f2015-05-06 23:54:14 +00001351
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001352void Function::setEntryCount(ProfileCount Count,
Dehao Chena60cdd32017-02-28 18:09:44 +00001353 const DenseSet<GlobalValue::GUID> *S) {
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001354 assert(Count.hasValue());
1355#if !defined(NDEBUG)
1356 auto PrevCount = getEntryCount();
1357 assert(!PrevCount.hasValue() || PrevCount.getType() == Count.getType());
1358#endif
Diego Novillo2567f3d2015-05-13 15:13:45 +00001359 MDBuilder MDB(getContext());
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001360 setMetadata(
1361 LLVMContext::MD_prof,
1362 MDB.createFunctionEntryCount(Count.getCount(), Count.isSynthetic(), S));
Diego Novillo2567f3d2015-05-13 15:13:45 +00001363}
1364
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001365void Function::setEntryCount(uint64_t Count, Function::ProfileCountType Type,
1366 const DenseSet<GlobalValue::GUID> *Imports) {
1367 setEntryCount(ProfileCount(Count, Type), Imports);
1368}
1369
1370ProfileCount Function::getEntryCount() const {
Diego Novillo2567f3d2015-05-13 15:13:45 +00001371 MDNode *MD = getMetadata(LLVMContext::MD_prof);
1372 if (MD && MD->getOperand(0))
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001373 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) {
Diego Novillo2567f3d2015-05-13 15:13:45 +00001374 if (MDS->getString().equals("function_entry_count")) {
1375 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
David Callahanc1c810d2016-08-05 18:38:19 +00001376 uint64_t Count = CI->getValue().getZExtValue();
Teresa Johnson915897e2017-12-18 20:02:43 +00001377 // A value of -1 is used for SamplePGO when there were no samples.
1378 // Treat this the same as unknown.
1379 if (Count == (uint64_t)-1)
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001380 return ProfileCount::getInvalid();
1381 return ProfileCount(Count, PCT_Real);
1382 } else if (MDS->getString().equals("synthetic_function_entry_count")) {
1383 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
1384 uint64_t Count = CI->getValue().getZExtValue();
1385 return ProfileCount(Count, PCT_Synthetic);
Diego Novillo2567f3d2015-05-13 15:13:45 +00001386 }
Easwaran Ramane5b8de22018-01-17 22:24:23 +00001387 }
1388 return ProfileCount::getInvalid();
Diego Novillo2567f3d2015-05-13 15:13:45 +00001389}
Dehao Chen302b69c2016-10-18 20:42:47 +00001390
Dehao Chena60cdd32017-02-28 18:09:44 +00001391DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
1392 DenseSet<GlobalValue::GUID> R;
1393 if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
1394 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
1395 if (MDS->getString().equals("function_entry_count"))
1396 for (unsigned i = 2; i < MD->getNumOperands(); i++)
1397 R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
1398 ->getValue()
1399 .getZExtValue());
1400 return R;
1401}
1402
Dehao Chen302b69c2016-10-18 20:42:47 +00001403void Function::setSectionPrefix(StringRef Prefix) {
1404 MDBuilder MDB(getContext());
1405 setMetadata(LLVMContext::MD_section_prefix,
1406 MDB.createFunctionSectionPrefix(Prefix));
1407}
1408
1409Optional<StringRef> Function::getSectionPrefix() const {
1410 if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
Craig Topper781aa182018-05-05 01:57:00 +00001411 assert(cast<MDString>(MD->getOperand(0))
Dehao Chen302b69c2016-10-18 20:42:47 +00001412 ->getString()
1413 .equals("function_section_prefix") &&
1414 "Metadata not match");
Craig Topper781aa182018-05-05 01:57:00 +00001415 return cast<MDString>(MD->getOperand(1))->getString();
Dehao Chen302b69c2016-10-18 20:42:47 +00001416 }
1417 return None;
1418}
Manoj Gupta77eeac32018-07-09 22:27:23 +00001419
1420bool Function::nullPointerIsDefined() const {
1421 return getFnAttribute("null-pointer-is-valid")
1422 .getValueAsString()
1423 .equals("true");
1424}
1425
1426bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
1427 if (F && F->nullPointerIsDefined())
1428 return true;
1429
1430 if (AS != 0)
1431 return true;
1432
1433 return false;
1434}