ART: Add support for DexFile compiler filter function

Add support for getting a non-profile-guided version of a given
compiler filter.

Bug: 27921071
Change-Id: I54735b61732e6a5c9dc62425d04bc740dd365083
diff --git a/runtime/compiler_filter.cc b/runtime/compiler_filter.cc
index 31a1bc1..d617caf 100644
--- a/runtime/compiler_filter.cc
+++ b/runtime/compiler_filter.cc
@@ -83,6 +83,34 @@
   UNREACHABLE();
 }
 
+CompilerFilter::Filter CompilerFilter::GetNonProfileDependentFilterFrom(Filter filter) {
+  switch (filter) {
+    case CompilerFilter::kVerifyNone:
+    case CompilerFilter::kVerifyAtRuntime:
+    case CompilerFilter::kInterpretOnly:
+    case CompilerFilter::kSpace:
+    case CompilerFilter::kBalanced:
+    case CompilerFilter::kTime:
+    case CompilerFilter::kSpeed:
+    case CompilerFilter::kEverything:
+      return filter;
+
+    case CompilerFilter::kVerifyProfile:
+      return CompilerFilter::kInterpretOnly;
+
+    case CompilerFilter::kSpaceProfile:
+      return CompilerFilter::kSpace;
+
+    case CompilerFilter::kSpeedProfile:
+      return CompilerFilter::kSpeed;
+
+    case CompilerFilter::kEverythingProfile:
+      return CompilerFilter::kEverything;
+  }
+  UNREACHABLE();
+}
+
+
 bool CompilerFilter::IsAsGoodAs(Filter current, Filter target) {
   return current >= target;
 }
diff --git a/runtime/compiler_filter.h b/runtime/compiler_filter.h
index 1bea8b4..6289d8a 100644
--- a/runtime/compiler_filter.h
+++ b/runtime/compiler_filter.h
@@ -59,6 +59,9 @@
   // profile.
   static bool DependsOnProfile(Filter filter);
 
+  // Returns a non-profile-guided version of the given filter.
+  static Filter GetNonProfileDependentFilterFrom(Filter filter);
+
   // Returns true if the 'current' compiler filter is considered at least as
   // good as the 'target' compilation type.
   // For example: kSpeed is as good as kInterpretOnly, but kInterpretOnly is
diff --git a/runtime/native/dalvik_system_DexFile.cc b/runtime/native/dalvik_system_DexFile.cc
index 1e4b35f..0abe39d 100644
--- a/runtime/native/dalvik_system_DexFile.cc
+++ b/runtime/native/dalvik_system_DexFile.cc
@@ -456,6 +456,31 @@
   return CompilerFilter::DependsOnProfile(filter) ? JNI_TRUE : JNI_FALSE;
 }
 
+static jstring DexFile_getNonProfileGuidedCompilerFilter(JNIEnv* env,
+                                                         jclass javeDexFileClass ATTRIBUTE_UNUSED,
+                                                         jstring javaCompilerFilter) {
+  ScopedUtfChars compiler_filter(env, javaCompilerFilter);
+  if (env->ExceptionCheck()) {
+    return nullptr;
+  }
+
+  CompilerFilter::Filter filter;
+  if (!CompilerFilter::ParseCompilerFilter(compiler_filter.c_str(), &filter)) {
+    return javaCompilerFilter;
+  }
+
+  CompilerFilter::Filter new_filter = CompilerFilter::GetNonProfileDependentFilterFrom(filter);
+
+  // Filter stayed the same, return input.
+  if (filter == new_filter) {
+    return javaCompilerFilter;
+  }
+
+  // Create a new string object and return.
+  std::string new_filter_str = CompilerFilter::NameOfFilter(new_filter);
+  return env->NewStringUTF(new_filter_str.c_str());
+}
+
 static JNINativeMethod gMethods[] = {
   NATIVE_METHOD(DexFile, closeDexFile, "(Ljava/lang/Object;)Z"),
   NATIVE_METHOD(DexFile,
@@ -478,6 +503,9 @@
                 ")Ljava/lang/Object;"),
   NATIVE_METHOD(DexFile, isValidCompilerFilter, "(Ljava/lang/String;)Z"),
   NATIVE_METHOD(DexFile, isProfileGuidedCompilerFilter, "(Ljava/lang/String;)Z"),
+  NATIVE_METHOD(DexFile,
+                getNonProfileGuidedCompilerFilter,
+                "(Ljava/lang/String;)Ljava/lang/String;"),
 };
 
 void register_dalvik_system_DexFile(JNIEnv* env) {