1 //===-- Attribute.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 Attribute, AttributeImpl, AttrBuilder,
11 // AttributeSetImpl, and AttributeSet classes.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/Attributes.h"
16 #include "AttributeImpl.h"
17 #include "LLVMContextImpl.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/Support/Atomic.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Mutex.h"
25 #include "llvm/Support/raw_ostream.h"
28 //===----------------------------------------------------------------------===//
29 // Attribute Implementation
30 //===----------------------------------------------------------------------===//
32 Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) {
34 for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end();
37 return Attribute::get(Context, B);
40 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
41 // If there are no attributes, return an empty Attribute class.
42 if (!B.hasAttributes())
45 // Otherwise, build a key to look up the existing attributes.
46 LLVMContextImpl *pImpl = Context.pImpl;
48 ID.AddInteger(B.getBitMask());
51 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
54 // If we didn't find any existing attributes of the same shape then create a
55 // new one and insert it.
56 PA = new AttributeImpl(Context, B.getBitMask());
57 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 // Return the AttributesList that we found or created.
64 bool Attribute::hasAttribute(AttrKind Val) const {
65 return pImpl && pImpl->hasAttribute(Val);
68 bool Attribute::hasAttributes() const {
69 return pImpl && pImpl->hasAttributes();
72 bool Attribute::hasAttributes(const Attribute &A) const {
73 return pImpl && pImpl->hasAttributes(A);
76 /// This returns the alignment field of an attribute as a byte alignment value.
77 unsigned Attribute::getAlignment() const {
78 if (!hasAttribute(Attribute::Alignment))
80 return 1U << ((pImpl->getAlignment() >> 16) - 1);
83 /// This returns the stack alignment field of an attribute as a byte alignment
85 unsigned Attribute::getStackAlignment() const {
86 if (!hasAttribute(Attribute::StackAlignment))
88 return 1U << ((pImpl->getStackAlignment() >> 26) - 1);
91 bool Attribute::operator==(AttrKind K) const {
92 return pImpl && pImpl->contains(K);
95 bool Attribute::operator!=(AttrKind K) const {
96 return !(pImpl && pImpl->contains(K));
99 uint64_t Attribute::getBitMask() const {
100 return pImpl ? pImpl->getBitMask() : 0;
103 Attribute Attribute::typeIncompatible(Type *Ty) {
104 AttrBuilder Incompatible;
106 if (!Ty->isIntegerTy())
107 // Attribute that only apply to integers.
108 Incompatible.addAttribute(Attribute::SExt)
109 .addAttribute(Attribute::ZExt);
111 if (!Ty->isPointerTy())
112 // Attribute that only apply to pointers.
113 Incompatible.addAttribute(Attribute::ByVal)
114 .addAttribute(Attribute::Nest)
115 .addAttribute(Attribute::NoAlias)
116 .addAttribute(Attribute::NoCapture)
117 .addAttribute(Attribute::StructRet);
119 return Attribute::get(Ty->getContext(), Incompatible);
122 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
123 /// encoding of all the LLVM attributes found in the given attribute bitset.
124 /// Any change to this encoding is a breaking change to bitcode compatibility.
125 uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
126 // FIXME: It doesn't make sense to store the alignment information as an
127 // expanded out value, we should store it as a log2 value. However, we can't
128 // just change that here without breaking bitcode compatibility. If this ever
129 // becomes a problem in practice, we should introduce new tag numbers in the
130 // bitcode file and have those tags use a more efficiently encoded alignment
133 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
134 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
135 uint64_t EncodedAttrs = Attrs.getBitMask() & 0xffff;
136 if (Attrs.hasAttribute(Attribute::Alignment))
137 EncodedAttrs |= Attrs.getAlignment() << 16;
138 EncodedAttrs |= (Attrs.getBitMask() & (0xffffULL << 21)) << 11;
142 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
143 /// the LLVM attributes that have been decoded from the given integer. This
144 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
145 Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
146 uint64_t EncodedAttrs) {
147 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
148 // the bits above 31 down by 11 bits.
149 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
150 assert((!Alignment || isPowerOf2_32(Alignment)) &&
151 "Alignment must be a power of two.");
153 AttrBuilder B(EncodedAttrs & 0xffff);
155 B.addAlignmentAttr(Alignment);
156 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
157 return Attribute::get(C, B);
160 std::string Attribute::getAsString() const {
162 if (hasAttribute(Attribute::ZExt))
163 Result += "zeroext ";
164 if (hasAttribute(Attribute::SExt))
165 Result += "signext ";
166 if (hasAttribute(Attribute::NoReturn))
167 Result += "noreturn ";
168 if (hasAttribute(Attribute::NoUnwind))
169 Result += "nounwind ";
170 if (hasAttribute(Attribute::UWTable))
171 Result += "uwtable ";
172 if (hasAttribute(Attribute::ReturnsTwice))
173 Result += "returns_twice ";
174 if (hasAttribute(Attribute::InReg))
176 if (hasAttribute(Attribute::NoAlias))
177 Result += "noalias ";
178 if (hasAttribute(Attribute::NoCapture))
179 Result += "nocapture ";
180 if (hasAttribute(Attribute::StructRet))
182 if (hasAttribute(Attribute::ByVal))
184 if (hasAttribute(Attribute::Nest))
186 if (hasAttribute(Attribute::ReadNone))
187 Result += "readnone ";
188 if (hasAttribute(Attribute::ReadOnly))
189 Result += "readonly ";
190 if (hasAttribute(Attribute::OptimizeForSize))
191 Result += "optsize ";
192 if (hasAttribute(Attribute::NoInline))
193 Result += "noinline ";
194 if (hasAttribute(Attribute::InlineHint))
195 Result += "inlinehint ";
196 if (hasAttribute(Attribute::AlwaysInline))
197 Result += "alwaysinline ";
198 if (hasAttribute(Attribute::StackProtect))
200 if (hasAttribute(Attribute::StackProtectReq))
202 if (hasAttribute(Attribute::NoRedZone))
203 Result += "noredzone ";
204 if (hasAttribute(Attribute::NoImplicitFloat))
205 Result += "noimplicitfloat ";
206 if (hasAttribute(Attribute::Naked))
208 if (hasAttribute(Attribute::NonLazyBind))
209 Result += "nonlazybind ";
210 if (hasAttribute(Attribute::AddressSafety))
211 Result += "address_safety ";
212 if (hasAttribute(Attribute::MinSize))
213 Result += "minsize ";
214 if (hasAttribute(Attribute::StackAlignment)) {
215 Result += "alignstack(";
216 Result += utostr(getStackAlignment());
219 if (hasAttribute(Attribute::Alignment)) {
221 Result += utostr(getAlignment());
224 if (hasAttribute(Attribute::NoDuplicate))
225 Result += "noduplicate ";
226 // Trim the trailing space.
227 assert(!Result.empty() && "Unknown attribute!");
228 Result.erase(Result.end()-1);
232 //===----------------------------------------------------------------------===//
233 // AttrBuilder Implementation
234 //===----------------------------------------------------------------------===//
236 void AttrBuilder::clear() {
238 Alignment = StackAlignment = Bits = 0;
241 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val){
242 Bits |= AttributeImpl::getAttrMask(Val);
248 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
253 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
254 if (Align == 0) return *this;
255 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
256 assert(Align <= 0x40000000 && "Alignment too large.");
257 Bits |= (Log2_32(Align) + 1) << 16;
259 AttrSet.insert(Attribute::Alignment);
263 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
264 // Default alignment, allow the target to define how to align it.
265 if (Align == 0) return *this;
266 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
267 assert(Align <= 0x100 && "Alignment too large.");
268 Bits |= (Log2_32(Align) + 1) << 26;
270 AttrSet.insert(Attribute::StackAlignment);
271 StackAlignment = Align;
275 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
276 Bits &= ~AttributeImpl::getAttrMask(Val);
279 if (Val == Attribute::Alignment)
281 else if (Val == Attribute::StackAlignment)
286 AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
287 Bits |= A.getBitMask();
291 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
292 Bits &= ~A.getBitMask();
296 bool AttrBuilder::contains(Attribute::AttrKind A) const {
297 return Bits & AttributeImpl::getAttrMask(A);
300 bool AttrBuilder::hasAttributes() const {
303 bool AttrBuilder::hasAttributes(const Attribute &A) const {
304 return Bits & A.getBitMask();
306 bool AttrBuilder::hasAlignmentAttr() const {
307 return Bits & AttributeImpl::getAttrMask(Attribute::Alignment);
310 uint64_t AttrBuilder::getAlignment() const {
311 if (!hasAlignmentAttr())
314 (((Bits & AttributeImpl::getAttrMask(Attribute::Alignment)) >> 16) - 1);
317 uint64_t AttrBuilder::getStackAlignment() const {
318 if (!hasAlignmentAttr())
321 (((Bits & AttributeImpl::getAttrMask(Attribute::StackAlignment))>>26)-1);
324 //===----------------------------------------------------------------------===//
325 // AttributeImpl Definition
326 //===----------------------------------------------------------------------===//
328 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data) {
329 Data = ConstantInt::get(Type::getInt64Ty(C), data);
331 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data) {
332 Data = ConstantInt::get(Type::getInt64Ty(C), data);
334 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
335 ArrayRef<Constant*> values) {
336 Data = ConstantInt::get(Type::getInt64Ty(C), data);
337 Vals.reserve(values.size());
338 Vals.append(values.begin(), values.end());
340 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data) {
341 Data = ConstantDataArray::getString(C, data);
344 bool AttributeImpl::contains(Attribute::AttrKind Kind) const {
345 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
346 return CI->getZExtValue() == Kind;
350 bool AttributeImpl::contains(StringRef Kind) const {
351 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
353 return CDA->getAsString() == Kind;
357 uint64_t AttributeImpl::getBitMask() const {
358 // FIXME: Remove this.
359 return cast<ConstantInt>(Data)->getZExtValue();
362 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
364 case Attribute::None: return 0;
365 case Attribute::ZExt: return 1 << 0;
366 case Attribute::SExt: return 1 << 1;
367 case Attribute::NoReturn: return 1 << 2;
368 case Attribute::InReg: return 1 << 3;
369 case Attribute::StructRet: return 1 << 4;
370 case Attribute::NoUnwind: return 1 << 5;
371 case Attribute::NoAlias: return 1 << 6;
372 case Attribute::ByVal: return 1 << 7;
373 case Attribute::Nest: return 1 << 8;
374 case Attribute::ReadNone: return 1 << 9;
375 case Attribute::ReadOnly: return 1 << 10;
376 case Attribute::NoInline: return 1 << 11;
377 case Attribute::AlwaysInline: return 1 << 12;
378 case Attribute::OptimizeForSize: return 1 << 13;
379 case Attribute::StackProtect: return 1 << 14;
380 case Attribute::StackProtectReq: return 1 << 15;
381 case Attribute::Alignment: return 31 << 16;
382 case Attribute::NoCapture: return 1 << 21;
383 case Attribute::NoRedZone: return 1 << 22;
384 case Attribute::NoImplicitFloat: return 1 << 23;
385 case Attribute::Naked: return 1 << 24;
386 case Attribute::InlineHint: return 1 << 25;
387 case Attribute::StackAlignment: return 7 << 26;
388 case Attribute::ReturnsTwice: return 1 << 29;
389 case Attribute::UWTable: return 1 << 30;
390 case Attribute::NonLazyBind: return 1U << 31;
391 case Attribute::AddressSafety: return 1ULL << 32;
392 case Attribute::MinSize: return 1ULL << 33;
393 case Attribute::NoDuplicate: return 1ULL << 34;
395 llvm_unreachable("Unsupported attribute type");
398 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
399 return (getBitMask() & getAttrMask(A)) != 0;
402 bool AttributeImpl::hasAttributes() const {
403 return getBitMask() != 0;
406 bool AttributeImpl::hasAttributes(const Attribute &A) const {
407 // FIXME: getBitMask() won't work here in the future.
408 return getBitMask() & A.getBitMask();
411 uint64_t AttributeImpl::getAlignment() const {
412 return getBitMask() & getAttrMask(Attribute::Alignment);
415 uint64_t AttributeImpl::getStackAlignment() const {
416 return getBitMask() & getAttrMask(Attribute::StackAlignment);
419 //===----------------------------------------------------------------------===//
420 // AttributeSetImpl Definition
421 //===----------------------------------------------------------------------===//
423 AttributeSet AttributeSet::get(LLVMContext &C,
424 ArrayRef<AttributeWithIndex> Attrs) {
425 // If there are no attributes then return a null AttributesList pointer.
427 return AttributeSet();
430 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
431 assert(Attrs[i].Attrs.hasAttributes() &&
432 "Pointless attribute!");
433 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
434 "Misordered AttributesList!");
438 // Otherwise, build a key to look up the existing attributes.
439 LLVMContextImpl *pImpl = C.pImpl;
441 AttributeSetImpl::Profile(ID, Attrs);
444 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
447 // If we didn't find any existing attributes of the same shape then
448 // create a new one and insert it.
450 PA = new AttributeSetImpl(C, Attrs);
451 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
454 // Return the AttributesList that we found or created.
455 return AttributeSet(PA);
458 //===----------------------------------------------------------------------===//
459 // AttributeSet Method Implementations
460 //===----------------------------------------------------------------------===//
462 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
463 AttrList = RHS.AttrList;
467 /// getNumSlots - Return the number of slots used in this attribute list.
468 /// This is the number of arguments that have an attribute set on them
469 /// (including the function itself).
470 unsigned AttributeSet::getNumSlots() const {
471 return AttrList ? AttrList->Attrs.size() : 0;
474 /// getSlot - Return the AttributeWithIndex at the specified slot. This
475 /// holds a number plus a set of attributes.
476 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
477 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
478 return AttrList->Attrs[Slot];
481 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
482 return getAttributes(Index).hasAttribute(Kind);
485 bool AttributeSet::hasAttributes(unsigned Index) const {
486 return getAttributes(Index).hasAttributes();
489 std::string AttributeSet::getAsString(unsigned Index) const {
490 return getAttributes(Index).getAsString();
493 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
494 return getAttributes(Index).getStackAlignment();
497 uint64_t AttributeSet::getBitMask(unsigned Index) const {
498 // FIXME: Remove this.
499 return getAttributes(Index).getBitMask();
502 /// getAttributes - The attributes for the specified index are returned.
503 /// Attributes for the result are denoted with Idx = 0. Function attributes are
504 /// denoted with Idx = ~0.
505 Attribute AttributeSet::getAttributes(unsigned Idx) const {
506 if (AttrList == 0) return Attribute();
508 const SmallVectorImpl<AttributeWithIndex> &Attrs = AttrList->Attrs;
509 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
510 if (Attrs[i].Index == Idx)
511 return Attrs[i].Attrs;
516 /// hasAttrSomewhere - Return true if the specified attribute is set for at
517 /// least one parameter or for the return value.
518 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
519 if (AttrList == 0) return false;
521 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
522 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
523 if (Attrs[i].Attrs.hasAttribute(Attr))
529 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
530 Attribute Attrs) const {
531 Attribute OldAttrs = getAttributes(Idx);
533 // FIXME it is not obvious how this should work for alignment.
534 // For now, say we can't change a known alignment.
535 unsigned OldAlign = OldAttrs.getAlignment();
536 unsigned NewAlign = Attrs.getAlignment();
537 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
538 "Attempt to change alignment!");
541 AttrBuilder NewAttrs =
542 AttrBuilder(OldAttrs).addAttributes(Attrs);
543 if (NewAttrs == AttrBuilder(OldAttrs))
546 SmallVector<AttributeWithIndex, 8> NewAttrList;
548 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
550 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
551 unsigned i = 0, e = OldAttrList.size();
552 // Copy attributes for arguments before this one.
553 for (; i != e && OldAttrList[i].Index < Idx; ++i)
554 NewAttrList.push_back(OldAttrList[i]);
556 // If there are attributes already at this index, merge them in.
557 if (i != e && OldAttrList[i].Index == Idx) {
559 Attribute::get(C, AttrBuilder(Attrs).
560 addAttributes(OldAttrList[i].Attrs));
564 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
566 // Copy attributes for arguments after this one.
567 NewAttrList.insert(NewAttrList.end(),
568 OldAttrList.begin()+i, OldAttrList.end());
571 return get(C, NewAttrList);
574 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
575 Attribute Attrs) const {
577 // FIXME it is not obvious how this should work for alignment.
578 // For now, say we can't pass in alignment, which no current use does.
579 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
580 "Attempt to exclude alignment!");
582 if (AttrList == 0) return AttributeSet();
584 Attribute OldAttrs = getAttributes(Idx);
585 AttrBuilder NewAttrs =
586 AttrBuilder(OldAttrs).removeAttributes(Attrs);
587 if (NewAttrs == AttrBuilder(OldAttrs))
590 SmallVector<AttributeWithIndex, 8> NewAttrList;
591 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
592 unsigned i = 0, e = OldAttrList.size();
594 // Copy attributes for arguments before this one.
595 for (; i != e && OldAttrList[i].Index < Idx; ++i)
596 NewAttrList.push_back(OldAttrList[i]);
598 // If there are attributes already at this index, merge them in.
599 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
600 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
601 removeAttributes(Attrs));
603 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
604 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
606 // Copy attributes for arguments after this one.
607 NewAttrList.insert(NewAttrList.end(),
608 OldAttrList.begin()+i, OldAttrList.end());
610 return get(C, NewAttrList);
613 void AttributeSet::dump() const {
615 for (unsigned i = 0; i < getNumSlots(); ++i) {
616 const AttributeWithIndex &PAWI = getSlot(i);
617 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";