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