Use the enum value of the attributes when adding them to the attributes builder.
[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::hasAttribute(AttrVal Val) const {
69   return Attrs.hasAttribute(Val);
70 }
71
72 bool Attributes::hasAttributes(const Attributes &A) const {
73   return Attrs.hasAttributes(A);
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.addAttribute(Attributes::SExt);
126     Incompatible.addAttribute(Attributes::ZExt);
127   }
128   
129   if (!Ty->isPointerTy()) {
130     // Attributes that only apply to pointers.
131     Incompatible.addAttribute(Attributes::ByVal);
132     Incompatible.addAttribute(Attributes::Nest);
133     Incompatible.addAttribute(Attributes::NoAlias);
134     Incompatible.addAttribute(Attributes::NoCapture);
135     Incompatible.addAttribute(Attributes::StructRet);
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::addAttribute(Attributes::AttrVal Val) {
214   Bits |= AttributesImpl::getAttrMask(Val);
215 }
216
217 void Attributes::Builder::addAlignmentAttr(unsigned Align) {
218   if (Align == 0) return;
219   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
220   assert(Align <= 0x40000000 && "Alignment too large.");
221   Bits |= (Log2_32(Align) + 1) << 16;
222 }
223 void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
224   // Default alignment, allow the target to define how to align it.
225   if (Align == 0) return;
226   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
227   assert(Align <= 0x100 && "Alignment too large.");
228   Bits |= (Log2_32(Align) + 1) << 26;
229 }
230
231 void Attributes::Builder::removeAttribute(Attributes::AttrVal Val) {
232   Bits &= ~AttributesImpl::getAttrMask(Val);
233 }
234
235 void Attributes::Builder::removeAttributes(const Attributes &A) {
236   Bits &= ~A.Raw();
237 }
238
239 void Attributes::Builder::removeAddressSafetyAttr() {
240   Bits &= ~Attribute::AddressSafety_i;
241 }
242 void Attributes::Builder::removeAlwaysInlineAttr() {
243   Bits &= ~Attribute::AlwaysInline_i;
244 }
245 void Attributes::Builder::removeByValAttr() {
246   Bits &= ~Attribute::ByVal_i;
247 }
248 void Attributes::Builder::removeInlineHintAttr() {
249   Bits &= ~Attribute::InlineHint_i;
250 }
251 void Attributes::Builder::removeInRegAttr() {
252   Bits &= ~Attribute::InReg_i;
253 }
254 void Attributes::Builder::removeNakedAttr() {
255   Bits &= ~Attribute::Naked_i;
256 }
257 void Attributes::Builder::removeNestAttr() {
258   Bits &= ~Attribute::Nest_i;
259 }
260 void Attributes::Builder::removeNoAliasAttr() {
261   Bits &= ~Attribute::NoAlias_i;
262 }
263 void Attributes::Builder::removeNoCaptureAttr() {
264   Bits &= ~Attribute::NoCapture_i;
265 }
266 void Attributes::Builder::removeNoImplicitFloatAttr() {
267   Bits &= ~Attribute::NoImplicitFloat_i;
268 }
269 void Attributes::Builder::removeNoInlineAttr() {
270   Bits &= ~Attribute::NoInline_i;
271 }
272 void Attributes::Builder::removeNonLazyBindAttr() {
273   Bits &= ~Attribute::NonLazyBind_i;
274 }
275 void Attributes::Builder::removeNoRedZoneAttr() {
276   Bits &= ~Attribute::NoRedZone_i;
277 }
278 void Attributes::Builder::removeNoReturnAttr() {
279   Bits &= ~Attribute::NoReturn_i;
280 }
281 void Attributes::Builder::removeNoUnwindAttr() {
282   Bits &= ~Attribute::NoUnwind_i;
283 }
284 void Attributes::Builder::removeOptimizeForSizeAttr() {
285   Bits &= ~Attribute::OptimizeForSize_i;
286 }
287 void Attributes::Builder::removeReadNoneAttr() {
288   Bits &= ~Attribute::ReadNone_i;
289 }
290 void Attributes::Builder::removeReadOnlyAttr() {
291   Bits &= ~Attribute::ReadOnly_i;
292 }
293 void Attributes::Builder::removeReturnsTwiceAttr() {
294   Bits &= ~Attribute::ReturnsTwice_i;
295 }
296 void Attributes::Builder::removeSExtAttr() {
297   Bits &= ~Attribute::SExt_i;
298 }
299 void Attributes::Builder::removeStackProtectAttr() {
300   Bits &= ~Attribute::StackProtect_i;
301 }
302 void Attributes::Builder::removeStackProtectReqAttr() {
303   Bits &= ~Attribute::StackProtectReq_i;
304 }
305 void Attributes::Builder::removeStructRetAttr() {
306   Bits &= ~Attribute::StructRet_i;
307 }
308 void Attributes::Builder::removeUWTableAttr() {
309   Bits &= ~Attribute::UWTable_i;
310 }
311 void Attributes::Builder::removeZExtAttr() {
312   Bits &= ~Attribute::ZExt_i;
313 }
314
315 void Attributes::Builder::removeAlignmentAttr() {
316   Bits &= ~Attribute::Alignment_i;
317 }
318 void Attributes::Builder::removeStackAlignmentAttr() {
319   Bits &= ~Attribute::StackAlignment_i;
320 }
321
322 bool Attributes::Builder::hasAttributes() const {
323   return Bits != 0;
324 }
325 bool Attributes::Builder::hasAttributes(const Attributes &A) const {
326   return Bits & A.Raw();
327 }
328 bool Attributes::Builder::hasAlignmentAttr() const {
329   return Bits & Attribute::Alignment_i;
330 }
331
332 uint64_t Attributes::Builder::getAlignment() const {
333   if (!hasAlignmentAttr())
334     return 0;
335   return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1);
336 }
337
338 //===----------------------------------------------------------------------===//
339 // AttributeImpl Definition
340 //===----------------------------------------------------------------------===//
341
342 uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
343   switch (Val) {
344   case Attributes::None:            return 0;
345   case Attributes::ZExt:            return 1 << 0;
346   case Attributes::SExt:            return 1 << 1;
347   case Attributes::NoReturn:        return 1 << 2;
348   case Attributes::InReg:           return 1 << 3;
349   case Attributes::StructRet:       return 1 << 4;
350   case Attributes::NoUnwind:        return 1 << 5;
351   case Attributes::NoAlias:         return 1 << 6;
352   case Attributes::ByVal:           return 1 << 7;
353   case Attributes::Nest:            return 1 << 8;
354   case Attributes::ReadNone:        return 1 << 9;
355   case Attributes::ReadOnly:        return 1 << 10;
356   case Attributes::NoInline:        return 1 << 11;
357   case Attributes::AlwaysInline:    return 1 << 12;
358   case Attributes::OptimizeForSize: return 1 << 13;
359   case Attributes::StackProtect:    return 1 << 14;
360   case Attributes::StackProtectReq: return 1 << 15;
361   case Attributes::Alignment:       return 31 << 16;
362   case Attributes::NoCapture:       return 1 << 21;
363   case Attributes::NoRedZone:       return 1 << 22;
364   case Attributes::NoImplicitFloat: return 1 << 23;
365   case Attributes::Naked:           return 1 << 24;
366   case Attributes::InlineHint:      return 1 << 25;
367   case Attributes::StackAlignment:  return 7 << 26;
368   case Attributes::ReturnsTwice:    return 1 << 29;
369   case Attributes::UWTable:         return 1 << 30;
370   case Attributes::NonLazyBind:     return 1U << 31;
371   case Attributes::AddressSafety:   return 1ULL << 32;
372   }
373   llvm_unreachable("Unsupported attribute type");
374 }
375
376 bool AttributesImpl::hasAttribute(uint64_t A) const {
377   return (Bits & getAttrMask(A)) != 0;
378 }
379
380 bool AttributesImpl::hasAttributes() const {
381   return Bits != 0;
382 }
383
384 bool AttributesImpl::hasAttributes(const Attributes &A) const {
385   return Bits & A.Raw();        // FIXME: Raw() won't work here in the future.
386 }
387
388 uint64_t AttributesImpl::getAlignment() const {
389   return Bits & Attribute::Alignment_i;
390 }
391
392 uint64_t AttributesImpl::getStackAlignment() const {
393   return Bits & Attribute::StackAlignment_i;
394 }
395
396 bool AttributesImpl::isEmptyOrSingleton() const {
397   return (Bits & (Bits - 1)) == 0;
398 }
399
400 //===----------------------------------------------------------------------===//
401 // AttributeListImpl Definition
402 //===----------------------------------------------------------------------===//
403
404 namespace llvm {
405   class AttributeListImpl;
406 }
407
408 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
409
410 namespace llvm {
411 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
412
413 class AttributeListImpl : public FoldingSetNode {
414   sys::cas_flag RefCount;
415   
416   // AttributesList is uniqued, these should not be publicly available.
417   void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
418   AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
419   ~AttributeListImpl();                        // Private implementation
420 public:
421   SmallVector<AttributeWithIndex, 4> Attrs;
422   
423   AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
424     : Attrs(attrs.begin(), attrs.end()) {
425     RefCount = 0;
426   }
427   
428   void AddRef() {
429     sys::SmartScopedLock<true> Lock(*ALMutex);
430     ++RefCount;
431   }
432   void DropRef() {
433     sys::SmartScopedLock<true> Lock(*ALMutex);
434     if (!AttributesLists.isConstructed())
435       return;
436     sys::cas_flag new_val = --RefCount;
437     if (new_val == 0)
438       delete this;
439   }
440   
441   void Profile(FoldingSetNodeID &ID) const {
442     Profile(ID, Attrs);
443   }
444   static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
445     for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
446       ID.AddInteger(Attrs[i].Attrs.Raw());
447       ID.AddInteger(Attrs[i].Index);
448     }
449   }
450 };
451 }
452
453 AttributeListImpl::~AttributeListImpl() {
454   // NOTE: Lock must be acquired by caller.
455   AttributesLists->RemoveNode(this);
456 }
457
458
459 AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
460   // If there are no attributes then return a null AttributesList pointer.
461   if (Attrs.empty())
462     return AttrListPtr();
463   
464 #ifndef NDEBUG
465   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
466     assert(Attrs[i].Attrs.hasAttributes() && 
467            "Pointless attribute!");
468     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
469            "Misordered AttributesList!");
470   }
471 #endif
472   
473   // Otherwise, build a key to look up the existing attributes.
474   FoldingSetNodeID ID;
475   AttributeListImpl::Profile(ID, Attrs);
476   void *InsertPos;
477   
478   sys::SmartScopedLock<true> Lock(*ALMutex);
479   
480   AttributeListImpl *PAL =
481     AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
482   
483   // If we didn't find any existing attributes of the same shape then
484   // create a new one and insert it.
485   if (!PAL) {
486     PAL = new AttributeListImpl(Attrs);
487     AttributesLists->InsertNode(PAL, InsertPos);
488   }
489   
490   // Return the AttributesList that we found or created.
491   return AttrListPtr(PAL);
492 }
493
494
495 //===----------------------------------------------------------------------===//
496 // AttrListPtr Method Implementations
497 //===----------------------------------------------------------------------===//
498
499 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
500   if (LI) LI->AddRef();
501 }
502
503 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
504   if (AttrList) AttrList->AddRef();  
505 }
506
507 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
508   sys::SmartScopedLock<true> Lock(*ALMutex);
509   if (AttrList == RHS.AttrList) return *this;
510   if (AttrList) AttrList->DropRef();
511   AttrList = RHS.AttrList;
512   if (AttrList) AttrList->AddRef();
513   return *this;
514 }
515
516 AttrListPtr::~AttrListPtr() {
517   if (AttrList) AttrList->DropRef();
518 }
519
520 /// getNumSlots - Return the number of slots used in this attribute list. 
521 /// This is the number of arguments that have an attribute set on them
522 /// (including the function itself).
523 unsigned AttrListPtr::getNumSlots() const {
524   return AttrList ? AttrList->Attrs.size() : 0;
525 }
526
527 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
528 /// holds a number plus a set of attributes.
529 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
530   assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
531   return AttrList->Attrs[Slot];
532 }
533
534
535 /// getAttributes - The attributes for the specified index are
536 /// returned.  Attributes for the result are denoted with Idx = 0.
537 /// Function notes are denoted with idx = ~0.
538 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
539   if (AttrList == 0) return Attributes();
540   
541   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
542   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
543     if (Attrs[i].Index == Idx)
544       return Attrs[i].Attrs;
545
546   return Attributes();
547 }
548
549 /// hasAttrSomewhere - Return true if the specified attribute is set for at
550 /// least one parameter or for the return value.
551 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
552   if (AttrList == 0) return false;
553   
554   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
555   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
556     if (Attrs[i].Attrs.hasAttributes(Attr))
557       return true;
558   return false;
559 }
560
561 unsigned AttrListPtr::getNumAttrs() const {
562   return AttrList ? AttrList->Attrs.size() : 0;
563 }
564
565 Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
566   assert(AttrList && "Trying to get an attribute from an empty list!");
567   assert(i < AttrList->Attrs.size() && "Index out of range!");
568   return AttrList->Attrs[i].Attrs;
569 }
570
571 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
572   Attributes OldAttrs = getAttributes(Idx);
573 #ifndef NDEBUG
574   // FIXME it is not obvious how this should work for alignment.
575   // For now, say we can't change a known alignment.
576   unsigned OldAlign = OldAttrs.getAlignment();
577   unsigned NewAlign = Attrs.getAlignment();
578   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
579          "Attempt to change alignment!");
580 #endif
581   
582   Attributes NewAttrs = OldAttrs | Attrs;
583   if (NewAttrs == OldAttrs)
584     return *this;
585   
586   SmallVector<AttributeWithIndex, 8> NewAttrList;
587   if (AttrList == 0)
588     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
589   else {
590     const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
591     unsigned i = 0, e = OldAttrList.size();
592     // Copy attributes for arguments before this one.
593     for (; i != e && OldAttrList[i].Index < Idx; ++i)
594       NewAttrList.push_back(OldAttrList[i]);
595
596     // If there are attributes already at this index, merge them in.
597     if (i != e && OldAttrList[i].Index == Idx) {
598       Attrs |= OldAttrList[i].Attrs;
599       ++i;
600     }
601     
602     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
603     
604     // Copy attributes for arguments after this one.
605     NewAttrList.insert(NewAttrList.end(), 
606                        OldAttrList.begin()+i, OldAttrList.end());
607   }
608   
609   return get(NewAttrList);
610 }
611
612 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
613 #ifndef NDEBUG
614   // FIXME it is not obvious how this should work for alignment.
615   // For now, say we can't pass in alignment, which no current use does.
616   assert(!Attrs.hasAttribute(Attributes::Alignment) &&
617          "Attempt to exclude alignment!");
618 #endif
619   if (AttrList == 0) return AttrListPtr();
620   
621   Attributes OldAttrs = getAttributes(Idx);
622   Attributes NewAttrs = OldAttrs & ~Attrs;
623   if (NewAttrs == OldAttrs)
624     return *this;
625
626   SmallVector<AttributeWithIndex, 8> NewAttrList;
627   const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
628   unsigned i = 0, e = OldAttrList.size();
629   
630   // Copy attributes for arguments before this one.
631   for (; i != e && OldAttrList[i].Index < Idx; ++i)
632     NewAttrList.push_back(OldAttrList[i]);
633   
634   // If there are attributes already at this index, merge them in.
635   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
636   Attrs = OldAttrList[i].Attrs & ~Attrs;
637   ++i;
638   if (Attrs)  // If any attributes left for this parameter, add them.
639     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
640   
641   // Copy attributes for arguments after this one.
642   NewAttrList.insert(NewAttrList.end(), 
643                      OldAttrList.begin()+i, OldAttrList.end());
644   
645   return get(NewAttrList);
646 }
647
648 void AttrListPtr::dump() const {
649   dbgs() << "PAL[ ";
650   for (unsigned i = 0; i < getNumSlots(); ++i) {
651     const AttributeWithIndex &PAWI = getSlot(i);
652     dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
653   }
654   
655   dbgs() << "]\n";
656 }