Be a bit more efficient when processing the active and inactive
[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/Constant.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Intrinsics.h"
20 #include "Support/LeakDetector.h"
21 #include "SymbolTableListTraitsImpl.h"
22 using namespace llvm;
23
24 BasicBlock *ilist_traits<BasicBlock>::createNode() {
25   BasicBlock *Ret = new BasicBlock();
26   // This should not be garbage monitored.
27   LeakDetector::removeGarbageObject(Ret);
28   return Ret;
29 }
30
31 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
32   return F->getBasicBlockList();
33 }
34
35 Argument *ilist_traits<Argument>::createNode() {
36   Argument *Ret = new Argument(Type::IntTy);
37   // This should not be garbage monitored.
38   LeakDetector::removeGarbageObject(Ret);
39   return Ret;
40 }
41
42 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
43   return F->getArgumentList();
44 }
45
46 // Explicit instantiations of SymbolTableListTraits since some of the methods
47 // are not in the public header file...
48 template class SymbolTableListTraits<Argument, Function, Function>;
49 template class SymbolTableListTraits<BasicBlock, Function, Function>;
50
51 //===----------------------------------------------------------------------===//
52 // Argument Implementation
53 //===----------------------------------------------------------------------===//
54
55 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) 
56   : Value(Ty, Value::ArgumentVal, Name) {
57   Parent = 0;
58
59   // Make sure that we get added to a function
60   LeakDetector::addGarbageObject(this);
61
62   if (Par)
63     Par->getArgumentList().push_back(this);
64 }
65
66
67 // Specialize setName to take care of symbol table majik
68 void Argument::setName(const std::string &name, SymbolTable *ST) {
69   Function *P;
70   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
71          "Invalid symtab argument!");
72   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
73   Value::setName(name);
74   if (P && hasName()) P->getSymbolTable().insert(this);
75 }
76
77 void Argument::setParent(Function *parent) {
78   if (getParent())
79     LeakDetector::addGarbageObject(this);
80   Parent = parent;
81   if (getParent())
82     LeakDetector::removeGarbageObject(this);
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 'f':
218     if (getName() == "llvm.frameaddress")  return Intrinsic::frameaddress;
219     break;
220   case 'g':
221     if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
222     if (getName() == "llvm.gcread")  return Intrinsic::gcread;
223     if (getName() == "llvm.gcroot")  return Intrinsic::gcroot;
224     break;
225   case 'i':
226     if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
227     break;
228   case 'l':
229     if (getName() == "llvm.longjmp")  return Intrinsic::longjmp;
230     break;
231   case 'm':
232     if (getName() == "llvm.memcpy")  return Intrinsic::memcpy;
233     if (getName() == "llvm.memmove")  return Intrinsic::memmove;
234     if (getName() == "llvm.memset")  return Intrinsic::memset;
235     break;
236   case 'r':
237     if (getName() == "llvm.returnaddress")  return Intrinsic::returnaddress;
238     if (getName() == "llvm.readport")       return Intrinsic::readport;
239     if (getName() == "llvm.readio")         return Intrinsic::readio;
240     break;
241   case 's':
242     if (getName() == "llvm.setjmp")     return Intrinsic::setjmp;
243     if (getName() == "llvm.sigsetjmp")  return Intrinsic::sigsetjmp;
244     if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
245     break;
246   case 'v':
247     if (getName() == "llvm.va_copy")  return Intrinsic::vacopy;
248     if (getName() == "llvm.va_end")   return Intrinsic::vaend;
249     if (getName() == "llvm.va_start") return Intrinsic::vastart;
250   case 'w':
251     if (getName() == "llvm.writeport") return Intrinsic::writeport;
252     if (getName() == "llvm.writeio")   return Intrinsic::writeio;
253     break;
254   }
255   // The "llvm." namespace is reserved!
256   assert(0 && "Unknown LLVM intrinsic function!");
257   return 0;
258 }
259
260
261 // vim: sw=2 ai