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