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