Quotes should be printed before private prefix; some code clean up.
[llvm/msp430.git] / lib / VMCore / Function.cpp
blob3a991f62d84e355a266c63858dce9ed9f3dd3fa1
1 //===-- Function.cpp - Implement the Global object classes ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Function class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/IntrinsicInst.h"
17 #include "llvm/CodeGen/ValueTypes.h"
18 #include "llvm/Support/LeakDetector.h"
19 #include "llvm/Support/StringPool.h"
20 #include "SymbolTableListTraitsImpl.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/StringExtras.h"
23 using namespace llvm;
26 // Explicit instantiations of SymbolTableListTraits since some of the methods
27 // are not in the public header file...
28 template class SymbolTableListTraits<Argument, Function>;
29 template class SymbolTableListTraits<BasicBlock, Function>;
31 //===----------------------------------------------------------------------===//
32 // Argument Implementation
33 //===----------------------------------------------------------------------===//
35 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
36 : Value(Ty, Value::ArgumentVal) {
37 Parent = 0;
39 // Make sure that we get added to a function
40 LeakDetector::addGarbageObject(this);
42 if (Par)
43 Par->getArgumentList().push_back(this);
44 setName(Name);
47 void Argument::setParent(Function *parent) {
48 if (getParent())
49 LeakDetector::addGarbageObject(this);
50 Parent = parent;
51 if (getParent())
52 LeakDetector::removeGarbageObject(this);
55 /// getArgNo - Return the index of this formal argument in its containing
56 /// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
57 unsigned Argument::getArgNo() const {
58 const Function *F = getParent();
59 assert(F && "Argument is not in a function");
61 Function::const_arg_iterator AI = F->arg_begin();
62 unsigned ArgIdx = 0;
63 for (; &*AI != this; ++AI)
64 ++ArgIdx;
66 return ArgIdx;
69 /// hasByValAttr - Return true if this argument has the byval attribute on it
70 /// in its containing function.
71 bool Argument::hasByValAttr() const {
72 if (!isa<PointerType>(getType())) return false;
73 return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal);
76 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on
77 /// it in its containing function.
78 bool Argument::hasNoAliasAttr() const {
79 if (!isa<PointerType>(getType())) return false;
80 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias);
83 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
84 /// on it in its containing function.
85 bool Argument::hasNoCaptureAttr() const {
86 if (!isa<PointerType>(getType())) return false;
87 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture);
90 /// hasSRetAttr - Return true if this argument has the sret attribute on
91 /// it in its containing function.
92 bool Argument::hasStructRetAttr() const {
93 if (!isa<PointerType>(getType())) return false;
94 if (this != getParent()->arg_begin())
95 return false; // StructRet param must be first param
96 return getParent()->paramHasAttr(1, Attribute::StructRet);
99 /// addAttr - Add a Attribute to an argument
100 void Argument::addAttr(Attributes attr) {
101 getParent()->addAttribute(getArgNo() + 1, attr);
104 /// removeAttr - Remove a Attribute from an argument
105 void Argument::removeAttr(Attributes attr) {
106 getParent()->removeAttribute(getArgNo() + 1, attr);
110 //===----------------------------------------------------------------------===//
111 // Helper Methods in Function
112 //===----------------------------------------------------------------------===//
114 const FunctionType *Function::getFunctionType() const {
115 return cast<FunctionType>(getType()->getElementType());
118 bool Function::isVarArg() const {
119 return getFunctionType()->isVarArg();
122 const Type *Function::getReturnType() const {
123 return getFunctionType()->getReturnType();
126 void Function::removeFromParent() {
127 getParent()->getFunctionList().remove(this);
130 void Function::eraseFromParent() {
131 getParent()->getFunctionList().erase(this);
134 //===----------------------------------------------------------------------===//
135 // Function Implementation
136 //===----------------------------------------------------------------------===//
138 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
139 const std::string &name, Module *ParentModule)
140 : GlobalValue(PointerType::getUnqual(Ty),
141 Value::FunctionVal, 0, 0, Linkage, name) {
142 assert(FunctionType::isValidReturnType(getReturnType()) &&
143 !isa<OpaqueType>(getReturnType()) && "invalid return type");
144 SymTab = new ValueSymbolTable();
146 // If the function has arguments, mark them as lazily built.
147 if (Ty->getNumParams())
148 SubclassData = 1; // Set the "has lazy arguments" bit.
150 // Make sure that we get added to a function
151 LeakDetector::addGarbageObject(this);
153 if (ParentModule)
154 ParentModule->getFunctionList().push_back(this);
156 // Ensure intrinsics have the right parameter attributes.
157 if (unsigned IID = getIntrinsicID())
158 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID)));
162 Function::~Function() {
163 dropAllReferences(); // After this it is safe to delete instructions.
165 // Delete all of the method arguments and unlink from symbol table...
166 ArgumentList.clear();
167 delete SymTab;
169 // Remove the function from the on-the-side GC table.
170 clearGC();
173 void Function::BuildLazyArguments() const {
174 // Create the arguments vector, all arguments start out unnamed.
175 const FunctionType *FT = getFunctionType();
176 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
177 assert(FT->getParamType(i) != Type::VoidTy &&
178 "Cannot have void typed arguments!");
179 ArgumentList.push_back(new Argument(FT->getParamType(i)));
182 // Clear the lazy arguments bit.
183 const_cast<Function*>(this)->SubclassData &= ~1;
186 size_t Function::arg_size() const {
187 return getFunctionType()->getNumParams();
189 bool Function::arg_empty() const {
190 return getFunctionType()->getNumParams() == 0;
193 void Function::setParent(Module *parent) {
194 if (getParent())
195 LeakDetector::addGarbageObject(this);
196 Parent = parent;
197 if (getParent())
198 LeakDetector::removeGarbageObject(this);
201 // dropAllReferences() - This function causes all the subinstructions to "let
202 // go" of all references that they are maintaining. This allows one to
203 // 'delete' a whole class at a time, even though there may be circular
204 // references... first all references are dropped, and all use counts go to
205 // zero. Then everything is deleted for real. Note that no operations are
206 // valid on an object that has "dropped all references", except operator
207 // delete.
209 void Function::dropAllReferences() {
210 for (iterator I = begin(), E = end(); I != E; ++I)
211 I->dropAllReferences();
212 BasicBlocks.clear(); // Delete all basic blocks...
215 void Function::addAttribute(unsigned i, Attributes attr) {
216 AttrListPtr PAL = getAttributes();
217 PAL = PAL.addAttr(i, attr);
218 setAttributes(PAL);
221 void Function::removeAttribute(unsigned i, Attributes attr) {
222 AttrListPtr PAL = getAttributes();
223 PAL = PAL.removeAttr(i, attr);
224 setAttributes(PAL);
227 // Maintain the GC name for each function in an on-the-side table. This saves
228 // allocating an additional word in Function for programs which do not use GC
229 // (i.e., most programs) at the cost of increased overhead for clients which do
230 // use GC.
231 static DenseMap<const Function*,PooledStringPtr> *GCNames;
232 static StringPool *GCNamePool;
234 bool Function::hasGC() const {
235 return GCNames && GCNames->count(this);
238 const char *Function::getGC() const {
239 assert(hasGC() && "Function has no collector");
240 return *(*GCNames)[this];
243 void Function::setGC(const char *Str) {
244 if (!GCNamePool)
245 GCNamePool = new StringPool();
246 if (!GCNames)
247 GCNames = new DenseMap<const Function*,PooledStringPtr>();
248 (*GCNames)[this] = GCNamePool->intern(Str);
251 void Function::clearGC() {
252 if (GCNames) {
253 GCNames->erase(this);
254 if (GCNames->empty()) {
255 delete GCNames;
256 GCNames = 0;
257 if (GCNamePool->empty()) {
258 delete GCNamePool;
259 GCNamePool = 0;
265 /// copyAttributesFrom - copy all additional attributes (those not needed to
266 /// create a Function) from the Function Src to this one.
267 void Function::copyAttributesFrom(const GlobalValue *Src) {
268 assert(isa<Function>(Src) && "Expected a Function!");
269 GlobalValue::copyAttributesFrom(Src);
270 const Function *SrcF = cast<Function>(Src);
271 setCallingConv(SrcF->getCallingConv());
272 setAttributes(SrcF->getAttributes());
273 if (SrcF->hasGC())
274 setGC(SrcF->getGC());
275 else
276 clearGC();
279 /// getIntrinsicID - This method returns the ID number of the specified
280 /// function, or Intrinsic::not_intrinsic if the function is not an
281 /// intrinsic, or if the pointer is null. This value is always defined to be
282 /// zero to allow easy checking for whether a function is intrinsic or not. The
283 /// particular intrinsic functions which correspond to this value are defined in
284 /// llvm/Intrinsics.h.
286 unsigned Function::getIntrinsicID() const {
287 const ValueName *ValName = this->getValueName();
288 if (!ValName)
289 return 0;
290 unsigned Len = ValName->getKeyLength();
291 const char *Name = ValName->getKeyData();
293 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
294 || Name[2] != 'v' || Name[3] != 'm')
295 return 0; // All intrinsics start with 'llvm.'
297 #define GET_FUNCTION_RECOGNIZER
298 #include "llvm/Intrinsics.gen"
299 #undef GET_FUNCTION_RECOGNIZER
300 return 0;
303 std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
304 assert(id < num_intrinsics && "Invalid intrinsic ID!");
305 const char * const Table[] = {
306 "not_intrinsic",
307 #define GET_INTRINSIC_NAME_TABLE
308 #include "llvm/Intrinsics.gen"
309 #undef GET_INTRINSIC_NAME_TABLE
311 if (numTys == 0)
312 return Table[id];
313 std::string Result(Table[id]);
314 for (unsigned i = 0; i < numTys; ++i) {
315 if (const PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
316 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
317 MVT::getMVT(PTyp->getElementType()).getMVTString();
319 else if (Tys[i])
320 Result += "." + MVT::getMVT(Tys[i]).getMVTString();
322 return Result;
325 const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
326 unsigned numTys) {
327 const Type *ResultTy = NULL;
328 std::vector<const Type*> ArgTys;
329 bool IsVarArg = false;
331 #define GET_INTRINSIC_GENERATOR
332 #include "llvm/Intrinsics.gen"
333 #undef GET_INTRINSIC_GENERATOR
335 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
338 bool Intrinsic::isOverloaded(ID id) {
339 const bool OTable[] = {
340 false,
341 #define GET_INTRINSIC_OVERLOAD_TABLE
342 #include "llvm/Intrinsics.gen"
343 #undef GET_INTRINSIC_OVERLOAD_TABLE
345 return OTable[id];
348 /// This defines the "Intrinsic::getAttributes(ID id)" method.
349 #define GET_INTRINSIC_ATTRIBUTES
350 #include "llvm/Intrinsics.gen"
351 #undef GET_INTRINSIC_ATTRIBUTES
353 Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
354 unsigned numTys) {
355 // There can never be multiple globals with the same name of different types,
356 // because intrinsics must be a specific type.
357 return
358 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
359 getType(id, Tys, numTys)));
362 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
363 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
364 #include "llvm/Intrinsics.gen"
365 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
367 // vim: sw=2 ai