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" |
Reid Spencer | 26d9ff6 | 2007-04-09 06:11:23 +0000 | [diff] [blame] | 16 | #include "llvm/ParameterAttributes.h" |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 19 | #include "SymbolTableListTraitsImpl.h" |
Chris Lattner | b392d30 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 23 | BasicBlock *ilist_traits<BasicBlock>::createSentinel() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 24 | BasicBlock *Ret = new BasicBlock(); |
| 25 | // This should not be garbage monitored. |
| 26 | LeakDetector::removeGarbageObject(Ret); |
| 27 | return Ret; |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 30 | iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) { |
| 31 | return F->getBasicBlockList(); |
| 32 | } |
| 33 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 34 | Argument *ilist_traits<Argument>::createSentinel() { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 35 | Argument *Ret = new Argument(Type::Int32Ty); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 36 | // This should not be garbage monitored. |
| 37 | LeakDetector::removeGarbageObject(Ret); |
| 38 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | iplist<Argument> &ilist_traits<Argument>::getList(Function *F) { |
| 42 | return F->getArgumentList(); |
| 43 | } |
| 44 | |
| 45 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 46 | // are not in the public header file... |
Chris Lattner | 41baa98 | 2003-11-05 05:15:42 +0000 | [diff] [blame] | 47 | template class SymbolTableListTraits<Argument, Function, Function>; |
| 48 | template class SymbolTableListTraits<BasicBlock, Function, Function>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 51 | // Argument Implementation |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 54 | Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 55 | : Value(Ty, Value::ArgumentVal) { |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 56 | Parent = 0; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 57 | |
| 58 | // Make sure that we get added to a function |
| 59 | LeakDetector::addGarbageObject(this); |
| 60 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 61 | if (Par) |
| 62 | Par->getArgumentList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 63 | setName(Name); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 66 | void Argument::setParent(Function *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 67 | if (getParent()) |
| 68 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 69 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 70 | if (getParent()) |
| 71 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 75 | // ParamAttrsList Implementation |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
| 78 | uint16_t |
| 79 | ParamAttrsList::getParamAttrs(uint16_t Index) const { |
| 80 | unsigned limit = attrs.size(); |
| 81 | for (unsigned i = 0; i < limit; ++i) |
| 82 | if (attrs[i].index == Index) |
| 83 | return attrs[i].attrs; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 84 | return ParamAttr::None; |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 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 | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 92 | Result += "zext "; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 93 | if (Attrs & ParamAttr::SExt) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 94 | Result += "sext "; |
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 "; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 101 | if (Attrs & ParamAttr::StructRet) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 102 | Result += "sret "; |
| 103 | return Result; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | ParamAttrsList::addAttributes(uint16_t Index, uint16_t Attrs) { |
| 108 | // First, try to replace an existing one |
| 109 | for (unsigned i = 0; i < attrs.size(); ++i) |
| 110 | if (attrs[i].index == Index) { |
| 111 | attrs[i].attrs |= Attrs; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // If not found, add a new one |
| 116 | ParamAttrsWithIndex Val; |
| 117 | Val.attrs = Attrs; |
| 118 | Val.index = Index; |
| 119 | attrs.push_back(Val); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | ParamAttrsList::removeAttributes(uint16_t Index, uint16_t Attrs) { |
| 124 | // Find the index from which to remove the attributes |
| 125 | for (unsigned i = 0; i < attrs.size(); ++i) |
| 126 | if (attrs[i].index == Index) { |
| 127 | attrs[i].attrs &= ~Attrs; |
Reid Spencer | a472f66 | 2007-04-11 02:44:20 +0000 | [diff] [blame] | 128 | if (attrs[i].attrs == ParamAttr::None) |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 129 | attrs.erase(&attrs[i]); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // The index wasn't found above |
| 134 | assert(0 && "Index not found for removeAttributes"); |
| 135 | } |
| 136 | |
| 137 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 138 | // Function Implementation |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
| 140 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 141 | Function::Function(const FunctionType *Ty, LinkageTypes Linkage, |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 142 | const std::string &name, Module *ParentModule) |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 143 | : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 144 | ParamAttrs = 0; |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 145 | CallingConvention = 0; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 146 | BasicBlocks.setItemParent(this); |
| 147 | BasicBlocks.setParent(this); |
| 148 | ArgumentList.setItemParent(this); |
| 149 | ArgumentList.setParent(this); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 150 | SymTab = new ValueSymbolTable(); |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 3ae303c | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 152 | assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy) |
| 153 | && "LLVM functions cannot return aggregate values!"); |
| 154 | |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 155 | // Create the arguments vector, all arguments start out unnamed. |
| 156 | for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) { |
| 157 | assert(Ty->getParamType(i) != Type::VoidTy && |
| 158 | "Cannot have void typed arguments!"); |
| 159 | ArgumentList.push_back(new Argument(Ty->getParamType(i))); |
| 160 | } |
| 161 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 162 | // Make sure that we get added to a function |
| 163 | LeakDetector::addGarbageObject(this); |
| 164 | |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 165 | if (ParentModule) |
| 166 | ParentModule->getFunctionList().push_back(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 169 | Function::~Function() { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 170 | dropAllReferences(); // After this it is safe to delete instructions. |
| 171 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 172 | // Delete all of the method arguments and unlink from symbol table... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 173 | ArgumentList.clear(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 174 | ArgumentList.setParent(0); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 175 | delete SymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 178 | void Function::setParent(Module *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 179 | if (getParent()) |
| 180 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 181 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 182 | if (getParent()) |
| 183 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 186 | const FunctionType *Function::getFunctionType() const { |
| 187 | return cast<FunctionType>(getType()->getElementType()); |
Chris Lattner | 7fac070 | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | 07b522d | 2005-01-07 07:40:32 +0000 | [diff] [blame] | 190 | bool Function::isVarArg() const { |
| 191 | return getFunctionType()->isVarArg(); |
| 192 | } |
| 193 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 194 | const Type *Function::getReturnType() const { |
Chris Lattner | 91db582 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 195 | return getFunctionType()->getReturnType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 198 | void Function::removeFromParent() { |
| 199 | getParent()->getFunctionList().remove(this); |
| 200 | } |
| 201 | |
| 202 | void Function::eraseFromParent() { |
| 203 | getParent()->getFunctionList().erase(this); |
| 204 | } |
| 205 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 206 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 207 | // go" of all references that they are maintaining. This allows one to |
| 208 | // 'delete' a whole class at a time, even though there may be circular |
| 209 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 210 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 211 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 212 | // delete. |
| 213 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 214 | void Function::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 215 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 216 | I->dropAllReferences(); |
Chris Lattner | c1b1651 | 2003-09-17 04:58:59 +0000 | [diff] [blame] | 217 | BasicBlocks.clear(); // Delete all basic blocks... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 219 | |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 220 | /// getIntrinsicID - This method returns the ID number of the specified |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 221 | /// function, or Intrinsic::not_intrinsic if the function is not an |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 222 | /// 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] | 223 | /// zero to allow easy checking for whether a function is intrinsic or not. The |
| 224 | /// particular intrinsic functions which correspond to this value are defined in |
| 225 | /// llvm/Intrinsics.h. |
| 226 | /// |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 227 | unsigned Function::getIntrinsicID(bool noAssert) const { |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 228 | const ValueName *ValName = this->getValueName(); |
Reid Spencer | c5f397a | 2007-04-16 07:08:44 +0000 | [diff] [blame] | 229 | if (!ValName) |
| 230 | return 0; |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 231 | unsigned Len = ValName->getKeyLength(); |
| 232 | const char *Name = ValName->getKeyData(); |
| 233 | |
Reid Spencer | 78d71f1 | 2007-04-16 16:56:54 +0000 | [diff] [blame^] | 234 | if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l' |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 235 | || Name[2] != 'v' || Name[3] != 'm') |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 236 | return 0; // All intrinsics start with 'llvm.' |
Chris Lattner | 3284ed7 | 2003-09-19 19:31:41 +0000 | [diff] [blame] | 237 | |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 238 | assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 239 | |
Chris Lattner | ff4d4ee | 2006-03-09 20:35:01 +0000 | [diff] [blame] | 240 | #define GET_FUNCTION_RECOGNIZER |
| 241 | #include "llvm/Intrinsics.gen" |
| 242 | #undef GET_FUNCTION_RECOGNIZER |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 243 | assert(noAssert && "Invalid LLVM intrinsic name"); |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 247 | std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) { |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 248 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
| 249 | const char * const Table[] = { |
| 250 | "not_intrinsic", |
| 251 | #define GET_INTRINSIC_NAME_TABLE |
| 252 | #include "llvm/Intrinsics.gen" |
| 253 | #undef GET_INTRINSIC_NAME_TABLE |
| 254 | }; |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 255 | if (numTys == 0) |
| 256 | return Table[id]; |
| 257 | std::string Result(Table[id]); |
| 258 | for (unsigned i = 0; i < numTys; ++i) |
| 259 | if (Tys[i]) |
| 260 | Result += "." + Tys[i]->getDescription(); |
| 261 | return Result; |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 264 | const FunctionType *Intrinsic::getType(ID id, const Type **Tys, |
| 265 | uint32_t numTys) { |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 266 | const Type *ResultTy = NULL; |
| 267 | std::vector<const Type*> ArgTys; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 268 | bool IsVarArg = false; |
| 269 | |
| 270 | #define GET_INTRINSIC_GENERATOR |
| 271 | #include "llvm/Intrinsics.gen" |
| 272 | #undef GET_INTRINSIC_GENERATOR |
| 273 | |
Reid Spencer | 26d9ff6 | 2007-04-09 06:11:23 +0000 | [diff] [blame] | 274 | return FunctionType::get(ResultTy, ArgTys, IsVarArg); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 277 | Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys, |
| 278 | unsigned numTys) { |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 279 | // There can never be multiple globals with the same name of different types, |
| 280 | // because intrinsics must be a specific type. |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 281 | return cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys), |
| 282 | getType(id, Tys, numTys))); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | 1ccab84 | 2004-10-12 04:32:37 +0000 | [diff] [blame] | 285 | Value *IntrinsicInst::StripPointerCasts(Value *Ptr) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 286 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 287 | if (CE->getOpcode() == Instruction::BitCast) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 288 | if (isa<PointerType>(CE->getOperand(0)->getType())) |
| 289 | return StripPointerCasts(CE->getOperand(0)); |
| 290 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 291 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 292 | if (!CE->getOperand(i)->isNullValue()) |
| 293 | return Ptr; |
| 294 | return StripPointerCasts(CE->getOperand(0)); |
| 295 | } |
| 296 | return Ptr; |
| 297 | } |
| 298 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 299 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) { |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 300 | if (isa<PointerType>(CI->getOperand(0)->getType())) |
| 301 | return StripPointerCasts(CI->getOperand(0)); |
| 302 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
Chris Lattner | 1ccab84 | 2004-10-12 04:32:37 +0000 | [diff] [blame] | 303 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) |
| 304 | if (!isa<Constant>(GEP->getOperand(i)) || |
| 305 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 306 | return Ptr; |
Chris Lattner | 1ccab84 | 2004-10-12 04:32:37 +0000 | [diff] [blame] | 307 | return StripPointerCasts(GEP->getOperand(0)); |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 308 | } |
| 309 | return Ptr; |
| 310 | } |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 311 | |
Reid Spencer | c49dd8d | 2004-07-17 23:50:19 +0000 | [diff] [blame] | 312 | // vim: sw=2 ai |