blob: ba07386f7e8ed033d8e4fd463efa1bf7870c4915 [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//
Chris Lattner57698e22002-03-26 18:01:55 +000010// This file implements the Function & GlobalVariable classes for the VMCore
Chris Lattnerda975502001-09-10 07:58:01 +000011// library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +000016#include "llvm/DerivedTypes.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() {
Chris Lattner184b2982002-09-08 18:59:35 +000035 Argument *Ret = new Argument(Type::IntTy);
36 // 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 Lattner9ed7aef2002-09-06 21:33:15 +000055 : Value(Ty, Value::ArgumentVal, Name) {
56 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);
63}
64
Chris Lattner9ed7aef2002-09-06 21:33:15 +000065void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000066 if (getParent())
67 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000068 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000069 if (getParent())
70 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000071}
72
Chris Lattnerd255ae22002-04-09 19:39:35 +000073//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000074// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000075//===----------------------------------------------------------------------===//
76
Chris Lattner379a8d22003-04-16 20:28:45 +000077Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +000078 const std::string &name, Module *ParentModule)
Chris Lattner5d1bc2c2005-01-29 00:35:33 +000079 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +000080 CallingConvention = 0;
Chris Lattner113f4f42002-06-25 16:13:24 +000081 BasicBlocks.setItemParent(this);
82 BasicBlocks.setParent(this);
83 ArgumentList.setItemParent(this);
84 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +000085 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000086
Chris Lattner3ae303c2003-11-21 22:32:23 +000087 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
88 && "LLVM functions cannot return aggregate values!");
89
Chris Lattner149376d2002-10-13 20:57:00 +000090 // Create the arguments vector, all arguments start out unnamed.
91 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
92 assert(Ty->getParamType(i) != Type::VoidTy &&
93 "Cannot have void typed arguments!");
94 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
95 }
96
Chris Lattner184b2982002-09-08 18:59:35 +000097 // Make sure that we get added to a function
98 LeakDetector::addGarbageObject(this);
99
Chris Lattner6213ae02002-09-06 20:46:32 +0000100 if (ParentModule)
101 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000102}
103
Chris Lattner4e8c4872002-03-23 22:51:58 +0000104Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105 dropAllReferences(); // After this it is safe to delete instructions.
106
Chris Lattner2f7c9632001-06-06 20:29:01 +0000107 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000108 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000110 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111}
112
Chris Lattner4e8c4872002-03-23 22:51:58 +0000113void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000114 if (getParent())
115 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000117 if (getParent())
118 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119}
120
Chris Lattner91db5822002-03-29 03:44:36 +0000121const FunctionType *Function::getFunctionType() const {
122 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000123}
124
Chris Lattner07b522d2005-01-07 07:40:32 +0000125bool Function::isVarArg() const {
126 return getFunctionType()->isVarArg();
127}
128
Misha Brukmanb1c93172005-04-21 23:48:37 +0000129const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000130 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131}
132
Chris Lattner02a71e72004-10-11 22:21:39 +0000133void Function::removeFromParent() {
134 getParent()->getFunctionList().remove(this);
135}
136
137void Function::eraseFromParent() {
138 getParent()->getFunctionList().erase(this);
139}
140
Chris Lattnerb392d302004-12-05 06:43:27 +0000141
142/// renameLocalSymbols - This method goes through the Function's symbol table
143/// and renames any symbols that conflict with symbols at global scope. This is
144/// required before printing out to a textual form, to ensure that there is no
145/// ambiguity when parsing.
146void Function::renameLocalSymbols() {
147 SymbolTable &LST = getSymbolTable(); // Local Symtab
148 SymbolTable &GST = getParent()->getSymbolTable(); // Global Symtab
149
150 for (SymbolTable::plane_iterator LPI = LST.plane_begin(), E = LST.plane_end();
151 LPI != E; ++LPI)
152 // All global symbols are of pointer type, ignore any non-pointer planes.
Chris Lattnered2fb1c2005-03-05 19:01:49 +0000153 if (const PointerType *CurTy = dyn_cast<PointerType>(LPI->first)) {
Chris Lattnerb392d302004-12-05 06:43:27 +0000154 // Only check if the global plane has any symbols of this type.
155 SymbolTable::plane_iterator GPI = GST.find(LPI->first);
156 if (GPI != GST.plane_end()) {
157 SymbolTable::ValueMap &LVM = LPI->second;
158 const SymbolTable::ValueMap &GVM = GPI->second;
159
160 // Loop over all local symbols, renaming those that are in the global
161 // symbol table already.
162 for (SymbolTable::value_iterator VI = LVM.begin(), E = LVM.end();
163 VI != E;) {
164 Value *V = VI->second;
165 const std::string &Name = VI->first;
166 ++VI;
167 if (GVM.count(Name)) {
168 static unsigned UniqueNum = 0;
169 // Find a name that does not conflict!
170 while (GVM.count(Name + "_" + utostr(++UniqueNum)) ||
171 LVM.count(Name + "_" + utostr(UniqueNum)))
172 /* scan for UniqueNum that works */;
173 V->setName(Name + "_" + utostr(UniqueNum));
174 }
175 }
176 }
177 }
178}
179
180
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181// dropAllReferences() - This function causes all the subinstructions to "let
182// go" of all references that they are maintaining. This allows one to
183// 'delete' a whole class at a time, even though there may be circular
184// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000185// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000186// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187// delete.
188//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000189void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000190 for (iterator I = begin(), E = end(); I != E; ++I)
191 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000192 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193}
Chris Lattnerda975502001-09-10 07:58:01 +0000194
Chris Lattnerbb346d02003-05-08 03:47:33 +0000195/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000196/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000197/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000198/// zero to allow easy checking for whether a function is intrinsic or not. The
199/// particular intrinsic functions which correspond to this value are defined in
200/// llvm/Intrinsics.h.
201///
202unsigned Function::getIntrinsicID() const {
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000203 const std::string& Name = this->getName();
204 if (Name.size() < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
205 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000206 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000207
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000208 assert(Name.size() != 5 && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000209
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000210#define GET_FUNCTION_RECOGNIZER
211#include "llvm/Intrinsics.gen"
212#undef GET_FUNCTION_RECOGNIZER
Chris Lattnerbb346d02003-05-08 03:47:33 +0000213 return 0;
214}
215
Chris Lattner1ccab842004-10-12 04:32:37 +0000216Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000217 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
218 if (CE->getOpcode() == Instruction::Cast) {
219 if (isa<PointerType>(CE->getOperand(0)->getType()))
220 return StripPointerCasts(CE->getOperand(0));
221 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
222 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
223 if (!CE->getOperand(i)->isNullValue())
224 return Ptr;
225 return StripPointerCasts(CE->getOperand(0));
226 }
227 return Ptr;
228 }
229
230 if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
231 if (isa<PointerType>(CI->getOperand(0)->getType()))
232 return StripPointerCasts(CI->getOperand(0));
233 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattner1ccab842004-10-12 04:32:37 +0000234 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
235 if (!isa<Constant>(GEP->getOperand(i)) ||
236 !cast<Constant>(GEP->getOperand(i))->isNullValue())
Chris Lattner89046ca2004-10-12 04:20:25 +0000237 return Ptr;
Chris Lattner1ccab842004-10-12 04:32:37 +0000238 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000239 }
240 return Ptr;
241}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000242
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000243// vim: sw=2 ai