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