Avoid dex2dex initialization if not compiling.

Reduces the compilation time for the boot image extension
in memory by ~0.1s.

Also record timing for ImageWriter::PreloadDexCaches() and
and fix naming for CompilerDriver::SetVerifiedDexFile()
timing to not be confused with VerifyDexFile().

(cherry picked from commit f639bf4c6b542553f727dc1a3262d9a4fd887fe5)

Test: aosp_taimen-userdebug boots;
  adb root && \
  adb shell stop && \
  adb shell setprop dalvik.vm.boot-image \
    'boot.art:/nonx/boot-framework.art!/system/etc/boot-image.prof' && \
  adb shell 'setprop dalvik.vm.extra-opts \
    "-Ximage-compiler-option --dump-timings"' && \
  adb shell start  # Starts correctly
  # Check dex2oat timing in logcat
Bug: 119800099
Bug: 150193586
Change-Id: I31846738a66c95eda28e873eac724a3949ac3f48
diff --git a/dex2oat/driver/compiler_driver.cc b/dex2oat/driver/compiler_driver.cc
index b1fb5af..15c7c65 100644
--- a/dex2oat/driver/compiler_driver.cc
+++ b/dex2oat/driver/compiler_driver.cc
@@ -852,7 +852,11 @@
   VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false);
 
   compiled_classes_.AddDexFiles(GetCompilerOptions().GetDexFilesForOatFile());
-  dex_to_dex_compiler_.SetDexFiles(GetCompilerOptions().GetDexFilesForOatFile());
+
+  if (GetCompilerOptions().IsAnyCompilationEnabled()) {
+    TimingLogger::ScopedTiming t2("Dex2Dex SetDexFiles", timings);
+    dex_to_dex_compiler_.SetDexFiles(GetCompilerOptions().GetDexFilesForOatFile());
+  }
 
   // Precompile:
   // 1) Load image classes.
@@ -2102,7 +2106,7 @@
                                         ThreadPool* thread_pool,
                                         size_t thread_count,
                                         TimingLogger* timings) {
-  TimingLogger::ScopedTiming t("Verify Dex File", timings);
+  TimingLogger::ScopedTiming t("Set Verified Dex File", timings);
   if (!compiled_classes_.HaveDexFile(&dex_file)) {
     compiled_classes_.AddDexFile(&dex_file);
   }
diff --git a/dex2oat/linker/image_writer.cc b/dex2oat/linker/image_writer.cc
index a5605f9..af8c50b 100644
--- a/dex2oat/linker/image_writer.cc
+++ b/dex2oat/linker/image_writer.cc
@@ -278,6 +278,7 @@
   }
 
   {
+    TimingLogger::ScopedTiming t("PreloadDexCaches", timings);
     // Preload deterministic contents to the dex cache arrays we're going to write.
     ScopedObjectAccess soa(self);
     ObjPtr<mirror::ClassLoader> class_loader = GetAppClassLoader();