Make dex2oat support profiles passed to non profile filters

Using a non profile filter with an input profile should compile
everything but generate an app image and optimize layout based on
the profile.

Change UseProfileGuidedCompilation to use the profile arguments.
Using profile arguments instead of being based on the compiler
filter lets us do full speed compile and layout based on profile.

Fix ShouldCompileBasedOnProfile to use the compiler filter instead
of the existence of a profile.

Fixed gtests.

Test: test-art-host

Bug: 34927277

Change-Id: I325a10d2072ed427bb32f96e4efa54cf81e94ad3
diff --git a/compiler/common_compiler_test.cc b/compiler/common_compiler_test.cc
index 2f9164c..d89cdba 100644
--- a/compiler/common_compiler_test.cc
+++ b/compiler/common_compiler_test.cc
@@ -175,6 +175,7 @@
                                               InstructionSet isa,
                                               size_t number_of_threads) {
   compiler_options_->boot_image_ = true;
+  compiler_options_->SetCompilerFilter(GetCompilerFilter());
   compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
                                             verification_results_.get(),
                                             kind,
diff --git a/compiler/common_compiler_test.h b/compiler/common_compiler_test.h
index 0d45a50..98dcf20 100644
--- a/compiler/common_compiler_test.h
+++ b/compiler/common_compiler_test.h
@@ -77,6 +77,10 @@
 
   virtual ProfileCompilationInfo* GetProfileCompilationInfo();
 
+  virtual CompilerFilter::Filter GetCompilerFilter() const {
+    return CompilerFilter::kDefaultCompilerFilter;
+  }
+
   virtual void TearDown();
 
   void CompileClass(mirror::ClassLoader* class_loader, const char* class_name)
diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc
index 26c0818..b6d1fef 100644
--- a/compiler/driver/compiler_driver.cc
+++ b/compiler/driver/compiler_driver.cc
@@ -1050,9 +1050,9 @@
 }
 
 bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const {
-  if (profile_compilation_info_ == nullptr) {
-    // If we miss profile information it means that we don't do a profile guided compilation.
-    // Return true, and let the other filters decide if the method should be compiled.
+  if (!CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter())) {
+    // Use the compiler filter instead of the presence of profile_compilation_info_ since
+    // we may want to have full speed compilation along with profile based layout optimizations.
     return true;
   }
   bool result = profile_compilation_info_->ContainsMethod(method_ref);
diff --git a/compiler/driver/compiler_driver_test.cc b/compiler/driver/compiler_driver_test.cc
index 1e4ca16..3879631 100644
--- a/compiler/driver/compiler_driver_test.cc
+++ b/compiler/driver/compiler_driver_test.cc
@@ -246,6 +246,11 @@
     return &profile_info_;
   }
 
+  CompilerFilter::Filter GetCompilerFilter() const OVERRIDE {
+    // Use a profile based filter.
+    return CompilerFilter::kSpeedProfile;
+  }
+
   std::unordered_set<std::string> GetExpectedMethodsForClass(const std::string& clazz) {
     if (clazz == "Main") {
       return std::unordered_set<std::string>({
@@ -304,7 +309,6 @@
 
   // Need to enable dex-file writability. Methods rejected to be compiled will run through the
   // dex-to-dex compiler.
-  ProfileCompilationInfo info;
   for (const DexFile* dex_file : GetDexFiles(class_loader)) {
     ASSERT_TRUE(dex_file->EnableWrite());
   }
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 026a567..e6b7930 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -2095,7 +2095,7 @@
   }
 
   bool UseProfileGuidedCompilation() const {
-    return CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter());
+    return profile_file_fd_ != -1 || !profile_file_.empty();
   }
 
   bool LoadProfile() {
diff --git a/runtime/dexopt_test.cc b/runtime/dexopt_test.cc
index 69c6151..5167869 100644
--- a/runtime/dexopt_test.cc
+++ b/runtime/dexopt_test.cc
@@ -70,6 +70,11 @@
   // rather than use this flag.
   args.push_back("-Xnorelocate");
 
+  ScratchFile profile_file;
+  if (CompilerFilter::DependsOnProfile(filter)) {
+    args.push_back("--profile-file=" + profile_file.GetFilename());
+  }
+
   if (pic) {
     args.push_back("--compile-pic");
   }