blob: f3169e1c5424b3118023d57cec13eecb8b9aea35 [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//
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000010// This file implements the Function class for the VMCore library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +000015#include "llvm/DerivedTypes.h"
Chris Lattner89046ca2004-10-12 04:20:25 +000016#include "llvm/IntrinsicInst.h"
Dan Gohman3a071482007-08-20 19:23:34 +000017#include "llvm/CodeGen/ValueTypes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Reid Spencer4388f0b2007-04-22 05:46:44 +000019#include "llvm/Support/ManagedStatic.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000020#include "llvm/Support/StringPool.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
Duncan Sands38ef3a82007-12-03 20:06:50 +000022#include "llvm/ADT/BitVector.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000023#include "llvm/ADT/DenseMap.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattnerf6c93e32005-01-30 00:09:23 +000027BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000028 BasicBlock *Ret = new BasicBlock();
29 // This should not be garbage monitored.
30 LeakDetector::removeGarbageObject(Ret);
31 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000032}
33
Chris Lattner113f4f42002-06-25 16:13:24 +000034iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
35 return F->getBasicBlockList();
36}
37
Chris Lattnerf6c93e32005-01-30 00:09:23 +000038Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000039 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000040 // This should not be garbage monitored.
41 LeakDetector::removeGarbageObject(Ret);
42 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000043}
44
45iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
46 return F->getArgumentList();
47}
48
49// Explicit instantiations of SymbolTableListTraits since some of the methods
50// are not in the public header file...
Chris Lattnerb47aa542007-04-17 03:26:42 +000051template class SymbolTableListTraits<Argument, Function>;
52template class SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000053
Chris Lattnerda975502001-09-10 07:58:01 +000054//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000055// Argument Implementation
56//===----------------------------------------------------------------------===//
57
Misha Brukmanb1c93172005-04-21 23:48:37 +000058Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000059 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000060 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000061
62 // Make sure that we get added to a function
63 LeakDetector::addGarbageObject(this);
64
Chris Lattner9ed7aef2002-09-06 21:33:15 +000065 if (Par)
66 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000067 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000068}
69
Chris Lattner9ed7aef2002-09-06 21:33:15 +000070void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000071 if (getParent())
72 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000073 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000074 if (getParent())
75 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000076}
77
Chris Lattnerd255ae22002-04-09 19:39:35 +000078//===----------------------------------------------------------------------===//
Reid Spencer019c8862007-04-09 15:01:12 +000079// ParamAttrsList Implementation
80//===----------------------------------------------------------------------===//
81
82uint16_t
83ParamAttrsList::getParamAttrs(uint16_t Index) const {
84 unsigned limit = attrs.size();
Duncan Sandsd4d7f9d2007-11-30 18:20:58 +000085 for (unsigned i = 0; i < limit && attrs[i].index <= Index; ++i)
Reid Spencer019c8862007-04-09 15:01:12 +000086 if (attrs[i].index == Index)
87 return attrs[i].attrs;
Reid Spencera472f662007-04-11 02:44:20 +000088 return ParamAttr::None;
Reid Spencer019c8862007-04-09 15:01:12 +000089}
90
Reid Spencer019c8862007-04-09 15:01:12 +000091std::string
92ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
93 std::string Result;
Reid Spencera472f662007-04-11 02:44:20 +000094 if (Attrs & ParamAttr::ZExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000095 Result += "zeroext ";
Reid Spencera472f662007-04-11 02:44:20 +000096 if (Attrs & ParamAttr::SExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000097 Result += "signext ";
Reid Spencera472f662007-04-11 02:44:20 +000098 if (Attrs & ParamAttr::NoReturn)
Reid Spencer019c8862007-04-09 15:01:12 +000099 Result += "noreturn ";
Reid Spencera472f662007-04-11 02:44:20 +0000100 if (Attrs & ParamAttr::NoUnwind)
Reid Spencer019c8862007-04-09 15:01:12 +0000101 Result += "nounwind ";
Reid Spencera472f662007-04-11 02:44:20 +0000102 if (Attrs & ParamAttr::InReg)
Reid Spencer019c8862007-04-09 15:01:12 +0000103 Result += "inreg ";
Zhou Sheng2444a9a2007-06-05 05:28:26 +0000104 if (Attrs & ParamAttr::NoAlias)
105 Result += "noalias ";
Reid Spencera472f662007-04-11 02:44:20 +0000106 if (Attrs & ParamAttr::StructRet)
Reid Spencer019c8862007-04-09 15:01:12 +0000107 Result += "sret ";
Rafael Espindolab567e3f2007-07-06 10:57:03 +0000108 if (Attrs & ParamAttr::ByVal)
109 Result += "byval ";
Duncan Sands644f9172007-07-27 12:58:54 +0000110 if (Attrs & ParamAttr::Nest)
111 Result += "nest ";
Duncan Sandsa89a1132007-11-22 20:23:04 +0000112 if (Attrs & ParamAttr::ReadNone)
113 Result += "readnone ";
114 if (Attrs & ParamAttr::ReadOnly)
115 Result += "readonly ";
Reid Spencer019c8862007-04-09 15:01:12 +0000116 return Result;
117}
118
Duncan Sands185eeac2007-11-25 14:10:56 +0000119/// onlyInformative - Returns whether only informative attributes are set.
120static inline bool onlyInformative(uint16_t attrs) {
121 return !(attrs & ~ParamAttr::Informative);
122}
123
124bool
125ParamAttrsList::areCompatible(const ParamAttrsList *A, const ParamAttrsList *B){
126 if (A == B)
127 return true;
128 unsigned ASize = A ? A->size() : 0;
129 unsigned BSize = B ? B->size() : 0;
130 unsigned AIndex = 0;
131 unsigned BIndex = 0;
132
133 while (AIndex < ASize && BIndex < BSize) {
134 uint16_t AIdx = A->getParamIndex(AIndex);
135 uint16_t BIdx = B->getParamIndex(BIndex);
136 uint16_t AAttrs = A->getParamAttrsAtIndex(AIndex);
137 uint16_t BAttrs = B->getParamAttrsAtIndex(AIndex);
138
139 if (AIdx < BIdx) {
140 if (!onlyInformative(AAttrs))
141 return false;
142 ++AIndex;
143 } else if (BIdx < AIdx) {
144 if (!onlyInformative(BAttrs))
145 return false;
146 ++BIndex;
147 } else {
148 if (!onlyInformative(AAttrs ^ BAttrs))
149 return false;
150 ++AIndex;
151 ++BIndex;
152 }
153 }
154 for (; AIndex < ASize; ++AIndex)
155 if (!onlyInformative(A->getParamAttrsAtIndex(AIndex)))
156 return false;
157 for (; BIndex < BSize; ++BIndex)
158 if (!onlyInformative(B->getParamAttrsAtIndex(AIndex)))
159 return false;
160 return true;
161}
162
Chris Lattner0acc15c2008-01-02 22:03:27 +0000163void ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
164 for (unsigned i = 0; i < attrs.size(); ++i)
165 ID.AddInteger(unsigned(attrs[i].attrs) << 16 | unsigned(attrs[i].index));
Reid Spencer019c8862007-04-09 15:01:12 +0000166}
167
Reid Spencer4388f0b2007-04-22 05:46:44 +0000168static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
Reid Spencer019c8862007-04-09 15:01:12 +0000169
Duncan Sandsb41f8722007-11-30 18:19:18 +0000170const ParamAttrsList *
Reid Spencer4388f0b2007-04-22 05:46:44 +0000171ParamAttrsList::get(const ParamAttrsVector &attrVec) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000172 // If there are no attributes then return a null ParamAttrsList pointer.
173 if (attrVec.empty())
174 return 0;
175
Duncan Sandsd7813352007-09-11 14:40:04 +0000176#ifndef NDEBUG
Duncan Sands04eb67e2007-11-20 14:09:29 +0000177 for (unsigned i = 0, e = attrVec.size(); i < e; ++i) {
178 assert(attrVec[i].attrs != ParamAttr::None
179 && "Pointless parameter attribute!");
180 assert((!i || attrVec[i-1].index < attrVec[i].index)
181 && "Misordered ParamAttrsList!");
182 }
Duncan Sandsd7813352007-09-11 14:40:04 +0000183#endif
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000184
185 // Otherwise, build a key to look up the existing attributes.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000186 ParamAttrsList key(attrVec);
187 FoldingSetNodeID ID;
188 key.Profile(ID);
189 void *InsertPos;
190 ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000191
192 // If we didn't find any existing attributes of the same shape then
193 // create a new one and insert it.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000194 if (!PAL) {
195 PAL = new ParamAttrsList(attrVec);
196 ParamAttrsLists->InsertNode(PAL, InsertPos);
197 }
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000198
199 // Return the ParamAttrsList that we found or created.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000200 return PAL;
Reid Spencer019c8862007-04-09 15:01:12 +0000201}
202
Duncan Sandsb41f8722007-11-30 18:19:18 +0000203const ParamAttrsList *
204ParamAttrsList::getModified(const ParamAttrsList *PAL,
205 const ParamAttrsVector &modVec) {
206 if (modVec.empty())
207 return PAL;
208
209#ifndef NDEBUG
210 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
211 assert((!i || modVec[i-1].index < modVec[i].index)
212 && "Misordered ParamAttrsList!");
213#endif
214
215 if (!PAL) {
216 // Strip any instances of ParamAttr::None from modVec before calling 'get'.
217 ParamAttrsVector newVec;
218 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
219 if (modVec[i].attrs != ParamAttr::None)
220 newVec.push_back(modVec[i]);
221 return get(newVec);
222 }
223
224 const ParamAttrsVector &oldVec = PAL->attrs;
225
226 ParamAttrsVector newVec;
227 unsigned oldI = 0;
228 unsigned modI = 0;
229 unsigned oldE = oldVec.size();
230 unsigned modE = modVec.size();
231
232 while (oldI < oldE && modI < modE) {
233 uint16_t oldIndex = oldVec[oldI].index;
234 uint16_t modIndex = modVec[modI].index;
235
236 if (oldIndex < modIndex) {
237 newVec.push_back(oldVec[oldI]);
238 ++oldI;
239 } else if (modIndex < oldIndex) {
240 if (modVec[modI].attrs != ParamAttr::None)
241 newVec.push_back(modVec[modI]);
242 ++modI;
243 } else {
244 // Same index - overwrite or delete existing attributes.
245 if (modVec[modI].attrs != ParamAttr::None)
246 newVec.push_back(modVec[modI]);
247 ++oldI;
248 ++modI;
249 }
250 }
251
252 for (; oldI < oldE; ++oldI)
253 newVec.push_back(oldVec[oldI]);
254 for (; modI < modE; ++modI)
255 if (modVec[modI].attrs != ParamAttr::None)
256 newVec.push_back(modVec[modI]);
257
258 return get(newVec);
259}
260
Duncan Sandsaa31b922007-12-19 21:13:37 +0000261const ParamAttrsList *
262ParamAttrsList::includeAttrs(const ParamAttrsList *PAL,
263 uint16_t idx, uint16_t attrs) {
264 uint16_t OldAttrs = PAL ? PAL->getParamAttrs(idx) : 0;
265 uint16_t NewAttrs = OldAttrs | attrs;
266 if (NewAttrs == OldAttrs)
267 return PAL;
268
269 ParamAttrsVector modVec;
270 modVec.push_back(ParamAttrsWithIndex::get(idx, NewAttrs));
271 return getModified(PAL, modVec);
272}
273
274const ParamAttrsList *
275ParamAttrsList::excludeAttrs(const ParamAttrsList *PAL,
276 uint16_t idx, uint16_t attrs) {
277 uint16_t OldAttrs = PAL ? PAL->getParamAttrs(idx) : 0;
278 uint16_t NewAttrs = OldAttrs & ~attrs;
279 if (NewAttrs == OldAttrs)
280 return PAL;
281
282 ParamAttrsVector modVec;
283 modVec.push_back(ParamAttrsWithIndex::get(idx, NewAttrs));
284 return getModified(PAL, modVec);
285}
286
Reid Spencerc6a83842007-04-22 17:28:03 +0000287ParamAttrsList::~ParamAttrsList() {
288 ParamAttrsLists->RemoveNode(this);
289}
290
Reid Spencer019c8862007-04-09 15:01:12 +0000291//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000292// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000293//===----------------------------------------------------------------------===//
294
Chris Lattner379a8d22003-04-16 20:28:45 +0000295Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000296 const std::string &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000297 : GlobalValue(PointerType::getUnqual(Ty),
298 Value::FunctionVal, 0, 0, Linkage, name),
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000299 ParamAttrs(0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000300 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000301
Chris Lattner3ae303c2003-11-21 22:32:23 +0000302 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
303 && "LLVM functions cannot return aggregate values!");
304
Chris Lattnere2de9082007-08-18 06:14:52 +0000305 // If the function has arguments, mark them as lazily built.
306 if (Ty->getNumParams())
307 SubclassData = 1; // Set the "has lazy arguments" bit.
308
Chris Lattner184b2982002-09-08 18:59:35 +0000309 // Make sure that we get added to a function
310 LeakDetector::addGarbageObject(this);
311
Chris Lattner6213ae02002-09-06 20:46:32 +0000312 if (ParentModule)
313 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000314}
315
Gordon Henriksen14a55692007-12-10 02:14:30 +0000316Function::~Function() {
317 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000318
Chris Lattner2f7c9632001-06-06 20:29:01 +0000319 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000320 ArgumentList.clear();
321 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000322
323 // Drop our reference to the parameter attributes, if any.
Gordon Henriksen14a55692007-12-10 02:14:30 +0000324 if (ParamAttrs)
325 ParamAttrs->dropRef();
Gordon Henriksen71183b62007-12-10 03:18:06 +0000326
327 // Remove the function from the on-the-side collector table.
328 clearCollector();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000329}
330
Chris Lattnere2de9082007-08-18 06:14:52 +0000331void Function::BuildLazyArguments() const {
332 // Create the arguments vector, all arguments start out unnamed.
333 const FunctionType *FT = getFunctionType();
334 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
335 assert(FT->getParamType(i) != Type::VoidTy &&
336 "Cannot have void typed arguments!");
337 ArgumentList.push_back(new Argument(FT->getParamType(i)));
338 }
339
340 // Clear the lazy arguments bit.
341 const_cast<Function*>(this)->SubclassData &= ~1;
342}
343
344size_t Function::arg_size() const {
345 return getFunctionType()->getNumParams();
346}
347bool Function::arg_empty() const {
348 return getFunctionType()->getNumParams() == 0;
349}
350
Chris Lattner4e8c4872002-03-23 22:51:58 +0000351void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000352 if (getParent())
353 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000354 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000355 if (getParent())
356 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357}
358
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000359void Function::setParamAttrs(const ParamAttrsList *attrs) {
360 // Avoid deleting the ParamAttrsList if they are setting the
361 // attributes to the same list.
362 if (ParamAttrs == attrs)
363 return;
364
365 // Drop reference on the old ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000366 if (ParamAttrs)
367 ParamAttrs->dropRef();
368
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000369 // Add reference to the new ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000370 if (attrs)
371 attrs->addRef();
372
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000373 // Set the new ParamAttrsList.
Reid Spencerc6a83842007-04-22 17:28:03 +0000374 ParamAttrs = attrs;
375}
376
Chris Lattner91db5822002-03-29 03:44:36 +0000377const FunctionType *Function::getFunctionType() const {
378 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000379}
380
Chris Lattner07b522d2005-01-07 07:40:32 +0000381bool Function::isVarArg() const {
382 return getFunctionType()->isVarArg();
383}
384
Misha Brukmanb1c93172005-04-21 23:48:37 +0000385const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000386 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387}
388
Chris Lattner02a71e72004-10-11 22:21:39 +0000389void Function::removeFromParent() {
390 getParent()->getFunctionList().remove(this);
391}
392
393void Function::eraseFromParent() {
394 getParent()->getFunctionList().erase(this);
395}
396
Chris Lattner2f7c9632001-06-06 20:29:01 +0000397// dropAllReferences() - This function causes all the subinstructions to "let
398// go" of all references that they are maintaining. This allows one to
399// 'delete' a whole class at a time, even though there may be circular
400// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000401// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000402// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000403// delete.
404//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000405void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000406 for (iterator I = begin(), E = end(); I != E; ++I)
407 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000408 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000409}
Chris Lattnerda975502001-09-10 07:58:01 +0000410
Gordon Henriksen71183b62007-12-10 03:18:06 +0000411// Maintain the collector name for each function in an on-the-side table. This
412// saves allocating an additional word in Function for programs which do not use
413// GC (i.e., most programs) at the cost of increased overhead for clients which
414// do use GC.
415static DenseMap<const Function*,PooledStringPtr> *CollectorNames;
416static StringPool *CollectorNamePool;
417
418bool Function::hasCollector() const {
419 return CollectorNames && CollectorNames->count(this);
420}
421
422const char *Function::getCollector() const {
423 assert(hasCollector() && "Function has no collector");
424 return *(*CollectorNames)[this];
425}
426
427void Function::setCollector(const char *Str) {
428 if (!CollectorNamePool)
429 CollectorNamePool = new StringPool();
430 if (!CollectorNames)
431 CollectorNames = new DenseMap<const Function*,PooledStringPtr>();
432 (*CollectorNames)[this] = CollectorNamePool->intern(Str);
433}
434
435void Function::clearCollector() {
436 if (CollectorNames) {
437 CollectorNames->erase(this);
438 if (CollectorNames->empty()) {
439 delete CollectorNames;
440 CollectorNames = 0;
Gordon Henriksen4b904b92007-12-10 03:35:18 +0000441 if (CollectorNamePool->empty()) {
442 delete CollectorNamePool;
443 CollectorNamePool = 0;
444 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000445 }
446 }
447}
448
Chris Lattnerbb346d02003-05-08 03:47:33 +0000449/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000450/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000451/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000452/// zero to allow easy checking for whether a function is intrinsic or not. The
453/// particular intrinsic functions which correspond to this value are defined in
454/// llvm/Intrinsics.h.
455///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000456unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000457 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000458 if (!ValName)
459 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000460 unsigned Len = ValName->getKeyLength();
461 const char *Name = ValName->getKeyData();
462
Reid Spencer78d71f12007-04-16 16:56:54 +0000463 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000464 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000465 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000466
Reid Spencer9c2eec32007-04-16 06:54:34 +0000467 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000468
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000469#define GET_FUNCTION_RECOGNIZER
470#include "llvm/Intrinsics.gen"
471#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000472 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000473 return 0;
474}
475
Reid Spencer2a2117c2007-04-01 07:25:33 +0000476std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000477 assert(id < num_intrinsics && "Invalid intrinsic ID!");
478 const char * const Table[] = {
479 "not_intrinsic",
480#define GET_INTRINSIC_NAME_TABLE
481#include "llvm/Intrinsics.gen"
482#undef GET_INTRINSIC_NAME_TABLE
483 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000484 if (numTys == 0)
485 return Table[id];
486 std::string Result(Table[id]);
487 for (unsigned i = 0; i < numTys; ++i)
488 if (Tys[i])
Dan Gohman3a071482007-08-20 19:23:34 +0000489 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer2a2117c2007-04-01 07:25:33 +0000490 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000491}
492
Reid Spencer2a2117c2007-04-01 07:25:33 +0000493const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000494 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000495 const Type *ResultTy = NULL;
496 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000497 bool IsVarArg = false;
498
499#define GET_INTRINSIC_GENERATOR
500#include "llvm/Intrinsics.gen"
501#undef GET_INTRINSIC_GENERATOR
502
Reid Spencer26d9ff62007-04-09 06:11:23 +0000503 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000504}
505
Duncan Sands38ef3a82007-12-03 20:06:50 +0000506const ParamAttrsList *Intrinsic::getParamAttrs(ID id) {
507 static const ParamAttrsList *IntrinsicAttributes[Intrinsic::num_intrinsics];
508
509 if (IntrinsicAttributes[id])
510 return IntrinsicAttributes[id];
511
512 ParamAttrsVector Attrs;
513 uint16_t Attr = ParamAttr::None;
514
515#define GET_INTRINSIC_ATTRIBUTES
516#include "llvm/Intrinsics.gen"
517#undef GET_INTRINSIC_ATTRIBUTES
518
519 // Intrinsics cannot throw exceptions.
520 Attr |= ParamAttr::NoUnwind;
521
522 Attrs.push_back(ParamAttrsWithIndex::get(0, Attr));
523 IntrinsicAttributes[id] = ParamAttrsList::get(Attrs);
524 return IntrinsicAttributes[id];
525}
526
Reid Spencer2a2117c2007-04-01 07:25:33 +0000527Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
528 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000529 // There can never be multiple globals with the same name of different types,
530 // because intrinsics must be a specific type.
531 Function *F =
532 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
533 getType(id, Tys, numTys)));
534 F->setParamAttrs(getParamAttrs(id));
535 return F;
Jim Laskey2682ea62007-02-07 20:38:26 +0000536}
537
Chris Lattner1ccab842004-10-12 04:32:37 +0000538Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000539 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000540 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000541 if (isa<PointerType>(CE->getOperand(0)->getType()))
542 return StripPointerCasts(CE->getOperand(0));
543 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
544 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
545 if (!CE->getOperand(i)->isNullValue())
546 return Ptr;
547 return StripPointerCasts(CE->getOperand(0));
548 }
549 return Ptr;
550 }
551
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000552 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000553 if (isa<PointerType>(CI->getOperand(0)->getType()))
554 return StripPointerCasts(CI->getOperand(0));
555 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000556 if (GEP->hasAllZeroIndices())
557 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000558 }
559 return Ptr;
560}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000561
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000562// vim: sw=2 ai