1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
3 // This file implements the Function & GlobalVariable classes for the VMCore
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Module.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/iOther.h"
11 #include "llvm/Intrinsics.h"
12 #include "Support/LeakDetector.h"
13 #include "SymbolTableListTraitsImpl.h"
15 BasicBlock *ilist_traits<BasicBlock>::createNode() {
16 BasicBlock *Ret = new BasicBlock();
17 // This should not be garbage monitored.
18 LeakDetector::removeGarbageObject(Ret);
22 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
23 return F->getBasicBlockList();
26 Argument *ilist_traits<Argument>::createNode() {
27 Argument *Ret = new Argument(Type::IntTy);
28 // This should not be garbage monitored.
29 LeakDetector::removeGarbageObject(Ret);
33 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
34 return F->getArgumentList();
37 // Explicit instantiations of SymbolTableListTraits since some of the methods
38 // are not in the public header file...
39 template SymbolTableListTraits<Argument, Function, Function>;
40 template SymbolTableListTraits<BasicBlock, Function, Function>;
42 //===----------------------------------------------------------------------===//
43 // Argument Implementation
44 //===----------------------------------------------------------------------===//
46 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
47 : Value(Ty, Value::ArgumentVal, Name) {
50 // Make sure that we get added to a function
51 LeakDetector::addGarbageObject(this);
54 Par->getArgumentList().push_back(this);
58 // Specialize setName to take care of symbol table majik
59 void Argument::setName(const std::string &name, SymbolTable *ST) {
61 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
62 "Invalid symtab argument!");
63 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
65 if (P && hasName()) P->getSymbolTable().insert(this);
68 void Argument::setParent(Function *parent) {
70 LeakDetector::addGarbageObject(this);
73 LeakDetector::removeGarbageObject(this);
77 //===----------------------------------------------------------------------===//
78 // Function Implementation
79 //===----------------------------------------------------------------------===//
81 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
82 const std::string &name, Module *ParentModule)
83 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
84 BasicBlocks.setItemParent(this);
85 BasicBlocks.setParent(this);
86 ArgumentList.setItemParent(this);
87 ArgumentList.setParent(this);
88 SymTab = new SymbolTable();
90 // Create the arguments vector, all arguments start out unnamed.
91 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
92 assert(Ty->getParamType(i) != Type::VoidTy &&
93 "Cannot have void typed arguments!");
94 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
97 // Make sure that we get added to a function
98 LeakDetector::addGarbageObject(this);
101 ParentModule->getFunctionList().push_back(this);
104 Function::~Function() {
105 dropAllReferences(); // After this it is safe to delete instructions.
107 // Delete all of the method arguments and unlink from symbol table...
108 ArgumentList.clear();
109 ArgumentList.setParent(0);
113 // Specialize setName to take care of symbol table majik
114 void Function::setName(const std::string &name, SymbolTable *ST) {
116 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
117 "Invalid symtab argument!");
118 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
119 Value::setName(name);
120 if (P && getName() != "") P->getSymbolTable().insert(this);
123 void Function::setParent(Module *parent) {
125 LeakDetector::addGarbageObject(this);
128 LeakDetector::removeGarbageObject(this);
131 const FunctionType *Function::getFunctionType() const {
132 return cast<FunctionType>(getType()->getElementType());
135 const Type *Function::getReturnType() const {
136 return getFunctionType()->getReturnType();
139 // dropAllReferences() - This function causes all the subinstructions to "let
140 // go" of all references that they are maintaining. This allows one to
141 // 'delete' a whole class at a time, even though there may be circular
142 // references... first all references are dropped, and all use counts go to
143 // zero. Then everything is deleted for real. Note that no operations are
144 // valid on an object that has "dropped all references", except operator
147 void Function::dropAllReferences() {
148 for (iterator I = begin(), E = end(); I != E; ++I)
149 I->dropAllReferences();
150 BasicBlocks.clear(); // Delete all basic blocks...
153 /// getIntrinsicID - This method returns the ID number of the specified
154 /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
155 /// intrinsic, or if the pointer is null. This value is always defined to be
156 /// zero to allow easy checking for whether a function is intrinsic or not. The
157 /// particular intrinsic functions which correspond to this value are defined in
158 /// llvm/Intrinsics.h.
160 unsigned Function::getIntrinsicID() const {
161 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
162 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
163 return 0; // All intrinsics start with 'llvm.'
165 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
167 // a table of all Alpha intrinsic functions
169 std::string name; // The name of the intrinsic
170 unsigned id; // Its ID number
171 } alpha_intrinsics[] = {
172 { "llvm.alpha.ctlz", LLVMIntrinsic::alpha_ctlz },
173 { "llvm.alpha.cttz", LLVMIntrinsic::alpha_cttz },
174 { "llvm.alpha.ctpop", LLVMIntrinsic::alpha_ctpop },
175 { "llvm.alpha.umulh", LLVMIntrinsic::alpha_umulh },
176 { "llvm.alpha.vecop", LLVMIntrinsic::alpha_vecop },
177 { "llvm.alpha.pup", LLVMIntrinsic::alpha_pup },
178 { "llvm.alpha.bytezap", LLVMIntrinsic::alpha_bytezap },
179 { "llvm.alpha.bytemanip", LLVMIntrinsic::alpha_bytemanip },
180 { "llvm.alpha.dfp_bop", LLVMIntrinsic::alpha_dfpbop },
181 { "llvm.alpha.dfp_uop", LLVMIntrinsic::alpha_dfpuop },
182 { "llvm.alpha.unordered", LLVMIntrinsic::alpha_unordered },
183 { "llvm.alpha.uqtodfp", LLVMIntrinsic::alpha_uqtodfp },
184 { "llvm.alpha.uqtosfp", LLVMIntrinsic::alpha_uqtosfp },
185 { "llvm.alpha.dfptosq", LLVMIntrinsic::alpha_dfptosq },
186 { "llvm.alpha.sfptosq", LLVMIntrinsic::alpha_sfptosq },
188 const unsigned num_alpha_intrinsics =
189 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
191 switch (getName()[5]) {
193 if (getName().size() > 11 &&
194 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
195 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
196 if (getName() == alpha_intrinsics[i].name)
197 return alpha_intrinsics[i].id;
200 if (getName() == "llvm.longjmp") return LLVMIntrinsic::longjmp;
203 if (getName() == "llvm.setjmp") return LLVMIntrinsic::setjmp;
204 if (getName() == "llvm.sigsetjmp") return LLVMIntrinsic::sigsetjmp;
205 if (getName() == "llvm.siglongjmp") return LLVMIntrinsic::siglongjmp;
208 if (getName() == "llvm.va_copy") return LLVMIntrinsic::va_copy;
209 if (getName() == "llvm.va_end") return LLVMIntrinsic::va_end;
210 if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
213 // The "llvm." namespace is reserved!
214 assert(0 && "Unknown LLVM intrinsic function!");
219 //===----------------------------------------------------------------------===//
220 // GlobalVariable Implementation
221 //===----------------------------------------------------------------------===//
223 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
224 Constant *Initializer,
225 const std::string &Name, Module *ParentModule)
226 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
227 isConstantGlobal(constant) {
228 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
230 LeakDetector::addGarbageObject(this);
233 ParentModule->getGlobalList().push_back(this);
236 void GlobalVariable::setParent(Module *parent) {
238 LeakDetector::addGarbageObject(this);
241 LeakDetector::removeGarbageObject(this);
244 // Specialize setName to take care of symbol table majik
245 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
247 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
248 "Invalid symtab argument!");
249 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
250 Value::setName(name);
251 if (P && getName() != "") P->getSymbolTable().insert(this);