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