blob: cc0cefad66bc58c3478a6a797e1ff39f5d451dce [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000018#include "SymbolTableListTraitsImpl.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000019#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chris Lattnerf6c93e32005-01-30 00:09:23 +000022BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000023 BasicBlock *Ret = new BasicBlock();
24 // This should not be garbage monitored.
25 LeakDetector::removeGarbageObject(Ret);
26 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000027}
28
Chris Lattner113f4f42002-06-25 16:13:24 +000029iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
30 return F->getBasicBlockList();
31}
32
Chris Lattnerf6c93e32005-01-30 00:09:23 +000033Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000034 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000035 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret);
37 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000038}
39
40iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
41 return F->getArgumentList();
42}
43
44// Explicit instantiations of SymbolTableListTraits since some of the methods
45// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000046template class SymbolTableListTraits<Argument, Function, Function>;
47template class SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000048
Chris Lattnerda975502001-09-10 07:58:01 +000049//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000050// Argument Implementation
51//===----------------------------------------------------------------------===//
52
Misha Brukmanb1c93172005-04-21 23:48:37 +000053Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner9ed7aef2002-09-06 21:33:15 +000054 : Value(Ty, Value::ArgumentVal, Name) {
55 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000056
57 // Make sure that we get added to a function
58 LeakDetector::addGarbageObject(this);
59
Chris Lattner9ed7aef2002-09-06 21:33:15 +000060 if (Par)
61 Par->getArgumentList().push_back(this);
62}
63
Chris Lattner9ed7aef2002-09-06 21:33:15 +000064void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000065 if (getParent())
66 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000067 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000068 if (getParent())
69 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000070}
71
Chris Lattnerd255ae22002-04-09 19:39:35 +000072//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000073// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000074//===----------------------------------------------------------------------===//
75
Chris Lattner379a8d22003-04-16 20:28:45 +000076Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +000077 const std::string &name, Module *ParentModule)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000078 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +000079 CallingConvention = 0;
Chris Lattner113f4f42002-06-25 16:13:24 +000080 BasicBlocks.setItemParent(this);
81 BasicBlocks.setParent(this);
82 ArgumentList.setItemParent(this);
83 ArgumentList.setParent(this);
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000084 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000085
Chris Lattner3ae303c2003-11-21 22:32:23 +000086 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
87 && "LLVM functions cannot return aggregate values!");
88
Chris Lattner149376d2002-10-13 20:57:00 +000089 // Create the arguments vector, all arguments start out unnamed.
90 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
91 assert(Ty->getParamType(i) != Type::VoidTy &&
92 "Cannot have void typed arguments!");
93 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
94 }
95
Chris Lattner184b2982002-09-08 18:59:35 +000096 // Make sure that we get added to a function
97 LeakDetector::addGarbageObject(this);
98
Chris Lattner6213ae02002-09-06 20:46:32 +000099 if (ParentModule)
100 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101}
102
Chris Lattner4e8c4872002-03-23 22:51:58 +0000103Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 dropAllReferences(); // After this it is safe to delete instructions.
105
Chris Lattner2f7c9632001-06-06 20:29:01 +0000106 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000107 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000109 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110}
111
Chris Lattner4e8c4872002-03-23 22:51:58 +0000112void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000113 if (getParent())
114 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000116 if (getParent())
117 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118}
119
Chris Lattner91db5822002-03-29 03:44:36 +0000120const FunctionType *Function::getFunctionType() const {
121 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000122}
123
Chris Lattner07b522d2005-01-07 07:40:32 +0000124bool Function::isVarArg() const {
125 return getFunctionType()->isVarArg();
126}
127
Misha Brukmanb1c93172005-04-21 23:48:37 +0000128const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000129 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130}
131
Chris Lattner02a71e72004-10-11 22:21:39 +0000132void Function::removeFromParent() {
133 getParent()->getFunctionList().remove(this);
134}
135
136void Function::eraseFromParent() {
137 getParent()->getFunctionList().erase(this);
138}
139
Chris Lattner2f7c9632001-06-06 20:29:01 +0000140// dropAllReferences() - This function causes all the subinstructions to "let
141// go" of all references that they are maintaining. This allows one to
142// 'delete' a whole class at a time, even though there may be circular
143// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000144// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000145// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146// delete.
147//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000148void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000149 for (iterator I = begin(), E = end(); I != E; ++I)
150 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000151 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152}
Chris Lattnerda975502001-09-10 07:58:01 +0000153
Chris Lattnerbb346d02003-05-08 03:47:33 +0000154/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000155/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000156/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000157/// zero to allow easy checking for whether a function is intrinsic or not. The
158/// particular intrinsic functions which correspond to this value are defined in
159/// llvm/Intrinsics.h.
160///
161unsigned Function::getIntrinsicID() const {
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000162 const std::string& Name = this->getName();
163 if (Name.size() < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
164 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000165 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000166
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000167 assert(Name.size() != 5 && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000168
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000169#define GET_FUNCTION_RECOGNIZER
170#include "llvm/Intrinsics.gen"
171#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerbb346d02003-05-08 03:47:33 +0000172 return 0;
173}
174
Chris Lattner71b8c982006-03-25 06:32:47 +0000175const char *Intrinsic::getName(ID id) {
176 assert(id < num_intrinsics && "Invalid intrinsic ID!");
177 const char * const Table[] = {
178 "not_intrinsic",
179#define GET_INTRINSIC_NAME_TABLE
180#include "llvm/Intrinsics.gen"
181#undef GET_INTRINSIC_NAME_TABLE
182 };
183 return Table[id];
184}
185
Chris Lattner1ccab842004-10-12 04:32:37 +0000186Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000187 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000188 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000189 if (isa<PointerType>(CE->getOperand(0)->getType()))
190 return StripPointerCasts(CE->getOperand(0));
191 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
192 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
193 if (!CE->getOperand(i)->isNullValue())
194 return Ptr;
195 return StripPointerCasts(CE->getOperand(0));
196 }
197 return Ptr;
198 }
199
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000200 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000201 if (isa<PointerType>(CI->getOperand(0)->getType()))
202 return StripPointerCasts(CI->getOperand(0));
203 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattner1ccab842004-10-12 04:32:37 +0000204 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
205 if (!isa<Constant>(GEP->getOperand(i)) ||
206 !cast<Constant>(GEP->getOperand(i))->isNullValue())
Chris Lattner89046ca2004-10-12 04:20:25 +0000207 return Ptr;
Chris Lattner1ccab842004-10-12 04:32:37 +0000208 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000209 }
210 return Ptr;
211}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000212
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000213// vim: sw=2 ai