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