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 */ 24 25 #include "incls/_precompiled.incl" 26 #include "incls/_c1_Compilation.cpp.incl" 27 28 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; 42 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 }; 55 56 static elapsedTimer timers[max_phase_timers]; 57 static int totalInstructionNodes = 0; 58 59 class PhaseTraceTime: public TraceTime { 60 private: 61 JavaThread* _thread; 62 63 public: 64 PhaseTraceTime(TimerName timer): 65 TraceTime("", &timers[timer], CITime || CITimeEach, Verbose) { 66 } 67 }; 68 69 // Implementation of Compilation 70 71 72 #ifndef PRODUCT 73 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 81 82 83 DebugInformationRecorder* Compilation::debug_info_recorder() const { 84 return _env->debug_info(); 85 } 86 87 88 Dependencies* Compilation::dependency_recorder() const { 89 return _env->dependencies(); 90 } 91 92 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 } 102 103 104 void Compilation::build_hir() { 105 CHECK_BAILOUT(); 106 107 // setup ir 108 _hir = new IR(this, method(), osr_bci()); 109 if (!_hir->is_valid()) { 110 bailout("invalid parsing"); 111 return; 112 } 113 114 #ifndef PRODUCT 115 if (PrintCFGToFile) { 116 CFGPrinter::print_cfg(_hir, "After Generation of HIR", true, false); 117 } 118 #endif 119 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 124 125 _hir->verify(); 126 127 if (UseC1Optimizations) { 128 NEEDS_CLEANUP 129 // optimization 130 PhaseTraceTime timeit(_t_optimizeIR); 131 132 _hir->optimize(); 133 } 134 135 _hir->verify(); 136 137 _hir->split_critical_edges(); 138 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 143 144 _hir->verify(); 145 146 // compute block ordering for code generation 147 // the control flow must not be changed from here on 148 _hir->compute_code(); 149 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 } 157 158 // compute use counts after global value numbering 159 _hir->compute_use_counts(); 160 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 165 166 _hir->verify(); 167 } 168 169 170 void Compilation::emit_lir() { 171 CHECK_BAILOUT(); 172 173 LIRGenerator gen(this, method()); 174 { 175 PhaseTraceTime timeit(_t_lirGeneration); 176 hir()->iterate_linear_scan_order(&gen); 177 } 178 179 CHECK_BAILOUT(); 180 181 { 182 PhaseTraceTime timeit(_t_linearScan); 183 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(); 189 190 _max_spills = allocator->max_spills(); 191 } 192 193 if (BailoutAfterLIR) { 194 if (PrintLIR && !bailed_out()) { 195 print_LIR(hir()->code()); 196 } 197 bailout("Bailing out because of -XX:+BailoutAfterLIR"); 198 } 199 } 200 201 202 void Compilation::emit_code_epilog(LIR_Assembler* assembler) { 203 CHECK_BAILOUT(); 204 205 CodeOffsets* code_offsets = assembler->offsets(); 206 207 // generate code or slow cases 208 assembler->emit_slow_case_stubs(); 209 CHECK_BAILOUT(); 210 211 // generate exception adapters 212 assembler->emit_exception_entries(exception_info_list()); 213 CHECK_BAILOUT(); 214 215 // Generate code for exception handler. 216 code_offsets->set_value(CodeOffsets::Exceptions, assembler->emit_exception_handler()); 217 CHECK_BAILOUT(); 218 219 // Generate code for deopt handler. 220 code_offsets->set_value(CodeOffsets::Deopt, assembler->emit_deopt_handler()); 221 CHECK_BAILOUT(); 222 223 // Emit the MethodHandle deopt handler code (if required). 224 if (has_method_handle_invokes()) { 225 // We can use the same code as for the normal deopt handler, we 226 // just need a different entry point address. 227 code_offsets->set_value(CodeOffsets::DeoptMH, assembler->emit_deopt_handler()); 228 CHECK_BAILOUT(); 229 } 230 231 // Emit the handler to remove the activation from the stack and 232 // dispatch to the caller. 233 offsets()->set_value(CodeOffsets::UnwindHandler, assembler->emit_unwind_handler()); 234 235 // done 236 masm()->flush(); 237 } 238 239 240 void Compilation::setup_code_buffer(CodeBuffer* code, int call_stub_estimate) { 241 // Preinitialize the consts section to some large size: 242 int locs_buffer_size = 20 * (relocInfo::length_limit + sizeof(relocInfo)); 243 char* locs_buffer = NEW_RESOURCE_ARRAY(char, locs_buffer_size); 244 code->insts()->initialize_shared_locs((relocInfo*)locs_buffer, 245 locs_buffer_size / sizeof(relocInfo)); 246 code->initialize_consts_size(Compilation::desired_max_constant_size()); 247 // Call stubs + two deopt handlers (regular and MH) + exception handler 248 code->initialize_stubs_size((call_stub_estimate * LIR_Assembler::call_stub_size) + 249 LIR_Assembler::exception_handler_size + 250 2 * LIR_Assembler::deopt_handler_size); 251 } 252 253 254 int Compilation::emit_code_body() { 255 // emit code 256 setup_code_buffer(code(), allocator()->num_calls()); 257 code()->initialize_oop_recorder(env()->oop_recorder()); 258 259 _masm = new C1_MacroAssembler(code()); 260 _masm->set_oop_recorder(env()->oop_recorder()); 261 262 LIR_Assembler lir_asm(this); 263 264 lir_asm.emit_code(hir()->code()); 265 CHECK_BAILOUT_(0); 266 267 emit_code_epilog(&lir_asm); 268 CHECK_BAILOUT_(0); 269 270 generate_exception_handler_table(); 271 272 #ifndef PRODUCT 273 if (PrintExceptionHandlers && Verbose) { 274 exception_handler_table()->print(); 275 } 276 #endif /* PRODUCT */ 277 278 return frame_map()->framesize(); 279 } 280 281 282 int Compilation::compile_java_method() { 283 assert(!method()->is_native(), "should not reach here"); 284 285 if (BailoutOnExceptionHandlers) { 286 if (method()->has_exception_handlers()) { 287 bailout("linear scan can't handle exception handlers"); 288 } 289 } 290 291 CHECK_BAILOUT_(no_frame_size); 292 293 if (is_profiling()) { 294 method()->build_method_data(); 295 } 296 297 { 298 PhaseTraceTime timeit(_t_buildIR); 299 build_hir(); 300 } 301 if (BailoutAfterHIR) { 302 BAILOUT_("Bailing out because of -XX:+BailoutAfterHIR", no_frame_size); 303 } 304 305 306 { 307 PhaseTraceTime timeit(_t_emit_lir); 308 309 _frame_map = new FrameMap(method(), hir()->number_of_locks(), MAX2(4, hir()->max_stack())); 310 emit_lir(); 311 } 312 CHECK_BAILOUT_(no_frame_size); 313 314 { 315 PhaseTraceTime timeit(_t_codeemit); 316 return emit_code_body(); 317 } 318 } 319 320 void Compilation::install_code(int frame_size) { 321 // frame_size is in 32-bit words so adjust it intptr_t words 322 assert(frame_size == frame_map()->framesize(), "must match"); 323 assert(in_bytes(frame_map()->framesize_in_bytes()) % sizeof(intptr_t) == 0, "must be at least pointer aligned"); 324 _env->register_method( 325 method(), 326 osr_bci(), 327 &_offsets, 328 in_bytes(_frame_map->sp_offset_for_orig_pc()), 329 code(), 330 in_bytes(frame_map()->framesize_in_bytes()) / sizeof(intptr_t), 331 debug_info_recorder()->_oopmaps, 332 exception_handler_table(), 333 implicit_exception_table(), 334 compiler(), 335 _env->comp_level(), 336 true, 337 has_unsafe_access() 338 ); 339 } 340 341 342 void Compilation::compile_method() { 343 // setup compilation 344 initialize(); 345 346 if (!method()->can_be_compiled()) { 347 // Prevent race condition 6328518. 348 // This can happen if the method is obsolete or breakpointed. 349 bailout("Bailing out because method is not compilable"); 350 return; 351 } 352 353 if (_env->jvmti_can_hotswap_or_post_breakpoint()) { 354 // We can assert evol_method because method->can_be_compiled is true. 355 dependency_recorder()->assert_evol_method(method()); 356 } 357 358 if (method()->break_at_execute()) { 359 BREAKPOINT; 360 } 361 362 #ifndef PRODUCT 363 if (PrintCFGToFile) { 364 CFGPrinter::print_compilation(this); 365 } 366 #endif 367 368 // compile method 369 int frame_size = compile_java_method(); 370 371 // bailout if method couldn't be compiled 372 // Note: make sure we mark the method as not compilable! 373 CHECK_BAILOUT(); 374 375 if (InstallMethods) { 376 // install code 377 PhaseTraceTime timeit(_t_codeinstall); 378 install_code(frame_size); 379 } 380 totalInstructionNodes += Instruction::number_of_instructions(); 381 } 382 383 384 void Compilation::generate_exception_handler_table() { 385 // Generate an ExceptionHandlerTable from the exception handler 386 // information accumulated during the compilation. 387 ExceptionInfoList* info_list = exception_info_list(); 388 389 if (info_list->length() == 0) { 390 return; 391 } 392 393 // allocate some arrays for use by the collection code. 394 const int num_handlers = 5; 395 GrowableArray<intptr_t>* bcis = new GrowableArray<intptr_t>(num_handlers); 396 GrowableArray<intptr_t>* scope_depths = new GrowableArray<intptr_t>(num_handlers); 397 GrowableArray<intptr_t>* pcos = new GrowableArray<intptr_t>(num_handlers); 398 399 for (int i = 0; i < info_list->length(); i++) { 400 ExceptionInfo* info = info_list->at(i); 401 XHandlers* handlers = info->exception_handlers(); 402 403 // empty the arrays 404 bcis->trunc_to(0); 405 scope_depths->trunc_to(0); 406 pcos->trunc_to(0); 407 408 for (int i = 0; i < handlers->length(); i++) { 409 XHandler* handler = handlers->handler_at(i); 410 assert(handler->entry_pco() != -1, "must have been generated"); 411 412 int e = bcis->find(handler->handler_bci()); 413 if (e >= 0 && scope_depths->at(e) == handler->scope_count()) { 414 // two different handlers are declared to dispatch to the same 415 // catch bci. During parsing we created edges for each 416 // handler but we really only need one. The exception handler 417 // table will also get unhappy if we try to declare both since 418 // it's nonsensical. Just skip this handler. 419 continue; 420 } 421 422 bcis->append(handler->handler_bci()); 423 if (handler->handler_bci() == -1) { 424 // insert a wildcard handler at scope depth 0 so that the 425 // exception lookup logic with find it. 426 scope_depths->append(0); 427 } else { 428 scope_depths->append(handler->scope_count()); 429 } 430 pcos->append(handler->entry_pco()); 431 432 // stop processing once we hit a catch any 433 if (handler->is_catch_all()) { 434 assert(i == handlers->length() - 1, "catch all must be last handler"); 435 } 436 } 437 exception_handler_table()->add_subtable(info->pco(), bcis, scope_depths, pcos); 438 } 439 } 440 441 442 Compilation::Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method, 443 int osr_bci, BufferBlob* buffer_blob) 444 : _compiler(compiler) 445 , _env(env) 446 , _method(method) 447 , _osr_bci(osr_bci) 448 , _hir(NULL) 449 , _max_spills(-1) 450 , _frame_map(NULL) 451 , _masm(NULL) 452 , _has_exception_handlers(false) 453 , _has_fpu_code(true) // pessimistic assumption 454 , _would_profile(false) 455 , _has_unsafe_access(false) 456 , _has_method_handle_invokes(false) 457 , _bailout_msg(NULL) 458 , _exception_info_list(NULL) 459 , _allocator(NULL) 460 , _next_id(0) 461 , _next_block_id(0) 462 , _code(buffer_blob) 463 , _current_instruction(NULL) 464 #ifndef PRODUCT 465 , _last_instruction_printed(NULL) 466 #endif // PRODUCT 467 { 468 PhaseTraceTime timeit(_t_compile); 469 _arena = Thread::current()->resource_area(); 470 _env->set_compiler_data(this); 471 _exception_info_list = new ExceptionInfoList(); 472 _implicit_exception_table.set_size(0); 473 compile_method(); 474 if (is_profiling() && _would_profile) { 475 ciMethodData *md = method->method_data(); 476 assert (md != NULL, "Should have MDO"); 477 md->set_would_profile(_would_profile); 478 } 479 } 480 481 Compilation::~Compilation() { 482 _env->set_compiler_data(NULL); 483 } 484 485 486 void Compilation::add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers) { 487 #ifndef PRODUCT 488 if (PrintExceptionHandlers && Verbose) { 489 tty->print_cr(" added exception scope for pco %d", pco); 490 } 491 #endif 492 // Note: we do not have program counters for these exception handlers yet 493 exception_info_list()->push(new ExceptionInfo(pco, exception_handlers)); 494 } 495 496 497 void Compilation::notice_inlined_method(ciMethod* method) { 498 _env->notice_inlined_method(method); 499 } 500 501 502 void Compilation::bailout(const char* msg) { 503 assert(msg != NULL, "bailout message must exist"); 504 if (!bailed_out()) { 505 // keep first bailout message 506 if (PrintBailouts) tty->print_cr("compilation bailout: %s", msg); 507 _bailout_msg = msg; 508 } 509 } 510 511 512 void Compilation::print_timers() { 513 // 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); 514 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(); 515 516 517 tty->print_cr(" Detailed C1 Timings"); 518 tty->print_cr(" Setup time: %6.3f s (%4.1f%%)", timers[_t_setup].seconds(), (timers[_t_setup].seconds() / total) * 100.0); 519 tty->print_cr(" Build IR: %6.3f s (%4.1f%%)", timers[_t_buildIR].seconds(), (timers[_t_buildIR].seconds() / total) * 100.0); 520 tty->print_cr(" Optimize: %6.3f s (%4.1f%%)", timers[_t_optimizeIR].seconds(), (timers[_t_optimizeIR].seconds() / total) * 100.0); 521 tty->print_cr(" Emit LIR: %6.3f s (%4.1f%%)", timers[_t_emit_lir].seconds(), (timers[_t_emit_lir].seconds() / total) * 100.0); 522 tty->print_cr(" LIR Gen: %6.3f s (%4.1f%%)", timers[_t_lirGeneration].seconds(), (timers[_t_lirGeneration].seconds() / total) * 100.0); 523 tty->print_cr(" Linear Scan: %6.3f s (%4.1f%%)", timers[_t_linearScan].seconds(), (timers[_t_linearScan].seconds() / total) * 100.0); 524 NOT_PRODUCT(LinearScan::print_timers(timers[_t_linearScan].seconds())); 525 tty->print_cr(" LIR Schedule: %6.3f s (%4.1f%%)", timers[_t_lir_schedule].seconds(), (timers[_t_lir_schedule].seconds() / total) * 100.0); 526 tty->print_cr(" Code Emission: %6.3f s (%4.1f%%)", timers[_t_codeemit].seconds(), (timers[_t_codeemit].seconds() / total) * 100.0); 527 tty->print_cr(" Code Installation: %6.3f s (%4.1f%%)", timers[_t_codeinstall].seconds(), (timers[_t_codeinstall].seconds() / total) * 100.0); 528 tty->print_cr(" Instruction Nodes: %6d nodes", totalInstructionNodes); 529 530 NOT_PRODUCT(LinearScan::print_statistics()); 531 } 532 533 534 #ifndef PRODUCT 535 void Compilation::compile_only_this_method() { 536 ResourceMark rm; 537 fileStream stream(fopen("c1_compile_only", "wt")); 538 stream.print_cr("# c1 compile only directives"); 539 compile_only_this_scope(&stream, hir()->top_scope()); 540 } 541 542 543 void Compilation::compile_only_this_scope(outputStream* st, IRScope* scope) { 544 st->print("CompileOnly="); 545 scope->method()->holder()->name()->print_symbol_on(st); 546 st->print("."); 547 scope->method()->name()->print_symbol_on(st); 548 st->cr(); 549 } 550 551 552 void Compilation::exclude_this_method() { 553 fileStream stream(fopen(".hotspot_compiler", "at")); 554 stream.print("exclude "); 555 method()->holder()->name()->print_symbol_on(&stream); 556 stream.print(" "); 557 method()->name()->print_symbol_on(&stream); 558 stream.cr(); 559 stream.cr(); 560 } 561 #endif