|
1 /* |
|
2 * Copyright 1999-2007 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 */ |
|
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 Arena* Compilation::_arena = NULL; |
|
70 Compilation* Compilation::_compilation = NULL; |
|
71 |
|
72 // Implementation of Compilation |
|
73 |
|
74 |
|
75 #ifndef PRODUCT |
|
76 |
|
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 |
|
84 |
|
85 |
|
86 DebugInformationRecorder* Compilation::debug_info_recorder() const { |
|
87 return _env->debug_info(); |
|
88 } |
|
89 |
|
90 |
|
91 Dependencies* Compilation::dependency_recorder() const { |
|
92 return _env->dependencies(); |
|
93 } |
|
94 |
|
95 |
|
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 } |
|
105 |
|
106 |
|
107 void 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 |
|
173 void 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 |
|
205 void Compilation::emit_code_epilog(LIR_Assembler* assembler) { |
|
206 CHECK_BAILOUT(); |
|
207 |
|
208 // generate code or slow cases |
|
209 assembler->emit_slow_case_stubs(); |
|
210 CHECK_BAILOUT(); |
|
211 |
|
212 // generate exception adapters |
|
213 assembler->emit_exception_entries(exception_info_list()); |
|
214 CHECK_BAILOUT(); |
|
215 |
|
216 // generate code for exception handler |
|
217 assembler->emit_exception_handler(); |
|
218 CHECK_BAILOUT(); |
|
219 assembler->emit_deopt_handler(); |
|
220 CHECK_BAILOUT(); |
|
221 |
|
222 // done |
|
223 masm()->flush(); |
|
224 } |
|
225 |
|
226 |
|
227 int Compilation::emit_code_body() { |
|
228 // emit code |
|
229 Runtime1::setup_code_buffer(code(), allocator()->num_calls()); |
|
230 code()->initialize_oop_recorder(env()->oop_recorder()); |
|
231 |
|
232 _masm = new C1_MacroAssembler(code()); |
|
233 _masm->set_oop_recorder(env()->oop_recorder()); |
|
234 |
|
235 LIR_Assembler lir_asm(this); |
|
236 |
|
237 lir_asm.emit_code(hir()->code()); |
|
238 CHECK_BAILOUT_(0); |
|
239 |
|
240 emit_code_epilog(&lir_asm); |
|
241 CHECK_BAILOUT_(0); |
|
242 |
|
243 generate_exception_handler_table(); |
|
244 |
|
245 #ifndef PRODUCT |
|
246 if (PrintExceptionHandlers && Verbose) { |
|
247 exception_handler_table()->print(); |
|
248 } |
|
249 #endif /* PRODUCT */ |
|
250 |
|
251 return frame_map()->framesize(); |
|
252 } |
|
253 |
|
254 |
|
255 int Compilation::compile_java_method() { |
|
256 assert(!method()->is_native(), "should not reach here"); |
|
257 |
|
258 if (BailoutOnExceptionHandlers) { |
|
259 if (method()->has_exception_handlers()) { |
|
260 bailout("linear scan can't handle exception handlers"); |
|
261 } |
|
262 } |
|
263 |
|
264 CHECK_BAILOUT_(no_frame_size); |
|
265 |
|
266 { |
|
267 PhaseTraceTime timeit(_t_buildIR); |
|
268 build_hir(); |
|
269 } |
|
270 if (BailoutAfterHIR) { |
|
271 BAILOUT_("Bailing out because of -XX:+BailoutAfterHIR", no_frame_size); |
|
272 } |
|
273 |
|
274 |
|
275 { |
|
276 PhaseTraceTime timeit(_t_emit_lir); |
|
277 |
|
278 _frame_map = new FrameMap(method(), hir()->number_of_locks(), MAX2(4, hir()->max_stack())); |
|
279 emit_lir(); |
|
280 } |
|
281 CHECK_BAILOUT_(no_frame_size); |
|
282 |
|
283 { |
|
284 PhaseTraceTime timeit(_t_codeemit); |
|
285 return emit_code_body(); |
|
286 } |
|
287 } |
|
288 |
|
289 void Compilation::install_code(int frame_size) { |
|
290 // frame_size is in 32-bit words so adjust it intptr_t words |
|
291 assert(frame_size == frame_map()->framesize(), "must match"); |
|
292 assert(in_bytes(frame_map()->framesize_in_bytes()) % sizeof(intptr_t) == 0, "must be at least pointer aligned"); |
|
293 _env->register_method( |
|
294 method(), |
|
295 osr_bci(), |
|
296 &_offsets, |
|
297 in_bytes(_frame_map->sp_offset_for_orig_pc()), |
|
298 code(), |
|
299 in_bytes(frame_map()->framesize_in_bytes()) / sizeof(intptr_t), |
|
300 debug_info_recorder()->_oopmaps, |
|
301 exception_handler_table(), |
|
302 implicit_exception_table(), |
|
303 compiler(), |
|
304 _env->comp_level(), |
|
305 needs_debug_information(), |
|
306 has_unsafe_access() |
|
307 ); |
|
308 } |
|
309 |
|
310 |
|
311 void Compilation::compile_method() { |
|
312 // setup compilation |
|
313 initialize(); |
|
314 |
|
315 if (!method()->can_be_compiled()) { |
|
316 // Prevent race condition 6328518. |
|
317 // This can happen if the method is obsolete or breakpointed. |
|
318 bailout("Bailing out because method is not compilable"); |
|
319 return; |
|
320 } |
|
321 |
|
322 if (JvmtiExport::can_hotswap_or_post_breakpoint()) { |
|
323 // We can assert evol_method because method->can_be_compiled is true. |
|
324 dependency_recorder()->assert_evol_method(method()); |
|
325 } |
|
326 |
|
327 if (method()->break_at_execute()) { |
|
328 BREAKPOINT; |
|
329 } |
|
330 |
|
331 #ifndef PRODUCT |
|
332 if (PrintCFGToFile) { |
|
333 CFGPrinter::print_compilation(this); |
|
334 } |
|
335 #endif |
|
336 |
|
337 // compile method |
|
338 int frame_size = compile_java_method(); |
|
339 |
|
340 // bailout if method couldn't be compiled |
|
341 // Note: make sure we mark the method as not compilable! |
|
342 CHECK_BAILOUT(); |
|
343 |
|
344 if (InstallMethods) { |
|
345 // install code |
|
346 PhaseTraceTime timeit(_t_codeinstall); |
|
347 install_code(frame_size); |
|
348 } |
|
349 totalInstructionNodes += Instruction::number_of_instructions(); |
|
350 } |
|
351 |
|
352 |
|
353 void Compilation::generate_exception_handler_table() { |
|
354 // Generate an ExceptionHandlerTable from the exception handler |
|
355 // information accumulated during the compilation. |
|
356 ExceptionInfoList* info_list = exception_info_list(); |
|
357 |
|
358 if (info_list->length() == 0) { |
|
359 return; |
|
360 } |
|
361 |
|
362 // allocate some arrays for use by the collection code. |
|
363 const int num_handlers = 5; |
|
364 GrowableArray<intptr_t>* bcis = new GrowableArray<intptr_t>(num_handlers); |
|
365 GrowableArray<intptr_t>* scope_depths = new GrowableArray<intptr_t>(num_handlers); |
|
366 GrowableArray<intptr_t>* pcos = new GrowableArray<intptr_t>(num_handlers); |
|
367 |
|
368 for (int i = 0; i < info_list->length(); i++) { |
|
369 ExceptionInfo* info = info_list->at(i); |
|
370 XHandlers* handlers = info->exception_handlers(); |
|
371 |
|
372 // empty the arrays |
|
373 bcis->trunc_to(0); |
|
374 scope_depths->trunc_to(0); |
|
375 pcos->trunc_to(0); |
|
376 |
|
377 for (int i = 0; i < handlers->length(); i++) { |
|
378 XHandler* handler = handlers->handler_at(i); |
|
379 assert(handler->entry_pco() != -1, "must have been generated"); |
|
380 |
|
381 int e = bcis->find(handler->handler_bci()); |
|
382 if (e >= 0 && scope_depths->at(e) == handler->scope_count()) { |
|
383 // two different handlers are declared to dispatch to the same |
|
384 // catch bci. During parsing we created edges for each |
|
385 // handler but we really only need one. The exception handler |
|
386 // table will also get unhappy if we try to declare both since |
|
387 // it's nonsensical. Just skip this handler. |
|
388 continue; |
|
389 } |
|
390 |
|
391 bcis->append(handler->handler_bci()); |
|
392 if (handler->handler_bci() == -1) { |
|
393 // insert a wildcard handler at scope depth 0 so that the |
|
394 // exception lookup logic with find it. |
|
395 scope_depths->append(0); |
|
396 } else { |
|
397 scope_depths->append(handler->scope_count()); |
|
398 } |
|
399 pcos->append(handler->entry_pco()); |
|
400 |
|
401 // stop processing once we hit a catch any |
|
402 if (handler->is_catch_all()) { |
|
403 assert(i == handlers->length() - 1, "catch all must be last handler"); |
|
404 } |
|
405 } |
|
406 exception_handler_table()->add_subtable(info->pco(), bcis, scope_depths, pcos); |
|
407 } |
|
408 } |
|
409 |
|
410 |
|
411 Compilation::Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method, int osr_bci) |
|
412 : _compiler(compiler) |
|
413 , _env(env) |
|
414 , _method(method) |
|
415 , _osr_bci(osr_bci) |
|
416 , _hir(NULL) |
|
417 , _max_spills(-1) |
|
418 , _frame_map(NULL) |
|
419 , _masm(NULL) |
|
420 , _has_exception_handlers(false) |
|
421 , _has_fpu_code(true) // pessimistic assumption |
|
422 , _has_unsafe_access(false) |
|
423 , _bailout_msg(NULL) |
|
424 , _exception_info_list(NULL) |
|
425 , _allocator(NULL) |
|
426 , _code(Runtime1::get_buffer_blob()->instructions_begin(), |
|
427 Runtime1::get_buffer_blob()->instructions_size()) |
|
428 , _current_instruction(NULL) |
|
429 #ifndef PRODUCT |
|
430 , _last_instruction_printed(NULL) |
|
431 #endif // PRODUCT |
|
432 { |
|
433 PhaseTraceTime timeit(_t_compile); |
|
434 |
|
435 assert(_arena == NULL, "shouldn't only one instance of Compilation in existence at a time"); |
|
436 _arena = Thread::current()->resource_area(); |
|
437 _compilation = this; |
|
438 _needs_debug_information = JvmtiExport::can_examine_or_deopt_anywhere() || |
|
439 JavaMonitorsInStackTrace || AlwaysEmitDebugInfo || DeoptimizeALot; |
|
440 _exception_info_list = new ExceptionInfoList(); |
|
441 _implicit_exception_table.set_size(0); |
|
442 compile_method(); |
|
443 } |
|
444 |
|
445 Compilation::~Compilation() { |
|
446 _arena = NULL; |
|
447 _compilation = NULL; |
|
448 } |
|
449 |
|
450 |
|
451 void Compilation::add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers) { |
|
452 #ifndef PRODUCT |
|
453 if (PrintExceptionHandlers && Verbose) { |
|
454 tty->print_cr(" added exception scope for pco %d", pco); |
|
455 } |
|
456 #endif |
|
457 // Note: we do not have program counters for these exception handlers yet |
|
458 exception_info_list()->push(new ExceptionInfo(pco, exception_handlers)); |
|
459 } |
|
460 |
|
461 |
|
462 void Compilation::notice_inlined_method(ciMethod* method) { |
|
463 _env->notice_inlined_method(method); |
|
464 } |
|
465 |
|
466 |
|
467 void Compilation::bailout(const char* msg) { |
|
468 assert(msg != NULL, "bailout message must exist"); |
|
469 if (!bailed_out()) { |
|
470 // keep first bailout message |
|
471 if (PrintBailouts) tty->print_cr("compilation bailout: %s", msg); |
|
472 _bailout_msg = msg; |
|
473 } |
|
474 } |
|
475 |
|
476 |
|
477 void Compilation::print_timers() { |
|
478 // 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); |
|
479 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(); |
|
480 |
|
481 |
|
482 tty->print_cr(" Detailed C1 Timings"); |
|
483 tty->print_cr(" Setup time: %6.3f s (%4.1f%%)", timers[_t_setup].seconds(), (timers[_t_setup].seconds() / total) * 100.0); |
|
484 tty->print_cr(" Build IR: %6.3f s (%4.1f%%)", timers[_t_buildIR].seconds(), (timers[_t_buildIR].seconds() / total) * 100.0); |
|
485 tty->print_cr(" Optimize: %6.3f s (%4.1f%%)", timers[_t_optimizeIR].seconds(), (timers[_t_optimizeIR].seconds() / total) * 100.0); |
|
486 tty->print_cr(" Emit LIR: %6.3f s (%4.1f%%)", timers[_t_emit_lir].seconds(), (timers[_t_emit_lir].seconds() / total) * 100.0); |
|
487 tty->print_cr(" LIR Gen: %6.3f s (%4.1f%%)", timers[_t_lirGeneration].seconds(), (timers[_t_lirGeneration].seconds() / total) * 100.0); |
|
488 tty->print_cr(" Linear Scan: %6.3f s (%4.1f%%)", timers[_t_linearScan].seconds(), (timers[_t_linearScan].seconds() / total) * 100.0); |
|
489 NOT_PRODUCT(LinearScan::print_timers(timers[_t_linearScan].seconds())); |
|
490 tty->print_cr(" LIR Schedule: %6.3f s (%4.1f%%)", timers[_t_lir_schedule].seconds(), (timers[_t_lir_schedule].seconds() / total) * 100.0); |
|
491 tty->print_cr(" Code Emission: %6.3f s (%4.1f%%)", timers[_t_codeemit].seconds(), (timers[_t_codeemit].seconds() / total) * 100.0); |
|
492 tty->print_cr(" Code Installation: %6.3f s (%4.1f%%)", timers[_t_codeinstall].seconds(), (timers[_t_codeinstall].seconds() / total) * 100.0); |
|
493 tty->print_cr(" Instruction Nodes: %6d nodes", totalInstructionNodes); |
|
494 |
|
495 NOT_PRODUCT(LinearScan::print_statistics()); |
|
496 } |
|
497 |
|
498 |
|
499 #ifndef PRODUCT |
|
500 void Compilation::compile_only_this_method() { |
|
501 ResourceMark rm; |
|
502 fileStream stream(fopen("c1_compile_only", "wt")); |
|
503 stream.print_cr("# c1 compile only directives"); |
|
504 compile_only_this_scope(&stream, hir()->top_scope()); |
|
505 } |
|
506 |
|
507 |
|
508 void Compilation::compile_only_this_scope(outputStream* st, IRScope* scope) { |
|
509 st->print("CompileOnly="); |
|
510 scope->method()->holder()->name()->print_symbol_on(st); |
|
511 st->print("."); |
|
512 scope->method()->name()->print_symbol_on(st); |
|
513 st->cr(); |
|
514 } |
|
515 |
|
516 |
|
517 void Compilation::exclude_this_method() { |
|
518 fileStream stream(fopen(".hotspot_compiler", "at")); |
|
519 stream.print("exclude "); |
|
520 method()->holder()->name()->print_symbol_on(&stream); |
|
521 stream.print(" "); |
|
522 method()->name()->print_symbol_on(&stream); |
|
523 stream.cr(); |
|
524 stream.cr(); |
|
525 } |
|
526 #endif |