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