blob: fc61ba7439ba0ce0c221979797893289bb982498 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +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 Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/Function.h"
Michael Ilseman516d7032013-03-01 18:48:54 +000015#include "LLVMContextImpl.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000016#include "SymbolTableListTraitsImpl.h"
Rafael Espindola2050af82011-05-16 03:05:33 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000018#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000020#include "llvm/IR/CallSite.h"
Diego Novillo2567f3d2015-05-13 15:13:45 +000021#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DerivedTypes.h"
Chandler Carruth83948572014-03-04 10:30:26 +000023#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/LLVMContext.h"
Diego Novillo2567f3d2015-05-13 15:13:45 +000026#include "llvm/IR/MDBuilder.h"
27#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Module.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner113f4f42002-06-25 16:13:24 +000031// Explicit instantiations of SymbolTableListTraits since some of the methods
32// are not in the public header file...
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000033template class llvm::SymbolTableListTraits<BasicBlock>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000034
Chris Lattnerda975502001-09-10 07:58:01 +000035//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000036// Argument Implementation
37//===----------------------------------------------------------------------===//
38
David Blaikiea379b1812011-12-20 02:50:00 +000039void Argument::anchor() { }
40
Reid Kleckner56d028d2017-03-17 17:16:39 +000041Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo)
42 : Value(Ty, Value::ArgumentVal), Parent(Par), ArgNo(ArgNo) {
Chris Lattner32ab6432007-02-12 05:18:08 +000043 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000044}
45
Chris Lattner9ed7aef2002-09-06 21:33:15 +000046void Argument::setParent(Function *parent) {
47 Parent = parent;
48}
49
Nick Lewyckyd52b1522014-05-20 01:23:40 +000050bool Argument::hasNonNullAttr() const {
51 if (!getType()->isPointerTy()) return false;
Reid Klecknerf021fab2017-04-13 23:12:13 +000052 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull))
Hal Finkelb0407ba2014-07-18 15:51:28 +000053 return true;
54 else if (getDereferenceableBytes() > 0 &&
55 getType()->getPointerAddressSpace() == 0)
56 return true;
57 return false;
Nick Lewyckyd52b1522014-05-20 01:23:40 +000058}
59
Chris Lattnere30f09d2008-01-24 17:47:11 +000060bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000061 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +000062 return hasAttribute(Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000063}
64
Manman Renf46262e2016-03-29 17:37:21 +000065bool Argument::hasSwiftSelfAttr() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +000066 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
Manman Renf46262e2016-03-29 17:37:21 +000067}
68
Manman Ren9bfd0d02016-04-01 21:41:15 +000069bool Argument::hasSwiftErrorAttr() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +000070 return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
Manman Ren9bfd0d02016-04-01 21:41:15 +000071}
72
Reid Klecknera534a382013-12-19 02:14:12 +000073bool Argument::hasInAllocaAttr() const {
74 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +000075 return hasAttribute(Attribute::InAlloca);
Reid Klecknera534a382013-12-19 02:14:12 +000076}
77
Reid Kleckner436c42e2014-01-17 23:58:17 +000078bool Argument::hasByValOrInAllocaAttr() const {
79 if (!getType()->isPointerTy()) return false;
Reid Klecknerb5180542017-03-21 16:57:19 +000080 AttributeList Attrs = getParent()->getAttributes();
Reid Klecknerf021fab2017-04-13 23:12:13 +000081 return Attrs.hasParamAttribute(getArgNo(), Attribute::ByVal) ||
82 Attrs.hasParamAttribute(getArgNo(), Attribute::InAlloca);
Reid Kleckner436c42e2014-01-17 23:58:17 +000083}
84
Chris Lattner4d37d992011-05-22 23:57:23 +000085unsigned Argument::getParamAlignment() const {
86 assert(getType()->isPointerTy() && "Only pointers have alignments");
Reid Kleckner859f8b52017-04-28 20:34:27 +000087 return getParent()->getParamAlignment(getArgNo());
Chris Lattner4d37d992011-05-22 23:57:23 +000088}
89
Hal Finkelb0407ba2014-07-18 15:51:28 +000090uint64_t Argument::getDereferenceableBytes() const {
91 assert(getType()->isPointerTy() &&
92 "Only pointers have dereferenceable bytes");
93 return getParent()->getDereferenceableBytes(getArgNo()+1);
94}
95
Sanjoy Das06cf33f2015-05-06 17:41:54 +000096uint64_t Argument::getDereferenceableOrNullBytes() const {
97 assert(getType()->isPointerTy() &&
98 "Only pointers have dereferenceable bytes");
99 return getParent()->getDereferenceableOrNullBytes(getArgNo()+1);
100}
101
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000102bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000103 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000104 return hasAttribute(Attribute::Nest);
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000105}
106
Chris Lattnere30f09d2008-01-24 17:47:11 +0000107bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000108 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000109 return hasAttribute(Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000110}
111
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000112bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000113 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000114 return hasAttribute(Attribute::NoCapture);
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000115}
116
Owen Andersonc64dfb42008-02-17 23:22:28 +0000117bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000118 if (!getType()->isPointerTy()) return false;
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000119 return hasAttribute(Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000120}
121
Stephen Linb8bd2322013-04-20 05:14:40 +0000122bool Argument::hasReturnedAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000123 return hasAttribute(Attribute::Returned);
Stephen Linb8bd2322013-04-20 05:14:40 +0000124}
125
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000126bool Argument::hasZExtAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000127 return hasAttribute(Attribute::ZExt);
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000128}
129
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000130bool Argument::hasSExtAttr() const {
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000131 return hasAttribute(Attribute::SExt);
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000132}
133
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000134bool Argument::onlyReadsMemory() const {
Reid Klecknerf021fab2017-04-13 23:12:13 +0000135 AttributeList Attrs = getParent()->getAttributes();
136 return Attrs.hasParamAttribute(getArgNo(), Attribute::ReadOnly) ||
137 Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000138}
139
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000140void Argument::addAttrs(AttrBuilder &B) {
141 AttributeList AL = getParent()->getAttributes();
142 AL = AL.addAttributes(Parent->getContext(), getArgNo() + 1, B);
143 getParent()->setAttributes(AL);
144}
145
146void Argument::addAttr(Attribute::AttrKind Kind) {
147 getParent()->addAttribute(getArgNo() + 1, Kind);
148}
149
150void Argument::addAttr(Attribute Attr) {
151 getParent()->addAttribute(getArgNo() + 1, Attr);
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000152}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000153
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000154void Argument::removeAttr(Attribute::AttrKind Kind) {
155 getParent()->removeAttribute(getArgNo() + 1, Kind);
156}
157
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000158bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
Reid Klecknerf021fab2017-04-13 23:12:13 +0000159 return getParent()->hasParamAttribute(getArgNo(), Kind);
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000160}
161
Chris Lattnerd255ae22002-04-09 19:39:35 +0000162//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000163// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000164//===----------------------------------------------------------------------===//
165
Owen Anderson47db9412009-07-22 00:24:57 +0000166LLVMContext &Function::getContext() const {
167 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000168}
169
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000170void Function::removeFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000171 getParent()->getFunctionList().remove(getIterator());
Duncan Sands185eeac2007-11-25 14:10:56 +0000172}
173
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000174void Function::eraseFromParent() {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000175 getParent()->getFunctionList().erase(getIterator());
Reid Spencer019c8862007-04-09 15:01:12 +0000176}
177
Reid Spencer019c8862007-04-09 15:01:12 +0000178//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000179// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000180//===----------------------------------------------------------------------===//
181
Rafael Espindola1b47a282014-10-23 15:20:05 +0000182Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name,
183 Module *ParentModule)
David Blaikied583b192015-08-21 21:35:28 +0000184 : GlobalObject(Ty, Value::FunctionVal,
Reid Kleckner56d028d2017-03-17 17:16:39 +0000185 OperandTraits<Function>::op_begin(this), 0, Linkage, name),
186 Arguments(nullptr), NumArgs(Ty->getNumParams()) {
Chris Lattner654695b2009-01-05 07:58:59 +0000187 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000188 "invalid return type");
Duncan P. N. Exon Smith4472b772015-04-24 20:47:23 +0000189 setGlobalObjectSubClassData(0);
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000190
191 // We only need a symbol table for a function if the context keeps value names
192 if (!getContext().shouldDiscardValueNames())
193 SymTab = make_unique<ValueSymbolTable>();
Chris Lattner6213ae02002-09-06 20:46:32 +0000194
Chris Lattnere2de9082007-08-18 06:14:52 +0000195 // If the function has arguments, mark them as lazily built.
196 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000197 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000198
Chris Lattner6213ae02002-09-06 20:46:32 +0000199 if (ParentModule)
200 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000201
Justin Lebar291abd32016-12-28 22:59:45 +0000202 HasLLVMReservedName = getName().startswith("llvm.");
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000203 // Ensure intrinsics have the right parameter attributes.
Pete Coopera7c0c182015-05-19 00:24:26 +0000204 // Note, the IntID field will have been set in Value::setName if this function
205 // name is a valid intrinsic ID.
206 if (IntID)
207 setAttributes(Intrinsic::getAttributes(getContext(), IntID));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000208}
209
Gordon Henriksen14a55692007-12-10 02:14:30 +0000210Function::~Function() {
211 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213 // Delete all of the method arguments and unlink from symbol table...
Reid Kleckner56d028d2017-03-17 17:16:39 +0000214 if (Arguments)
215 clearArguments();
Reid Spencerc6a83842007-04-22 17:28:03 +0000216
Gordon Henriksend930f912008-08-17 18:44:35 +0000217 // Remove the function from the on-the-side GC table.
218 clearGC();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219}
220
Chris Lattnere2de9082007-08-18 06:14:52 +0000221void Function::BuildLazyArguments() const {
222 // Create the arguments vector, all arguments start out unnamed.
Reid Kleckner56d028d2017-03-17 17:16:39 +0000223 auto *FT = getFunctionType();
224 if (NumArgs > 0) {
225 Arguments = std::allocator<Argument>().allocate(NumArgs);
226 for (unsigned i = 0, e = NumArgs; i != e; ++i) {
227 Type *ArgTy = FT->getParamType(i);
228 assert(!ArgTy->isVoidTy() && "Cannot have void typed arguments!");
229 new (Arguments + i) Argument(ArgTy, "", const_cast<Function *>(this), i);
230 }
Chris Lattnere2de9082007-08-18 06:14:52 +0000231 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000232
Chris Lattnere2de9082007-08-18 06:14:52 +0000233 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000234 unsigned SDC = getSubclassDataFromValue();
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000235 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0));
Reid Kleckner56d028d2017-03-17 17:16:39 +0000236 assert(!hasLazyArguments());
237}
238
239static MutableArrayRef<Argument> makeArgArray(Argument *Args, size_t Count) {
240 return MutableArrayRef<Argument>(Args, Count);
241}
242
243void Function::clearArguments() {
244 for (Argument &A : makeArgArray(Arguments, NumArgs)) {
245 A.setName("");
246 A.~Argument();
247 }
248 std::allocator<Argument>().deallocate(Arguments, NumArgs);
249 Arguments = nullptr;
Chris Lattnere2de9082007-08-18 06:14:52 +0000250}
251
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000252void Function::stealArgumentListFrom(Function &Src) {
253 assert(isDeclaration() && "Expected no references to current arguments");
254
255 // Drop the current arguments, if any, and set the lazy argument bit.
256 if (!hasLazyArguments()) {
Reid Kleckner56d028d2017-03-17 17:16:39 +0000257 assert(llvm::all_of(makeArgArray(Arguments, NumArgs),
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000258 [](const Argument &A) { return A.use_empty(); }) &&
259 "Expected arguments to be unused in declaration");
Reid Kleckner56d028d2017-03-17 17:16:39 +0000260 clearArguments();
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000261 setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
262 }
263
264 // Nothing to steal if Src has lazy arguments.
265 if (Src.hasLazyArguments())
266 return;
267
268 // Steal arguments from Src, and fix the lazy argument bits.
Reid Kleckner56d028d2017-03-17 17:16:39 +0000269 assert(arg_size() == Src.arg_size());
270 Arguments = Src.Arguments;
271 Src.Arguments = nullptr;
272 for (Argument &A : makeArgArray(Arguments, NumArgs)) {
273 // FIXME: This does the work of transferNodesFromList inefficiently.
274 SmallString<128> Name;
275 if (A.hasName())
276 Name = A.getName();
277 if (!Name.empty())
278 A.setName("");
279 A.setParent(this);
280 if (!Name.empty())
281 A.setName(Name);
282 }
283
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000284 setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
Reid Kleckner56d028d2017-03-17 17:16:39 +0000285 assert(!hasLazyArguments());
Duncan P. N. Exon Smithbdfc9842016-04-06 06:38:15 +0000286 Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
287}
288
Chris Lattner2f7c9632001-06-06 20:29:01 +0000289// dropAllReferences() - This function causes all the subinstructions to "let
290// go" of all references that they are maintaining. This allows one to
291// 'delete' a whole class at a time, even though there may be circular
292// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000293// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295// delete.
296//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000297void Function::dropAllReferences() {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000298 setIsMaterializable(false);
299
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000300 for (BasicBlock &BB : *this)
301 BB.dropAllReferences();
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000302
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000303 // Delete all basic blocks. They are now unused, except possibly by
304 // blockaddresses, but BasicBlock's destructor takes care of those.
305 while (!BasicBlocks.empty())
306 BasicBlocks.begin()->eraseFromParent();
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000307
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000308 // Drop uses of any optional data (real or placeholder).
309 if (getNumOperands()) {
310 User::dropAllReferences();
311 setNumHungOffUseOperands(0);
312 setValueSubclassData(getSubclassDataFromValue() & ~0xe);
313 }
Duncan P. N. Exon Smithe2510cd2015-04-24 21:51:02 +0000314
315 // Metadata is stored in a side-table.
316 clearMetadata();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317}
Chris Lattnerda975502001-09-10 07:58:01 +0000318
Amaury Sechet392638d2016-06-14 20:27:35 +0000319void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000320 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000321 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000322 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000323}
324
Amaury Sechet5db224e2016-06-12 06:17:24 +0000325void Function::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000326 AttributeList PAL = getAttributes();
Amaury Sechet5db224e2016-06-12 06:17:24 +0000327 PAL = PAL.addAttribute(getContext(), i, Attr);
328 setAttributes(PAL);
329}
330
Hans Wennborg0f88d862017-04-28 23:01:32 +0000331void Function::addAttributes(unsigned i, AttributeList Attrs) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000332 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000333 PAL = PAL.addAttributes(getContext(), i, Attrs);
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000334 setAttributes(PAL);
335}
336
Amaury Sechet392638d2016-06-14 20:27:35 +0000337void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000338 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000339 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet7b05a4c2016-03-14 01:37:29 +0000340 setAttributes(PAL);
341}
342
Amaury Sechet6100adf2016-06-15 17:50:39 +0000343void Function::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000344 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000345 PAL = PAL.removeAttribute(getContext(), i, Kind);
346 setAttributes(PAL);
347}
348
Hans Wennborg0f88d862017-04-28 23:01:32 +0000349void Function::removeAttributes(unsigned i, AttributeList Attrs) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000350 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000351 PAL = PAL.removeAttributes(getContext(), i, Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000352 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000353}
354
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000355void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000356 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000357 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
358 setAttributes(PAL);
359}
360
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000361void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000362 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000363 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
364 setAttributes(PAL);
365}
366
Mehdi Amini599ebf22016-01-08 02:28:20 +0000367const std::string &Function::getGC() const {
Gordon Henriksend930f912008-08-17 18:44:35 +0000368 assert(hasGC() && "Function has no collector");
Mehdi Amini599ebf22016-01-08 02:28:20 +0000369 return getContext().getGC(*this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000370}
371
Benjamin Kramer728f4442016-05-29 10:46:35 +0000372void Function::setGC(std::string Str) {
Mehdi Amini599ebf22016-01-08 02:28:20 +0000373 setValueSubclassDataBit(14, !Str.empty());
374 getContext().setGC(*this, std::move(Str));
Gordon Henriksen71183b62007-12-10 03:18:06 +0000375}
376
Gordon Henriksend930f912008-08-17 18:44:35 +0000377void Function::clearGC() {
Mehdi Amini599ebf22016-01-08 02:28:20 +0000378 if (!hasGC())
379 return;
380 getContext().deleteGC(*this);
381 setValueSubclassDataBit(14, false);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000382}
383
Rafael Espindola769efe62015-12-02 20:03:17 +0000384/// Copy all additional attributes (those not needed to create a Function) from
385/// the Function Src to this one.
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000386void Function::copyAttributesFrom(const GlobalValue *Src) {
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000387 GlobalObject::copyAttributesFrom(Src);
Rafael Espindola769efe62015-12-02 20:03:17 +0000388 const Function *SrcF = dyn_cast<Function>(Src);
389 if (!SrcF)
390 return;
391
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000392 setCallingConv(SrcF->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000393 setAttributes(SrcF->getAttributes());
Gordon Henriksend930f912008-08-17 18:44:35 +0000394 if (SrcF->hasGC())
395 setGC(SrcF->getGC());
396 else
397 clearGC();
Vedant Kumar44dd9872015-12-19 08:48:43 +0000398 if (SrcF->hasPersonalityFn())
399 setPersonalityFn(SrcF->getPersonalityFn());
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000400 if (SrcF->hasPrefixData())
401 setPrefixData(SrcF->getPrefixData());
402 if (SrcF->hasPrologueData())
403 setPrologueData(SrcF->getPrologueData());
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000404}
405
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000406/// Table of string intrinsic names indexed by enum value.
407static const char * const IntrinsicNameTable[] = {
408 "not_intrinsic",
409#define GET_INTRINSIC_NAME_TABLE
410#include "llvm/IR/Intrinsics.gen"
411#undef GET_INTRINSIC_NAME_TABLE
412};
413
Justin Bogner92a8c612016-07-15 16:31:37 +0000414/// Table of per-target intrinsic name tables.
415#define GET_INTRINSIC_TARGET_DATA
416#include "llvm/IR/Intrinsics.gen"
417#undef GET_INTRINSIC_TARGET_DATA
418
419/// Find the segment of \c IntrinsicNameTable for intrinsics with the same
420/// target as \c Name, or the generic table if \c Name is not target specific.
421///
422/// Returns the relevant slice of \c IntrinsicNameTable
423static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
424 assert(Name.startswith("llvm."));
425
426 ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
427 // Drop "llvm." and take the first dotted component. That will be the target
428 // if this is target specific.
429 StringRef Target = Name.drop_front(5).split('.').first;
430 auto It = std::lower_bound(Targets.begin(), Targets.end(), Target,
431 [](const IntrinsicTargetInfo &TI,
432 StringRef Target) { return TI.Name < Target; });
433 // We've either found the target or just fall back to the generic set, which
434 // is always first.
435 const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
436 return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
437}
438
Pete Cooper21241132015-05-19 00:02:25 +0000439/// \brief This does the actual lookup of an intrinsic ID which
440/// matches the given function name.
Tim Northover6b3bd612016-07-29 20:32:59 +0000441Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
Justin Bogner92a8c612016-07-15 16:31:37 +0000442 ArrayRef<const char *> NameTable = findTargetSubtable(Name);
Reid Klecknerc2752da2016-01-26 22:33:19 +0000443 int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
Justin Bogner92a8c612016-07-15 16:31:37 +0000444 if (Idx == -1)
445 return Intrinsic::not_intrinsic;
446
447 // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
448 // an index into a sub-table.
449 int Adjust = NameTable.data() - IntrinsicNameTable;
450 Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
Pete Cooper21241132015-05-19 00:02:25 +0000451
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000452 // If the intrinsic is not overloaded, require an exact match. If it is
453 // overloaded, require a prefix match.
454 bool IsPrefixMatch = Name.size() > strlen(NameTable[Idx]);
455 return IsPrefixMatch == isOverloaded(ID) ? ID : Intrinsic::not_intrinsic;
Pete Cooper21241132015-05-19 00:02:25 +0000456}
457
Pete Coopera7c0c182015-05-19 00:24:26 +0000458void Function::recalculateIntrinsicID() {
Justin Lebar291abd32016-12-28 22:59:45 +0000459 StringRef Name = getName();
460 if (!Name.startswith("llvm.")) {
461 HasLLVMReservedName = false;
Pete Coopera7c0c182015-05-19 00:24:26 +0000462 IntID = Intrinsic::not_intrinsic;
463 return;
Michael Ilseman516d7032013-03-01 18:48:54 +0000464 }
Justin Lebar291abd32016-12-28 22:59:45 +0000465 HasLLVMReservedName = true;
466 IntID = lookupIntrinsicID(Name);
Michael Ilseman516d7032013-03-01 18:48:54 +0000467}
468
Philip Reames319c48e2014-11-12 00:21:51 +0000469/// Returns a stable mangling for the type specified for use in the name
Philip Reames059ecbf2014-11-24 23:24:24 +0000470/// mangling scheme used by 'any' types in intrinsic signatures. The mangling
471/// of named types is simply their name. Manglings for unnamed types consist
472/// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
473/// combined with the mangling of their component types. A vararg function
474/// type will have a suffix of 'vararg'. Since function types can contain
475/// other function types, we close a function type mangling with suffix 'f'
476/// which can't be confused with it's prefix. This ensures we don't have
477/// collisions between two unrelated function types. Otherwise, you might
478/// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.)
Philip Reames8d5d68f2015-01-14 23:05:17 +0000479/// Manglings of integers, floats, and vectors ('i', 'f', and 'v' prefix in most
480/// cases) fall back to the MVT codepath, where they could be mangled to
481/// 'x86mmx', for example; matching on derived types is not sufficient to mangle
482/// everything.
Philip Reames319c48e2014-11-12 00:21:51 +0000483static std::string getMangledTypeStr(Type* Ty) {
484 std::string Result;
485 if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
486 Result += "p" + llvm::utostr(PTyp->getAddressSpace()) +
487 getMangledTypeStr(PTyp->getElementType());
488 } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) {
489 Result += "a" + llvm::utostr(ATyp->getNumElements()) +
490 getMangledTypeStr(ATyp->getElementType());
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000491 } else if (StructType *STyp = dyn_cast<StructType>(Ty)) {
492 if (!STyp->isLiteral()) {
493 Result += "s_";
494 Result += STyp->getName();
495 } else {
496 Result += "sl_";
497 for (auto Elem : STyp->elements())
498 Result += getMangledTypeStr(Elem);
499 }
500 // Ensure nested structs are distinguishable.
501 Result += "s";
502 } else if (FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
Philip Reames319c48e2014-11-12 00:21:51 +0000503 Result += "f_" + getMangledTypeStr(FT->getReturnType());
504 for (size_t i = 0; i < FT->getNumParams(); i++)
505 Result += getMangledTypeStr(FT->getParamType(i));
506 if (FT->isVarArg())
507 Result += "vararg";
Philip Reames059ecbf2014-11-24 23:24:24 +0000508 // Ensure nested function types are distinguishable.
509 Result += "f";
Elena Demikhovsky1ca72e12015-11-19 07:17:16 +0000510 } else if (isa<VectorType>(Ty))
511 Result += "v" + utostr(Ty->getVectorNumElements()) +
512 getMangledTypeStr(Ty->getVectorElementType());
513 else if (Ty)
Philip Reames319c48e2014-11-12 00:21:51 +0000514 Result += EVT::getEVT(Ty).getEVTString();
515 return Result;
516}
517
Pete Coopera8db71e2016-08-18 18:30:54 +0000518StringRef Intrinsic::getName(ID id) {
519 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Pete Coopera5f8c722016-08-22 20:18:28 +0000520 assert(!isOverloaded(id) &&
521 "This version of getName does not support overloading");
Pete Coopera8db71e2016-08-18 18:30:54 +0000522 return IntrinsicNameTable[id];
523}
524
Benjamin Kramere6e19332011-07-14 17:45:39 +0000525std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000526 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Reid Kleckner0b5220d2016-01-26 02:06:41 +0000527 std::string Result(IntrinsicNameTable[id]);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000528 for (Type *Ty : Tys) {
529 Result += "." + getMangledTypeStr(Ty);
Mon P Wang2c839d42008-07-30 04:36:53 +0000530 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000531 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000532}
533
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000534
Chris Lattnerf39c2782012-05-27 18:28:35 +0000535/// IIT_Info - These are enumerators that describe the entries returned by the
536/// getIntrinsicInfoTableEntries function.
537///
538/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
539enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000540 // Common values should be encoded with 0-15.
Chris Lattnerf39c2782012-05-27 18:28:35 +0000541 IIT_Done = 0,
542 IIT_I1 = 1,
543 IIT_I8 = 2,
544 IIT_I16 = 3,
545 IIT_I32 = 4,
546 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000547 IIT_F16 = 6,
548 IIT_F32 = 7,
549 IIT_F64 = 8,
550 IIT_V2 = 9,
551 IIT_V4 = 10,
552 IIT_V8 = 11,
553 IIT_V16 = 12,
554 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000555 IIT_PTR = 14,
556 IIT_ARG = 15,
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000557
Robert Khasanov65c27562014-10-20 19:25:05 +0000558 // Values from 16+ are only encodable with the inefficient encoding.
559 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000560 IIT_MMX = 17,
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000561 IIT_TOKEN = 18,
562 IIT_METADATA = 19,
563 IIT_EMPTYSTRUCT = 20,
564 IIT_STRUCT2 = 21,
565 IIT_STRUCT3 = 22,
566 IIT_STRUCT4 = 23,
567 IIT_STRUCT5 = 24,
568 IIT_EXTEND_ARG = 25,
569 IIT_TRUNC_ARG = 26,
570 IIT_ANYPTR = 27,
571 IIT_V1 = 28,
572 IIT_VARARG = 29,
573 IIT_HALF_VEC_ARG = 30,
574 IIT_SAME_VEC_WIDTH_ARG = 31,
575 IIT_PTR_TO_ARG = 32,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000576 IIT_PTR_TO_ELT = 33,
577 IIT_VEC_OF_PTRS_TO_ELT = 34,
578 IIT_I128 = 35,
579 IIT_V512 = 36,
580 IIT_V1024 = 37
Chris Lattnerf39c2782012-05-27 18:28:35 +0000581};
582
583
584static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
585 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
Chris Lattnera57c7972012-05-17 05:13:57 +0000586 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000587 unsigned StructElts = 2;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000588 using namespace Intrinsic;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000589
Chris Lattnera57c7972012-05-17 05:13:57 +0000590 switch (Info) {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000591 case IIT_Done:
592 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
593 return;
Andrew Tricka2efd992013-10-31 17:18:11 +0000594 case IIT_VARARG:
595 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
596 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000597 case IIT_MMX:
598 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
599 return;
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000600 case IIT_TOKEN:
601 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
602 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000603 case IIT_METADATA:
604 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
605 return;
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000606 case IIT_F16:
607 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
608 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000609 case IIT_F32:
610 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
611 return;
612 case IIT_F64:
613 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
614 return;
615 case IIT_I1:
616 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
617 return;
618 case IIT_I8:
619 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
620 return;
621 case IIT_I16:
622 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
623 return;
624 case IIT_I32:
625 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
626 return;
627 case IIT_I64:
628 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
629 return;
Kit Barton66460332015-05-25 15:49:26 +0000630 case IIT_I128:
631 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
632 return;
Jiangning Liu63dc8402013-09-24 02:47:27 +0000633 case IIT_V1:
634 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
635 DecodeIITType(NextElt, Infos, OutputTable);
636 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000637 case IIT_V2:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000638 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
639 DecodeIITType(NextElt, Infos, OutputTable);
640 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000641 case IIT_V4:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000642 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
643 DecodeIITType(NextElt, Infos, OutputTable);
644 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000645 case IIT_V8:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000646 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
647 DecodeIITType(NextElt, Infos, OutputTable);
648 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000649 case IIT_V16:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000650 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
651 DecodeIITType(NextElt, Infos, OutputTable);
652 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000653 case IIT_V32:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000654 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
655 DecodeIITType(NextElt, Infos, OutputTable);
656 return;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000657 case IIT_V64:
658 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
659 DecodeIITType(NextElt, Infos, OutputTable);
660 return;
Krzysztof Parzyszekb8bb90b2015-11-24 16:28:14 +0000661 case IIT_V512:
662 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512));
663 DecodeIITType(NextElt, Infos, OutputTable);
664 return;
665 case IIT_V1024:
666 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024));
667 DecodeIITType(NextElt, Infos, OutputTable);
668 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000669 case IIT_PTR:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000670 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
671 DecodeIITType(NextElt, Infos, OutputTable);
672 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000673 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000674 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000675 Infos[NextElt++]));
676 DecodeIITType(NextElt, Infos, OutputTable);
677 return;
Pete Cooper243efd72012-05-21 23:21:28 +0000678 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000679 case IIT_ARG: {
680 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
681 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
682 return;
683 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000684 case IIT_EXTEND_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000685 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000686 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000687 ArgInfo));
688 return;
689 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000690 case IIT_TRUNC_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000691 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000692 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000693 ArgInfo));
694 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000695 }
Tim Northover4516de32014-03-29 07:04:54 +0000696 case IIT_HALF_VEC_ARG: {
697 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
698 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
699 ArgInfo));
700 return;
701 }
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000702 case IIT_SAME_VEC_WIDTH_ARG: {
703 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
704 OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
705 ArgInfo));
706 return;
707 }
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000708 case IIT_PTR_TO_ARG: {
709 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
710 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
711 ArgInfo));
712 return;
713 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000714 case IIT_PTR_TO_ELT: {
715 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
716 OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToElt, ArgInfo));
717 return;
718 }
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000719 case IIT_VEC_OF_PTRS_TO_ELT: {
720 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
721 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfPtrsToElt,
722 ArgInfo));
723 return;
724 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000725 case IIT_EMPTYSTRUCT:
726 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
727 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000728 case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH;
729 case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH;
730 case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000731 case IIT_STRUCT2: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000732 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
733
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000734 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattnerf39c2782012-05-27 18:28:35 +0000735 DecodeIITType(NextElt, Infos, OutputTable);
736 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000737 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000738 }
739 llvm_unreachable("unhandled");
740}
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000741
Chris Lattnerf39c2782012-05-27 18:28:35 +0000742
743#define GET_INTRINSIC_GENERATOR_GLOBAL
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000744#include "llvm/IR/Intrinsics.gen"
Chris Lattnerf39c2782012-05-27 18:28:35 +0000745#undef GET_INTRINSIC_GENERATOR_GLOBAL
746
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000747void Intrinsic::getIntrinsicInfoTableEntries(ID id,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000748 SmallVectorImpl<IITDescriptor> &T){
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000749 // Check to see if the intrinsic's type was expressible by the table.
750 unsigned TableVal = IIT_Table[id-1];
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000751
Chris Lattnera3b0f522012-05-17 15:55:41 +0000752 // Decode the TableVal into an array of IITValues.
753 SmallVector<unsigned char, 8> IITValues;
754 ArrayRef<unsigned char> IITEntries;
755 unsigned NextElt = 0;
756 if ((TableVal >> 31) != 0) {
757 // This is an offset into the IIT_LongEncodingTable.
758 IITEntries = IIT_LongEncodingTable;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000759
Chris Lattnera3b0f522012-05-17 15:55:41 +0000760 // Strip sentinel bit.
761 NextElt = (TableVal << 1) >> 1;
762 } else {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000763 // Decode the TableVal into an array of IITValues. If the entry was encoded
764 // into a single word in the table itself, decode it now.
Chris Lattnera57c7972012-05-17 05:13:57 +0000765 do {
766 IITValues.push_back(TableVal & 0xF);
767 TableVal >>= 4;
768 } while (TableVal);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000769
Chris Lattnera3b0f522012-05-17 15:55:41 +0000770 IITEntries = IITValues;
771 NextElt = 0;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000772 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000773
774 // Okay, decode the table into the output vector of IITDescriptors.
775 DecodeIITType(NextElt, IITEntries, T);
776 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
777 DecodeIITType(NextElt, IITEntries, T);
778}
779
780
781static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
782 ArrayRef<Type*> Tys, LLVMContext &Context) {
783 using namespace Intrinsic;
784 IITDescriptor D = Infos.front();
785 Infos = Infos.slice(1);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000786
Chris Lattnerf39c2782012-05-27 18:28:35 +0000787 switch (D.Kind) {
788 case IITDescriptor::Void: return Type::getVoidTy(Context);
Andrew Tricka2efd992013-10-31 17:18:11 +0000789 case IITDescriptor::VarArg: return Type::getVoidTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000790 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000791 case IITDescriptor::Token: return Type::getTokenTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000792 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000793 case IITDescriptor::Half: return Type::getHalfTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000794 case IITDescriptor::Float: return Type::getFloatTy(Context);
795 case IITDescriptor::Double: return Type::getDoubleTy(Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000796
Chris Lattnerf39c2782012-05-27 18:28:35 +0000797 case IITDescriptor::Integer:
798 return IntegerType::get(Context, D.Integer_Width);
799 case IITDescriptor::Vector:
800 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
801 case IITDescriptor::Pointer:
802 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
803 D.Pointer_AddressSpace);
804 case IITDescriptor::Struct: {
805 Type *Elts[5];
806 assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
807 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
808 Elts[i] = DecodeFixedType(Infos, Tys, Context);
Craig Toppere1d12942014-08-27 05:25:25 +0000809 return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements));
Chris Lattnerf39c2782012-05-27 18:28:35 +0000810 }
811
812 case IITDescriptor::Argument:
813 return Tys[D.getArgumentNumber()];
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000814 case IITDescriptor::ExtendArgument: {
815 Type *Ty = Tys[D.getArgumentNumber()];
816 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
817 return VectorType::getExtendedElementVectorType(VTy);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000818
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000819 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
820 }
821 case IITDescriptor::TruncArgument: {
822 Type *Ty = Tys[D.getArgumentNumber()];
823 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
824 return VectorType::getTruncatedElementVectorType(VTy);
825
826 IntegerType *ITy = cast<IntegerType>(Ty);
827 assert(ITy->getBitWidth() % 2 == 0);
828 return IntegerType::get(Context, ITy->getBitWidth() / 2);
829 }
Tim Northover4516de32014-03-29 07:04:54 +0000830 case IITDescriptor::HalfVecArgument:
831 return VectorType::getHalfElementsVectorType(cast<VectorType>(
832 Tys[D.getArgumentNumber()]));
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000833 case IITDescriptor::SameVecWidthArgument: {
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000834 Type *EltTy = DecodeFixedType(Infos, Tys, Context);
835 Type *Ty = Tys[D.getArgumentNumber()];
836 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
837 return VectorType::get(EltTy, VTy->getNumElements());
838 }
839 llvm_unreachable("unhandled");
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000840 }
841 case IITDescriptor::PtrToArgument: {
842 Type *Ty = Tys[D.getArgumentNumber()];
843 return PointerType::getUnqual(Ty);
844 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000845 case IITDescriptor::PtrToElt: {
846 Type *Ty = Tys[D.getArgumentNumber()];
847 VectorType *VTy = dyn_cast<VectorType>(Ty);
848 if (!VTy)
849 llvm_unreachable("Expected an argument of Vector Type");
850 Type *EltTy = VTy->getVectorElementType();
851 return PointerType::getUnqual(EltTy);
852 }
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000853 case IITDescriptor::VecOfPtrsToElt: {
854 Type *Ty = Tys[D.getArgumentNumber()];
855 VectorType *VTy = dyn_cast<VectorType>(Ty);
856 if (!VTy)
857 llvm_unreachable("Expected an argument of Vector Type");
858 Type *EltTy = VTy->getVectorElementType();
859 return VectorType::get(PointerType::getUnqual(EltTy),
860 VTy->getNumElements());
861 }
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000862 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000863 llvm_unreachable("unhandled");
864}
865
866
867
868FunctionType *Intrinsic::getType(LLVMContext &Context,
869 ID id, ArrayRef<Type*> Tys) {
870 SmallVector<IITDescriptor, 8> Table;
871 getIntrinsicInfoTableEntries(id, Table);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000872
Chris Lattnerf39c2782012-05-27 18:28:35 +0000873 ArrayRef<IITDescriptor> TableRef = Table;
874 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000875
Chris Lattnera3b0f522012-05-17 15:55:41 +0000876 SmallVector<Type*, 8> ArgTys;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000877 while (!TableRef.empty())
878 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
Jim Laskey2682ea62007-02-07 20:38:26 +0000879
Steven Wud05affc2014-10-20 15:47:24 +0000880 // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
881 // If we see void type as the type of the last argument, it is vararg intrinsic
882 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
883 ArgTys.pop_back();
884 return FunctionType::get(ResultTy, ArgTys, true);
885 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000886 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey2682ea62007-02-07 20:38:26 +0000887}
888
Mon P Wangb4024932009-02-24 23:17:49 +0000889bool Intrinsic::isOverloaded(ID id) {
Mon P Wangb4024932009-02-24 23:17:49 +0000890#define GET_INTRINSIC_OVERLOAD_TABLE
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000891#include "llvm/IR/Intrinsics.gen"
Mon P Wangb4024932009-02-24 23:17:49 +0000892#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wangb4024932009-02-24 23:17:49 +0000893}
894
Sanjoy Dasc65d43e2015-06-18 19:28:26 +0000895bool Intrinsic::isLeaf(ID id) {
896 switch (id) {
897 default:
898 return true;
899
900 case Intrinsic::experimental_gc_statepoint:
901 case Intrinsic::experimental_patchpoint_void:
902 case Intrinsic::experimental_patchpoint_i64:
903 return false;
904 }
905}
906
Chris Lattner49b7ee12009-01-12 01:18:58 +0000907/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000908#define GET_INTRINSIC_ATTRIBUTES
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000909#include "llvm/IR/Intrinsics.gen"
Duncan Sands38ef3a82007-12-03 20:06:50 +0000910#undef GET_INTRINSIC_ATTRIBUTES
911
Benjamin Kramere6e19332011-07-14 17:45:39 +0000912Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000913 // There can never be multiple globals with the same name of different types,
914 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000915 return
Benjamin Kramere6e19332011-07-14 17:45:39 +0000916 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
917 getType(M->getContext(), id, Tys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000918}
919
Dale Johannesenb842d522009-02-05 01:49:45 +0000920// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
921#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000922#include "llvm/IR/Intrinsics.gen"
Dale Johannesenb842d522009-02-05 01:49:45 +0000923#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
924
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000925// This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
926#define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
927#include "llvm/IR/Intrinsics.gen"
928#undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
929
Artur Pilipenkobc552272016-06-22 14:56:33 +0000930bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
931 SmallVectorImpl<Type*> &ArgTys) {
932 using namespace Intrinsic;
933
934 // If we ran out of descriptors, there are too many arguments.
935 if (Infos.empty()) return true;
936 IITDescriptor D = Infos.front();
937 Infos = Infos.slice(1);
938
939 switch (D.Kind) {
940 case IITDescriptor::Void: return !Ty->isVoidTy();
941 case IITDescriptor::VarArg: return true;
942 case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
943 case IITDescriptor::Token: return !Ty->isTokenTy();
944 case IITDescriptor::Metadata: return !Ty->isMetadataTy();
945 case IITDescriptor::Half: return !Ty->isHalfTy();
946 case IITDescriptor::Float: return !Ty->isFloatTy();
947 case IITDescriptor::Double: return !Ty->isDoubleTy();
948 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
949 case IITDescriptor::Vector: {
950 VectorType *VT = dyn_cast<VectorType>(Ty);
951 return !VT || VT->getNumElements() != D.Vector_Width ||
952 matchIntrinsicType(VT->getElementType(), Infos, ArgTys);
953 }
954 case IITDescriptor::Pointer: {
955 PointerType *PT = dyn_cast<PointerType>(Ty);
956 return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace ||
957 matchIntrinsicType(PT->getElementType(), Infos, ArgTys);
958 }
959
960 case IITDescriptor::Struct: {
961 StructType *ST = dyn_cast<StructType>(Ty);
962 if (!ST || ST->getNumElements() != D.Struct_NumElements)
963 return true;
964
965 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
966 if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys))
967 return true;
968 return false;
969 }
970
971 case IITDescriptor::Argument:
972 // Two cases here - If this is the second occurrence of an argument, verify
973 // that the later instance matches the previous instance.
974 if (D.getArgumentNumber() < ArgTys.size())
975 return Ty != ArgTys[D.getArgumentNumber()];
976
977 // Otherwise, if this is the first instance of an argument, record it and
978 // verify the "Any" kind.
979 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
980 ArgTys.push_back(Ty);
981
982 switch (D.getArgumentKind()) {
983 case IITDescriptor::AK_Any: return false; // Success
984 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
985 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
986 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
987 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
988 }
989 llvm_unreachable("all argument kinds not covered");
990
991 case IITDescriptor::ExtendArgument: {
992 // This may only be used when referring to a previous vector argument.
993 if (D.getArgumentNumber() >= ArgTys.size())
994 return true;
995
996 Type *NewTy = ArgTys[D.getArgumentNumber()];
997 if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
998 NewTy = VectorType::getExtendedElementVectorType(VTy);
999 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1000 NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
1001 else
1002 return true;
1003
1004 return Ty != NewTy;
1005 }
1006 case IITDescriptor::TruncArgument: {
1007 // This may only be used when referring to a previous vector argument.
1008 if (D.getArgumentNumber() >= ArgTys.size())
1009 return true;
1010
1011 Type *NewTy = ArgTys[D.getArgumentNumber()];
1012 if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
1013 NewTy = VectorType::getTruncatedElementVectorType(VTy);
1014 else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
1015 NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
1016 else
1017 return true;
1018
1019 return Ty != NewTy;
1020 }
1021 case IITDescriptor::HalfVecArgument:
1022 // This may only be used when referring to a previous vector argument.
1023 return D.getArgumentNumber() >= ArgTys.size() ||
1024 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1025 VectorType::getHalfElementsVectorType(
1026 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1027 case IITDescriptor::SameVecWidthArgument: {
1028 if (D.getArgumentNumber() >= ArgTys.size())
1029 return true;
1030 VectorType * ReferenceType =
Elena Demikhovskycaaceef2016-11-03 03:23:55 +00001031 dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
Artur Pilipenkobc552272016-06-22 14:56:33 +00001032 VectorType *ThisArgType = dyn_cast<VectorType>(Ty);
1033 if (!ThisArgType || !ReferenceType ||
1034 (ReferenceType->getVectorNumElements() !=
1035 ThisArgType->getVectorNumElements()))
1036 return true;
1037 return matchIntrinsicType(ThisArgType->getVectorElementType(),
1038 Infos, ArgTys);
1039 }
1040 case IITDescriptor::PtrToArgument: {
1041 if (D.getArgumentNumber() >= ArgTys.size())
1042 return true;
1043 Type * ReferenceType = ArgTys[D.getArgumentNumber()];
1044 PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1045 return (!ThisArgType || ThisArgType->getElementType() != ReferenceType);
1046 }
Elena Demikhovskycaaceef2016-11-03 03:23:55 +00001047 case IITDescriptor::PtrToElt: {
1048 if (D.getArgumentNumber() >= ArgTys.size())
1049 return true;
1050 VectorType * ReferenceType =
1051 dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
1052 PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
1053
1054 return (!ThisArgType || !ReferenceType ||
1055 ThisArgType->getElementType() != ReferenceType->getElementType());
1056 }
Artur Pilipenkobc552272016-06-22 14:56:33 +00001057 case IITDescriptor::VecOfPtrsToElt: {
1058 if (D.getArgumentNumber() >= ArgTys.size())
1059 return true;
1060 VectorType * ReferenceType =
1061 dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
1062 VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty);
1063 if (!ThisArgVecTy || !ReferenceType ||
1064 (ReferenceType->getVectorNumElements() !=
1065 ThisArgVecTy->getVectorNumElements()))
1066 return true;
1067 PointerType *ThisArgEltTy =
1068 dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType());
1069 if (!ThisArgEltTy)
1070 return true;
1071 return ThisArgEltTy->getElementType() !=
1072 ReferenceType->getVectorElementType();
1073 }
1074 }
1075 llvm_unreachable("unhandled");
1076}
1077
Artur Pilipenkob68b8212016-06-24 14:47:27 +00001078bool
1079Intrinsic::matchIntrinsicVarArg(bool isVarArg,
1080 ArrayRef<Intrinsic::IITDescriptor> &Infos) {
1081 // If there are no descriptors left, then it can't be a vararg.
1082 if (Infos.empty())
1083 return isVarArg;
1084
1085 // There should be only one descriptor remaining at this point.
1086 if (Infos.size() != 1)
1087 return true;
1088
1089 // Check and verify the descriptor.
1090 IITDescriptor D = Infos.front();
1091 Infos = Infos.slice(1);
1092 if (D.Kind == IITDescriptor::VarArg)
1093 return !isVarArg;
1094
1095 return true;
1096}
1097
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +00001098Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) {
1099 Intrinsic::ID ID = F->getIntrinsicID();
1100 if (!ID)
1101 return None;
1102
1103 FunctionType *FTy = F->getFunctionType();
1104 // Accumulate an array of overloaded types for the given intrinsic
1105 SmallVector<Type *, 4> ArgTys;
1106 {
1107 SmallVector<Intrinsic::IITDescriptor, 8> Table;
1108 getIntrinsicInfoTableEntries(ID, Table);
1109 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
1110
1111 // If we encounter any problems matching the signature with the descriptor
1112 // just give up remangling. It's up to verifier to report the discrepancy.
1113 if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
1114 return None;
1115 for (auto Ty : FTy->params())
1116 if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
1117 return None;
1118 if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
1119 return None;
1120 }
1121
1122 StringRef Name = F->getName();
1123 if (Name == Intrinsic::getName(ID, ArgTys))
1124 return None;
1125
1126 auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1127 NewDecl->setCallingConv(F->getCallingConv());
1128 assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
1129 return NewDecl;
1130}
1131
Gabor Greif161cb042010-03-23 14:40:20 +00001132/// hasAddressTaken - returns true if there are any uses of this function
1133/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001134bool Function::hasAddressTaken(const User* *PutOffender) const {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001135 for (const Use &U : uses()) {
1136 const User *FU = U.getUser();
1137 if (isa<BlockAddress>(FU))
Jay Foadca0c4992012-05-12 08:30:16 +00001138 continue;
Richard Trieu7a083812016-02-18 22:09:30 +00001139 if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU)) {
1140 if (PutOffender)
1141 *PutOffender = FU;
1142 return true;
1143 }
Chandler Carruthcdf47882014-03-09 03:16:01 +00001144 ImmutableCallSite CS(cast<Instruction>(FU));
Richard Trieu7a083812016-02-18 22:09:30 +00001145 if (!CS.isCallee(&U)) {
1146 if (PutOffender)
1147 *PutOffender = FU;
1148 return true;
1149 }
Jay Foad557169d2009-06-10 08:41:11 +00001150 }
1151 return false;
1152}
1153
Eli Friedman1923a332011-10-20 05:23:42 +00001154bool Function::isDefTriviallyDead() const {
1155 // Check the linkage
1156 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
1157 !hasAvailableExternallyLinkage())
1158 return false;
1159
1160 // Check if the function is used by anything other than a blockaddress.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001161 for (const User *U : users())
1162 if (!isa<BlockAddress>(U))
Eli Friedman1923a332011-10-20 05:23:42 +00001163 return false;
1164
1165 return true;
1166}
1167
Bill Wendling63a4ea12011-10-17 18:43:40 +00001168/// callsFunctionThatReturnsTwice - Return true if the function has a call to
1169/// setjmp or other function that gcc recognizes as "returning twice".
1170bool Function::callsFunctionThatReturnsTwice() const {
1171 for (const_inst_iterator
1172 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
Mark Seaborn82711182014-01-14 04:20:01 +00001173 ImmutableCallSite CS(&*I);
1174 if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
Bill Wendling63a4ea12011-10-17 18:43:40 +00001175 return true;
1176 }
1177
1178 return false;
1179}
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001180
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001181Constant *Function::getPersonalityFn() const {
1182 assert(hasPersonalityFn() && getNumOperands());
1183 return cast<Constant>(Op<0>());
Vedant Kumar1ab5ea52015-10-06 20:31:57 +00001184}
1185
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001186void Function::setPersonalityFn(Constant *Fn) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001187 setHungoffOperand<0>(Fn);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001188 setValueSubclassDataBit(3, Fn != nullptr);
Vedant Kumar1ab5ea52015-10-06 20:31:57 +00001189}
1190
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001191Constant *Function::getPrefixData() const {
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001192 assert(hasPrefixData() && getNumOperands());
1193 return cast<Constant>(Op<1>());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001194}
1195
1196void Function::setPrefixData(Constant *PrefixData) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001197 setHungoffOperand<1>(PrefixData);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001198 setValueSubclassDataBit(1, PrefixData != nullptr);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001199}
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001200
1201Constant *Function::getPrologueData() const {
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001202 assert(hasPrologueData() && getNumOperands());
1203 return cast<Constant>(Op<2>());
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001204}
1205
1206void Function::setPrologueData(Constant *PrologueData) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001207 setHungoffOperand<2>(PrologueData);
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001208 setValueSubclassDataBit(2, PrologueData != nullptr);
1209}
1210
1211void Function::allocHungoffUselist() {
1212 // If we've already allocated a uselist, stop here.
1213 if (getNumOperands())
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001214 return;
1215
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001216 allocHungoffUses(3, /*IsPhi=*/ false);
1217 setNumHungOffUseOperands(3);
1218
1219 // Initialize the uselist with placeholder operands to allow traversal.
1220 auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
1221 Op<0>().set(CPN);
1222 Op<1>().set(CPN);
1223 Op<2>().set(CPN);
1224}
1225
1226template <int Idx>
1227void Function::setHungoffOperand(Constant *C) {
Keno Fischer9bc46b12015-12-23 18:27:23 +00001228 if (C) {
1229 allocHungoffUselist();
1230 Op<Idx>().set(C);
1231 } else if (getNumOperands()) {
1232 Op<Idx>().set(
1233 ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
1234 }
Vedant Kumar3a63fb32015-12-19 08:52:49 +00001235}
1236
1237void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
1238 assert(Bit < 16 && "SubclassData contains only 16 bits");
1239 if (On)
1240 setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
1241 else
1242 setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
Peter Collingbourne51d2de72014-12-03 02:08:38 +00001243}
Akira Hatanaka3058d0f2015-05-06 23:54:14 +00001244
Dehao Chena60cdd32017-02-28 18:09:44 +00001245void Function::setEntryCount(uint64_t Count,
1246 const DenseSet<GlobalValue::GUID> *S) {
Diego Novillo2567f3d2015-05-13 15:13:45 +00001247 MDBuilder MDB(getContext());
Dehao Chena60cdd32017-02-28 18:09:44 +00001248 setMetadata(LLVMContext::MD_prof, MDB.createFunctionEntryCount(Count, S));
Diego Novillo2567f3d2015-05-13 15:13:45 +00001249}
1250
1251Optional<uint64_t> Function::getEntryCount() const {
1252 MDNode *MD = getMetadata(LLVMContext::MD_prof);
1253 if (MD && MD->getOperand(0))
1254 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
1255 if (MDS->getString().equals("function_entry_count")) {
1256 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
David Callahanc1c810d2016-08-05 18:38:19 +00001257 uint64_t Count = CI->getValue().getZExtValue();
1258 if (Count == 0)
1259 return None;
1260 return Count;
Diego Novillo2567f3d2015-05-13 15:13:45 +00001261 }
1262 return None;
1263}
Dehao Chen302b69c2016-10-18 20:42:47 +00001264
Dehao Chena60cdd32017-02-28 18:09:44 +00001265DenseSet<GlobalValue::GUID> Function::getImportGUIDs() const {
1266 DenseSet<GlobalValue::GUID> R;
1267 if (MDNode *MD = getMetadata(LLVMContext::MD_prof))
1268 if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
1269 if (MDS->getString().equals("function_entry_count"))
1270 for (unsigned i = 2; i < MD->getNumOperands(); i++)
1271 R.insert(mdconst::extract<ConstantInt>(MD->getOperand(i))
1272 ->getValue()
1273 .getZExtValue());
1274 return R;
1275}
1276
Dehao Chen302b69c2016-10-18 20:42:47 +00001277void Function::setSectionPrefix(StringRef Prefix) {
1278 MDBuilder MDB(getContext());
1279 setMetadata(LLVMContext::MD_section_prefix,
1280 MDB.createFunctionSectionPrefix(Prefix));
1281}
1282
1283Optional<StringRef> Function::getSectionPrefix() const {
1284 if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
1285 assert(dyn_cast<MDString>(MD->getOperand(0))
1286 ->getString()
1287 .equals("function_section_prefix") &&
1288 "Metadata not match");
1289 return dyn_cast<MDString>(MD->getOperand(1))->getString();
1290 }
1291 return None;
1292}