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 | // |
Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the Function class for the IR library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Function.h" |
Michael Ilseman | 516d703 | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 16 | #include "SymbolTableListTraitsImpl.h" |
Rafael Espindola | 2050af8 | 2011-05-16 03:05:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b392d30 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ValueTypes.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 20 | #include "llvm/IR/CallSite.h" |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 23 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IntrinsicInst.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 26 | #include "llvm/IR/MDBuilder.h" |
| 27 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ManagedStatic.h" |
| 30 | #include "llvm/Support/RWMutex.h" |
| 31 | #include "llvm/Support/StringPool.h" |
| 32 | #include "llvm/Support/Threading.h" |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 35 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 36 | // are not in the public header file... |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 37 | template class llvm::SymbolTableListTraits<Argument>; |
| 38 | template class llvm::SymbolTableListTraits<BasicBlock>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 41 | // Argument Implementation |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 44 | void Argument::anchor() { } |
| 45 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 46 | Argument::Argument(Type *Ty, const Twine &Name, Function *Par) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 47 | : Value(Ty, Value::ArgumentVal) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 48 | Parent = nullptr; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 50 | if (Par) |
| 51 | Par->getArgumentList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 52 | setName(Name); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 55 | void Argument::setParent(Function *parent) { |
| 56 | Parent = parent; |
| 57 | } |
| 58 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 59 | /// getArgNo - Return the index of this formal argument in its containing |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 60 | /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 61 | unsigned Argument::getArgNo() const { |
| 62 | const Function *F = getParent(); |
| 63 | assert(F && "Argument is not in a function"); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 64 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 65 | Function::const_arg_iterator AI = F->arg_begin(); |
| 66 | unsigned ArgIdx = 0; |
| 67 | for (; &*AI != this; ++AI) |
| 68 | ++ArgIdx; |
| 69 | |
| 70 | return ArgIdx; |
| 71 | } |
| 72 | |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 73 | /// hasNonNullAttr - Return true if this argument has the nonnull attribute on |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 74 | /// it in its containing function. Also returns true if at least one byte is |
| 75 | /// known to be dereferenceable and the pointer is in addrspace(0). |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 76 | bool Argument::hasNonNullAttr() const { |
| 77 | if (!getType()->isPointerTy()) return false; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 78 | if (getParent()->getAttributes(). |
| 79 | hasAttribute(getArgNo()+1, Attribute::NonNull)) |
| 80 | return true; |
| 81 | else if (getDereferenceableBytes() > 0 && |
| 82 | getType()->getPointerAddressSpace() == 0) |
| 83 | return true; |
| 84 | return false; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 87 | /// hasByValAttr - Return true if this argument has the byval attribute on it |
| 88 | /// in its containing function. |
| 89 | bool Argument::hasByValAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 90 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 91 | return hasAttribute(Attribute::ByVal); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 94 | bool Argument::hasSwiftSelfAttr() const { |
| 95 | return getParent()->getAttributes(). |
| 96 | hasAttribute(getArgNo()+1, Attribute::SwiftSelf); |
| 97 | } |
| 98 | |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 99 | bool Argument::hasSwiftErrorAttr() const { |
| 100 | return getParent()->getAttributes(). |
| 101 | hasAttribute(getArgNo()+1, Attribute::SwiftError); |
| 102 | } |
| 103 | |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 104 | /// \brief Return true if this argument has the inalloca attribute on it in |
| 105 | /// its containing function. |
| 106 | bool Argument::hasInAllocaAttr() const { |
| 107 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 108 | return hasAttribute(Attribute::InAlloca); |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 111 | bool Argument::hasByValOrInAllocaAttr() const { |
| 112 | if (!getType()->isPointerTy()) return false; |
| 113 | AttributeSet Attrs = getParent()->getAttributes(); |
| 114 | return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) || |
| 115 | Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca); |
| 116 | } |
| 117 | |
Chris Lattner | 4d37d99 | 2011-05-22 23:57:23 +0000 | [diff] [blame] | 118 | unsigned Argument::getParamAlignment() const { |
| 119 | assert(getType()->isPointerTy() && "Only pointers have alignments"); |
| 120 | return getParent()->getParamAlignment(getArgNo()+1); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 4d37d99 | 2011-05-22 23:57:23 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 124 | uint64_t Argument::getDereferenceableBytes() const { |
| 125 | assert(getType()->isPointerTy() && |
| 126 | "Only pointers have dereferenceable bytes"); |
| 127 | return getParent()->getDereferenceableBytes(getArgNo()+1); |
| 128 | } |
| 129 | |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 130 | uint64_t Argument::getDereferenceableOrNullBytes() const { |
| 131 | assert(getType()->isPointerTy() && |
| 132 | "Only pointers have dereferenceable bytes"); |
| 133 | return getParent()->getDereferenceableOrNullBytes(getArgNo()+1); |
| 134 | } |
| 135 | |
Duncan Sands | 5d96f3f | 2009-12-11 08:36:17 +0000 | [diff] [blame] | 136 | /// hasNestAttr - Return true if this argument has the nest attribute on |
| 137 | /// it in its containing function. |
| 138 | bool Argument::hasNestAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 139 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 140 | return hasAttribute(Attribute::Nest); |
Duncan Sands | 5d96f3f | 2009-12-11 08:36:17 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 143 | /// hasNoAliasAttr - Return true if this argument has the noalias attribute on |
| 144 | /// it in its containing function. |
| 145 | bool Argument::hasNoAliasAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 146 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 147 | return hasAttribute(Attribute::NoAlias); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Duncan Sands | df128eb | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 150 | /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute |
| 151 | /// on it in its containing function. |
| 152 | bool Argument::hasNoCaptureAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 153 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 154 | return hasAttribute(Attribute::NoCapture); |
Duncan Sands | df128eb | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 157 | /// hasSRetAttr - Return true if this argument has the sret attribute on |
| 158 | /// it in its containing function. |
| 159 | bool Argument::hasStructRetAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 160 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 161 | return hasAttribute(Attribute::StructRet); |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 164 | /// hasReturnedAttr - Return true if this argument has the returned attribute on |
| 165 | /// it in its containing function. |
| 166 | bool Argument::hasReturnedAttr() const { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 167 | return hasAttribute(Attribute::Returned); |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Juergen Ributzka | 384c3b5 | 2014-08-05 05:43:41 +0000 | [diff] [blame] | 170 | /// hasZExtAttr - Return true if this argument has the zext attribute on it in |
| 171 | /// its containing function. |
| 172 | bool Argument::hasZExtAttr() const { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 173 | return hasAttribute(Attribute::ZExt); |
Juergen Ributzka | 384c3b5 | 2014-08-05 05:43:41 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /// hasSExtAttr Return true if this argument has the sext attribute on it in its |
| 177 | /// containing function. |
| 178 | bool Argument::hasSExtAttr() const { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 179 | return hasAttribute(Attribute::SExt); |
Juergen Ributzka | 384c3b5 | 2014-08-05 05:43:41 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 182 | /// Return true if this argument has the readonly or readnone attribute on it |
| 183 | /// in its containing function. |
| 184 | bool Argument::onlyReadsMemory() const { |
| 185 | return getParent()->getAttributes(). |
| 186 | hasAttribute(getArgNo()+1, Attribute::ReadOnly) || |
| 187 | getParent()->getAttributes(). |
| 188 | hasAttribute(getArgNo()+1, Attribute::ReadNone); |
| 189 | } |
| 190 | |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 191 | /// addAttr - Add attributes to an argument. |
| 192 | void Argument::addAttr(AttributeSet AS) { |
Bill Wendling | efbbbfd | 2013-02-21 19:46:51 +0000 | [diff] [blame] | 193 | assert(AS.getNumSlots() <= 1 && |
Bill Wendling | 1c20ff0 | 2013-02-20 23:04:11 +0000 | [diff] [blame] | 194 | "Trying to add more than one attribute set to an argument!"); |
| 195 | AttrBuilder B(AS, AS.getSlotIndex(0)); |
| 196 | getParent()->addAttributes(getArgNo() + 1, |
| 197 | AttributeSet::get(Parent->getContext(), |
| 198 | getArgNo() + 1, B)); |
Gordon Henriksen | 2d9cc21 | 2008-04-28 17:37:06 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 200 | |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 201 | /// removeAttr - Remove attributes from an argument. |
| 202 | void Argument::removeAttr(AttributeSet AS) { |
Bill Wendling | efbbbfd | 2013-02-21 19:46:51 +0000 | [diff] [blame] | 203 | assert(AS.getNumSlots() <= 1 && |
Bill Wendling | 1c20ff0 | 2013-02-20 23:04:11 +0000 | [diff] [blame] | 204 | "Trying to remove more than one attribute set from an argument!"); |
| 205 | AttrBuilder B(AS, AS.getSlotIndex(0)); |
| 206 | getParent()->removeAttributes(getArgNo() + 1, |
| 207 | AttributeSet::get(Parent->getContext(), |
| 208 | getArgNo() + 1, B)); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 209 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 210 | |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 211 | /// hasAttribute - Checks if an argument has a given attribute. |
| 212 | bool Argument::hasAttribute(Attribute::AttrKind Kind) const { |
| 213 | return getParent()->hasAttribute(getArgNo() + 1, Kind); |
| 214 | } |
| 215 | |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 216 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 217 | // Helper Methods in Function |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 218 | //===----------------------------------------------------------------------===// |
| 219 | |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 220 | bool Function::isMaterializable() const { |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 221 | return getGlobalObjectSubClassData() & (1 << IsMaterializableBit); |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Duncan P. N. Exon Smith | 4472b77 | 2015-04-24 20:47:23 +0000 | [diff] [blame] | 224 | void Function::setIsMaterializable(bool V) { |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 225 | unsigned Mask = 1 << IsMaterializableBit; |
| 226 | setGlobalObjectSubClassData((~Mask & getGlobalObjectSubClassData()) | |
| 227 | (V ? Mask : 0u)); |
Duncan P. N. Exon Smith | 4472b77 | 2015-04-24 20:47:23 +0000 | [diff] [blame] | 228 | } |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 229 | |
Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 230 | LLVMContext &Function::getContext() const { |
| 231 | return getType()->getContext(); |
Owen Anderson | 0a2c458 | 2009-07-02 18:03:58 +0000 | [diff] [blame] | 232 | } |
| 233 | |
David Blaikie | ab105bb | 2016-01-15 23:07:58 +0000 | [diff] [blame] | 234 | FunctionType *Function::getFunctionType() const { |
| 235 | return cast<FunctionType>(getValueType()); |
| 236 | } |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 238 | bool Function::isVarArg() const { |
| 239 | return getFunctionType()->isVarArg(); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 242 | Type *Function::getReturnType() const { |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 243 | return getFunctionType()->getReturnType(); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 246 | void Function::removeFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 247 | getParent()->getFunctionList().remove(getIterator()); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 250 | void Function::eraseFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 251 | getParent()->getFunctionList().erase(getIterator()); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 254 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 255 | // Function Implementation |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 256 | //===----------------------------------------------------------------------===// |
| 257 | |
Rafael Espindola | 1b47a28 | 2014-10-23 15:20:05 +0000 | [diff] [blame] | 258 | Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name, |
| 259 | Module *ParentModule) |
David Blaikie | d583b19 | 2015-08-21 21:35:28 +0000 | [diff] [blame] | 260 | : GlobalObject(Ty, Value::FunctionVal, |
David Blaikie | ab105bb | 2016-01-15 23:07:58 +0000 | [diff] [blame] | 261 | OperandTraits<Function>::op_begin(this), 0, Linkage, name) { |
Chris Lattner | 654695b | 2009-01-05 07:58:59 +0000 | [diff] [blame] | 262 | assert(FunctionType::isValidReturnType(getReturnType()) && |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 263 | "invalid return type"); |
Duncan P. N. Exon Smith | 4472b77 | 2015-04-24 20:47:23 +0000 | [diff] [blame] | 264 | setGlobalObjectSubClassData(0); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 265 | SymTab = new ValueSymbolTable(); |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 266 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 267 | // If the function has arguments, mark them as lazily built. |
| 268 | if (Ty->getNumParams()) |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 269 | setValueSubclassData(1); // Set the "has lazy arguments" bit. |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 271 | if (ParentModule) |
| 272 | ParentModule->getFunctionList().push_back(this); |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 273 | |
| 274 | // Ensure intrinsics have the right parameter attributes. |
Pete Cooper | a7c0c18 | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 275 | // Note, the IntID field will have been set in Value::setName if this function |
| 276 | // name is a valid intrinsic ID. |
| 277 | if (IntID) |
| 278 | setAttributes(Intrinsic::getAttributes(getContext(), IntID)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 281 | Function::~Function() { |
| 282 | dropAllReferences(); // After this it is safe to delete instructions. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 284 | // Delete all of the method arguments and unlink from symbol table... |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 285 | ArgumentList.clear(); |
| 286 | delete SymTab; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 287 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 288 | // Remove the function from the on-the-side GC table. |
| 289 | clearGC(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 292 | void Function::BuildLazyArguments() const { |
| 293 | // Create the arguments vector, all arguments start out unnamed. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 294 | FunctionType *FT = getFunctionType(); |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 295 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 296 | assert(!FT->getParamType(i)->isVoidTy() && |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 297 | "Cannot have void typed arguments!"); |
| 298 | ArgumentList.push_back(new Argument(FT->getParamType(i))); |
| 299 | } |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 300 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 301 | // Clear the lazy arguments bit. |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 302 | unsigned SDC = getSubclassDataFromValue(); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 303 | const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0)); |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Duncan P. N. Exon Smith | bdfc984 | 2016-04-06 06:38:15 +0000 | [diff] [blame] | 306 | void Function::stealArgumentListFrom(Function &Src) { |
| 307 | assert(isDeclaration() && "Expected no references to current arguments"); |
| 308 | |
| 309 | // Drop the current arguments, if any, and set the lazy argument bit. |
| 310 | if (!hasLazyArguments()) { |
| 311 | assert(llvm::all_of(ArgumentList, |
| 312 | [](const Argument &A) { return A.use_empty(); }) && |
| 313 | "Expected arguments to be unused in declaration"); |
| 314 | ArgumentList.clear(); |
| 315 | setValueSubclassData(getSubclassDataFromValue() | (1 << 0)); |
| 316 | } |
| 317 | |
| 318 | // Nothing to steal if Src has lazy arguments. |
| 319 | if (Src.hasLazyArguments()) |
| 320 | return; |
| 321 | |
| 322 | // Steal arguments from Src, and fix the lazy argument bits. |
| 323 | ArgumentList.splice(ArgumentList.end(), Src.ArgumentList); |
| 324 | setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0)); |
| 325 | Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0)); |
| 326 | } |
| 327 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 328 | size_t Function::arg_size() const { |
| 329 | return getFunctionType()->getNumParams(); |
| 330 | } |
| 331 | bool Function::arg_empty() const { |
| 332 | return getFunctionType()->getNumParams() == 0; |
| 333 | } |
| 334 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 335 | void Function::setParent(Module *parent) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 336 | Parent = parent; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 339 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 340 | // go" of all references that they are maintaining. This allows one to |
| 341 | // 'delete' a whole class at a time, even though there may be circular |
| 342 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 343 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 344 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 345 | // delete. |
| 346 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 347 | void Function::dropAllReferences() { |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 348 | setIsMaterializable(false); |
| 349 | |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 350 | for (BasicBlock &BB : *this) |
| 351 | BB.dropAllReferences(); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 352 | |
Dan Gohman | f844b3b | 2010-12-07 19:56:51 +0000 | [diff] [blame] | 353 | // Delete all basic blocks. They are now unused, except possibly by |
| 354 | // blockaddresses, but BasicBlock's destructor takes care of those. |
| 355 | while (!BasicBlocks.empty()) |
| 356 | BasicBlocks.begin()->eraseFromParent(); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 357 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 358 | // Drop uses of any optional data (real or placeholder). |
| 359 | if (getNumOperands()) { |
| 360 | User::dropAllReferences(); |
| 361 | setNumHungOffUseOperands(0); |
| 362 | setValueSubclassData(getSubclassDataFromValue() & ~0xe); |
| 363 | } |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 364 | |
| 365 | // Metadata is stored in a side-table. |
| 366 | clearMetadata(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 368 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 369 | void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 370 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 371 | PAL = PAL.addAttribute(getContext(), i, Kind); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 372 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Amaury Sechet | 5db224e | 2016-06-12 06:17:24 +0000 | [diff] [blame] | 375 | void Function::addAttribute(unsigned i, Attribute Attr) { |
| 376 | AttributeSet PAL = getAttributes(); |
| 377 | PAL = PAL.addAttribute(getContext(), i, Attr); |
| 378 | setAttributes(PAL); |
| 379 | } |
| 380 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 381 | void Function::addAttributes(unsigned i, AttributeSet Attrs) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 382 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 383 | PAL = PAL.addAttributes(getContext(), i, Attrs); |
Bill Wendling | c0e2a1f | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 384 | setAttributes(PAL); |
| 385 | } |
| 386 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 387 | void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 388 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 389 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 390 | setAttributes(PAL); |
| 391 | } |
| 392 | |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 393 | void Function::removeAttribute(unsigned i, StringRef Kind) { |
| 394 | AttributeSet PAL = getAttributes(); |
| 395 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
| 396 | setAttributes(PAL); |
| 397 | } |
| 398 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 399 | void Function::removeAttributes(unsigned i, AttributeSet Attrs) { |
Bill Wendling | c0e2a1f | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 400 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 401 | PAL = PAL.removeAttributes(getContext(), i, Attrs); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 402 | setAttributes(PAL); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 405 | void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
| 406 | AttributeSet PAL = getAttributes(); |
| 407 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
| 408 | setAttributes(PAL); |
| 409 | } |
| 410 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 411 | void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
| 412 | AttributeSet PAL = getAttributes(); |
| 413 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
| 414 | setAttributes(PAL); |
| 415 | } |
| 416 | |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 417 | const std::string &Function::getGC() const { |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 418 | assert(hasGC() && "Function has no collector"); |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 419 | return getContext().getGC(*this); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Benjamin Kramer | 728f444 | 2016-05-29 10:46:35 +0000 | [diff] [blame] | 422 | void Function::setGC(std::string Str) { |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 423 | setValueSubclassDataBit(14, !Str.empty()); |
| 424 | getContext().setGC(*this, std::move(Str)); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 427 | void Function::clearGC() { |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 428 | if (!hasGC()) |
| 429 | return; |
| 430 | getContext().deleteGC(*this); |
| 431 | setValueSubclassDataBit(14, false); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Rafael Espindola | 769efe6 | 2015-12-02 20:03:17 +0000 | [diff] [blame] | 434 | /// Copy all additional attributes (those not needed to create a Function) from |
| 435 | /// the Function Src to this one. |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 436 | void Function::copyAttributesFrom(const GlobalValue *Src) { |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 437 | GlobalObject::copyAttributesFrom(Src); |
Rafael Espindola | 769efe6 | 2015-12-02 20:03:17 +0000 | [diff] [blame] | 438 | const Function *SrcF = dyn_cast<Function>(Src); |
| 439 | if (!SrcF) |
| 440 | return; |
| 441 | |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 442 | setCallingConv(SrcF->getCallingConv()); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 443 | setAttributes(SrcF->getAttributes()); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 444 | if (SrcF->hasGC()) |
| 445 | setGC(SrcF->getGC()); |
| 446 | else |
| 447 | clearGC(); |
Vedant Kumar | 44dd987 | 2015-12-19 08:48:43 +0000 | [diff] [blame] | 448 | if (SrcF->hasPersonalityFn()) |
| 449 | setPersonalityFn(SrcF->getPersonalityFn()); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 450 | if (SrcF->hasPrefixData()) |
| 451 | setPrefixData(SrcF->getPrefixData()); |
| 452 | if (SrcF->hasPrologueData()) |
| 453 | setPrologueData(SrcF->getPrologueData()); |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 456 | /// Table of string intrinsic names indexed by enum value. |
| 457 | static const char * const IntrinsicNameTable[] = { |
| 458 | "not_intrinsic", |
| 459 | #define GET_INTRINSIC_NAME_TABLE |
| 460 | #include "llvm/IR/Intrinsics.gen" |
| 461 | #undef GET_INTRINSIC_NAME_TABLE |
| 462 | }; |
| 463 | |
Pete Cooper | 2124113 | 2015-05-19 00:02:25 +0000 | [diff] [blame] | 464 | /// \brief This does the actual lookup of an intrinsic ID which |
| 465 | /// matches the given function name. |
| 466 | static Intrinsic::ID lookupIntrinsicID(const ValueName *ValName) { |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 467 | StringRef Name = ValName->getKey(); |
Pete Cooper | 2124113 | 2015-05-19 00:02:25 +0000 | [diff] [blame] | 468 | |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 469 | ArrayRef<const char *> NameTable(&IntrinsicNameTable[1], |
| 470 | std::end(IntrinsicNameTable)); |
Reid Kleckner | c2752da | 2016-01-26 22:33:19 +0000 | [diff] [blame] | 471 | int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name); |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 472 | Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + 1); |
| 473 | if (ID == Intrinsic::not_intrinsic) |
| 474 | return ID; |
Pete Cooper | 2124113 | 2015-05-19 00:02:25 +0000 | [diff] [blame] | 475 | |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 476 | // If the intrinsic is not overloaded, require an exact match. If it is |
| 477 | // overloaded, require a prefix match. |
| 478 | bool IsPrefixMatch = Name.size() > strlen(NameTable[Idx]); |
| 479 | return IsPrefixMatch == isOverloaded(ID) ? ID : Intrinsic::not_intrinsic; |
Pete Cooper | 2124113 | 2015-05-19 00:02:25 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Pete Cooper | a7c0c18 | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 482 | void Function::recalculateIntrinsicID() { |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 483 | const ValueName *ValName = this->getValueName(); |
Pete Cooper | a7c0c18 | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 484 | if (!ValName || !isIntrinsic()) { |
| 485 | IntID = Intrinsic::not_intrinsic; |
| 486 | return; |
Michael Ilseman | 516d703 | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 487 | } |
Pete Cooper | a7c0c18 | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 488 | IntID = lookupIntrinsicID(ValName); |
Michael Ilseman | 516d703 | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 491 | /// Returns a stable mangling for the type specified for use in the name |
Philip Reames | 059ecbf | 2014-11-24 23:24:24 +0000 | [diff] [blame] | 492 | /// mangling scheme used by 'any' types in intrinsic signatures. The mangling |
| 493 | /// of named types is simply their name. Manglings for unnamed types consist |
| 494 | /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) |
| 495 | /// combined with the mangling of their component types. A vararg function |
| 496 | /// type will have a suffix of 'vararg'. Since function types can contain |
| 497 | /// other function types, we close a function type mangling with suffix 'f' |
| 498 | /// which can't be confused with it's prefix. This ensures we don't have |
| 499 | /// collisions between two unrelated function types. Otherwise, you might |
| 500 | /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) |
Philip Reames | 8d5d68f | 2015-01-14 23:05:17 +0000 | [diff] [blame] | 501 | /// Manglings of integers, floats, and vectors ('i', 'f', and 'v' prefix in most |
| 502 | /// cases) fall back to the MVT codepath, where they could be mangled to |
| 503 | /// 'x86mmx', for example; matching on derived types is not sufficient to mangle |
| 504 | /// everything. |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 505 | static std::string getMangledTypeStr(Type* Ty) { |
| 506 | std::string Result; |
| 507 | if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) { |
| 508 | Result += "p" + llvm::utostr(PTyp->getAddressSpace()) + |
| 509 | getMangledTypeStr(PTyp->getElementType()); |
| 510 | } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) { |
| 511 | Result += "a" + llvm::utostr(ATyp->getNumElements()) + |
| 512 | getMangledTypeStr(ATyp->getElementType()); |
| 513 | } else if (StructType* STyp = dyn_cast<StructType>(Ty)) { |
David Blaikie | b340f0a | 2015-05-08 18:52:28 +0000 | [diff] [blame] | 514 | assert(!STyp->isLiteral() && "TODO: implement literal types"); |
| 515 | Result += STyp->getName(); |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 516 | } else if (FunctionType* FT = dyn_cast<FunctionType>(Ty)) { |
| 517 | Result += "f_" + getMangledTypeStr(FT->getReturnType()); |
| 518 | for (size_t i = 0; i < FT->getNumParams(); i++) |
| 519 | Result += getMangledTypeStr(FT->getParamType(i)); |
| 520 | if (FT->isVarArg()) |
| 521 | Result += "vararg"; |
Philip Reames | 059ecbf | 2014-11-24 23:24:24 +0000 | [diff] [blame] | 522 | // Ensure nested function types are distinguishable. |
| 523 | Result += "f"; |
Elena Demikhovsky | 1ca72e1 | 2015-11-19 07:17:16 +0000 | [diff] [blame] | 524 | } else if (isa<VectorType>(Ty)) |
| 525 | Result += "v" + utostr(Ty->getVectorNumElements()) + |
| 526 | getMangledTypeStr(Ty->getVectorElementType()); |
| 527 | else if (Ty) |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 528 | Result += EVT::getEVT(Ty).getEVTString(); |
| 529 | return Result; |
| 530 | } |
| 531 | |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 532 | std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 533 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 534 | std::string Result(IntrinsicNameTable[id]); |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 535 | for (Type *Ty : Tys) { |
| 536 | Result += "." + getMangledTypeStr(Ty); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 537 | } |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 538 | return Result; |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 541 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 542 | /// IIT_Info - These are enumerators that describe the entries returned by the |
| 543 | /// getIntrinsicInfoTableEntries function. |
| 544 | /// |
| 545 | /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter! |
| 546 | enum IIT_Info { |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 547 | // Common values should be encoded with 0-15. |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 548 | IIT_Done = 0, |
| 549 | IIT_I1 = 1, |
| 550 | IIT_I8 = 2, |
| 551 | IIT_I16 = 3, |
| 552 | IIT_I32 = 4, |
| 553 | IIT_I64 = 5, |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 554 | IIT_F16 = 6, |
| 555 | IIT_F32 = 7, |
| 556 | IIT_F64 = 8, |
| 557 | IIT_V2 = 9, |
| 558 | IIT_V4 = 10, |
| 559 | IIT_V8 = 11, |
| 560 | IIT_V16 = 12, |
| 561 | IIT_V32 = 13, |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 562 | IIT_PTR = 14, |
| 563 | IIT_ARG = 15, |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 564 | |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 565 | // Values from 16+ are only encodable with the inefficient encoding. |
| 566 | IIT_V64 = 16, |
Robert Khasanov | b25e562 | 2014-09-30 11:32:22 +0000 | [diff] [blame] | 567 | IIT_MMX = 17, |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 568 | IIT_TOKEN = 18, |
| 569 | IIT_METADATA = 19, |
| 570 | IIT_EMPTYSTRUCT = 20, |
| 571 | IIT_STRUCT2 = 21, |
| 572 | IIT_STRUCT3 = 22, |
| 573 | IIT_STRUCT4 = 23, |
| 574 | IIT_STRUCT5 = 24, |
| 575 | IIT_EXTEND_ARG = 25, |
| 576 | IIT_TRUNC_ARG = 26, |
| 577 | IIT_ANYPTR = 27, |
| 578 | IIT_V1 = 28, |
| 579 | IIT_VARARG = 29, |
| 580 | IIT_HALF_VEC_ARG = 30, |
| 581 | IIT_SAME_VEC_WIDTH_ARG = 31, |
| 582 | IIT_PTR_TO_ARG = 32, |
| 583 | IIT_VEC_OF_PTRS_TO_ELT = 33, |
Krzysztof Parzyszek | b8bb90b | 2015-11-24 16:28:14 +0000 | [diff] [blame] | 584 | IIT_I128 = 34, |
| 585 | IIT_V512 = 35, |
| 586 | IIT_V1024 = 36 |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 587 | }; |
| 588 | |
| 589 | |
| 590 | static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, |
| 591 | SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { |
Chris Lattner | a57c797 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 592 | IIT_Info Info = IIT_Info(Infos[NextElt++]); |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 593 | unsigned StructElts = 2; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 594 | using namespace Intrinsic; |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 595 | |
Chris Lattner | a57c797 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 596 | switch (Info) { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 597 | case IIT_Done: |
| 598 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); |
| 599 | return; |
Andrew Trick | a2efd99 | 2013-10-31 17:18:11 +0000 | [diff] [blame] | 600 | case IIT_VARARG: |
| 601 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0)); |
| 602 | return; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 603 | case IIT_MMX: |
| 604 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); |
| 605 | return; |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 606 | case IIT_TOKEN: |
| 607 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0)); |
| 608 | return; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 609 | case IIT_METADATA: |
| 610 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); |
| 611 | return; |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 612 | case IIT_F16: |
| 613 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); |
| 614 | return; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 615 | case IIT_F32: |
| 616 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); |
| 617 | return; |
| 618 | case IIT_F64: |
| 619 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); |
| 620 | return; |
| 621 | case IIT_I1: |
| 622 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); |
| 623 | return; |
| 624 | case IIT_I8: |
| 625 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); |
| 626 | return; |
| 627 | case IIT_I16: |
| 628 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16)); |
| 629 | return; |
| 630 | case IIT_I32: |
| 631 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); |
| 632 | return; |
| 633 | case IIT_I64: |
| 634 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); |
| 635 | return; |
Kit Barton | 6646033 | 2015-05-25 15:49:26 +0000 | [diff] [blame] | 636 | case IIT_I128: |
| 637 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128)); |
| 638 | return; |
Jiangning Liu | 63dc840 | 2013-09-24 02:47:27 +0000 | [diff] [blame] | 639 | case IIT_V1: |
| 640 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1)); |
| 641 | DecodeIITType(NextElt, Infos, OutputTable); |
| 642 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 643 | case IIT_V2: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 644 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2)); |
| 645 | DecodeIITType(NextElt, Infos, OutputTable); |
| 646 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 647 | case IIT_V4: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 648 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4)); |
| 649 | DecodeIITType(NextElt, Infos, OutputTable); |
| 650 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 651 | case IIT_V8: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 652 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8)); |
| 653 | DecodeIITType(NextElt, Infos, OutputTable); |
| 654 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 655 | case IIT_V16: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 656 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16)); |
| 657 | DecodeIITType(NextElt, Infos, OutputTable); |
| 658 | return; |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 659 | case IIT_V32: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 660 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32)); |
| 661 | DecodeIITType(NextElt, Infos, OutputTable); |
| 662 | return; |
Robert Khasanov | b25e562 | 2014-09-30 11:32:22 +0000 | [diff] [blame] | 663 | case IIT_V64: |
| 664 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64)); |
| 665 | DecodeIITType(NextElt, Infos, OutputTable); |
| 666 | return; |
Krzysztof Parzyszek | b8bb90b | 2015-11-24 16:28:14 +0000 | [diff] [blame] | 667 | case IIT_V512: |
| 668 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512)); |
| 669 | DecodeIITType(NextElt, Infos, OutputTable); |
| 670 | return; |
| 671 | case IIT_V1024: |
| 672 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024)); |
| 673 | DecodeIITType(NextElt, Infos, OutputTable); |
| 674 | return; |
Chris Lattner | 4f18aa8 | 2012-05-23 05:19:18 +0000 | [diff] [blame] | 675 | case IIT_PTR: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 676 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); |
| 677 | DecodeIITType(NextElt, Infos, OutputTable); |
| 678 | return; |
Chris Lattner | 4f18aa8 | 2012-05-23 05:19:18 +0000 | [diff] [blame] | 679 | case IIT_ANYPTR: { // [ANYPTR addrspace, subtype] |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 680 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 681 | Infos[NextElt++])); |
| 682 | DecodeIITType(NextElt, Infos, OutputTable); |
| 683 | return; |
Pete Cooper | 243efd7 | 2012-05-21 23:21:28 +0000 | [diff] [blame] | 684 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 685 | case IIT_ARG: { |
| 686 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 687 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); |
| 688 | return; |
| 689 | } |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 690 | case IIT_EXTEND_ARG: { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 691 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 692 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 693 | ArgInfo)); |
| 694 | return; |
| 695 | } |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 696 | case IIT_TRUNC_ARG: { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 697 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 698 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 699 | ArgInfo)); |
| 700 | return; |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 701 | } |
Tim Northover | 4516de3 | 2014-03-29 07:04:54 +0000 | [diff] [blame] | 702 | case IIT_HALF_VEC_ARG: { |
| 703 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 704 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument, |
| 705 | ArgInfo)); |
| 706 | return; |
| 707 | } |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 708 | case IIT_SAME_VEC_WIDTH_ARG: { |
| 709 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 710 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument, |
| 711 | ArgInfo)); |
| 712 | return; |
| 713 | } |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 714 | case IIT_PTR_TO_ARG: { |
| 715 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 716 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument, |
| 717 | ArgInfo)); |
| 718 | return; |
| 719 | } |
Elena Demikhovsky | 23a485a | 2015-02-08 08:27:19 +0000 | [diff] [blame] | 720 | case IIT_VEC_OF_PTRS_TO_ELT: { |
| 721 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 722 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfPtrsToElt, |
| 723 | ArgInfo)); |
| 724 | return; |
| 725 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 726 | case IIT_EMPTYSTRUCT: |
| 727 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); |
| 728 | return; |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 729 | case IIT_STRUCT5: ++StructElts; // FALL THROUGH. |
| 730 | case IIT_STRUCT4: ++StructElts; // FALL THROUGH. |
| 731 | case IIT_STRUCT3: ++StructElts; // FALL THROUGH. |
| 732 | case IIT_STRUCT2: { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 733 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts)); |
| 734 | |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 735 | for (unsigned i = 0; i != StructElts; ++i) |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 736 | DecodeIITType(NextElt, Infos, OutputTable); |
| 737 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 739 | } |
| 740 | llvm_unreachable("unhandled"); |
| 741 | } |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 742 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 743 | |
| 744 | #define GET_INTRINSIC_GENERATOR_GLOBAL |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 745 | #include "llvm/IR/Intrinsics.gen" |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 746 | #undef GET_INTRINSIC_GENERATOR_GLOBAL |
| 747 | |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 748 | void Intrinsic::getIntrinsicInfoTableEntries(ID id, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 749 | SmallVectorImpl<IITDescriptor> &T){ |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 750 | // Check to see if the intrinsic's type was expressible by the table. |
| 751 | unsigned TableVal = IIT_Table[id-1]; |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 752 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 753 | // Decode the TableVal into an array of IITValues. |
| 754 | SmallVector<unsigned char, 8> IITValues; |
| 755 | ArrayRef<unsigned char> IITEntries; |
| 756 | unsigned NextElt = 0; |
| 757 | if ((TableVal >> 31) != 0) { |
| 758 | // This is an offset into the IIT_LongEncodingTable. |
| 759 | IITEntries = IIT_LongEncodingTable; |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 760 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 761 | // Strip sentinel bit. |
| 762 | NextElt = (TableVal << 1) >> 1; |
| 763 | } else { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 764 | // Decode the TableVal into an array of IITValues. If the entry was encoded |
| 765 | // into a single word in the table itself, decode it now. |
Chris Lattner | a57c797 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 766 | do { |
| 767 | IITValues.push_back(TableVal & 0xF); |
| 768 | TableVal >>= 4; |
| 769 | } while (TableVal); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 770 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 771 | IITEntries = IITValues; |
| 772 | NextElt = 0; |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 773 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 774 | |
| 775 | // Okay, decode the table into the output vector of IITDescriptors. |
| 776 | DecodeIITType(NextElt, IITEntries, T); |
| 777 | while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) |
| 778 | DecodeIITType(NextElt, IITEntries, T); |
| 779 | } |
| 780 | |
| 781 | |
| 782 | static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, |
| 783 | ArrayRef<Type*> Tys, LLVMContext &Context) { |
| 784 | using namespace Intrinsic; |
| 785 | IITDescriptor D = Infos.front(); |
| 786 | Infos = Infos.slice(1); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 787 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 788 | switch (D.Kind) { |
| 789 | case IITDescriptor::Void: return Type::getVoidTy(Context); |
Andrew Trick | a2efd99 | 2013-10-31 17:18:11 +0000 | [diff] [blame] | 790 | case IITDescriptor::VarArg: return Type::getVoidTy(Context); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 791 | case IITDescriptor::MMX: return Type::getX86_MMXTy(Context); |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 792 | case IITDescriptor::Token: return Type::getTokenTy(Context); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 793 | case IITDescriptor::Metadata: return Type::getMetadataTy(Context); |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 794 | case IITDescriptor::Half: return Type::getHalfTy(Context); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 795 | case IITDescriptor::Float: return Type::getFloatTy(Context); |
| 796 | case IITDescriptor::Double: return Type::getDoubleTy(Context); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 797 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 798 | case IITDescriptor::Integer: |
| 799 | return IntegerType::get(Context, D.Integer_Width); |
| 800 | case IITDescriptor::Vector: |
| 801 | return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width); |
| 802 | case IITDescriptor::Pointer: |
| 803 | return PointerType::get(DecodeFixedType(Infos, Tys, Context), |
| 804 | D.Pointer_AddressSpace); |
| 805 | case IITDescriptor::Struct: { |
| 806 | Type *Elts[5]; |
| 807 | assert(D.Struct_NumElements <= 5 && "Can't handle this yet"); |
| 808 | for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) |
| 809 | Elts[i] = DecodeFixedType(Infos, Tys, Context); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 810 | return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements)); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | case IITDescriptor::Argument: |
| 814 | return Tys[D.getArgumentNumber()]; |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 815 | case IITDescriptor::ExtendArgument: { |
| 816 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 817 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 818 | return VectorType::getExtendedElementVectorType(VTy); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 819 | |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 820 | return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth()); |
| 821 | } |
| 822 | case IITDescriptor::TruncArgument: { |
| 823 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 824 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 825 | return VectorType::getTruncatedElementVectorType(VTy); |
| 826 | |
| 827 | IntegerType *ITy = cast<IntegerType>(Ty); |
| 828 | assert(ITy->getBitWidth() % 2 == 0); |
| 829 | return IntegerType::get(Context, ITy->getBitWidth() / 2); |
| 830 | } |
Tim Northover | 4516de3 | 2014-03-29 07:04:54 +0000 | [diff] [blame] | 831 | case IITDescriptor::HalfVecArgument: |
| 832 | return VectorType::getHalfElementsVectorType(cast<VectorType>( |
| 833 | Tys[D.getArgumentNumber()])); |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 834 | case IITDescriptor::SameVecWidthArgument: { |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 835 | Type *EltTy = DecodeFixedType(Infos, Tys, Context); |
| 836 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 837 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 838 | return VectorType::get(EltTy, VTy->getNumElements()); |
| 839 | } |
| 840 | llvm_unreachable("unhandled"); |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 841 | } |
| 842 | case IITDescriptor::PtrToArgument: { |
| 843 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 844 | return PointerType::getUnqual(Ty); |
| 845 | } |
Elena Demikhovsky | 23a485a | 2015-02-08 08:27:19 +0000 | [diff] [blame] | 846 | case IITDescriptor::VecOfPtrsToElt: { |
| 847 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 848 | VectorType *VTy = dyn_cast<VectorType>(Ty); |
| 849 | if (!VTy) |
| 850 | llvm_unreachable("Expected an argument of Vector Type"); |
| 851 | Type *EltTy = VTy->getVectorElementType(); |
| 852 | return VectorType::get(PointerType::getUnqual(EltTy), |
| 853 | VTy->getNumElements()); |
| 854 | } |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 855 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 856 | llvm_unreachable("unhandled"); |
| 857 | } |
| 858 | |
| 859 | |
| 860 | |
| 861 | FunctionType *Intrinsic::getType(LLVMContext &Context, |
| 862 | ID id, ArrayRef<Type*> Tys) { |
| 863 | SmallVector<IITDescriptor, 8> Table; |
| 864 | getIntrinsicInfoTableEntries(id, Table); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 865 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 866 | ArrayRef<IITDescriptor> TableRef = Table; |
| 867 | Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 868 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 869 | SmallVector<Type*, 8> ArgTys; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 870 | while (!TableRef.empty()) |
| 871 | ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 872 | |
Steven Wu | d05affc | 2014-10-20 15:47:24 +0000 | [diff] [blame] | 873 | // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg |
| 874 | // If we see void type as the type of the last argument, it is vararg intrinsic |
| 875 | if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) { |
| 876 | ArgTys.pop_back(); |
| 877 | return FunctionType::get(ResultTy, ArgTys, true); |
| 878 | } |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 879 | return FunctionType::get(ResultTy, ArgTys, false); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 882 | bool Intrinsic::isOverloaded(ID id) { |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 883 | #define GET_INTRINSIC_OVERLOAD_TABLE |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 884 | #include "llvm/IR/Intrinsics.gen" |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 885 | #undef GET_INTRINSIC_OVERLOAD_TABLE |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Sanjoy Das | c65d43e | 2015-06-18 19:28:26 +0000 | [diff] [blame] | 888 | bool Intrinsic::isLeaf(ID id) { |
| 889 | switch (id) { |
| 890 | default: |
| 891 | return true; |
| 892 | |
| 893 | case Intrinsic::experimental_gc_statepoint: |
| 894 | case Intrinsic::experimental_patchpoint_void: |
| 895 | case Intrinsic::experimental_patchpoint_i64: |
| 896 | return false; |
| 897 | } |
| 898 | } |
| 899 | |
Chris Lattner | 49b7ee1 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 900 | /// This defines the "Intrinsic::getAttributes(ID id)" method. |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 901 | #define GET_INTRINSIC_ATTRIBUTES |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 902 | #include "llvm/IR/Intrinsics.gen" |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 903 | #undef GET_INTRINSIC_ATTRIBUTES |
| 904 | |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 905 | Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 906 | // There can never be multiple globals with the same name of different types, |
| 907 | // because intrinsics must be a specific type. |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 908 | return |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 909 | cast<Function>(M->getOrInsertFunction(getName(id, Tys), |
| 910 | getType(M->getContext(), id, Tys))); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 913 | // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. |
| 914 | #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 915 | #include "llvm/IR/Intrinsics.gen" |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 916 | #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
| 917 | |
Saleem Abdulrasool | 4e63fc4 | 2014-07-04 18:42:25 +0000 | [diff] [blame] | 918 | // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method. |
| 919 | #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN |
| 920 | #include "llvm/IR/Intrinsics.gen" |
| 921 | #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN |
| 922 | |
Artur Pilipenko | bc55227 | 2016-06-22 14:56:33 +0000 | [diff] [blame] | 923 | bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos, |
| 924 | SmallVectorImpl<Type*> &ArgTys) { |
| 925 | using namespace Intrinsic; |
| 926 | |
| 927 | // If we ran out of descriptors, there are too many arguments. |
| 928 | if (Infos.empty()) return true; |
| 929 | IITDescriptor D = Infos.front(); |
| 930 | Infos = Infos.slice(1); |
| 931 | |
| 932 | switch (D.Kind) { |
| 933 | case IITDescriptor::Void: return !Ty->isVoidTy(); |
| 934 | case IITDescriptor::VarArg: return true; |
| 935 | case IITDescriptor::MMX: return !Ty->isX86_MMXTy(); |
| 936 | case IITDescriptor::Token: return !Ty->isTokenTy(); |
| 937 | case IITDescriptor::Metadata: return !Ty->isMetadataTy(); |
| 938 | case IITDescriptor::Half: return !Ty->isHalfTy(); |
| 939 | case IITDescriptor::Float: return !Ty->isFloatTy(); |
| 940 | case IITDescriptor::Double: return !Ty->isDoubleTy(); |
| 941 | case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width); |
| 942 | case IITDescriptor::Vector: { |
| 943 | VectorType *VT = dyn_cast<VectorType>(Ty); |
| 944 | return !VT || VT->getNumElements() != D.Vector_Width || |
| 945 | matchIntrinsicType(VT->getElementType(), Infos, ArgTys); |
| 946 | } |
| 947 | case IITDescriptor::Pointer: { |
| 948 | PointerType *PT = dyn_cast<PointerType>(Ty); |
| 949 | return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace || |
| 950 | matchIntrinsicType(PT->getElementType(), Infos, ArgTys); |
| 951 | } |
| 952 | |
| 953 | case IITDescriptor::Struct: { |
| 954 | StructType *ST = dyn_cast<StructType>(Ty); |
| 955 | if (!ST || ST->getNumElements() != D.Struct_NumElements) |
| 956 | return true; |
| 957 | |
| 958 | for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) |
| 959 | if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys)) |
| 960 | return true; |
| 961 | return false; |
| 962 | } |
| 963 | |
| 964 | case IITDescriptor::Argument: |
| 965 | // Two cases here - If this is the second occurrence of an argument, verify |
| 966 | // that the later instance matches the previous instance. |
| 967 | if (D.getArgumentNumber() < ArgTys.size()) |
| 968 | return Ty != ArgTys[D.getArgumentNumber()]; |
| 969 | |
| 970 | // Otherwise, if this is the first instance of an argument, record it and |
| 971 | // verify the "Any" kind. |
| 972 | assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error"); |
| 973 | ArgTys.push_back(Ty); |
| 974 | |
| 975 | switch (D.getArgumentKind()) { |
| 976 | case IITDescriptor::AK_Any: return false; // Success |
| 977 | case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy(); |
| 978 | case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy(); |
| 979 | case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty); |
| 980 | case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty); |
| 981 | } |
| 982 | llvm_unreachable("all argument kinds not covered"); |
| 983 | |
| 984 | case IITDescriptor::ExtendArgument: { |
| 985 | // This may only be used when referring to a previous vector argument. |
| 986 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 987 | return true; |
| 988 | |
| 989 | Type *NewTy = ArgTys[D.getArgumentNumber()]; |
| 990 | if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) |
| 991 | NewTy = VectorType::getExtendedElementVectorType(VTy); |
| 992 | else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) |
| 993 | NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth()); |
| 994 | else |
| 995 | return true; |
| 996 | |
| 997 | return Ty != NewTy; |
| 998 | } |
| 999 | case IITDescriptor::TruncArgument: { |
| 1000 | // This may only be used when referring to a previous vector argument. |
| 1001 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1002 | return true; |
| 1003 | |
| 1004 | Type *NewTy = ArgTys[D.getArgumentNumber()]; |
| 1005 | if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) |
| 1006 | NewTy = VectorType::getTruncatedElementVectorType(VTy); |
| 1007 | else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) |
| 1008 | NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2); |
| 1009 | else |
| 1010 | return true; |
| 1011 | |
| 1012 | return Ty != NewTy; |
| 1013 | } |
| 1014 | case IITDescriptor::HalfVecArgument: |
| 1015 | // This may only be used when referring to a previous vector argument. |
| 1016 | return D.getArgumentNumber() >= ArgTys.size() || |
| 1017 | !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || |
| 1018 | VectorType::getHalfElementsVectorType( |
| 1019 | cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; |
| 1020 | case IITDescriptor::SameVecWidthArgument: { |
| 1021 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1022 | return true; |
| 1023 | VectorType * ReferenceType = |
| 1024 | dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); |
| 1025 | VectorType *ThisArgType = dyn_cast<VectorType>(Ty); |
| 1026 | if (!ThisArgType || !ReferenceType || |
| 1027 | (ReferenceType->getVectorNumElements() != |
| 1028 | ThisArgType->getVectorNumElements())) |
| 1029 | return true; |
| 1030 | return matchIntrinsicType(ThisArgType->getVectorElementType(), |
| 1031 | Infos, ArgTys); |
| 1032 | } |
| 1033 | case IITDescriptor::PtrToArgument: { |
| 1034 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1035 | return true; |
| 1036 | Type * ReferenceType = ArgTys[D.getArgumentNumber()]; |
| 1037 | PointerType *ThisArgType = dyn_cast<PointerType>(Ty); |
| 1038 | return (!ThisArgType || ThisArgType->getElementType() != ReferenceType); |
| 1039 | } |
| 1040 | case IITDescriptor::VecOfPtrsToElt: { |
| 1041 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1042 | return true; |
| 1043 | VectorType * ReferenceType = |
| 1044 | dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]); |
| 1045 | VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty); |
| 1046 | if (!ThisArgVecTy || !ReferenceType || |
| 1047 | (ReferenceType->getVectorNumElements() != |
| 1048 | ThisArgVecTy->getVectorNumElements())) |
| 1049 | return true; |
| 1050 | PointerType *ThisArgEltTy = |
| 1051 | dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType()); |
| 1052 | if (!ThisArgEltTy) |
| 1053 | return true; |
| 1054 | return ThisArgEltTy->getElementType() != |
| 1055 | ReferenceType->getVectorElementType(); |
| 1056 | } |
| 1057 | } |
| 1058 | llvm_unreachable("unhandled"); |
| 1059 | } |
| 1060 | |
Artur Pilipenko | b68b821 | 2016-06-24 14:47:27 +0000 | [diff] [blame] | 1061 | bool |
| 1062 | Intrinsic::matchIntrinsicVarArg(bool isVarArg, |
| 1063 | ArrayRef<Intrinsic::IITDescriptor> &Infos) { |
| 1064 | // If there are no descriptors left, then it can't be a vararg. |
| 1065 | if (Infos.empty()) |
| 1066 | return isVarArg; |
| 1067 | |
| 1068 | // There should be only one descriptor remaining at this point. |
| 1069 | if (Infos.size() != 1) |
| 1070 | return true; |
| 1071 | |
| 1072 | // Check and verify the descriptor. |
| 1073 | IITDescriptor D = Infos.front(); |
| 1074 | Infos = Infos.slice(1); |
| 1075 | if (D.Kind == IITDescriptor::VarArg) |
| 1076 | return !isVarArg; |
| 1077 | |
| 1078 | return true; |
| 1079 | } |
| 1080 | |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 1081 | Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) { |
| 1082 | Intrinsic::ID ID = F->getIntrinsicID(); |
| 1083 | if (!ID) |
| 1084 | return None; |
| 1085 | |
| 1086 | FunctionType *FTy = F->getFunctionType(); |
| 1087 | // Accumulate an array of overloaded types for the given intrinsic |
| 1088 | SmallVector<Type *, 4> ArgTys; |
| 1089 | { |
| 1090 | SmallVector<Intrinsic::IITDescriptor, 8> Table; |
| 1091 | getIntrinsicInfoTableEntries(ID, Table); |
| 1092 | ArrayRef<Intrinsic::IITDescriptor> TableRef = Table; |
| 1093 | |
| 1094 | // If we encounter any problems matching the signature with the descriptor |
| 1095 | // just give up remangling. It's up to verifier to report the discrepancy. |
| 1096 | if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys)) |
| 1097 | return None; |
| 1098 | for (auto Ty : FTy->params()) |
| 1099 | if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys)) |
| 1100 | return None; |
| 1101 | if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef)) |
| 1102 | return None; |
| 1103 | } |
| 1104 | |
| 1105 | StringRef Name = F->getName(); |
| 1106 | if (Name == Intrinsic::getName(ID, ArgTys)) |
| 1107 | return None; |
| 1108 | |
| 1109 | auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys); |
| 1110 | NewDecl->setCallingConv(F->getCallingConv()); |
| 1111 | assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature"); |
| 1112 | return NewDecl; |
| 1113 | } |
| 1114 | |
Gabor Greif | 161cb04 | 2010-03-23 14:40:20 +0000 | [diff] [blame] | 1115 | /// hasAddressTaken - returns true if there are any uses of this function |
| 1116 | /// other than direct calls or invokes to it. |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1117 | bool Function::hasAddressTaken(const User* *PutOffender) const { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1118 | for (const Use &U : uses()) { |
| 1119 | const User *FU = U.getUser(); |
| 1120 | if (isa<BlockAddress>(FU)) |
Jay Foad | ca0c499 | 2012-05-12 08:30:16 +0000 | [diff] [blame] | 1121 | continue; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1122 | if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU)) { |
| 1123 | if (PutOffender) |
| 1124 | *PutOffender = FU; |
| 1125 | return true; |
| 1126 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1127 | ImmutableCallSite CS(cast<Instruction>(FU)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1128 | if (!CS.isCallee(&U)) { |
| 1129 | if (PutOffender) |
| 1130 | *PutOffender = FU; |
| 1131 | return true; |
| 1132 | } |
Jay Foad | 557169d | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 1133 | } |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
Eli Friedman | 1923a33 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 1137 | bool Function::isDefTriviallyDead() const { |
| 1138 | // Check the linkage |
| 1139 | if (!hasLinkOnceLinkage() && !hasLocalLinkage() && |
| 1140 | !hasAvailableExternallyLinkage()) |
| 1141 | return false; |
| 1142 | |
| 1143 | // Check if the function is used by anything other than a blockaddress. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1144 | for (const User *U : users()) |
| 1145 | if (!isa<BlockAddress>(U)) |
Eli Friedman | 1923a33 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 1146 | return false; |
| 1147 | |
| 1148 | return true; |
| 1149 | } |
| 1150 | |
Bill Wendling | 63a4ea1 | 2011-10-17 18:43:40 +0000 | [diff] [blame] | 1151 | /// callsFunctionThatReturnsTwice - Return true if the function has a call to |
| 1152 | /// setjmp or other function that gcc recognizes as "returning twice". |
| 1153 | bool Function::callsFunctionThatReturnsTwice() const { |
| 1154 | for (const_inst_iterator |
| 1155 | I = inst_begin(this), E = inst_end(this); I != E; ++I) { |
Mark Seaborn | 8271118 | 2014-01-14 04:20:01 +0000 | [diff] [blame] | 1156 | ImmutableCallSite CS(&*I); |
| 1157 | if (CS && CS.hasFnAttr(Attribute::ReturnsTwice)) |
Bill Wendling | 63a4ea1 | 2011-10-17 18:43:40 +0000 | [diff] [blame] | 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | return false; |
| 1162 | } |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1163 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1164 | Constant *Function::getPersonalityFn() const { |
| 1165 | assert(hasPersonalityFn() && getNumOperands()); |
| 1166 | return cast<Constant>(Op<0>()); |
Vedant Kumar | 1ab5ea5 | 2015-10-06 20:31:57 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1169 | void Function::setPersonalityFn(Constant *Fn) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1170 | setHungoffOperand<0>(Fn); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1171 | setValueSubclassDataBit(3, Fn != nullptr); |
Vedant Kumar | 1ab5ea5 | 2015-10-06 20:31:57 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1174 | Constant *Function::getPrefixData() const { |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1175 | assert(hasPrefixData() && getNumOperands()); |
| 1176 | return cast<Constant>(Op<1>()); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | void Function::setPrefixData(Constant *PrefixData) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1180 | setHungoffOperand<1>(PrefixData); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1181 | setValueSubclassDataBit(1, PrefixData != nullptr); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1182 | } |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1183 | |
| 1184 | Constant *Function::getPrologueData() const { |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1185 | assert(hasPrologueData() && getNumOperands()); |
| 1186 | return cast<Constant>(Op<2>()); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | void Function::setPrologueData(Constant *PrologueData) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1190 | setHungoffOperand<2>(PrologueData); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1191 | setValueSubclassDataBit(2, PrologueData != nullptr); |
| 1192 | } |
| 1193 | |
| 1194 | void Function::allocHungoffUselist() { |
| 1195 | // If we've already allocated a uselist, stop here. |
| 1196 | if (getNumOperands()) |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1197 | return; |
| 1198 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1199 | allocHungoffUses(3, /*IsPhi=*/ false); |
| 1200 | setNumHungOffUseOperands(3); |
| 1201 | |
| 1202 | // Initialize the uselist with placeholder operands to allow traversal. |
| 1203 | auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)); |
| 1204 | Op<0>().set(CPN); |
| 1205 | Op<1>().set(CPN); |
| 1206 | Op<2>().set(CPN); |
| 1207 | } |
| 1208 | |
| 1209 | template <int Idx> |
| 1210 | void Function::setHungoffOperand(Constant *C) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1211 | if (C) { |
| 1212 | allocHungoffUselist(); |
| 1213 | Op<Idx>().set(C); |
| 1214 | } else if (getNumOperands()) { |
| 1215 | Op<Idx>().set( |
| 1216 | ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0))); |
| 1217 | } |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | void Function::setValueSubclassDataBit(unsigned Bit, bool On) { |
| 1221 | assert(Bit < 16 && "SubclassData contains only 16 bits"); |
| 1222 | if (On) |
| 1223 | setValueSubclassData(getSubclassDataFromValue() | (1 << Bit)); |
| 1224 | else |
| 1225 | setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit)); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1226 | } |
Akira Hatanaka | 3058d0f | 2015-05-06 23:54:14 +0000 | [diff] [blame] | 1227 | |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 1228 | void Function::setEntryCount(uint64_t Count) { |
| 1229 | MDBuilder MDB(getContext()); |
| 1230 | setMetadata(LLVMContext::MD_prof, MDB.createFunctionEntryCount(Count)); |
| 1231 | } |
| 1232 | |
| 1233 | Optional<uint64_t> Function::getEntryCount() const { |
| 1234 | MDNode *MD = getMetadata(LLVMContext::MD_prof); |
| 1235 | if (MD && MD->getOperand(0)) |
| 1236 | if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) |
| 1237 | if (MDS->getString().equals("function_entry_count")) { |
| 1238 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); |
| 1239 | return CI->getValue().getZExtValue(); |
| 1240 | } |
| 1241 | return None; |
| 1242 | } |