Reverted commits 333390, 333391 and 333394

Build of shared library LLVMDemangle.so fails due to dependency problem.

llvm-svn: 333395
diff --git a/llvm/lib/Demangle/CMakeLists.txt b/llvm/lib/Demangle/CMakeLists.txt
index e03b555..74e3765 100644
--- a/llvm/lib/Demangle/CMakeLists.txt
+++ b/llvm/lib/Demangle/CMakeLists.txt
@@ -1,6 +1,3 @@
 add_llvm_library(LLVMDemangle
   ItaniumDemangle.cpp
-
-  LINK_LIBS
-  LLVMSupport
 )
diff --git a/llvm/lib/Demangle/ItaniumDemangle.cpp b/llvm/lib/Demangle/ItaniumDemangle.cpp
index df0106a..8955c55 100644
--- a/llvm/lib/Demangle/ItaniumDemangle.cpp
+++ b/llvm/lib/Demangle/ItaniumDemangle.cpp
@@ -12,8 +12,8 @@
 //   - C++ modules TS
 
 #include "llvm/Demangle/Demangle.h"
+
 #include "llvm/Demangle/Compiler.h"
-#include "llvm/Support/MemAlloc.h"
 
 #include <algorithm>
 #include <cassert>
@@ -89,7 +89,7 @@
       BufferCapacity *= 2;
       if (BufferCapacity < N + CurrentPosition)
         BufferCapacity = N + CurrentPosition;
-      Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, BufferCapacity));
+      Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
     }
   }
 
@@ -274,7 +274,7 @@
 
 #ifndef NDEBUG
   LLVM_DUMP_METHOD void dump() const {
-    char *Buffer = static_cast<char*>(llvm::safe_malloc(1024));
+    char *Buffer = static_cast<char*>(std::malloc(1024));
     OutputStream S(Buffer, 1024);
     print(S);
     S += '\0';
@@ -1947,11 +1947,11 @@
   void reserve(size_t NewCap) {
     size_t S = size();
     if (isInline()) {
-      auto* Tmp = static_cast<T*>(llvm::safe_malloc(NewCap * sizeof(T)));
+      auto* Tmp = static_cast<T*>(std::malloc(NewCap * sizeof(T)));
       std::copy(First, Last, Tmp);
       First = Tmp;
     } else
-      First = static_cast<T*>(llvm::safe_realloc(First, NewCap * sizeof(T)));
+      First = static_cast<T*>(std::realloc(First, NewCap * sizeof(T)));
     Last = First + S;
     Cap = First + NewCap;
   }
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index 62c6712..fff2f67 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -596,8 +596,10 @@
   // Otherwise, create the struct layout.  Because it is variable length, we
   // malloc it, then use placement new.
   int NumElts = Ty->getNumElements();
-  StructLayout *L = (StructLayout *)
-      safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
+  StructLayout *L =
+    (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
+  if (L == nullptr)
+    report_bad_alloc_error("Allocation of StructLayout elements failed.");
 
   // Set SL before calling StructLayout's ctor.  The ctor could cause other
   // entries to be added to TheMap, invalidating our reference.
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index ec7d575..9423795 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -214,8 +214,11 @@
 
 /// AllocateBuckets - Allocated initialized bucket memory.
 static void **AllocateBuckets(unsigned NumBuckets) {
-  void **Buckets = static_cast<void**>(safe_calloc(NumBuckets + 1,
-                                                   sizeof(void*)));
+  void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
+
+  if (Buckets == nullptr)
+    report_bad_alloc_error("Allocation of Buckets failed.");
+  
   // Set the very last bucket to be a non-null "pointer".
   Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
   return Buckets;
diff --git a/llvm/lib/Support/Mutex.cpp b/llvm/lib/Support/Mutex.cpp
index 7138c7a..c70125f 100644
--- a/llvm/lib/Support/Mutex.cpp
+++ b/llvm/lib/Support/Mutex.cpp
@@ -47,7 +47,10 @@
 {
   // Declare the pthread_mutex data structures
   pthread_mutex_t* mutex =
-    static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));
+    static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
+
+  if (mutex == nullptr)
+    report_bad_alloc_error("Mutex allocation failed");
 
   pthread_mutexattr_t attr;
 
diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp
index fed4a17..119bb87 100644
--- a/llvm/lib/Support/SmallPtrSet.cpp
+++ b/llvm/lib/Support/SmallPtrSet.cpp
@@ -32,7 +32,9 @@
   NumNonEmpty = NumTombstones = 0;
 
   // Install the new array.  Clear all the buckets to empty.
-  CurArray = (const void**)safe_malloc(sizeof(void*) * CurArraySize);
+  CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
+  if (CurArray == nullptr)
+    report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
 
   memset(CurArray, -1, CurArraySize*sizeof(void*));
 }
@@ -98,7 +100,9 @@
   bool WasSmall = isSmall();
 
   // Install the new array.  Clear all the buckets to empty.
-  const void **NewBuckets = (const void**) safe_malloc(sizeof(void*) * NewSize);
+  const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize);
+  if (NewBuckets == nullptr)
+    report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
 
   // Reset member only if memory was allocated successfully
   CurArray = NewBuckets;
@@ -128,7 +132,9 @@
     CurArray = SmallArray;
   // Otherwise, allocate new heap space (unless we were the same size)
   } else {
-    CurArray = (const void**)safe_malloc(sizeof(void*) * that.CurArraySize);
+    CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
+    if (CurArray == nullptr)
+      report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
   }
 
   // Copy over the that array.
@@ -157,12 +163,16 @@
   // Otherwise, allocate new heap space (unless we were the same size)
   } else if (CurArraySize != RHS.CurArraySize) {
     if (isSmall())
-      CurArray = (const void**)safe_malloc(sizeof(void*) * RHS.CurArraySize);
+      CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
     else {
-      const void **T = (const void**)safe_realloc(CurArray,
+      const void **T = (const void**)realloc(CurArray,
                                              sizeof(void*) * RHS.CurArraySize);
+      if (!T)
+        free(CurArray);
       CurArray = T;
     }
+    if (CurArray == nullptr)
+      report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
   }
 
   CopyHelper(RHS);
diff --git a/llvm/lib/Support/SmallVector.cpp b/llvm/lib/Support/SmallVector.cpp
index ccab4a1..74313151 100644
--- a/llvm/lib/Support/SmallVector.cpp
+++ b/llvm/lib/Support/SmallVector.cpp
@@ -25,13 +25,17 @@
 
   void *NewElts;
   if (BeginX == FirstEl) {
-    NewElts = safe_malloc(NewCapacityInBytes);
+    NewElts = malloc(NewCapacityInBytes);
+    if (NewElts == nullptr)
+      report_bad_alloc_error("Allocation of SmallVector element failed.");
 
     // Copy the elements over.  No need to run dtors on PODs.
     memcpy(NewElts, this->BeginX, CurSizeBytes);
   } else {
     // If this wasn't grown from the inline copy, grow the allocated space.
-    NewElts = safe_realloc(this->BeginX, NewCapacityInBytes);
+    NewElts = realloc(this->BeginX, NewCapacityInBytes);
+    if (NewElts == nullptr)
+      report_bad_alloc_error("Reallocation of SmallVector element failed.");
   }
 
   this->EndX = (char*)NewElts+CurSizeBytes;
diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp
index c1f707c..79262cc 100644
--- a/llvm/lib/Support/StringMap.cpp
+++ b/llvm/lib/Support/StringMap.cpp
@@ -59,8 +59,10 @@
   NumTombstones = 0;
 
   TheTable = static_cast<StringMapEntryBase **>(
-      safe_calloc(NewNumBuckets+1,
+      std::calloc(NewNumBuckets+1,
                   sizeof(StringMapEntryBase **) + sizeof(unsigned)));
+  if (TheTable == nullptr)
+    report_bad_alloc_error("Allocation of StringMap table failed.");
 
   // Set the member only if TheTable was successfully allocated
   NumBuckets = NewNumBuckets;
@@ -218,7 +220,9 @@
   // Allocate one extra bucket which will always be non-empty.  This allows the
   // iterators to stop at end.
   auto NewTableArray = static_cast<StringMapEntryBase **>(
-      safe_calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
+      std::calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
+  if (NewTableArray == nullptr)
+    report_bad_alloc_error("Allocation of StringMap hash table failed.");
 
   unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
   NewTableArray[NewSize] = (StringMapEntryBase*)2;