Only check for should_deoptimize_flag if there was a redefinition

We only need to check for the deoptimize flag on stack when a
redefinition has happened. After a redefinition we cannot use any JITed
code and hence we need to check if we need to deoptimize a particular
frame even when the method isn't marked for deoptimization. In all other
cases it isn't really required to check this bit.

Bug: 253232638
Test: art/test.py
Change-Id: Ib7192db004347d407ec55454b4de040d300d3280
diff --git a/runtime/instrumentation.cc b/runtime/instrumentation.cc
index dc9d0e1..ab0998b 100644
--- a/runtime/instrumentation.cc
+++ b/runtime/instrumentation.cc
@@ -1707,9 +1707,10 @@
   bool needs_deopt = NeedsSlowInterpreterForMethod(self, caller);
 
   // Non java debuggable apps don't support redefinition and hence it isn't required to check if
-  // frame needs to be deoptimized. We also want to avoid getting method header when we need a
-  // deopt anyway.
-  if (Runtime::Current()->IsJavaDebuggable() && !needs_deopt) {
+  // frame needs to be deoptimized. Even in debuggable apps, we only need this check when a
+  // redefinition has actually happened. This is indicated by IsDeoptCheckRequired flag. We also
+  // want to avoid getting method header when we need a deopt anyway.
+  if (Runtime::Current()->IsJavaDebuggable() && !needs_deopt && self->IsDeoptCheckRequired()) {
     const OatQuickMethodHeader* header = caller->GetOatQuickMethodHeader(caller_pc);
     if (header != nullptr && header->HasShouldDeoptimizeFlag()) {
       DCHECK(header->IsOptimized());