Handle baseline/optimized compilation kind in the runtime.

It used to be adjusted in the compiler, but that does not work anymore
as the compiler now always requests a baseline compilation to have a
profiling info.

Test: 457-regs
Bug: 146423102
Change-Id: I522bc515ef1c6f8737ada311265a6f8e0f4db3fd
diff --git a/compiler/jit/jit_compiler.cc b/compiler/jit/jit_compiler.cc
index f819cd8..44a4283 100644
--- a/compiler/jit/jit_compiler.cc
+++ b/compiler/jit/jit_compiler.cc
@@ -208,5 +208,9 @@
   return success;
 }
 
+bool JitCompiler::IsBaselineCompiler() const {
+  return compiler_options_->IsBaseline();
+}
+
 }  // namespace jit
 }  // namespace art
diff --git a/compiler/jit/jit_compiler.h b/compiler/jit/jit_compiler.h
index 9dd84f0..8e9966d 100644
--- a/compiler/jit/jit_compiler.h
+++ b/compiler/jit/jit_compiler.h
@@ -48,6 +48,8 @@
     return *compiler_options_.get();
   }
 
+  bool IsBaselineCompiler() const override;
+
   bool GenerateDebugInfo() override;
 
   void ParseCompilerOptions() override;
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index 10b59d2..18e0512 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -1213,11 +1213,6 @@
                                     CompilationKind compilation_kind,
                                     jit::JitLogger* jit_logger) {
   const CompilerOptions& compiler_options = GetCompilerOptions();
-  // If the baseline flag was explicitly passed, change the compilation kind
-  // from optimized to baseline.
-  if (compiler_options.IsBaseline() && compilation_kind == CompilationKind::kOptimized) {
-    compilation_kind = CompilationKind::kBaseline;
-  }
   DCHECK(compiler_options.IsJitCompiler());
   DCHECK_EQ(compiler_options.IsJitCompilerForSharedCode(), code_cache->IsSharedRegion(*region));
   StackHandleScope<3> hs(self);
diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc
index b964b7c..9c5eff5 100644
--- a/runtime/jit/jit.cc
+++ b/runtime/jit/jit.cc
@@ -273,6 +273,19 @@
   DCHECK(Runtime::Current()->UseJitCompilation());
   DCHECK(!method->IsRuntimeMethod());
 
+  // If the baseline flag was explicitly passed in the compiler options, change the compilation kind
+  // from optimized to baseline.
+  if (jit_compiler_->IsBaselineCompiler() && compilation_kind == CompilationKind::kOptimized) {
+    compilation_kind = CompilationKind::kBaseline;
+  }
+
+  // If we're asked to compile baseline, but we cannot allocate profiling infos,
+  // change the compilation kind to optimized.
+  if ((compilation_kind == CompilationKind::kBaseline) &&
+      !GetCodeCache()->CanAllocateProfilingInfo()) {
+    compilation_kind = CompilationKind::kOptimized;
+  }
+
   RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
   // Don't compile the method if it has breakpoints.
   if (cb->IsMethodBeingInspected(method) && !cb->IsMethodSafeToJit(method)) {
diff --git a/runtime/jit/jit.h b/runtime/jit/jit.h
index e6c93aa..24dab6d 100644
--- a/runtime/jit/jit.h
+++ b/runtime/jit/jit.h
@@ -200,6 +200,7 @@
       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
   virtual bool GenerateDebugInfo() = 0;
   virtual void ParseCompilerOptions() = 0;
+  virtual bool IsBaselineCompiler() const;
 
   virtual std::vector<uint8_t> PackElfFileForJIT(ArrayRef<const JITCodeEntry*> elf_files,
                                                  ArrayRef<const void*> removed_symbols,
diff --git a/runtime/jit/jit_code_cache.cc b/runtime/jit/jit_code_cache.cc
index 047f7a4..8b91f9e 100644
--- a/runtime/jit/jit_code_cache.cc
+++ b/runtime/jit/jit_code_cache.cc
@@ -1670,7 +1670,8 @@
     }
     return new_compilation;
   } else {
-    if (CanAllocateProfilingInfo() && (compilation_kind == CompilationKind::kBaseline)) {
+    if (compilation_kind == CompilationKind::kBaseline) {
+      DCHECK(CanAllocateProfilingInfo());
       bool has_profiling_info = false;
       {
         MutexLock mu(self, *Locks::jit_lock_);