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