Create enums for the different attributes.
[oota-llvm.git] / lib / VMCore / Attributes.cpp
1 //===-- Attributes.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 AttributesList class and Attribute utilities.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Attributes.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/Type.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/Support/Atomic.h"
20 #include "llvm/Support/Mutex.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // Attributes Implementation
28 //===----------------------------------------------------------------------===//
29
30 Attributes::Attributes(uint64_t Val) : Attrs(Val) {}
31
32 Attributes::Attributes(Attribute::AttrConst Val) : Attrs(Val.v) {}
33
34 Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
35
36 Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
37
38 // FIXME: This is temporary until we have implemented the uniquified version of
39 // AttributesImpl.
40 Attributes Attributes::get(Attributes::Builder &B) {
41   return Attributes(B.Bits);
42 }
43
44 Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
45   // If there are no attributes, return an empty Attributes class.
46   if (B.Bits == 0)
47     return Attributes();
48
49   // Otherwise, build a key to look up the existing attributes.
50   LLVMContextImpl *pImpl = Context.pImpl;
51   FoldingSetNodeID ID;
52   ID.AddInteger(B.Bits);
53
54   void *InsertPoint;
55   AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
56
57   if (!PA) {
58     // If we didn't find any existing attributes of the same shape then create a
59     // new one and insert it.
60     PA = new AttributesImpl(B.Bits);
61     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
62   }
63
64   // Return the AttributesList that we found or created.
65   return Attributes(PA);
66 }
67
68 bool Attributes::hasAttributes(const Attributes &A) const {
69   return Attrs.hasAttributes(A);
70 }
71
72 bool Attributes::hasAttribute(AttrVal Val) const {
73   return Attrs.hasAttribute(Val);
74 }
75
76 /// This returns the alignment field of an attribute as a byte alignment value.
77 unsigned Attributes::getAlignment() const {
78   if (!hasAttribute(Attributes::Alignment))
79     return 0;
80   return 1U << ((Attrs.getAlignment() >> 16) - 1);
81 }
82
83 /// This returns the stack alignment field of an attribute as a byte alignment
84 /// value.
85 unsigned Attributes::getStackAlignment() const {
86   if (!hasAttribute(Attributes::StackAlignment))
87     return 0;
88   return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
89 }
90
91 bool Attributes::isEmptyOrSingleton() const {
92   return Attrs.isEmptyOrSingleton();
93 }
94
95 Attributes Attributes::operator | (const Attributes &A) const {
96   return Attributes(Raw() | A.Raw());
97 }
98 Attributes Attributes::operator & (const Attributes &A) const {
99   return Attributes(Raw() & A.Raw());
100 }
101 Attributes Attributes::operator ^ (const Attributes &A) const {
102   return Attributes(Raw() ^ A.Raw());
103 }
104 Attributes &Attributes::operator |= (const Attributes &A) {
105   Attrs.Bits |= A.Raw();
106   return *this;
107 }
108 Attributes &Attributes::operator &= (const Attributes &A) {
109   Attrs.Bits &= A.Raw();
110   return *this;
111 }
112 Attributes Attributes::operator ~ () const {
113   return Attributes(~Raw());
114 }
115
116 uint64_t Attributes::Raw() const {
117   return Attrs.Bits;
118 }
119
120 Attributes Attributes::typeIncompatible(Type *Ty) {
121   Attributes::Builder Incompatible;
122   
123   if (!Ty->isIntegerTy()) {
124     // Attributes that only apply to integers.
125     Incompatible.addSExtAttr();
126     Incompatible.addZExtAttr();
127   }
128   
129   if (!Ty->isPointerTy()) {
130     // Attributes that only apply to pointers.
131     Incompatible.addByValAttr();
132     Incompatible.addNestAttr();
133     Incompatible.addNoAliasAttr();
134     Incompatible.addNoCaptureAttr();
135     Incompatible.addStructRetAttr();
136   }
137   
138   return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
139 }
140
141 std::string Attributes::getAsString() const {
142   std::string Result;
143   if (hasAttribute(Attributes::ZExt))
144     Result += "zeroext ";
145   if (hasAttribute(Attributes::SExt))
146     Result += "signext ";
147   if (hasAttribute(Attributes::NoReturn))
148     Result += "noreturn ";
149   if (hasAttribute(Attributes::NoUnwind))
150     Result += "nounwind ";
151   if (hasAttribute(Attributes::UWTable))
152     Result += "uwtable ";
153   if (hasAttribute(Attributes::ReturnsTwice))
154     Result += "returns_twice ";
155   if (hasAttribute(Attributes::InReg))
156     Result += "inreg ";
157   if (hasAttribute(Attributes::NoAlias))
158     Result += "noalias ";
159   if (hasAttribute(Attributes::NoCapture))
160     Result += "nocapture ";
161   if (hasAttribute(Attributes::StructRet))
162     Result += "sret ";
163   if (hasAttribute(Attributes::ByVal))
164     Result += "byval ";
165   if (hasAttribute(Attributes::Nest))
166     Result += "nest ";
167   if (hasAttribute(Attributes::ReadNone))
168     Result += "readnone ";
169   if (hasAttribute(Attributes::ReadOnly))
170     Result += "readonly ";
171   if (hasAttribute(Attributes::OptimizeForSize))
172     Result += "optsize ";
173   if (hasAttribute(Attributes::NoInline))
174     Result += "noinline ";
175   if (hasAttribute(Attributes::InlineHint))
176     Result += "inlinehint ";
177   if (hasAttribute(Attributes::AlwaysInline))
178     Result += "alwaysinline ";
179   if (hasAttribute(Attributes::StackProtect))
180     Result += "ssp ";
181   if (hasAttribute(Attributes::StackProtectReq))
182     Result += "sspreq ";
183   if (hasAttribute(Attributes::NoRedZone))
184     Result += "noredzone ";
185   if (hasAttribute(Attributes::NoImplicitFloat))
186     Result += "noimplicitfloat ";
187   if (hasAttribute(Attributes::Naked))
188     Result += "naked ";
189   if (hasAttribute(Attributes::NonLazyBind))
190     Result += "nonlazybind ";
191   if (hasAttribute(Attributes::AddressSafety))
192     Result += "address_safety ";
193   if (hasAttribute(Attributes::StackAlignment)) {
194     Result += "alignstack(";
195     Result += utostr(getStackAlignment());
196     Result += ") ";
197   }
198   if (hasAttribute(Attributes::Alignment)) {
199     Result += "align ";
200     Result += utostr(getAlignment());
201     Result += " ";
202   }
203   // Trim the trailing space.
204   assert(!Result.empty() && "Unknown attribute!");
205   Result.erase(Result.end()-1);
206   return Result;
207 }
208
209 //===----------------------------------------------------------------------===//
210 // Attributes::Builder Implementation
211 //===----------------------------------------------------------------------===//
212
213 void Attributes::Builder::addAddressSafetyAttr() {
214   Bits |= Attribute::AddressSafety_i;
215 }
216 void Attributes::Builder::addAlwaysInlineAttr() {
217   Bits |= Attribute::AlwaysInline_i;
218 }
219 void Attributes::Builder::addByValAttr() {
220   Bits |= Attribute::ByVal_i;
221 }
222 void Attributes::Builder::addInlineHintAttr() {
223   Bits |= Attribute::InlineHint_i;
224 }
225 void Attributes::Builder::addInRegAttr() {
226   Bits |= Attribute::InReg_i;
227 }
228 void Attributes::Builder::addNakedAttr() {
229   Bits |= Attribute::Naked_i;
230 }
231 void Attributes::Builder::addNestAttr() {
232   Bits |= Attribute::Nest_i;
233 }
234 void Attributes::Builder::addNoAliasAttr() {
235   Bits |= Attribute::NoAlias_i;
236 }
237 void Attributes::Builder::addNoCaptureAttr() {
238   Bits |= Attribute::NoCapture_i;
239 }
240 void Attributes::Builder::addNoImplicitFloatAttr() {
241   Bits |= Attribute::NoImplicitFloat_i;
242 }
243 void Attributes::Builder::addNoInlineAttr() {
244   Bits |= Attribute::NoInline_i;
245 }
246 void Attributes::Builder::addNonLazyBindAttr() {
247   Bits |= Attribute::NonLazyBind_i;
248 }
249 void Attributes::Builder::addNoRedZoneAttr() {
250   Bits |= Attribute::NoRedZone_i;
251 }
252 void Attributes::Builder::addNoReturnAttr() {
253   Bits |= Attribute::NoReturn_i;
254 }
255 void Attributes::Builder::addNoUnwindAttr() {
256   Bits |= Attribute::NoUnwind_i;
257 }
258 void Attributes::Builder::addOptimizeForSizeAttr() {
259   Bits |= Attribute::OptimizeForSize_i;
260 }
261 void Attributes::Builder::addReadNoneAttr() {
262   Bits |= Attribute::ReadNone_i;
263 }
264 void Attributes::Builder::addReadOnlyAttr() {
265   Bits |= Attribute::ReadOnly_i;
266 }
267 void Attributes::Builder::addReturnsTwiceAttr() {
268   Bits |= Attribute::ReturnsTwice_i;
269 }
270 void Attributes::Builder::addSExtAttr() {
271   Bits |= Attribute::SExt_i;
272 }
273 void Attributes::Builder::addStackProtectAttr() {
274   Bits |= Attribute::StackProtect_i;
275 }
276 void Attributes::Builder::addStackProtectReqAttr() {
277   Bits |= Attribute::StackProtectReq_i;
278 }
279 void Attributes::Builder::addStructRetAttr() {
280   Bits |= Attribute::StructRet_i;
281 }
282 void Attributes::Builder::addUWTableAttr() {
283   Bits |= Attribute::UWTable_i;
284 }
285 void Attributes::Builder::addZExtAttr() {
286   Bits |= Attribute::ZExt_i;
287 }
288
289 void Attributes::Builder::addAlignmentAttr(unsigned Align) {
290   if (Align == 0) return;
291   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
292   assert(Align <= 0x40000000 && "Alignment too large.");
293   Bits |= (Log2_32(Align) + 1) << 16;
294 }
295 void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
296   // Default alignment, allow the target to define how to align it.
297   if (Align == 0) return;
298   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
299   assert(Align <= 0x100 && "Alignment too large.");
300   Bits |= (Log2_32(Align) + 1) << 26;
301 }
302
303 void Attributes::Builder::removeAttributes(const Attributes &A) {
304   Bits &= ~A.Raw();
305 }
306
307 void Attributes::Builder::removeAddressSafetyAttr() {
308   Bits &= ~Attribute::AddressSafety_i;
309 }
310 void Attributes::Builder::removeAlwaysInlineAttr() {
311   Bits &= ~Attribute::AlwaysInline_i;
312 }
313 void Attributes::Builder::removeByValAttr() {
314   Bits &= ~Attribute::ByVal_i;
315 }
316 void Attributes::Builder::removeInlineHintAttr() {
317   Bits &= ~Attribute::InlineHint_i;
318 }
319 void Attributes::Builder::removeInRegAttr() {
320   Bits &= ~Attribute::InReg_i;
321 }
322 void Attributes::Builder::removeNakedAttr() {
323   Bits &= ~Attribute::Naked_i;
324 }
325 void Attributes::Builder::removeNestAttr() {
326   Bits &= ~Attribute::Nest_i;
327 }
328 void Attributes::Builder::removeNoAliasAttr() {
329   Bits &= ~Attribute::NoAlias_i;
330 }
331 void Attributes::Builder::removeNoCaptureAttr() {
332   Bits &= ~Attribute::NoCapture_i;
333 }
334 void Attributes::Builder::removeNoImplicitFloatAttr() {
335   Bits &= ~Attribute::NoImplicitFloat_i;
336 }
337 void Attributes::Builder::removeNoInlineAttr() {
338   Bits &= ~Attribute::NoInline_i;
339 }
340 void Attributes::Builder::removeNonLazyBindAttr() {
341   Bits &= ~Attribute::NonLazyBind_i;
342 }
343 void Attributes::Builder::removeNoRedZoneAttr() {
344   Bits &= ~Attribute::NoRedZone_i;
345 }
346 void Attributes::Builder::removeNoReturnAttr() {
347   Bits &= ~Attribute::NoReturn_i;
348 }
349 void Attributes::Builder::removeNoUnwindAttr() {
350   Bits &= ~Attribute::NoUnwind_i;
351 }
352 void Attributes::Builder::removeOptimizeForSizeAttr() {
353   Bits &= ~Attribute::OptimizeForSize_i;
354 }
355 void Attributes::Builder::removeReadNoneAttr() {
356   Bits &= ~Attribute::ReadNone_i;
357 }
358 void Attributes::Builder::removeReadOnlyAttr() {
359   Bits &= ~Attribute::ReadOnly_i;
360 }
361 void Attributes::Builder::removeReturnsTwiceAttr() {
362   Bits &= ~Attribute::ReturnsTwice_i;
363 }
364 void Attributes::Builder::removeSExtAttr() {
365   Bits &= ~Attribute::SExt_i;
366 }
367 void Attributes::Builder::removeStackProtectAttr() {
368   Bits &= ~Attribute::StackProtect_i;
369 }
370 void Attributes::Builder::removeStackProtectReqAttr() {
371   Bits &= ~Attribute::StackProtectReq_i;
372 }
373 void Attributes::Builder::removeStructRetAttr() {
374   Bits &= ~Attribute::StructRet_i;
375 }
376 void Attributes::Builder::removeUWTableAttr() {
377   Bits &= ~Attribute::UWTable_i;
378 }
379 void Attributes::Builder::removeZExtAttr() {
380   Bits &= ~Attribute::ZExt_i;
381 }
382
383 void Attributes::Builder::removeAlignmentAttr() {
384   Bits &= ~Attribute::Alignment_i;
385 }
386 void Attributes::Builder::removeStackAlignmentAttr() {
387   Bits &= ~Attribute::StackAlignment_i;
388 }
389
390 bool Attributes::Builder::hasAttributes() const {
391   return Bits != 0;
392 }
393 bool Attributes::Builder::hasAttributes(const Attributes &A) const {
394   return Bits & A.Raw();
395 }
396 bool Attributes::Builder::hasAlignmentAttr() const {
397   return Bits & Attribute::Alignment_i;
398 }
399
400 uint64_t Attributes::Builder::getAlignment() const {
401   if (!hasAlignmentAttr())
402     return 0;
403   return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1);
404 }
405
406 //===----------------------------------------------------------------------===//
407 // AttributeImpl Definition
408 //===----------------------------------------------------------------------===//
409
410 uint64_t AttributesImpl::getAttrMask(uint64_t Val) const {
411   switch (Val) {
412   case Attributes::None:            return 0;
413   case Attributes::ZExt:            return 1 << 0;
414   case Attributes::SExt:            return 1 << 1;
415   case Attributes::NoReturn:        return 1 << 2;
416   case Attributes::InReg:           return 1 << 3;
417   case Attributes::StructRet:       return 1 << 4;
418   case Attributes::NoUnwind:        return 1 << 5;
419   case Attributes::NoAlias:         return 1 << 6;
420   case Attributes::ByVal:           return 1 << 7;
421   case Attributes::Nest:            return 1 << 8;
422   case Attributes::ReadNone:        return 1 << 9;
423   case Attributes::ReadOnly:        return 1 << 10;
424   case Attributes::NoInline:        return 1 << 11;
425   case Attributes::AlwaysInline:    return 1 << 12;
426   case Attributes::OptimizeForSize: return 1 << 13;
427   case Attributes::StackProtect:    return 1 << 14;
428   case Attributes::StackProtectReq: return 1 << 15;
429   case Attributes::Alignment:       return 31 << 16;
430   case Attributes::NoCapture:       return 1 << 21;
431   case Attributes::NoRedZone:       return 1 << 22;
432   case Attributes::NoImplicitFloat: return 1 << 23;
433   case Attributes::Naked:           return 1 << 24;
434   case Attributes::InlineHint:      return 1 << 25;
435   case Attributes::StackAlignment:  return 7 << 26;
436   case Attributes::ReturnsTwice:    return 1 << 29;
437   case Attributes::UWTable:         return 1 << 30;
438   case Attributes::NonLazyBind:     return 1U << 31;
439   case Attributes::AddressSafety:   return 1ULL << 32;
440   }
441   llvm_unreachable("Unsupported attribute type");
442 }
443
444 bool AttributesImpl::hasAttribute(uint64_t A) const {
445   return (Bits & getAttrMask(A)) != 0;
446 }
447
448 bool AttributesImpl::hasAttributes() const {
449   return Bits != 0;
450 }
451
452 bool AttributesImpl::hasAttributes(const Attributes &A) const {
453   return Bits & A.Raw();        // FIXME: Raw() won't work here in the future.
454 }
455
456 uint64_t AttributesImpl::getAlignment() const {
457   return Bits & Attribute::Alignment_i;
458 }
459
460 uint64_t AttributesImpl::getStackAlignment() const {
461   return Bits & Attribute::StackAlignment_i;
462 }
463
464 bool AttributesImpl::isEmptyOrSingleton() const {
465   return (Bits & (Bits - 1)) == 0;
466 }
467
468 //===----------------------------------------------------------------------===//
469 // AttributeListImpl Definition
470 //===----------------------------------------------------------------------===//
471
472 namespace llvm {
473   class AttributeListImpl;
474 }
475
476 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
477
478 namespace llvm {
479 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
480
481 class AttributeListImpl : public FoldingSetNode {
482   sys::cas_flag RefCount;
483   
484   // AttributesList is uniqued, these should not be publicly available.
485   void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
486   AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
487   ~AttributeListImpl();                        // Private implementation
488 public:
489   SmallVector<AttributeWithIndex, 4> Attrs;
490   
491   AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
492     : Attrs(attrs.begin(), attrs.end()) {
493     RefCount = 0;
494   }
495   
496   void AddRef() {
497     sys::SmartScopedLock<true> Lock(*ALMutex);
498     ++RefCount;
499   }
500   void DropRef() {
501     sys::SmartScopedLock<true> Lock(*ALMutex);
502     if (!AttributesLists.isConstructed())
503       return;
504     sys::cas_flag new_val = --RefCount;
505     if (new_val == 0)
506       delete this;
507   }
508   
509   void Profile(FoldingSetNodeID &ID) const {
510     Profile(ID, Attrs);
511   }
512   static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
513     for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
514       ID.AddInteger(Attrs[i].Attrs.Raw());
515       ID.AddInteger(Attrs[i].Index);
516     }
517   }
518 };
519 }
520
521 AttributeListImpl::~AttributeListImpl() {
522   // NOTE: Lock must be acquired by caller.
523   AttributesLists->RemoveNode(this);
524 }
525
526
527 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
528   // If there are no attributes then return a null AttributesList pointer.
529   if (Attrs.empty())
530     return AttrListPtr();
531   
532 #ifndef NDEBUG
533   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
534     assert(Attrs[i].Attrs.hasAttributes() && 
535            "Pointless attribute!");
536     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
537            "Misordered AttributesList!");
538   }
539 #endif
540   
541   // Otherwise, build a key to look up the existing attributes.
542   FoldingSetNodeID ID;
543   AttributeListImpl::Profile(ID, Attrs);
544   void *InsertPos;
545   
546   sys::SmartScopedLock<true> Lock(*ALMutex);
547   
548   AttributeListImpl *PAL =
549     AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
550   
551   // If we didn't find any existing attributes of the same shape then
552   // create a new one and insert it.
553   if (!PAL) {
554     PAL = new AttributeListImpl(Attrs);
555     AttributesLists->InsertNode(PAL, InsertPos);
556   }
557   
558   // Return the AttributesList that we found or created.
559   return AttrListPtr(PAL);
560 }
561
562
563 //===----------------------------------------------------------------------===//
564 // AttrListPtr Method Implementations
565 //===----------------------------------------------------------------------===//
566
567 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
568   if (LI) LI->AddRef();
569 }
570
571 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
572   if (AttrList) AttrList->AddRef();  
573 }
574
575 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
576   sys::SmartScopedLock<true> Lock(*ALMutex);
577   if (AttrList == RHS.AttrList) return *this;
578   if (AttrList) AttrList->DropRef();
579   AttrList = RHS.AttrList;
580   if (AttrList) AttrList->AddRef();
581   return *this;
582 }
583
584 AttrListPtr::~AttrListPtr() {
585   if (AttrList) AttrList->DropRef();
586 }
587
588 /// getNumSlots - Return the number of slots used in this attribute list. 
589 /// This is the number of arguments that have an attribute set on them
590 /// (including the function itself).
591 unsigned AttrListPtr::getNumSlots() const {
592   return AttrList ? AttrList->Attrs.size() : 0;
593 }
594
595 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
596 /// holds a number plus a set of attributes.
597 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
598   assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
599   return AttrList->Attrs[Slot];
600 }
601
602
603 /// getAttributes - The attributes for the specified index are
604 /// returned.  Attributes for the result are denoted with Idx = 0.
605 /// Function notes are denoted with idx = ~0.
606 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
607   if (AttrList == 0) return Attributes();
608   
609   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
610   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
611     if (Attrs[i].Index == Idx)
612       return Attrs[i].Attrs;
613
614   return Attributes();
615 }
616
617 /// hasAttrSomewhere - Return true if the specified attribute is set for at
618 /// least one parameter or for the return value.
619 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
620   if (AttrList == 0) return false;
621   
622   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
623   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
624     if (Attrs[i].Attrs.hasAttributes(Attr))
625       return true;
626   return false;
627 }
628
629 unsigned AttrListPtr::getNumAttrs() const {
630   return AttrList ? AttrList->Attrs.size() : 0;
631 }
632
633 Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
634   assert(AttrList && "Trying to get an attribute from an empty list!");
635   assert(i < AttrList->Attrs.size() && "Index out of range!");
636   return AttrList->Attrs[i].Attrs;
637 }
638
639 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
640   Attributes OldAttrs = getAttributes(Idx);
641 #ifndef NDEBUG
642   // FIXME it is not obvious how this should work for alignment.
643   // For now, say we can't change a known alignment.
644   unsigned OldAlign = OldAttrs.getAlignment();
645   unsigned NewAlign = Attrs.getAlignment();
646   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
647          "Attempt to change alignment!");
648 #endif
649   
650   Attributes NewAttrs = OldAttrs | Attrs;
651   if (NewAttrs == OldAttrs)
652     return *this;
653   
654   SmallVector<AttributeWithIndex, 8> NewAttrList;
655   if (AttrList == 0)
656     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
657   else {
658     const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
659     unsigned i = 0, e = OldAttrList.size();
660     // Copy attributes for arguments before this one.
661     for (; i != e && OldAttrList[i].Index < Idx; ++i)
662       NewAttrList.push_back(OldAttrList[i]);
663
664     // If there are attributes already at this index, merge them in.
665     if (i != e && OldAttrList[i].Index == Idx) {
666       Attrs |= OldAttrList[i].Attrs;
667       ++i;
668     }
669     
670     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
671     
672     // Copy attributes for arguments after this one.
673     NewAttrList.insert(NewAttrList.end(), 
674                        OldAttrList.begin()+i, OldAttrList.end());
675   }
676   
677   return get(NewAttrList);
678 }
679
680 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
681 #ifndef NDEBUG
682   // FIXME it is not obvious how this should work for alignment.
683   // For now, say we can't pass in alignment, which no current use does.
684   assert(!Attrs.hasAttribute(Attributes::Alignment) &&
685          "Attempt to exclude alignment!");
686 #endif
687   if (AttrList == 0) return AttrListPtr();
688   
689   Attributes OldAttrs = getAttributes(Idx);
690   Attributes NewAttrs = OldAttrs & ~Attrs;
691   if (NewAttrs == OldAttrs)
692     return *this;
693
694   SmallVector<AttributeWithIndex, 8> NewAttrList;
695   const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
696   unsigned i = 0, e = OldAttrList.size();
697   
698   // Copy attributes for arguments before this one.
699   for (; i != e && OldAttrList[i].Index < Idx; ++i)
700     NewAttrList.push_back(OldAttrList[i]);
701   
702   // If there are attributes already at this index, merge them in.
703   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
704   Attrs = OldAttrList[i].Attrs & ~Attrs;
705   ++i;
706   if (Attrs)  // If any attributes left for this parameter, add them.
707     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
708   
709   // Copy attributes for arguments after this one.
710   NewAttrList.insert(NewAttrList.end(), 
711                      OldAttrList.begin()+i, OldAttrList.end());
712   
713   return get(NewAttrList);
714 }
715
716 void AttrListPtr::dump() const {
717   dbgs() << "PAL[ ";
718   for (unsigned i = 0; i < getNumSlots(); ++i) {
719     const AttributeWithIndex &PAWI = getSlot(i);
720     dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
721   }
722   
723   dbgs() << "]\n";
724 }