1 //===-- Function.cpp - Implement the Global object classes ----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Function & GlobalVariable classes for the VMCore
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Module.h"
16 #include "llvm/Constant.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/iOther.h"
19 #include "llvm/Intrinsics.h"
20 #include "Support/LeakDetector.h"
21 #include "SymbolTableListTraitsImpl.h"
24 BasicBlock *ilist_traits<BasicBlock>::createNode() {
25 BasicBlock *Ret = new BasicBlock();
26 // This should not be garbage monitored.
27 LeakDetector::removeGarbageObject(Ret);
31 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
32 return F->getBasicBlockList();
35 Argument *ilist_traits<Argument>::createNode() {
36 Argument *Ret = new Argument(Type::IntTy);
37 // This should not be garbage monitored.
38 LeakDetector::removeGarbageObject(Ret);
42 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
43 return F->getArgumentList();
46 // Explicit instantiations of SymbolTableListTraits since some of the methods
47 // are not in the public header file...
48 template class SymbolTableListTraits<Argument, Function, Function>;
49 template class SymbolTableListTraits<BasicBlock, Function, Function>;
51 //===----------------------------------------------------------------------===//
52 // Argument Implementation
53 //===----------------------------------------------------------------------===//
55 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
56 : Value(Ty, Value::ArgumentVal, Name) {
59 // Make sure that we get added to a function
60 LeakDetector::addGarbageObject(this);
63 Par->getArgumentList().push_back(this);
67 // Specialize setName to take care of symbol table majik
68 void Argument::setName(const std::string &name, SymbolTable *ST) {
70 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
71 "Invalid symtab argument!");
72 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
74 if (P && hasName()) P->getSymbolTable().insert(this);
77 void Argument::setParent(Function *parent) {
79 LeakDetector::addGarbageObject(this);
82 LeakDetector::removeGarbageObject(this);
85 static bool removeDeadConstantUsers(Constant *C) {
86 while (!C->use_empty()) {
87 if (Constant *CU = dyn_cast<Constant>(C->use_back())) {
88 if (!removeDeadConstantUsers(CU))
89 return false; // Constant wasn't dead.
91 return false; // Nonconstant user of the global.
100 /// removeDeadConstantUsers - If there are any dead constant users dangling
101 /// off of this global value, remove them. This method is useful for clients
102 /// that want to check to see if a global is unused, but don't want to deal
103 /// with potentially dead constants hanging off of the globals.
105 /// This function returns true if the global value is now dead. If all
106 /// users of this global are not dead, this method may return false and
107 /// leave some of them around.
108 bool GlobalValue::removeDeadConstantUsers() {
109 while (!use_empty()) {
110 if (Constant *C = dyn_cast<Constant>(use_back())) {
111 if (!::removeDeadConstantUsers(C))
112 return false; // Constant wasn't dead.
114 return false; // Nonconstant user of the global.
121 //===----------------------------------------------------------------------===//
122 // Function Implementation
123 //===----------------------------------------------------------------------===//
125 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
126 const std::string &name, Module *ParentModule)
127 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
128 BasicBlocks.setItemParent(this);
129 BasicBlocks.setParent(this);
130 ArgumentList.setItemParent(this);
131 ArgumentList.setParent(this);
132 SymTab = new SymbolTable();
134 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
135 && "LLVM functions cannot return aggregate values!");
137 // Create the arguments vector, all arguments start out unnamed.
138 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
139 assert(Ty->getParamType(i) != Type::VoidTy &&
140 "Cannot have void typed arguments!");
141 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
144 // Make sure that we get added to a function
145 LeakDetector::addGarbageObject(this);
148 ParentModule->getFunctionList().push_back(this);
151 Function::~Function() {
152 dropAllReferences(); // After this it is safe to delete instructions.
154 // Delete all of the method arguments and unlink from symbol table...
155 ArgumentList.clear();
156 ArgumentList.setParent(0);
160 // Specialize setName to take care of symbol table majik
161 void Function::setName(const std::string &name, SymbolTable *ST) {
163 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
164 "Invalid symtab argument!");
165 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
166 Value::setName(name);
167 if (P && hasName()) P->getSymbolTable().insert(this);
170 void Function::setParent(Module *parent) {
172 LeakDetector::addGarbageObject(this);
175 LeakDetector::removeGarbageObject(this);
178 const FunctionType *Function::getFunctionType() const {
179 return cast<FunctionType>(getType()->getElementType());
182 const Type *Function::getReturnType() const {
183 return getFunctionType()->getReturnType();
186 // dropAllReferences() - This function causes all the subinstructions to "let
187 // go" of all references that they are maintaining. This allows one to
188 // 'delete' a whole class at a time, even though there may be circular
189 // references... first all references are dropped, and all use counts go to
190 // zero. Then everything is deleted for real. Note that no operations are
191 // valid on an object that has "dropped all references", except operator
194 void Function::dropAllReferences() {
195 for (iterator I = begin(), E = end(); I != E; ++I)
196 I->dropAllReferences();
197 BasicBlocks.clear(); // Delete all basic blocks...
200 /// getIntrinsicID - This method returns the ID number of the specified
201 /// function, or Intrinsic::not_intrinsic if the function is not an
202 /// intrinsic, or if the pointer is null. This value is always defined to be
203 /// zero to allow easy checking for whether a function is intrinsic or not. The
204 /// particular intrinsic functions which correspond to this value are defined in
205 /// llvm/Intrinsics.h.
207 unsigned Function::getIntrinsicID() const {
208 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
209 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
210 return 0; // All intrinsics start with 'llvm.'
212 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
214 // a table of all Alpha intrinsic functions
216 std::string name; // The name of the intrinsic
217 unsigned id; // Its ID number
218 } alpha_intrinsics[] = {
219 { "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
220 { "llvm.alpha.cttz", Intrinsic::alpha_cttz },
221 { "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
222 { "llvm.alpha.umulh", Intrinsic::alpha_umulh },
223 { "llvm.alpha.vecop", Intrinsic::alpha_vecop },
224 { "llvm.alpha.pup", Intrinsic::alpha_pup },
225 { "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
226 { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
227 { "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
228 { "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
229 { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
230 { "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
231 { "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
232 { "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
233 { "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
235 const unsigned num_alpha_intrinsics =
236 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
238 switch (getName()[5]) {
240 if (getName().size() > 11 &&
241 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
242 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
243 if (getName() == alpha_intrinsics[i].name)
244 return alpha_intrinsics[i].id;
247 if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
248 if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
249 if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
250 if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
251 if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
254 if (getName() == "llvm.frameaddress") return Intrinsic::frameaddress;
257 if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
258 if (getName() == "llvm.gcread") return Intrinsic::gcread;
259 if (getName() == "llvm.gcroot") return Intrinsic::gcroot;
262 if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
265 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
268 if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
269 if (getName() == "llvm.memmove") return Intrinsic::memmove;
270 if (getName() == "llvm.memset") return Intrinsic::memset;
273 if (getName() == "llvm.returnaddress") return Intrinsic::returnaddress;
274 if (getName() == "llvm.readport") return Intrinsic::readport;
275 if (getName() == "llvm.readio") return Intrinsic::readio;
278 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
279 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
280 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
283 if (getName() == "llvm.va_copy") return Intrinsic::vacopy;
284 if (getName() == "llvm.va_end") return Intrinsic::vaend;
285 if (getName() == "llvm.va_start") return Intrinsic::vastart;
287 if (getName() == "llvm.writeport") return Intrinsic::writeport;
288 if (getName() == "llvm.writeio") return Intrinsic::writeio;
291 // The "llvm." namespace is reserved!
292 assert(0 && "Unknown LLVM intrinsic function!");
297 //===----------------------------------------------------------------------===//
298 // GlobalVariable Implementation
299 //===----------------------------------------------------------------------===//
301 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
302 Constant *Initializer,
303 const std::string &Name, Module *ParentModule)
304 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
305 isConstantGlobal(constant) {
306 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
308 LeakDetector::addGarbageObject(this);
311 ParentModule->getGlobalList().push_back(this);
314 void GlobalVariable::setParent(Module *parent) {
316 LeakDetector::addGarbageObject(this);
319 LeakDetector::removeGarbageObject(this);
322 // Specialize setName to take care of symbol table majik
323 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
325 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
326 "Invalid symtab argument!");
327 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
328 Value::setName(name);
329 if (P && hasName()) P->getSymbolTable().insert(this);