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