068b504294877e529359df4386d8a3a6ffb23142
[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.getBitMask());
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.getBitMask());
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::getBitMask() const {
95   return pImpl ? pImpl->getBitMask() : 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.getBitMask() & 0xffff;
131   if (Attrs.hasAttribute(Attribute::Alignment))
132     EncodedAttrs |= Attrs.getAlignment() << 16;
133   EncodedAttrs |= (Attrs.getBitMask() & (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::NoRedZone))
198     Result += "noredzone ";
199   if (hasAttribute(Attribute::NoImplicitFloat))
200     Result += "noimplicitfloat ";
201   if (hasAttribute(Attribute::Naked))
202     Result += "naked ";
203   if (hasAttribute(Attribute::NonLazyBind))
204     Result += "nonlazybind ";
205   if (hasAttribute(Attribute::AddressSafety))
206     Result += "address_safety ";
207   if (hasAttribute(Attribute::MinSize))
208     Result += "minsize ";
209   if (hasAttribute(Attribute::StackAlignment)) {
210     Result += "alignstack(";
211     Result += utostr(getStackAlignment());
212     Result += ") ";
213   }
214   if (hasAttribute(Attribute::Alignment)) {
215     Result += "align ";
216     Result += utostr(getAlignment());
217     Result += " ";
218   }
219   if (hasAttribute(Attribute::NoDuplicate))
220     Result += "noduplicate ";
221   // Trim the trailing space.
222   assert(!Result.empty() && "Unknown attribute!");
223   Result.erase(Result.end()-1);
224   return Result;
225 }
226
227 //===----------------------------------------------------------------------===//
228 // AttrBuilder Method Implementations
229 //===----------------------------------------------------------------------===//
230
231 void AttrBuilder::clear() {
232   Attrs.clear();
233   Alignment = StackAlignment = 0;
234 }
235
236 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
237   Attrs.insert(Val);
238   return *this;
239 }
240
241 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
242   Attrs.erase(Val);
243   if (Val == Attribute::Alignment)
244     Alignment = 0;
245   else if (Val == Attribute::StackAlignment)
246     StackAlignment = 0;
247
248   return *this;
249 }
250
251 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
252   if (Align == 0) return *this;
253
254   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
255   assert(Align <= 0x40000000 && "Alignment too large.");
256
257   Attrs.insert(Attribute::Alignment);
258   Alignment = Align;
259   return *this;
260 }
261
262 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
263   // Default alignment, allow the target to define how to align it.
264   if (Align == 0) return *this;
265
266   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
267   assert(Align <= 0x100 && "Alignment too large.");
268
269   Attrs.insert(Attribute::StackAlignment);
270   StackAlignment = Align;
271   return *this;
272 }
273
274 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
275   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
276        I = Attribute::AttrKind(I + 1)) {
277     if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
278       Attrs.insert(I);
279
280       if (I == Attribute::Alignment)
281         Alignment = 1ULL << ((A >> 16) - 1);
282       else if (I == Attribute::StackAlignment)
283         StackAlignment = 1ULL << ((A >> 26)-1);
284     }
285   }
286
287   return *this;
288 }
289
290 AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
291   uint64_t Mask = A.getBitMask();
292
293   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
294        I = Attribute::AttrKind(I + 1)) {
295     if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
296       Attrs.insert(I);
297
298       if (I == Attribute::Alignment)
299         Alignment = 1ULL << ((A >> 16) - 1);
300       else if (I == Attribute::StackAlignment)
301         StackAlignment = 1ULL << ((A >> 26)-1);
302     }
303   }
304
305   return *this;
306 }
307
308 AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
309   uint64_t Mask = A.getBitMask();
310
311   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
312        I = Attribute::AttrKind(I + 1)) {
313     if (Mask & AttributeImpl::getAttrMask(I)) {
314       Attrs.erase(I);
315
316       if (I == Attribute::Alignment)
317         Alignment = 0;
318       else if (I == Attribute::StackAlignment)
319         StackAlignment = 0;
320     }
321   }
322
323   return *this;
324 }
325
326 bool AttrBuilder::contains(Attribute::AttrKind A) const {
327   return Attrs.count(A);
328 }
329
330 bool AttrBuilder::hasAttributes() const {
331   return !Attrs.empty();
332 }
333
334 bool AttrBuilder::hasAttributes(const Attribute &A) const {
335   return getBitMask() & A.getBitMask();
336 }
337
338 bool AttrBuilder::hasAlignmentAttr() const {
339   return Alignment != 0;
340 }
341
342 uint64_t AttrBuilder::getBitMask() const {
343   uint64_t Mask = 0;
344
345   for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
346          E = Attrs.end(); I != E; ++I) {
347     Attribute::AttrKind Kind = *I;
348
349     if (Kind == Attribute::Alignment)
350       Mask |= (Log2_32(Alignment) + 1) << 16;
351     else if (Kind == Attribute::StackAlignment)
352       Mask |= (Log2_32(StackAlignment) + 1) << 26;
353     else
354       Mask |= AttributeImpl::getAttrMask(Kind);
355   }
356
357   return Mask;
358 }
359
360 bool AttrBuilder::operator==(const AttrBuilder &B) {
361   SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
362   SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
363   return This == That;
364 }
365
366 //===----------------------------------------------------------------------===//
367 // AttributeImpl Definition
368 //===----------------------------------------------------------------------===//
369
370 AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data) {
371   Data = ConstantInt::get(Type::getInt64Ty(C), data);
372 }
373 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data) {
374   Data = ConstantInt::get(Type::getInt64Ty(C), data);
375 }
376 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
377                              ArrayRef<Constant*> values) {
378   Data = ConstantInt::get(Type::getInt64Ty(C), data);
379   Vals.reserve(values.size());
380   Vals.append(values.begin(), values.end());
381 }
382 AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data) {
383   Data = ConstantDataArray::getString(C, data);
384 }
385
386 bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
387   if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
388     return CI->getZExtValue() == Kind;
389   return false;
390 }
391 bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
392   return !(*this == Kind);
393 }
394
395 bool AttributeImpl::operator==(StringRef Kind) const {
396   if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
397     if (CDA->isString())
398       return CDA->getAsString() == Kind;
399   return false;
400 }
401 bool AttributeImpl::operator!=(StringRef Kind) const {
402   return !(*this == Kind);
403 }
404
405 uint64_t AttributeImpl::getBitMask() const {
406   // FIXME: Remove this.
407   return cast<ConstantInt>(Data)->getZExtValue();
408 }
409
410 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
411   switch (Val) {
412   case Attribute::EndAttrKinds:    break;
413   case Attribute::None:            return 0;
414   case Attribute::ZExt:            return 1 << 0;
415   case Attribute::SExt:            return 1 << 1;
416   case Attribute::NoReturn:        return 1 << 2;
417   case Attribute::InReg:           return 1 << 3;
418   case Attribute::StructRet:       return 1 << 4;
419   case Attribute::NoUnwind:        return 1 << 5;
420   case Attribute::NoAlias:         return 1 << 6;
421   case Attribute::ByVal:           return 1 << 7;
422   case Attribute::Nest:            return 1 << 8;
423   case Attribute::ReadNone:        return 1 << 9;
424   case Attribute::ReadOnly:        return 1 << 10;
425   case Attribute::NoInline:        return 1 << 11;
426   case Attribute::AlwaysInline:    return 1 << 12;
427   case Attribute::OptimizeForSize: return 1 << 13;
428   case Attribute::StackProtect:    return 1 << 14;
429   case Attribute::StackProtectReq: return 1 << 15;
430   case Attribute::Alignment:       return 31 << 16;
431   case Attribute::NoCapture:       return 1 << 21;
432   case Attribute::NoRedZone:       return 1 << 22;
433   case Attribute::NoImplicitFloat: return 1 << 23;
434   case Attribute::Naked:           return 1 << 24;
435   case Attribute::InlineHint:      return 1 << 25;
436   case Attribute::StackAlignment:  return 7 << 26;
437   case Attribute::ReturnsTwice:    return 1 << 29;
438   case Attribute::UWTable:         return 1 << 30;
439   case Attribute::NonLazyBind:     return 1U << 31;
440   case Attribute::AddressSafety:   return 1ULL << 32;
441   case Attribute::MinSize:         return 1ULL << 33;
442   case Attribute::NoDuplicate:     return 1ULL << 34;
443   }
444   llvm_unreachable("Unsupported attribute type");
445 }
446
447 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
448   return (getBitMask() & getAttrMask(A)) != 0;
449 }
450
451 bool AttributeImpl::hasAttributes() const {
452   return getBitMask() != 0;
453 }
454
455 uint64_t AttributeImpl::getAlignment() const {
456   return getBitMask() & getAttrMask(Attribute::Alignment);
457 }
458
459 uint64_t AttributeImpl::getStackAlignment() const {
460   return getBitMask() & getAttrMask(Attribute::StackAlignment);
461 }
462
463 //===----------------------------------------------------------------------===//
464 // AttributeSetImpl Definition
465 //===----------------------------------------------------------------------===//
466
467 AttributeSet AttributeSet::get(LLVMContext &C,
468                                ArrayRef<AttributeWithIndex> Attrs) {
469   // If there are no attributes then return a null AttributesList pointer.
470   if (Attrs.empty())
471     return AttributeSet();
472
473 #ifndef NDEBUG
474   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
475     assert(Attrs[i].Attrs.hasAttributes() &&
476            "Pointless attribute!");
477     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
478            "Misordered AttributesList!");
479   }
480 #endif
481
482   // Otherwise, build a key to look up the existing attributes.
483   LLVMContextImpl *pImpl = C.pImpl;
484   FoldingSetNodeID ID;
485   AttributeSetImpl::Profile(ID, Attrs);
486
487   void *InsertPoint;
488   AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
489                                                                 InsertPoint);
490
491   // If we didn't find any existing attributes of the same shape then
492   // create a new one and insert it.
493   if (!PA) {
494     PA = new AttributeSetImpl(C, Attrs);
495     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
496   }
497
498   // Return the AttributesList that we found or created.
499   return AttributeSet(PA);
500 }
501
502 //===----------------------------------------------------------------------===//
503 // AttributeSet Method Implementations
504 //===----------------------------------------------------------------------===//
505
506 const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
507   AttrList = RHS.AttrList;
508   return *this;
509 }
510
511 /// getNumSlots - Return the number of slots used in this attribute list.
512 /// This is the number of arguments that have an attribute set on them
513 /// (including the function itself).
514 unsigned AttributeSet::getNumSlots() const {
515   return AttrList ? AttrList->getNumAttributes() : 0;
516 }
517
518 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
519 /// holds a number plus a set of attributes.
520 const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
521   assert(AttrList && Slot < AttrList->getNumAttributes() &&
522          "Slot # out of range!");
523   return AttrList->getAttributes()[Slot];
524 }
525
526 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
527   return getAttributes(Index).hasAttribute(Kind);
528 }
529
530 bool AttributeSet::hasAttributes(unsigned Index) const {
531   return getAttributes(Index).hasAttributes();
532 }
533
534 std::string AttributeSet::getAsString(unsigned Index) const {
535   return getAttributes(Index).getAsString();
536 }
537
538 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
539   return getAttributes(Index).getStackAlignment();
540 }
541
542 uint64_t AttributeSet::getBitMask(unsigned Index) const {
543   // FIXME: Remove this.
544   return getAttributes(Index).getBitMask();
545 }
546
547 /// getAttributes - The attributes for the specified index are returned.
548 /// Attributes for the result are denoted with Idx = 0.  Function attributes are
549 /// denoted with Idx = ~0.
550 Attribute AttributeSet::getAttributes(unsigned Idx) const {
551   if (AttrList == 0) return Attribute();
552
553   ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
554   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
555     if (Attrs[i].Index == Idx)
556       return Attrs[i].Attrs;
557
558   return Attribute();
559 }
560
561 /// hasAttrSomewhere - Return true if the specified attribute is set for at
562 /// least one parameter or for the return value.
563 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
564   if (AttrList == 0) return false;
565
566   ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
567   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
568     if (Attrs[i].Attrs.hasAttribute(Attr))
569       return true;
570
571   return false;
572 }
573
574 AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
575                                    Attribute Attrs) const {
576   Attribute OldAttrs = getAttributes(Idx);
577 #ifndef NDEBUG
578   // FIXME it is not obvious how this should work for alignment.
579   // For now, say we can't change a known alignment.
580   unsigned OldAlign = OldAttrs.getAlignment();
581   unsigned NewAlign = Attrs.getAlignment();
582   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
583          "Attempt to change alignment!");
584 #endif
585
586   AttrBuilder NewAttrs =
587     AttrBuilder(OldAttrs).addAttributes(Attrs);
588   if (NewAttrs == AttrBuilder(OldAttrs))
589     return *this;
590
591   SmallVector<AttributeWithIndex, 8> NewAttrList;
592   if (AttrList == 0)
593     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
594   else {
595     ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
596     unsigned i = 0, e = OldAttrList.size();
597     // Copy attributes for arguments before this one.
598     for (; i != e && OldAttrList[i].Index < Idx; ++i)
599       NewAttrList.push_back(OldAttrList[i]);
600
601     // If there are attributes already at this index, merge them in.
602     if (i != e && OldAttrList[i].Index == Idx) {
603       Attrs =
604         Attribute::get(C, AttrBuilder(Attrs).
605                         addAttributes(OldAttrList[i].Attrs));
606       ++i;
607     }
608
609     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
610
611     // Copy attributes for arguments after this one.
612     NewAttrList.insert(NewAttrList.end(),
613                        OldAttrList.begin()+i, OldAttrList.end());
614   }
615
616   return get(C, NewAttrList);
617 }
618
619 AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
620                                       Attribute Attrs) const {
621 #ifndef NDEBUG
622   // FIXME it is not obvious how this should work for alignment.
623   // For now, say we can't pass in alignment, which no current use does.
624   assert(!Attrs.hasAttribute(Attribute::Alignment) &&
625          "Attempt to exclude alignment!");
626 #endif
627   if (AttrList == 0) return AttributeSet();
628
629   Attribute OldAttrs = getAttributes(Idx);
630   AttrBuilder NewAttrs =
631     AttrBuilder(OldAttrs).removeAttributes(Attrs);
632   if (NewAttrs == AttrBuilder(OldAttrs))
633     return *this;
634
635   SmallVector<AttributeWithIndex, 8> NewAttrList;
636   ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
637   unsigned i = 0, e = OldAttrList.size();
638
639   // Copy attributes for arguments before this one.
640   for (; i != e && OldAttrList[i].Index < Idx; ++i)
641     NewAttrList.push_back(OldAttrList[i]);
642
643   // If there are attributes already at this index, merge them in.
644   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
645   Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
646                           removeAttributes(Attrs));
647   ++i;
648   if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
649     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
650
651   // Copy attributes for arguments after this one.
652   NewAttrList.insert(NewAttrList.end(),
653                      OldAttrList.begin()+i, OldAttrList.end());
654
655   return get(C, NewAttrList);
656 }
657
658 void AttributeSet::dump() const {
659   dbgs() << "PAL[ ";
660   for (unsigned i = 0; i < getNumSlots(); ++i) {
661     const AttributeWithIndex &PAWI = getSlot(i);
662     dbgs() << "{ " << PAWI.Index << ", " << PAWI.Attrs.getAsString() << " } ";
663   }
664
665   dbgs() << "]\n";
666 }