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_COMPILATION_HPP
 26 #define SHARE_C1_C1_COMPILATION_HPP
 27 
 28 #include "ci/ciEnv.hpp"
 29 #include "ci/ciMethodData.hpp"
 30 #include "code/exceptionHandlerTable.hpp"
 31 #include "compiler/compiler_globals.hpp"
 32 #include "compiler/compilerDefinitions.inline.hpp"
 33 #include "compiler/compilerDirectives.hpp"
 34 #include "runtime/deoptimization.hpp"
 35 #include "runtime/sharedRuntime.hpp"
 36 
 37 class CompilationFailureInfo;
 38 class CompilationResourceObj;
 39 class XHandlers;
 40 class ExceptionInfo;
 41 class DebugInformationRecorder;
 42 class FrameMap;
 43 class IR;
 44 class IRScope;
 45 class Instruction;
 46 class LinearScan;
 47 class OopMap;
 48 class LIR_Emitter;
 49 class LIR_Assembler;
 50 class CodeEmitInfo;
 51 class ciEnv;
 52 class ciMethod;
 53 class ValueStack;
 54 class C1_MacroAssembler;
 55 class CFGPrinter;
 56 class CFGPrinterOutput;
 57 
 58 typedef GrowableArray<BasicType> BasicTypeArray;
 59 typedef GrowableArray<BasicType> BasicTypeList;
 60 typedef GrowableArray<ExceptionInfo*> ExceptionInfoList;
 61 
 62 class Compilation: public StackObj {
 63   friend class CompilationResourceObj;
 64  private:
 65   // compilation specifics
 66   Arena* _arena;
 67   int _next_id;
 68   int _next_block_id;
 69   AbstractCompiler*  _compiler;
 70   DirectiveSet*      _directive;
 71   ciEnv*             _env;
 72   CompileLog*        _log;
 73   ciMethod*          _method;
 74   int                _osr_bci;
 75   IR*                _hir;
 76   FrameMap*          _frame_map;
 77   C1_MacroAssembler* _masm;
 78   bool               _has_exception_handlers;
 79   bool               _has_fpu_code;
 80   bool               _has_unsafe_access;
 81   bool               _has_irreducible_loops;
 82   bool               _would_profile;
 83   bool               _has_method_handle_invokes;  // True if this method has MethodHandle invokes.
 84   bool               _has_reserved_stack_access;
 85   bool               _has_monitors; // Fastpath monitors detection for Continuations
 86   bool               _has_scoped_access; // For shared scope closure
 87   bool               _install_code;
 88   const char*        _bailout_msg;
 89   CompilationFailureInfo* _first_failure_details; // Details for the first failure happening during compilation
 90   bool               _oom;
 91   ExceptionInfoList* _exception_info_list;
 92   ExceptionHandlerTable _exception_handler_table;
 93   ImplicitExceptionTable _implicit_exception_table;
 94   LinearScan*        _allocator;
 95   CodeOffsets        _offsets;
 96   CodeBuffer         _code;
 97   bool               _has_access_indexed;
 98   int                _interpreter_frame_size; // Stack space needed in case of a deoptimization
 99   CompiledEntrySignature _compiled_entry_signature;
100   int                _immediate_oops_patched;
101 
102   // compilation helpers
103   void initialize();
104   void build_hir();
105   void emit_lir();
106 
107   void emit_code_epilog(LIR_Assembler* assembler);
108   int  emit_code_body();
109 
110   int  compile_java_method();
111   void install_code(int frame_size);
112   void compile_method();
113 
114   void generate_exception_handler_table();
115 
116   ExceptionInfoList* exception_info_list() const { return _exception_info_list; }
117   ExceptionHandlerTable* exception_handler_table() { return &_exception_handler_table; }
118 
119   void        set_allocator(LinearScan* allocator) { _allocator = allocator; }
120 
121   Instruction*       _current_instruction;       // the instruction currently being processed
122 #ifndef PRODUCT
123   Instruction*       _last_instruction_printed;  // the last instruction printed during traversal
124   CFGPrinterOutput*  _cfg_printer_output;
125 #endif // PRODUCT
126 
127  public:
128   // creation
129   Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method,
130               int osr_bci, BufferBlob* buffer_blob, bool install_code, DirectiveSet* directive);
131   ~Compilation();
132 
133 
134   static Compilation* current() {
135     return (Compilation*) ciEnv::current()->compiler_data();
136   }
137 
138   // accessors
139   ciEnv* env() const                             { return _env; }
140   DirectiveSet* directive() const                { return _directive; }
141   CompileLog* log() const                        { return _log; }
142   AbstractCompiler* compiler() const             { return _compiler; }
143   bool has_exception_handlers() const            { return _has_exception_handlers; }
144   bool has_fpu_code() const                      { return _has_fpu_code; }
145   bool has_unsafe_access() const                 { return _has_unsafe_access; }
146   bool has_monitors() const                      { return _has_monitors; }
147   bool has_scoped_access() const                 { return _has_scoped_access; }
148   bool has_irreducible_loops() const             { return _has_irreducible_loops; }
149   int max_vector_size() const                    { return 0; }
150   ciMethod* method() const                       { return _method; }
151   int osr_bci() const                            { return _osr_bci; }
152   bool is_osr_compile() const                    { return osr_bci() >= 0; }
153   IR* hir() const                                { return _hir; }
154   FrameMap* frame_map() const                    { return _frame_map; }
155   CodeBuffer* code()                             { return &_code; }
156   C1_MacroAssembler* masm() const                { return _masm; }
157   CodeOffsets* offsets()                         { return &_offsets; }
158   Arena* arena()                                 { return _arena; }
159   bool has_access_indexed()                      { return _has_access_indexed; }
160   bool should_install_code()                     { return _install_code && InstallMethods; }
161   LinearScan* allocator()                        { return _allocator; }
162 
163   // Instruction ids
164   int get_next_id()                              { return _next_id++; }
165   int number_of_instructions() const             { return _next_id; }
166 
167   // BlockBegin ids
168   int get_next_block_id()                        { return _next_block_id++; }
169   int number_of_blocks() const                   { return _next_block_id; }
170 
171   // setters
172   void set_has_exception_handlers(bool f)        { _has_exception_handlers = f; }
173   void set_has_fpu_code(bool f)                  { _has_fpu_code = f; }
174   void set_has_unsafe_access(bool f)             { _has_unsafe_access = f; }
175   void set_has_irreducible_loops(bool f)         { _has_irreducible_loops = f; }
176   void set_would_profile(bool f)                 { _would_profile = f; }
177   void set_has_access_indexed(bool f)            { _has_access_indexed = f; }
178   void set_has_monitors(bool f)                  { _has_monitors = f; }
179   void set_has_scoped_access(bool f)             { _has_scoped_access = f; }
180   // Add a set of exception handlers covering the given PC offset
181   void add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers);
182   // Statistics gathering
183   void notice_inlined_method(ciMethod* method);
184 
185   // JSR 292
186   bool     has_method_handle_invokes() const { return _has_method_handle_invokes;     }
187   void set_has_method_handle_invokes(bool z) {        _has_method_handle_invokes = z; }
188 
189   bool     has_reserved_stack_access() const { return _has_reserved_stack_access; }
190   void set_has_reserved_stack_access(bool z) { _has_reserved_stack_access = z; }
191 
192   DebugInformationRecorder* debug_info_recorder() const; // = _env->debug_info();
193   Dependencies* dependency_recorder() const; // = _env->dependencies()
194   ImplicitExceptionTable* implicit_exception_table()     { return &_implicit_exception_table; }
195 
196   Instruction* current_instruction() const       { return _current_instruction; }
197   Instruction* set_current_instruction(Instruction* instr) {
198     Instruction* previous = _current_instruction;
199     _current_instruction = instr;
200     return previous;
201   }
202 
203 #ifndef PRODUCT
204   void maybe_print_current_instruction();
205   CFGPrinterOutput* cfg_printer_output() {
206     guarantee(_cfg_printer_output != nullptr, "CFG printer output not initialized");
207     return _cfg_printer_output;
208   }
209 #endif // PRODUCT
210 
211   // MemLimit handling
212   bool oom() const { return _oom; }
213   void set_oom() { _oom = true; }
214 
215   // error handling
216   void bailout(const char* msg);
217   bool bailed_out() const                        { return _bailout_msg != nullptr; }
218   const char* bailout_msg() const                { return _bailout_msg; }
219   const CompilationFailureInfo* first_failure_details() const { return _first_failure_details; }
220 
221   const static uint desired_max_code_buffer_size = 64*K * wordSize;
222   const static uint desired_max_constant_size = desired_max_code_buffer_size / 10;
223 
224   static bool setup_code_buffer(CodeBuffer* cb, int call_stub_estimate);
225 
226   // timers
227   static void print_timers();
228 
229   bool is_profiling() {
230     return env()->comp_level() == CompLevel_full_profile ||
231            env()->comp_level() == CompLevel_limited_profile;
232   }
233 
234   // Helpers for generation of profile information
235   bool profile_branches() {
236     return env()->comp_level() == CompLevel_full_profile &&
237       C1UpdateMethodData && C1ProfileBranches;
238   }
239   bool profile_calls() {
240     return env()->comp_level() == CompLevel_full_profile &&
241       C1UpdateMethodData && C1ProfileCalls;
242   }
243   bool profile_inlined_calls() {
244     return profile_calls() && C1ProfileInlinedCalls;
245   }
246   bool profile_checkcasts() {
247     return env()->comp_level() == CompLevel_full_profile &&
248       C1UpdateMethodData && C1ProfileCheckcasts;
249   }
250   bool profile_parameters() {
251     return env()->comp_level() == CompLevel_full_profile &&
252       C1UpdateMethodData && MethodData::profile_parameters();
253   }
254   bool profile_arguments() {
255     return env()->comp_level() == CompLevel_full_profile &&
256       C1UpdateMethodData && MethodData::profile_arguments();
257   }
258   bool profile_return() {
259     return env()->comp_level() == CompLevel_full_profile &&
260       C1UpdateMethodData && MethodData::profile_return();
261   }
262   bool profile_array_accesses() {
263     return env()->comp_level() == CompLevel_full_profile &&
264       C1UpdateMethodData;
265   }
266 
267   // will compilation make optimistic assumptions that might lead to
268   // deoptimization and that the runtime will account for?
269   bool is_optimistic() {
270     return CompilerConfig::is_c1_only_no_jvmci() && !is_profiling() &&
271       (RangeCheckElimination || UseLoopInvariantCodeMotion) &&
272       method()->method_data()->trap_count(Deoptimization::Reason_none) == 0;
273   }
274 
275   ciKlass* cha_exact_type(ciType* type);
276 
277   // Dump inlining replay data to the stream.
278   void dump_inline_data(outputStream* out) { /* do nothing now */ }
279 
280   // How much stack space would the interpreter need in case of a
281   // deoptimization (worst case)
282   void update_interpreter_frame_size(int size) {
283     if (_interpreter_frame_size < size) {
284       _interpreter_frame_size = size;
285     }
286   }
287 
288   int interpreter_frame_size() const {
289     return _interpreter_frame_size;
290   }
291 
292   const CompiledEntrySignature* compiled_entry_signature() const {
293     return &_compiled_entry_signature;
294   }
295   bool needs_stack_repair() const {
296     return compiled_entry_signature()->c1_needs_stack_repair();
297   }
298 };
299 
300 
301 // Macro definitions for unified bailout-support
302 // The methods bailout() and bailed_out() are present in all classes
303 // that might bailout, but forward all calls to Compilation
304 #define BAILOUT(msg)               { bailout(msg); return;              }
305 #define BAILOUT_(msg, res)         { bailout(msg); return res;          }
306 
307 #define CHECK_BAILOUT()            { if (bailed_out()) return;          }
308 #define CHECK_BAILOUT_(res)        { if (bailed_out()) return res;      }
309 
310 // BAILOUT check with reset of bound labels
311 #define CHECK_BAILOUT1(l1)         { if (bailed_out()) { l1.reset();                         return; } }
312 #define CHECK_BAILOUT2(l1, l2)     { if (bailed_out()) { l1.reset(); l2.reset();             return; } }
313 #define CHECK_BAILOUT3(l1, l2, l3) { if (bailed_out()) { l1.reset(); l2.reset(); l3.reset(); return; } }
314 
315 
316 class InstructionMark: public StackObj {
317  private:
318   Compilation* _compilation;
319   Instruction*  _previous;
320 
321  public:
322   InstructionMark(Compilation* compilation, Instruction* instr) {
323     _compilation = compilation;
324     _previous = _compilation->set_current_instruction(instr);
325   }
326   ~InstructionMark() {
327     _compilation->set_current_instruction(_previous);
328   }
329 };
330 
331 
332 //----------------------------------------------------------------------
333 // Base class for objects allocated by the compiler in the compilation arena
334 class CompilationResourceObj {
335  public:
336   void* operator new(size_t size) throw() { return Compilation::current()->arena()->Amalloc(size); }
337   void* operator new(size_t size, Arena* arena) throw() {
338     return arena->Amalloc(size);
339   }
340   void  operator delete(void* p) {} // nothing to do
341 
342 #ifndef PRODUCT
343   // Printing support
344   void print() const;
345   virtual void print_on(outputStream* st) const;
346 #endif
347 };
348 
349 
350 //----------------------------------------------------------------------
351 // Class for aggregating exception handler information.
352 
353 // Effectively extends XHandlers class with PC offset of
354 // potentially exception-throwing instruction.
355 // This class is used at the end of the compilation to build the
356 // ExceptionHandlerTable.
357 class ExceptionInfo: public CompilationResourceObj {
358  private:
359   int             _pco;                // PC of potentially exception-throwing instruction
360   XHandlers*      _exception_handlers; // flat list of exception handlers covering this PC
361 
362  public:
363   ExceptionInfo(int pco, XHandlers* exception_handlers)
364     : _pco(pco)
365     , _exception_handlers(exception_handlers)
366   { }
367 
368   int pco()                                      { return _pco; }
369   XHandlers* exception_handlers()                { return _exception_handlers; }
370 };
371 
372 #endif // SHARE_C1_C1_COMPILATION_HPP