Add preparePassManager() hook. This allows each pass to check whether
[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 class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Module.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/IntrinsicInst.h"
17 #include "llvm/Support/LeakDetector.h"
18 #include "SymbolTableListTraitsImpl.h"
19 #include "llvm/ADT/StringExtras.h"
20 using namespace llvm;
21
22 BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
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>::createSentinel() {
34   Argument *Ret = new Argument(Type::Int32Ty);
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) {
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   setName(Name);
63 }
64
65 void Argument::setParent(Function *parent) {
66   if (getParent())
67     LeakDetector::addGarbageObject(this);
68   Parent = parent;
69   if (getParent())
70     LeakDetector::removeGarbageObject(this);
71 }
72
73 //===----------------------------------------------------------------------===//
74 // Function Implementation
75 //===----------------------------------------------------------------------===//
76
77 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
78                    const std::string &name, Module *ParentModule)
79   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
80   CallingConvention = 0;
81   BasicBlocks.setItemParent(this);
82   BasicBlocks.setParent(this);
83   ArgumentList.setItemParent(this);
84   ArgumentList.setParent(this);
85   SymTab = new ValueSymbolTable();
86
87   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
88          && "LLVM functions cannot return aggregate values!");
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   // Delete all of the method arguments and unlink from symbol table...
108   ArgumentList.clear();
109   ArgumentList.setParent(0);
110   delete SymTab;
111 }
112
113 void Function::setParent(Module *parent) {
114   if (getParent())
115     LeakDetector::addGarbageObject(this);
116   Parent = parent;
117   if (getParent())
118     LeakDetector::removeGarbageObject(this);
119 }
120
121 const FunctionType *Function::getFunctionType() const {
122   return cast<FunctionType>(getType()->getElementType());
123 }
124
125 bool Function::isVarArg() const {
126   return getFunctionType()->isVarArg();
127 }
128
129 const Type *Function::getReturnType() const {
130   return getFunctionType()->getReturnType();
131 }
132
133 void Function::removeFromParent() {
134   getParent()->getFunctionList().remove(this);
135 }
136
137 void Function::eraseFromParent() {
138   getParent()->getFunctionList().erase(this);
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 deleted 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   BasicBlocks.clear();    // Delete all basic blocks...
153 }
154
155 /// getIntrinsicID - This method returns the ID number of the specified
156 /// function, or Intrinsic::not_intrinsic if the function is not an
157 /// intrinsic, or if the pointer is null.  This value is always defined to be
158 /// zero to allow easy checking for whether a function is intrinsic or not.  The
159 /// particular intrinsic functions which correspond to this value are defined in
160 /// llvm/Intrinsics.h.
161 ///
162 unsigned Function::getIntrinsicID() const {
163   const ValueName *ValName = this->getValueName();
164   unsigned Len = ValName->getKeyLength();
165   const char *Name = ValName->getKeyData();
166   
167   if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
168       || Name[2] != 'v' || Name[3] != 'm')
169     return 0;  // All intrinsics start with 'llvm.'
170
171   assert(Len != 5 && "'llvm.' is an invalid intrinsic name!");
172
173 #define GET_FUNCTION_RECOGNIZER
174 #include "llvm/Intrinsics.gen"
175 #undef GET_FUNCTION_RECOGNIZER
176   return 0;
177 }
178
179 const char *Intrinsic::getName(ID id) {
180   assert(id < num_intrinsics && "Invalid intrinsic ID!");
181   const char * const Table[] = {
182     "not_intrinsic",
183 #define GET_INTRINSIC_NAME_TABLE
184 #include "llvm/Intrinsics.gen"
185 #undef GET_INTRINSIC_NAME_TABLE
186   };
187   return Table[id];
188 }
189
190 const FunctionType *Intrinsic::getType(ID id) {
191   const Type *ResultTy = NULL;
192   std::vector<const Type*> ArgTys;
193   std::vector<FunctionType::ParameterAttributes> Attrs;
194   bool IsVarArg = false;
195   
196 #define GET_INTRINSIC_GENERATOR
197 #include "llvm/Intrinsics.gen"
198 #undef GET_INTRINSIC_GENERATOR
199
200   return FunctionType::get(ResultTy, ArgTys, IsVarArg, Attrs); 
201 }
202
203 Function *Intrinsic::getDeclaration(Module *M, ID id) {
204 // There can never be multiple globals with the same name of different types,
205 // because intrinsics must be a specific type.
206   return cast<Function>(M->getOrInsertFunction(getName(id), getType(id)));
207 }
208
209 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
210   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
211     if (CE->getOpcode() == Instruction::BitCast) {
212       if (isa<PointerType>(CE->getOperand(0)->getType()))
213         return StripPointerCasts(CE->getOperand(0));
214     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
215       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
216         if (!CE->getOperand(i)->isNullValue())
217           return Ptr;
218       return StripPointerCasts(CE->getOperand(0));
219     }
220     return Ptr;
221   }
222
223   if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
224     if (isa<PointerType>(CI->getOperand(0)->getType()))
225       return StripPointerCasts(CI->getOperand(0));
226   } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
227     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
228       if (!isa<Constant>(GEP->getOperand(i)) ||
229           !cast<Constant>(GEP->getOperand(i))->isNullValue())
230         return Ptr;
231     return StripPointerCasts(GEP->getOperand(0));
232   }
233   return Ptr;
234 }
235
236 // vim: sw=2 ai