Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes ----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Function & GlobalVariable classes for the VMCore
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
21
22 BasicBlock *ilist_traits<BasicBlock>::createNode() {
23   BasicBlock *Ret = new BasicBlock();
24   // This should not be garbage monitored.
25   LeakDetector::removeGarbageObject(Ret);
26   return Ret;
27 }
28
29 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
30   return F->getBasicBlockList();
31 }
32
33 Argument *ilist_traits<Argument>::createNode() {
34   Argument *Ret = new Argument(Type::IntTy);
35   // This should not be garbage monitored.
36   LeakDetector::removeGarbageObject(Ret);
37   return Ret;
38 }
39
40 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
41   return F->getArgumentList();
42 }
43
44 // Explicit instantiations of SymbolTableListTraits since some of the methods
45 // are not in the public header file...
46 template SymbolTableListTraits<Argument, Function, Function>;
47 template SymbolTableListTraits<BasicBlock, Function, Function>;
48
49 //===----------------------------------------------------------------------===//
50 // Argument Implementation
51 //===----------------------------------------------------------------------===//
52
53 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) 
54   : Value(Ty, Value::ArgumentVal, Name) {
55   Parent = 0;
56
57   // Make sure that we get added to a function
58   LeakDetector::addGarbageObject(this);
59
60   if (Par)
61     Par->getArgumentList().push_back(this);
62 }
63
64
65 // Specialize setName to take care of symbol table majik
66 void Argument::setName(const std::string &name, SymbolTable *ST) {
67   Function *P;
68   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
69          "Invalid symtab argument!");
70   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
71   Value::setName(name);
72   if (P && hasName()) P->getSymbolTable().insert(this);
73 }
74
75 void Argument::setParent(Function *parent) {
76   if (getParent())
77     LeakDetector::addGarbageObject(this);
78   Parent = parent;
79   if (getParent())
80     LeakDetector::removeGarbageObject(this);
81 }
82
83
84 //===----------------------------------------------------------------------===//
85 // Function Implementation
86 //===----------------------------------------------------------------------===//
87
88 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
89                    const std::string &name, Module *ParentModule)
90   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
91   BasicBlocks.setItemParent(this);
92   BasicBlocks.setParent(this);
93   ArgumentList.setItemParent(this);
94   ArgumentList.setParent(this);
95   SymTab = new SymbolTable();
96
97   // Create the arguments vector, all arguments start out unnamed.
98   for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
99     assert(Ty->getParamType(i) != Type::VoidTy &&
100            "Cannot have void typed arguments!");
101     ArgumentList.push_back(new Argument(Ty->getParamType(i)));
102   }
103
104   // Make sure that we get added to a function
105   LeakDetector::addGarbageObject(this);
106
107   if (ParentModule)
108     ParentModule->getFunctionList().push_back(this);
109 }
110
111 Function::~Function() {
112   dropAllReferences();    // After this it is safe to delete instructions.
113
114   // Delete all of the method arguments and unlink from symbol table...
115   ArgumentList.clear();
116   ArgumentList.setParent(0);
117   delete SymTab;
118 }
119
120 // Specialize setName to take care of symbol table majik
121 void Function::setName(const std::string &name, SymbolTable *ST) {
122   Module *P;
123   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
124          "Invalid symtab argument!");
125   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
126   Value::setName(name);
127   if (P && getName() != "") P->getSymbolTable().insert(this);
128 }
129
130 void Function::setParent(Module *parent) {
131   if (getParent())
132     LeakDetector::addGarbageObject(this);
133   Parent = parent;
134   if (getParent())
135     LeakDetector::removeGarbageObject(this);
136 }
137
138 const FunctionType *Function::getFunctionType() const {
139   return cast<FunctionType>(getType()->getElementType());
140 }
141
142 const Type *Function::getReturnType() const { 
143   return getFunctionType()->getReturnType();
144 }
145
146 // dropAllReferences() - This function causes all the subinstructions to "let
147 // go" of all references that they are maintaining.  This allows one to
148 // 'delete' a whole class at a time, even though there may be circular
149 // references... first all references are dropped, and all use counts go to
150 // zero.  Then everything is deleted for real.  Note that no operations are
151 // valid on an object that has "dropped all references", except operator 
152 // delete.
153 //
154 void Function::dropAllReferences() {
155   for (iterator I = begin(), E = end(); I != E; ++I)
156     I->dropAllReferences();
157   BasicBlocks.clear();    // Delete all basic blocks...
158 }
159
160 /// getIntrinsicID - This method returns the ID number of the specified
161 /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
162 /// intrinsic, or if the pointer is null.  This value is always defined to be
163 /// zero to allow easy checking for whether a function is intrinsic or not.  The
164 /// particular intrinsic functions which correspond to this value are defined in
165 /// llvm/Intrinsics.h.
166 ///
167 unsigned Function::getIntrinsicID() const {
168   if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
169       getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
170     return 0;  // All intrinsics start with 'llvm.'
171
172   assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
173   
174   // a table of all Alpha intrinsic functions
175   struct {
176    std::string name;  // The name of the intrinsic 
177    unsigned id;       // Its ID number
178   } alpha_intrinsics[] = {
179      { "llvm.alpha.ctlz",      LLVMIntrinsic::alpha_ctlz },
180      { "llvm.alpha.cttz",      LLVMIntrinsic::alpha_cttz },
181      { "llvm.alpha.ctpop",     LLVMIntrinsic::alpha_ctpop },
182      { "llvm.alpha.umulh",     LLVMIntrinsic::alpha_umulh },
183      { "llvm.alpha.vecop",     LLVMIntrinsic::alpha_vecop },
184      { "llvm.alpha.pup",       LLVMIntrinsic::alpha_pup },
185      { "llvm.alpha.bytezap",   LLVMIntrinsic::alpha_bytezap },
186      { "llvm.alpha.bytemanip", LLVMIntrinsic::alpha_bytemanip },
187      { "llvm.alpha.dfp_bop",   LLVMIntrinsic::alpha_dfpbop }, 
188      { "llvm.alpha.dfp_uop",   LLVMIntrinsic::alpha_dfpuop },
189      { "llvm.alpha.unordered", LLVMIntrinsic::alpha_unordered },
190      { "llvm.alpha.uqtodfp",   LLVMIntrinsic::alpha_uqtodfp },
191      { "llvm.alpha.uqtosfp",   LLVMIntrinsic::alpha_uqtosfp },
192      { "llvm.alpha.dfptosq",   LLVMIntrinsic::alpha_dfptosq },
193      { "llvm.alpha.sfptosq",   LLVMIntrinsic::alpha_sfptosq },
194   };
195   const unsigned num_alpha_intrinsics = 
196                  sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
197
198   switch (getName()[5]) {
199   case 'a':
200     if (getName().size() > 11 &&
201         std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
202       for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
203         if (getName() == alpha_intrinsics[i].name)
204           return alpha_intrinsics[i].id;
205     break;
206   case 'l':
207     if (getName() == "llvm.longjmp")  return LLVMIntrinsic::longjmp;
208     break;
209   case 's':
210     if (getName() == "llvm.setjmp")     return LLVMIntrinsic::setjmp;
211     if (getName() == "llvm.sigsetjmp")  return LLVMIntrinsic::sigsetjmp;
212     if (getName() == "llvm.siglongjmp") return LLVMIntrinsic::siglongjmp;
213     break;
214   case 'v':
215     if (getName() == "llvm.va_copy")  return LLVMIntrinsic::va_copy;
216     if (getName() == "llvm.va_end")   return LLVMIntrinsic::va_end;
217     if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
218     break;
219   }
220   // The "llvm." namespace is reserved!
221   assert(0 && "Unknown LLVM intrinsic function!");
222   return 0;
223 }
224
225
226 //===----------------------------------------------------------------------===//
227 // GlobalVariable Implementation
228 //===----------------------------------------------------------------------===//
229
230 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
231                                Constant *Initializer,
232                                const std::string &Name, Module *ParentModule)
233   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
234     isConstantGlobal(constant) {
235   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
236
237   LeakDetector::addGarbageObject(this);
238
239   if (ParentModule)
240     ParentModule->getGlobalList().push_back(this);
241 }
242
243 void GlobalVariable::setParent(Module *parent) {
244   if (getParent())
245     LeakDetector::addGarbageObject(this);
246   Parent = parent;
247   if (getParent())
248     LeakDetector::removeGarbageObject(this);
249 }
250
251 // Specialize setName to take care of symbol table majik
252 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
253   Module *P;
254   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
255          "Invalid symtab argument!");
256   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
257   Value::setName(name);
258   if (P && getName() != "") P->getSymbolTable().insert(this);
259 }