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