Devang Patel | 4c758ea | 2008-09-25 21:00:45 +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 | // |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10 | // This file implements the AttributesList class and Attribute utilities. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 14 | #include "llvm/Attributes.h" |
Bill Wendling | 8c3e65d | 2012-10-15 05:40:12 +0000 | [diff] [blame] | 15 | #include "AttributesImpl.h" |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 17 | #include "llvm/Type.h" |
Dan Gohman | fc42961 | 2008-03-10 23:55:07 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/FoldingSet.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Atomic.h" |
| 21 | #include "llvm/Support/Mutex.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" |
Benjamin Kramer | 1a25d73 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 28 | // Attributes Implementation |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Chris Lattner | d0e1f10 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 30 | |
Bill Wendling | 79d45db | 2012-10-15 06:53:28 +0000 | [diff] [blame] | 31 | Attributes::Attributes(AttributesImpl *A) : Attrs(A) {} |
| 32 | |
| 33 | Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {} |
| 34 | |
| 35 | Attributes Attributes::get(LLVMContext &Context, ArrayRef<AttrVal> Vals) { |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 36 | AttrBuilder B; |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 37 | for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end(); |
| 38 | I != E; ++I) |
| 39 | B.addAttribute(*I); |
Bill Wendling | 79d45db | 2012-10-15 06:53:28 +0000 | [diff] [blame] | 40 | return Attributes::get(Context, B); |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 41 | } |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 42 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 43 | Attributes Attributes::get(LLVMContext &Context, AttrBuilder &B) { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 44 | // If there are no attributes, return an empty Attributes class. |
| 45 | if (B.Bits == 0) |
| 46 | return Attributes(); |
| 47 | |
| 48 | // Otherwise, build a key to look up the existing attributes. |
| 49 | LLVMContextImpl *pImpl = Context.pImpl; |
| 50 | FoldingSetNodeID ID; |
| 51 | ID.AddInteger(B.Bits); |
| 52 | |
| 53 | void *InsertPoint; |
| 54 | AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 55 | |
| 56 | if (!PA) { |
| 57 | // If we didn't find any existing attributes of the same shape then create a |
| 58 | // new one and insert it. |
| 59 | PA = new AttributesImpl(B.Bits); |
| 60 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 61 | } |
| 62 | |
| 63 | // Return the AttributesList that we found or created. |
| 64 | return Attributes(PA); |
| 65 | } |
| 66 | |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 67 | bool Attributes::hasAttribute(AttrVal Val) const { |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 68 | return Attrs && Attrs->hasAttribute(Val); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Bill Wendling | 8c3e65d | 2012-10-15 05:40:12 +0000 | [diff] [blame] | 71 | bool Attributes::hasAttributes() const { |
| 72 | return Attrs && Attrs->hasAttributes(); |
| 73 | } |
| 74 | |
Bill Wendling | 93f70b7 | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 75 | bool Attributes::hasAttributes(const Attributes &A) const { |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 76 | return Attrs && Attrs->hasAttributes(A); |
Bill Wendling | 93f70b7 | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 79 | /// This returns the alignment field of an attribute as a byte alignment value. |
| 80 | unsigned Attributes::getAlignment() const { |
Bill Wendling | b372334 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 81 | if (!hasAttribute(Attributes::Alignment)) |
| 82 | return 0; |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 83 | return 1U << ((Attrs->getAlignment() >> 16) - 1); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /// This returns the stack alignment field of an attribute as a byte alignment |
| 87 | /// value. |
| 88 | unsigned Attributes::getStackAlignment() const { |
Bill Wendling | b372334 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 89 | if (!hasAttribute(Attributes::StackAlignment)) |
| 90 | return 0; |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 91 | return 1U << ((Attrs->getStackAlignment() >> 26) - 1); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 94 | uint64_t Attributes::Raw() const { |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 95 | return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly! |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 98 | Attributes Attributes::typeIncompatible(Type *Ty) { |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 99 | AttrBuilder Incompatible; |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 100 | |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 101 | if (!Ty->isIntegerTy()) |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 102 | // Attributes that only apply to integers. |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 103 | Incompatible.addAttribute(Attributes::SExt) |
| 104 | .addAttribute(Attributes::ZExt); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 105 | |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 106 | if (!Ty->isPointerTy()) |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 107 | // Attributes that only apply to pointers. |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 108 | Incompatible.addAttribute(Attributes::ByVal) |
| 109 | .addAttribute(Attributes::Nest) |
| 110 | .addAttribute(Attributes::NoAlias) |
| 111 | .addAttribute(Attributes::NoCapture) |
| 112 | .addAttribute(Attributes::StructRet); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 113 | |
Bill Wendling | d079a44 | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 114 | return Attributes::get(Ty->getContext(), Incompatible); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 117 | /// encodeLLVMAttributesForBitcode - This returns an integer containing an |
| 118 | /// encoding of all the LLVM attributes found in the given attribute bitset. |
| 119 | /// Any change to this encoding is a breaking change to bitcode compatibility. |
| 120 | uint64_t Attributes::encodeLLVMAttributesForBitcode(Attributes Attrs) { |
| 121 | // FIXME: It doesn't make sense to store the alignment information as an |
| 122 | // expanded out value, we should store it as a log2 value. However, we can't |
| 123 | // just change that here without breaking bitcode compatibility. If this ever |
| 124 | // becomes a problem in practice, we should introduce new tag numbers in the |
| 125 | // bitcode file and have those tags use a more efficiently encoded alignment |
| 126 | // field. |
| 127 | |
| 128 | // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit |
| 129 | // log2 encoded value. Shift the bits above the alignment up by 11 bits. |
| 130 | uint64_t EncodedAttrs = Attrs.Raw() & 0xffff; |
| 131 | if (Attrs.hasAttribute(Attributes::Alignment)) |
| 132 | EncodedAttrs |= Attrs.getAlignment() << 16; |
| 133 | EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11; |
| 134 | return EncodedAttrs; |
| 135 | } |
| 136 | |
| 137 | /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing |
| 138 | /// the LLVM attributes that have been decoded from the given integer. This |
| 139 | /// function must stay in sync with 'encodeLLVMAttributesForBitcode'. |
| 140 | Attributes Attributes::decodeLLVMAttributesForBitcode(LLVMContext &C, |
| 141 | uint64_t EncodedAttrs) { |
| 142 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| 143 | // the bits above 31 down by 11 bits. |
| 144 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| 145 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
| 146 | "Alignment must be a power of two."); |
| 147 | |
| 148 | AttrBuilder B(EncodedAttrs & 0xffff); |
| 149 | if (Alignment) |
| 150 | B.addAlignmentAttr(Alignment); |
| 151 | B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11); |
| 152 | return Attributes::get(C, B); |
| 153 | } |
| 154 | |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 155 | std::string Attributes::getAsString() const { |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 156 | std::string Result; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 157 | if (hasAttribute(Attributes::ZExt)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 158 | Result += "zeroext "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 159 | if (hasAttribute(Attributes::SExt)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 160 | Result += "signext "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 161 | if (hasAttribute(Attributes::NoReturn)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 162 | Result += "noreturn "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 163 | if (hasAttribute(Attributes::NoUnwind)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 164 | Result += "nounwind "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 165 | if (hasAttribute(Attributes::UWTable)) |
Rafael Espindola | fc9bae6 | 2011-05-25 03:44:17 +0000 | [diff] [blame] | 166 | Result += "uwtable "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 167 | if (hasAttribute(Attributes::ReturnsTwice)) |
Rafael Espindola | cc349c8 | 2011-10-03 14:45:37 +0000 | [diff] [blame] | 168 | Result += "returns_twice "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 169 | if (hasAttribute(Attributes::InReg)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 170 | Result += "inreg "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 171 | if (hasAttribute(Attributes::NoAlias)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 172 | Result += "noalias "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 173 | if (hasAttribute(Attributes::NoCapture)) |
Nick Lewycky | 8d69f48 | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 174 | Result += "nocapture "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 175 | if (hasAttribute(Attributes::StructRet)) |
Anton Korobeynikov | c8ce7b08 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 176 | Result += "sret "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 177 | if (hasAttribute(Attributes::ByVal)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 178 | Result += "byval "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 179 | if (hasAttribute(Attributes::Nest)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 180 | Result += "nest "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 181 | if (hasAttribute(Attributes::ReadNone)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 182 | Result += "readnone "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 183 | if (hasAttribute(Attributes::ReadOnly)) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 184 | Result += "readonly "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 185 | if (hasAttribute(Attributes::OptimizeForSize)) |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 186 | Result += "optsize "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 187 | if (hasAttribute(Attributes::NoInline)) |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 188 | Result += "noinline "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 189 | if (hasAttribute(Attributes::InlineHint)) |
Jakob Stoklund Olesen | 74bb06c | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 190 | Result += "inlinehint "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 191 | if (hasAttribute(Attributes::AlwaysInline)) |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 192 | Result += "alwaysinline "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 193 | if (hasAttribute(Attributes::StackProtect)) |
Bill Wendling | ccb67a3d | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 194 | Result += "ssp "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 195 | if (hasAttribute(Attributes::StackProtectReq)) |
Bill Wendling | ccb67a3d | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 196 | Result += "sspreq "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 197 | if (hasAttribute(Attributes::NoRedZone)) |
Devang Patel | 72a4d2f | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 198 | Result += "noredzone "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 199 | if (hasAttribute(Attributes::NoImplicitFloat)) |
Devang Patel | d1c7d34 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 200 | Result += "noimplicitfloat "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 201 | if (hasAttribute(Attributes::Naked)) |
Anton Korobeynikov | c8ce7b08 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 202 | Result += "naked "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 203 | if (hasAttribute(Attributes::NonLazyBind)) |
John McCall | 4b7a8d6 | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 204 | Result += "nonlazybind "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 205 | if (hasAttribute(Attributes::AddressSafety)) |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 206 | Result += "address_safety "; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 207 | if (hasAttribute(Attributes::StackAlignment)) { |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 208 | Result += "alignstack("; |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 209 | Result += utostr(getStackAlignment()); |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 210 | Result += ") "; |
| 211 | } |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 212 | if (hasAttribute(Attributes::Alignment)) { |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 213 | Result += "align "; |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 214 | Result += utostr(getAlignment()); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 215 | Result += " "; |
| 216 | } |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 217 | // Trim the trailing space. |
Nick Lewycky | 8d69f48 | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 218 | assert(!Result.empty() && "Unknown attribute!"); |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 219 | Result.erase(Result.end()-1); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 220 | return Result; |
| 221 | } |
| 222 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 223 | //===----------------------------------------------------------------------===// |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 224 | // AttrBuilder Implementation |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 225 | //===----------------------------------------------------------------------===// |
| 226 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 227 | AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){ |
Bill Wendling | 93f70b7 | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 228 | Bits |= AttributesImpl::getAttrMask(Val); |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 229 | return *this; |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 232 | AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) { |
Bill Wendling | 1fcc822 | 2012-10-14 04:10:01 +0000 | [diff] [blame] | 233 | Bits |= Val; |
| 234 | return *this; |
| 235 | } |
| 236 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 237 | AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 238 | if (Align == 0) return *this; |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 239 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 240 | assert(Align <= 0x40000000 && "Alignment too large."); |
| 241 | Bits |= (Log2_32(Align) + 1) << 16; |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 242 | return *this; |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 243 | } |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 244 | AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){ |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 245 | // Default alignment, allow the target to define how to align it. |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 246 | if (Align == 0) return *this; |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 247 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 248 | assert(Align <= 0x100 && "Alignment too large."); |
| 249 | Bits |= (Log2_32(Align) + 1) << 26; |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 250 | return *this; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 253 | AttrBuilder &AttrBuilder:: |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 254 | removeAttribute(Attributes::AttrVal Val) { |
Bill Wendling | 93f70b7 | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 255 | Bits &= ~AttributesImpl::getAttrMask(Val); |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 256 | return *this; |
Bill Wendling | 93f70b7 | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 259 | AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) { |
Bill Wendling | 5c407ed | 2012-10-14 07:17:34 +0000 | [diff] [blame] | 260 | Bits |= A.Raw(); |
| 261 | return *this; |
| 262 | } |
| 263 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 264 | AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){ |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 265 | Bits &= ~A.Raw(); |
Bill Wendling | 85a64c2 | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 266 | return *this; |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 269 | bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const { |
Bill Wendling | bbcdf4e | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 270 | return Bits & AttributesImpl::getAttrMask(A); |
| 271 | } |
| 272 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 273 | bool AttrBuilder::hasAttributes() const { |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 274 | return Bits != 0; |
| 275 | } |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 276 | bool AttrBuilder::hasAttributes(const Attributes &A) const { |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 277 | return Bits & A.Raw(); |
| 278 | } |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 279 | bool AttrBuilder::hasAlignmentAttr() const { |
Bill Wendling | 4caad41 | 2012-10-09 20:28:54 +0000 | [diff] [blame] | 280 | return Bits & AttributesImpl::getAttrMask(Attributes::Alignment); |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 283 | uint64_t AttrBuilder::getAlignment() const { |
Bill Wendling | b372334 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 284 | if (!hasAlignmentAttr()) |
| 285 | return 0; |
Bill Wendling | 4caad41 | 2012-10-09 20:28:54 +0000 | [diff] [blame] | 286 | return 1U << |
| 287 | (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1); |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 290 | uint64_t AttrBuilder::getStackAlignment() const { |
Bill Wendling | bbcdf4e | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 291 | if (!hasAlignmentAttr()) |
| 292 | return 0; |
| 293 | return 1U << |
| 294 | (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1); |
| 295 | } |
| 296 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 297 | //===----------------------------------------------------------------------===// |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 298 | // AttributeImpl Definition |
| 299 | //===----------------------------------------------------------------------===// |
| 300 | |
Bill Wendling | 93f70b7 | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 301 | uint64_t AttributesImpl::getAttrMask(uint64_t Val) { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 302 | switch (Val) { |
| 303 | case Attributes::None: return 0; |
| 304 | case Attributes::ZExt: return 1 << 0; |
| 305 | case Attributes::SExt: return 1 << 1; |
| 306 | case Attributes::NoReturn: return 1 << 2; |
| 307 | case Attributes::InReg: return 1 << 3; |
| 308 | case Attributes::StructRet: return 1 << 4; |
| 309 | case Attributes::NoUnwind: return 1 << 5; |
| 310 | case Attributes::NoAlias: return 1 << 6; |
| 311 | case Attributes::ByVal: return 1 << 7; |
| 312 | case Attributes::Nest: return 1 << 8; |
| 313 | case Attributes::ReadNone: return 1 << 9; |
| 314 | case Attributes::ReadOnly: return 1 << 10; |
| 315 | case Attributes::NoInline: return 1 << 11; |
| 316 | case Attributes::AlwaysInline: return 1 << 12; |
| 317 | case Attributes::OptimizeForSize: return 1 << 13; |
| 318 | case Attributes::StackProtect: return 1 << 14; |
| 319 | case Attributes::StackProtectReq: return 1 << 15; |
| 320 | case Attributes::Alignment: return 31 << 16; |
| 321 | case Attributes::NoCapture: return 1 << 21; |
| 322 | case Attributes::NoRedZone: return 1 << 22; |
| 323 | case Attributes::NoImplicitFloat: return 1 << 23; |
| 324 | case Attributes::Naked: return 1 << 24; |
| 325 | case Attributes::InlineHint: return 1 << 25; |
| 326 | case Attributes::StackAlignment: return 7 << 26; |
| 327 | case Attributes::ReturnsTwice: return 1 << 29; |
| 328 | case Attributes::UWTable: return 1 << 30; |
| 329 | case Attributes::NonLazyBind: return 1U << 31; |
| 330 | case Attributes::AddressSafety: return 1ULL << 32; |
| 331 | } |
| 332 | llvm_unreachable("Unsupported attribute type"); |
| 333 | } |
| 334 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 335 | bool AttributesImpl::hasAttribute(uint64_t A) const { |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 336 | return (Bits & getAttrMask(A)) != 0; |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 337 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 338 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 339 | bool AttributesImpl::hasAttributes() const { |
| 340 | return Bits != 0; |
| 341 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 342 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 343 | bool AttributesImpl::hasAttributes(const Attributes &A) const { |
| 344 | return Bits & A.Raw(); // FIXME: Raw() won't work here in the future. |
| 345 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 346 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 347 | uint64_t AttributesImpl::getAlignment() const { |
Bill Wendling | b372334 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 348 | return Bits & getAttrMask(Attributes::Alignment); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 349 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 350 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 351 | uint64_t AttributesImpl::getStackAlignment() const { |
Bill Wendling | b372334 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 352 | return Bits & getAttrMask(Attributes::StackAlignment); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 353 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 354 | |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 355 | //===----------------------------------------------------------------------===// |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 356 | // AttributeListImpl Definition |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 357 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 358 | |
Owen Anderson | 2e83189 | 2010-11-18 18:59:13 +0000 | [diff] [blame] | 359 | namespace llvm { |
| 360 | class AttributeListImpl; |
| 361 | } |
| 362 | |
| 363 | static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists; |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 365 | namespace llvm { |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 366 | static ManagedStatic<sys::SmartMutex<true> > ALMutex; |
| 367 | |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 368 | class AttributeListImpl : public FoldingSetNode { |
Owen Anderson | c1a3a47 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 369 | sys::cas_flag RefCount; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 370 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 371 | // AttributesList is uniqued, these should not be publicly available. |
Craig Topper | b1d83e8 | 2012-09-18 02:01:41 +0000 | [diff] [blame] | 372 | void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION; |
| 373 | AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION; |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 374 | ~AttributeListImpl(); // Private implementation |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 375 | public: |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 376 | SmallVector<AttributeWithIndex, 4> Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 378 | AttributeListImpl(ArrayRef<AttributeWithIndex> attrs) |
| 379 | : Attrs(attrs.begin(), attrs.end()) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 380 | RefCount = 0; |
| 381 | } |
| 382 | |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 383 | void AddRef() { |
| 384 | sys::SmartScopedLock<true> Lock(*ALMutex); |
| 385 | ++RefCount; |
| 386 | } |
Owen Anderson | c1a3a47 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 387 | void DropRef() { |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 388 | sys::SmartScopedLock<true> Lock(*ALMutex); |
Owen Anderson | 2e83189 | 2010-11-18 18:59:13 +0000 | [diff] [blame] | 389 | if (!AttributesLists.isConstructed()) |
| 390 | return; |
Owen Anderson | 91bfeb1 | 2010-11-09 17:47:10 +0000 | [diff] [blame] | 391 | sys::cas_flag new_val = --RefCount; |
Owen Anderson | 2d33543 | 2010-11-09 17:46:38 +0000 | [diff] [blame] | 392 | if (new_val == 0) |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 393 | delete this; |
Owen Anderson | c1a3a47 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 395 | |
| 396 | void Profile(FoldingSetNodeID &ID) const { |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 397 | Profile(ID, Attrs); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 399 | static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){ |
| 400 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
| 401 | ID.AddInteger(Attrs[i].Attrs.Raw()); |
| 402 | ID.AddInteger(Attrs[i].Index); |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 403 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 404 | } |
| 405 | }; |
| 406 | } |
| 407 | |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 408 | AttributeListImpl::~AttributeListImpl() { |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 409 | // NOTE: Lock must be acquired by caller. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 410 | AttributesLists->RemoveNode(this); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 414 | AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 415 | // If there are no attributes then return a null AttributesList pointer. |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 416 | if (Attrs.empty()) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 417 | return AttrListPtr(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 419 | #ifndef NDEBUG |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 420 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 421 | assert(Attrs[i].Attrs.hasAttributes() && |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 422 | "Pointless attribute!"); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 423 | assert((!i || Attrs[i-1].Index < Attrs[i].Index) && |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 424 | "Misordered AttributesList!"); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 425 | } |
| 426 | #endif |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 428 | // Otherwise, build a key to look up the existing attributes. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 429 | FoldingSetNodeID ID; |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 430 | AttributeListImpl::Profile(ID, Attrs); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 431 | void *InsertPos; |
Owen Anderson | d91e6b0 | 2009-08-17 17:10:58 +0000 | [diff] [blame] | 432 | |
| 433 | sys::SmartScopedLock<true> Lock(*ALMutex); |
| 434 | |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 435 | AttributeListImpl *PAL = |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 436 | AttributesLists->FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 438 | // If we didn't find any existing attributes of the same shape then |
| 439 | // create a new one and insert it. |
| 440 | if (!PAL) { |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 441 | PAL = new AttributeListImpl(Attrs); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 442 | AttributesLists->InsertNode(PAL, InsertPos); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 443 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 444 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 445 | // Return the AttributesList that we found or created. |
| 446 | return AttrListPtr(PAL); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 450 | //===----------------------------------------------------------------------===// |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 451 | // AttrListPtr Method Implementations |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 453 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 454 | AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 455 | if (LI) LI->AddRef(); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 458 | AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) { |
| 459 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 462 | const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) { |
Owen Anderson | 8dc0b04 | 2010-09-16 00:27:35 +0000 | [diff] [blame] | 463 | sys::SmartScopedLock<true> Lock(*ALMutex); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 464 | if (AttrList == RHS.AttrList) return *this; |
| 465 | if (AttrList) AttrList->DropRef(); |
| 466 | AttrList = RHS.AttrList; |
| 467 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 468 | return *this; |
| 469 | } |
| 470 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 471 | AttrListPtr::~AttrListPtr() { |
| 472 | if (AttrList) AttrList->DropRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /// getNumSlots - Return the number of slots used in this attribute list. |
| 476 | /// This is the number of arguments that have an attribute set on them |
| 477 | /// (including the function itself). |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 478 | unsigned AttrListPtr::getNumSlots() const { |
| 479 | return AttrList ? AttrList->Attrs.size() : 0; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 482 | /// getSlot - Return the AttributeWithIndex at the specified slot. This |
| 483 | /// holds a number plus a set of attributes. |
| 484 | const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const { |
| 485 | assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!"); |
| 486 | return AttrList->Attrs[Slot]; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 490 | /// getAttributes - The attributes for the specified index are |
| 491 | /// returned. Attributes for the result are denoted with Idx = 0. |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 492 | /// Function notes are denoted with idx = ~0. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 493 | Attributes AttrListPtr::getAttributes(unsigned Idx) const { |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 494 | if (AttrList == 0) return Attributes(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 495 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 496 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 497 | for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i) |
| 498 | if (Attrs[i].Index == Idx) |
| 499 | return Attrs[i].Attrs; |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 500 | |
| 501 | return Attributes(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /// hasAttrSomewhere - Return true if the specified attribute is set for at |
| 505 | /// least one parameter or for the return value. |
Bill Wendling | bbcdf4e | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 506 | bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 507 | if (AttrList == 0) return false; |
Bill Wendling | bbcdf4e | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 508 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 509 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 510 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) |
Bill Wendling | bbcdf4e | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 511 | if (Attrs[i].Attrs.hasAttribute(Attr)) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 512 | return true; |
| 513 | return false; |
| 514 | } |
| 515 | |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 516 | unsigned AttrListPtr::getNumAttrs() const { |
| 517 | return AttrList ? AttrList->Attrs.size() : 0; |
| 518 | } |
| 519 | |
| 520 | Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const { |
| 521 | assert(AttrList && "Trying to get an attribute from an empty list!"); |
| 522 | assert(i < AttrList->Attrs.size() && "Index out of range!"); |
| 523 | return AttrList->Attrs[i].Attrs; |
| 524 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 525 | |
Bill Wendling | 722b26c | 2012-10-14 07:35:59 +0000 | [diff] [blame] | 526 | AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx, |
| 527 | Attributes Attrs) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 528 | Attributes OldAttrs = getAttributes(Idx); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 529 | #ifndef NDEBUG |
| 530 | // FIXME it is not obvious how this should work for alignment. |
| 531 | // For now, say we can't change a known alignment. |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 532 | unsigned OldAlign = OldAttrs.getAlignment(); |
| 533 | unsigned NewAlign = Attrs.getAlignment(); |
Anton Korobeynikov | 18991d7 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 534 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 535 | "Attempt to change alignment!"); |
| 536 | #endif |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 537 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 538 | AttrBuilder NewAttrs = |
| 539 | AttrBuilder(OldAttrs).addAttributes(Attrs); |
| 540 | if (NewAttrs == AttrBuilder(OldAttrs)) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 541 | return *this; |
| 542 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 543 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 544 | if (AttrList == 0) |
| 545 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 546 | else { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 547 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 548 | unsigned i = 0, e = OldAttrList.size(); |
| 549 | // Copy attributes for arguments before this one. |
| 550 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 551 | NewAttrList.push_back(OldAttrList[i]); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 553 | // If there are attributes already at this index, merge them in. |
| 554 | if (i != e && OldAttrList[i].Index == Idx) { |
Bill Wendling | 722b26c | 2012-10-14 07:35:59 +0000 | [diff] [blame] | 555 | Attrs = |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 556 | Attributes::get(C, AttrBuilder(Attrs). |
Bill Wendling | 722b26c | 2012-10-14 07:35:59 +0000 | [diff] [blame] | 557 | addAttributes(OldAttrList[i].Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 558 | ++i; |
| 559 | } |
| 560 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 561 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 562 | |
| 563 | // Copy attributes for arguments after this one. |
| 564 | NewAttrList.insert(NewAttrList.end(), |
| 565 | OldAttrList.begin()+i, OldAttrList.end()); |
| 566 | } |
| 567 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 568 | return get(NewAttrList); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Bill Wendling | 85a64c2 | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 571 | AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx, |
| 572 | Attributes Attrs) const { |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 573 | #ifndef NDEBUG |
| 574 | // FIXME it is not obvious how this should work for alignment. |
| 575 | // For now, say we can't pass in alignment, which no current use does. |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 576 | assert(!Attrs.hasAttribute(Attributes::Alignment) && |
| 577 | "Attempt to exclude alignment!"); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 578 | #endif |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 579 | if (AttrList == 0) return AttrListPtr(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 580 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 581 | Attributes OldAttrs = getAttributes(Idx); |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 582 | AttrBuilder NewAttrs = |
| 583 | AttrBuilder(OldAttrs).removeAttributes(Attrs); |
| 584 | if (NewAttrs == AttrBuilder(OldAttrs)) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 585 | return *this; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 586 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 587 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 588 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 589 | unsigned i = 0, e = OldAttrList.size(); |
| 590 | |
| 591 | // Copy attributes for arguments before this one. |
| 592 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 593 | NewAttrList.push_back(OldAttrList[i]); |
| 594 | |
| 595 | // If there are attributes already at this index, merge them in. |
| 596 | assert(OldAttrList[i].Index == Idx && "Attribute isn't set?"); |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame^] | 597 | Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs). |
Bill Wendling | 85a64c2 | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 598 | removeAttributes(Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 599 | ++i; |
Bill Wendling | 76d2cd2 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 600 | if (Attrs.hasAttributes()) // If any attributes left for this param, add them. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 601 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 602 | |
| 603 | // Copy attributes for arguments after this one. |
| 604 | NewAttrList.insert(NewAttrList.end(), |
| 605 | OldAttrList.begin()+i, OldAttrList.end()); |
| 606 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 607 | return get(NewAttrList); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 610 | void AttrListPtr::dump() const { |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 611 | dbgs() << "PAL[ "; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 612 | for (unsigned i = 0; i < getNumSlots(); ++i) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 613 | const AttributeWithIndex &PAWI = getSlot(i); |
Bill Wendling | 76d2cd2 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 614 | dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} "; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 615 | } |
| 616 | |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 617 | dbgs() << "]\n"; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 618 | } |