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(LLVMContext &C, ArrayRef<AttrVal> Vals) {
31 Attributes::Builder B;
32 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
35 Attrs = Attributes::get(C, B).Attrs;
38 Attributes::Attributes(AttributesImpl *A) : Attrs(A) {}
40 Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
42 Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
43 // If there are no attributes, return an empty Attributes class.
47 // Otherwise, build a key to look up the existing attributes.
48 LLVMContextImpl *pImpl = Context.pImpl;
50 ID.AddInteger(B.Bits);
53 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
56 // If we didn't find any existing attributes of the same shape then create a
57 // new one and insert it.
58 PA = new AttributesImpl(B.Bits);
59 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
62 // Return the AttributesList that we found or created.
63 return Attributes(PA);
66 bool Attributes::hasAttribute(AttrVal Val) const {
67 return Attrs && Attrs->hasAttribute(Val);
70 bool Attributes::hasAttributes(const Attributes &A) const {
71 return Attrs && Attrs->hasAttributes(A);
74 /// This returns the alignment field of an attribute as a byte alignment value.
75 unsigned Attributes::getAlignment() const {
76 if (!hasAttribute(Attributes::Alignment))
78 return 1U << ((Attrs->getAlignment() >> 16) - 1);
81 /// This returns the stack alignment field of an attribute as a byte alignment
83 unsigned Attributes::getStackAlignment() const {
84 if (!hasAttribute(Attributes::StackAlignment))
86 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
89 uint64_t Attributes::Raw() const {
90 return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly!
93 Attributes Attributes::typeIncompatible(Type *Ty) {
94 Attributes::Builder Incompatible;
96 if (!Ty->isIntegerTy())
97 // Attributes that only apply to integers.
98 Incompatible.addAttribute(Attributes::SExt)
99 .addAttribute(Attributes::ZExt);
101 if (!Ty->isPointerTy())
102 // Attributes that only apply to pointers.
103 Incompatible.addAttribute(Attributes::ByVal)
104 .addAttribute(Attributes::Nest)
105 .addAttribute(Attributes::NoAlias)
106 .addAttribute(Attributes::NoCapture)
107 .addAttribute(Attributes::StructRet);
109 return Attributes::get(Ty->getContext(), Incompatible);
112 std::string Attributes::getAsString() const {
114 if (hasAttribute(Attributes::ZExt))
115 Result += "zeroext ";
116 if (hasAttribute(Attributes::SExt))
117 Result += "signext ";
118 if (hasAttribute(Attributes::NoReturn))
119 Result += "noreturn ";
120 if (hasAttribute(Attributes::NoUnwind))
121 Result += "nounwind ";
122 if (hasAttribute(Attributes::UWTable))
123 Result += "uwtable ";
124 if (hasAttribute(Attributes::ReturnsTwice))
125 Result += "returns_twice ";
126 if (hasAttribute(Attributes::InReg))
128 if (hasAttribute(Attributes::NoAlias))
129 Result += "noalias ";
130 if (hasAttribute(Attributes::NoCapture))
131 Result += "nocapture ";
132 if (hasAttribute(Attributes::StructRet))
134 if (hasAttribute(Attributes::ByVal))
136 if (hasAttribute(Attributes::Nest))
138 if (hasAttribute(Attributes::ReadNone))
139 Result += "readnone ";
140 if (hasAttribute(Attributes::ReadOnly))
141 Result += "readonly ";
142 if (hasAttribute(Attributes::OptimizeForSize))
143 Result += "optsize ";
144 if (hasAttribute(Attributes::NoInline))
145 Result += "noinline ";
146 if (hasAttribute(Attributes::InlineHint))
147 Result += "inlinehint ";
148 if (hasAttribute(Attributes::AlwaysInline))
149 Result += "alwaysinline ";
150 if (hasAttribute(Attributes::StackProtect))
152 if (hasAttribute(Attributes::StackProtectReq))
154 if (hasAttribute(Attributes::NoRedZone))
155 Result += "noredzone ";
156 if (hasAttribute(Attributes::NoImplicitFloat))
157 Result += "noimplicitfloat ";
158 if (hasAttribute(Attributes::Naked))
160 if (hasAttribute(Attributes::NonLazyBind))
161 Result += "nonlazybind ";
162 if (hasAttribute(Attributes::AddressSafety))
163 Result += "address_safety ";
164 if (hasAttribute(Attributes::StackAlignment)) {
165 Result += "alignstack(";
166 Result += utostr(getStackAlignment());
169 if (hasAttribute(Attributes::Alignment)) {
171 Result += utostr(getAlignment());
174 // Trim the trailing space.
175 assert(!Result.empty() && "Unknown attribute!");
176 Result.erase(Result.end()-1);
180 //===----------------------------------------------------------------------===//
181 // Attributes::Builder Implementation
182 //===----------------------------------------------------------------------===//
184 Attributes::Builder &Attributes::Builder::
185 addAttribute(Attributes::AttrVal Val) {
186 Bits |= AttributesImpl::getAttrMask(Val);
190 Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
195 Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
196 if (Align == 0) return *this;
197 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
198 assert(Align <= 0x40000000 && "Alignment too large.");
199 Bits |= (Log2_32(Align) + 1) << 16;
202 Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
203 // Default alignment, allow the target to define how to align it.
204 if (Align == 0) return *this;
205 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
206 assert(Align <= 0x100 && "Alignment too large.");
207 Bits |= (Log2_32(Align) + 1) << 26;
211 Attributes::Builder &Attributes::Builder::
212 removeAttribute(Attributes::AttrVal Val) {
213 Bits &= ~AttributesImpl::getAttrMask(Val);
217 Attributes::Builder &Attributes::Builder::addAttributes(const Attributes &A) {
222 Attributes::Builder &Attributes::Builder::removeAttributes(const Attributes &A){
227 bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
228 return Bits & AttributesImpl::getAttrMask(A);
231 bool Attributes::Builder::hasAttributes() const {
234 bool Attributes::Builder::hasAttributes(const Attributes &A) const {
235 return Bits & A.Raw();
237 bool Attributes::Builder::hasAlignmentAttr() const {
238 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
241 uint64_t Attributes::Builder::getAlignment() const {
242 if (!hasAlignmentAttr())
245 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
248 uint64_t Attributes::Builder::getStackAlignment() const {
249 if (!hasAlignmentAttr())
252 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
255 //===----------------------------------------------------------------------===//
256 // AttributeImpl Definition
257 //===----------------------------------------------------------------------===//
259 uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
261 case Attributes::None: return 0;
262 case Attributes::ZExt: return 1 << 0;
263 case Attributes::SExt: return 1 << 1;
264 case Attributes::NoReturn: return 1 << 2;
265 case Attributes::InReg: return 1 << 3;
266 case Attributes::StructRet: return 1 << 4;
267 case Attributes::NoUnwind: return 1 << 5;
268 case Attributes::NoAlias: return 1 << 6;
269 case Attributes::ByVal: return 1 << 7;
270 case Attributes::Nest: return 1 << 8;
271 case Attributes::ReadNone: return 1 << 9;
272 case Attributes::ReadOnly: return 1 << 10;
273 case Attributes::NoInline: return 1 << 11;
274 case Attributes::AlwaysInline: return 1 << 12;
275 case Attributes::OptimizeForSize: return 1 << 13;
276 case Attributes::StackProtect: return 1 << 14;
277 case Attributes::StackProtectReq: return 1 << 15;
278 case Attributes::Alignment: return 31 << 16;
279 case Attributes::NoCapture: return 1 << 21;
280 case Attributes::NoRedZone: return 1 << 22;
281 case Attributes::NoImplicitFloat: return 1 << 23;
282 case Attributes::Naked: return 1 << 24;
283 case Attributes::InlineHint: return 1 << 25;
284 case Attributes::StackAlignment: return 7 << 26;
285 case Attributes::ReturnsTwice: return 1 << 29;
286 case Attributes::UWTable: return 1 << 30;
287 case Attributes::NonLazyBind: return 1U << 31;
288 case Attributes::AddressSafety: return 1ULL << 32;
290 llvm_unreachable("Unsupported attribute type");
293 bool AttributesImpl::hasAttribute(uint64_t A) const {
294 return (Bits & getAttrMask(A)) != 0;
297 bool AttributesImpl::hasAttributes() const {
301 bool AttributesImpl::hasAttributes(const Attributes &A) const {
302 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
305 uint64_t AttributesImpl::getAlignment() const {
306 return Bits & getAttrMask(Attributes::Alignment);
309 uint64_t AttributesImpl::getStackAlignment() const {
310 return Bits & getAttrMask(Attributes::StackAlignment);
313 //===----------------------------------------------------------------------===//
314 // AttributeListImpl Definition
315 //===----------------------------------------------------------------------===//
318 class AttributeListImpl;
321 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
324 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
326 class AttributeListImpl : public FoldingSetNode {
327 sys::cas_flag RefCount;
329 // AttributesList is uniqued, these should not be publicly available.
330 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
331 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
332 ~AttributeListImpl(); // Private implementation
334 SmallVector<AttributeWithIndex, 4> Attrs;
336 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
337 : Attrs(attrs.begin(), attrs.end()) {
342 sys::SmartScopedLock<true> Lock(*ALMutex);
346 sys::SmartScopedLock<true> Lock(*ALMutex);
347 if (!AttributesLists.isConstructed())
349 sys::cas_flag new_val = --RefCount;
354 void Profile(FoldingSetNodeID &ID) const {
357 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
358 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
359 ID.AddInteger(Attrs[i].Attrs.Raw());
360 ID.AddInteger(Attrs[i].Index);
366 AttributeListImpl::~AttributeListImpl() {
367 // NOTE: Lock must be acquired by caller.
368 AttributesLists->RemoveNode(this);
372 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
373 // If there are no attributes then return a null AttributesList pointer.
375 return AttrListPtr();
378 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
379 assert(Attrs[i].Attrs.hasAttributes() &&
380 "Pointless attribute!");
381 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
382 "Misordered AttributesList!");
386 // Otherwise, build a key to look up the existing attributes.
388 AttributeListImpl::Profile(ID, Attrs);
391 sys::SmartScopedLock<true> Lock(*ALMutex);
393 AttributeListImpl *PAL =
394 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
396 // If we didn't find any existing attributes of the same shape then
397 // create a new one and insert it.
399 PAL = new AttributeListImpl(Attrs);
400 AttributesLists->InsertNode(PAL, InsertPos);
403 // Return the AttributesList that we found or created.
404 return AttrListPtr(PAL);
408 //===----------------------------------------------------------------------===//
409 // AttrListPtr Method Implementations
410 //===----------------------------------------------------------------------===//
412 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
413 if (LI) LI->AddRef();
416 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
417 if (AttrList) AttrList->AddRef();
420 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
421 sys::SmartScopedLock<true> Lock(*ALMutex);
422 if (AttrList == RHS.AttrList) return *this;
423 if (AttrList) AttrList->DropRef();
424 AttrList = RHS.AttrList;
425 if (AttrList) AttrList->AddRef();
429 AttrListPtr::~AttrListPtr() {
430 if (AttrList) AttrList->DropRef();
433 /// getNumSlots - Return the number of slots used in this attribute list.
434 /// This is the number of arguments that have an attribute set on them
435 /// (including the function itself).
436 unsigned AttrListPtr::getNumSlots() const {
437 return AttrList ? AttrList->Attrs.size() : 0;
440 /// getSlot - Return the AttributeWithIndex at the specified slot. This
441 /// holds a number plus a set of attributes.
442 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
443 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
444 return AttrList->Attrs[Slot];
448 /// getAttributes - The attributes for the specified index are
449 /// returned. Attributes for the result are denoted with Idx = 0.
450 /// Function notes are denoted with idx = ~0.
451 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
452 if (AttrList == 0) return Attributes();
454 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
455 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
456 if (Attrs[i].Index == Idx)
457 return Attrs[i].Attrs;
462 /// hasAttrSomewhere - Return true if the specified attribute is set for at
463 /// least one parameter or for the return value.
464 bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
465 if (AttrList == 0) return false;
467 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
468 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
469 if (Attrs[i].Attrs.hasAttribute(Attr))
474 unsigned AttrListPtr::getNumAttrs() const {
475 return AttrList ? AttrList->Attrs.size() : 0;
478 Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
479 assert(AttrList && "Trying to get an attribute from an empty list!");
480 assert(i < AttrList->Attrs.size() && "Index out of range!");
481 return AttrList->Attrs[i].Attrs;
484 AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
485 Attributes Attrs) const {
486 Attributes OldAttrs = getAttributes(Idx);
488 // FIXME it is not obvious how this should work for alignment.
489 // For now, say we can't change a known alignment.
490 unsigned OldAlign = OldAttrs.getAlignment();
491 unsigned NewAlign = Attrs.getAlignment();
492 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
493 "Attempt to change alignment!");
496 Attributes::Builder NewAttrs =
497 Attributes::Builder(OldAttrs).addAttributes(Attrs);
498 if (NewAttrs == Attributes::Builder(OldAttrs))
501 SmallVector<AttributeWithIndex, 8> NewAttrList;
503 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
505 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
506 unsigned i = 0, e = OldAttrList.size();
507 // Copy attributes for arguments before this one.
508 for (; i != e && OldAttrList[i].Index < Idx; ++i)
509 NewAttrList.push_back(OldAttrList[i]);
511 // If there are attributes already at this index, merge them in.
512 if (i != e && OldAttrList[i].Index == Idx) {
514 Attributes::get(C, Attributes::Builder(Attrs).
515 addAttributes(OldAttrList[i].Attrs));
519 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
521 // Copy attributes for arguments after this one.
522 NewAttrList.insert(NewAttrList.end(),
523 OldAttrList.begin()+i, OldAttrList.end());
526 return get(NewAttrList);
529 AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
530 Attributes Attrs) const {
532 // FIXME it is not obvious how this should work for alignment.
533 // For now, say we can't pass in alignment, which no current use does.
534 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
535 "Attempt to exclude alignment!");
537 if (AttrList == 0) return AttrListPtr();
539 Attributes OldAttrs = getAttributes(Idx);
540 Attributes::Builder NewAttrs =
541 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
542 if (NewAttrs == Attributes::Builder(OldAttrs))
545 SmallVector<AttributeWithIndex, 8> NewAttrList;
546 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
547 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 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
555 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
556 removeAttributes(Attrs));
558 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
559 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
561 // Copy attributes for arguments after this one.
562 NewAttrList.insert(NewAttrList.end(),
563 OldAttrList.begin()+i, OldAttrList.end());
565 return get(NewAttrList);
568 void AttrListPtr::dump() const {
570 for (unsigned i = 0; i < getNumSlots(); ++i) {
571 const AttributeWithIndex &PAWI = getSlot(i);
572 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";