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