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 | // |
| 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. |
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" |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 20 | #include "SymbolTableListTraitsImpl.h" |
Chris Lattner | b392d30 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 24 | BasicBlock *ilist_traits<BasicBlock>::createSentinel() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 25 | BasicBlock *Ret = new BasicBlock(); |
| 26 | // This should not be garbage monitored. |
| 27 | LeakDetector::removeGarbageObject(Ret); |
| 28 | return Ret; |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 31 | iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) { |
| 32 | return F->getBasicBlockList(); |
| 33 | } |
| 34 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 35 | Argument *ilist_traits<Argument>::createSentinel() { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 36 | Argument *Ret = new Argument(Type::Int32Ty); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 37 | // This should not be garbage monitored. |
| 38 | LeakDetector::removeGarbageObject(Ret); |
| 39 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | iplist<Argument> &ilist_traits<Argument>::getList(Function *F) { |
| 43 | return F->getArgumentList(); |
| 44 | } |
| 45 | |
| 46 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 47 | // are not in the public header file... |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 48 | template class SymbolTableListTraits<Argument, Function>; |
| 49 | template class SymbolTableListTraits<BasicBlock, Function>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 50 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 52 | // Argument Implementation |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 55 | Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 56 | : Value(Ty, Value::ArgumentVal) { |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 57 | Parent = 0; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 58 | |
| 59 | // Make sure that we get added to a function |
| 60 | LeakDetector::addGarbageObject(this); |
| 61 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 62 | if (Par) |
| 63 | Par->getArgumentList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 64 | setName(Name); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 67 | void Argument::setParent(Function *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 68 | if (getParent()) |
| 69 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 70 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 71 | if (getParent()) |
| 72 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 76 | // ParamAttrsList Implementation |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | |
| 79 | uint16_t |
| 80 | ParamAttrsList::getParamAttrs(uint16_t Index) const { |
| 81 | unsigned limit = attrs.size(); |
Duncan Sands | d4d7f9d | 2007-11-30 18:20:58 +0000 | [diff] [blame^] | 82 | for (unsigned i = 0; i < limit && attrs[i].index <= Index; ++i) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 83 | if (attrs[i].index == Index) |
| 84 | return attrs[i].attrs; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 85 | return ParamAttr::None; |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 88 | std::string |
| 89 | ParamAttrsList::getParamAttrsText(uint16_t Attrs) { |
| 90 | std::string Result; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 91 | if (Attrs & ParamAttr::ZExt) |
Reid Spencer | 314e1cb | 2007-07-19 23:13:04 +0000 | [diff] [blame] | 92 | Result += "zeroext "; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 93 | if (Attrs & ParamAttr::SExt) |
Reid Spencer | 314e1cb | 2007-07-19 23:13:04 +0000 | [diff] [blame] | 94 | Result += "signext "; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 95 | if (Attrs & ParamAttr::NoReturn) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 96 | Result += "noreturn "; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 97 | if (Attrs & ParamAttr::NoUnwind) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 98 | Result += "nounwind "; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 99 | if (Attrs & ParamAttr::InReg) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 100 | Result += "inreg "; |
Zhou Sheng | 2444a9a | 2007-06-05 05:28:26 +0000 | [diff] [blame] | 101 | if (Attrs & ParamAttr::NoAlias) |
| 102 | Result += "noalias "; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 103 | if (Attrs & ParamAttr::StructRet) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 104 | Result += "sret "; |
Rafael Espindola | b567e3f | 2007-07-06 10:57:03 +0000 | [diff] [blame] | 105 | if (Attrs & ParamAttr::ByVal) |
| 106 | Result += "byval "; |
Duncan Sands | 644f917 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 107 | if (Attrs & ParamAttr::Nest) |
| 108 | Result += "nest "; |
Duncan Sands | a89a113 | 2007-11-22 20:23:04 +0000 | [diff] [blame] | 109 | if (Attrs & ParamAttr::ReadNone) |
| 110 | Result += "readnone "; |
| 111 | if (Attrs & ParamAttr::ReadOnly) |
| 112 | Result += "readonly "; |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 113 | return Result; |
| 114 | } |
| 115 | |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 116 | /// onlyInformative - Returns whether only informative attributes are set. |
| 117 | static inline bool onlyInformative(uint16_t attrs) { |
| 118 | return !(attrs & ~ParamAttr::Informative); |
| 119 | } |
| 120 | |
| 121 | bool |
| 122 | ParamAttrsList::areCompatible(const ParamAttrsList *A, const ParamAttrsList *B){ |
| 123 | if (A == B) |
| 124 | return true; |
| 125 | unsigned ASize = A ? A->size() : 0; |
| 126 | unsigned BSize = B ? B->size() : 0; |
| 127 | unsigned AIndex = 0; |
| 128 | unsigned BIndex = 0; |
| 129 | |
| 130 | while (AIndex < ASize && BIndex < BSize) { |
| 131 | uint16_t AIdx = A->getParamIndex(AIndex); |
| 132 | uint16_t BIdx = B->getParamIndex(BIndex); |
| 133 | uint16_t AAttrs = A->getParamAttrsAtIndex(AIndex); |
| 134 | uint16_t BAttrs = B->getParamAttrsAtIndex(AIndex); |
| 135 | |
| 136 | if (AIdx < BIdx) { |
| 137 | if (!onlyInformative(AAttrs)) |
| 138 | return false; |
| 139 | ++AIndex; |
| 140 | } else if (BIdx < AIdx) { |
| 141 | if (!onlyInformative(BAttrs)) |
| 142 | return false; |
| 143 | ++BIndex; |
| 144 | } else { |
| 145 | if (!onlyInformative(AAttrs ^ BAttrs)) |
| 146 | return false; |
| 147 | ++AIndex; |
| 148 | ++BIndex; |
| 149 | } |
| 150 | } |
| 151 | for (; AIndex < ASize; ++AIndex) |
| 152 | if (!onlyInformative(A->getParamAttrsAtIndex(AIndex))) |
| 153 | return false; |
| 154 | for (; BIndex < BSize; ++BIndex) |
| 155 | if (!onlyInformative(B->getParamAttrsAtIndex(AIndex))) |
| 156 | return false; |
| 157 | return true; |
| 158 | } |
| 159 | |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 160 | void |
| 161 | ParamAttrsList::Profile(FoldingSetNodeID &ID) const { |
| 162 | for (unsigned i = 0; i < attrs.size(); ++i) { |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 163 | uint32_t val = uint32_t(attrs[i].attrs) << 16 | attrs[i].index; |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 164 | ID.AddInteger(val); |
| 165 | } |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 168 | static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists; |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 169 | |
Duncan Sands | b41f872 | 2007-11-30 18:19:18 +0000 | [diff] [blame] | 170 | const ParamAttrsList * |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 171 | ParamAttrsList::get(const ParamAttrsVector &attrVec) { |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 172 | // If there are no attributes then return a null ParamAttrsList pointer. |
| 173 | if (attrVec.empty()) |
| 174 | return 0; |
| 175 | |
Duncan Sands | d781335 | 2007-09-11 14:40:04 +0000 | [diff] [blame] | 176 | #ifndef NDEBUG |
Duncan Sands | 04eb67e | 2007-11-20 14:09:29 +0000 | [diff] [blame] | 177 | for (unsigned i = 0, e = attrVec.size(); i < e; ++i) { |
| 178 | assert(attrVec[i].attrs != ParamAttr::None |
| 179 | && "Pointless parameter attribute!"); |
| 180 | assert((!i || attrVec[i-1].index < attrVec[i].index) |
| 181 | && "Misordered ParamAttrsList!"); |
| 182 | } |
Duncan Sands | d781335 | 2007-09-11 14:40:04 +0000 | [diff] [blame] | 183 | #endif |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 184 | |
| 185 | // Otherwise, build a key to look up the existing attributes. |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 186 | ParamAttrsList key(attrVec); |
| 187 | FoldingSetNodeID ID; |
| 188 | key.Profile(ID); |
| 189 | void *InsertPos; |
| 190 | ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 191 | |
| 192 | // If we didn't find any existing attributes of the same shape then |
| 193 | // create a new one and insert it. |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 194 | if (!PAL) { |
| 195 | PAL = new ParamAttrsList(attrVec); |
| 196 | ParamAttrsLists->InsertNode(PAL, InsertPos); |
| 197 | } |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 198 | |
| 199 | // Return the ParamAttrsList that we found or created. |
Reid Spencer | 4388f0b | 2007-04-22 05:46:44 +0000 | [diff] [blame] | 200 | return PAL; |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Duncan Sands | b41f872 | 2007-11-30 18:19:18 +0000 | [diff] [blame] | 203 | const ParamAttrsList * |
| 204 | ParamAttrsList::getModified(const ParamAttrsList *PAL, |
| 205 | const ParamAttrsVector &modVec) { |
| 206 | if (modVec.empty()) |
| 207 | return PAL; |
| 208 | |
| 209 | #ifndef NDEBUG |
| 210 | for (unsigned i = 0, e = modVec.size(); i < e; ++i) |
| 211 | assert((!i || modVec[i-1].index < modVec[i].index) |
| 212 | && "Misordered ParamAttrsList!"); |
| 213 | #endif |
| 214 | |
| 215 | if (!PAL) { |
| 216 | // Strip any instances of ParamAttr::None from modVec before calling 'get'. |
| 217 | ParamAttrsVector newVec; |
| 218 | for (unsigned i = 0, e = modVec.size(); i < e; ++i) |
| 219 | if (modVec[i].attrs != ParamAttr::None) |
| 220 | newVec.push_back(modVec[i]); |
| 221 | return get(newVec); |
| 222 | } |
| 223 | |
| 224 | const ParamAttrsVector &oldVec = PAL->attrs; |
| 225 | |
| 226 | ParamAttrsVector newVec; |
| 227 | unsigned oldI = 0; |
| 228 | unsigned modI = 0; |
| 229 | unsigned oldE = oldVec.size(); |
| 230 | unsigned modE = modVec.size(); |
| 231 | |
| 232 | while (oldI < oldE && modI < modE) { |
| 233 | uint16_t oldIndex = oldVec[oldI].index; |
| 234 | uint16_t modIndex = modVec[modI].index; |
| 235 | |
| 236 | if (oldIndex < modIndex) { |
| 237 | newVec.push_back(oldVec[oldI]); |
| 238 | ++oldI; |
| 239 | } else if (modIndex < oldIndex) { |
| 240 | if (modVec[modI].attrs != ParamAttr::None) |
| 241 | newVec.push_back(modVec[modI]); |
| 242 | ++modI; |
| 243 | } else { |
| 244 | // Same index - overwrite or delete existing attributes. |
| 245 | if (modVec[modI].attrs != ParamAttr::None) |
| 246 | newVec.push_back(modVec[modI]); |
| 247 | ++oldI; |
| 248 | ++modI; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | for (; oldI < oldE; ++oldI) |
| 253 | newVec.push_back(oldVec[oldI]); |
| 254 | for (; modI < modE; ++modI) |
| 255 | if (modVec[modI].attrs != ParamAttr::None) |
| 256 | newVec.push_back(modVec[modI]); |
| 257 | |
| 258 | return get(newVec); |
| 259 | } |
| 260 | |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 261 | ParamAttrsList::~ParamAttrsList() { |
| 262 | ParamAttrsLists->RemoveNode(this); |
| 263 | } |
| 264 | |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 265 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 266 | // Function Implementation |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 267 | //===----------------------------------------------------------------------===// |
| 268 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 269 | Function::Function(const FunctionType *Ty, LinkageTypes Linkage, |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 270 | const std::string &name, Module *ParentModule) |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 271 | : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name), |
| 272 | ParamAttrs(0) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 273 | SymTab = new ValueSymbolTable(); |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 3ae303c | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 275 | assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy) |
| 276 | && "LLVM functions cannot return aggregate values!"); |
| 277 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 278 | // If the function has arguments, mark them as lazily built. |
| 279 | if (Ty->getNumParams()) |
| 280 | SubclassData = 1; // Set the "has lazy arguments" bit. |
| 281 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 282 | // Make sure that we get added to a function |
| 283 | LeakDetector::addGarbageObject(this); |
| 284 | |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 285 | if (ParentModule) |
| 286 | ParentModule->getFunctionList().push_back(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 289 | Function::~Function() { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 290 | dropAllReferences(); // After this it is safe to delete instructions. |
| 291 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 292 | // Delete all of the method arguments and unlink from symbol table... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 293 | ArgumentList.clear(); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 294 | delete SymTab; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 295 | |
| 296 | // Drop our reference to the parameter attributes, if any. |
| 297 | if (ParamAttrs) |
| 298 | ParamAttrs->dropRef(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 301 | void Function::BuildLazyArguments() const { |
| 302 | // Create the arguments vector, all arguments start out unnamed. |
| 303 | const FunctionType *FT = getFunctionType(); |
| 304 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| 305 | assert(FT->getParamType(i) != Type::VoidTy && |
| 306 | "Cannot have void typed arguments!"); |
| 307 | ArgumentList.push_back(new Argument(FT->getParamType(i))); |
| 308 | } |
| 309 | |
| 310 | // Clear the lazy arguments bit. |
| 311 | const_cast<Function*>(this)->SubclassData &= ~1; |
| 312 | } |
| 313 | |
| 314 | size_t Function::arg_size() const { |
| 315 | return getFunctionType()->getNumParams(); |
| 316 | } |
| 317 | bool Function::arg_empty() const { |
| 318 | return getFunctionType()->getNumParams() == 0; |
| 319 | } |
| 320 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 321 | void Function::setParent(Module *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 322 | if (getParent()) |
| 323 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 324 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 325 | if (getParent()) |
| 326 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 329 | void Function::setParamAttrs(const ParamAttrsList *attrs) { |
| 330 | // Avoid deleting the ParamAttrsList if they are setting the |
| 331 | // attributes to the same list. |
| 332 | if (ParamAttrs == attrs) |
| 333 | return; |
| 334 | |
| 335 | // Drop reference on the old ParamAttrsList |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 336 | if (ParamAttrs) |
| 337 | ParamAttrs->dropRef(); |
| 338 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 339 | // Add reference to the new ParamAttrsList |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 340 | if (attrs) |
| 341 | attrs->addRef(); |
| 342 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 343 | // Set the new ParamAttrsList. |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 344 | ParamAttrs = attrs; |
| 345 | } |
| 346 | |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 347 | const FunctionType *Function::getFunctionType() const { |
| 348 | return cast<FunctionType>(getType()->getElementType()); |
Chris Lattner | 7fac070 | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | 07b522d | 2005-01-07 07:40:32 +0000 | [diff] [blame] | 351 | bool Function::isVarArg() const { |
| 352 | return getFunctionType()->isVarArg(); |
| 353 | } |
| 354 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 355 | const Type *Function::getReturnType() const { |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 356 | return getFunctionType()->getReturnType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 359 | void Function::removeFromParent() { |
| 360 | getParent()->getFunctionList().remove(this); |
| 361 | } |
| 362 | |
| 363 | void Function::eraseFromParent() { |
| 364 | getParent()->getFunctionList().erase(this); |
| 365 | } |
| 366 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 367 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 368 | // go" of all references that they are maintaining. This allows one to |
| 369 | // 'delete' a whole class at a time, even though there may be circular |
| 370 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 371 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 372 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 373 | // delete. |
| 374 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 375 | void Function::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 376 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 377 | I->dropAllReferences(); |
Chris Lattner | c1b1651 | 2003-09-17 04:58:59 +0000 | [diff] [blame] | 378 | BasicBlocks.clear(); // Delete all basic blocks... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 379 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 380 | |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 381 | /// getIntrinsicID - This method returns the ID number of the specified |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 382 | /// function, or Intrinsic::not_intrinsic if the function is not an |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 383 | /// 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] | 384 | /// zero to allow easy checking for whether a function is intrinsic or not. The |
| 385 | /// particular intrinsic functions which correspond to this value are defined in |
| 386 | /// llvm/Intrinsics.h. |
| 387 | /// |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 388 | unsigned Function::getIntrinsicID(bool noAssert) const { |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 389 | const ValueName *ValName = this->getValueName(); |
Reid Spencer | c5f397a | 2007-04-16 07:08:44 +0000 | [diff] [blame] | 390 | if (!ValName) |
| 391 | return 0; |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 392 | unsigned Len = ValName->getKeyLength(); |
| 393 | const char *Name = ValName->getKeyData(); |
| 394 | |
Reid Spencer | 78d71f1 | 2007-04-16 16:56:54 +0000 | [diff] [blame] | 395 | if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l' |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 396 | || Name[2] != 'v' || Name[3] != 'm') |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 397 | return 0; // All intrinsics start with 'llvm.' |
Chris Lattner | 3284ed7 | 2003-09-19 19:31:41 +0000 | [diff] [blame] | 398 | |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 399 | assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 400 | |
Chris Lattner | ff4d4ee | 2006-03-09 20:35:01 +0000 | [diff] [blame] | 401 | #define GET_FUNCTION_RECOGNIZER |
| 402 | #include "llvm/Intrinsics.gen" |
| 403 | #undef GET_FUNCTION_RECOGNIZER |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 404 | assert(noAssert && "Invalid LLVM intrinsic name"); |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 408 | std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) { |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 409 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
| 410 | const char * const Table[] = { |
| 411 | "not_intrinsic", |
| 412 | #define GET_INTRINSIC_NAME_TABLE |
| 413 | #include "llvm/Intrinsics.gen" |
| 414 | #undef GET_INTRINSIC_NAME_TABLE |
| 415 | }; |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 416 | if (numTys == 0) |
| 417 | return Table[id]; |
| 418 | std::string Result(Table[id]); |
| 419 | for (unsigned i = 0; i < numTys; ++i) |
| 420 | if (Tys[i]) |
Dan Gohman | 3a07148 | 2007-08-20 19:23:34 +0000 | [diff] [blame] | 421 | Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i])); |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 422 | return Result; |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 425 | const FunctionType *Intrinsic::getType(ID id, const Type **Tys, |
Chris Lattner | 31f82df | 2007-06-05 23:49:06 +0000 | [diff] [blame] | 426 | unsigned numTys) { |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 427 | const Type *ResultTy = NULL; |
| 428 | std::vector<const Type*> ArgTys; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 429 | bool IsVarArg = false; |
| 430 | |
| 431 | #define GET_INTRINSIC_GENERATOR |
| 432 | #include "llvm/Intrinsics.gen" |
| 433 | #undef GET_INTRINSIC_GENERATOR |
| 434 | |
Reid Spencer | 26d9ff6 | 2007-04-09 06:11:23 +0000 | [diff] [blame] | 435 | return FunctionType::get(ResultTy, ArgTys, IsVarArg); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 438 | Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys, |
| 439 | unsigned numTys) { |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 440 | // There can never be multiple globals with the same name of different types, |
| 441 | // because intrinsics must be a specific type. |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 442 | return cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys), |
| 443 | getType(id, Tys, numTys))); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Chris Lattner | 1ccab84 | 2004-10-12 04:32:37 +0000 | [diff] [blame] | 446 | Value *IntrinsicInst::StripPointerCasts(Value *Ptr) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 447 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 448 | if (CE->getOpcode() == Instruction::BitCast) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 449 | if (isa<PointerType>(CE->getOperand(0)->getType())) |
| 450 | return StripPointerCasts(CE->getOperand(0)); |
| 451 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 452 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 453 | if (!CE->getOperand(i)->isNullValue()) |
| 454 | return Ptr; |
| 455 | return StripPointerCasts(CE->getOperand(0)); |
| 456 | } |
| 457 | return Ptr; |
| 458 | } |
| 459 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 460 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 461 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 462 | return StripPointerCasts(CI->getOperand(0)); |
| 463 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
Chris Lattner | bbe9b8a | 2007-04-25 05:49:09 +0000 | [diff] [blame] | 464 | if (GEP->hasAllZeroIndices()) |
| 465 | return StripPointerCasts(GEP->getOperand(0)); |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 466 | } |
| 467 | return Ptr; |
| 468 | } |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 469 | |
Reid Spencer | c49dd8d | 2004-07-17 23:50:19 +0000 | [diff] [blame] | 470 | // vim: sw=2 ai |