Support FastVerify with speed-profile.

Tweak a few compiler behavior to make it work and match test
expectations.

Test: test.py
Bug: 279728780
Change-Id: I350ff313ca53e2c19b637af7521683cc2b09d66f
diff --git a/compiler/jni/quick/jni_compiler.cc b/compiler/jni/quick/jni_compiler.cc
index c60d974..763d379 100644
--- a/compiler/jni/quick/jni_compiler.cc
+++ b/compiler/jni/quick/jni_compiler.cc
@@ -154,11 +154,11 @@
     // -- Don't allow any objects as parameter or return value
     if (UNLIKELY(is_critical_native)) {
       CHECK(is_static)
-          << "@CriticalNative functions cannot be virtual since that would"
+          << "@CriticalNative functions cannot be virtual since that would "
           << "require passing a reference parameter (this), which is illegal "
           << dex_file.PrettyMethod(method_idx, /* with_signature= */ true);
       CHECK(!is_synchronized)
-          << "@CriticalNative functions cannot be synchronized since that would"
+          << "@CriticalNative functions cannot be synchronized since that would "
           << "require passing a (class and/or this) reference parameter, which is illegal "
           << dex_file.PrettyMethod(method_idx, /* with_signature= */ true);
       for (size_t i = 0; i < strlen(shorty); ++i) {
diff --git a/compiler/optimizing/reference_type_propagation.cc b/compiler/optimizing/reference_type_propagation.cc
index 6d4fdf3..3a5ccee 100644
--- a/compiler/optimizing/reference_type_propagation.cc
+++ b/compiler/optimizing/reference_type_propagation.cc
@@ -539,9 +539,14 @@
   DCHECK_EQ(instr->GetType(), DataType::Type::kReference);
 
   ScopedObjectAccess soa(Thread::Current());
-  ObjPtr<mirror::DexCache> dex_cache = FindDexCacheWithHint(soa.Self(), dex_file, hint_dex_cache_);
-  ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
-      type_idx, dex_cache, dex_cache->GetClassLoader());
+  StackHandleScope<2> hs(soa.Self());
+  Handle<mirror::DexCache> dex_cache =
+      hs.NewHandle(FindDexCacheWithHint(soa.Self(), dex_file, hint_dex_cache_));
+  Handle<mirror::ClassLoader> loader = hs.NewHandle(dex_cache->GetClassLoader());
+  ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->ResolveType(
+      type_idx, dex_cache, loader);
+  DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
+  soa.Self()->ClearException();  // Clean up the exception left by type resolution if any.
   SetClassAsTypeInfo(instr, klass, is_exact);
 }
 
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 6c71372..f4c3e2a 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -1752,9 +1752,7 @@
     }
 
     // Setup VerifierDeps for compilation and report if we fail to parse the data.
-    // When we do profile guided optimizations, the compiler currently needs to run
-    // full verification.
-    if (!DoProfileGuidedOptimizations() && input_vdex_file_ != nullptr) {
+    if (input_vdex_file_ != nullptr) {
       std::unique_ptr<verifier::VerifierDeps> verifier_deps(
           new verifier::VerifierDeps(dex_files, /*output_only=*/ false));
       if (!verifier_deps->ParseStoredData(dex_files, input_vdex_file_->GetVerifierDepsData())) {
diff --git a/dex2oat/driver/compiler_driver.cc b/dex2oat/driver/compiler_driver.cc
index 23bbe14..df7835d 100644
--- a/dex2oat/driver/compiler_driver.cc
+++ b/dex2oat/driver/compiler_driver.cc
@@ -471,7 +471,13 @@
       const DexFile& dex_file,
       Handle<mirror::DexCache> dex_cache) {
     DCHECK(driver != nullptr);
+    const VerificationResults* results = driver->GetVerificationResults();
+    DCHECK(results != nullptr);
+    MethodReference method_ref(&dex_file, method_idx);
     CompiledMethod* compiled_method = nullptr;
+    if (results->IsUncompilableMethod(method_ref)) {
+      return compiled_method;
+    }
 
     if ((access_flags & kAccNative) != 0) {
       // Are we extracting only and have support for generic JNI down calls?
@@ -496,14 +502,9 @@
       // Method is annotated with @NeverCompile and should not be compiled.
     } else {
       const CompilerOptions& compiler_options = driver->GetCompilerOptions();
-      const VerificationResults* results = driver->GetVerificationResults();
-      DCHECK(results != nullptr);
-      MethodReference method_ref(&dex_file, method_idx);
       // Don't compile class initializers unless kEverything.
       bool compile = (compiler_options.GetCompilerFilter() == CompilerFilter::kEverything) ||
          ((access_flags & kAccConstructor) == 0) || ((access_flags & kAccStatic) == 0);
-      // Check if it's an uncompilable method found by the verifier.
-      compile = compile && !results->IsUncompilableMethod(method_ref);
       // Check if we should compile based on the profile.
       compile = compile && ShouldCompileBasedOnProfile(compiler_options, profile_index, method_ref);
 
@@ -1711,6 +1712,7 @@
     // a boot image class, or a class in a different dex file for multidex, and
     // we should not update the status in that case.
     if (&cls->GetDexFile() == &accessor.GetDexFile()) {
+      VLOG(compiler) << "Updating class status of " << std::string(descriptor) << " to " << status;
       ObjectLock<mirror::Class> lock(self, cls);
       mirror::Class::SetStatus(cls, status, self);
     }
@@ -1753,6 +1755,8 @@
       !GetCompilerOptions().IsAnyCompilationEnabled() &&
       !GetCompilerOptions().IsGeneratingImage();
 
+  const bool is_generating_image = GetCompilerOptions().IsGeneratingImage();
+
   // We successfully validated the dependencies, now update class status
   // of verified classes. Note that the dependencies also record which classes
   // could not be fully verified; we could try again, but that would hurt verification
@@ -1776,6 +1780,16 @@
         // fail, but that's OK.
         compiled_classes_.Insert(ref, existing, status);
       } else {
+        if (is_generating_image &&
+            status == ClassStatus::kVerifiedNeedsAccessChecks &&
+            GetCompilerOptions().IsImageClass(accessor.GetDescriptor())) {
+          // If the class will be in the image, we can rely on the ArtMethods
+          // telling that they need access checks.
+          VLOG(compiler) << "Promoting "
+                         << std::string(accessor.GetDescriptor())
+                         << " from needs access checks to verified given it is an image class";
+          status = ClassStatus::kVerified;
+        }
         // Update the class status, so later compilation stages know they don't need to verify
         // the class.
         LoadAndUpdateStatus(accessor, status, class_loader, soa.Self());
@@ -1783,7 +1797,7 @@
 
       // Vdex marks class as unverified for two reasons only:
       // 1. It has a hard failure, or
-      // 2. Once of its method needs lock counting.
+      // 2. One of its method needs lock counting.
       //
       // The optimizing compiler expects a method to not have a hard failure before
       // compiling it, so for simplicity just disable any compilation of methods
@@ -2143,6 +2157,7 @@
       // Also return early and don't store the class status in the recorded class status.
       return;
     }
+
     ClassStatus old_status = klass->GetStatus();
     // Only try to initialize classes that were successfully verified.
     if (klass->IsVerified()) {
diff --git a/test/660-clinit/profile b/test/660-clinit/profile
index 9eb4924..3fa1d3a 100644
--- a/test/660-clinit/profile
+++ b/test/660-clinit/profile
@@ -9,5 +9,6 @@
 LGs;
 LObjectRef;
 LInvokeStatic;
+LInImage;
 LClinitE;
 LPrint;
diff --git a/test/660-clinit/src/Main.java b/test/660-clinit/src/Main.java
index 0d6837e..ba7a9d2 100644
--- a/test/660-clinit/src/Main.java
+++ b/test/660-clinit/src/Main.java
@@ -202,13 +202,19 @@
   }
 }
 
+class InImage {
+  static int exec(int a, int b) {
+    return a + b;
+  }
+}
+
 // test of INVOKE_STATIC instruction
 class InvokeStatic {
   static int a;
   static int b;
   static {
-    a = Add.exec(10, 20);
-    b = Mul.exec(10, 20);
+    a = InImage.exec(10, 20);
+    b = InImage.exec(10, 20);
   }
 }