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