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 
get(LLVMContext & Context,Attribute::AttrKind Kind,uint64_t Val)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 
get(LLVMContext & Context,StringRef Kind,StringRef Val)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 
getWithAlignment(LLVMContext & Context,uint64_t Align)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 
getWithStackAlignment(LLVMContext & Context,uint64_t Align)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 
getWithDereferenceableBytes(LLVMContext & Context,uint64_t Bytes)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 
isEnumAttribute() const101 bool Attribute::isEnumAttribute() const {
102   return pImpl && pImpl->isEnumAttribute();
103 }
104 
isIntAttribute() const105 bool Attribute::isIntAttribute() const {
106   return pImpl && pImpl->isIntAttribute();
107 }
108 
isStringAttribute() const109 bool Attribute::isStringAttribute() const {
110   return pImpl && pImpl->isStringAttribute();
111 }
112 
getKindAsEnum() const113 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 
getValueAsInt() const120 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 
getKindAsString() const127 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 
getValueAsString() const134 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 
hasAttribute(AttrKind Kind) const141 bool Attribute::hasAttribute(AttrKind Kind) const {
142   return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
143 }
144 
hasAttribute(StringRef Kind) const145 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.
getAlignment() const151 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.
getStackAlignment() const159 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.
getDereferenceableBytes() const166 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 
getAsString(bool InAttrGrp) const173 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 += '\"' + getKindAsString().str() + '"';
302 
303     StringRef Val = pImpl->getValueAsString();
304     if (Val.empty()) return Result;
305 
306     Result += "=\"" + Val.str() + '"';
307     return Result;
308   }
309 
310   llvm_unreachable("Unknown attribute");
311 }
312 
operator <(Attribute A) const313 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.
~AttributeImpl()325 AttributeImpl::~AttributeImpl() {}
anchor()326 void EnumAttributeImpl::anchor() {}
anchor()327 void IntAttributeImpl::anchor() {}
anchor()328 void StringAttributeImpl::anchor() {}
329 
hasAttribute(Attribute::AttrKind A) const330 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
331   if (isStringAttribute()) return false;
332   return getKindAsEnum() == A;
333 }
334 
hasAttribute(StringRef Kind) const335 bool AttributeImpl::hasAttribute(StringRef Kind) const {
336   if (!isStringAttribute()) return false;
337   return getKindAsString() == Kind;
338 }
339 
getKindAsEnum() const340 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
341   assert(isEnumAttribute() || isIntAttribute());
342   return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
343 }
344 
getValueAsInt() const345 uint64_t AttributeImpl::getValueAsInt() const {
346   assert(isIntAttribute());
347   return static_cast<const IntAttributeImpl *>(this)->getValue();
348 }
349 
getKindAsString() const350 StringRef AttributeImpl::getKindAsString() const {
351   assert(isStringAttribute());
352   return static_cast<const StringAttributeImpl *>(this)->getStringKind();
353 }
354 
getValueAsString() const355 StringRef AttributeImpl::getValueAsString() const {
356   assert(isStringAttribute());
357   return static_cast<const StringAttributeImpl *>(this)->getStringValue();
358 }
359 
operator <(const AttributeImpl & AI) const360 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 
getAttrMask(Attribute::AttrKind Val)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 
get(LLVMContext & C,ArrayRef<Attribute> Attrs)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 
hasAttribute(Attribute::AttrKind Kind) const473 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 
hasAttribute(StringRef Kind) const480 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 
getAttribute(Attribute::AttrKind Kind) const487 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 
getAttribute(StringRef Kind) const494 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 
getAlignment() const501 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 
getStackAlignment() const508 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 
getDereferenceableBytes() const515 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 
getAsString(bool InAttrGrp) const522 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 
Raw(unsigned Index) const536 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 
dump() const567 void AttributeSetImpl::dump() const {
568   AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
569 }
570 
571 //===----------------------------------------------------------------------===//
572 // AttributeSet Construction and Mutation Methods
573 //===----------------------------------------------------------------------===//
574 
575 AttributeSet
getImpl(LLVMContext & C,ArrayRef<std::pair<unsigned,AttributeSetNode * >> Attrs)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 
get(LLVMContext & C,ArrayRef<std::pair<unsigned,Attribute>> Attrs)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 
get(LLVMContext & C,ArrayRef<std::pair<unsigned,AttributeSetNode * >> Attrs)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 
get(LLVMContext & C,unsigned Index,const AttrBuilder & B)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 
get(LLVMContext & C,unsigned Index,ArrayRef<Attribute::AttrKind> Kind)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 
get(LLVMContext & C,ArrayRef<AttributeSet> Attrs)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 
addAttribute(LLVMContext & C,unsigned Index,Attribute::AttrKind Attr) const718 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 
addAttribute(LLVMContext & C,unsigned Index,StringRef Kind) const724 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 
addAttribute(LLVMContext & C,unsigned Index,StringRef Kind,StringRef Value) const731 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 
addAttributes(LLVMContext & C,unsigned Index,AttributeSet Attrs) const738 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 
removeAttribute(LLVMContext & C,unsigned Index,Attribute::AttrKind Attr) const787 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 
removeAttributes(LLVMContext & C,unsigned Index,AttributeSet Attrs) const793 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 //===----------------------------------------------------------------------===//
839 // AttributeSet Accessor Methods
840 //===----------------------------------------------------------------------===//
841 
getContext() const842 LLVMContext &AttributeSet::getContext() const {
843   return pImpl->getContext();
844 }
845 
getParamAttributes(unsigned Index) const846 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
847   return pImpl && hasAttributes(Index) ?
848     AttributeSet::get(pImpl->getContext(),
849                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
850                         std::make_pair(Index, getAttributes(Index)))) :
851     AttributeSet();
852 }
853 
getRetAttributes() const854 AttributeSet AttributeSet::getRetAttributes() const {
855   return pImpl && hasAttributes(ReturnIndex) ?
856     AttributeSet::get(pImpl->getContext(),
857                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
858                         std::make_pair(ReturnIndex,
859                                        getAttributes(ReturnIndex)))) :
860     AttributeSet();
861 }
862 
getFnAttributes() const863 AttributeSet AttributeSet::getFnAttributes() const {
864   return pImpl && hasAttributes(FunctionIndex) ?
865     AttributeSet::get(pImpl->getContext(),
866                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
867                         std::make_pair(FunctionIndex,
868                                        getAttributes(FunctionIndex)))) :
869     AttributeSet();
870 }
871 
hasAttribute(unsigned Index,Attribute::AttrKind Kind) const872 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
873   AttributeSetNode *ASN = getAttributes(Index);
874   return ASN ? ASN->hasAttribute(Kind) : false;
875 }
876 
hasAttribute(unsigned Index,StringRef Kind) const877 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
878   AttributeSetNode *ASN = getAttributes(Index);
879   return ASN ? ASN->hasAttribute(Kind) : false;
880 }
881 
hasAttributes(unsigned Index) const882 bool AttributeSet::hasAttributes(unsigned Index) const {
883   AttributeSetNode *ASN = getAttributes(Index);
884   return ASN ? ASN->hasAttributes() : false;
885 }
886 
887 /// \brief Return true if the specified attribute is set for at least one
888 /// parameter or for the return value.
hasAttrSomewhere(Attribute::AttrKind Attr) const889 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
890   if (!pImpl) return false;
891 
892   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
893     for (AttributeSetImpl::iterator II = pImpl->begin(I),
894            IE = pImpl->end(I); II != IE; ++II)
895       if (II->hasAttribute(Attr))
896         return true;
897 
898   return false;
899 }
900 
getAttribute(unsigned Index,Attribute::AttrKind Kind) const901 Attribute AttributeSet::getAttribute(unsigned Index,
902                                      Attribute::AttrKind Kind) const {
903   AttributeSetNode *ASN = getAttributes(Index);
904   return ASN ? ASN->getAttribute(Kind) : Attribute();
905 }
906 
getAttribute(unsigned Index,StringRef Kind) const907 Attribute AttributeSet::getAttribute(unsigned Index,
908                                      StringRef Kind) const {
909   AttributeSetNode *ASN = getAttributes(Index);
910   return ASN ? ASN->getAttribute(Kind) : Attribute();
911 }
912 
getParamAlignment(unsigned Index) const913 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
914   AttributeSetNode *ASN = getAttributes(Index);
915   return ASN ? ASN->getAlignment() : 0;
916 }
917 
getStackAlignment(unsigned Index) const918 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
919   AttributeSetNode *ASN = getAttributes(Index);
920   return ASN ? ASN->getStackAlignment() : 0;
921 }
922 
getDereferenceableBytes(unsigned Index) const923 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
924   AttributeSetNode *ASN = getAttributes(Index);
925   return ASN ? ASN->getDereferenceableBytes() : 0;
926 }
927 
getAsString(unsigned Index,bool InAttrGrp) const928 std::string AttributeSet::getAsString(unsigned Index,
929                                       bool InAttrGrp) const {
930   AttributeSetNode *ASN = getAttributes(Index);
931   return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
932 }
933 
934 /// \brief The attributes for the specified index are returned.
getAttributes(unsigned Index) const935 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
936   if (!pImpl) return nullptr;
937 
938   // Loop through to find the attribute node we want.
939   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
940     if (pImpl->getSlotIndex(I) == Index)
941       return pImpl->getSlotNode(I);
942 
943   return nullptr;
944 }
945 
begin(unsigned Slot) const946 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
947   if (!pImpl)
948     return ArrayRef<Attribute>().begin();
949   return pImpl->begin(Slot);
950 }
951 
end(unsigned Slot) const952 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
953   if (!pImpl)
954     return ArrayRef<Attribute>().end();
955   return pImpl->end(Slot);
956 }
957 
958 //===----------------------------------------------------------------------===//
959 // AttributeSet Introspection Methods
960 //===----------------------------------------------------------------------===//
961 
962 /// \brief Return the number of slots used in this attribute list.  This is the
963 /// number of arguments that have an attribute set on them (including the
964 /// function itself).
getNumSlots() const965 unsigned AttributeSet::getNumSlots() const {
966   return pImpl ? pImpl->getNumAttributes() : 0;
967 }
968 
getSlotIndex(unsigned Slot) const969 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
970   assert(pImpl && Slot < pImpl->getNumAttributes() &&
971          "Slot # out of range!");
972   return pImpl->getSlotIndex(Slot);
973 }
974 
getSlotAttributes(unsigned Slot) const975 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
976   assert(pImpl && Slot < pImpl->getNumAttributes() &&
977          "Slot # out of range!");
978   return pImpl->getSlotAttributes(Slot);
979 }
980 
Raw(unsigned Index) const981 uint64_t AttributeSet::Raw(unsigned Index) const {
982   // FIXME: Remove this.
983   return pImpl ? pImpl->Raw(Index) : 0;
984 }
985 
dump() const986 void AttributeSet::dump() const {
987   dbgs() << "PAL[\n";
988 
989   for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
990     uint64_t Index = getSlotIndex(i);
991     dbgs() << "  { ";
992     if (Index == ~0U)
993       dbgs() << "~0U";
994     else
995       dbgs() << Index;
996     dbgs() << " => " << getAsString(Index) << " }\n";
997   }
998 
999   dbgs() << "]\n";
1000 }
1001 
1002 //===----------------------------------------------------------------------===//
1003 // AttrBuilder Method Implementations
1004 //===----------------------------------------------------------------------===//
1005 
AttrBuilder(AttributeSet AS,unsigned Index)1006 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
1007   : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
1008   AttributeSetImpl *pImpl = AS.pImpl;
1009   if (!pImpl) return;
1010 
1011   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
1012     if (pImpl->getSlotIndex(I) != Index) continue;
1013 
1014     for (AttributeSetImpl::iterator II = pImpl->begin(I),
1015            IE = pImpl->end(I); II != IE; ++II)
1016       addAttribute(*II);
1017 
1018     break;
1019   }
1020 }
1021 
clear()1022 void AttrBuilder::clear() {
1023   Attrs.reset();
1024   Alignment = StackAlignment = DerefBytes = 0;
1025 }
1026 
addAttribute(Attribute::AttrKind Val)1027 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
1028   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1029   assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
1030          Val != Attribute::Dereferenceable &&
1031          "Adding integer attribute without adding a value!");
1032   Attrs[Val] = true;
1033   return *this;
1034 }
1035 
addAttribute(Attribute Attr)1036 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
1037   if (Attr.isStringAttribute()) {
1038     addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
1039     return *this;
1040   }
1041 
1042   Attribute::AttrKind Kind = Attr.getKindAsEnum();
1043   Attrs[Kind] = true;
1044 
1045   if (Kind == Attribute::Alignment)
1046     Alignment = Attr.getAlignment();
1047   else if (Kind == Attribute::StackAlignment)
1048     StackAlignment = Attr.getStackAlignment();
1049   else if (Kind == Attribute::Dereferenceable)
1050     DerefBytes = Attr.getDereferenceableBytes();
1051   return *this;
1052 }
1053 
addAttribute(StringRef A,StringRef V)1054 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
1055   TargetDepAttrs[A] = V;
1056   return *this;
1057 }
1058 
removeAttribute(Attribute::AttrKind Val)1059 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
1060   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
1061   Attrs[Val] = false;
1062 
1063   if (Val == Attribute::Alignment)
1064     Alignment = 0;
1065   else if (Val == Attribute::StackAlignment)
1066     StackAlignment = 0;
1067   else if (Val == Attribute::Dereferenceable)
1068     DerefBytes = 0;
1069 
1070   return *this;
1071 }
1072 
removeAttributes(AttributeSet A,uint64_t Index)1073 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
1074   unsigned Slot = ~0U;
1075   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1076     if (A.getSlotIndex(I) == Index) {
1077       Slot = I;
1078       break;
1079     }
1080 
1081   assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
1082 
1083   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
1084     Attribute Attr = *I;
1085     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1086       Attribute::AttrKind Kind = I->getKindAsEnum();
1087       Attrs[Kind] = false;
1088 
1089       if (Kind == Attribute::Alignment)
1090         Alignment = 0;
1091       else if (Kind == Attribute::StackAlignment)
1092         StackAlignment = 0;
1093       else if (Kind == Attribute::Dereferenceable)
1094         DerefBytes = 0;
1095     } else {
1096       assert(Attr.isStringAttribute() && "Invalid attribute type!");
1097       std::map<std::string, std::string>::iterator
1098         Iter = TargetDepAttrs.find(Attr.getKindAsString());
1099       if (Iter != TargetDepAttrs.end())
1100         TargetDepAttrs.erase(Iter);
1101     }
1102   }
1103 
1104   return *this;
1105 }
1106 
removeAttribute(StringRef A)1107 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1108   std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1109   if (I != TargetDepAttrs.end())
1110     TargetDepAttrs.erase(I);
1111   return *this;
1112 }
1113 
addAlignmentAttr(unsigned Align)1114 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
1115   if (Align == 0) return *this;
1116 
1117   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1118   assert(Align <= 0x40000000 && "Alignment too large.");
1119 
1120   Attrs[Attribute::Alignment] = true;
1121   Alignment = Align;
1122   return *this;
1123 }
1124 
addStackAlignmentAttr(unsigned Align)1125 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1126   // Default alignment, allow the target to define how to align it.
1127   if (Align == 0) return *this;
1128 
1129   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1130   assert(Align <= 0x100 && "Alignment too large.");
1131 
1132   Attrs[Attribute::StackAlignment] = true;
1133   StackAlignment = Align;
1134   return *this;
1135 }
1136 
addDereferenceableAttr(uint64_t Bytes)1137 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
1138   if (Bytes == 0) return *this;
1139 
1140   Attrs[Attribute::Dereferenceable] = true;
1141   DerefBytes = Bytes;
1142   return *this;
1143 }
1144 
merge(const AttrBuilder & B)1145 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1146   // FIXME: What if both have alignments, but they don't match?!
1147   if (!Alignment)
1148     Alignment = B.Alignment;
1149 
1150   if (!StackAlignment)
1151     StackAlignment = B.StackAlignment;
1152 
1153   if (!DerefBytes)
1154     DerefBytes = B.DerefBytes;
1155 
1156   Attrs |= B.Attrs;
1157 
1158   for (td_const_iterator I = B.TargetDepAttrs.begin(),
1159          E = B.TargetDepAttrs.end(); I != E; ++I)
1160     TargetDepAttrs[I->first] = I->second;
1161 
1162   return *this;
1163 }
1164 
contains(StringRef A) const1165 bool AttrBuilder::contains(StringRef A) const {
1166   return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1167 }
1168 
hasAttributes() const1169 bool AttrBuilder::hasAttributes() const {
1170   return !Attrs.none() || !TargetDepAttrs.empty();
1171 }
1172 
hasAttributes(AttributeSet A,uint64_t Index) const1173 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
1174   unsigned Slot = ~0U;
1175   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1176     if (A.getSlotIndex(I) == Index) {
1177       Slot = I;
1178       break;
1179     }
1180 
1181   assert(Slot != ~0U && "Couldn't find the index!");
1182 
1183   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
1184        I != E; ++I) {
1185     Attribute Attr = *I;
1186     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
1187       if (Attrs[I->getKindAsEnum()])
1188         return true;
1189     } else {
1190       assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1191       return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1192     }
1193   }
1194 
1195   return false;
1196 }
1197 
hasAlignmentAttr() const1198 bool AttrBuilder::hasAlignmentAttr() const {
1199   return Alignment != 0;
1200 }
1201 
operator ==(const AttrBuilder & B)1202 bool AttrBuilder::operator==(const AttrBuilder &B) {
1203   if (Attrs != B.Attrs)
1204     return false;
1205 
1206   for (td_const_iterator I = TargetDepAttrs.begin(),
1207          E = TargetDepAttrs.end(); I != E; ++I)
1208     if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1209       return false;
1210 
1211   return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
1212          DerefBytes == B.DerefBytes;
1213 }
1214 
addRawValue(uint64_t Val)1215 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
1216   // FIXME: Remove this in 4.0.
1217   if (!Val) return *this;
1218 
1219   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1220        I = Attribute::AttrKind(I + 1)) {
1221     if (I == Attribute::Dereferenceable)
1222       continue;
1223     if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1224       Attrs[I] = true;
1225 
1226       if (I == Attribute::Alignment)
1227         Alignment = 1ULL << ((A >> 16) - 1);
1228       else if (I == Attribute::StackAlignment)
1229         StackAlignment = 1ULL << ((A >> 26)-1);
1230     }
1231   }
1232 
1233   return *this;
1234 }
1235 
1236 //===----------------------------------------------------------------------===//
1237 // AttributeFuncs Function Defintions
1238 //===----------------------------------------------------------------------===//
1239 
1240 /// \brief Which attributes cannot be applied to a type.
typeIncompatible(Type * Ty,uint64_t Index)1241 AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
1242   AttrBuilder Incompatible;
1243 
1244   if (!Ty->isIntegerTy())
1245     // Attribute that only apply to integers.
1246     Incompatible.addAttribute(Attribute::SExt)
1247       .addAttribute(Attribute::ZExt);
1248 
1249   if (!Ty->isPointerTy())
1250     // Attribute that only apply to pointers.
1251     Incompatible.addAttribute(Attribute::ByVal)
1252       .addAttribute(Attribute::Nest)
1253       .addAttribute(Attribute::NoAlias)
1254       .addAttribute(Attribute::NoCapture)
1255       .addAttribute(Attribute::NonNull)
1256       .addDereferenceableAttr(1) // the int here is ignored
1257       .addAttribute(Attribute::ReadNone)
1258       .addAttribute(Attribute::ReadOnly)
1259       .addAttribute(Attribute::StructRet)
1260       .addAttribute(Attribute::InAlloca);
1261 
1262   return AttributeSet::get(Ty->getContext(), Index, Incompatible);
1263 }
1264