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 uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
00536   for (iterator I = begin(), E = end(); I != E; ++I)
00537     if (I->hasAttribute(Attribute::DereferenceableOrNull))
00538       return I->getDereferenceableOrNullBytes();
00539   return 0;
00540 }
00541 
00542 std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
00543   std::string Str;
00544   for (iterator I = begin(), E = end(); I != E; ++I) {
00545     if (I != begin())
00546       Str += ' ';
00547     Str += I->getAsString(InAttrGrp);
00548   }
00549   return Str;
00550 }
00551 
00552 //===----------------------------------------------------------------------===//
00553 // AttributeSetImpl Definition
00554 //===----------------------------------------------------------------------===//
00555 
00556 uint64_t AttributeSetImpl::Raw(unsigned Index) const {
00557   for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
00558     if (getSlotIndex(I) != Index) continue;
00559     const AttributeSetNode *ASN = getSlotNode(I);
00560     uint64_t Mask = 0;
00561 
00562     for (AttributeSetNode::iterator II = ASN->begin(),
00563            IE = ASN->end(); II != IE; ++II) {
00564       Attribute Attr = *II;
00565 
00566       // This cannot handle string attributes.
00567       if (Attr.isStringAttribute()) continue;
00568 
00569       Attribute::AttrKind Kind = Attr.getKindAsEnum();
00570 
00571       if (Kind == Attribute::Alignment)
00572         Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
00573       else if (Kind == Attribute::StackAlignment)
00574         Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
00575       else if (Kind == Attribute::Dereferenceable)
00576         llvm_unreachable("dereferenceable not supported in bit mask");
00577       else
00578         Mask |= AttributeImpl::getAttrMask(Kind);
00579     }
00580 
00581     return Mask;
00582   }
00583 
00584   return 0;
00585 }
00586 
00587 void AttributeSetImpl::dump() const {
00588   AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
00589 }
00590 
00591 //===----------------------------------------------------------------------===//
00592 // AttributeSet Construction and Mutation Methods
00593 //===----------------------------------------------------------------------===//
00594 
00595 AttributeSet
00596 AttributeSet::getImpl(LLVMContext &C,
00597                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
00598   LLVMContextImpl *pImpl = C.pImpl;
00599   FoldingSetNodeID ID;
00600   AttributeSetImpl::Profile(ID, Attrs);
00601 
00602   void *InsertPoint;
00603   AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
00604 
00605   // If we didn't find any existing attributes of the same shape then
00606   // create a new one and insert it.
00607   if (!PA) {
00608     // Coallocate entries after the AttributeSetImpl itself.
00609     void *Mem = ::operator new(sizeof(AttributeSetImpl) +
00610                                sizeof(std::pair<unsigned, AttributeSetNode *>) *
00611                                    Attrs.size());
00612     PA = new (Mem) AttributeSetImpl(C, Attrs);
00613     pImpl->AttrsLists.InsertNode(PA, InsertPoint);
00614   }
00615 
00616   // Return the AttributesList that we found or created.
00617   return AttributeSet(PA);
00618 }
00619 
00620 AttributeSet AttributeSet::get(LLVMContext &C,
00621                                ArrayRef<std::pair<unsigned, Attribute> > Attrs){
00622   // If there are no attributes then return a null AttributesList pointer.
00623   if (Attrs.empty())
00624     return AttributeSet();
00625 
00626 #ifndef NDEBUG
00627   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
00628     assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
00629            "Misordered Attributes list!");
00630     assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
00631            "Pointless attribute!");
00632   }
00633 #endif
00634 
00635   // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
00636   // list.
00637   SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
00638   for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
00639          E = Attrs.end(); I != E; ) {
00640     unsigned Index = I->first;
00641     SmallVector<Attribute, 4> AttrVec;
00642     while (I != E && I->first == Index) {
00643       AttrVec.push_back(I->second);
00644       ++I;
00645     }
00646 
00647     AttrPairVec.push_back(std::make_pair(Index,
00648                                          AttributeSetNode::get(C, AttrVec)));
00649   }
00650 
00651   return getImpl(C, AttrPairVec);
00652 }
00653 
00654 AttributeSet AttributeSet::get(LLVMContext &C,
00655                                ArrayRef<std::pair<unsigned,
00656                                                   AttributeSetNode*> > Attrs) {
00657   // If there are no attributes then return a null AttributesList pointer.
00658   if (Attrs.empty())
00659     return AttributeSet();
00660 
00661   return getImpl(C, Attrs);
00662 }
00663 
00664 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
00665                                const AttrBuilder &B) {
00666   if (!B.hasAttributes())
00667     return AttributeSet();
00668 
00669   // Add target-independent attributes.
00670   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
00671   for (Attribute::AttrKind Kind = Attribute::None;
00672        Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
00673     if (!B.contains(Kind))
00674       continue;
00675 
00676     if (Kind == Attribute::Alignment)
00677       Attrs.push_back(std::make_pair(Index, Attribute::
00678                                      getWithAlignment(C, B.getAlignment())));
00679     else if (Kind == Attribute::StackAlignment)
00680       Attrs.push_back(std::make_pair(Index, Attribute::
00681                               getWithStackAlignment(C, B.getStackAlignment())));
00682     else if (Kind == Attribute::Dereferenceable)
00683       Attrs.push_back(std::make_pair(Index,
00684                                      Attribute::getWithDereferenceableBytes(C,
00685                                        B.getDereferenceableBytes())));
00686     else if (Kind == Attribute::DereferenceableOrNull)
00687       Attrs.push_back(
00688           std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
00689                                     C, B.getDereferenceableOrNullBytes())));
00690     else
00691       Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
00692   }
00693 
00694   // Add target-dependent (string) attributes.
00695   for (const AttrBuilder::td_type &TDA : B.td_attrs())
00696     Attrs.push_back(
00697         std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
00698 
00699   return get(C, Attrs);
00700 }
00701 
00702 AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
00703                                ArrayRef<Attribute::AttrKind> Kind) {
00704   SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
00705   for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
00706          E = Kind.end(); I != E; ++I)
00707     Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
00708   return get(C, Attrs);
00709 }
00710 
00711 AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
00712   if (Attrs.empty()) return AttributeSet();
00713   if (Attrs.size() == 1) return Attrs[0];
00714 
00715   SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
00716   AttributeSetImpl *A0 = Attrs[0].pImpl;
00717   if (A0)
00718     AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
00719   // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
00720   // ordered by index.  Because we know that each list in Attrs is ordered by
00721   // index we only need to merge each successive list in rather than doing a
00722   // full sort.
00723   for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
00724     AttributeSetImpl *AS = Attrs[I].pImpl;
00725     if (!AS) continue;
00726     SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
00727       ANVI = AttrNodeVec.begin(), ANVE;
00728     for (const AttributeSetImpl::IndexAttrPair
00729              *AI = AS->getNode(0),
00730              *AE = AS->getNode(AS->getNumAttributes());
00731          AI != AE; ++AI) {
00732       ANVE = AttrNodeVec.end();
00733       while (ANVI != ANVE && ANVI->first <= AI->first)
00734         ++ANVI;
00735       ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
00736     }
00737   }
00738 
00739   return getImpl(C, AttrNodeVec);
00740 }
00741 
00742 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
00743                                         Attribute::AttrKind Attr) const {
00744   if (hasAttribute(Index, Attr)) return *this;
00745   return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
00746 }
00747 
00748 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
00749                                         StringRef Kind) const {
00750   llvm::AttrBuilder B;
00751   B.addAttribute(Kind);
00752   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00753 }
00754 
00755 AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
00756                                         StringRef Kind, StringRef Value) const {
00757   llvm::AttrBuilder B;
00758   B.addAttribute(Kind, Value);
00759   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00760 }
00761 
00762 AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
00763                                          AttributeSet Attrs) const {
00764   if (!pImpl) return Attrs;
00765   if (!Attrs.pImpl) return *this;
00766 
00767 #ifndef NDEBUG
00768   // FIXME it is not obvious how this should work for alignment. For now, say
00769   // we can't change a known alignment.
00770   unsigned OldAlign = getParamAlignment(Index);
00771   unsigned NewAlign = Attrs.getParamAlignment(Index);
00772   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
00773          "Attempt to change alignment!");
00774 #endif
00775 
00776   // Add the attribute slots before the one we're trying to add.
00777   SmallVector<AttributeSet, 4> AttrSet;
00778   uint64_t NumAttrs = pImpl->getNumAttributes();
00779   AttributeSet AS;
00780   uint64_t LastIndex = 0;
00781   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
00782     if (getSlotIndex(I) >= Index) {
00783       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
00784       break;
00785     }
00786     LastIndex = I + 1;
00787     AttrSet.push_back(getSlotAttributes(I));
00788   }
00789 
00790   // Now add the attribute into the correct slot. There may already be an
00791   // AttributeSet there.
00792   AttrBuilder B(AS, Index);
00793 
00794   for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
00795     if (Attrs.getSlotIndex(I) == Index) {
00796       for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
00797              IE = Attrs.pImpl->end(I); II != IE; ++II)
00798         B.addAttribute(*II);
00799       break;
00800     }
00801 
00802   AttrSet.push_back(AttributeSet::get(C, Index, B));
00803 
00804   // Add the remaining attribute slots.
00805   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
00806     AttrSet.push_back(getSlotAttributes(I));
00807 
00808   return get(C, AttrSet);
00809 }
00810 
00811 AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
00812                                            Attribute::AttrKind Attr) const {
00813   if (!hasAttribute(Index, Attr)) return *this;
00814   return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
00815 }
00816 
00817 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
00818                                             AttributeSet Attrs) const {
00819   if (!pImpl) return AttributeSet();
00820   if (!Attrs.pImpl) return *this;
00821 
00822   // FIXME it is not obvious how this should work for alignment.
00823   // For now, say we can't pass in alignment, which no current use does.
00824   assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
00825          "Attempt to change alignment!");
00826 
00827   // Add the attribute slots before the one we're trying to add.
00828   SmallVector<AttributeSet, 4> AttrSet;
00829   uint64_t NumAttrs = pImpl->getNumAttributes();
00830   AttributeSet AS;
00831   uint64_t LastIndex = 0;
00832   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
00833     if (getSlotIndex(I) >= Index) {
00834       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
00835       break;
00836     }
00837     LastIndex = I + 1;
00838     AttrSet.push_back(getSlotAttributes(I));
00839   }
00840 
00841   // Now remove the attribute from the correct slot. There may already be an
00842   // AttributeSet there.
00843   AttrBuilder B(AS, Index);
00844 
00845   for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
00846     if (Attrs.getSlotIndex(I) == Index) {
00847       B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
00848       break;
00849     }
00850 
00851   AttrSet.push_back(AttributeSet::get(C, Index, B));
00852 
00853   // Add the remaining attribute slots.
00854   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
00855     AttrSet.push_back(getSlotAttributes(I));
00856 
00857   return get(C, AttrSet);
00858 }
00859 
00860 AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
00861                                             const AttrBuilder &Attrs) const {
00862   if (!pImpl) return AttributeSet();
00863 
00864   // FIXME it is not obvious how this should work for alignment.
00865   // For now, say we can't pass in alignment, which no current use does.
00866   assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
00867 
00868   // Add the attribute slots before the one we're trying to add.
00869   SmallVector<AttributeSet, 4> AttrSet;
00870   uint64_t NumAttrs = pImpl->getNumAttributes();
00871   AttributeSet AS;
00872   uint64_t LastIndex = 0;
00873   for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
00874     if (getSlotIndex(I) >= Index) {
00875       if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
00876       break;
00877     }
00878     LastIndex = I + 1;
00879     AttrSet.push_back(getSlotAttributes(I));
00880   }
00881 
00882   // Now remove the attribute from the correct slot. There may already be an
00883   // AttributeSet there.
00884   AttrBuilder B(AS, Index);
00885   B.remove(Attrs);
00886 
00887   AttrSet.push_back(AttributeSet::get(C, Index, B));
00888 
00889   // Add the remaining attribute slots.
00890   for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
00891     AttrSet.push_back(getSlotAttributes(I));
00892 
00893   return get(C, AttrSet);
00894 }
00895 
00896 AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
00897                                                   uint64_t Bytes) const {
00898   llvm::AttrBuilder B;
00899   B.addDereferenceableAttr(Bytes);
00900   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00901 }
00902 
00903 AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
00904                                                         unsigned Index,
00905                                                         uint64_t Bytes) const {
00906   llvm::AttrBuilder B;
00907   B.addDereferenceableOrNullAttr(Bytes);
00908   return addAttributes(C, Index, AttributeSet::get(C, Index, B));
00909 }
00910 
00911 //===----------------------------------------------------------------------===//
00912 // AttributeSet Accessor Methods
00913 //===----------------------------------------------------------------------===//
00914 
00915 LLVMContext &AttributeSet::getContext() const {
00916   return pImpl->getContext();
00917 }
00918 
00919 AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
00920   return pImpl && hasAttributes(Index) ?
00921     AttributeSet::get(pImpl->getContext(),
00922                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
00923                         std::make_pair(Index, getAttributes(Index)))) :
00924     AttributeSet();
00925 }
00926 
00927 AttributeSet AttributeSet::getRetAttributes() const {
00928   return pImpl && hasAttributes(ReturnIndex) ?
00929     AttributeSet::get(pImpl->getContext(),
00930                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
00931                         std::make_pair(ReturnIndex,
00932                                        getAttributes(ReturnIndex)))) :
00933     AttributeSet();
00934 }
00935 
00936 AttributeSet AttributeSet::getFnAttributes() const {
00937   return pImpl && hasAttributes(FunctionIndex) ?
00938     AttributeSet::get(pImpl->getContext(),
00939                       ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
00940                         std::make_pair(FunctionIndex,
00941                                        getAttributes(FunctionIndex)))) :
00942     AttributeSet();
00943 }
00944 
00945 bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
00946   AttributeSetNode *ASN = getAttributes(Index);
00947   return ASN ? ASN->hasAttribute(Kind) : false;
00948 }
00949 
00950 bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
00951   AttributeSetNode *ASN = getAttributes(Index);
00952   return ASN ? ASN->hasAttribute(Kind) : false;
00953 }
00954 
00955 bool AttributeSet::hasAttributes(unsigned Index) const {
00956   AttributeSetNode *ASN = getAttributes(Index);
00957   return ASN ? ASN->hasAttributes() : false;
00958 }
00959 
00960 /// \brief Return true if the specified attribute is set for at least one
00961 /// parameter or for the return value.
00962 bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
00963   if (!pImpl) return false;
00964 
00965   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
00966     for (AttributeSetImpl::iterator II = pImpl->begin(I),
00967            IE = pImpl->end(I); II != IE; ++II)
00968       if (II->hasAttribute(Attr))
00969         return true;
00970 
00971   return false;
00972 }
00973 
00974 Attribute AttributeSet::getAttribute(unsigned Index,
00975                                      Attribute::AttrKind Kind) const {
00976   AttributeSetNode *ASN = getAttributes(Index);
00977   return ASN ? ASN->getAttribute(Kind) : Attribute();
00978 }
00979 
00980 Attribute AttributeSet::getAttribute(unsigned Index,
00981                                      StringRef Kind) const {
00982   AttributeSetNode *ASN = getAttributes(Index);
00983   return ASN ? ASN->getAttribute(Kind) : Attribute();
00984 }
00985 
00986 unsigned AttributeSet::getParamAlignment(unsigned Index) const {
00987   AttributeSetNode *ASN = getAttributes(Index);
00988   return ASN ? ASN->getAlignment() : 0;
00989 }
00990 
00991 unsigned AttributeSet::getStackAlignment(unsigned Index) const {
00992   AttributeSetNode *ASN = getAttributes(Index);
00993   return ASN ? ASN->getStackAlignment() : 0;
00994 }
00995 
00996 uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
00997   AttributeSetNode *ASN = getAttributes(Index);
00998   return ASN ? ASN->getDereferenceableBytes() : 0;
00999 }
01000 
01001 uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
01002   AttributeSetNode *ASN = getAttributes(Index);
01003   return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
01004 }
01005 
01006 std::string AttributeSet::getAsString(unsigned Index,
01007                                       bool InAttrGrp) const {
01008   AttributeSetNode *ASN = getAttributes(Index);
01009   return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
01010 }
01011 
01012 /// \brief The attributes for the specified index are returned.
01013 AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
01014   if (!pImpl) return nullptr;
01015 
01016   // Loop through to find the attribute node we want.
01017   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
01018     if (pImpl->getSlotIndex(I) == Index)
01019       return pImpl->getSlotNode(I);
01020 
01021   return nullptr;
01022 }
01023 
01024 AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
01025   if (!pImpl)
01026     return ArrayRef<Attribute>().begin();
01027   return pImpl->begin(Slot);
01028 }
01029 
01030 AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
01031   if (!pImpl)
01032     return ArrayRef<Attribute>().end();
01033   return pImpl->end(Slot);
01034 }
01035 
01036 //===----------------------------------------------------------------------===//
01037 // AttributeSet Introspection Methods
01038 //===----------------------------------------------------------------------===//
01039 
01040 /// \brief Return the number of slots used in this attribute list.  This is the
01041 /// number of arguments that have an attribute set on them (including the
01042 /// function itself).
01043 unsigned AttributeSet::getNumSlots() const {
01044   return pImpl ? pImpl->getNumAttributes() : 0;
01045 }
01046 
01047 unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
01048   assert(pImpl && Slot < pImpl->getNumAttributes() &&
01049          "Slot # out of range!");
01050   return pImpl->getSlotIndex(Slot);
01051 }
01052 
01053 AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
01054   assert(pImpl && Slot < pImpl->getNumAttributes() &&
01055          "Slot # out of range!");
01056   return pImpl->getSlotAttributes(Slot);
01057 }
01058 
01059 uint64_t AttributeSet::Raw(unsigned Index) const {
01060   // FIXME: Remove this.
01061   return pImpl ? pImpl->Raw(Index) : 0;
01062 }
01063 
01064 void AttributeSet::dump() const {
01065   dbgs() << "PAL[\n";
01066 
01067   for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
01068     uint64_t Index = getSlotIndex(i);
01069     dbgs() << "  { ";
01070     if (Index == ~0U)
01071       dbgs() << "~0U";
01072     else
01073       dbgs() << Index;
01074     dbgs() << " => " << getAsString(Index) << " }\n";
01075   }
01076 
01077   dbgs() << "]\n";
01078 }
01079 
01080 //===----------------------------------------------------------------------===//
01081 // AttrBuilder Method Implementations
01082 //===----------------------------------------------------------------------===//
01083 
01084 AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
01085     : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
01086       DerefOrNullBytes(0) {
01087   AttributeSetImpl *pImpl = AS.pImpl;
01088   if (!pImpl) return;
01089 
01090   for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
01091     if (pImpl->getSlotIndex(I) != Index) continue;
01092 
01093     for (AttributeSetImpl::iterator II = pImpl->begin(I),
01094            IE = pImpl->end(I); II != IE; ++II)
01095       addAttribute(*II);
01096 
01097     break;
01098   }
01099 }
01100 
01101 void AttrBuilder::clear() {
01102   Attrs.reset();
01103   Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
01104 }
01105 
01106 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
01107   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
01108   assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
01109          Val != Attribute::Dereferenceable &&
01110          "Adding integer attribute without adding a value!");
01111   Attrs[Val] = true;
01112   return *this;
01113 }
01114 
01115 AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
01116   if (Attr.isStringAttribute()) {
01117     addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
01118     return *this;
01119   }
01120 
01121   Attribute::AttrKind Kind = Attr.getKindAsEnum();
01122   Attrs[Kind] = true;
01123 
01124   if (Kind == Attribute::Alignment)
01125     Alignment = Attr.getAlignment();
01126   else if (Kind == Attribute::StackAlignment)
01127     StackAlignment = Attr.getStackAlignment();
01128   else if (Kind == Attribute::Dereferenceable)
01129     DerefBytes = Attr.getDereferenceableBytes();
01130   else if (Kind == Attribute::DereferenceableOrNull)
01131     DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
01132   return *this;
01133 }
01134 
01135 AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
01136   TargetDepAttrs[A] = V;
01137   return *this;
01138 }
01139 
01140 AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
01141   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
01142   Attrs[Val] = false;
01143 
01144   if (Val == Attribute::Alignment)
01145     Alignment = 0;
01146   else if (Val == Attribute::StackAlignment)
01147     StackAlignment = 0;
01148   else if (Val == Attribute::Dereferenceable)
01149     DerefBytes = 0;
01150   else if (Val == Attribute::DereferenceableOrNull)
01151     DerefOrNullBytes = 0;
01152 
01153   return *this;
01154 }
01155 
01156 AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
01157   unsigned Slot = ~0U;
01158   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
01159     if (A.getSlotIndex(I) == Index) {
01160       Slot = I;
01161       break;
01162     }
01163 
01164   assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
01165 
01166   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
01167     Attribute Attr = *I;
01168     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
01169       Attribute::AttrKind Kind = I->getKindAsEnum();
01170       Attrs[Kind] = false;
01171 
01172       if (Kind == Attribute::Alignment)
01173         Alignment = 0;
01174       else if (Kind == Attribute::StackAlignment)
01175         StackAlignment = 0;
01176       else if (Kind == Attribute::Dereferenceable)
01177         DerefBytes = 0;
01178       else if (Kind == Attribute::DereferenceableOrNull)
01179         DerefOrNullBytes = 0;
01180     } else {
01181       assert(Attr.isStringAttribute() && "Invalid attribute type!");
01182       std::map<std::string, std::string>::iterator
01183         Iter = TargetDepAttrs.find(Attr.getKindAsString());
01184       if (Iter != TargetDepAttrs.end())
01185         TargetDepAttrs.erase(Iter);
01186     }
01187   }
01188 
01189   return *this;
01190 }
01191 
01192 AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
01193   std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
01194   if (I != TargetDepAttrs.end())
01195     TargetDepAttrs.erase(I);
01196   return *this;
01197 }
01198 
01199 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
01200   if (Align == 0) return *this;
01201 
01202   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
01203   assert(Align <= 0x40000000 && "Alignment too large.");
01204 
01205   Attrs[Attribute::Alignment] = true;
01206   Alignment = Align;
01207   return *this;
01208 }
01209 
01210 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
01211   // Default alignment, allow the target to define how to align it.
01212   if (Align == 0) return *this;
01213 
01214   assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
01215   assert(Align <= 0x100 && "Alignment too large.");
01216 
01217   Attrs[Attribute::StackAlignment] = true;
01218   StackAlignment = Align;
01219   return *this;
01220 }
01221 
01222 AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
01223   if (Bytes == 0) return *this;
01224 
01225   Attrs[Attribute::Dereferenceable] = true;
01226   DerefBytes = Bytes;
01227   return *this;
01228 }
01229 
01230 AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
01231   if (Bytes == 0)
01232     return *this;
01233 
01234   Attrs[Attribute::DereferenceableOrNull] = true;
01235   DerefOrNullBytes = Bytes;
01236   return *this;
01237 }
01238 
01239 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
01240   // FIXME: What if both have alignments, but they don't match?!
01241   if (!Alignment)
01242     Alignment = B.Alignment;
01243 
01244   if (!StackAlignment)
01245     StackAlignment = B.StackAlignment;
01246 
01247   if (!DerefBytes)
01248     DerefBytes = B.DerefBytes;
01249 
01250   if (!DerefOrNullBytes)
01251     DerefOrNullBytes = B.DerefOrNullBytes;
01252 
01253   Attrs |= B.Attrs;
01254 
01255   for (auto I : B.td_attrs())
01256     TargetDepAttrs[I.first] = I.second;
01257 
01258   return *this;
01259 }
01260 
01261 AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
01262   // FIXME: What if both have alignments, but they don't match?!
01263   if (B.Alignment)
01264     Alignment = 0;
01265 
01266   if (B.StackAlignment)
01267     StackAlignment = 0;
01268 
01269   if (B.DerefBytes)
01270     DerefBytes = 0;
01271 
01272   if (B.DerefOrNullBytes)
01273     DerefOrNullBytes = 0;
01274 
01275   Attrs &= ~B.Attrs;
01276 
01277   for (auto I : B.td_attrs())
01278     TargetDepAttrs.erase(I.first);
01279 
01280   return *this;
01281 }
01282 
01283 bool AttrBuilder::overlaps(const AttrBuilder &B) const {
01284   // First check if any of the target independent attributes overlap.
01285   if ((Attrs & B.Attrs).any())
01286     return true;
01287 
01288   // Then check if any target dependent ones do.
01289   for (auto I : td_attrs())
01290     if (B.contains(I.first))
01291       return true;
01292 
01293   return false;
01294 }
01295 
01296 bool AttrBuilder::contains(StringRef A) const {
01297   return TargetDepAttrs.find(A) != TargetDepAttrs.end();
01298 }
01299 
01300 bool AttrBuilder::hasAttributes() const {
01301   return !Attrs.none() || !TargetDepAttrs.empty();
01302 }
01303 
01304 bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
01305   unsigned Slot = ~0U;
01306   for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
01307     if (A.getSlotIndex(I) == Index) {
01308       Slot = I;
01309       break;
01310     }
01311 
01312   assert(Slot != ~0U && "Couldn't find the index!");
01313 
01314   for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
01315        I != E; ++I) {
01316     Attribute Attr = *I;
01317     if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
01318       if (Attrs[I->getKindAsEnum()])
01319         return true;
01320     } else {
01321       assert(Attr.isStringAttribute() && "Invalid attribute kind!");
01322       return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
01323     }
01324   }
01325 
01326   return false;
01327 }
01328 
01329 bool AttrBuilder::hasAlignmentAttr() const {
01330   return Alignment != 0;
01331 }
01332 
01333 bool AttrBuilder::operator==(const AttrBuilder &B) {
01334   if (Attrs != B.Attrs)
01335     return false;
01336 
01337   for (td_const_iterator I = TargetDepAttrs.begin(),
01338          E = TargetDepAttrs.end(); I != E; ++I)
01339     if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
01340       return false;
01341 
01342   return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
01343          DerefBytes == B.DerefBytes;
01344 }
01345 
01346 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
01347   // FIXME: Remove this in 4.0.
01348   if (!Val) return *this;
01349 
01350   for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
01351        I = Attribute::AttrKind(I + 1)) {
01352     if (I == Attribute::Dereferenceable ||
01353         I == Attribute::DereferenceableOrNull)
01354       continue;
01355     if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
01356       Attrs[I] = true;
01357  
01358       if (I == Attribute::Alignment)
01359         Alignment = 1ULL << ((A >> 16) - 1);
01360       else if (I == Attribute::StackAlignment)
01361         StackAlignment = 1ULL << ((A >> 26)-1);
01362     }
01363   }
01364  
01365   return *this;
01366 }
01367 
01368 //===----------------------------------------------------------------------===//
01369 // AttributeFuncs Function Defintions
01370 //===----------------------------------------------------------------------===//
01371 
01372 /// \brief Which attributes cannot be applied to a type.
01373 AttrBuilder AttributeFuncs::typeIncompatible(const Type *Ty) {
01374   AttrBuilder Incompatible;
01375 
01376   if (!Ty->isIntegerTy())
01377     // Attribute that only apply to integers.
01378     Incompatible.addAttribute(Attribute::SExt)
01379       .addAttribute(Attribute::ZExt);
01380 
01381   if (!Ty->isPointerTy())
01382     // Attribute that only apply to pointers.
01383     Incompatible.addAttribute(Attribute::ByVal)
01384       .addAttribute(Attribute::Nest)
01385       .addAttribute(Attribute::NoAlias)
01386       .addAttribute(Attribute::NoCapture)
01387       .addAttribute(Attribute::NonNull)
01388       .addDereferenceableAttr(1) // the int here is ignored
01389       .addDereferenceableOrNullAttr(1) // the int here is ignored
01390       .addAttribute(Attribute::ReadNone)
01391       .addAttribute(Attribute::ReadOnly)
01392       .addAttribute(Attribute::StructRet)
01393       .addAttribute(Attribute::InAlloca);
01394 
01395   return Incompatible;
01396 }