Simplify conditional and fix LICM/2004-11-17-UndefIndexCrash.ll
[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/IntrinsicInst.h"
18 #include "llvm/Support/LeakDetector.h"
19 #include "SymbolTableListTraitsImpl.h"
20 using namespace llvm;
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 class SymbolTableListTraits<Argument, Function, Function>;
47 template class 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 // Function Implementation
85 //===----------------------------------------------------------------------===//
86
87 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
88                    const std::string &name, Module *ParentModule)
89   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
90   BasicBlocks.setItemParent(this);
91   BasicBlocks.setParent(this);
92   ArgumentList.setItemParent(this);
93   ArgumentList.setParent(this);
94   SymTab = new SymbolTable();
95
96   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
97          && "LLVM functions cannot return aggregate values!");
98
99   // Create the arguments vector, all arguments start out unnamed.
100   for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
101     assert(Ty->getParamType(i) != Type::VoidTy &&
102            "Cannot have void typed arguments!");
103     ArgumentList.push_back(new Argument(Ty->getParamType(i)));
104   }
105
106   // Make sure that we get added to a function
107   LeakDetector::addGarbageObject(this);
108
109   if (ParentModule)
110     ParentModule->getFunctionList().push_back(this);
111 }
112
113 Function::~Function() {
114   dropAllReferences();    // After this it is safe to delete instructions.
115
116   // Delete all of the method arguments and unlink from symbol table...
117   ArgumentList.clear();
118   ArgumentList.setParent(0);
119   delete SymTab;
120 }
121
122 // Specialize setName to take care of symbol table majik
123 void Function::setName(const std::string &name, SymbolTable *ST) {
124   Module *P;
125   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
126          "Invalid symtab argument!");
127   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
128   Value::setName(name);
129   if (P && hasName()) P->getSymbolTable().insert(this);
130 }
131
132 void Function::setParent(Module *parent) {
133   if (getParent())
134     LeakDetector::addGarbageObject(this);
135   Parent = parent;
136   if (getParent())
137     LeakDetector::removeGarbageObject(this);
138 }
139
140 const FunctionType *Function::getFunctionType() const {
141   return cast<FunctionType>(getType()->getElementType());
142 }
143
144 const Type *Function::getReturnType() const { 
145   return getFunctionType()->getReturnType();
146 }
147
148 void Function::removeFromParent() {
149   getParent()->getFunctionList().remove(this);
150 }
151
152 void Function::eraseFromParent() {
153   getParent()->getFunctionList().erase(this);
154 }
155
156 // dropAllReferences() - This function causes all the subinstructions to "let
157 // go" of all references that they are maintaining.  This allows one to
158 // 'delete' a whole class at a time, even though there may be circular
159 // references... first all references are dropped, and all use counts go to
160 // zero.  Then everything is deleted for real.  Note that no operations are
161 // valid on an object that has "dropped all references", except operator 
162 // delete.
163 //
164 void Function::dropAllReferences() {
165   for (iterator I = begin(), E = end(); I != E; ++I)
166     I->dropAllReferences();
167   BasicBlocks.clear();    // Delete all basic blocks...
168 }
169
170 /// getIntrinsicID - This method returns the ID number of the specified
171 /// function, or Intrinsic::not_intrinsic if the function is not an
172 /// intrinsic, or if the pointer is null.  This value is always defined to be
173 /// zero to allow easy checking for whether a function is intrinsic or not.  The
174 /// particular intrinsic functions which correspond to this value are defined in
175 /// llvm/Intrinsics.h.
176 ///
177 unsigned Function::getIntrinsicID() const {
178   if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
179       getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
180     return 0;  // All intrinsics start with 'llvm.'
181
182   assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
183   
184   switch (getName()[5]) {
185   case 'd':
186     if (getName() == "llvm.dbg.stoppoint")   return Intrinsic::dbg_stoppoint;
187     if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
188     if (getName() == "llvm.dbg.region.end")  return Intrinsic::dbg_region_end;
189     if (getName() == "llvm.dbg.func.start")  return Intrinsic::dbg_func_start;
190     if (getName() == "llvm.dbg.declare")     return Intrinsic::dbg_declare;
191     break;
192   case 'f':
193     if (getName() == "llvm.frameaddress")  return Intrinsic::frameaddress;
194     break;
195   case 'g':
196     if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
197     if (getName() == "llvm.gcread")  return Intrinsic::gcread;
198     if (getName() == "llvm.gcroot")  return Intrinsic::gcroot;
199     break;
200   case 'i':
201     if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
202     break;
203   case 'l':
204     if (getName() == "llvm.longjmp")  return Intrinsic::longjmp;
205     break;
206   case 'm':
207     if (getName() == "llvm.memcpy")  return Intrinsic::memcpy;
208     if (getName() == "llvm.memmove")  return Intrinsic::memmove;
209     if (getName() == "llvm.memset")  return Intrinsic::memset;
210     break;
211   case 'r':
212     if (getName() == "llvm.returnaddress")  return Intrinsic::returnaddress;
213     if (getName() == "llvm.readport")       return Intrinsic::readport;
214     if (getName() == "llvm.readio")         return Intrinsic::readio;
215     break;
216   case 's':
217     if (getName() == "llvm.setjmp")     return Intrinsic::setjmp;
218     if (getName() == "llvm.sigsetjmp")  return Intrinsic::sigsetjmp;
219     if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
220     break;
221   case 'v':
222     if (getName() == "llvm.va_copy")  return Intrinsic::vacopy;
223     if (getName() == "llvm.va_end")   return Intrinsic::vaend;
224     if (getName() == "llvm.va_start") return Intrinsic::vastart;
225   case 'w':
226     if (getName() == "llvm.writeport") return Intrinsic::writeport;
227     if (getName() == "llvm.writeio")   return Intrinsic::writeio;
228     break;
229   }
230   // The "llvm." namespace is reserved!
231   assert(0 && "Unknown LLVM intrinsic function!");
232   return 0;
233 }
234
235 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
236   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
237     if (CE->getOpcode() == Instruction::Cast) {
238       if (isa<PointerType>(CE->getOperand(0)->getType()))
239         return StripPointerCasts(CE->getOperand(0));
240     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
241       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
242         if (!CE->getOperand(i)->isNullValue())
243           return Ptr;
244       return StripPointerCasts(CE->getOperand(0));
245     }
246     return Ptr;
247   }
248
249   if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
250     if (isa<PointerType>(CI->getOperand(0)->getType()))
251       return StripPointerCasts(CI->getOperand(0));
252   } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
253     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
254       if (!isa<Constant>(GEP->getOperand(i)) ||
255           !cast<Constant>(GEP->getOperand(i))->isNullValue())
256         return Ptr;
257     return StripPointerCasts(GEP->getOperand(0));
258   }
259   return Ptr;
260 }
261
262 // vim: sw=2 ai