JIT baseline: trigger optimized compilation on hotness threshold.

- Add a new hotness count in the ProfilingInfo to not conflict with
interpreter hotness which may use it for OSR.
- Add a baseline flag in the OatQuickMethodHeader to identify baseline
compiled methods.
- Add a -Xusetieredjit flag to experiment and test.

Bug: 119800099
Test: test.py with Xusetieredjit to true

Change-Id: I8512853f869f1312e3edc60bf64413dee9143c52
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 54da879..3077be0 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -1072,6 +1072,72 @@
   return dwarf::Reg::X86Core(static_cast<int>(reg));
 }
 
+void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
+  if (GetCompilerOptions().CountHotnessInCompiledCode()) {
+    Register reg = EAX;
+    if (is_frame_entry) {
+      reg = kMethodRegisterArgument;
+    } else {
+      __ pushl(EAX);
+      __ movl(EAX, Address(ESP, kX86WordSize));
+    }
+    NearLabel overflow;
+    __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
+            Immediate(ArtMethod::MaxCounter()));
+    __ j(kEqual, &overflow);
+    __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
+            Immediate(1));
+    __ Bind(&overflow);
+    if (!is_frame_entry) {
+      __ popl(EAX);
+    }
+  }
+
+  if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
+    ScopedObjectAccess soa(Thread::Current());
+    ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
+    uint32_t address = reinterpret_cast32<uint32_t>(info);
+    NearLabel done;
+    if (HasEmptyFrame()) {
+      CHECK(is_frame_entry);
+      // Alignment
+      __ subl(ESP, Immediate(8));
+      __ cfi().AdjustCFAOffset(8);
+      // We need a temporary. The stub also expects the method at bottom of stack.
+      __ pushl(EAX);
+      __ cfi().AdjustCFAOffset(4);
+      __ movl(EAX, Immediate(address));
+      __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()), Immediate(1));
+      __ j(kCarryClear, &done);
+      GenerateInvokeRuntime(
+          GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
+      __ Bind(&done);
+      // We don't strictly require to restore EAX, but this makes the generated
+      // code easier to reason about.
+      __ popl(EAX);
+      __ cfi().AdjustCFAOffset(-4);
+      __ addl(ESP, Immediate(8));
+      __ cfi().AdjustCFAOffset(-8);
+    } else {
+      if (!RequiresCurrentMethod()) {
+        CHECK(is_frame_entry);
+        __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
+      }
+      // We need a temporary.
+      __ pushl(EAX);
+      __ cfi().AdjustCFAOffset(4);
+      __ movl(EAX, Immediate(address));
+      __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()), Immediate(1));
+      __ popl(EAX);  // Put stack as expected before exiting or calling stub.
+      __ cfi().AdjustCFAOffset(-4);
+      __ j(kCarryClear, &done);
+      GenerateInvokeRuntime(
+          GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
+      __ Bind(&done);
+    }
+  }
+}
+
 void CodeGeneratorX86::GenerateFrameEntry() {
   __ cfi().SetCurrentCFAOffset(kX86WordSize);  // return address
   __ Bind(&frame_entry_label_);
@@ -1079,51 +1145,39 @@
       IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
   DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
 
-  if (GetCompilerOptions().CountHotnessInCompiledCode()) {
-    NearLabel overflow;
-    __ cmpw(Address(kMethodRegisterArgument,
-                    ArtMethod::HotnessCountOffset().Int32Value()),
-            Immediate(ArtMethod::MaxCounter()));
-    __ j(kEqual, &overflow);
-    __ addw(Address(kMethodRegisterArgument,
-                    ArtMethod::HotnessCountOffset().Int32Value()),
-            Immediate(1));
-    __ Bind(&overflow);
-  }
-
   if (!skip_overflow_check) {
     size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
     __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
     RecordPcInfo(nullptr, 0);
   }
 
-  if (HasEmptyFrame()) {
-    return;
-  }
+  if (!HasEmptyFrame()) {
+    for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
+      Register reg = kCoreCalleeSaves[i];
+      if (allocated_registers_.ContainsCoreRegister(reg)) {
+        __ pushl(reg);
+        __ cfi().AdjustCFAOffset(kX86WordSize);
+        __ cfi().RelOffset(DWARFReg(reg), 0);
+      }
+    }
 
-  for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
-    Register reg = kCoreCalleeSaves[i];
-    if (allocated_registers_.ContainsCoreRegister(reg)) {
-      __ pushl(reg);
-      __ cfi().AdjustCFAOffset(kX86WordSize);
-      __ cfi().RelOffset(DWARFReg(reg), 0);
+    int adjust = GetFrameSize() - FrameEntrySpillSize();
+    __ subl(ESP, Immediate(adjust));
+    __ cfi().AdjustCFAOffset(adjust);
+    // Save the current method if we need it. Note that we do not
+    // do this in HCurrentMethod, as the instruction might have been removed
+    // in the SSA graph.
+    if (RequiresCurrentMethod()) {
+      __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
+    }
+
+    if (GetGraph()->HasShouldDeoptimizeFlag()) {
+      // Initialize should_deoptimize flag to 0.
+      __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
     }
   }
 
-  int adjust = GetFrameSize() - FrameEntrySpillSize();
-  __ subl(ESP, Immediate(adjust));
-  __ cfi().AdjustCFAOffset(adjust);
-  // Save the current method if we need it. Note that we do not
-  // do this in HCurrentMethod, as the instruction might have been removed
-  // in the SSA graph.
-  if (RequiresCurrentMethod()) {
-    __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
-  }
-
-  if (GetGraph()->HasShouldDeoptimizeFlag()) {
-    // Initialize should_deoptimize flag to 0.
-    __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
-  }
+  MaybeIncrementHotness(/* is_frame_entry= */ true);
 }
 
 void CodeGeneratorX86::GenerateFrameExit() {
@@ -1391,18 +1445,7 @@
 
   HLoopInformation* info = block->GetLoopInformation();
   if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
-    if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
-      __ pushl(EAX);
-      __ movl(EAX, Address(ESP, kX86WordSize));
-          NearLabel overflow;
-      __ cmpw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()),
-              Immediate(ArtMethod::MaxCounter()));
-      __ j(kEqual, &overflow);
-      __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()),
-              Immediate(1));
-      __ Bind(&overflow);
-      __ popl(EAX);
-    }
+    codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
     GenerateSuspendCheck(info->GetSuspendCheck(), successor);
     return;
   }