LLVM API Documentation

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 // This file implements the AttributesList class and Attribute utilities.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Attributes.h"
00015 #include "llvm/Type.h"
00016 #include "llvm/ADT/StringExtras.h"
00017 #include "llvm/ADT/FoldingSet.h"
00018 #include "llvm/System/Atomic.h"
00019 #include "llvm/System/Mutex.h"
00020 #include "llvm/Support/Streams.h"
00021 #include "llvm/Support/ManagedStatic.h"
00022 using namespace llvm;
00023 
00024 //===----------------------------------------------------------------------===//
00025 // Attribute Function Definitions
00026 //===----------------------------------------------------------------------===//
00027 
00028 std::string Attribute::getAsString(Attributes Attrs) {
00029   std::string Result;
00030   if (Attrs & Attribute::ZExt)
00031     Result += "zeroext ";
00032   if (Attrs & Attribute::SExt)
00033     Result += "signext ";
00034   if (Attrs & Attribute::NoReturn)
00035     Result += "noreturn ";
00036   if (Attrs & Attribute::NoUnwind)
00037     Result += "nounwind ";
00038   if (Attrs & Attribute::InReg)
00039     Result += "inreg ";
00040   if (Attrs & Attribute::NoAlias)
00041     Result += "noalias ";
00042   if (Attrs & Attribute::NoCapture)
00043     Result += "nocapture ";
00044   if (Attrs & Attribute::StructRet)
00045     Result += "sret ";
00046   if (Attrs & Attribute::ByVal)
00047     Result += "byval ";
00048   if (Attrs & Attribute::Nest)
00049     Result += "nest ";
00050   if (Attrs & Attribute::ReadNone)
00051     Result += "readnone ";
00052   if (Attrs & Attribute::ReadOnly)
00053     Result += "readonly ";
00054   if (Attrs & Attribute::OptimizeForSize)
00055     Result += "optsize ";
00056   if (Attrs & Attribute::NoInline)
00057     Result += "noinline ";
00058   if (Attrs & Attribute::AlwaysInline)
00059     Result += "alwaysinline ";
00060   if (Attrs & Attribute::StackProtect)
00061     Result += "ssp ";
00062   if (Attrs & Attribute::StackProtectReq)
00063     Result += "sspreq ";
00064   if (Attrs & Attribute::NoRedZone)
00065     Result += "noredzone ";
00066   if (Attrs & Attribute::NoImplicitFloat)
00067     Result += "noimplicitfloat ";
00068   if (Attrs & Attribute::Naked)
00069     Result += "naked ";
00070   if (Attrs & Attribute::Alignment) {
00071     Result += "align ";
00072     Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
00073     Result += " ";
00074   }
00075   // Trim the trailing space.
00076   assert(!Result.empty() && "Unknown attribute!");
00077   Result.erase(Result.end()-1);
00078   return Result;
00079 }
00080 
00081 Attributes Attribute::typeIncompatible(const Type *Ty) {
00082   Attributes Incompatible = None;
00083   
00084   if (!Ty->isInteger())
00085     // Attributes that only apply to integers.
00086     Incompatible |= SExt | ZExt;
00087   
00088   if (!isa<PointerType>(Ty))
00089     // Attributes that only apply to pointers.
00090     Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
00091   
00092   return Incompatible;
00093 }
00094 
00095 //===----------------------------------------------------------------------===//
00096 // AttributeListImpl Definition
00097 //===----------------------------------------------------------------------===//
00098 
00099 namespace llvm {
00100 class AttributeListImpl : public FoldingSetNode {
00101   sys::cas_flag RefCount;
00102   
00103   // AttributesList is uniqued, these should not be publicly available.
00104   void operator=(const AttributeListImpl &); // Do not implement
00105   AttributeListImpl(const AttributeListImpl &); // Do not implement
00106   ~AttributeListImpl();                        // Private implementation
00107 public:
00108   SmallVector<AttributeWithIndex, 4> Attrs;
00109   
00110   AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
00111     : Attrs(Attr, Attr+NumAttrs) {
00112     RefCount = 0;
00113   }
00114   
00115   void AddRef() { sys::AtomicIncrement(&RefCount); }
00116   void DropRef() {
00117     sys::cas_flag old = sys::AtomicDecrement(&RefCount);
00118     if (old == 0) delete this;
00119   }
00120   
00121   void Profile(FoldingSetNodeID &ID) const {
00122     Profile(ID, Attrs.data(), Attrs.size());
00123   }
00124   static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
00125                       unsigned NumAttrs) {
00126     for (unsigned i = 0; i != NumAttrs; ++i)
00127       ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
00128   }
00129 };
00130 }
00131 
00132 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
00133 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
00134 
00135 AttributeListImpl::~AttributeListImpl() {
00136   sys::SmartScopedLock<true> Lock(*ALMutex);
00137   AttributesLists->RemoveNode(this);
00138 }
00139 
00140 
00141 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
00142   // If there are no attributes then return a null AttributesList pointer.
00143   if (NumAttrs == 0)
00144     return AttrListPtr();
00145   
00146 #ifndef NDEBUG
00147   for (unsigned i = 0; i != NumAttrs; ++i) {
00148     assert(Attrs[i].Attrs != Attribute::None && 
00149            "Pointless attribute!");
00150     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
00151            "Misordered AttributesList!");
00152   }
00153 #endif
00154   
00155   // Otherwise, build a key to look up the existing attributes.
00156   FoldingSetNodeID ID;
00157   AttributeListImpl::Profile(ID, Attrs, NumAttrs);
00158   void *InsertPos;
00159   
00160   sys::SmartScopedLock<true> Lock(*ALMutex);
00161   
00162   AttributeListImpl *PAL =
00163     AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
00164   
00165   // If we didn't find any existing attributes of the same shape then
00166   // create a new one and insert it.
00167   if (!PAL) {
00168     PAL = new AttributeListImpl(Attrs, NumAttrs);
00169     AttributesLists->InsertNode(PAL, InsertPos);
00170   }
00171   
00172   // Return the AttributesList that we found or created.
00173   return AttrListPtr(PAL);
00174 }
00175 
00176 
00177 //===----------------------------------------------------------------------===//
00178 // AttrListPtr Method Implementations
00179 //===----------------------------------------------------------------------===//
00180 
00181 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
00182   if (LI) LI->AddRef();
00183 }
00184 
00185 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
00186   if (AttrList) AttrList->AddRef();  
00187 }
00188 
00189 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
00190   if (AttrList == RHS.AttrList) return *this;
00191   if (AttrList) AttrList->DropRef();
00192   AttrList = RHS.AttrList;
00193   if (AttrList) AttrList->AddRef();
00194   return *this;
00195 }
00196 
00197 AttrListPtr::~AttrListPtr() {
00198   if (AttrList) AttrList->DropRef();
00199 }
00200 
00201 /// getNumSlots - Return the number of slots used in this attribute list. 
00202 /// This is the number of arguments that have an attribute set on them
00203 /// (including the function itself).
00204 unsigned AttrListPtr::getNumSlots() const {
00205   return AttrList ? AttrList->Attrs.size() : 0;
00206 }
00207 
00208 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
00209 /// holds a number plus a set of attributes.
00210 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
00211   assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
00212   return AttrList->Attrs[Slot];
00213 }
00214 
00215 
00216 /// getAttributes - The attributes for the specified index are
00217 /// returned.  Attributes for the result are denoted with Idx = 0.
00218 /// Function notes are denoted with idx = ~0.
00219 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
00220   if (AttrList == 0) return Attribute::None;
00221   
00222   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
00223   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
00224     if (Attrs[i].Index == Idx)
00225       return Attrs[i].Attrs;
00226   return Attribute::None;
00227 }
00228 
00229 /// hasAttrSomewhere - Return true if the specified attribute is set for at
00230 /// least one parameter or for the return value.
00231 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
00232   if (AttrList == 0) return false;
00233   
00234   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
00235   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
00236     if (Attrs[i].Attrs & Attr)
00237       return true;
00238   return false;
00239 }
00240 
00241 
00242 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
00243   Attributes OldAttrs = getAttributes(Idx);
00244 #ifndef NDEBUG
00245   // FIXME it is not obvious how this should work for alignment.
00246   // For now, say we can't change a known alignment.
00247   Attributes OldAlign = OldAttrs & Attribute::Alignment;
00248   Attributes NewAlign = Attrs & Attribute::Alignment;
00249   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
00250          "Attempt to change alignment!");
00251 #endif
00252   
00253   Attributes NewAttrs = OldAttrs | Attrs;
00254   if (NewAttrs == OldAttrs)
00255     return *this;
00256   
00257   SmallVector<AttributeWithIndex, 8> NewAttrList;
00258   if (AttrList == 0)
00259     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
00260   else {
00261     const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
00262     unsigned i = 0, e = OldAttrList.size();
00263     // Copy attributes for arguments before this one.
00264     for (; i != e && OldAttrList[i].Index < Idx; ++i)
00265       NewAttrList.push_back(OldAttrList[i]);
00266 
00267     // If there are attributes already at this index, merge them in.
00268     if (i != e && OldAttrList[i].Index == Idx) {
00269       Attrs |= OldAttrList[i].Attrs;
00270       ++i;
00271     }
00272     
00273     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
00274     
00275     // Copy attributes for arguments after this one.
00276     NewAttrList.insert(NewAttrList.end(), 
00277                        OldAttrList.begin()+i, OldAttrList.end());
00278   }
00279   
00280   return get(NewAttrList.data(), NewAttrList.size());
00281 }
00282 
00283 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
00284 #ifndef NDEBUG
00285   // FIXME it is not obvious how this should work for alignment.
00286   // For now, say we can't pass in alignment, which no current use does.
00287   assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
00288 #endif
00289   if (AttrList == 0) return AttrListPtr();
00290   
00291   Attributes OldAttrs = getAttributes(Idx);
00292   Attributes NewAttrs = OldAttrs & ~Attrs;
00293   if (NewAttrs == OldAttrs)
00294     return *this;
00295 
00296   SmallVector<AttributeWithIndex, 8> NewAttrList;
00297   const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
00298   unsigned i = 0, e = OldAttrList.size();
00299   
00300   // Copy attributes for arguments before this one.
00301   for (; i != e && OldAttrList[i].Index < Idx; ++i)
00302     NewAttrList.push_back(OldAttrList[i]);
00303   
00304   // If there are attributes already at this index, merge them in.
00305   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
00306   Attrs = OldAttrList[i].Attrs & ~Attrs;
00307   ++i;
00308   if (Attrs)  // If any attributes left for this parameter, add them.
00309     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
00310   
00311   // Copy attributes for arguments after this one.
00312   NewAttrList.insert(NewAttrList.end(), 
00313                      OldAttrList.begin()+i, OldAttrList.end());
00314   
00315   return get(NewAttrList.data(), NewAttrList.size());
00316 }
00317 
00318 void AttrListPtr::dump() const {
00319   cerr << "PAL[ ";
00320   for (unsigned i = 0; i < getNumSlots(); ++i) {
00321     const AttributeWithIndex &PAWI = getSlot(i);
00322     cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
00323   }
00324   
00325   cerr << "]\n";
00326 }