Fix JIT options with late init

Previously we couldn't create the JIT since we didn't have any
JITOptions.

Bug: 19735273

Change-Id: I24b8ed131ed8b18f75ec94291e135d8a1c089ebd
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 189559e..0f0c327 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -483,11 +483,10 @@
     }
   }
 
-  // If we are the zygote then we need to wait until after forking to create the code cache due to
-  // SELinux restrictions on r/w/x memory regions.
-  if (!IsZygote() && jit_.get() != nullptr) {
-    jit_->CreateInstrumentationCache(jit_options_->GetCompileThreshold());
-    jit_->CreateThreadPool();
+  // If we are the zygote then we need to wait until after forking to create the code cache
+  // due to SELinux restrictions on r/w/x memory regions.
+  if (!IsZygote() && jit_options_->UseJIT()) {
+    CreateJit();
   }
 
   if (!IsImageDex2OatEnabled() || !GetHeap()->HasImageSpace()) {
@@ -611,11 +610,9 @@
 
   // Create the thread pools.
   heap_->CreateThreadPool();
-  if (jit_options_.get() != nullptr && jit_.get() == nullptr) {
+  if (jit_.get() == nullptr && jit_options_->UseJIT()) {
     // Create the JIT if the flag is set and we haven't already create it (happens for run-tests).
     CreateJit();
-    jit_->CreateInstrumentationCache(jit_options_->GetCompileThreshold());
-    jit_->CreateThreadPool();
   }
 
   StartSignalCatcher();
@@ -843,20 +840,19 @@
     Dbg::ConfigureJdwp(runtime_options.GetOrDefault(Opt::JdwpOptions));
   }
 
-  if (!IsAotCompiler()) {
+  jit_options_.reset(jit::JitOptions::CreateFromRuntimeArguments(runtime_options));
+  bool use_jit = jit_options_->UseJIT();
+  if (IsAotCompiler()) {
     // If we are already the compiler at this point, we must be dex2oat. Don't create the jit in
     // this case.
     // If runtime_options doesn't have UseJIT set to true then CreateFromRuntimeArguments returns
     // nullptr and we don't create the jit.
-    jit_options_.reset(jit::JitOptions::CreateFromRuntimeArguments(runtime_options));
-  }
-  if (!IsZygote() && jit_options_.get() != nullptr) {
-    CreateJit();
+    use_jit = false;
   }
 
   // Use MemMap arena pool for jit, malloc otherwise. Malloc arenas are faster to allocate but
   // can't be trimmed as easily.
-  const bool use_malloc = jit_options_.get() == nullptr;
+  const bool use_malloc = !use_jit;
   arena_pool_.reset(new ArenaPool(use_malloc));
 
   BlockSignals();
@@ -1661,11 +1657,13 @@
 }
 
 void Runtime::CreateJit() {
-  CHECK(jit_options_.get() != nullptr);
+  CHECK(!IsAotCompiler());
   std::string error_msg;
   jit_.reset(jit::Jit::Create(jit_options_.get(), &error_msg));
   if (jit_.get() != nullptr) {
     compiler_callbacks_ = jit_->GetCompilerCallbacks();
+    jit_->CreateInstrumentationCache(jit_options_->GetCompileThreshold());
+    jit_->CreateThreadPool();
   } else {
     LOG(WARNING) << "Failed to create JIT " << error_msg;
   }