For PR411:
[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, 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 void Argument::setParent(Function *parent) {
65   if (getParent())
66     LeakDetector::addGarbageObject(this);
67   Parent = parent;
68   if (getParent())
69     LeakDetector::removeGarbageObject(this);
70 }
71
72 //===----------------------------------------------------------------------===//
73 // Function Implementation
74 //===----------------------------------------------------------------------===//
75
76 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
77                    const std::string &name, Module *ParentModule)
78   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
79   CallingConvention = 0;
80   BasicBlocks.setItemParent(this);
81   BasicBlocks.setParent(this);
82   ArgumentList.setItemParent(this);
83   ArgumentList.setParent(this);
84   SymTab = new ValueSymbolTable();
85
86   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
87          && "LLVM functions cannot return aggregate values!");
88
89   // Create the arguments vector, all arguments start out unnamed.
90   for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
91     assert(Ty->getParamType(i) != Type::VoidTy &&
92            "Cannot have void typed arguments!");
93     ArgumentList.push_back(new Argument(Ty->getParamType(i)));
94   }
95
96   // Make sure that we get added to a function
97   LeakDetector::addGarbageObject(this);
98
99   if (ParentModule)
100     ParentModule->getFunctionList().push_back(this);
101 }
102
103 Function::~Function() {
104   dropAllReferences();    // After this it is safe to delete instructions.
105
106   // Delete all of the method arguments and unlink from symbol table...
107   ArgumentList.clear();
108   ArgumentList.setParent(0);
109   delete SymTab;
110 }
111
112 void Function::setParent(Module *parent) {
113   if (getParent())
114     LeakDetector::addGarbageObject(this);
115   Parent = parent;
116   if (getParent())
117     LeakDetector::removeGarbageObject(this);
118 }
119
120 const FunctionType *Function::getFunctionType() const {
121   return cast<FunctionType>(getType()->getElementType());
122 }
123
124 bool Function::isVarArg() const {
125   return getFunctionType()->isVarArg();
126 }
127
128 const Type *Function::getReturnType() const {
129   return getFunctionType()->getReturnType();
130 }
131
132 void Function::removeFromParent() {
133   getParent()->getFunctionList().remove(this);
134 }
135
136 void Function::eraseFromParent() {
137   getParent()->getFunctionList().erase(this);
138 }
139
140 // dropAllReferences() - This function causes all the subinstructions to "let
141 // go" of all references that they are maintaining.  This allows one to
142 // 'delete' a whole class at a time, even though there may be circular
143 // references... first all references are dropped, and all use counts go to
144 // zero.  Then everything is deleted for real.  Note that no operations are
145 // valid on an object that has "dropped all references", except operator
146 // delete.
147 //
148 void Function::dropAllReferences() {
149   for (iterator I = begin(), E = end(); I != E; ++I)
150     I->dropAllReferences();
151   BasicBlocks.clear();    // Delete all basic blocks...
152 }
153
154 /// getIntrinsicID - This method returns the ID number of the specified
155 /// function, or Intrinsic::not_intrinsic if the function is not an
156 /// intrinsic, or if the pointer is null.  This value is always defined to be
157 /// zero to allow easy checking for whether a function is intrinsic or not.  The
158 /// particular intrinsic functions which correspond to this value are defined in
159 /// llvm/Intrinsics.h.
160 ///
161 unsigned Function::getIntrinsicID() const {
162   const std::string& Name = this->getName();
163   if (Name.size() < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
164       || Name[2] != 'v' || Name[3] != 'm')
165     return 0;  // All intrinsics start with 'llvm.'
166
167   assert(Name.size() != 5 && "'llvm.' is an invalid intrinsic name!");
168
169 #define GET_FUNCTION_RECOGNIZER
170 #include "llvm/Intrinsics.gen"
171 #undef GET_FUNCTION_RECOGNIZER
172   return 0;
173 }
174
175 const char *Intrinsic::getName(ID id) {
176   assert(id < num_intrinsics && "Invalid intrinsic ID!");
177   const char * const Table[] = {
178     "not_intrinsic",
179 #define GET_INTRINSIC_NAME_TABLE
180 #include "llvm/Intrinsics.gen"
181 #undef GET_INTRINSIC_NAME_TABLE
182   };
183   return Table[id];
184 }
185
186 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
187   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
188     if (CE->getOpcode() == Instruction::BitCast) {
189       if (isa<PointerType>(CE->getOperand(0)->getType()))
190         return StripPointerCasts(CE->getOperand(0));
191     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
192       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
193         if (!CE->getOperand(i)->isNullValue())
194           return Ptr;
195       return StripPointerCasts(CE->getOperand(0));
196     }
197     return Ptr;
198   }
199
200   if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
201     if (isa<PointerType>(CI->getOperand(0)->getType()))
202       return StripPointerCasts(CI->getOperand(0));
203   } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
204     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
205       if (!isa<Constant>(GEP->getOperand(i)) ||
206           !cast<Constant>(GEP->getOperand(i))->isNullValue())
207         return Ptr;
208     return StripPointerCasts(GEP->getOperand(0));
209   }
210   return Ptr;
211 }
212
213 // vim: sw=2 ai