src/share/vm/c1/c1_IR.cpp

Print this page
rev 4136 : 7153771: array bound check elimination for c1
Summary: when possible optimize out array bound checks, inserting predicates when needed.
Reviewed-by:


 165     callee_max = MAX2(callee_max, callee_no(i)->max_stack());
 166   }
 167   return my_max + callee_max;
 168 }
 169 
 170 
 171 bool IRScopeDebugInfo::should_reexecute() {
 172   ciMethod* cur_method = scope()->method();
 173   int       cur_bci    = bci();
 174   if (cur_method != NULL && cur_bci != SynchronizationEntryBCI) {
 175     Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci);
 176     return Interpreter::bytecode_should_reexecute(code);
 177   } else
 178     return false;
 179 }
 180 
 181 
 182 // Implementation of CodeEmitInfo
 183 
 184 // Stack must be NON-null
 185 CodeEmitInfo::CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers)
 186   : _scope(stack->scope())
 187   , _scope_debug_info(NULL)
 188   , _oop_map(NULL)
 189   , _stack(stack)
 190   , _exception_handlers(exception_handlers)
 191   , _is_method_handle_invoke(false) {

 192   assert(_stack != NULL, "must be non null");
 193 }
 194 
 195 
 196 CodeEmitInfo::CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack)
 197   : _scope(info->_scope)
 198   , _exception_handlers(NULL)
 199   , _scope_debug_info(NULL)
 200   , _oop_map(NULL)
 201   , _stack(stack == NULL ? info->_stack : stack)
 202   , _is_method_handle_invoke(info->_is_method_handle_invoke) {

 203 
 204   // deep copy of exception handlers
 205   if (info->_exception_handlers != NULL) {
 206     _exception_handlers = new XHandlers(info->_exception_handlers);
 207   }
 208 }
 209 
 210 
 211 void CodeEmitInfo::record_debug_info(DebugInformationRecorder* recorder, int pc_offset) {
 212   // record the safepoint before recording the debug info for enclosing scopes
 213   recorder->add_safepoint(pc_offset, _oop_map->deep_copy());
 214   _scope_debug_info->record_debug_info(recorder, pc_offset, true/*topmost*/, _is_method_handle_invoke);
 215   recorder->end_safepoint(pc_offset);
 216 }
 217 
 218 
 219 void CodeEmitInfo::add_register_oop(LIR_Opr opr) {
 220   assert(_oop_map != NULL, "oop map must already exist");
 221   assert(opr->is_single_cpu(), "should not call otherwise");
 222 
 223   VMReg name = frame_map()->regname(opr);
 224   _oop_map->set_oop(name);
 225 }
 226 
 227 
 228 
 229 
 230 // Implementation of IR
 231 
 232 IR::IR(Compilation* compilation, ciMethod* method, int osr_bci) :
 233     _locals_size(in_WordSize(-1))
 234   , _num_loops(0) {
 235   // setup IR fields
 236   _compilation = compilation;
 237   _top_scope   = new IRScope(compilation, NULL, -1, method, osr_bci, true);
 238   _code        = NULL;
 239 }
 240 
 241 
 242 void IR::optimize() {
 243   Optimizer opt(this);
 244   if (!compilation()->profile_branches()) {
 245     if (DoCEE) {
 246       opt.eliminate_conditional_expressions();
 247 #ifndef PRODUCT
 248       if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after CEE"); print(true); }
 249       if (PrintIR  || PrintIR1 ) { tty->print_cr("IR after CEE"); print(false); }
 250 #endif
 251     }
 252     if (EliminateBlocks) {
 253       opt.eliminate_blocks();
 254 #ifndef PRODUCT
 255       if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after block elimination"); print(true); }
 256       if (PrintIR  || PrintIR1 ) { tty->print_cr("IR after block elimination"); print(false); }
 257 #endif
 258     }
 259   }




 260   if (EliminateNullChecks) {
 261     opt.eliminate_null_checks();
 262 #ifndef PRODUCT
 263     if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after null check elimination"); print(true); }
 264     if (PrintIR  || PrintIR1 ) { tty->print_cr("IR after null check elimination"); print(false); }
 265 #endif
 266   }
 267 }
 268 
 269 
 270 static int sort_pairs(BlockPair** a, BlockPair** b) {
 271   if ((*a)->from() == (*b)->from()) {
 272     return (*a)->to()->block_id() - (*b)->to()->block_id();
 273   } else {
 274     return (*a)->from()->block_id() - (*b)->from()->block_id();
 275   }
 276 }
 277 
 278 
 279 class CriticalEdgeFinder: public BlockClosure {


 412 #else
 413   #define TRACE_LINEAR_SCAN(level, code)
 414 #endif
 415 
 416 class ComputeLinearScanOrder : public StackObj {
 417  private:
 418   int        _max_block_id;        // the highest block_id of a block
 419   int        _num_blocks;          // total number of blocks (smaller than _max_block_id)
 420   int        _num_loops;           // total number of loops
 421   bool       _iterative_dominators;// method requires iterative computation of dominatiors
 422 
 423   BlockList* _linear_scan_order;   // the resulting list of blocks in correct order
 424 
 425   BitMap     _visited_blocks;      // used for recursive processing of blocks
 426   BitMap     _active_blocks;       // used for recursive processing of blocks
 427   BitMap     _dominator_blocks;    // temproary BitMap used for computation of dominator
 428   intArray   _forward_branches;    // number of incoming forward branches for each block
 429   BlockList  _loop_end_blocks;     // list of all loop end blocks collected during count_edges
 430   BitMap2D   _loop_map;            // two-dimensional bit set: a bit is set if a block is contained in a loop
 431   BlockList  _work_list;           // temporary list (used in mark_loops and compute_order)

 432 
 433   Compilation* _compilation;
 434 
 435   // accessors for _visited_blocks and _active_blocks
 436   void init_visited()                     { _active_blocks.clear(); _visited_blocks.clear(); }
 437   bool is_visited(BlockBegin* b) const    { return _visited_blocks.at(b->block_id()); }
 438   bool is_active(BlockBegin* b) const     { return _active_blocks.at(b->block_id()); }
 439   void set_visited(BlockBegin* b)         { assert(!is_visited(b), "already set"); _visited_blocks.set_bit(b->block_id()); }
 440   void set_active(BlockBegin* b)          { assert(!is_active(b), "already set");  _active_blocks.set_bit(b->block_id()); }
 441   void clear_active(BlockBegin* b)        { assert(is_active(b), "not already");   _active_blocks.clear_bit(b->block_id()); }
 442 
 443   // accessors for _forward_branches
 444   void inc_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) + 1); }
 445   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()); }
 446 
 447   // accessors for _loop_map
 448   bool is_block_in_loop   (int loop_idx, BlockBegin* b) const { return _loop_map.at(loop_idx, b->block_id()); }
 449   void set_block_in_loop  (int loop_idx, BlockBegin* b)       { _loop_map.set_bit(loop_idx, b->block_id()); }
 450   void clear_block_in_loop(int loop_idx, int block_id)        { _loop_map.clear_bit(loop_idx, block_id); }
 451 


 577   int i;
 578   for (i = cur->number_of_sux() - 1; i >= 0; i--) {
 579     count_edges(cur->sux_at(i), cur);
 580   }
 581   for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) {
 582     count_edges(cur->exception_handler_at(i), cur);
 583   }
 584 
 585   clear_active(cur);
 586 
 587   // Each loop has a unique number.
 588   // When multiple loops are nested, assign_loop_depth assumes that the
 589   // innermost loop has the lowest number. This is guaranteed by setting
 590   // the loop number after the recursive calls for the successors above
 591   // have returned.
 592   if (cur->is_set(BlockBegin::linear_scan_loop_header_flag)) {
 593     assert(cur->loop_index() == -1, "cannot set loop-index twice");
 594     TRACE_LINEAR_SCAN(3, tty->print_cr("Block B%d is loop header of loop %d", cur->block_id(), _num_loops));
 595 
 596     cur->set_loop_index(_num_loops);

 597     _num_loops++;
 598   }
 599 
 600   TRACE_LINEAR_SCAN(3, tty->print_cr("Finished count_edges for block B%d", cur->block_id()));
 601 }
 602 
 603 
 604 void ComputeLinearScanOrder::mark_loops() {
 605   TRACE_LINEAR_SCAN(3, tty->print_cr("----- marking loops"));
 606 
 607   _loop_map = BitMap2D(_num_loops, _max_block_id);
 608   _loop_map.clear();
 609 
 610   for (int i = _loop_end_blocks.length() - 1; i >= 0; i--) {
 611     BlockBegin* loop_end   = _loop_end_blocks.at(i);
 612     BlockBegin* loop_start = loop_end->sux_at(0);
 613     int         loop_idx   = loop_start->loop_index();
 614 
 615     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));
 616     assert(loop_end->is_set(BlockBegin::linear_scan_loop_end_flag), "loop end flag must be set");


 639             _work_list.push(pred);
 640             set_block_in_loop(loop_idx, pred);
 641           }
 642         }
 643       }
 644     } while (!_work_list.is_empty());
 645   }
 646 }
 647 
 648 
 649 // check for non-natural loops (loops where the loop header does not dominate
 650 // all other loop blocks = loops with mulitple entries).
 651 // such loops are ignored
 652 void ComputeLinearScanOrder::clear_non_natural_loops(BlockBegin* start_block) {
 653   for (int i = _num_loops - 1; i >= 0; i--) {
 654     if (is_block_in_loop(i, start_block)) {
 655       // loop i contains the entry block of the method
 656       // -> this is not a natural loop, so ignore it
 657       TRACE_LINEAR_SCAN(2, tty->print_cr("Loop %d is non-natural, so it is ignored", i));
 658 











 659       for (int block_id = _max_block_id - 1; block_id >= 0; block_id--) {
 660         clear_block_in_loop(i, block_id);
 661       }
 662       _iterative_dominators = true;
 663     }
 664   }
 665 }
 666 
 667 void ComputeLinearScanOrder::assign_loop_depth(BlockBegin* start_block) {
 668   TRACE_LINEAR_SCAN(3, "----- computing loop-depth and weight");
 669   init_visited();
 670 
 671   assert(_work_list.is_empty(), "work list must be empty before processing");
 672   _work_list.append(start_block);
 673 
 674   do {
 675     BlockBegin* cur = _work_list.pop();
 676 
 677     if (!is_visited(cur)) {
 678       set_visited(cur);


 712     _dominator_blocks.set_bit(a->block_id());
 713     assert(a->dominator() != NULL || a == _linear_scan_order->at(0), "dominator must be initialized");
 714     a = a->dominator();
 715   }
 716   while (b != NULL && !_dominator_blocks.at(b->block_id())) {
 717     assert(b->dominator() != NULL || b == _linear_scan_order->at(0), "dominator must be initialized");
 718     b = b->dominator();
 719   }
 720 
 721   assert(b != NULL, "could not find dominator");
 722   return b;
 723 }
 724 
 725 void ComputeLinearScanOrder::compute_dominator(BlockBegin* cur, BlockBegin* parent) {
 726   if (cur->dominator() == NULL) {
 727     TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: initializing dominator of B%d to B%d", cur->block_id(), parent->block_id()));
 728     cur->set_dominator(parent);
 729 
 730   } else if (!(cur->is_set(BlockBegin::linear_scan_loop_header_flag) && parent->is_set(BlockBegin::linear_scan_loop_end_flag))) {
 731     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()));
 732     assert(cur->number_of_preds() > 1, "");

 733     cur->set_dominator(common_dominator(cur->dominator(), parent));
 734   }










 735 }
 736 
 737 
 738 int ComputeLinearScanOrder::compute_weight(BlockBegin* cur) {
 739   BlockBegin* single_sux = NULL;
 740   if (cur->number_of_sux() == 1) {
 741     single_sux = cur->sux_at(0);
 742   }
 743 
 744   // limit loop-depth to 15 bit (only for security reason, it will never be so big)
 745   int weight = (cur->loop_depth() & 0x7FFF) << 16;
 746 
 747   // general macro for short definition of weight flags
 748   // the first instance of INC_WEIGHT_IF has the highest priority
 749   int cur_bit = 15;
 750   #define INC_WEIGHT_IF(condition) if ((condition)) { weight |= (1 << cur_bit); } cur_bit--;
 751 
 752   // this is necessery for the (very rare) case that two successing blocks have
 753   // the same loop depth, but a different loop index (can happen for endless loops
 754   // with exception handlers)


 881       // the osr entry block is ignored in normal processing, it is never added to the
 882       // work list. Instead, it is added as late as possible manually here.
 883       append_block(osr_entry);
 884       compute_dominator(cur, osr_entry);
 885     }
 886     append_block(cur);
 887 
 888     int i;
 889     int num_sux = cur->number_of_sux();
 890     // changed loop order to get "intuitive" order of if- and else-blocks
 891     for (i = 0; i < num_sux; i++) {
 892       BlockBegin* sux = cur->sux_at(i);
 893       compute_dominator(sux, cur);
 894       if (ready_for_processing(sux)) {
 895         sort_into_work_list(sux);
 896       }
 897     }
 898     num_sux = cur->number_of_exception_handlers();
 899     for (i = 0; i < num_sux; i++) {
 900       BlockBegin* sux = cur->exception_handler_at(i);
 901       compute_dominator(sux, cur);
 902       if (ready_for_processing(sux)) {
 903         sort_into_work_list(sux);
 904       }
 905     }
 906   } while (_work_list.length() > 0);
 907 }
 908 
 909 
 910 bool ComputeLinearScanOrder::compute_dominators_iter() {
 911   bool changed = false;
 912   int num_blocks = _linear_scan_order->length();
 913 
 914   assert(_linear_scan_order->at(0)->dominator() == NULL, "must not have dominator");
 915   assert(_linear_scan_order->at(0)->number_of_preds() == 0, "must not have predecessors");
 916   for (int i = 1; i < num_blocks; i++) {
 917     BlockBegin* block = _linear_scan_order->at(i);
 918 
 919     BlockBegin* dominator = block->pred_at(0);
 920     int num_preds = block->number_of_preds();
 921     for (int i = 1; i < num_preds; i++) {
 922       dominator = common_dominator(dominator, block->pred_at(i));















 923     }
 924 
 925     if (dominator != block->dominator()) {
 926       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()));
 927 
 928       block->set_dominator(dominator);
 929       changed = true;
 930     }
 931   }
 932   return changed;
 933 }
 934 
 935 void ComputeLinearScanOrder::compute_dominators() {
 936   TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing dominators (iterative computation reqired: %d)", _iterative_dominators));
 937 
 938   // iterative computation of dominators is only required for methods with non-natural loops
 939   // and OSR-methods. For all other methods, the dominators computed when generating the
 940   // linear scan block order are correct.
 941   if (_iterative_dominators) {
 942     do {
 943       TRACE_LINEAR_SCAN(1, tty->print_cr("DOM: next iteration of fix-point calculation"));
 944     } while (compute_dominators_iter());
 945   }
 946 
 947   // check that dominators are correct
 948   assert(!compute_dominators_iter(), "fix point not reached");















 949 }
 950 
 951 
 952 #ifndef PRODUCT
 953 void ComputeLinearScanOrder::print_blocks() {
 954   if (TraceLinearScanLevel >= 2) {
 955     tty->print_cr("----- loop information:");
 956     for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) {
 957       BlockBegin* cur = _linear_scan_order->at(block_idx);
 958 
 959       tty->print("%4d: B%2d: ", cur->linear_scan_number(), cur->block_id());
 960       for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {
 961         tty->print ("%d ", is_block_in_loop(loop_idx, cur));
 962       }
 963       tty->print_cr(" -> loop_index: %2d, loop_depth: %2d", cur->loop_index(), cur->loop_depth());
 964     }
 965   }
 966 
 967   if (TraceLinearScanLevel >= 1) {
 968     tty->print_cr("----- linear-scan block order:");


1015   if (StressLinearScan) {
1016     // blocks are scrambled when StressLinearScan is used
1017     return;
1018   }
1019 
1020   // check that all successors of a block have a higher linear-scan-number
1021   // and that all predecessors of a block have a lower linear-scan-number
1022   // (only backward branches of loops are ignored)
1023   int i;
1024   for (i = 0; i < _linear_scan_order->length(); i++) {
1025     BlockBegin* cur = _linear_scan_order->at(i);
1026 
1027     assert(cur->linear_scan_number() == i, "incorrect linear_scan_number");
1028     assert(cur->linear_scan_number() >= 0 && cur->linear_scan_number() == _linear_scan_order->index_of(cur), "incorrect linear_scan_number");
1029 
1030     int j;
1031     for (j = cur->number_of_sux() - 1; j >= 0; j--) {
1032       BlockBegin* sux = cur->sux_at(j);
1033 
1034       assert(sux->linear_scan_number() >= 0 && sux->linear_scan_number() == _linear_scan_order->index_of(sux), "incorrect linear_scan_number");
1035       if (!cur->is_set(BlockBegin::linear_scan_loop_end_flag)) {
1036         assert(cur->linear_scan_number() < sux->linear_scan_number(), "invalid order");
1037       }
1038       if (cur->loop_depth() == sux->loop_depth()) {
1039         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");
1040       }
1041     }
1042 
1043     for (j = cur->number_of_preds() - 1; j >= 0; j--) {
1044       BlockBegin* pred = cur->pred_at(j);
1045 
1046       assert(pred->linear_scan_number() >= 0 && pred->linear_scan_number() == _linear_scan_order->index_of(pred), "incorrect linear_scan_number");
1047       if (!cur->is_set(BlockBegin::linear_scan_loop_header_flag)) {
1048         assert(cur->linear_scan_number() > pred->linear_scan_number(), "invalid order");
1049       }
1050       if (cur->loop_depth() == pred->loop_depth()) {
1051         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");
1052       }
1053 
1054       assert(cur->dominator()->linear_scan_number() <= cur->pred_at(j)->linear_scan_number(), "dominator must be before predecessors");
1055     }
1056 
1057     // check dominator
1058     if (i == 0) {
1059       assert(cur->dominator() == NULL, "first block has no dominator");
1060     } else {
1061       assert(cur->dominator() != NULL, "all but first block must have dominator");
1062     }
1063     assert(cur->number_of_preds() != 1 || cur->dominator() == cur->pred_at(0), "Single predecessor must also be dominator");

1064   }
1065 
1066   // check that all loops are continuous
1067   for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {
1068     int block_idx = 0;
1069     assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "the first block must not be present in any loop");
1070 
1071     // skip blocks before the loop
1072     while (block_idx < _num_blocks && !is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {
1073       block_idx++;
1074     }
1075     // skip blocks of loop
1076     while (block_idx < _num_blocks && is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {
1077       block_idx++;
1078     }
1079     // after the first non-loop block, there must not be another loop-block
1080     while (block_idx < _num_blocks) {
1081       assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "loop not continuous in linear-scan order");
1082       block_idx++;
1083     }


1232         _predecessors->at_put(sux->block_id(), preds);
1233       }
1234       preds->append(block);
1235     }
1236 
1237     n = block->number_of_exception_handlers();
1238     for (i = 0; i < n; i++) {
1239       BlockBegin* sux = block->exception_handler_at(i);
1240       assert(sux->is_set(BlockBegin::exception_entry_flag), "must be xhandler");
1241 
1242       BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL);
1243       if (preds == NULL) {
1244         preds = new BlockList();
1245         _predecessors->at_put(sux->block_id(), preds);
1246       }
1247       preds->append(block);
1248     }
1249   }
1250 };
1251 














1252 void IR::verify() {
1253 #ifdef ASSERT
1254   PredecessorValidator pv(this);


1255 #endif
1256 }
1257 
1258 #endif // PRODUCT
1259 
1260 void SubstitutionResolver::visit(Value* v) {
1261   Value v0 = *v;
1262   if (v0) {
1263     Value vs = v0->subst();
1264     if (vs != v0) {
1265       *v = v0->subst();
1266     }
1267   }
1268 }
1269 
1270 #ifdef ASSERT
1271 class SubstitutionChecker: public ValueVisitor {
1272   void visit(Value* v) {
1273     Value v0 = *v;
1274     if (v0) {




 165     callee_max = MAX2(callee_max, callee_no(i)->max_stack());
 166   }
 167   return my_max + callee_max;
 168 }
 169 
 170 
 171 bool IRScopeDebugInfo::should_reexecute() {
 172   ciMethod* cur_method = scope()->method();
 173   int       cur_bci    = bci();
 174   if (cur_method != NULL && cur_bci != SynchronizationEntryBCI) {
 175     Bytecodes::Code code = cur_method->java_code_at_bci(cur_bci);
 176     return Interpreter::bytecode_should_reexecute(code);
 177   } else
 178     return false;
 179 }
 180 
 181 
 182 // Implementation of CodeEmitInfo
 183 
 184 // Stack must be NON-null
 185 CodeEmitInfo::CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers, bool deoptimize_on_exception)
 186   : _scope(stack->scope())
 187   , _scope_debug_info(NULL)
 188   , _oop_map(NULL)
 189   , _stack(stack)
 190   , _exception_handlers(exception_handlers)
 191   , _is_method_handle_invoke(false) 
 192   , _deoptimize_on_exception(deoptimize_on_exception) {
 193   assert(_stack != NULL, "must be non null");
 194 }
 195 
 196 
 197 CodeEmitInfo::CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack)
 198   : _scope(info->_scope)
 199   , _exception_handlers(NULL)
 200   , _scope_debug_info(NULL)
 201   , _oop_map(NULL)
 202   , _stack(stack == NULL ? info->_stack : stack)
 203   , _is_method_handle_invoke(info->_is_method_handle_invoke)
 204   , _deoptimize_on_exception(info->_deoptimize_on_exception) {
 205 
 206   // deep copy of exception handlers
 207   if (info->_exception_handlers != NULL) {
 208     _exception_handlers = new XHandlers(info->_exception_handlers);
 209   }
 210 }
 211 
 212 
 213 void CodeEmitInfo::record_debug_info(DebugInformationRecorder* recorder, int pc_offset) {
 214   // record the safepoint before recording the debug info for enclosing scopes
 215   recorder->add_safepoint(pc_offset, _oop_map->deep_copy());
 216   _scope_debug_info->record_debug_info(recorder, pc_offset, true/*topmost*/, _is_method_handle_invoke);
 217   recorder->end_safepoint(pc_offset);
 218 }
 219 
 220 
 221 void CodeEmitInfo::add_register_oop(LIR_Opr opr) {
 222   assert(_oop_map != NULL, "oop map must already exist");
 223   assert(opr->is_single_cpu(), "should not call otherwise");
 224 
 225   VMReg name = frame_map()->regname(opr);
 226   _oop_map->set_oop(name);
 227 }
 228 
 229 
 230 
 231 
 232 // Implementation of IR
 233 
 234 IR::IR(Compilation* compilation, ciMethod* method, int osr_bci) :
 235     _locals_size(in_WordSize(-1))
 236   , _num_loops(0) {
 237   // setup IR fields
 238   _compilation = compilation;
 239   _top_scope   = new IRScope(compilation, NULL, -1, method, osr_bci, true);
 240   _code        = NULL;
 241 }
 242 
 243 
 244 void IR::optimize_blocks() {
 245   Optimizer opt(this);
 246   if (!compilation()->profile_branches()) {
 247     if (DoCEE) {
 248       opt.eliminate_conditional_expressions();
 249 #ifndef PRODUCT
 250       if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after CEE"); print(true); }
 251       if (PrintIR  || PrintIR1 ) { tty->print_cr("IR after CEE"); print(false); }
 252 #endif
 253     }
 254     if (EliminateBlocks) {
 255       opt.eliminate_blocks();
 256 #ifndef PRODUCT
 257       if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after block elimination"); print(true); }
 258       if (PrintIR  || PrintIR1 ) { tty->print_cr("IR after block elimination"); print(false); }
 259 #endif
 260     }
 261   }
 262 }
 263 
 264 void IR::eliminate_null_checks() {
 265   Optimizer opt(this);
 266   if (EliminateNullChecks) {
 267     opt.eliminate_null_checks();
 268 #ifndef PRODUCT
 269     if (PrintCFG || PrintCFG1) { tty->print_cr("CFG after null check elimination"); print(true); }
 270     if (PrintIR  || PrintIR1 ) { tty->print_cr("IR after null check elimination"); print(false); }
 271 #endif
 272   }
 273 }
 274 
 275 
 276 static int sort_pairs(BlockPair** a, BlockPair** b) {
 277   if ((*a)->from() == (*b)->from()) {
 278     return (*a)->to()->block_id() - (*b)->to()->block_id();
 279   } else {
 280     return (*a)->from()->block_id() - (*b)->from()->block_id();
 281   }
 282 }
 283 
 284 
 285 class CriticalEdgeFinder: public BlockClosure {


 418 #else
 419   #define TRACE_LINEAR_SCAN(level, code)
 420 #endif
 421 
 422 class ComputeLinearScanOrder : public StackObj {
 423  private:
 424   int        _max_block_id;        // the highest block_id of a block
 425   int        _num_blocks;          // total number of blocks (smaller than _max_block_id)
 426   int        _num_loops;           // total number of loops
 427   bool       _iterative_dominators;// method requires iterative computation of dominatiors
 428 
 429   BlockList* _linear_scan_order;   // the resulting list of blocks in correct order
 430 
 431   BitMap     _visited_blocks;      // used for recursive processing of blocks
 432   BitMap     _active_blocks;       // used for recursive processing of blocks
 433   BitMap     _dominator_blocks;    // temproary BitMap used for computation of dominator
 434   intArray   _forward_branches;    // number of incoming forward branches for each block
 435   BlockList  _loop_end_blocks;     // list of all loop end blocks collected during count_edges
 436   BitMap2D   _loop_map;            // two-dimensional bit set: a bit is set if a block is contained in a loop
 437   BlockList  _work_list;           // temporary list (used in mark_loops and compute_order)
 438   BlockList  _loop_headers;
 439 
 440   Compilation* _compilation;
 441 
 442   // accessors for _visited_blocks and _active_blocks
 443   void init_visited()                     { _active_blocks.clear(); _visited_blocks.clear(); }
 444   bool is_visited(BlockBegin* b) const    { return _visited_blocks.at(b->block_id()); }
 445   bool is_active(BlockBegin* b) const     { return _active_blocks.at(b->block_id()); }
 446   void set_visited(BlockBegin* b)         { assert(!is_visited(b), "already set"); _visited_blocks.set_bit(b->block_id()); }
 447   void set_active(BlockBegin* b)          { assert(!is_active(b), "already set");  _active_blocks.set_bit(b->block_id()); }
 448   void clear_active(BlockBegin* b)        { assert(is_active(b), "not already");   _active_blocks.clear_bit(b->block_id()); }
 449 
 450   // accessors for _forward_branches
 451   void inc_forward_branches(BlockBegin* b) { _forward_branches.at_put(b->block_id(), _forward_branches.at(b->block_id()) + 1); }
 452   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()); }
 453 
 454   // accessors for _loop_map
 455   bool is_block_in_loop   (int loop_idx, BlockBegin* b) const { return _loop_map.at(loop_idx, b->block_id()); }
 456   void set_block_in_loop  (int loop_idx, BlockBegin* b)       { _loop_map.set_bit(loop_idx, b->block_id()); }
 457   void clear_block_in_loop(int loop_idx, int block_id)        { _loop_map.clear_bit(loop_idx, block_id); }
 458 


 584   int i;
 585   for (i = cur->number_of_sux() - 1; i >= 0; i--) {
 586     count_edges(cur->sux_at(i), cur);
 587   }
 588   for (i = cur->number_of_exception_handlers() - 1; i >= 0; i--) {
 589     count_edges(cur->exception_handler_at(i), cur);
 590   }
 591 
 592   clear_active(cur);
 593 
 594   // Each loop has a unique number.
 595   // When multiple loops are nested, assign_loop_depth assumes that the
 596   // innermost loop has the lowest number. This is guaranteed by setting
 597   // the loop number after the recursive calls for the successors above
 598   // have returned.
 599   if (cur->is_set(BlockBegin::linear_scan_loop_header_flag)) {
 600     assert(cur->loop_index() == -1, "cannot set loop-index twice");
 601     TRACE_LINEAR_SCAN(3, tty->print_cr("Block B%d is loop header of loop %d", cur->block_id(), _num_loops));
 602 
 603     cur->set_loop_index(_num_loops);
 604     _loop_headers.append(cur);
 605     _num_loops++;
 606   }
 607 
 608   TRACE_LINEAR_SCAN(3, tty->print_cr("Finished count_edges for block B%d", cur->block_id()));
 609 }
 610 
 611 
 612 void ComputeLinearScanOrder::mark_loops() {
 613   TRACE_LINEAR_SCAN(3, tty->print_cr("----- marking loops"));
 614 
 615   _loop_map = BitMap2D(_num_loops, _max_block_id);
 616   _loop_map.clear();
 617 
 618   for (int i = _loop_end_blocks.length() - 1; i >= 0; i--) {
 619     BlockBegin* loop_end   = _loop_end_blocks.at(i);
 620     BlockBegin* loop_start = loop_end->sux_at(0);
 621     int         loop_idx   = loop_start->loop_index();
 622 
 623     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));
 624     assert(loop_end->is_set(BlockBegin::linear_scan_loop_end_flag), "loop end flag must be set");


 647             _work_list.push(pred);
 648             set_block_in_loop(loop_idx, pred);
 649           }
 650         }
 651       }
 652     } while (!_work_list.is_empty());
 653   }
 654 }
 655 
 656 
 657 // check for non-natural loops (loops where the loop header does not dominate
 658 // all other loop blocks = loops with mulitple entries).
 659 // such loops are ignored
 660 void ComputeLinearScanOrder::clear_non_natural_loops(BlockBegin* start_block) {
 661   for (int i = _num_loops - 1; i >= 0; i--) {
 662     if (is_block_in_loop(i, start_block)) {
 663       // loop i contains the entry block of the method
 664       // -> this is not a natural loop, so ignore it
 665       TRACE_LINEAR_SCAN(2, tty->print_cr("Loop %d is non-natural, so it is ignored", i));
 666 
 667       BlockBegin *loop_header = _loop_headers.at(i);
 668       assert(loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Must be loop header");
 669 
 670       for (int j = 0; j < loop_header->number_of_preds(); j++)
 671       {
 672         BlockBegin *pred = loop_header->pred_at(j);
 673         pred->clear(BlockBegin::linear_scan_loop_end_flag);
 674       }
 675 
 676       loop_header->clear(BlockBegin::linear_scan_loop_header_flag);
 677 
 678       for (int block_id = _max_block_id - 1; block_id >= 0; block_id--) {
 679         clear_block_in_loop(i, block_id);
 680       }
 681       _iterative_dominators = true;
 682     }
 683   }
 684 }
 685 
 686 void ComputeLinearScanOrder::assign_loop_depth(BlockBegin* start_block) {
 687   TRACE_LINEAR_SCAN(3, "----- computing loop-depth and weight");
 688   init_visited();
 689 
 690   assert(_work_list.is_empty(), "work list must be empty before processing");
 691   _work_list.append(start_block);
 692 
 693   do {
 694     BlockBegin* cur = _work_list.pop();
 695 
 696     if (!is_visited(cur)) {
 697       set_visited(cur);


 731     _dominator_blocks.set_bit(a->block_id());
 732     assert(a->dominator() != NULL || a == _linear_scan_order->at(0), "dominator must be initialized");
 733     a = a->dominator();
 734   }
 735   while (b != NULL && !_dominator_blocks.at(b->block_id())) {
 736     assert(b->dominator() != NULL || b == _linear_scan_order->at(0), "dominator must be initialized");
 737     b = b->dominator();
 738   }
 739 
 740   assert(b != NULL, "could not find dominator");
 741   return b;
 742 }
 743 
 744 void ComputeLinearScanOrder::compute_dominator(BlockBegin* cur, BlockBegin* parent) {
 745   if (cur->dominator() == NULL) {
 746     TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: initializing dominator of B%d to B%d", cur->block_id(), parent->block_id()));
 747     cur->set_dominator(parent);
 748 
 749   } else if (!(cur->is_set(BlockBegin::linear_scan_loop_header_flag) && parent->is_set(BlockBegin::linear_scan_loop_end_flag))) {
 750     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()));
 751     // Does not hold for exception blocks
 752     assert(cur->number_of_preds() > 1 || cur->is_set(BlockBegin::exception_entry_flag), "");
 753     cur->set_dominator(common_dominator(cur->dominator(), parent));
 754   }
 755 
 756   // Additional edge to xhandler of all our successors
 757   // range check elimination needs that the state at the end of a
 758   // block be valid in every block it dominates so cur must dominate
 759   // the exception handlers of its successors.
 760   int num_cur_xhandler = cur->number_of_exception_handlers();
 761   for (int j = 0; j < num_cur_xhandler; j++) {
 762     BlockBegin* xhandler = cur->exception_handler_at(j);
 763     compute_dominator(xhandler, parent);
 764   }
 765 }
 766 
 767 
 768 int ComputeLinearScanOrder::compute_weight(BlockBegin* cur) {
 769   BlockBegin* single_sux = NULL;
 770   if (cur->number_of_sux() == 1) {
 771     single_sux = cur->sux_at(0);
 772   }
 773 
 774   // limit loop-depth to 15 bit (only for security reason, it will never be so big)
 775   int weight = (cur->loop_depth() & 0x7FFF) << 16;
 776 
 777   // general macro for short definition of weight flags
 778   // the first instance of INC_WEIGHT_IF has the highest priority
 779   int cur_bit = 15;
 780   #define INC_WEIGHT_IF(condition) if ((condition)) { weight |= (1 << cur_bit); } cur_bit--;
 781 
 782   // this is necessery for the (very rare) case that two successing blocks have
 783   // the same loop depth, but a different loop index (can happen for endless loops
 784   // with exception handlers)


 911       // the osr entry block is ignored in normal processing, it is never added to the
 912       // work list. Instead, it is added as late as possible manually here.
 913       append_block(osr_entry);
 914       compute_dominator(cur, osr_entry);
 915     }
 916     append_block(cur);
 917 
 918     int i;
 919     int num_sux = cur->number_of_sux();
 920     // changed loop order to get "intuitive" order of if- and else-blocks
 921     for (i = 0; i < num_sux; i++) {
 922       BlockBegin* sux = cur->sux_at(i);
 923       compute_dominator(sux, cur);
 924       if (ready_for_processing(sux)) {
 925         sort_into_work_list(sux);
 926       }
 927     }
 928     num_sux = cur->number_of_exception_handlers();
 929     for (i = 0; i < num_sux; i++) {
 930       BlockBegin* sux = cur->exception_handler_at(i);

 931       if (ready_for_processing(sux)) {
 932         sort_into_work_list(sux);
 933       }
 934     }
 935   } while (_work_list.length() > 0);
 936 }
 937 
 938 
 939 bool ComputeLinearScanOrder::compute_dominators_iter() {
 940   bool changed = false;
 941   int num_blocks = _linear_scan_order->length();
 942 
 943   assert(_linear_scan_order->at(0)->dominator() == NULL, "must not have dominator");
 944   assert(_linear_scan_order->at(0)->number_of_preds() == 0, "must not have predecessors");
 945   for (int i = 1; i < num_blocks; i++) {
 946     BlockBegin* block = _linear_scan_order->at(i);
 947 
 948     BlockBegin* dominator = block->pred_at(0);
 949     int num_preds = block->number_of_preds();
 950 
 951     TRACE_LINEAR_SCAN(4, tty->print_cr("DOM: Processing B%d", block->block_id()));
 952 
 953     for (int j = 0; j < num_preds; j++) {
 954 
 955       BlockBegin *pred = block->pred_at(j);
 956       TRACE_LINEAR_SCAN(4, tty->print_cr("   DOM: Subrocessing B%d", pred->block_id()));
 957 
 958       if (block->is_set(BlockBegin::exception_entry_flag)) {
 959         dominator = common_dominator(dominator, pred);
 960         int num_pred_preds = pred->number_of_preds();
 961         for (int k = 0; k < num_pred_preds; k++) {
 962           dominator = common_dominator(dominator, pred->pred_at(k));
 963         }
 964       } else {
 965         dominator = common_dominator(dominator, pred);
 966       }
 967     }
 968 
 969     if (dominator != block->dominator()) {
 970       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()));
 971 
 972       block->set_dominator(dominator);
 973       changed = true;
 974     }
 975   }
 976   return changed;
 977 }
 978 
 979 void ComputeLinearScanOrder::compute_dominators() {
 980   TRACE_LINEAR_SCAN(3, tty->print_cr("----- computing dominators (iterative computation reqired: %d)", _iterative_dominators));
 981 
 982   // iterative computation of dominators is only required for methods with non-natural loops
 983   // and OSR-methods. For all other methods, the dominators computed when generating the
 984   // linear scan block order are correct.
 985   if (_iterative_dominators) {
 986     do {
 987       TRACE_LINEAR_SCAN(1, tty->print_cr("DOM: next iteration of fix-point calculation"));
 988     } while (compute_dominators_iter());
 989   }
 990 
 991   // check that dominators are correct
 992   assert(!compute_dominators_iter(), "fix point not reached");
 993 
 994   // Add Blocks to dominates-Array
 995   int num_blocks = _linear_scan_order->length();
 996   for (int i = 0; i < num_blocks; i++) {
 997     BlockBegin* block = _linear_scan_order->at(i);
 998 
 999     BlockBegin *dom = block->dominator();
1000     if (dom) {
1001       assert(dom->dominator_depth() != -1, "Dominator must have been visited before");
1002       dom->dominates()->append(block);
1003       block->set_dominator_depth(dom->dominator_depth() + 1);
1004     } else {
1005       block->set_dominator_depth(0);
1006     }
1007   }
1008 }
1009 
1010 
1011 #ifndef PRODUCT
1012 void ComputeLinearScanOrder::print_blocks() {
1013   if (TraceLinearScanLevel >= 2) {
1014     tty->print_cr("----- loop information:");
1015     for (int block_idx = 0; block_idx < _linear_scan_order->length(); block_idx++) {
1016       BlockBegin* cur = _linear_scan_order->at(block_idx);
1017 
1018       tty->print("%4d: B%2d: ", cur->linear_scan_number(), cur->block_id());
1019       for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {
1020         tty->print ("%d ", is_block_in_loop(loop_idx, cur));
1021       }
1022       tty->print_cr(" -> loop_index: %2d, loop_depth: %2d", cur->loop_index(), cur->loop_depth());
1023     }
1024   }
1025 
1026   if (TraceLinearScanLevel >= 1) {
1027     tty->print_cr("----- linear-scan block order:");


1074   if (StressLinearScan) {
1075     // blocks are scrambled when StressLinearScan is used
1076     return;
1077   }
1078 
1079   // check that all successors of a block have a higher linear-scan-number
1080   // and that all predecessors of a block have a lower linear-scan-number
1081   // (only backward branches of loops are ignored)
1082   int i;
1083   for (i = 0; i < _linear_scan_order->length(); i++) {
1084     BlockBegin* cur = _linear_scan_order->at(i);
1085 
1086     assert(cur->linear_scan_number() == i, "incorrect linear_scan_number");
1087     assert(cur->linear_scan_number() >= 0 && cur->linear_scan_number() == _linear_scan_order->index_of(cur), "incorrect linear_scan_number");
1088 
1089     int j;
1090     for (j = cur->number_of_sux() - 1; j >= 0; j--) {
1091       BlockBegin* sux = cur->sux_at(j);
1092 
1093       assert(sux->linear_scan_number() >= 0 && sux->linear_scan_number() == _linear_scan_order->index_of(sux), "incorrect linear_scan_number");
1094       if (!sux->is_set(BlockBegin::backward_branch_target_flag)) {
1095         assert(cur->linear_scan_number() < sux->linear_scan_number(), "invalid order");
1096       }
1097       if (cur->loop_depth() == sux->loop_depth()) {
1098         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");
1099       }
1100     }
1101 
1102     for (j = cur->number_of_preds() - 1; j >= 0; j--) {
1103       BlockBegin* pred = cur->pred_at(j);
1104 
1105       assert(pred->linear_scan_number() >= 0 && pred->linear_scan_number() == _linear_scan_order->index_of(pred), "incorrect linear_scan_number");
1106       if (!cur->is_set(BlockBegin::backward_branch_target_flag)) {
1107         assert(cur->linear_scan_number() > pred->linear_scan_number(), "invalid order");
1108       }
1109       if (cur->loop_depth() == pred->loop_depth()) {
1110         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");
1111       }
1112 
1113       assert(cur->dominator()->linear_scan_number() <= cur->pred_at(j)->linear_scan_number(), "dominator must be before predecessors");
1114     }
1115 
1116     // check dominator
1117     if (i == 0) {
1118       assert(cur->dominator() == NULL, "first block has no dominator");
1119     } else {
1120       assert(cur->dominator() != NULL, "all but first block must have dominator");
1121     }
1122     // Assertion does not hold for exception handlers
1123     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");
1124   }
1125 
1126   // check that all loops are continuous
1127   for (int loop_idx = 0; loop_idx < _num_loops; loop_idx++) {
1128     int block_idx = 0;
1129     assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "the first block must not be present in any loop");
1130 
1131     // skip blocks before the loop
1132     while (block_idx < _num_blocks && !is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {
1133       block_idx++;
1134     }
1135     // skip blocks of loop
1136     while (block_idx < _num_blocks && is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx))) {
1137       block_idx++;
1138     }
1139     // after the first non-loop block, there must not be another loop-block
1140     while (block_idx < _num_blocks) {
1141       assert(!is_block_in_loop(loop_idx, _linear_scan_order->at(block_idx)), "loop not continuous in linear-scan order");
1142       block_idx++;
1143     }


1292         _predecessors->at_put(sux->block_id(), preds);
1293       }
1294       preds->append(block);
1295     }
1296 
1297     n = block->number_of_exception_handlers();
1298     for (i = 0; i < n; i++) {
1299       BlockBegin* sux = block->exception_handler_at(i);
1300       assert(sux->is_set(BlockBegin::exception_entry_flag), "must be xhandler");
1301 
1302       BlockList* preds = _predecessors->at_grow(sux->block_id(), NULL);
1303       if (preds == NULL) {
1304         preds = new BlockList();
1305         _predecessors->at_put(sux->block_id(), preds);
1306       }
1307       preds->append(block);
1308     }
1309   }
1310 };
1311 
1312 class VerifyBlockBeginField : public BlockClosure {
1313 
1314 public:
1315 
1316   virtual void block_do(BlockBegin *block) {
1317 
1318     Instruction *cur = block;
1319     while (cur) {
1320       assert(cur->block() == block, "Block begin is not correct");
1321       cur = cur->next();
1322     }
1323   }
1324 };
1325 
1326 void IR::verify() {
1327 #ifdef ASSERT
1328   PredecessorValidator pv(this);
1329   VerifyBlockBeginField verifier;
1330   this->iterate_postorder(&verifier);
1331 #endif
1332 }
1333 
1334 #endif // PRODUCT
1335 
1336 void SubstitutionResolver::visit(Value* v) {
1337   Value v0 = *v;
1338   if (v0) {
1339     Value vs = v0->subst();
1340     if (vs != v0) {
1341       *v = v0->subst();
1342     }
1343   }
1344 }
1345 
1346 #ifdef ASSERT
1347 class SubstitutionChecker: public ValueVisitor {
1348   void visit(Value* v) {
1349     Value v0 = *v;
1350     if (v0) {