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/DerivedTypes.h"
17 #include "llvm/iOther.h"
18 #include "llvm/Intrinsics.h"
19 #include "Support/LeakDetector.h"
20 #include "SymbolTableListTraitsImpl.h"
22 BasicBlock *ilist_traits<BasicBlock>::createNode() {
23 BasicBlock *Ret = new BasicBlock();
24 // This should not be garbage monitored.
25 LeakDetector::removeGarbageObject(Ret);
29 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
30 return F->getBasicBlockList();
33 Argument *ilist_traits<Argument>::createNode() {
34 Argument *Ret = new Argument(Type::IntTy);
35 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret);
40 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
41 return F->getArgumentList();
44 // Explicit instantiations of SymbolTableListTraits since some of the methods
45 // are not in the public header file...
46 template SymbolTableListTraits<Argument, Function, Function>;
47 template SymbolTableListTraits<BasicBlock, Function, Function>;
49 //===----------------------------------------------------------------------===//
50 // Argument Implementation
51 //===----------------------------------------------------------------------===//
53 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
54 : Value(Ty, Value::ArgumentVal, Name) {
57 // Make sure that we get added to a function
58 LeakDetector::addGarbageObject(this);
61 Par->getArgumentList().push_back(this);
65 // Specialize setName to take care of symbol table majik
66 void Argument::setName(const std::string &name, SymbolTable *ST) {
68 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
69 "Invalid symtab argument!");
70 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
72 if (P && hasName()) P->getSymbolTable().insert(this);
75 void Argument::setParent(Function *parent) {
77 LeakDetector::addGarbageObject(this);
80 LeakDetector::removeGarbageObject(this);
84 //===----------------------------------------------------------------------===//
85 // Function Implementation
86 //===----------------------------------------------------------------------===//
88 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
89 const std::string &name, Module *ParentModule)
90 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
91 BasicBlocks.setItemParent(this);
92 BasicBlocks.setParent(this);
93 ArgumentList.setItemParent(this);
94 ArgumentList.setParent(this);
95 SymTab = new SymbolTable();
97 // Create the arguments vector, all arguments start out unnamed.
98 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
99 assert(Ty->getParamType(i) != Type::VoidTy &&
100 "Cannot have void typed arguments!");
101 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
104 // Make sure that we get added to a function
105 LeakDetector::addGarbageObject(this);
108 ParentModule->getFunctionList().push_back(this);
111 Function::~Function() {
112 dropAllReferences(); // After this it is safe to delete instructions.
114 // Delete all of the method arguments and unlink from symbol table...
115 ArgumentList.clear();
116 ArgumentList.setParent(0);
120 // Specialize setName to take care of symbol table majik
121 void Function::setName(const std::string &name, SymbolTable *ST) {
123 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
124 "Invalid symtab argument!");
125 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
126 Value::setName(name);
127 if (P && getName() != "") P->getSymbolTable().insert(this);
130 void Function::setParent(Module *parent) {
132 LeakDetector::addGarbageObject(this);
135 LeakDetector::removeGarbageObject(this);
138 const FunctionType *Function::getFunctionType() const {
139 return cast<FunctionType>(getType()->getElementType());
142 const Type *Function::getReturnType() const {
143 return getFunctionType()->getReturnType();
146 // dropAllReferences() - This function causes all the subinstructions to "let
147 // go" of all references that they are maintaining. This allows one to
148 // 'delete' a whole class at a time, even though there may be circular
149 // references... first all references are dropped, and all use counts go to
150 // zero. Then everything is deleted for real. Note that no operations are
151 // valid on an object that has "dropped all references", except operator
154 void Function::dropAllReferences() {
155 for (iterator I = begin(), E = end(); I != E; ++I)
156 I->dropAllReferences();
157 BasicBlocks.clear(); // Delete all basic blocks...
160 /// getIntrinsicID - This method returns the ID number of the specified
161 /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
162 /// intrinsic, or if the pointer is null. This value is always defined to be
163 /// zero to allow easy checking for whether a function is intrinsic or not. The
164 /// particular intrinsic functions which correspond to this value are defined in
165 /// llvm/Intrinsics.h.
167 unsigned Function::getIntrinsicID() const {
168 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
169 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
170 return 0; // All intrinsics start with 'llvm.'
172 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
174 // a table of all Alpha intrinsic functions
176 std::string name; // The name of the intrinsic
177 unsigned id; // Its ID number
178 } alpha_intrinsics[] = {
179 { "llvm.alpha.ctlz", LLVMIntrinsic::alpha_ctlz },
180 { "llvm.alpha.cttz", LLVMIntrinsic::alpha_cttz },
181 { "llvm.alpha.ctpop", LLVMIntrinsic::alpha_ctpop },
182 { "llvm.alpha.umulh", LLVMIntrinsic::alpha_umulh },
183 { "llvm.alpha.vecop", LLVMIntrinsic::alpha_vecop },
184 { "llvm.alpha.pup", LLVMIntrinsic::alpha_pup },
185 { "llvm.alpha.bytezap", LLVMIntrinsic::alpha_bytezap },
186 { "llvm.alpha.bytemanip", LLVMIntrinsic::alpha_bytemanip },
187 { "llvm.alpha.dfp_bop", LLVMIntrinsic::alpha_dfpbop },
188 { "llvm.alpha.dfp_uop", LLVMIntrinsic::alpha_dfpuop },
189 { "llvm.alpha.unordered", LLVMIntrinsic::alpha_unordered },
190 { "llvm.alpha.uqtodfp", LLVMIntrinsic::alpha_uqtodfp },
191 { "llvm.alpha.uqtosfp", LLVMIntrinsic::alpha_uqtosfp },
192 { "llvm.alpha.dfptosq", LLVMIntrinsic::alpha_dfptosq },
193 { "llvm.alpha.sfptosq", LLVMIntrinsic::alpha_sfptosq },
195 const unsigned num_alpha_intrinsics =
196 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
198 switch (getName()[5]) {
200 if (getName().size() > 11 &&
201 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
202 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
203 if (getName() == alpha_intrinsics[i].name)
204 return alpha_intrinsics[i].id;
207 if (getName() == "llvm.longjmp") return LLVMIntrinsic::longjmp;
210 if (getName() == "llvm.setjmp") return LLVMIntrinsic::setjmp;
211 if (getName() == "llvm.sigsetjmp") return LLVMIntrinsic::sigsetjmp;
212 if (getName() == "llvm.siglongjmp") return LLVMIntrinsic::siglongjmp;
215 if (getName() == "llvm.va_copy") return LLVMIntrinsic::va_copy;
216 if (getName() == "llvm.va_end") return LLVMIntrinsic::va_end;
217 if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
220 // The "llvm." namespace is reserved!
221 assert(0 && "Unknown LLVM intrinsic function!");
226 //===----------------------------------------------------------------------===//
227 // GlobalVariable Implementation
228 //===----------------------------------------------------------------------===//
230 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
231 Constant *Initializer,
232 const std::string &Name, Module *ParentModule)
233 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
234 isConstantGlobal(constant) {
235 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
237 LeakDetector::addGarbageObject(this);
240 ParentModule->getGlobalList().push_back(this);
243 void GlobalVariable::setParent(Module *parent) {
245 LeakDetector::addGarbageObject(this);
248 LeakDetector::removeGarbageObject(this);
251 // Specialize setName to take care of symbol table majik
252 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
254 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
255 "Invalid symtab argument!");
256 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
257 Value::setName(name);
258 if (P && getName() != "") P->getSymbolTable().insert(this);