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