Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1 | //===-- Attribute.cpp - Implement AttributesList -------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 10 | // This file implements the Attribute, AttributeImpl, AttrBuilder, |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 11 | // AttributeSetImpl, and AttributeSet classes. |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Attributes.h" |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 16 | #include "AttributeImpl.h" |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 17 | #include "LLVMContextImpl.h" |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Type.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Atomic.h" |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mutex.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 29 | // Attribute Implementation |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
Chris Lattner | fabfde3 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 31 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 32 | Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) { |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 33 | AttrBuilder B; |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 34 | for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end(); |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 35 | I != E; ++I) |
| 36 | B.addAttribute(*I); |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 37 | return Attribute::get(Context, B); |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 38 | } |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 39 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 40 | Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) { |
| 41 | // If there are no attributes, return an empty Attribute class. |
Bill Wendling | a5c699d | 2012-10-16 05:55:09 +0000 | [diff] [blame] | 42 | if (!B.hasAttributes()) |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 43 | return Attribute(); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 44 | |
| 45 | // Otherwise, build a key to look up the existing attributes. |
| 46 | LLVMContextImpl *pImpl = Context.pImpl; |
| 47 | FoldingSetNodeID ID; |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 48 | ID.AddInteger(B.getBitMask()); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 49 | |
| 50 | void *InsertPoint; |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 51 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 52 | |
| 53 | if (!PA) { |
| 54 | // If we didn't find any existing attributes of the same shape then create a |
| 55 | // new one and insert it. |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 56 | PA = new AttributeImpl(Context, B.getBitMask()); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 57 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 58 | } |
| 59 | |
| 60 | // Return the AttributesList that we found or created. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 61 | return Attribute(PA); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 64 | bool Attribute::hasAttribute(AttrKind Val) const { |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 65 | return pImpl && pImpl->hasAttribute(Val); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 68 | bool Attribute::hasAttributes() const { |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 69 | return pImpl && pImpl->hasAttributes(); |
Bill Wendling | 05cc40d | 2012-10-15 05:40:12 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 72 | bool Attribute::hasAttributes(const Attribute &A) const { |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 73 | return pImpl && pImpl->hasAttributes(A); |
Bill Wendling | 2e879bc | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 76 | /// 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] | 77 | unsigned Attribute::getAlignment() const { |
| 78 | if (!hasAttribute(Attribute::Alignment)) |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 79 | return 0; |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 80 | return 1U << ((pImpl->getAlignment() >> 16) - 1); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /// This returns the stack alignment field of an attribute as a byte alignment |
| 84 | /// value. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 85 | unsigned Attribute::getStackAlignment() const { |
| 86 | if (!hasAttribute(Attribute::StackAlignment)) |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 87 | return 0; |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 88 | return 1U << ((pImpl->getStackAlignment() >> 26) - 1); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Bill Wendling | 92e287f | 2012-12-31 11:51:54 +0000 | [diff] [blame] | 91 | bool Attribute::operator==(AttrKind K) const { |
Bill Wendling | 22bd641 | 2013-01-03 01:54:39 +0000 | [diff] [blame^] | 92 | return pImpl && pImpl->contains(K); |
Bill Wendling | 92e287f | 2012-12-31 11:51:54 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool Attribute::operator!=(AttrKind K) const { |
Bill Wendling | 22bd641 | 2013-01-03 01:54:39 +0000 | [diff] [blame^] | 96 | return !(pImpl && pImpl->contains(K)); |
Bill Wendling | 92e287f | 2012-12-31 11:51:54 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 99 | uint64_t Attribute::getBitMask() const { |
| 100 | return pImpl ? pImpl->getBitMask() : 0; |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 103 | Attribute Attribute::typeIncompatible(Type *Ty) { |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 104 | AttrBuilder Incompatible; |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 105 | |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 106 | if (!Ty->isIntegerTy()) |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 107 | // Attribute that only apply to integers. |
| 108 | Incompatible.addAttribute(Attribute::SExt) |
| 109 | .addAttribute(Attribute::ZExt); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 110 | |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 111 | if (!Ty->isPointerTy()) |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 112 | // Attribute that only apply to pointers. |
| 113 | Incompatible.addAttribute(Attribute::ByVal) |
| 114 | .addAttribute(Attribute::Nest) |
| 115 | .addAttribute(Attribute::NoAlias) |
| 116 | .addAttribute(Attribute::NoCapture) |
| 117 | .addAttribute(Attribute::StructRet); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 118 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 119 | return Attribute::get(Ty->getContext(), Incompatible); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 122 | /// encodeLLVMAttributesForBitcode - This returns an integer containing an |
| 123 | /// encoding of all the LLVM attributes found in the given attribute bitset. |
| 124 | /// Any change to this encoding is a breaking change to bitcode compatibility. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 125 | uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) { |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 126 | // FIXME: It doesn't make sense to store the alignment information as an |
| 127 | // expanded out value, we should store it as a log2 value. However, we can't |
| 128 | // just change that here without breaking bitcode compatibility. If this ever |
| 129 | // becomes a problem in practice, we should introduce new tag numbers in the |
| 130 | // bitcode file and have those tags use a more efficiently encoded alignment |
| 131 | // field. |
| 132 | |
| 133 | // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit |
| 134 | // log2 encoded value. Shift the bits above the alignment up by 11 bits. |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 135 | uint64_t EncodedAttrs = Attrs.getBitMask() & 0xffff; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 136 | if (Attrs.hasAttribute(Attribute::Alignment)) |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 137 | EncodedAttrs |= Attrs.getAlignment() << 16; |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 138 | EncodedAttrs |= (Attrs.getBitMask() & (0xffffULL << 21)) << 11; |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 139 | return EncodedAttrs; |
| 140 | } |
| 141 | |
| 142 | /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing |
| 143 | /// the LLVM attributes that have been decoded from the given integer. This |
| 144 | /// function must stay in sync with 'encodeLLVMAttributesForBitcode'. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 145 | Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C, |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 146 | uint64_t EncodedAttrs) { |
| 147 | // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| 148 | // the bits above 31 down by 11 bits. |
| 149 | unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| 150 | assert((!Alignment || isPowerOf2_32(Alignment)) && |
| 151 | "Alignment must be a power of two."); |
| 152 | |
| 153 | AttrBuilder B(EncodedAttrs & 0xffff); |
| 154 | if (Alignment) |
| 155 | B.addAlignmentAttr(Alignment); |
Nadav Rotem | e743942 | 2012-10-22 17:33:31 +0000 | [diff] [blame] | 156 | B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11); |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 157 | return Attribute::get(C, B); |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 160 | std::string Attribute::getAsString() const { |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 161 | std::string Result; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 162 | if (hasAttribute(Attribute::ZExt)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 163 | Result += "zeroext "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 164 | if (hasAttribute(Attribute::SExt)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 165 | Result += "signext "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 166 | if (hasAttribute(Attribute::NoReturn)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 167 | Result += "noreturn "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 168 | if (hasAttribute(Attribute::NoUnwind)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 169 | Result += "nounwind "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 170 | if (hasAttribute(Attribute::UWTable)) |
Rafael Espindola | fc2bb8c | 2011-05-25 03:44:17 +0000 | [diff] [blame] | 171 | Result += "uwtable "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 172 | if (hasAttribute(Attribute::ReturnsTwice)) |
Rafael Espindola | 25456ef | 2011-10-03 14:45:37 +0000 | [diff] [blame] | 173 | Result += "returns_twice "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 174 | if (hasAttribute(Attribute::InReg)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 175 | Result += "inreg "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 176 | if (hasAttribute(Attribute::NoAlias)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 177 | Result += "noalias "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 178 | if (hasAttribute(Attribute::NoCapture)) |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 179 | Result += "nocapture "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 180 | if (hasAttribute(Attribute::StructRet)) |
Anton Korobeynikov | c5ec8a7 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 181 | Result += "sret "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 182 | if (hasAttribute(Attribute::ByVal)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 183 | Result += "byval "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 184 | if (hasAttribute(Attribute::Nest)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 185 | Result += "nest "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 186 | if (hasAttribute(Attribute::ReadNone)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 187 | Result += "readnone "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 188 | if (hasAttribute(Attribute::ReadOnly)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 189 | Result += "readonly "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 190 | if (hasAttribute(Attribute::OptimizeForSize)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 191 | Result += "optsize "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 192 | if (hasAttribute(Attribute::NoInline)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 193 | Result += "noinline "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 194 | if (hasAttribute(Attribute::InlineHint)) |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 195 | Result += "inlinehint "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 196 | if (hasAttribute(Attribute::AlwaysInline)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 197 | Result += "alwaysinline "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 198 | if (hasAttribute(Attribute::StackProtect)) |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 199 | Result += "ssp "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 200 | if (hasAttribute(Attribute::StackProtectReq)) |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 201 | Result += "sspreq "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 202 | if (hasAttribute(Attribute::NoRedZone)) |
Devang Patel | d18e31a | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 203 | Result += "noredzone "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 204 | if (hasAttribute(Attribute::NoImplicitFloat)) |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 205 | Result += "noimplicitfloat "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 206 | if (hasAttribute(Attribute::Naked)) |
Anton Korobeynikov | c5ec8a7 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 207 | Result += "naked "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 208 | if (hasAttribute(Attribute::NonLazyBind)) |
John McCall | 3a3465b | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 209 | Result += "nonlazybind "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 210 | if (hasAttribute(Attribute::AddressSafety)) |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 211 | Result += "address_safety "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 212 | if (hasAttribute(Attribute::MinSize)) |
Quentin Colombet | 9a419f6 | 2012-10-30 16:32:52 +0000 | [diff] [blame] | 213 | Result += "minsize "; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 214 | if (hasAttribute(Attribute::StackAlignment)) { |
Charles Davis | 1e063d1 | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 215 | Result += "alignstack("; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 216 | Result += utostr(getStackAlignment()); |
Charles Davis | 1e063d1 | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 217 | Result += ") "; |
| 218 | } |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 219 | if (hasAttribute(Attribute::Alignment)) { |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 220 | Result += "align "; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 221 | Result += utostr(getAlignment()); |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 222 | Result += " "; |
| 223 | } |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 224 | if (hasAttribute(Attribute::NoDuplicate)) |
| 225 | Result += "noduplicate "; |
Dan Gohman | c3be0fd | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 226 | // Trim the trailing space. |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 227 | assert(!Result.empty() && "Unknown attribute!"); |
Dan Gohman | c3be0fd | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 228 | Result.erase(Result.end()-1); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 229 | return Result; |
| 230 | } |
| 231 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 232 | //===----------------------------------------------------------------------===// |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 233 | // AttrBuilder Implementation |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 234 | //===----------------------------------------------------------------------===// |
| 235 | |
Bill Wendling | ded28ac | 2013-01-03 01:46:27 +0000 | [diff] [blame] | 236 | void AttrBuilder::clear() { |
| 237 | AttrSet.clear(); |
| 238 | Alignment = StackAlignment = Bits = 0; |
| 239 | } |
| 240 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 241 | AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val){ |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 242 | Bits |= AttributeImpl::getAttrMask(Val); |
Bill Wendling | ded28ac | 2013-01-03 01:46:27 +0000 | [diff] [blame] | 243 | |
| 244 | AttrSet.insert(Val); |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 245 | return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 248 | AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) { |
Bill Wendling | a19a530 | 2012-10-14 04:10:01 +0000 | [diff] [blame] | 249 | Bits |= Val; |
| 250 | return *this; |
| 251 | } |
| 252 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 253 | AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 254 | if (Align == 0) return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 255 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 256 | assert(Align <= 0x40000000 && "Alignment too large."); |
| 257 | Bits |= (Log2_32(Align) + 1) << 16; |
Bill Wendling | ded28ac | 2013-01-03 01:46:27 +0000 | [diff] [blame] | 258 | |
| 259 | AttrSet.insert(Attribute::Alignment); |
| 260 | Alignment = Align; |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 261 | return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 262 | } |
Bill Wendling | ded28ac | 2013-01-03 01:46:27 +0000 | [diff] [blame] | 263 | AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 264 | // Default alignment, allow the target to define how to align it. |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 265 | if (Align == 0) return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 266 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 267 | assert(Align <= 0x100 && "Alignment too large."); |
| 268 | Bits |= (Log2_32(Align) + 1) << 26; |
Bill Wendling | ded28ac | 2013-01-03 01:46:27 +0000 | [diff] [blame] | 269 | |
| 270 | AttrSet.insert(Attribute::StackAlignment); |
| 271 | StackAlignment = Align; |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 272 | return *this; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 275 | AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 276 | Bits &= ~AttributeImpl::getAttrMask(Val); |
Bill Wendling | ded28ac | 2013-01-03 01:46:27 +0000 | [diff] [blame] | 277 | |
| 278 | AttrSet.erase(Val); |
| 279 | if (Val == Attribute::Alignment) |
| 280 | Alignment = 0; |
| 281 | else if (Val == Attribute::StackAlignment) |
| 282 | StackAlignment = 0; |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 283 | return *this; |
Bill Wendling | 2e879bc | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 286 | AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) { |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 287 | Bits |= A.getBitMask(); |
Bill Wendling | 432e606 | 2012-10-14 07:17:34 +0000 | [diff] [blame] | 288 | return *this; |
| 289 | } |
| 290 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 291 | AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){ |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 292 | Bits &= ~A.getBitMask(); |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 293 | return *this; |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Bill Wendling | 22bd641 | 2013-01-03 01:54:39 +0000 | [diff] [blame^] | 296 | bool AttrBuilder::contains(Attribute::AttrKind A) const { |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 297 | return Bits & AttributeImpl::getAttrMask(A); |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 300 | bool AttrBuilder::hasAttributes() const { |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 301 | return Bits != 0; |
| 302 | } |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 303 | bool AttrBuilder::hasAttributes(const Attribute &A) const { |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 304 | return Bits & A.getBitMask(); |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 305 | } |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 306 | bool AttrBuilder::hasAlignmentAttr() const { |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 307 | return Bits & AttributeImpl::getAttrMask(Attribute::Alignment); |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 310 | uint64_t AttrBuilder::getAlignment() const { |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 311 | if (!hasAlignmentAttr()) |
| 312 | return 0; |
NAKAMURA Takumi | 1fcbb8f | 2012-11-19 10:03:09 +0000 | [diff] [blame] | 313 | return 1ULL << |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 314 | (((Bits & AttributeImpl::getAttrMask(Attribute::Alignment)) >> 16) - 1); |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 317 | uint64_t AttrBuilder::getStackAlignment() const { |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 318 | if (!hasAlignmentAttr()) |
| 319 | return 0; |
NAKAMURA Takumi | 1fcbb8f | 2012-11-19 10:03:09 +0000 | [diff] [blame] | 320 | return 1ULL << |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 321 | (((Bits & AttributeImpl::getAttrMask(Attribute::StackAlignment))>>26)-1); |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 324 | //===----------------------------------------------------------------------===// |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 325 | // AttributeImpl Definition |
| 326 | //===----------------------------------------------------------------------===// |
| 327 | |
Bill Wendling | 7c1683d | 2012-12-29 12:29:38 +0000 | [diff] [blame] | 328 | AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data) { |
| 329 | Data = ConstantInt::get(Type::getInt64Ty(C), data); |
| 330 | } |
Bill Wendling | 979aff6 | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 331 | AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data) { |
| 332 | Data = ConstantInt::get(Type::getInt64Ty(C), data); |
| 333 | } |
| 334 | AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data, |
| 335 | ArrayRef<Constant*> values) { |
| 336 | Data = ConstantInt::get(Type::getInt64Ty(C), data); |
| 337 | Vals.reserve(values.size()); |
| 338 | Vals.append(values.begin(), values.end()); |
| 339 | } |
| 340 | AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data) { |
| 341 | Data = ConstantDataArray::getString(C, data); |
| 342 | } |
Bill Wendling | 7c1683d | 2012-12-29 12:29:38 +0000 | [diff] [blame] | 343 | |
Bill Wendling | 22bd641 | 2013-01-03 01:54:39 +0000 | [diff] [blame^] | 344 | bool AttributeImpl::contains(Attribute::AttrKind Kind) const { |
Bill Wendling | 529ec71 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 345 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Data)) |
| 346 | return CI->getZExtValue() == Kind; |
| 347 | return false; |
| 348 | } |
| 349 | |
Bill Wendling | 22bd641 | 2013-01-03 01:54:39 +0000 | [diff] [blame^] | 350 | bool AttributeImpl::contains(StringRef Kind) const { |
Bill Wendling | 529ec71 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 351 | if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data)) |
| 352 | if (CDA->isString()) |
| 353 | return CDA->getAsString() == Kind; |
| 354 | return false; |
| 355 | } |
| 356 | |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 357 | uint64_t AttributeImpl::getBitMask() const { |
Bill Wendling | 529ec71 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 358 | // FIXME: Remove this. |
Bill Wendling | 7c1683d | 2012-12-29 12:29:38 +0000 | [diff] [blame] | 359 | return cast<ConstantInt>(Data)->getZExtValue(); |
| 360 | } |
| 361 | |
Bill Wendling | 47990e9 | 2013-01-03 00:46:43 +0000 | [diff] [blame] | 362 | uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) { |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 363 | switch (Val) { |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 364 | case Attribute::None: return 0; |
| 365 | case Attribute::ZExt: return 1 << 0; |
| 366 | case Attribute::SExt: return 1 << 1; |
| 367 | case Attribute::NoReturn: return 1 << 2; |
| 368 | case Attribute::InReg: return 1 << 3; |
| 369 | case Attribute::StructRet: return 1 << 4; |
| 370 | case Attribute::NoUnwind: return 1 << 5; |
| 371 | case Attribute::NoAlias: return 1 << 6; |
| 372 | case Attribute::ByVal: return 1 << 7; |
| 373 | case Attribute::Nest: return 1 << 8; |
| 374 | case Attribute::ReadNone: return 1 << 9; |
| 375 | case Attribute::ReadOnly: return 1 << 10; |
| 376 | case Attribute::NoInline: return 1 << 11; |
| 377 | case Attribute::AlwaysInline: return 1 << 12; |
| 378 | case Attribute::OptimizeForSize: return 1 << 13; |
| 379 | case Attribute::StackProtect: return 1 << 14; |
| 380 | case Attribute::StackProtectReq: return 1 << 15; |
| 381 | case Attribute::Alignment: return 31 << 16; |
| 382 | case Attribute::NoCapture: return 1 << 21; |
| 383 | case Attribute::NoRedZone: return 1 << 22; |
| 384 | case Attribute::NoImplicitFloat: return 1 << 23; |
| 385 | case Attribute::Naked: return 1 << 24; |
| 386 | case Attribute::InlineHint: return 1 << 25; |
| 387 | case Attribute::StackAlignment: return 7 << 26; |
| 388 | case Attribute::ReturnsTwice: return 1 << 29; |
| 389 | case Attribute::UWTable: return 1 << 30; |
| 390 | case Attribute::NonLazyBind: return 1U << 31; |
| 391 | case Attribute::AddressSafety: return 1ULL << 32; |
| 392 | case Attribute::MinSize: return 1ULL << 33; |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 393 | case Attribute::NoDuplicate: return 1ULL << 34; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 394 | } |
| 395 | llvm_unreachable("Unsupported attribute type"); |
| 396 | } |
| 397 | |
Bill Wendling | 47990e9 | 2013-01-03 00:46:43 +0000 | [diff] [blame] | 398 | bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 399 | return (getBitMask() & getAttrMask(A)) != 0; |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 400 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 401 | |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 402 | bool AttributeImpl::hasAttributes() const { |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 403 | return getBitMask() != 0; |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 404 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 405 | |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 406 | bool AttributeImpl::hasAttributes(const Attribute &A) const { |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 407 | // FIXME: getBitMask() won't work here in the future. |
| 408 | return getBitMask() & A.getBitMask(); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 409 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 410 | |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 411 | uint64_t AttributeImpl::getAlignment() const { |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 412 | return getBitMask() & getAttrMask(Attribute::Alignment); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 413 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 414 | |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 415 | uint64_t AttributeImpl::getStackAlignment() const { |
Bill Wendling | c966e08 | 2012-12-30 01:05:42 +0000 | [diff] [blame] | 416 | return getBitMask() & getAttrMask(Attribute::StackAlignment); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 417 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 418 | |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 419 | //===----------------------------------------------------------------------===// |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 420 | // AttributeSetImpl Definition |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 421 | //===----------------------------------------------------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 422 | |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 423 | AttributeSet AttributeSet::get(LLVMContext &C, |
Bill Wendling | 1ce47ac | 2012-12-12 19:21:53 +0000 | [diff] [blame] | 424 | ArrayRef<AttributeWithIndex> Attrs) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 425 | // If there are no attributes then return a null AttributesList pointer. |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 426 | if (Attrs.empty()) |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 427 | return AttributeSet(); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 429 | #ifndef NDEBUG |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 430 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 431 | assert(Attrs[i].Attrs.hasAttributes() && |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 432 | "Pointless attribute!"); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 433 | assert((!i || Attrs[i-1].Index < Attrs[i].Index) && |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 434 | "Misordered AttributesList!"); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 435 | } |
| 436 | #endif |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 438 | // Otherwise, build a key to look up the existing attributes. |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 439 | LLVMContextImpl *pImpl = C.pImpl; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 440 | FoldingSetNodeID ID; |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 441 | AttributeSetImpl::Profile(ID, Attrs); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 442 | |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 443 | void *InsertPoint; |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 444 | AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 445 | InsertPoint); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 447 | // If we didn't find any existing attributes of the same shape then |
| 448 | // create a new one and insert it. |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 449 | if (!PA) { |
Bill Wendling | 5f93e2b | 2012-12-19 23:55:43 +0000 | [diff] [blame] | 450 | PA = new AttributeSetImpl(C, Attrs); |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 451 | pImpl->AttrsLists.InsertNode(PA, InsertPoint); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 452 | } |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 453 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 454 | // Return the AttributesList that we found or created. |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 455 | return AttributeSet(PA); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 458 | //===----------------------------------------------------------------------===// |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 459 | // AttributeSet Method Implementations |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 460 | //===----------------------------------------------------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 461 | |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 462 | const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) { |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 463 | AttrList = RHS.AttrList; |
| 464 | return *this; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 467 | /// getNumSlots - Return the number of slots used in this attribute list. |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 468 | /// This is the number of arguments that have an attribute set on them |
| 469 | /// (including the function itself). |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 470 | unsigned AttributeSet::getNumSlots() const { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 471 | return AttrList ? AttrList->Attrs.size() : 0; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 474 | /// getSlot - Return the AttributeWithIndex at the specified slot. This |
| 475 | /// holds a number plus a set of attributes. |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 476 | const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 477 | assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!"); |
| 478 | return AttrList->Attrs[Slot]; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 481 | bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{ |
| 482 | return getAttributes(Index).hasAttribute(Kind); |
| 483 | } |
| 484 | |
| 485 | bool AttributeSet::hasAttributes(unsigned Index) const { |
| 486 | return getAttributes(Index).hasAttributes(); |
| 487 | } |
| 488 | |
| 489 | std::string AttributeSet::getAsString(unsigned Index) const { |
| 490 | return getAttributes(Index).getAsString(); |
| 491 | } |
| 492 | |
| 493 | unsigned AttributeSet::getStackAlignment(unsigned Index) const { |
| 494 | return getAttributes(Index).getStackAlignment(); |
| 495 | } |
| 496 | |
| 497 | uint64_t AttributeSet::getBitMask(unsigned Index) const { |
| 498 | // FIXME: Remove this. |
| 499 | return getAttributes(Index).getBitMask(); |
| 500 | } |
| 501 | |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 502 | /// getAttributes - The attributes for the specified index are returned. |
Bill Wendling | 92e287f | 2012-12-31 11:51:54 +0000 | [diff] [blame] | 503 | /// Attributes for the result are denoted with Idx = 0. Function attributes are |
| 504 | /// denoted with Idx = ~0. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 505 | Attribute AttributeSet::getAttributes(unsigned Idx) const { |
| 506 | if (AttrList == 0) return Attribute(); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 507 | |
Bill Wendling | 9d30e72 | 2012-12-31 00:49:59 +0000 | [diff] [blame] | 508 | const SmallVectorImpl<AttributeWithIndex> &Attrs = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 509 | for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i) |
| 510 | if (Attrs[i].Index == Idx) |
| 511 | return Attrs[i].Attrs; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 512 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 513 | return Attribute(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /// hasAttrSomewhere - Return true if the specified attribute is set for at |
| 517 | /// least one parameter or for the return value. |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 518 | bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 519 | if (AttrList == 0) return false; |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 520 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 521 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 522 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 523 | if (Attrs[i].Attrs.hasAttribute(Attr)) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 524 | return true; |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 529 | AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx, |
Bill Wendling | 9d30e72 | 2012-12-31 00:49:59 +0000 | [diff] [blame] | 530 | Attribute Attrs) const { |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 531 | Attribute OldAttrs = getAttributes(Idx); |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 532 | #ifndef NDEBUG |
| 533 | // FIXME it is not obvious how this should work for alignment. |
| 534 | // For now, say we can't change a known alignment. |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 535 | unsigned OldAlign = OldAttrs.getAlignment(); |
| 536 | unsigned NewAlign = Attrs.getAlignment(); |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 537 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 538 | "Attempt to change alignment!"); |
| 539 | #endif |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 540 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 541 | AttrBuilder NewAttrs = |
| 542 | AttrBuilder(OldAttrs).addAttributes(Attrs); |
| 543 | if (NewAttrs == AttrBuilder(OldAttrs)) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 544 | return *this; |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 545 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 546 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 547 | if (AttrList == 0) |
| 548 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 549 | else { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 550 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 551 | unsigned i = 0, e = OldAttrList.size(); |
| 552 | // Copy attributes for arguments before this one. |
| 553 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 554 | NewAttrList.push_back(OldAttrList[i]); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 556 | // If there are attributes already at this index, merge them in. |
| 557 | if (i != e && OldAttrList[i].Index == Idx) { |
Bill Wendling | c416795 | 2012-10-14 07:35:59 +0000 | [diff] [blame] | 558 | Attrs = |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 559 | Attribute::get(C, AttrBuilder(Attrs). |
Bill Wendling | c416795 | 2012-10-14 07:35:59 +0000 | [diff] [blame] | 560 | addAttributes(OldAttrList[i].Attrs)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 561 | ++i; |
| 562 | } |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 563 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 564 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 566 | // Copy attributes for arguments after this one. |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 567 | NewAttrList.insert(NewAttrList.end(), |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 568 | OldAttrList.begin()+i, OldAttrList.end()); |
| 569 | } |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 570 | |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 571 | return get(C, NewAttrList); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 574 | AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx, |
Bill Wendling | 9d30e72 | 2012-12-31 00:49:59 +0000 | [diff] [blame] | 575 | Attribute Attrs) const { |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 576 | #ifndef NDEBUG |
| 577 | // FIXME it is not obvious how this should work for alignment. |
| 578 | // 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] | 579 | assert(!Attrs.hasAttribute(Attribute::Alignment) && |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 580 | "Attempt to exclude alignment!"); |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 581 | #endif |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 582 | if (AttrList == 0) return AttributeSet(); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 583 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 584 | Attribute OldAttrs = getAttributes(Idx); |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 585 | AttrBuilder NewAttrs = |
| 586 | AttrBuilder(OldAttrs).removeAttributes(Attrs); |
| 587 | if (NewAttrs == AttrBuilder(OldAttrs)) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 588 | return *this; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 589 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 590 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 591 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 592 | unsigned i = 0, e = OldAttrList.size(); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 594 | // Copy attributes for arguments before this one. |
| 595 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 596 | NewAttrList.push_back(OldAttrList[i]); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 598 | // If there are attributes already at this index, merge them in. |
| 599 | assert(OldAttrList[i].Index == Idx && "Attribute isn't set?"); |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 600 | Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs). |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 601 | removeAttributes(Attrs)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 602 | ++i; |
Bill Wendling | 7be7848 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 603 | if (Attrs.hasAttributes()) // If any attributes left for this param, add them. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 604 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 606 | // Copy attributes for arguments after this one. |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 607 | NewAttrList.insert(NewAttrList.end(), |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 608 | OldAttrList.begin()+i, OldAttrList.end()); |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 609 | |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 610 | return get(C, NewAttrList); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 613 | void AttributeSet::dump() const { |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 614 | dbgs() << "PAL[ "; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 615 | for (unsigned i = 0; i < getNumSlots(); ++i) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 616 | const AttributeWithIndex &PAWI = getSlot(i); |
Bill Wendling | 7be7848 | 2012-10-14 08:54:26 +0000 | [diff] [blame] | 617 | dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} "; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 618 | } |
Bill Wendling | 7789868 | 2012-10-16 06:01:44 +0000 | [diff] [blame] | 619 | |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 620 | dbgs() << "]\n"; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 621 | } |