Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 1 | //===-- Attributes.cpp - Implement AttributesList -------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 10 | // \file |
| 11 | // \brief This file implements the Attribute, AttributeImpl, AttrBuilder, |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 12 | // AttributeSetImpl, and AttributeSet classes. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Attributes.h" |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 18 | #include "AttributeImpl.h" |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 19 | #include "LLVMContextImpl.h" |
Benjamin Kramer | 502b9e1 | 2014-04-12 16:15:53 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Type.h" |
Benjamin Kramer | 17388a6 | 2014-03-03 18:02:34 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Atomic.h" |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Mutex.h" |
Benjamin Kramer | 1a25d73 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Bill Wendling | 7707c5a | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 32 | // Attribute Construction Methods |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
Chris Lattner | d0e1f10 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 34 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 35 | // allocsize has two integer arguments, but because they're both 32 bits, we can |
| 36 | // pack them into one 64-bit value, at the cost of making said value |
| 37 | // nonsensical. |
| 38 | // |
| 39 | // In order to do this, we need to reserve one value of the second (optional) |
| 40 | // allocsize argument to signify "not present." |
George Burgess IV | 4540ca3 | 2016-04-12 01:44:13 +0000 | [diff] [blame] | 41 | LLVM_CONSTEXPR static unsigned AllocSizeNumElemsNotPresent = -1; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 42 | |
| 43 | static uint64_t packAllocSizeArgs(unsigned ElemSizeArg, |
| 44 | const Optional<unsigned> &NumElemsArg) { |
| 45 | assert((!NumElemsArg.hasValue() || |
| 46 | *NumElemsArg != AllocSizeNumElemsNotPresent) && |
| 47 | "Attempting to pack a reserved value"); |
| 48 | |
| 49 | return uint64_t(ElemSizeArg) << 32 | |
| 50 | NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent); |
| 51 | } |
| 52 | |
| 53 | static std::pair<unsigned, Optional<unsigned>> |
| 54 | unpackAllocSizeArgs(uint64_t Num) { |
| 55 | unsigned NumElems = Num & std::numeric_limits<unsigned>::max(); |
| 56 | unsigned ElemSizeArg = Num >> 32; |
| 57 | |
| 58 | Optional<unsigned> NumElemsArg; |
| 59 | if (NumElems != AllocSizeNumElemsNotPresent) |
| 60 | NumElemsArg = NumElems; |
| 61 | return std::make_pair(ElemSizeArg, NumElemsArg); |
| 62 | } |
| 63 | |
Matt Arsenault | e1cef6e | 2014-09-03 23:24:31 +0000 | [diff] [blame] | 64 | Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, |
| 65 | uint64_t Val) { |
| 66 | LLVMContextImpl *pImpl = Context.pImpl; |
| 67 | FoldingSetNodeID ID; |
| 68 | ID.AddInteger(Kind); |
Matt Arsenault | fb4308e | 2014-09-03 23:38:05 +0000 | [diff] [blame] | 69 | if (Val) ID.AddInteger(Val); |
Matt Arsenault | e1cef6e | 2014-09-03 23:24:31 +0000 | [diff] [blame] | 70 | |
| 71 | void *InsertPoint; |
| 72 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 73 | |
| 74 | if (!PA) { |
| 75 | // If we didn't find any existing attributes of the same shape then create a |
| 76 | // new one and insert it. |
Matt Arsenault | fb4308e | 2014-09-03 23:38:05 +0000 | [diff] [blame] | 77 | if (!Val) |
| 78 | PA = new EnumAttributeImpl(Kind); |
| 79 | else |
| 80 | PA = new IntAttributeImpl(Kind, Val); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 81 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 82 | } |
| 83 | |
| 84 | // Return the Attribute that we found or created. |
| 85 | return Attribute(PA); |
| 86 | } |
| 87 | |
| 88 | Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) { |
| 89 | LLVMContextImpl *pImpl = Context.pImpl; |
| 90 | FoldingSetNodeID ID; |
| 91 | ID.AddString(Kind); |
| 92 | if (!Val.empty()) ID.AddString(Val); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 93 | |
| 94 | void *InsertPoint; |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 95 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 96 | |
| 97 | if (!PA) { |
| 98 | // If we didn't find any existing attributes of the same shape then create a |
| 99 | // new one and insert it. |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 100 | PA = new StringAttributeImpl(Kind, Val); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 101 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 102 | } |
| 103 | |
Bill Wendling | b9c5b1a | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 104 | // Return the Attribute that we found or created. |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 105 | return Attribute(PA); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Bill Wendling | 4bbe9db | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 108 | Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) { |
Bill Wendling | 1c7cc8a | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 109 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 110 | assert(Align <= 0x40000000 && "Alignment too large."); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 111 | return get(Context, Alignment, Align); |
Bill Wendling | 4bbe9db | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | Attribute Attribute::getWithStackAlignment(LLVMContext &Context, |
| 115 | uint64_t Align) { |
Bill Wendling | 1c7cc8a | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 116 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 117 | assert(Align <= 0x100 && "Alignment too large."); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 118 | return get(Context, StackAlignment, Align); |
Bill Wendling | 4bbe9db | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 121 | Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context, |
| 122 | uint64_t Bytes) { |
| 123 | assert(Bytes && "Bytes must be non-zero."); |
| 124 | return get(Context, Dereferenceable, Bytes); |
| 125 | } |
| 126 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 127 | Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context, |
| 128 | uint64_t Bytes) { |
| 129 | assert(Bytes && "Bytes must be non-zero."); |
| 130 | return get(Context, DereferenceableOrNull, Bytes); |
| 131 | } |
| 132 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 133 | Attribute |
| 134 | Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg, |
| 135 | const Optional<unsigned> &NumElemsArg) { |
| 136 | assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) && |
| 137 | "Invalid allocsize arguments -- given allocsize(0, 0)"); |
| 138 | return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg)); |
| 139 | } |
| 140 | |
Bill Wendling | 7707c5a | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 141 | //===----------------------------------------------------------------------===// |
| 142 | // Attribute Accessor Methods |
| 143 | //===----------------------------------------------------------------------===// |
| 144 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 145 | bool Attribute::isEnumAttribute() const { |
| 146 | return pImpl && pImpl->isEnumAttribute(); |
| 147 | } |
| 148 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 149 | bool Attribute::isIntAttribute() const { |
| 150 | return pImpl && pImpl->isIntAttribute(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool Attribute::isStringAttribute() const { |
| 154 | return pImpl && pImpl->isStringAttribute(); |
| 155 | } |
| 156 | |
| 157 | Attribute::AttrKind Attribute::getKindAsEnum() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 158 | if (!pImpl) return None; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 159 | assert((isEnumAttribute() || isIntAttribute()) && |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 160 | "Invalid attribute type to get the kind as an enum!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 161 | return pImpl->getKindAsEnum(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | uint64_t Attribute::getValueAsInt() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 165 | if (!pImpl) return 0; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 166 | assert(isIntAttribute() && |
| 167 | "Expected the attribute to be an integer attribute!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 168 | return pImpl->getValueAsInt(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | StringRef Attribute::getKindAsString() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 172 | if (!pImpl) return StringRef(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 173 | assert(isStringAttribute() && |
| 174 | "Invalid attribute type to get the kind as a string!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 175 | return pImpl->getKindAsString(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | StringRef Attribute::getValueAsString() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 179 | if (!pImpl) return StringRef(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 180 | assert(isStringAttribute() && |
| 181 | "Invalid attribute type to get the value as a string!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 182 | return pImpl->getValueAsString(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Bill Wendling | ae89a0f | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 185 | bool Attribute::hasAttribute(AttrKind Kind) const { |
| 186 | return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None); |
| 187 | } |
| 188 | |
| 189 | bool Attribute::hasAttribute(StringRef Kind) const { |
| 190 | if (!isStringAttribute()) return false; |
| 191 | return pImpl && pImpl->hasAttribute(Kind); |
Bill Wendling | 03eefb3 | 2013-01-29 20:45:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 194 | unsigned Attribute::getAlignment() const { |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 195 | assert(hasAttribute(Attribute::Alignment) && |
| 196 | "Trying to get alignment from non-alignment attribute!"); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 197 | return pImpl->getValueAsInt(); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 200 | unsigned Attribute::getStackAlignment() const { |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 201 | assert(hasAttribute(Attribute::StackAlignment) && |
| 202 | "Trying to get alignment from non-alignment attribute!"); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 203 | return pImpl->getValueAsInt(); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 206 | uint64_t Attribute::getDereferenceableBytes() const { |
| 207 | assert(hasAttribute(Attribute::Dereferenceable) && |
| 208 | "Trying to get dereferenceable bytes from " |
| 209 | "non-dereferenceable attribute!"); |
| 210 | return pImpl->getValueAsInt(); |
| 211 | } |
| 212 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 213 | uint64_t Attribute::getDereferenceableOrNullBytes() const { |
| 214 | assert(hasAttribute(Attribute::DereferenceableOrNull) && |
| 215 | "Trying to get dereferenceable bytes from " |
| 216 | "non-dereferenceable attribute!"); |
| 217 | return pImpl->getValueAsInt(); |
| 218 | } |
| 219 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 220 | std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const { |
| 221 | assert(hasAttribute(Attribute::AllocSize) && |
| 222 | "Trying to get allocsize args from non-allocsize attribute"); |
| 223 | return unpackAllocSizeArgs(pImpl->getValueAsInt()); |
| 224 | } |
| 225 | |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 226 | std::string Attribute::getAsString(bool InAttrGrp) const { |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 227 | if (!pImpl) return ""; |
| 228 | |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 229 | if (hasAttribute(Attribute::SanitizeAddress)) |
| 230 | return "sanitize_address"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 231 | if (hasAttribute(Attribute::AlwaysInline)) |
| 232 | return "alwaysinline"; |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 233 | if (hasAttribute(Attribute::ArgMemOnly)) |
| 234 | return "argmemonly"; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 235 | if (hasAttribute(Attribute::Builtin)) |
| 236 | return "builtin"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 237 | if (hasAttribute(Attribute::ByVal)) |
| 238 | return "byval"; |
Owen Anderson | 85fa7d5 | 2015-05-26 23:48:40 +0000 | [diff] [blame] | 239 | if (hasAttribute(Attribute::Convergent)) |
| 240 | return "convergent"; |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 241 | if (hasAttribute(Attribute::SwiftError)) |
| 242 | return "swifterror"; |
Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 243 | if (hasAttribute(Attribute::SwiftSelf)) |
| 244 | return "swiftself"; |
Vaivaswatha Nagaraj | fb3f490 | 2015-12-16 16:16:19 +0000 | [diff] [blame] | 245 | if (hasAttribute(Attribute::InaccessibleMemOnly)) |
| 246 | return "inaccessiblememonly"; |
| 247 | if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly)) |
| 248 | return "inaccessiblemem_or_argmemonly"; |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 249 | if (hasAttribute(Attribute::InAlloca)) |
| 250 | return "inalloca"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 251 | if (hasAttribute(Attribute::InlineHint)) |
| 252 | return "inlinehint"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 253 | if (hasAttribute(Attribute::InReg)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 254 | return "inreg"; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 255 | if (hasAttribute(Attribute::JumpTable)) |
| 256 | return "jumptable"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 257 | if (hasAttribute(Attribute::MinSize)) |
| 258 | return "minsize"; |
| 259 | if (hasAttribute(Attribute::Naked)) |
| 260 | return "naked"; |
| 261 | if (hasAttribute(Attribute::Nest)) |
| 262 | return "nest"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 263 | if (hasAttribute(Attribute::NoAlias)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 264 | return "noalias"; |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 265 | if (hasAttribute(Attribute::NoBuiltin)) |
| 266 | return "nobuiltin"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 267 | if (hasAttribute(Attribute::NoCapture)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 268 | return "nocapture"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 269 | if (hasAttribute(Attribute::NoDuplicate)) |
| 270 | return "noduplicate"; |
| 271 | if (hasAttribute(Attribute::NoImplicitFloat)) |
| 272 | return "noimplicitfloat"; |
| 273 | if (hasAttribute(Attribute::NoInline)) |
| 274 | return "noinline"; |
| 275 | if (hasAttribute(Attribute::NonLazyBind)) |
| 276 | return "nonlazybind"; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 277 | if (hasAttribute(Attribute::NonNull)) |
| 278 | return "nonnull"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 279 | if (hasAttribute(Attribute::NoRedZone)) |
| 280 | return "noredzone"; |
| 281 | if (hasAttribute(Attribute::NoReturn)) |
| 282 | return "noreturn"; |
James Molloy | e6f87ca | 2015-11-06 10:32:53 +0000 | [diff] [blame] | 283 | if (hasAttribute(Attribute::NoRecurse)) |
| 284 | return "norecurse"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 285 | if (hasAttribute(Attribute::NoUnwind)) |
| 286 | return "nounwind"; |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 287 | if (hasAttribute(Attribute::OptimizeNone)) |
| 288 | return "optnone"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 289 | if (hasAttribute(Attribute::OptimizeForSize)) |
| 290 | return "optsize"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 291 | if (hasAttribute(Attribute::ReadNone)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 292 | return "readnone"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 293 | if (hasAttribute(Attribute::ReadOnly)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 294 | return "readonly"; |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 295 | if (hasAttribute(Attribute::Returned)) |
| 296 | return "returned"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 297 | if (hasAttribute(Attribute::ReturnsTwice)) |
| 298 | return "returns_twice"; |
| 299 | if (hasAttribute(Attribute::SExt)) |
| 300 | return "signext"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 301 | if (hasAttribute(Attribute::StackProtect)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 302 | return "ssp"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 303 | if (hasAttribute(Attribute::StackProtectReq)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 304 | return "sspreq"; |
Bill Wendling | d154e283 | 2013-01-23 06:41:41 +0000 | [diff] [blame] | 305 | if (hasAttribute(Attribute::StackProtectStrong)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 306 | return "sspstrong"; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 307 | if (hasAttribute(Attribute::SafeStack)) |
| 308 | return "safestack"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 309 | if (hasAttribute(Attribute::StructRet)) |
| 310 | return "sret"; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 311 | if (hasAttribute(Attribute::SanitizeThread)) |
| 312 | return "sanitize_thread"; |
| 313 | if (hasAttribute(Attribute::SanitizeMemory)) |
| 314 | return "sanitize_memory"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 315 | if (hasAttribute(Attribute::UWTable)) |
| 316 | return "uwtable"; |
| 317 | if (hasAttribute(Attribute::ZExt)) |
| 318 | return "zeroext"; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 319 | if (hasAttribute(Attribute::Cold)) |
| 320 | return "cold"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 321 | |
| 322 | // FIXME: These should be output like this: |
| 323 | // |
| 324 | // align=4 |
| 325 | // alignstack=8 |
| 326 | // |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 327 | if (hasAttribute(Attribute::Alignment)) { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 328 | std::string Result; |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 329 | Result += "align"; |
| 330 | Result += (InAttrGrp) ? "=" : " "; |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 331 | Result += utostr(getValueAsInt()); |
| 332 | return Result; |
| 333 | } |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 334 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 335 | auto AttrWithBytesToString = [&](const char *Name) { |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 336 | std::string Result; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 337 | Result += Name; |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 338 | if (InAttrGrp) { |
| 339 | Result += "="; |
| 340 | Result += utostr(getValueAsInt()); |
| 341 | } else { |
| 342 | Result += "("; |
| 343 | Result += utostr(getValueAsInt()); |
| 344 | Result += ")"; |
| 345 | } |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 346 | return Result; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 347 | }; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 348 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 349 | if (hasAttribute(Attribute::StackAlignment)) |
| 350 | return AttrWithBytesToString("alignstack"); |
| 351 | |
| 352 | if (hasAttribute(Attribute::Dereferenceable)) |
| 353 | return AttrWithBytesToString("dereferenceable"); |
| 354 | |
| 355 | if (hasAttribute(Attribute::DereferenceableOrNull)) |
| 356 | return AttrWithBytesToString("dereferenceable_or_null"); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 357 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 358 | if (hasAttribute(Attribute::AllocSize)) { |
| 359 | unsigned ElemSize; |
| 360 | Optional<unsigned> NumElems; |
| 361 | std::tie(ElemSize, NumElems) = getAllocSizeArgs(); |
| 362 | |
| 363 | std::string Result = "allocsize("; |
| 364 | Result += utostr(ElemSize); |
| 365 | if (NumElems.hasValue()) { |
| 366 | Result += ','; |
| 367 | Result += utostr(*NumElems); |
| 368 | } |
| 369 | Result += ')'; |
| 370 | return Result; |
| 371 | } |
| 372 | |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 373 | // Convert target-dependent attributes to strings of the form: |
| 374 | // |
| 375 | // "kind" |
| 376 | // "kind" = "value" |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 377 | // |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 378 | if (isStringAttribute()) { |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 379 | std::string Result; |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 380 | Result += (Twine('"') + getKindAsString() + Twine('"')).str(); |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 381 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 382 | StringRef Val = pImpl->getValueAsString(); |
| 383 | if (Val.empty()) return Result; |
Bill Wendling | 7a33f77 | 2013-02-01 22:32:30 +0000 | [diff] [blame] | 384 | |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 385 | Result += ("=\"" + Val + Twine('"')).str(); |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 386 | return Result; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 387 | } |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 388 | |
| 389 | llvm_unreachable("Unknown attribute"); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 392 | bool Attribute::operator<(Attribute A) const { |
| 393 | if (!pImpl && !A.pImpl) return false; |
| 394 | if (!pImpl) return true; |
| 395 | if (!A.pImpl) return false; |
| 396 | return *pImpl < *A.pImpl; |
| 397 | } |
| 398 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | // AttributeImpl Definition |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | |
Eric Christopher | 0eaa541 | 2014-07-02 22:05:40 +0000 | [diff] [blame] | 403 | // Pin the vtables to this file. |
Alexey Samsonov | 49109a2 | 2013-11-18 09:31:53 +0000 | [diff] [blame] | 404 | AttributeImpl::~AttributeImpl() {} |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 405 | void EnumAttributeImpl::anchor() {} |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 406 | void IntAttributeImpl::anchor() {} |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 407 | void StringAttributeImpl::anchor() {} |
Alexey Samsonov | 49109a2 | 2013-11-18 09:31:53 +0000 | [diff] [blame] | 408 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 409 | bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 410 | if (isStringAttribute()) return false; |
| 411 | return getKindAsEnum() == A; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 414 | bool AttributeImpl::hasAttribute(StringRef Kind) const { |
| 415 | if (!isStringAttribute()) return false; |
| 416 | return getKindAsString() == Kind; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 419 | Attribute::AttrKind AttributeImpl::getKindAsEnum() const { |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 420 | assert(isEnumAttribute() || isIntAttribute()); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 421 | return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 424 | uint64_t AttributeImpl::getValueAsInt() const { |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 425 | assert(isIntAttribute()); |
| 426 | return static_cast<const IntAttributeImpl *>(this)->getValue(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 429 | StringRef AttributeImpl::getKindAsString() const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 430 | assert(isStringAttribute()); |
| 431 | return static_cast<const StringAttributeImpl *>(this)->getStringKind(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 434 | StringRef AttributeImpl::getValueAsString() const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 435 | assert(isStringAttribute()); |
| 436 | return static_cast<const StringAttributeImpl *>(this)->getStringValue(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | bool AttributeImpl::operator<(const AttributeImpl &AI) const { |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 440 | // This sorts the attributes with Attribute::AttrKinds coming first (sorted |
| 441 | // relative to their enum value) and then strings. |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 442 | if (isEnumAttribute()) { |
| 443 | if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 444 | if (AI.isIntAttribute()) return true; |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 445 | if (AI.isStringAttribute()) return true; |
| 446 | } |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 447 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 448 | if (isIntAttribute()) { |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 449 | if (AI.isEnumAttribute()) return false; |
Reid Kleckner | 7de6761 | 2016-04-04 23:06:05 +0000 | [diff] [blame] | 450 | if (AI.isIntAttribute()) { |
| 451 | if (getKindAsEnum() == AI.getKindAsEnum()) |
| 452 | return getValueAsInt() < AI.getValueAsInt(); |
| 453 | return getKindAsEnum() < AI.getKindAsEnum(); |
| 454 | } |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 455 | if (AI.isStringAttribute()) return true; |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 456 | } |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 457 | |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 458 | if (AI.isEnumAttribute()) return false; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 459 | if (AI.isIntAttribute()) return false; |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 460 | if (getKindAsString() == AI.getKindAsString()) |
| 461 | return getValueAsString() < AI.getValueAsString(); |
| 462 | return getKindAsString() < AI.getKindAsString(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 465 | uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) { |
| 466 | // FIXME: Remove this. |
| 467 | switch (Val) { |
| 468 | case Attribute::EndAttrKinds: |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 469 | llvm_unreachable("Synthetic enumerators which should never get here"); |
| 470 | |
| 471 | case Attribute::None: return 0; |
| 472 | case Attribute::ZExt: return 1 << 0; |
| 473 | case Attribute::SExt: return 1 << 1; |
| 474 | case Attribute::NoReturn: return 1 << 2; |
| 475 | case Attribute::InReg: return 1 << 3; |
| 476 | case Attribute::StructRet: return 1 << 4; |
| 477 | case Attribute::NoUnwind: return 1 << 5; |
| 478 | case Attribute::NoAlias: return 1 << 6; |
| 479 | case Attribute::ByVal: return 1 << 7; |
| 480 | case Attribute::Nest: return 1 << 8; |
| 481 | case Attribute::ReadNone: return 1 << 9; |
| 482 | case Attribute::ReadOnly: return 1 << 10; |
| 483 | case Attribute::NoInline: return 1 << 11; |
| 484 | case Attribute::AlwaysInline: return 1 << 12; |
| 485 | case Attribute::OptimizeForSize: return 1 << 13; |
| 486 | case Attribute::StackProtect: return 1 << 14; |
| 487 | case Attribute::StackProtectReq: return 1 << 15; |
| 488 | case Attribute::Alignment: return 31 << 16; |
| 489 | case Attribute::NoCapture: return 1 << 21; |
| 490 | case Attribute::NoRedZone: return 1 << 22; |
| 491 | case Attribute::NoImplicitFloat: return 1 << 23; |
| 492 | case Attribute::Naked: return 1 << 24; |
| 493 | case Attribute::InlineHint: return 1 << 25; |
| 494 | case Attribute::StackAlignment: return 7 << 26; |
| 495 | case Attribute::ReturnsTwice: return 1 << 29; |
| 496 | case Attribute::UWTable: return 1 << 30; |
| 497 | case Attribute::NonLazyBind: return 1U << 31; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 498 | case Attribute::SanitizeAddress: return 1ULL << 32; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 499 | case Attribute::MinSize: return 1ULL << 33; |
| 500 | case Attribute::NoDuplicate: return 1ULL << 34; |
| 501 | case Attribute::StackProtectStrong: return 1ULL << 35; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 502 | case Attribute::SanitizeThread: return 1ULL << 36; |
| 503 | case Attribute::SanitizeMemory: return 1ULL << 37; |
Bill Wendling | 7bfdfe7 | 2013-02-22 00:40:12 +0000 | [diff] [blame] | 504 | case Attribute::NoBuiltin: return 1ULL << 38; |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 505 | case Attribute::Returned: return 1ULL << 39; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 506 | case Attribute::Cold: return 1ULL << 40; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 507 | case Attribute::Builtin: return 1ULL << 41; |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 508 | case Attribute::OptimizeNone: return 1ULL << 42; |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 509 | case Attribute::InAlloca: return 1ULL << 43; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 510 | case Attribute::NonNull: return 1ULL << 44; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 511 | case Attribute::JumpTable: return 1ULL << 45; |
Owen Anderson | 85fa7d5 | 2015-05-26 23:48:40 +0000 | [diff] [blame] | 512 | case Attribute::Convergent: return 1ULL << 46; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 513 | case Attribute::SafeStack: return 1ULL << 47; |
James Molloy | e6f87ca | 2015-11-06 10:32:53 +0000 | [diff] [blame] | 514 | case Attribute::NoRecurse: return 1ULL << 48; |
Vaivaswatha Nagaraj | fb3f490 | 2015-12-16 16:16:19 +0000 | [diff] [blame] | 515 | case Attribute::InaccessibleMemOnly: return 1ULL << 49; |
| 516 | case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; |
Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 517 | case Attribute::SwiftSelf: return 1ULL << 51; |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 518 | case Attribute::SwiftError: return 1ULL << 52; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 519 | case Attribute::Dereferenceable: |
| 520 | llvm_unreachable("dereferenceable attribute not supported in raw format"); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 521 | break; |
| 522 | case Attribute::DereferenceableOrNull: |
| 523 | llvm_unreachable("dereferenceable_or_null attribute not supported in raw " |
| 524 | "format"); |
| 525 | break; |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 526 | case Attribute::ArgMemOnly: |
| 527 | llvm_unreachable("argmemonly attribute not supported in raw format"); |
| 528 | break; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 529 | case Attribute::AllocSize: |
| 530 | llvm_unreachable("allocsize not supported in raw format"); |
| 531 | break; |
Bill Wendling | 25342e1 | 2013-02-22 00:50:09 +0000 | [diff] [blame] | 532 | } |
| 533 | llvm_unreachable("Unsupported attribute type"); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | //===----------------------------------------------------------------------===// |
| 537 | // AttributeSetNode Definition |
| 538 | //===----------------------------------------------------------------------===// |
| 539 | |
| 540 | AttributeSetNode *AttributeSetNode::get(LLVMContext &C, |
| 541 | ArrayRef<Attribute> Attrs) { |
| 542 | if (Attrs.empty()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 543 | return nullptr; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 544 | |
| 545 | // Otherwise, build a key to look up the existing attributes. |
| 546 | LLVMContextImpl *pImpl = C.pImpl; |
| 547 | FoldingSetNodeID ID; |
| 548 | |
| 549 | SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); |
Reid Kleckner | 7de6761 | 2016-04-04 23:06:05 +0000 | [diff] [blame] | 550 | std::sort(SortedAttrs.begin(), SortedAttrs.end()); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 551 | |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 552 | for (Attribute Attr : SortedAttrs) |
| 553 | Attr.Profile(ID); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 554 | |
| 555 | void *InsertPoint; |
| 556 | AttributeSetNode *PA = |
| 557 | pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); |
| 558 | |
| 559 | // If we didn't find any existing attributes of the same shape then create a |
| 560 | // new one and insert it. |
| 561 | if (!PA) { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 562 | // Coallocate entries after the AttributeSetNode itself. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 563 | void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 564 | PA = new (Mem) AttributeSetNode(SortedAttrs); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 565 | pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); |
| 566 | } |
| 567 | |
| 568 | // Return the AttributesListNode that we found or created. |
| 569 | return PA; |
| 570 | } |
| 571 | |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 572 | bool AttributeSetNode::hasAttribute(StringRef Kind) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 573 | for (iterator I = begin(), E = end(); I != E; ++I) |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 574 | if (I->hasAttribute(Kind)) |
| 575 | return true; |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { |
Matthias Braun | 31eeb76f | 2016-01-29 22:25:13 +0000 | [diff] [blame] | 580 | if (hasAttribute(Kind)) { |
| 581 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 582 | if (I->hasAttribute(Kind)) |
| 583 | return *I; |
| 584 | } |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 585 | return Attribute(); |
| 586 | } |
| 587 | |
| 588 | Attribute AttributeSetNode::getAttribute(StringRef Kind) const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 589 | for (iterator I = begin(), E = end(); I != E; ++I) |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 590 | if (I->hasAttribute(Kind)) |
| 591 | return *I; |
| 592 | return Attribute(); |
| 593 | } |
| 594 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 595 | unsigned AttributeSetNode::getAlignment() const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 596 | for (iterator I = begin(), E = end(); I != E; ++I) |
NAKAMURA Takumi | b094439 | 2013-01-31 03:47:28 +0000 | [diff] [blame] | 597 | if (I->hasAttribute(Attribute::Alignment)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 598 | return I->getAlignment(); |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | unsigned AttributeSetNode::getStackAlignment() const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 603 | for (iterator I = begin(), E = end(); I != E; ++I) |
NAKAMURA Takumi | b094439 | 2013-01-31 03:47:28 +0000 | [diff] [blame] | 604 | if (I->hasAttribute(Attribute::StackAlignment)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 605 | return I->getStackAlignment(); |
| 606 | return 0; |
| 607 | } |
| 608 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 609 | uint64_t AttributeSetNode::getDereferenceableBytes() const { |
| 610 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 611 | if (I->hasAttribute(Attribute::Dereferenceable)) |
| 612 | return I->getDereferenceableBytes(); |
| 613 | return 0; |
| 614 | } |
| 615 | |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 616 | uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { |
| 617 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 618 | if (I->hasAttribute(Attribute::DereferenceableOrNull)) |
| 619 | return I->getDereferenceableOrNullBytes(); |
| 620 | return 0; |
| 621 | } |
| 622 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 623 | std::pair<unsigned, Optional<unsigned>> |
| 624 | AttributeSetNode::getAllocSizeArgs() const { |
| 625 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 626 | if (I->hasAttribute(Attribute::AllocSize)) |
| 627 | return I->getAllocSizeArgs(); |
| 628 | return std::make_pair(0, 0); |
| 629 | } |
| 630 | |
Rafael Espindola | cbf5a7a | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 631 | std::string AttributeSetNode::getAsString(bool InAttrGrp) const { |
Benjamin Kramer | 0baf8f4 | 2013-04-19 11:43:21 +0000 | [diff] [blame] | 632 | std::string Str; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 633 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 634 | if (I != begin()) |
Rafael Espindola | cbf5a7a | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 635 | Str += ' '; |
| 636 | Str += I->getAsString(InAttrGrp); |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 637 | } |
| 638 | return Str; |
| 639 | } |
| 640 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 641 | //===----------------------------------------------------------------------===// |
| 642 | // AttributeSetImpl Definition |
| 643 | //===----------------------------------------------------------------------===// |
| 644 | |
Rafael Espindola | dd27530 | 2013-04-30 16:53:38 +0000 | [diff] [blame] | 645 | uint64_t AttributeSetImpl::Raw(unsigned Index) const { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 646 | for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) { |
| 647 | if (getSlotIndex(I) != Index) continue; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 648 | const AttributeSetNode *ASN = getSlotNode(I); |
Bill Wendling | 9122618 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 649 | uint64_t Mask = 0; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 650 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 651 | for (AttributeSetNode::iterator II = ASN->begin(), |
Bill Wendling | 9122618 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 652 | IE = ASN->end(); II != IE; ++II) { |
| 653 | Attribute Attr = *II; |
Bill Wendling | d746e40 | 2013-02-10 23:18:05 +0000 | [diff] [blame] | 654 | |
| 655 | // This cannot handle string attributes. |
| 656 | if (Attr.isStringAttribute()) continue; |
| 657 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 658 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
Bill Wendling | 9122618 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 659 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 660 | if (Kind == Attribute::Alignment) |
Bill Wendling | 9122618 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 661 | Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16; |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 662 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | 9122618 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 663 | Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 664 | else if (Kind == Attribute::Dereferenceable) |
| 665 | llvm_unreachable("dereferenceable not supported in bit mask"); |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 666 | else if (Kind == Attribute::AllocSize) |
| 667 | llvm_unreachable("allocsize not supported in bit mask"); |
Bill Wendling | 9122618 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 668 | else |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 669 | Mask |= AttributeImpl::getAttrMask(Kind); |
Bill Wendling | 9122618 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | return Mask; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 678 | LLVM_DUMP_METHOD void AttributeSetImpl::dump() const { |
Peter Collingbourne | abca2ec | 2013-08-02 22:34:30 +0000 | [diff] [blame] | 679 | AttributeSet(const_cast<AttributeSetImpl *>(this)).dump(); |
| 680 | } |
| 681 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 682 | //===----------------------------------------------------------------------===// |
| 683 | // AttributeSet Construction and Mutation Methods |
| 684 | //===----------------------------------------------------------------------===// |
| 685 | |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 686 | AttributeSet |
| 687 | AttributeSet::getImpl(LLVMContext &C, |
| 688 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 689 | LLVMContextImpl *pImpl = C.pImpl; |
| 690 | FoldingSetNodeID ID; |
| 691 | AttributeSetImpl::Profile(ID, Attrs); |
| 692 | |
| 693 | void *InsertPoint; |
| 694 | AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); |
| 695 | |
| 696 | // If we didn't find any existing attributes of the same shape then |
| 697 | // create a new one and insert it. |
| 698 | if (!PA) { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 699 | // Coallocate entries after the AttributeSetImpl itself. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 700 | void *Mem = ::operator new( |
| 701 | AttributeSetImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size())); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 702 | PA = new (Mem) AttributeSetImpl(C, Attrs); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 703 | pImpl->AttrsLists.InsertNode(PA, InsertPoint); |
| 704 | } |
| 705 | |
| 706 | // Return the AttributesList that we found or created. |
| 707 | return AttributeSet(PA); |
| 708 | } |
| 709 | |
| 710 | AttributeSet AttributeSet::get(LLVMContext &C, |
| 711 | ArrayRef<std::pair<unsigned, Attribute> > Attrs){ |
| 712 | // If there are no attributes then return a null AttributesList pointer. |
| 713 | if (Attrs.empty()) |
| 714 | return AttributeSet(); |
| 715 | |
Craig Topper | e30b8ca | 2016-01-03 19:43:40 +0000 | [diff] [blame] | 716 | assert(std::is_sorted(Attrs.begin(), Attrs.end(), |
| 717 | [](const std::pair<unsigned, Attribute> &LHS, |
| 718 | const std::pair<unsigned, Attribute> &RHS) { |
| 719 | return LHS.first < RHS.first; |
| 720 | }) && "Misordered Attributes list!"); |
| 721 | assert(std::none_of(Attrs.begin(), Attrs.end(), |
| 722 | [](const std::pair<unsigned, Attribute> &Pair) { |
| 723 | return Pair.second.hasAttribute(Attribute::None); |
| 724 | }) && "Pointless attribute!"); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 725 | |
| 726 | // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes |
| 727 | // list. |
| 728 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec; |
| 729 | for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(), |
| 730 | E = Attrs.end(); I != E; ) { |
| 731 | unsigned Index = I->first; |
| 732 | SmallVector<Attribute, 4> AttrVec; |
NAKAMURA Takumi | f05d2f2 | 2013-01-29 15:18:16 +0000 | [diff] [blame] | 733 | while (I != E && I->first == Index) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 734 | AttrVec.push_back(I->second); |
| 735 | ++I; |
| 736 | } |
| 737 | |
| 738 | AttrPairVec.push_back(std::make_pair(Index, |
| 739 | AttributeSetNode::get(C, AttrVec))); |
| 740 | } |
| 741 | |
| 742 | return getImpl(C, AttrPairVec); |
| 743 | } |
| 744 | |
| 745 | AttributeSet AttributeSet::get(LLVMContext &C, |
| 746 | ArrayRef<std::pair<unsigned, |
| 747 | AttributeSetNode*> > Attrs) { |
| 748 | // If there are no attributes then return a null AttributesList pointer. |
| 749 | if (Attrs.empty()) |
| 750 | return AttributeSet(); |
| 751 | |
| 752 | return getImpl(C, Attrs); |
| 753 | } |
| 754 | |
David Majnemer | cf63a79 | 2014-05-03 23:00:35 +0000 | [diff] [blame] | 755 | AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, |
| 756 | const AttrBuilder &B) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 757 | if (!B.hasAttributes()) |
| 758 | return AttributeSet(); |
Bill Wendling | f713481 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 759 | |
Bill Wendling | ae89a0f | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 760 | // Add target-independent attributes. |
Bill Wendling | f713481 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 761 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
Benjamin Kramer | 45e7d53 | 2013-02-16 19:13:18 +0000 | [diff] [blame] | 762 | for (Attribute::AttrKind Kind = Attribute::None; |
Benjamin Kramer | 6f37dac | 2013-02-16 19:22:28 +0000 | [diff] [blame] | 763 | Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { |
Benjamin Kramer | 45e7d53 | 2013-02-16 19:13:18 +0000 | [diff] [blame] | 764 | if (!B.contains(Kind)) |
| 765 | continue; |
| 766 | |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 767 | Attribute Attr; |
| 768 | switch (Kind) { |
| 769 | case Attribute::Alignment: |
| 770 | Attr = Attribute::getWithAlignment(C, B.getAlignment()); |
| 771 | break; |
| 772 | case Attribute::StackAlignment: |
| 773 | Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment()); |
| 774 | break; |
| 775 | case Attribute::Dereferenceable: |
| 776 | Attr = Attribute::getWithDereferenceableBytes( |
| 777 | C, B.getDereferenceableBytes()); |
| 778 | break; |
| 779 | case Attribute::DereferenceableOrNull: |
| 780 | Attr = Attribute::getWithDereferenceableOrNullBytes( |
| 781 | C, B.getDereferenceableOrNullBytes()); |
| 782 | break; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 783 | case Attribute::AllocSize: { |
| 784 | auto A = B.getAllocSizeArgs(); |
| 785 | Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second); |
| 786 | break; |
| 787 | } |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 788 | default: |
| 789 | Attr = Attribute::get(C, Kind); |
| 790 | } |
| 791 | Attrs.push_back(std::make_pair(Index, Attr)); |
Bill Wendling | f713481 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Bill Wendling | ae89a0f | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 794 | // Add target-dependent (string) attributes. |
Benjamin Kramer | ea76b6f | 2016-06-12 19:02:34 +0000 | [diff] [blame^] | 795 | for (const auto &TDA : B.td_attrs()) |
David Majnemer | cf63a79 | 2014-05-03 23:00:35 +0000 | [diff] [blame] | 796 | Attrs.push_back( |
| 797 | std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second))); |
Bill Wendling | ae89a0f | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 798 | |
Bill Wendling | f713481 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 799 | return get(C, Attrs); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 802 | AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index, |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 803 | ArrayRef<Attribute::AttrKind> Kind) { |
| 804 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 805 | for (Attribute::AttrKind K : Kind) |
| 806 | Attrs.push_back(std::make_pair(Index, Attribute::get(C, K))); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 807 | return get(C, Attrs); |
| 808 | } |
| 809 | |
| 810 | AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) { |
| 811 | if (Attrs.empty()) return AttributeSet(); |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 812 | if (Attrs.size() == 1) return Attrs[0]; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 813 | |
| 814 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec; |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 815 | AttributeSetImpl *A0 = Attrs[0].pImpl; |
| 816 | if (A0) |
| 817 | AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes())); |
| 818 | // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec |
| 819 | // ordered by index. Because we know that each list in Attrs is ordered by |
| 820 | // index we only need to merge each successive list in rather than doing a |
| 821 | // full sort. |
| 822 | for (unsigned I = 1, E = Attrs.size(); I != E; ++I) { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 823 | AttributeSetImpl *AS = Attrs[I].pImpl; |
| 824 | if (!AS) continue; |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 825 | SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator |
| 826 | ANVI = AttrNodeVec.begin(), ANVE; |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 827 | for (const IndexAttrPair *AI = AS->getNode(0), |
| 828 | *AE = AS->getNode(AS->getNumAttributes()); |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 829 | AI != AE; ++AI) { |
| 830 | ANVE = AttrNodeVec.end(); |
| 831 | while (ANVI != ANVE && ANVI->first <= AI->first) |
| 832 | ++ANVI; |
| 833 | ANVI = AttrNodeVec.insert(ANVI, *AI) + 1; |
| 834 | } |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | return getImpl(C, AttrNodeVec); |
| 838 | } |
| 839 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 840 | AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index, |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 841 | Attribute::AttrKind Attr) const { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 842 | if (hasAttribute(Index, Attr)) return *this; |
| 843 | return addAttributes(C, Index, AttributeSet::get(C, Index, Attr)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 846 | AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index, |
Reed Kotler | 795c7b4 | 2013-03-13 20:20:08 +0000 | [diff] [blame] | 847 | StringRef Kind) const { |
| 848 | llvm::AttrBuilder B; |
| 849 | B.addAttribute(Kind); |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 850 | return addAttributes(C, Index, AttributeSet::get(C, Index, B)); |
Reed Kotler | 795c7b4 | 2013-03-13 20:20:08 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Bill Wendling | 3b2f610 | 2013-07-25 18:34:24 +0000 | [diff] [blame] | 853 | AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index, |
| 854 | StringRef Kind, StringRef Value) const { |
| 855 | llvm::AttrBuilder B; |
| 856 | B.addAttribute(Kind, Value); |
| 857 | return addAttributes(C, Index, AttributeSet::get(C, Index, B)); |
| 858 | } |
| 859 | |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 860 | AttributeSet AttributeSet::addAttribute(LLVMContext &C, |
| 861 | ArrayRef<unsigned> Indices, |
| 862 | Attribute A) const { |
| 863 | unsigned I = 0, E = pImpl ? pImpl->getNumAttributes() : 0; |
| 864 | auto IdxI = Indices.begin(), IdxE = Indices.end(); |
| 865 | SmallVector<AttributeSet, 4> AttrSet; |
| 866 | |
| 867 | while (I != E && IdxI != IdxE) { |
| 868 | if (getSlotIndex(I) < *IdxI) |
| 869 | AttrSet.emplace_back(getSlotAttributes(I++)); |
| 870 | else if (getSlotIndex(I) > *IdxI) |
| 871 | AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A))); |
| 872 | else { |
| 873 | AttrBuilder B(getSlotAttributes(I), *IdxI); |
| 874 | B.addAttribute(A); |
| 875 | AttrSet.emplace_back(AttributeSet::get(C, *IdxI, B)); |
| 876 | ++I; |
| 877 | ++IdxI; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | while (I != E) |
| 882 | AttrSet.emplace_back(getSlotAttributes(I++)); |
| 883 | |
| 884 | while (IdxI != IdxE) |
| 885 | AttrSet.emplace_back(AttributeSet::get(C, std::make_pair(*IdxI++, A))); |
| 886 | |
| 887 | return get(C, AttrSet); |
| 888 | } |
| 889 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 890 | AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index, |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 891 | AttributeSet Attrs) const { |
| 892 | if (!pImpl) return Attrs; |
| 893 | if (!Attrs.pImpl) return *this; |
| 894 | |
| 895 | #ifndef NDEBUG |
| 896 | // FIXME it is not obvious how this should work for alignment. For now, say |
| 897 | // we can't change a known alignment. |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 898 | unsigned OldAlign = getParamAlignment(Index); |
| 899 | unsigned NewAlign = Attrs.getParamAlignment(Index); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 900 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
| 901 | "Attempt to change alignment!"); |
| 902 | #endif |
| 903 | |
| 904 | // Add the attribute slots before the one we're trying to add. |
| 905 | SmallVector<AttributeSet, 4> AttrSet; |
| 906 | uint64_t NumAttrs = pImpl->getNumAttributes(); |
| 907 | AttributeSet AS; |
| 908 | uint64_t LastIndex = 0; |
| 909 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 910 | if (getSlotIndex(I) >= Index) { |
| 911 | if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 912 | break; |
| 913 | } |
| 914 | LastIndex = I + 1; |
| 915 | AttrSet.push_back(getSlotAttributes(I)); |
| 916 | } |
| 917 | |
| 918 | // Now add the attribute into the correct slot. There may already be an |
| 919 | // AttributeSet there. |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 920 | AttrBuilder B(AS, Index); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 921 | |
| 922 | for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 923 | if (Attrs.getSlotIndex(I) == Index) { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 924 | for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I), |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 925 | IE = Attrs.pImpl->end(I); II != IE; ++II) |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 926 | B.addAttribute(*II); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 927 | break; |
| 928 | } |
| 929 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 930 | AttrSet.push_back(AttributeSet::get(C, Index, B)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 931 | |
| 932 | // Add the remaining attribute slots. |
| 933 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 934 | AttrSet.push_back(getSlotAttributes(I)); |
| 935 | |
| 936 | return get(C, AttrSet); |
| 937 | } |
| 938 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 939 | AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index, |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 940 | Attribute::AttrKind Attr) const { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 941 | if (!hasAttribute(Index, Attr)) return *this; |
| 942 | return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 945 | AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index, |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 946 | AttributeSet Attrs) const { |
| 947 | if (!pImpl) return AttributeSet(); |
| 948 | if (!Attrs.pImpl) return *this; |
| 949 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 950 | // FIXME it is not obvious how this should work for alignment. |
| 951 | // For now, say we can't pass in alignment, which no current use does. |
| 952 | assert(!Attrs.hasAttribute(Index, Attribute::Alignment) && |
| 953 | "Attempt to change alignment!"); |
| 954 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 955 | // Add the attribute slots before the one we're trying to add. |
| 956 | SmallVector<AttributeSet, 4> AttrSet; |
| 957 | uint64_t NumAttrs = pImpl->getNumAttributes(); |
| 958 | AttributeSet AS; |
| 959 | uint64_t LastIndex = 0; |
| 960 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 961 | if (getSlotIndex(I) >= Index) { |
| 962 | if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 963 | break; |
| 964 | } |
| 965 | LastIndex = I + 1; |
| 966 | AttrSet.push_back(getSlotAttributes(I)); |
| 967 | } |
| 968 | |
Bill Wendling | d219675 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 969 | // Now remove the attribute from the correct slot. There may already be an |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 970 | // AttributeSet there. |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 971 | AttrBuilder B(AS, Index); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 972 | |
| 973 | for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 974 | if (Attrs.getSlotIndex(I) == Index) { |
| 975 | B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 976 | break; |
| 977 | } |
| 978 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 979 | AttrSet.push_back(AttributeSet::get(C, Index, B)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 980 | |
| 981 | // Add the remaining attribute slots. |
| 982 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 983 | AttrSet.push_back(getSlotAttributes(I)); |
| 984 | |
| 985 | return get(C, AttrSet); |
| 986 | } |
| 987 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 988 | AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index, |
| 989 | const AttrBuilder &Attrs) const { |
| 990 | if (!pImpl) return AttributeSet(); |
| 991 | |
| 992 | // FIXME it is not obvious how this should work for alignment. |
| 993 | // For now, say we can't pass in alignment, which no current use does. |
| 994 | assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!"); |
| 995 | |
| 996 | // Add the attribute slots before the one we're trying to add. |
| 997 | SmallVector<AttributeSet, 4> AttrSet; |
| 998 | uint64_t NumAttrs = pImpl->getNumAttributes(); |
| 999 | AttributeSet AS; |
| 1000 | uint64_t LastIndex = 0; |
| 1001 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
| 1002 | if (getSlotIndex(I) >= Index) { |
| 1003 | if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); |
| 1004 | break; |
| 1005 | } |
| 1006 | LastIndex = I + 1; |
| 1007 | AttrSet.push_back(getSlotAttributes(I)); |
| 1008 | } |
| 1009 | |
| 1010 | // Now remove the attribute from the correct slot. There may already be an |
| 1011 | // AttributeSet there. |
| 1012 | AttrBuilder B(AS, Index); |
| 1013 | B.remove(Attrs); |
| 1014 | |
| 1015 | AttrSet.push_back(AttributeSet::get(C, Index, B)); |
| 1016 | |
| 1017 | // Add the remaining attribute slots. |
| 1018 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 1019 | AttrSet.push_back(getSlotAttributes(I)); |
| 1020 | |
| 1021 | return get(C, AttrSet); |
| 1022 | } |
| 1023 | |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 1024 | AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index, |
| 1025 | uint64_t Bytes) const { |
| 1026 | llvm::AttrBuilder B; |
| 1027 | B.addDereferenceableAttr(Bytes); |
| 1028 | return addAttributes(C, Index, AttributeSet::get(C, Index, B)); |
| 1029 | } |
| 1030 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1031 | AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C, |
| 1032 | unsigned Index, |
| 1033 | uint64_t Bytes) const { |
| 1034 | llvm::AttrBuilder B; |
| 1035 | B.addDereferenceableOrNullAttr(Bytes); |
| 1036 | return addAttributes(C, Index, AttributeSet::get(C, Index, B)); |
| 1037 | } |
| 1038 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1039 | AttributeSet |
| 1040 | AttributeSet::addAllocSizeAttr(LLVMContext &C, unsigned Index, |
| 1041 | unsigned ElemSizeArg, |
| 1042 | const Optional<unsigned> &NumElemsArg) { |
| 1043 | llvm::AttrBuilder B; |
| 1044 | B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); |
| 1045 | return addAttributes(C, Index, AttributeSet::get(C, Index, B)); |
| 1046 | } |
| 1047 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1048 | //===----------------------------------------------------------------------===// |
| 1049 | // AttributeSet Accessor Methods |
| 1050 | //===----------------------------------------------------------------------===// |
| 1051 | |
Bill Wendling | 5d020a3 | 2013-02-10 05:00:40 +0000 | [diff] [blame] | 1052 | LLVMContext &AttributeSet::getContext() const { |
| 1053 | return pImpl->getContext(); |
| 1054 | } |
| 1055 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1056 | AttributeSet AttributeSet::getParamAttributes(unsigned Index) const { |
| 1057 | return pImpl && hasAttributes(Index) ? |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1058 | AttributeSet::get(pImpl->getContext(), |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1059 | ArrayRef<std::pair<unsigned, AttributeSetNode*> >( |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1060 | std::make_pair(Index, getAttributes(Index)))) : |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1061 | AttributeSet(); |
| 1062 | } |
| 1063 | |
| 1064 | AttributeSet AttributeSet::getRetAttributes() const { |
| 1065 | return pImpl && hasAttributes(ReturnIndex) ? |
| 1066 | AttributeSet::get(pImpl->getContext(), |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1067 | ArrayRef<std::pair<unsigned, AttributeSetNode*> >( |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1068 | std::make_pair(ReturnIndex, |
| 1069 | getAttributes(ReturnIndex)))) : |
| 1070 | AttributeSet(); |
| 1071 | } |
| 1072 | |
| 1073 | AttributeSet AttributeSet::getFnAttributes() const { |
| 1074 | return pImpl && hasAttributes(FunctionIndex) ? |
| 1075 | AttributeSet::get(pImpl->getContext(), |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1076 | ArrayRef<std::pair<unsigned, AttributeSetNode*> >( |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1077 | std::make_pair(FunctionIndex, |
| 1078 | getAttributes(FunctionIndex)))) : |
| 1079 | AttributeSet(); |
| 1080 | } |
| 1081 | |
| 1082 | bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{ |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1083 | AttributeSetNode *ASN = getAttributes(Index); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1084 | return ASN && ASN->hasAttribute(Kind); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 1087 | bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const { |
| 1088 | AttributeSetNode *ASN = getAttributes(Index); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1089 | return ASN && ASN->hasAttribute(Kind); |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1092 | bool AttributeSet::hasAttributes(unsigned Index) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1093 | AttributeSetNode *ASN = getAttributes(Index); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1094 | return ASN && ASN->hasAttributes(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 1097 | bool AttributeSet::hasFnAttribute(Attribute::AttrKind Kind) const { |
| 1098 | return pImpl && pImpl->hasFnAttribute(Kind); |
| 1099 | } |
| 1100 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1101 | bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1102 | if (!pImpl) return false; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1103 | |
| 1104 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 1105 | for (AttributeSetImpl::iterator II = pImpl->begin(I), |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1106 | IE = pImpl->end(I); II != IE; ++II) |
NAKAMURA Takumi | b094439 | 2013-01-31 03:47:28 +0000 | [diff] [blame] | 1107 | if (II->hasAttribute(Attr)) |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1108 | return true; |
| 1109 | |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 1113 | Attribute AttributeSet::getAttribute(unsigned Index, |
| 1114 | Attribute::AttrKind Kind) const { |
| 1115 | AttributeSetNode *ASN = getAttributes(Index); |
| 1116 | return ASN ? ASN->getAttribute(Kind) : Attribute(); |
| 1117 | } |
| 1118 | |
| 1119 | Attribute AttributeSet::getAttribute(unsigned Index, |
| 1120 | StringRef Kind) const { |
| 1121 | AttributeSetNode *ASN = getAttributes(Index); |
| 1122 | return ASN ? ASN->getAttribute(Kind) : Attribute(); |
| 1123 | } |
| 1124 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1125 | unsigned AttributeSet::getParamAlignment(unsigned Index) const { |
| 1126 | AttributeSetNode *ASN = getAttributes(Index); |
| 1127 | return ASN ? ASN->getAlignment() : 0; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | unsigned AttributeSet::getStackAlignment(unsigned Index) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1131 | AttributeSetNode *ASN = getAttributes(Index); |
| 1132 | return ASN ? ASN->getStackAlignment() : 0; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1135 | uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const { |
| 1136 | AttributeSetNode *ASN = getAttributes(Index); |
| 1137 | return ASN ? ASN->getDereferenceableBytes() : 0; |
| 1138 | } |
| 1139 | |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 1140 | uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const { |
| 1141 | AttributeSetNode *ASN = getAttributes(Index); |
| 1142 | return ASN ? ASN->getDereferenceableOrNullBytes() : 0; |
| 1143 | } |
| 1144 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1145 | std::pair<unsigned, Optional<unsigned>> |
| 1146 | AttributeSet::getAllocSizeArgs(unsigned Index) const { |
| 1147 | AttributeSetNode *ASN = getAttributes(Index); |
| 1148 | return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0, 0); |
| 1149 | } |
| 1150 | |
| 1151 | std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1152 | AttributeSetNode *ASN = getAttributes(Index); |
Rafael Espindola | cbf5a7a | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 1153 | return ASN ? ASN->getAsString(InAttrGrp) : std::string(""); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1156 | AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1157 | if (!pImpl) return nullptr; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1158 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1159 | // Loop through to find the attribute node we want. |
| 1160 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1161 | if (pImpl->getSlotIndex(I) == Index) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1162 | return pImpl->getSlotNode(I); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1163 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1164 | return nullptr; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1167 | AttributeSet::iterator AttributeSet::begin(unsigned Slot) const { |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1168 | if (!pImpl) |
| 1169 | return ArrayRef<Attribute>().begin(); |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1170 | return pImpl->begin(Slot); |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1173 | AttributeSet::iterator AttributeSet::end(unsigned Slot) const { |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1174 | if (!pImpl) |
| 1175 | return ArrayRef<Attribute>().end(); |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1176 | return pImpl->end(Slot); |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1179 | //===----------------------------------------------------------------------===// |
| 1180 | // AttributeSet Introspection Methods |
| 1181 | //===----------------------------------------------------------------------===// |
| 1182 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1183 | unsigned AttributeSet::getNumSlots() const { |
| 1184 | return pImpl ? pImpl->getNumAttributes() : 0; |
| 1185 | } |
| 1186 | |
Rafael Espindola | dd27530 | 2013-04-30 16:53:38 +0000 | [diff] [blame] | 1187 | unsigned AttributeSet::getSlotIndex(unsigned Slot) const { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1188 | assert(pImpl && Slot < pImpl->getNumAttributes() && |
| 1189 | "Slot # out of range!"); |
| 1190 | return pImpl->getSlotIndex(Slot); |
| 1191 | } |
| 1192 | |
| 1193 | AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const { |
| 1194 | assert(pImpl && Slot < pImpl->getNumAttributes() && |
| 1195 | "Slot # out of range!"); |
| 1196 | return pImpl->getSlotAttributes(Slot); |
| 1197 | } |
| 1198 | |
| 1199 | uint64_t AttributeSet::Raw(unsigned Index) const { |
| 1200 | // FIXME: Remove this. |
| 1201 | return pImpl ? pImpl->Raw(Index) : 0; |
| 1202 | } |
| 1203 | |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 1204 | LLVM_DUMP_METHOD void AttributeSet::dump() const { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1205 | dbgs() << "PAL[\n"; |
| 1206 | |
| 1207 | for (unsigned i = 0, e = getNumSlots(); i < e; ++i) { |
| 1208 | uint64_t Index = getSlotIndex(i); |
| 1209 | dbgs() << " { "; |
| 1210 | if (Index == ~0U) |
| 1211 | dbgs() << "~0U"; |
| 1212 | else |
| 1213 | dbgs() << Index; |
| 1214 | dbgs() << " => " << getAsString(Index) << " }\n"; |
| 1215 | } |
| 1216 | |
| 1217 | dbgs() << "]\n"; |
| 1218 | } |
| 1219 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1220 | //===----------------------------------------------------------------------===// |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1221 | // AttrBuilder Method Implementations |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1222 | //===----------------------------------------------------------------------===// |
| 1223 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1224 | AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index) |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1225 | : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1226 | DerefOrNullBytes(0), AllocSizeArgs(0) { |
Bill Wendling | 56b0b2a | 2013-01-27 21:23:46 +0000 | [diff] [blame] | 1227 | AttributeSetImpl *pImpl = AS.pImpl; |
Bill Wendling | 096f544 | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 1228 | if (!pImpl) return; |
| 1229 | |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 1230 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1231 | if (pImpl->getSlotIndex(I) != Index) continue; |
Bill Wendling | 096f544 | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 1232 | |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 1233 | for (AttributeSetImpl::iterator II = pImpl->begin(I), |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 1234 | IE = pImpl->end(I); II != IE; ++II) |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1235 | addAttribute(*II); |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 1236 | |
| 1237 | break; |
| 1238 | } |
Bill Wendling | 096f544 | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1241 | void AttrBuilder::clear() { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1242 | Attrs.reset(); |
Sanjoy Das | 88d0fde | 2015-09-03 22:27:42 +0000 | [diff] [blame] | 1243 | TargetDepAttrs.clear(); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1244 | Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1245 | AllocSizeArgs = 0; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1249 | assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); |
Bill Wendling | 1c7cc8a | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 1250 | assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment && |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1251 | Val != Attribute::Dereferenceable && Val != Attribute::AllocSize && |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1252 | "Adding integer attribute without adding a value!"); |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1253 | Attrs[Val] = true; |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 1254 | return *this; |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1257 | AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { |
Bill Wendling | 0a43730 | 2013-02-10 10:13:23 +0000 | [diff] [blame] | 1258 | if (Attr.isStringAttribute()) { |
| 1259 | addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); |
| 1260 | return *this; |
| 1261 | } |
| 1262 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 1263 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1264 | Attrs[Kind] = true; |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1265 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 1266 | if (Kind == Attribute::Alignment) |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1267 | Alignment = Attr.getAlignment(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 1268 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1269 | StackAlignment = Attr.getStackAlignment(); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1270 | else if (Kind == Attribute::Dereferenceable) |
| 1271 | DerefBytes = Attr.getDereferenceableBytes(); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1272 | else if (Kind == Attribute::DereferenceableOrNull) |
| 1273 | DerefOrNullBytes = Attr.getDereferenceableOrNullBytes(); |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1274 | else if (Kind == Attribute::AllocSize) |
| 1275 | AllocSizeArgs = Attr.getValueAsInt(); |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1276 | return *this; |
| 1277 | } |
| 1278 | |
Bill Wendling | b9c5b1a | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 1279 | AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { |
| 1280 | TargetDepAttrs[A] = V; |
| 1281 | return *this; |
| 1282 | } |
| 1283 | |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1284 | AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1285 | assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); |
| 1286 | Attrs[Val] = false; |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (Val == Attribute::Alignment) |
| 1289 | Alignment = 0; |
| 1290 | else if (Val == Attribute::StackAlignment) |
| 1291 | StackAlignment = 0; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1292 | else if (Val == Attribute::Dereferenceable) |
| 1293 | DerefBytes = 0; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1294 | else if (Val == Attribute::DereferenceableOrNull) |
| 1295 | DerefOrNullBytes = 0; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1296 | else if (Val == Attribute::AllocSize) |
| 1297 | AllocSizeArgs = 0; |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1298 | |
| 1299 | return *this; |
| 1300 | } |
| 1301 | |
Bill Wendling | d219675 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 1302 | AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1303 | unsigned Slot = ~0U; |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1304 | for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) |
| 1305 | if (A.getSlotIndex(I) == Index) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1306 | Slot = I; |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1307 | break; |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1308 | } |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1309 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1310 | assert(Slot != ~0U && "Couldn't find index in AttributeSet!"); |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1311 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1312 | for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) { |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1313 | Attribute Attr = *I; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 1314 | if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1315 | removeAttribute(Attr.getKindAsEnum()); |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1316 | } else { |
| 1317 | assert(Attr.isStringAttribute() && "Invalid attribute type!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1318 | removeAttribute(Attr.getKindAsString()); |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1319 | } |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | return *this; |
| 1323 | } |
| 1324 | |
Bill Wendling | b9c5b1a | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 1325 | AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { |
| 1326 | std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A); |
| 1327 | if (I != TargetDepAttrs.end()) |
| 1328 | TargetDepAttrs.erase(I); |
| 1329 | return *this; |
| 1330 | } |
| 1331 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1332 | std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const { |
| 1333 | return unpackAllocSizeArgs(AllocSizeArgs); |
| 1334 | } |
| 1335 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1336 | AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 1337 | if (Align == 0) return *this; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1338 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1339 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 1340 | assert(Align <= 0x40000000 && "Alignment too large."); |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1341 | |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1342 | Attrs[Attribute::Alignment] = true; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1343 | Alignment = Align; |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 1344 | return *this; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1347 | AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { |
| 1348 | // Default alignment, allow the target to define how to align it. |
| 1349 | if (Align == 0) return *this; |
| 1350 | |
| 1351 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 1352 | assert(Align <= 0x100 && "Alignment too large."); |
| 1353 | |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1354 | Attrs[Attribute::StackAlignment] = true; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1355 | StackAlignment = Align; |
| 1356 | return *this; |
| 1357 | } |
| 1358 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1359 | AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { |
| 1360 | if (Bytes == 0) return *this; |
| 1361 | |
| 1362 | Attrs[Attribute::Dereferenceable] = true; |
| 1363 | DerefBytes = Bytes; |
| 1364 | return *this; |
| 1365 | } |
| 1366 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1367 | AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { |
| 1368 | if (Bytes == 0) |
| 1369 | return *this; |
| 1370 | |
| 1371 | Attrs[Attribute::DereferenceableOrNull] = true; |
| 1372 | DerefOrNullBytes = Bytes; |
| 1373 | return *this; |
| 1374 | } |
| 1375 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1376 | AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize, |
| 1377 | const Optional<unsigned> &NumElems) { |
| 1378 | return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems)); |
| 1379 | } |
| 1380 | |
| 1381 | AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) { |
| 1382 | // (0, 0) is our "not present" value, so we need to check for it here. |
| 1383 | assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"); |
| 1384 | |
| 1385 | Attrs[Attribute::AllocSize] = true; |
| 1386 | // Reuse existing machinery to store this as a single 64-bit integer so we can |
| 1387 | // save a few bytes over using a pair<unsigned, Optional<unsigned>>. |
| 1388 | AllocSizeArgs = RawArgs; |
| 1389 | return *this; |
| 1390 | } |
| 1391 | |
Bill Wendling | e261492 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1392 | AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { |
| 1393 | // FIXME: What if both have alignments, but they don't match?! |
| 1394 | if (!Alignment) |
| 1395 | Alignment = B.Alignment; |
| 1396 | |
| 1397 | if (!StackAlignment) |
| 1398 | StackAlignment = B.StackAlignment; |
| 1399 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1400 | if (!DerefBytes) |
| 1401 | DerefBytes = B.DerefBytes; |
| 1402 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1403 | if (!DerefOrNullBytes) |
| 1404 | DerefOrNullBytes = B.DerefOrNullBytes; |
| 1405 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1406 | if (!AllocSizeArgs) |
| 1407 | AllocSizeArgs = B.AllocSizeArgs; |
| 1408 | |
Benjamin Kramer | 45e7d53 | 2013-02-16 19:13:18 +0000 | [diff] [blame] | 1409 | Attrs |= B.Attrs; |
Bill Wendling | e261492 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1410 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1411 | for (auto I : B.td_attrs()) |
| 1412 | TargetDepAttrs[I.first] = I.second; |
Bill Wendling | e261492 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1413 | |
| 1414 | return *this; |
| 1415 | } |
| 1416 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1417 | AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { |
| 1418 | // FIXME: What if both have alignments, but they don't match?! |
| 1419 | if (B.Alignment) |
| 1420 | Alignment = 0; |
| 1421 | |
| 1422 | if (B.StackAlignment) |
| 1423 | StackAlignment = 0; |
| 1424 | |
| 1425 | if (B.DerefBytes) |
| 1426 | DerefBytes = 0; |
| 1427 | |
| 1428 | if (B.DerefOrNullBytes) |
| 1429 | DerefOrNullBytes = 0; |
| 1430 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1431 | if (B.AllocSizeArgs) |
| 1432 | AllocSizeArgs = 0; |
| 1433 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1434 | Attrs &= ~B.Attrs; |
| 1435 | |
| 1436 | for (auto I : B.td_attrs()) |
| 1437 | TargetDepAttrs.erase(I.first); |
| 1438 | |
| 1439 | return *this; |
| 1440 | } |
| 1441 | |
| 1442 | bool AttrBuilder::overlaps(const AttrBuilder &B) const { |
| 1443 | // First check if any of the target independent attributes overlap. |
| 1444 | if ((Attrs & B.Attrs).any()) |
| 1445 | return true; |
| 1446 | |
| 1447 | // Then check if any target dependent ones do. |
| 1448 | for (auto I : td_attrs()) |
| 1449 | if (B.contains(I.first)) |
| 1450 | return true; |
| 1451 | |
| 1452 | return false; |
| 1453 | } |
| 1454 | |
Bill Wendling | 4b00144 | 2013-02-06 01:33:42 +0000 | [diff] [blame] | 1455 | bool AttrBuilder::contains(StringRef A) const { |
| 1456 | return TargetDepAttrs.find(A) != TargetDepAttrs.end(); |
| 1457 | } |
| 1458 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1459 | bool AttrBuilder::hasAttributes() const { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1460 | return !Attrs.none() || !TargetDepAttrs.empty(); |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 1461 | } |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 1462 | |
Bill Wendling | d219675 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 1463 | bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1464 | unsigned Slot = ~0U; |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1465 | for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) |
| 1466 | if (A.getSlotIndex(I) == Index) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1467 | Slot = I; |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1468 | break; |
| 1469 | } |
| 1470 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1471 | assert(Slot != ~0U && "Couldn't find the index!"); |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1472 | |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1473 | for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) { |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1474 | Attribute Attr = *I; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 1475 | if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1476 | if (Attrs[I->getKindAsEnum()]) |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1477 | return true; |
| 1478 | } else { |
| 1479 | assert(Attr.isStringAttribute() && "Invalid attribute kind!"); |
| 1480 | return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end(); |
| 1481 | } |
| 1482 | } |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1483 | |
| 1484 | return false; |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1485 | } |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 1486 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1487 | bool AttrBuilder::hasAlignmentAttr() const { |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1488 | return Alignment != 0; |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1491 | bool AttrBuilder::operator==(const AttrBuilder &B) { |
Benjamin Kramer | 45e7d53 | 2013-02-16 19:13:18 +0000 | [diff] [blame] | 1492 | if (Attrs != B.Attrs) |
| 1493 | return false; |
Bill Wendling | 4b00144 | 2013-02-06 01:33:42 +0000 | [diff] [blame] | 1494 | |
| 1495 | for (td_const_iterator I = TargetDepAttrs.begin(), |
| 1496 | E = TargetDepAttrs.end(); I != E; ++I) |
| 1497 | if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) |
| 1498 | return false; |
| 1499 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1500 | return Alignment == B.Alignment && StackAlignment == B.StackAlignment && |
| 1501 | DerefBytes == B.DerefBytes; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) { |
Bill Wendling | 56aeccc | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1505 | // FIXME: Remove this in 4.0. |
Bill Wendling | 60011b8 | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 1506 | if (!Val) return *this; |
| 1507 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1508 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| 1509 | I = Attribute::AttrKind(I + 1)) { |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1510 | if (I == Attribute::Dereferenceable || |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 1511 | I == Attribute::DereferenceableOrNull || |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1512 | I == Attribute::ArgMemOnly || |
| 1513 | I == Attribute::AllocSize) |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1514 | continue; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1515 | if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1516 | Attrs[I] = true; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1517 | |
| 1518 | if (I == Attribute::Alignment) |
| 1519 | Alignment = 1ULL << ((A >> 16) - 1); |
| 1520 | else if (I == Attribute::StackAlignment) |
| 1521 | StackAlignment = 1ULL << ((A >> 26)-1); |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | return *this; |
| 1526 | } |
| 1527 | |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1528 | //===----------------------------------------------------------------------===// |
| 1529 | // AttributeFuncs Function Defintions |
| 1530 | //===----------------------------------------------------------------------===// |
| 1531 | |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 1532 | /// \brief Which attributes cannot be applied to a type. |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1533 | AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1534 | AttrBuilder Incompatible; |
| 1535 | |
| 1536 | if (!Ty->isIntegerTy()) |
| 1537 | // Attribute that only apply to integers. |
| 1538 | Incompatible.addAttribute(Attribute::SExt) |
| 1539 | .addAttribute(Attribute::ZExt); |
| 1540 | |
| 1541 | if (!Ty->isPointerTy()) |
| 1542 | // Attribute that only apply to pointers. |
| 1543 | Incompatible.addAttribute(Attribute::ByVal) |
| 1544 | .addAttribute(Attribute::Nest) |
| 1545 | .addAttribute(Attribute::NoAlias) |
| 1546 | .addAttribute(Attribute::NoCapture) |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1547 | .addAttribute(Attribute::NonNull) |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1548 | .addDereferenceableAttr(1) // the int here is ignored |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1549 | .addDereferenceableOrNullAttr(1) // the int here is ignored |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1550 | .addAttribute(Attribute::ReadNone) |
| 1551 | .addAttribute(Attribute::ReadOnly) |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1552 | .addAttribute(Attribute::StructRet) |
| 1553 | .addAttribute(Attribute::InAlloca); |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1554 | |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 1555 | return Incompatible; |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1556 | } |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1557 | |
| 1558 | template<typename AttrClass> |
| 1559 | static bool isEqual(const Function &Caller, const Function &Callee) { |
| 1560 | return Caller.getFnAttribute(AttrClass::getKind()) == |
| 1561 | Callee.getFnAttribute(AttrClass::getKind()); |
| 1562 | } |
| 1563 | |
| 1564 | /// \brief Compute the logical AND of the attributes of the caller and the |
| 1565 | /// callee. |
| 1566 | /// |
| 1567 | /// This function sets the caller's attribute to false if the callee's attribute |
| 1568 | /// is false. |
| 1569 | template<typename AttrClass> |
| 1570 | static void setAND(Function &Caller, const Function &Callee) { |
| 1571 | if (AttrClass::isSet(Caller, AttrClass::getKind()) && |
| 1572 | !AttrClass::isSet(Callee, AttrClass::getKind())) |
| 1573 | AttrClass::set(Caller, AttrClass::getKind(), false); |
| 1574 | } |
| 1575 | |
| 1576 | /// \brief Compute the logical OR of the attributes of the caller and the |
| 1577 | /// callee. |
| 1578 | /// |
| 1579 | /// This function sets the caller's attribute to true if the callee's attribute |
| 1580 | /// is true. |
| 1581 | template<typename AttrClass> |
| 1582 | static void setOR(Function &Caller, const Function &Callee) { |
| 1583 | if (!AttrClass::isSet(Caller, AttrClass::getKind()) && |
| 1584 | AttrClass::isSet(Callee, AttrClass::getKind())) |
| 1585 | AttrClass::set(Caller, AttrClass::getKind(), true); |
| 1586 | } |
| 1587 | |
| 1588 | /// \brief If the inlined function had a higher stack protection level than the |
| 1589 | /// calling function, then bump up the caller's stack protection level. |
| 1590 | static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { |
| 1591 | // If upgrading the SSP attribute, clear out the old SSP Attributes first. |
| 1592 | // Having multiple SSP attributes doesn't actually hurt, but it adds useless |
| 1593 | // clutter to the IR. |
| 1594 | AttrBuilder B; |
| 1595 | B.addAttribute(Attribute::StackProtect) |
| 1596 | .addAttribute(Attribute::StackProtectStrong) |
| 1597 | .addAttribute(Attribute::StackProtectReq); |
| 1598 | AttributeSet OldSSPAttr = AttributeSet::get(Caller.getContext(), |
| 1599 | AttributeSet::FunctionIndex, |
| 1600 | B); |
| 1601 | |
Evgeniy Stepanov | f17120a | 2016-04-11 22:27:48 +0000 | [diff] [blame] | 1602 | if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1603 | Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); |
| 1604 | Caller.addFnAttr(Attribute::StackProtectReq); |
| 1605 | } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1606 | !Caller.hasFnAttribute(Attribute::StackProtectReq)) { |
| 1607 | Caller.removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); |
| 1608 | Caller.addFnAttr(Attribute::StackProtectStrong); |
| 1609 | } else if (Callee.hasFnAttribute(Attribute::StackProtect) && |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1610 | !Caller.hasFnAttribute(Attribute::StackProtectReq) && |
| 1611 | !Caller.hasFnAttribute(Attribute::StackProtectStrong)) |
| 1612 | Caller.addFnAttr(Attribute::StackProtect); |
| 1613 | } |
| 1614 | |
| 1615 | #define GET_ATTR_COMPAT_FUNC |
| 1616 | #include "AttributesCompatFunc.inc" |
| 1617 | |
| 1618 | bool AttributeFuncs::areInlineCompatible(const Function &Caller, |
| 1619 | const Function &Callee) { |
| 1620 | return hasCompatibleFnAttrs(Caller, Callee); |
| 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | void AttributeFuncs::mergeAttributesForInlining(Function &Caller, |
| 1625 | const Function &Callee) { |
| 1626 | mergeFnAttrs(Caller, Callee); |
| 1627 | } |