1 //===-- Attributes.cpp - Implement AttributesList -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AttributesList class and Attribute utilities.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Attributes.h"
15 #include "AttributesImpl.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/Type.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/Support/Atomic.h"
21 #include "llvm/Support/Mutex.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/raw_ostream.h"
27 //===----------------------------------------------------------------------===//
28 // Attributes Implementation
29 //===----------------------------------------------------------------------===//
31 Attributes::Attributes(AttributesImpl *A) : Attrs(A) {}
33 Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
35 Attributes Attributes::get(LLVMContext &Context, ArrayRef<AttrVal> Vals) {
37 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
40 return Attributes::get(Context, B);
43 Attributes Attributes::get(LLVMContext &Context, AttrBuilder &B) {
44 // If there are no attributes, return an empty Attributes class.
48 // Otherwise, build a key to look up the existing attributes.
49 LLVMContextImpl *pImpl = Context.pImpl;
51 ID.AddInteger(B.Bits);
54 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
57 // If we didn't find any existing attributes of the same shape then create a
58 // new one and insert it.
59 PA = new AttributesImpl(B.Bits);
60 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
63 // Return the AttributesList that we found or created.
64 return Attributes(PA);
67 bool Attributes::hasAttribute(AttrVal Val) const {
68 return Attrs && Attrs->hasAttribute(Val);
71 bool Attributes::hasAttributes() const {
72 return Attrs && Attrs->hasAttributes();
75 bool Attributes::hasAttributes(const Attributes &A) const {
76 return Attrs && Attrs->hasAttributes(A);
79 /// This returns the alignment field of an attribute as a byte alignment value.
80 unsigned Attributes::getAlignment() const {
81 if (!hasAttribute(Attributes::Alignment))
83 return 1U << ((Attrs->getAlignment() >> 16) - 1);
86 /// This returns the stack alignment field of an attribute as a byte alignment
88 unsigned Attributes::getStackAlignment() const {
89 if (!hasAttribute(Attributes::StackAlignment))
91 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
94 uint64_t Attributes::Raw() const {
95 return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly!
98 Attributes Attributes::typeIncompatible(Type *Ty) {
99 AttrBuilder Incompatible;
101 if (!Ty->isIntegerTy())
102 // Attributes that only apply to integers.
103 Incompatible.addAttribute(Attributes::SExt)
104 .addAttribute(Attributes::ZExt);
106 if (!Ty->isPointerTy())
107 // Attributes that only apply to pointers.
108 Incompatible.addAttribute(Attributes::ByVal)
109 .addAttribute(Attributes::Nest)
110 .addAttribute(Attributes::NoAlias)
111 .addAttribute(Attributes::NoCapture)
112 .addAttribute(Attributes::StructRet);
114 return Attributes::get(Ty->getContext(), Incompatible);
117 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
118 /// encoding of all the LLVM attributes found in the given attribute bitset.
119 /// Any change to this encoding is a breaking change to bitcode compatibility.
120 uint64_t Attributes::encodeLLVMAttributesForBitcode(Attributes Attrs) {
121 // FIXME: It doesn't make sense to store the alignment information as an
122 // expanded out value, we should store it as a log2 value. However, we can't
123 // just change that here without breaking bitcode compatibility. If this ever
124 // becomes a problem in practice, we should introduce new tag numbers in the
125 // bitcode file and have those tags use a more efficiently encoded alignment
128 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
129 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
130 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
131 if (Attrs.hasAttribute(Attributes::Alignment))
132 EncodedAttrs |= Attrs.getAlignment() << 16;
133 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
137 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
138 /// the LLVM attributes that have been decoded from the given integer. This
139 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
140 Attributes Attributes::decodeLLVMAttributesForBitcode(LLVMContext &C,
141 uint64_t EncodedAttrs) {
142 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
143 // the bits above 31 down by 11 bits.
144 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
145 assert((!Alignment || isPowerOf2_32(Alignment)) &&
146 "Alignment must be a power of two.");
148 AttrBuilder B(EncodedAttrs & 0xffff);
150 B.addAlignmentAttr(Alignment);
151 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
152 return Attributes::get(C, B);
155 std::string Attributes::getAsString() const {
157 if (hasAttribute(Attributes::ZExt))
158 Result += "zeroext ";
159 if (hasAttribute(Attributes::SExt))
160 Result += "signext ";
161 if (hasAttribute(Attributes::NoReturn))
162 Result += "noreturn ";
163 if (hasAttribute(Attributes::NoUnwind))
164 Result += "nounwind ";
165 if (hasAttribute(Attributes::UWTable))
166 Result += "uwtable ";
167 if (hasAttribute(Attributes::ReturnsTwice))
168 Result += "returns_twice ";
169 if (hasAttribute(Attributes::InReg))
171 if (hasAttribute(Attributes::NoAlias))
172 Result += "noalias ";
173 if (hasAttribute(Attributes::NoCapture))
174 Result += "nocapture ";
175 if (hasAttribute(Attributes::StructRet))
177 if (hasAttribute(Attributes::ByVal))
179 if (hasAttribute(Attributes::Nest))
181 if (hasAttribute(Attributes::ReadNone))
182 Result += "readnone ";
183 if (hasAttribute(Attributes::ReadOnly))
184 Result += "readonly ";
185 if (hasAttribute(Attributes::OptimizeForSize))
186 Result += "optsize ";
187 if (hasAttribute(Attributes::NoInline))
188 Result += "noinline ";
189 if (hasAttribute(Attributes::InlineHint))
190 Result += "inlinehint ";
191 if (hasAttribute(Attributes::AlwaysInline))
192 Result += "alwaysinline ";
193 if (hasAttribute(Attributes::StackProtect))
195 if (hasAttribute(Attributes::StackProtectReq))
197 if (hasAttribute(Attributes::NoRedZone))
198 Result += "noredzone ";
199 if (hasAttribute(Attributes::NoImplicitFloat))
200 Result += "noimplicitfloat ";
201 if (hasAttribute(Attributes::Naked))
203 if (hasAttribute(Attributes::NonLazyBind))
204 Result += "nonlazybind ";
205 if (hasAttribute(Attributes::AddressSafety))
206 Result += "address_safety ";
207 if (hasAttribute(Attributes::StackAlignment)) {
208 Result += "alignstack(";
209 Result += utostr(getStackAlignment());
212 if (hasAttribute(Attributes::Alignment)) {
214 Result += utostr(getAlignment());
217 // Trim the trailing space.
218 assert(!Result.empty() && "Unknown attribute!");
219 Result.erase(Result.end()-1);
223 //===----------------------------------------------------------------------===//
224 // AttrBuilder Implementation
225 //===----------------------------------------------------------------------===//
227 AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){
228 Bits |= AttributesImpl::getAttrMask(Val);
232 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
237 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
238 if (Align == 0) return *this;
239 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
240 assert(Align <= 0x40000000 && "Alignment too large.");
241 Bits |= (Log2_32(Align) + 1) << 16;
244 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
245 // Default alignment, allow the target to define how to align it.
246 if (Align == 0) return *this;
247 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
248 assert(Align <= 0x100 && "Alignment too large.");
249 Bits |= (Log2_32(Align) + 1) << 26;
253 AttrBuilder &AttrBuilder::
254 removeAttribute(Attributes::AttrVal Val) {
255 Bits &= ~AttributesImpl::getAttrMask(Val);
259 AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) {
264 AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){
269 bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const {
270 return Bits & AttributesImpl::getAttrMask(A);
273 bool AttrBuilder::hasAttributes() const {
276 bool AttrBuilder::hasAttributes(const Attributes &A) const {
277 return Bits & A.Raw();
279 bool AttrBuilder::hasAlignmentAttr() const {
280 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
283 uint64_t AttrBuilder::getAlignment() const {
284 if (!hasAlignmentAttr())
287 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
290 uint64_t AttrBuilder::getStackAlignment() const {
291 if (!hasAlignmentAttr())
294 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
297 //===----------------------------------------------------------------------===//
298 // AttributeImpl Definition
299 //===----------------------------------------------------------------------===//
301 uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
303 case Attributes::None: return 0;
304 case Attributes::ZExt: return 1 << 0;
305 case Attributes::SExt: return 1 << 1;
306 case Attributes::NoReturn: return 1 << 2;
307 case Attributes::InReg: return 1 << 3;
308 case Attributes::StructRet: return 1 << 4;
309 case Attributes::NoUnwind: return 1 << 5;
310 case Attributes::NoAlias: return 1 << 6;
311 case Attributes::ByVal: return 1 << 7;
312 case Attributes::Nest: return 1 << 8;
313 case Attributes::ReadNone: return 1 << 9;
314 case Attributes::ReadOnly: return 1 << 10;
315 case Attributes::NoInline: return 1 << 11;
316 case Attributes::AlwaysInline: return 1 << 12;
317 case Attributes::OptimizeForSize: return 1 << 13;
318 case Attributes::StackProtect: return 1 << 14;
319 case Attributes::StackProtectReq: return 1 << 15;
320 case Attributes::Alignment: return 31 << 16;
321 case Attributes::NoCapture: return 1 << 21;
322 case Attributes::NoRedZone: return 1 << 22;
323 case Attributes::NoImplicitFloat: return 1 << 23;
324 case Attributes::Naked: return 1 << 24;
325 case Attributes::InlineHint: return 1 << 25;
326 case Attributes::StackAlignment: return 7 << 26;
327 case Attributes::ReturnsTwice: return 1 << 29;
328 case Attributes::UWTable: return 1 << 30;
329 case Attributes::NonLazyBind: return 1U << 31;
330 case Attributes::AddressSafety: return 1ULL << 32;
332 llvm_unreachable("Unsupported attribute type");
335 bool AttributesImpl::hasAttribute(uint64_t A) const {
336 return (Bits & getAttrMask(A)) != 0;
339 bool AttributesImpl::hasAttributes() const {
343 bool AttributesImpl::hasAttributes(const Attributes &A) const {
344 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
347 uint64_t AttributesImpl::getAlignment() const {
348 return Bits & getAttrMask(Attributes::Alignment);
351 uint64_t AttributesImpl::getStackAlignment() const {
352 return Bits & getAttrMask(Attributes::StackAlignment);
355 //===----------------------------------------------------------------------===//
356 // AttributeListImpl Definition
357 //===----------------------------------------------------------------------===//
360 class AttributeListImpl;
363 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
366 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
368 class AttributeListImpl : public FoldingSetNode {
369 sys::cas_flag RefCount;
371 // AttributesList is uniqued, these should not be publicly available.
372 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
373 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
374 ~AttributeListImpl(); // Private implementation
376 SmallVector<AttributeWithIndex, 4> Attrs;
378 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
379 : Attrs(attrs.begin(), attrs.end()) {
384 sys::SmartScopedLock<true> Lock(*ALMutex);
388 sys::SmartScopedLock<true> Lock(*ALMutex);
389 if (!AttributesLists.isConstructed())
391 sys::cas_flag new_val = --RefCount;
396 void Profile(FoldingSetNodeID &ID) const {
399 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
400 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
401 ID.AddInteger(Attrs[i].Attrs.Raw());
402 ID.AddInteger(Attrs[i].Index);
408 AttributeListImpl::~AttributeListImpl() {
409 // NOTE: Lock must be acquired by caller.
410 AttributesLists->RemoveNode(this);
414 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
415 // If there are no attributes then return a null AttributesList pointer.
417 return AttrListPtr();
420 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
421 assert(Attrs[i].Attrs.hasAttributes() &&
422 "Pointless attribute!");
423 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
424 "Misordered AttributesList!");
428 // Otherwise, build a key to look up the existing attributes.
430 AttributeListImpl::Profile(ID, Attrs);
433 sys::SmartScopedLock<true> Lock(*ALMutex);
435 AttributeListImpl *PAL =
436 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
438 // If we didn't find any existing attributes of the same shape then
439 // create a new one and insert it.
441 PAL = new AttributeListImpl(Attrs);
442 AttributesLists->InsertNode(PAL, InsertPos);
445 // Return the AttributesList that we found or created.
446 return AttrListPtr(PAL);
450 //===----------------------------------------------------------------------===//
451 // AttrListPtr Method Implementations
452 //===----------------------------------------------------------------------===//
454 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
455 if (LI) LI->AddRef();
458 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
459 if (AttrList) AttrList->AddRef();
462 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
463 sys::SmartScopedLock<true> Lock(*ALMutex);
464 if (AttrList == RHS.AttrList) return *this;
465 if (AttrList) AttrList->DropRef();
466 AttrList = RHS.AttrList;
467 if (AttrList) AttrList->AddRef();
471 AttrListPtr::~AttrListPtr() {
472 if (AttrList) AttrList->DropRef();
475 /// getNumSlots - Return the number of slots used in this attribute list.
476 /// This is the number of arguments that have an attribute set on them
477 /// (including the function itself).
478 unsigned AttrListPtr::getNumSlots() const {
479 return AttrList ? AttrList->Attrs.size() : 0;
482 /// getSlot - Return the AttributeWithIndex at the specified slot. This
483 /// holds a number plus a set of attributes.
484 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
485 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
486 return AttrList->Attrs[Slot];
490 /// getAttributes - The attributes for the specified index are
491 /// returned. Attributes for the result are denoted with Idx = 0.
492 /// Function notes are denoted with idx = ~0.
493 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
494 if (AttrList == 0) return Attributes();
496 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
497 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
498 if (Attrs[i].Index == Idx)
499 return Attrs[i].Attrs;
504 /// hasAttrSomewhere - Return true if the specified attribute is set for at
505 /// least one parameter or for the return value.
506 bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
507 if (AttrList == 0) return false;
509 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
510 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
511 if (Attrs[i].Attrs.hasAttribute(Attr))
516 unsigned AttrListPtr::getNumAttrs() const {
517 return AttrList ? AttrList->Attrs.size() : 0;
520 Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
521 assert(AttrList && "Trying to get an attribute from an empty list!");
522 assert(i < AttrList->Attrs.size() && "Index out of range!");
523 return AttrList->Attrs[i].Attrs;
526 AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
527 Attributes Attrs) const {
528 Attributes OldAttrs = getAttributes(Idx);
530 // FIXME it is not obvious how this should work for alignment.
531 // For now, say we can't change a known alignment.
532 unsigned OldAlign = OldAttrs.getAlignment();
533 unsigned NewAlign = Attrs.getAlignment();
534 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
535 "Attempt to change alignment!");
538 AttrBuilder NewAttrs =
539 AttrBuilder(OldAttrs).addAttributes(Attrs);
540 if (NewAttrs == AttrBuilder(OldAttrs))
543 SmallVector<AttributeWithIndex, 8> NewAttrList;
545 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
547 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
548 unsigned i = 0, e = OldAttrList.size();
549 // Copy attributes for arguments before this one.
550 for (; i != e && OldAttrList[i].Index < Idx; ++i)
551 NewAttrList.push_back(OldAttrList[i]);
553 // If there are attributes already at this index, merge them in.
554 if (i != e && OldAttrList[i].Index == Idx) {
556 Attributes::get(C, AttrBuilder(Attrs).
557 addAttributes(OldAttrList[i].Attrs));
561 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
563 // Copy attributes for arguments after this one.
564 NewAttrList.insert(NewAttrList.end(),
565 OldAttrList.begin()+i, OldAttrList.end());
568 return get(NewAttrList);
571 AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
572 Attributes Attrs) const {
574 // FIXME it is not obvious how this should work for alignment.
575 // For now, say we can't pass in alignment, which no current use does.
576 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
577 "Attempt to exclude alignment!");
579 if (AttrList == 0) return AttrListPtr();
581 Attributes OldAttrs = getAttributes(Idx);
582 AttrBuilder NewAttrs =
583 AttrBuilder(OldAttrs).removeAttributes(Attrs);
584 if (NewAttrs == AttrBuilder(OldAttrs))
587 SmallVector<AttributeWithIndex, 8> NewAttrList;
588 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
589 unsigned i = 0, e = OldAttrList.size();
591 // Copy attributes for arguments before this one.
592 for (; i != e && OldAttrList[i].Index < Idx; ++i)
593 NewAttrList.push_back(OldAttrList[i]);
595 // If there are attributes already at this index, merge them in.
596 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
597 Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs).
598 removeAttributes(Attrs));
600 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
601 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
603 // Copy attributes for arguments after this one.
604 NewAttrList.insert(NewAttrList.end(),
605 OldAttrList.begin()+i, OldAttrList.end());
607 return get(NewAttrList);
610 void AttrListPtr::dump() const {
612 for (unsigned i = 0; i < getNumSlots(); ++i) {
613 const AttributeWithIndex &PAWI = getSlot(i);
614 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";