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(Attribute::AttrConst Val) : Attrs(Val.v) {}
34 Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
36 Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
38 // FIXME: This is temporary until we have implemented the uniquified version of
40 Attributes Attributes::get(Attributes::Builder &B) {
41 return Attributes(B.Bits);
44 Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
45 // If there are no attributes, return an empty Attributes class.
49 // Otherwise, build a key to look up the existing attributes.
50 LLVMContextImpl *pImpl = Context.pImpl;
52 ID.AddInteger(B.Bits);
55 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
58 // If we didn't find any existing attributes of the same shape then create a
59 // new one and insert it.
60 PA = new AttributesImpl(B.Bits);
61 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
64 // Return the AttributesList that we found or created.
65 return Attributes(PA);
68 bool Attributes::hasAttribute(AttrVal Val) const {
69 return Attrs.hasAttribute(Val);
72 bool Attributes::hasAttributes(const Attributes &A) const {
73 return Attrs.hasAttributes(A);
76 /// This returns the alignment field of an attribute as a byte alignment value.
77 unsigned Attributes::getAlignment() const {
78 if (!hasAttribute(Attributes::Alignment))
80 return 1U << ((Attrs.getAlignment() >> 16) - 1);
83 /// This returns the stack alignment field of an attribute as a byte alignment
85 unsigned Attributes::getStackAlignment() const {
86 if (!hasAttribute(Attributes::StackAlignment))
88 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
91 bool Attributes::isEmptyOrSingleton() const {
92 return Attrs.isEmptyOrSingleton();
95 Attributes Attributes::operator | (const Attributes &A) const {
96 return Attributes(Raw() | A.Raw());
98 Attributes Attributes::operator & (const Attributes &A) const {
99 return Attributes(Raw() & A.Raw());
101 Attributes Attributes::operator ^ (const Attributes &A) const {
102 return Attributes(Raw() ^ A.Raw());
104 Attributes &Attributes::operator |= (const Attributes &A) {
105 Attrs.Bits |= A.Raw();
108 Attributes &Attributes::operator &= (const Attributes &A) {
109 Attrs.Bits &= A.Raw();
112 Attributes Attributes::operator ~ () const {
113 return Attributes(~Raw());
116 uint64_t Attributes::Raw() const {
120 Attributes Attributes::typeIncompatible(Type *Ty) {
121 Attributes::Builder Incompatible;
123 if (!Ty->isIntegerTy()) {
124 // Attributes that only apply to integers.
125 Incompatible.addAttribute(Attributes::SExt);
126 Incompatible.addAttribute(Attributes::ZExt);
129 if (!Ty->isPointerTy()) {
130 // Attributes that only apply to pointers.
131 Incompatible.addAttribute(Attributes::ByVal);
132 Incompatible.addAttribute(Attributes::Nest);
133 Incompatible.addAttribute(Attributes::NoAlias);
134 Incompatible.addAttribute(Attributes::NoCapture);
135 Incompatible.addAttribute(Attributes::StructRet);
138 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
141 std::string Attributes::getAsString() const {
143 if (hasAttribute(Attributes::ZExt))
144 Result += "zeroext ";
145 if (hasAttribute(Attributes::SExt))
146 Result += "signext ";
147 if (hasAttribute(Attributes::NoReturn))
148 Result += "noreturn ";
149 if (hasAttribute(Attributes::NoUnwind))
150 Result += "nounwind ";
151 if (hasAttribute(Attributes::UWTable))
152 Result += "uwtable ";
153 if (hasAttribute(Attributes::ReturnsTwice))
154 Result += "returns_twice ";
155 if (hasAttribute(Attributes::InReg))
157 if (hasAttribute(Attributes::NoAlias))
158 Result += "noalias ";
159 if (hasAttribute(Attributes::NoCapture))
160 Result += "nocapture ";
161 if (hasAttribute(Attributes::StructRet))
163 if (hasAttribute(Attributes::ByVal))
165 if (hasAttribute(Attributes::Nest))
167 if (hasAttribute(Attributes::ReadNone))
168 Result += "readnone ";
169 if (hasAttribute(Attributes::ReadOnly))
170 Result += "readonly ";
171 if (hasAttribute(Attributes::OptimizeForSize))
172 Result += "optsize ";
173 if (hasAttribute(Attributes::NoInline))
174 Result += "noinline ";
175 if (hasAttribute(Attributes::InlineHint))
176 Result += "inlinehint ";
177 if (hasAttribute(Attributes::AlwaysInline))
178 Result += "alwaysinline ";
179 if (hasAttribute(Attributes::StackProtect))
181 if (hasAttribute(Attributes::StackProtectReq))
183 if (hasAttribute(Attributes::NoRedZone))
184 Result += "noredzone ";
185 if (hasAttribute(Attributes::NoImplicitFloat))
186 Result += "noimplicitfloat ";
187 if (hasAttribute(Attributes::Naked))
189 if (hasAttribute(Attributes::NonLazyBind))
190 Result += "nonlazybind ";
191 if (hasAttribute(Attributes::AddressSafety))
192 Result += "address_safety ";
193 if (hasAttribute(Attributes::StackAlignment)) {
194 Result += "alignstack(";
195 Result += utostr(getStackAlignment());
198 if (hasAttribute(Attributes::Alignment)) {
200 Result += utostr(getAlignment());
203 // Trim the trailing space.
204 assert(!Result.empty() && "Unknown attribute!");
205 Result.erase(Result.end()-1);
209 //===----------------------------------------------------------------------===//
210 // Attributes::Builder Implementation
211 //===----------------------------------------------------------------------===//
213 void Attributes::Builder::addAttribute(Attributes::AttrVal Val) {
214 Bits |= AttributesImpl::getAttrMask(Val);
217 void Attributes::Builder::addAlignmentAttr(unsigned Align) {
218 if (Align == 0) return;
219 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
220 assert(Align <= 0x40000000 && "Alignment too large.");
221 Bits |= (Log2_32(Align) + 1) << 16;
223 void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
224 // Default alignment, allow the target to define how to align it.
225 if (Align == 0) return;
226 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
227 assert(Align <= 0x100 && "Alignment too large.");
228 Bits |= (Log2_32(Align) + 1) << 26;
231 void Attributes::Builder::removeAttribute(Attributes::AttrVal Val) {
232 Bits &= ~AttributesImpl::getAttrMask(Val);
235 void Attributes::Builder::removeAttributes(const Attributes &A) {
239 void Attributes::Builder::removeAddressSafetyAttr() {
240 Bits &= ~Attribute::AddressSafety_i;
242 void Attributes::Builder::removeAlwaysInlineAttr() {
243 Bits &= ~Attribute::AlwaysInline_i;
245 void Attributes::Builder::removeByValAttr() {
246 Bits &= ~Attribute::ByVal_i;
248 void Attributes::Builder::removeInlineHintAttr() {
249 Bits &= ~Attribute::InlineHint_i;
251 void Attributes::Builder::removeInRegAttr() {
252 Bits &= ~Attribute::InReg_i;
254 void Attributes::Builder::removeNakedAttr() {
255 Bits &= ~Attribute::Naked_i;
257 void Attributes::Builder::removeNestAttr() {
258 Bits &= ~Attribute::Nest_i;
260 void Attributes::Builder::removeNoAliasAttr() {
261 Bits &= ~Attribute::NoAlias_i;
263 void Attributes::Builder::removeNoCaptureAttr() {
264 Bits &= ~Attribute::NoCapture_i;
266 void Attributes::Builder::removeNoImplicitFloatAttr() {
267 Bits &= ~Attribute::NoImplicitFloat_i;
269 void Attributes::Builder::removeNoInlineAttr() {
270 Bits &= ~Attribute::NoInline_i;
272 void Attributes::Builder::removeNonLazyBindAttr() {
273 Bits &= ~Attribute::NonLazyBind_i;
275 void Attributes::Builder::removeNoRedZoneAttr() {
276 Bits &= ~Attribute::NoRedZone_i;
278 void Attributes::Builder::removeNoReturnAttr() {
279 Bits &= ~Attribute::NoReturn_i;
281 void Attributes::Builder::removeNoUnwindAttr() {
282 Bits &= ~Attribute::NoUnwind_i;
284 void Attributes::Builder::removeOptimizeForSizeAttr() {
285 Bits &= ~Attribute::OptimizeForSize_i;
287 void Attributes::Builder::removeReadNoneAttr() {
288 Bits &= ~Attribute::ReadNone_i;
290 void Attributes::Builder::removeReadOnlyAttr() {
291 Bits &= ~Attribute::ReadOnly_i;
293 void Attributes::Builder::removeReturnsTwiceAttr() {
294 Bits &= ~Attribute::ReturnsTwice_i;
296 void Attributes::Builder::removeSExtAttr() {
297 Bits &= ~Attribute::SExt_i;
299 void Attributes::Builder::removeStackProtectAttr() {
300 Bits &= ~Attribute::StackProtect_i;
302 void Attributes::Builder::removeStackProtectReqAttr() {
303 Bits &= ~Attribute::StackProtectReq_i;
305 void Attributes::Builder::removeStructRetAttr() {
306 Bits &= ~Attribute::StructRet_i;
308 void Attributes::Builder::removeUWTableAttr() {
309 Bits &= ~Attribute::UWTable_i;
311 void Attributes::Builder::removeZExtAttr() {
312 Bits &= ~Attribute::ZExt_i;
315 void Attributes::Builder::removeAlignmentAttr() {
316 Bits &= ~Attribute::Alignment_i;
318 void Attributes::Builder::removeStackAlignmentAttr() {
319 Bits &= ~Attribute::StackAlignment_i;
322 bool Attributes::Builder::hasAttributes() const {
325 bool Attributes::Builder::hasAttributes(const Attributes &A) const {
326 return Bits & A.Raw();
328 bool Attributes::Builder::hasAlignmentAttr() const {
329 return Bits & Attribute::Alignment_i;
332 uint64_t Attributes::Builder::getAlignment() const {
333 if (!hasAlignmentAttr())
335 return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1);
338 //===----------------------------------------------------------------------===//
339 // AttributeImpl Definition
340 //===----------------------------------------------------------------------===//
342 uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
344 case Attributes::None: return 0;
345 case Attributes::ZExt: return 1 << 0;
346 case Attributes::SExt: return 1 << 1;
347 case Attributes::NoReturn: return 1 << 2;
348 case Attributes::InReg: return 1 << 3;
349 case Attributes::StructRet: return 1 << 4;
350 case Attributes::NoUnwind: return 1 << 5;
351 case Attributes::NoAlias: return 1 << 6;
352 case Attributes::ByVal: return 1 << 7;
353 case Attributes::Nest: return 1 << 8;
354 case Attributes::ReadNone: return 1 << 9;
355 case Attributes::ReadOnly: return 1 << 10;
356 case Attributes::NoInline: return 1 << 11;
357 case Attributes::AlwaysInline: return 1 << 12;
358 case Attributes::OptimizeForSize: return 1 << 13;
359 case Attributes::StackProtect: return 1 << 14;
360 case Attributes::StackProtectReq: return 1 << 15;
361 case Attributes::Alignment: return 31 << 16;
362 case Attributes::NoCapture: return 1 << 21;
363 case Attributes::NoRedZone: return 1 << 22;
364 case Attributes::NoImplicitFloat: return 1 << 23;
365 case Attributes::Naked: return 1 << 24;
366 case Attributes::InlineHint: return 1 << 25;
367 case Attributes::StackAlignment: return 7 << 26;
368 case Attributes::ReturnsTwice: return 1 << 29;
369 case Attributes::UWTable: return 1 << 30;
370 case Attributes::NonLazyBind: return 1U << 31;
371 case Attributes::AddressSafety: return 1ULL << 32;
373 llvm_unreachable("Unsupported attribute type");
376 bool AttributesImpl::hasAttribute(uint64_t A) const {
377 return (Bits & getAttrMask(A)) != 0;
380 bool AttributesImpl::hasAttributes() const {
384 bool AttributesImpl::hasAttributes(const Attributes &A) const {
385 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
388 uint64_t AttributesImpl::getAlignment() const {
389 return Bits & Attribute::Alignment_i;
392 uint64_t AttributesImpl::getStackAlignment() const {
393 return Bits & Attribute::StackAlignment_i;
396 bool AttributesImpl::isEmptyOrSingleton() const {
397 return (Bits & (Bits - 1)) == 0;
400 //===----------------------------------------------------------------------===//
401 // AttributeListImpl Definition
402 //===----------------------------------------------------------------------===//
405 class AttributeListImpl;
408 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
411 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
413 class AttributeListImpl : public FoldingSetNode {
414 sys::cas_flag RefCount;
416 // AttributesList is uniqued, these should not be publicly available.
417 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
418 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
419 ~AttributeListImpl(); // Private implementation
421 SmallVector<AttributeWithIndex, 4> Attrs;
423 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
424 : Attrs(attrs.begin(), attrs.end()) {
429 sys::SmartScopedLock<true> Lock(*ALMutex);
433 sys::SmartScopedLock<true> Lock(*ALMutex);
434 if (!AttributesLists.isConstructed())
436 sys::cas_flag new_val = --RefCount;
441 void Profile(FoldingSetNodeID &ID) const {
444 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
445 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
446 ID.AddInteger(Attrs[i].Attrs.Raw());
447 ID.AddInteger(Attrs[i].Index);
453 AttributeListImpl::~AttributeListImpl() {
454 // NOTE: Lock must be acquired by caller.
455 AttributesLists->RemoveNode(this);
459 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
460 // If there are no attributes then return a null AttributesList pointer.
462 return AttrListPtr();
465 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
466 assert(Attrs[i].Attrs.hasAttributes() &&
467 "Pointless attribute!");
468 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
469 "Misordered AttributesList!");
473 // Otherwise, build a key to look up the existing attributes.
475 AttributeListImpl::Profile(ID, Attrs);
478 sys::SmartScopedLock<true> Lock(*ALMutex);
480 AttributeListImpl *PAL =
481 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
483 // If we didn't find any existing attributes of the same shape then
484 // create a new one and insert it.
486 PAL = new AttributeListImpl(Attrs);
487 AttributesLists->InsertNode(PAL, InsertPos);
490 // Return the AttributesList that we found or created.
491 return AttrListPtr(PAL);
495 //===----------------------------------------------------------------------===//
496 // AttrListPtr Method Implementations
497 //===----------------------------------------------------------------------===//
499 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
500 if (LI) LI->AddRef();
503 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
504 if (AttrList) AttrList->AddRef();
507 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
508 sys::SmartScopedLock<true> Lock(*ALMutex);
509 if (AttrList == RHS.AttrList) return *this;
510 if (AttrList) AttrList->DropRef();
511 AttrList = RHS.AttrList;
512 if (AttrList) AttrList->AddRef();
516 AttrListPtr::~AttrListPtr() {
517 if (AttrList) AttrList->DropRef();
520 /// getNumSlots - Return the number of slots used in this attribute list.
521 /// This is the number of arguments that have an attribute set on them
522 /// (including the function itself).
523 unsigned AttrListPtr::getNumSlots() const {
524 return AttrList ? AttrList->Attrs.size() : 0;
527 /// getSlot - Return the AttributeWithIndex at the specified slot. This
528 /// holds a number plus a set of attributes.
529 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
530 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
531 return AttrList->Attrs[Slot];
535 /// getAttributes - The attributes for the specified index are
536 /// returned. Attributes for the result are denoted with Idx = 0.
537 /// Function notes are denoted with idx = ~0.
538 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
539 if (AttrList == 0) return Attributes();
541 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
542 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
543 if (Attrs[i].Index == Idx)
544 return Attrs[i].Attrs;
549 /// hasAttrSomewhere - Return true if the specified attribute is set for at
550 /// least one parameter or for the return value.
551 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
552 if (AttrList == 0) return false;
554 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
555 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
556 if (Attrs[i].Attrs.hasAttributes(Attr))
561 unsigned AttrListPtr::getNumAttrs() const {
562 return AttrList ? AttrList->Attrs.size() : 0;
565 Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
566 assert(AttrList && "Trying to get an attribute from an empty list!");
567 assert(i < AttrList->Attrs.size() && "Index out of range!");
568 return AttrList->Attrs[i].Attrs;
571 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
572 Attributes OldAttrs = getAttributes(Idx);
574 // FIXME it is not obvious how this should work for alignment.
575 // For now, say we can't change a known alignment.
576 unsigned OldAlign = OldAttrs.getAlignment();
577 unsigned NewAlign = Attrs.getAlignment();
578 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
579 "Attempt to change alignment!");
582 Attributes NewAttrs = OldAttrs | Attrs;
583 if (NewAttrs == OldAttrs)
586 SmallVector<AttributeWithIndex, 8> NewAttrList;
588 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
590 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
591 unsigned i = 0, e = OldAttrList.size();
592 // Copy attributes for arguments before this one.
593 for (; i != e && OldAttrList[i].Index < Idx; ++i)
594 NewAttrList.push_back(OldAttrList[i]);
596 // If there are attributes already at this index, merge them in.
597 if (i != e && OldAttrList[i].Index == Idx) {
598 Attrs |= OldAttrList[i].Attrs;
602 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
604 // Copy attributes for arguments after this one.
605 NewAttrList.insert(NewAttrList.end(),
606 OldAttrList.begin()+i, OldAttrList.end());
609 return get(NewAttrList);
612 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
614 // FIXME it is not obvious how this should work for alignment.
615 // For now, say we can't pass in alignment, which no current use does.
616 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
617 "Attempt to exclude alignment!");
619 if (AttrList == 0) return AttrListPtr();
621 Attributes OldAttrs = getAttributes(Idx);
622 Attributes NewAttrs = OldAttrs & ~Attrs;
623 if (NewAttrs == OldAttrs)
626 SmallVector<AttributeWithIndex, 8> NewAttrList;
627 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
628 unsigned i = 0, e = OldAttrList.size();
630 // Copy attributes for arguments before this one.
631 for (; i != e && OldAttrList[i].Index < Idx; ++i)
632 NewAttrList.push_back(OldAttrList[i]);
634 // If there are attributes already at this index, merge them in.
635 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
636 Attrs = OldAttrList[i].Attrs & ~Attrs;
638 if (Attrs) // If any attributes left for this parameter, add them.
639 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
641 // Copy attributes for arguments after this one.
642 NewAttrList.insert(NewAttrList.end(),
643 OldAttrList.begin()+i, OldAttrList.end());
645 return get(NewAttrList);
648 void AttrListPtr::dump() const {
650 for (unsigned i = 0; i < getNumSlots(); ++i) {
651 const AttributeWithIndex &PAWI = getSlot(i);
652 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";