aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_C1_C1_COMPILATION_HPP aoqi@0: #define SHARE_VM_C1_C1_COMPILATION_HPP aoqi@0: aoqi@0: #include "ci/ciEnv.hpp" aoqi@0: #include "ci/ciMethodData.hpp" aoqi@0: #include "code/exceptionHandlerTable.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "runtime/deoptimization.hpp" aoqi@0: aoqi@0: class CompilationResourceObj; aoqi@0: class XHandlers; aoqi@0: class ExceptionInfo; aoqi@0: class DebugInformationRecorder; aoqi@0: class FrameMap; aoqi@0: class IR; aoqi@0: class IRScope; aoqi@0: class Instruction; aoqi@0: class LinearScan; aoqi@0: class OopMap; aoqi@0: class LIR_Emitter; aoqi@0: class LIR_Assembler; aoqi@0: class CodeEmitInfo; aoqi@0: class ciEnv; aoqi@0: class ciMethod; aoqi@0: class ValueStack; aoqi@0: class LIR_OprDesc; aoqi@0: class C1_MacroAssembler; aoqi@0: class CFGPrinter; aoqi@0: typedef LIR_OprDesc* LIR_Opr; aoqi@0: aoqi@0: aoqi@0: define_array(BasicTypeArray, BasicType) aoqi@0: define_stack(BasicTypeList, BasicTypeArray) aoqi@0: aoqi@0: define_array(ExceptionInfoArray, ExceptionInfo*) aoqi@0: define_stack(ExceptionInfoList, ExceptionInfoArray) aoqi@0: aoqi@0: class Compilation: public StackObj { aoqi@0: friend class CompilationResourceObj; aoqi@0: private: aoqi@0: // compilation specifics aoqi@0: Arena* _arena; aoqi@0: int _next_id; aoqi@0: int _next_block_id; aoqi@0: AbstractCompiler* _compiler; aoqi@0: ciEnv* _env; aoqi@0: CompileLog* _log; aoqi@0: ciMethod* _method; aoqi@0: int _osr_bci; aoqi@0: IR* _hir; aoqi@0: int _max_spills; aoqi@0: FrameMap* _frame_map; aoqi@0: C1_MacroAssembler* _masm; aoqi@0: bool _has_exception_handlers; aoqi@0: bool _has_fpu_code; aoqi@0: bool _has_unsafe_access; aoqi@0: bool _would_profile; aoqi@0: bool _has_method_handle_invokes; // True if this method has MethodHandle invokes. aoqi@0: const char* _bailout_msg; aoqi@0: ExceptionInfoList* _exception_info_list; aoqi@0: ExceptionHandlerTable _exception_handler_table; aoqi@0: ImplicitExceptionTable _implicit_exception_table; aoqi@0: LinearScan* _allocator; aoqi@0: CodeOffsets _offsets; aoqi@0: CodeBuffer _code; aoqi@0: bool _has_access_indexed; aoqi@0: int _interpreter_frame_size; // Stack space needed in case of a deoptimization aoqi@0: aoqi@0: // compilation helpers aoqi@0: void initialize(); aoqi@0: void build_hir(); aoqi@0: void emit_lir(); aoqi@0: aoqi@0: void emit_code_epilog(LIR_Assembler* assembler); aoqi@0: int emit_code_body(); aoqi@0: aoqi@0: int compile_java_method(); aoqi@0: void install_code(int frame_size); aoqi@0: void compile_method(); aoqi@0: aoqi@0: void generate_exception_handler_table(); aoqi@0: aoqi@0: ExceptionInfoList* exception_info_list() const { return _exception_info_list; } aoqi@0: ExceptionHandlerTable* exception_handler_table() { return &_exception_handler_table; } aoqi@0: aoqi@0: LinearScan* allocator() { return _allocator; } aoqi@0: void set_allocator(LinearScan* allocator) { _allocator = allocator; } aoqi@0: aoqi@0: Instruction* _current_instruction; // the instruction currently being processed aoqi@0: #ifndef PRODUCT aoqi@0: Instruction* _last_instruction_printed; // the last instruction printed during traversal aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method, aoqi@0: int osr_bci, BufferBlob* buffer_blob); aoqi@0: ~Compilation(); aoqi@0: aoqi@0: aoqi@0: static Compilation* current() { aoqi@0: return (Compilation*) ciEnv::current()->compiler_data(); aoqi@0: } aoqi@0: aoqi@0: // accessors aoqi@0: ciEnv* env() const { return _env; } aoqi@0: CompileLog* log() const { return _log; } aoqi@0: AbstractCompiler* compiler() const { return _compiler; } aoqi@0: bool has_exception_handlers() const { return _has_exception_handlers; } aoqi@0: bool has_fpu_code() const { return _has_fpu_code; } aoqi@0: bool has_unsafe_access() const { return _has_unsafe_access; } aoqi@0: int max_vector_size() const { return 0; } aoqi@0: ciMethod* method() const { return _method; } aoqi@0: int osr_bci() const { return _osr_bci; } aoqi@0: bool is_osr_compile() const { return osr_bci() >= 0; } aoqi@0: IR* hir() const { return _hir; } aoqi@0: int max_spills() const { return _max_spills; } aoqi@0: FrameMap* frame_map() const { return _frame_map; } aoqi@0: CodeBuffer* code() { return &_code; } aoqi@0: C1_MacroAssembler* masm() const { return _masm; } aoqi@0: CodeOffsets* offsets() { return &_offsets; } aoqi@0: Arena* arena() { return _arena; } aoqi@0: bool has_access_indexed() { return _has_access_indexed; } aoqi@0: aoqi@0: // Instruction ids aoqi@0: int get_next_id() { return _next_id++; } aoqi@0: int number_of_instructions() const { return _next_id; } aoqi@0: aoqi@0: // BlockBegin ids aoqi@0: int get_next_block_id() { return _next_block_id++; } aoqi@0: int number_of_blocks() const { return _next_block_id; } aoqi@0: aoqi@0: // setters aoqi@0: void set_has_exception_handlers(bool f) { _has_exception_handlers = f; } aoqi@0: void set_has_fpu_code(bool f) { _has_fpu_code = f; } aoqi@0: void set_has_unsafe_access(bool f) { _has_unsafe_access = f; } aoqi@0: void set_would_profile(bool f) { _would_profile = f; } aoqi@0: void set_has_access_indexed(bool f) { _has_access_indexed = f; } aoqi@0: // Add a set of exception handlers covering the given PC offset aoqi@0: void add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers); aoqi@0: // Statistics gathering aoqi@0: void notice_inlined_method(ciMethod* method); aoqi@0: aoqi@0: // JSR 292 aoqi@0: bool has_method_handle_invokes() const { return _has_method_handle_invokes; } aoqi@0: void set_has_method_handle_invokes(bool z) { _has_method_handle_invokes = z; } aoqi@0: aoqi@0: DebugInformationRecorder* debug_info_recorder() const; // = _env->debug_info(); aoqi@0: Dependencies* dependency_recorder() const; // = _env->dependencies() aoqi@0: ImplicitExceptionTable* implicit_exception_table() { return &_implicit_exception_table; } aoqi@0: aoqi@0: Instruction* current_instruction() const { return _current_instruction; } aoqi@0: Instruction* set_current_instruction(Instruction* instr) { aoqi@0: Instruction* previous = _current_instruction; aoqi@0: _current_instruction = instr; aoqi@0: return previous; aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void maybe_print_current_instruction(); aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: // error handling aoqi@0: void bailout(const char* msg); aoqi@0: bool bailed_out() const { return _bailout_msg != NULL; } aoqi@0: const char* bailout_msg() const { return _bailout_msg; } aoqi@0: aoqi@0: static int desired_max_code_buffer_size() { aoqi@0: #ifndef PPC aoqi@0: return (int) NMethodSizeLimit; // default 256K or 512K aoqi@0: #else aoqi@0: // conditional branches on PPC are restricted to 16 bit signed aoqi@0: return MIN2((unsigned int)NMethodSizeLimit,32*K); aoqi@0: #endif aoqi@0: } aoqi@0: static int desired_max_constant_size() { aoqi@0: return desired_max_code_buffer_size() / 10; aoqi@0: } aoqi@0: aoqi@0: static bool setup_code_buffer(CodeBuffer* cb, int call_stub_estimate); aoqi@0: aoqi@0: // timers aoqi@0: static void print_timers(); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // debugging support. aoqi@0: // produces a file named c1compileonly in the current directory with aoqi@0: // directives to compile only the current method and it's inlines. aoqi@0: // The file can be passed to the command line option -XX:Flags= aoqi@0: void compile_only_this_method(); aoqi@0: void compile_only_this_scope(outputStream* st, IRScope* scope); aoqi@0: void exclude_this_method(); aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: bool is_profiling() { aoqi@0: return env()->comp_level() == CompLevel_full_profile || aoqi@0: env()->comp_level() == CompLevel_limited_profile; aoqi@0: } aoqi@0: bool count_invocations() { return is_profiling(); } aoqi@0: bool count_backedges() { return is_profiling(); } aoqi@0: aoqi@0: // Helpers for generation of profile information aoqi@0: bool profile_branches() { aoqi@0: return env()->comp_level() == CompLevel_full_profile && aoqi@0: C1UpdateMethodData && C1ProfileBranches; aoqi@0: } aoqi@0: bool profile_calls() { aoqi@0: return env()->comp_level() == CompLevel_full_profile && aoqi@0: C1UpdateMethodData && C1ProfileCalls; aoqi@0: } aoqi@0: bool profile_inlined_calls() { aoqi@0: return profile_calls() && C1ProfileInlinedCalls; aoqi@0: } aoqi@0: bool profile_checkcasts() { aoqi@0: return env()->comp_level() == CompLevel_full_profile && aoqi@0: C1UpdateMethodData && C1ProfileCheckcasts; aoqi@0: } aoqi@0: bool profile_parameters() { aoqi@0: return env()->comp_level() == CompLevel_full_profile && aoqi@0: C1UpdateMethodData && MethodData::profile_parameters(); aoqi@0: } aoqi@0: bool profile_arguments() { aoqi@0: return env()->comp_level() == CompLevel_full_profile && aoqi@0: C1UpdateMethodData && MethodData::profile_arguments(); aoqi@0: } aoqi@0: bool profile_return() { aoqi@0: return env()->comp_level() == CompLevel_full_profile && aoqi@0: C1UpdateMethodData && MethodData::profile_return(); aoqi@0: } aoqi@0: // will compilation make optimistic assumptions that might lead to aoqi@0: // deoptimization and that the runtime will account for? aoqi@0: bool is_optimistic() const { aoqi@0: return !TieredCompilation && aoqi@0: (RangeCheckElimination || UseLoopInvariantCodeMotion) && aoqi@0: method()->method_data()->trap_count(Deoptimization::Reason_none) == 0; aoqi@0: } aoqi@0: aoqi@0: ciKlass* cha_exact_type(ciType* type); aoqi@0: aoqi@0: // Dump inlining replay data to the stream. aoqi@0: void dump_inline_data(outputStream* out) { /* do nothing now */ } aoqi@0: aoqi@0: // How much stack space would the interpreter need in case of a aoqi@0: // deoptimization (worst case) aoqi@0: void update_interpreter_frame_size(int size) { aoqi@0: if (_interpreter_frame_size < size) { aoqi@0: _interpreter_frame_size = size; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: int interpreter_frame_size() const { aoqi@0: return _interpreter_frame_size; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Macro definitions for unified bailout-support aoqi@0: // The methods bailout() and bailed_out() are present in all classes aoqi@0: // that might bailout, but forward all calls to Compilation aoqi@0: #define BAILOUT(msg) { bailout(msg); return; } aoqi@0: #define BAILOUT_(msg, res) { bailout(msg); return res; } aoqi@0: aoqi@0: #define CHECK_BAILOUT() { if (bailed_out()) return; } aoqi@0: #define CHECK_BAILOUT_(res) { if (bailed_out()) return res; } aoqi@0: aoqi@0: aoqi@0: class InstructionMark: public StackObj { aoqi@0: private: aoqi@0: Compilation* _compilation; aoqi@0: Instruction* _previous; aoqi@0: aoqi@0: public: aoqi@0: InstructionMark(Compilation* compilation, Instruction* instr) { aoqi@0: _compilation = compilation; aoqi@0: _previous = _compilation->set_current_instruction(instr); aoqi@0: } aoqi@0: ~InstructionMark() { aoqi@0: _compilation->set_current_instruction(_previous); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: //---------------------------------------------------------------------- aoqi@0: // Base class for objects allocated by the compiler in the compilation arena aoqi@0: class CompilationResourceObj ALLOCATION_SUPER_CLASS_SPEC { aoqi@0: public: aoqi@0: void* operator new(size_t size) throw() { return Compilation::current()->arena()->Amalloc(size); } aoqi@0: void* operator new(size_t size, Arena* arena) throw() { aoqi@0: return arena->Amalloc(size); aoqi@0: } aoqi@0: void operator delete(void* p) {} // nothing to do aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: //---------------------------------------------------------------------- aoqi@0: // Class for aggregating exception handler information. aoqi@0: aoqi@0: // Effectively extends XHandlers class with PC offset of aoqi@0: // potentially exception-throwing instruction. aoqi@0: // This class is used at the end of the compilation to build the aoqi@0: // ExceptionHandlerTable. aoqi@0: class ExceptionInfo: public CompilationResourceObj { aoqi@0: private: aoqi@0: int _pco; // PC of potentially exception-throwing instruction aoqi@0: XHandlers* _exception_handlers; // flat list of exception handlers covering this PC aoqi@0: aoqi@0: public: aoqi@0: ExceptionInfo(int pco, XHandlers* exception_handlers) aoqi@0: : _pco(pco) aoqi@0: , _exception_handlers(exception_handlers) aoqi@0: { } aoqi@0: aoqi@0: int pco() { return _pco; } aoqi@0: XHandlers* exception_handlers() { return _exception_handlers; } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_C1_C1_COMPILATION_HPP