Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Function.cpp - Implement the Global object classes ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 10 | // This file implements the Function class for the VMCore library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 16 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | 3a07148 | 2007-08-20 19:23:34 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/ValueTypes.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/LeakDetector.h" |
Owen Anderson | aab59c5 | 2009-06-17 22:23:31 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 20 | #include "llvm/Support/StringPool.h" |
Owen Anderson | aab59c5 | 2009-06-17 22:23:31 +0000 | [diff] [blame] | 21 | #include "llvm/System/RWMutex.h" |
Owen Anderson | 7d42b95 | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 22 | #include "llvm/System/Threading.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 23 | #include "SymbolTableListTraitsImpl.h" |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | b392d30 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 28 | |
| 29 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 30 | // are not in the public header file... |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 31 | template class SymbolTableListTraits<Argument, Function>; |
| 32 | template class SymbolTableListTraits<BasicBlock, Function>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 35 | // Argument Implementation |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 38 | Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 39 | : Value(Ty, Value::ArgumentVal) { |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 40 | Parent = 0; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 41 | |
| 42 | // Make sure that we get added to a function |
| 43 | LeakDetector::addGarbageObject(this); |
| 44 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 45 | if (Par) |
| 46 | Par->getArgumentList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 47 | setName(Name); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 50 | void Argument::setParent(Function *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 51 | if (getParent()) |
| 52 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 53 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 54 | if (getParent()) |
| 55 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 58 | /// getArgNo - Return the index of this formal argument in its containing |
| 59 | /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. |
| 60 | unsigned Argument::getArgNo() const { |
| 61 | const Function *F = getParent(); |
| 62 | assert(F && "Argument is not in a function"); |
| 63 | |
| 64 | Function::const_arg_iterator AI = F->arg_begin(); |
| 65 | unsigned ArgIdx = 0; |
| 66 | for (; &*AI != this; ++AI) |
| 67 | ++ArgIdx; |
| 68 | |
| 69 | return ArgIdx; |
| 70 | } |
| 71 | |
| 72 | /// hasByValAttr - Return true if this argument has the byval attribute on it |
| 73 | /// in its containing function. |
| 74 | bool Argument::hasByValAttr() const { |
| 75 | if (!isa<PointerType>(getType())) return false; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 76 | return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /// hasNoAliasAttr - Return true if this argument has the noalias attribute on |
| 80 | /// it in its containing function. |
| 81 | bool Argument::hasNoAliasAttr() const { |
| 82 | if (!isa<PointerType>(getType())) return false; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 83 | return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Duncan Sands | df128eb | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 86 | /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute |
| 87 | /// on it in its containing function. |
| 88 | bool Argument::hasNoCaptureAttr() const { |
| 89 | if (!isa<PointerType>(getType())) return false; |
| 90 | return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture); |
| 91 | } |
| 92 | |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 93 | /// hasSRetAttr - Return true if this argument has the sret attribute on |
| 94 | /// it in its containing function. |
| 95 | bool Argument::hasStructRetAttr() const { |
| 96 | if (!isa<PointerType>(getType())) return false; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 97 | if (this != getParent()->arg_begin()) |
| 98 | return false; // StructRet param must be first param |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 99 | return getParent()->paramHasAttr(1, Attribute::StructRet); |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 102 | /// addAttr - Add a Attribute to an argument |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 103 | void Argument::addAttr(Attributes attr) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 104 | getParent()->addAttribute(getArgNo() + 1, attr); |
Gordon Henriksen | 2d9cc21 | 2008-04-28 17:37:06 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 106 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 107 | /// removeAttr - Remove a Attribute from an argument |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 108 | void Argument::removeAttr(Attributes attr) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 109 | getParent()->removeAttribute(getArgNo() + 1, attr); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 111 | |
| 112 | |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 113 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 114 | // Helper Methods in Function |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
| 116 | |
Owen Anderson | 38264b1 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 117 | LLVMContext *Function::getContext() const { |
Owen Anderson | e70b637 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 118 | const Module* M = getParent(); |
Owen Anderson | 0a2c458 | 2009-07-02 18:03:58 +0000 | [diff] [blame] | 119 | if (M) return &M->getContext(); |
| 120 | return 0; |
| 121 | } |
| 122 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 123 | const FunctionType *Function::getFunctionType() const { |
| 124 | return cast<FunctionType>(getType()->getElementType()); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 127 | bool Function::isVarArg() const { |
| 128 | return getFunctionType()->isVarArg(); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 131 | const Type *Function::getReturnType() const { |
| 132 | return getFunctionType()->getReturnType(); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 135 | void Function::removeFromParent() { |
| 136 | getParent()->getFunctionList().remove(this); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 139 | void Function::eraseFromParent() { |
| 140 | getParent()->getFunctionList().erase(this); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 143 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 144 | // Function Implementation |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
| 146 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 147 | Function::Function(const FunctionType *Ty, LinkageTypes Linkage, |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 148 | const std::string &name, Module *ParentModule) |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 149 | : GlobalValue(PointerType::getUnqual(Ty), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 150 | Value::FunctionVal, 0, 0, Linkage, name) { |
Chris Lattner | 654695b | 2009-01-05 07:58:59 +0000 | [diff] [blame] | 151 | assert(FunctionType::isValidReturnType(getReturnType()) && |
| 152 | !isa<OpaqueType>(getReturnType()) && "invalid return type"); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 153 | SymTab = new ValueSymbolTable(); |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 154 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 155 | // If the function has arguments, mark them as lazily built. |
| 156 | if (Ty->getNumParams()) |
| 157 | SubclassData = 1; // Set the "has lazy arguments" bit. |
| 158 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 159 | // Make sure that we get added to a function |
| 160 | LeakDetector::addGarbageObject(this); |
| 161 | |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 162 | if (ParentModule) |
| 163 | ParentModule->getFunctionList().push_back(this); |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 164 | |
| 165 | // Ensure intrinsics have the right parameter attributes. |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 166 | if (unsigned IID = getIntrinsicID()) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 167 | setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID))); |
Devang Patel | d334a43 | 2008-09-02 20:51:15 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 171 | Function::~Function() { |
| 172 | dropAllReferences(); // After this it is safe to delete instructions. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 174 | // Delete all of the method arguments and unlink from symbol table... |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 175 | ArgumentList.clear(); |
| 176 | delete SymTab; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 177 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 178 | // Remove the function from the on-the-side GC table. |
| 179 | clearGC(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 182 | void Function::BuildLazyArguments() const { |
| 183 | // Create the arguments vector, all arguments start out unnamed. |
| 184 | const FunctionType *FT = getFunctionType(); |
| 185 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| 186 | assert(FT->getParamType(i) != Type::VoidTy && |
| 187 | "Cannot have void typed arguments!"); |
| 188 | ArgumentList.push_back(new Argument(FT->getParamType(i))); |
| 189 | } |
| 190 | |
| 191 | // Clear the lazy arguments bit. |
| 192 | const_cast<Function*>(this)->SubclassData &= ~1; |
| 193 | } |
| 194 | |
| 195 | size_t Function::arg_size() const { |
| 196 | return getFunctionType()->getNumParams(); |
| 197 | } |
| 198 | bool Function::arg_empty() const { |
| 199 | return getFunctionType()->getNumParams() == 0; |
| 200 | } |
| 201 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 202 | void Function::setParent(Module *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 203 | if (getParent()) |
| 204 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 206 | if (getParent()) |
| 207 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 211 | // go" of all references that they are maintaining. This allows one to |
| 212 | // 'delete' a whole class at a time, even though there may be circular |
| 213 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 214 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 215 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 216 | // delete. |
| 217 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 218 | void Function::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 219 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 220 | I->dropAllReferences(); |
Chris Lattner | c1b1651 | 2003-09-17 04:58:59 +0000 | [diff] [blame] | 221 | BasicBlocks.clear(); // Delete all basic blocks... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 222 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 223 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 224 | void Function::addAttribute(unsigned i, Attributes attr) { |
| 225 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 226 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 227 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 230 | void Function::removeAttribute(unsigned i, Attributes attr) { |
| 231 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 232 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 233 | setAttributes(PAL); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 236 | // Maintain the GC name for each function in an on-the-side table. This saves |
| 237 | // allocating an additional word in Function for programs which do not use GC |
| 238 | // (i.e., most programs) at the cost of increased overhead for clients which do |
| 239 | // use GC. |
Owen Anderson | ed14e76 | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 240 | static DenseMap<const Function*,PooledStringPtr> *GCNames; |
| 241 | static StringPool *GCNamePool; |
Owen Anderson | 7f1ef67 | 2009-06-18 20:56:48 +0000 | [diff] [blame] | 242 | static ManagedStatic<sys::SmartRWMutex<true> > GCLock; |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 243 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 244 | bool Function::hasGC() const { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame^] | 245 | sys::SmartScopedReader<true> Reader(*GCLock); |
Owen Anderson | 7f1ef67 | 2009-06-18 20:56:48 +0000 | [diff] [blame] | 246 | return GCNames && GCNames->count(this); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 249 | const char *Function::getGC() const { |
| 250 | assert(hasGC() && "Function has no collector"); |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame^] | 251 | sys::SmartScopedReader<true> Reader(*GCLock); |
Owen Anderson | 7f1ef67 | 2009-06-18 20:56:48 +0000 | [diff] [blame] | 252 | return *(*GCNames)[this]; |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 255 | void Function::setGC(const char *Str) { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame^] | 256 | sys::SmartScopedWriter<true> Writer(*GCLock); |
Owen Anderson | ed14e76 | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 257 | if (!GCNamePool) |
| 258 | GCNamePool = new StringPool(); |
| 259 | if (!GCNames) |
| 260 | GCNames = new DenseMap<const Function*,PooledStringPtr>(); |
| 261 | (*GCNames)[this] = GCNamePool->intern(Str); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 264 | void Function::clearGC() { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame^] | 265 | sys::SmartScopedWriter<true> Writer(*GCLock); |
Owen Anderson | ed14e76 | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 266 | if (GCNames) { |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 267 | GCNames->erase(this); |
Owen Anderson | ed14e76 | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 268 | if (GCNames->empty()) { |
| 269 | delete GCNames; |
| 270 | GCNames = 0; |
| 271 | if (GCNamePool->empty()) { |
| 272 | delete GCNamePool; |
| 273 | GCNamePool = 0; |
| 274 | } |
| 275 | } |
| 276 | } |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 279 | /// copyAttributesFrom - copy all additional attributes (those not needed to |
| 280 | /// create a Function) from the Function Src to this one. |
| 281 | void Function::copyAttributesFrom(const GlobalValue *Src) { |
| 282 | assert(isa<Function>(Src) && "Expected a Function!"); |
| 283 | GlobalValue::copyAttributesFrom(Src); |
| 284 | const Function *SrcF = cast<Function>(Src); |
| 285 | setCallingConv(SrcF->getCallingConv()); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 286 | setAttributes(SrcF->getAttributes()); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 287 | if (SrcF->hasGC()) |
| 288 | setGC(SrcF->getGC()); |
| 289 | else |
| 290 | clearGC(); |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 293 | /// getIntrinsicID - This method returns the ID number of the specified |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 294 | /// function, or Intrinsic::not_intrinsic if the function is not an |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 295 | /// intrinsic, or if the pointer is null. This value is always defined to be |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 296 | /// zero to allow easy checking for whether a function is intrinsic or not. The |
| 297 | /// particular intrinsic functions which correspond to this value are defined in |
| 298 | /// llvm/Intrinsics.h. |
| 299 | /// |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 300 | unsigned Function::getIntrinsicID() const { |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 301 | const ValueName *ValName = this->getValueName(); |
Reid Spencer | c5f397a | 2007-04-16 07:08:44 +0000 | [diff] [blame] | 302 | if (!ValName) |
| 303 | return 0; |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 304 | unsigned Len = ValName->getKeyLength(); |
| 305 | const char *Name = ValName->getKeyData(); |
| 306 | |
Reid Spencer | 78d71f1 | 2007-04-16 16:56:54 +0000 | [diff] [blame] | 307 | if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l' |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 308 | || Name[2] != 'v' || Name[3] != 'm') |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 309 | return 0; // All intrinsics start with 'llvm.' |
Chris Lattner | 3284ed7 | 2003-09-19 19:31:41 +0000 | [diff] [blame] | 310 | |
Chris Lattner | ff4d4ee | 2006-03-09 20:35:01 +0000 | [diff] [blame] | 311 | #define GET_FUNCTION_RECOGNIZER |
| 312 | #include "llvm/Intrinsics.gen" |
| 313 | #undef GET_FUNCTION_RECOGNIZER |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 317 | std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) { |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 318 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
| 319 | const char * const Table[] = { |
| 320 | "not_intrinsic", |
| 321 | #define GET_INTRINSIC_NAME_TABLE |
| 322 | #include "llvm/Intrinsics.gen" |
| 323 | #undef GET_INTRINSIC_NAME_TABLE |
| 324 | }; |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 325 | if (numTys == 0) |
| 326 | return Table[id]; |
| 327 | std::string Result(Table[id]); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 328 | for (unsigned i = 0; i < numTys; ++i) { |
| 329 | if (const PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) { |
| 330 | Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) + |
| 331 | MVT::getMVT(PTyp->getElementType()).getMVTString(); |
| 332 | } |
| 333 | else if (Tys[i]) |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 334 | Result += "." + MVT::getMVT(Tys[i]).getMVTString(); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 335 | } |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 336 | return Result; |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 339 | const FunctionType *Intrinsic::getType(ID id, const Type **Tys, |
Chris Lattner | 31f82df | 2007-06-05 23:49:06 +0000 | [diff] [blame] | 340 | unsigned numTys) { |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 341 | const Type *ResultTy = NULL; |
| 342 | std::vector<const Type*> ArgTys; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 343 | bool IsVarArg = false; |
| 344 | |
| 345 | #define GET_INTRINSIC_GENERATOR |
| 346 | #include "llvm/Intrinsics.gen" |
| 347 | #undef GET_INTRINSIC_GENERATOR |
| 348 | |
Reid Spencer | 26d9ff6 | 2007-04-09 06:11:23 +0000 | [diff] [blame] | 349 | return FunctionType::get(ResultTy, ArgTys, IsVarArg); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 352 | bool Intrinsic::isOverloaded(ID id) { |
| 353 | const bool OTable[] = { |
| 354 | false, |
| 355 | #define GET_INTRINSIC_OVERLOAD_TABLE |
| 356 | #include "llvm/Intrinsics.gen" |
| 357 | #undef GET_INTRINSIC_OVERLOAD_TABLE |
| 358 | }; |
| 359 | return OTable[id]; |
| 360 | } |
| 361 | |
Chris Lattner | 49b7ee1 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 362 | /// This defines the "Intrinsic::getAttributes(ID id)" method. |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 363 | #define GET_INTRINSIC_ATTRIBUTES |
| 364 | #include "llvm/Intrinsics.gen" |
| 365 | #undef GET_INTRINSIC_ATTRIBUTES |
| 366 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 367 | Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys, |
| 368 | unsigned numTys) { |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 369 | // There can never be multiple globals with the same name of different types, |
| 370 | // because intrinsics must be a specific type. |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 371 | return |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 372 | cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys), |
| 373 | getType(id, Tys, numTys))); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 376 | // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. |
| 377 | #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
| 378 | #include "llvm/Intrinsics.gen" |
| 379 | #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
| 380 | |
Jay Foad | 557169d | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 381 | /// hasAddressTaken - returns true if there are any uses of this function |
| 382 | /// other than direct calls or invokes to it. |
| 383 | bool Function::hasAddressTaken() const { |
| 384 | for (Value::use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) { |
| 385 | if (I.getOperandNo() != 0 || |
| 386 | (!isa<CallInst>(*I) && !isa<InvokeInst>(*I))) |
| 387 | return true; |
| 388 | } |
| 389 | return false; |
| 390 | } |
| 391 | |
Reid Spencer | c49dd8d | 2004-07-17 23:50:19 +0000 | [diff] [blame] | 392 | // vim: sw=2 ai |