duke@435: /* duke@435: * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_c1_Compilation.cpp.incl" duke@435: duke@435: duke@435: typedef enum { duke@435: _t_compile, duke@435: _t_setup, duke@435: _t_optimizeIR, duke@435: _t_buildIR, duke@435: _t_emit_lir, duke@435: _t_linearScan, duke@435: _t_lirGeneration, duke@435: _t_lir_schedule, duke@435: _t_codeemit, duke@435: _t_codeinstall, duke@435: max_phase_timers duke@435: } TimerName; duke@435: duke@435: static const char * timer_name[] = { duke@435: "compile", duke@435: "setup", duke@435: "optimizeIR", duke@435: "buildIR", duke@435: "emit_lir", duke@435: "linearScan", duke@435: "lirGeneration", duke@435: "lir_schedule", duke@435: "codeemit", duke@435: "codeinstall" duke@435: }; duke@435: duke@435: static elapsedTimer timers[max_phase_timers]; duke@435: static int totalInstructionNodes = 0; duke@435: duke@435: class PhaseTraceTime: public TraceTime { duke@435: private: duke@435: JavaThread* _thread; duke@435: duke@435: public: duke@435: PhaseTraceTime(TimerName timer): duke@435: TraceTime("", &timers[timer], CITime || CITimeEach, Verbose) { duke@435: } duke@435: }; duke@435: duke@435: Arena* Compilation::_arena = NULL; duke@435: Compilation* Compilation::_compilation = NULL; duke@435: duke@435: // Implementation of Compilation duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void Compilation::maybe_print_current_instruction() { duke@435: if (_current_instruction != NULL && _last_instruction_printed != _current_instruction) { duke@435: _last_instruction_printed = _current_instruction; duke@435: _current_instruction->print_line(); duke@435: } duke@435: } duke@435: #endif // PRODUCT duke@435: duke@435: duke@435: DebugInformationRecorder* Compilation::debug_info_recorder() const { duke@435: return _env->debug_info(); duke@435: } duke@435: duke@435: duke@435: Dependencies* Compilation::dependency_recorder() const { duke@435: return _env->dependencies(); duke@435: } duke@435: duke@435: duke@435: void Compilation::initialize() { duke@435: // Use an oop recorder bound to the CI environment. duke@435: // (The default oop recorder is ignorant of the CI.) duke@435: OopRecorder* ooprec = new OopRecorder(_env->arena()); duke@435: _env->set_oop_recorder(ooprec); duke@435: _env->set_debug_info(new DebugInformationRecorder(ooprec)); duke@435: debug_info_recorder()->set_oopmaps(new OopMapSet()); duke@435: _env->set_dependencies(new Dependencies(_env)); duke@435: } duke@435: duke@435: duke@435: void Compilation::build_hir() { duke@435: CHECK_BAILOUT(); duke@435: duke@435: // setup ir duke@435: _hir = new IR(this, method(), osr_bci()); duke@435: if (!_hir->is_valid()) { duke@435: bailout("invalid parsing"); duke@435: return; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: if (PrintCFGToFile) { duke@435: CFGPrinter::print_cfg(_hir, "After Generation of HIR", true, false); duke@435: } duke@435: #endif duke@435: duke@435: #ifndef PRODUCT duke@435: if (PrintCFG || PrintCFG0) { tty->print_cr("CFG after parsing"); _hir->print(true); } duke@435: if (PrintIR || PrintIR0 ) { tty->print_cr("IR after parsing"); _hir->print(false); } duke@435: #endif duke@435: duke@435: _hir->verify(); duke@435: duke@435: if (UseC1Optimizations) { duke@435: NEEDS_CLEANUP duke@435: // optimization duke@435: PhaseTraceTime timeit(_t_optimizeIR); duke@435: duke@435: _hir->optimize(); duke@435: } duke@435: duke@435: _hir->verify(); duke@435: duke@435: _hir->split_critical_edges(); duke@435: duke@435: #ifndef PRODUCT duke@435: if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after optimizations"); _hir->print(true); } duke@435: if (PrintIR || PrintIR1 ) { tty->print_cr("IR after optimizations"); _hir->print(false); } duke@435: #endif duke@435: duke@435: _hir->verify(); duke@435: duke@435: // compute block ordering for code generation duke@435: // the control flow must not be changed from here on duke@435: _hir->compute_code(); duke@435: duke@435: if (UseGlobalValueNumbering) { duke@435: ResourceMark rm; duke@435: int instructions = Instruction::number_of_instructions(); duke@435: GlobalValueNumbering gvn(_hir); duke@435: assert(instructions == Instruction::number_of_instructions(), duke@435: "shouldn't have created an instructions"); duke@435: } duke@435: duke@435: // compute use counts after global value numbering duke@435: _hir->compute_use_counts(); duke@435: duke@435: #ifndef PRODUCT duke@435: if (PrintCFG || PrintCFG2) { tty->print_cr("CFG before code generation"); _hir->code()->print(true); } duke@435: if (PrintIR || PrintIR2 ) { tty->print_cr("IR before code generation"); _hir->code()->print(false, true); } duke@435: #endif duke@435: duke@435: _hir->verify(); duke@435: } duke@435: duke@435: duke@435: void Compilation::emit_lir() { duke@435: CHECK_BAILOUT(); duke@435: duke@435: LIRGenerator gen(this, method()); duke@435: { duke@435: PhaseTraceTime timeit(_t_lirGeneration); duke@435: hir()->iterate_linear_scan_order(&gen); duke@435: } duke@435: duke@435: CHECK_BAILOUT(); duke@435: duke@435: { duke@435: PhaseTraceTime timeit(_t_linearScan); duke@435: duke@435: LinearScan* allocator = new LinearScan(hir(), &gen, frame_map()); duke@435: set_allocator(allocator); duke@435: // Assign physical registers to LIR operands using a linear scan algorithm. duke@435: allocator->do_linear_scan(); duke@435: CHECK_BAILOUT(); duke@435: duke@435: _max_spills = allocator->max_spills(); duke@435: } duke@435: duke@435: if (BailoutAfterLIR) { duke@435: if (PrintLIR && !bailed_out()) { duke@435: print_LIR(hir()->code()); duke@435: } duke@435: bailout("Bailing out because of -XX:+BailoutAfterLIR"); duke@435: } duke@435: } duke@435: duke@435: duke@435: void Compilation::emit_code_epilog(LIR_Assembler* assembler) { duke@435: CHECK_BAILOUT(); duke@435: duke@435: // generate code or slow cases duke@435: assembler->emit_slow_case_stubs(); duke@435: CHECK_BAILOUT(); duke@435: duke@435: // generate exception adapters duke@435: assembler->emit_exception_entries(exception_info_list()); duke@435: CHECK_BAILOUT(); duke@435: duke@435: // generate code for exception handler duke@435: assembler->emit_exception_handler(); duke@435: CHECK_BAILOUT(); duke@435: assembler->emit_deopt_handler(); duke@435: CHECK_BAILOUT(); duke@435: duke@435: // done duke@435: masm()->flush(); duke@435: } duke@435: duke@435: duke@435: int Compilation::emit_code_body() { duke@435: // emit code duke@435: Runtime1::setup_code_buffer(code(), allocator()->num_calls()); duke@435: code()->initialize_oop_recorder(env()->oop_recorder()); duke@435: duke@435: _masm = new C1_MacroAssembler(code()); duke@435: _masm->set_oop_recorder(env()->oop_recorder()); duke@435: duke@435: LIR_Assembler lir_asm(this); duke@435: duke@435: lir_asm.emit_code(hir()->code()); duke@435: CHECK_BAILOUT_(0); duke@435: duke@435: emit_code_epilog(&lir_asm); duke@435: CHECK_BAILOUT_(0); duke@435: duke@435: generate_exception_handler_table(); duke@435: duke@435: #ifndef PRODUCT duke@435: if (PrintExceptionHandlers && Verbose) { duke@435: exception_handler_table()->print(); duke@435: } duke@435: #endif /* PRODUCT */ duke@435: duke@435: return frame_map()->framesize(); duke@435: } duke@435: duke@435: duke@435: int Compilation::compile_java_method() { duke@435: assert(!method()->is_native(), "should not reach here"); duke@435: duke@435: if (BailoutOnExceptionHandlers) { duke@435: if (method()->has_exception_handlers()) { duke@435: bailout("linear scan can't handle exception handlers"); duke@435: } duke@435: } duke@435: duke@435: CHECK_BAILOUT_(no_frame_size); duke@435: duke@435: { duke@435: PhaseTraceTime timeit(_t_buildIR); duke@435: build_hir(); duke@435: } duke@435: if (BailoutAfterHIR) { duke@435: BAILOUT_("Bailing out because of -XX:+BailoutAfterHIR", no_frame_size); duke@435: } duke@435: duke@435: duke@435: { duke@435: PhaseTraceTime timeit(_t_emit_lir); duke@435: duke@435: _frame_map = new FrameMap(method(), hir()->number_of_locks(), MAX2(4, hir()->max_stack())); duke@435: emit_lir(); duke@435: } duke@435: CHECK_BAILOUT_(no_frame_size); duke@435: duke@435: { duke@435: PhaseTraceTime timeit(_t_codeemit); duke@435: return emit_code_body(); duke@435: } duke@435: } duke@435: duke@435: void Compilation::install_code(int frame_size) { duke@435: // frame_size is in 32-bit words so adjust it intptr_t words duke@435: assert(frame_size == frame_map()->framesize(), "must match"); duke@435: assert(in_bytes(frame_map()->framesize_in_bytes()) % sizeof(intptr_t) == 0, "must be at least pointer aligned"); duke@435: _env->register_method( duke@435: method(), duke@435: osr_bci(), duke@435: &_offsets, duke@435: in_bytes(_frame_map->sp_offset_for_orig_pc()), duke@435: code(), duke@435: in_bytes(frame_map()->framesize_in_bytes()) / sizeof(intptr_t), duke@435: debug_info_recorder()->_oopmaps, duke@435: exception_handler_table(), duke@435: implicit_exception_table(), duke@435: compiler(), duke@435: _env->comp_level(), duke@435: needs_debug_information(), duke@435: has_unsafe_access() duke@435: ); duke@435: } duke@435: duke@435: duke@435: void Compilation::compile_method() { duke@435: // setup compilation duke@435: initialize(); duke@435: duke@435: if (!method()->can_be_compiled()) { duke@435: // Prevent race condition 6328518. duke@435: // This can happen if the method is obsolete or breakpointed. duke@435: bailout("Bailing out because method is not compilable"); duke@435: return; duke@435: } duke@435: kvn@1215: if (_env->jvmti_can_hotswap_or_post_breakpoint()) { duke@435: // We can assert evol_method because method->can_be_compiled is true. duke@435: dependency_recorder()->assert_evol_method(method()); duke@435: } duke@435: duke@435: if (method()->break_at_execute()) { duke@435: BREAKPOINT; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: if (PrintCFGToFile) { duke@435: CFGPrinter::print_compilation(this); duke@435: } duke@435: #endif duke@435: duke@435: // compile method duke@435: int frame_size = compile_java_method(); duke@435: duke@435: // bailout if method couldn't be compiled duke@435: // Note: make sure we mark the method as not compilable! duke@435: CHECK_BAILOUT(); duke@435: duke@435: if (InstallMethods) { duke@435: // install code duke@435: PhaseTraceTime timeit(_t_codeinstall); duke@435: install_code(frame_size); duke@435: } duke@435: totalInstructionNodes += Instruction::number_of_instructions(); duke@435: } duke@435: duke@435: duke@435: void Compilation::generate_exception_handler_table() { duke@435: // Generate an ExceptionHandlerTable from the exception handler duke@435: // information accumulated during the compilation. duke@435: ExceptionInfoList* info_list = exception_info_list(); duke@435: duke@435: if (info_list->length() == 0) { duke@435: return; duke@435: } duke@435: duke@435: // allocate some arrays for use by the collection code. duke@435: const int num_handlers = 5; duke@435: GrowableArray* bcis = new GrowableArray(num_handlers); duke@435: GrowableArray* scope_depths = new GrowableArray(num_handlers); duke@435: GrowableArray* pcos = new GrowableArray(num_handlers); duke@435: duke@435: for (int i = 0; i < info_list->length(); i++) { duke@435: ExceptionInfo* info = info_list->at(i); duke@435: XHandlers* handlers = info->exception_handlers(); duke@435: duke@435: // empty the arrays duke@435: bcis->trunc_to(0); duke@435: scope_depths->trunc_to(0); duke@435: pcos->trunc_to(0); duke@435: duke@435: for (int i = 0; i < handlers->length(); i++) { duke@435: XHandler* handler = handlers->handler_at(i); duke@435: assert(handler->entry_pco() != -1, "must have been generated"); duke@435: duke@435: int e = bcis->find(handler->handler_bci()); duke@435: if (e >= 0 && scope_depths->at(e) == handler->scope_count()) { duke@435: // two different handlers are declared to dispatch to the same duke@435: // catch bci. During parsing we created edges for each duke@435: // handler but we really only need one. The exception handler duke@435: // table will also get unhappy if we try to declare both since duke@435: // it's nonsensical. Just skip this handler. duke@435: continue; duke@435: } duke@435: duke@435: bcis->append(handler->handler_bci()); duke@435: if (handler->handler_bci() == -1) { duke@435: // insert a wildcard handler at scope depth 0 so that the duke@435: // exception lookup logic with find it. duke@435: scope_depths->append(0); duke@435: } else { duke@435: scope_depths->append(handler->scope_count()); duke@435: } duke@435: pcos->append(handler->entry_pco()); duke@435: duke@435: // stop processing once we hit a catch any duke@435: if (handler->is_catch_all()) { duke@435: assert(i == handlers->length() - 1, "catch all must be last handler"); duke@435: } duke@435: } duke@435: exception_handler_table()->add_subtable(info->pco(), bcis, scope_depths, pcos); duke@435: } duke@435: } duke@435: duke@435: duke@435: Compilation::Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method, int osr_bci) duke@435: : _compiler(compiler) duke@435: , _env(env) duke@435: , _method(method) duke@435: , _osr_bci(osr_bci) duke@435: , _hir(NULL) duke@435: , _max_spills(-1) duke@435: , _frame_map(NULL) duke@435: , _masm(NULL) duke@435: , _has_exception_handlers(false) duke@435: , _has_fpu_code(true) // pessimistic assumption duke@435: , _has_unsafe_access(false) duke@435: , _bailout_msg(NULL) duke@435: , _exception_info_list(NULL) duke@435: , _allocator(NULL) duke@435: , _code(Runtime1::get_buffer_blob()->instructions_begin(), duke@435: Runtime1::get_buffer_blob()->instructions_size()) duke@435: , _current_instruction(NULL) duke@435: #ifndef PRODUCT duke@435: , _last_instruction_printed(NULL) duke@435: #endif // PRODUCT duke@435: { duke@435: PhaseTraceTime timeit(_t_compile); duke@435: duke@435: assert(_arena == NULL, "shouldn't only one instance of Compilation in existence at a time"); duke@435: _arena = Thread::current()->resource_area(); duke@435: _compilation = this; kvn@1215: _needs_debug_information = _env->jvmti_can_examine_or_deopt_anywhere() || duke@435: JavaMonitorsInStackTrace || AlwaysEmitDebugInfo || DeoptimizeALot; duke@435: _exception_info_list = new ExceptionInfoList(); duke@435: _implicit_exception_table.set_size(0); duke@435: compile_method(); duke@435: } duke@435: duke@435: Compilation::~Compilation() { duke@435: _arena = NULL; duke@435: _compilation = NULL; duke@435: } duke@435: duke@435: duke@435: void Compilation::add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers) { duke@435: #ifndef PRODUCT duke@435: if (PrintExceptionHandlers && Verbose) { duke@435: tty->print_cr(" added exception scope for pco %d", pco); duke@435: } duke@435: #endif duke@435: // Note: we do not have program counters for these exception handlers yet duke@435: exception_info_list()->push(new ExceptionInfo(pco, exception_handlers)); duke@435: } duke@435: duke@435: duke@435: void Compilation::notice_inlined_method(ciMethod* method) { duke@435: _env->notice_inlined_method(method); duke@435: } duke@435: duke@435: duke@435: void Compilation::bailout(const char* msg) { duke@435: assert(msg != NULL, "bailout message must exist"); duke@435: if (!bailed_out()) { duke@435: // keep first bailout message duke@435: if (PrintBailouts) tty->print_cr("compilation bailout: %s", msg); duke@435: _bailout_msg = msg; duke@435: } duke@435: } duke@435: duke@435: duke@435: void Compilation::print_timers() { duke@435: // tty->print_cr(" Native methods : %6.3f s, Average : %2.3f", CompileBroker::_t_native_compilation.seconds(), CompileBroker::_t_native_compilation.seconds() / CompileBroker::_total_native_compile_count); duke@435: float total = timers[_t_setup].seconds() + timers[_t_buildIR].seconds() + timers[_t_emit_lir].seconds() + timers[_t_lir_schedule].seconds() + timers[_t_codeemit].seconds() + timers[_t_codeinstall].seconds(); duke@435: duke@435: duke@435: tty->print_cr(" Detailed C1 Timings"); duke@435: tty->print_cr(" Setup time: %6.3f s (%4.1f%%)", timers[_t_setup].seconds(), (timers[_t_setup].seconds() / total) * 100.0); duke@435: tty->print_cr(" Build IR: %6.3f s (%4.1f%%)", timers[_t_buildIR].seconds(), (timers[_t_buildIR].seconds() / total) * 100.0); duke@435: tty->print_cr(" Optimize: %6.3f s (%4.1f%%)", timers[_t_optimizeIR].seconds(), (timers[_t_optimizeIR].seconds() / total) * 100.0); duke@435: tty->print_cr(" Emit LIR: %6.3f s (%4.1f%%)", timers[_t_emit_lir].seconds(), (timers[_t_emit_lir].seconds() / total) * 100.0); duke@435: tty->print_cr(" LIR Gen: %6.3f s (%4.1f%%)", timers[_t_lirGeneration].seconds(), (timers[_t_lirGeneration].seconds() / total) * 100.0); duke@435: tty->print_cr(" Linear Scan: %6.3f s (%4.1f%%)", timers[_t_linearScan].seconds(), (timers[_t_linearScan].seconds() / total) * 100.0); duke@435: NOT_PRODUCT(LinearScan::print_timers(timers[_t_linearScan].seconds())); duke@435: tty->print_cr(" LIR Schedule: %6.3f s (%4.1f%%)", timers[_t_lir_schedule].seconds(), (timers[_t_lir_schedule].seconds() / total) * 100.0); duke@435: tty->print_cr(" Code Emission: %6.3f s (%4.1f%%)", timers[_t_codeemit].seconds(), (timers[_t_codeemit].seconds() / total) * 100.0); duke@435: tty->print_cr(" Code Installation: %6.3f s (%4.1f%%)", timers[_t_codeinstall].seconds(), (timers[_t_codeinstall].seconds() / total) * 100.0); duke@435: tty->print_cr(" Instruction Nodes: %6d nodes", totalInstructionNodes); duke@435: duke@435: NOT_PRODUCT(LinearScan::print_statistics()); duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: void Compilation::compile_only_this_method() { duke@435: ResourceMark rm; duke@435: fileStream stream(fopen("c1_compile_only", "wt")); duke@435: stream.print_cr("# c1 compile only directives"); duke@435: compile_only_this_scope(&stream, hir()->top_scope()); duke@435: } duke@435: duke@435: duke@435: void Compilation::compile_only_this_scope(outputStream* st, IRScope* scope) { duke@435: st->print("CompileOnly="); duke@435: scope->method()->holder()->name()->print_symbol_on(st); duke@435: st->print("."); duke@435: scope->method()->name()->print_symbol_on(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: duke@435: void Compilation::exclude_this_method() { duke@435: fileStream stream(fopen(".hotspot_compiler", "at")); duke@435: stream.print("exclude "); duke@435: method()->holder()->name()->print_symbol_on(&stream); duke@435: stream.print(" "); duke@435: method()->name()->print_symbol_on(&stream); duke@435: stream.cr(); duke@435: stream.cr(); duke@435: } duke@435: #endif