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::get(LLVMContext &Context, ArrayRef<AttrVal> Vals) {
33 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
36 return Attributes::get(Context, B);
39 Attributes Attributes::get(LLVMContext &Context, AttrBuilder &B) {
40 // If there are no attributes, return an empty Attributes class.
44 // Otherwise, build a key to look up the existing attributes.
45 LLVMContextImpl *pImpl = Context.pImpl;
47 ID.AddInteger(B.Bits);
50 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
53 // If we didn't find any existing attributes of the same shape then create a
54 // new one and insert it.
55 PA = new AttributesImpl(B.Bits);
56 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 // Return the AttributesList that we found or created.
60 return Attributes(PA);
63 bool Attributes::hasAttribute(AttrVal Val) const {
64 return Attrs && Attrs->hasAttribute(Val);
67 bool Attributes::hasAttributes() const {
68 return Attrs && Attrs->hasAttributes();
71 bool Attributes::hasAttributes(const Attributes &A) const {
72 return Attrs && Attrs->hasAttributes(A);
75 /// This returns the alignment field of an attribute as a byte alignment value.
76 unsigned Attributes::getAlignment() const {
77 if (!hasAttribute(Attributes::Alignment))
79 return 1U << ((Attrs->getAlignment() >> 16) - 1);
82 /// This returns the stack alignment field of an attribute as a byte alignment
84 unsigned Attributes::getStackAlignment() const {
85 if (!hasAttribute(Attributes::StackAlignment))
87 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
90 uint64_t Attributes::Raw() const {
91 return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly!
94 Attributes Attributes::typeIncompatible(Type *Ty) {
95 AttrBuilder Incompatible;
97 if (!Ty->isIntegerTy())
98 // Attributes that only apply to integers.
99 Incompatible.addAttribute(Attributes::SExt)
100 .addAttribute(Attributes::ZExt);
102 if (!Ty->isPointerTy())
103 // Attributes that only apply to pointers.
104 Incompatible.addAttribute(Attributes::ByVal)
105 .addAttribute(Attributes::Nest)
106 .addAttribute(Attributes::NoAlias)
107 .addAttribute(Attributes::NoCapture)
108 .addAttribute(Attributes::StructRet);
110 return Attributes::get(Ty->getContext(), Incompatible);
113 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
114 /// encoding of all the LLVM attributes found in the given attribute bitset.
115 /// Any change to this encoding is a breaking change to bitcode compatibility.
116 uint64_t Attributes::encodeLLVMAttributesForBitcode(Attributes Attrs) {
117 // FIXME: It doesn't make sense to store the alignment information as an
118 // expanded out value, we should store it as a log2 value. However, we can't
119 // just change that here without breaking bitcode compatibility. If this ever
120 // becomes a problem in practice, we should introduce new tag numbers in the
121 // bitcode file and have those tags use a more efficiently encoded alignment
124 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
125 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
126 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
127 if (Attrs.hasAttribute(Attributes::Alignment))
128 EncodedAttrs |= Attrs.getAlignment() << 16;
129 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
133 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
134 /// the LLVM attributes that have been decoded from the given integer. This
135 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
136 Attributes Attributes::decodeLLVMAttributesForBitcode(LLVMContext &C,
137 uint64_t EncodedAttrs) {
138 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
139 // the bits above 31 down by 11 bits.
140 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
141 assert((!Alignment || isPowerOf2_32(Alignment)) &&
142 "Alignment must be a power of two.");
144 AttrBuilder B(EncodedAttrs & 0xffff);
146 B.addAlignmentAttr(Alignment);
147 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
148 return Attributes::get(C, B);
151 std::string Attributes::getAsString() const {
153 if (hasAttribute(Attributes::ZExt))
154 Result += "zeroext ";
155 if (hasAttribute(Attributes::SExt))
156 Result += "signext ";
157 if (hasAttribute(Attributes::NoReturn))
158 Result += "noreturn ";
159 if (hasAttribute(Attributes::NoUnwind))
160 Result += "nounwind ";
161 if (hasAttribute(Attributes::UWTable))
162 Result += "uwtable ";
163 if (hasAttribute(Attributes::ReturnsTwice))
164 Result += "returns_twice ";
165 if (hasAttribute(Attributes::InReg))
167 if (hasAttribute(Attributes::NoAlias))
168 Result += "noalias ";
169 if (hasAttribute(Attributes::NoCapture))
170 Result += "nocapture ";
171 if (hasAttribute(Attributes::StructRet))
173 if (hasAttribute(Attributes::ByVal))
175 if (hasAttribute(Attributes::Nest))
177 if (hasAttribute(Attributes::ReadNone))
178 Result += "readnone ";
179 if (hasAttribute(Attributes::ReadOnly))
180 Result += "readonly ";
181 if (hasAttribute(Attributes::OptimizeForSize))
182 Result += "optsize ";
183 if (hasAttribute(Attributes::NoInline))
184 Result += "noinline ";
185 if (hasAttribute(Attributes::InlineHint))
186 Result += "inlinehint ";
187 if (hasAttribute(Attributes::AlwaysInline))
188 Result += "alwaysinline ";
189 if (hasAttribute(Attributes::StackProtect))
191 if (hasAttribute(Attributes::StackProtectReq))
193 if (hasAttribute(Attributes::NoRedZone))
194 Result += "noredzone ";
195 if (hasAttribute(Attributes::NoImplicitFloat))
196 Result += "noimplicitfloat ";
197 if (hasAttribute(Attributes::Naked))
199 if (hasAttribute(Attributes::NonLazyBind))
200 Result += "nonlazybind ";
201 if (hasAttribute(Attributes::AddressSafety))
202 Result += "address_safety ";
203 if (hasAttribute(Attributes::StackAlignment)) {
204 Result += "alignstack(";
205 Result += utostr(getStackAlignment());
208 if (hasAttribute(Attributes::Alignment)) {
210 Result += utostr(getAlignment());
213 // Trim the trailing space.
214 assert(!Result.empty() && "Unknown attribute!");
215 Result.erase(Result.end()-1);
219 //===----------------------------------------------------------------------===//
220 // AttrBuilder Implementation
221 //===----------------------------------------------------------------------===//
223 AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){
224 Bits |= AttributesImpl::getAttrMask(Val);
228 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
233 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
234 if (Align == 0) return *this;
235 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
236 assert(Align <= 0x40000000 && "Alignment too large.");
237 Bits |= (Log2_32(Align) + 1) << 16;
240 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
241 // Default alignment, allow the target to define how to align it.
242 if (Align == 0) return *this;
243 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
244 assert(Align <= 0x100 && "Alignment too large.");
245 Bits |= (Log2_32(Align) + 1) << 26;
249 AttrBuilder &AttrBuilder::removeAttribute(Attributes::AttrVal Val) {
250 Bits &= ~AttributesImpl::getAttrMask(Val);
254 AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) {
259 AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){
264 bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const {
265 return Bits & AttributesImpl::getAttrMask(A);
268 bool AttrBuilder::hasAttributes() const {
271 bool AttrBuilder::hasAttributes(const Attributes &A) const {
272 return Bits & A.Raw();
274 bool AttrBuilder::hasAlignmentAttr() const {
275 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
278 uint64_t AttrBuilder::getAlignment() const {
279 if (!hasAlignmentAttr())
282 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
285 uint64_t AttrBuilder::getStackAlignment() const {
286 if (!hasAlignmentAttr())
289 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
292 //===----------------------------------------------------------------------===//
293 // AttributeImpl Definition
294 //===----------------------------------------------------------------------===//
296 uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
298 case Attributes::None: return 0;
299 case Attributes::ZExt: return 1 << 0;
300 case Attributes::SExt: return 1 << 1;
301 case Attributes::NoReturn: return 1 << 2;
302 case Attributes::InReg: return 1 << 3;
303 case Attributes::StructRet: return 1 << 4;
304 case Attributes::NoUnwind: return 1 << 5;
305 case Attributes::NoAlias: return 1 << 6;
306 case Attributes::ByVal: return 1 << 7;
307 case Attributes::Nest: return 1 << 8;
308 case Attributes::ReadNone: return 1 << 9;
309 case Attributes::ReadOnly: return 1 << 10;
310 case Attributes::NoInline: return 1 << 11;
311 case Attributes::AlwaysInline: return 1 << 12;
312 case Attributes::OptimizeForSize: return 1 << 13;
313 case Attributes::StackProtect: return 1 << 14;
314 case Attributes::StackProtectReq: return 1 << 15;
315 case Attributes::Alignment: return 31 << 16;
316 case Attributes::NoCapture: return 1 << 21;
317 case Attributes::NoRedZone: return 1 << 22;
318 case Attributes::NoImplicitFloat: return 1 << 23;
319 case Attributes::Naked: return 1 << 24;
320 case Attributes::InlineHint: return 1 << 25;
321 case Attributes::StackAlignment: return 7 << 26;
322 case Attributes::ReturnsTwice: return 1 << 29;
323 case Attributes::UWTable: return 1 << 30;
324 case Attributes::NonLazyBind: return 1U << 31;
325 case Attributes::AddressSafety: return 1ULL << 32;
327 llvm_unreachable("Unsupported attribute type");
330 bool AttributesImpl::hasAttribute(uint64_t A) const {
331 return (Bits & getAttrMask(A)) != 0;
334 bool AttributesImpl::hasAttributes() const {
338 bool AttributesImpl::hasAttributes(const Attributes &A) const {
339 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
342 uint64_t AttributesImpl::getAlignment() const {
343 return Bits & getAttrMask(Attributes::Alignment);
346 uint64_t AttributesImpl::getStackAlignment() const {
347 return Bits & getAttrMask(Attributes::StackAlignment);
350 //===----------------------------------------------------------------------===//
351 // AttributeListImpl Definition
352 //===----------------------------------------------------------------------===//
355 class AttributeListImpl;
358 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
361 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
363 class AttributeListImpl : public FoldingSetNode {
364 sys::cas_flag RefCount;
366 // AttributesList is uniqued, these should not be publicly available.
367 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
368 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
369 ~AttributeListImpl(); // Private implementation
371 SmallVector<AttributeWithIndex, 4> Attrs;
373 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
374 : Attrs(attrs.begin(), attrs.end()) {
379 sys::SmartScopedLock<true> Lock(*ALMutex);
383 sys::SmartScopedLock<true> Lock(*ALMutex);
384 if (!AttributesLists.isConstructed())
386 sys::cas_flag new_val = --RefCount;
391 void Profile(FoldingSetNodeID &ID) const {
394 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
395 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
396 ID.AddInteger(Attrs[i].Attrs.Raw());
397 ID.AddInteger(Attrs[i].Index);
403 AttributeListImpl::~AttributeListImpl() {
404 // NOTE: Lock must be acquired by caller.
405 AttributesLists->RemoveNode(this);
409 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
410 // If there are no attributes then return a null AttributesList pointer.
412 return AttrListPtr();
415 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
416 assert(Attrs[i].Attrs.hasAttributes() &&
417 "Pointless attribute!");
418 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
419 "Misordered AttributesList!");
423 // Otherwise, build a key to look up the existing attributes.
425 AttributeListImpl::Profile(ID, Attrs);
428 sys::SmartScopedLock<true> Lock(*ALMutex);
430 AttributeListImpl *PAL =
431 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
433 // If we didn't find any existing attributes of the same shape then
434 // create a new one and insert it.
436 PAL = new AttributeListImpl(Attrs);
437 AttributesLists->InsertNode(PAL, InsertPos);
440 // Return the AttributesList that we found or created.
441 return AttrListPtr(PAL);
445 //===----------------------------------------------------------------------===//
446 // AttrListPtr Method Implementations
447 //===----------------------------------------------------------------------===//
449 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
450 if (LI) LI->AddRef();
453 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
454 if (AttrList) AttrList->AddRef();
457 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
458 sys::SmartScopedLock<true> Lock(*ALMutex);
459 if (AttrList == RHS.AttrList) return *this;
460 if (AttrList) AttrList->DropRef();
461 AttrList = RHS.AttrList;
462 if (AttrList) AttrList->AddRef();
466 AttrListPtr::~AttrListPtr() {
467 if (AttrList) AttrList->DropRef();
470 /// getNumSlots - Return the number of slots used in this attribute list.
471 /// This is the number of arguments that have an attribute set on them
472 /// (including the function itself).
473 unsigned AttrListPtr::getNumSlots() const {
474 return AttrList ? AttrList->Attrs.size() : 0;
477 /// getSlot - Return the AttributeWithIndex at the specified slot. This
478 /// holds a number plus a set of attributes.
479 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
480 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
481 return AttrList->Attrs[Slot];
485 /// getAttributes - The attributes for the specified index are
486 /// returned. Attributes for the result are denoted with Idx = 0.
487 /// Function notes are denoted with idx = ~0.
488 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
489 if (AttrList == 0) return Attributes();
491 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
492 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
493 if (Attrs[i].Index == Idx)
494 return Attrs[i].Attrs;
499 /// hasAttrSomewhere - Return true if the specified attribute is set for at
500 /// least one parameter or for the return value.
501 bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
502 if (AttrList == 0) return false;
504 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
505 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
506 if (Attrs[i].Attrs.hasAttribute(Attr))
511 unsigned AttrListPtr::getNumAttrs() const {
512 return AttrList ? AttrList->Attrs.size() : 0;
515 Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
516 assert(AttrList && "Trying to get an attribute from an empty list!");
517 assert(i < AttrList->Attrs.size() && "Index out of range!");
518 return AttrList->Attrs[i].Attrs;
521 AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
522 Attributes Attrs) const {
523 Attributes OldAttrs = getAttributes(Idx);
525 // FIXME it is not obvious how this should work for alignment.
526 // For now, say we can't change a known alignment.
527 unsigned OldAlign = OldAttrs.getAlignment();
528 unsigned NewAlign = Attrs.getAlignment();
529 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
530 "Attempt to change alignment!");
533 AttrBuilder NewAttrs =
534 AttrBuilder(OldAttrs).addAttributes(Attrs);
535 if (NewAttrs == AttrBuilder(OldAttrs))
538 SmallVector<AttributeWithIndex, 8> NewAttrList;
540 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
542 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
543 unsigned i = 0, e = OldAttrList.size();
544 // Copy attributes for arguments before this one.
545 for (; i != e && OldAttrList[i].Index < Idx; ++i)
546 NewAttrList.push_back(OldAttrList[i]);
548 // If there are attributes already at this index, merge them in.
549 if (i != e && OldAttrList[i].Index == Idx) {
551 Attributes::get(C, AttrBuilder(Attrs).
552 addAttributes(OldAttrList[i].Attrs));
556 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
558 // Copy attributes for arguments after this one.
559 NewAttrList.insert(NewAttrList.end(),
560 OldAttrList.begin()+i, OldAttrList.end());
563 return get(NewAttrList);
566 AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
567 Attributes Attrs) const {
569 // FIXME it is not obvious how this should work for alignment.
570 // For now, say we can't pass in alignment, which no current use does.
571 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
572 "Attempt to exclude alignment!");
574 if (AttrList == 0) return AttrListPtr();
576 Attributes OldAttrs = getAttributes(Idx);
577 AttrBuilder NewAttrs =
578 AttrBuilder(OldAttrs).removeAttributes(Attrs);
579 if (NewAttrs == AttrBuilder(OldAttrs))
582 SmallVector<AttributeWithIndex, 8> NewAttrList;
583 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
584 unsigned i = 0, e = OldAttrList.size();
586 // Copy attributes for arguments before this one.
587 for (; i != e && OldAttrList[i].Index < Idx; ++i)
588 NewAttrList.push_back(OldAttrList[i]);
590 // If there are attributes already at this index, merge them in.
591 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
592 Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs).
593 removeAttributes(Attrs));
595 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
596 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
598 // Copy attributes for arguments after this one.
599 NewAttrList.insert(NewAttrList.end(),
600 OldAttrList.begin()+i, OldAttrList.end());
602 return get(NewAttrList);
605 void AttrListPtr::dump() const {
607 for (unsigned i = 0; i < getNumSlots(); ++i) {
608 const AttributeWithIndex &PAWI = getSlot(i);
609 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";