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