src/share/vm/c1/c1_Compilation.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
equal deleted inserted replaced
-1:000000000000 0:f90c822e73f8
1 /*
2 * Copyright (c) 1999, 2013, 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 "precompiled.hpp"
26 #include "c1/c1_CFGPrinter.hpp"
27 #include "c1/c1_Compilation.hpp"
28 #include "c1/c1_IR.hpp"
29 #include "c1/c1_LIRAssembler.hpp"
30 #include "c1/c1_LinearScan.hpp"
31 #include "c1/c1_MacroAssembler.hpp"
32 #include "c1/c1_ValueMap.hpp"
33 #include "c1/c1_ValueStack.hpp"
34 #include "code/debugInfoRec.hpp"
35 #include "compiler/compileLog.hpp"
36 #include "c1/c1_RangeCheckElimination.hpp"
37
38
39 typedef enum {
40 _t_compile,
41 _t_setup,
42 _t_buildIR,
43 _t_optimize_blocks,
44 _t_optimize_null_checks,
45 _t_rangeCheckElimination,
46 _t_emit_lir,
47 _t_linearScan,
48 _t_lirGeneration,
49 _t_lir_schedule,
50 _t_codeemit,
51 _t_codeinstall,
52 max_phase_timers
53 } TimerName;
54
55 static const char * timer_name[] = {
56 "compile",
57 "setup",
58 "buildIR",
59 "optimize_blocks",
60 "optimize_null_checks",
61 "rangeCheckElimination",
62 "emit_lir",
63 "linearScan",
64 "lirGeneration",
65 "lir_schedule",
66 "codeemit",
67 "codeinstall"
68 };
69
70 static elapsedTimer timers[max_phase_timers];
71 static int totalInstructionNodes = 0;
72
73 class PhaseTraceTime: public TraceTime {
74 private:
75 JavaThread* _thread;
76 CompileLog* _log;
77 TimerName _timer;
78
79 public:
80 PhaseTraceTime(TimerName timer)
81 : TraceTime("", &timers[timer], CITime || CITimeEach, Verbose),
82 _log(NULL), _timer(timer)
83 {
84 if (Compilation::current() != NULL) {
85 _log = Compilation::current()->log();
86 }
87
88 if (_log != NULL) {
89 _log->begin_head("phase name='%s'", timer_name[_timer]);
90 _log->stamp();
91 _log->end_head();
92 }
93 }
94
95 ~PhaseTraceTime() {
96 if (_log != NULL)
97 _log->done("phase name='%s'", timer_name[_timer]);
98 }
99 };
100
101 // Implementation of Compilation
102
103
104 #ifndef PRODUCT
105
106 void Compilation::maybe_print_current_instruction() {
107 if (_current_instruction != NULL && _last_instruction_printed != _current_instruction) {
108 _last_instruction_printed = _current_instruction;
109 _current_instruction->print_line();
110 }
111 }
112 #endif // PRODUCT
113
114
115 DebugInformationRecorder* Compilation::debug_info_recorder() const {
116 return _env->debug_info();
117 }
118
119
120 Dependencies* Compilation::dependency_recorder() const {
121 return _env->dependencies();
122 }
123
124
125 void Compilation::initialize() {
126 // Use an oop recorder bound to the CI environment.
127 // (The default oop recorder is ignorant of the CI.)
128 OopRecorder* ooprec = new OopRecorder(_env->arena());
129 _env->set_oop_recorder(ooprec);
130 _env->set_debug_info(new DebugInformationRecorder(ooprec));
131 debug_info_recorder()->set_oopmaps(new OopMapSet());
132 _env->set_dependencies(new Dependencies(_env));
133 }
134
135
136 void Compilation::build_hir() {
137 CHECK_BAILOUT();
138
139 // setup ir
140 CompileLog* log = this->log();
141 if (log != NULL) {
142 log->begin_head("parse method='%d' ",
143 log->identify(_method));
144 log->stamp();
145 log->end_head();
146 }
147 _hir = new IR(this, method(), osr_bci());
148 if (log) log->done("parse");
149 if (!_hir->is_valid()) {
150 bailout("invalid parsing");
151 return;
152 }
153
154 #ifndef PRODUCT
155 if (PrintCFGToFile) {
156 CFGPrinter::print_cfg(_hir, "After Generation of HIR", true, false);
157 }
158 #endif
159
160 #ifndef PRODUCT
161 if (PrintCFG || PrintCFG0) { tty->print_cr("CFG after parsing"); _hir->print(true); }
162 if (PrintIR || PrintIR0 ) { tty->print_cr("IR after parsing"); _hir->print(false); }
163 #endif
164
165 _hir->verify();
166
167 if (UseC1Optimizations) {
168 NEEDS_CLEANUP
169 // optimization
170 PhaseTraceTime timeit(_t_optimize_blocks);
171
172 _hir->optimize_blocks();
173 }
174
175 _hir->verify();
176
177 _hir->split_critical_edges();
178
179 #ifndef PRODUCT
180 if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after optimizations"); _hir->print(true); }
181 if (PrintIR || PrintIR1 ) { tty->print_cr("IR after optimizations"); _hir->print(false); }
182 #endif
183
184 _hir->verify();
185
186 // compute block ordering for code generation
187 // the control flow must not be changed from here on
188 _hir->compute_code();
189
190 if (UseGlobalValueNumbering) {
191 // No resource mark here! LoopInvariantCodeMotion can allocate ValueStack objects.
192 int instructions = Instruction::number_of_instructions();
193 GlobalValueNumbering gvn(_hir);
194 assert(instructions == Instruction::number_of_instructions(),
195 "shouldn't have created an instructions");
196 }
197
198 _hir->verify();
199
200 #ifndef PRODUCT
201 if (PrintCFGToFile) {
202 CFGPrinter::print_cfg(_hir, "Before RangeCheckElimination", true, false);
203 }
204 #endif
205
206 if (RangeCheckElimination) {
207 if (_hir->osr_entry() == NULL) {
208 PhaseTraceTime timeit(_t_rangeCheckElimination);
209 RangeCheckElimination::eliminate(_hir);
210 }
211 }
212
213 #ifndef PRODUCT
214 if (PrintCFGToFile) {
215 CFGPrinter::print_cfg(_hir, "After RangeCheckElimination", true, false);
216 }
217 #endif
218
219 if (UseC1Optimizations) {
220 // loop invariant code motion reorders instructions and range
221 // check elimination adds new instructions so do null check
222 // elimination after.
223 NEEDS_CLEANUP
224 // optimization
225 PhaseTraceTime timeit(_t_optimize_null_checks);
226
227 _hir->eliminate_null_checks();
228 }
229
230 _hir->verify();
231
232 // compute use counts after global value numbering
233 _hir->compute_use_counts();
234
235 #ifndef PRODUCT
236 if (PrintCFG || PrintCFG2) { tty->print_cr("CFG before code generation"); _hir->code()->print(true); }
237 if (PrintIR || PrintIR2 ) { tty->print_cr("IR before code generation"); _hir->code()->print(false, true); }
238 #endif
239
240 _hir->verify();
241 }
242
243
244 void Compilation::emit_lir() {
245 CHECK_BAILOUT();
246
247 LIRGenerator gen(this, method());
248 {
249 PhaseTraceTime timeit(_t_lirGeneration);
250 hir()->iterate_linear_scan_order(&gen);
251 }
252
253 CHECK_BAILOUT();
254
255 {
256 PhaseTraceTime timeit(_t_linearScan);
257
258 LinearScan* allocator = new LinearScan(hir(), &gen, frame_map());
259 set_allocator(allocator);
260 // Assign physical registers to LIR operands using a linear scan algorithm.
261 allocator->do_linear_scan();
262 CHECK_BAILOUT();
263
264 _max_spills = allocator->max_spills();
265 }
266
267 if (BailoutAfterLIR) {
268 if (PrintLIR && !bailed_out()) {
269 print_LIR(hir()->code());
270 }
271 bailout("Bailing out because of -XX:+BailoutAfterLIR");
272 }
273 }
274
275
276 void Compilation::emit_code_epilog(LIR_Assembler* assembler) {
277 CHECK_BAILOUT();
278
279 CodeOffsets* code_offsets = assembler->offsets();
280
281 // generate code or slow cases
282 assembler->emit_slow_case_stubs();
283 CHECK_BAILOUT();
284
285 // generate exception adapters
286 assembler->emit_exception_entries(exception_info_list());
287 CHECK_BAILOUT();
288
289 // Generate code for exception handler.
290 code_offsets->set_value(CodeOffsets::Exceptions, assembler->emit_exception_handler());
291 CHECK_BAILOUT();
292
293 // Generate code for deopt handler.
294 code_offsets->set_value(CodeOffsets::Deopt, assembler->emit_deopt_handler());
295 CHECK_BAILOUT();
296
297 // Emit the MethodHandle deopt handler code (if required).
298 if (has_method_handle_invokes()) {
299 // We can use the same code as for the normal deopt handler, we
300 // just need a different entry point address.
301 code_offsets->set_value(CodeOffsets::DeoptMH, assembler->emit_deopt_handler());
302 CHECK_BAILOUT();
303 }
304
305 // Emit the handler to remove the activation from the stack and
306 // dispatch to the caller.
307 offsets()->set_value(CodeOffsets::UnwindHandler, assembler->emit_unwind_handler());
308
309 // done
310 masm()->flush();
311 }
312
313
314 bool Compilation::setup_code_buffer(CodeBuffer* code, int call_stub_estimate) {
315 // Preinitialize the consts section to some large size:
316 int locs_buffer_size = 20 * (relocInfo::length_limit + sizeof(relocInfo));
317 char* locs_buffer = NEW_RESOURCE_ARRAY(char, locs_buffer_size);
318 code->insts()->initialize_shared_locs((relocInfo*)locs_buffer,
319 locs_buffer_size / sizeof(relocInfo));
320 code->initialize_consts_size(Compilation::desired_max_constant_size());
321 // Call stubs + two deopt handlers (regular and MH) + exception handler
322 int stub_size = (call_stub_estimate * LIR_Assembler::call_stub_size) +
323 LIR_Assembler::exception_handler_size +
324 (2 * LIR_Assembler::deopt_handler_size);
325 if (stub_size >= code->insts_capacity()) return false;
326 code->initialize_stubs_size(stub_size);
327 return true;
328 }
329
330
331 int Compilation::emit_code_body() {
332 // emit code
333 if (!setup_code_buffer(code(), allocator()->num_calls())) {
334 BAILOUT_("size requested greater than avail code buffer size", 0);
335 }
336 code()->initialize_oop_recorder(env()->oop_recorder());
337
338 _masm = new C1_MacroAssembler(code());
339 _masm->set_oop_recorder(env()->oop_recorder());
340
341 LIR_Assembler lir_asm(this);
342
343 lir_asm.emit_code(hir()->code());
344 CHECK_BAILOUT_(0);
345
346 emit_code_epilog(&lir_asm);
347 CHECK_BAILOUT_(0);
348
349 generate_exception_handler_table();
350
351 #ifndef PRODUCT
352 if (PrintExceptionHandlers && Verbose) {
353 exception_handler_table()->print();
354 }
355 #endif /* PRODUCT */
356
357 return frame_map()->framesize();
358 }
359
360
361 int Compilation::compile_java_method() {
362 assert(!method()->is_native(), "should not reach here");
363
364 if (BailoutOnExceptionHandlers) {
365 if (method()->has_exception_handlers()) {
366 bailout("linear scan can't handle exception handlers");
367 }
368 }
369
370 CHECK_BAILOUT_(no_frame_size);
371
372 if (is_profiling() && !method()->ensure_method_data()) {
373 BAILOUT_("mdo allocation failed", no_frame_size);
374 }
375
376 {
377 PhaseTraceTime timeit(_t_buildIR);
378 build_hir();
379 }
380 if (BailoutAfterHIR) {
381 BAILOUT_("Bailing out because of -XX:+BailoutAfterHIR", no_frame_size);
382 }
383
384
385 {
386 PhaseTraceTime timeit(_t_emit_lir);
387
388 _frame_map = new FrameMap(method(), hir()->number_of_locks(), MAX2(4, hir()->max_stack()));
389 emit_lir();
390 }
391 CHECK_BAILOUT_(no_frame_size);
392
393 {
394 PhaseTraceTime timeit(_t_codeemit);
395 return emit_code_body();
396 }
397 }
398
399 void Compilation::install_code(int frame_size) {
400 // frame_size is in 32-bit words so adjust it intptr_t words
401 assert(frame_size == frame_map()->framesize(), "must match");
402 assert(in_bytes(frame_map()->framesize_in_bytes()) % sizeof(intptr_t) == 0, "must be at least pointer aligned");
403 _env->register_method(
404 method(),
405 osr_bci(),
406 &_offsets,
407 in_bytes(_frame_map->sp_offset_for_orig_pc()),
408 code(),
409 in_bytes(frame_map()->framesize_in_bytes()) / sizeof(intptr_t),
410 debug_info_recorder()->_oopmaps,
411 exception_handler_table(),
412 implicit_exception_table(),
413 compiler(),
414 _env->comp_level(),
415 has_unsafe_access(),
416 SharedRuntime::is_wide_vector(max_vector_size())
417 );
418 }
419
420
421 void Compilation::compile_method() {
422 // setup compilation
423 initialize();
424
425 if (!method()->can_be_compiled()) {
426 // Prevent race condition 6328518.
427 // This can happen if the method is obsolete or breakpointed.
428 bailout("Bailing out because method is not compilable");
429 return;
430 }
431
432 if (_env->jvmti_can_hotswap_or_post_breakpoint()) {
433 // We can assert evol_method because method->can_be_compiled is true.
434 dependency_recorder()->assert_evol_method(method());
435 }
436
437 if (method()->break_at_execute()) {
438 BREAKPOINT;
439 }
440
441 #ifndef PRODUCT
442 if (PrintCFGToFile) {
443 CFGPrinter::print_compilation(this);
444 }
445 #endif
446
447 // compile method
448 int frame_size = compile_java_method();
449
450 // bailout if method couldn't be compiled
451 // Note: make sure we mark the method as not compilable!
452 CHECK_BAILOUT();
453
454 if (InstallMethods) {
455 // install code
456 PhaseTraceTime timeit(_t_codeinstall);
457 install_code(frame_size);
458 }
459
460 if (log() != NULL) // Print code cache state into compiler log
461 log()->code_cache_state();
462
463 totalInstructionNodes += Instruction::number_of_instructions();
464 }
465
466
467 void Compilation::generate_exception_handler_table() {
468 // Generate an ExceptionHandlerTable from the exception handler
469 // information accumulated during the compilation.
470 ExceptionInfoList* info_list = exception_info_list();
471
472 if (info_list->length() == 0) {
473 return;
474 }
475
476 // allocate some arrays for use by the collection code.
477 const int num_handlers = 5;
478 GrowableArray<intptr_t>* bcis = new GrowableArray<intptr_t>(num_handlers);
479 GrowableArray<intptr_t>* scope_depths = new GrowableArray<intptr_t>(num_handlers);
480 GrowableArray<intptr_t>* pcos = new GrowableArray<intptr_t>(num_handlers);
481
482 for (int i = 0; i < info_list->length(); i++) {
483 ExceptionInfo* info = info_list->at(i);
484 XHandlers* handlers = info->exception_handlers();
485
486 // empty the arrays
487 bcis->trunc_to(0);
488 scope_depths->trunc_to(0);
489 pcos->trunc_to(0);
490
491 for (int i = 0; i < handlers->length(); i++) {
492 XHandler* handler = handlers->handler_at(i);
493 assert(handler->entry_pco() != -1, "must have been generated");
494
495 int e = bcis->find(handler->handler_bci());
496 if (e >= 0 && scope_depths->at(e) == handler->scope_count()) {
497 // two different handlers are declared to dispatch to the same
498 // catch bci. During parsing we created edges for each
499 // handler but we really only need one. The exception handler
500 // table will also get unhappy if we try to declare both since
501 // it's nonsensical. Just skip this handler.
502 continue;
503 }
504
505 bcis->append(handler->handler_bci());
506 if (handler->handler_bci() == -1) {
507 // insert a wildcard handler at scope depth 0 so that the
508 // exception lookup logic with find it.
509 scope_depths->append(0);
510 } else {
511 scope_depths->append(handler->scope_count());
512 }
513 pcos->append(handler->entry_pco());
514
515 // stop processing once we hit a catch any
516 if (handler->is_catch_all()) {
517 assert(i == handlers->length() - 1, "catch all must be last handler");
518 }
519 }
520 exception_handler_table()->add_subtable(info->pco(), bcis, scope_depths, pcos);
521 }
522 }
523
524
525 Compilation::Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method,
526 int osr_bci, BufferBlob* buffer_blob)
527 : _compiler(compiler)
528 , _env(env)
529 , _log(env->log())
530 , _method(method)
531 , _osr_bci(osr_bci)
532 , _hir(NULL)
533 , _max_spills(-1)
534 , _frame_map(NULL)
535 , _masm(NULL)
536 , _has_exception_handlers(false)
537 , _has_fpu_code(true) // pessimistic assumption
538 , _would_profile(false)
539 , _has_unsafe_access(false)
540 , _has_method_handle_invokes(false)
541 , _bailout_msg(NULL)
542 , _exception_info_list(NULL)
543 , _allocator(NULL)
544 , _next_id(0)
545 , _next_block_id(0)
546 , _code(buffer_blob)
547 , _has_access_indexed(false)
548 , _current_instruction(NULL)
549 , _interpreter_frame_size(0)
550 #ifndef PRODUCT
551 , _last_instruction_printed(NULL)
552 #endif // PRODUCT
553 {
554 PhaseTraceTime timeit(_t_compile);
555 _arena = Thread::current()->resource_area();
556 _env->set_compiler_data(this);
557 _exception_info_list = new ExceptionInfoList();
558 _implicit_exception_table.set_size(0);
559 compile_method();
560 if (bailed_out()) {
561 _env->record_method_not_compilable(bailout_msg(), !TieredCompilation);
562 if (is_profiling()) {
563 // Compilation failed, create MDO, which would signal the interpreter
564 // to start profiling on its own.
565 _method->ensure_method_data();
566 }
567 } else if (is_profiling()) {
568 ciMethodData *md = method->method_data_or_null();
569 if (md != NULL) {
570 md->set_would_profile(_would_profile);
571 }
572 }
573 }
574
575 Compilation::~Compilation() {
576 _env->set_compiler_data(NULL);
577 }
578
579
580 void Compilation::add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers) {
581 #ifndef PRODUCT
582 if (PrintExceptionHandlers && Verbose) {
583 tty->print_cr(" added exception scope for pco %d", pco);
584 }
585 #endif
586 // Note: we do not have program counters for these exception handlers yet
587 exception_info_list()->push(new ExceptionInfo(pco, exception_handlers));
588 }
589
590
591 void Compilation::notice_inlined_method(ciMethod* method) {
592 _env->notice_inlined_method(method);
593 }
594
595
596 void Compilation::bailout(const char* msg) {
597 assert(msg != NULL, "bailout message must exist");
598 if (!bailed_out()) {
599 // keep first bailout message
600 if (PrintCompilation || PrintBailouts) tty->print_cr("compilation bailout: %s", msg);
601 _bailout_msg = msg;
602 }
603 }
604
605 ciKlass* Compilation::cha_exact_type(ciType* type) {
606 if (type != NULL && type->is_loaded() && type->is_instance_klass()) {
607 ciInstanceKlass* ik = type->as_instance_klass();
608 assert(ik->exact_klass() == NULL, "no cha for final klass");
609 if (DeoptC1 && UseCHA && !(ik->has_subklass() || ik->is_interface())) {
610 dependency_recorder()->assert_leaf_type(ik);
611 return ik;
612 }
613 }
614 return NULL;
615 }
616
617 void Compilation::print_timers() {
618 // 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);
619 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();
620
621
622 tty->print_cr(" Detailed C1 Timings");
623 tty->print_cr(" Setup time: %6.3f s (%4.1f%%)", timers[_t_setup].seconds(), (timers[_t_setup].seconds() / total) * 100.0);
624 tty->print_cr(" Build IR: %6.3f s (%4.1f%%)", timers[_t_buildIR].seconds(), (timers[_t_buildIR].seconds() / total) * 100.0);
625 float t_optimizeIR = timers[_t_optimize_blocks].seconds() + timers[_t_optimize_null_checks].seconds();
626 tty->print_cr(" Optimize: %6.3f s (%4.1f%%)", t_optimizeIR, (t_optimizeIR / total) * 100.0);
627 tty->print_cr(" RCE: %6.3f s (%4.1f%%)", timers[_t_rangeCheckElimination].seconds(), (timers[_t_rangeCheckElimination].seconds() / total) * 100.0);
628 tty->print_cr(" Emit LIR: %6.3f s (%4.1f%%)", timers[_t_emit_lir].seconds(), (timers[_t_emit_lir].seconds() / total) * 100.0);
629 tty->print_cr(" LIR Gen: %6.3f s (%4.1f%%)", timers[_t_lirGeneration].seconds(), (timers[_t_lirGeneration].seconds() / total) * 100.0);
630 tty->print_cr(" Linear Scan: %6.3f s (%4.1f%%)", timers[_t_linearScan].seconds(), (timers[_t_linearScan].seconds() / total) * 100.0);
631 NOT_PRODUCT(LinearScan::print_timers(timers[_t_linearScan].seconds()));
632 tty->print_cr(" LIR Schedule: %6.3f s (%4.1f%%)", timers[_t_lir_schedule].seconds(), (timers[_t_lir_schedule].seconds() / total) * 100.0);
633 tty->print_cr(" Code Emission: %6.3f s (%4.1f%%)", timers[_t_codeemit].seconds(), (timers[_t_codeemit].seconds() / total) * 100.0);
634 tty->print_cr(" Code Installation: %6.3f s (%4.1f%%)", timers[_t_codeinstall].seconds(), (timers[_t_codeinstall].seconds() / total) * 100.0);
635 tty->print_cr(" Instruction Nodes: %6d nodes", totalInstructionNodes);
636
637 NOT_PRODUCT(LinearScan::print_statistics());
638 }
639
640
641 #ifndef PRODUCT
642 void Compilation::compile_only_this_method() {
643 ResourceMark rm;
644 fileStream stream(fopen("c1_compile_only", "wt"));
645 stream.print_cr("# c1 compile only directives");
646 compile_only_this_scope(&stream, hir()->top_scope());
647 }
648
649
650 void Compilation::compile_only_this_scope(outputStream* st, IRScope* scope) {
651 st->print("CompileOnly=");
652 scope->method()->holder()->name()->print_symbol_on(st);
653 st->print(".");
654 scope->method()->name()->print_symbol_on(st);
655 st->cr();
656 }
657
658
659 void Compilation::exclude_this_method() {
660 fileStream stream(fopen(".hotspot_compiler", "at"));
661 stream.print("exclude ");
662 method()->holder()->name()->print_symbol_on(&stream);
663 stream.print(" ");
664 method()->name()->print_symbol_on(&stream);
665 stream.cr();
666 stream.cr();
667 }
668 #endif

mercurial