src/share/vm/c1/c1_Compilation.cpp

Fri, 04 Jun 2010 11:18:04 -0700

author
iveresov
date
Fri, 04 Jun 2010 11:18:04 -0700
changeset 1939
b812ff5abc73
parent 1907
c18cbe5936b8
child 1972
9887b5e57f9e
permissions
-rw-r--r--

6958292: C1: Enable parallel compilation
Summary: Enable parallel compilation in C1
Reviewed-by: never, kvn

     1 /*
     2  * Copyright (c) 1999, 2010, 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  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_c1_Compilation.cpp.incl"
    29 typedef enum {
    30   _t_compile,
    31   _t_setup,
    32   _t_optimizeIR,
    33   _t_buildIR,
    34   _t_emit_lir,
    35   _t_linearScan,
    36   _t_lirGeneration,
    37   _t_lir_schedule,
    38   _t_codeemit,
    39   _t_codeinstall,
    40   max_phase_timers
    41 } TimerName;
    43 static const char * timer_name[] = {
    44   "compile",
    45   "setup",
    46   "optimizeIR",
    47   "buildIR",
    48   "emit_lir",
    49   "linearScan",
    50   "lirGeneration",
    51   "lir_schedule",
    52   "codeemit",
    53   "codeinstall"
    54 };
    56 static elapsedTimer timers[max_phase_timers];
    57 static int totalInstructionNodes = 0;
    59 class PhaseTraceTime: public TraceTime {
    60  private:
    61   JavaThread* _thread;
    63  public:
    64   PhaseTraceTime(TimerName timer):
    65     TraceTime("", &timers[timer], CITime || CITimeEach, Verbose) {
    66   }
    67 };
    69 // Implementation of Compilation
    72 #ifndef PRODUCT
    74 void Compilation::maybe_print_current_instruction() {
    75   if (_current_instruction != NULL && _last_instruction_printed != _current_instruction) {
    76     _last_instruction_printed = _current_instruction;
    77     _current_instruction->print_line();
    78   }
    79 }
    80 #endif // PRODUCT
    83 DebugInformationRecorder* Compilation::debug_info_recorder() const {
    84   return _env->debug_info();
    85 }
    88 Dependencies* Compilation::dependency_recorder() const {
    89   return _env->dependencies();
    90 }
    93 void Compilation::initialize() {
    94   // Use an oop recorder bound to the CI environment.
    95   // (The default oop recorder is ignorant of the CI.)
    96   OopRecorder* ooprec = new OopRecorder(_env->arena());
    97   _env->set_oop_recorder(ooprec);
    98   _env->set_debug_info(new DebugInformationRecorder(ooprec));
    99   debug_info_recorder()->set_oopmaps(new OopMapSet());
   100   _env->set_dependencies(new Dependencies(_env));
   101 }
   104 void Compilation::build_hir() {
   105   CHECK_BAILOUT();
   107   // setup ir
   108   _hir = new IR(this, method(), osr_bci());
   109   if (!_hir->is_valid()) {
   110     bailout("invalid parsing");
   111     return;
   112   }
   114 #ifndef PRODUCT
   115   if (PrintCFGToFile) {
   116     CFGPrinter::print_cfg(_hir, "After Generation of HIR", true, false);
   117   }
   118 #endif
   120 #ifndef PRODUCT
   121   if (PrintCFG || PrintCFG0) { tty->print_cr("CFG after parsing"); _hir->print(true); }
   122   if (PrintIR  || PrintIR0 ) { tty->print_cr("IR after parsing"); _hir->print(false); }
   123 #endif
   125   _hir->verify();
   127   if (UseC1Optimizations) {
   128     NEEDS_CLEANUP
   129     // optimization
   130     PhaseTraceTime timeit(_t_optimizeIR);
   132     _hir->optimize();
   133   }
   135   _hir->verify();
   137   _hir->split_critical_edges();
   139 #ifndef PRODUCT
   140   if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after optimizations"); _hir->print(true); }
   141   if (PrintIR  || PrintIR1 ) { tty->print_cr("IR after optimizations"); _hir->print(false); }
   142 #endif
   144   _hir->verify();
   146   // compute block ordering for code generation
   147   // the control flow must not be changed from here on
   148   _hir->compute_code();
   150   if (UseGlobalValueNumbering) {
   151     ResourceMark rm;
   152     int instructions = Instruction::number_of_instructions();
   153     GlobalValueNumbering gvn(_hir);
   154     assert(instructions == Instruction::number_of_instructions(),
   155            "shouldn't have created an instructions");
   156   }
   158   // compute use counts after global value numbering
   159   _hir->compute_use_counts();
   161 #ifndef PRODUCT
   162   if (PrintCFG || PrintCFG2) { tty->print_cr("CFG before code generation"); _hir->code()->print(true); }
   163   if (PrintIR  || PrintIR2 ) { tty->print_cr("IR before code generation"); _hir->code()->print(false, true); }
   164 #endif
   166   _hir->verify();
   167 }
   170 void Compilation::emit_lir() {
   171   CHECK_BAILOUT();
   173   LIRGenerator gen(this, method());
   174   {
   175     PhaseTraceTime timeit(_t_lirGeneration);
   176     hir()->iterate_linear_scan_order(&gen);
   177   }
   179   CHECK_BAILOUT();
   181   {
   182     PhaseTraceTime timeit(_t_linearScan);
   184     LinearScan* allocator = new LinearScan(hir(), &gen, frame_map());
   185     set_allocator(allocator);
   186     // Assign physical registers to LIR operands using a linear scan algorithm.
   187     allocator->do_linear_scan();
   188     CHECK_BAILOUT();
   190     _max_spills = allocator->max_spills();
   191   }
   193   if (BailoutAfterLIR) {
   194     if (PrintLIR && !bailed_out()) {
   195       print_LIR(hir()->code());
   196     }
   197     bailout("Bailing out because of -XX:+BailoutAfterLIR");
   198   }
   199 }
   202 void Compilation::emit_code_epilog(LIR_Assembler* assembler) {
   203   CHECK_BAILOUT();
   205   CodeOffsets* code_offsets = assembler->offsets();
   207   // generate code or slow cases
   208   assembler->emit_slow_case_stubs();
   209   CHECK_BAILOUT();
   211   // generate exception adapters
   212   assembler->emit_exception_entries(exception_info_list());
   213   CHECK_BAILOUT();
   215   // Generate code for exception handler.
   216   code_offsets->set_value(CodeOffsets::Exceptions, assembler->emit_exception_handler());
   217   CHECK_BAILOUT();
   219   // Generate code for deopt handler.
   220   code_offsets->set_value(CodeOffsets::Deopt, assembler->emit_deopt_handler());
   221   CHECK_BAILOUT();
   223   // Generate code for MethodHandle deopt handler.  We can use the
   224   // same code as for the normal deopt handler, we just need a
   225   // different entry point address.
   226   code_offsets->set_value(CodeOffsets::DeoptMH, assembler->emit_deopt_handler());
   227   CHECK_BAILOUT();
   229   // Emit the handler to remove the activation from the stack and
   230   // dispatch to the caller.
   231   offsets()->set_value(CodeOffsets::UnwindHandler, assembler->emit_unwind_handler());
   233   // done
   234   masm()->flush();
   235 }
   238 void Compilation::setup_code_buffer(CodeBuffer* code, int call_stub_estimate) {
   239   // Preinitialize the consts section to some large size:
   240   int locs_buffer_size = 20 * (relocInfo::length_limit + sizeof(relocInfo));
   241   char* locs_buffer = NEW_RESOURCE_ARRAY(char, locs_buffer_size);
   242   code->insts()->initialize_shared_locs((relocInfo*)locs_buffer,
   243                                         locs_buffer_size / sizeof(relocInfo));
   244   code->initialize_consts_size(Compilation::desired_max_constant_size());
   245   // Call stubs + deopt/exception handler
   246   code->initialize_stubs_size((call_stub_estimate * LIR_Assembler::call_stub_size) +
   247                               LIR_Assembler::exception_handler_size +
   248                               LIR_Assembler::deopt_handler_size);
   249 }
   252 int Compilation::emit_code_body() {
   253   // emit code
   254   setup_code_buffer(code(), allocator()->num_calls());
   255   code()->initialize_oop_recorder(env()->oop_recorder());
   257   _masm = new C1_MacroAssembler(code());
   258   _masm->set_oop_recorder(env()->oop_recorder());
   260   LIR_Assembler lir_asm(this);
   262   lir_asm.emit_code(hir()->code());
   263   CHECK_BAILOUT_(0);
   265   emit_code_epilog(&lir_asm);
   266   CHECK_BAILOUT_(0);
   268   generate_exception_handler_table();
   270 #ifndef PRODUCT
   271   if (PrintExceptionHandlers && Verbose) {
   272     exception_handler_table()->print();
   273   }
   274 #endif /* PRODUCT */
   276   return frame_map()->framesize();
   277 }
   280 int Compilation::compile_java_method() {
   281   assert(!method()->is_native(), "should not reach here");
   283   if (BailoutOnExceptionHandlers) {
   284     if (method()->has_exception_handlers()) {
   285       bailout("linear scan can't handle exception handlers");
   286     }
   287   }
   289   CHECK_BAILOUT_(no_frame_size);
   291   {
   292     PhaseTraceTime timeit(_t_buildIR);
   293   build_hir();
   294   }
   295   if (BailoutAfterHIR) {
   296     BAILOUT_("Bailing out because of -XX:+BailoutAfterHIR", no_frame_size);
   297   }
   300   {
   301     PhaseTraceTime timeit(_t_emit_lir);
   303     _frame_map = new FrameMap(method(), hir()->number_of_locks(), MAX2(4, hir()->max_stack()));
   304     emit_lir();
   305   }
   306   CHECK_BAILOUT_(no_frame_size);
   308   {
   309     PhaseTraceTime timeit(_t_codeemit);
   310     return emit_code_body();
   311   }
   312 }
   314 void Compilation::install_code(int frame_size) {
   315   // frame_size is in 32-bit words so adjust it intptr_t words
   316   assert(frame_size == frame_map()->framesize(), "must match");
   317   assert(in_bytes(frame_map()->framesize_in_bytes()) % sizeof(intptr_t) == 0, "must be at least pointer aligned");
   318   _env->register_method(
   319     method(),
   320     osr_bci(),
   321     &_offsets,
   322     in_bytes(_frame_map->sp_offset_for_orig_pc()),
   323     code(),
   324     in_bytes(frame_map()->framesize_in_bytes()) / sizeof(intptr_t),
   325     debug_info_recorder()->_oopmaps,
   326     exception_handler_table(),
   327     implicit_exception_table(),
   328     compiler(),
   329     _env->comp_level(),
   330     true,
   331     has_unsafe_access()
   332   );
   333 }
   336 void Compilation::compile_method() {
   337   // setup compilation
   338   initialize();
   340   if (!method()->can_be_compiled()) {
   341     // Prevent race condition 6328518.
   342     // This can happen if the method is obsolete or breakpointed.
   343     bailout("Bailing out because method is not compilable");
   344     return;
   345   }
   347   if (_env->jvmti_can_hotswap_or_post_breakpoint()) {
   348     // We can assert evol_method because method->can_be_compiled is true.
   349     dependency_recorder()->assert_evol_method(method());
   350   }
   352   if (method()->break_at_execute()) {
   353     BREAKPOINT;
   354   }
   356 #ifndef PRODUCT
   357   if (PrintCFGToFile) {
   358     CFGPrinter::print_compilation(this);
   359   }
   360 #endif
   362   // compile method
   363   int frame_size = compile_java_method();
   365   // bailout if method couldn't be compiled
   366   // Note: make sure we mark the method as not compilable!
   367   CHECK_BAILOUT();
   369   if (InstallMethods) {
   370     // install code
   371     PhaseTraceTime timeit(_t_codeinstall);
   372     install_code(frame_size);
   373   }
   374   totalInstructionNodes += Instruction::number_of_instructions();
   375 }
   378 void Compilation::generate_exception_handler_table() {
   379   // Generate an ExceptionHandlerTable from the exception handler
   380   // information accumulated during the compilation.
   381   ExceptionInfoList* info_list = exception_info_list();
   383   if (info_list->length() == 0) {
   384     return;
   385   }
   387   // allocate some arrays for use by the collection code.
   388   const int num_handlers = 5;
   389   GrowableArray<intptr_t>* bcis = new GrowableArray<intptr_t>(num_handlers);
   390   GrowableArray<intptr_t>* scope_depths = new GrowableArray<intptr_t>(num_handlers);
   391   GrowableArray<intptr_t>* pcos = new GrowableArray<intptr_t>(num_handlers);
   393   for (int i = 0; i < info_list->length(); i++) {
   394     ExceptionInfo* info = info_list->at(i);
   395     XHandlers* handlers = info->exception_handlers();
   397     // empty the arrays
   398     bcis->trunc_to(0);
   399     scope_depths->trunc_to(0);
   400     pcos->trunc_to(0);
   402     for (int i = 0; i < handlers->length(); i++) {
   403       XHandler* handler = handlers->handler_at(i);
   404       assert(handler->entry_pco() != -1, "must have been generated");
   406       int e = bcis->find(handler->handler_bci());
   407       if (e >= 0 && scope_depths->at(e) == handler->scope_count()) {
   408         // two different handlers are declared to dispatch to the same
   409         // catch bci.  During parsing we created edges for each
   410         // handler but we really only need one.  The exception handler
   411         // table will also get unhappy if we try to declare both since
   412         // it's nonsensical.  Just skip this handler.
   413         continue;
   414       }
   416       bcis->append(handler->handler_bci());
   417       if (handler->handler_bci() == -1) {
   418         // insert a wildcard handler at scope depth 0 so that the
   419         // exception lookup logic with find it.
   420         scope_depths->append(0);
   421       } else {
   422         scope_depths->append(handler->scope_count());
   423     }
   424       pcos->append(handler->entry_pco());
   426       // stop processing once we hit a catch any
   427       if (handler->is_catch_all()) {
   428         assert(i == handlers->length() - 1, "catch all must be last handler");
   429   }
   430     }
   431     exception_handler_table()->add_subtable(info->pco(), bcis, scope_depths, pcos);
   432   }
   433 }
   436 Compilation::Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method,
   437                          int osr_bci, BufferBlob* buffer_blob)
   438 : _compiler(compiler)
   439 , _env(env)
   440 , _method(method)
   441 , _osr_bci(osr_bci)
   442 , _hir(NULL)
   443 , _max_spills(-1)
   444 , _frame_map(NULL)
   445 , _masm(NULL)
   446 , _has_exception_handlers(false)
   447 , _has_fpu_code(true)   // pessimistic assumption
   448 , _has_unsafe_access(false)
   449 , _bailout_msg(NULL)
   450 , _exception_info_list(NULL)
   451 , _allocator(NULL)
   452 , _next_id(0)
   453 , _next_block_id(0)
   454 , _code(buffer_blob->instructions_begin(),
   455         buffer_blob->instructions_size())
   456 , _current_instruction(NULL)
   457 #ifndef PRODUCT
   458 , _last_instruction_printed(NULL)
   459 #endif // PRODUCT
   460 {
   461   PhaseTraceTime timeit(_t_compile);
   463   _arena = Thread::current()->resource_area();
   464   _env->set_compiler_data(this);
   465   _exception_info_list = new ExceptionInfoList();
   466   _implicit_exception_table.set_size(0);
   467   compile_method();
   468 }
   470 Compilation::~Compilation() {
   471   _env->set_compiler_data(NULL);
   472 }
   475 void Compilation::add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers) {
   476 #ifndef PRODUCT
   477   if (PrintExceptionHandlers && Verbose) {
   478     tty->print_cr("  added exception scope for pco %d", pco);
   479   }
   480 #endif
   481   // Note: we do not have program counters for these exception handlers yet
   482   exception_info_list()->push(new ExceptionInfo(pco, exception_handlers));
   483 }
   486 void Compilation::notice_inlined_method(ciMethod* method) {
   487   _env->notice_inlined_method(method);
   488 }
   491 void Compilation::bailout(const char* msg) {
   492   assert(msg != NULL, "bailout message must exist");
   493   if (!bailed_out()) {
   494     // keep first bailout message
   495     if (PrintBailouts) tty->print_cr("compilation bailout: %s", msg);
   496     _bailout_msg = msg;
   497   }
   498 }
   501 void Compilation::print_timers() {
   502   // 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);
   503   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();
   506   tty->print_cr("    Detailed C1 Timings");
   507   tty->print_cr("       Setup time:        %6.3f s (%4.1f%%)",    timers[_t_setup].seconds(),           (timers[_t_setup].seconds() / total) * 100.0);
   508   tty->print_cr("       Build IR:          %6.3f s (%4.1f%%)",    timers[_t_buildIR].seconds(),         (timers[_t_buildIR].seconds() / total) * 100.0);
   509   tty->print_cr("         Optimize:           %6.3f s (%4.1f%%)", timers[_t_optimizeIR].seconds(),      (timers[_t_optimizeIR].seconds() / total) * 100.0);
   510   tty->print_cr("       Emit LIR:          %6.3f s (%4.1f%%)",    timers[_t_emit_lir].seconds(),        (timers[_t_emit_lir].seconds() / total) * 100.0);
   511   tty->print_cr("         LIR Gen:          %6.3f s (%4.1f%%)",   timers[_t_lirGeneration].seconds(), (timers[_t_lirGeneration].seconds() / total) * 100.0);
   512   tty->print_cr("         Linear Scan:      %6.3f s (%4.1f%%)",   timers[_t_linearScan].seconds(),    (timers[_t_linearScan].seconds() / total) * 100.0);
   513   NOT_PRODUCT(LinearScan::print_timers(timers[_t_linearScan].seconds()));
   514   tty->print_cr("       LIR Schedule:      %6.3f s (%4.1f%%)",    timers[_t_lir_schedule].seconds(),  (timers[_t_lir_schedule].seconds() / total) * 100.0);
   515   tty->print_cr("       Code Emission:     %6.3f s (%4.1f%%)",    timers[_t_codeemit].seconds(),        (timers[_t_codeemit].seconds() / total) * 100.0);
   516   tty->print_cr("       Code Installation: %6.3f s (%4.1f%%)",    timers[_t_codeinstall].seconds(),     (timers[_t_codeinstall].seconds() / total) * 100.0);
   517   tty->print_cr("       Instruction Nodes: %6d nodes",    totalInstructionNodes);
   519   NOT_PRODUCT(LinearScan::print_statistics());
   520 }
   523 #ifndef PRODUCT
   524 void Compilation::compile_only_this_method() {
   525   ResourceMark rm;
   526   fileStream stream(fopen("c1_compile_only", "wt"));
   527   stream.print_cr("# c1 compile only directives");
   528   compile_only_this_scope(&stream, hir()->top_scope());
   529 }
   532 void Compilation::compile_only_this_scope(outputStream* st, IRScope* scope) {
   533   st->print("CompileOnly=");
   534   scope->method()->holder()->name()->print_symbol_on(st);
   535   st->print(".");
   536   scope->method()->name()->print_symbol_on(st);
   537   st->cr();
   538 }
   541 void Compilation::exclude_this_method() {
   542   fileStream stream(fopen(".hotspot_compiler", "at"));
   543   stream.print("exclude ");
   544   method()->holder()->name()->print_symbol_on(&stream);
   545   stream.print(" ");
   546   method()->name()->print_symbol_on(&stream);
   547   stream.cr();
   548   stream.cr();
   549 }
   550 #endif

mercurial