Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 1 | //===-- Attributes.cpp - Implement AttributesList -------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 10 | // \file |
| 11 | // \brief This file implements the Attribute, AttributeImpl, AttrBuilder, |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 12 | // AttributeSetImpl, and AttributeSet classes. |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Attributes.h" |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 17 | #include "AttributeImpl.h" |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 18 | #include "LLVMContextImpl.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Type.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Atomic.h" |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mutex.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Bill Wendling | 817abdd | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 30 | // Attribute Construction Methods |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Chris Lattner | fabfde3 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 32 | |
Bill Wendling | 6bdbf06 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 33 | Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) { |
Bill Wendling | 16f9566 | 2013-01-27 10:28:39 +0000 | [diff] [blame] | 34 | AttrBuilder B; |
Bill Wendling | 6bdbf06 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 35 | return Attribute::get(Context, B.addAttribute(Kind)); |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 36 | } |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 37 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 38 | Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) { |
| 39 | // If there are no attributes, return an empty Attribute class. |
Bill Wendling | a5c699d | 2012-10-16 05:55:09 +0000 | [diff] [blame] | 40 | if (!B.hasAttributes()) |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 41 | return Attribute(); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 42 | |
| 43 | // Otherwise, build a key to look up the existing attributes. |
| 44 | LLVMContextImpl *pImpl = Context.pImpl; |
| 45 | FoldingSetNodeID ID; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 46 | ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), B.Raw()); |
| 47 | ID.AddPointer(CI); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 48 | |
| 49 | void *InsertPoint; |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 50 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 51 | |
| 52 | if (!PA) { |
| 53 | // If we didn't find any existing attributes of the same shape then create a |
| 54 | // new one and insert it. |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 55 | PA = new AttributeImpl(Context, CI); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 56 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 57 | } |
| 58 | |
| 59 | // Return the AttributesList that we found or created. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 60 | return Attribute(PA); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Bill Wendling | c08a5ef | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 63 | Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) { |
| 64 | AttrBuilder B; |
| 65 | return get(Context, B.addAlignmentAttr(Align)); |
| 66 | } |
| 67 | |
| 68 | Attribute Attribute::getWithStackAlignment(LLVMContext &Context, |
| 69 | uint64_t Align) { |
| 70 | AttrBuilder B; |
| 71 | return get(Context, B.addStackAlignmentAttr(Align)); |
| 72 | } |
| 73 | |
Bill Wendling | 817abdd | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // Attribute Accessor Methods |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 78 | bool Attribute::hasAttribute(AttrKind Val) const { |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 79 | return pImpl && pImpl->hasAttribute(Val); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 82 | bool Attribute::hasAttributes() const { |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 83 | return pImpl && pImpl->hasAttributes(); |
Bill Wendling | 05cc40d | 2012-10-15 05:40:12 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 86 | /// This returns the alignment field of an attribute as a byte alignment value. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 87 | unsigned Attribute::getAlignment() const { |
| 88 | if (!hasAttribute(Attribute::Alignment)) |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 89 | return 0; |
Bill Wendling | a8ab5fc | 2013-01-23 23:00:05 +0000 | [diff] [blame] | 90 | return pImpl->getAlignment(); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /// This returns the stack alignment field of an attribute as a byte alignment |
| 94 | /// value. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 95 | unsigned Attribute::getStackAlignment() const { |
| 96 | if (!hasAttribute(Attribute::StackAlignment)) |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 97 | return 0; |
Bill Wendling | a8ab5fc | 2013-01-23 23:00:05 +0000 | [diff] [blame] | 98 | return pImpl->getStackAlignment(); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 101 | std::string Attribute::getAsString() const { |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 102 | std::string Result; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 103 | if (hasAttribute(Attribute::ZExt)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 104 | Result += "zeroext "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 105 | if (hasAttribute(Attribute::SExt)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 106 | Result += "signext "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 107 | if (hasAttribute(Attribute::NoReturn)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 108 | Result += "noreturn "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 109 | if (hasAttribute(Attribute::NoUnwind)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 110 | Result += "nounwind "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 111 | if (hasAttribute(Attribute::UWTable)) |
Rafael Espindola | fc2bb8c | 2011-05-25 03:44:17 +0000 | [diff] [blame] | 112 | Result += "uwtable "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 113 | if (hasAttribute(Attribute::ReturnsTwice)) |
Rafael Espindola | 25456ef | 2011-10-03 14:45:37 +0000 | [diff] [blame] | 114 | Result += "returns_twice "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 115 | if (hasAttribute(Attribute::InReg)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 116 | Result += "inreg "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 117 | if (hasAttribute(Attribute::NoAlias)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 118 | Result += "noalias "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 119 | if (hasAttribute(Attribute::NoCapture)) |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 120 | Result += "nocapture "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 121 | if (hasAttribute(Attribute::StructRet)) |
Anton Korobeynikov | c5ec8a7 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 122 | Result += "sret "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 123 | if (hasAttribute(Attribute::ByVal)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 124 | Result += "byval "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 125 | if (hasAttribute(Attribute::Nest)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 126 | Result += "nest "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 127 | if (hasAttribute(Attribute::ReadNone)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 128 | Result += "readnone "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 129 | if (hasAttribute(Attribute::ReadOnly)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 130 | Result += "readonly "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 131 | if (hasAttribute(Attribute::OptimizeForSize)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 132 | Result += "optsize "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 133 | if (hasAttribute(Attribute::NoInline)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 134 | Result += "noinline "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 135 | if (hasAttribute(Attribute::InlineHint)) |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 136 | Result += "inlinehint "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 137 | if (hasAttribute(Attribute::AlwaysInline)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 138 | Result += "alwaysinline "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 139 | if (hasAttribute(Attribute::StackProtect)) |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 140 | Result += "ssp "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 141 | if (hasAttribute(Attribute::StackProtectReq)) |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 142 | Result += "sspreq "; |
Bill Wendling | 114baee | 2013-01-23 06:41:41 +0000 | [diff] [blame] | 143 | if (hasAttribute(Attribute::StackProtectStrong)) |
| 144 | Result += "sspstrong "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 145 | if (hasAttribute(Attribute::NoRedZone)) |
Devang Patel | d18e31a | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 146 | Result += "noredzone "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 147 | if (hasAttribute(Attribute::NoImplicitFloat)) |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 148 | Result += "noimplicitfloat "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 149 | if (hasAttribute(Attribute::Naked)) |
Anton Korobeynikov | c5ec8a7 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 150 | Result += "naked "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 151 | if (hasAttribute(Attribute::NonLazyBind)) |
John McCall | 3a3465b | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 152 | Result += "nonlazybind "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 153 | if (hasAttribute(Attribute::AddressSafety)) |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 154 | Result += "address_safety "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 155 | if (hasAttribute(Attribute::MinSize)) |
Quentin Colombet | 9a419f6 | 2012-10-30 16:32:52 +0000 | [diff] [blame] | 156 | Result += "minsize "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 157 | if (hasAttribute(Attribute::StackAlignment)) { |
Charles Davis | 1e063d1 | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 158 | Result += "alignstack("; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 159 | Result += utostr(getStackAlignment()); |
Charles Davis | 1e063d1 | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 160 | Result += ") "; |
| 161 | } |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 162 | if (hasAttribute(Attribute::Alignment)) { |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 163 | Result += "align "; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 164 | Result += utostr(getAlignment()); |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 165 | Result += " "; |
| 166 | } |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 167 | if (hasAttribute(Attribute::NoDuplicate)) |
| 168 | Result += "noduplicate "; |
Dan Gohman | c3be0fd | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 169 | // Trim the trailing space. |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 170 | assert(!Result.empty() && "Unknown attribute!"); |
Dan Gohman | c3be0fd | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 171 | Result.erase(Result.end()-1); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 172 | return Result; |
| 173 | } |
| 174 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 175 | bool Attribute::operator==(AttrKind K) const { |
| 176 | return pImpl && *pImpl == K; |
| 177 | } |
| 178 | bool Attribute::operator!=(AttrKind K) const { |
| 179 | return !(*this == K); |
| 180 | } |
| 181 | |
| 182 | bool Attribute::operator<(Attribute A) const { |
| 183 | if (!pImpl && !A.pImpl) return false; |
| 184 | if (!pImpl) return true; |
| 185 | if (!A.pImpl) return false; |
| 186 | return *pImpl < *A.pImpl; |
| 187 | } |
| 188 | |
| 189 | uint64_t Attribute::Raw() const { |
| 190 | return pImpl ? pImpl->Raw() : 0; |
| 191 | } |
| 192 | |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | // AttributeImpl Definition |
| 195 | //===----------------------------------------------------------------------===// |
| 196 | |
| 197 | AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data) |
| 198 | : Context(C) { |
| 199 | Data = ConstantInt::get(Type::getInt64Ty(C), data); |
| 200 | } |
| 201 | AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data, |
| 202 | ArrayRef<Constant*> values) |
| 203 | : Context(C) { |
| 204 | Data = ConstantInt::get(Type::getInt64Ty(C), data); |
| 205 | Vals.reserve(values.size()); |
| 206 | Vals.append(values.begin(), values.end()); |
| 207 | } |
| 208 | AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data) |
| 209 | : Context(C) { |
| 210 | Data = ConstantDataArray::getString(C, data); |
| 211 | } |
| 212 | |
| 213 | bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { |
| 214 | return (Raw() & getAttrMask(A)) != 0; |
| 215 | } |
| 216 | |
| 217 | bool AttributeImpl::hasAttributes() const { |
| 218 | return Raw() != 0; |
| 219 | } |
| 220 | |
| 221 | uint64_t AttributeImpl::getAlignment() const { |
| 222 | uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment); |
| 223 | return 1ULL << ((Mask >> 16) - 1); |
| 224 | } |
| 225 | |
| 226 | uint64_t AttributeImpl::getStackAlignment() const { |
| 227 | uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment); |
| 228 | return 1ULL << ((Mask >> 26) - 1); |
| 229 | } |
| 230 | |
| 231 | bool AttributeImpl::operator==(Attribute::AttrKind Kind) const { |
| 232 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Data)) |
| 233 | return CI->getZExtValue() == Kind; |
| 234 | return false; |
| 235 | } |
| 236 | bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const { |
| 237 | return !(*this == Kind); |
| 238 | } |
| 239 | |
| 240 | bool AttributeImpl::operator==(StringRef Kind) const { |
| 241 | if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data)) |
| 242 | if (CDA->isString()) |
| 243 | return CDA->getAsString() == Kind; |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | bool AttributeImpl::operator!=(StringRef Kind) const { |
| 248 | return !(*this == Kind); |
| 249 | } |
| 250 | |
| 251 | bool AttributeImpl::operator<(const AttributeImpl &AI) const { |
| 252 | if (!Data && !AI.Data) return false; |
| 253 | if (!Data && AI.Data) return true; |
| 254 | if (Data && !AI.Data) return false; |
| 255 | |
| 256 | ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data); |
| 257 | ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data); |
| 258 | |
| 259 | ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data); |
| 260 | ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data); |
| 261 | |
| 262 | if (ThisCI && ThatCI) |
| 263 | return ThisCI->getZExtValue() < ThatCI->getZExtValue(); |
| 264 | |
| 265 | if (ThisCI && ThatCDA) |
| 266 | return true; |
| 267 | |
| 268 | if (ThisCDA && ThatCI) |
| 269 | return false; |
| 270 | |
| 271 | return ThisCDA->getAsString() < ThatCDA->getAsString(); |
| 272 | } |
| 273 | |
| 274 | uint64_t AttributeImpl::Raw() const { |
| 275 | // FIXME: Remove this. |
| 276 | return cast<ConstantInt>(Data)->getZExtValue(); |
| 277 | } |
| 278 | |
| 279 | uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) { |
| 280 | // FIXME: Remove this. |
| 281 | switch (Val) { |
| 282 | case Attribute::EndAttrKinds: |
| 283 | case Attribute::AttrKindEmptyKey: |
| 284 | case Attribute::AttrKindTombstoneKey: |
| 285 | llvm_unreachable("Synthetic enumerators which should never get here"); |
| 286 | |
| 287 | case Attribute::None: return 0; |
| 288 | case Attribute::ZExt: return 1 << 0; |
| 289 | case Attribute::SExt: return 1 << 1; |
| 290 | case Attribute::NoReturn: return 1 << 2; |
| 291 | case Attribute::InReg: return 1 << 3; |
| 292 | case Attribute::StructRet: return 1 << 4; |
| 293 | case Attribute::NoUnwind: return 1 << 5; |
| 294 | case Attribute::NoAlias: return 1 << 6; |
| 295 | case Attribute::ByVal: return 1 << 7; |
| 296 | case Attribute::Nest: return 1 << 8; |
| 297 | case Attribute::ReadNone: return 1 << 9; |
| 298 | case Attribute::ReadOnly: return 1 << 10; |
| 299 | case Attribute::NoInline: return 1 << 11; |
| 300 | case Attribute::AlwaysInline: return 1 << 12; |
| 301 | case Attribute::OptimizeForSize: return 1 << 13; |
| 302 | case Attribute::StackProtect: return 1 << 14; |
| 303 | case Attribute::StackProtectReq: return 1 << 15; |
| 304 | case Attribute::Alignment: return 31 << 16; |
| 305 | case Attribute::NoCapture: return 1 << 21; |
| 306 | case Attribute::NoRedZone: return 1 << 22; |
| 307 | case Attribute::NoImplicitFloat: return 1 << 23; |
| 308 | case Attribute::Naked: return 1 << 24; |
| 309 | case Attribute::InlineHint: return 1 << 25; |
| 310 | case Attribute::StackAlignment: return 7 << 26; |
| 311 | case Attribute::ReturnsTwice: return 1 << 29; |
| 312 | case Attribute::UWTable: return 1 << 30; |
| 313 | case Attribute::NonLazyBind: return 1U << 31; |
| 314 | case Attribute::AddressSafety: return 1ULL << 32; |
| 315 | case Attribute::MinSize: return 1ULL << 33; |
| 316 | case Attribute::NoDuplicate: return 1ULL << 34; |
| 317 | case Attribute::StackProtectStrong: return 1ULL << 35; |
| 318 | } |
| 319 | llvm_unreachable("Unsupported attribute type"); |
| 320 | } |
| 321 | |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | // AttributeSetNode Definition |
| 324 | //===----------------------------------------------------------------------===// |
| 325 | |
| 326 | AttributeSetNode *AttributeSetNode::get(LLVMContext &C, |
| 327 | ArrayRef<Attribute> Attrs) { |
| 328 | if (Attrs.empty()) |
| 329 | return 0; |
| 330 | |
| 331 | // Otherwise, build a key to look up the existing attributes. |
| 332 | LLVMContextImpl *pImpl = C.pImpl; |
| 333 | FoldingSetNodeID ID; |
| 334 | |
| 335 | SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); |
| 336 | std::sort(SortedAttrs.begin(), SortedAttrs.end()); |
| 337 | |
| 338 | for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(), |
| 339 | E = SortedAttrs.end(); I != E; ++I) |
| 340 | I->Profile(ID); |
| 341 | |
| 342 | void *InsertPoint; |
| 343 | AttributeSetNode *PA = |
| 344 | pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); |
| 345 | |
| 346 | // If we didn't find any existing attributes of the same shape then create a |
| 347 | // new one and insert it. |
| 348 | if (!PA) { |
| 349 | PA = new AttributeSetNode(SortedAttrs); |
| 350 | pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); |
| 351 | } |
| 352 | |
| 353 | // Return the AttributesListNode that we found or created. |
| 354 | return PA; |
| 355 | } |
| 356 | |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | // AttributeSetImpl Definition |
| 359 | //===----------------------------------------------------------------------===// |
| 360 | |
| 361 | uint64_t AttributeSetImpl::Raw(uint64_t Index) const { |
| 362 | for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) { |
| 363 | if (getSlotIndex(I) != Index) continue; |
| 364 | const AttributeSetNode *ASN = AttrNodes[I].second; |
| 365 | AttrBuilder B; |
| 366 | |
| 367 | for (AttributeSetNode::const_iterator II = ASN->begin(), |
| 368 | IE = ASN->end(); II != IE; ++II) |
| 369 | B.addAttributes(*II); |
| 370 | return B.Raw(); |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | // AttributeSet Construction and Mutation Methods |
| 378 | //===----------------------------------------------------------------------===// |
| 379 | |
| 380 | AttributeSet AttributeSet::getImpl(LLVMContext &C, |
| 381 | ArrayRef<std::pair<unsigned, |
| 382 | AttributeSetNode*> > Attrs) { |
| 383 | LLVMContextImpl *pImpl = C.pImpl; |
| 384 | FoldingSetNodeID ID; |
| 385 | AttributeSetImpl::Profile(ID, Attrs); |
| 386 | |
| 387 | void *InsertPoint; |
| 388 | AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); |
| 389 | |
| 390 | // If we didn't find any existing attributes of the same shape then |
| 391 | // create a new one and insert it. |
| 392 | if (!PA) { |
| 393 | PA = new AttributeSetImpl(C, Attrs); |
| 394 | pImpl->AttrsLists.InsertNode(PA, InsertPoint); |
| 395 | } |
| 396 | |
| 397 | // Return the AttributesList that we found or created. |
| 398 | return AttributeSet(PA); |
| 399 | } |
| 400 | |
| 401 | AttributeSet AttributeSet::get(LLVMContext &C, |
| 402 | ArrayRef<std::pair<unsigned, Attribute> > Attrs){ |
| 403 | // If there are no attributes then return a null AttributesList pointer. |
| 404 | if (Attrs.empty()) |
| 405 | return AttributeSet(); |
| 406 | |
| 407 | #ifndef NDEBUG |
| 408 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
| 409 | assert((!i || Attrs[i-1].first <= Attrs[i].first) && |
| 410 | "Misordered Attributes list!"); |
| 411 | assert(Attrs[i].second.hasAttributes() && |
| 412 | "Pointless attribute!"); |
| 413 | } |
| 414 | #endif |
| 415 | |
| 416 | // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes |
| 417 | // list. |
| 418 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec; |
| 419 | for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(), |
| 420 | E = Attrs.end(); I != E; ) { |
| 421 | unsigned Index = I->first; |
| 422 | SmallVector<Attribute, 4> AttrVec; |
| 423 | while (I->first == Index && I != E) { |
| 424 | AttrVec.push_back(I->second); |
| 425 | ++I; |
| 426 | } |
| 427 | |
| 428 | AttrPairVec.push_back(std::make_pair(Index, |
| 429 | AttributeSetNode::get(C, AttrVec))); |
| 430 | } |
| 431 | |
| 432 | return getImpl(C, AttrPairVec); |
| 433 | } |
| 434 | |
| 435 | AttributeSet AttributeSet::get(LLVMContext &C, |
| 436 | ArrayRef<std::pair<unsigned, |
| 437 | AttributeSetNode*> > Attrs) { |
| 438 | // If there are no attributes then return a null AttributesList pointer. |
| 439 | if (Attrs.empty()) |
| 440 | return AttributeSet(); |
| 441 | |
| 442 | return getImpl(C, Attrs); |
| 443 | } |
| 444 | |
| 445 | AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) { |
| 446 | if (!B.hasAttributes()) |
| 447 | return AttributeSet(); |
Bill Wendling | 8fbc0c2 | 2013-01-29 01:02:03 +0000 | [diff] [blame^] | 448 | |
| 449 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
| 450 | for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 451 | Attribute::AttrKind Kind = *I; |
| 452 | if (Kind == Attribute::Alignment) |
| 453 | Attrs.push_back(std::make_pair(Idx, Attribute:: |
| 454 | getWithAlignment(C, B.getAlignment()))); |
| 455 | else if (Kind == Attribute::StackAlignment) |
| 456 | Attrs.push_back(std::make_pair(Idx, Attribute:: |
| 457 | getWithStackAlignment(C, B.getStackAlignment()))); |
| 458 | else |
| 459 | Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind))); |
| 460 | } |
| 461 | |
| 462 | return get(C, Attrs); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, |
| 466 | ArrayRef<Attribute::AttrKind> Kind) { |
| 467 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
| 468 | for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(), |
| 469 | E = Kind.end(); I != E; ++I) |
| 470 | Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I))); |
| 471 | return get(C, Attrs); |
| 472 | } |
| 473 | |
| 474 | AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) { |
| 475 | if (Attrs.empty()) return AttributeSet(); |
| 476 | |
| 477 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec; |
| 478 | for (unsigned I = 0, E = Attrs.size(); I != E; ++I) { |
| 479 | AttributeSet AS = Attrs[I]; |
| 480 | if (!AS.pImpl) continue; |
| 481 | AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end()); |
| 482 | } |
| 483 | |
| 484 | return getImpl(C, AttrNodeVec); |
| 485 | } |
| 486 | |
| 487 | AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx, |
| 488 | Attribute::AttrKind Attr) const { |
| 489 | return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr)); |
| 490 | } |
| 491 | |
| 492 | AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx, |
| 493 | AttributeSet Attrs) const { |
| 494 | if (!pImpl) return Attrs; |
| 495 | if (!Attrs.pImpl) return *this; |
| 496 | |
| 497 | #ifndef NDEBUG |
| 498 | // FIXME it is not obvious how this should work for alignment. For now, say |
| 499 | // we can't change a known alignment. |
| 500 | unsigned OldAlign = getParamAlignment(Idx); |
| 501 | unsigned NewAlign = Attrs.getParamAlignment(Idx); |
| 502 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
| 503 | "Attempt to change alignment!"); |
| 504 | #endif |
| 505 | |
| 506 | // Add the attribute slots before the one we're trying to add. |
| 507 | SmallVector<AttributeSet, 4> AttrSet; |
| 508 | uint64_t NumAttrs = pImpl->getNumAttributes(); |
| 509 | AttributeSet AS; |
| 510 | uint64_t LastIndex = 0; |
| 511 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
| 512 | if (getSlotIndex(I) >= Idx) { |
| 513 | if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++); |
| 514 | break; |
| 515 | } |
| 516 | LastIndex = I + 1; |
| 517 | AttrSet.push_back(getSlotAttributes(I)); |
| 518 | } |
| 519 | |
| 520 | // Now add the attribute into the correct slot. There may already be an |
| 521 | // AttributeSet there. |
| 522 | AttrBuilder B(AS, Idx); |
| 523 | |
| 524 | for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) |
| 525 | if (Attrs.getSlotIndex(I) == Idx) { |
| 526 | for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I), |
| 527 | IE = Attrs.pImpl->end(I); II != IE; ++II) |
| 528 | B.addAttributes(*II); |
| 529 | break; |
| 530 | } |
| 531 | |
| 532 | AttrSet.push_back(AttributeSet::get(C, Idx, B)); |
| 533 | |
| 534 | // Add the remaining attribute slots. |
| 535 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 536 | AttrSet.push_back(getSlotAttributes(I)); |
| 537 | |
| 538 | return get(C, AttrSet); |
| 539 | } |
| 540 | |
| 541 | AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx, |
| 542 | Attribute::AttrKind Attr) const { |
| 543 | return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr)); |
| 544 | } |
| 545 | |
| 546 | AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx, |
| 547 | AttributeSet Attrs) const { |
| 548 | if (!pImpl) return AttributeSet(); |
| 549 | if (!Attrs.pImpl) return *this; |
| 550 | |
| 551 | #ifndef NDEBUG |
| 552 | // FIXME it is not obvious how this should work for alignment. |
| 553 | // For now, say we can't pass in alignment, which no current use does. |
| 554 | assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) && |
| 555 | "Attempt to change alignment!"); |
| 556 | #endif |
| 557 | |
| 558 | // Add the attribute slots before the one we're trying to add. |
| 559 | SmallVector<AttributeSet, 4> AttrSet; |
| 560 | uint64_t NumAttrs = pImpl->getNumAttributes(); |
| 561 | AttributeSet AS; |
| 562 | uint64_t LastIndex = 0; |
| 563 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
| 564 | if (getSlotIndex(I) >= Idx) { |
| 565 | if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++); |
| 566 | break; |
| 567 | } |
| 568 | LastIndex = I + 1; |
| 569 | AttrSet.push_back(getSlotAttributes(I)); |
| 570 | } |
| 571 | |
| 572 | // Now add the attribute into the correct slot. There may already be an |
| 573 | // AttributeSet there. |
| 574 | AttrBuilder B(AS, Idx); |
| 575 | |
| 576 | for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) |
| 577 | if (Attrs.getSlotIndex(I) == Idx) { |
| 578 | for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I), |
| 579 | IE = Attrs.pImpl->end(I); II != IE; ++II) |
| 580 | B.removeAttributes(*II); |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | AttrSet.push_back(AttributeSet::get(C, Idx, B)); |
| 585 | |
| 586 | // Add the remaining attribute slots. |
| 587 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 588 | AttrSet.push_back(getSlotAttributes(I)); |
| 589 | |
| 590 | return get(C, AttrSet); |
| 591 | } |
| 592 | |
| 593 | //===----------------------------------------------------------------------===// |
| 594 | // AttributeSet Accessor Methods |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | |
| 597 | AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const { |
| 598 | return pImpl && hasAttributes(Idx) ? |
| 599 | AttributeSet::get(pImpl->getContext(), |
| 600 | ArrayRef<std::pair<unsigned, Attribute> >( |
| 601 | std::make_pair(Idx, getAttributes(Idx)))) : |
| 602 | AttributeSet(); |
| 603 | } |
| 604 | |
| 605 | AttributeSet AttributeSet::getRetAttributes() const { |
| 606 | return pImpl && hasAttributes(ReturnIndex) ? |
| 607 | AttributeSet::get(pImpl->getContext(), |
| 608 | ArrayRef<std::pair<unsigned, Attribute> >( |
| 609 | std::make_pair(ReturnIndex, |
| 610 | getAttributes(ReturnIndex)))) : |
| 611 | AttributeSet(); |
| 612 | } |
| 613 | |
| 614 | AttributeSet AttributeSet::getFnAttributes() const { |
| 615 | return pImpl && hasAttributes(FunctionIndex) ? |
| 616 | AttributeSet::get(pImpl->getContext(), |
| 617 | ArrayRef<std::pair<unsigned, Attribute> >( |
| 618 | std::make_pair(FunctionIndex, |
| 619 | getAttributes(FunctionIndex)))) : |
| 620 | AttributeSet(); |
| 621 | } |
| 622 | |
| 623 | bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{ |
| 624 | return getAttributes(Index).hasAttribute(Kind); |
| 625 | } |
| 626 | |
| 627 | bool AttributeSet::hasAttributes(unsigned Index) const { |
| 628 | return getAttributes(Index).hasAttributes(); |
| 629 | } |
| 630 | |
| 631 | /// \brief Return true if the specified attribute is set for at least one |
| 632 | /// parameter or for the return value. |
| 633 | bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const { |
| 634 | if (pImpl == 0) return false; |
| 635 | |
| 636 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) |
| 637 | for (AttributeSetImpl::const_iterator II = pImpl->begin(I), |
| 638 | IE = pImpl->end(I); II != IE; ++II) |
| 639 | if (II->hasAttribute(Attr)) |
| 640 | return true; |
| 641 | |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | unsigned AttributeSet::getParamAlignment(unsigned Idx) const { |
| 646 | return getAttributes(Idx).getAlignment(); |
| 647 | } |
| 648 | |
| 649 | unsigned AttributeSet::getStackAlignment(unsigned Index) const { |
| 650 | return getAttributes(Index).getStackAlignment(); |
| 651 | } |
| 652 | |
| 653 | std::string AttributeSet::getAsString(unsigned Index) const { |
| 654 | return getAttributes(Index).getAsString(); |
| 655 | } |
| 656 | |
| 657 | /// \brief The attributes for the specified index are returned. |
| 658 | /// |
| 659 | /// FIXME: This shouldn't return 'Attribute'. |
| 660 | Attribute AttributeSet::getAttributes(unsigned Idx) const { |
| 661 | if (pImpl == 0) return Attribute(); |
| 662 | |
| 663 | // Loop through to find the attribute we want. |
| 664 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) { |
| 665 | if (pImpl->getSlotIndex(I) != Idx) continue; |
| 666 | |
| 667 | AttrBuilder B; |
| 668 | for (AttributeSetImpl::const_iterator II = pImpl->begin(I), |
| 669 | IE = pImpl->end(I); II != IE; ++II) |
| 670 | B.addAttributes(*II); |
| 671 | return Attribute::get(pImpl->getContext(), B); |
| 672 | } |
| 673 | |
| 674 | return Attribute(); |
| 675 | } |
| 676 | |
| 677 | //===----------------------------------------------------------------------===// |
| 678 | // AttributeSet Introspection Methods |
| 679 | //===----------------------------------------------------------------------===// |
| 680 | |
| 681 | /// \brief Return the number of slots used in this attribute list. This is the |
| 682 | /// number of arguments that have an attribute set on them (including the |
| 683 | /// function itself). |
| 684 | unsigned AttributeSet::getNumSlots() const { |
| 685 | return pImpl ? pImpl->getNumAttributes() : 0; |
| 686 | } |
| 687 | |
| 688 | uint64_t AttributeSet::getSlotIndex(unsigned Slot) const { |
| 689 | assert(pImpl && Slot < pImpl->getNumAttributes() && |
| 690 | "Slot # out of range!"); |
| 691 | return pImpl->getSlotIndex(Slot); |
| 692 | } |
| 693 | |
| 694 | AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const { |
| 695 | assert(pImpl && Slot < pImpl->getNumAttributes() && |
| 696 | "Slot # out of range!"); |
| 697 | return pImpl->getSlotAttributes(Slot); |
| 698 | } |
| 699 | |
| 700 | uint64_t AttributeSet::Raw(unsigned Index) const { |
| 701 | // FIXME: Remove this. |
| 702 | return pImpl ? pImpl->Raw(Index) : 0; |
| 703 | } |
| 704 | |
| 705 | void AttributeSet::dump() const { |
| 706 | dbgs() << "PAL[\n"; |
| 707 | |
| 708 | for (unsigned i = 0, e = getNumSlots(); i < e; ++i) { |
| 709 | uint64_t Index = getSlotIndex(i); |
| 710 | dbgs() << " { "; |
| 711 | if (Index == ~0U) |
| 712 | dbgs() << "~0U"; |
| 713 | else |
| 714 | dbgs() << Index; |
| 715 | dbgs() << " => " << getAsString(Index) << " }\n"; |
| 716 | } |
| 717 | |
| 718 | dbgs() << "]\n"; |
| 719 | } |
| 720 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 721 | //===----------------------------------------------------------------------===// |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 722 | // AttrBuilder Method Implementations |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 723 | //===----------------------------------------------------------------------===// |
| 724 | |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 725 | AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx) |
| 726 | : Alignment(0), StackAlignment(0) { |
Bill Wendling | ec25898 | 2013-01-27 21:23:46 +0000 | [diff] [blame] | 727 | AttributeSetImpl *pImpl = AS.pImpl; |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 728 | if (!pImpl) return; |
| 729 | |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 730 | AttrBuilder B; |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 731 | |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 732 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) { |
| 733 | if (pImpl->getSlotIndex(I) != Idx) continue; |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 734 | |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 735 | for (AttributeSetNode::const_iterator II = pImpl->begin(I), |
| 736 | IE = pImpl->end(I); II != IE; ++II) |
| 737 | B.addAttributes(*II); |
| 738 | |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | if (!B.hasAttributes()) return; |
| 743 | |
| 744 | uint64_t Mask = B.Raw(); |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 745 | |
Bill Wendling | 956f134 | 2013-01-18 21:11:39 +0000 | [diff] [blame] | 746 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| 747 | I = Attribute::AttrKind(I + 1)) { |
| 748 | if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) { |
| 749 | Attrs.insert(I); |
| 750 | |
| 751 | if (I == Attribute::Alignment) |
| 752 | Alignment = 1ULL << ((A >> 16) - 1); |
| 753 | else if (I == Attribute::StackAlignment) |
| 754 | StackAlignment = 1ULL << ((A >> 26)-1); |
| 755 | } |
| 756 | } |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 759 | void AttrBuilder::clear() { |
| 760 | Attrs.clear(); |
| 761 | Alignment = StackAlignment = 0; |
| 762 | } |
| 763 | |
| 764 | AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { |
| 765 | Attrs.insert(Val); |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 766 | return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 769 | AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { |
| 770 | Attrs.erase(Val); |
| 771 | if (Val == Attribute::Alignment) |
| 772 | Alignment = 0; |
| 773 | else if (Val == Attribute::StackAlignment) |
| 774 | StackAlignment = 0; |
| 775 | |
Bill Wendling | a19a530 | 2012-10-14 04:10:01 +0000 | [diff] [blame] | 776 | return *this; |
| 777 | } |
| 778 | |
Bill Wendling | 49f6060 | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 779 | AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) { |
| 780 | uint64_t Mask = Attr.Raw(); |
| 781 | |
| 782 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| 783 | I = Attribute::AttrKind(I + 1)) |
| 784 | if ((Mask & AttributeImpl::getAttrMask(I)) != 0) |
| 785 | Attrs.insert(I); |
| 786 | |
| 787 | if (Attr.getAlignment()) |
| 788 | Alignment = Attr.getAlignment(); |
| 789 | if (Attr.getStackAlignment()) |
| 790 | StackAlignment = Attr.getStackAlignment(); |
| 791 | return *this; |
| 792 | } |
| 793 | |
| 794 | AttrBuilder &AttrBuilder::removeAttributes(Attribute A) { |
| 795 | uint64_t Mask = A.Raw(); |
| 796 | |
| 797 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| 798 | I = Attribute::AttrKind(I + 1)) { |
| 799 | if (Mask & AttributeImpl::getAttrMask(I)) { |
| 800 | Attrs.erase(I); |
| 801 | |
| 802 | if (I == Attribute::Alignment) |
| 803 | Alignment = 0; |
| 804 | else if (I == Attribute::StackAlignment) |
| 805 | StackAlignment = 0; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | return *this; |
| 810 | } |
| 811 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 812 | AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 813 | if (Align == 0) return *this; |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 814 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 815 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 816 | assert(Align <= 0x40000000 && "Alignment too large."); |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 817 | |
| 818 | Attrs.insert(Attribute::Alignment); |
| 819 | Alignment = Align; |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 820 | return *this; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 823 | AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { |
| 824 | // Default alignment, allow the target to define how to align it. |
| 825 | if (Align == 0) return *this; |
| 826 | |
| 827 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 828 | assert(Align <= 0x100 && "Alignment too large."); |
| 829 | |
| 830 | Attrs.insert(Attribute::StackAlignment); |
| 831 | StackAlignment = Align; |
| 832 | return *this; |
| 833 | } |
| 834 | |
Bill Wendling | 22bd641 | 2013-01-03 01:54:39 +0000 | [diff] [blame] | 835 | bool AttrBuilder::contains(Attribute::AttrKind A) const { |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 836 | return Attrs.count(A); |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 839 | bool AttrBuilder::hasAttributes() const { |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 840 | return !Attrs.empty(); |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 841 | } |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 842 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 843 | bool AttrBuilder::hasAttributes(const Attribute &A) const { |
Bill Wendling | 1db9b69 | 2013-01-09 23:36:50 +0000 | [diff] [blame] | 844 | return Raw() & A.Raw(); |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 845 | } |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 846 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 847 | bool AttrBuilder::hasAlignmentAttr() const { |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 848 | return Alignment != 0; |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 851 | bool AttrBuilder::operator==(const AttrBuilder &B) { |
| 852 | SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end()); |
| 853 | SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end()); |
| 854 | return This == That; |
| 855 | } |
| 856 | |
| 857 | AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) { |
| 858 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| 859 | I = Attribute::AttrKind(I + 1)) { |
| 860 | if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) { |
| 861 | Attrs.insert(I); |
| 862 | |
| 863 | if (I == Attribute::Alignment) |
| 864 | Alignment = 1ULL << ((A >> 16) - 1); |
| 865 | else if (I == Attribute::StackAlignment) |
| 866 | StackAlignment = 1ULL << ((A >> 26)-1); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | return *this; |
| 871 | } |
| 872 | |
Bill Wendling | 1db9b69 | 2013-01-09 23:36:50 +0000 | [diff] [blame] | 873 | uint64_t AttrBuilder::Raw() const { |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 874 | uint64_t Mask = 0; |
| 875 | |
| 876 | for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(), |
| 877 | E = Attrs.end(); I != E; ++I) { |
| 878 | Attribute::AttrKind Kind = *I; |
| 879 | |
| 880 | if (Kind == Attribute::Alignment) |
| 881 | Mask |= (Log2_32(Alignment) + 1) << 16; |
| 882 | else if (Kind == Attribute::StackAlignment) |
| 883 | Mask |= (Log2_32(StackAlignment) + 1) << 26; |
| 884 | else |
| 885 | Mask |= AttributeImpl::getAttrMask(Kind); |
| 886 | } |
| 887 | |
| 888 | return Mask; |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 891 | //===----------------------------------------------------------------------===// |
| 892 | // AttributeFuncs Function Defintions |
| 893 | //===----------------------------------------------------------------------===// |
| 894 | |
| 895 | Attribute AttributeFuncs::typeIncompatible(Type *Ty) { |
| 896 | AttrBuilder Incompatible; |
| 897 | |
| 898 | if (!Ty->isIntegerTy()) |
| 899 | // Attribute that only apply to integers. |
| 900 | Incompatible.addAttribute(Attribute::SExt) |
| 901 | .addAttribute(Attribute::ZExt); |
| 902 | |
| 903 | if (!Ty->isPointerTy()) |
| 904 | // Attribute that only apply to pointers. |
| 905 | Incompatible.addAttribute(Attribute::ByVal) |
| 906 | .addAttribute(Attribute::Nest) |
| 907 | .addAttribute(Attribute::NoAlias) |
| 908 | .addAttribute(Attribute::NoCapture) |
| 909 | .addAttribute(Attribute::StructRet); |
| 910 | |
| 911 | return Attribute::get(Ty->getContext(), Incompatible); |
| 912 | } |
| 913 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 914 | /// \brief This returns an integer containing an encoding of all the LLVM |
| 915 | /// attributes found in the given attribute bitset. Any change to this encoding |
| 916 | /// is a breaking change to bitcode compatibility. |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 917 | uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs, |
| 918 | unsigned Index) { |
| 919 | // FIXME: It doesn't make sense to store the alignment information as an |
| 920 | // expanded out value, we should store it as a log2 value. However, we can't |
| 921 | // just change that here without breaking bitcode compatibility. If this ever |
| 922 | // becomes a problem in practice, we should introduce new tag numbers in the |
| 923 | // bitcode file and have those tags use a more efficiently encoded alignment |
| 924 | // field. |
| 925 | |
| 926 | // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit |
| 927 | // log2 encoded value. Shift the bits above the alignment up by 11 bits. |
| 928 | uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff; |
| 929 | if (Attrs.hasAttribute(Index, Attribute::Alignment)) |
| 930 | EncodedAttrs |= Attrs.getParamAlignment(Index) << 16; |
| 931 | EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11; |
| 932 | return EncodedAttrs; |
| 933 | } |
| 934 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 935 | /// \brief This returns an attribute bitset containing the LLVM attributes that |
| 936 | /// have been decoded from the given integer. This function must stay in sync |
| 937 | /// with 'encodeLLVMAttributesForBitcode'. |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 938 | Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C, |
| 939 | uint64_t EncodedAttrs){ |
| 940 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| 941 | // the bits above 31 down by 11 bits. |
| 942 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| 943 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
| 944 | "Alignment must be a power of two."); |
| 945 | |
| 946 | AttrBuilder B(EncodedAttrs & 0xffff); |
| 947 | if (Alignment) |
| 948 | B.addAlignmentAttr(Alignment); |
| 949 | B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11); |
| 950 | return Attribute::get(C, B); |
| 951 | } |
| 952 | |