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