1 /*
  2  * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_RUNTIME_DEOPTIMIZATION_HPP
 26 #define SHARE_RUNTIME_DEOPTIMIZATION_HPP
 27 
 28 #include "interpreter/bytecodes.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "runtime/frame.hpp"
 31 
 32 class ProfileData;
 33 class vframeArray;
 34 class MonitorInfo;
 35 class MonitorValue;
 36 class ObjectValue;
 37 class AutoBoxObjectValue;
 38 class ScopeValue;
 39 class compiledVFrame;
 40 
 41 template<class E> class GrowableArray;
 42 
 43 class DeoptimizationScope {
 44  private:
 45   // What gen we have done the deopt handshake for.
 46   static uint64_t _committed_deopt_gen;
 47   // What gen to mark a method with, hence larger than _committed_deopt_gen.
 48   static uint64_t _active_deopt_gen;
 49   // Indicate an in-progress deopt handshake.
 50   static bool     _committing_in_progress;
 51 
 52   // The required gen we need to execute/wait for
 53   uint64_t _required_gen;
 54   DEBUG_ONLY(bool _deopted;)
 55 
 56  public:
 57   DeoptimizationScope();
 58   ~DeoptimizationScope();
 59   // Mark a method, if already marked as dependent.
 60   void mark(nmethod* nm, bool inc_recompile_counts = true);
 61   // Record this as a dependent method.
 62   void dependent(nmethod* nm);
 63 
 64   // Execute the deoptimization.
 65   // Make the nmethods not entrant, stackwalks and patch return pcs and sets post call nops.
 66   void deoptimize_marked();
 67 };
 68 
 69 class Deoptimization : AllStatic {
 70   friend class VMStructs;
 71   friend class EscapeBarrier;
 72 
 73  public:
 74   // What condition caused the deoptimization?
 75   // Note: Keep this enum in sync. with Deoptimization::_trap_reason_name.
 76   enum DeoptReason {
 77     Reason_many = -1,             // indicates presence of several reasons
 78     Reason_none = 0,              // indicates absence of a relevant deopt.
 79     // Next 8 reasons are recorded per bytecode in DataLayout::trap_bits.
 80     Reason_null_check,            // saw unexpected null or zero divisor (@bci)
 81     Reason_null_assert,           // saw unexpected non-null or non-zero (@bci)
 82     Reason_range_check,           // saw unexpected array index (@bci)
 83     Reason_class_check,           // saw unexpected object class (@bci)
 84     Reason_array_check,           // saw unexpected array class (aastore @bci)
 85     Reason_intrinsic,             // saw unexpected operand to intrinsic (@bci)
 86     Reason_bimorphic,             // saw unexpected object class in bimorphic inlining (@bci)
 87 
 88     Reason_profile_predicate,     // compiler generated predicate moved from frequent branch in a loop failed
 89     Reason_auto_vectorization_check, // compiler generated (speculative) auto vectorization checks failed
 90 
 91     // recorded per method
 92     Reason_unloaded,              // unloaded class or constant pool entry
 93     Reason_uninitialized,         // bad class state (uninitialized)
 94     Reason_initialized,           // class has been fully initialized
 95     Reason_unreached,             // code is not reached, compiler
 96     Reason_unhandled,             // arbitrary compiler limitation
 97     Reason_constraint,            // arbitrary runtime constraint violated
 98     Reason_div0_check,            // a null_check due to division by zero
 99     Reason_age,                   // nmethod too old; tier threshold reached
100     Reason_predicate,             // compiler generated predicate failed
101     Reason_loop_limit_check,      // compiler generated loop limits check failed
102     Reason_speculate_class_check, // saw unexpected object class from type speculation
103     Reason_speculate_null_check,  // saw unexpected null from type speculation
104     Reason_speculate_null_assert, // saw unexpected null from type speculation
105     Reason_unstable_if,           // a branch predicted always false was taken
106     Reason_unstable_fused_if,     // fused two ifs that had each one untaken branch. One is now taken.
107     Reason_receiver_constraint,   // receiver subtype check failed
108     Reason_not_compiled_exception_handler, // missing compiled exception handler
109     Reason_short_running_long_loop,    // profile reports loop runs for small number of iterations
110 
111     // Used to define MethodData::_trap_hist_limit where Reason_tenured isn't included
112     Reason_TRAP_HISTORY_LENGTH,
113 
114     // Reason_tenured is counted separately, add normal counted Reasons above.
115     Reason_tenured = Reason_TRAP_HISTORY_LENGTH, // age of the code has reached the limit
116     Reason_LIMIT,
117 
118     // Note:  Reason_RECORDED_LIMIT should fit into 31 bits of
119     // DataLayout::trap_bits.  This dependency is enforced indirectly
120     // via asserts, to avoid excessive direct header-to-header dependencies.
121     // See Deoptimization::trap_state_reason and class DataLayout.
122     Reason_RECORDED_LIMIT = Reason_profile_predicate,  // some are not recorded per bc
123   };
124 
125   // What action must be taken by the runtime?
126   // Note: Keep this enum in sync. with Deoptimization::_trap_action_name.
127   enum DeoptAction {
128     Action_none,                  // just interpret, do not invalidate nmethod
129     Action_maybe_recompile,       // recompile the nmethod; need not invalidate
130     Action_reinterpret,           // invalidate the nmethod, reset IC, maybe recompile
131     Action_make_not_entrant,      // invalidate the nmethod, recompile (probably)
132     Action_make_not_compilable,   // invalidate the nmethod and do not compile
133     Action_LIMIT
134   };
135 
136   enum {
137     _action_bits = 3,
138     _reason_bits = 5,
139     _action_shift = 0,
140     _reason_shift = _action_shift+_action_bits,
141     BC_CASE_LIMIT = PRODUCT_ONLY(1) NOT_PRODUCT(4) // for _deoptimization_hist
142   };
143 
144   enum UnpackType {
145     Unpack_deopt                = 0, // normal deoptimization, use pc computed in unpack_vframe_on_stack
146     Unpack_exception            = 1, // exception is pending
147     Unpack_uncommon_trap        = 2, // redo last byte code (C2 only)
148     Unpack_reexecute            = 3, // reexecute bytecode (C1 only)
149     Unpack_none                 = 4, // not deoptimizing the frame, just reallocating/relocking for JVMTI
150     Unpack_LIMIT                = 5
151   };
152 
153   // Make all nmethods that are marked_for_deoptimization not_entrant and deoptimize any live
154   // activations using those nmethods. Scan of the code cache is done to
155   // find all marked nmethods and they are made not_entrant.
156   static void deoptimize_all_marked();
157 
158  public:
159   // Deoptimizes a frame lazily. Deopt happens on return to the frame.
160   static void deoptimize(JavaThread* thread, frame fr, DeoptReason reason = Reason_constraint);
161 
162   static address deoptimize_for_missing_exception_handler(nmethod* nm, bool make_not_entrant);
163 
164   private:
165   // Does the actual work for deoptimizing a single frame
166   static void deoptimize_single_frame(JavaThread* thread, frame fr, DeoptReason reason);
167 
168 #ifdef COMPILER2
169   // Deoptimize objects, that is reallocate and relock them, just before they
170   // escape through JVMTI.  The given vframes cover one physical frame.
171   static bool deoptimize_objects_internal(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk,
172                                           bool& realloc_failures);
173 
174  public:
175 
176   // Support for restoring non-escaping objects
177   static bool realloc_objects(JavaThread* thread, frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, TRAPS);
178   static bool realloc_inline_type_result(InlineKlass* vk, const RegisterMap& map, GrowableArray<Handle>& return_oops, TRAPS);
179   static void reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type);
180   static void reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj);
181   static void reassign_flat_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, flatArrayOop obj, FlatArrayKlass* vak, TRAPS);
182   static void reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures, TRAPS);
183   static bool relock_objects(JavaThread* thread, GrowableArray<MonitorInfo*>* monitors,
184                              JavaThread* deoptee_thread, frame& fr, int exec_mode, bool realloc_failures);
185   static void pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array);
186 #endif // COMPILER2
187 
188   public:
189   static vframeArray* create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures);
190 
191   // Interface used for unpacking deoptimized frames
192 
193   // UnrollBlock is returned by fetch_unroll_info() to the deoptimization handler (blob).
194   // This is only a CheapObj to ease debugging after a deopt failure
195   class UnrollBlock : public CHeapObj<mtCompiler> {
196     friend class VMStructs;
197    private:
198     int       _size_of_deoptimized_frame; // Size, in bytes, of current deoptimized frame
199     int       _caller_adjustment;         // Adjustment, in bytes, to caller's SP by initial interpreted frame
200     int       _number_of_frames;          // Number frames to unroll
201     int       _total_frame_sizes;         // Total of number*sizes frames
202     intptr_t* _frame_sizes;               // Array of frame sizes, in bytes, for unrolling the stack
203     address*  _frame_pcs;                 // Array of frame pc's, in bytes, for unrolling the stack
204     intptr_t* _register_block;            // Block for storing callee-saved registers.
205     BasicType _return_type;               // Tells if we have to restore double or long return value
206     intptr_t  _initial_info;              // Platform dependent data for the sender frame (was FP on x86)
207     int       _caller_actual_parameters;  // The number of actual arguments at the
208                                           // interpreted caller of the deoptimized frame
209     int       _unpack_kind;               // exec_mode that can be changed during fetch_unroll_info
210 
211     // The following fields are used as temps during the unpacking phase
212     // (which is tight on registers, especially on x86). They really ought
213     // to be PD variables but that involves moving this class into its own
214     // file to use the pd include mechanism. Maybe in a later cleanup ...
215     intptr_t  _counter_temp;              // SHOULD BE PD VARIABLE (x86 frame count temp)
216     intptr_t  _sender_sp_temp;            // SHOULD BE PD VARIABLE (x86 sender_sp)
217    public:
218     // Constructor
219     UnrollBlock(int  size_of_deoptimized_frame,
220                 int  caller_adjustment,
221                 int  caller_actual_parameters,
222                 int  number_of_frames,
223                 intptr_t* frame_sizes,
224                 address* frames_pcs,
225                 BasicType return_type,
226                 int unpack_kind);
227     ~UnrollBlock();
228 
229     // Accessors
230     intptr_t* frame_sizes()  const { return _frame_sizes; }
231     int number_of_frames()  const { return _number_of_frames; }
232 
233     // Returns the total size of frames
234     int size_of_frames() const;
235 
236     void set_initial_info(intptr_t info) { _initial_info = info; }
237 
238     int caller_actual_parameters() const { return _caller_actual_parameters; }
239 
240     // Accessors used by the code generator for the unpack stub.
241     static ByteSize size_of_deoptimized_frame_offset() { return byte_offset_of(UnrollBlock, _size_of_deoptimized_frame); }
242     static ByteSize caller_adjustment_offset()         { return byte_offset_of(UnrollBlock, _caller_adjustment);         }
243     static ByteSize number_of_frames_offset()          { return byte_offset_of(UnrollBlock, _number_of_frames);          }
244     static ByteSize frame_sizes_offset()               { return byte_offset_of(UnrollBlock, _frame_sizes);               }
245     static ByteSize total_frame_sizes_offset()         { return byte_offset_of(UnrollBlock, _total_frame_sizes);         }
246     static ByteSize frame_pcs_offset()                 { return byte_offset_of(UnrollBlock, _frame_pcs);                 }
247     static ByteSize counter_temp_offset()              { return byte_offset_of(UnrollBlock, _counter_temp);              }
248     static ByteSize initial_info_offset()              { return byte_offset_of(UnrollBlock, _initial_info);              }
249     static ByteSize unpack_kind_offset()               { return byte_offset_of(UnrollBlock, _unpack_kind);               }
250     static ByteSize sender_sp_temp_offset()            { return byte_offset_of(UnrollBlock, _sender_sp_temp);            }
251 
252     BasicType return_type() const { return _return_type; }
253     void print();
254   };
255 
256   //** Returns an UnrollBlock continuing information
257   // how to make room for the resulting interpreter frames.
258   // Called by assembly stub after execution has returned to
259   // deoptimized frame.
260   // @argument thread.     Thread where stub_frame resides.
261   // @see OptoRuntime::deoptimization_fetch_unroll_info_C
262   static UnrollBlock* fetch_unroll_info(JavaThread* current, int exec_mode);
263 
264   //** Unpacks vframeArray onto execution stack
265   // Called by assembly stub after execution has returned to
266   // deoptimized frame and after the stack unrolling.
267   // @argument thread.     Thread where stub_frame resides.
268   // @argument exec_mode.  Determines how execution should be continued in top frame.
269   //                       0 means continue after current byte code
270   //                       1 means exception has happened, handle exception
271   //                       2 means reexecute current bytecode (for uncommon traps).
272   // @see OptoRuntime::deoptimization_unpack_frames_C
273   // Return BasicType of call return type, if any
274   static BasicType unpack_frames(JavaThread* thread, int exec_mode);
275 
276   // Cleans up deoptimization bits on thread after unpacking or in the
277   // case of an exception.
278   static void cleanup_deopt_info(JavaThread  *thread,
279                                  vframeArray * array);
280 
281   // Restores callee saved values from deoptimized frame into oldest interpreter frame
282   // so caller of the deoptimized frame will get back the values it expects.
283   static void unwind_callee_save_values(frame* f, vframeArray* vframe_array);
284 
285   //** Performs an uncommon trap for compiled code.
286   // The top most compiler frame is converted into interpreter frames
287   static UnrollBlock* uncommon_trap(JavaThread* current, jint unloaded_class_index, jint exec_mode);
288   // Helper routine that enters the VM and may block
289   static void uncommon_trap_inner(JavaThread* current, jint unloaded_class_index);
290 
291   //** Deoptimizes the frame identified by id.
292   // Only called from VMDeoptimizeFrame
293   // @argument thread.     Thread where stub_frame resides.
294   // @argument id.         id of frame that should be deoptimized.
295   static void deoptimize_frame_internal(JavaThread* thread, intptr_t* id, DeoptReason reason);
296 
297   // if thread is not the current thread then execute
298   // VM_DeoptimizeFrame otherwise deoptimize directly.
299   static void deoptimize_frame(JavaThread* thread, intptr_t* id, DeoptReason reason);
300   static void deoptimize_frame(JavaThread* thread, intptr_t* id);
301 
302   // Statistics
303   static void gather_statistics(DeoptReason reason, DeoptAction action,
304                                 Bytecodes::Code bc = Bytecodes::_illegal);
305   static void print_statistics();
306 
307   // How much room to adjust the last frame's SP by, to make space for
308   // the callee's interpreter frame (which expects locals to be next to
309   // incoming arguments)
310   static int last_frame_adjust(int callee_parameters, int callee_locals);
311 
312   // trap_request codes
313   static DeoptReason trap_request_reason(int trap_request) {
314     if (trap_request < 0)
315       return (DeoptReason)
316         ((~(trap_request) >> _reason_shift) & right_n_bits(_reason_bits));
317     else
318       // standard reason for unloaded CP entry
319       return Reason_unloaded;
320   }
321   static DeoptAction trap_request_action(int trap_request) {
322     if (trap_request < 0)
323       return (DeoptAction)
324         ((~(trap_request) >> _action_shift) & right_n_bits(_action_bits));
325     else
326       // standard action for unloaded CP entry
327       return _unloaded_action;
328   }
329   static int trap_request_index(int trap_request) {
330     if (trap_request < 0)
331       return -1;
332     else
333       return trap_request;
334   }
335   static int make_trap_request(DeoptReason reason, DeoptAction action,
336                                int index = -1) {
337     assert((1 << _reason_bits) >= Reason_LIMIT, "enough bits");
338     assert((1 << _action_bits) >= Action_LIMIT, "enough bits");
339     int trap_request;
340     if (index != -1)
341       trap_request = index;
342     else
343       trap_request = (~(((reason) << _reason_shift)
344                         + ((action) << _action_shift)));
345     assert(reason == trap_request_reason(trap_request), "valid reason");
346     assert(action == trap_request_action(trap_request), "valid action");
347     assert(index  == trap_request_index(trap_request),  "valid index");
348     return trap_request;
349   }
350 
351   // The trap_state stored in a MDO is decoded here.
352   // It records two items of information.
353   //  reason:  If a deoptimization happened here, what its reason was,
354   //           or if there were multiple deopts with differing reasons.
355   //  recompiled: If a deoptimization here triggered a recompilation.
356   // Note that not all reasons are recorded per-bci.
357   static DeoptReason trap_state_reason(int trap_state);
358   static int  trap_state_has_reason(int trap_state, int reason);
359   static int  trap_state_add_reason(int trap_state, int reason);
360   static bool trap_state_is_recompiled(int trap_state);
361   static int  trap_state_set_recompiled(int trap_state, bool z);
362   static const char* format_trap_state(char* buf, size_t buflen,
363                                        int trap_state);
364 
365   static bool reason_is_recorded_per_bytecode(DeoptReason reason) {
366     return reason > Reason_none && reason <= Reason_RECORDED_LIMIT;
367   }
368 
369   static DeoptReason reason_recorded_per_bytecode_if_any(DeoptReason reason) {
370     if (reason_is_recorded_per_bytecode(reason))
371       return reason;
372     else if (reason == Reason_div0_check) // null check due to divide-by-zero?
373       return Reason_null_check;           // recorded per BCI as a null check
374     else if (reason == Reason_speculate_class_check)
375       return Reason_class_check;
376     else if (reason == Reason_speculate_null_check)
377       return Reason_null_check;
378     else if (reason == Reason_speculate_null_assert)
379       return Reason_null_assert;
380     else if (reason == Reason_unstable_if)
381       return Reason_intrinsic;
382     else if (reason == Reason_unstable_fused_if)
383       return Reason_range_check;
384     else
385       return Reason_none;
386   }
387 
388   static bool reason_is_speculate(int reason) {
389     if (reason == Reason_speculate_class_check ||
390         reason == Reason_speculate_null_check ||
391         reason == Reason_speculate_null_assert) {
392       return true;
393     }
394     return false;
395   }
396 
397   static DeoptReason reason_null_check(bool speculative) {
398     return speculative ? Deoptimization::Reason_speculate_null_check : Deoptimization::Reason_null_check;
399   }
400 
401   static DeoptReason reason_class_check(bool speculative) {
402     return speculative ? Deoptimization::Reason_speculate_class_check : Deoptimization::Reason_class_check;
403   }
404 
405   static DeoptReason reason_null_assert(bool speculative) {
406     return speculative ? Deoptimization::Reason_speculate_null_assert : Deoptimization::Reason_null_assert;
407   }
408 
409   static uint per_method_trap_limit(int reason) {
410     return reason_is_speculate(reason) ? (uint)PerMethodSpecTrapLimit : (uint)PerMethodTrapLimit;
411   }
412 
413   static const char* trap_reason_name(int reason);
414   static const char* trap_action_name(int action);
415   // Format like reason='foo' action='bar' index='123'.
416   // This is suitable both for XML and for tty output.
417   static const char* format_trap_request(char* buf, size_t buflen,
418                                          int trap_request);
419 
420   static jint total_deoptimization_count();
421   static jint deoptimization_count(const char* reason_str, const char* action_str);
422 
423   // JVMTI PopFrame support
424 
425   // Preserves incoming arguments to the popped frame when it is
426   // returning to a deoptimized caller
427   static void popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address);
428 
429   static MethodData* get_method_data(JavaThread* thread, const methodHandle& m, bool create_if_missing);
430  private:
431   // Update the mdo's count and per-BCI reason bits, returning previous state:
432   static ProfileData* query_update_method_data(MethodData* trap_mdo,
433                                                int trap_bci,
434                                                DeoptReason reason,
435                                                Method* compiled_method,
436                                                //outputs:
437                                                uint& ret_this_trap_count,
438                                                bool& ret_maybe_prior_trap,
439                                                bool& ret_maybe_prior_recompile);
440   // class loading support for uncommon trap
441   static void load_class_by_index(const constantPoolHandle& constant_pool, int index, TRAPS);
442 
443   static UnrollBlock* fetch_unroll_info_helper(JavaThread* current, int exec_mode);
444 
445   static DeoptAction _unloaded_action; // == Action_reinterpret;
446   static const char* _trap_reason_name[];
447   static const char* _trap_action_name[];
448 
449   static juint _deoptimization_hist[Reason_LIMIT][1+Action_LIMIT][BC_CASE_LIMIT];
450   // Note:  Histogram array size is 1-2 Kb.
451 
452  public:
453   static void update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason);
454 };
455 
456 #endif // SHARE_RUNTIME_DEOPTIMIZATION_HPP