blob: edce58d6aaa103ec7b1c150b64f85f017b5ca4da [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"
Reid Spencer26d9ff62007-04-09 06:11:23 +000016#include "llvm/ParameterAttributes.h"
Chris Lattner89046ca2004-10-12 04:20:25 +000017#include "llvm/IntrinsicInst.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000019#include "SymbolTableListTraitsImpl.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattnerf6c93e32005-01-30 00:09:23 +000023BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000024 BasicBlock *Ret = new BasicBlock();
25 // This should not be garbage monitored.
26 LeakDetector::removeGarbageObject(Ret);
27 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000028}
29
Chris Lattner113f4f42002-06-25 16:13:24 +000030iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
31 return F->getBasicBlockList();
32}
33
Chris Lattnerf6c93e32005-01-30 00:09:23 +000034Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000035 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000036 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
38 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000039}
40
41iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
42 return F->getArgumentList();
43}
44
45// Explicit instantiations of SymbolTableListTraits since some of the methods
46// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000047template class SymbolTableListTraits<Argument, Function, Function>;
48template class SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
Chris Lattnerda975502001-09-10 07:58:01 +000050//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000051// Argument Implementation
52//===----------------------------------------------------------------------===//
53
Misha Brukmanb1c93172005-04-21 23:48:37 +000054Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000055 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000056 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000057
58 // Make sure that we get added to a function
59 LeakDetector::addGarbageObject(this);
60
Chris Lattner9ed7aef2002-09-06 21:33:15 +000061 if (Par)
62 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000063 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000064}
65
Chris Lattner9ed7aef2002-09-06 21:33:15 +000066void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000067 if (getParent())
68 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000069 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000070 if (getParent())
71 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000072}
73
Chris Lattnerd255ae22002-04-09 19:39:35 +000074//===----------------------------------------------------------------------===//
Reid Spencer019c8862007-04-09 15:01:12 +000075// ParamAttrsList Implementation
76//===----------------------------------------------------------------------===//
77
78uint16_t
79ParamAttrsList::getParamAttrs(uint16_t Index) const {
80 unsigned limit = attrs.size();
81 for (unsigned i = 0; i < limit; ++i)
82 if (attrs[i].index == Index)
83 return attrs[i].attrs;
Reid Spencera472f662007-04-11 02:44:20 +000084 return ParamAttr::None;
Reid Spencer019c8862007-04-09 15:01:12 +000085}
86
87
88std::string
89ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
90 std::string Result;
Reid Spencera472f662007-04-11 02:44:20 +000091 if (Attrs & ParamAttr::ZExt)
Reid Spencer019c8862007-04-09 15:01:12 +000092 Result += "zext ";
Reid Spencera472f662007-04-11 02:44:20 +000093 if (Attrs & ParamAttr::SExt)
Reid Spencer019c8862007-04-09 15:01:12 +000094 Result += "sext ";
Reid Spencera472f662007-04-11 02:44:20 +000095 if (Attrs & ParamAttr::NoReturn)
Reid Spencer019c8862007-04-09 15:01:12 +000096 Result += "noreturn ";
Reid Spencera472f662007-04-11 02:44:20 +000097 if (Attrs & ParamAttr::NoUnwind)
Reid Spencer019c8862007-04-09 15:01:12 +000098 Result += "nounwind ";
Reid Spencera472f662007-04-11 02:44:20 +000099 if (Attrs & ParamAttr::InReg)
Reid Spencer019c8862007-04-09 15:01:12 +0000100 Result += "inreg ";
Reid Spencera472f662007-04-11 02:44:20 +0000101 if (Attrs & ParamAttr::StructRet)
Reid Spencer019c8862007-04-09 15:01:12 +0000102 Result += "sret ";
103 return Result;
104}
105
106void
107ParamAttrsList::addAttributes(uint16_t Index, uint16_t Attrs) {
108 // First, try to replace an existing one
109 for (unsigned i = 0; i < attrs.size(); ++i)
110 if (attrs[i].index == Index) {
111 attrs[i].attrs |= Attrs;
112 return;
113 }
114
115 // If not found, add a new one
116 ParamAttrsWithIndex Val;
117 Val.attrs = Attrs;
118 Val.index = Index;
119 attrs.push_back(Val);
120}
121
122void
123ParamAttrsList::removeAttributes(uint16_t Index, uint16_t Attrs) {
124 // Find the index from which to remove the attributes
125 for (unsigned i = 0; i < attrs.size(); ++i)
126 if (attrs[i].index == Index) {
127 attrs[i].attrs &= ~Attrs;
Reid Spencera472f662007-04-11 02:44:20 +0000128 if (attrs[i].attrs == ParamAttr::None)
Reid Spencer019c8862007-04-09 15:01:12 +0000129 attrs.erase(&attrs[i]);
130 return;
131 }
132
133 // The index wasn't found above
134 assert(0 && "Index not found for removeAttributes");
135}
136
137//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000138// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000139//===----------------------------------------------------------------------===//
140
Chris Lattner379a8d22003-04-16 20:28:45 +0000141Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000142 const std::string &name, Module *ParentModule)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +0000143 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
Reid Spencer019c8862007-04-09 15:01:12 +0000144 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000145 CallingConvention = 0;
Chris Lattner113f4f42002-06-25 16:13:24 +0000146 BasicBlocks.setItemParent(this);
147 BasicBlocks.setParent(this);
148 ArgumentList.setItemParent(this);
149 ArgumentList.setParent(this);
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000150 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000151
Chris Lattner3ae303c2003-11-21 22:32:23 +0000152 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
153 && "LLVM functions cannot return aggregate values!");
154
Chris Lattner149376d2002-10-13 20:57:00 +0000155 // Create the arguments vector, all arguments start out unnamed.
156 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
157 assert(Ty->getParamType(i) != Type::VoidTy &&
158 "Cannot have void typed arguments!");
159 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
160 }
161
Chris Lattner184b2982002-09-08 18:59:35 +0000162 // Make sure that we get added to a function
163 LeakDetector::addGarbageObject(this);
164
Chris Lattner6213ae02002-09-06 20:46:32 +0000165 if (ParentModule)
166 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000167}
168
Chris Lattner4e8c4872002-03-23 22:51:58 +0000169Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170 dropAllReferences(); // After this it is safe to delete instructions.
171
Chris Lattner2f7c9632001-06-06 20:29:01 +0000172 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000173 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000174 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000175 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000176}
177
Chris Lattner4e8c4872002-03-23 22:51:58 +0000178void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000179 if (getParent())
180 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000182 if (getParent())
183 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184}
185
Chris Lattner91db5822002-03-29 03:44:36 +0000186const FunctionType *Function::getFunctionType() const {
187 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000188}
189
Chris Lattner07b522d2005-01-07 07:40:32 +0000190bool Function::isVarArg() const {
191 return getFunctionType()->isVarArg();
192}
193
Misha Brukmanb1c93172005-04-21 23:48:37 +0000194const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000195 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196}
197
Chris Lattner02a71e72004-10-11 22:21:39 +0000198void Function::removeFromParent() {
199 getParent()->getFunctionList().remove(this);
200}
201
202void Function::eraseFromParent() {
203 getParent()->getFunctionList().erase(this);
204}
205
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206// dropAllReferences() - This function causes all the subinstructions to "let
207// go" of all references that they are maintaining. This allows one to
208// 'delete' a whole class at a time, even though there may be circular
209// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000210// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000211// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212// delete.
213//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000214void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000215 for (iterator I = begin(), E = end(); I != E; ++I)
216 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000217 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218}
Chris Lattnerda975502001-09-10 07:58:01 +0000219
Chris Lattnerbb346d02003-05-08 03:47:33 +0000220/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000221/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000222/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000223/// zero to allow easy checking for whether a function is intrinsic or not. The
224/// particular intrinsic functions which correspond to this value are defined in
225/// llvm/Intrinsics.h.
226///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000227unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000228 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000229 if (!ValName)
230 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000231 unsigned Len = ValName->getKeyLength();
232 const char *Name = ValName->getKeyData();
233
Reid Spencer78d71f12007-04-16 16:56:54 +0000234 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000235 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000236 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000237
Reid Spencer9c2eec32007-04-16 06:54:34 +0000238 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000239
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000240#define GET_FUNCTION_RECOGNIZER
241#include "llvm/Intrinsics.gen"
242#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000243 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000244 return 0;
245}
246
Reid Spencer2a2117c2007-04-01 07:25:33 +0000247std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000248 assert(id < num_intrinsics && "Invalid intrinsic ID!");
249 const char * const Table[] = {
250 "not_intrinsic",
251#define GET_INTRINSIC_NAME_TABLE
252#include "llvm/Intrinsics.gen"
253#undef GET_INTRINSIC_NAME_TABLE
254 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000255 if (numTys == 0)
256 return Table[id];
257 std::string Result(Table[id]);
258 for (unsigned i = 0; i < numTys; ++i)
259 if (Tys[i])
260 Result += "." + Tys[i]->getDescription();
261 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000262}
263
Reid Spencer2a2117c2007-04-01 07:25:33 +0000264const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
265 uint32_t numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000266 const Type *ResultTy = NULL;
267 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000268 bool IsVarArg = false;
269
270#define GET_INTRINSIC_GENERATOR
271#include "llvm/Intrinsics.gen"
272#undef GET_INTRINSIC_GENERATOR
273
Reid Spencer26d9ff62007-04-09 06:11:23 +0000274 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000275}
276
Reid Spencer2a2117c2007-04-01 07:25:33 +0000277Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
278 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000279// There can never be multiple globals with the same name of different types,
280// because intrinsics must be a specific type.
Reid Spencer2a2117c2007-04-01 07:25:33 +0000281 return cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
282 getType(id, Tys, numTys)));
Jim Laskey2682ea62007-02-07 20:38:26 +0000283}
284
Chris Lattner1ccab842004-10-12 04:32:37 +0000285Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000286 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000287 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000288 if (isa<PointerType>(CE->getOperand(0)->getType()))
289 return StripPointerCasts(CE->getOperand(0));
290 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
291 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
292 if (!CE->getOperand(i)->isNullValue())
293 return Ptr;
294 return StripPointerCasts(CE->getOperand(0));
295 }
296 return Ptr;
297 }
298
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000299 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000300 if (isa<PointerType>(CI->getOperand(0)->getType()))
301 return StripPointerCasts(CI->getOperand(0));
302 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattner1ccab842004-10-12 04:32:37 +0000303 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
304 if (!isa<Constant>(GEP->getOperand(i)) ||
305 !cast<Constant>(GEP->getOperand(i))->isNullValue())
Chris Lattner89046ca2004-10-12 04:20:25 +0000306 return Ptr;
Chris Lattner1ccab842004-10-12 04:32:37 +0000307 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000308 }
309 return Ptr;
310}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000311
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000312// vim: sw=2 ai