Merge "Change CHECK(dex_cache != NULL) to use DCHECK" into dalvik-dev
diff --git a/Android.mk b/Android.mk
index 0d240245..02080cc 100644
--- a/Android.mk
+++ b/Android.mk
@@ -109,14 +109,20 @@
 test-art-target-oat: $(ART_TEST_OAT_TARGETS)
 	@echo test-art-target-oat PASSED
 
-.PHONY: test-art-target-run-test
-test-art-target-run-test: test-art-target-run-test-002
-	@echo test-art-target-run-test PASSED
+define declare-test-art-target-run-test
+.PHONY: test-art-target-run-test-$(1)
+test-art-target-run-test-$(1): test-art-target-sync
+	art/test/run-test $(1)
+	@echo test-art-target-run-test-$(1) PASSED
 
-.PHONY: test-art-target-run-test-002
-test-art-target-run-test-002: test-art-target-sync
-	art/test/run-test 002
-	@echo test-art-target-run-test-002 PASSED
+TEST_ART_TARGET_RUN_TEST_TARGETS += test-art-target-run-test-$(1)
+endef
+
+$(foreach test, $(wildcard art/test/0*), $(eval $(call declare-test-art-target-run-test,$(notdir $(test)))))
+
+.PHONY: test-art-target-run-test
+test-art-target-run-test: $(TEST_ART_TARGET_RUN_TEST_TARGETS)
+	@echo test-art-target-run-test PASSED
 
 ########################################################################
 # oat-target and oat-target-sync targets
diff --git a/src/compiler.cc b/src/compiler.cc
index 1797530..2ba89d4 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -983,7 +983,7 @@
   const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
 
 #if defined(ART_USE_LLVM_COMPILER)
-  compiler_llvm::CompilerLLVM* compiler_llvm = context->compiler->GetCompilerLLVM();
+  compiler_llvm::CompilerLLVM* compiler_llvm = context->GetCompiler()->GetCompilerLLVM();
 
   MutexLock GUARD(compiler_llvm->compiler_lock_);
   // TODO: Remove this.  We should not lock the compiler_lock_ in CompileClass()
diff --git a/src/compiler_llvm/compiler_llvm.cc b/src/compiler_llvm/compiler_llvm.cc
index b2a6d28..6646b19 100644
--- a/src/compiler_llvm/compiler_llvm.cc
+++ b/src/compiler_llvm/compiler_llvm.cc
@@ -41,11 +41,10 @@
 // ARMISelLowering.cpp, however, it is not in the llvm namespace.
 
 
-namespace {
+namespace art {
+namespace compiler_llvm {
 
-pthread_once_t llvm_initialized = PTHREAD_ONCE_INIT;
-
-void InitializeLLVM() {
+CompilerLLVM::LLVMInitializer::LLVMInitializer() {
   // NOTE: Uncomment following line to show the time consumption of LLVM passes
   //llvm::TimePassesIsEnabled = true;
 
@@ -76,11 +75,13 @@
   llvm::llvm_start_multithreaded();
 }
 
-} // anonymous namespace
+CompilerLLVM::LLVMInitializer::~LLVMInitializer() {
+  llvm::llvm_shutdown();
+}
 
-
-namespace art {
-namespace compiler_llvm {
+// Singleton. Otherwise, multiple CompilerLLVM instances may cause crashes if
+// one shuts down prematurely.
+CompilerLLVM::LLVMInitializer CompilerLLVM::llvm_initialize_guard;
 
 
 llvm::Module* makeLLVMModuleContents(llvm::Module* module);
@@ -89,20 +90,15 @@
 CompilerLLVM::CompilerLLVM(Compiler* compiler, InstructionSet insn_set)
     : compiler_(compiler), compiler_lock_("llvm_compiler_lock"),
       insn_set_(insn_set), curr_cunit_(NULL) {
-
-  // Initialize LLVM libraries
-  pthread_once(&llvm_initialized, InitializeLLVM);
 }
 
 
 CompilerLLVM::~CompilerLLVM() {
   STLDeleteElements(&cunits_);
-  llvm::llvm_shutdown();
 }
 
 
 void CompilerLLVM::EnsureCompilationUnit() {
-  DCHECK_NE(llvm_initialized, PTHREAD_ONCE_INIT);
   compiler_lock_.AssertHeld();
 
   if (curr_cunit_ != NULL) {
diff --git a/src/compiler_llvm/compiler_llvm.h b/src/compiler_llvm/compiler_llvm.h
index 7481c8c..6332fb3 100644
--- a/src/compiler_llvm/compiler_llvm.h
+++ b/src/compiler_llvm/compiler_llvm.h
@@ -107,6 +107,13 @@
 
   std::string bitcode_filename_;
 
+  class LLVMInitializer {
+   public:
+    LLVMInitializer();
+    ~LLVMInitializer();
+  };
+  static LLVMInitializer llvm_initialize_guard;
+
   DISALLOW_COPY_AND_ASSIGN(CompilerLLVM);
 };