blob: b9a139d75b4bb5fcacf160773fa5158fa9ff38fc [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"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000017#include "llvm/ParameterAttributes.h"
Dan Gohman3a071482007-08-20 19:23:34 +000018#include "llvm/CodeGen/ValueTypes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/LeakDetector.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//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +000079// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +000080//===----------------------------------------------------------------------===//
81
Chris Lattner3e13b8c2008-01-02 23:42:30 +000082const FunctionType *Function::getFunctionType() const {
83 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +000084}
85
Chris Lattner3e13b8c2008-01-02 23:42:30 +000086bool Function::isVarArg() const {
87 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +000088}
89
Chris Lattner3e13b8c2008-01-02 23:42:30 +000090const Type *Function::getReturnType() const {
91 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +000092}
93
Chris Lattner3e13b8c2008-01-02 23:42:30 +000094void Function::removeFromParent() {
95 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +000096}
97
Chris Lattner3e13b8c2008-01-02 23:42:30 +000098void Function::eraseFromParent() {
99 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000100}
101
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000102/// @brief Determine whether the function has the given attribute.
103bool Function::paramHasAttr(uint16_t i, unsigned attr) const {
104 return ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr);
Reid Spencer019c8862007-04-09 15:01:12 +0000105}
106
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000107/// @brief Determine if the function cannot return.
108bool Function::doesNotReturn() const {
109 return paramHasAttr(0, ParamAttr::NoReturn);
Duncan Sandsb41f8722007-11-30 18:19:18 +0000110}
111
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000112/// @brief Determine if the function cannot unwind.
113bool Function::doesNotThrow() const {
114 return paramHasAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000115}
116
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000117/// @brief Determine if the function does not access memory.
118bool Function::doesNotAccessMemory() const {
119 return paramHasAttr(0, ParamAttr::ReadNone);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000120}
121
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000122/// @brief Determine if the function does not access or only reads memory.
123bool Function::onlyReadsMemory() const {
124 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
125}
126
127/// @brief Determine if the function returns a structure.
128bool Function::isStructReturn() const {
129 return paramHasAttr(1, ParamAttr::StructRet);
Reid Spencerc6a83842007-04-22 17:28:03 +0000130}
131
Reid Spencer019c8862007-04-09 15:01:12 +0000132//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000133// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000134//===----------------------------------------------------------------------===//
135
Chris Lattner379a8d22003-04-16 20:28:45 +0000136Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000137 const std::string &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000138 : GlobalValue(PointerType::getUnqual(Ty),
139 Value::FunctionVal, 0, 0, Linkage, name),
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000140 ParamAttrs(0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000141 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000142
Chris Lattner3ae303c2003-11-21 22:32:23 +0000143 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
144 && "LLVM functions cannot return aggregate values!");
145
Chris Lattnere2de9082007-08-18 06:14:52 +0000146 // If the function has arguments, mark them as lazily built.
147 if (Ty->getNumParams())
148 SubclassData = 1; // Set the "has lazy arguments" bit.
149
Chris Lattner184b2982002-09-08 18:59:35 +0000150 // Make sure that we get added to a function
151 LeakDetector::addGarbageObject(this);
152
Chris Lattner6213ae02002-09-06 20:46:32 +0000153 if (ParentModule)
154 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000155}
156
Gordon Henriksen14a55692007-12-10 02:14:30 +0000157Function::~Function() {
158 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000159
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000161 ArgumentList.clear();
162 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000163
164 // Drop our reference to the parameter attributes, if any.
Gordon Henriksen14a55692007-12-10 02:14:30 +0000165 if (ParamAttrs)
166 ParamAttrs->dropRef();
Gordon Henriksen71183b62007-12-10 03:18:06 +0000167
168 // Remove the function from the on-the-side collector table.
169 clearCollector();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170}
171
Chris Lattnere2de9082007-08-18 06:14:52 +0000172void Function::BuildLazyArguments() const {
173 // Create the arguments vector, all arguments start out unnamed.
174 const FunctionType *FT = getFunctionType();
175 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
176 assert(FT->getParamType(i) != Type::VoidTy &&
177 "Cannot have void typed arguments!");
178 ArgumentList.push_back(new Argument(FT->getParamType(i)));
179 }
180
181 // Clear the lazy arguments bit.
182 const_cast<Function*>(this)->SubclassData &= ~1;
183}
184
185size_t Function::arg_size() const {
186 return getFunctionType()->getNumParams();
187}
188bool Function::arg_empty() const {
189 return getFunctionType()->getNumParams() == 0;
190}
191
Chris Lattner4e8c4872002-03-23 22:51:58 +0000192void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000193 if (getParent())
194 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000196 if (getParent())
197 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198}
199
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000200void Function::setParamAttrs(const ParamAttrsList *attrs) {
201 // Avoid deleting the ParamAttrsList if they are setting the
202 // attributes to the same list.
203 if (ParamAttrs == attrs)
204 return;
205
206 // Drop reference on the old ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000207 if (ParamAttrs)
208 ParamAttrs->dropRef();
209
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000210 // Add reference to the new ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000211 if (attrs)
212 attrs->addRef();
213
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000214 // Set the new ParamAttrsList.
Reid Spencerc6a83842007-04-22 17:28:03 +0000215 ParamAttrs = attrs;
216}
217
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218// dropAllReferences() - This function causes all the subinstructions to "let
219// go" of all references that they are maintaining. This allows one to
220// 'delete' a whole class at a time, even though there may be circular
221// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000222// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000223// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224// delete.
225//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000226void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000227 for (iterator I = begin(), E = end(); I != E; ++I)
228 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000229 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230}
Chris Lattnerda975502001-09-10 07:58:01 +0000231
Gordon Henriksen71183b62007-12-10 03:18:06 +0000232// Maintain the collector name for each function in an on-the-side table. This
233// saves allocating an additional word in Function for programs which do not use
234// GC (i.e., most programs) at the cost of increased overhead for clients which
235// do use GC.
236static DenseMap<const Function*,PooledStringPtr> *CollectorNames;
237static StringPool *CollectorNamePool;
238
239bool Function::hasCollector() const {
240 return CollectorNames && CollectorNames->count(this);
241}
242
243const char *Function::getCollector() const {
244 assert(hasCollector() && "Function has no collector");
245 return *(*CollectorNames)[this];
246}
247
248void Function::setCollector(const char *Str) {
249 if (!CollectorNamePool)
250 CollectorNamePool = new StringPool();
251 if (!CollectorNames)
252 CollectorNames = new DenseMap<const Function*,PooledStringPtr>();
253 (*CollectorNames)[this] = CollectorNamePool->intern(Str);
254}
255
256void Function::clearCollector() {
257 if (CollectorNames) {
258 CollectorNames->erase(this);
259 if (CollectorNames->empty()) {
260 delete CollectorNames;
261 CollectorNames = 0;
Gordon Henriksen4b904b92007-12-10 03:35:18 +0000262 if (CollectorNamePool->empty()) {
263 delete CollectorNamePool;
264 CollectorNamePool = 0;
265 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000266 }
267 }
268}
269
Chris Lattnerbb346d02003-05-08 03:47:33 +0000270/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000271/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000272/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000273/// zero to allow easy checking for whether a function is intrinsic or not. The
274/// particular intrinsic functions which correspond to this value are defined in
275/// llvm/Intrinsics.h.
276///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000277unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000278 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000279 if (!ValName)
280 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000281 unsigned Len = ValName->getKeyLength();
282 const char *Name = ValName->getKeyData();
283
Reid Spencer78d71f12007-04-16 16:56:54 +0000284 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000285 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000286 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000287
Reid Spencer9c2eec32007-04-16 06:54:34 +0000288 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000289
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000290#define GET_FUNCTION_RECOGNIZER
291#include "llvm/Intrinsics.gen"
292#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000293 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000294 return 0;
295}
296
Reid Spencer2a2117c2007-04-01 07:25:33 +0000297std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000298 assert(id < num_intrinsics && "Invalid intrinsic ID!");
299 const char * const Table[] = {
300 "not_intrinsic",
301#define GET_INTRINSIC_NAME_TABLE
302#include "llvm/Intrinsics.gen"
303#undef GET_INTRINSIC_NAME_TABLE
304 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000305 if (numTys == 0)
306 return Table[id];
307 std::string Result(Table[id]);
308 for (unsigned i = 0; i < numTys; ++i)
309 if (Tys[i])
Dan Gohman3a071482007-08-20 19:23:34 +0000310 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer2a2117c2007-04-01 07:25:33 +0000311 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000312}
313
Reid Spencer2a2117c2007-04-01 07:25:33 +0000314const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000315 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000316 const Type *ResultTy = NULL;
317 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000318 bool IsVarArg = false;
319
320#define GET_INTRINSIC_GENERATOR
321#include "llvm/Intrinsics.gen"
322#undef GET_INTRINSIC_GENERATOR
323
Reid Spencer26d9ff62007-04-09 06:11:23 +0000324 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000325}
326
Duncan Sands38ef3a82007-12-03 20:06:50 +0000327const ParamAttrsList *Intrinsic::getParamAttrs(ID id) {
328 static const ParamAttrsList *IntrinsicAttributes[Intrinsic::num_intrinsics];
329
330 if (IntrinsicAttributes[id])
331 return IntrinsicAttributes[id];
332
333 ParamAttrsVector Attrs;
334 uint16_t Attr = ParamAttr::None;
335
336#define GET_INTRINSIC_ATTRIBUTES
337#include "llvm/Intrinsics.gen"
338#undef GET_INTRINSIC_ATTRIBUTES
339
340 // Intrinsics cannot throw exceptions.
341 Attr |= ParamAttr::NoUnwind;
342
343 Attrs.push_back(ParamAttrsWithIndex::get(0, Attr));
344 IntrinsicAttributes[id] = ParamAttrsList::get(Attrs);
345 return IntrinsicAttributes[id];
346}
347
Reid Spencer2a2117c2007-04-01 07:25:33 +0000348Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
349 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000350 // There can never be multiple globals with the same name of different types,
351 // because intrinsics must be a specific type.
352 Function *F =
353 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
354 getType(id, Tys, numTys)));
355 F->setParamAttrs(getParamAttrs(id));
356 return F;
Jim Laskey2682ea62007-02-07 20:38:26 +0000357}
358
Chris Lattner1ccab842004-10-12 04:32:37 +0000359Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000360 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000361 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000362 if (isa<PointerType>(CE->getOperand(0)->getType()))
363 return StripPointerCasts(CE->getOperand(0));
364 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
365 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
366 if (!CE->getOperand(i)->isNullValue())
367 return Ptr;
368 return StripPointerCasts(CE->getOperand(0));
369 }
370 return Ptr;
371 }
372
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000373 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000374 if (isa<PointerType>(CI->getOperand(0)->getType()))
375 return StripPointerCasts(CI->getOperand(0));
376 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000377 if (GEP->hasAllZeroIndices())
378 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000379 }
380 return Ptr;
381}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000382
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000383// vim: sw=2 ai