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_Compilation.hpp" 27 #include "c1/c1_FrameMap.hpp" 28 #include "c1/c1_GraphBuilder.hpp" 29 #include "c1/c1_IR.hpp" 30 #include "c1/c1_InstructionPrinter.hpp" 31 #include "c1/c1_Optimizer.hpp" 32 #include "utilities/bitMap.inline.hpp" 33 34 35 // Implementation of XHandlers 36 // 37 // Note: This code could eventually go away if we are 38 // just using the ciExceptionHandlerStream. 39 40 XHandlers::XHandlers(ciMethod* method) : _list(method->exception_table_length()) { 41 ciExceptionHandlerStream s(method); 42 while (!s.is_done()) { 43 _list.append(new XHandler(s.handler())); 44 s.next(); 45 } 46 assert(s.count() == method->exception_table_length(), "exception table lengths inconsistent"); 47 } 48 49 // deep copy of all XHandler contained in list 50 XHandlers::XHandlers(XHandlers* other) : 51 _list(other->length()) 52 { 53 for (int i = 0; i < other->length(); i++) { 54 _list.append(new XHandler(other->handler_at(i))); 55 } 56 } 57 58 // Returns whether a particular exception type can be caught. Also 59 // returns true if klass is unloaded or any exception handler 60 // classes are unloaded. type_is_exact indicates whether the throw 61 // is known to be exactly that class or it might throw a subtype. 62 bool XHandlers::could_catch(ciInstanceKlass* klass, bool type_is_exact) const { 63 // the type is unknown so be conservative 64 if (!klass->is_loaded()) { 65 return true; 66 } 67 68 for (int i = 0; i < length(); i++) { 69 XHandler* handler = handler_at(i); 70 if (handler->is_catch_all()) { 71 // catch of ANY 72 return true; 73 } 74 ciInstanceKlass* handler_klass = handler->catch_klass(); 75 // if it's unknown it might be catchable 76 if (!handler_klass->is_loaded()) { 77 return true; 78 } 79 // if the throw type is definitely a subtype of the catch type 80 // then it can be caught. 81 if (klass->is_subtype_of(handler_klass)) { 82 return true; 83 } 84 if (!type_is_exact) { 85 // If the type isn't exactly known then it can also be caught by 86 // catch statements where the inexact type is a subtype of the 87 // catch type. 88 // given: foo extends bar extends Exception 89 // throw bar can be caught by catch foo, catch bar, and catch 90 // Exception, however it can't be caught by any handlers without 91 // bar in its type hierarchy. 92 if (handler_klass->is_subtype_of(klass)) { 93 return true; 94 } 95 } 96 } 97 98 return false; 99 } 100 101 102 bool XHandlers::equals(XHandlers* others) const { 103 if (others == NULL) return false; 104 if (length() != others->length()) return false; 105 106 for (int i = 0; i < length(); i++) { 107 if (!handler_at(i)->equals(others->handler_at(i))) return false; 108 } 109 return true; 110 } 111 112 bool XHandler::equals(XHandler* other) const { 113 assert(entry_pco() != -1 && other->entry_pco() != -1, "must have entry_pco"); 114 115 if (entry_pco() != other->entry_pco()) return false; 116 if (scope_count() != other->scope_count()) return false; 117 if (_desc != other->_desc) return false; 118 119 assert(entry_block() == other->entry_block(), "entry_block must be equal when entry_pco is equal"); 120 return true; 121 } 122 123 124 // Implementation of IRScope 125 BlockBegin* IRScope::build_graph(Compilation* compilation, int osr_bci) { 126 GraphBuilder gm(compilation, this); 127 NOT_PRODUCT(if (PrintValueNumbering && Verbose) gm.print_stats()); 128 if (compilation->bailed_out()) return NULL; 129 return gm.start(); 130 } 131 132 133 IRScope::IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph) 134 : _callees(2) 135 , _compilation(compilation) 136 , _requires_phi_function(method->max_locals()) 137 { 138 _caller = caller; 139 _level = caller == NULL ? 0 : caller->level() + 1; 140 _method = method; 141 _xhandlers = new XHandlers(method); 142 _number_of_locks = 0; 143 _monitor_pairing_ok = method->has_balanced_monitors(); 144 _wrote_final = false; 145 _wrote_fields = false; 146 _start = NULL; 147 148 if (osr_bci == -1) { 149 _requires_phi_function.clear(); 150 } else { 151 // selective creation of phi functions is not possibel in osr-methods 152 _requires_phi_function.set_range(0, method->max_locals()); 153 } 154 155 assert(method->holder()->is_loaded() , "method holder must be loaded"); 156 157 // build graph if monitor pairing is ok 158 if (create_graph && monitor_pairing_ok()) _start = build_graph(compilation, osr_bci); 159 } 160 161 162 int IRScope::max_stack() const { 163 int my_max = method()->max_stack(); 164 int callee_max = 0; 165 for (int i = 0; i < number_of_callees(); i++) { 166 callee_max = MAX2(callee_max, callee_no(i)->max_stack()); 167 } 168 return my_max + callee_max; 169 } 170 171 172 bool IRScopeDebugInfo::should_reexecute() { 173 ciMethod* cur_method = scope()->method(); 174 int cur_bci = bci(); 175 if (cur_method != NULL && cur_bci != SynchronizationEntryBCI) { 176 Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci); 177 return Interpreter::bytecode_should_reexecute(code); 178 } else 179 return false; 180 } 181 182 183 // Implementation of CodeEmitInfo 184 185 // Stack must be NON-null 186 CodeEmitInfo::CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers, bool deoptimize_on_exception) 187 : _scope(stack->scope()) 188 , _scope_debug_info(NULL) 189 , _oop_map(NULL) 190 , _stack(stack) 191 , _exception_handlers(exception_handlers) 192 , _is_method_handle_invoke(false) 193 , _deoptimize_on_exception(deoptimize_on_exception) { 194 assert(_stack != NULL, "must be non null"); 195 } 196 197 198 CodeEmitInfo::CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack) 199 : _scope(info->_scope) 200 , _exception_handlers(NULL) 201 , _scope_debug_info(NULL) 202 , _oop_map(NULL) 203 , _stack(stack == NULL ? info->_stack : stack) 204 , _is_method_handle_invoke(info->_is_method_handle_invoke) 205 , _deoptimize_on_exception(info->_deoptimize_on_exception) { 206 207 // deep copy of exception handlers 208 if (info->_exception_handlers != NULL) { 209 _exception_handlers = new XHandlers(info->_exception_handlers); 210 } 211 } 212 213 214 void CodeEmitInfo::record_debug_info(DebugInformationRecorder* recorder, int pc_offset) { 215 // record the safepoint before recording the debug info for enclosing scopes 216 recorder->add_safepoint(pc_offset, _oop_map->deep_copy()); 217 _scope_debug_info->record_debug_info(recorder, pc_offset, true/*topmost*/, _is_method_handle_invoke); 218 recorder->end_safepoint(pc_offset); 219 } 220 221 222 void CodeEmitInfo::add_register_oop(LIR_Opr opr) { 223 assert(_oop_map != NULL, "oop map must already exist"); 224 assert(opr->is_single_cpu(), "should not call otherwise"); 225 226 VMReg name = frame_map()->regname(opr); 227 _oop_map->set_oop(name); 228 } 229 230 // Mirror the stack size calculation in the deopt code 231 // How much stack space would we need at this point in the program in 232 // case of deoptimization? 233 int CodeEmitInfo::interpreter_frame_size() const { 234 ValueStack* state = _stack; 235 int size = 0; 236 int callee_parameters = 0; 237 int callee_locals = 0; 238 int popframe_extra_args = 0; 239 240 if (JvmtiExport::can_pop_frame() && state->kind() != ValueStack::StateBefore) { 241 ciMethod* method = state->scope()->method(); 242 int bci = state->bci(); 243 popframe_extra_args = MAX2(-method->stack_effect_if_at_invoke(bci), 0); 244 } 245 246 while (state != NULL) { 247 int locks = state->locks_size(); 248 int temps = state->stack_size(); 249 bool is_top_frame = (state->caller_state() == NULL); 250 ciMethod* method = state->scope()->method(); 251 252 int frame_size = BytesPerWord * Interpreter::size_activation<ciMethod>(method, 253 temps + callee_parameters, 254 popframe_extra_args, 255 locks, 256 callee_parameters, 257 callee_locals, 258 is_top_frame); 259 size += frame_size; 260 261 callee_parameters = method->size_of_parameters(); 262 callee_locals = method->max_locals(); 263 popframe_extra_args = 0; 264 state = state->caller_state(); 265 } 266 return size + Deoptimization::last_frame_adjust(0, callee_locals) * BytesPerWord; 267 } 268 269 // Implementation of IR 270 271 IR::IR(Compilation* compilation, ciMethod* method, int osr_bci) : 272 _locals_size(in_WordSize(-1)) 273 , _num_loops(0) { 274 // setup IR fields 275 _compilation = compilation; 276 _top_scope = new IRScope(compilation, NULL, -1, method, osr_bci, true); 277 _code = NULL; 278 } 279 280 281 void IR::optimize_blocks() { 282 Optimizer opt(this); 283 if (!compilation()->profile_branches()) { 284 if (DoCEE) { 285 opt.eliminate_conditional_expressions(); 286 #ifndef PRODUCT 287 if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after CEE"); print(true); } 288 if (PrintIR || PrintIR1 ) { tty->print_cr("IR after CEE"); print(false); } 289 #endif 290 } 291 if (EliminateBlocks) { 292 opt.eliminate_blocks(); 293 #ifndef PRODUCT 294 if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after block elimination"); print(true); } 295 if (PrintIR || PrintIR1 ) { tty->print_cr("IR after block elimination"); print(false); } 296 #endif 297 } 298 } 299 } 300 301 void IR::eliminate_null_checks() { 302 Optimizer opt(this); 303 if (EliminateNullChecks) { 304 opt.eliminate_null_checks(); 305 #ifndef PRODUCT 306 if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after null check elimination"); print(true); } 307 if (PrintIR || PrintIR1 ) { tty->print_cr("IR after null check elimination"); print(false); } 308 #endif 309 } 310 } 311 312 313 static int sort_pairs(BlockPair** a, BlockPair** b) { 314 if ((*a)->from() == (*b)->from()) { 315 return (*a)->to()->block_id() - (*b)->to()->block_id(); 316 } else { 317 return (*a)->from()->block_id() - (*b)->from()->block_id(); 318 } 319 } 320 321 322 class CriticalEdgeFinder: public BlockClosure { 323 BlockPairList blocks; 324 IR* _ir; 325 326 public: 327 CriticalEdgeFinder(IR* ir): _ir(ir) {} 328 void block_do(BlockBegin* bb) { 329 BlockEnd* be = bb->end(); 330 int nos = be->number_of_sux(); 331 if (nos >= 2) { 332 for (int i = 0; i < nos; i++) { 333 BlockBegin* sux = be->sux_at(i); 334 if (sux->number_of_preds() >= 2) { 335 blocks.append(new BlockPair(bb, sux)); 336 } 337 } 338 } 339 } 340 341 void split_edges() { 342 BlockPair* last_pair = NULL; 343 blocks.sort(sort_pairs); 344 for (int i = 0; i < blocks.length(); i++) { 345 BlockPair* pair = blocks.at(i); 346 if (last_pair != NULL && pair->is_same(last_pair)) continue; 347 BlockBegin* from = pair->from(); 348 BlockBegin* to = pair->to(); 349 BlockBegin* split = from->insert_block_between(to); 350 #ifndef PRODUCT 351 if ((PrintIR || PrintIR1) && Verbose) { 352 tty->print_cr("Split critical edge B%d -> B%d (new block B%d)", 353 from->block_id(), to->block_id(), split->block_id()); 354 } 355 #endif 356 last_pair = pair; 357 } 358 } 359 }; 360 361 void IR::split_critical_edges() { 362 CriticalEdgeFinder cef(this); 363 364 iterate_preorder(&cef); 365 cef.split_edges(); 366 } 367 368 369 class UseCountComputer: public ValueVisitor, BlockClosure { 370 private: 371 void visit(Value* n) { 372 // Local instructions and Phis for expression stack values at the 373 // start of basic blocks are not added to the instruction list 374 if (!(*n)->is_linked() && (*n)->can_be_linked()) { 375 assert(false, "a node was not appended to the graph"); 376 Compilation::current()->bailout("a node was not appended to the graph"); 377 } 378 // use n's input if not visited before 379 if (!(*n)->is_pinned() && !(*n)->has_uses()) { 380 // note: a) if the instruction is pinned, it will be handled by compute_use_count 381 // b) if the instruction has uses, it was touched before 382 // => in both cases we don't need to update n's values 383 uses_do(n); 384 } 385 // use n 386 (*n)->_use_count++; 387 } 388 389 Values* worklist; 390 int depth; 391 enum { 392 max_recurse_depth = 20 393 }; 394 395 void uses_do(Value* n) { 396 depth++; 397 if (depth > max_recurse_depth) { 398 // don't allow the traversal to recurse too deeply 399 worklist->push(*n); 400 } else { 401 (*n)->input_values_do(this); 402 // special handling for some instructions 403 if ((*n)->as_BlockEnd() != NULL) { 404 // note on BlockEnd: 405 // must 'use' the stack only if the method doesn't 406 // terminate, however, in those cases stack is empty 407 (*n)->state_values_do(this); 408 } 409 } 410 depth--; 411 } 412 413 void block_do(BlockBegin* b) { 414 depth = 0; 415 // process all pinned nodes as the roots of expression trees 416 for (Instruction* n = b; n != NULL; n = n->next()) { 417 if (n->is_pinned()) uses_do(&n); 418 } 419 assert(depth == 0, "should have counted back down"); 420 421 // now process any unpinned nodes which recursed too deeply 422 while (worklist->length() > 0) { 423 Value t = worklist->pop(); 424 if (!t->is_pinned()) { 425 // compute the use count 426 uses_do(&t); 427 428 // pin the instruction so that LIRGenerator doesn't recurse 429 // too deeply during it's evaluation. 430 t->pin(); 431 } 432 } 433 assert(depth == 0, "should have counted back down"); 434 } 435 436 UseCountComputer() { 437 worklist = new Values(); 438 depth = 0; 439 } 440 441 public: 442 static void compute(BlockList* blocks) { 443 UseCountComputer ucc; 444 blocks->iterate_backward(&ucc); 445 } 446 }; 447 448 449 // helper macro for short definition of trace-output inside code 450 #ifndef PRODUCT 451 #define TRACE_LINEAR_SCAN(level, code) \ 452 if (TraceLinearScanLevel >= level) { \ 453 code; \ 454 } 455 #else 456 #define TRACE_LINEAR_SCAN(level, code) 457 #endif 458 459 class ComputeLinearScanOrder : public StackObj { 460 private: 461 int _max_block_id; // the highest block_id of a block 462 int _num_blocks; // total number of blocks (smaller than _max_block_id) 463 int _num_loops; // total number of loops 464 bool _iterative_dominators;// method requires iterative computation of dominatiors 465 466 BlockList* _linear_scan_order; // the resulting list of blocks in correct order 467 468 BitMap _visited_blocks; // used for recursive processing of blocks 469 BitMap _active_blocks; // used for recursive processing of blocks 470 BitMap _dominator_blocks; // temproary BitMap used for computation of dominator 471 intArray _forward_branches; // number of incoming forward branches for each block 472 BlockList _loop_end_blocks; // list of all loop end blocks collected during count_edges 473 BitMap2D _loop_map; // two-dimensional bit set: a bit is set if a block is contained in a loop 474 BlockList _work_list; // temporary list (used in mark_loops and compute_order) 475 BlockList _loop_headers; 476 477 Compilation* _compilation; 478 479 // accessors for _visited_blocks and _active_blocks 480 void init_visited() { _active_blocks.clear(); _visited_blocks.clear(); } 481 bool is_visited(BlockBegin* b) const { return _visited_blocks.at(b->block_id()); } 482 bool is_active(BlockBegin* b) const { return _active_blocks.at(b->block_id()); } 483 void set_visited(BlockBegin* b) { assert(!is_visited(b), "already set"); _visited_blocks.set_bit(b->block_id()); } 484 void set_active(BlockBegin* b) { assert(!is_active(b), "already set"); _active_blocks.set_bit(b->block_id()); } 485 void clear_active(BlockBegin* b) { assert(is_active(b), "not already"); _active_blocks.clear_bit(b->block_id()); } 486 487 // accessors for _forward_branches 488 void inc_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) + 1); } 489 int dec_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) - 1); return _forward_branches.at(b->block_id()); } 490 491 // accessors for _loop_map 492 bool is_block_in_loop (int loop_idx, BlockBegin* b) const { return _loop_map.at(loop_idx, b->block_id()); } 493 void set_block_in_loop (int loop_idx, BlockBegin* b) { _loop_map.set_bit(loop_idx, b->block_id()); } 494 void clear_block_in_loop(int loop_idx, int block_id) { _loop_map.clear_bit(loop_idx, block_id); } 495 496 // count edges between blocks 497 void count_edges(BlockBegin* cur, BlockBegin* parent); 498 499 // loop detection 500 void mark_loops(); 501 void clear_non_natural_loops(BlockBegin* start_block); 502 void assign_loop_depth(BlockBegin* start_block); 503 504 // computation of final block order 505 BlockBegin* common_dominator(BlockBegin* a, BlockBegin* b); 506 void compute_dominator(BlockBegin* cur, BlockBegin* parent); 507 int compute_weight(BlockBegin* cur); 508 bool ready_for_processing(BlockBegin* cur); 509 void sort_into_work_list(BlockBegin* b); 510 void append_block(BlockBegin* cur); 511 void compute_order(BlockBegin* start_block); 512 513 // fixup of dominators for non-natural loops 514 bool compute_dominators_iter(); 515 void compute_dominators(); 516 517 // debug functions 518 NOT_PRODUCT(void print_blocks();) 519 DEBUG_ONLY(void verify();) 520 521 Compilation* compilation() const { return _compilation; } 522 public: 523 ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block); 524 525 // accessors for final result 526 BlockList* linear_scan_order() const { return _linear_scan_order; } 527 int num_loops() const { return _num_loops; } 528 }; 529 530 531 ComputeLinearScanOrder::ComputeLinearScanOrder(Compilation* c, BlockBegin* start_block) : 532 _max_block_id(BlockBegin::number_of_blocks()), 533 _num_blocks(0), 534 _num_loops(0), 535 _iterative_dominators(false), 536 _visited_blocks(_max_block_id), 537 _active_blocks(_max_block_id), 538 _dominator_blocks(_max_block_id), 539 _forward_branches(_max_block_id, 0), 540 _loop_end_blocks(8), 541 _work_list(8), 542 _linear_scan_order(NULL), // initialized later with correct size 543 _loop_map(0, 0), // initialized later with correct size 544 _compilation(c) 545 { 546 TRACE_LINEAR_SCAN(2, tty->print_cr("***** computing linear-scan block order")); 547 548 init_visited(); 549 count_edges(start_block, NULL); 550 551 if (compilation()->is_profiling()) { 552 ciMethod *method = compilation()->method(); 553 if (!method->is_accessor()) { 554 ciMethodData* md = method->method_data_or_null(); 555 assert(md != NULL, "Sanity"); 556 md->set_compilation_stats(_num_loops, _num_blocks); 557 } 558 } 559 560 if (_num_loops > 0) { 561 mark_loops(); 562 clear_non_natural_loops(start_block); 563 assign_loop_depth(start_block); 564 } 565 566 compute_order(start_block); 567 compute_dominators(); 568 569 NOT_PRODUCT(print_blocks()); 570 DEBUG_ONLY(verify()); 571 } 572 573 574 // Traverse the CFG: 575 // * count total number of blocks 576 // * count all incoming edges and backward incoming edges 577 // * number loop header blocks 578 // * create a list with all loop end blocks 579 void ComputeLinearScanOrder::count_edges(BlockBegin* cur, BlockBegin* parent) { 580 TRACE_LINEAR_SCAN(3, tty->print_cr("Enter count_edges for block B%d coming from B%d", cur->block_id(), parent != NULL ? parent->block_id() : -1)); 581 assert(cur->dominator() == NULL, "dominator already initialized"); 582 583 if (is_active(cur)) { 584 TRACE_LINEAR_SCAN(3, tty->print_cr("backward branch")); 585 assert(is_visited(cur), "block must be visisted when block is active"); 586 assert(parent != NULL, "must have parent"); 587 588 cur->set(BlockBegin::linear_scan_loop_header_flag); 589 cur->set(BlockBegin::backward_branch_target_flag); 590 591 parent->set(BlockBegin::linear_scan_loop_end_flag); 592 593 // When a loop header is also the start of an exception handler, then the backward branch is 594 // an exception edge. Because such edges are usually critical edges which cannot be split, the 595 // loop must be excluded here from processing. 596 if (cur->is_set(BlockBegin::exception_entry_flag)) { 597 // Make sure that dominators are correct in this weird situation 598 _iterative_dominators = true; 599 return; 600 } 601 assert(parent->number_of_sux() == 1 && parent->sux_at(0) == cur, 602 "loop end blocks must have one successor (critical edges are split)"); 603 604 _loop_end_blocks.append(parent); 605 return; 606 } 607 608 // increment number of incoming forward branches 609 inc_forward_branches(cur); 610 611 if (is_visited(cur)) { 612 TRACE_LINEAR_SCAN(3, tty->print_cr("block already visited")); 613 return; 614 } 615 616 _num_blocks++; 617 set_visited(cur); 618 set_active(cur); 619 620 // recursive call for all successors 621 int i; 622 for (i = cur->number_of_sux() - 1; i >= 0; i--) { 623 count_edges(cur->sux_at(i), cur); 624 } 625 for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) { 626 count_edges(cur->exception_handler_at(i), cur); 627 } 628 629 clear_active(cur); 630 631 // Each loop has a unique number. 632 // When multiple loops are nested, assign_loop_depth assumes that the 633 // innermost loop has the lowest number. This is guaranteed by setting 634 // the loop number after the recursive calls for the successors above 635 // have returned. 636 if (cur->is_set(BlockBegin::linear_scan_loop_header_flag)) { 637 assert(cur->loop_index() == -1, "cannot set loop-index twice"); 638 TRACE_LINEAR_SCAN(3, tty->print_cr("Block B%d is loop header of loop %d", cur->block_id(), _num_loops)); 639 640 cur->set_loop_index(_num_loops); 641 _loop_headers.append(cur); 642 _num_loops++; 643 } 644 645 TRACE_LINEAR_SCAN(3, tty->print_cr("Finished count_edges for block B%d", cur->block_id())); 646 } 647 648 649 void ComputeLinearScanOrder::mark_loops() { 650 TRACE_LINEAR_SCAN(3, tty->print_cr("----- marking loops")); 651 652 _loop_map = BitMap2D(_num_loops, _max_block_id); 653 _loop_map.clear(); 654 655 for (int i = _loop_end_blocks.length() - 1; i >= 0; i--) { 656 BlockBegin* loop_end = _loop_end_blocks.at(i); 657 BlockBegin* loop_start = loop_end->sux_at(0); 658 int loop_idx = loop_start->loop_index(); 659 660 TRACE_LINEAR_SCAN(3, tty->print_cr("Processing loop from B%d to B%d (loop %d):", loop_start->block_id(), loop_end->block_id(), loop_idx)); 661 assert(loop_end->is_set(BlockBegin::linear_scan_loop_end_flag), "loop end flag must be set"); 662 assert(loop_end->number_of_sux() == 1, "incorrect number of successors"); 663 assert(loop_start->is_set(BlockBegin::linear_scan_loop_header_flag), "loop header flag must be set"); 664 assert(loop_idx >= 0 && loop_idx < _num_loops, "loop index not set"); 665 assert(_work_list.is_empty(), "work list must be empty before processing"); 666 667 // add the end-block of the loop to the working list 668 _work_list.push(loop_end); 669 set_block_in_loop(loop_idx, loop_end); 670 do { 671 BlockBegin* cur = _work_list.pop(); 672 673 TRACE_LINEAR_SCAN(3, tty->print_cr(" processing B%d", cur->block_id())); 674 assert(is_block_in_loop(loop_idx, cur), "bit in loop map must be set when block is in work list"); 675 676 // recursive processing of all predecessors ends when start block of loop is reached 677 if (cur != loop_start && !cur->is_set(BlockBegin::osr_entry_flag)) { 678 for (int j = cur->number_of_preds() - 1; j >= 0; j--) { 679 BlockBegin* pred = cur->pred_at(j); 680 681 if (!is_block_in_loop(loop_idx, pred) /*&& !pred->is_set(BlockBeginosr_entry_flag)*/) { 682 // this predecessor has not been processed yet, so add it to work list 683 TRACE_LINEAR_SCAN(3, tty->print_cr(" pushing B%d", pred->block_id())); 684 _work_list.push(pred); 685 set_block_in_loop(loop_idx, pred); 686 } 687 } 688 } 689 } while (!_work_list.is_empty()); 690 } 691 } 692 693 694 // check for non-natural loops (loops where the loop header does not dominate 695 // all other loop blocks = loops with mulitple entries). 696 // such loops are ignored 697 void ComputeLinearScanOrder::clear_non_natural_loops(BlockBegin* start_block) { 698 for (int i = _num_loops - 1; i >= 0; i--) { 699 if (is_block_in_loop(i, start_block)) { 700 // loop i contains the entry block of the method 701 // -> this is not a natural loop, so ignore it 702 TRACE_LINEAR_SCAN(2, tty->print_cr("Loop %d is non-natural, so it is ignored", i)); 703 704 BlockBegin *loop_header = _loop_headers.at(i); 705 assert(loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Must be loop header"); 706 707 for (int j = 0; j < loop_header->number_of_preds(); j++) { 708 BlockBegin *pred = loop_header->pred_at(j); 709 pred->clear(BlockBegin::linear_scan_loop_end_flag); 710 } 711 712 loop_header->clear(BlockBegin::linear_scan_loop_header_flag); 713 714 for (int block_id = _max_block_id - 1; block_id >= 0; block_id--) { 715 clear_block_in_loop(i, block_id); 716 } 717 _iterative_dominators = true; 718 } 719 } 720 } 721 722 void ComputeLinearScanOrder::assign_loop_depth(BlockBegin* start_block) { 723 TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing loop-depth and weight")); 724 init_visited(); 725 726 assert(_work_list.is_empty(), "work list must be empty before processing"); 727 _work_list.append(start_block); 728 729 do { 730 BlockBegin* cur = _work_list.pop(); 731 732 if (!is_visited(cur)) { 733 set_visited(cur); 734 TRACE_LINEAR_SCAN(4, tty->print_cr("Computing loop depth for block B%d", cur->block_id())); 735 736 // compute loop-depth and loop-index for the block 737 assert(cur->loop_depth() == 0, "cannot set loop-depth twice"); 738 int i; 739 int loop_depth = 0; 740 int min_loop_idx = -1; 741 for (i = _num_loops - 1; i >= 0; i--) { 742 if (is_block_in_loop(i, cur)) { 743 loop_depth++; 744 min_loop_idx = i; 745 } 746 } 747 cur->set_loop_depth(loop_depth); 748 cur->set_loop_index(min_loop_idx); 749 750 // append all unvisited successors to work list 751 for (i = cur->number_of_sux() - 1; i >= 0; i--) { 752 _work_list.append(cur->sux_at(i)); 753 } 754 for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) { 755 _work_list.append(cur->exception_handler_at(i)); 756 } 757 } 758 } while (!_work_list.is_empty()); 759 } 760 761 762 BlockBegin* ComputeLinearScanOrder::common_dominator(BlockBegin* a, BlockBegin* b) { 763 assert(a != NULL && b != NULL, "must have input blocks"); 764 765 _dominator_blocks.clear(); 766 while (a != NULL) { 767 _dominator_blocks.set_bit(a->block_id()); 768 assert(a->dominator() != NULL || a == _linear_scan_order->at(0), "dominator must be initialized"); 769 a = a->dominator(); 770 } 771 while (b != NULL && !_dominator_blocks.at(b->block_id())) { 772 assert(b->dominator() != NULL || b == _linear_scan_order->at(0), "dominator must be initialized"); 773 b = b->dominator(); 774 } 775 776 assert(b != NULL, "could not find dominator"); 777 return b; 778 } 779 780 void ComputeLinearScanOrder::compute_dominator(BlockBegin* cur, BlockBegin* parent) { 781 if (cur->dominator() == NULL) { 782 TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: initializing dominator of B%d to B%d", cur->block_id(), parent->block_id())); 783 cur->set_dominator(parent); 784 785 } else if (!(cur->is_set(BlockBegin::linear_scan_loop_header_flag) && parent->is_set(BlockBegin::linear_scan_loop_end_flag))) { 786 TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: computing dominator of B%d: common dominator of B%d and B%d is B%d", cur->block_id(), parent->block_id(), cur->dominator()->block_id(), common_dominator(cur->dominator(), parent)->block_id())); 787 // Does not hold for exception blocks 788 assert(cur->number_of_preds() > 1 || cur->is_set(BlockBegin::exception_entry_flag), ""); 789 cur->set_dominator(common_dominator(cur->dominator(), parent)); 790 } 791 792 // Additional edge to xhandler of all our successors 793 // range check elimination needs that the state at the end of a 794 // block be valid in every block it dominates so cur must dominate 795 // the exception handlers of its successors. 796 int num_cur_xhandler = cur->number_of_exception_handlers(); 797 for (int j = 0; j < num_cur_xhandler; j++) { 798 BlockBegin* xhandler = cur->exception_handler_at(j); 799 compute_dominator(xhandler, parent); 800 } 801 } 802 803 804 int ComputeLinearScanOrder::compute_weight(BlockBegin* cur) { 805 BlockBegin* single_sux = NULL; 806 if (cur->number_of_sux() == 1) { 807 single_sux = cur->sux_at(0); 808 } 809 810 // limit loop-depth to 15 bit (only for security reason, it will never be so big) 811 int weight = (cur->loop_depth() & 0x7FFF) << 16; 812 813 // general macro for short definition of weight flags 814 // the first instance of INC_WEIGHT_IF has the highest priority 815 int cur_bit = 15; 816 #define INC_WEIGHT_IF(condition) if ((condition)) { weight |= (1 << cur_bit); } cur_bit--; 817 818 // this is necessery for the (very rare) case that two successing blocks have 819 // the same loop depth, but a different loop index (can happen for endless loops 820 // with exception handlers) 821 INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_header_flag)); 822 823 // loop end blocks (blocks that end with a backward branch) are added 824 // after all other blocks of the loop. 825 INC_WEIGHT_IF(!cur->is_set(BlockBegin::linear_scan_loop_end_flag)); 826 827 // critical edge split blocks are prefered because than they have a bigger 828 // proability to be completely empty 829 INC_WEIGHT_IF(cur->is_set(BlockBegin::critical_edge_split_flag)); 830 831 // exceptions should not be thrown in normal control flow, so these blocks 832 // are added as late as possible 833 INC_WEIGHT_IF(cur->end()->as_Throw() == NULL && (single_sux == NULL || single_sux->end()->as_Throw() == NULL)); 834 INC_WEIGHT_IF(cur->end()->as_Return() == NULL && (single_sux == NULL || single_sux->end()->as_Return() == NULL)); 835 836 // exceptions handlers are added as late as possible 837 INC_WEIGHT_IF(!cur->is_set(BlockBegin::exception_entry_flag)); 838 839 // guarantee that weight is > 0 840 weight |= 1; 841 842 #undef INC_WEIGHT_IF 843 assert(cur_bit >= 0, "too many flags"); 844 assert(weight > 0, "weight cannot become negative"); 845 846 return weight; 847 } 848 849 bool ComputeLinearScanOrder::ready_for_processing(BlockBegin* cur) { 850 // Discount the edge just traveled. 851 // When the number drops to zero, all forward branches were processed 852 if (dec_forward_branches(cur) != 0) { 853 return false; 854 } 855 856 assert(_linear_scan_order->index_of(cur) == -1, "block already processed (block can be ready only once)"); 857 assert(_work_list.index_of(cur) == -1, "block already in work-list (block can be ready only once)"); 858 return true; 859 } 860 861 void ComputeLinearScanOrder::sort_into_work_list(BlockBegin* cur) { 862 assert(_work_list.index_of(cur) == -1, "block already in work list"); 863 864 int cur_weight = compute_weight(cur); 865 866 // the linear_scan_number is used to cache the weight of a block 867 cur->set_linear_scan_number(cur_weight); 868 869 #ifndef PRODUCT 870 if (StressLinearScan) { 871 _work_list.insert_before(0, cur); 872 return; 873 } 874 #endif 875 876 _work_list.append(NULL); // provide space for new element 877 878 int insert_idx = _work_list.length() - 1; 879 while (insert_idx > 0 && _work_list.at(insert_idx - 1)->linear_scan_number() > cur_weight) { 880 _work_list.at_put(insert_idx, _work_list.at(insert_idx - 1)); 881 insert_idx--; 882 } 883 _work_list.at_put(insert_idx, cur); 884 885 TRACE_LINEAR_SCAN(3, tty->print_cr("Sorted B%d into worklist. new worklist:", cur->block_id())); 886 TRACE_LINEAR_SCAN(3, for (int i = 0; i < _work_list.length(); i++) tty->print_cr("%8d B%2d weight:%6x", i, _work_list.at(i)->block_id(), _work_list.at(i)->linear_scan_number())); 887 888 #ifdef ASSERT 889 for (int i = 0; i < _work_list.length(); i++) { 890 assert(_work_list.at(i)->linear_scan_number() > 0, "weight not set"); 891 assert(i == 0 || _work_list.at(i - 1)->linear_scan_number() <= _work_list.at(i)->linear_scan_number(), "incorrect order in worklist"); 892 } 893 #endif 894 } 895 896 void ComputeLinearScanOrder::append_block(BlockBegin* cur) { 897 TRACE_LINEAR_SCAN(3, tty->print_cr("appending block B%d (weight 0x%6x) to linear-scan order", cur->block_id(), cur->linear_scan_number())); 898 assert(_linear_scan_order->index_of(cur) == -1, "cannot add the same block twice"); 899 900 // currently, the linear scan order and code emit order are equal. 901 // therefore the linear_scan_number and the weight of a block must also 902 // be equal. 903 cur->set_linear_scan_number(_linear_scan_order->length()); 904 _linear_scan_order->append(cur); 905 } 906 907 void ComputeLinearScanOrder::compute_order(BlockBegin* start_block) { 908 TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing final block order")); 909 910 // the start block is always the first block in the linear scan order 911 _linear_scan_order = new BlockList(_num_blocks); 912 append_block(start_block); 913 914 assert(start_block->end()->as_Base() != NULL, "start block must end with Base-instruction"); 915 BlockBegin* std_entry = ((Base*)start_block->end())->std_entry(); 916 BlockBegin* osr_entry = ((Base*)start_block->end())->osr_entry(); 917 918 BlockBegin* sux_of_osr_entry = NULL; 919 if (osr_entry != NULL) { 920 // special handling for osr entry: 921 // ignore the edge between the osr entry and its successor for processing 922 // the osr entry block is added manually below 923 assert(osr_entry->number_of_sux() == 1, "osr entry must have exactly one successor"); 924 assert(osr_entry->sux_at(0)->number_of_preds() >= 2, "sucessor of osr entry must have two predecessors (otherwise it is not present in normal control flow"); 925 926 sux_of_osr_entry = osr_entry->sux_at(0); 927 dec_forward_branches(sux_of_osr_entry); 928 929 compute_dominator(osr_entry, start_block); 930 _iterative_dominators = true; 931 } 932 compute_dominator(std_entry, start_block); 933 934 // start processing with standard entry block 935 assert(_work_list.is_empty(), "list must be empty before processing"); 936 937 if (ready_for_processing(std_entry)) { 938 sort_into_work_list(std_entry); 939 } else { 940 assert(false, "the std_entry must be ready for processing (otherwise, the method has no start block)"); 941 } 942 943 do { 944 BlockBegin* cur = _work_list.pop(); 945 946 if (cur == sux_of_osr_entry) { 947 // the osr entry block is ignored in normal processing, it is never added to the 948 // work list. Instead, it is added as late as possible manually here. 949 append_block(osr_entry); 950 compute_dominator(cur, osr_entry); 951 } 952 append_block(cur); 953 954 int i; 955 int num_sux = cur->number_of_sux(); 956 // changed loop order to get "intuitive" order of if- and else-blocks 957 for (i = 0; i < num_sux; i++) { 958 BlockBegin* sux = cur->sux_at(i); 959 compute_dominator(sux, cur); 960 if (ready_for_processing(sux)) { 961 sort_into_work_list(sux); 962 } 963 } 964 num_sux = cur->number_of_exception_handlers(); 965 for (i = 0; i < num_sux; i++) { 966 BlockBegin* sux = cur->exception_handler_at(i); 967 if (ready_for_processing(sux)) { 968 sort_into_work_list(sux); 969 } 970 } 971 } while (_work_list.length() > 0); 972 } 973 974 975 bool ComputeLinearScanOrder::compute_dominators_iter() { 976 bool changed = false; 977 int num_blocks = _linear_scan_order->length(); 978 979 assert(_linear_scan_order->at(0)->dominator() == NULL, "must not have dominator"); 980 assert(_linear_scan_order->at(0)->number_of_preds() == 0, "must not have predecessors"); 981 for (int i = 1; i < num_blocks; i++) { 982 BlockBegin* block = _linear_scan_order->at(i); 983 984 BlockBegin* dominator = block->pred_at(0); 985 int num_preds = block->number_of_preds(); 986 987 TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: Processing B%d", block->block_id())); 988 989 for (int j = 0; j < num_preds; j++) { 990 991 BlockBegin *pred = block->pred_at(j); 992 TRACE_LINEAR_SCAN(4, tty->print_cr(" DOM: Subrocessing B%d", pred->block_id())); 993 994 if (block->is_set(BlockBegin::exception_entry_flag)) { 995 dominator = common_dominator(dominator, pred); 996 int num_pred_preds = pred->number_of_preds(); 997 for (int k = 0; k < num_pred_preds; k++) { 998 dominator = common_dominator(dominator, pred->pred_at(k)); 999 } 1000 } else { 1001 dominator = common_dominator(dominator, pred); 1002 } 1003 } 1004 1005 if (dominator != block->dominator()) { 1006 TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: updating dominator of B%d from B%d to B%d", block->block_id(), block->dominator()->block_id(), dominator->block_id())); 1007 1008 block->set_dominator(dominator); 1009 changed = true; 1010 } 1011 } 1012 return changed; 1013 } 1014 1015 void ComputeLinearScanOrder::compute_dominators() { 1016 TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing dominators (iterative computation reqired: %d)", _iterative_dominators)); 1017 1018 // iterative computation of dominators is only required for methods with non-natural loops 1019 // and OSR-methods. For all other methods, the dominators computed when generating the 1020 // linear scan block order are correct. 1021 if (_iterative_dominators) { 1022 do { 1023 TRACE_LINEAR_SCAN(1, tty->print_cr("DOM: next iteration of fix-point calculation")); 1024 } while (compute_dominators_iter()); 1025 } 1026 1027 // check that dominators are correct 1028 assert(!compute_dominators_iter(), "fix point not reached"); 1029 1030 // Add Blocks to dominates-Array 1031 int num_blocks = _linear_scan_order->length(); 1032 for (int i = 0; i < num_blocks; i++) { 1033 BlockBegin* block = _linear_scan_order->at(i); 1034 1035 BlockBegin *dom = block->dominator(); 1036 if (dom) { 1037 assert(dom->dominator_depth() != -1, "Dominator must have been visited before"); 1038 dom->dominates()->append(block); 1039 block->set_dominator_depth(dom->dominator_depth() + 1); 1040 } else { 1041 block->set_dominator_depth(0); 1042 } 1043 } 1044 } 1045 1046 1047 #ifndef PRODUCT 1048 void ComputeLinearScanOrder::print_blocks() { 1049 if (TraceLinearScanLevel >= 2) { 1050 tty->print_cr("----- loop information:"); 1051 for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) { 1052 BlockBegin* cur = _linear_scan_order->at(block_idx); 1053 1054 tty->print("%4d: B%2d: ", cur->linear_scan_number(), cur->block_id()); 1055 for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) { 1056 tty->print ("%d ", is_block_in_loop(loop_idx, cur)); 1057 } 1058 tty->print_cr(" -> loop_index: %2d, loop_depth: %2d", cur->loop_index(), cur->loop_depth()); 1059 } 1060 } 1061 1062 if (TraceLinearScanLevel >= 1) { 1063 tty->print_cr("----- linear-scan block order:"); 1064 for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) { 1065 BlockBegin* cur = _linear_scan_order->at(block_idx); 1066 tty->print("%4d: B%2d loop: %2d depth: %2d", cur->linear_scan_number(), cur->block_id(), cur->loop_index(), cur->loop_depth()); 1067 1068 tty->print(cur->is_set(BlockBegin::exception_entry_flag) ? " ex" : " "); 1069 tty->print(cur->is_set(BlockBegin::critical_edge_split_flag) ? " ce" : " "); 1070 tty->print(cur->is_set(BlockBegin::linear_scan_loop_header_flag) ? " lh" : " "); 1071 tty->print(cur->is_set(BlockBegin::linear_scan_loop_end_flag) ? " le" : " "); 1072 1073 if (cur->dominator() != NULL) { 1074 tty->print(" dom: B%d ", cur->dominator()->block_id()); 1075 } else { 1076 tty->print(" dom: NULL "); 1077 } 1078 1079 if (cur->number_of_preds() > 0) { 1080 tty->print(" preds: "); 1081 for (int j = 0; j < cur->number_of_preds(); j++) { 1082 BlockBegin* pred = cur->pred_at(j); 1083 tty->print("B%d ", pred->block_id()); 1084 } 1085 } 1086 if (cur->number_of_sux() > 0) { 1087 tty->print(" sux: "); 1088 for (int j = 0; j < cur->number_of_sux(); j++) { 1089 BlockBegin* sux = cur->sux_at(j); 1090 tty->print("B%d ", sux->block_id()); 1091 } 1092 } 1093 if (cur->number_of_exception_handlers() > 0) { 1094 tty->print(" ex: "); 1095 for (int j = 0; j < cur->number_of_exception_handlers(); j++) { 1096 BlockBegin* ex = cur->exception_handler_at(j); 1097 tty->print("B%d ", ex->block_id()); 1098 } 1099 } 1100 tty->cr(); 1101 } 1102 } 1103 } 1104 #endif 1105 1106 #ifdef ASSERT 1107 void ComputeLinearScanOrder::verify() { 1108 assert(_linear_scan_order->length() == _num_blocks, "wrong number of blocks in list"); 1109 1110 if (StressLinearScan) { 1111 // blocks are scrambled when StressLinearScan is used 1112 return; 1113 } 1114 1115 // check that all successors of a block have a higher linear-scan-number 1116 // and that all predecessors of a block have a lower linear-scan-number 1117 // (only backward branches of loops are ignored) 1118 int i; 1119 for (i = 0; i < _linear_scan_order->length(); i++) { 1120 BlockBegin* cur = _linear_scan_order->at(i); 1121 1122 assert(cur->linear_scan_number() == i, "incorrect linear_scan_number"); 1123 assert(cur->linear_scan_number() >= 0 && cur->linear_scan_number() == _linear_scan_order->index_of(cur), "incorrect linear_scan_number"); 1124 1125 int j; 1126 for (j = cur->number_of_sux() - 1; j >= 0; j--) { 1127 BlockBegin* sux = cur->sux_at(j); 1128 1129 assert(sux->linear_scan_number() >= 0 && sux->linear_scan_number() == _linear_scan_order->index_of(sux), "incorrect linear_scan_number"); 1130 if (!sux->is_set(BlockBegin::backward_branch_target_flag)) { 1131 assert(cur->linear_scan_number() < sux->linear_scan_number(), "invalid order"); 1132 } 1133 if (cur->loop_depth() == sux->loop_depth()) { 1134 assert(cur->loop_index() == sux->loop_index() || sux->is_set(BlockBegin::linear_scan_loop_header_flag), "successing blocks with same loop depth must have same loop index"); 1135 } 1136 } 1137 1138 for (j = cur->number_of_preds() - 1; j >= 0; j--) { 1139 BlockBegin* pred = cur->pred_at(j); 1140 1141 assert(pred->linear_scan_number() >= 0 && pred->linear_scan_number() == _linear_scan_order->index_of(pred), "incorrect linear_scan_number"); 1142 if (!cur->is_set(BlockBegin::backward_branch_target_flag)) { 1143 assert(cur->linear_scan_number() > pred->linear_scan_number(), "invalid order"); 1144 } 1145 if (cur->loop_depth() == pred->loop_depth()) { 1146 assert(cur->loop_index() == pred->loop_index() || cur->is_set(BlockBegin::linear_scan_loop_header_flag), "successing blocks with same loop depth must have same loop index"); 1147 } 1148 1149 assert(cur->dominator()->linear_scan_number() <= cur->pred_at(j)->linear_scan_number(), "dominator must be before predecessors"); 1150 } 1151 1152 // check dominator 1153 if (i == 0) { 1154 assert(cur->dominator() == NULL, "first block has no dominator"); 1155 } else { 1156 assert(cur->dominator() != NULL, "all but first block must have dominator"); 1157 } 1158 // Assertion does not hold for exception handlers 1159 assert(cur->number_of_preds() != 1 || cur->dominator() == cur->pred_at(0) || cur->is_set(BlockBegin::exception_entry_flag), "Single predecessor must also be dominator"); 1160 } 1161 1162 // check that all loops are continuous 1163 for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) { 1164 int block_idx = 0; 1165 assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "the first block must not be present in any loop"); 1166 1167 // skip blocks before the loop 1168 while (block_idx < _num_blocks && !is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) { 1169 block_idx++; 1170 } 1171 // skip blocks of loop 1172 while (block_idx < _num_blocks && is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) { 1173 block_idx++; 1174 } 1175 // after the first non-loop block, there must not be another loop-block 1176 while (block_idx < _num_blocks) { 1177 assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "loop not continuous in linear-scan order"); 1178 block_idx++; 1179 } 1180 } 1181 } 1182 #endif 1183 1184 1185 void IR::compute_code() { 1186 assert(is_valid(), "IR must be valid"); 1187 1188 ComputeLinearScanOrder compute_order(compilation(), start()); 1189 _num_loops = compute_order.num_loops(); 1190 _code = compute_order.linear_scan_order(); 1191 } 1192 1193 1194 void IR::compute_use_counts() { 1195 // make sure all values coming out of this block get evaluated. 1196 int num_blocks = _code->length(); 1197 for (int i = 0; i < num_blocks; i++) { 1198 _code->at(i)->end()->state()->pin_stack_for_linear_scan(); 1199 } 1200 1201 // compute use counts 1202 UseCountComputer::compute(_code); 1203 } 1204 1205 1206 void IR::iterate_preorder(BlockClosure* closure) { 1207 assert(is_valid(), "IR must be valid"); 1208 start()->iterate_preorder(closure); 1209 } 1210 1211 1212 void IR::iterate_postorder(BlockClosure* closure) { 1213 assert(is_valid(), "IR must be valid"); 1214 start()->iterate_postorder(closure); 1215 } 1216 1217 void IR::iterate_linear_scan_order(BlockClosure* closure) { 1218 linear_scan_order()->iterate_forward(closure); 1219 } 1220 1221 1222 #ifndef PRODUCT 1223 class BlockPrinter: public BlockClosure { 1224 private: 1225 InstructionPrinter* _ip; 1226 bool _cfg_only; 1227 bool _live_only; 1228 1229 public: 1230 BlockPrinter(InstructionPrinter* ip, bool cfg_only, bool live_only = false) { 1231 _ip = ip; 1232 _cfg_only = cfg_only; 1233 _live_only = live_only; 1234 } 1235 1236 virtual void block_do(BlockBegin* block) { 1237 if (_cfg_only) { 1238 _ip->print_instr(block); tty->cr(); 1239 } else { 1240 block->print_block(*_ip, _live_only); 1241 } 1242 } 1243 }; 1244 1245 1246 void IR::print(BlockBegin* start, bool cfg_only, bool live_only) { 1247 ttyLocker ttyl; 1248 InstructionPrinter ip(!cfg_only); 1249 BlockPrinter bp(&ip, cfg_only, live_only); 1250 start->iterate_preorder(&bp); 1251 tty->cr(); 1252 } 1253 1254 void IR::print(bool cfg_only, bool live_only) { 1255 if (is_valid()) { 1256 print(start(), cfg_only, live_only); 1257 } else { 1258 tty->print_cr("invalid IR"); 1259 } 1260 } 1261 1262 1263 define_array(BlockListArray, BlockList*) 1264 define_stack(BlockListList, BlockListArray) 1265 1266 class PredecessorValidator : public BlockClosure { 1267 private: 1268 BlockListList* _predecessors; 1269 BlockList* _blocks; 1270 1271 static int cmp(BlockBegin** a, BlockBegin** b) { 1272 return (*a)->block_id() - (*b)->block_id(); 1273 } 1274 1275 public: 1276 PredecessorValidator(IR* hir) { 1277 ResourceMark rm; 1278 _predecessors = new BlockListList(BlockBegin::number_of_blocks(), NULL); 1279 _blocks = new BlockList(); 1280 1281 int i; 1282 hir->start()->iterate_preorder(this); 1283 if (hir->code() != NULL) { 1284 assert(hir->code()->length() == _blocks->length(), "must match"); 1285 for (i = 0; i < _blocks->length(); i++) { 1286 assert(hir->code()->contains(_blocks->at(i)), "should be in both lists"); 1287 } 1288 } 1289 1290 for (i = 0; i < _blocks->length(); i++) { 1291 BlockBegin* block = _blocks->at(i); 1292 BlockList* preds = _predecessors->at(block->block_id()); 1293 if (preds == NULL) { 1294 assert(block->number_of_preds() == 0, "should be the same"); 1295 continue; 1296 } 1297 1298 // clone the pred list so we can mutate it 1299 BlockList* pred_copy = new BlockList(); 1300 int j; 1301 for (j = 0; j < block->number_of_preds(); j++) { 1302 pred_copy->append(block->pred_at(j)); 1303 } 1304 // sort them in the same order 1305 preds->sort(cmp); 1306 pred_copy->sort(cmp); 1307 int length = MIN2(preds->length(), block->number_of_preds()); 1308 for (j = 0; j < block->number_of_preds(); j++) { 1309 assert(preds->at(j) == pred_copy->at(j), "must match"); 1310 } 1311 1312 assert(preds->length() == block->number_of_preds(), "should be the same"); 1313 } 1314 } 1315 1316 virtual void block_do(BlockBegin* block) { 1317 _blocks->append(block); 1318 BlockEnd* be = block->end(); 1319 int n = be->number_of_sux(); 1320 int i; 1321 for (i = 0; i < n; i++) { 1322 BlockBegin* sux = be->sux_at(i); 1323 assert(!sux->is_set(BlockBegin::exception_entry_flag), "must not be xhandler"); 1324 1325 BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL); 1326 if (preds == NULL) { 1327 preds = new BlockList(); 1328 _predecessors->at_put(sux->block_id(), preds); 1329 } 1330 preds->append(block); 1331 } 1332 1333 n = block->number_of_exception_handlers(); 1334 for (i = 0; i < n; i++) { 1335 BlockBegin* sux = block->exception_handler_at(i); 1336 assert(sux->is_set(BlockBegin::exception_entry_flag), "must be xhandler"); 1337 1338 BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL); 1339 if (preds == NULL) { 1340 preds = new BlockList(); 1341 _predecessors->at_put(sux->block_id(), preds); 1342 } 1343 preds->append(block); 1344 } 1345 } 1346 }; 1347 1348 class VerifyBlockBeginField : public BlockClosure { 1349 1350 public: 1351 1352 virtual void block_do(BlockBegin *block) { 1353 for ( Instruction *cur = block; cur != NULL; cur = cur->next()) { 1354 assert(cur->block() == block, "Block begin is not correct"); 1355 } 1356 } 1357 }; 1358 1359 void IR::verify() { 1360 #ifdef ASSERT 1361 PredecessorValidator pv(this); 1362 VerifyBlockBeginField verifier; 1363 this->iterate_postorder(&verifier); 1364 #endif 1365 } 1366 1367 #endif // PRODUCT 1368 1369 void SubstitutionResolver::visit(Value* v) { 1370 Value v0 = *v; 1371 if (v0) { 1372 Value vs = v0->subst(); 1373 if (vs != v0) { 1374 *v = v0->subst(); 1375 } 1376 } 1377 } 1378 1379 #ifdef ASSERT 1380 class SubstitutionChecker: public ValueVisitor { 1381 void visit(Value* v) { 1382 Value v0 = *v; 1383 if (v0) { 1384 Value vs = v0->subst(); 1385 assert(vs == v0, "missed substitution"); 1386 } 1387 } 1388 }; 1389 #endif 1390 1391 1392 void SubstitutionResolver::block_do(BlockBegin* block) { 1393 Instruction* last = NULL; 1394 for (Instruction* n = block; n != NULL;) { 1395 n->values_do(this); 1396 // need to remove this instruction from the instruction stream 1397 if (n->subst() != n) { 1398 assert(last != NULL, "must have last"); 1399 last->set_next(n->next()); 1400 } else { 1401 last = n; 1402 } 1403 n = last->next(); 1404 } 1405 1406 #ifdef ASSERT 1407 SubstitutionChecker check_substitute; 1408 if (block->state()) block->state()->values_do(&check_substitute); 1409 block->block_values_do(&check_substitute); 1410 if (block->end() && block->end()->state()) block->end()->state()->values_do(&check_substitute); 1411 #endif 1412 }