Cleanup LLVM code generation code.

Materialize the bitcode in the end of CompileClass,
so that the code generation time won't be calculated
in method compilation time.

Change-Id: I1e2bd0b23bcb5c55bd966a31b1df0b8b34922f94
diff --git a/src/compiler.cc b/src/compiler.cc
index 4b03433..51354aa 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -347,7 +347,7 @@
                            const std::vector<const DexFile*>& dex_files) {
   SetGcMaps(class_loader, dex_files);
 #if defined(ART_USE_LLVM_COMPILER)
-  compiler_llvm_->MaterializeEveryCompilationUnit();
+  compiler_llvm_->MaterializeRemainder();
 #endif
 }
 
@@ -925,6 +925,15 @@
   const ClassLoader* class_loader = context->class_loader;
   const DexFile& dex_file = *context->dex_file;
   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();
+
+  MutexLock GUARD(compiler_llvm->compiler_lock_);
+  // TODO: Remove this.  We should not lock the compiler_lock_ in CompileClass()
+  // However, without this mutex lock, we will get segmentation fault.
+#endif
+
   if (SkipClass(class_loader, dex_file, class_def)) {
     return;
   }
@@ -959,6 +968,10 @@
     it.Next();
   }
   DCHECK(!it.HasNext());
+
+#if defined(ART_USE_LLVM_COMPILER)
+  compiler_llvm->MaterializeIfThresholdReached();
+#endif
 }
 
 void Compiler::CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file) {