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" |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 17 | #include "llvm/ParameterAttributes.h" |
Dan Gohman | 3a07148 | 2007-08-20 19:23:34 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ValueTypes.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/LeakDetector.h" |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 20 | #include "llvm/Support/StringPool.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 21 | #include "SymbolTableListTraitsImpl.h" |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/BitVector.h" |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | b392d30 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 27 | BasicBlock *ilist_traits<BasicBlock>::createSentinel() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 28 | BasicBlock *Ret = new BasicBlock(); |
| 29 | // This should not be garbage monitored. |
| 30 | LeakDetector::removeGarbageObject(Ret); |
| 31 | return Ret; |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 34 | iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) { |
| 35 | return F->getBasicBlockList(); |
| 36 | } |
| 37 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 38 | Argument *ilist_traits<Argument>::createSentinel() { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 39 | Argument *Ret = new Argument(Type::Int32Ty); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 40 | // This should not be garbage monitored. |
| 41 | LeakDetector::removeGarbageObject(Ret); |
| 42 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | iplist<Argument> &ilist_traits<Argument>::getList(Function *F) { |
| 46 | return F->getArgumentList(); |
| 47 | } |
| 48 | |
| 49 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 50 | // are not in the public header file... |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 51 | template class SymbolTableListTraits<Argument, Function>; |
| 52 | template class SymbolTableListTraits<BasicBlock, Function>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 55 | // Argument Implementation |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 58 | Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 59 | : Value(Ty, Value::ArgumentVal) { |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 60 | Parent = 0; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 61 | |
| 62 | // Make sure that we get added to a function |
| 63 | LeakDetector::addGarbageObject(this); |
| 64 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 65 | if (Par) |
| 66 | Par->getArgumentList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 67 | setName(Name); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 70 | void Argument::setParent(Function *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 71 | if (getParent()) |
| 72 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 73 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 74 | if (getParent()) |
| 75 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 78 | /// getArgNo - Return the index of this formal argument in its containing |
| 79 | /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. |
| 80 | unsigned Argument::getArgNo() const { |
| 81 | const Function *F = getParent(); |
| 82 | assert(F && "Argument is not in a function"); |
| 83 | |
| 84 | Function::const_arg_iterator AI = F->arg_begin(); |
| 85 | unsigned ArgIdx = 0; |
| 86 | for (; &*AI != this; ++AI) |
| 87 | ++ArgIdx; |
| 88 | |
| 89 | return ArgIdx; |
| 90 | } |
| 91 | |
| 92 | /// hasByValAttr - Return true if this argument has the byval attribute on it |
| 93 | /// in its containing function. |
| 94 | bool Argument::hasByValAttr() const { |
| 95 | if (!isa<PointerType>(getType())) return false; |
| 96 | return getParent()->paramHasAttr(getArgNo()+1, ParamAttr::ByVal); |
| 97 | } |
| 98 | |
| 99 | /// hasNoAliasAttr - Return true if this argument has the noalias attribute on |
| 100 | /// it in its containing function. |
| 101 | bool Argument::hasNoAliasAttr() const { |
| 102 | if (!isa<PointerType>(getType())) return false; |
| 103 | return getParent()->paramHasAttr(getArgNo()+1, ParamAttr::NoAlias); |
| 104 | } |
| 105 | |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 106 | /// hasSRetAttr - Return true if this argument has the sret attribute on |
| 107 | /// it in its containing function. |
| 108 | bool Argument::hasStructRetAttr() const { |
| 109 | if (!isa<PointerType>(getType())) return false; |
Owen Anderson | a54570d | 2008-02-18 09:22:21 +0000 | [diff] [blame] | 110 | if (this != getParent()->arg_begin()) return false; // StructRet param must be first param |
Owen Anderson | c66655e | 2008-02-18 04:06:26 +0000 | [diff] [blame] | 111 | return getParent()->paramHasAttr(1, ParamAttr::StructRet); |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 117 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 118 | // Helper Methods in Function |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 119 | //===----------------------------------------------------------------------===// |
| 120 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 121 | const FunctionType *Function::getFunctionType() const { |
| 122 | return cast<FunctionType>(getType()->getElementType()); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 125 | bool Function::isVarArg() const { |
| 126 | return getFunctionType()->isVarArg(); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 129 | const Type *Function::getReturnType() const { |
| 130 | return getFunctionType()->getReturnType(); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 133 | void Function::removeFromParent() { |
| 134 | getParent()->getFunctionList().remove(this); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 137 | void Function::eraseFromParent() { |
| 138 | getParent()->getFunctionList().erase(this); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 141 | /// @brief Determine whether the function has the given attribute. |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 142 | bool Function::paramHasAttr(uint16_t i, ParameterAttributes attr) const { |
| 143 | return ParamAttrs && ParamAttrs->paramHasAttr(i, attr); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 146 | /// @brief Determine if the function cannot return. |
| 147 | bool Function::doesNotReturn() const { |
| 148 | return paramHasAttr(0, ParamAttr::NoReturn); |
Duncan Sands | b41f872 | 2007-11-30 18:19:18 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 151 | /// @brief Determine if the function cannot unwind. |
| 152 | bool Function::doesNotThrow() const { |
| 153 | return paramHasAttr(0, ParamAttr::NoUnwind); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 156 | /// @brief Determine if the function does not access memory. |
| 157 | bool Function::doesNotAccessMemory() const { |
| 158 | return paramHasAttr(0, ParamAttr::ReadNone); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 161 | /// @brief Determine if the function does not access or only reads memory. |
| 162 | bool Function::onlyReadsMemory() const { |
| 163 | return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly); |
| 164 | } |
| 165 | |
| 166 | /// @brief Determine if the function returns a structure. |
| 167 | bool Function::isStructReturn() const { |
| 168 | return paramHasAttr(1, ParamAttr::StructRet); |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 171 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 172 | // Function Implementation |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 173 | //===----------------------------------------------------------------------===// |
| 174 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 175 | Function::Function(const FunctionType *Ty, LinkageTypes Linkage, |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 176 | const std::string &name, Module *ParentModule) |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 177 | : GlobalValue(PointerType::getUnqual(Ty), |
| 178 | Value::FunctionVal, 0, 0, Linkage, name), |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 179 | ParamAttrs(0) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 180 | SymTab = new ValueSymbolTable(); |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 181 | |
Devang Patel | 616f0e0 | 2008-02-20 22:36:03 +0000 | [diff] [blame] | 182 | assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy |
Devang Patel | 9fea019 | 2008-02-21 22:24:17 +0000 | [diff] [blame^] | 183 | || isa<StructType>(getReturnType())) |
Chris Lattner | 3ae303c | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 184 | && "LLVM functions cannot return aggregate values!"); |
| 185 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 186 | // If the function has arguments, mark them as lazily built. |
| 187 | if (Ty->getNumParams()) |
| 188 | SubclassData = 1; // Set the "has lazy arguments" bit. |
| 189 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 190 | // Make sure that we get added to a function |
| 191 | LeakDetector::addGarbageObject(this); |
| 192 | |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 193 | if (ParentModule) |
| 194 | ParentModule->getFunctionList().push_back(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 197 | Function::~Function() { |
| 198 | dropAllReferences(); // After this it is safe to delete instructions. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 200 | // Delete all of the method arguments and unlink from symbol table... |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 201 | ArgumentList.clear(); |
| 202 | delete SymTab; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 203 | |
| 204 | // Drop our reference to the parameter attributes, if any. |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 205 | if (ParamAttrs) |
| 206 | ParamAttrs->dropRef(); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 207 | |
| 208 | // Remove the function from the on-the-side collector table. |
| 209 | clearCollector(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 212 | void Function::BuildLazyArguments() const { |
| 213 | // Create the arguments vector, all arguments start out unnamed. |
| 214 | const FunctionType *FT = getFunctionType(); |
| 215 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| 216 | assert(FT->getParamType(i) != Type::VoidTy && |
| 217 | "Cannot have void typed arguments!"); |
| 218 | ArgumentList.push_back(new Argument(FT->getParamType(i))); |
| 219 | } |
| 220 | |
| 221 | // Clear the lazy arguments bit. |
| 222 | const_cast<Function*>(this)->SubclassData &= ~1; |
| 223 | } |
| 224 | |
| 225 | size_t Function::arg_size() const { |
| 226 | return getFunctionType()->getNumParams(); |
| 227 | } |
| 228 | bool Function::arg_empty() const { |
| 229 | return getFunctionType()->getNumParams() == 0; |
| 230 | } |
| 231 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 232 | void Function::setParent(Module *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 233 | if (getParent()) |
| 234 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 236 | if (getParent()) |
| 237 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 240 | void Function::setParamAttrs(const ParamAttrsList *attrs) { |
| 241 | // Avoid deleting the ParamAttrsList if they are setting the |
| 242 | // attributes to the same list. |
| 243 | if (ParamAttrs == attrs) |
| 244 | return; |
| 245 | |
| 246 | // Drop reference on the old ParamAttrsList |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 247 | if (ParamAttrs) |
| 248 | ParamAttrs->dropRef(); |
| 249 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 250 | // Add reference to the new ParamAttrsList |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 251 | if (attrs) |
| 252 | attrs->addRef(); |
| 253 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 254 | // Set the new ParamAttrsList. |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 255 | ParamAttrs = attrs; |
| 256 | } |
| 257 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 258 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 259 | // go" of all references that they are maintaining. This allows one to |
| 260 | // 'delete' a whole class at a time, even though there may be circular |
| 261 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 262 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 263 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 264 | // delete. |
| 265 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 266 | void Function::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 267 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 268 | I->dropAllReferences(); |
Chris Lattner | c1b1651 | 2003-09-17 04:58:59 +0000 | [diff] [blame] | 269 | BasicBlocks.clear(); // Delete all basic blocks... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 271 | |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 272 | // Maintain the collector name for each function in an on-the-side table. This |
| 273 | // saves allocating an additional word in Function for programs which do not use |
| 274 | // GC (i.e., most programs) at the cost of increased overhead for clients which |
| 275 | // do use GC. |
| 276 | static DenseMap<const Function*,PooledStringPtr> *CollectorNames; |
| 277 | static StringPool *CollectorNamePool; |
| 278 | |
| 279 | bool Function::hasCollector() const { |
| 280 | return CollectorNames && CollectorNames->count(this); |
| 281 | } |
| 282 | |
| 283 | const char *Function::getCollector() const { |
| 284 | assert(hasCollector() && "Function has no collector"); |
| 285 | return *(*CollectorNames)[this]; |
| 286 | } |
| 287 | |
| 288 | void Function::setCollector(const char *Str) { |
| 289 | if (!CollectorNamePool) |
| 290 | CollectorNamePool = new StringPool(); |
| 291 | if (!CollectorNames) |
| 292 | CollectorNames = new DenseMap<const Function*,PooledStringPtr>(); |
| 293 | (*CollectorNames)[this] = CollectorNamePool->intern(Str); |
| 294 | } |
| 295 | |
| 296 | void Function::clearCollector() { |
| 297 | if (CollectorNames) { |
| 298 | CollectorNames->erase(this); |
| 299 | if (CollectorNames->empty()) { |
| 300 | delete CollectorNames; |
| 301 | CollectorNames = 0; |
Gordon Henriksen | 4b904b9 | 2007-12-10 03:35:18 +0000 | [diff] [blame] | 302 | if (CollectorNamePool->empty()) { |
| 303 | delete CollectorNamePool; |
| 304 | CollectorNamePool = 0; |
| 305 | } |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 310 | /// getIntrinsicID - This method returns the ID number of the specified |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 311 | /// function, or Intrinsic::not_intrinsic if the function is not an |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 312 | /// 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] | 313 | /// zero to allow easy checking for whether a function is intrinsic or not. The |
| 314 | /// particular intrinsic functions which correspond to this value are defined in |
| 315 | /// llvm/Intrinsics.h. |
| 316 | /// |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 317 | unsigned Function::getIntrinsicID(bool noAssert) const { |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 318 | const ValueName *ValName = this->getValueName(); |
Reid Spencer | c5f397a | 2007-04-16 07:08:44 +0000 | [diff] [blame] | 319 | if (!ValName) |
| 320 | return 0; |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 321 | unsigned Len = ValName->getKeyLength(); |
| 322 | const char *Name = ValName->getKeyData(); |
| 323 | |
Reid Spencer | 78d71f1 | 2007-04-16 16:56:54 +0000 | [diff] [blame] | 324 | if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l' |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 325 | || Name[2] != 'v' || Name[3] != 'm') |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 326 | return 0; // All intrinsics start with 'llvm.' |
Chris Lattner | 3284ed7 | 2003-09-19 19:31:41 +0000 | [diff] [blame] | 327 | |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 328 | assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 329 | |
Chris Lattner | ff4d4ee | 2006-03-09 20:35:01 +0000 | [diff] [blame] | 330 | #define GET_FUNCTION_RECOGNIZER |
| 331 | #include "llvm/Intrinsics.gen" |
| 332 | #undef GET_FUNCTION_RECOGNIZER |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 333 | assert(noAssert && "Invalid LLVM intrinsic name"); |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 337 | std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) { |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 338 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
| 339 | const char * const Table[] = { |
| 340 | "not_intrinsic", |
| 341 | #define GET_INTRINSIC_NAME_TABLE |
| 342 | #include "llvm/Intrinsics.gen" |
| 343 | #undef GET_INTRINSIC_NAME_TABLE |
| 344 | }; |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 345 | if (numTys == 0) |
| 346 | return Table[id]; |
| 347 | std::string Result(Table[id]); |
| 348 | for (unsigned i = 0; i < numTys; ++i) |
| 349 | if (Tys[i]) |
Dan Gohman | 3a07148 | 2007-08-20 19:23:34 +0000 | [diff] [blame] | 350 | Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i])); |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 351 | return Result; |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 354 | const FunctionType *Intrinsic::getType(ID id, const Type **Tys, |
Chris Lattner | 31f82df | 2007-06-05 23:49:06 +0000 | [diff] [blame] | 355 | unsigned numTys) { |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 356 | const Type *ResultTy = NULL; |
| 357 | std::vector<const Type*> ArgTys; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 358 | bool IsVarArg = false; |
| 359 | |
| 360 | #define GET_INTRINSIC_GENERATOR |
| 361 | #include "llvm/Intrinsics.gen" |
| 362 | #undef GET_INTRINSIC_GENERATOR |
| 363 | |
Reid Spencer | 26d9ff6 | 2007-04-09 06:11:23 +0000 | [diff] [blame] | 364 | return FunctionType::get(ResultTy, ArgTys, IsVarArg); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 367 | const ParamAttrsList *Intrinsic::getParamAttrs(ID id) { |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 368 | ParamAttrsVector Attrs; |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 369 | ParameterAttributes Attr = ParamAttr::None; |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 370 | |
| 371 | #define GET_INTRINSIC_ATTRIBUTES |
| 372 | #include "llvm/Intrinsics.gen" |
| 373 | #undef GET_INTRINSIC_ATTRIBUTES |
| 374 | |
| 375 | // Intrinsics cannot throw exceptions. |
| 376 | Attr |= ParamAttr::NoUnwind; |
| 377 | |
| 378 | Attrs.push_back(ParamAttrsWithIndex::get(0, Attr)); |
Chris Lattner | 76719ba | 2008-01-03 01:20:12 +0000 | [diff] [blame] | 379 | return ParamAttrsList::get(Attrs); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 382 | Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys, |
| 383 | unsigned numTys) { |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 384 | // There can never be multiple globals with the same name of different types, |
| 385 | // because intrinsics must be a specific type. |
| 386 | Function *F = |
| 387 | cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys), |
| 388 | getType(id, Tys, numTys))); |
| 389 | F->setParamAttrs(getParamAttrs(id)); |
| 390 | return F; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Chris Lattner | 1ccab84 | 2004-10-12 04:32:37 +0000 | [diff] [blame] | 393 | Value *IntrinsicInst::StripPointerCasts(Value *Ptr) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 394 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 395 | if (CE->getOpcode() == Instruction::BitCast) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 396 | if (isa<PointerType>(CE->getOperand(0)->getType())) |
| 397 | return StripPointerCasts(CE->getOperand(0)); |
| 398 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 399 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 400 | if (!CE->getOperand(i)->isNullValue()) |
| 401 | return Ptr; |
| 402 | return StripPointerCasts(CE->getOperand(0)); |
| 403 | } |
| 404 | return Ptr; |
| 405 | } |
| 406 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 407 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 408 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 409 | return StripPointerCasts(CI->getOperand(0)); |
| 410 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
Chris Lattner | bbe9b8a | 2007-04-25 05:49:09 +0000 | [diff] [blame] | 411 | if (GEP->hasAllZeroIndices()) |
| 412 | return StripPointerCasts(GEP->getOperand(0)); |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 413 | } |
| 414 | return Ptr; |
| 415 | } |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 416 | |
Reid Spencer | c49dd8d | 2004-07-17 23:50:19 +0000 | [diff] [blame] | 417 | // vim: sw=2 ai |