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