As pointed out by Duncan, I accidentally dropped the first MemoryFence of the
[oota-llvm.git] / lib / VMCore / Function.cpp
index 3a991f62d84e355a266c63858dce9ed9f3dd3fa1..00f9fa30067691e783911f4fe1a907831b8c3ff3 100644 (file)
 #include "llvm/IntrinsicInst.h"
 #include "llvm/CodeGen/ValueTypes.h"
 #include "llvm/Support/LeakDetector.h"
+#include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/StringPool.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/System/RWMutex.h"
 #include "SymbolTableListTraitsImpl.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringExtras.h"
@@ -230,25 +233,37 @@ void Function::removeAttribute(unsigned i, Attributes attr) {
 // use GC.
 static DenseMap<const Function*,PooledStringPtr> *GCNames;
 static StringPool *GCNamePool;
+static ManagedStatic<sys::RWMutex> GCLock;
 
 bool Function::hasGC() const {
-  return GCNames && GCNames->count(this);
+  if (llvm_is_multithreaded()) {
+    sys::ScopedReader Reader(&*GCLock);
+    return GCNames && GCNames->count(this);
+  } else
+    return GCNames && GCNames->count(this);
 }
 
 const char *Function::getGC() const {
   assert(hasGC() && "Function has no collector");
-  return *(*GCNames)[this];
+  if (llvm_is_multithreaded()) {
+    sys::ScopedReader Reader(&*GCLock);
+    return *(*GCNames)[this];
+  } else
+    return *(*GCNames)[this];
 }
 
 void Function::setGC(const char *Str) {
+  if (llvm_is_multithreaded()) GCLock->writer_acquire();
   if (!GCNamePool)
     GCNamePool = new StringPool();
   if (!GCNames)
     GCNames = new DenseMap<const Function*,PooledStringPtr>();
   (*GCNames)[this] = GCNamePool->intern(Str);
+  if (llvm_is_multithreaded()) GCLock->writer_release();
 }
 
 void Function::clearGC() {
+  if (llvm_is_multithreaded()) GCLock->writer_acquire();
   if (GCNames) {
     GCNames->erase(this);
     if (GCNames->empty()) {
@@ -260,6 +275,7 @@ void Function::clearGC() {
       }
     }
   }
+  if (llvm_is_multithreaded()) GCLock->writer_release();
 }
 
 /// copyAttributesFrom - copy all additional attributes (those not needed to
@@ -364,4 +380,15 @@ Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
 #include "llvm/Intrinsics.gen"
 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
 
+  /// hasAddressTaken - returns true if there are any uses of this function
+  /// other than direct calls or invokes to it.
+bool Function::hasAddressTaken() const {
+  for (Value::use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) {
+    if (I.getOperandNo() != 0 ||
+        (!isa<CallInst>(*I) && !isa<InvokeInst>(*I)))
+      return true;
+  }
+  return false;
+}
+
 // vim: sw=2 ai