Remove dead methods.
[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 /// 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 1U << ((pImpl->getStackAlignment() >> 26) - 1);
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   return Raw() & getAttrMask(Attribute::Alignment);
495 }
496
497 uint64_t AttributeImpl::getStackAlignment() const {
498   return Raw() & getAttrMask(Attribute::StackAlignment);
499 }
500
501 void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
502                             ArrayRef<Constant*> Vals) {
503   ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
504 #if 0
505   // FIXME: Not yet supported.
506   for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
507        I != E; ++I)
508     ID.AddPointer(*I);
509 #endif
510 }
511
512 //===----------------------------------------------------------------------===//
513 // AttributeWithIndex Definition
514 //===----------------------------------------------------------------------===//
515
516 AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
517                                            AttributeSet AS) {
518   // FIXME: This is temporary, but necessary for the conversion.
519   AttrBuilder B(AS, Idx);
520   return get(Idx, Attribute::get(C, B));
521 }
522
523 //===----------------------------------------------------------------------===//
524 // AttributeSetImpl Definition
525 //===----------------------------------------------------------------------===//
526
527 AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
528   // FIXME: Remove.
529   return AttrList && hasAttributes(Idx) ?
530     AttributeSet::get(AttrList->getContext(),
531                       AttributeWithIndex::get(Idx, getAttributes(Idx))) :
532     AttributeSet();
533 }
534
535 AttributeSet AttributeSet::getRetAttributes() const {
536   // FIXME: Remove.
537   return AttrList && hasAttributes(ReturnIndex) ?
538     AttributeSet::get(AttrList->getContext(),
539                       AttributeWithIndex::get(ReturnIndex,
540                                               getAttributes(ReturnIndex))) :
541     AttributeSet();
542 }
543
544 AttributeSet AttributeSet::getFnAttributes() const {
545   // FIXME: Remove.
546   return AttrList && hasAttributes(FunctionIndex) ?
547     AttributeSet::get(AttrList->getContext(),
548                       AttributeWithIndex::get(FunctionIndex,
549                                               getAttributes(FunctionIndex))) :
550     AttributeSet();
551 }
552
553 AttributeSet AttributeSet::get(LLVMContext &C,
554                                ArrayRef<AttributeWithIndex> Attrs) {
555   // If there are no attributes then return a null AttributesList pointer.
556   if (Attrs.empty())
557     return AttributeSet();
558
559 #ifndef NDEBUG
560   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
561     assert(Attrs[i].Attrs.hasAttributes() &&
562            "Pointless attribute!");
563     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
564            "Misordered AttributesList!");
565   }
566 #endif
567
568   // Otherwise, build a key to look up the existing attributes.
569   LLVMContextImpl *pImpl = C.pImpl;
570   FoldingSetNodeID ID;
571   AttributeSetImpl::Profile(ID, Attrs);
572
573   void *InsertPoint;
574   AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
575
576   // If we didn't find any existing attributes of the same shape then
577   // create a new one and insert it.
578   if (!PA) {
579     PA = new AttributeSetImpl(C, Attrs);
580     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
581   }
582
583   // Return the AttributesList that we found or created.
584   return AttributeSet(PA);
585 }
586
587 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
588   // FIXME: This should be implemented as a loop that creates the
589   // AttributeWithIndexes that then are used to create the AttributeSet.
590   if (!B.hasAttributes())
591     return AttributeSet();
592   return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
593 }
594
595 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
596                                Attribute::AttrKind Kind) {
597   return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
598 }
599
600 //===----------------------------------------------------------------------===//
601 // AttributeSet Method Implementations
602 //===----------------------------------------------------------------------===//
603
604 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
605   AttrList = RHS.AttrList;
606   return *this;
607 }
608
609 /// getNumSlots - Return the number of slots used in this attribute list.
610 /// This is the number of arguments that have an attribute set on them
611 /// (including the function itself).
612 unsigned AttributeSet::getNumSlots() const {
613   return AttrList ? AttrList->getNumAttributes() : 0;
614 }
615
616 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
617 /// holds a number plus a set of attributes.
618 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
619   assert(AttrList && Slot < AttrList->getNumAttributes() &&
620          "Slot # out of range!");
621   return AttrList->getAttributes()[Slot];
622 }
623
624 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
625   return getAttributes(Index).hasAttribute(Kind);
626 }
627
628 bool AttributeSet::hasAttributes(unsigned Index) const {
629   return getAttributes(Index).hasAttributes();
630 }
631
632 std::string AttributeSet::getAsString(unsigned Index) const {
633   return getAttributes(Index).getAsString();
634 }
635
636 unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
637   return getAttributes(Idx).getAlignment();
638 }
639
640 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
641   return getAttributes(Index).getStackAlignment();
642 }
643
644 uint64_t AttributeSet::Raw(unsigned Index) const {
645   // FIXME: Remove this.
646   return getAttributes(Index).Raw();
647 }
648
649 /// getAttributes - The attributes for the specified index are returned.
650 Attribute AttributeSet::getAttributes(unsigned Idx) const {
651   if (AttrList == 0) return Attribute();
652
653   ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
654   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
655     if (Attrs[i].Index == Idx)
656       return Attrs[i].Attrs;
657
658   return Attribute();
659 }
660
661 /// hasAttrSomewhere - Return true if the specified attribute is set for at
662 /// least one parameter or for the return value.
663 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
664   if (AttrList == 0) return false;
665
666   ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
667   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
668     if (Attrs[i].Attrs.hasAttribute(Attr))
669       return true;
670
671   return false;
672 }
673
674 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
675                                         Attribute::AttrKind Attr) const {
676   return addAttr(C, Idx, Attribute::get(C, Attr));
677 }
678
679 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
680                                          AttributeSet Attrs) const {
681   return addAttr(C, Idx, Attrs.getAttributes(Idx));
682 }
683
684 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
685                                    Attribute Attrs) const {
686   Attribute OldAttrs = getAttributes(Idx);
687 #ifndef NDEBUG
688   // FIXME it is not obvious how this should work for alignment.
689   // For now, say we can't change a known alignment.
690   unsigned OldAlign = OldAttrs.getAlignment();
691   unsigned NewAlign = Attrs.getAlignment();
692   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
693          "Attempt to change alignment!");
694 #endif
695
696   AttrBuilder NewAttrs =
697     AttrBuilder(OldAttrs).addAttributes(Attrs);
698   if (NewAttrs == AttrBuilder(OldAttrs))
699     return *this;
700
701   SmallVector<AttributeWithIndex, 8> NewAttrList;
702   if (AttrList == 0)
703     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
704   else {
705     ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
706     unsigned i = 0, e = OldAttrList.size();
707     // Copy attributes for arguments before this one.
708     for (; i != e && OldAttrList[i].Index < Idx; ++i)
709       NewAttrList.push_back(OldAttrList[i]);
710
711     // If there are attributes already at this index, merge them in.
712     if (i != e && OldAttrList[i].Index == Idx) {
713       Attrs =
714         Attribute::get(C, AttrBuilder(Attrs).
715                         addAttributes(OldAttrList[i].Attrs));
716       ++i;
717     }
718
719     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
720
721     // Copy attributes for arguments after this one.
722     NewAttrList.insert(NewAttrList.end(),
723                        OldAttrList.begin()+i, OldAttrList.end());
724   }
725
726   return get(C, NewAttrList);
727 }
728
729 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
730                                            Attribute::AttrKind Attr) const {
731   return removeAttr(C, Idx, Attribute::get(C, Attr));
732 }
733
734 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
735                                             AttributeSet Attrs) const {
736   return removeAttr(C, Idx, Attrs.getAttributes(Idx));
737 }
738
739 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
740                                       Attribute Attrs) const {
741 #ifndef NDEBUG
742   // FIXME it is not obvious how this should work for alignment.
743   // For now, say we can't pass in alignment, which no current use does.
744   assert(!Attrs.hasAttribute(Attribute::Alignment) &&
745          "Attempt to exclude alignment!");
746 #endif
747   if (AttrList == 0) return AttributeSet();
748
749   Attribute OldAttrs = getAttributes(Idx);
750   AttrBuilder NewAttrs =
751     AttrBuilder(OldAttrs).removeAttributes(Attrs);
752   if (NewAttrs == AttrBuilder(OldAttrs))
753     return *this;
754
755   SmallVector<AttributeWithIndex, 8> NewAttrList;
756   ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
757   unsigned i = 0, e = OldAttrList.size();
758
759   // Copy attributes for arguments before this one.
760   for (; i != e && OldAttrList[i].Index < Idx; ++i)
761     NewAttrList.push_back(OldAttrList[i]);
762
763   // If there are attributes already at this index, merge them in.
764   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
765   Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
766                           removeAttributes(Attrs));
767   ++i;
768   if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
769     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
770
771   // Copy attributes for arguments after this one.
772   NewAttrList.insert(NewAttrList.end(),
773                      OldAttrList.begin()+i, OldAttrList.end());
774
775   return get(C, NewAttrList);
776 }
777
778 void AttributeSet::dump() const {
779   dbgs() << "PAL[ ";
780   for (unsigned i = 0; i < getNumSlots(); ++i) {
781     const AttributeWithIndex &PAWI = getSlot(i);
782     dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
783   }
784
785   dbgs() << "]\n";
786 }