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