blob: 4d123b563a1912e15921ff61577e937c560b2402 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Function.cpp - Implement the Global object classes ----------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattner189d19f2003-11-21 20:23:48 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Chris Lattner9ed7aef2002-09-06 21:33:15 +000022BasicBlock *ilist_traits<BasicBlock>::createNode() {
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
33Argument *ilist_traits<Argument>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000034 Argument *Ret = new Argument(Type::IntTy);
35 // 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
Vikram S. Adve0267d0c2002-09-17 01:17:57 +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
64
Chris Lattnerd255ae22002-04-09 19:39:35 +000065// Specialize setName to take care of symbol table majik
66void Argument::setName(const std::string &name, SymbolTable *ST) {
67 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +000068 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Reid Spencerc49dd8d2004-07-17 23:50:19 +000069 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +000070 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000071 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000072 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000073}
74
Chris Lattner9ed7aef2002-09-06 21:33:15 +000075void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000076 if (getParent())
77 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000078 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000079 if (getParent())
80 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000081}
82
Chris Lattnerd255ae22002-04-09 19:39:35 +000083//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000084// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000085//===----------------------------------------------------------------------===//
86
Chris Lattner379a8d22003-04-16 20:28:45 +000087Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +000088 const std::string &name, Module *ParentModule)
Chris Lattner379a8d22003-04-16 20:28:45 +000089 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
Chris Lattner113f4f42002-06-25 16:13:24 +000090 BasicBlocks.setItemParent(this);
91 BasicBlocks.setParent(this);
92 ArgumentList.setItemParent(this);
93 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +000094 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000095
Chris Lattner3ae303c2003-11-21 22:32:23 +000096 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
97 && "LLVM functions cannot return aggregate values!");
98
Chris Lattner149376d2002-10-13 20:57:00 +000099 // Create the arguments vector, all arguments start out unnamed.
100 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
101 assert(Ty->getParamType(i) != Type::VoidTy &&
102 "Cannot have void typed arguments!");
103 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
104 }
105
Chris Lattner184b2982002-09-08 18:59:35 +0000106 // Make sure that we get added to a function
107 LeakDetector::addGarbageObject(this);
108
Chris Lattner6213ae02002-09-06 20:46:32 +0000109 if (ParentModule)
110 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000111}
112
Chris Lattner4e8c4872002-03-23 22:51:58 +0000113Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114 dropAllReferences(); // After this it is safe to delete instructions.
115
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000117 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000119 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120}
121
122// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000123void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000125 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000126 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000127 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128 Value::setName(name);
Chris Lattner570c69c2004-01-10 21:42:24 +0000129 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130}
131
Chris Lattner4e8c4872002-03-23 22:51:58 +0000132void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000133 if (getParent())
134 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000136 if (getParent())
137 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138}
139
Chris Lattner91db5822002-03-29 03:44:36 +0000140const FunctionType *Function::getFunctionType() const {
141 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000142}
143
Chris Lattner4e8c4872002-03-23 22:51:58 +0000144const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000145 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146}
147
Chris Lattner02a71e72004-10-11 22:21:39 +0000148void Function::removeFromParent() {
149 getParent()->getFunctionList().remove(this);
150}
151
152void Function::eraseFromParent() {
153 getParent()->getFunctionList().erase(this);
154}
155
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156// dropAllReferences() - This function causes all the subinstructions to "let
157// go" of all references that they are maintaining. This allows one to
158// 'delete' a whole class at a time, even though there may be circular
159// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000160// zero. Then everything is deleted for real. Note that no operations are
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161// valid on an object that has "dropped all references", except operator
162// delete.
163//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000164void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000165 for (iterator I = begin(), E = end(); I != E; ++I)
166 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000167 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168}
Chris Lattnerda975502001-09-10 07:58:01 +0000169
Chris Lattnerbb346d02003-05-08 03:47:33 +0000170/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000171/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000172/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000173/// zero to allow easy checking for whether a function is intrinsic or not. The
174/// particular intrinsic functions which correspond to this value are defined in
175/// llvm/Intrinsics.h.
176///
177unsigned Function::getIntrinsicID() const {
Chris Lattner3284ed72003-09-19 19:31:41 +0000178 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
Chris Lattnerbb346d02003-05-08 03:47:33 +0000179 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
180 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000181
182 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000183
Chris Lattner4159fdae2003-08-06 20:08:25 +0000184 // a table of all Alpha intrinsic functions
185 struct {
186 std::string name; // The name of the intrinsic
187 unsigned id; // Its ID number
188 } alpha_intrinsics[] = {
Brian Gaeke960707c2003-11-11 22:41:34 +0000189 { "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
190 { "llvm.alpha.cttz", Intrinsic::alpha_cttz },
191 { "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
192 { "llvm.alpha.umulh", Intrinsic::alpha_umulh },
193 { "llvm.alpha.vecop", Intrinsic::alpha_vecop },
194 { "llvm.alpha.pup", Intrinsic::alpha_pup },
195 { "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
196 { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
197 { "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
198 { "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
199 { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
200 { "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
201 { "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
202 { "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
203 { "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
Chris Lattner4159fdae2003-08-06 20:08:25 +0000204 };
205 const unsigned num_alpha_intrinsics =
206 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
207
Chris Lattnerbb346d02003-05-08 03:47:33 +0000208 switch (getName()[5]) {
Chris Lattner60104f02003-07-28 21:20:57 +0000209 case 'a':
Chris Lattnerade94102003-08-24 05:30:29 +0000210 if (getName().size() > 11 &&
211 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
212 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
213 if (getName() == alpha_intrinsics[i].name)
214 return alpha_intrinsics[i].id;
215 break;
Chris Lattner3d903f02004-01-05 05:36:30 +0000216 case 'd':
217 if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
218 if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
219 if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
220 if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
Chris Lattner59f1ef42004-01-06 05:33:02 +0000221 if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
Chris Lattner3d903f02004-01-05 05:36:30 +0000222 break;
Chris Lattnerdc632112004-02-14 02:47:17 +0000223 case 'f':
224 if (getName() == "llvm.frameaddress") return Intrinsic::frameaddress;
225 break;
Chris Lattner9c251eb2004-05-23 21:16:51 +0000226 case 'g':
227 if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
228 if (getName() == "llvm.gcread") return Intrinsic::gcread;
229 if (getName() == "llvm.gcroot") return Intrinsic::gcroot;
230 break;
Alkis Evlogimenosd0b5d0c2004-06-11 01:08:18 +0000231 case 'i':
Alkis Evlogimenos9d740622004-06-12 19:19:14 +0000232 if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
Alkis Evlogimenosd0b5d0c2004-06-11 01:08:18 +0000233 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000234 case 'l':
Brian Gaeke960707c2003-11-11 22:41:34 +0000235 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000236 break;
Chris Lattner17d028d2004-02-12 17:01:09 +0000237 case 'm':
238 if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
Chris Lattner5ed171e2004-02-12 18:11:20 +0000239 if (getName() == "llvm.memmove") return Intrinsic::memmove;
Chris Lattnerdc632112004-02-14 02:47:17 +0000240 if (getName() == "llvm.memset") return Intrinsic::memset;
241 break;
242 case 'r':
243 if (getName() == "llvm.returnaddress") return Intrinsic::returnaddress;
John Criswell52010042004-04-08 20:27:38 +0000244 if (getName() == "llvm.readport") return Intrinsic::readport;
John Criswell23c48d62004-04-14 13:46:52 +0000245 if (getName() == "llvm.readio") return Intrinsic::readio;
Chris Lattner17d028d2004-02-12 17:01:09 +0000246 break;
Chris Lattner192623e2003-05-17 22:26:33 +0000247 case 's':
Brian Gaeke960707c2003-11-11 22:41:34 +0000248 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
249 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
250 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
Chris Lattner192623e2003-05-17 22:26:33 +0000251 break;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000252 case 'v':
Chris Lattner071a5e52004-03-13 00:24:00 +0000253 if (getName() == "llvm.va_copy") return Intrinsic::vacopy;
254 if (getName() == "llvm.va_end") return Intrinsic::vaend;
255 if (getName() == "llvm.va_start") return Intrinsic::vastart;
John Criswell52010042004-04-08 20:27:38 +0000256 case 'w':
257 if (getName() == "llvm.writeport") return Intrinsic::writeport;
John Criswell23c48d62004-04-14 13:46:52 +0000258 if (getName() == "llvm.writeio") return Intrinsic::writeio;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000259 break;
260 }
261 // The "llvm." namespace is reserved!
262 assert(0 && "Unknown LLVM intrinsic function!");
263 return 0;
264}
265
Chris Lattner89046ca2004-10-12 04:20:25 +0000266Value *MemIntrinsic::StripPointerCasts(Value *Ptr) {
267 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
268 if (CE->getOpcode() == Instruction::Cast) {
269 if (isa<PointerType>(CE->getOperand(0)->getType()))
270 return StripPointerCasts(CE->getOperand(0));
271 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
272 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
273 if (!CE->getOperand(i)->isNullValue())
274 return Ptr;
275 return StripPointerCasts(CE->getOperand(0));
276 }
277 return Ptr;
278 }
279
280 if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
281 if (isa<PointerType>(CI->getOperand(0)->getType()))
282 return StripPointerCasts(CI->getOperand(0));
283 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
284 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
285 if (!isa<Constant>(CE->getOperand(i)) ||
286 !cast<Constant>(CE->getOperand(i))->isNullValue())
287 return Ptr;
288 return StripPointerCasts(CE->getOperand(0));
289 }
290 return Ptr;
291}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000292
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000293// vim: sw=2 ai