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