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 "LLVMContextImpl.h"
16 #include "llvm/Type.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/Support/Atomic.h"
20 #include "llvm/Support/Mutex.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/raw_ostream.h"
26 //===----------------------------------------------------------------------===//
27 // Attributes Implementation
28 //===----------------------------------------------------------------------===//
30 Attributes::Attributes(uint64_t Val) : Attrs(Val) {}
32 Attributes::Attributes(LLVMContext &C, AttrVal Val)
33 : Attrs(Attributes::get(Attributes::Builder().addAttribute(Val)).Attrs) {}
35 Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
37 Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
39 // FIXME: This is temporary until we have implemented the uniquified version of
41 Attributes Attributes::get(Attributes::Builder &B) {
42 return Attributes(B.Bits);
45 Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
46 // If there are no attributes, return an empty Attributes class.
50 // Otherwise, build a key to look up the existing attributes.
51 LLVMContextImpl *pImpl = Context.pImpl;
53 ID.AddInteger(B.Bits);
56 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
59 // If we didn't find any existing attributes of the same shape then create a
60 // new one and insert it.
61 PA = new AttributesImpl(B.Bits);
62 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
65 // Return the AttributesList that we found or created.
66 return Attributes(PA);
69 bool Attributes::hasAttribute(AttrVal Val) const {
70 return Attrs.hasAttribute(Val);
73 bool Attributes::hasAttributes(const Attributes &A) const {
74 return Attrs.hasAttributes(A);
77 /// This returns the alignment field of an attribute as a byte alignment value.
78 unsigned Attributes::getAlignment() const {
79 if (!hasAttribute(Attributes::Alignment))
81 return 1U << ((Attrs.getAlignment() >> 16) - 1);
84 /// This returns the stack alignment field of an attribute as a byte alignment
86 unsigned Attributes::getStackAlignment() const {
87 if (!hasAttribute(Attributes::StackAlignment))
89 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
92 bool Attributes::isEmptyOrSingleton() const {
93 return Attrs.isEmptyOrSingleton();
96 Attributes Attributes::operator & (const Attributes &A) const {
97 return Attributes(Raw() & A.Raw());
99 Attributes &Attributes::operator &= (const Attributes &A) {
100 Attrs.Bits &= A.Raw();
104 uint64_t Attributes::Raw() const {
108 Attributes Attributes::typeIncompatible(Type *Ty) {
109 Attributes::Builder Incompatible;
111 if (!Ty->isIntegerTy())
112 // Attributes that only apply to integers.
113 Incompatible.addAttribute(Attributes::SExt)
114 .addAttribute(Attributes::ZExt);
116 if (!Ty->isPointerTy())
117 // Attributes that only apply to pointers.
118 Incompatible.addAttribute(Attributes::ByVal)
119 .addAttribute(Attributes::Nest)
120 .addAttribute(Attributes::NoAlias)
121 .addAttribute(Attributes::NoCapture)
122 .addAttribute(Attributes::StructRet);
124 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
127 std::string Attributes::getAsString() const {
129 if (hasAttribute(Attributes::ZExt))
130 Result += "zeroext ";
131 if (hasAttribute(Attributes::SExt))
132 Result += "signext ";
133 if (hasAttribute(Attributes::NoReturn))
134 Result += "noreturn ";
135 if (hasAttribute(Attributes::NoUnwind))
136 Result += "nounwind ";
137 if (hasAttribute(Attributes::UWTable))
138 Result += "uwtable ";
139 if (hasAttribute(Attributes::ReturnsTwice))
140 Result += "returns_twice ";
141 if (hasAttribute(Attributes::InReg))
143 if (hasAttribute(Attributes::NoAlias))
144 Result += "noalias ";
145 if (hasAttribute(Attributes::NoCapture))
146 Result += "nocapture ";
147 if (hasAttribute(Attributes::StructRet))
149 if (hasAttribute(Attributes::ByVal))
151 if (hasAttribute(Attributes::Nest))
153 if (hasAttribute(Attributes::ReadNone))
154 Result += "readnone ";
155 if (hasAttribute(Attributes::ReadOnly))
156 Result += "readonly ";
157 if (hasAttribute(Attributes::OptimizeForSize))
158 Result += "optsize ";
159 if (hasAttribute(Attributes::NoInline))
160 Result += "noinline ";
161 if (hasAttribute(Attributes::InlineHint))
162 Result += "inlinehint ";
163 if (hasAttribute(Attributes::AlwaysInline))
164 Result += "alwaysinline ";
165 if (hasAttribute(Attributes::StackProtect))
167 if (hasAttribute(Attributes::StackProtectReq))
169 if (hasAttribute(Attributes::NoRedZone))
170 Result += "noredzone ";
171 if (hasAttribute(Attributes::NoImplicitFloat))
172 Result += "noimplicitfloat ";
173 if (hasAttribute(Attributes::Naked))
175 if (hasAttribute(Attributes::NonLazyBind))
176 Result += "nonlazybind ";
177 if (hasAttribute(Attributes::AddressSafety))
178 Result += "address_safety ";
179 if (hasAttribute(Attributes::StackAlignment)) {
180 Result += "alignstack(";
181 Result += utostr(getStackAlignment());
184 if (hasAttribute(Attributes::Alignment)) {
186 Result += utostr(getAlignment());
189 // Trim the trailing space.
190 assert(!Result.empty() && "Unknown attribute!");
191 Result.erase(Result.end()-1);
195 //===----------------------------------------------------------------------===//
196 // Attributes::Builder Implementation
197 //===----------------------------------------------------------------------===//
199 Attributes::Builder &Attributes::Builder::
200 addAttribute(Attributes::AttrVal Val) {
201 Bits |= AttributesImpl::getAttrMask(Val);
205 Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
210 Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
211 if (Align == 0) return *this;
212 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
213 assert(Align <= 0x40000000 && "Alignment too large.");
214 Bits |= (Log2_32(Align) + 1) << 16;
217 Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
218 // Default alignment, allow the target to define how to align it.
219 if (Align == 0) return *this;
220 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
221 assert(Align <= 0x100 && "Alignment too large.");
222 Bits |= (Log2_32(Align) + 1) << 26;
226 Attributes::Builder &Attributes::Builder::
227 removeAttribute(Attributes::AttrVal Val) {
228 Bits &= ~AttributesImpl::getAttrMask(Val);
232 Attributes::Builder &Attributes::Builder::addAttributes(const Attributes &A) {
237 Attributes::Builder &Attributes::Builder::removeAttributes(const Attributes &A){
242 bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
243 return Bits & AttributesImpl::getAttrMask(A);
246 bool Attributes::Builder::hasAttributes() const {
249 bool Attributes::Builder::hasAttributes(const Attributes &A) const {
250 return Bits & A.Raw();
252 bool Attributes::Builder::hasAlignmentAttr() const {
253 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
256 uint64_t Attributes::Builder::getAlignment() const {
257 if (!hasAlignmentAttr())
260 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
263 uint64_t Attributes::Builder::getStackAlignment() const {
264 if (!hasAlignmentAttr())
267 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
270 //===----------------------------------------------------------------------===//
271 // AttributeImpl Definition
272 //===----------------------------------------------------------------------===//
274 uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
276 case Attributes::None: return 0;
277 case Attributes::ZExt: return 1 << 0;
278 case Attributes::SExt: return 1 << 1;
279 case Attributes::NoReturn: return 1 << 2;
280 case Attributes::InReg: return 1 << 3;
281 case Attributes::StructRet: return 1 << 4;
282 case Attributes::NoUnwind: return 1 << 5;
283 case Attributes::NoAlias: return 1 << 6;
284 case Attributes::ByVal: return 1 << 7;
285 case Attributes::Nest: return 1 << 8;
286 case Attributes::ReadNone: return 1 << 9;
287 case Attributes::ReadOnly: return 1 << 10;
288 case Attributes::NoInline: return 1 << 11;
289 case Attributes::AlwaysInline: return 1 << 12;
290 case Attributes::OptimizeForSize: return 1 << 13;
291 case Attributes::StackProtect: return 1 << 14;
292 case Attributes::StackProtectReq: return 1 << 15;
293 case Attributes::Alignment: return 31 << 16;
294 case Attributes::NoCapture: return 1 << 21;
295 case Attributes::NoRedZone: return 1 << 22;
296 case Attributes::NoImplicitFloat: return 1 << 23;
297 case Attributes::Naked: return 1 << 24;
298 case Attributes::InlineHint: return 1 << 25;
299 case Attributes::StackAlignment: return 7 << 26;
300 case Attributes::ReturnsTwice: return 1 << 29;
301 case Attributes::UWTable: return 1 << 30;
302 case Attributes::NonLazyBind: return 1U << 31;
303 case Attributes::AddressSafety: return 1ULL << 32;
305 llvm_unreachable("Unsupported attribute type");
308 bool AttributesImpl::hasAttribute(uint64_t A) const {
309 return (Bits & getAttrMask(A)) != 0;
312 bool AttributesImpl::hasAttributes() const {
316 bool AttributesImpl::hasAttributes(const Attributes &A) const {
317 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
320 uint64_t AttributesImpl::getAlignment() const {
321 return Bits & getAttrMask(Attributes::Alignment);
324 uint64_t AttributesImpl::getStackAlignment() const {
325 return Bits & getAttrMask(Attributes::StackAlignment);
328 bool AttributesImpl::isEmptyOrSingleton() const {
329 return (Bits & (Bits - 1)) == 0;
332 //===----------------------------------------------------------------------===//
333 // AttributeListImpl Definition
334 //===----------------------------------------------------------------------===//
337 class AttributeListImpl;
340 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
343 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
345 class AttributeListImpl : public FoldingSetNode {
346 sys::cas_flag RefCount;
348 // AttributesList is uniqued, these should not be publicly available.
349 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
350 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
351 ~AttributeListImpl(); // Private implementation
353 SmallVector<AttributeWithIndex, 4> Attrs;
355 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
356 : Attrs(attrs.begin(), attrs.end()) {
361 sys::SmartScopedLock<true> Lock(*ALMutex);
365 sys::SmartScopedLock<true> Lock(*ALMutex);
366 if (!AttributesLists.isConstructed())
368 sys::cas_flag new_val = --RefCount;
373 void Profile(FoldingSetNodeID &ID) const {
376 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
377 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
378 ID.AddInteger(Attrs[i].Attrs.Raw());
379 ID.AddInteger(Attrs[i].Index);
385 AttributeListImpl::~AttributeListImpl() {
386 // NOTE: Lock must be acquired by caller.
387 AttributesLists->RemoveNode(this);
391 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
392 // If there are no attributes then return a null AttributesList pointer.
394 return AttrListPtr();
397 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
398 assert(Attrs[i].Attrs.hasAttributes() &&
399 "Pointless attribute!");
400 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
401 "Misordered AttributesList!");
405 // Otherwise, build a key to look up the existing attributes.
407 AttributeListImpl::Profile(ID, Attrs);
410 sys::SmartScopedLock<true> Lock(*ALMutex);
412 AttributeListImpl *PAL =
413 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
415 // If we didn't find any existing attributes of the same shape then
416 // create a new one and insert it.
418 PAL = new AttributeListImpl(Attrs);
419 AttributesLists->InsertNode(PAL, InsertPos);
422 // Return the AttributesList that we found or created.
423 return AttrListPtr(PAL);
427 //===----------------------------------------------------------------------===//
428 // AttrListPtr Method Implementations
429 //===----------------------------------------------------------------------===//
431 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
432 if (LI) LI->AddRef();
435 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
436 if (AttrList) AttrList->AddRef();
439 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
440 sys::SmartScopedLock<true> Lock(*ALMutex);
441 if (AttrList == RHS.AttrList) return *this;
442 if (AttrList) AttrList->DropRef();
443 AttrList = RHS.AttrList;
444 if (AttrList) AttrList->AddRef();
448 AttrListPtr::~AttrListPtr() {
449 if (AttrList) AttrList->DropRef();
452 /// getNumSlots - Return the number of slots used in this attribute list.
453 /// This is the number of arguments that have an attribute set on them
454 /// (including the function itself).
455 unsigned AttrListPtr::getNumSlots() const {
456 return AttrList ? AttrList->Attrs.size() : 0;
459 /// getSlot - Return the AttributeWithIndex at the specified slot. This
460 /// holds a number plus a set of attributes.
461 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
462 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
463 return AttrList->Attrs[Slot];
467 /// getAttributes - The attributes for the specified index are
468 /// returned. Attributes for the result are denoted with Idx = 0.
469 /// Function notes are denoted with idx = ~0.
470 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
471 if (AttrList == 0) return Attributes();
473 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
474 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
475 if (Attrs[i].Index == Idx)
476 return Attrs[i].Attrs;
481 /// hasAttrSomewhere - Return true if the specified attribute is set for at
482 /// least one parameter or for the return value.
483 bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
484 if (AttrList == 0) return false;
486 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
487 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
488 if (Attrs[i].Attrs.hasAttribute(Attr))
493 unsigned AttrListPtr::getNumAttrs() const {
494 return AttrList ? AttrList->Attrs.size() : 0;
497 Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
498 assert(AttrList && "Trying to get an attribute from an empty list!");
499 assert(i < AttrList->Attrs.size() && "Index out of range!");
500 return AttrList->Attrs[i].Attrs;
503 AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
504 Attributes Attrs) const {
505 Attributes OldAttrs = getAttributes(Idx);
507 // FIXME it is not obvious how this should work for alignment.
508 // For now, say we can't change a known alignment.
509 unsigned OldAlign = OldAttrs.getAlignment();
510 unsigned NewAlign = Attrs.getAlignment();
511 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
512 "Attempt to change alignment!");
515 Attributes::Builder NewAttrs =
516 Attributes::Builder(OldAttrs).addAttributes(Attrs);
517 if (NewAttrs == Attributes::Builder(OldAttrs))
520 SmallVector<AttributeWithIndex, 8> NewAttrList;
522 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
524 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
525 unsigned i = 0, e = OldAttrList.size();
526 // Copy attributes for arguments before this one.
527 for (; i != e && OldAttrList[i].Index < Idx; ++i)
528 NewAttrList.push_back(OldAttrList[i]);
530 // If there are attributes already at this index, merge them in.
531 if (i != e && OldAttrList[i].Index == Idx) {
533 Attributes::get(C, Attributes::Builder(Attrs).
534 addAttributes(OldAttrList[i].Attrs));
538 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
540 // Copy attributes for arguments after this one.
541 NewAttrList.insert(NewAttrList.end(),
542 OldAttrList.begin()+i, OldAttrList.end());
545 return get(NewAttrList);
548 AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
549 Attributes Attrs) const {
551 // FIXME it is not obvious how this should work for alignment.
552 // For now, say we can't pass in alignment, which no current use does.
553 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
554 "Attempt to exclude alignment!");
556 if (AttrList == 0) return AttrListPtr();
558 Attributes OldAttrs = getAttributes(Idx);
559 Attributes::Builder NewAttrs =
560 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
561 if (NewAttrs == Attributes::Builder(OldAttrs))
564 SmallVector<AttributeWithIndex, 8> NewAttrList;
565 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
566 unsigned i = 0, e = OldAttrList.size();
568 // Copy attributes for arguments before this one.
569 for (; i != e && OldAttrList[i].Index < Idx; ++i)
570 NewAttrList.push_back(OldAttrList[i]);
572 // If there are attributes already at this index, merge them in.
573 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
574 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
575 removeAttributes(Attrs));
577 if (Attrs) // If any attributes left for this parameter, add them.
578 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
580 // Copy attributes for arguments after this one.
581 NewAttrList.insert(NewAttrList.end(),
582 OldAttrList.begin()+i, OldAttrList.end());
584 return get(NewAttrList);
587 void AttrListPtr::dump() const {
589 for (unsigned i = 0; i < getNumSlots(); ++i) {
590 const AttributeWithIndex &PAWI = getSlot(i);
591 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";