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"
23 BasicBlock *ilist_traits<BasicBlock>::createNode() {
24 BasicBlock *Ret = new BasicBlock();
25 // This should not be garbage monitored.
26 LeakDetector::removeGarbageObject(Ret);
30 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
31 return F->getBasicBlockList();
34 Argument *ilist_traits<Argument>::createNode() {
35 Argument *Ret = new Argument(Type::IntTy);
36 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
41 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
42 return F->getArgumentList();
45 // Explicit instantiations of SymbolTableListTraits since some of the methods
46 // are not in the public header file...
47 template class SymbolTableListTraits<Argument, Function, Function>;
48 template class SymbolTableListTraits<BasicBlock, Function, Function>;
50 //===----------------------------------------------------------------------===//
51 // Argument Implementation
52 //===----------------------------------------------------------------------===//
54 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
55 : Value(Ty, Value::ArgumentVal, Name) {
58 // Make sure that we get added to a function
59 LeakDetector::addGarbageObject(this);
62 Par->getArgumentList().push_back(this);
66 // Specialize setName to take care of symbol table majik
67 void Argument::setName(const std::string &name, SymbolTable *ST) {
69 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
70 "Invalid symtab argument!");
71 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
73 if (P && hasName()) P->getSymbolTable().insert(this);
76 void Argument::setParent(Function *parent) {
78 LeakDetector::addGarbageObject(this);
81 LeakDetector::removeGarbageObject(this);
85 //===----------------------------------------------------------------------===//
86 // Function Implementation
87 //===----------------------------------------------------------------------===//
89 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
90 const std::string &name, Module *ParentModule)
91 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
92 BasicBlocks.setItemParent(this);
93 BasicBlocks.setParent(this);
94 ArgumentList.setItemParent(this);
95 ArgumentList.setParent(this);
96 SymTab = new SymbolTable();
98 // Create the arguments vector, all arguments start out unnamed.
99 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
100 assert(Ty->getParamType(i) != Type::VoidTy &&
101 "Cannot have void typed arguments!");
102 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
105 // Make sure that we get added to a function
106 LeakDetector::addGarbageObject(this);
109 ParentModule->getFunctionList().push_back(this);
112 Function::~Function() {
113 dropAllReferences(); // After this it is safe to delete instructions.
115 // Delete all of the method arguments and unlink from symbol table...
116 ArgumentList.clear();
117 ArgumentList.setParent(0);
121 // Specialize setName to take care of symbol table majik
122 void Function::setName(const std::string &name, SymbolTable *ST) {
124 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
125 "Invalid symtab argument!");
126 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
127 Value::setName(name);
128 if (P && getName() != "") P->getSymbolTable().insert(this);
131 void Function::setParent(Module *parent) {
133 LeakDetector::addGarbageObject(this);
136 LeakDetector::removeGarbageObject(this);
139 const FunctionType *Function::getFunctionType() const {
140 return cast<FunctionType>(getType()->getElementType());
143 const Type *Function::getReturnType() const {
144 return getFunctionType()->getReturnType();
147 // dropAllReferences() - This function causes all the subinstructions to "let
148 // go" of all references that they are maintaining. This allows one to
149 // 'delete' a whole class at a time, even though there may be circular
150 // references... first all references are dropped, and all use counts go to
151 // zero. Then everything is deleted for real. Note that no operations are
152 // valid on an object that has "dropped all references", except operator
155 void Function::dropAllReferences() {
156 for (iterator I = begin(), E = end(); I != E; ++I)
157 I->dropAllReferences();
158 BasicBlocks.clear(); // Delete all basic blocks...
161 /// getIntrinsicID - This method returns the ID number of the specified
162 /// function, or Intrinsic::not_intrinsic if the function is not an
163 /// intrinsic, or if the pointer is null. This value is always defined to be
164 /// zero to allow easy checking for whether a function is intrinsic or not. The
165 /// particular intrinsic functions which correspond to this value are defined in
166 /// llvm/Intrinsics.h.
168 unsigned Function::getIntrinsicID() const {
169 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
170 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
171 return 0; // All intrinsics start with 'llvm.'
173 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
175 // a table of all Alpha intrinsic functions
177 std::string name; // The name of the intrinsic
178 unsigned id; // Its ID number
179 } alpha_intrinsics[] = {
180 { "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
181 { "llvm.alpha.cttz", Intrinsic::alpha_cttz },
182 { "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
183 { "llvm.alpha.umulh", Intrinsic::alpha_umulh },
184 { "llvm.alpha.vecop", Intrinsic::alpha_vecop },
185 { "llvm.alpha.pup", Intrinsic::alpha_pup },
186 { "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
187 { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
188 { "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
189 { "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
190 { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
191 { "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
192 { "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
193 { "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
194 { "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
196 const unsigned num_alpha_intrinsics =
197 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
199 switch (getName()[5]) {
201 if (getName().size() > 11 &&
202 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
203 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
204 if (getName() == alpha_intrinsics[i].name)
205 return alpha_intrinsics[i].id;
208 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
211 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
212 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
213 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
216 if (getName() == "llvm.va_copy") return Intrinsic::va_copy;
217 if (getName() == "llvm.va_end") return Intrinsic::va_end;
218 if (getName() == "llvm.va_start") return Intrinsic::va_start;
221 // The "llvm." namespace is reserved!
222 assert(0 && "Unknown LLVM intrinsic function!");
227 //===----------------------------------------------------------------------===//
228 // GlobalVariable Implementation
229 //===----------------------------------------------------------------------===//
231 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
232 Constant *Initializer,
233 const std::string &Name, Module *ParentModule)
234 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
235 isConstantGlobal(constant) {
236 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
238 LeakDetector::addGarbageObject(this);
241 ParentModule->getGlobalList().push_back(this);
244 void GlobalVariable::setParent(Module *parent) {
246 LeakDetector::addGarbageObject(this);
249 LeakDetector::removeGarbageObject(this);
252 // Specialize setName to take care of symbol table majik
253 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
255 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
256 "Invalid symtab argument!");
257 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
258 Value::setName(name);
259 if (P && getName() != "") P->getSymbolTable().insert(this);