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