Mon, 26 Apr 2010 23:59:45 -0700
6943485: JVMTI always on capabilities change code generation too much
Reviewed-by: twisti, dcubed
1 /*
2 * Copyright 1999-2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 Arena* Compilation::_arena = NULL;
70 Compilation* Compilation::_compilation = NULL;
72 // Implementation of Compilation
75 #ifndef PRODUCT
77 void 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
86 DebugInformationRecorder* Compilation::debug_info_recorder() const {
87 return _env->debug_info();
88 }
91 Dependencies* Compilation::dependency_recorder() const {
92 return _env->dependencies();
93 }
96 void 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 }
107 void Compilation::build_hir() {
108 CHECK_BAILOUT();
110 // setup ir
111 _hir = new IR(this, method(), osr_bci());
112 if (!_hir->is_valid()) {
113 bailout("invalid parsing");
114 return;
115 }
117 #ifndef PRODUCT
118 if (PrintCFGToFile) {
119 CFGPrinter::print_cfg(_hir, "After Generation of HIR", true, false);
120 }
121 #endif
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
128 _hir->verify();
130 if (UseC1Optimizations) {
131 NEEDS_CLEANUP
132 // optimization
133 PhaseTraceTime timeit(_t_optimizeIR);
135 _hir->optimize();
136 }
138 _hir->verify();
140 _hir->split_critical_edges();
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
147 _hir->verify();
149 // compute block ordering for code generation
150 // the control flow must not be changed from here on
151 _hir->compute_code();
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 }
161 // compute use counts after global value numbering
162 _hir->compute_use_counts();
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
169 _hir->verify();
170 }
173 void Compilation::emit_lir() {
174 CHECK_BAILOUT();
176 LIRGenerator gen(this, method());
177 {
178 PhaseTraceTime timeit(_t_lirGeneration);
179 hir()->iterate_linear_scan_order(&gen);
180 }
182 CHECK_BAILOUT();
184 {
185 PhaseTraceTime timeit(_t_linearScan);
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();
193 _max_spills = allocator->max_spills();
194 }
196 if (BailoutAfterLIR) {
197 if (PrintLIR && !bailed_out()) {
198 print_LIR(hir()->code());
199 }
200 bailout("Bailing out because of -XX:+BailoutAfterLIR");
201 }
202 }
205 void Compilation::emit_code_epilog(LIR_Assembler* assembler) {
206 CHECK_BAILOUT();
208 CodeOffsets* code_offsets = assembler->offsets();
210 // generate code or slow cases
211 assembler->emit_slow_case_stubs();
212 CHECK_BAILOUT();
214 // generate exception adapters
215 assembler->emit_exception_entries(exception_info_list());
216 CHECK_BAILOUT();
218 // Generate code for exception handler.
219 code_offsets->set_value(CodeOffsets::Exceptions, assembler->emit_exception_handler());
220 CHECK_BAILOUT();
222 // Generate code for deopt handler.
223 code_offsets->set_value(CodeOffsets::Deopt, assembler->emit_deopt_handler());
224 CHECK_BAILOUT();
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();
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());
236 // done
237 masm()->flush();
238 }
241 int Compilation::emit_code_body() {
242 // emit code
243 Runtime1::setup_code_buffer(code(), allocator()->num_calls());
244 code()->initialize_oop_recorder(env()->oop_recorder());
246 _masm = new C1_MacroAssembler(code());
247 _masm->set_oop_recorder(env()->oop_recorder());
249 LIR_Assembler lir_asm(this);
251 lir_asm.emit_code(hir()->code());
252 CHECK_BAILOUT_(0);
254 emit_code_epilog(&lir_asm);
255 CHECK_BAILOUT_(0);
257 generate_exception_handler_table();
259 #ifndef PRODUCT
260 if (PrintExceptionHandlers && Verbose) {
261 exception_handler_table()->print();
262 }
263 #endif /* PRODUCT */
265 return frame_map()->framesize();
266 }
269 int Compilation::compile_java_method() {
270 assert(!method()->is_native(), "should not reach here");
272 if (BailoutOnExceptionHandlers) {
273 if (method()->has_exception_handlers()) {
274 bailout("linear scan can't handle exception handlers");
275 }
276 }
278 CHECK_BAILOUT_(no_frame_size);
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 }
289 {
290 PhaseTraceTime timeit(_t_emit_lir);
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);
297 {
298 PhaseTraceTime timeit(_t_codeemit);
299 return emit_code_body();
300 }
301 }
303 void 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 }
325 void Compilation::compile_method() {
326 // setup compilation
327 initialize();
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 }
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 }
341 if (method()->break_at_execute()) {
342 BREAKPOINT;
343 }
345 #ifndef PRODUCT
346 if (PrintCFGToFile) {
347 CFGPrinter::print_compilation(this);
348 }
349 #endif
351 // compile method
352 int frame_size = compile_java_method();
354 // bailout if method couldn't be compiled
355 // Note: make sure we mark the method as not compilable!
356 CHECK_BAILOUT();
358 if (InstallMethods) {
359 // install code
360 PhaseTraceTime timeit(_t_codeinstall);
361 install_code(frame_size);
362 }
363 totalInstructionNodes += Instruction::number_of_instructions();
364 }
367 void 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();
372 if (info_list->length() == 0) {
373 return;
374 }
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);
382 for (int i = 0; i < info_list->length(); i++) {
383 ExceptionInfo* info = info_list->at(i);
384 XHandlers* handlers = info->exception_handlers();
386 // empty the arrays
387 bcis->trunc_to(0);
388 scope_depths->trunc_to(0);
389 pcos->trunc_to(0);
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");
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 }
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());
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 }
425 Compilation::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);
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 }
457 Compilation::~Compilation() {
458 _arena = NULL;
459 _compilation = NULL;
460 }
463 void 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 }
474 void Compilation::notice_inlined_method(ciMethod* method) {
475 _env->notice_inlined_method(method);
476 }
479 void 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 }
489 void 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();
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);
507 NOT_PRODUCT(LinearScan::print_statistics());
508 }
511 #ifndef PRODUCT
512 void 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 }
520 void 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 }
529 void 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