Create an instance of CompilerLLVM in the constructor of Compiler.

(cherry picked from commit 120539c9a333789f52dd75aa3ec2d303c7cf2119)

Change-Id: I43d10f712e515cffd2f97e628a9d2aa2ff994f5b
diff --git a/src/compiler.cc b/src/compiler.cc
index 0eb4301..0431d74 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -330,6 +330,18 @@
   }
   VLOG(compiler) << "dlopen(\"" << compiler_so_name << "\", RTLD_LAZY) returned " << compiler_library_;
 
+#if defined(ART_USE_LLVM_COMPILER)
+  // Initialize compiler_context_
+  typedef void (*InitCompilerContextFn)(Compiler&);
+
+  InitCompilerContextFn init_compiler_context =
+    FindFunction<void (*)(Compiler&)>(compiler_so_name,
+                                      compiler_library_,
+                                      "ArtInitCompilerContext");
+
+  init_compiler_context(*this);
+#endif
+
   compiler_ = FindFunction<CompilerFn>(compiler_so_name, compiler_library_, "ArtCompileMethod");
   jni_compiler_ = FindFunction<JniCompilerFn>(compiler_so_name, compiler_library_, "ArtJniCompileMethod");
   create_invoke_stub_ = FindFunction<CreateInvokeStubFn>(compiler_so_name, compiler_library_, "ArtCreateInvokeStub");
@@ -1397,17 +1409,25 @@
 
 #if defined(ART_USE_LLVM_COMPILER)
 void Compiler::SetElfFileName(std::string const& filename) {
-  elf_filename_ = filename;
+  typedef void (*SetElfFileNameFn)(Compiler&, std::string const&);
+
+  SetElfFileNameFn set_elf_file_name =
+    FindFunction<SetElfFileNameFn>(MakeCompilerSoName(instruction_set_),
+                                   compiler_library_,
+                                   "compilerLLVMSetElfFileName");
+
+  set_elf_file_name(*this, filename);
 }
 
 void Compiler::SetBitcodeFileName(std::string const& filename) {
-  bitcode_filename_ = filename;
-}
-std::string const& Compiler::GetElfFileName() {
-  return elf_filename_;
-}
-std::string const& Compiler::GetBitcodeFileName() {
-  return bitcode_filename_;
+  typedef void (*SetBitcodeFileNameFn)(Compiler&, std::string const&);
+
+  SetBitcodeFileNameFn set_bitcode_file_name =
+    FindFunction<SetBitcodeFileNameFn>(MakeCompilerSoName(instruction_set_),
+                                       compiler_library_,
+                                       "compilerLLVMSetBitcodeFileName");
+
+  set_bitcode_file_name(*this, filename);
 }
 #endif
 
diff --git a/src/compiler.h b/src/compiler.h
index dee498b..4adb447 100644
--- a/src/compiler.h
+++ b/src/compiler.h
@@ -137,8 +137,6 @@
 #if defined(ART_USE_LLVM_COMPILER)
   void SetElfFileName(std::string const& filename);
   void SetBitcodeFileName(std::string const& filename);
-  std::string const& GetElfFileName();
-  std::string const& GetBitcodeFileName();
 #endif
 
   void SetCompilerContext(void* compiler_context) {
@@ -284,8 +282,6 @@
   const std::set<std::string>* image_classes_;
 
 #if defined(ART_USE_LLVM_COMPILER)
-  std::string elf_filename_;
-  std::string bitcode_filename_;
   typedef void (*CompilerCallbackFn)(Compiler& compiler);
   typedef MutexLock* (*CompilerMutexLockFn)(Compiler& compiler);
 #endif
diff --git a/src/compiler_llvm/compiler_llvm.cc b/src/compiler_llvm/compiler_llvm.cc
index 48c6062..84e66de 100644
--- a/src/compiler_llvm/compiler_llvm.cc
+++ b/src/compiler_llvm/compiler_llvm.cc
@@ -209,19 +209,31 @@
   return upcall_compiler->CreateStub(is_static, shorty);
 }
 
-CompilerLLVM* EnsureCompilerLLVM(art::Compiler& compiler) {
-  if (compiler.GetCompilerContext() == NULL) {
-    compiler.SetCompilerContext(new CompilerLLVM(&compiler, compiler.GetInstructionSet()));
-  }
-  CompilerLLVM* compiler_llvm = reinterpret_cast<CompilerLLVM*>(compiler.GetCompilerContext());
-  compiler_llvm->SetElfFileName(compiler.GetElfFileName());
-  compiler_llvm->SetBitcodeFileName(compiler.GetBitcodeFileName());
-  return compiler_llvm;
-}
-
 } // namespace compiler_llvm
 } // namespace art
 
+inline static art::compiler_llvm::CompilerLLVM* ContextOf(art::Compiler& compiler) {
+  void *compiler_context = compiler.GetCompilerContext();
+  CHECK(compiler_context != NULL);
+  return reinterpret_cast<art::compiler_llvm::CompilerLLVM*>(compiler_context);
+}
+
+inline static const art::compiler_llvm::CompilerLLVM* ContextOf(const art::Compiler& compiler) {
+  void *compiler_context = compiler.GetCompilerContext();
+  CHECK(compiler_context != NULL);
+  return reinterpret_cast<const art::compiler_llvm::CompilerLLVM*>(compiler_context);
+}
+
+extern "C" void ArtInitCompilerContext(art::Compiler& compiler) {
+  CHECK(compiler.GetCompilerContext() == NULL);
+
+  art::compiler_llvm::CompilerLLVM* compiler_llvm =
+      new art::compiler_llvm::CompilerLLVM(&compiler,
+                                           compiler.GetInstructionSet());
+
+  compiler.SetCompilerContext(compiler_llvm);
+}
+
 extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler,
                                                  const art::DexFile::CodeItem* code_item,
                                                  uint32_t access_flags, uint32_t method_idx,
@@ -235,7 +247,7 @@
     class_loader, class_linker, dex_file, *dex_cache, code_item,
     method_idx, access_flags);
 
-  return art::compiler_llvm::EnsureCompilerLLVM(compiler)->CompileDexMethod(&oat_compilation_unit);
+  return ContextOf(compiler)->CompileDexMethod(&oat_compilation_unit);
 }
 
 extern "C" art::CompiledMethod* ArtJniCompileMethod(art::Compiler& compiler,
@@ -249,7 +261,7 @@
     class_loader, class_linker, dex_file, *dex_cache, NULL,
     method_idx, access_flags);
 
-  art::compiler_llvm::CompilerLLVM* compiler_llvm = art::compiler_llvm::EnsureCompilerLLVM(compiler);
+  art::compiler_llvm::CompilerLLVM* compiler_llvm = ContextOf(compiler);
   art::CompiledMethod* result = compiler_llvm->CompileNativeMethod(&oat_compilation_unit);
   compiler_llvm->MaterializeIfThresholdReached();
   return result;
@@ -257,18 +269,28 @@
 
 extern "C" art::CompiledInvokeStub* ArtCreateInvokeStub(art::Compiler& compiler, bool is_static,
                                                         const char* shorty, uint32_t shorty_len) {
-  return art::compiler_llvm::EnsureCompilerLLVM(compiler)->CreateInvokeStub(is_static, shorty);
+  return ContextOf(compiler)->CreateInvokeStub(is_static, shorty);
+}
+
+extern "C" void compilerLLVMSetElfFileName(art::Compiler& compiler,
+                                           std::string const& filename) {
+  ContextOf(compiler)->SetElfFileName(filename);
+}
+
+extern "C" void compilerLLVMSetBitcodeFileName(art::Compiler& compiler,
+                                               std::string const& filename) {
+  ContextOf(compiler)->SetBitcodeFileName(filename);
 }
 
 extern "C" void compilerLLVMMaterializeRemainder(art::Compiler& compiler) {
-  art::compiler_llvm::EnsureCompilerLLVM(compiler)->MaterializeRemainder();
+  ContextOf(compiler)->MaterializeRemainder();
 }
 
 // Note: Using this function carefully!!! This is temporary solution, we will remove it.
 extern "C" art::MutexLock* compilerLLVMMutexLock(art::Compiler& compiler) {
-  return new art::MutexLock(art::compiler_llvm::EnsureCompilerLLVM(compiler)->compiler_lock_);
+  return new art::MutexLock(ContextOf(compiler)->compiler_lock_);
 }
 
 extern "C" void compilerLLVMDispose(art::Compiler& compiler) {
-  delete art::compiler_llvm::EnsureCompilerLLVM(compiler);
+  delete ContextOf(compiler);
 }