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