blob: f985899b207a08dc9728b250bab5bff9c70bd48a [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
Reid Spencer4388f0b2007-04-22 05:46:44 +0000163void
164ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
165 for (unsigned i = 0; i < attrs.size(); ++i) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000166 uint32_t val = uint32_t(attrs[i].attrs) << 16 | attrs[i].index;
Reid Spencer4388f0b2007-04-22 05:46:44 +0000167 ID.AddInteger(val);
168 }
Reid Spencer019c8862007-04-09 15:01:12 +0000169}
170
Reid Spencer4388f0b2007-04-22 05:46:44 +0000171static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
Reid Spencer019c8862007-04-09 15:01:12 +0000172
Duncan Sandsb41f8722007-11-30 18:19:18 +0000173const ParamAttrsList *
Reid Spencer4388f0b2007-04-22 05:46:44 +0000174ParamAttrsList::get(const ParamAttrsVector &attrVec) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000175 // If there are no attributes then return a null ParamAttrsList pointer.
176 if (attrVec.empty())
177 return 0;
178
Duncan Sandsd7813352007-09-11 14:40:04 +0000179#ifndef NDEBUG
Duncan Sands04eb67e2007-11-20 14:09:29 +0000180 for (unsigned i = 0, e = attrVec.size(); i < e; ++i) {
181 assert(attrVec[i].attrs != ParamAttr::None
182 && "Pointless parameter attribute!");
183 assert((!i || attrVec[i-1].index < attrVec[i].index)
184 && "Misordered ParamAttrsList!");
185 }
Duncan Sandsd7813352007-09-11 14:40:04 +0000186#endif
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000187
188 // Otherwise, build a key to look up the existing attributes.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000189 ParamAttrsList key(attrVec);
190 FoldingSetNodeID ID;
191 key.Profile(ID);
192 void *InsertPos;
193 ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000194
195 // If we didn't find any existing attributes of the same shape then
196 // create a new one and insert it.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000197 if (!PAL) {
198 PAL = new ParamAttrsList(attrVec);
199 ParamAttrsLists->InsertNode(PAL, InsertPos);
200 }
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000201
202 // Return the ParamAttrsList that we found or created.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000203 return PAL;
Reid Spencer019c8862007-04-09 15:01:12 +0000204}
205
Duncan Sandsb41f8722007-11-30 18:19:18 +0000206const ParamAttrsList *
207ParamAttrsList::getModified(const ParamAttrsList *PAL,
208 const ParamAttrsVector &modVec) {
209 if (modVec.empty())
210 return PAL;
211
212#ifndef NDEBUG
213 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
214 assert((!i || modVec[i-1].index < modVec[i].index)
215 && "Misordered ParamAttrsList!");
216#endif
217
218 if (!PAL) {
219 // Strip any instances of ParamAttr::None from modVec before calling 'get'.
220 ParamAttrsVector newVec;
221 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
222 if (modVec[i].attrs != ParamAttr::None)
223 newVec.push_back(modVec[i]);
224 return get(newVec);
225 }
226
227 const ParamAttrsVector &oldVec = PAL->attrs;
228
229 ParamAttrsVector newVec;
230 unsigned oldI = 0;
231 unsigned modI = 0;
232 unsigned oldE = oldVec.size();
233 unsigned modE = modVec.size();
234
235 while (oldI < oldE && modI < modE) {
236 uint16_t oldIndex = oldVec[oldI].index;
237 uint16_t modIndex = modVec[modI].index;
238
239 if (oldIndex < modIndex) {
240 newVec.push_back(oldVec[oldI]);
241 ++oldI;
242 } else if (modIndex < oldIndex) {
243 if (modVec[modI].attrs != ParamAttr::None)
244 newVec.push_back(modVec[modI]);
245 ++modI;
246 } else {
247 // Same index - overwrite or delete existing attributes.
248 if (modVec[modI].attrs != ParamAttr::None)
249 newVec.push_back(modVec[modI]);
250 ++oldI;
251 ++modI;
252 }
253 }
254
255 for (; oldI < oldE; ++oldI)
256 newVec.push_back(oldVec[oldI]);
257 for (; modI < modE; ++modI)
258 if (modVec[modI].attrs != ParamAttr::None)
259 newVec.push_back(modVec[modI]);
260
261 return get(newVec);
262}
263
Reid Spencerc6a83842007-04-22 17:28:03 +0000264ParamAttrsList::~ParamAttrsList() {
265 ParamAttrsLists->RemoveNode(this);
266}
267
Reid Spencer019c8862007-04-09 15:01:12 +0000268//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000269// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000270//===----------------------------------------------------------------------===//
271
Chris Lattner379a8d22003-04-16 20:28:45 +0000272Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000273 const std::string &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000274 : GlobalValue(PointerType::getUnqual(Ty),
275 Value::FunctionVal, 0, 0, Linkage, name),
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000276 ParamAttrs(0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000277 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000278
Chris Lattner3ae303c2003-11-21 22:32:23 +0000279 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
280 && "LLVM functions cannot return aggregate values!");
281
Chris Lattnere2de9082007-08-18 06:14:52 +0000282 // If the function has arguments, mark them as lazily built.
283 if (Ty->getNumParams())
284 SubclassData = 1; // Set the "has lazy arguments" bit.
285
Chris Lattner184b2982002-09-08 18:59:35 +0000286 // Make sure that we get added to a function
287 LeakDetector::addGarbageObject(this);
288
Chris Lattner6213ae02002-09-06 20:46:32 +0000289 if (ParentModule)
290 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291}
292
Gordon Henriksen14a55692007-12-10 02:14:30 +0000293Function::~Function() {
294 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000297 ArgumentList.clear();
298 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000299
300 // Drop our reference to the parameter attributes, if any.
Gordon Henriksen14a55692007-12-10 02:14:30 +0000301 if (ParamAttrs)
302 ParamAttrs->dropRef();
Gordon Henriksen71183b62007-12-10 03:18:06 +0000303
304 // Remove the function from the on-the-side collector table.
305 clearCollector();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306}
307
Chris Lattnere2de9082007-08-18 06:14:52 +0000308void Function::BuildLazyArguments() const {
309 // Create the arguments vector, all arguments start out unnamed.
310 const FunctionType *FT = getFunctionType();
311 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
312 assert(FT->getParamType(i) != Type::VoidTy &&
313 "Cannot have void typed arguments!");
314 ArgumentList.push_back(new Argument(FT->getParamType(i)));
315 }
316
317 // Clear the lazy arguments bit.
318 const_cast<Function*>(this)->SubclassData &= ~1;
319}
320
321size_t Function::arg_size() const {
322 return getFunctionType()->getNumParams();
323}
324bool Function::arg_empty() const {
325 return getFunctionType()->getNumParams() == 0;
326}
327
Chris Lattner4e8c4872002-03-23 22:51:58 +0000328void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000329 if (getParent())
330 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000331 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000332 if (getParent())
333 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000334}
335
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000336void Function::setParamAttrs(const ParamAttrsList *attrs) {
337 // Avoid deleting the ParamAttrsList if they are setting the
338 // attributes to the same list.
339 if (ParamAttrs == attrs)
340 return;
341
342 // Drop reference on the old ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000343 if (ParamAttrs)
344 ParamAttrs->dropRef();
345
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000346 // Add reference to the new ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000347 if (attrs)
348 attrs->addRef();
349
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000350 // Set the new ParamAttrsList.
Reid Spencerc6a83842007-04-22 17:28:03 +0000351 ParamAttrs = attrs;
352}
353
Chris Lattner91db5822002-03-29 03:44:36 +0000354const FunctionType *Function::getFunctionType() const {
355 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000356}
357
Chris Lattner07b522d2005-01-07 07:40:32 +0000358bool Function::isVarArg() const {
359 return getFunctionType()->isVarArg();
360}
361
Misha Brukmanb1c93172005-04-21 23:48:37 +0000362const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000363 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000364}
365
Chris Lattner02a71e72004-10-11 22:21:39 +0000366void Function::removeFromParent() {
367 getParent()->getFunctionList().remove(this);
368}
369
370void Function::eraseFromParent() {
371 getParent()->getFunctionList().erase(this);
372}
373
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374// dropAllReferences() - This function causes all the subinstructions to "let
375// go" of all references that they are maintaining. This allows one to
376// 'delete' a whole class at a time, even though there may be circular
377// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000378// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000379// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000380// delete.
381//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000382void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000383 for (iterator I = begin(), E = end(); I != E; ++I)
384 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000385 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000386}
Chris Lattnerda975502001-09-10 07:58:01 +0000387
Gordon Henriksen71183b62007-12-10 03:18:06 +0000388// Maintain the collector name for each function in an on-the-side table. This
389// saves allocating an additional word in Function for programs which do not use
390// GC (i.e., most programs) at the cost of increased overhead for clients which
391// do use GC.
392static DenseMap<const Function*,PooledStringPtr> *CollectorNames;
393static StringPool *CollectorNamePool;
394
395bool Function::hasCollector() const {
396 return CollectorNames && CollectorNames->count(this);
397}
398
399const char *Function::getCollector() const {
400 assert(hasCollector() && "Function has no collector");
401 return *(*CollectorNames)[this];
402}
403
404void Function::setCollector(const char *Str) {
405 if (!CollectorNamePool)
406 CollectorNamePool = new StringPool();
407 if (!CollectorNames)
408 CollectorNames = new DenseMap<const Function*,PooledStringPtr>();
409 (*CollectorNames)[this] = CollectorNamePool->intern(Str);
410}
411
412void Function::clearCollector() {
413 if (CollectorNames) {
414 CollectorNames->erase(this);
415 if (CollectorNames->empty()) {
416 delete CollectorNames;
417 CollectorNames = 0;
Gordon Henriksen4b904b92007-12-10 03:35:18 +0000418 if (CollectorNamePool->empty()) {
419 delete CollectorNamePool;
420 CollectorNamePool = 0;
421 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000422 }
423 }
424}
425
Chris Lattnerbb346d02003-05-08 03:47:33 +0000426/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000427/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000428/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000429/// zero to allow easy checking for whether a function is intrinsic or not. The
430/// particular intrinsic functions which correspond to this value are defined in
431/// llvm/Intrinsics.h.
432///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000433unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000434 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000435 if (!ValName)
436 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000437 unsigned Len = ValName->getKeyLength();
438 const char *Name = ValName->getKeyData();
439
Reid Spencer78d71f12007-04-16 16:56:54 +0000440 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000441 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000442 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000443
Reid Spencer9c2eec32007-04-16 06:54:34 +0000444 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000445
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000446#define GET_FUNCTION_RECOGNIZER
447#include "llvm/Intrinsics.gen"
448#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000449 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000450 return 0;
451}
452
Reid Spencer2a2117c2007-04-01 07:25:33 +0000453std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000454 assert(id < num_intrinsics && "Invalid intrinsic ID!");
455 const char * const Table[] = {
456 "not_intrinsic",
457#define GET_INTRINSIC_NAME_TABLE
458#include "llvm/Intrinsics.gen"
459#undef GET_INTRINSIC_NAME_TABLE
460 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000461 if (numTys == 0)
462 return Table[id];
463 std::string Result(Table[id]);
464 for (unsigned i = 0; i < numTys; ++i)
465 if (Tys[i])
Dan Gohman3a071482007-08-20 19:23:34 +0000466 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer2a2117c2007-04-01 07:25:33 +0000467 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000468}
469
Reid Spencer2a2117c2007-04-01 07:25:33 +0000470const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000471 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000472 const Type *ResultTy = NULL;
473 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000474 bool IsVarArg = false;
475
476#define GET_INTRINSIC_GENERATOR
477#include "llvm/Intrinsics.gen"
478#undef GET_INTRINSIC_GENERATOR
479
Reid Spencer26d9ff62007-04-09 06:11:23 +0000480 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000481}
482
Duncan Sands38ef3a82007-12-03 20:06:50 +0000483const ParamAttrsList *Intrinsic::getParamAttrs(ID id) {
484 static const ParamAttrsList *IntrinsicAttributes[Intrinsic::num_intrinsics];
485
486 if (IntrinsicAttributes[id])
487 return IntrinsicAttributes[id];
488
489 ParamAttrsVector Attrs;
490 uint16_t Attr = ParamAttr::None;
491
492#define GET_INTRINSIC_ATTRIBUTES
493#include "llvm/Intrinsics.gen"
494#undef GET_INTRINSIC_ATTRIBUTES
495
496 // Intrinsics cannot throw exceptions.
497 Attr |= ParamAttr::NoUnwind;
498
499 Attrs.push_back(ParamAttrsWithIndex::get(0, Attr));
500 IntrinsicAttributes[id] = ParamAttrsList::get(Attrs);
501 return IntrinsicAttributes[id];
502}
503
Reid Spencer2a2117c2007-04-01 07:25:33 +0000504Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
505 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000506 // There can never be multiple globals with the same name of different types,
507 // because intrinsics must be a specific type.
508 Function *F =
509 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
510 getType(id, Tys, numTys)));
511 F->setParamAttrs(getParamAttrs(id));
512 return F;
Jim Laskey2682ea62007-02-07 20:38:26 +0000513}
514
Chris Lattner1ccab842004-10-12 04:32:37 +0000515Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000516 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000517 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000518 if (isa<PointerType>(CE->getOperand(0)->getType()))
519 return StripPointerCasts(CE->getOperand(0));
520 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
521 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
522 if (!CE->getOperand(i)->isNullValue())
523 return Ptr;
524 return StripPointerCasts(CE->getOperand(0));
525 }
526 return Ptr;
527 }
528
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000529 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000530 if (isa<PointerType>(CI->getOperand(0)->getType()))
531 return StripPointerCasts(CI->getOperand(0));
532 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000533 if (GEP->hasAllZeroIndices())
534 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000535 }
536 return Ptr;
537}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000538
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000539// vim: sw=2 ai