Fix quick fly2iceland after rebase.

Change-Id: I844f005782b3ecdcb52dc2484d44f4ae34e1c670
diff --git a/build/Android.libart-compiler-llvm.mk b/build/Android.libart-compiler-llvm.mk
index 79665e5..332b809 100644
--- a/build/Android.libart-compiler-llvm.mk
+++ b/build/Android.libart-compiler-llvm.mk
@@ -62,8 +62,6 @@
       src/compiler_llvm/dalvik_reg.cc \
       src/compiler_llvm/gbc_expander.cc \
       src/compiler_llvm/method_compiler.cc \
-      src/greenland/dalvik_reg.cc \
-      src/greenland/dex_lang.cc \
       src/greenland/intrinsic_helper.cc \
       src/greenland/ir_builder.cc
   else
diff --git a/src/compiler.h b/src/compiler.h
index c27bf95..3376bc2 100644
--- a/src/compiler.h
+++ b/src/compiler.h
@@ -308,7 +308,7 @@
 #if defined(ART_USE_LLVM_COMPILER)
   typedef SafeMap<std::string, const CompiledInvokeStub*> ProxyStubTable;
   // Proxy stubs created for proxy invocation delegation
-  mutable Mutex compiled_proxy_stubs_lock_;
+  mutable Mutex compiled_proxy_stubs_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
   ProxyStubTable compiled_proxy_stubs_ GUARDED_BY(compiled_proxy_stubs_lock_);
 #endif
 
diff --git a/src/compiler/Compiler.h b/src/compiler/Compiler.h
index 4a7683b..8bda3fe 100644
--- a/src/compiler/Compiler.h
+++ b/src/compiler/Compiler.h
@@ -180,19 +180,15 @@
 #if defined(ART_USE_QUICK_COMPILER)
 class QuickCompiler {
   public:
-    QuickCompiler(art::Compiler* compiler);
+    QuickCompiler();
     ~QuickCompiler();
 
-    const art::Compiler* GetCompiler() const {
-      return compiler_;
-    }
-
     llvm::LLVMContext* GetLLVMContext() {
       return llvm_context_.get();
     }
 
     llvm::Module* GetLLVMModule() {
-      return llvm_module_.get();
+      return llvm_module_;
     }
 
     art::greenland::IntrinsicHelper* GetIntrinsicHelper() {
@@ -204,9 +200,8 @@
     }
 
   private:
-    const art::Compiler* const compiler_;
     UniquePtr<llvm::LLVMContext> llvm_context_;
-    UniquePtr<llvm::Module> llvm_module_;
+    llvm::Module* llvm_module_; // Managed by context_
     UniquePtr<art::greenland::IntrinsicHelper> intrinsic_helper_;
     UniquePtr<art::greenland::IRBuilder> ir_builder_;
 };
diff --git a/src/compiler/CompilerIR.h b/src/compiler/CompilerIR.h
index d63b295..f7b1bc6 100644
--- a/src/compiler/CompilerIR.h
+++ b/src/compiler/CompilerIR.h
@@ -424,7 +424,6 @@
       checkstats(NULL),
 #if defined(ART_USE_QUICK_COMPILER)
       genBitcode(false),
-      gbcOnly(false),
       context(NULL),
       module(NULL),
       func(NULL),
@@ -597,7 +596,7 @@
   Checkstats* checkstats;
 #if defined(ART_USE_QUICK_COMPILER)
   bool genBitcode;
-  bool gbcOnly;
+  QuickCompiler* quick_compiler;
   llvm::LLVMContext* context;
   llvm::Module* module;
   llvm::Function* func;
diff --git a/src/compiler/Frontend.cc b/src/compiler/Frontend.cc
index 3862471..ca6ccca 100644
--- a/src/compiler/Frontend.cc
+++ b/src/compiler/Frontend.cc
@@ -24,14 +24,11 @@
 namespace art {
 
 #if defined(ART_USE_QUICK_COMPILER)
-QuickCompiler::QuickCompiler(art::Compiler* compiler)
-    : compiler_(compiler) {
+QuickCompiler::QuickCompiler() {
   // Create context, module, intrinsic helper & ir builder
   llvm_context_.reset(new llvm::LLVMContext());
-  llvm_module_.reset(new llvm::Module("art", *llvm_context_));
+  llvm_module_ = new llvm::Module("art", *llvm_context_);
   llvm::StructType::create(*llvm_context_, "JavaObject");
-  llvm::StructType::create(*llvm_context_, "Method");
-  llvm::StructType::create(*llvm_context_, "Thread");
   intrinsic_helper_.reset( new greenland::IntrinsicHelper(*llvm_context_, *llvm_module_));
   ir_builder_.reset(new greenland::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
 }
@@ -41,7 +38,7 @@
 
 extern "C" void ArtInitQuickCompilerContext(art::Compiler& compiler) {
   CHECK(compiler.GetCompilerContext() == NULL);
-  QuickCompiler* quickCompiler = new QuickCompiler(&compiler);
+  QuickCompiler* quickCompiler = new QuickCompiler();
   compiler.SetCompilerContext(quickCompiler);
 }
 
@@ -779,10 +776,7 @@
                               uint32_t method_idx, jobject class_loader,
                               const DexFile& dex_file
 #if defined(ART_USE_QUICK_COMPILER)
-                              , llvm::Module* module,
-                              llvm::LLVMContext* context,
-                              greenland::IntrinsicHelper* intrinsic_helper,
-                              greenland::IRBuilder* irb,
+                              , QuickCompiler* quick_compiler,
                               bool gbcOnly
 #endif
                              )
@@ -817,11 +811,14 @@
   DCHECK((cUnit->instructionSet == kThumb2) ||
          (cUnit->instructionSet == kX86) ||
          (cUnit->instructionSet == kMips));
-  cUnit->module = module;
-  cUnit->context = context;
-  cUnit->intrinsic_helper = intrinsic_helper;
-  cUnit->irb = irb;
-  cUnit->gbcOnly = gbcOnly;
+  if (gbcOnly) {
+    cUnit->quick_compiler = quick_compiler;
+  } else {
+    // TODO: We need one LLVMContext per thread.
+    cUnit->quick_compiler =
+        reinterpret_cast<QuickCompiler*>(compiler.GetCompilerContext());
+  }
+  DCHECK(cUnit->quick_compiler != NULL);
   if (cUnit->instructionSet == kThumb2) {
     // TODO: remove this once x86 is tested
     cUnit->genBitcode = true;
@@ -1142,7 +1139,7 @@
   if (cUnit->genBitcode) {
     // MIR->Bitcode
     oatMethodMIR2Bitcode(cUnit.get());
-    if (cUnit->gbcOnly) {
+    if (gbcOnly) {
       // all done
       oatArenaReset(cUnit.get());
       return NULL;
@@ -1245,7 +1242,7 @@
                                  const DexFile& dex_file)
 {
   return compileMethod(compiler, code_item, access_flags, invoke_type, method_idx, class_loader,
-                       dex_file, NULL, NULL, NULL, NULL, false);
+                       dex_file, NULL, false);
 }
 
 /*
@@ -1257,13 +1254,10 @@
                            uint32_t access_flags, InvokeType invoke_type,
                            uint32_t method_idx, jobject class_loader,
                            const DexFile& dex_file,
-                           llvm::Module* module,
-                           llvm::LLVMContext* context,
-                           greenland::IntrinsicHelper* intrinsic_helper,
-                           greenland::IRBuilder* irb)
+                           QuickCompiler* quick_compiler)
 {
   compileMethod(compiler, code_item, access_flags, invoke_type, method_idx, class_loader,
-                dex_file, module, context, intrinsic_helper, irb, true);
+                dex_file, quick_compiler, true);
 }
 #else
 CompiledMethod* oatCompileMethod(Compiler& compiler,
diff --git a/src/compiler/codegen/MethodBitcode.cc b/src/compiler/codegen/MethodBitcode.cc
index 9427dcc..c176c4e 100644
--- a/src/compiler/codegen/MethodBitcode.cc
+++ b/src/compiler/codegen/MethodBitcode.cc
@@ -170,8 +170,7 @@
 }
 void initIR(CompilationUnit* cUnit)
 {
-  QuickCompiler* quick =
-      reinterpret_cast<QuickCompiler*>(cUnit->compiler->GetCompilerContext());
+  QuickCompiler* quick = cUnit->quick_compiler;
   cUnit->context = quick->GetLLVMContext();
   cUnit->module = quick->GetLLVMModule();
   cUnit->intrinsic_helper = quick->GetIntrinsicHelper();
diff --git a/src/compiler_llvm/compilation_unit.cc b/src/compiler_llvm/compilation_unit.cc
index eaa8be1..95073ea 100644
--- a/src/compiler_llvm/compilation_unit.cc
+++ b/src/compiler_llvm/compilation_unit.cc
@@ -150,11 +150,11 @@
 namespace art {
 namespace compiler_llvm {
 
-#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
+#if defined(ART_USE_DEXLANG_FRONTEND)
 llvm::FunctionPass*
 CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper,
                       IRBuilder& irb);
-
+#elif defined(ART_USE_QUICK_COMPILER)
 llvm::FunctionPass*
 CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
                       Compiler* compiler, OatCompilationUnit* oat_compilation_unit);
@@ -165,19 +165,23 @@
 
 CompilationUnit::CompilationUnit(const CompilerLLVM* compiler_llvm,
                                  size_t cunit_idx)
-: compiler_llvm_(compiler_llvm), cunit_idx_(cunit_idx),
-  context_(new llvm::LLVMContext()) {
-
-  // Create the module and include the runtime function declaration
+: compiler_llvm_(compiler_llvm), cunit_idx_(cunit_idx) {
+#if !defined(ART_USE_QUICK_COMPILER)
+  context_.reset(new llvm::LLVMContext());
   module_ = new llvm::Module("art", *context_);
-  makeLLVMModuleContents(module_);
-
-#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
-  dex_lang_ctx_ = new greenland::DexLang::Context(*module_);
-#endif
-#if defined(ART_USE_QUICK_COMPILER)
+#else
   compiler_ = NULL;
   oat_compilation_unit_ = NULL;
+  quick_ctx_.reset(new QuickCompiler());
+  context_.reset(quick_ctx_->GetLLVMContext());
+  module_ = quick_ctx_->GetLLVMModule();
+#endif
+
+  // Include the runtime function declaration
+  makeLLVMModuleContents(module_);
+
+#if defined(ART_USE_DEXLANG_FRONTEND)
+  dex_lang_ctx_ = new greenland::DexLang::Context(*module_);
 #endif
 
   // Create IRBuilder
@@ -204,8 +208,11 @@
 
 
 CompilationUnit::~CompilationUnit() {
-#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
+#if defined(ART_USE_DEXLANG_FRONTEND)
   delete dex_lang_ctx_;
+#elif defined(ART_USE_QUICK_COMPILER)
+  llvm::LLVMContext* llvm_context = context_.release(); // Managed by quick_ctx_
+  CHECK(llvm_context != NULL);
 #endif
 }
 
@@ -221,7 +228,6 @@
   // Compile and prelink llvm::Module
   if (!MaterializeToString(elf_image)) {
     LOG(ERROR) << "Failed to materialize compilation unit " << cunit_idx_;
-    DeleteResources();
     return false;
   }
 
@@ -237,11 +243,9 @@
   // Extract the .text section and prelink the code
   if (!ExtractCodeAndPrelink(elf_image)) {
     LOG(ERROR) << "Failed to extract code from compilation unit " << cunit_idx_;
-    DeleteResources();
     return false;
   }
 
-  DeleteResources();
   return true;
 }
 
@@ -327,7 +331,7 @@
 #if defined(ART_USE_DEXLANG_FRONTEND)
     fpm.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get()));
 #elif defined(ART_USE_QUICK_COMPILER)
-    fpm.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get(),
+    fpm.add(CreateGBCExpanderPass(*quick_ctx_->GetIntrinsicHelper(), *irb_.get(),
                                   compiler_, oat_compilation_unit_));
 #endif
     fpm.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
@@ -337,7 +341,7 @@
 #if defined(ART_USE_DEXLANG_FRONTEND)
     fpm2.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get()));
 #elif defined(ART_USE_QUICK_COMPILER)
-    fpm2.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get(),
+    fpm2.add(CreateGBCExpanderPass(*quick_ctx_->GetIntrinsicHelper(), *irb_.get(),
                                    compiler_, oat_compilation_unit_));
 #endif
     fpm2.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
diff --git a/src/compiler_llvm/compilation_unit.h b/src/compiler_llvm/compilation_unit.h
index 6244eea..c4fbae4 100644
--- a/src/compiler_llvm/compilation_unit.h
+++ b/src/compiler_llvm/compilation_unit.h
@@ -19,7 +19,7 @@
 
 #include "../mutex.h"
 #include "globals.h"
-#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
+#if defined(ART_USE_DEXLANG_FRONTEND)
 # include "greenland/dex_lang.h"
 #endif
 #include "instruction_set.h"
@@ -29,6 +29,7 @@
 #include "safe_map.h"
 
 #if defined(ART_USE_QUICK_COMPILER)
+# include "compiler/Dalvik.h"
 # include "compiler.h"
 # include "oat_compilation_unit.h"
 #endif
@@ -79,7 +80,7 @@
     return irb_.get();
   }
 
-#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
+#if defined(ART_USE_DEXLANG_FRONTEND)
   greenland::DexLang::Context* GetDexLangContext() const {
     return dex_lang_ctx_;
   }
@@ -90,6 +91,9 @@
   }
 
 #if defined(ART_USE_QUICK_COMPILER)
+  QuickCompiler* GetQuickContext() const {
+    return quick_ctx_.get();
+  }
   void SetCompiler(Compiler* compiler) {
     compiler_ = compiler;
   }
@@ -101,7 +105,7 @@
   bool Materialize();
 
   bool IsMaterialized() const {
-    return (context_.get() == NULL);
+    return !compiled_code_.empty();
   }
 
   const std::vector<uint8_t>& GetCompiledCode() const {
@@ -116,11 +120,12 @@
   UniquePtr<llvm::LLVMContext> context_;
   UniquePtr<IRBuilder> irb_;
   UniquePtr<RuntimeSupportBuilder> runtime_support_;
-  llvm::Module* module_;
-#if defined(ART_USE_DEXLANG_FRONTEND) || defined(ART_USE_QUICK_COMPILER)
+  llvm::Module* module_; // Managed by context_
+#if defined(ART_USE_DEXLANG_FRONTEND)
   greenland::DexLang::Context* dex_lang_ctx_;
 #endif
 #if defined(ART_USE_QUICK_COMPILER)
+  UniquePtr<QuickCompiler> quick_ctx_;
   Compiler* compiler_;
   OatCompilationUnit* oat_compilation_unit_;
 #endif
@@ -133,13 +138,6 @@
 
   void CheckCodeAlign(uint32_t offset) const;
 
-  void DeleteResources() {
-    module_ = NULL; // Managed by context_
-    context_.reset(NULL);
-    irb_.reset(NULL);
-    runtime_support_.reset(NULL);
-  }
-
   bool MaterializeToString(std::string& str_buffer);
   bool MaterializeToRawOStream(llvm::raw_ostream& out_stream);
 
diff --git a/src/compiler_llvm/compiler_llvm.cc b/src/compiler_llvm/compiler_llvm.cc
index b81bfdd..f65f684 100644
--- a/src/compiler_llvm/compiler_llvm.cc
+++ b/src/compiler_llvm/compiler_llvm.cc
@@ -45,10 +45,7 @@
                            uint32_t access_flags, InvokeType invoke_type,
                            uint32_t method_idx, jobject class_loader,
                            const DexFile& dex_file,
-                           llvm::Module* module,
-                           llvm::LLVMContext* context,
-                           greenland::IntrinsicHelper* intrinsic_helper,
-                           greenland::IRBuilder* irb);
+                           QuickCompiler* quick_compiler);
 }
 #endif
 
@@ -166,11 +163,6 @@
     return method_compiler->Compile();
   } else {
     // Use quick
-    llvm::LLVMContext* context = cunit->GetLLVMContext();
-    llvm::Module* module = cunit->GetModule();
-    greenland::IntrinsicHelper* intrinsic_helper = &cunit->GetDexLangContext()->GetIntrinsicHelper();
-    UniquePtr<greenland::IRBuilder> greenland_irbuilder(
-        new greenland::IRBuilder(*context, *module, *intrinsic_helper));
     oatCompileMethodToGBC(*compiler_,
                           oat_compilation_unit->GetCodeItem(),
                           oat_compilation_unit->access_flags_,
@@ -178,10 +170,7 @@
                           oat_compilation_unit->GetDexMethodIndex(),
                           oat_compilation_unit->GetClassLoader(),
                           *oat_compilation_unit->GetDexFile(),
-                          module,
-                          context,
-                          intrinsic_helper,
-                          greenland_irbuilder.get()
+                          cunit->GetQuickContext()
                           );
 
     cunit->SetCompiler(compiler_);
diff --git a/src/compiler_llvm/compiler_llvm.h b/src/compiler_llvm/compiler_llvm.h
index dabd7be..e9ccd4d 100644
--- a/src/compiler_llvm/compiler_llvm.h
+++ b/src/compiler_llvm/compiler_llvm.h
@@ -98,8 +98,8 @@
 
   InstructionSet insn_set_;
 
-  Mutex num_cunits_lock_;
-  size_t num_cunits_;
+  Mutex num_cunits_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
+  size_t num_cunits_ GUARDED_BY(num_cunits_lock_);
 
   std::string bitcode_filename_;
 
diff --git a/src/compiler_llvm/gbc_expander.cc b/src/compiler_llvm/gbc_expander.cc
index 710d540..89dcf1d 100644
--- a/src/compiler_llvm/gbc_expander.cc
+++ b/src/compiler_llvm/gbc_expander.cc
@@ -73,7 +73,7 @@
   std::vector<llvm::BasicBlock*> basic_blocks_;
 
   std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
-  llvm::BasicBlock* old_basic_block_;
+  llvm::BasicBlock* current_bb_;
   std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
       landing_pad_phi_mapping_;
   llvm::BasicBlock* basic_block_unwind_;
@@ -454,7 +454,7 @@
     // Set insert point to current basic block.
     irb_.SetInsertPoint(bb_iter);
 
-    old_basic_block_ = bb_iter->getUniquePredecessor();
+    current_bb_ = bb_iter;
 
     // Rewrite the basic block
     RewriteBasicBlock(bb_iter);
@@ -2675,7 +2675,7 @@
 
 void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
   if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
-    landing_pad_phi_mapping_[lpad].push_back(std::make_pair(old_basic_block_,
+    landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
                                                             irb_.GetInsertBlock()));
     irb_.CreateBr(lpad);
   } else {
@@ -2689,7 +2689,7 @@
   llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
 
   if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
-    landing_pad_phi_mapping_[lpad].push_back(std::make_pair(old_basic_block_,
+    landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
                                                             irb_.GetInsertBlock()));
     irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
   } else {
@@ -3612,6 +3612,7 @@
 
     //==- Exception --------------------------------------------------------==//
     case IntrinsicHelper::CatchTargets: {
+      UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
       llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
       CHECK(si != NULL);
       irb_.CreateBr(si->getDefaultDest());