Move mirror::ArtMethod to native

Optimizing + quick tests are passing, devices boot.

TODO: Test and fix bugs in mips64.

Saves 16 bytes per most ArtMethod, 7.5MB reduction in system PSS.
Some of the savings are from removal of virtual methods and direct
methods object arrays.

Bug: 19264997
Change-Id: I622469a0cfa0e7082a2119f3d6a9491eb61e3f3d
diff --git a/runtime/native/dalvik_system_VMRuntime.cc b/runtime/native/dalvik_system_VMRuntime.cc
index a172197..5dd354d 100644
--- a/runtime/native/dalvik_system_VMRuntime.cc
+++ b/runtime/native/dalvik_system_VMRuntime.cc
@@ -27,6 +27,7 @@
 #include "toStringArray.h"
 #pragma GCC diagnostic pop
 
+#include "art_method-inl.h"
 #include "arch/instruction_set.h"
 #include "class_linker-inl.h"
 #include "common_throws.h"
@@ -40,7 +41,6 @@
 #include "gc/task_processor.h"
 #include "intern_table.h"
 #include "jni_internal.h"
-#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
 #include "mirror/dex_cache-inl.h"
 #include "mirror/object-inl.h"
@@ -350,7 +350,7 @@
 static void PreloadDexCachesResolveMethod(Handle<mirror::DexCache> dex_cache, uint32_t method_idx,
                                           InvokeType invoke_type)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
-  mirror::ArtMethod* method = dex_cache->GetResolvedMethod(method_idx);
+  ArtMethod* method = dex_cache->GetResolvedMethod(method_idx, sizeof(void*));
   if (method != nullptr) {
     return;
   }
@@ -363,14 +363,14 @@
   switch (invoke_type) {
     case kDirect:
     case kStatic:
-      method = klass->FindDirectMethod(dex_cache.Get(), method_idx);
+      method = klass->FindDirectMethod(dex_cache.Get(), method_idx, sizeof(void*));
       break;
     case kInterface:
-      method = klass->FindInterfaceMethod(dex_cache.Get(), method_idx);
+      method = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, sizeof(void*));
       break;
     case kSuper:
     case kVirtual:
-      method = klass->FindVirtualMethod(dex_cache.Get(), method_idx);
+      method = klass->FindVirtualMethod(dex_cache.Get(), method_idx, sizeof(void*));
       break;
     default:
       LOG(FATAL) << "Unreachable - invocation type: " << invoke_type;
@@ -380,7 +380,7 @@
     return;
   }
   // LOG(INFO) << "VMRuntime.preloadDexCaches resolved method " << PrettyMethod(method);
-  dex_cache->SetResolvedMethod(method_idx, method);
+  dex_cache->SetResolvedMethod(method_idx, method, sizeof(void*));
 }
 
 struct DexCacheStats {
@@ -452,7 +452,7 @@
       }
     }
     for (size_t j = 0; j < dex_cache->NumResolvedMethods(); j++) {
-      mirror::ArtMethod* method = dex_cache->GetResolvedMethod(j);
+      ArtMethod* method = dex_cache->GetResolvedMethod(j, sizeof(void*));
       if (method != nullptr) {
         filled->num_methods++;
       }
diff --git a/runtime/native/dalvik_system_VMStack.cc b/runtime/native/dalvik_system_VMStack.cc
index 1d7d853..ee62755 100644
--- a/runtime/native/dalvik_system_VMStack.cc
+++ b/runtime/native/dalvik_system_VMStack.cc
@@ -16,9 +16,9 @@
 
 #include "dalvik_system_VMStack.h"
 
+#include "art_method-inl.h"
 #include "jni_internal.h"
 #include "nth_caller_visitor.h"
-#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
 #include "mirror/class_loader.h"
 #include "mirror/object-inl.h"
@@ -90,10 +90,13 @@
     bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
       DCHECK(class_loader == nullptr);
       mirror::Class* c = GetMethod()->GetDeclaringClass();
-      mirror::Object* cl = c->GetClassLoader();
-      if (cl != nullptr) {
-        class_loader = cl;
-        return false;
+      // c is null for runtime methods.
+      if (c != nullptr) {
+        mirror::Object* cl = c->GetClassLoader();
+        if (cl != nullptr) {
+          class_loader = cl;
+          return false;
+        }
       }
       return true;
     }
diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc
index 795a0ea..94024ef 100644
--- a/runtime/native/java_lang_Class.cc
+++ b/runtime/native/java_lang_Class.cc
@@ -273,7 +273,7 @@
   return nullptr;
 }
 
-static ALWAYS_INLINE inline bool MethodMatchesConstructor(mirror::ArtMethod* m, bool public_only)
+static ALWAYS_INLINE inline bool MethodMatchesConstructor(ArtMethod* m, bool public_only)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   DCHECK(m != nullptr);
   return (!public_only || m->IsPublic()) && !m->IsStatic() && m->IsConstructor();
@@ -283,14 +283,11 @@
     JNIEnv* env, jobject javaThis, jboolean publicOnly) {
   ScopedFastNativeObjectAccess soa(env);
   auto* klass = DecodeClass(soa, javaThis);
-  StackHandleScope<2> hs(soa.Self());
-  auto h_direct_methods = hs.NewHandle(klass->GetDirectMethods());
+  StackHandleScope<1> hs(soa.Self());
   size_t constructor_count = 0;
-  auto count = h_direct_methods.Get() != nullptr ? h_direct_methods->GetLength() : 0u;
   // Two pass approach for speed.
-  for (size_t i = 0; i < count; ++i) {
-    constructor_count += MethodMatchesConstructor(h_direct_methods->GetWithoutChecks(i),
-                                                  publicOnly != JNI_FALSE) ? 1u : 0u;
+  for (auto& m : klass->GetDirectMethods(sizeof(void*))) {
+    constructor_count += MethodMatchesConstructor(&m, publicOnly != JNI_FALSE) ? 1u : 0u;
   }
   auto h_constructors = hs.NewHandle(mirror::ObjectArray<mirror::Constructor>::Alloc(
       soa.Self(), mirror::Constructor::ArrayClass(), constructor_count));
@@ -299,12 +296,11 @@
     return nullptr;
   }
   constructor_count = 0;
-  for (size_t i = 0; i < count; ++i) {
-    auto* method = h_direct_methods->GetWithoutChecks(i);
-    if (MethodMatchesConstructor(method, publicOnly != JNI_FALSE)) {
-      auto* constructor = mirror::Constructor::CreateFromArtMethod(soa.Self(), method);
+  for (auto& m : klass->GetDirectMethods(sizeof(void*))) {
+    if (MethodMatchesConstructor(&m, publicOnly != JNI_FALSE)) {
+      auto* constructor = mirror::Constructor::CreateFromArtMethod(soa.Self(), &m);
       if (UNLIKELY(constructor == nullptr)) {
-        soa.Self()->AssertPendingException();
+        soa.Self()->AssertPendingOOMException();
         return nullptr;
       }
       h_constructors->SetWithoutChecks<false>(constructor_count++, constructor);
@@ -323,7 +319,7 @@
   // were synthesized by the runtime.
   constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
   ScopedFastNativeObjectAccess soa(env);
-  StackHandleScope<5> hs(soa.Self());
+  StackHandleScope<4> hs(soa.Self());
   auto h_method_name = hs.NewHandle(soa.Decode<mirror::String*>(name));
   if (UNLIKELY(h_method_name.Get() == nullptr)) {
     ThrowNullPointerException("name == null");
@@ -331,60 +327,49 @@
   }
   auto h_args = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(args));
   auto* klass = DecodeClass(soa, javaThis);
-  mirror::ArtMethod* result = nullptr;
-  auto* virtual_methods = klass->GetVirtualMethods();
-  if (virtual_methods != nullptr) {
-    auto h_virtual_methods = hs.NewHandle(virtual_methods);
-    for (size_t i = 0, count = virtual_methods->GetLength(); i < count; ++i) {
-      auto* m = h_virtual_methods->GetWithoutChecks(i);
-      auto* np_method = m->GetInterfaceMethodIfProxy();
+  ArtMethod* result = nullptr;
+  for (auto& m : klass->GetVirtualMethods(sizeof(void*))) {
+    auto* np_method = m.GetInterfaceMethodIfProxy(sizeof(void*));
+    // May cause thread suspension.
+    mirror::String* np_name = np_method->GetNameAsString(soa.Self());
+    if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
+      if (UNLIKELY(soa.Self()->IsExceptionPending())) {
+        return nullptr;
+      }
+      continue;
+    }
+    auto modifiers = m.GetAccessFlags();
+    if ((modifiers & kSkipModifiers) == 0) {
+      return soa.AddLocalReference<jobject>(mirror::Method::CreateFromArtMethod(soa.Self(), &m));
+    }
+    if ((modifiers & kAccMiranda) == 0) {
+      result = &m;  // Remember as potential result if it's not a miranda method.
+    }
+  }
+  if (result == nullptr) {
+    for (auto& m : klass->GetDirectMethods(sizeof(void*))) {
+      auto modifiers = m.GetAccessFlags();
+      if ((modifiers & kAccConstructor) != 0) {
+        continue;
+      }
+      auto* np_method = m.GetInterfaceMethodIfProxy(sizeof(void*));
       // May cause thread suspension.
       mirror::String* np_name = np_method->GetNameAsString(soa.Self());
+      if (np_name == nullptr) {
+        soa.Self()->AssertPendingException();
+        return nullptr;
+      }
       if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
         if (UNLIKELY(soa.Self()->IsExceptionPending())) {
           return nullptr;
         }
         continue;
       }
-      auto modifiers = m->GetAccessFlags();
       if ((modifiers & kSkipModifiers) == 0) {
-        return soa.AddLocalReference<jobject>(mirror::Method::CreateFromArtMethod(soa.Self(), m));
+        return soa.AddLocalReference<jobject>(mirror::Method::CreateFromArtMethod(soa.Self(), &m));
       }
-      if ((modifiers & kAccMiranda) == 0) {
-        result = m;  // Remember as potential result if it's not a miranda method.
-      }
-    }
-  }
-  if (result == nullptr) {
-    auto* direct_methods = klass->GetDirectMethods();
-    if (direct_methods != nullptr) {
-      auto h_direct_methods = hs.NewHandle(direct_methods);
-      for (size_t i = 0, count = direct_methods->GetLength(); i < count; ++i) {
-        auto* m = h_direct_methods->GetWithoutChecks(i);
-        auto modifiers = m->GetAccessFlags();
-        if ((modifiers & kAccConstructor) != 0) {
-          continue;
-        }
-        auto* np_method = m->GetInterfaceMethodIfProxy();
-        // May cause thread suspension.
-        mirror::String* np_name = np_method ->GetNameAsString(soa.Self());
-        if (np_name == nullptr) {
-          soa.Self()->AssertPendingException();
-          return nullptr;
-        }
-        if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
-          if (UNLIKELY(soa.Self()->IsExceptionPending())) {
-            return nullptr;
-          }
-          continue;
-        }
-        if ((modifiers & kSkipModifiers) == 0) {
-          return soa.AddLocalReference<jobject>(mirror::Method::CreateFromArtMethod(
-              soa.Self(), m));
-        }
-        // Direct methods cannot be miranda methods, so this potential result must be synthetic.
-        result = m;
-      }
+      // Direct methods cannot be miranda methods, so this potential result must be synthetic.
+      result = &m;
     }
   }
   return result != nullptr ?
@@ -395,64 +380,50 @@
 static jobjectArray Class_getDeclaredMethodsUnchecked(JNIEnv* env, jobject javaThis,
                                                       jboolean publicOnly) {
   ScopedFastNativeObjectAccess soa(env);
-  StackHandleScope<5> hs(soa.Self());
+  StackHandleScope<3> hs(soa.Self());
   auto* klass = DecodeClass(soa, javaThis);
-  auto virtual_methods = hs.NewHandle(klass->GetVirtualMethods());
-  auto direct_methods = hs.NewHandle(klass->GetDirectMethods());
   size_t num_methods = 0;
-  if (virtual_methods.Get() != nullptr) {
-    for (size_t i = 0, count = virtual_methods->GetLength(); i < count; ++i) {
-      auto* m = virtual_methods->GetWithoutChecks(i);
-      auto modifiers = m->GetAccessFlags();
-      if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
-          (modifiers & kAccMiranda) == 0) {
-        ++num_methods;
-      }
+  for (auto& m : klass->GetVirtualMethods(sizeof(void*))) {
+    auto modifiers = m.GetAccessFlags();
+    if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
+        (modifiers & kAccMiranda) == 0) {
+      ++num_methods;
     }
   }
-  if (direct_methods.Get() != nullptr) {
-    for (size_t i = 0, count = direct_methods->GetLength(); i < count; ++i) {
-      auto* m = direct_methods->GetWithoutChecks(i);
-      auto modifiers = m->GetAccessFlags();
-      // Add non-constructor direct/static methods.
-      if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
-          (modifiers & kAccConstructor) == 0) {
-        ++num_methods;
-      }
+  for (auto& m : klass->GetDirectMethods(sizeof(void*))) {
+    auto modifiers = m.GetAccessFlags();
+    // Add non-constructor direct/static methods.
+    if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
+        (modifiers & kAccConstructor) == 0) {
+      ++num_methods;
     }
   }
   auto ret = hs.NewHandle(mirror::ObjectArray<mirror::Method>::Alloc(
       soa.Self(), mirror::Method::ArrayClass(), num_methods));
   num_methods = 0;
-  if (virtual_methods.Get() != nullptr) {
-    for (size_t i = 0, count = virtual_methods->GetLength(); i < count; ++i) {
-      auto* m = virtual_methods->GetWithoutChecks(i);
-      auto modifiers = m->GetAccessFlags();
-      if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
-          (modifiers & kAccMiranda) == 0) {
-        auto* method = mirror::Method::CreateFromArtMethod(soa.Self(), m);
-        if (method == nullptr) {
-          soa.Self()->AssertPendingException();
-          return nullptr;
-        }
-        ret->SetWithoutChecks<false>(num_methods++, method);
+  for (auto& m : klass->GetVirtualMethods(sizeof(void*))) {
+    auto modifiers = m.GetAccessFlags();
+    if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
+        (modifiers & kAccMiranda) == 0) {
+      auto* method = mirror::Method::CreateFromArtMethod(soa.Self(), &m);
+      if (method == nullptr) {
+        soa.Self()->AssertPendingException();
+        return nullptr;
       }
+      ret->SetWithoutChecks<false>(num_methods++, method);
     }
   }
-  if (direct_methods.Get() != nullptr) {
-    for (size_t i = 0, count = direct_methods->GetLength(); i < count; ++i) {
-      auto* m = direct_methods->GetWithoutChecks(i);
-      auto modifiers = m->GetAccessFlags();
-      // Add non-constructor direct/static methods.
-      if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
-          (modifiers & kAccConstructor) == 0) {
-        auto* method = mirror::Method::CreateFromArtMethod(soa.Self(), m);
-        if (method == nullptr) {
-          soa.Self()->AssertPendingException();
-          return nullptr;
-        }
-        ret->SetWithoutChecks<false>(num_methods++, method);
+  for (auto& m : klass->GetDirectMethods(sizeof(void*))) {
+    auto modifiers = m.GetAccessFlags();
+    // Add non-constructor direct/static methods.
+    if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
+        (modifiers & kAccConstructor) == 0) {
+      auto* method = mirror::Method::CreateFromArtMethod(soa.Self(), &m);
+      if (method == nullptr) {
+        soa.Self()->AssertPendingException();
+        return nullptr;
       }
+      ret->SetWithoutChecks<false>(num_methods++, method);
     }
   }
   return soa.AddLocalReference<jobjectArray>(ret.Get());
diff --git a/runtime/native/java_lang_DexCache.cc b/runtime/native/java_lang_DexCache.cc
index b9f8d01..a2d9797 100644
--- a/runtime/native/java_lang_DexCache.cc
+++ b/runtime/native/java_lang_DexCache.cc
@@ -18,6 +18,7 @@
 
 #include "dex_file.h"
 #include "jni_internal.h"
+#include "mirror/class-inl.h"
 #include "mirror/dex_cache-inl.h"
 #include "mirror/object-inl.h"
 #include "scoped_fast_native_object_access.h"
diff --git a/runtime/native/java_lang_reflect_Constructor.cc b/runtime/native/java_lang_reflect_Constructor.cc
index 0fd6759..93ba84a 100644
--- a/runtime/native/java_lang_reflect_Constructor.cc
+++ b/runtime/native/java_lang_reflect_Constructor.cc
@@ -16,10 +16,9 @@
 
 #include "java_lang_reflect_Constructor.h"
 
+#include "art_method-inl.h"
 #include "class_linker.h"
 #include "jni_internal.h"
-#include "mirror/art_method.h"
-#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
 #include "mirror/method.h"
 #include "mirror/object-inl.h"
diff --git a/runtime/native/java_lang_reflect_Field.cc b/runtime/native/java_lang_reflect_Field.cc
index d6aa9b5..ba898c6 100644
--- a/runtime/native/java_lang_reflect_Field.cc
+++ b/runtime/native/java_lang_reflect_Field.cc
@@ -21,7 +21,6 @@
 #include "common_throws.h"
 #include "dex_file-inl.h"
 #include "jni_internal.h"
-#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
 #include "mirror/field.h"
 #include "reflection-inl.h"
diff --git a/runtime/native/java_lang_reflect_Method.cc b/runtime/native/java_lang_reflect_Method.cc
index c20d832..9533b4d 100644
--- a/runtime/native/java_lang_reflect_Method.cc
+++ b/runtime/native/java_lang_reflect_Method.cc
@@ -16,10 +16,9 @@
 
 #include "java_lang_reflect_Method.h"
 
+#include "art_method-inl.h"
 #include "class_linker.h"
 #include "jni_internal.h"
-#include "mirror/art_method.h"
-#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
 #include "mirror/object-inl.h"
 #include "mirror/object_array-inl.h"
@@ -37,16 +36,17 @@
 
 static jobject Method_getExceptionTypesNative(JNIEnv* env, jobject javaMethod) {
   ScopedFastNativeObjectAccess soa(env);
-  mirror::ArtMethod* proxy_method = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
+  ArtMethod* proxy_method = ArtMethod::FromReflectedMethod(soa, javaMethod);
   CHECK(proxy_method->GetDeclaringClass()->IsProxyClass());
   mirror::Class* proxy_class = proxy_method->GetDeclaringClass();
   int throws_index = -1;
-  size_t num_virt_methods = proxy_class->NumVirtualMethods();
-  for (size_t i = 0; i < num_virt_methods; i++) {
-    if (proxy_class->GetVirtualMethod(i) == proxy_method) {
+  size_t i = 0;
+  for (const auto& m : proxy_class->GetVirtualMethods(sizeof(void*))) {
+    if (&m == proxy_method) {
       throws_index = i;
       break;
     }
+    ++i;
   }
   CHECK_NE(throws_index, -1);
   mirror::ObjectArray<mirror::Class>* declared_exceptions =
diff --git a/runtime/native/scoped_fast_native_object_access.h b/runtime/native/scoped_fast_native_object_access.h
index dfabff5..57b873b 100644
--- a/runtime/native/scoped_fast_native_object_access.h
+++ b/runtime/native/scoped_fast_native_object_access.h
@@ -17,7 +17,7 @@
 #ifndef ART_RUNTIME_NATIVE_SCOPED_FAST_NATIVE_OBJECT_ACCESS_H_
 #define ART_RUNTIME_NATIVE_SCOPED_FAST_NATIVE_OBJECT_ACCESS_H_
 
-#include "mirror/art_method-inl.h"
+#include "art_method-inl.h"
 #include "scoped_thread_state_change.h"
 
 namespace art {
@@ -31,7 +31,7 @@
     SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE
      : ScopedObjectAccessAlreadyRunnable(env) {
     Locks::mutator_lock_->AssertSharedHeld(Self());
-    DCHECK(Self()->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr()->IsFastNative());
+    DCHECK((*Self()->GetManagedStack()->GetTopQuickFrame())->IsFastNative());
     // Don't work with raw objects in non-runnable states.
     DCHECK_EQ(Self()->GetState(), kRunnable);
   }
diff --git a/runtime/native/sun_misc_Unsafe.cc b/runtime/native/sun_misc_Unsafe.cc
index 17ebdff..770644c 100644
--- a/runtime/native/sun_misc_Unsafe.cc
+++ b/runtime/native/sun_misc_Unsafe.cc
@@ -19,7 +19,7 @@
 #include "gc/accounting/card_table-inl.h"
 #include "jni_internal.h"
 #include "mirror/array.h"
-#include "mirror/object.h"
+#include "mirror/class-inl.h"
 #include "mirror/object-inl.h"
 #include "scoped_fast_native_object_access.h"