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