cf8fcdec6a879d2f88d88c711a4912c902545173
[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 #include "llvm/ADT/StringExtras.h"
21 using namespace llvm;
22
23 BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
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>::createSentinel() {
35   Argument *Ret = new Argument(Type::Int32Ty);
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 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 SymbolTable();
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
142 // dropAllReferences() - This function causes all the subinstructions to "let
143 // go" of all references that they are maintaining.  This allows one to
144 // 'delete' a whole class at a time, even though there may be circular
145 // references... first all references are dropped, and all use counts go to
146 // zero.  Then everything is deleted for real.  Note that no operations are
147 // valid on an object that has "dropped all references", except operator
148 // delete.
149 //
150 void Function::dropAllReferences() {
151   for (iterator I = begin(), E = end(); I != E; ++I)
152     I->dropAllReferences();
153   BasicBlocks.clear();    // Delete all basic blocks...
154 }
155
156 /// getIntrinsicID - This method returns the ID number of the specified
157 /// function, or Intrinsic::not_intrinsic if the function is not an
158 /// intrinsic, or if the pointer is null.  This value is always defined to be
159 /// zero to allow easy checking for whether a function is intrinsic or not.  The
160 /// particular intrinsic functions which correspond to this value are defined in
161 /// llvm/Intrinsics.h.
162 ///
163 unsigned Function::getIntrinsicID() const {
164   const std::string& Name = this->getName();
165   if (Name.size() < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
166       || Name[2] != 'v' || Name[3] != 'm')
167     return 0;  // All intrinsics start with 'llvm.'
168
169   assert(Name.size() != 5 && "'llvm.' is an invalid intrinsic name!");
170
171 #define GET_FUNCTION_RECOGNIZER
172 #include "llvm/Intrinsics.gen"
173 #undef GET_FUNCTION_RECOGNIZER
174   return 0;
175 }
176
177 const char *Intrinsic::getName(ID id) {
178   assert(id < num_intrinsics && "Invalid intrinsic ID!");
179   const char * const Table[] = {
180     "not_intrinsic",
181 #define GET_INTRINSIC_NAME_TABLE
182 #include "llvm/Intrinsics.gen"
183 #undef GET_INTRINSIC_NAME_TABLE
184   };
185   return Table[id];
186 }
187
188 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
189   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
190     if (CE->getOpcode() == Instruction::BitCast) {
191       if (isa<PointerType>(CE->getOperand(0)->getType()))
192         return StripPointerCasts(CE->getOperand(0));
193     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
194       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
195         if (!CE->getOperand(i)->isNullValue())
196           return Ptr;
197       return StripPointerCasts(CE->getOperand(0));
198     }
199     return Ptr;
200   }
201
202   if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
203     if (isa<PointerType>(CI->getOperand(0)->getType()))
204       return StripPointerCasts(CI->getOperand(0));
205   } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
206     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
207       if (!isa<Constant>(GEP->getOperand(i)) ||
208           !cast<Constant>(GEP->getOperand(i))->isNullValue())
209         return Ptr;
210     return StripPointerCasts(GEP->getOperand(0));
211   }
212   return Ptr;
213 }
214
215 // vim: sw=2 ai