74098ffc45ae1266e274a7bd2e5619b2980801d1
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
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   BasicBlocks.clear();    // Delete all basic blocks...
108
109   // Delete all of the method arguments and unlink from symbol table...
110   ArgumentList.clear();
111   ArgumentList.setParent(0);
112   delete SymTab;
113 }
114
115 // Specialize setName to take care of symbol table majik
116 void Function::setName(const std::string &name, SymbolTable *ST) {
117   Module *P;
118   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
119          "Invalid symtab argument!");
120   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
121   Value::setName(name);
122   if (P && getName() != "") P->getSymbolTable().insert(this);
123 }
124
125 void Function::setParent(Module *parent) {
126   if (getParent())
127     LeakDetector::addGarbageObject(this);
128   Parent = parent;
129   if (getParent())
130     LeakDetector::removeGarbageObject(this);
131 }
132
133 const FunctionType *Function::getFunctionType() const {
134   return cast<FunctionType>(getType()->getElementType());
135 }
136
137 const Type *Function::getReturnType() const { 
138   return getFunctionType()->getReturnType();
139 }
140
141 // dropAllReferences() - This function causes all the subinstructions to "let
142 // go" of all references that they are maintaining.  This allows one to
143 // 'delete' a whole class at a time, even though there may be circular
144 // references... first all references are dropped, and all use counts go to
145 // zero.  Then everything is delete'd for real.  Note that no operations are
146 // valid on an object that has "dropped all references", except operator 
147 // delete.
148 //
149 void Function::dropAllReferences() {
150   for (iterator I = begin(), E = end(); I != E; ++I)
151     I->dropAllReferences();
152 }
153
154 /// getIntrinsicID - This method returns the ID number of the specified
155 /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
156 /// instrinsic, or if the pointer is null.  This value is always defined to be
157 /// zero to allow easy checking for whether a function is intrinsic or not.  The
158 /// particular intrinsic functions which correspond to this value are defined in
159 /// llvm/Intrinsics.h.
160 ///
161 unsigned Function::getIntrinsicID() const {
162   if (getName().size() <= 5 || getName()[4] != '.' || getName()[0] != 'l' ||
163       getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
164     return 0;  // All intrinsics start with 'llvm.'
165   
166   // a table of all Alpha intrinsic functions
167   struct {
168    std::string name;  // The name of the intrinsic 
169    unsigned id;       // Its ID number
170   } alpha_intrinsics[] = {
171      { "llvm.alpha.ctlz",      LLVMIntrinsic::alpha_ctlz },
172      { "llvm.alpha.cttz",      LLVMIntrinsic::alpha_cttz },
173      { "llvm.alpha.ctpop",     LLVMIntrinsic::alpha_ctpop },
174      { "llvm.alpha.umulh",     LLVMIntrinsic::alpha_umulh },
175      { "llvm.alpha.vecop",     LLVMIntrinsic::alpha_vecop },
176      { "llvm.alpha.pup",       LLVMIntrinsic::alpha_pup },
177      { "llvm.alpha.bytezap",   LLVMIntrinsic::alpha_bytezap },
178      { "llvm.alpha.bytemanip", LLVMIntrinsic::alpha_bytemanip },
179      { "llvm.alpha.dfp_bop",   LLVMIntrinsic::alpha_dfpbop }, 
180      { "llvm.alpha.dfp_uop",   LLVMIntrinsic::alpha_dfpuop },
181      { "llvm.alpha.unordered", LLVMIntrinsic::alpha_unordered },
182      { "llvm.alpha.uqtodfp",   LLVMIntrinsic::alpha_uqtodfp },
183      { "llvm.alpha.uqtosfp",   LLVMIntrinsic::alpha_uqtosfp },
184      { "llvm.alpha.dfptosq",   LLVMIntrinsic::alpha_dfptosq },
185      { "llvm.alpha.sfptosq",   LLVMIntrinsic::alpha_sfptosq },
186   };
187   const unsigned num_alpha_intrinsics = 
188                  sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
189
190   switch (getName()[5]) {
191   case 'a':
192     if (getName().size() > 11 &&
193         std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
194       for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
195         if (getName() == alpha_intrinsics[i].name)
196           return alpha_intrinsics[i].id;
197     break;
198   case 'l':
199     if (getName() == "llvm.longjmp")  return LLVMIntrinsic::longjmp;
200     break;
201   case 's':
202     if (getName() == "llvm.setjmp")     return LLVMIntrinsic::setjmp;
203     if (getName() == "llvm.sigsetjmp")  return LLVMIntrinsic::sigsetjmp;
204     if (getName() == "llvm.siglongjmp") return LLVMIntrinsic::siglongjmp;
205     break;
206   case 'v':
207     if (getName() == "llvm.va_copy")  return LLVMIntrinsic::va_copy;
208     if (getName() == "llvm.va_end")   return LLVMIntrinsic::va_end;
209     if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
210     break;
211   }
212   // The "llvm." namespace is reserved!
213   assert(0 && "Unknown LLVM intrinsic function!");
214   return 0;
215 }
216
217
218 //===----------------------------------------------------------------------===//
219 // GlobalVariable Implementation
220 //===----------------------------------------------------------------------===//
221
222 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
223                                Constant *Initializer,
224                                const std::string &Name, Module *ParentModule)
225   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
226     isConstantGlobal(constant) {
227   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
228
229   LeakDetector::addGarbageObject(this);
230
231   if (ParentModule)
232     ParentModule->getGlobalList().push_back(this);
233 }
234
235 void GlobalVariable::setParent(Module *parent) {
236   if (getParent())
237     LeakDetector::addGarbageObject(this);
238   Parent = parent;
239   if (getParent())
240     LeakDetector::removeGarbageObject(this);
241 }
242
243 // Specialize setName to take care of symbol table majik
244 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
245   Module *P;
246   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
247          "Invalid symtab argument!");
248   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
249   Value::setName(name);
250   if (P && getName() != "") P->getSymbolTable().insert(this);
251 }