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