d31a69e49abe4275124f376979a2d250e9547eb5
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes ----------------===//
2 //
3 // This file implements the Function & GlobalVariable classes for the VMCore
4 // library.
5 //
6 //===----------------------------------------------------------------------===//
7
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"
14
15 BasicBlock *ilist_traits<BasicBlock>::createNode() {
16   BasicBlock *Ret = new BasicBlock();
17   // This should not be garbage monitored.
18   LeakDetector::removeGarbageObject(Ret);
19   return Ret;
20 }
21
22 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
23   return F->getBasicBlockList();
24 }
25
26 Argument *ilist_traits<Argument>::createNode() {
27   Argument *Ret = new Argument(Type::IntTy);
28   // This should not be garbage monitored.
29   LeakDetector::removeGarbageObject(Ret);
30   return Ret;
31 }
32
33 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
34   return F->getArgumentList();
35 }
36
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>;
41
42 //===----------------------------------------------------------------------===//
43 // Argument Implementation
44 //===----------------------------------------------------------------------===//
45
46 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) 
47   : Value(Ty, Value::ArgumentVal, Name) {
48   Parent = 0;
49
50   // Make sure that we get added to a function
51   LeakDetector::addGarbageObject(this);
52
53   if (Par)
54     Par->getArgumentList().push_back(this);
55 }
56
57
58 // Specialize setName to take care of symbol table majik
59 void Argument::setName(const std::string &name, SymbolTable *ST) {
60   Function *P;
61   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
62          "Invalid symtab argument!");
63   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
64   Value::setName(name);
65   if (P && hasName()) P->getSymbolTable().insert(this);
66 }
67
68 void Argument::setParent(Function *parent) {
69   if (getParent())
70     LeakDetector::addGarbageObject(this);
71   Parent = parent;
72   if (getParent())
73     LeakDetector::removeGarbageObject(this);
74 }
75
76
77 //===----------------------------------------------------------------------===//
78 // Function Implementation
79 //===----------------------------------------------------------------------===//
80
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();
89
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)));
95   }
96
97   // Make sure that we get added to a function
98   LeakDetector::addGarbageObject(this);
99
100   if (ParentModule)
101     ParentModule->getFunctionList().push_back(this);
102 }
103
104 Function::~Function() {
105   dropAllReferences();    // After this it is safe to delete instructions.
106
107   // Delete all of the method arguments and unlink from symbol table...
108   ArgumentList.clear();
109   ArgumentList.setParent(0);
110   delete SymTab;
111 }
112
113 // Specialize setName to take care of symbol table majik
114 void Function::setName(const std::string &name, SymbolTable *ST) {
115   Module *P;
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);
121 }
122
123 void Function::setParent(Module *parent) {
124   if (getParent())
125     LeakDetector::addGarbageObject(this);
126   Parent = parent;
127   if (getParent())
128     LeakDetector::removeGarbageObject(this);
129 }
130
131 const FunctionType *Function::getFunctionType() const {
132   return cast<FunctionType>(getType()->getElementType());
133 }
134
135 const Type *Function::getReturnType() const { 
136   return getFunctionType()->getReturnType();
137 }
138
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 
145 // delete.
146 //
147 void Function::dropAllReferences() {
148   for (iterator I = begin(), E = end(); I != E; ++I)
149     I->dropAllReferences();
150   BasicBlocks.clear();    // Delete all basic blocks...
151 }
152
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.
159 ///
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.'
164
165   assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
166   
167   // a table of all Alpha intrinsic functions
168   struct {
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 },
187   };
188   const unsigned num_alpha_intrinsics = 
189                  sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
190
191   switch (getName()[5]) {
192   case 'a':
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;
198     break;
199   case 'l':
200     if (getName() == "llvm.longjmp")  return LLVMIntrinsic::longjmp;
201     break;
202   case 's':
203     if (getName() == "llvm.setjmp")     return LLVMIntrinsic::setjmp;
204     if (getName() == "llvm.sigsetjmp")  return LLVMIntrinsic::sigsetjmp;
205     if (getName() == "llvm.siglongjmp") return LLVMIntrinsic::siglongjmp;
206     break;
207   case 'v':
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;
211     break;
212   }
213   // The "llvm." namespace is reserved!
214   assert(0 && "Unknown LLVM intrinsic function!");
215   return 0;
216 }
217
218
219 //===----------------------------------------------------------------------===//
220 // GlobalVariable Implementation
221 //===----------------------------------------------------------------------===//
222
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));
229
230   LeakDetector::addGarbageObject(this);
231
232   if (ParentModule)
233     ParentModule->getGlobalList().push_back(this);
234 }
235
236 void GlobalVariable::setParent(Module *parent) {
237   if (getParent())
238     LeakDetector::addGarbageObject(this);
239   Parent = parent;
240   if (getParent())
241     LeakDetector::removeGarbageObject(this);
242 }
243
244 // Specialize setName to take care of symbol table majik
245 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
246   Module *P;
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);
252 }