Reduce contention on the Attributes lock by using atomic operations for reference...
[oota-llvm.git] / lib / VMCore / Attributes.cpp
index b2f2150bf6ee46cdd4ff3c62b3010793dc465701..fb1a9b8cd5fbdf28aeceb7241bd417669428c146 100644 (file)
@@ -15,6 +15,8 @@
 #include "llvm/Type.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/FoldingSet.h"
+#include "llvm/System/Atomic.h"
+#include "llvm/System/Mutex.h"
 #include "llvm/Support/Streams.h"
 #include "llvm/Support/ManagedStatic.h"
 using namespace llvm;
@@ -37,8 +39,10 @@ std::string Attribute::getAsString(Attributes Attrs) {
     Result += "inreg ";
   if (Attrs & Attribute::NoAlias)
     Result += "noalias ";
+  if (Attrs & Attribute::NoCapture)
+    Result += "nocapture ";
   if (Attrs & Attribute::StructRet)
-    Result += "sret ";  
+    Result += "sret ";
   if (Attrs & Attribute::ByVal)
     Result += "byval ";
   if (Attrs & Attribute::Nest)
@@ -47,12 +51,29 @@ std::string Attribute::getAsString(Attributes Attrs) {
     Result += "readnone ";
   if (Attrs & Attribute::ReadOnly)
     Result += "readonly ";
+  if (Attrs & Attribute::OptimizeForSize)
+    Result += "optsize ";
+  if (Attrs & Attribute::NoInline)
+    Result += "noinline ";
+  if (Attrs & Attribute::AlwaysInline)
+    Result += "alwaysinline ";
+  if (Attrs & Attribute::StackProtect)
+    Result += "ssp ";
+  if (Attrs & Attribute::StackProtectReq)
+    Result += "sspreq ";
+  if (Attrs & Attribute::NoRedZone)
+    Result += "noredzone ";
+  if (Attrs & Attribute::NoImplicitFloat)
+    Result += "noimplicitfloat ";
+  if (Attrs & Attribute::Naked)
+    Result += "naked ";
   if (Attrs & Attribute::Alignment) {
     Result += "align ";
-    Result += utostr((Attrs & Attribute::Alignment) >> 16);
+    Result += utostr(Attribute::getAlignmentFromAttrs(Attrs));
     Result += " ";
   }
   // Trim the trailing space.
+  assert(!Result.empty() && "Unknown attribute!");
   Result.erase(Result.end()-1);
   return Result;
 }
@@ -66,7 +87,7 @@ Attributes Attribute::typeIncompatible(const Type *Ty) {
   
   if (!isa<PointerType>(Ty))
     // Attributes that only apply to pointers.
-    Incompatible |= ByVal | Nest | NoAlias | StructRet;
+    Incompatible |= ByVal | Nest | NoAlias | StructRet | NoCapture;
   
   return Incompatible;
 }
@@ -77,7 +98,7 @@ Attributes Attribute::typeIncompatible(const Type *Ty) {
 
 namespace llvm {
 class AttributeListImpl : public FoldingSetNode {
-  unsigned RefCount;
+  sys::cas_flag RefCount;
   
   // AttributesList is uniqued, these should not be publicly available.
   void operator=(const AttributeListImpl &); // Do not implement
@@ -91,11 +112,14 @@ public:
     RefCount = 0;
   }
   
-  void AddRef() { ++RefCount; }
-  void DropRef() { if (--RefCount == 0) delete this; }
+  void AddRef() { sys::AtomicIncrement(&RefCount); }
+  void DropRef() {
+    sys::cas_flag old = sys::AtomicDecrement(&RefCount);
+    if (old == 0) delete this;
+  }
   
   void Profile(FoldingSetNodeID &ID) const {
-    Profile(ID, &Attrs[0], Attrs.size());
+    Profile(ID, Attrs.data(), Attrs.size());
   }
   static void Profile(FoldingSetNodeID &ID, const AttributeWithIndex *Attr,
                       unsigned NumAttrs) {
@@ -105,9 +129,11 @@ public:
 };
 }
 
+static ManagedStatic<sys::SmartMutex<true> > ALMutex;
 static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
 
 AttributeListImpl::~AttributeListImpl() {
+  sys::SmartScopedLock<true> Lock(*ALMutex);
   AttributesLists->RemoveNode(this);
 }
 
@@ -130,6 +156,9 @@ AttrListPtr AttrListPtr::get(const AttributeWithIndex *Attrs, unsigned NumAttrs)
   FoldingSetNodeID ID;
   AttributeListImpl::Profile(ID, Attrs, NumAttrs);
   void *InsertPos;
+  
+  sys::SmartScopedLock<true> Lock(*ALMutex);
+  
   AttributeListImpl *PAL =
     AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
   
@@ -248,7 +277,7 @@ AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
                        OldAttrList.begin()+i, OldAttrList.end());
   }
   
-  return get(&NewAttrList[0], NewAttrList.size());
+  return get(NewAttrList.data(), NewAttrList.size());
 }
 
 AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
@@ -283,7 +312,7 @@ AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
   NewAttrList.insert(NewAttrList.end(), 
                      OldAttrList.begin()+i, OldAttrList.end());
   
-  return get(&NewAttrList[0], NewAttrList.size());
+  return get(NewAttrList.data(), NewAttrList.size());
 }
 
 void AttrListPtr::dump() const {