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