Make compiling an apex image explicit.

And when compiling an apex image, discard dex files not present
in an apex.

Test: m
Bug: 119800099

(cherry picked from commit c5e3a52473772a6aba3a8407b0151f6046c5e21b)

Change-Id: Iabbb7a479bd093c84c69d3d58884710f7a2f6823
Merged-In: Ie91c5b8d271783f04e4c1501f315a3ec59137475
diff --git a/compiler/driver/compiler_options.h b/compiler/driver/compiler_options.h
index 5908b87..0ab5ff1 100644
--- a/compiler/driver/compiler_options.h
+++ b/compiler/driver/compiler_options.h
@@ -75,6 +75,7 @@
     kNone,                // JIT or AOT app compilation producing only an oat file but no image.
     kBootImage,           // Creating boot image.
     kAppImage,            // Creating app image.
+    kApexBootImage,       // Creating the apex image for jit/zygote experiment b/119800099.
   };
 
   CompilerOptions();
@@ -210,7 +211,11 @@
 
   // Are we compiling a boot image?
   bool IsBootImage() const {
-    return image_type_ == ImageType::kBootImage;
+    return image_type_ == ImageType::kBootImage || image_type_ == ImageType::kApexBootImage;
+  }
+
+  bool IsApexBootImage() const {
+    return image_type_ == ImageType::kApexBootImage;
   }
 
   bool IsBaseline() const {
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index b44dc3f..278523e 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -765,7 +765,11 @@
     compiler_options_->compile_pic_ = true;  // All AOT compilation is PIC.
     DCHECK(compiler_options_->image_type_ == CompilerOptions::ImageType::kNone);
     if (!image_filenames_.empty()) {
-      compiler_options_->image_type_ = CompilerOptions::ImageType::kBootImage;
+      if (android::base::EndsWith(image_filenames_[0], "apex.art")) {
+        compiler_options_->image_type_ = CompilerOptions::ImageType::kApexBootImage;
+      } else {
+        compiler_options_->image_type_ = CompilerOptions::ImageType::kBootImage;
+      }
     }
     if (app_image_fd_ != -1 || !app_image_file_name_.empty()) {
       if (compiler_options_->IsBootImage()) {
diff --git a/dex2oat/driver/compiler_driver.cc b/dex2oat/driver/compiler_driver.cc
index 5373435..540b8a6 100644
--- a/dex2oat/driver/compiler_driver.cc
+++ b/dex2oat/driver/compiler_driver.cc
@@ -930,6 +930,14 @@
 }
 
 bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const {
+  // If compiling the apex image, filter out methods not in an apex file (the profile used
+  // for boot classpath is the same between the apex image and the boot image, so it includes
+  /// framewkro methods).
+  if (compiler_options_->IsApexBootImage() &&
+      !android::base::StartsWith(method_ref.dex_file->GetLocation(), "/apex")) {
+    return false;
+  }
+
   // Profile compilation info may be null if no profile is passed.
   if (!CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter())) {
     // Use the compiler filter instead of the presence of profile_compilation_info_ since
@@ -950,6 +958,7 @@
     LOG(INFO) << "[ProfileGuidedCompilation] "
         << (result ? "Compiled" : "Skipped") << " method:" << method_ref.PrettyMethod(true);
   }
+
   return result;
 }
 
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index f8d51c6..dd07545 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -691,11 +691,12 @@
     }
     // To speed up class lookups, generate a type lookup table for
     // the dex file.
-    DCHECK(dex_file->GetOatDexFile() == nullptr);
-    TypeLookupTable type_lookup_table = TypeLookupTable::Create(*dex_file);
-    type_lookup_tables_.push_back(
-          std::make_unique<art::OatDexFile>(std::move(type_lookup_table)));
-    dex_file->SetOatDexFile(type_lookup_tables_.back().get());
+    if (dex_file->GetOatDexFile() == nullptr) {
+      TypeLookupTable type_lookup_table = TypeLookupTable::Create(*dex_file);
+      type_lookup_tables_.push_back(
+            std::make_unique<art::OatDexFile>(std::move(type_lookup_table)));
+      dex_file->SetOatDexFile(type_lookup_tables_.back().get());
+    }
 
     std::set<dex::TypeIndex> class_types;
     std::set<uint16_t> all_methods;