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