Add JIT

Currently disabled by default unless -Xjit is passed in.

The proposed JIT is a method JIT which works by utilizing interpreter
instrumentation to request compilation of hot methods async during
runtime.

JIT options:
-Xjit / -Xnojit
-Xjitcodecachesize:N
-Xjitthreshold:integervalue

The JIT has a shared copy of a compiler driver which is accessed
by worker threads to compile individual methods.

Added JIT code cache and data cache, currently sized at 2 MB
capacity by default. Most apps will only fill a small fraction of
this cache however.

Added support to the compiler for compiling interpreter quickened
byte codes.

Added test target ART_TEST_JIT=TRUE and --jit for run-test.

TODO:
Clean up code cache.
Delete compiled methods after they are added to code cache.
Add more optimizations related to runtime checks e.g. direct pointers
for invokes.
Add method recompilation.
Move instrumentation to DexFile to improve performance and reduce
memory usage.

Bug: 17950037

Change-Id: Ifa5b2684a2d5059ec5a5210733900aafa3c51bca
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index ee66b49..8fe2ec8 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -43,6 +43,8 @@
 #include "handle_scope.h"
 #include "intern_table.h"
 #include "interpreter/interpreter.h"
+#include "jit/jit.h"
+#include "jit/jit_code_cache.h"
 #include "leb128.h"
 #include "oat.h"
 #include "oat_file.h"
@@ -91,15 +93,14 @@
   // a NoClassDefFoundError (v2 2.17.5).  The exception to this rule is if we
   // failed in verification, in which case v2 5.4.1 says we need to re-throw
   // the previous error.
-  Runtime* runtime = Runtime::Current();
-  bool is_compiler = runtime->IsCompiler();
-  if (!is_compiler) {  // Give info if this occurs at runtime.
+  Runtime* const runtime = Runtime::Current();
+  if (!runtime->IsAotCompiler()) {  // Give info if this occurs at runtime.
     LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c);
   }
 
   CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus();
   Thread* self = Thread::Current();
-  if (is_compiler) {
+  if (runtime->IsAotCompiler()) {
     // At compile time, accurate errors and NCDFE are disabled to speed compilation.
     mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError();
     self->SetException(ThrowLocation(), pre_allocated);
@@ -428,7 +429,7 @@
   // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that
   // we do not need friend classes or a publicly exposed setter.
   quick_generic_jni_trampoline_ = GetQuickGenericJniStub();
-  if (!runtime->IsCompiler()) {
+  if (!runtime->IsAotCompiler()) {
     // We need to set up the generic trampolines since we don't have an image.
     quick_resolution_trampoline_ = GetQuickResolutionStub();
     quick_imt_conflict_trampoline_ = GetQuickImtConflictStub();
@@ -1032,8 +1033,7 @@
                                                                const char* oat_location,
                                                                std::string* error_msg) {
   std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, nullptr,
-                                            !Runtime::Current()->IsCompiler(),
-                                            error_msg));
+                                                  !Runtime::Current()->IsAotCompiler(), error_msg));
   if (oat_file.get() == nullptr) {
     *error_msg = StringPrintf("Failed to find existing oat file at %s: %s", oat_location,
                               error_msg->c_str());
@@ -1104,8 +1104,8 @@
     return nullptr;
   }
   std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, nullptr,
-                                            !Runtime::Current()->IsCompiler(),
-                                            &error_msg));
+                                                  !Runtime::Current()->IsAotCompiler(),
+                                                  &error_msg));
   if (oat_file.get() == nullptr) {
     std::string compound_msg = StringPrintf("\nFailed to open generated oat file '%s': %s",
                                             oat_location, error_msg.c_str());
@@ -1345,7 +1345,7 @@
   *already_opened = false;
   const Runtime* runtime = Runtime::Current();
   CHECK(runtime != nullptr);
-  bool executable = !runtime->IsCompiler();
+  bool executable = !runtime->IsAotCompiler();
 
   std::string odex_error_msg;
   bool should_patch_system = false;
@@ -1513,7 +1513,7 @@
   bool success = Exec(argv, error_msg);
   if (success) {
     std::unique_ptr<OatFile> output(OatFile::Open(output_oat, output_oat, nullptr, nullptr,
-                                                  !runtime->IsCompiler(), error_msg));
+                                                  !runtime->IsAotCompiler(), error_msg));
     bool checksum_verified = false;
     if (output.get() != nullptr && CheckOatFile(runtime, output.get(), isa, &checksum_verified,
                                                 error_msg)) {
@@ -1527,7 +1527,7 @@
                                 "but was unable to open output file '%s': %s",
                                 input_oat.c_str(), output_oat.c_str(), error_msg->c_str());
     }
-  } else if (!runtime->IsCompiler()) {
+  } else if (!runtime->IsAotCompiler()) {
     // patchoat failed which means we probably don't have enough room to place the output oat file,
     // instead of failing we should just run the interpreter from the dex files in the input oat.
     LOG(WARNING) << "Patching of oat file '" << input_oat << "' failed. Attempting to use oat file "
@@ -1614,22 +1614,20 @@
   if (oat_file != nullptr) {
     return oat_file;
   }
-
-  return OatFile::Open(oat_location, oat_location, nullptr, nullptr, !Runtime::Current()->IsCompiler(),
-                       error_msg);
+  return OatFile::Open(oat_location, oat_location, nullptr, nullptr,
+                       !Runtime::Current()->IsAotCompiler(), error_msg);
 }
 
 void ClassLinker::InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg) {
   ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg);
   DCHECK(obj != nullptr);
   DCHECK(class_linker != nullptr);
-  size_t pointer_size = class_linker->image_pointer_size_;
-
   if (obj->IsArtMethod()) {
     mirror::ArtMethod* method = obj->AsArtMethod();
     if (!method->IsNative()) {
+      const size_t pointer_size = class_linker->image_pointer_size_;
       method->SetEntryPointFromInterpreterPtrSize(artInterpreterToInterpreterBridge, pointer_size);
-      if (method != Runtime::Current()->GetResolutionMethod()) {
+      if (!method->IsRuntimeMethod() && method != Runtime::Current()->GetResolutionMethod()) {
         method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(),
                                                           pointer_size);
       }
@@ -1698,8 +1696,8 @@
   // bitmap walk.
   mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod));
   size_t art_method_object_size = mirror::ArtMethod::GetJavaLangReflectArtMethod()->GetObjectSize();
-  if (!Runtime::Current()->IsCompiler()) {
-    // Compiler supports having an image with a different pointer size than the runtime. This
+  if (!Runtime::Current()->IsAotCompiler()) {
+    // Aot compiler supports having an image with a different pointer size than the runtime. This
     // happens on the host for compile 32 bit tests since we use a 64 bit libart compiler. We may
     // also use 32 bit dex2oat on a system with 64 bit apps.
     CHECK_EQ(art_method_object_size, mirror::ArtMethod::InstanceSize(sizeof(void*)))
@@ -1714,7 +1712,7 @@
 
   // Set entry point to interpreter if in InterpretOnly mode.
   Runtime* runtime = Runtime::Current();
-  if (!runtime->IsCompiler() && runtime->GetInstrumentation()->InterpretOnly()) {
+  if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) {
     heap->VisitObjects(InitFromImageInterpretOnlyCallback, this);
   }
 
@@ -2517,31 +2515,44 @@
     return GetQuickProxyInvokeHandler();
   }
   bool found;
-  OatFile::OatMethod oat_method = FindOatMethodFor(method, &found);
-  const void* result = nullptr;
-  if (found) {
-    result = oat_method.GetQuickCode();
-  }
-
-  if (result == nullptr) {
-    if (method->IsNative()) {
-      // No code and native? Use generic trampoline.
-      result = GetQuickGenericJniStub();
-    } else {
-      // No code? You must mean to go into the interpreter.
-      result = GetQuickToInterpreterBridge();
+  jit::Jit* const jit = Runtime::Current()->GetJit();
+  if (jit != nullptr) {
+    auto* code = jit->GetCodeCache()->GetCodeFor(method);
+    if (code != nullptr) {
+      return code;
     }
   }
-  return result;
+  OatFile::OatMethod oat_method = FindOatMethodFor(method, &found);
+  if (found) {
+    auto* code = oat_method.GetQuickCode();
+    if (code != nullptr) {
+      return code;
+    }
+  }
+  if (method->IsNative()) {
+    // No code and native? Use generic trampoline.
+    return GetQuickGenericJniStub();
+  }
+  return GetQuickToInterpreterBridge();
 }
 
 const void* ClassLinker::GetOatMethodQuickCodeFor(mirror::ArtMethod* method) {
   if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) {
     return nullptr;
   }
+  jit::Jit* jit = Runtime::Current()->GetJit();
+  if (jit != nullptr) {
+    auto* code = jit->GetCodeCache()->GetCodeFor(method);
+    if (code != nullptr) {
+      return code;
+    }
+  }
   bool found;
   OatFile::OatMethod oat_method = FindOatMethodFor(method, &found);
-  return found ? oat_method.GetQuickCode() : nullptr;
+  if (found) {
+    return oat_method.GetQuickCode();
+  }
+  return nullptr;
 }
 
 const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx,
@@ -2577,7 +2588,7 @@
   }
   Runtime* runtime = Runtime::Current();
   if (!runtime->IsStarted() || runtime->UseCompileTimeClassPath()) {
-    if (runtime->IsCompiler() || runtime->GetHeap()->HasImageSpace()) {
+    if (runtime->IsAotCompiler() || runtime->GetHeap()->HasImageSpace()) {
       return;  // OAT file unavailable.
     }
   }
@@ -2630,7 +2641,7 @@
                            const OatFile::OatClass* oat_class,
                            uint32_t class_def_method_index) {
   Runtime* runtime = Runtime::Current();
-  if (runtime->IsCompiler()) {
+  if (runtime->IsAotCompiler()) {
     // The following code only applies to a non-compiler runtime.
     return;
   }
@@ -3469,7 +3480,7 @@
     EnsurePreverifiedMethods(klass);
     return;
   }
-  if (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler()) {
+  if (klass->IsCompileTimeVerified() && Runtime::Current()->IsAotCompiler()) {
     return;
   }
 
@@ -3485,7 +3496,7 @@
   } else {
     CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime)
         << PrettyClass(klass.Get());
-    CHECK(!Runtime::Current()->IsCompiler());
+    CHECK(!Runtime::Current()->IsAotCompiler());
     klass->SetStatus(mirror::Class::kStatusVerifyingAtRuntime, self);
   }
 
@@ -3521,7 +3532,7 @@
         self->GetException(nullptr)->SetCause(cause.Get());
       }
       ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex());
-      if (Runtime::Current()->IsCompiler()) {
+      if (Runtime::Current()->IsAotCompiler()) {
         Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
       }
       klass->SetStatus(mirror::Class::kStatusError, self);
@@ -3546,7 +3557,7 @@
   std::string error_msg;
   if (!preverified) {
     verifier_failure = verifier::MethodVerifier::VerifyClass(self, klass.Get(),
-                                                             Runtime::Current()->IsCompiler(),
+                                                             Runtime::Current()->IsAotCompiler(),
                                                              &error_msg);
   }
   if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) {
@@ -3574,7 +3585,7 @@
       // Soft failures at compile time should be retried at runtime. Soft
       // failures at runtime will be handled by slow paths in the generated
       // code. Set status accordingly.
-      if (Runtime::Current()->IsCompiler()) {
+      if (Runtime::Current()->IsAotCompiler()) {
         klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self);
       } else {
         klass->SetStatus(mirror::Class::kStatusVerified, self);
@@ -3615,7 +3626,7 @@
   // we are not compiling the image or if the class we're verifying is not part of
   // the app.  In other words, we will only check for preverification of bootclasspath
   // classes.
-  if (Runtime::Current()->IsCompiler()) {
+  if (Runtime::Current()->IsAotCompiler()) {
     // Are we compiling the bootclasspath?
     if (!Runtime::Current()->UseCompileTimeClassPath()) {
       return false;
@@ -3641,7 +3652,7 @@
   // image (that we just failed loading), and the verifier can't be run on quickened opcodes when
   // the runtime isn't started. On the other hand, app classes can be re-verified even if they are
   // already pre-opted, as then the runtime is started.
-  if (!Runtime::Current()->IsCompiler() &&
+  if (!Runtime::Current()->IsAotCompiler() &&
       !Runtime::Current()->GetHeap()->HasImageSpace() &&
       klass->GetClassLoader() != nullptr) {
     return false;
@@ -4089,7 +4100,7 @@
           CHECK(self->IsExceptionPending());
           VlogClassInitializationFailure(klass);
         } else {
-          CHECK(Runtime::Current()->IsCompiler());
+          CHECK(Runtime::Current()->IsAotCompiler());
           CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime);
         }
         return false;
@@ -4270,7 +4281,8 @@
     if (klass->GetStatus() == mirror::Class::kStatusInitializing) {
       continue;
     }
-    if (klass->GetStatus() == mirror::Class::kStatusVerified && Runtime::Current()->IsCompiler()) {
+    if (klass->GetStatus() == mirror::Class::kStatusVerified &&
+        Runtime::Current()->IsAotCompiler()) {
       // Compile time initialization failed.
       return false;
     }