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