Home | History | Annotate | Download | only in VMCore
      1 //===-- Attributes.cpp - Implement AttributesList -------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the AttributesList class and Attribute utilities.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Attributes.h"
     15 #include "llvm/Type.h"
     16 #include "llvm/ADT/StringExtras.h"
     17 #include "llvm/ADT/FoldingSet.h"
     18 #include "llvm/Support/Atomic.h"
     19 #include "llvm/Support/Mutex.h"
     20 #include "llvm/Support/Debug.h"
     21 #include "llvm/Support/ManagedStatic.h"
     22 #include "llvm/Support/raw_ostream.h"
     23 using namespace llvm;
     24 
     25 //===----------------------------------------------------------------------===//
     26 // Attribute Function Definitions
     27 //===----------------------------------------------------------------------===//
     28 
     29 std::string Attribute::getAsString(Attributes Attrs) {
     30   std::string Result;
     31   if (Attrs & Attribute::ZExt)
     32     Result += "zeroext ";
     33   if (Attrs & Attribute::SExt)
     34     Result += "signext ";
     35   if (Attrs & Attribute::NoReturn)
     36     Result += "noreturn ";
     37   if (Attrs & Attribute::NoUnwind)
     38     Result += "nounwind ";
     39   if (Attrs & Attribute::UWTable)
     40     Result += "uwtable ";
     41   if (Attrs & Attribute::ReturnsTwice)
     42     Result += "returns_twice ";
     43   if (Attrs & Attribute::InReg)
     44     Result += "inreg ";
     45   if (Attrs & Attribute::NoAlias)
     46     Result += "noalias ";
     47   if (Attrs & Attribute::NoCapture)
     48     Result += "nocapture ";
     49   if (Attrs & Attribute::StructRet)
     50     Result += "sret ";
     51   if (Attrs & Attribute::ByVal)
     52     Result += "byval ";
     53   if (Attrs & Attribute::Nest)
     54     Result += "nest ";
     55   if (Attrs & Attribute::ReadNone)
     56     Result += "readnone ";
     57   if (Attrs & Attribute::ReadOnly)
     58     Result += "readonly ";
     59   if (Attrs & Attribute::OptimizeForSize)
     60     Result += "optsize ";
     61   if (Attrs & Attribute::NoInline)
     62     Result += "noinline ";
     63   if (Attrs & Attribute::InlineHint)
     64     Result += "inlinehint ";
     65   if (Attrs & Attribute::AlwaysInline)
     66     Result += "alwaysinline ";
     67   if (Attrs & Attribute::StackProtect)
     68     Result += "ssp ";
     69   if (Attrs & Attribute::StackProtectReq)
     70     Result += "sspreq ";
     71   if (Attrs & Attribute::NoRedZone)
     72     Result += "noredzone ";
     73   if (Attrs & Attribute::NoImplicitFloat)
     74     Result += "noimplicitfloat ";
     75   if (Attrs & Attribute::Naked)
     76     Result += "naked ";
     77   if (Attrs & Attribute::NonLazyBind)
     78     Result += "nonlazybind ";
     79   if (Attrs & Attribute::AddressSafety)
     80     Result += "address_safety ";
     81   if (Attrs & Attribute::StackAlignment) {
     82     Result += "alignstack(";
     83     Result += utostr(Attribute::getStackAlignmentFromAttrs(Attrs));
     84     Result += ") ";
     85   }
     86   if (Attrs & Attribute::Alignment) {
     87     Result += "align ";
     88     Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
     89     Result += " ";
     90   }
     91   // Trim the trailing space.
     92   assert(!Result.empty() && "Unknown attribute!");
     93   Result.erase(Result.end()-1);
     94   return Result;
     95 }
     96 
     97 Attributes Attribute::typeIncompatible(Type *Ty) {
     98   Attributes Incompatible = None;
     99 
    100   if (!Ty->isIntegerTy())
    101     // Attributes that only apply to integers.
    102     Incompatible |= SExt | ZExt;
    103 
    104   if (!Ty->isPointerTy())
    105     // Attributes that only apply to pointers.
    106     Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
    107 
    108   return Incompatible;
    109 }
    110 
    111 //===----------------------------------------------------------------------===//
    112 // AttributeListImpl Definition
    113 //===----------------------------------------------------------------------===//
    114 
    115 namespace llvm {
    116   class AttributeListImpl;
    117 }
    118 
    119 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
    120 
    121 namespace llvm {
    122 static ManagedStatic<sys::SmartMutex<true> > ALMutex;
    123 
    124 class AttributeListImpl : public FoldingSetNode {
    125   sys::cas_flag RefCount;
    126 
    127   // AttributesList is uniqued, these should not be publicly available.
    128   void operator=(const AttributeListImpl &); // Do not implement
    129   AttributeListImpl(const AttributeListImpl &); // Do not implement
    130   ~AttributeListImpl();                        // Private implementation
    131 public:
    132   SmallVector<AttributeWithIndex, 4> Attrs;
    133 
    134   AttributeListImpl(const AttributeWithIndex *Attr, unsigned NumAttrs)
    135     : Attrs(Attr, Attr+NumAttrs) {
    136     RefCount = 0;
    137   }
    138 
    139   void AddRef() {
    140     sys::SmartScopedLock<true> Lock(*ALMutex);
    141     ++RefCount;
    142   }
    143   void DropRef() {
    144     sys::SmartScopedLock<true> Lock(*ALMutex);
    145     if (!AttributesLists.isConstructed())
    146       return;
    147     sys::cas_flag new_val = --RefCount;
    148     if (new_val == 0)
    149       delete this;
    150   }
    151 
    152   void Profile(FoldingSetNodeID &ID) const {
    153     Profile(ID, Attrs.data(), Attrs.size());
    154   }
    155   static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
    156                       unsigned NumAttrs) {
    157     for (unsigned i = 0; i != NumAttrs; ++i) {
    158       ID.AddInteger(Attr[i].Attrs.Raw());
    159       ID.AddInteger(Attr[i].Index);
    160     }
    161   }
    162 };
    163 }
    164 
    165 AttributeListImpl::~AttributeListImpl() {
    166   // NOTE: Lock must be acquired by caller.
    167   AttributesLists->RemoveNode(this);
    168 }
    169 
    170 
    171 AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs) {
    172   // If there are no attributes then return a null AttributesList pointer.
    173   if (NumAttrs == 0)
    174     return AttrListPtr();
    175 
    176 #ifndef NDEBUG
    177   for (unsigned i = 0; i != NumAttrs; ++i) {
    178     assert(Attrs[i].Attrs != Attribute::None &&
    179            "Pointless attribute!");
    180     assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
    181            "Misordered AttributesList!");
    182   }
    183 #endif
    184 
    185   // Otherwise, build a key to look up the existing attributes.
    186   FoldingSetNodeID ID;
    187   AttributeListImpl::Profile(ID, Attrs, NumAttrs);
    188   void *InsertPos;
    189 
    190   sys::SmartScopedLock<true> Lock(*ALMutex);
    191 
    192   AttributeListImpl *PAL =
    193     AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
    194 
    195   // If we didn't find any existing attributes of the same shape then
    196   // create a new one and insert it.
    197   if (!PAL) {
    198     PAL = new AttributeListImpl(Attrs, NumAttrs);
    199     AttributesLists->InsertNode(PAL, InsertPos);
    200   }
    201 
    202   // Return the AttributesList that we found or created.
    203   return AttrListPtr(PAL);
    204 }
    205 
    206 
    207 //===----------------------------------------------------------------------===//
    208 // AttrListPtr Method Implementations
    209 //===----------------------------------------------------------------------===//
    210 
    211 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
    212   if (LI) LI->AddRef();
    213 }
    214 
    215 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
    216   if (AttrList) AttrList->AddRef();
    217 }
    218 
    219 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
    220   sys::SmartScopedLock<true> Lock(*ALMutex);
    221   if (AttrList == RHS.AttrList) return *this;
    222   if (AttrList) AttrList->DropRef();
    223   AttrList = RHS.AttrList;
    224   if (AttrList) AttrList->AddRef();
    225   return *this;
    226 }
    227 
    228 AttrListPtr::~AttrListPtr() {
    229   if (AttrList) AttrList->DropRef();
    230 }
    231 
    232 /// getNumSlots - Return the number of slots used in this attribute list.
    233 /// This is the number of arguments that have an attribute set on them
    234 /// (including the function itself).
    235 unsigned AttrListPtr::getNumSlots() const {
    236   return AttrList ? AttrList->Attrs.size() : 0;
    237 }
    238 
    239 /// getSlot - Return the AttributeWithIndex at the specified slot.  This
    240 /// holds a number plus a set of attributes.
    241 const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
    242   assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
    243   return AttrList->Attrs[Slot];
    244 }
    245 
    246 
    247 /// getAttributes - The attributes for the specified index are
    248 /// returned.  Attributes for the result are denoted with Idx = 0.
    249 /// Function notes are denoted with idx = ~0.
    250 Attributes AttrListPtr::getAttributes(unsigned Idx) const {
    251   if (AttrList == 0) return Attribute::None;
    252 
    253   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
    254   for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
    255     if (Attrs[i].Index == Idx)
    256       return Attrs[i].Attrs;
    257   return Attribute::None;
    258 }
    259 
    260 /// hasAttrSomewhere - Return true if the specified attribute is set for at
    261 /// least one parameter or for the return value.
    262 bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
    263   if (AttrList == 0) return false;
    264 
    265   const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
    266   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
    267     if (Attrs[i].Attrs & Attr)
    268       return true;
    269   return false;
    270 }
    271 
    272 
    273 AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
    274   Attributes OldAttrs = getAttributes(Idx);
    275 #ifndef NDEBUG
    276   // FIXME it is not obvious how this should work for alignment.
    277   // For now, say we can't change a known alignment.
    278   Attributes OldAlign = OldAttrs & Attribute::Alignment;
    279   Attributes NewAlign = Attrs & Attribute::Alignment;
    280   assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
    281          "Attempt to change alignment!");
    282 #endif
    283 
    284   Attributes NewAttrs = OldAttrs | Attrs;
    285   if (NewAttrs == OldAttrs)
    286     return *this;
    287 
    288   SmallVector<AttributeWithIndex, 8> NewAttrList;
    289   if (AttrList == 0)
    290     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
    291   else {
    292     const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
    293     unsigned i = 0, e = OldAttrList.size();
    294     // Copy attributes for arguments before this one.
    295     for (; i != e && OldAttrList[i].Index < Idx; ++i)
    296       NewAttrList.push_back(OldAttrList[i]);
    297 
    298     // If there are attributes already at this index, merge them in.
    299     if (i != e && OldAttrList[i].Index == Idx) {
    300       Attrs |= OldAttrList[i].Attrs;
    301       ++i;
    302     }
    303 
    304     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
    305 
    306     // Copy attributes for arguments after this one.
    307     NewAttrList.insert(NewAttrList.end(),
    308                        OldAttrList.begin()+i, OldAttrList.end());
    309   }
    310 
    311   return get(NewAttrList.data(), NewAttrList.size());
    312 }
    313 
    314 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
    315 #ifndef NDEBUG
    316   // FIXME it is not obvious how this should work for alignment.
    317   // For now, say we can't pass in alignment, which no current use does.
    318   assert(!(Attrs & Attribute::Alignment) && "Attempt to exclude alignment!");
    319 #endif
    320   if (AttrList == 0) return AttrListPtr();
    321 
    322   Attributes OldAttrs = getAttributes(Idx);
    323   Attributes NewAttrs = OldAttrs & ~Attrs;
    324   if (NewAttrs == OldAttrs)
    325     return *this;
    326 
    327   SmallVector<AttributeWithIndex, 8> NewAttrList;
    328   const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
    329   unsigned i = 0, e = OldAttrList.size();
    330 
    331   // Copy attributes for arguments before this one.
    332   for (; i != e && OldAttrList[i].Index < Idx; ++i)
    333     NewAttrList.push_back(OldAttrList[i]);
    334 
    335   // If there are attributes already at this index, merge them in.
    336   assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
    337   Attrs = OldAttrList[i].Attrs & ~Attrs;
    338   ++i;
    339   if (Attrs)  // If any attributes left for this parameter, add them.
    340     NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
    341 
    342   // Copy attributes for arguments after this one.
    343   NewAttrList.insert(NewAttrList.end(),
    344                      OldAttrList.begin()+i, OldAttrList.end());
    345 
    346   return get(NewAttrList.data(), NewAttrList.size());
    347 }
    348 
    349 void AttrListPtr::dump() const {
    350   dbgs() << "PAL[ ";
    351   for (unsigned i = 0; i < getNumSlots(); ++i) {
    352     const AttributeWithIndex &PAWI = getSlot(i);
    353     dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
    354   }
    355 
    356   dbgs() << "]\n";
    357 }
    358