dex2oat: default to speed-profile if we have a profile.

And move the check for /system/etc/boot-image.prof from
AndroidRuntime.cpp to the deprecated GenerateImage() in
art/runtime/gc/space/image_space.cc . If it's not found,
just report the error and compile with the "speed" filter.

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Test: aosp_taimen-userdebug boots.
Bug: 119800099
Bug: 148928928
Change-Id: I536ed7e6d7ebaa562cba3448a37c5d873bc0a682
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index d983d94..e8f160c 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -341,7 +341,8 @@
                 "|everything):");
   UsageError("      select compiler filter.");
   UsageError("      Example: --compiler-filter=everything");
-  UsageError("      Default: speed");
+  UsageError("      Default: speed-profile if --profile-file or --profile-file-fd is used,");
+  UsageError("               speed otherwise");
   UsageError("");
   UsageError("  --huge-method-max=<method-instruction-count>: threshold size for a huge");
   UsageError("      method for compiler filter tuning.");
@@ -1316,6 +1317,14 @@
             "--class-loader-context is also specified");
     }
 
+    // If we have a profile, change the default compiler filter to speed-profile
+    // before reading compiler options.
+    static_assert(CompilerFilter::kDefaultCompilerFilter == CompilerFilter::kSpeed);
+    DCHECK_EQ(compiler_options_->GetCompilerFilter(), CompilerFilter::kSpeed);
+    if (UseProfile()) {
+      compiler_options_->SetCompilerFilter(CompilerFilter::kSpeedProfile);
+    }
+
     if (!ReadCompilerOptions(args, compiler_options_.get(), &error_msg)) {
       Usage(error_msg.c_str());
     }
diff --git a/runtime/gc/space/image_space.cc b/runtime/gc/space/image_space.cc
index 948debf..f3ce583 100644
--- a/runtime/gc/space/image_space.cc
+++ b/runtime/gc/space/image_space.cc
@@ -181,6 +181,15 @@
     arg_vector.push_back("--host");
   }
 
+  // Check if there is a boot profile, and pass it to dex2oat.
+  if (OS::FileExists("/system/etc/boot-image.prof")) {
+    arg_vector.push_back("--profile-file=/system/etc/boot-image.prof");
+  } else {
+    // We will compile the boot image with compiler filter "speed" unless overridden below.
+    LOG(WARNING) << "Missing boot-image.prof file, /system/etc/boot-image.prof not found: "
+                 << strerror(errno);
+  }
+
   const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
   for (size_t i = 0; i < compiler_options.size(); ++i) {
     arg_vector.push_back(compiler_options[i].c_str());
diff --git a/test/661-oat-writer-layout/run b/test/661-oat-writer-layout/run
index f93d7b7..99e78af 100644
--- a/test/661-oat-writer-layout/run
+++ b/test/661-oat-writer-layout/run
@@ -16,5 +16,7 @@
 
 # Always use the 'profile'.
 # Note that this test only works with --compiler-filter=speed
-# -- we accomplish this by blacklisting other compiler variants.
-"${RUN}" "$@" --profile
+# -- we accomplish this by blacklisting other compiler variants
+# and we also have to pass the option explicitly as dex2oat
+# defaults to speed-profile if a profile is specified.
+"${RUN}" "$@" --profile -Xcompiler-option --compiler-filter=speed
diff --git a/test/knownfailures.json b/test/knownfailures.json
index bb80531..f5d51c1 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -780,7 +780,7 @@
     },
     {
         "tests": ["004-StackWalk"],
-        "variant": "interp-ac | interpreter | jit | no-prebuild | no-image | trace | redefine-stress | jvmti-stress | debuggable",
+        "variant": "speed-profile | interp-ac | interpreter | jit | no-prebuild | no-image | trace | redefine-stress | jvmti-stress | debuggable",
         "description": ["Test is designed to only check --optimizing"]
     },
     {