8875661ff04e136118934ee061f6f06a45d0a107
[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 using namespace llvm;
22
23 BasicBlock *ilist_traits<BasicBlock>::createNode() {
24   BasicBlock *Ret = new BasicBlock();
25   // This should not be garbage monitored.
26   LeakDetector::removeGarbageObject(Ret);
27   return Ret;
28 }
29
30 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
31   return F->getBasicBlockList();
32 }
33
34 Argument *ilist_traits<Argument>::createNode() {
35   Argument *Ret = new Argument(Type::IntTy);
36   // This should not be garbage monitored.
37   LeakDetector::removeGarbageObject(Ret);
38   return Ret;
39 }
40
41 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
42   return F->getArgumentList();
43 }
44
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>;
49
50 //===----------------------------------------------------------------------===//
51 // Argument Implementation
52 //===----------------------------------------------------------------------===//
53
54 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) 
55   : Value(Ty, Value::ArgumentVal, Name) {
56   Parent = 0;
57
58   // Make sure that we get added to a function
59   LeakDetector::addGarbageObject(this);
60
61   if (Par)
62     Par->getArgumentList().push_back(this);
63 }
64
65
66 // Specialize setName to take care of symbol table majik
67 void Argument::setName(const std::string &name, SymbolTable *ST) {
68   Function *P;
69   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
70          "Invalid symtab argument!");
71   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
72   Value::setName(name);
73   if (P && hasName()) P->getSymbolTable().insert(this);
74 }
75
76 void Argument::setParent(Function *parent) {
77   if (getParent())
78     LeakDetector::addGarbageObject(this);
79   Parent = parent;
80   if (getParent())
81     LeakDetector::removeGarbageObject(this);
82 }
83
84
85 //===----------------------------------------------------------------------===//
86 // Function Implementation
87 //===----------------------------------------------------------------------===//
88
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();
97
98   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
99          && "LLVM functions cannot return aggregate values!");
100
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)));
106   }
107
108   // Make sure that we get added to a function
109   LeakDetector::addGarbageObject(this);
110
111   if (ParentModule)
112     ParentModule->getFunctionList().push_back(this);
113 }
114
115 Function::~Function() {
116   dropAllReferences();    // After this it is safe to delete instructions.
117
118   // Delete all of the method arguments and unlink from symbol table...
119   ArgumentList.clear();
120   ArgumentList.setParent(0);
121   delete SymTab;
122 }
123
124 // Specialize setName to take care of symbol table majik
125 void Function::setName(const std::string &name, SymbolTable *ST) {
126   Module *P;
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);
132 }
133
134 void Function::setParent(Module *parent) {
135   if (getParent())
136     LeakDetector::addGarbageObject(this);
137   Parent = parent;
138   if (getParent())
139     LeakDetector::removeGarbageObject(this);
140 }
141
142 const FunctionType *Function::getFunctionType() const {
143   return cast<FunctionType>(getType()->getElementType());
144 }
145
146 const Type *Function::getReturnType() const { 
147   return getFunctionType()->getReturnType();
148 }
149
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 
156 // delete.
157 //
158 void Function::dropAllReferences() {
159   for (iterator I = begin(), E = end(); I != E; ++I)
160     I->dropAllReferences();
161   BasicBlocks.clear();    // Delete all basic blocks...
162 }
163
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.
170 ///
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.'
175
176   assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
177   
178   // a table of all Alpha intrinsic functions
179   struct {
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 },
198   };
199   const unsigned num_alpha_intrinsics = 
200                  sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
201
202   switch (getName()[5]) {
203   case 'a':
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;
209     break;
210   case 'd':
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;
216     break;
217   case 'l':
218     if (getName() == "llvm.longjmp")  return Intrinsic::longjmp;
219     break;
220   case 'm':
221     if (getName() == "llvm.memcpy")  return Intrinsic::memcpy;
222     if (getName() == "llvm.memmove")  return Intrinsic::memmove;
223     break;
224   case 's':
225     if (getName() == "llvm.setjmp")     return Intrinsic::setjmp;
226     if (getName() == "llvm.sigsetjmp")  return Intrinsic::sigsetjmp;
227     if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
228     break;
229   case 'v':
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;
233     break;
234   }
235   // The "llvm." namespace is reserved!
236   assert(0 && "Unknown LLVM intrinsic function!");
237   return 0;
238 }
239
240
241 //===----------------------------------------------------------------------===//
242 // GlobalVariable Implementation
243 //===----------------------------------------------------------------------===//
244
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));
251
252   LeakDetector::addGarbageObject(this);
253
254   if (ParentModule)
255     ParentModule->getGlobalList().push_back(this);
256 }
257
258 void GlobalVariable::setParent(Module *parent) {
259   if (getParent())
260     LeakDetector::addGarbageObject(this);
261   Parent = parent;
262   if (getParent())
263     LeakDetector::removeGarbageObject(this);
264 }
265
266 // Specialize setName to take care of symbol table majik
267 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
268   Module *P;
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);
274 }