1 /*
  2  * Copyright (c) 1999, 2025, 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_C1_C1_IR_HPP
 26 #define SHARE_C1_C1_IR_HPP
 27 
 28 #include "c1/c1_Instruction.hpp"
 29 #include "ci/ciExceptionHandler.hpp"
 30 #include "ci/ciStreams.hpp"
 31 #include "memory/allocation.hpp"
 32 
 33 // An XHandler is a C1 internal description for an exception handler
 34 
 35 class XHandler: public CompilationResourceObj {
 36  private:
 37   ciExceptionHandler* _desc;
 38 
 39   BlockBegin*         _entry_block;  // Entry block of xhandler
 40   LIR_List*           _entry_code;   // LIR-operations that must be executed before jumping to entry_block
 41   int                 _entry_pco;    // pco where entry_code (or entry_block if no entry_code) starts
 42   int                 _phi_operand;  // For resolving of phi functions at begin of entry_block
 43   int                 _scope_count;  // for filling ExceptionRangeEntry::scope_count
 44 
 45 #ifdef ASSERT
 46   int                 _lir_op_id;    // op_id of the LIR-operation throwing to this handler
 47 #endif
 48 
 49  public:
 50   // creation
 51   XHandler(ciExceptionHandler* desc)
 52     : _desc(desc)
 53     , _entry_block(nullptr)
 54     , _entry_code(nullptr)
 55     , _entry_pco(-1)
 56     , _phi_operand(-1)
 57     , _scope_count(-1)
 58 #ifdef ASSERT
 59     , _lir_op_id(-1)
 60 #endif
 61   { }
 62 
 63   XHandler(XHandler* other)
 64     : _desc(other->_desc)
 65     , _entry_block(other->_entry_block)
 66     , _entry_code(other->_entry_code)
 67     , _entry_pco(other->_entry_pco)
 68     , _phi_operand(other->_phi_operand)
 69     , _scope_count(other->_scope_count)
 70 #ifdef ASSERT
 71     , _lir_op_id(other->_lir_op_id)
 72 #endif
 73   { }
 74 
 75   // accessors for data of ciExceptionHandler
 76   int  beg_bci() const                           { return _desc->start(); }
 77   int  end_bci() const                           { return _desc->limit(); }
 78   int  handler_bci() const                       { return _desc->handler_bci(); }
 79   bool is_catch_all() const                      { return _desc->is_catch_all(); }
 80   int  catch_type() const                        { return _desc->catch_klass_index(); }
 81   ciInstanceKlass* catch_klass() const           { return _desc->catch_klass(); }
 82   bool covers(int bci) const                     { return beg_bci() <= bci && bci < end_bci(); }
 83 
 84   // accessors for additional fields
 85   BlockBegin* entry_block() const                { return _entry_block; }
 86   LIR_List*   entry_code() const                 { return _entry_code; }
 87   int         entry_pco() const                  { return _entry_pco; }
 88   int         phi_operand() const                { assert(_phi_operand != -1, "not set"); return _phi_operand; }
 89   int         scope_count() const                { assert(_scope_count != -1, "not set"); return _scope_count; }
 90   DEBUG_ONLY(int lir_op_id() const               { return _lir_op_id; });
 91 
 92   void set_entry_block(BlockBegin* entry_block) {
 93     assert(entry_block->is_set(BlockBegin::exception_entry_flag), "must be an exception handler entry");
 94     assert(entry_block->bci() == handler_bci(), "bci's must correspond");
 95     _entry_block = entry_block;
 96   }
 97   void set_entry_code(LIR_List* entry_code)      { _entry_code = entry_code; }
 98   void set_entry_pco(int entry_pco)              { _entry_pco = entry_pco; }
 99   void set_phi_operand(int phi_operand)          { _phi_operand = phi_operand; }
100   void set_scope_count(int scope_count)          { _scope_count = scope_count; }
101   DEBUG_ONLY(void set_lir_op_id(int lir_op_id)   { _lir_op_id = lir_op_id; });
102 
103   bool equals(XHandler* other) const;
104 };
105 
106 typedef GrowableArray<XHandler*> _XHandlerList;
107 
108 // XHandlers is the C1 internal list of exception handlers for a method
109 class XHandlers: public CompilationResourceObj {
110  private:
111   _XHandlerList    _list;
112 
113  public:
114   // creation
115   XHandlers() : _list()                          { }
116   XHandlers(ciMethod* method);
117   XHandlers(XHandlers* other);
118 
119   // accessors
120   int       length() const                       { return _list.length(); }
121   XHandler* handler_at(int i) const              { return _list.at(i); }
122   bool      has_handlers() const                 { return _list.length() > 0; }
123   void      append(XHandler* h)                  { _list.append(h); }
124   XHandler* remove_last()                        { return _list.pop(); }
125 
126   bool      could_catch(ciInstanceKlass* klass, bool type_is_exact) const;
127   bool      equals(XHandlers* others) const;
128 };
129 
130 
131 class IRScope;
132 typedef GrowableArray<IRScope*> IRScopeList;
133 
134 class Compilation;
135 class IRScope: public CompilationResourceObj {
136  private:
137   // hierarchy
138   Compilation*  _compilation;                    // the current compilation
139   IRScope*      _caller;                         // the caller scope, or null
140   int           _level;                          // the inlining level
141   ciMethod*     _method;                         // the corresponding method
142   IRScopeList   _callees;                        // the inlined method scopes
143 
144   // graph
145   XHandlers*    _xhandlers;                      // the exception handlers
146   int           _number_of_locks;                // the number of monitor lock slots needed
147   bool          _monitor_pairing_ok;             // the monitor pairing info
148   bool          _wrote_final;                    // has written final field
149   bool          _wrote_fields;                   // has written fields
150   bool          _wrote_volatile;                 // has written volatile field
151   bool          _wrote_stable;                   // has written @Stable field
152   BlockBegin*   _start;                          // the start block, successsors are method entries
153 
154   ResourceBitMap _requires_phi_function;         // bit is set if phi functions at loop headers are necessary for a local variable
155 
156   // helper functions
157   BlockBegin* build_graph(Compilation* compilation, int osr_bci);
158 
159  public:
160   // creation
161   IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph = false);
162 
163   // accessors
164   Compilation*  compilation() const              { return _compilation; }
165   IRScope*      caller() const                   { return _caller; }
166   int           level() const                    { return _level; }
167   ciMethod*     method() const                   { return _method; }
168   int           max_stack() const;               // NOTE: expensive
169   BitMap&       requires_phi_function()          { return _requires_phi_function; }
170 
171   // hierarchy
172   bool          is_top_scope() const             { return _caller == nullptr; }
173   void          add_callee(IRScope* callee)      { _callees.append(callee); }
174   int           number_of_callees() const        { return _callees.length(); }
175   IRScope*      callee_no(int i) const           { return _callees.at(i); }
176 
177   // accessors, graph
178   bool          is_valid() const                 { return start() != nullptr; }
179   XHandlers*    xhandlers() const                { return _xhandlers; }
180   int           number_of_locks() const          { return _number_of_locks; }
181   void          set_min_number_of_locks(int n)   { if (n > _number_of_locks) _number_of_locks = n; }
182   bool          monitor_pairing_ok() const       { return _monitor_pairing_ok; }
183   BlockBegin*   start() const                    { return _start; }
184   void          set_wrote_final()                { _wrote_final = true; }
185   bool          wrote_final    () const          { return _wrote_final; }
186   void          set_wrote_fields()               { _wrote_fields = true; }
187   bool          wrote_fields    () const         { return _wrote_fields; }
188   void          set_wrote_volatile()             { _wrote_volatile = true; }
189   bool          wrote_volatile    () const       { return _wrote_volatile; }
190   void          set_wrote_stable()               { _wrote_stable = true; }
191   bool          wrote_stable() const             { return _wrote_stable; }
192 };
193 
194 
195 //
196 // IRScopeDebugInfo records the debug information for a particular IRScope
197 // in a particular CodeEmitInfo.  This allows the information to be computed
198 // once early enough for the OopMap to be available to the LIR and also to be
199 // reemited for different pcs using the same CodeEmitInfo without recomputing
200 // everything.
201 //
202 
203 class IRScopeDebugInfo: public CompilationResourceObj {
204  private:
205   IRScope*                      _scope;
206   int                           _bci;
207   GrowableArray<ScopeValue*>*   _locals;
208   GrowableArray<ScopeValue*>*   _expressions;
209   GrowableArray<MonitorValue*>* _monitors;
210   IRScopeDebugInfo*             _caller;
211 
212  public:
213   IRScopeDebugInfo(IRScope*                      scope,
214                    int                           bci,
215                    GrowableArray<ScopeValue*>*   locals,
216                    GrowableArray<ScopeValue*>*   expressions,
217                    GrowableArray<MonitorValue*>* monitors,
218                    IRScopeDebugInfo*             caller):
219       _scope(scope)
220     , _bci(bci)
221     , _locals(locals)
222     , _expressions(expressions)
223     , _monitors(monitors)
224     , _caller(caller) {}
225 
226 
227   IRScope*                      scope()       { return _scope;       }
228   int                           bci()         { return _bci;         }
229   GrowableArray<ScopeValue*>*   locals()      { return _locals;      }
230   GrowableArray<ScopeValue*>*   expressions() { return _expressions; }
231   GrowableArray<MonitorValue*>* monitors()    { return _monitors;    }
232   IRScopeDebugInfo*             caller()      { return _caller;      }
233 
234   //Whether we should reexecute this bytecode for deopt
235   bool should_reexecute();
236 
237   void record_debug_info(DebugInformationRecorder* recorder, int pc_offset, bool reexecute, bool is_method_handle_invoke = false) {
238     if (caller() != nullptr) {
239       // Order is significant:  Must record caller first.
240       caller()->record_debug_info(recorder, pc_offset, false/*reexecute*/);
241     }
242     DebugToken* locvals = recorder->create_scope_values(locals());
243     DebugToken* expvals = recorder->create_scope_values(expressions());
244     DebugToken* monvals = recorder->create_monitor_values(monitors());
245     // reexecute allowed only for the topmost frame
246     bool return_oop = false; // This flag will be ignored since it used only for C2 with escape analysis.
247     bool rethrow_exception = false;
248     bool has_ea_local_in_scope = false;
249     bool arg_escape = false;
250     recorder->describe_scope(pc_offset, methodHandle(), scope()->method(), bci(),
251                              reexecute, rethrow_exception, is_method_handle_invoke, return_oop,
252                              has_ea_local_in_scope, arg_escape, locvals, expvals, monvals);
253   }
254 };
255 
256 
257 class CodeEmitInfo: public CompilationResourceObj {
258   friend class LinearScan;
259  private:
260   IRScopeDebugInfo* _scope_debug_info;
261   IRScope*          _scope;
262   XHandlers*        _exception_handlers;
263   OopMap*           _oop_map;
264   ValueStack*       _stack;                      // used by deoptimization (contains also monitors
265   bool              _is_method_handle_invoke;    // true if the associated call site is a MethodHandle call site.
266   bool              _deoptimize_on_exception;
267   bool              _force_reexecute;            // force the reexecute flag on, used for patching stub
268 
269   FrameMap*     frame_map() const                { return scope()->compilation()->frame_map(); }
270   Compilation*  compilation() const              { return scope()->compilation(); }
271 
272  public:
273 
274   // use scope from ValueStack
275   CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers, bool deoptimize_on_exception = false);
276 
277   // make a copy
278   CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack = nullptr);
279 
280   // accessors
281   OopMap* oop_map()                              { return _oop_map; }
282   ciMethod* method() const                       { return _scope->method(); }
283   IRScope* scope() const                         { return _scope; }
284   XHandlers* exception_handlers() const          { return _exception_handlers; }
285   ValueStack* stack() const                      { return _stack; }
286   bool deoptimize_on_exception() const           { return _deoptimize_on_exception; }
287 
288   void add_register_oop(LIR_Opr opr);
289   void record_debug_info(DebugInformationRecorder* recorder, int pc_offset);
290 
291   bool     is_method_handle_invoke() const { return _is_method_handle_invoke;     }
292   void set_is_method_handle_invoke(bool x) {        _is_method_handle_invoke = x; }
293 
294   bool     force_reexecute() const         { return _force_reexecute;             }
295   void     set_force_reexecute()           { _force_reexecute = true;             }
296 
297   int interpreter_frame_size() const;
298 
299 };
300 
301 
302 class IR: public CompilationResourceObj {
303  private:
304   Compilation*     _compilation;                 // the current compilation
305   IRScope*         _top_scope;                   // the root of the scope hierarchy
306   int              _num_loops;                   // Total number of loops
307   BlockList*       _code;                        // the blocks in code generation order w/ use counts
308 
309  public:
310   // creation
311   IR(Compilation* compilation, ciMethod* method, int osr_bci);
312 
313   // accessors
314   bool             is_valid() const              { return top_scope()->is_valid(); }
315   Compilation*     compilation() const           { return _compilation; }
316   IRScope*         top_scope() const             { return _top_scope; }
317   int              number_of_locks() const       { return top_scope()->number_of_locks(); }
318   ciMethod*        method() const                { return top_scope()->method(); }
319   BlockBegin*      start() const                 { return top_scope()->start(); }
320   BlockBegin*      std_entry() const             { return start()->end()->as_Base()->std_entry(); }
321   BlockBegin*      osr_entry() const             { return start()->end()->as_Base()->osr_entry(); }
322   BlockList*       code() const                  { return _code; }
323   int              num_loops() const             { return _num_loops; }
324   int              max_stack() const             { return top_scope()->max_stack(); } // expensive
325 
326   // ir manipulation
327   void optimize_blocks();
328   void eliminate_null_checks();
329   void compute_predecessors();
330   void split_critical_edges();
331   void compute_code();
332   void compute_use_counts();
333 
334   // The linear-scan order and the code emission order are equal, but
335   // this may change in future
336   BlockList* linear_scan_order() {  assert(_code != nullptr, "not computed"); return _code; }
337 
338   // iteration
339   void iterate_preorder   (BlockClosure* closure);
340   void iterate_postorder  (BlockClosure* closure);
341   void iterate_linear_scan_order(BlockClosure* closure);
342 
343   // debugging
344   static void print(BlockBegin* start, bool cfg_only, bool live_only = false) PRODUCT_RETURN;
345   void print(bool cfg_only, bool live_only = false)                           PRODUCT_RETURN;
346 
347   void expand_with_neighborhood(BlockList& blocks)                          NOT_DEBUG_RETURN;
348   void verify_local(BlockList&)                                             NOT_DEBUG_RETURN;
349   void verify()                                                             NOT_DEBUG_RETURN;
350 };
351 
352 
353 // Globally do instruction substitution and remove substituted
354 // instructions from the instruction list.
355 //
356 
357 class SubstitutionResolver: public BlockClosure, ValueVisitor {
358   virtual void visit(Value* v);
359 
360  public:
361   SubstitutionResolver(IR* hir) {
362     hir->iterate_preorder(this);
363   }
364 
365   SubstitutionResolver(BlockBegin* block) {
366     block->iterate_preorder(this);
367   }
368 
369   virtual void block_do(BlockBegin* block);
370 };
371 
372 #endif // SHARE_C1_C1_IR_HPP