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