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 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
99 && "LLVM functions cannot return aggregate values!");
101 // Create the arguments vector, all arguments start out unnamed.
102 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
103 assert(Ty->getParamType(i) != Type::VoidTy &&
104 "Cannot have void typed arguments!");
105 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
108 // Make sure that we get added to a function
109 LeakDetector::addGarbageObject(this);
112 ParentModule->getFunctionList().push_back(this);
115 Function::~Function() {
116 dropAllReferences(); // After this it is safe to delete instructions.
118 // Delete all of the method arguments and unlink from symbol table...
119 ArgumentList.clear();
120 ArgumentList.setParent(0);
124 // Specialize setName to take care of symbol table majik
125 void Function::setName(const std::string &name, SymbolTable *ST) {
127 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
128 "Invalid symtab argument!");
129 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
130 Value::setName(name);
131 if (P && hasName()) P->getSymbolTable().insert(this);
134 void Function::setParent(Module *parent) {
136 LeakDetector::addGarbageObject(this);
139 LeakDetector::removeGarbageObject(this);
142 const FunctionType *Function::getFunctionType() const {
143 return cast<FunctionType>(getType()->getElementType());
146 const Type *Function::getReturnType() const {
147 return getFunctionType()->getReturnType();
150 // dropAllReferences() - This function causes all the subinstructions to "let
151 // go" of all references that they are maintaining. This allows one to
152 // 'delete' a whole class at a time, even though there may be circular
153 // references... first all references are dropped, and all use counts go to
154 // zero. Then everything is deleted for real. Note that no operations are
155 // valid on an object that has "dropped all references", except operator
158 void Function::dropAllReferences() {
159 for (iterator I = begin(), E = end(); I != E; ++I)
160 I->dropAllReferences();
161 BasicBlocks.clear(); // Delete all basic blocks...
164 /// getIntrinsicID - This method returns the ID number of the specified
165 /// function, or Intrinsic::not_intrinsic if the function is not an
166 /// intrinsic, or if the pointer is null. This value is always defined to be
167 /// zero to allow easy checking for whether a function is intrinsic or not. The
168 /// particular intrinsic functions which correspond to this value are defined in
169 /// llvm/Intrinsics.h.
171 unsigned Function::getIntrinsicID() const {
172 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
173 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
174 return 0; // All intrinsics start with 'llvm.'
176 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
178 // a table of all Alpha intrinsic functions
180 std::string name; // The name of the intrinsic
181 unsigned id; // Its ID number
182 } alpha_intrinsics[] = {
183 { "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
184 { "llvm.alpha.cttz", Intrinsic::alpha_cttz },
185 { "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
186 { "llvm.alpha.umulh", Intrinsic::alpha_umulh },
187 { "llvm.alpha.vecop", Intrinsic::alpha_vecop },
188 { "llvm.alpha.pup", Intrinsic::alpha_pup },
189 { "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
190 { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
191 { "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
192 { "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
193 { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
194 { "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
195 { "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
196 { "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
197 { "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
199 const unsigned num_alpha_intrinsics =
200 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
202 switch (getName()[5]) {
204 if (getName().size() > 11 &&
205 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
206 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
207 if (getName() == alpha_intrinsics[i].name)
208 return alpha_intrinsics[i].id;
211 if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
212 if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
213 if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
214 if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
215 if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
218 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
221 if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
222 if (getName() == "llvm.memmove") return Intrinsic::memmove;
225 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
226 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
227 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
230 if (getName() == "llvm.va_copy") return Intrinsic::va_copy;
231 if (getName() == "llvm.va_end") return Intrinsic::va_end;
232 if (getName() == "llvm.va_start") return Intrinsic::va_start;
235 // The "llvm." namespace is reserved!
236 assert(0 && "Unknown LLVM intrinsic function!");
241 //===----------------------------------------------------------------------===//
242 // GlobalVariable Implementation
243 //===----------------------------------------------------------------------===//
245 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
246 Constant *Initializer,
247 const std::string &Name, Module *ParentModule)
248 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
249 isConstantGlobal(constant) {
250 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
252 LeakDetector::addGarbageObject(this);
255 ParentModule->getGlobalList().push_back(this);
258 void GlobalVariable::setParent(Module *parent) {
260 LeakDetector::addGarbageObject(this);
263 LeakDetector::removeGarbageObject(this);
266 // Specialize setName to take care of symbol table majik
267 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
269 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
270 "Invalid symtab argument!");
271 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
272 Value::setName(name);
273 if (P && hasName()) P->getSymbolTable().insert(this);