Fix quick fly2iceland after rebase.

Change-Id: I844f005782b3ecdcb52dc2484d44f4ae34e1c670
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());