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/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/Support/Atomic.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/Mutex.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Type.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 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val){
237 Bits |= AttributeImpl::getAttrMask(Val);
241 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
246 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
247 if (Align == 0) return *this;
248 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
249 assert(Align <= 0x40000000 && "Alignment too large.");
250 Bits |= (Log2_32(Align) + 1) << 16;
253 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
254 // Default alignment, allow the target to define how to align it.
255 if (Align == 0) return *this;
256 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
257 assert(Align <= 0x100 && "Alignment too large.");
258 Bits |= (Log2_32(Align) + 1) << 26;
262 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
263 Bits &= ~AttributeImpl::getAttrMask(Val);
267 AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
268 Bits |= A.getBitMask();
272 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
273 Bits &= ~A.getBitMask();
277 bool AttrBuilder::contains(Attribute::AttrKind A) const {
278 return Bits & AttributeImpl::getAttrMask(A);
281 bool AttrBuilder::hasAttributes() const {
284 bool AttrBuilder::hasAttributes(const Attribute &A) const {
285 return Bits & A.getBitMask();
287 bool AttrBuilder::hasAlignmentAttr() const {
288 return Bits & AttributeImpl::getAttrMask(Attribute::Alignment);
291 uint64_t AttrBuilder::getAlignment() const {
292 if (!hasAlignmentAttr())
295 (((Bits & AttributeImpl::getAttrMask(Attribute::Alignment)) >> 16) - 1);
298 uint64_t AttrBuilder::getStackAlignment() const {
299 if (!hasAlignmentAttr())
302 (((Bits & AttributeImpl::getAttrMask(Attribute::StackAlignment))>>26)-1);
305 //===----------------------------------------------------------------------===//
306 // AttributeImpl Definition
307 //===----------------------------------------------------------------------===//
309 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data) {
310 Data = ConstantInt::get(Type::getInt64Ty(C), data);
312 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data) {
313 Data = ConstantInt::get(Type::getInt64Ty(C), data);
315 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
316 ArrayRef<Constant*> values) {
317 Data = ConstantInt::get(Type::getInt64Ty(C), data);
318 Vals.reserve(values.size());
319 Vals.append(values.begin(), values.end());
321 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data) {
322 Data = ConstantDataArray::getString(C, data);
325 bool AttributeImpl::contains(Attribute::AttrKind Kind) const {
326 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
327 return CI->getZExtValue() == Kind;
331 bool AttributeImpl::contains(StringRef Kind) const {
332 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
334 return CDA->getAsString() == Kind;
338 uint64_t AttributeImpl::getBitMask() const {
339 // FIXME: Remove this.
340 return cast<ConstantInt>(Data)->getZExtValue();
343 uint64_t AttributeImpl::getAttrMask(uint64_t Val) {
345 case Attribute::None: return 0;
346 case Attribute::ZExt: return 1 << 0;
347 case Attribute::SExt: return 1 << 1;
348 case Attribute::NoReturn: return 1 << 2;
349 case Attribute::InReg: return 1 << 3;
350 case Attribute::StructRet: return 1 << 4;
351 case Attribute::NoUnwind: return 1 << 5;
352 case Attribute::NoAlias: return 1 << 6;
353 case Attribute::ByVal: return 1 << 7;
354 case Attribute::Nest: return 1 << 8;
355 case Attribute::ReadNone: return 1 << 9;
356 case Attribute::ReadOnly: return 1 << 10;
357 case Attribute::NoInline: return 1 << 11;
358 case Attribute::AlwaysInline: return 1 << 12;
359 case Attribute::OptimizeForSize: return 1 << 13;
360 case Attribute::StackProtect: return 1 << 14;
361 case Attribute::StackProtectReq: return 1 << 15;
362 case Attribute::Alignment: return 31 << 16;
363 case Attribute::NoCapture: return 1 << 21;
364 case Attribute::NoRedZone: return 1 << 22;
365 case Attribute::NoImplicitFloat: return 1 << 23;
366 case Attribute::Naked: return 1 << 24;
367 case Attribute::InlineHint: return 1 << 25;
368 case Attribute::StackAlignment: return 7 << 26;
369 case Attribute::ReturnsTwice: return 1 << 29;
370 case Attribute::UWTable: return 1 << 30;
371 case Attribute::NonLazyBind: return 1U << 31;
372 case Attribute::AddressSafety: return 1ULL << 32;
373 case Attribute::MinSize: return 1ULL << 33;
374 case Attribute::NoDuplicate: return 1ULL << 34;
376 llvm_unreachable("Unsupported attribute type");
379 bool AttributeImpl::hasAttribute(uint64_t A) const {
380 return (getBitMask() & getAttrMask(A)) != 0;
383 bool AttributeImpl::hasAttributes() const {
384 return getBitMask() != 0;
387 bool AttributeImpl::hasAttributes(const Attribute &A) const {
388 // FIXME: getBitMask() won't work here in the future.
389 return getBitMask() & A.getBitMask();
392 uint64_t AttributeImpl::getAlignment() const {
393 return getBitMask() & getAttrMask(Attribute::Alignment);
396 uint64_t AttributeImpl::getStackAlignment() const {
397 return getBitMask() & getAttrMask(Attribute::StackAlignment);
400 //===----------------------------------------------------------------------===//
401 // AttributeSetImpl Definition
402 //===----------------------------------------------------------------------===//
404 AttributeSet AttributeSet::get(LLVMContext &C,
405 ArrayRef<AttributeWithIndex> Attrs) {
406 // If there are no attributes then return a null AttributesList pointer.
408 return AttributeSet();
411 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
412 assert(Attrs[i].Attrs.hasAttributes() &&
413 "Pointless attribute!");
414 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
415 "Misordered AttributesList!");
419 // Otherwise, build a key to look up the existing attributes.
420 LLVMContextImpl *pImpl = C.pImpl;
422 AttributeSetImpl::Profile(ID, Attrs);
425 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
428 // If we didn't find any existing attributes of the same shape then
429 // create a new one and insert it.
431 PA = new AttributeSetImpl(C, Attrs);
432 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
435 // Return the AttributesList that we found or created.
436 return AttributeSet(PA);
439 //===----------------------------------------------------------------------===//
440 // AttributeSet Method Implementations
441 //===----------------------------------------------------------------------===//
443 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
444 AttrList = RHS.AttrList;
448 /// getNumSlots - Return the number of slots used in this attribute list.
449 /// This is the number of arguments that have an attribute set on them
450 /// (including the function itself).
451 unsigned AttributeSet::getNumSlots() const {
452 return AttrList ? AttrList->Attrs.size() : 0;
455 /// getSlot - Return the AttributeWithIndex at the specified slot. This
456 /// holds a number plus a set of attributes.
457 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
458 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
459 return AttrList->Attrs[Slot];
462 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
463 return getAttributes(Index).hasAttribute(Kind);
466 bool AttributeSet::hasAttributes(unsigned Index) const {
467 return getAttributes(Index).hasAttributes();
470 std::string AttributeSet::getAsString(unsigned Index) const {
471 return getAttributes(Index).getAsString();
474 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
475 return getAttributes(Index).getStackAlignment();
478 uint64_t AttributeSet::getBitMask(unsigned Index) const {
479 // FIXME: Remove this.
480 return getAttributes(Index).getBitMask();
483 /// getAttributes - The attributes for the specified index are returned.
484 /// Attributes for the result are denoted with Idx = 0. Function attributes are
485 /// denoted with Idx = ~0.
486 Attribute AttributeSet::getAttributes(unsigned Idx) const {
487 if (AttrList == 0) return Attribute();
489 const SmallVectorImpl<AttributeWithIndex> &Attrs = AttrList->Attrs;
490 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
491 if (Attrs[i].Index == Idx)
492 return Attrs[i].Attrs;
497 /// hasAttrSomewhere - Return true if the specified attribute is set for at
498 /// least one parameter or for the return value.
499 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
500 if (AttrList == 0) return false;
502 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
503 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
504 if (Attrs[i].Attrs.hasAttribute(Attr))
510 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
511 Attribute Attrs) const {
512 Attribute OldAttrs = getAttributes(Idx);
514 // FIXME it is not obvious how this should work for alignment.
515 // For now, say we can't change a known alignment.
516 unsigned OldAlign = OldAttrs.getAlignment();
517 unsigned NewAlign = Attrs.getAlignment();
518 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
519 "Attempt to change alignment!");
522 AttrBuilder NewAttrs =
523 AttrBuilder(OldAttrs).addAttributes(Attrs);
524 if (NewAttrs == AttrBuilder(OldAttrs))
527 SmallVector<AttributeWithIndex, 8> NewAttrList;
529 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
531 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
532 unsigned i = 0, e = OldAttrList.size();
533 // Copy attributes for arguments before this one.
534 for (; i != e && OldAttrList[i].Index < Idx; ++i)
535 NewAttrList.push_back(OldAttrList[i]);
537 // If there are attributes already at this index, merge them in.
538 if (i != e && OldAttrList[i].Index == Idx) {
540 Attribute::get(C, AttrBuilder(Attrs).
541 addAttributes(OldAttrList[i].Attrs));
545 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
547 // Copy attributes for arguments after this one.
548 NewAttrList.insert(NewAttrList.end(),
549 OldAttrList.begin()+i, OldAttrList.end());
552 return get(C, NewAttrList);
555 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
556 Attribute Attrs) const {
558 // FIXME it is not obvious how this should work for alignment.
559 // For now, say we can't pass in alignment, which no current use does.
560 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
561 "Attempt to exclude alignment!");
563 if (AttrList == 0) return AttributeSet();
565 Attribute OldAttrs = getAttributes(Idx);
566 AttrBuilder NewAttrs =
567 AttrBuilder(OldAttrs).removeAttributes(Attrs);
568 if (NewAttrs == AttrBuilder(OldAttrs))
571 SmallVector<AttributeWithIndex, 8> NewAttrList;
572 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
573 unsigned i = 0, e = OldAttrList.size();
575 // Copy attributes for arguments before this one.
576 for (; i != e && OldAttrList[i].Index < Idx; ++i)
577 NewAttrList.push_back(OldAttrList[i]);
579 // If there are attributes already at this index, merge them in.
580 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
581 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
582 removeAttributes(Attrs));
584 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
585 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
587 // Copy attributes for arguments after this one.
588 NewAttrList.insert(NewAttrList.end(),
589 OldAttrList.begin()+i, OldAttrList.end());
591 return get(C, NewAttrList);
594 void AttributeSet::dump() const {
596 for (unsigned i = 0; i < getNumSlots(); ++i) {
597 const AttributeWithIndex &PAWI = getSlot(i);
598 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";