Push down the conversion of the alignment from the bit mask to a real number into...
[oota-llvm.git] / lib / IR / Attributes.cpp
1 //===-- Attribute.cpp - Implement AttributesList -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Attribute, AttributeImpl, AttrBuilder,
11 // AttributeSetImpl, and AttributeSet classes.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
26 using namespace llvm;
27
28 //===----------------------------------------------------------------------===//
29 // Attribute Implementation
30 //===----------------------------------------------------------------------===//
31
32 Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) {
33   AttrBuilder B;
34   for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end();
35        I != E; ++I)
36     B.addAttribute(*I);
37   return Attribute::get(Context, B);
38 }
39
40 Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
41   // If there are no attributes, return an empty Attribute class.
42   if (!B.hasAttributes())
43     return Attribute();
44
45   // Otherwise, build a key to look up the existing attributes.
46   LLVMContextImpl *pImpl = Context.pImpl;
47   FoldingSetNodeID ID;
48   ID.AddInteger(B.Raw());
49
50   void *InsertPoint;
51   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
52
53   if (!PA) {
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.Raw());
57     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
58   }
59
60   // Return the AttributesList that we found or created.
61   return Attribute(PA);
62 }
63
64 bool Attribute::hasAttribute(AttrKind Val) const {
65   return pImpl && pImpl->hasAttribute(Val);
66 }
67
68 bool Attribute::hasAttributes() const {
69   return pImpl && pImpl->hasAttributes();
70 }
71
72 /// This returns the alignment field of an attribute as a byte alignment value.
73 unsigned Attribute::getAlignment() const {
74   if (!hasAttribute(Attribute::Alignment))
75     return 0;
76   return pImpl->getAlignment();
77 }
78
79 /// This returns the stack alignment field of an attribute as a byte alignment
80 /// value.
81 unsigned Attribute::getStackAlignment() const {
82   if (!hasAttribute(Attribute::StackAlignment))
83     return 0;
84   return pImpl->getStackAlignment();
85 }
86
87 bool Attribute::operator==(AttrKind K) const {
88   return pImpl && *pImpl == K;
89 }
90 bool Attribute::operator!=(AttrKind K) const {
91   return !(*this == K);
92 }
93
94 uint64_t Attribute::Raw() const {
95   return pImpl ? pImpl->Raw() : 0;
96 }
97
98 Attribute Attribute::typeIncompatible(Type *Ty) {
99   AttrBuilder Incompatible;
100
101   if (!Ty->isIntegerTy())
102     // Attribute that only apply to integers.
103     Incompatible.addAttribute(Attribute::SExt)
104       .addAttribute(Attribute::ZExt);
105
106   if (!Ty->isPointerTy())
107     // Attribute that only apply to pointers.
108     Incompatible.addAttribute(Attribute::ByVal)
109       .addAttribute(Attribute::Nest)
110       .addAttribute(Attribute::NoAlias)
111       .addAttribute(Attribute::NoCapture)
112       .addAttribute(Attribute::StructRet);
113
114   return Attribute::get(Ty->getContext(), Incompatible);
115 }
116
117 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
118 /// encoding of all the LLVM attributes found in the given attribute bitset.
119 /// Any change to this encoding is a breaking change to bitcode compatibility.
120 uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
121   // FIXME: It doesn't make sense to store the alignment information as an
122   // expanded out value, we should store it as a log2 value.  However, we can't
123   // just change that here without breaking bitcode compatibility.  If this ever
124   // becomes a problem in practice, we should introduce new tag numbers in the
125   // bitcode file and have those tags use a more efficiently encoded alignment
126   // field.
127
128   // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
129   // log2 encoded value. Shift the bits above the alignment up by 11 bits.
130   uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
131   if (Attrs.hasAttribute(Attribute::Alignment))
132     EncodedAttrs |= Attrs.getAlignment() << 16;
133   EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
134   return EncodedAttrs;
135 }
136
137 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
138 /// the LLVM attributes that have been decoded from the given integer.  This
139 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
140 Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
141                                                     uint64_t EncodedAttrs) {
142   // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
143   // the bits above 31 down by 11 bits.
144   unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
145   assert((!Alignment || isPowerOf2_32(Alignment)) &&
146          "Alignment must be a power of two.");
147
148   AttrBuilder B(EncodedAttrs & 0xffff);
149   if (Alignment)
150     B.addAlignmentAttr(Alignment);
151   B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
152   return Attribute::get(C, B);
153 }
154
155 std::string Attribute::getAsString() const {
156   std::string Result;
157   if (hasAttribute(Attribute::ZExt))
158     Result += "zeroext ";
159   if (hasAttribute(Attribute::SExt))
160     Result += "signext ";
161   if (hasAttribute(Attribute::NoReturn))
162     Result += "noreturn ";
163   if (hasAttribute(Attribute::NoUnwind))
164     Result += "nounwind ";
165   if (hasAttribute(Attribute::UWTable))
166     Result += "uwtable ";
167   if (hasAttribute(Attribute::ReturnsTwice))
168     Result += "returns_twice ";
169   if (hasAttribute(Attribute::InReg))
170     Result += "inreg ";
171   if (hasAttribute(Attribute::NoAlias))
172     Result += "noalias ";
173   if (hasAttribute(Attribute::NoCapture))
174     Result += "nocapture ";
175   if (hasAttribute(Attribute::StructRet))
176     Result += "sret ";
177   if (hasAttribute(Attribute::ByVal))
178     Result += "byval ";
179   if (hasAttribute(Attribute::Nest))
180     Result += "nest ";
181   if (hasAttribute(Attribute::ReadNone))
182     Result += "readnone ";
183   if (hasAttribute(Attribute::ReadOnly))
184     Result += "readonly ";
185   if (hasAttribute(Attribute::OptimizeForSize))
186     Result += "optsize ";
187   if (hasAttribute(Attribute::NoInline))
188     Result += "noinline ";
189   if (hasAttribute(Attribute::InlineHint))
190     Result += "inlinehint ";
191   if (hasAttribute(Attribute::AlwaysInline))
192     Result += "alwaysinline ";
193   if (hasAttribute(Attribute::StackProtect))
194     Result += "ssp ";
195   if (hasAttribute(Attribute::StackProtectReq))
196     Result += "sspreq ";
197   if (hasAttribute(Attribute::StackProtectStrong))
198     Result += "sspstrong ";
199   if (hasAttribute(Attribute::NoRedZone))
200     Result += "noredzone ";
201   if (hasAttribute(Attribute::NoImplicitFloat))
202     Result += "noimplicitfloat ";
203   if (hasAttribute(Attribute::Naked))
204     Result += "naked ";
205   if (hasAttribute(Attribute::NonLazyBind))
206     Result += "nonlazybind ";
207   if (hasAttribute(Attribute::AddressSafety))
208     Result += "address_safety ";
209   if (hasAttribute(Attribute::MinSize))
210     Result += "minsize ";
211   if (hasAttribute(Attribute::StackAlignment)) {
212     Result += "alignstack(";
213     Result += utostr(getStackAlignment());
214     Result += ") ";
215   }
216   if (hasAttribute(Attribute::Alignment)) {
217     Result += "align ";
218     Result += utostr(getAlignment());
219     Result += " ";
220   }
221   if (hasAttribute(Attribute::NoDuplicate))
222     Result += "noduplicate ";
223   // Trim the trailing space.
224   assert(!Result.empty() && "Unknown attribute!");
225   Result.erase(Result.end()-1);
226   return Result;
227 }
228
229 //===----------------------------------------------------------------------===//
230 // AttrBuilder Method Implementations
231 //===----------------------------------------------------------------------===//
232
233 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
234   : Alignment(0), StackAlignment(0) {
235   AttributeSetImpl *pImpl = AS.AttrList;
236   if (!pImpl) return;
237
238   ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
239   const AttributeWithIndex *AWI = 0;
240   for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
241     if (AttrList[I].Index == Idx) {
242       AWI = &AttrList[I];
243       break;
244     }
245
246   if (!AWI) return;
247
248   uint64_t Mask = AWI->Attrs.Raw();
249
250   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
251        I = Attribute::AttrKind(I + 1)) {
252     if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
253       Attrs.insert(I);
254
255       if (I == Attribute::Alignment)
256         Alignment = 1ULL << ((A >> 16) - 1);
257       else if (I == Attribute::StackAlignment)
258         StackAlignment = 1ULL << ((A >> 26)-1);
259     }
260   }
261 }
262
263 void AttrBuilder::clear() {
264   Attrs.clear();
265   Alignment = StackAlignment = 0;
266 }
267
268 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
269   Attrs.insert(Val);
270   return *this;
271 }
272
273 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
274   Attrs.erase(Val);
275   if (Val == Attribute::Alignment)
276     Alignment = 0;
277   else if (Val == Attribute::StackAlignment)
278     StackAlignment = 0;
279
280   return *this;
281 }
282
283 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
284   if (Align == 0) return *this;
285
286   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
287   assert(Align <= 0x40000000 && "Alignment too large.");
288
289   Attrs.insert(Attribute::Alignment);
290   Alignment = Align;
291   return *this;
292 }
293
294 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
295   // Default alignment, allow the target to define how to align it.
296   if (Align == 0) return *this;
297
298   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
299   assert(Align <= 0x100 && "Alignment too large.");
300
301   Attrs.insert(Attribute::StackAlignment);
302   StackAlignment = Align;
303   return *this;
304 }
305
306 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
307   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
308        I = Attribute::AttrKind(I + 1)) {
309     if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
310       Attrs.insert(I);
311  
312       if (I == Attribute::Alignment)
313         Alignment = 1ULL << ((A >> 16) - 1);
314       else if (I == Attribute::StackAlignment)
315         StackAlignment = 1ULL << ((A >> 26)-1);
316     }
317   }
318  
319   return *this;
320 }
321
322 AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
323   uint64_t Mask = Attr.Raw();
324
325   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
326        I = Attribute::AttrKind(I + 1))
327     if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
328       Attrs.insert(I);
329
330   if (Attr.getAlignment())
331     Alignment = Attr.getAlignment();
332   if (Attr.getStackAlignment())
333     StackAlignment = Attr.getStackAlignment();
334   return *this;
335 }
336
337 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
338   uint64_t Mask = A.Raw();
339
340   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
341        I = Attribute::AttrKind(I + 1)) {
342     if (Mask & AttributeImpl::getAttrMask(I)) {
343       Attrs.erase(I);
344
345       if (I == Attribute::Alignment)
346         Alignment = 0;
347       else if (I == Attribute::StackAlignment)
348         StackAlignment = 0;
349     }
350   }
351
352   return *this;
353 }
354
355 bool AttrBuilder::contains(Attribute::AttrKind A) const {
356   return Attrs.count(A);
357 }
358
359 bool AttrBuilder::hasAttributes() const {
360   return !Attrs.empty();
361 }
362
363 bool AttrBuilder::hasAttributes(const Attribute &A) const {
364   return Raw() & A.Raw();
365 }
366
367 bool AttrBuilder::hasAlignmentAttr() const {
368   return Alignment != 0;
369 }
370
371 uint64_t AttrBuilder::Raw() const {
372   uint64_t Mask = 0;
373
374   for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
375          E = Attrs.end(); I != E; ++I) {
376     Attribute::AttrKind Kind = *I;
377
378     if (Kind == Attribute::Alignment)
379       Mask |= (Log2_32(Alignment) + 1) << 16;
380     else if (Kind == Attribute::StackAlignment)
381       Mask |= (Log2_32(StackAlignment) + 1) << 26;
382     else
383       Mask |= AttributeImpl::getAttrMask(Kind);
384   }
385
386   return Mask;
387 }
388
389 bool AttrBuilder::operator==(const AttrBuilder &B) {
390   SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
391   SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
392   return This == That;
393 }
394
395 //===----------------------------------------------------------------------===//
396 // AttributeImpl Definition
397 //===----------------------------------------------------------------------===//
398
399 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
400   : Context(C) {
401   Data = ConstantInt::get(Type::getInt64Ty(C), data);
402 }
403 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
404   : Context(C) {
405   Data = ConstantInt::get(Type::getInt64Ty(C), data);
406 }
407 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
408                              ArrayRef<Constant*> values)
409   : Context(C) {
410   Data = ConstantInt::get(Type::getInt64Ty(C), data);
411   Vals.reserve(values.size());
412   Vals.append(values.begin(), values.end());
413 }
414 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
415   : Context(C) {
416   Data = ConstantDataArray::getString(C, data);
417 }
418
419 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
420   if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
421     return CI->getZExtValue() == Kind;
422   return false;
423 }
424 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
425   return !(*this == Kind);
426 }
427
428 bool AttributeImpl::operator==(StringRef Kind) const {
429   if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
430     if (CDA->isString())
431       return CDA->getAsString() == Kind;
432   return false;
433 }
434 bool AttributeImpl::operator!=(StringRef Kind) const {
435   return !(*this == Kind);
436 }
437
438 uint64_t AttributeImpl::Raw() const {
439   // FIXME: Remove this.
440   return cast<ConstantInt>(Data)->getZExtValue();
441 }
442
443 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
444   switch (Val) {
445   case Attribute::EndAttrKinds:
446   case Attribute::AttrKindEmptyKey:
447   case Attribute::AttrKindTombstoneKey:
448     llvm_unreachable("Synthetic enumerators which should never get here");
449
450   case Attribute::None:            return 0;
451   case Attribute::ZExt:            return 1 << 0;
452   case Attribute::SExt:            return 1 << 1;
453   case Attribute::NoReturn:        return 1 << 2;
454   case Attribute::InReg:           return 1 << 3;
455   case Attribute::StructRet:       return 1 << 4;
456   case Attribute::NoUnwind:        return 1 << 5;
457   case Attribute::NoAlias:         return 1 << 6;
458   case Attribute::ByVal:           return 1 << 7;
459   case Attribute::Nest:            return 1 << 8;
460   case Attribute::ReadNone:        return 1 << 9;
461   case Attribute::ReadOnly:        return 1 << 10;
462   case Attribute::NoInline:        return 1 << 11;
463   case Attribute::AlwaysInline:    return 1 << 12;
464   case Attribute::OptimizeForSize: return 1 << 13;
465   case Attribute::StackProtect:    return 1 << 14;
466   case Attribute::StackProtectReq: return 1 << 15;
467   case Attribute::Alignment:       return 31 << 16;
468   case Attribute::NoCapture:       return 1 << 21;
469   case Attribute::NoRedZone:       return 1 << 22;
470   case Attribute::NoImplicitFloat: return 1 << 23;
471   case Attribute::Naked:           return 1 << 24;
472   case Attribute::InlineHint:      return 1 << 25;
473   case Attribute::StackAlignment:  return 7 << 26;
474   case Attribute::ReturnsTwice:    return 1 << 29;
475   case Attribute::UWTable:         return 1 << 30;
476   case Attribute::NonLazyBind:     return 1U << 31;
477   case Attribute::AddressSafety:   return 1ULL << 32;
478   case Attribute::MinSize:         return 1ULL << 33;
479   case Attribute::NoDuplicate:     return 1ULL << 34;
480   case Attribute::StackProtectStrong: return 1ULL << 35;
481   }
482   llvm_unreachable("Unsupported attribute type");
483 }
484
485 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
486   return (Raw() & getAttrMask(A)) != 0;
487 }
488
489 bool AttributeImpl::hasAttributes() const {
490   return Raw() != 0;
491 }
492
493 uint64_t AttributeImpl::getAlignment() const {
494   uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
495   return 1U << ((Mask >> 16) - 1);
496 }
497
498 uint64_t AttributeImpl::getStackAlignment() const {
499   uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
500   return 1U << ((Mask >> 26) - 1);
501 }
502
503 void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
504                             ArrayRef<Constant*> Vals) {
505   ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
506 #if 0
507   // FIXME: Not yet supported.
508   for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
509        I != E; ++I)
510     ID.AddPointer(*I);
511 #endif
512 }
513
514 //===----------------------------------------------------------------------===//
515 // AttributeWithIndex Definition
516 //===----------------------------------------------------------------------===//
517
518 AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
519                                            AttributeSet AS) {
520   // FIXME: This is temporary, but necessary for the conversion.
521   AttrBuilder B(AS, Idx);
522   return get(Idx, Attribute::get(C, B));
523 }
524
525 //===----------------------------------------------------------------------===//
526 // AttributeSetImpl Definition
527 //===----------------------------------------------------------------------===//
528
529 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
530   // FIXME: Remove.
531   return AttrList && hasAttributes(Idx) ?
532     AttributeSet::get(AttrList->getContext(),
533                       AttributeWithIndex::get(Idx, getAttributes(Idx))) :
534     AttributeSet();
535 }
536
537 AttributeSet AttributeSet::getRetAttributes() const {
538   // FIXME: Remove.
539   return AttrList && hasAttributes(ReturnIndex) ?
540     AttributeSet::get(AttrList->getContext(),
541                       AttributeWithIndex::get(ReturnIndex,
542                                               getAttributes(ReturnIndex))) :
543     AttributeSet();
544 }
545
546 AttributeSet AttributeSet::getFnAttributes() const {
547   // FIXME: Remove.
548   return AttrList && hasAttributes(FunctionIndex) ?
549     AttributeSet::get(AttrList->getContext(),
550                       AttributeWithIndex::get(FunctionIndex,
551                                               getAttributes(FunctionIndex))) :
552     AttributeSet();
553 }
554
555 AttributeSet AttributeSet::get(LLVMContext &C,
556                                ArrayRef<AttributeWithIndex> Attrs) {
557   // If there are no attributes then return a null AttributesList pointer.
558   if (Attrs.empty())
559     return AttributeSet();
560
561 #ifndef NDEBUG
562   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
563     assert(Attrs[i].Attrs.hasAttributes() &&
564            "Pointless attribute!");
565     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
566            "Misordered AttributesList!");
567   }
568 #endif
569
570   // Otherwise, build a key to look up the existing attributes.
571   LLVMContextImpl *pImpl = C.pImpl;
572   FoldingSetNodeID ID;
573   AttributeSetImpl::Profile(ID, Attrs);
574
575   void *InsertPoint;
576   AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
577
578   // If we didn't find any existing attributes of the same shape then
579   // create a new one and insert it.
580   if (!PA) {
581     PA = new AttributeSetImpl(C, Attrs);
582     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
583   }
584
585   // Return the AttributesList that we found or created.
586   return AttributeSet(PA);
587 }
588
589 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
590   // FIXME: This should be implemented as a loop that creates the
591   // AttributeWithIndexes that then are used to create the AttributeSet.
592   if (!B.hasAttributes())
593     return AttributeSet();
594   return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
595 }
596
597 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
598                                Attribute::AttrKind Kind) {
599   return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
600 }
601
602 //===----------------------------------------------------------------------===//
603 // AttributeSet Method Implementations
604 //===----------------------------------------------------------------------===//
605
606 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
607   AttrList = RHS.AttrList;
608   return *this;
609 }
610
611 /// getNumSlots - Return the number of slots used in this attribute list.
612 /// This is the number of arguments that have an attribute set on them
613 /// (including the function itself).
614 unsigned AttributeSet::getNumSlots() const {
615   return AttrList ? AttrList->getNumAttributes() : 0;
616 }
617
618 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
619 /// holds a number plus a set of attributes.
620 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
621   assert(AttrList && Slot < AttrList->getNumAttributes() &&
622          "Slot # out of range!");
623   return AttrList->getAttributes()[Slot];
624 }
625
626 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
627   return getAttributes(Index).hasAttribute(Kind);
628 }
629
630 bool AttributeSet::hasAttributes(unsigned Index) const {
631   return getAttributes(Index).hasAttributes();
632 }
633
634 std::string AttributeSet::getAsString(unsigned Index) const {
635   return getAttributes(Index).getAsString();
636 }
637
638 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
639   return getAttributes(Idx).getAlignment();
640 }
641
642 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
643   return getAttributes(Index).getStackAlignment();
644 }
645
646 uint64_t AttributeSet::Raw(unsigned Index) const {
647   // FIXME: Remove this.
648   return getAttributes(Index).Raw();
649 }
650
651 /// getAttributes - The attributes for the specified index are returned.
652 Attribute AttributeSet::getAttributes(unsigned Idx) const {
653   if (AttrList == 0) return Attribute();
654
655   ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
656   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
657     if (Attrs[i].Index == Idx)
658       return Attrs[i].Attrs;
659
660   return Attribute();
661 }
662
663 /// hasAttrSomewhere - Return true if the specified attribute is set for at
664 /// least one parameter or for the return value.
665 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
666   if (AttrList == 0) return false;
667
668   ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
669   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
670     if (Attrs[i].Attrs.hasAttribute(Attr))
671       return true;
672
673   return false;
674 }
675
676 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
677                                         Attribute::AttrKind Attr) const {
678   return addAttr(C, Idx, Attribute::get(C, Attr));
679 }
680
681 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
682                                          AttributeSet Attrs) const {
683   return addAttr(C, Idx, Attrs.getAttributes(Idx));
684 }
685
686 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
687                                    Attribute Attrs) const {
688   Attribute OldAttrs = getAttributes(Idx);
689 #ifndef NDEBUG
690   // FIXME it is not obvious how this should work for alignment.
691   // For now, say we can't change a known alignment.
692   unsigned OldAlign = OldAttrs.getAlignment();
693   unsigned NewAlign = Attrs.getAlignment();
694   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
695          "Attempt to change alignment!");
696 #endif
697
698   AttrBuilder NewAttrs =
699     AttrBuilder(OldAttrs).addAttributes(Attrs);
700   if (NewAttrs == AttrBuilder(OldAttrs))
701     return *this;
702
703   SmallVector<AttributeWithIndex, 8> NewAttrList;
704   if (AttrList == 0)
705     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
706   else {
707     ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
708     unsigned i = 0, e = OldAttrList.size();
709     // Copy attributes for arguments before this one.
710     for (; i != e && OldAttrList[i].Index < Idx; ++i)
711       NewAttrList.push_back(OldAttrList[i]);
712
713     // If there are attributes already at this index, merge them in.
714     if (i != e && OldAttrList[i].Index == Idx) {
715       Attrs =
716         Attribute::get(C, AttrBuilder(Attrs).
717                         addAttributes(OldAttrList[i].Attrs));
718       ++i;
719     }
720
721     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
722
723     // Copy attributes for arguments after this one.
724     NewAttrList.insert(NewAttrList.end(),
725                        OldAttrList.begin()+i, OldAttrList.end());
726   }
727
728   return get(C, NewAttrList);
729 }
730
731 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
732                                            Attribute::AttrKind Attr) const {
733   return removeAttr(C, Idx, Attribute::get(C, Attr));
734 }
735
736 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
737                                             AttributeSet Attrs) const {
738   return removeAttr(C, Idx, Attrs.getAttributes(Idx));
739 }
740
741 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
742                                       Attribute Attrs) const {
743 #ifndef NDEBUG
744   // FIXME it is not obvious how this should work for alignment.
745   // For now, say we can't pass in alignment, which no current use does.
746   assert(!Attrs.hasAttribute(Attribute::Alignment) &&
747          "Attempt to exclude alignment!");
748 #endif
749   if (AttrList == 0) return AttributeSet();
750
751   Attribute OldAttrs = getAttributes(Idx);
752   AttrBuilder NewAttrs =
753     AttrBuilder(OldAttrs).removeAttributes(Attrs);
754   if (NewAttrs == AttrBuilder(OldAttrs))
755     return *this;
756
757   SmallVector<AttributeWithIndex, 8> NewAttrList;
758   ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
759   unsigned i = 0, e = OldAttrList.size();
760
761   // Copy attributes for arguments before this one.
762   for (; i != e && OldAttrList[i].Index < Idx; ++i)
763     NewAttrList.push_back(OldAttrList[i]);
764
765   // If there are attributes already at this index, merge them in.
766   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
767   Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
768                           removeAttributes(Attrs));
769   ++i;
770   if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
771     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
772
773   // Copy attributes for arguments after this one.
774   NewAttrList.insert(NewAttrList.end(),
775                      OldAttrList.begin()+i, OldAttrList.end());
776
777   return get(C, NewAttrList);
778 }
779
780 void AttributeSet::dump() const {
781   dbgs() << "PAL[ ";
782   for (unsigned i = 0; i < getNumSlots(); ++i) {
783     const AttributeWithIndex &PAWI = getSlot(i);
784     dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
785   }
786
787   dbgs() << "]\n";
788 }