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