[WebAssembly] Update WebAssemblyLowerEmscriptenEHSjLj to handle separate compilation

Previously we were assuming whole program compilation. Now that
separate compilation is a thing we need to update this pass.
Firstly, it can no longer assert on the existence of malloc and free.
This functions might not be in the current translation unit.  If we
need them then we will generate not imports for them.

Secondly the global helper function we create should be marked as
weak since we will be generating a separate copy in each translation
unit.

Finally the names of the symbols used must be unique and fixed since
they need to agree across translation units.

Differential Revision: https://reviews.llvm.org/D49263

llvm-svn: 337301
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
index 06c6dc6..e9cb7c1 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
@@ -225,13 +225,8 @@
 
 namespace {
 class WebAssemblyLowerEmscriptenEHSjLj final : public ModulePass {
-  static const char *ThrewGVName;
-  static const char *ThrewValueGVName;
-  static const char *TempRet0GVName;
   static const char *ResumeFName;
   static const char *EHTypeIDFName;
-  static const char *SetThrewFName;
-  static const char *SetTempRet0FName;
   static const char *EmLongjmpFName;
   static const char *EmLongjmpJmpbufFName;
   static const char *SaveSetjmpFName;
@@ -300,14 +295,9 @@
 };
 } // End anonymous namespace
 
-const char *WebAssemblyLowerEmscriptenEHSjLj::ThrewGVName = "__THREW__";
-const char *WebAssemblyLowerEmscriptenEHSjLj::ThrewValueGVName = "__threwValue";
-const char *WebAssemblyLowerEmscriptenEHSjLj::TempRet0GVName = "__tempRet0";
 const char *WebAssemblyLowerEmscriptenEHSjLj::ResumeFName = "__resumeException";
 const char *WebAssemblyLowerEmscriptenEHSjLj::EHTypeIDFName =
     "llvm_eh_typeid_for";
-const char *WebAssemblyLowerEmscriptenEHSjLj::SetThrewFName = "setThrew";
-const char *WebAssemblyLowerEmscriptenEHSjLj::SetTempRet0FName = "setTempRet0";
 const char *WebAssemblyLowerEmscriptenEHSjLj::EmLongjmpFName =
     "emscripten_longjmp";
 const char *WebAssemblyLowerEmscriptenEHSjLj::EmLongjmpJmpbufFName =
@@ -343,15 +333,13 @@
   return true;
 }
 
-// Returns an available name for a global value.
-// If the proposed name already exists in the module, adds '_' at the end of
-// the name until the name is available.
-static inline std::string createGlobalValueName(const Module &M,
-                                                const std::string &Propose) {
-  std::string Name = Propose;
-  while (M.getNamedGlobal(Name))
-    Name += "_";
-  return Name;
+static GlobalVariable *createGlobalVariableI32(Module &M, IRBuilder<> &IRB,
+                                               const char *Name) {
+  if (M.getNamedGlobal(Name))
+    report_fatal_error(Twine("variable name is reserved: ") + Name);
+
+  return new GlobalVariable(M, IRB.getInt32Ty(), false,
+                            GlobalValue::WeakODRLinkage, IRB.getInt32(0), Name);
 }
 
 // Simple function name mangler.
@@ -613,11 +601,13 @@
   LLVMContext &C = M.getContext();
   IRBuilder<> IRB(C);
 
-  assert(!M.getNamedGlobal(SetThrewFName) && "setThrew already exists");
+  if (M.getNamedGlobal("setThrew"))
+    report_fatal_error("setThrew already exists");
+
   Type *Params[] = {IRB.getInt32Ty(), IRB.getInt32Ty()};
   FunctionType *FTy = FunctionType::get(IRB.getVoidTy(), Params, false);
   Function *F =
-      Function::Create(FTy, GlobalValue::ExternalLinkage, SetThrewFName, &M);
+      Function::Create(FTy, GlobalValue::WeakODRLinkage, "setThrew", &M);
   Argument *Arg1 = &*(F->arg_begin());
   Argument *Arg2 = &*std::next(F->arg_begin());
   Arg1->setName("threw");
@@ -648,11 +638,12 @@
   LLVMContext &C = M.getContext();
   IRBuilder<> IRB(C);
 
-  assert(!M.getNamedGlobal(SetTempRet0FName) && "setTempRet0 already exists");
+  if (M.getNamedGlobal("setTempRet0"))
+    report_fatal_error("setTempRet0 already exists");
   Type *Params[] = {IRB.getInt32Ty()};
   FunctionType *FTy = FunctionType::get(IRB.getVoidTy(), Params, false);
   Function *F =
-      Function::Create(FTy, GlobalValue::ExternalLinkage, SetTempRet0FName, &M);
+      Function::Create(FTy, GlobalValue::WeakODRLinkage, "setTempRet0", &M);
   F->arg_begin()->setName("value");
   BasicBlock *EntryBB = BasicBlock::Create(C, "entry", F);
   IRB.SetInsertPoint(EntryBB);
@@ -699,15 +690,9 @@
 
   // Create global variables __THREW__, threwValue, and __tempRet0, which are
   // used in common for both exception handling and setjmp/longjmp handling
-  ThrewGV = new GlobalVariable(M, IRB.getInt32Ty(), false,
-                               GlobalValue::ExternalLinkage, IRB.getInt32(0),
-                               createGlobalValueName(M, ThrewGVName));
-  ThrewValueGV = new GlobalVariable(
-      M, IRB.getInt32Ty(), false, GlobalValue::ExternalLinkage, IRB.getInt32(0),
-      createGlobalValueName(M, ThrewValueGVName));
-  TempRet0GV = new GlobalVariable(M, IRB.getInt32Ty(), false,
-                                  GlobalValue::ExternalLinkage, IRB.getInt32(0),
-                                  createGlobalValueName(M, TempRet0GVName));
+  ThrewGV = createGlobalVariableI32(M, IRB, "__THREW__");
+  ThrewValueGV = createGlobalVariableI32(M, IRB, "__threwValue");
+  TempRet0GV = createGlobalVariableI32(M, IRB, "__tempRet0");
 
   bool Changed = false;
 
@@ -736,12 +721,6 @@
   if (DoSjLj) {
     Changed = true; // We have setjmp or longjmp somewhere
 
-    Function *MallocF = M.getFunction("malloc");
-    Function *FreeF = M.getFunction("free");
-    if (!MallocF || !FreeF)
-      report_fatal_error(
-          "malloc and free must be linked into the module if setjmp is used");
-
     // Register saveSetjmp function
     FunctionType *SetjmpFTy = SetjmpF->getFunctionType();
     SmallVector<Type *, 4> Params = {SetjmpFTy->getParamType(0),