Don't treat 0 as a special value for int attributes.
[oota-llvm.git] / lib / IR / 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 // \file
11 // \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
12 // AttributeSetImpl, and AttributeSet classes.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/IR/Attributes.h"
17 #include "AttributeImpl.h"
18 #include "LLVMContextImpl.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Atomic.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Mutex.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <algorithm>
28 using namespace llvm;
29
30 //===----------------------------------------------------------------------===//
31 // Attribute Construction Methods
32 //===----------------------------------------------------------------------===//
33
34 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind) {
35   LLVMContextImpl *pImpl = Context.pImpl;
36   FoldingSetNodeID ID;
37   ID.AddInteger(Kind);
38
39   void *InsertPoint;
40   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
41
42   if (!PA) {
43     // If we didn't find any existing attributes of the same shape then create a
44     // new one and insert it.
45     PA = new EnumAttributeImpl(Kind);
46     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
47   }
48
49   // Return the Attribute that we found or created.
50   return Attribute(PA);
51 }
52
53 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
54                          uint64_t Val) {
55   LLVMContextImpl *pImpl = Context.pImpl;
56   FoldingSetNodeID ID;
57   ID.AddInteger(Kind);
58   ID.AddInteger(Val);
59
60   void *InsertPoint;
61   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
62
63   if (!PA) {
64     // If we didn't find any existing attributes of the same shape then create a
65     // new one and insert it.
66     PA = new IntAttributeImpl(Kind, Val);
67     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
68   }
69
70   // Return the Attribute that we found or created.
71   return Attribute(PA);
72 }
73
74 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
75   LLVMContextImpl *pImpl = Context.pImpl;
76   FoldingSetNodeID ID;
77   ID.AddString(Kind);
78   if (!Val.empty()) ID.AddString(Val);
79
80   void *InsertPoint;
81   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
82
83   if (!PA) {
84     // If we didn't find any existing attributes of the same shape then create a
85     // new one and insert it.
86     PA = new StringAttributeImpl(Kind, Val);
87     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
88   }
89
90   // Return the Attribute that we found or created.
91   return Attribute(PA);
92 }
93
94 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
95   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
96   assert(Align <= 0x40000000 && "Alignment too large.");
97   return get(Context, Alignment, Align);
98 }
99
100 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
101                                            uint64_t Align) {
102   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
103   assert(Align <= 0x100 && "Alignment too large.");
104   return get(Context, StackAlignment, Align);
105 }
106
107 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
108                                                 uint64_t Bytes) {
109   assert(Bytes && "Bytes must be non-zero.");
110   return get(Context, Dereferenceable, Bytes);
111 }
112
113 //===----------------------------------------------------------------------===//
114 // Attribute Accessor Methods
115 //===----------------------------------------------------------------------===//
116
117 bool Attribute::isEnumAttribute() const {
118   return pImpl && pImpl->isEnumAttribute();
119 }
120
121 bool Attribute::isIntAttribute() const {
122   return pImpl && pImpl->isIntAttribute();
123 }
124
125 bool Attribute::isStringAttribute() const {
126   return pImpl && pImpl->isStringAttribute();
127 }
128
129 Attribute::AttrKind Attribute::getKindAsEnum() const {
130   if (!pImpl) return None;
131   assert((isEnumAttribute() || isIntAttribute()) &&
132          "Invalid attribute type to get the kind as an enum!");
133   return pImpl ? pImpl->getKindAsEnum() : None;
134 }
135
136 uint64_t Attribute::getValueAsInt() const {
137   if (!pImpl) return 0;
138   assert(isIntAttribute() &&
139          "Expected the attribute to be an integer attribute!");
140   return pImpl ? pImpl->getValueAsInt() : 0;
141 }
142
143 StringRef Attribute::getKindAsString() const {
144   if (!pImpl) return StringRef();
145   assert(isStringAttribute() &&
146          "Invalid attribute type to get the kind as a string!");
147   return pImpl ? pImpl->getKindAsString() : StringRef();
148 }
149
150 StringRef Attribute::getValueAsString() const {
151   if (!pImpl) return StringRef();
152   assert(isStringAttribute() &&
153          "Invalid attribute type to get the value as a string!");
154   return pImpl ? pImpl->getValueAsString() : StringRef();
155 }
156
157 bool Attribute::hasAttribute(AttrKind Kind) const {
158   return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
159 }
160
161 bool Attribute::hasAttribute(StringRef Kind) const {
162   if (!isStringAttribute()) return false;
163   return pImpl && pImpl->hasAttribute(Kind);
164 }
165
166 /// This returns the alignment field of an attribute as a byte alignment value.
167 unsigned Attribute::getAlignment() const {
168   assert(hasAttribute(Attribute::Alignment) &&
169          "Trying to get alignment from non-alignment attribute!");
170   return pImpl->getValueAsInt();
171 }
172
173 /// This returns the stack alignment field of an attribute as a byte alignment
174 /// value.
175 unsigned Attribute::getStackAlignment() const {
176   assert(hasAttribute(Attribute::StackAlignment) &&
177          "Trying to get alignment from non-alignment attribute!");
178   return pImpl->getValueAsInt();
179 }
180
181 /// This returns the number of dereferenceable bytes.
182 uint64_t Attribute::getDereferenceableBytes() const {
183   assert(hasAttribute(Attribute::Dereferenceable) &&
184          "Trying to get dereferenceable bytes from "
185          "non-dereferenceable attribute!");
186   return pImpl->getValueAsInt();
187 }
188
189 std::string Attribute::getAsString(bool InAttrGrp) const {
190   if (!pImpl) return "";
191
192   if (hasAttribute(Attribute::SanitizeAddress))
193     return "sanitize_address";
194   if (hasAttribute(Attribute::AlwaysInline))
195     return "alwaysinline";
196   if (hasAttribute(Attribute::Builtin))
197     return "builtin";
198   if (hasAttribute(Attribute::ByVal))
199     return "byval";
200   if (hasAttribute(Attribute::InAlloca))
201     return "inalloca";
202   if (hasAttribute(Attribute::InlineHint))
203     return "inlinehint";
204   if (hasAttribute(Attribute::InReg))
205     return "inreg";
206   if (hasAttribute(Attribute::JumpTable))
207     return "jumptable";
208   if (hasAttribute(Attribute::MinSize))
209     return "minsize";
210   if (hasAttribute(Attribute::Naked))
211     return "naked";
212   if (hasAttribute(Attribute::Nest))
213     return "nest";
214   if (hasAttribute(Attribute::NoAlias))
215     return "noalias";
216   if (hasAttribute(Attribute::NoBuiltin))
217     return "nobuiltin";
218   if (hasAttribute(Attribute::NoCapture))
219     return "nocapture";
220   if (hasAttribute(Attribute::NoDuplicate))
221     return "noduplicate";
222   if (hasAttribute(Attribute::NoImplicitFloat))
223     return "noimplicitfloat";
224   if (hasAttribute(Attribute::NoInline))
225     return "noinline";
226   if (hasAttribute(Attribute::NonLazyBind))
227     return "nonlazybind";
228   if (hasAttribute(Attribute::NonNull))
229     return "nonnull";
230   if (hasAttribute(Attribute::NoRedZone))
231     return "noredzone";
232   if (hasAttribute(Attribute::NoReturn))
233     return "noreturn";
234   if (hasAttribute(Attribute::NoUnwind))
235     return "nounwind";
236   if (hasAttribute(Attribute::OptimizeNone))
237     return "optnone";
238   if (hasAttribute(Attribute::OptimizeForSize))
239     return "optsize";
240   if (hasAttribute(Attribute::ReadNone))
241     return "readnone";
242   if (hasAttribute(Attribute::ReadOnly))
243     return "readonly";
244   if (hasAttribute(Attribute::Returned))
245     return "returned";
246   if (hasAttribute(Attribute::ReturnsTwice))
247     return "returns_twice";
248   if (hasAttribute(Attribute::SExt))
249     return "signext";
250   if (hasAttribute(Attribute::StackProtect))
251     return "ssp";
252   if (hasAttribute(Attribute::StackProtectReq))
253     return "sspreq";
254   if (hasAttribute(Attribute::StackProtectStrong))
255     return "sspstrong";
256   if (hasAttribute(Attribute::StructRet))
257     return "sret";
258   if (hasAttribute(Attribute::SanitizeThread))
259     return "sanitize_thread";
260   if (hasAttribute(Attribute::SanitizeMemory))
261     return "sanitize_memory";
262   if (hasAttribute(Attribute::UWTable))
263     return "uwtable";
264   if (hasAttribute(Attribute::ZExt))
265     return "zeroext";
266   if (hasAttribute(Attribute::Cold))
267     return "cold";
268
269   // FIXME: These should be output like this:
270   //
271   //   align=4
272   //   alignstack=8
273   //
274   if (hasAttribute(Attribute::Alignment)) {
275     std::string Result;
276     Result += "align";
277     Result += (InAttrGrp) ? "=" : " ";
278     Result += utostr(getValueAsInt());
279     return Result;
280   }
281
282   if (hasAttribute(Attribute::StackAlignment)) {
283     std::string Result;
284     Result += "alignstack";
285     if (InAttrGrp) {
286       Result += "=";
287       Result += utostr(getValueAsInt());
288     } else {
289       Result += "(";
290       Result += utostr(getValueAsInt());
291       Result += ")";
292     }
293     return Result;
294   }
295
296   if (hasAttribute(Attribute::Dereferenceable)) {
297     std::string Result;
298     Result += "dereferenceable";
299     if (InAttrGrp) {
300       Result += "=";
301       Result += utostr(getValueAsInt());
302     } else {
303       Result += "(";
304       Result += utostr(getValueAsInt());
305       Result += ")";
306     }
307     return Result;
308   }
309
310   // Convert target-dependent attributes to strings of the form:
311   //
312   //   "kind"
313   //   "kind" = "value"
314   //
315   if (isStringAttribute()) {
316     std::string Result;
317     Result += '\"' + getKindAsString().str() + '"';
318
319     StringRef Val = pImpl->getValueAsString();
320     if (Val.empty()) return Result;
321
322     Result += "=\"" + Val.str() + '"';
323     return Result;
324   }
325
326   llvm_unreachable("Unknown attribute");
327 }
328
329 bool Attribute::operator<(Attribute A) const {
330   if (!pImpl && !A.pImpl) return false;
331   if (!pImpl) return true;
332   if (!A.pImpl) return false;
333   return *pImpl < *A.pImpl;
334 }
335
336 //===----------------------------------------------------------------------===//
337 // AttributeImpl Definition
338 //===----------------------------------------------------------------------===//
339
340 // Pin the vtables to this file.
341 AttributeImpl::~AttributeImpl() {}
342 void EnumAttributeImpl::anchor() {}
343 void IntAttributeImpl::anchor() {}
344 void StringAttributeImpl::anchor() {}
345
346 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
347   if (isStringAttribute()) return false;
348   return getKindAsEnum() == A;
349 }
350
351 bool AttributeImpl::hasAttribute(StringRef Kind) const {
352   if (!isStringAttribute()) return false;
353   return getKindAsString() == Kind;
354 }
355
356 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
357   assert(isEnumAttribute() || isIntAttribute());
358   return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
359 }
360
361 uint64_t AttributeImpl::getValueAsInt() const {
362   assert(isIntAttribute());
363   return static_cast<const IntAttributeImpl *>(this)->getValue();
364 }
365
366 StringRef AttributeImpl::getKindAsString() const {
367   assert(isStringAttribute());
368   return static_cast<const StringAttributeImpl *>(this)->getStringKind();
369 }
370
371 StringRef AttributeImpl::getValueAsString() const {
372   assert(isStringAttribute());
373   return static_cast<const StringAttributeImpl *>(this)->getStringValue();
374 }
375
376 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
377   // This sorts the attributes with Attribute::AttrKinds coming first (sorted
378   // relative to their enum value) and then strings.
379   if (isEnumAttribute()) {
380     if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
381     if (AI.isIntAttribute()) return true;
382     if (AI.isStringAttribute()) return true;
383   }
384
385   if (isIntAttribute()) {
386     if (AI.isEnumAttribute()) return false;
387     if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
388     if (AI.isStringAttribute()) return true;
389   }
390
391   if (AI.isEnumAttribute()) return false;
392   if (AI.isIntAttribute()) return false;
393   if (getKindAsString() == AI.getKindAsString())
394     return getValueAsString() < AI.getValueAsString();
395   return getKindAsString() < AI.getKindAsString();
396 }
397
398 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
399   // FIXME: Remove this.
400   switch (Val) {
401   case Attribute::EndAttrKinds:
402     llvm_unreachable("Synthetic enumerators which should never get here");
403
404   case Attribute::None:            return 0;
405   case Attribute::ZExt:            return 1 << 0;
406   case Attribute::SExt:            return 1 << 1;
407   case Attribute::NoReturn:        return 1 << 2;
408   case Attribute::InReg:           return 1 << 3;
409   case Attribute::StructRet:       return 1 << 4;
410   case Attribute::NoUnwind:        return 1 << 5;
411   case Attribute::NoAlias:         return 1 << 6;
412   case Attribute::ByVal:           return 1 << 7;
413   case Attribute::Nest:            return 1 << 8;
414   case Attribute::ReadNone:        return 1 << 9;
415   case Attribute::ReadOnly:        return 1 << 10;
416   case Attribute::NoInline:        return 1 << 11;
417   case Attribute::AlwaysInline:    return 1 << 12;
418   case Attribute::OptimizeForSize: return 1 << 13;
419   case Attribute::StackProtect:    return 1 << 14;
420   case Attribute::StackProtectReq: return 1 << 15;
421   case Attribute::Alignment:       return 31 << 16;
422   case Attribute::NoCapture:       return 1 << 21;
423   case Attribute::NoRedZone:       return 1 << 22;
424   case Attribute::NoImplicitFloat: return 1 << 23;
425   case Attribute::Naked:           return 1 << 24;
426   case Attribute::InlineHint:      return 1 << 25;
427   case Attribute::StackAlignment:  return 7 << 26;
428   case Attribute::ReturnsTwice:    return 1 << 29;
429   case Attribute::UWTable:         return 1 << 30;
430   case Attribute::NonLazyBind:     return 1U << 31;
431   case Attribute::SanitizeAddress: return 1ULL << 32;
432   case Attribute::MinSize:         return 1ULL << 33;
433   case Attribute::NoDuplicate:     return 1ULL << 34;
434   case Attribute::StackProtectStrong: return 1ULL << 35;
435   case Attribute::SanitizeThread:  return 1ULL << 36;
436   case Attribute::SanitizeMemory:  return 1ULL << 37;
437   case Attribute::NoBuiltin:       return 1ULL << 38;
438   case Attribute::Returned:        return 1ULL << 39;
439   case Attribute::Cold:            return 1ULL << 40;
440   case Attribute::Builtin:         return 1ULL << 41;
441   case Attribute::OptimizeNone:    return 1ULL << 42;
442   case Attribute::InAlloca:        return 1ULL << 43;
443   case Attribute::NonNull:         return 1ULL << 44;
444   case Attribute::JumpTable:       return 1ULL << 45;
445   case Attribute::Dereferenceable:
446     llvm_unreachable("dereferenceable attribute not supported in raw format");
447   }
448   llvm_unreachable("Unsupported attribute type");
449 }
450
451 //===----------------------------------------------------------------------===//
452 // AttributeSetNode Definition
453 //===----------------------------------------------------------------------===//
454
455 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
456                                         ArrayRef<Attribute> Attrs) {
457   if (Attrs.empty())
458     return nullptr;
459
460   // Otherwise, build a key to look up the existing attributes.
461   LLVMContextImpl *pImpl = C.pImpl;
462   FoldingSetNodeID ID;
463
464   SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
465   array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
466
467   for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
468          E = SortedAttrs.end(); I != E; ++I)
469     I->Profile(ID);
470
471   void *InsertPoint;
472   AttributeSetNode *PA =
473     pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
474
475   // If we didn't find any existing attributes of the same shape then create a
476   // new one and insert it.
477   if (!PA) {
478     // Coallocate entries after the AttributeSetNode itself.
479     void *Mem = ::operator new(sizeof(AttributeSetNode) +
480                                sizeof(Attribute) * SortedAttrs.size());
481     PA = new (Mem) AttributeSetNode(SortedAttrs);
482     pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
483   }
484
485   // Return the AttributesListNode that we found or created.
486   return PA;
487 }
488
489 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
490   for (iterator I = begin(), E = end(); I != E; ++I)
491     if (I->hasAttribute(Kind))
492       return true;
493   return false;
494 }
495
496 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
497   for (iterator I = begin(), E = end(); I != E; ++I)
498     if (I->hasAttribute(Kind))
499       return true;
500   return false;
501 }
502
503 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
504   for (iterator I = begin(), E = end(); I != E; ++I)
505     if (I->hasAttribute(Kind))
506       return *I;
507   return Attribute();
508 }
509
510 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
511   for (iterator I = begin(), E = end(); I != E; ++I)
512     if (I->hasAttribute(Kind))
513       return *I;
514   return Attribute();
515 }
516
517 unsigned AttributeSetNode::getAlignment() const {
518   for (iterator I = begin(), E = end(); I != E; ++I)
519     if (I->hasAttribute(Attribute::Alignment))
520       return I->getAlignment();
521   return 0;
522 }
523
524 unsigned AttributeSetNode::getStackAlignment() const {
525   for (iterator I = begin(), E = end(); I != E; ++I)
526     if (I->hasAttribute(Attribute::StackAlignment))
527       return I->getStackAlignment();
528   return 0;
529 }
530
531 uint64_t AttributeSetNode::getDereferenceableBytes() const {
532   for (iterator I = begin(), E = end(); I != E; ++I)
533     if (I->hasAttribute(Attribute::Dereferenceable))
534       return I->getDereferenceableBytes();
535   return 0;
536 }
537
538 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
539   std::string Str;
540   for (iterator I = begin(), E = end(); I != E; ++I) {
541     if (I != begin())
542       Str += ' ';
543     Str += I->getAsString(InAttrGrp);
544   }
545   return Str;
546 }
547
548 //===----------------------------------------------------------------------===//
549 // AttributeSetImpl Definition
550 //===----------------------------------------------------------------------===//
551
552 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
553   for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
554     if (getSlotIndex(I) != Index) continue;
555     const AttributeSetNode *ASN = getSlotNode(I);
556     uint64_t Mask = 0;
557
558     for (AttributeSetNode::iterator II = ASN->begin(),
559            IE = ASN->end(); II != IE; ++II) {
560       Attribute Attr = *II;
561
562       // This cannot handle string attributes.
563       if (Attr.isStringAttribute()) continue;
564
565       Attribute::AttrKind Kind = Attr.getKindAsEnum();
566
567       if (Kind == Attribute::Alignment)
568         Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
569       else if (Kind == Attribute::StackAlignment)
570         Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
571       else if (Kind == Attribute::Dereferenceable)
572         llvm_unreachable("dereferenceable not supported in bit mask");
573       else
574         Mask |= AttributeImpl::getAttrMask(Kind);
575     }
576
577     return Mask;
578   }
579
580   return 0;
581 }
582
583 void AttributeSetImpl::dump() const {
584   AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
585 }
586
587 //===----------------------------------------------------------------------===//
588 // AttributeSet Construction and Mutation Methods
589 //===----------------------------------------------------------------------===//
590
591 AttributeSet
592 AttributeSet::getImpl(LLVMContext &C,
593                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
594   LLVMContextImpl *pImpl = C.pImpl;
595   FoldingSetNodeID ID;
596   AttributeSetImpl::Profile(ID, Attrs);
597
598   void *InsertPoint;
599   AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
600
601   // If we didn't find any existing attributes of the same shape then
602   // create a new one and insert it.
603   if (!PA) {
604     // Coallocate entries after the AttributeSetImpl itself.
605     void *Mem = ::operator new(sizeof(AttributeSetImpl) +
606                                sizeof(std::pair<unsigned, AttributeSetNode *>) *
607                                    Attrs.size());
608     PA = new (Mem) AttributeSetImpl(C, Attrs);
609     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
610   }
611
612   // Return the AttributesList that we found or created.
613   return AttributeSet(PA);
614 }
615
616 AttributeSet AttributeSet::get(LLVMContext &C,
617                                ArrayRef<std::pair<unsigned, Attribute> > Attrs){
618   // If there are no attributes then return a null AttributesList pointer.
619   if (Attrs.empty())
620     return AttributeSet();
621
622 #ifndef NDEBUG
623   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
624     assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
625            "Misordered Attributes list!");
626     assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
627            "Pointless attribute!");
628   }
629 #endif
630
631   // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
632   // list.
633   SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
634   for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
635          E = Attrs.end(); I != E; ) {
636     unsigned Index = I->first;
637     SmallVector<Attribute, 4> AttrVec;
638     while (I != E && I->first == Index) {
639       AttrVec.push_back(I->second);
640       ++I;
641     }
642
643     AttrPairVec.push_back(std::make_pair(Index,
644                                          AttributeSetNode::get(C, AttrVec)));
645   }
646
647   return getImpl(C, AttrPairVec);
648 }
649
650 AttributeSet AttributeSet::get(LLVMContext &C,
651                                ArrayRef<std::pair<unsigned,
652                                                   AttributeSetNode*> > Attrs) {
653   // If there are no attributes then return a null AttributesList pointer.
654   if (Attrs.empty())
655     return AttributeSet();
656
657   return getImpl(C, Attrs);
658 }
659
660 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
661                                const AttrBuilder &B) {
662   if (!B.hasAttributes())
663     return AttributeSet();
664
665   // Add target-independent attributes.
666   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
667   for (Attribute::AttrKind Kind = Attribute::None;
668        Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
669     if (!B.contains(Kind))
670       continue;
671
672     if (Kind == Attribute::Alignment)
673       Attrs.push_back(std::make_pair(Index, Attribute::
674                                      getWithAlignment(C, B.getAlignment())));
675     else if (Kind == Attribute::StackAlignment)
676       Attrs.push_back(std::make_pair(Index, Attribute::
677                               getWithStackAlignment(C, B.getStackAlignment())));
678     else if (Kind == Attribute::Dereferenceable)
679       Attrs.push_back(std::make_pair(Index,
680                                      Attribute::getWithDereferenceableBytes(C,
681                                        B.getDereferenceableBytes())));
682     else
683       Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
684   }
685
686   // Add target-dependent (string) attributes.
687   for (const AttrBuilder::td_type &TDA : B.td_attrs())
688     Attrs.push_back(
689         std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
690
691   return get(C, Attrs);
692 }
693
694 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
695                                ArrayRef<Attribute::AttrKind> Kind) {
696   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
697   for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
698          E = Kind.end(); I != E; ++I)
699     Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
700   return get(C, Attrs);
701 }
702
703 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
704   if (Attrs.empty()) return AttributeSet();
705   if (Attrs.size() == 1) return Attrs[0];
706
707   SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
708   AttributeSetImpl *A0 = Attrs[0].pImpl;
709   if (A0)
710     AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
711   // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
712   // ordered by index.  Because we know that each list in Attrs is ordered by
713   // index we only need to merge each successive list in rather than doing a
714   // full sort.
715   for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
716     AttributeSetImpl *AS = Attrs[I].pImpl;
717     if (!AS) continue;
718     SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
719       ANVI = AttrNodeVec.begin(), ANVE;
720     for (const AttributeSetImpl::IndexAttrPair
721              *AI = AS->getNode(0),
722              *AE = AS->getNode(AS->getNumAttributes());
723          AI != AE; ++AI) {
724       ANVE = AttrNodeVec.end();
725       while (ANVI != ANVE && ANVI->first <= AI->first)
726         ++ANVI;
727       ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
728     }
729   }
730
731   return getImpl(C, AttrNodeVec);
732 }
733
734 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
735                                         Attribute::AttrKind Attr) const {
736   if (hasAttribute(Index, Attr)) return *this;
737   return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
738 }
739
740 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
741                                         StringRef Kind) const {
742   llvm::AttrBuilder B;
743   B.addAttribute(Kind);
744   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
745 }
746
747 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
748                                         StringRef Kind, StringRef Value) const {
749   llvm::AttrBuilder B;
750   B.addAttribute(Kind, Value);
751   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
752 }
753
754 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
755                                          AttributeSet Attrs) const {
756   if (!pImpl) return Attrs;
757   if (!Attrs.pImpl) return *this;
758
759 #ifndef NDEBUG
760   // FIXME it is not obvious how this should work for alignment. For now, say
761   // we can't change a known alignment.
762   unsigned OldAlign = getParamAlignment(Index);
763   unsigned NewAlign = Attrs.getParamAlignment(Index);
764   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
765          "Attempt to change alignment!");
766 #endif
767
768   // Add the attribute slots before the one we're trying to add.
769   SmallVector<AttributeSet, 4> AttrSet;
770   uint64_t NumAttrs = pImpl->getNumAttributes();
771   AttributeSet AS;
772   uint64_t LastIndex = 0;
773   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
774     if (getSlotIndex(I) >= Index) {
775       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
776       break;
777     }
778     LastIndex = I + 1;
779     AttrSet.push_back(getSlotAttributes(I));
780   }
781
782   // Now add the attribute into the correct slot. There may already be an
783   // AttributeSet there.
784   AttrBuilder B(AS, Index);
785
786   for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
787     if (Attrs.getSlotIndex(I) == Index) {
788       for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
789              IE = Attrs.pImpl->end(I); II != IE; ++II)
790         B.addAttribute(*II);
791       break;
792     }
793
794   AttrSet.push_back(AttributeSet::get(C, Index, B));
795
796   // Add the remaining attribute slots.
797   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
798     AttrSet.push_back(getSlotAttributes(I));
799
800   return get(C, AttrSet);
801 }
802
803 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
804                                            Attribute::AttrKind Attr) const {
805   if (!hasAttribute(Index, Attr)) return *this;
806   return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
807 }
808
809 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
810                                             AttributeSet Attrs) const {
811   if (!pImpl) return AttributeSet();
812   if (!Attrs.pImpl) return *this;
813
814 #ifndef NDEBUG
815   // FIXME it is not obvious how this should work for alignment.
816   // For now, say we can't pass in alignment, which no current use does.
817   assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
818          "Attempt to change alignment!");
819 #endif
820
821   // Add the attribute slots before the one we're trying to add.
822   SmallVector<AttributeSet, 4> AttrSet;
823   uint64_t NumAttrs = pImpl->getNumAttributes();
824   AttributeSet AS;
825   uint64_t LastIndex = 0;
826   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
827     if (getSlotIndex(I) >= Index) {
828       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
829       break;
830     }
831     LastIndex = I + 1;
832     AttrSet.push_back(getSlotAttributes(I));
833   }
834
835   // Now remove the attribute from the correct slot. There may already be an
836   // AttributeSet there.
837   AttrBuilder B(AS, Index);
838
839   for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
840     if (Attrs.getSlotIndex(I) == Index) {
841       B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
842       break;
843     }
844
845   AttrSet.push_back(AttributeSet::get(C, Index, B));
846
847   // Add the remaining attribute slots.
848   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
849     AttrSet.push_back(getSlotAttributes(I));
850
851   return get(C, AttrSet);
852 }
853
854 //===----------------------------------------------------------------------===//
855 // AttributeSet Accessor Methods
856 //===----------------------------------------------------------------------===//
857
858 LLVMContext &AttributeSet::getContext() const {
859   return pImpl->getContext();
860 }
861
862 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
863   return pImpl && hasAttributes(Index) ?
864     AttributeSet::get(pImpl->getContext(),
865                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
866                         std::make_pair(Index, getAttributes(Index)))) :
867     AttributeSet();
868 }
869
870 AttributeSet AttributeSet::getRetAttributes() const {
871   return pImpl && hasAttributes(ReturnIndex) ?
872     AttributeSet::get(pImpl->getContext(),
873                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
874                         std::make_pair(ReturnIndex,
875                                        getAttributes(ReturnIndex)))) :
876     AttributeSet();
877 }
878
879 AttributeSet AttributeSet::getFnAttributes() const {
880   return pImpl && hasAttributes(FunctionIndex) ?
881     AttributeSet::get(pImpl->getContext(),
882                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
883                         std::make_pair(FunctionIndex,
884                                        getAttributes(FunctionIndex)))) :
885     AttributeSet();
886 }
887
888 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
889   AttributeSetNode *ASN = getAttributes(Index);
890   return ASN ? ASN->hasAttribute(Kind) : false;
891 }
892
893 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
894   AttributeSetNode *ASN = getAttributes(Index);
895   return ASN ? ASN->hasAttribute(Kind) : false;
896 }
897
898 bool AttributeSet::hasAttributes(unsigned Index) const {
899   AttributeSetNode *ASN = getAttributes(Index);
900   return ASN ? ASN->hasAttributes() : false;
901 }
902
903 /// \brief Return true if the specified attribute is set for at least one
904 /// parameter or for the return value.
905 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
906   if (!pImpl) return false;
907
908   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
909     for (AttributeSetImpl::iterator II = pImpl->begin(I),
910            IE = pImpl->end(I); II != IE; ++II)
911       if (II->hasAttribute(Attr))
912         return true;
913
914   return false;
915 }
916
917 Attribute AttributeSet::getAttribute(unsigned Index,
918                                      Attribute::AttrKind Kind) const {
919   AttributeSetNode *ASN = getAttributes(Index);
920   return ASN ? ASN->getAttribute(Kind) : Attribute();
921 }
922
923 Attribute AttributeSet::getAttribute(unsigned Index,
924                                      StringRef Kind) const {
925   AttributeSetNode *ASN = getAttributes(Index);
926   return ASN ? ASN->getAttribute(Kind) : Attribute();
927 }
928
929 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
930   AttributeSetNode *ASN = getAttributes(Index);
931   return ASN ? ASN->getAlignment() : 0;
932 }
933
934 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
935   AttributeSetNode *ASN = getAttributes(Index);
936   return ASN ? ASN->getStackAlignment() : 0;
937 }
938
939 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
940   AttributeSetNode *ASN = getAttributes(Index);
941   return ASN ? ASN->getDereferenceableBytes() : 0;
942 }
943
944 std::string AttributeSet::getAsString(unsigned Index,
945                                       bool InAttrGrp) const {
946   AttributeSetNode *ASN = getAttributes(Index);
947   return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
948 }
949
950 /// \brief The attributes for the specified index are returned.
951 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
952   if (!pImpl) return nullptr;
953
954   // Loop through to find the attribute node we want.
955   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
956     if (pImpl->getSlotIndex(I) == Index)
957       return pImpl->getSlotNode(I);
958
959   return nullptr;
960 }
961
962 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
963   if (!pImpl)
964     return ArrayRef<Attribute>().begin();
965   return pImpl->begin(Slot);
966 }
967
968 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
969   if (!pImpl)
970     return ArrayRef<Attribute>().end();
971   return pImpl->end(Slot);
972 }
973
974 //===----------------------------------------------------------------------===//
975 // AttributeSet Introspection Methods
976 //===----------------------------------------------------------------------===//
977
978 /// \brief Return the number of slots used in this attribute list.  This is the
979 /// number of arguments that have an attribute set on them (including the
980 /// function itself).
981 unsigned AttributeSet::getNumSlots() const {
982   return pImpl ? pImpl->getNumAttributes() : 0;
983 }
984
985 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
986   assert(pImpl && Slot < pImpl->getNumAttributes() &&
987          "Slot # out of range!");
988   return pImpl->getSlotIndex(Slot);
989 }
990
991 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
992   assert(pImpl && Slot < pImpl->getNumAttributes() &&
993          "Slot # out of range!");
994   return pImpl->getSlotAttributes(Slot);
995 }
996
997 uint64_t AttributeSet::Raw(unsigned Index) const {
998   // FIXME: Remove this.
999   return pImpl ? pImpl->Raw(Index) : 0;
1000 }
1001
1002 void AttributeSet::dump() const {
1003   dbgs() << "PAL[\n";
1004
1005   for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
1006     uint64_t Index = getSlotIndex(i);
1007     dbgs() << "  { ";
1008     if (Index == ~0U)
1009       dbgs() << "~0U";
1010     else
1011       dbgs() << Index;
1012     dbgs() << " => " << getAsString(Index) << " }\n";
1013   }
1014
1015   dbgs() << "]\n";
1016 }
1017
1018 //===----------------------------------------------------------------------===//
1019 // AttrBuilder Method Implementations
1020 //===----------------------------------------------------------------------===//
1021
1022 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1023   : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
1024   AttributeSetImpl *pImpl = AS.pImpl;
1025   if (!pImpl) return;
1026
1027   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1028     if (pImpl->getSlotIndex(I) != Index) continue;
1029
1030     for (AttributeSetImpl::iterator II = pImpl->begin(I),
1031            IE = pImpl->end(I); II != IE; ++II)
1032       addAttribute(*II);
1033
1034     break;
1035   }
1036 }
1037
1038 void AttrBuilder::clear() {
1039   Attrs.reset();
1040   Alignment = StackAlignment = DerefBytes = 0;
1041 }
1042
1043 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1044   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1045   assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1046          Val != Attribute::Dereferenceable &&
1047          "Adding integer attribute without adding a value!");
1048   Attrs[Val] = true;
1049   return *this;
1050 }
1051
1052 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1053   if (Attr.isStringAttribute()) {
1054     addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1055     return *this;
1056   }
1057
1058   Attribute::AttrKind Kind = Attr.getKindAsEnum();
1059   Attrs[Kind] = true;
1060
1061   if (Kind == Attribute::Alignment)
1062     Alignment = Attr.getAlignment();
1063   else if (Kind == Attribute::StackAlignment)
1064     StackAlignment = Attr.getStackAlignment();
1065   else if (Kind == Attribute::Dereferenceable)
1066     DerefBytes = Attr.getDereferenceableBytes();
1067   return *this;
1068 }
1069
1070 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1071   TargetDepAttrs[A] = V;
1072   return *this;
1073 }
1074
1075 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1076   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1077   Attrs[Val] = false;
1078
1079   if (Val == Attribute::Alignment)
1080     Alignment = 0;
1081   else if (Val == Attribute::StackAlignment)
1082     StackAlignment = 0;
1083   else if (Val == Attribute::Dereferenceable)
1084     DerefBytes = 0;
1085
1086   return *this;
1087 }
1088
1089 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1090   unsigned Slot = ~0U;
1091   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1092     if (A.getSlotIndex(I) == Index) {
1093       Slot = I;
1094       break;
1095     }
1096
1097   assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1098
1099   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1100     Attribute Attr = *I;
1101     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1102       Attribute::AttrKind Kind = I->getKindAsEnum();
1103       Attrs[Kind] = false;
1104
1105       if (Kind == Attribute::Alignment)
1106         Alignment = 0;
1107       else if (Kind == Attribute::StackAlignment)
1108         StackAlignment = 0;
1109       else if (Kind == Attribute::Dereferenceable)
1110         DerefBytes = 0;
1111     } else {
1112       assert(Attr.isStringAttribute() && "Invalid attribute type!");
1113       std::map<std::string, std::string>::iterator
1114         Iter = TargetDepAttrs.find(Attr.getKindAsString());
1115       if (Iter != TargetDepAttrs.end())
1116         TargetDepAttrs.erase(Iter);
1117     }
1118   }
1119
1120   return *this;
1121 }
1122
1123 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1124   std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1125   if (I != TargetDepAttrs.end())
1126     TargetDepAttrs.erase(I);
1127   return *this;
1128 }
1129
1130 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1131   if (Align == 0) return *this;
1132
1133   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1134   assert(Align <= 0x40000000 && "Alignment too large.");
1135
1136   Attrs[Attribute::Alignment] = true;
1137   Alignment = Align;
1138   return *this;
1139 }
1140
1141 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1142   // Default alignment, allow the target to define how to align it.
1143   if (Align == 0) return *this;
1144
1145   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1146   assert(Align <= 0x100 && "Alignment too large.");
1147
1148   Attrs[Attribute::StackAlignment] = true;
1149   StackAlignment = Align;
1150   return *this;
1151 }
1152
1153 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1154   if (Bytes == 0) return *this;
1155
1156   Attrs[Attribute::Dereferenceable] = true;
1157   DerefBytes = Bytes;
1158   return *this;
1159 }
1160
1161 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1162   // FIXME: What if both have alignments, but they don't match?!
1163   if (!Alignment)
1164     Alignment = B.Alignment;
1165
1166   if (!StackAlignment)
1167     StackAlignment = B.StackAlignment;
1168
1169   if (!DerefBytes)
1170     DerefBytes = B.DerefBytes;
1171
1172   Attrs |= B.Attrs;
1173
1174   for (td_const_iterator I = B.TargetDepAttrs.begin(),
1175          E = B.TargetDepAttrs.end(); I != E; ++I)
1176     TargetDepAttrs[I->first] = I->second;
1177
1178   return *this;
1179 }
1180
1181 bool AttrBuilder::contains(StringRef A) const {
1182   return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1183 }
1184
1185 bool AttrBuilder::hasAttributes() const {
1186   return !Attrs.none() || !TargetDepAttrs.empty();
1187 }
1188
1189 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1190   unsigned Slot = ~0U;
1191   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1192     if (A.getSlotIndex(I) == Index) {
1193       Slot = I;
1194       break;
1195     }
1196
1197   assert(Slot != ~0U && "Couldn't find the index!");
1198
1199   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1200        I != E; ++I) {
1201     Attribute Attr = *I;
1202     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1203       if (Attrs[I->getKindAsEnum()])
1204         return true;
1205     } else {
1206       assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1207       return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1208     }
1209   }
1210
1211   return false;
1212 }
1213
1214 bool AttrBuilder::hasAlignmentAttr() const {
1215   return Alignment != 0;
1216 }
1217
1218 bool AttrBuilder::operator==(const AttrBuilder &B) {
1219   if (Attrs != B.Attrs)
1220     return false;
1221
1222   for (td_const_iterator I = TargetDepAttrs.begin(),
1223          E = TargetDepAttrs.end(); I != E; ++I)
1224     if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1225       return false;
1226
1227   return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1228          DerefBytes == B.DerefBytes;
1229 }
1230
1231 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1232   // FIXME: Remove this in 4.0.
1233   if (!Val) return *this;
1234
1235   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1236        I = Attribute::AttrKind(I + 1)) {
1237     if (I == Attribute::Dereferenceable)
1238       continue;
1239     if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1240       Attrs[I] = true;
1241  
1242       if (I == Attribute::Alignment)
1243         Alignment = 1ULL << ((A >> 16) - 1);
1244       else if (I == Attribute::StackAlignment)
1245         StackAlignment = 1ULL << ((A >> 26)-1);
1246     }
1247   }
1248  
1249   return *this;
1250 }
1251
1252 //===----------------------------------------------------------------------===//
1253 // AttributeFuncs Function Defintions
1254 //===----------------------------------------------------------------------===//
1255
1256 /// \brief Which attributes cannot be applied to a type.
1257 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1258   AttrBuilder Incompatible;
1259
1260   if (!Ty->isIntegerTy())
1261     // Attribute that only apply to integers.
1262     Incompatible.addAttribute(Attribute::SExt)
1263       .addAttribute(Attribute::ZExt);
1264
1265   if (!Ty->isPointerTy())
1266     // Attribute that only apply to pointers.
1267     Incompatible.addAttribute(Attribute::ByVal)
1268       .addAttribute(Attribute::Nest)
1269       .addAttribute(Attribute::NoAlias)
1270       .addAttribute(Attribute::NoCapture)
1271       .addAttribute(Attribute::NonNull)
1272       .addDereferenceableAttr(1) // the int here is ignored
1273       .addAttribute(Attribute::ReadNone)
1274       .addAttribute(Attribute::ReadOnly)
1275       .addAttribute(Attribute::StructRet)
1276       .addAttribute(Attribute::InAlloca);
1277
1278   return AttributeSet::get(Ty->getContext(), Index, Incompatible);
1279 }