blob: 8a69e39ee329c5fc8687b5e5a4a3a6f51d8c95e9 [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"
Gordon Henriksen71183b62007-12-10 03:18:06 +000017#include "llvm/ADT/DenseMap.h"
Rafael Espindola2050af82011-05-16 03:05:33 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000021#include "llvm/IR/CallSite.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"
Chandler Carruth4b6845c2014-03-04 12:46:06 +000026#include "llvm/IR/LeakDetector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/RWMutex.h"
30#include "llvm/Support/StringPool.h"
31#include "llvm/Support/Threading.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner113f4f42002-06-25 16:13:24 +000034// Explicit instantiations of SymbolTableListTraits since some of the methods
35// are not in the public header file...
John McCall086bb4e2009-12-19 00:55:12 +000036template class llvm::SymbolTableListTraits<Argument, Function>;
37template class llvm::SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000038
Chris Lattnerda975502001-09-10 07:58:01 +000039//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000040// Argument Implementation
41//===----------------------------------------------------------------------===//
42
David Blaikiea379b1812011-12-20 02:50:00 +000043void Argument::anchor() { }
44
Chris Lattner229907c2011-07-18 04:54:35 +000045Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000046 : Value(Ty, Value::ArgumentVal) {
Craig Topperc6207612014-04-09 06:08:46 +000047 Parent = nullptr;
Chris Lattner184b2982002-09-08 18:59:35 +000048
49 // Make sure that we get added to a function
50 LeakDetector::addGarbageObject(this);
51
Chris Lattner9ed7aef2002-09-06 21:33:15 +000052 if (Par)
53 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000054 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000055}
56
Chris Lattner9ed7aef2002-09-06 21:33:15 +000057void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000058 if (getParent())
59 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000060 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000061 if (getParent())
62 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000063}
64
Chris Lattnere30f09d2008-01-24 17:47:11 +000065/// getArgNo - Return the index of this formal argument in its containing
Michael Ilsemanacdb76d2012-12-17 20:37:55 +000066/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
Chris Lattnere30f09d2008-01-24 17:47:11 +000067unsigned Argument::getArgNo() const {
68 const Function *F = getParent();
69 assert(F && "Argument is not in a function");
Michael Ilsemanacdb76d2012-12-17 20:37:55 +000070
Chris Lattnere30f09d2008-01-24 17:47:11 +000071 Function::const_arg_iterator AI = F->arg_begin();
72 unsigned ArgIdx = 0;
73 for (; &*AI != this; ++AI)
74 ++ArgIdx;
75
76 return ArgIdx;
77}
78
Nick Lewyckyd52b1522014-05-20 01:23:40 +000079/// hasNonNullAttr - Return true if this argument has the nonnull attribute on
Hal Finkelb0407ba2014-07-18 15:51:28 +000080/// it in its containing function. Also returns true if at least one byte is
81/// known to be dereferenceable and the pointer is in addrspace(0).
Nick Lewyckyd52b1522014-05-20 01:23:40 +000082bool Argument::hasNonNullAttr() const {
83 if (!getType()->isPointerTy()) return false;
Hal Finkelb0407ba2014-07-18 15:51:28 +000084 if (getParent()->getAttributes().
85 hasAttribute(getArgNo()+1, Attribute::NonNull))
86 return true;
87 else if (getDereferenceableBytes() > 0 &&
88 getType()->getPointerAddressSpace() == 0)
89 return true;
90 return false;
Nick Lewyckyd52b1522014-05-20 01:23:40 +000091}
92
Chris Lattnere30f09d2008-01-24 17:47:11 +000093/// hasByValAttr - Return true if this argument has the byval attribute on it
94/// in its containing function.
95bool Argument::hasByValAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +000096 if (!getType()->isPointerTy()) return false;
Bill Wendling94dcaf82012-12-30 12:45:13 +000097 return getParent()->getAttributes().
98 hasAttribute(getArgNo()+1, Attribute::ByVal);
Chris Lattnere30f09d2008-01-24 17:47:11 +000099}
100
Reid Klecknera534a382013-12-19 02:14:12 +0000101/// \brief Return true if this argument has the inalloca attribute on it in
102/// its containing function.
103bool Argument::hasInAllocaAttr() const {
104 if (!getType()->isPointerTy()) return false;
105 return getParent()->getAttributes().
106 hasAttribute(getArgNo()+1, Attribute::InAlloca);
107}
108
Reid Kleckner436c42e2014-01-17 23:58:17 +0000109bool Argument::hasByValOrInAllocaAttr() const {
110 if (!getType()->isPointerTy()) return false;
111 AttributeSet Attrs = getParent()->getAttributes();
112 return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) ||
113 Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca);
114}
115
Chris Lattner4d37d992011-05-22 23:57:23 +0000116unsigned Argument::getParamAlignment() const {
117 assert(getType()->isPointerTy() && "Only pointers have alignments");
118 return getParent()->getParamAlignment(getArgNo()+1);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000119
Chris Lattner4d37d992011-05-22 23:57:23 +0000120}
121
Hal Finkelb0407ba2014-07-18 15:51:28 +0000122uint64_t Argument::getDereferenceableBytes() const {
123 assert(getType()->isPointerTy() &&
124 "Only pointers have dereferenceable bytes");
125 return getParent()->getDereferenceableBytes(getArgNo()+1);
126}
127
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000128/// hasNestAttr - Return true if this argument has the nest attribute on
129/// it in its containing function.
130bool Argument::hasNestAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000131 if (!getType()->isPointerTy()) return false;
Bill Wendling94dcaf82012-12-30 12:45:13 +0000132 return getParent()->getAttributes().
133 hasAttribute(getArgNo()+1, Attribute::Nest);
Duncan Sands5d96f3f2009-12-11 08:36:17 +0000134}
135
Chris Lattnere30f09d2008-01-24 17:47:11 +0000136/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
137/// it in its containing function.
138bool Argument::hasNoAliasAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000139 if (!getType()->isPointerTy()) return false;
Bill Wendling94dcaf82012-12-30 12:45:13 +0000140 return getParent()->getAttributes().
141 hasAttribute(getArgNo()+1, Attribute::NoAlias);
Chris Lattnere30f09d2008-01-24 17:47:11 +0000142}
143
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000144/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
145/// on it in its containing function.
146bool Argument::hasNoCaptureAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000147 if (!getType()->isPointerTy()) return false;
Bill Wendling94dcaf82012-12-30 12:45:13 +0000148 return getParent()->getAttributes().
149 hasAttribute(getArgNo()+1, Attribute::NoCapture);
Duncan Sandsdf128eb2008-12-31 18:08:59 +0000150}
151
Owen Andersonc64dfb42008-02-17 23:22:28 +0000152/// hasSRetAttr - Return true if this argument has the sret attribute on
153/// it in its containing function.
154bool Argument::hasStructRetAttr() const {
Duncan Sands19d0b472010-02-16 11:11:14 +0000155 if (!getType()->isPointerTy()) return false;
Gabor Greif697e94c2008-05-15 10:04:30 +0000156 if (this != getParent()->arg_begin())
157 return false; // StructRet param must be first param
Bill Wendling94dcaf82012-12-30 12:45:13 +0000158 return getParent()->getAttributes().
159 hasAttribute(1, Attribute::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000160}
161
Stephen Linb8bd2322013-04-20 05:14:40 +0000162/// hasReturnedAttr - Return true if this argument has the returned attribute on
163/// it in its containing function.
164bool Argument::hasReturnedAttr() const {
165 return getParent()->getAttributes().
166 hasAttribute(getArgNo()+1, Attribute::Returned);
167}
168
Juergen Ributzka384c3b52014-08-05 05:43:41 +0000169/// hasZExtAttr - Return true if this argument has the zext attribute on it in
170/// its containing function.
171bool Argument::hasZExtAttr() const {
172 return getParent()->getAttributes().
173 hasAttribute(getArgNo()+1, Attribute::ZExt);
174}
175
176/// hasSExtAttr Return true if this argument has the sext attribute on it in its
177/// containing function.
178bool Argument::hasSExtAttr() const {
179 return getParent()->getAttributes().
180 hasAttribute(getArgNo()+1, Attribute::SExt);
181}
182
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000183/// Return true if this argument has the readonly or readnone attribute on it
184/// in its containing function.
185bool Argument::onlyReadsMemory() const {
186 return getParent()->getAttributes().
187 hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
188 getParent()->getAttributes().
189 hasAttribute(getArgNo()+1, Attribute::ReadNone);
190}
191
Bill Wendling49bc76c2013-01-23 06:14:59 +0000192/// addAttr - Add attributes to an argument.
193void Argument::addAttr(AttributeSet AS) {
Bill Wendlingefbbbfd2013-02-21 19:46:51 +0000194 assert(AS.getNumSlots() <= 1 &&
Bill Wendling1c20ff02013-02-20 23:04:11 +0000195 "Trying to add more than one attribute set to an argument!");
196 AttrBuilder B(AS, AS.getSlotIndex(0));
197 getParent()->addAttributes(getArgNo() + 1,
198 AttributeSet::get(Parent->getContext(),
199 getArgNo() + 1, B));
Gordon Henriksen2d9cc212008-04-28 17:37:06 +0000200}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000201
Bill Wendling49bc76c2013-01-23 06:14:59 +0000202/// removeAttr - Remove attributes from an argument.
203void Argument::removeAttr(AttributeSet AS) {
Bill Wendlingefbbbfd2013-02-21 19:46:51 +0000204 assert(AS.getNumSlots() <= 1 &&
Bill Wendling1c20ff02013-02-20 23:04:11 +0000205 "Trying to remove more than one attribute set from an argument!");
206 AttrBuilder B(AS, AS.getSlotIndex(0));
207 getParent()->removeAttributes(getArgNo() + 1,
208 AttributeSet::get(Parent->getContext(),
209 getArgNo() + 1, B));
Duncan Sands66336db2008-07-08 09:41:30 +0000210}
Chris Lattnere30f09d2008-01-24 17:47:11 +0000211
Chris Lattnerd255ae22002-04-09 19:39:35 +0000212//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000213// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000214//===----------------------------------------------------------------------===//
215
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000216bool Function::isMaterializable() const {
217 return getGlobalObjectSubClassData();
218}
219
220void Function::setIsMaterializable(bool V) { setGlobalObjectSubClassData(V); }
221
Owen Anderson47db9412009-07-22 00:24:57 +0000222LLVMContext &Function::getContext() const {
223 return getType()->getContext();
Owen Anderson0a2c4582009-07-02 18:03:58 +0000224}
225
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000226FunctionType *Function::getFunctionType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000227 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000228}
229
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000230bool Function::isVarArg() const {
231 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000232}
233
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000234Type *Function::getReturnType() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000235 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000236}
237
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000238void Function::removeFromParent() {
239 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000240}
241
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000242void Function::eraseFromParent() {
243 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000244}
245
Reid Spencer019c8862007-04-09 15:01:12 +0000246//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000247// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000248//===----------------------------------------------------------------------===//
249
Rafael Espindola1b47a282014-10-23 15:20:05 +0000250Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name,
251 Module *ParentModule)
252 : GlobalObject(PointerType::getUnqual(Ty), Value::FunctionVal, nullptr, 0,
253 Linkage, name) {
Chris Lattner654695b2009-01-05 07:58:59 +0000254 assert(FunctionType::isValidReturnType(getReturnType()) &&
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000255 "invalid return type");
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000256 setIsMaterializable(false);
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000257 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000258
Chris Lattnere2de9082007-08-18 06:14:52 +0000259 // If the function has arguments, mark them as lazily built.
260 if (Ty->getNumParams())
Chris Lattnerb9c86512009-12-29 02:14:09 +0000261 setValueSubclassData(1); // Set the "has lazy arguments" bit.
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000262
Chris Lattner184b2982002-09-08 18:59:35 +0000263 // Make sure that we get added to a function
264 LeakDetector::addGarbageObject(this);
265
Chris Lattner6213ae02002-09-06 20:46:32 +0000266 if (ParentModule)
267 ParentModule->getFunctionList().push_back(this);
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000268
269 // Ensure intrinsics have the right parameter attributes.
Dale Johannesenb842d522009-02-05 01:49:45 +0000270 if (unsigned IID = getIntrinsicID())
Bill Wendlingd079a442012-10-15 04:46:55 +0000271 setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
Devang Pateld334a432008-09-02 20:51:15 +0000272
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273}
274
Gordon Henriksen14a55692007-12-10 02:14:30 +0000275Function::~Function() {
276 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277
Chris Lattner2f7c9632001-06-06 20:29:01 +0000278 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000279 ArgumentList.clear();
280 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000281
Gordon Henriksend930f912008-08-17 18:44:35 +0000282 // Remove the function from the on-the-side GC table.
283 clearGC();
Michael Ilseman516d7032013-03-01 18:48:54 +0000284
285 // Remove the intrinsicID from the Cache.
Chris Lattnerf83a6642013-03-20 21:04:53 +0000286 if (getValueName() && isIntrinsic())
Michael Ilseman516d7032013-03-01 18:48:54 +0000287 getContext().pImpl->IntrinsicIDCache.erase(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000288}
289
Chris Lattnere2de9082007-08-18 06:14:52 +0000290void Function::BuildLazyArguments() const {
291 // Create the arguments vector, all arguments start out unnamed.
Chris Lattner229907c2011-07-18 04:54:35 +0000292 FunctionType *FT = getFunctionType();
Chris Lattnere2de9082007-08-18 06:14:52 +0000293 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000294 assert(!FT->getParamType(i)->isVoidTy() &&
Chris Lattnere2de9082007-08-18 06:14:52 +0000295 "Cannot have void typed arguments!");
296 ArgumentList.push_back(new Argument(FT->getParamType(i)));
297 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000298
Chris Lattnere2de9082007-08-18 06:14:52 +0000299 // Clear the lazy arguments bit.
Chris Lattnerb9c86512009-12-29 02:14:09 +0000300 unsigned SDC = getSubclassDataFromValue();
301 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
Chris Lattnere2de9082007-08-18 06:14:52 +0000302}
303
304size_t Function::arg_size() const {
305 return getFunctionType()->getNumParams();
306}
307bool Function::arg_empty() const {
308 return getFunctionType()->getNumParams() == 0;
309}
310
Chris Lattner4e8c4872002-03-23 22:51:58 +0000311void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000312 if (getParent())
313 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000314 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000315 if (getParent())
316 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317}
318
Chris Lattner2f7c9632001-06-06 20:29:01 +0000319// dropAllReferences() - This function causes all the subinstructions to "let
320// go" of all references that they are maintaining. This allows one to
321// 'delete' a whole class at a time, even though there may be circular
322// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000323// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000324// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325// delete.
326//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000327void Function::dropAllReferences() {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000328 setIsMaterializable(false);
329
Chris Lattner113f4f42002-06-25 16:13:24 +0000330 for (iterator I = begin(), E = end(); I != E; ++I)
331 I->dropAllReferences();
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000332
Dan Gohmanf844b3b2010-12-07 19:56:51 +0000333 // Delete all basic blocks. They are now unused, except possibly by
334 // blockaddresses, but BasicBlock's destructor takes care of those.
335 while (!BasicBlocks.empty())
336 BasicBlocks.begin()->eraseFromParent();
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000337
338 // Prefix data is stored in a side table.
Craig Topperc6207612014-04-09 06:08:46 +0000339 setPrefixData(nullptr);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340}
Chris Lattnerda975502001-09-10 07:58:01 +0000341
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000342void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000343 AttributeSet PAL = getAttributes();
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000344 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000345 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000346}
347
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000348void Function::addAttributes(unsigned i, AttributeSet attrs) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000349 AttributeSet PAL = getAttributes();
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000350 PAL = PAL.addAttributes(getContext(), i, attrs);
351 setAttributes(PAL);
352}
353
Bill Wendling430fa9b2013-01-23 00:45:55 +0000354void Function::removeAttributes(unsigned i, AttributeSet attrs) {
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000355 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000356 PAL = PAL.removeAttributes(getContext(), i, attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000357 setAttributes(PAL);
Duncan Sands66336db2008-07-08 09:41:30 +0000358}
359
Gordon Henriksend930f912008-08-17 18:44:35 +0000360// Maintain the GC name for each function in an on-the-side table. This saves
361// allocating an additional word in Function for programs which do not use GC
362// (i.e., most programs) at the cost of increased overhead for clients which do
363// use GC.
Owen Andersoned14e762009-06-17 23:49:06 +0000364static DenseMap<const Function*,PooledStringPtr> *GCNames;
365static StringPool *GCNamePool;
Owen Anderson7f1ef672009-06-18 20:56:48 +0000366static ManagedStatic<sys::SmartRWMutex<true> > GCLock;
Gordon Henriksen71183b62007-12-10 03:18:06 +0000367
Gordon Henriksend930f912008-08-17 18:44:35 +0000368bool Function::hasGC() const {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000369 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000370 return GCNames && GCNames->count(this);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000371}
372
Gordon Henriksend930f912008-08-17 18:44:35 +0000373const char *Function::getGC() const {
374 assert(hasGC() && "Function has no collector");
Owen Anderson5c96ef72009-07-07 18:33:04 +0000375 sys::SmartScopedReader<true> Reader(*GCLock);
Owen Anderson7f1ef672009-06-18 20:56:48 +0000376 return *(*GCNames)[this];
Gordon Henriksen71183b62007-12-10 03:18:06 +0000377}
378
Gordon Henriksend930f912008-08-17 18:44:35 +0000379void Function::setGC(const char *Str) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000380 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000381 if (!GCNamePool)
382 GCNamePool = new StringPool();
383 if (!GCNames)
384 GCNames = new DenseMap<const Function*,PooledStringPtr>();
385 (*GCNames)[this] = GCNamePool->intern(Str);
Gordon Henriksen71183b62007-12-10 03:18:06 +0000386}
387
Gordon Henriksend930f912008-08-17 18:44:35 +0000388void Function::clearGC() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000389 sys::SmartScopedWriter<true> Writer(*GCLock);
Owen Andersoned14e762009-06-17 23:49:06 +0000390 if (GCNames) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000391 GCNames->erase(this);
Owen Andersoned14e762009-06-17 23:49:06 +0000392 if (GCNames->empty()) {
393 delete GCNames;
Craig Topperc6207612014-04-09 06:08:46 +0000394 GCNames = nullptr;
Owen Andersoned14e762009-06-17 23:49:06 +0000395 if (GCNamePool->empty()) {
396 delete GCNamePool;
Craig Topperc6207612014-04-09 06:08:46 +0000397 GCNamePool = nullptr;
Owen Andersoned14e762009-06-17 23:49:06 +0000398 }
399 }
400 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000401}
402
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000403/// copyAttributesFrom - copy all additional attributes (those not needed to
404/// create a Function) from the Function Src to this one.
405void Function::copyAttributesFrom(const GlobalValue *Src) {
406 assert(isa<Function>(Src) && "Expected a Function!");
Rafael Espindola99e05cf2014-05-13 18:45:48 +0000407 GlobalObject::copyAttributesFrom(Src);
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000408 const Function *SrcF = cast<Function>(Src);
409 setCallingConv(SrcF->getCallingConv());
Devang Patel4c758ea2008-09-25 21:00:45 +0000410 setAttributes(SrcF->getAttributes());
Gordon Henriksend930f912008-08-17 18:44:35 +0000411 if (SrcF->hasGC())
412 setGC(SrcF->getGC());
413 else
414 clearGC();
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000415 if (SrcF->hasPrefixData())
416 setPrefixData(SrcF->getPrefixData());
417 else
Craig Topperc6207612014-04-09 06:08:46 +0000418 setPrefixData(nullptr);
Duncan Sandsdd7daee2008-05-26 19:58:59 +0000419}
420
Chris Lattnerbb346d02003-05-08 03:47:33 +0000421/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000422/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000423/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000424/// zero to allow easy checking for whether a function is intrinsic or not. The
425/// particular intrinsic functions which correspond to this value are defined in
Michael Ilseman516d7032013-03-01 18:48:54 +0000426/// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent
427/// requests for the same ID return results much faster from the cache.
Chris Lattnerbb346d02003-05-08 03:47:33 +0000428///
Dale Johannesenb842d522009-02-05 01:49:45 +0000429unsigned Function::getIntrinsicID() const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000430 const ValueName *ValName = this->getValueName();
Michael Ilsemanb99f80d2012-12-19 23:17:20 +0000431 if (!ValName || !isIntrinsic())
Reid Spencerc5f397a2007-04-16 07:08:44 +0000432 return 0;
Michael Ilseman516d7032013-03-01 18:48:54 +0000433
434 LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
435 getContext().pImpl->IntrinsicIDCache;
Chris Lattnerf83a6642013-03-20 21:04:53 +0000436 if (!IntrinsicIDCache.count(this)) {
Michael Ilseman516d7032013-03-01 18:48:54 +0000437 unsigned Id = lookupIntrinsicID();
438 IntrinsicIDCache[this]=Id;
439 return Id;
440 }
441 return IntrinsicIDCache[this];
442}
443
444/// This private method does the actual lookup of an intrinsic ID when the query
445/// could not be answered from the cache.
446unsigned Function::lookupIntrinsicID() const {
447 const ValueName *ValName = this->getValueName();
Chris Lattner1e92e062007-02-15 19:17:16 +0000448 unsigned Len = ValName->getKeyLength();
449 const char *Name = ValName->getKeyData();
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000450
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000451#define GET_FUNCTION_RECOGNIZER
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000452#include "llvm/IR/Intrinsics.gen"
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000453#undef GET_FUNCTION_RECOGNIZER
Michael Ilseman516d7032013-03-01 18:48:54 +0000454
Chris Lattnerbb346d02003-05-08 03:47:33 +0000455 return 0;
456}
457
Benjamin Kramere6e19332011-07-14 17:45:39 +0000458std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000459 assert(id < num_intrinsics && "Invalid intrinsic ID!");
Chris Lattnerf5ba0412011-04-25 22:14:33 +0000460 static const char * const Table[] = {
Chris Lattner71b8c982006-03-25 06:32:47 +0000461 "not_intrinsic",
462#define GET_INTRINSIC_NAME_TABLE
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000463#include "llvm/IR/Intrinsics.gen"
Chris Lattner71b8c982006-03-25 06:32:47 +0000464#undef GET_INTRINSIC_NAME_TABLE
465 };
Benjamin Kramere6e19332011-07-14 17:45:39 +0000466 if (Tys.empty())
Reid Spencer2a2117c2007-04-01 07:25:33 +0000467 return Table[id];
468 std::string Result(Table[id]);
Benjamin Kramere6e19332011-07-14 17:45:39 +0000469 for (unsigned i = 0; i < Tys.size(); ++i) {
Chris Lattner229907c2011-07-18 04:54:35 +0000470 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000471 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
Owen Anderson53aa7a92009-08-10 22:56:29 +0000472 EVT::getEVT(PTyp->getElementType()).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000473 }
474 else if (Tys[i])
Owen Anderson53aa7a92009-08-10 22:56:29 +0000475 Result += "." + EVT::getEVT(Tys[i]).getEVTString();
Mon P Wang2c839d42008-07-30 04:36:53 +0000476 }
Reid Spencer2a2117c2007-04-01 07:25:33 +0000477 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000478}
479
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000480
Chris Lattnerf39c2782012-05-27 18:28:35 +0000481/// IIT_Info - These are enumerators that describe the entries returned by the
482/// getIntrinsicInfoTableEntries function.
483///
484/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
485enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000486 // Common values should be encoded with 0-15.
Chris Lattnerf39c2782012-05-27 18:28:35 +0000487 IIT_Done = 0,
488 IIT_I1 = 1,
489 IIT_I8 = 2,
490 IIT_I16 = 3,
491 IIT_I32 = 4,
492 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000493 IIT_F16 = 6,
494 IIT_F32 = 7,
495 IIT_F64 = 8,
496 IIT_V2 = 9,
497 IIT_V4 = 10,
498 IIT_V8 = 11,
499 IIT_V16 = 12,
500 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000501 IIT_PTR = 14,
502 IIT_ARG = 15,
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000503
Robert Khasanov65c27562014-10-20 19:25:05 +0000504 // Values from 16+ are only encodable with the inefficient encoding.
505 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000506 IIT_MMX = 17,
507 IIT_METADATA = 18,
508 IIT_EMPTYSTRUCT = 19,
509 IIT_STRUCT2 = 20,
510 IIT_STRUCT3 = 21,
511 IIT_STRUCT4 = 22,
512 IIT_STRUCT5 = 23,
513 IIT_EXTEND_ARG = 24,
514 IIT_TRUNC_ARG = 25,
515 IIT_ANYPTR = 26,
516 IIT_V1 = 27,
517 IIT_VARARG = 28,
518 IIT_HALF_VEC_ARG = 29
Chris Lattnerf39c2782012-05-27 18:28:35 +0000519};
520
521
522static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
523 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
Chris Lattnera57c7972012-05-17 05:13:57 +0000524 IIT_Info Info = IIT_Info(Infos[NextElt++]);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000525 unsigned StructElts = 2;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000526 using namespace Intrinsic;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000527
Chris Lattnera57c7972012-05-17 05:13:57 +0000528 switch (Info) {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000529 case IIT_Done:
530 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
531 return;
Andrew Tricka2efd992013-10-31 17:18:11 +0000532 case IIT_VARARG:
533 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
534 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000535 case IIT_MMX:
536 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
537 return;
538 case IIT_METADATA:
539 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
540 return;
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000541 case IIT_F16:
542 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
543 return;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000544 case IIT_F32:
545 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
546 return;
547 case IIT_F64:
548 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
549 return;
550 case IIT_I1:
551 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
552 return;
553 case IIT_I8:
554 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
555 return;
556 case IIT_I16:
557 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
558 return;
559 case IIT_I32:
560 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
561 return;
562 case IIT_I64:
563 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
564 return;
Jiangning Liu63dc8402013-09-24 02:47:27 +0000565 case IIT_V1:
566 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
567 DecodeIITType(NextElt, Infos, OutputTable);
568 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000569 case IIT_V2:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000570 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
571 DecodeIITType(NextElt, Infos, OutputTable);
572 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000573 case IIT_V4:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000574 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
575 DecodeIITType(NextElt, Infos, OutputTable);
576 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000577 case IIT_V8:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000578 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
579 DecodeIITType(NextElt, Infos, OutputTable);
580 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000581 case IIT_V16:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000582 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
583 DecodeIITType(NextElt, Infos, OutputTable);
584 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000585 case IIT_V32:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000586 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
587 DecodeIITType(NextElt, Infos, OutputTable);
588 return;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000589 case IIT_V64:
590 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
591 DecodeIITType(NextElt, Infos, OutputTable);
592 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000593 case IIT_PTR:
Chris Lattnerf39c2782012-05-27 18:28:35 +0000594 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
595 DecodeIITType(NextElt, Infos, OutputTable);
596 return;
Chris Lattner4f18aa82012-05-23 05:19:18 +0000597 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000598 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000599 Infos[NextElt++]));
600 DecodeIITType(NextElt, Infos, OutputTable);
601 return;
Pete Cooper243efd72012-05-21 23:21:28 +0000602 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000603 case IIT_ARG: {
604 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
605 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
606 return;
607 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000608 case IIT_EXTEND_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000609 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000610 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000611 ArgInfo));
612 return;
613 }
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000614 case IIT_TRUNC_ARG: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000615 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000616 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000617 ArgInfo));
618 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000619 }
Tim Northover4516de32014-03-29 07:04:54 +0000620 case IIT_HALF_VEC_ARG: {
621 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
622 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
623 ArgInfo));
624 return;
625 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000626 case IIT_EMPTYSTRUCT:
627 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
628 return;
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000629 case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
630 case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
631 case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
632 case IIT_STRUCT2: {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000633 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
634
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000635 for (unsigned i = 0; i != StructElts; ++i)
Chris Lattnerf39c2782012-05-27 18:28:35 +0000636 DecodeIITType(NextElt, Infos, OutputTable);
637 return;
Chris Lattner827b2532012-05-17 04:30:58 +0000638 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000639 }
640 llvm_unreachable("unhandled");
641}
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000642
Chris Lattnerf39c2782012-05-27 18:28:35 +0000643
644#define GET_INTRINSIC_GENERATOR_GLOBAL
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000645#include "llvm/IR/Intrinsics.gen"
Chris Lattnerf39c2782012-05-27 18:28:35 +0000646#undef GET_INTRINSIC_GENERATOR_GLOBAL
647
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000648void Intrinsic::getIntrinsicInfoTableEntries(ID id,
Chris Lattnerf39c2782012-05-27 18:28:35 +0000649 SmallVectorImpl<IITDescriptor> &T){
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000650 // Check to see if the intrinsic's type was expressible by the table.
651 unsigned TableVal = IIT_Table[id-1];
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000652
Chris Lattnera3b0f522012-05-17 15:55:41 +0000653 // Decode the TableVal into an array of IITValues.
654 SmallVector<unsigned char, 8> IITValues;
655 ArrayRef<unsigned char> IITEntries;
656 unsigned NextElt = 0;
657 if ((TableVal >> 31) != 0) {
658 // This is an offset into the IIT_LongEncodingTable.
659 IITEntries = IIT_LongEncodingTable;
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000660
Chris Lattnera3b0f522012-05-17 15:55:41 +0000661 // Strip sentinel bit.
662 NextElt = (TableVal << 1) >> 1;
663 } else {
Chris Lattnerf39c2782012-05-27 18:28:35 +0000664 // Decode the TableVal into an array of IITValues. If the entry was encoded
665 // into a single word in the table itself, decode it now.
Chris Lattnera57c7972012-05-17 05:13:57 +0000666 do {
667 IITValues.push_back(TableVal & 0xF);
668 TableVal >>= 4;
669 } while (TableVal);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000670
Chris Lattnera3b0f522012-05-17 15:55:41 +0000671 IITEntries = IITValues;
672 NextElt = 0;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000673 }
Chris Lattnerf39c2782012-05-27 18:28:35 +0000674
675 // Okay, decode the table into the output vector of IITDescriptors.
676 DecodeIITType(NextElt, IITEntries, T);
677 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
678 DecodeIITType(NextElt, IITEntries, T);
679}
680
681
682static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
683 ArrayRef<Type*> Tys, LLVMContext &Context) {
684 using namespace Intrinsic;
685 IITDescriptor D = Infos.front();
686 Infos = Infos.slice(1);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000687
Chris Lattnerf39c2782012-05-27 18:28:35 +0000688 switch (D.Kind) {
689 case IITDescriptor::Void: return Type::getVoidTy(Context);
Andrew Tricka2efd992013-10-31 17:18:11 +0000690 case IITDescriptor::VarArg: return Type::getVoidTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000691 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
692 case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000693 case IITDescriptor::Half: return Type::getHalfTy(Context);
Chris Lattnerf39c2782012-05-27 18:28:35 +0000694 case IITDescriptor::Float: return Type::getFloatTy(Context);
695 case IITDescriptor::Double: return Type::getDoubleTy(Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000696
Chris Lattnerf39c2782012-05-27 18:28:35 +0000697 case IITDescriptor::Integer:
698 return IntegerType::get(Context, D.Integer_Width);
699 case IITDescriptor::Vector:
700 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
701 case IITDescriptor::Pointer:
702 return PointerType::get(DecodeFixedType(Infos, Tys, Context),
703 D.Pointer_AddressSpace);
704 case IITDescriptor::Struct: {
705 Type *Elts[5];
706 assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
707 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
708 Elts[i] = DecodeFixedType(Infos, Tys, Context);
Craig Toppere1d12942014-08-27 05:25:25 +0000709 return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements));
Chris Lattnerf39c2782012-05-27 18:28:35 +0000710 }
711
712 case IITDescriptor::Argument:
713 return Tys[D.getArgumentNumber()];
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000714 case IITDescriptor::ExtendArgument: {
715 Type *Ty = Tys[D.getArgumentNumber()];
716 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
717 return VectorType::getExtendedElementVectorType(VTy);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000718
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000719 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
720 }
721 case IITDescriptor::TruncArgument: {
722 Type *Ty = Tys[D.getArgumentNumber()];
723 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
724 return VectorType::getTruncatedElementVectorType(VTy);
725
726 IntegerType *ITy = cast<IntegerType>(Ty);
727 assert(ITy->getBitWidth() % 2 == 0);
728 return IntegerType::get(Context, ITy->getBitWidth() / 2);
729 }
Tim Northover4516de32014-03-29 07:04:54 +0000730 case IITDescriptor::HalfVecArgument:
731 return VectorType::getHalfElementsVectorType(cast<VectorType>(
732 Tys[D.getArgumentNumber()]));
Chris Lattnerf39c2782012-05-27 18:28:35 +0000733 }
734 llvm_unreachable("unhandled");
735}
736
737
738
739FunctionType *Intrinsic::getType(LLVMContext &Context,
740 ID id, ArrayRef<Type*> Tys) {
741 SmallVector<IITDescriptor, 8> Table;
742 getIntrinsicInfoTableEntries(id, Table);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000743
Chris Lattnerf39c2782012-05-27 18:28:35 +0000744 ArrayRef<IITDescriptor> TableRef = Table;
745 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000746
Chris Lattnera3b0f522012-05-17 15:55:41 +0000747 SmallVector<Type*, 8> ArgTys;
Chris Lattnerf39c2782012-05-27 18:28:35 +0000748 while (!TableRef.empty())
749 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
Jim Laskey2682ea62007-02-07 20:38:26 +0000750
Steven Wud05affc2014-10-20 15:47:24 +0000751 // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
752 // If we see void type as the type of the last argument, it is vararg intrinsic
753 if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
754 ArgTys.pop_back();
755 return FunctionType::get(ResultTy, ArgTys, true);
756 }
Michael Ilsemanacdb76d2012-12-17 20:37:55 +0000757 return FunctionType::get(ResultTy, ArgTys, false);
Jim Laskey2682ea62007-02-07 20:38:26 +0000758}
759
Mon P Wangb4024932009-02-24 23:17:49 +0000760bool Intrinsic::isOverloaded(ID id) {
Mon P Wangb4024932009-02-24 23:17:49 +0000761#define GET_INTRINSIC_OVERLOAD_TABLE
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000762#include "llvm/IR/Intrinsics.gen"
Mon P Wangb4024932009-02-24 23:17:49 +0000763#undef GET_INTRINSIC_OVERLOAD_TABLE
Mon P Wangb4024932009-02-24 23:17:49 +0000764}
765
Chris Lattner49b7ee12009-01-12 01:18:58 +0000766/// This defines the "Intrinsic::getAttributes(ID id)" method.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000767#define GET_INTRINSIC_ATTRIBUTES
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000768#include "llvm/IR/Intrinsics.gen"
Duncan Sands38ef3a82007-12-03 20:06:50 +0000769#undef GET_INTRINSIC_ATTRIBUTES
770
Benjamin Kramere6e19332011-07-14 17:45:39 +0000771Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000772 // There can never be multiple globals with the same name of different types,
773 // because intrinsics must be a specific type.
Duncan Sandsa8ff6ca2008-04-07 13:39:11 +0000774 return
Benjamin Kramere6e19332011-07-14 17:45:39 +0000775 cast<Function>(M->getOrInsertFunction(getName(id, Tys),
776 getType(M->getContext(), id, Tys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000777}
778
Dale Johannesenb842d522009-02-05 01:49:45 +0000779// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
780#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
Chandler Carruthdb25c6c2013-01-02 12:09:16 +0000781#include "llvm/IR/Intrinsics.gen"
Dale Johannesenb842d522009-02-05 01:49:45 +0000782#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
783
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000784// This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
785#define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
786#include "llvm/IR/Intrinsics.gen"
787#undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
788
Gabor Greif161cb042010-03-23 14:40:20 +0000789/// hasAddressTaken - returns true if there are any uses of this function
790/// other than direct calls or invokes to it.
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000791bool Function::hasAddressTaken(const User* *PutOffender) const {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000792 for (const Use &U : uses()) {
793 const User *FU = U.getUser();
794 if (isa<BlockAddress>(FU))
Jay Foadca0c4992012-05-12 08:30:16 +0000795 continue;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000796 if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU))
797 return PutOffender ? (*PutOffender = FU, true) : true;
798 ImmutableCallSite CS(cast<Instruction>(FU));
799 if (!CS.isCallee(&U))
800 return PutOffender ? (*PutOffender = FU, true) : true;
Jay Foad557169d2009-06-10 08:41:11 +0000801 }
802 return false;
803}
804
Eli Friedman1923a332011-10-20 05:23:42 +0000805bool Function::isDefTriviallyDead() const {
806 // Check the linkage
807 if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
808 !hasAvailableExternallyLinkage())
809 return false;
810
811 // Check if the function is used by anything other than a blockaddress.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000812 for (const User *U : users())
813 if (!isa<BlockAddress>(U))
Eli Friedman1923a332011-10-20 05:23:42 +0000814 return false;
815
816 return true;
817}
818
Bill Wendling63a4ea12011-10-17 18:43:40 +0000819/// callsFunctionThatReturnsTwice - Return true if the function has a call to
820/// setjmp or other function that gcc recognizes as "returning twice".
821bool Function::callsFunctionThatReturnsTwice() const {
822 for (const_inst_iterator
823 I = inst_begin(this), E = inst_end(this); I != E; ++I) {
Mark Seaborn82711182014-01-14 04:20:01 +0000824 ImmutableCallSite CS(&*I);
825 if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
Bill Wendling63a4ea12011-10-17 18:43:40 +0000826 return true;
827 }
828
829 return false;
830}
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000831
832Constant *Function::getPrefixData() const {
833 assert(hasPrefixData());
834 const LLVMContextImpl::PrefixDataMapTy &PDMap =
835 getContext().pImpl->PrefixDataMap;
836 assert(PDMap.find(this) != PDMap.end());
837 return cast<Constant>(PDMap.find(this)->second->getReturnValue());
838}
839
840void Function::setPrefixData(Constant *PrefixData) {
841 if (!PrefixData && !hasPrefixData())
842 return;
843
844 unsigned SCData = getSubclassDataFromValue();
845 LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap;
846 ReturnInst *&PDHolder = PDMap[this];
847 if (PrefixData) {
848 if (PDHolder)
849 PDHolder->setOperand(0, PrefixData);
850 else
851 PDHolder = ReturnInst::Create(getContext(), PrefixData);
852 SCData |= 2;
853 } else {
854 delete PDHolder;
855 PDMap.erase(this);
856 SCData &= ~2;
857 }
858 setValueSubclassData(SCData);
859}