LLVM  mainline
Attributes.cpp
Go to the documentation of this file.
00001 //===-- Attributes.cpp - Implement AttributesList -------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // \file
00011 // \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
00012 // AttributeSetImpl, and AttributeSet classes.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/IR/Attributes.h"
00017 #include "AttributeImpl.h"
00018 #include "LLVMContextImpl.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/ADT/StringExtras.h"
00021 #include "llvm/IR/Type.h"
00022 #include "llvm/Support/Atomic.h"
00023 #include "llvm/Support/Debug.h"
00024 #include "llvm/Support/ManagedStatic.h"
00025 #include "llvm/Support/Mutex.h"
00026 #include "llvm/Support/raw_ostream.h"
00027 #include <algorithm>
00028 using namespace llvm;
00029 
00030 //===----------------------------------------------------------------------===//
00031 // Attribute Construction Methods
00032 //===----------------------------------------------------------------------===//
00033 
00034 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
00035                          uint64_t Val) {
00036   LLVMContextImpl *pImpl = Context.pImpl;
00037   FoldingSetNodeID ID;
00038   ID.AddInteger(Kind);
00039   if (Val) ID.AddInteger(Val);
00040 
00041   void *InsertPoint;
00042   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
00043 
00044   if (!PA) {
00045     // If we didn't find any existing attributes of the same shape then create a
00046     // new one and insert it.
00047     if (!Val)
00048       PA = new EnumAttributeImpl(Kind);
00049     else
00050       PA = new IntAttributeImpl(Kind, Val);
00051     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
00052   }
00053 
00054   // Return the Attribute that we found or created.
00055   return Attribute(PA);
00056 }
00057 
00058 Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
00059   LLVMContextImpl *pImpl = Context.pImpl;
00060   FoldingSetNodeID ID;
00061   ID.AddString(Kind);
00062   if (!Val.empty()) ID.AddString(Val);
00063 
00064   void *InsertPoint;
00065   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
00066 
00067   if (!PA) {
00068     // If we didn't find any existing attributes of the same shape then create a
00069     // new one and insert it.
00070     PA = new StringAttributeImpl(Kind, Val);
00071     pImpl->AttrsSet.InsertNode(PA, InsertPoint);
00072   }
00073 
00074   // Return the Attribute that we found or created.
00075   return Attribute(PA);
00076 }
00077 
00078 Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
00079   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
00080   assert(Align <= 0x40000000 && "Alignment too large.");
00081   return get(Context, Alignment, Align);
00082 }
00083 
00084 Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
00085                                            uint64_t Align) {
00086   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
00087   assert(Align <= 0x100 && "Alignment too large.");
00088   return get(Context, StackAlignment, Align);
00089 }
00090 
00091 Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
00092                                                 uint64_t Bytes) {
00093   assert(Bytes && "Bytes must be non-zero.");
00094   return get(Context, Dereferenceable, Bytes);
00095 }
00096 
00097 Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
00098                                                        uint64_t Bytes) {
00099   assert(Bytes && "Bytes must be non-zero.");
00100   return get(Context, DereferenceableOrNull, Bytes);
00101 }
00102 
00103 //===----------------------------------------------------------------------===//
00104 // Attribute Accessor Methods
00105 //===----------------------------------------------------------------------===//
00106 
00107 bool Attribute::isEnumAttribute() const {
00108   return pImpl && pImpl->isEnumAttribute();
00109 }
00110 
00111 bool Attribute::isIntAttribute() const {
00112   return pImpl && pImpl->isIntAttribute();
00113 }
00114 
00115 bool Attribute::isStringAttribute() const {
00116   return pImpl && pImpl->isStringAttribute();
00117 }
00118 
00119 Attribute::AttrKind Attribute::getKindAsEnum() const {
00120   if (!pImpl) return None;
00121   assert((isEnumAttribute() || isIntAttribute()) &&
00122          "Invalid attribute type to get the kind as an enum!");
00123   return pImpl ? pImpl->getKindAsEnum() : None;
00124 }
00125 
00126 uint64_t Attribute::getValueAsInt() const {
00127   if (!pImpl) return 0;
00128   assert(isIntAttribute() &&
00129          "Expected the attribute to be an integer attribute!");
00130   return pImpl ? pImpl->getValueAsInt() : 0;
00131 }
00132 
00133 StringRef Attribute::getKindAsString() const {
00134   if (!pImpl) return StringRef();
00135   assert(isStringAttribute() &&
00136          "Invalid attribute type to get the kind as a string!");
00137   return pImpl ? pImpl->getKindAsString() : StringRef();
00138 }
00139 
00140 StringRef Attribute::getValueAsString() const {
00141   if (!pImpl) return StringRef();
00142   assert(isStringAttribute() &&
00143          "Invalid attribute type to get the value as a string!");
00144   return pImpl ? pImpl->getValueAsString() : StringRef();
00145 }
00146 
00147 bool Attribute::hasAttribute(AttrKind Kind) const {
00148   return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
00149 }
00150 
00151 bool Attribute::hasAttribute(StringRef Kind) const {
00152   if (!isStringAttribute()) return false;
00153   return pImpl && pImpl->hasAttribute(Kind);
00154 }
00155 
00156 /// This returns the alignment field of an attribute as a byte alignment value.
00157 unsigned Attribute::getAlignment() const {
00158   assert(hasAttribute(Attribute::Alignment) &&
00159          "Trying to get alignment from non-alignment attribute!");
00160   return pImpl->getValueAsInt();
00161 }
00162 
00163 /// This returns the stack alignment field of an attribute as a byte alignment
00164 /// value.
00165 unsigned Attribute::getStackAlignment() const {
00166   assert(hasAttribute(Attribute::StackAlignment) &&
00167          "Trying to get alignment from non-alignment attribute!");
00168   return pImpl->getValueAsInt();
00169 }
00170 
00171 /// This returns the number of dereferenceable bytes.
00172 uint64_t Attribute::getDereferenceableBytes() const {
00173   assert(hasAttribute(Attribute::Dereferenceable) &&
00174          "Trying to get dereferenceable bytes from "
00175          "non-dereferenceable attribute!");
00176   return pImpl->getValueAsInt();
00177 }
00178 
00179 uint64_t Attribute::getDereferenceableOrNullBytes() const {
00180   assert(hasAttribute(Attribute::DereferenceableOrNull) &&
00181          "Trying to get dereferenceable bytes from "
00182          "non-dereferenceable attribute!");
00183   return pImpl->getValueAsInt();
00184 }
00185 
00186 std::string Attribute::getAsString(bool InAttrGrp) const {
00187   if (!pImpl) return "";
00188 
00189   if (hasAttribute(Attribute::SanitizeAddress))
00190     return "sanitize_address";
00191   if (hasAttribute(Attribute::AlwaysInline))
00192     return "alwaysinline";
00193   if (hasAttribute(Attribute::Builtin))
00194     return "builtin";
00195   if (hasAttribute(Attribute::ByVal))
00196     return "byval";
00197   if (hasAttribute(Attribute::Convergent))
00198     return "convergent";
00199   if (hasAttribute(Attribute::InAlloca))
00200     return "inalloca";
00201   if (hasAttribute(Attribute::InlineHint))
00202     return "inlinehint";
00203   if (hasAttribute(Attribute::InReg))
00204     return "inreg";
00205   if (hasAttribute(Attribute::JumpTable))
00206     return "jumptable";
00207   if (hasAttribute(Attribute::MinSize))
00208     return "minsize";
00209   if (hasAttribute(Attribute::Naked))
00210     return "naked";
00211   if (hasAttribute(Attribute::Nest))
00212     return "nest";
00213   if (hasAttribute(Attribute::NoAlias))
00214     return "noalias";
00215   if (hasAttribute(Attribute::NoBuiltin))
00216     return "nobuiltin";
00217   if (hasAttribute(Attribute::NoCapture))
00218     return "nocapture";
00219   if (hasAttribute(Attribute::NoDuplicate))
00220     return "noduplicate";
00221   if (hasAttribute(Attribute::NoImplicitFloat))
00222     return "noimplicitfloat";
00223   if (hasAttribute(Attribute::NoInline))
00224     return "noinline";
00225   if (hasAttribute(Attribute::NonLazyBind))
00226     return "nonlazybind";
00227   if (hasAttribute(Attribute::NonNull))
00228     return "nonnull";
00229   if (hasAttribute(Attribute::NoRedZone))
00230     return "noredzone";
00231   if (hasAttribute(Attribute::NoReturn))
00232     return "noreturn";
00233   if (hasAttribute(Attribute::NoUnwind))
00234     return "nounwind";
00235   if (hasAttribute(Attribute::OptimizeNone))
00236     return "optnone";
00237   if (hasAttribute(Attribute::OptimizeForSize))
00238     return "optsize";
00239   if (hasAttribute(Attribute::ReadNone))
00240     return "readnone";
00241   if (hasAttribute(Attribute::ReadOnly))
00242     return "readonly";
00243   if (hasAttribute(Attribute::Returned))
00244     return "returned";
00245   if (hasAttribute(Attribute::ReturnsTwice))
00246     return "returns_twice";
00247   if (hasAttribute(Attribute::SExt))
00248     return "signext";
00249   if (hasAttribute(Attribute::StackProtect))
00250     return "ssp";
00251   if (hasAttribute(Attribute::StackProtectReq))
00252     return "sspreq";
00253   if (hasAttribute(Attribute::StackProtectStrong))
00254     return "sspstrong";
00255   if (hasAttribute(Attribute::SafeStack))
00256     return "safestack";
00257   if (hasAttribute(Attribute::StructRet))
00258     return "sret";
00259   if (hasAttribute(Attribute::SanitizeThread))
00260     return "sanitize_thread";
00261   if (hasAttribute(Attribute::SanitizeMemory))
00262     return "sanitize_memory";
00263   if (hasAttribute(Attribute::UWTable))
00264     return "uwtable";
00265   if (hasAttribute(Attribute::ZExt))
00266     return "zeroext";
00267   if (hasAttribute(Attribute::Cold))
00268     return "cold";
00269 
00270   // FIXME: These should be output like this:
00271   //
00272   //   align=4
00273   //   alignstack=8
00274   //
00275   if (hasAttribute(Attribute::Alignment)) {
00276     std::string Result;
00277     Result += "align";
00278     Result += (InAttrGrp) ? "=" : " ";
00279     Result += utostr(getValueAsInt());
00280     return Result;
00281   }
00282 
00283   auto AttrWithBytesToString = [&](const char *Name) {
00284     std::string Result;
00285     Result += Name;
00286     if (InAttrGrp) {
00287       Result += "=";
00288       Result += utostr(getValueAsInt());
00289     } else {
00290       Result += "(";
00291       Result += utostr(getValueAsInt());
00292       Result += ")";
00293     }
00294     return Result;
00295   };
00296 
00297   if (hasAttribute(Attribute::StackAlignment))
00298     return AttrWithBytesToString("alignstack");
00299 
00300   if (hasAttribute(Attribute::Dereferenceable))
00301     return AttrWithBytesToString("dereferenceable");
00302 
00303   if (hasAttribute(Attribute::DereferenceableOrNull))
00304     return AttrWithBytesToString("dereferenceable_or_null");
00305 
00306   // Convert target-dependent attributes to strings of the form:
00307   //
00308   //   "kind"
00309   //   "kind" = "value"
00310   //
00311   if (isStringAttribute()) {
00312     std::string Result;
00313     Result += (Twine('"') + getKindAsString() + Twine('"')).str();
00314 
00315     StringRef Val = pImpl->getValueAsString();
00316     if (Val.empty()) return Result;
00317 
00318     Result += ("=\"" + Val + Twine('"')).str();
00319     return Result;
00320   }
00321 
00322   llvm_unreachable("Unknown attribute");
00323 }
00324 
00325 bool Attribute::operator<(Attribute A) const {
00326   if (!pImpl && !A.pImpl) return false;
00327   if (!pImpl) return true;
00328   if (!A.pImpl) return false;
00329   return *pImpl < *A.pImpl;
00330 }
00331 
00332 //===----------------------------------------------------------------------===//
00333 // AttributeImpl Definition
00334 //===----------------------------------------------------------------------===//
00335 
00336 // Pin the vtables to this file.
00337 AttributeImpl::~AttributeImpl() {}
00338 void EnumAttributeImpl::anchor() {}
00339 void IntAttributeImpl::anchor() {}
00340 void StringAttributeImpl::anchor() {}
00341 
00342 bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
00343   if (isStringAttribute()) return false;
00344   return getKindAsEnum() == A;
00345 }
00346 
00347 bool AttributeImpl::hasAttribute(StringRef Kind) const {
00348   if (!isStringAttribute()) return false;
00349   return getKindAsString() == Kind;
00350 }
00351 
00352 Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
00353   assert(isEnumAttribute() || isIntAttribute());
00354   return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
00355 }
00356 
00357 uint64_t AttributeImpl::getValueAsInt() const {
00358   assert(isIntAttribute());
00359   return static_cast<const IntAttributeImpl *>(this)->getValue();
00360 }
00361 
00362 StringRef AttributeImpl::getKindAsString() const {
00363   assert(isStringAttribute());
00364   return static_cast<const StringAttributeImpl *>(this)->getStringKind();
00365 }
00366 
00367 StringRef AttributeImpl::getValueAsString() const {
00368   assert(isStringAttribute());
00369   return static_cast<const StringAttributeImpl *>(this)->getStringValue();
00370 }
00371 
00372 bool AttributeImpl::operator<(const AttributeImpl &AI) const {
00373   // This sorts the attributes with Attribute::AttrKinds coming first (sorted
00374   // relative to their enum value) and then strings.
00375   if (isEnumAttribute()) {
00376     if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
00377     if (AI.isIntAttribute()) return true;
00378     if (AI.isStringAttribute()) return true;
00379   }
00380 
00381   if (isIntAttribute()) {
00382     if (AI.isEnumAttribute()) return false;
00383     if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
00384     if (AI.isStringAttribute()) return true;
00385   }
00386 
00387   if (AI.isEnumAttribute()) return false;
00388   if (AI.isIntAttribute()) return false;
00389   if (getKindAsString() == AI.getKindAsString())
00390     return getValueAsString() < AI.getValueAsString();
00391   return getKindAsString() < AI.getKindAsString();
00392 }
00393 
00394 uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
00395   // FIXME: Remove this.
00396   switch (Val) {
00397   case Attribute::EndAttrKinds:
00398     llvm_unreachable("Synthetic enumerators which should never get here");
00399 
00400   case Attribute::None:            return 0;
00401   case Attribute::ZExt:            return 1 << 0;
00402   case Attribute::SExt:            return 1 << 1;
00403   case Attribute::NoReturn:        return 1 << 2;
00404   case Attribute::InReg:           return 1 << 3;
00405   case Attribute::StructRet:       return 1 << 4;
00406   case Attribute::NoUnwind:        return 1 << 5;
00407   case Attribute::NoAlias:         return 1 << 6;
00408   case Attribute::ByVal:           return 1 << 7;
00409   case Attribute::Nest:            return 1 << 8;
00410   case Attribute::ReadNone:        return 1 << 9;
00411   case Attribute::ReadOnly:        return 1 << 10;
00412   case Attribute::NoInline:        return 1 << 11;
00413   case Attribute::AlwaysInline:    return 1 << 12;
00414   case Attribute::OptimizeForSize: return 1 << 13;
00415   case Attribute::StackProtect:    return 1 << 14;
00416   case Attribute::StackProtectReq: return 1 << 15;
00417   case Attribute::Alignment:       return 31 << 16;
00418   case Attribute::NoCapture:       return 1 << 21;
00419   case Attribute::NoRedZone:       return 1 << 22;
00420   case Attribute::NoImplicitFloat: return 1 << 23;
00421   case Attribute::Naked:           return 1 << 24;
00422   case Attribute::InlineHint:      return 1 << 25;
00423   case Attribute::StackAlignment:  return 7 << 26;
00424   case Attribute::ReturnsTwice:    return 1 << 29;
00425   case Attribute::UWTable:         return 1 << 30;
00426   case Attribute::NonLazyBind:     return 1U << 31;
00427   case Attribute::SanitizeAddress: return 1ULL << 32;
00428   case Attribute::MinSize:         return 1ULL << 33;
00429   case Attribute::NoDuplicate:     return 1ULL << 34;
00430   case Attribute::StackProtectStrong: return 1ULL << 35;
00431   case Attribute::SanitizeThread:  return 1ULL << 36;
00432   case Attribute::SanitizeMemory:  return 1ULL << 37;
00433   case Attribute::NoBuiltin:       return 1ULL << 38;
00434   case Attribute::Returned:        return 1ULL << 39;
00435   case Attribute::Cold:            return 1ULL << 40;
00436   case Attribute::Builtin:         return 1ULL << 41;
00437   case Attribute::OptimizeNone:    return 1ULL << 42;
00438   case Attribute::InAlloca:        return 1ULL << 43;
00439   case Attribute::NonNull:         return 1ULL << 44;
00440   case Attribute::JumpTable:       return 1ULL << 45;
00441   case Attribute::Convergent:      return 1ULL << 46;
00442   case Attribute::SafeStack:       return 1ULL << 47;
00443   case Attribute::Dereferenceable:
00444     llvm_unreachable("dereferenceable attribute not supported in raw format");
00445     break;
00446   case Attribute::DereferenceableOrNull:
00447     llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
00448                      "format");
00449     break;
00450   }
00451   llvm_unreachable("Unsupported attribute type");
00452 }
00453 
00454 //===----------------------------------------------------------------------===//
00455 // AttributeSetNode Definition
00456 //===----------------------------------------------------------------------===//
00457 
00458 AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
00459                                         ArrayRef<Attribute> Attrs) {
00460   if (Attrs.empty())
00461     return nullptr;
00462 
00463   // Otherwise, build a key to look up the existing attributes.
00464   LLVMContextImpl *pImpl = C.pImpl;
00465   FoldingSetNodeID ID;
00466 
00467   SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
00468   array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
00469 
00470   for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
00471          E = SortedAttrs.end(); I != E; ++I)
00472     I->Profile(ID);
00473 
00474   void *InsertPoint;
00475   AttributeSetNode *PA =
00476     pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
00477 
00478   // If we didn't find any existing attributes of the same shape then create a
00479   // new one and insert it.
00480   if (!PA) {
00481     // Coallocate entries after the AttributeSetNode itself.
00482     void *Mem = ::operator new(sizeof(AttributeSetNode) +
00483                                sizeof(Attribute) * SortedAttrs.size());
00484     PA = new (Mem) AttributeSetNode(SortedAttrs);
00485     pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
00486   }
00487 
00488   // Return the AttributesListNode that we found or created.
00489   return PA;
00490 }
00491 
00492 bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
00493   for (iterator I = begin(), E = end(); I != E; ++I)
00494     if (I->hasAttribute(Kind))
00495       return true;
00496   return false;
00497 }
00498 
00499 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
00500   for (iterator I = begin(), E = end(); I != E; ++I)
00501     if (I->hasAttribute(Kind))
00502       return true;
00503   return false;
00504 }
00505 
00506 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
00507   for (iterator I = begin(), E = end(); I != E; ++I)
00508     if (I->hasAttribute(Kind))
00509       return *I;
00510   return Attribute();
00511 }
00512 
00513 Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
00514   for (iterator I = begin(), E = end(); I != E; ++I)
00515     if (I->hasAttribute(Kind))
00516       return *I;
00517   return Attribute();
00518 }
00519 
00520 unsigned AttributeSetNode::getAlignment() const {
00521   for (iterator I = begin(), E = end(); I != E; ++I)
00522     if (I->hasAttribute(Attribute::Alignment))
00523       return I->getAlignment();
00524   return 0;
00525 }
00526 
00527 unsigned AttributeSetNode::getStackAlignment() const {
00528   for (iterator I = begin(), E = end(); I != E; ++I)
00529     if (I->hasAttribute(Attribute::StackAlignment))
00530       return I->getStackAlignment();
00531   return 0;
00532 }
00533 
00534 uint64_t AttributeSetNode::getDereferenceableBytes() const {
00535   for (iterator I = begin(), E = end(); I != E; ++I)
00536     if (I->hasAttribute(Attribute::Dereferenceable))
00537       return I->getDereferenceableBytes();
00538   return 0;
00539 }
00540 
00541 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
00542   for (iterator I = begin(), E = end(); I != E; ++I)
00543     if (I->hasAttribute(Attribute::DereferenceableOrNull))
00544       return I->getDereferenceableOrNullBytes();
00545   return 0;
00546 }
00547 
00548 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
00549   std::string Str;
00550   for (iterator I = begin(), E = end(); I != E; ++I) {
00551     if (I != begin())
00552       Str += ' ';
00553     Str += I->getAsString(InAttrGrp);
00554   }
00555   return Str;
00556 }
00557 
00558 //===----------------------------------------------------------------------===//
00559 // AttributeSetImpl Definition
00560 //===----------------------------------------------------------------------===//
00561 
00562 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
00563   for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
00564     if (getSlotIndex(I) != Index) continue;
00565     const AttributeSetNode *ASN = getSlotNode(I);
00566     uint64_t Mask = 0;
00567 
00568     for (AttributeSetNode::iterator II = ASN->begin(),
00569            IE = ASN->end(); II != IE; ++II) {
00570       Attribute Attr = *II;
00571 
00572       // This cannot handle string attributes.
00573       if (Attr.isStringAttribute()) continue;
00574 
00575       Attribute::AttrKind Kind = Attr.getKindAsEnum();
00576 
00577       if (Kind == Attribute::Alignment)
00578         Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
00579       else if (Kind == Attribute::StackAlignment)
00580         Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
00581       else if (Kind == Attribute::Dereferenceable)
00582         llvm_unreachable("dereferenceable not supported in bit mask");
00583       else
00584         Mask |= AttributeImpl::getAttrMask(Kind);
00585     }
00586 
00587     return Mask;
00588   }
00589 
00590   return 0;
00591 }
00592 
00593 void AttributeSetImpl::dump() const {
00594   AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
00595 }
00596 
00597 //===----------------------------------------------------------------------===//
00598 // AttributeSet Construction and Mutation Methods
00599 //===----------------------------------------------------------------------===//
00600 
00601 AttributeSet
00602 AttributeSet::getImpl(LLVMContext &C,
00603                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
00604   LLVMContextImpl *pImpl = C.pImpl;
00605   FoldingSetNodeID ID;
00606   AttributeSetImpl::Profile(ID, Attrs);
00607 
00608   void *InsertPoint;
00609   AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
00610 
00611   // If we didn't find any existing attributes of the same shape then
00612   // create a new one and insert it.
00613   if (!PA) {
00614     // Coallocate entries after the AttributeSetImpl itself.
00615     void *Mem = ::operator new(sizeof(AttributeSetImpl) +
00616                                sizeof(std::pair<unsigned, AttributeSetNode *>) *
00617                                    Attrs.size());
00618     PA = new (Mem) AttributeSetImpl(C, Attrs);
00619     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
00620   }
00621 
00622   // Return the AttributesList that we found or created.
00623   return AttributeSet(PA);
00624 }
00625 
00626 AttributeSet AttributeSet::get(LLVMContext &C,
00627                                ArrayRef<std::pair<unsigned, Attribute> > Attrs){
00628   // If there are no attributes then return a null AttributesList pointer.
00629   if (Attrs.empty())
00630     return AttributeSet();
00631 
00632 #ifndef NDEBUG
00633   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
00634     assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
00635            "Misordered Attributes list!");
00636     assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
00637            "Pointless attribute!");
00638   }
00639 #endif
00640 
00641   // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
00642   // list.
00643   SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
00644   for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
00645          E = Attrs.end(); I != E; ) {
00646     unsigned Index = I->first;
00647     SmallVector<Attribute, 4> AttrVec;
00648     while (I != E && I->first == Index) {
00649       AttrVec.push_back(I->second);
00650       ++I;
00651     }
00652 
00653     AttrPairVec.push_back(std::make_pair(Index,
00654                                          AttributeSetNode::get(C, AttrVec)));
00655   }
00656 
00657   return getImpl(C, AttrPairVec);
00658 }
00659 
00660 AttributeSet AttributeSet::get(LLVMContext &C,
00661                                ArrayRef<std::pair<unsigned,
00662                                                   AttributeSetNode*> > Attrs) {
00663   // If there are no attributes then return a null AttributesList pointer.
00664   if (Attrs.empty())
00665     return AttributeSet();
00666 
00667   return getImpl(C, Attrs);
00668 }
00669 
00670 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
00671                                const AttrBuilder &B) {
00672   if (!B.hasAttributes())
00673     return AttributeSet();
00674 
00675   // Add target-independent attributes.
00676   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
00677   for (Attribute::AttrKind Kind = Attribute::None;
00678        Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
00679     if (!B.contains(Kind))
00680       continue;
00681 
00682     if (Kind == Attribute::Alignment)
00683       Attrs.push_back(std::make_pair(Index, Attribute::
00684                                      getWithAlignment(C, B.getAlignment())));
00685     else if (Kind == Attribute::StackAlignment)
00686       Attrs.push_back(std::make_pair(Index, Attribute::
00687                               getWithStackAlignment(C, B.getStackAlignment())));
00688     else if (Kind == Attribute::Dereferenceable)
00689       Attrs.push_back(std::make_pair(Index,
00690                                      Attribute::getWithDereferenceableBytes(C,
00691                                        B.getDereferenceableBytes())));
00692     else if (Kind == Attribute::DereferenceableOrNull)
00693       Attrs.push_back(
00694           std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
00695                                     C, B.getDereferenceableOrNullBytes())));
00696     else
00697       Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
00698   }
00699 
00700   // Add target-dependent (string) attributes.
00701   for (const AttrBuilder::td_type &TDA : B.td_attrs())
00702     Attrs.push_back(
00703         std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
00704 
00705   return get(C, Attrs);
00706 }
00707 
00708 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
00709                                ArrayRef<Attribute::AttrKind> Kind) {
00710   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
00711   for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
00712          E = Kind.end(); I != E; ++I)
00713     Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
00714   return get(C, Attrs);
00715 }
00716 
00717 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
00718   if (Attrs.empty()) return AttributeSet();
00719   if (Attrs.size() == 1) return Attrs[0];
00720 
00721   SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
00722   AttributeSetImpl *A0 = Attrs[0].pImpl;
00723   if (A0)
00724     AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
00725   // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
00726   // ordered by index.  Because we know that each list in Attrs is ordered by
00727   // index we only need to merge each successive list in rather than doing a
00728   // full sort.
00729   for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
00730     AttributeSetImpl *AS = Attrs[I].pImpl;
00731     if (!AS) continue;
00732     SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
00733       ANVI = AttrNodeVec.begin(), ANVE;
00734     for (const AttributeSetImpl::IndexAttrPair
00735              *AI = AS->getNode(0),
00736              *AE = AS->getNode(AS->getNumAttributes());
00737          AI != AE; ++AI) {
00738       ANVE = AttrNodeVec.end();
00739       while (ANVI != ANVE && ANVI->first <= AI->first)
00740         ++ANVI;
00741       ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
00742     }
00743   }
00744 
00745   return getImpl(C, AttrNodeVec);
00746 }
00747 
00748 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
00749                                         Attribute::AttrKind Attr) const {
00750   if (hasAttribute(Index, Attr)) return *this;
00751   return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
00752 }
00753 
00754 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
00755                                         StringRef Kind) const {
00756   llvm::AttrBuilder B;
00757   B.addAttribute(Kind);
00758   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00759 }
00760 
00761 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
00762                                         StringRef Kind, StringRef Value) const {
00763   llvm::AttrBuilder B;
00764   B.addAttribute(Kind, Value);
00765   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00766 }
00767 
00768 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
00769                                          AttributeSet Attrs) const {
00770   if (!pImpl) return Attrs;
00771   if (!Attrs.pImpl) return *this;
00772 
00773 #ifndef NDEBUG
00774   // FIXME it is not obvious how this should work for alignment. For now, say
00775   // we can't change a known alignment.
00776   unsigned OldAlign = getParamAlignment(Index);
00777   unsigned NewAlign = Attrs.getParamAlignment(Index);
00778   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
00779          "Attempt to change alignment!");
00780 #endif
00781 
00782   // Add the attribute slots before the one we're trying to add.
00783   SmallVector<AttributeSet, 4> AttrSet;
00784   uint64_t NumAttrs = pImpl->getNumAttributes();
00785   AttributeSet AS;
00786   uint64_t LastIndex = 0;
00787   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
00788     if (getSlotIndex(I) >= Index) {
00789       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
00790       break;
00791     }
00792     LastIndex = I + 1;
00793     AttrSet.push_back(getSlotAttributes(I));
00794   }
00795 
00796   // Now add the attribute into the correct slot. There may already be an
00797   // AttributeSet there.
00798   AttrBuilder B(AS, Index);
00799 
00800   for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
00801     if (Attrs.getSlotIndex(I) == Index) {
00802       for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
00803              IE = Attrs.pImpl->end(I); II != IE; ++II)
00804         B.addAttribute(*II);
00805       break;
00806     }
00807 
00808   AttrSet.push_back(AttributeSet::get(C, Index, B));
00809 
00810   // Add the remaining attribute slots.
00811   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
00812     AttrSet.push_back(getSlotAttributes(I));
00813 
00814   return get(C, AttrSet);
00815 }
00816 
00817 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
00818                                            Attribute::AttrKind Attr) const {
00819   if (!hasAttribute(Index, Attr)) return *this;
00820   return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
00821 }
00822 
00823 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
00824                                             AttributeSet Attrs) const {
00825   if (!pImpl) return AttributeSet();
00826   if (!Attrs.pImpl) return *this;
00827 
00828   // FIXME it is not obvious how this should work for alignment.
00829   // For now, say we can't pass in alignment, which no current use does.
00830   assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
00831          "Attempt to change alignment!");
00832 
00833   // Add the attribute slots before the one we're trying to add.
00834   SmallVector<AttributeSet, 4> AttrSet;
00835   uint64_t NumAttrs = pImpl->getNumAttributes();
00836   AttributeSet AS;
00837   uint64_t LastIndex = 0;
00838   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
00839     if (getSlotIndex(I) >= Index) {
00840       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
00841       break;
00842     }
00843     LastIndex = I + 1;
00844     AttrSet.push_back(getSlotAttributes(I));
00845   }
00846 
00847   // Now remove the attribute from the correct slot. There may already be an
00848   // AttributeSet there.
00849   AttrBuilder B(AS, Index);
00850 
00851   for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
00852     if (Attrs.getSlotIndex(I) == Index) {
00853       B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
00854       break;
00855     }
00856 
00857   AttrSet.push_back(AttributeSet::get(C, Index, B));
00858 
00859   // Add the remaining attribute slots.
00860   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
00861     AttrSet.push_back(getSlotAttributes(I));
00862 
00863   return get(C, AttrSet);
00864 }
00865 
00866 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
00867                                             const AttrBuilder &Attrs) const {
00868   if (!pImpl) return AttributeSet();
00869 
00870   // FIXME it is not obvious how this should work for alignment.
00871   // For now, say we can't pass in alignment, which no current use does.
00872   assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
00873 
00874   // Add the attribute slots before the one we're trying to add.
00875   SmallVector<AttributeSet, 4> AttrSet;
00876   uint64_t NumAttrs = pImpl->getNumAttributes();
00877   AttributeSet AS;
00878   uint64_t LastIndex = 0;
00879   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
00880     if (getSlotIndex(I) >= Index) {
00881       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
00882       break;
00883     }
00884     LastIndex = I + 1;
00885     AttrSet.push_back(getSlotAttributes(I));
00886   }
00887 
00888   // Now remove the attribute from the correct slot. There may already be an
00889   // AttributeSet there.
00890   AttrBuilder B(AS, Index);
00891   B.remove(Attrs);
00892 
00893   AttrSet.push_back(AttributeSet::get(C, Index, B));
00894 
00895   // Add the remaining attribute slots.
00896   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
00897     AttrSet.push_back(getSlotAttributes(I));
00898 
00899   return get(C, AttrSet);
00900 }
00901 
00902 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
00903                                                   uint64_t Bytes) const {
00904   llvm::AttrBuilder B;
00905   B.addDereferenceableAttr(Bytes);
00906   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00907 }
00908 
00909 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
00910                                                         unsigned Index,
00911                                                         uint64_t Bytes) const {
00912   llvm::AttrBuilder B;
00913   B.addDereferenceableOrNullAttr(Bytes);
00914   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00915 }
00916 
00917 //===----------------------------------------------------------------------===//
00918 // AttributeSet Accessor Methods
00919 //===----------------------------------------------------------------------===//
00920 
00921 LLVMContext &AttributeSet::getContext() const {
00922   return pImpl->getContext();
00923 }
00924 
00925 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
00926   return pImpl && hasAttributes(Index) ?
00927     AttributeSet::get(pImpl->getContext(),
00928                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
00929                         std::make_pair(Index, getAttributes(Index)))) :
00930     AttributeSet();
00931 }
00932 
00933 AttributeSet AttributeSet::getRetAttributes() const {
00934   return pImpl && hasAttributes(ReturnIndex) ?
00935     AttributeSet::get(pImpl->getContext(),
00936                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
00937                         std::make_pair(ReturnIndex,
00938                                        getAttributes(ReturnIndex)))) :
00939     AttributeSet();
00940 }
00941 
00942 AttributeSet AttributeSet::getFnAttributes() const {
00943   return pImpl && hasAttributes(FunctionIndex) ?
00944     AttributeSet::get(pImpl->getContext(),
00945                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
00946                         std::make_pair(FunctionIndex,
00947                                        getAttributes(FunctionIndex)))) :
00948     AttributeSet();
00949 }
00950 
00951 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
00952   AttributeSetNode *ASN = getAttributes(Index);
00953   return ASN ? ASN->hasAttribute(Kind) : false;
00954 }
00955 
00956 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
00957   AttributeSetNode *ASN = getAttributes(Index);
00958   return ASN ? ASN->hasAttribute(Kind) : false;
00959 }
00960 
00961 bool AttributeSet::hasAttributes(unsigned Index) const {
00962   AttributeSetNode *ASN = getAttributes(Index);
00963   return ASN ? ASN->hasAttributes() : false;
00964 }
00965 
00966 /// \brief Return true if the specified attribute is set for at least one
00967 /// parameter or for the return value.
00968 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
00969   if (!pImpl) return false;
00970 
00971   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
00972     for (AttributeSetImpl::iterator II = pImpl->begin(I),
00973            IE = pImpl->end(I); II != IE; ++II)
00974       if (II->hasAttribute(Attr))
00975         return true;
00976 
00977   return false;
00978 }
00979 
00980 Attribute AttributeSet::getAttribute(unsigned Index,
00981                                      Attribute::AttrKind Kind) const {
00982   AttributeSetNode *ASN = getAttributes(Index);
00983   return ASN ? ASN->getAttribute(Kind) : Attribute();
00984 }
00985 
00986 Attribute AttributeSet::getAttribute(unsigned Index,
00987                                      StringRef Kind) const {
00988   AttributeSetNode *ASN = getAttributes(Index);
00989   return ASN ? ASN->getAttribute(Kind) : Attribute();
00990 }
00991 
00992 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
00993   AttributeSetNode *ASN = getAttributes(Index);
00994   return ASN ? ASN->getAlignment() : 0;
00995 }
00996 
00997 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
00998   AttributeSetNode *ASN = getAttributes(Index);
00999   return ASN ? ASN->getStackAlignment() : 0;
01000 }
01001 
01002 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
01003   AttributeSetNode *ASN = getAttributes(Index);
01004   return ASN ? ASN->getDereferenceableBytes() : 0;
01005 }
01006 
01007 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
01008   AttributeSetNode *ASN = getAttributes(Index);
01009   return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
01010 }
01011 
01012 std::string AttributeSet::getAsString(unsigned Index,
01013                                       bool InAttrGrp) const {
01014   AttributeSetNode *ASN = getAttributes(Index);
01015   return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
01016 }
01017 
01018 /// \brief The attributes for the specified index are returned.
01019 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
01020   if (!pImpl) return nullptr;
01021 
01022   // Loop through to find the attribute node we want.
01023   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
01024     if (pImpl->getSlotIndex(I) == Index)
01025       return pImpl->getSlotNode(I);
01026 
01027   return nullptr;
01028 }
01029 
01030 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
01031   if (!pImpl)
01032     return ArrayRef<Attribute>().begin();
01033   return pImpl->begin(Slot);
01034 }
01035 
01036 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
01037   if (!pImpl)
01038     return ArrayRef<Attribute>().end();
01039   return pImpl->end(Slot);
01040 }
01041 
01042 //===----------------------------------------------------------------------===//
01043 // AttributeSet Introspection Methods
01044 //===----------------------------------------------------------------------===//
01045 
01046 /// \brief Return the number of slots used in this attribute list.  This is the
01047 /// number of arguments that have an attribute set on them (including the
01048 /// function itself).
01049 unsigned AttributeSet::getNumSlots() const {
01050   return pImpl ? pImpl->getNumAttributes() : 0;
01051 }
01052 
01053 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
01054   assert(pImpl && Slot < pImpl->getNumAttributes() &&
01055          "Slot # out of range!");
01056   return pImpl->getSlotIndex(Slot);
01057 }
01058 
01059 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
01060   assert(pImpl && Slot < pImpl->getNumAttributes() &&
01061          "Slot # out of range!");
01062   return pImpl->getSlotAttributes(Slot);
01063 }
01064 
01065 uint64_t AttributeSet::Raw(unsigned Index) const {
01066   // FIXME: Remove this.
01067   return pImpl ? pImpl->Raw(Index) : 0;
01068 }
01069 
01070 void AttributeSet::dump() const {
01071   dbgs() << "PAL[\n";
01072 
01073   for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
01074     uint64_t Index = getSlotIndex(i);
01075     dbgs() << "  { ";
01076     if (Index == ~0U)
01077       dbgs() << "~0U";
01078     else
01079       dbgs() << Index;
01080     dbgs() << " => " << getAsString(Index) << " }\n";
01081   }
01082 
01083   dbgs() << "]\n";
01084 }
01085 
01086 //===----------------------------------------------------------------------===//
01087 // AttrBuilder Method Implementations
01088 //===----------------------------------------------------------------------===//
01089 
01090 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
01091     : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
01092       DerefOrNullBytes(0) {
01093   AttributeSetImpl *pImpl = AS.pImpl;
01094   if (!pImpl) return;
01095 
01096   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
01097     if (pImpl->getSlotIndex(I) != Index) continue;
01098 
01099     for (AttributeSetImpl::iterator II = pImpl->begin(I),
01100            IE = pImpl->end(I); II != IE; ++II)
01101       addAttribute(*II);
01102 
01103     break;
01104   }
01105 }
01106 
01107 void AttrBuilder::clear() {
01108   Attrs.reset();
01109   Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
01110 }
01111 
01112 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
01113   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
01114   assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
01115          Val != Attribute::Dereferenceable &&
01116          "Adding integer attribute without adding a value!");
01117   Attrs[Val] = true;
01118   return *this;
01119 }
01120 
01121 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
01122   if (Attr.isStringAttribute()) {
01123     addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
01124     return *this;
01125   }
01126 
01127   Attribute::AttrKind Kind = Attr.getKindAsEnum();
01128   Attrs[Kind] = true;
01129 
01130   if (Kind == Attribute::Alignment)
01131     Alignment = Attr.getAlignment();
01132   else if (Kind == Attribute::StackAlignment)
01133     StackAlignment = Attr.getStackAlignment();
01134   else if (Kind == Attribute::Dereferenceable)
01135     DerefBytes = Attr.getDereferenceableBytes();
01136   else if (Kind == Attribute::DereferenceableOrNull)
01137     DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
01138   return *this;
01139 }
01140 
01141 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
01142   TargetDepAttrs[A] = V;
01143   return *this;
01144 }
01145 
01146 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
01147   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
01148   Attrs[Val] = false;
01149 
01150   if (Val == Attribute::Alignment)
01151     Alignment = 0;
01152   else if (Val == Attribute::StackAlignment)
01153     StackAlignment = 0;
01154   else if (Val == Attribute::Dereferenceable)
01155     DerefBytes = 0;
01156   else if (Val == Attribute::DereferenceableOrNull)
01157     DerefOrNullBytes = 0;
01158 
01159   return *this;
01160 }
01161 
01162 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
01163   unsigned Slot = ~0U;
01164   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
01165     if (A.getSlotIndex(I) == Index) {
01166       Slot = I;
01167       break;
01168     }
01169 
01170   assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
01171 
01172   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
01173     Attribute Attr = *I;
01174     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
01175       Attribute::AttrKind Kind = I->getKindAsEnum();
01176       Attrs[Kind] = false;
01177 
01178       if (Kind == Attribute::Alignment)
01179         Alignment = 0;
01180       else if (Kind == Attribute::StackAlignment)
01181         StackAlignment = 0;
01182       else if (Kind == Attribute::Dereferenceable)
01183         DerefBytes = 0;
01184       else if (Kind == Attribute::DereferenceableOrNull)
01185         DerefOrNullBytes = 0;
01186     } else {
01187       assert(Attr.isStringAttribute() && "Invalid attribute type!");
01188       std::map<std::string, std::string>::iterator
01189         Iter = TargetDepAttrs.find(Attr.getKindAsString());
01190       if (Iter != TargetDepAttrs.end())
01191         TargetDepAttrs.erase(Iter);
01192     }
01193   }
01194 
01195   return *this;
01196 }
01197 
01198 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
01199   std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
01200   if (I != TargetDepAttrs.end())
01201     TargetDepAttrs.erase(I);
01202   return *this;
01203 }
01204 
01205 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
01206   if (Align == 0) return *this;
01207 
01208   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
01209   assert(Align <= 0x40000000 && "Alignment too large.");
01210 
01211   Attrs[Attribute::Alignment] = true;
01212   Alignment = Align;
01213   return *this;
01214 }
01215 
01216 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
01217   // Default alignment, allow the target to define how to align it.
01218   if (Align == 0) return *this;
01219 
01220   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
01221   assert(Align <= 0x100 && "Alignment too large.");
01222 
01223   Attrs[Attribute::StackAlignment] = true;
01224   StackAlignment = Align;
01225   return *this;
01226 }
01227 
01228 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
01229   if (Bytes == 0) return *this;
01230 
01231   Attrs[Attribute::Dereferenceable] = true;
01232   DerefBytes = Bytes;
01233   return *this;
01234 }
01235 
01236 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
01237   if (Bytes == 0)
01238     return *this;
01239 
01240   Attrs[Attribute::DereferenceableOrNull] = true;
01241   DerefOrNullBytes = Bytes;
01242   return *this;
01243 }
01244 
01245 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
01246   // FIXME: What if both have alignments, but they don't match?!
01247   if (!Alignment)
01248     Alignment = B.Alignment;
01249 
01250   if (!StackAlignment)
01251     StackAlignment = B.StackAlignment;
01252 
01253   if (!DerefBytes)
01254     DerefBytes = B.DerefBytes;
01255 
01256   if (!DerefOrNullBytes)
01257     DerefOrNullBytes = B.DerefOrNullBytes;
01258 
01259   Attrs |= B.Attrs;
01260 
01261   for (auto I : B.td_attrs())
01262     TargetDepAttrs[I.first] = I.second;
01263 
01264   return *this;
01265 }
01266 
01267 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
01268   // FIXME: What if both have alignments, but they don't match?!
01269   if (B.Alignment)
01270     Alignment = 0;
01271 
01272   if (B.StackAlignment)
01273     StackAlignment = 0;
01274 
01275   if (B.DerefBytes)
01276     DerefBytes = 0;
01277 
01278   if (B.DerefOrNullBytes)
01279     DerefOrNullBytes = 0;
01280 
01281   Attrs &= ~B.Attrs;
01282 
01283   for (auto I : B.td_attrs())
01284     TargetDepAttrs.erase(I.first);
01285 
01286   return *this;
01287 }
01288 
01289 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
01290   // First check if any of the target independent attributes overlap.
01291   if ((Attrs & B.Attrs).any())
01292     return true;
01293 
01294   // Then check if any target dependent ones do.
01295   for (auto I : td_attrs())
01296     if (B.contains(I.first))
01297       return true;
01298 
01299   return false;
01300 }
01301 
01302 bool AttrBuilder::contains(StringRef A) const {
01303   return TargetDepAttrs.find(A) != TargetDepAttrs.end();
01304 }
01305 
01306 bool AttrBuilder::hasAttributes() const {
01307   return !Attrs.none() || !TargetDepAttrs.empty();
01308 }
01309 
01310 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
01311   unsigned Slot = ~0U;
01312   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
01313     if (A.getSlotIndex(I) == Index) {
01314       Slot = I;
01315       break;
01316     }
01317 
01318   assert(Slot != ~0U && "Couldn't find the index!");
01319 
01320   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
01321        I != E; ++I) {
01322     Attribute Attr = *I;
01323     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
01324       if (Attrs[I->getKindAsEnum()])
01325         return true;
01326     } else {
01327       assert(Attr.isStringAttribute() && "Invalid attribute kind!");
01328       return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
01329     }
01330   }
01331 
01332   return false;
01333 }
01334 
01335 bool AttrBuilder::hasAlignmentAttr() const {
01336   return Alignment != 0;
01337 }
01338 
01339 bool AttrBuilder::operator==(const AttrBuilder &B) {
01340   if (Attrs != B.Attrs)
01341     return false;
01342 
01343   for (td_const_iterator I = TargetDepAttrs.begin(),
01344          E = TargetDepAttrs.end(); I != E; ++I)
01345     if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
01346       return false;
01347 
01348   return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
01349          DerefBytes == B.DerefBytes;
01350 }
01351 
01352 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
01353   // FIXME: Remove this in 4.0.
01354   if (!Val) return *this;
01355 
01356   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
01357        I = Attribute::AttrKind(I + 1)) {
01358     if (I == Attribute::Dereferenceable ||
01359         I == Attribute::DereferenceableOrNull)
01360       continue;
01361     if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
01362       Attrs[I] = true;
01363  
01364       if (I == Attribute::Alignment)
01365         Alignment = 1ULL << ((A >> 16) - 1);
01366       else if (I == Attribute::StackAlignment)
01367         StackAlignment = 1ULL << ((A >> 26)-1);
01368     }
01369   }
01370  
01371   return *this;
01372 }
01373 
01374 //===----------------------------------------------------------------------===//
01375 // AttributeFuncs Function Defintions
01376 //===----------------------------------------------------------------------===//
01377 
01378 /// \brief Which attributes cannot be applied to a type.
01379 AttrBuilder AttributeFuncs::typeIncompatible(const Type *Ty) {
01380   AttrBuilder Incompatible;
01381 
01382   if (!Ty->isIntegerTy())
01383     // Attribute that only apply to integers.
01384     Incompatible.addAttribute(Attribute::SExt)
01385       .addAttribute(Attribute::ZExt);
01386 
01387   if (!Ty->isPointerTy())
01388     // Attribute that only apply to pointers.
01389     Incompatible.addAttribute(Attribute::ByVal)
01390       .addAttribute(Attribute::Nest)
01391       .addAttribute(Attribute::NoAlias)
01392       .addAttribute(Attribute::NoCapture)
01393       .addAttribute(Attribute::NonNull)
01394       .addDereferenceableAttr(1) // the int here is ignored
01395       .addDereferenceableOrNullAttr(1) // the int here is ignored
01396       .addAttribute(Attribute::ReadNone)
01397       .addAttribute(Attribute::ReadOnly)
01398       .addAttribute(Attribute::StructRet)
01399       .addAttribute(Attribute::InAlloca);
01400 
01401   return Incompatible;
01402 }