blob: 04db3aa06c31fadba07a20f1939663fd7d7e488a [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"
Chris Lattner113f4f42002-06-25 16:13:24 +000020#include "SymbolTableListTraitsImpl.h"
Duncan Sands38ef3a82007-12-03 20:06:50 +000021#include "llvm/ADT/BitVector.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000022#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattnerf6c93e32005-01-30 00:09:23 +000025BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000026 BasicBlock *Ret = new BasicBlock();
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
29 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000030}
31
Chris Lattner113f4f42002-06-25 16:13:24 +000032iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
33 return F->getBasicBlockList();
34}
35
Chris Lattnerf6c93e32005-01-30 00:09:23 +000036Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000037 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000038 // This should not be garbage monitored.
39 LeakDetector::removeGarbageObject(Ret);
40 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000041}
42
43iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
44 return F->getArgumentList();
45}
46
47// Explicit instantiations of SymbolTableListTraits since some of the methods
48// are not in the public header file...
Chris Lattnerb47aa542007-04-17 03:26:42 +000049template class SymbolTableListTraits<Argument, Function>;
50template class SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000051
Chris Lattnerda975502001-09-10 07:58:01 +000052//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000053// Argument Implementation
54//===----------------------------------------------------------------------===//
55
Misha Brukmanb1c93172005-04-21 23:48:37 +000056Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000057 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000058 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000059
60 // Make sure that we get added to a function
61 LeakDetector::addGarbageObject(this);
62
Chris Lattner9ed7aef2002-09-06 21:33:15 +000063 if (Par)
64 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000065 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000066}
67
Chris Lattner9ed7aef2002-09-06 21:33:15 +000068void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000069 if (getParent())
70 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000071 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000072 if (getParent())
73 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000074}
75
Chris Lattnerd255ae22002-04-09 19:39:35 +000076//===----------------------------------------------------------------------===//
Reid Spencer019c8862007-04-09 15:01:12 +000077// ParamAttrsList Implementation
78//===----------------------------------------------------------------------===//
79
80uint16_t
81ParamAttrsList::getParamAttrs(uint16_t Index) const {
82 unsigned limit = attrs.size();
Duncan Sandsd4d7f9d2007-11-30 18:20:58 +000083 for (unsigned i = 0; i < limit && attrs[i].index <= Index; ++i)
Reid Spencer019c8862007-04-09 15:01:12 +000084 if (attrs[i].index == Index)
85 return attrs[i].attrs;
Reid Spencera472f662007-04-11 02:44:20 +000086 return ParamAttr::None;
Reid Spencer019c8862007-04-09 15:01:12 +000087}
88
Reid Spencer019c8862007-04-09 15:01:12 +000089std::string
90ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
91 std::string Result;
Reid Spencera472f662007-04-11 02:44:20 +000092 if (Attrs & ParamAttr::ZExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000093 Result += "zeroext ";
Reid Spencera472f662007-04-11 02:44:20 +000094 if (Attrs & ParamAttr::SExt)
Reid Spencer314e1cb2007-07-19 23:13:04 +000095 Result += "signext ";
Reid Spencera472f662007-04-11 02:44:20 +000096 if (Attrs & ParamAttr::NoReturn)
Reid Spencer019c8862007-04-09 15:01:12 +000097 Result += "noreturn ";
Reid Spencera472f662007-04-11 02:44:20 +000098 if (Attrs & ParamAttr::NoUnwind)
Reid Spencer019c8862007-04-09 15:01:12 +000099 Result += "nounwind ";
Reid Spencera472f662007-04-11 02:44:20 +0000100 if (Attrs & ParamAttr::InReg)
Reid Spencer019c8862007-04-09 15:01:12 +0000101 Result += "inreg ";
Zhou Sheng2444a9a2007-06-05 05:28:26 +0000102 if (Attrs & ParamAttr::NoAlias)
103 Result += "noalias ";
Reid Spencera472f662007-04-11 02:44:20 +0000104 if (Attrs & ParamAttr::StructRet)
Reid Spencer019c8862007-04-09 15:01:12 +0000105 Result += "sret ";
Rafael Espindolab567e3f2007-07-06 10:57:03 +0000106 if (Attrs & ParamAttr::ByVal)
107 Result += "byval ";
Duncan Sands644f9172007-07-27 12:58:54 +0000108 if (Attrs & ParamAttr::Nest)
109 Result += "nest ";
Duncan Sandsa89a1132007-11-22 20:23:04 +0000110 if (Attrs & ParamAttr::ReadNone)
111 Result += "readnone ";
112 if (Attrs & ParamAttr::ReadOnly)
113 Result += "readonly ";
Reid Spencer019c8862007-04-09 15:01:12 +0000114 return Result;
115}
116
Duncan Sands185eeac2007-11-25 14:10:56 +0000117/// onlyInformative - Returns whether only informative attributes are set.
118static inline bool onlyInformative(uint16_t attrs) {
119 return !(attrs & ~ParamAttr::Informative);
120}
121
122bool
123ParamAttrsList::areCompatible(const ParamAttrsList *A, const ParamAttrsList *B){
124 if (A == B)
125 return true;
126 unsigned ASize = A ? A->size() : 0;
127 unsigned BSize = B ? B->size() : 0;
128 unsigned AIndex = 0;
129 unsigned BIndex = 0;
130
131 while (AIndex < ASize && BIndex < BSize) {
132 uint16_t AIdx = A->getParamIndex(AIndex);
133 uint16_t BIdx = B->getParamIndex(BIndex);
134 uint16_t AAttrs = A->getParamAttrsAtIndex(AIndex);
135 uint16_t BAttrs = B->getParamAttrsAtIndex(AIndex);
136
137 if (AIdx < BIdx) {
138 if (!onlyInformative(AAttrs))
139 return false;
140 ++AIndex;
141 } else if (BIdx < AIdx) {
142 if (!onlyInformative(BAttrs))
143 return false;
144 ++BIndex;
145 } else {
146 if (!onlyInformative(AAttrs ^ BAttrs))
147 return false;
148 ++AIndex;
149 ++BIndex;
150 }
151 }
152 for (; AIndex < ASize; ++AIndex)
153 if (!onlyInformative(A->getParamAttrsAtIndex(AIndex)))
154 return false;
155 for (; BIndex < BSize; ++BIndex)
156 if (!onlyInformative(B->getParamAttrsAtIndex(AIndex)))
157 return false;
158 return true;
159}
160
Reid Spencer4388f0b2007-04-22 05:46:44 +0000161void
162ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
163 for (unsigned i = 0; i < attrs.size(); ++i) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000164 uint32_t val = uint32_t(attrs[i].attrs) << 16 | attrs[i].index;
Reid Spencer4388f0b2007-04-22 05:46:44 +0000165 ID.AddInteger(val);
166 }
Reid Spencer019c8862007-04-09 15:01:12 +0000167}
168
Reid Spencer4388f0b2007-04-22 05:46:44 +0000169static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
Reid Spencer019c8862007-04-09 15:01:12 +0000170
Duncan Sandsb41f8722007-11-30 18:19:18 +0000171const ParamAttrsList *
Reid Spencer4388f0b2007-04-22 05:46:44 +0000172ParamAttrsList::get(const ParamAttrsVector &attrVec) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000173 // If there are no attributes then return a null ParamAttrsList pointer.
174 if (attrVec.empty())
175 return 0;
176
Duncan Sandsd7813352007-09-11 14:40:04 +0000177#ifndef NDEBUG
Duncan Sands04eb67e2007-11-20 14:09:29 +0000178 for (unsigned i = 0, e = attrVec.size(); i < e; ++i) {
179 assert(attrVec[i].attrs != ParamAttr::None
180 && "Pointless parameter attribute!");
181 assert((!i || attrVec[i-1].index < attrVec[i].index)
182 && "Misordered ParamAttrsList!");
183 }
Duncan Sandsd7813352007-09-11 14:40:04 +0000184#endif
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000185
186 // Otherwise, build a key to look up the existing attributes.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000187 ParamAttrsList key(attrVec);
188 FoldingSetNodeID ID;
189 key.Profile(ID);
190 void *InsertPos;
191 ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000192
193 // If we didn't find any existing attributes of the same shape then
194 // create a new one and insert it.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000195 if (!PAL) {
196 PAL = new ParamAttrsList(attrVec);
197 ParamAttrsLists->InsertNode(PAL, InsertPos);
198 }
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000199
200 // Return the ParamAttrsList that we found or created.
Reid Spencer4388f0b2007-04-22 05:46:44 +0000201 return PAL;
Reid Spencer019c8862007-04-09 15:01:12 +0000202}
203
Duncan Sandsb41f8722007-11-30 18:19:18 +0000204const ParamAttrsList *
205ParamAttrsList::getModified(const ParamAttrsList *PAL,
206 const ParamAttrsVector &modVec) {
207 if (modVec.empty())
208 return PAL;
209
210#ifndef NDEBUG
211 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
212 assert((!i || modVec[i-1].index < modVec[i].index)
213 && "Misordered ParamAttrsList!");
214#endif
215
216 if (!PAL) {
217 // Strip any instances of ParamAttr::None from modVec before calling 'get'.
218 ParamAttrsVector newVec;
219 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
220 if (modVec[i].attrs != ParamAttr::None)
221 newVec.push_back(modVec[i]);
222 return get(newVec);
223 }
224
225 const ParamAttrsVector &oldVec = PAL->attrs;
226
227 ParamAttrsVector newVec;
228 unsigned oldI = 0;
229 unsigned modI = 0;
230 unsigned oldE = oldVec.size();
231 unsigned modE = modVec.size();
232
233 while (oldI < oldE && modI < modE) {
234 uint16_t oldIndex = oldVec[oldI].index;
235 uint16_t modIndex = modVec[modI].index;
236
237 if (oldIndex < modIndex) {
238 newVec.push_back(oldVec[oldI]);
239 ++oldI;
240 } else if (modIndex < oldIndex) {
241 if (modVec[modI].attrs != ParamAttr::None)
242 newVec.push_back(modVec[modI]);
243 ++modI;
244 } else {
245 // Same index - overwrite or delete existing attributes.
246 if (modVec[modI].attrs != ParamAttr::None)
247 newVec.push_back(modVec[modI]);
248 ++oldI;
249 ++modI;
250 }
251 }
252
253 for (; oldI < oldE; ++oldI)
254 newVec.push_back(oldVec[oldI]);
255 for (; modI < modE; ++modI)
256 if (modVec[modI].attrs != ParamAttr::None)
257 newVec.push_back(modVec[modI]);
258
259 return get(newVec);
260}
261
Reid Spencerc6a83842007-04-22 17:28:03 +0000262ParamAttrsList::~ParamAttrsList() {
263 ParamAttrsLists->RemoveNode(this);
264}
265
Reid Spencer019c8862007-04-09 15:01:12 +0000266//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000267// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000268//===----------------------------------------------------------------------===//
269
Chris Lattner379a8d22003-04-16 20:28:45 +0000270Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000271 const std::string &name, Module *ParentModule)
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000272 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name),
273 ParamAttrs(0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000274 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000275
Chris Lattner3ae303c2003-11-21 22:32:23 +0000276 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
277 && "LLVM functions cannot return aggregate values!");
278
Chris Lattnere2de9082007-08-18 06:14:52 +0000279 // If the function has arguments, mark them as lazily built.
280 if (Ty->getNumParams())
281 SubclassData = 1; // Set the "has lazy arguments" bit.
282
Chris Lattner184b2982002-09-08 18:59:35 +0000283 // Make sure that we get added to a function
284 LeakDetector::addGarbageObject(this);
285
Chris Lattner6213ae02002-09-06 20:46:32 +0000286 if (ParentModule)
287 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000288}
289
Chris Lattner4e8c4872002-03-23 22:51:58 +0000290Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291 dropAllReferences(); // After this it is safe to delete instructions.
292
Chris Lattner2f7c9632001-06-06 20:29:01 +0000293 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000294 ArgumentList.clear();
Chris Lattner2c8ff632002-04-28 04:51:51 +0000295 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000296
297 // Drop our reference to the parameter attributes, if any.
298 if (ParamAttrs)
299 ParamAttrs->dropRef();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000300}
301
Chris Lattnere2de9082007-08-18 06:14:52 +0000302void Function::BuildLazyArguments() const {
303 // Create the arguments vector, all arguments start out unnamed.
304 const FunctionType *FT = getFunctionType();
305 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
306 assert(FT->getParamType(i) != Type::VoidTy &&
307 "Cannot have void typed arguments!");
308 ArgumentList.push_back(new Argument(FT->getParamType(i)));
309 }
310
311 // Clear the lazy arguments bit.
312 const_cast<Function*>(this)->SubclassData &= ~1;
313}
314
315size_t Function::arg_size() const {
316 return getFunctionType()->getNumParams();
317}
318bool Function::arg_empty() const {
319 return getFunctionType()->getNumParams() == 0;
320}
321
Chris Lattner4e8c4872002-03-23 22:51:58 +0000322void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000323 if (getParent())
324 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000326 if (getParent())
327 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000328}
329
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000330void Function::setParamAttrs(const ParamAttrsList *attrs) {
331 // Avoid deleting the ParamAttrsList if they are setting the
332 // attributes to the same list.
333 if (ParamAttrs == attrs)
334 return;
335
336 // Drop reference on the old ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000337 if (ParamAttrs)
338 ParamAttrs->dropRef();
339
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000340 // Add reference to the new ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000341 if (attrs)
342 attrs->addRef();
343
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000344 // Set the new ParamAttrsList.
Reid Spencerc6a83842007-04-22 17:28:03 +0000345 ParamAttrs = attrs;
346}
347
Chris Lattner91db5822002-03-29 03:44:36 +0000348const FunctionType *Function::getFunctionType() const {
349 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000350}
351
Chris Lattner07b522d2005-01-07 07:40:32 +0000352bool Function::isVarArg() const {
353 return getFunctionType()->isVarArg();
354}
355
Misha Brukmanb1c93172005-04-21 23:48:37 +0000356const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000357 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000358}
359
Chris Lattner02a71e72004-10-11 22:21:39 +0000360void Function::removeFromParent() {
361 getParent()->getFunctionList().remove(this);
362}
363
364void Function::eraseFromParent() {
365 getParent()->getFunctionList().erase(this);
366}
367
Chris Lattner2f7c9632001-06-06 20:29:01 +0000368// dropAllReferences() - This function causes all the subinstructions to "let
369// go" of all references that they are maintaining. This allows one to
370// 'delete' a whole class at a time, even though there may be circular
371// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000372// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000373// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374// delete.
375//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000376void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000377 for (iterator I = begin(), E = end(); I != E; ++I)
378 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000379 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000380}
Chris Lattnerda975502001-09-10 07:58:01 +0000381
Chris Lattnerbb346d02003-05-08 03:47:33 +0000382/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000383/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000384/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000385/// zero to allow easy checking for whether a function is intrinsic or not. The
386/// particular intrinsic functions which correspond to this value are defined in
387/// llvm/Intrinsics.h.
388///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000389unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000390 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000391 if (!ValName)
392 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000393 unsigned Len = ValName->getKeyLength();
394 const char *Name = ValName->getKeyData();
395
Reid Spencer78d71f12007-04-16 16:56:54 +0000396 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000397 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000398 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000399
Reid Spencer9c2eec32007-04-16 06:54:34 +0000400 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000401
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000402#define GET_FUNCTION_RECOGNIZER
403#include "llvm/Intrinsics.gen"
404#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000405 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000406 return 0;
407}
408
Reid Spencer2a2117c2007-04-01 07:25:33 +0000409std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000410 assert(id < num_intrinsics && "Invalid intrinsic ID!");
411 const char * const Table[] = {
412 "not_intrinsic",
413#define GET_INTRINSIC_NAME_TABLE
414#include "llvm/Intrinsics.gen"
415#undef GET_INTRINSIC_NAME_TABLE
416 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000417 if (numTys == 0)
418 return Table[id];
419 std::string Result(Table[id]);
420 for (unsigned i = 0; i < numTys; ++i)
421 if (Tys[i])
Dan Gohman3a071482007-08-20 19:23:34 +0000422 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer2a2117c2007-04-01 07:25:33 +0000423 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000424}
425
Reid Spencer2a2117c2007-04-01 07:25:33 +0000426const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000427 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000428 const Type *ResultTy = NULL;
429 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000430 bool IsVarArg = false;
431
432#define GET_INTRINSIC_GENERATOR
433#include "llvm/Intrinsics.gen"
434#undef GET_INTRINSIC_GENERATOR
435
Reid Spencer26d9ff62007-04-09 06:11:23 +0000436 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000437}
438
Duncan Sands38ef3a82007-12-03 20:06:50 +0000439const ParamAttrsList *Intrinsic::getParamAttrs(ID id) {
440 static const ParamAttrsList *IntrinsicAttributes[Intrinsic::num_intrinsics];
441
442 if (IntrinsicAttributes[id])
443 return IntrinsicAttributes[id];
444
445 ParamAttrsVector Attrs;
446 uint16_t Attr = ParamAttr::None;
447
448#define GET_INTRINSIC_ATTRIBUTES
449#include "llvm/Intrinsics.gen"
450#undef GET_INTRINSIC_ATTRIBUTES
451
452 // Intrinsics cannot throw exceptions.
453 Attr |= ParamAttr::NoUnwind;
454
455 Attrs.push_back(ParamAttrsWithIndex::get(0, Attr));
456 IntrinsicAttributes[id] = ParamAttrsList::get(Attrs);
457 return IntrinsicAttributes[id];
458}
459
Reid Spencer2a2117c2007-04-01 07:25:33 +0000460Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
461 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000462 // There can never be multiple globals with the same name of different types,
463 // because intrinsics must be a specific type.
464 Function *F =
465 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
466 getType(id, Tys, numTys)));
467 F->setParamAttrs(getParamAttrs(id));
468 return F;
Jim Laskey2682ea62007-02-07 20:38:26 +0000469}
470
Chris Lattner1ccab842004-10-12 04:32:37 +0000471Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000472 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000473 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000474 if (isa<PointerType>(CE->getOperand(0)->getType()))
475 return StripPointerCasts(CE->getOperand(0));
476 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
477 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
478 if (!CE->getOperand(i)->isNullValue())
479 return Ptr;
480 return StripPointerCasts(CE->getOperand(0));
481 }
482 return Ptr;
483 }
484
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000485 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000486 if (isa<PointerType>(CI->getOperand(0)->getType()))
487 return StripPointerCasts(CI->getOperand(0));
488 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000489 if (GEP->hasAllZeroIndices())
490 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000491 }
492 return Ptr;
493}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000494
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000495// vim: sw=2 ai