< prev index next >

src/hotspot/share/c1/c1_Optimizer.cpp

Print this page

   1 /*
   2  * Copyright (c) 1999, 2025, 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  *

  69   void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) {
  70     int e = sux->number_of_exception_handlers();
  71     for (int i = 0; i < e; i++) {
  72       BlockBegin* xhandler = sux->exception_handler_at(i);
  73       block->add_exception_handler(xhandler);
  74 
  75       assert(xhandler->is_predecessor(sux), "missing predecessor");
  76       if (sux->number_of_preds() == 0) {
  77         // sux is disconnected from graph so disconnect from exception handlers
  78         xhandler->remove_predecessor(sux);
  79       }
  80       if (!xhandler->is_predecessor(block)) {
  81         xhandler->add_predecessor(block);
  82       }
  83     }
  84   }
  85 
  86   virtual void block_do(BlockBegin* block);
  87 
  88  private:
  89   Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval);

  90 };
  91 
  92 void CE_Eliminator::block_do(BlockBegin* block) {
  93   // 1) find conditional expression
  94   // check if block ends with an If
  95   If* if_ = block->end()->as_If();
  96   if (if_ == nullptr) return;
  97 
  98   // check if If works on int or object types
  99   // (we cannot handle If's working on long, float or doubles yet,
 100   // since IfOp doesn't support them - these If's show up if cmp
 101   // operations followed by If's are eliminated)
 102   ValueType* if_type = if_->x()->type();
 103   if (!if_type->is_int() && !if_type->is_object()) return;
 104 
 105   BlockBegin* t_block = if_->tsux();
 106   BlockBegin* f_block = if_->fsux();
 107   Instruction* t_cur = t_block->next();
 108   Instruction* f_cur = f_block->next();
 109 

 198 
 199   // 2) substitute conditional expression
 200   //    with an IfOp followed by a Goto
 201   // cut if_ away and get node before
 202   Instruction* cur_end = if_->prev();
 203 
 204   // append constants of true- and false-block if necessary
 205   // clone constants because original block must not be destroyed
 206   assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
 207   if (t_value == t_const) {
 208     t_value = new Constant(t_const->type());
 209     NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
 210     cur_end = cur_end->set_next(t_value);
 211   }
 212   if (f_value == f_const) {
 213     f_value = new Constant(f_const->type());
 214     NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
 215     cur_end = cur_end->set_next(f_value);
 216   }
 217 
 218   Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value);

 219   assert(result != nullptr, "make_ifop must return a non-null instruction");
 220   if (!result->is_linked() && result->can_be_linked()) {
 221     NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
 222     cur_end = cur_end->set_next(result);
 223   }
 224 
 225   // append Goto to successor
 226   ValueStack* state_before = if_->state_before();
 227   Goto* goto_ = new Goto(sux, state_before, is_safepoint);
 228 
 229   // prepare state for Goto
 230   ValueStack* goto_state = if_state;
 231   goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
 232   goto_state->push(result->type(), result);
 233   assert(goto_state->is_same(sux_state), "states must match now");
 234   goto_->set_state(goto_state);
 235 
 236   cur_end = cur_end->set_next(goto_, goto_state->bci());
 237 
 238   // Adjust control flow graph

 253   // substitute the phi if possible
 254   if (sux_phi->as_Phi()->operand_count() == 1) {
 255     assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
 256     sux_phi->set_subst(result);
 257     _has_substitution = true;
 258   }
 259 
 260   // 3) successfully eliminated a conditional expression
 261   _cee_count++;
 262   if (PrintCEE) {
 263     tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id());
 264     tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
 265   }
 266 
 267 #ifdef DO_DELAYED_VERIFICATION
 268   _hir->verify_local(blocks_to_verify_later);
 269 #endif // DO_DELAYED_VERIFICATION
 270 
 271 }
 272 
 273 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) {

 274   if (!OptimizeIfOps) {
 275     return new IfOp(x, cond, y, tval, fval);
 276   }
 277 
 278   tval = tval->subst();
 279   fval = fval->subst();
 280   if (tval == fval) {
 281     _ifop_count++;
 282     return tval;
 283   }
 284 
 285   x = x->subst();
 286   y = y->subst();
 287 
 288   Constant* y_const = y->as_Constant();
 289   if (y_const != nullptr) {

 290     IfOp* x_ifop = x->as_IfOp();
 291     if (x_ifop != nullptr) {                 // x is an ifop, y is a constant
 292       Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
 293       Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
 294 
 295       if (x_tval_const != nullptr && x_fval_const != nullptr) {
 296         Instruction::Condition x_ifop_cond = x_ifop->cond();
 297 
 298         Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
 299         Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
 300 
 301         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 302         if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
 303           Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
 304           Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
 305 
 306           _ifop_count++;
 307           if (new_tval == new_fval) {
 308             return new_tval;
 309           } else {
 310             return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval);
 311           }
 312         }
 313       }
 314     } else {
 315       Constant* x_const = x->as_Constant();
 316       if (x_const != nullptr) { // x and y are constants
 317         Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
 318         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 319         if (x_compare_res != Constant::not_comparable) {
 320           _ifop_count++;
 321           return x_compare_res == Constant::cond_true ? tval : fval;
 322         }
 323       }
 324     }
 325   }
 326   return new IfOp(x, cond, y, tval, fval);
 327 }
 328 
 329 void Optimizer::eliminate_conditional_expressions() {
 330   // find conditional expressions & replace them with IfOps
 331   CE_Eliminator ce(ir());
 332 }
 333 
 334 // This removes others' relation to block, but doesn't empty block's lists
 335 static void disconnect_from_graph(BlockBegin* block) {
 336   for (int p = 0; p < block->number_of_preds(); p++) {
 337     BlockBegin* pred = block->pred_at(p);
 338     int idx;
 339     while ((idx = pred->end()->find_sux(block)) >= 0) {
 340       pred->end()->remove_sux_at(idx);
 341     }
 342   }
 343   for (int s = 0; s < block->number_of_sux(); s++) {
 344     block->sux_at(s)->remove_predecessor(block);
 345   }
 346 }

 446     _merge_count++;
 447     if (PrintBlockElimination) {
 448       tty->print_cr("%d. merged B%d & B%d (stack size = %d)",
 449                     _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size());
 450     }
 451 
 452 #ifdef DO_DELAYED_VERIFICATION
 453     _hir->verify_local(blocks_to_verify_later);
 454 #endif // DO_DELAYED_VERIFICATION
 455 
 456     If* if_ = block->end()->as_If();
 457     if (if_) {
 458       IfOp* ifop    = if_->x()->as_IfOp();
 459       Constant* con = if_->y()->as_Constant();
 460       bool swapped = false;
 461       if (!con || !ifop) {
 462         ifop = if_->y()->as_IfOp();
 463         con  = if_->x()->as_Constant();
 464         swapped = true;
 465       }
 466       if (con && ifop) {
 467         Constant* tval = ifop->tval()->as_Constant();
 468         Constant* fval = ifop->fval()->as_Constant();
 469         if (tval && fval) {
 470           // Find the instruction before if_, starting with ifop.
 471           // When if_ and ifop are not in the same block, prev
 472           // becomes null In such (rare) cases it is not
 473           // profitable to perform the optimization.
 474           Value prev = ifop;
 475           while (prev != nullptr && prev->next() != if_) {
 476             prev = prev->next();
 477           }
 478 
 479           if (prev != nullptr) {
 480             Instruction::Condition cond = if_->cond();
 481             BlockBegin* tsux = if_->tsux();
 482             BlockBegin* fsux = if_->fsux();
 483             if (swapped) {
 484               cond = Instruction::mirror(cond);
 485             }
 486 
 487             BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
 488             BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
 489             if (tblock != fblock && !if_->is_safepoint()) {
 490               If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
 491                                  tblock, fblock, if_->state_before(), if_->is_safepoint());
 492               newif->set_state(if_->state()->copy());
 493 
 494               assert(prev->next() == if_, "must be guaranteed by above search");
 495               NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
 496               prev->set_next(newif);
 497               block->set_end(newif);
 498 
 499               _merge_count++;
 500               if (PrintBlockElimination) {
 501                 tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
 502               }
 503 
 504 #ifdef DO_DELAYED_VERIFICATION
 505               _hir->verify_local(blocks_to_verify_later);
 506 #endif // DO_DELAYED_VERIFICATION
 507             }
 508           }
 509         }
 510       }
 511     }

 565   void do_CheckCast      (CheckCast*       x);
 566   void do_InstanceOf     (InstanceOf*      x);
 567   void do_MonitorEnter   (MonitorEnter*    x);
 568   void do_MonitorExit    (MonitorExit*     x);
 569   void do_Intrinsic      (Intrinsic*       x);
 570   void do_BlockBegin     (BlockBegin*      x);
 571   void do_Goto           (Goto*            x);
 572   void do_If             (If*              x);
 573   void do_TableSwitch    (TableSwitch*     x);
 574   void do_LookupSwitch   (LookupSwitch*    x);
 575   void do_Return         (Return*          x);
 576   void do_Throw          (Throw*           x);
 577   void do_Base           (Base*            x);
 578   void do_OsrEntry       (OsrEntry*        x);
 579   void do_ExceptionObject(ExceptionObject* x);
 580   void do_UnsafeGet      (UnsafeGet*       x);
 581   void do_UnsafePut      (UnsafePut*       x);
 582   void do_UnsafeGetAndSet(UnsafeGetAndSet* x);
 583   void do_ProfileCall    (ProfileCall*     x);
 584   void do_ProfileReturnType (ProfileReturnType*  x);

 585   void do_ProfileInvoke  (ProfileInvoke*   x);
 586   void do_RuntimeCall    (RuntimeCall*     x);
 587   void do_MemBar         (MemBar*          x);
 588   void do_RangeCheckPredicate(RangeCheckPredicate* x);
 589 #ifdef ASSERT
 590   void do_Assert         (Assert*          x);
 591 #endif
 592 };
 593 
 594 
 595 // Because of a static contained within (for the purpose of iteration
 596 // over instructions), it is only valid to have one of these active at
 597 // a time
 598 class NullCheckEliminator: public ValueVisitor {
 599  private:
 600   Optimizer*        _opt;
 601 
 602   ValueSet*         _visitable_instructions;        // Visit each instruction only once per basic block
 603   BlockList*        _work_list;                   // Basic blocks to visit
 604 

 693   // (separated out from NullCheckVisitor for clarity)
 694 
 695   // The basic contract is that these must leave the instruction in
 696   // the desired state; must not assume anything about the state of
 697   // the instruction. We make multiple passes over some basic blocks
 698   // and the last pass is the only one whose result is valid.
 699   void handle_AccessField     (AccessField* x);
 700   void handle_ArrayLength     (ArrayLength* x);
 701   void handle_LoadIndexed     (LoadIndexed* x);
 702   void handle_StoreIndexed    (StoreIndexed* x);
 703   void handle_NullCheck       (NullCheck* x);
 704   void handle_Invoke          (Invoke* x);
 705   void handle_NewInstance     (NewInstance* x);
 706   void handle_NewArray        (NewArray* x);
 707   void handle_AccessMonitor   (AccessMonitor* x);
 708   void handle_Intrinsic       (Intrinsic* x);
 709   void handle_ExceptionObject (ExceptionObject* x);
 710   void handle_Phi             (Phi* x);
 711   void handle_ProfileCall     (ProfileCall* x);
 712   void handle_ProfileReturnType (ProfileReturnType* x);

 713   void handle_Constant        (Constant* x);
 714   void handle_IfOp            (IfOp* x);
 715 };
 716 
 717 
 718 // NEEDS_CLEANUP
 719 // There may be other instructions which need to clear the last
 720 // explicit null check. Anything across which we can not hoist the
 721 // debug information for a NullCheck instruction must clear it. It
 722 // might be safer to pattern match "NullCheck ; {AccessField,
 723 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
 724 // Should test to see performance hit of clearing it for all handlers
 725 // with empty bodies below. If it is negligible then we should leave
 726 // that in for safety, otherwise should think more about it.
 727 void NullCheckVisitor::do_Phi            (Phi*             x) { nce()->handle_Phi(x);      }
 728 void NullCheckVisitor::do_Local          (Local*           x) {}
 729 void NullCheckVisitor::do_Constant       (Constant*        x) { nce()->handle_Constant(x); }
 730 void NullCheckVisitor::do_LoadField      (LoadField*       x) { nce()->handle_AccessField(x); }
 731 void NullCheckVisitor::do_StoreField     (StoreField*      x) { nce()->handle_AccessField(x); }
 732 void NullCheckVisitor::do_ArrayLength    (ArrayLength*     x) { nce()->handle_ArrayLength(x); }

 751 void NullCheckVisitor::do_MonitorEnter   (MonitorEnter*    x) { nce()->handle_AccessMonitor(x); }
 752 void NullCheckVisitor::do_MonitorExit    (MonitorExit*     x) { nce()->handle_AccessMonitor(x); }
 753 void NullCheckVisitor::do_Intrinsic      (Intrinsic*       x) { nce()->handle_Intrinsic(x);     }
 754 void NullCheckVisitor::do_BlockBegin     (BlockBegin*      x) {}
 755 void NullCheckVisitor::do_Goto           (Goto*            x) {}
 756 void NullCheckVisitor::do_If             (If*              x) {}
 757 void NullCheckVisitor::do_TableSwitch    (TableSwitch*     x) {}
 758 void NullCheckVisitor::do_LookupSwitch   (LookupSwitch*    x) {}
 759 void NullCheckVisitor::do_Return         (Return*          x) {}
 760 void NullCheckVisitor::do_Throw          (Throw*           x) { nce()->clear_last_explicit_null_check(); }
 761 void NullCheckVisitor::do_Base           (Base*            x) {}
 762 void NullCheckVisitor::do_OsrEntry       (OsrEntry*        x) {}
 763 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
 764 void NullCheckVisitor::do_UnsafeGet      (UnsafeGet*       x) {}
 765 void NullCheckVisitor::do_UnsafePut      (UnsafePut*       x) {}
 766 void NullCheckVisitor::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {}
 767 void NullCheckVisitor::do_ProfileCall    (ProfileCall*     x) { nce()->clear_last_explicit_null_check();
 768                                                                 nce()->handle_ProfileCall(x); }
 769 void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); }
 770 void NullCheckVisitor::do_ProfileInvoke  (ProfileInvoke*   x) {}

 771 void NullCheckVisitor::do_RuntimeCall    (RuntimeCall*     x) {}
 772 void NullCheckVisitor::do_MemBar         (MemBar*          x) {}
 773 void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
 774 #ifdef ASSERT
 775 void NullCheckVisitor::do_Assert         (Assert*          x) {}
 776 #endif
 777 
 778 void NullCheckEliminator::visit(Value* p) {
 779   assert(*p != nullptr, "should not find null instructions");
 780   if (visitable(*p)) {
 781     mark_visited(*p);
 782     (*p)->visit(&_visitor);
 783   }
 784 }
 785 
 786 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
 787   ValueSet* state = state_for(block);
 788   if (state == nullptr) {
 789     state = incoming_state->copy();
 790     set_state_for(block, state);

1180     // Value is non-null => update Phi
1181     if (PrintNullCheckElimination) {
1182       tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1183     }
1184     x->set_needs_null_check(false);
1185   } else if (set_contains(x)) {
1186     set_remove(x);
1187   }
1188 }
1189 
1190 void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
1191   for (int i = 0; i < x->nb_profiled_args(); i++) {
1192     x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
1193   }
1194 }
1195 
1196 void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) {
1197   x->set_needs_null_check(!set_contains(x->ret()));
1198 }
1199 





1200 void NullCheckEliminator::handle_Constant(Constant *x) {
1201   ObjectType* ot = x->type()->as_ObjectType();
1202   if (ot != nullptr && ot->is_loaded()) {
1203     ObjectConstant* oc = ot->as_ObjectConstant();
1204     if (oc == nullptr || !oc->value()->is_null_object()) {
1205       set_put(x);
1206       if (PrintNullCheckElimination) {
1207         tty->print_cr("Constant %d is non-null", x->id());
1208       }
1209     }
1210   }
1211 }
1212 
1213 void NullCheckEliminator::handle_IfOp(IfOp *x) {
1214   if (x->type()->is_object() && set_contains(x->tval()) && set_contains(x->fval())) {
1215     set_put(x);
1216     if (PrintNullCheckElimination) {
1217       tty->print_cr("IfOp %d is non-null", x->id());
1218     }
1219   }

   1 /*
   2  * Copyright (c) 1999, 2026, 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  *

  69   void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) {
  70     int e = sux->number_of_exception_handlers();
  71     for (int i = 0; i < e; i++) {
  72       BlockBegin* xhandler = sux->exception_handler_at(i);
  73       block->add_exception_handler(xhandler);
  74 
  75       assert(xhandler->is_predecessor(sux), "missing predecessor");
  76       if (sux->number_of_preds() == 0) {
  77         // sux is disconnected from graph so disconnect from exception handlers
  78         xhandler->remove_predecessor(sux);
  79       }
  80       if (!xhandler->is_predecessor(block)) {
  81         xhandler->add_predecessor(block);
  82       }
  83     }
  84   }
  85 
  86   virtual void block_do(BlockBegin* block);
  87 
  88  private:
  89   Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval,
  90                   ValueStack* state_before, bool substitutability_check);
  91 };
  92 
  93 void CE_Eliminator::block_do(BlockBegin* block) {
  94   // 1) find conditional expression
  95   // check if block ends with an If
  96   If* if_ = block->end()->as_If();
  97   if (if_ == nullptr) return;
  98 
  99   // check if If works on int or object types
 100   // (we cannot handle If's working on long, float or doubles yet,
 101   // since IfOp doesn't support them - these If's show up if cmp
 102   // operations followed by If's are eliminated)
 103   ValueType* if_type = if_->x()->type();
 104   if (!if_type->is_int() && !if_type->is_object()) return;
 105 
 106   BlockBegin* t_block = if_->tsux();
 107   BlockBegin* f_block = if_->fsux();
 108   Instruction* t_cur = t_block->next();
 109   Instruction* f_cur = f_block->next();
 110 

 199 
 200   // 2) substitute conditional expression
 201   //    with an IfOp followed by a Goto
 202   // cut if_ away and get node before
 203   Instruction* cur_end = if_->prev();
 204 
 205   // append constants of true- and false-block if necessary
 206   // clone constants because original block must not be destroyed
 207   assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
 208   if (t_value == t_const) {
 209     t_value = new Constant(t_const->type());
 210     NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
 211     cur_end = cur_end->set_next(t_value);
 212   }
 213   if (f_value == f_const) {
 214     f_value = new Constant(f_const->type());
 215     NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
 216     cur_end = cur_end->set_next(f_value);
 217   }
 218 
 219   Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value,
 220                            if_->state_before(), if_->substitutability_check());
 221   assert(result != nullptr, "make_ifop must return a non-null instruction");
 222   if (!result->is_linked() && result->can_be_linked()) {
 223     NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
 224     cur_end = cur_end->set_next(result);
 225   }
 226 
 227   // append Goto to successor
 228   ValueStack* state_before = if_->state_before();
 229   Goto* goto_ = new Goto(sux, state_before, is_safepoint);
 230 
 231   // prepare state for Goto
 232   ValueStack* goto_state = if_state;
 233   goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
 234   goto_state->push(result->type(), result);
 235   assert(goto_state->is_same(sux_state), "states must match now");
 236   goto_->set_state(goto_state);
 237 
 238   cur_end = cur_end->set_next(goto_, goto_state->bci());
 239 
 240   // Adjust control flow graph

 255   // substitute the phi if possible
 256   if (sux_phi->as_Phi()->operand_count() == 1) {
 257     assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
 258     sux_phi->set_subst(result);
 259     _has_substitution = true;
 260   }
 261 
 262   // 3) successfully eliminated a conditional expression
 263   _cee_count++;
 264   if (PrintCEE) {
 265     tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id());
 266     tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
 267   }
 268 
 269 #ifdef DO_DELAYED_VERIFICATION
 270   _hir->verify_local(blocks_to_verify_later);
 271 #endif // DO_DELAYED_VERIFICATION
 272 
 273 }
 274 
 275 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval,
 276                                ValueStack* state_before, bool substitutability_check) {
 277   if (!OptimizeIfOps) {
 278     return new IfOp(x, cond, y, tval, fval, state_before, substitutability_check);
 279   }
 280 
 281   tval = tval->subst();
 282   fval = fval->subst();
 283   if (tval == fval) {
 284     _ifop_count++;
 285     return tval;
 286   }
 287 
 288   x = x->subst();
 289   y = y->subst();
 290 
 291   Constant* y_const = y->as_Constant();
 292   // We must not optimize a substitutability check to a pointer comparison.
 293   if (!substitutability_check && y_const != nullptr) {
 294     IfOp* x_ifop = x->as_IfOp();
 295     if (x_ifop != nullptr) {                 // x is an ifop, y is a constant
 296       Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
 297       Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
 298 
 299       if (x_tval_const != nullptr && x_fval_const != nullptr) {
 300         Instruction::Condition x_ifop_cond = x_ifop->cond();
 301 
 302         Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
 303         Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
 304 
 305         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 306         if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
 307           Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
 308           Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
 309 
 310           _ifop_count++;
 311           if (new_tval == new_fval) {
 312             return new_tval;
 313           } else {
 314             return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval, x_ifop->state_before(), x_ifop->substitutability_check());
 315           }
 316         }
 317       }
 318     } else {
 319       Constant* x_const = x->as_Constant();
 320       if (x_const != nullptr) { // x and y are constants
 321         Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
 322         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 323         if (x_compare_res != Constant::not_comparable) {
 324           _ifop_count++;
 325           return x_compare_res == Constant::cond_true ? tval : fval;
 326         }
 327       }
 328     }
 329   }
 330   return new IfOp(x, cond, y, tval, fval, state_before, substitutability_check);
 331 }
 332 
 333 void Optimizer::eliminate_conditional_expressions() {
 334   // find conditional expressions & replace them with IfOps
 335   CE_Eliminator ce(ir());
 336 }
 337 
 338 // This removes others' relation to block, but doesn't empty block's lists
 339 static void disconnect_from_graph(BlockBegin* block) {
 340   for (int p = 0; p < block->number_of_preds(); p++) {
 341     BlockBegin* pred = block->pred_at(p);
 342     int idx;
 343     while ((idx = pred->end()->find_sux(block)) >= 0) {
 344       pred->end()->remove_sux_at(idx);
 345     }
 346   }
 347   for (int s = 0; s < block->number_of_sux(); s++) {
 348     block->sux_at(s)->remove_predecessor(block);
 349   }
 350 }

 450     _merge_count++;
 451     if (PrintBlockElimination) {
 452       tty->print_cr("%d. merged B%d & B%d (stack size = %d)",
 453                     _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size());
 454     }
 455 
 456 #ifdef DO_DELAYED_VERIFICATION
 457     _hir->verify_local(blocks_to_verify_later);
 458 #endif // DO_DELAYED_VERIFICATION
 459 
 460     If* if_ = block->end()->as_If();
 461     if (if_) {
 462       IfOp* ifop    = if_->x()->as_IfOp();
 463       Constant* con = if_->y()->as_Constant();
 464       bool swapped = false;
 465       if (!con || !ifop) {
 466         ifop = if_->y()->as_IfOp();
 467         con  = if_->x()->as_Constant();
 468         swapped = true;
 469       }
 470       if (con && ifop && !ifop->substitutability_check()) {
 471         Constant* tval = ifop->tval()->as_Constant();
 472         Constant* fval = ifop->fval()->as_Constant();
 473         if (tval && fval) {
 474           // Find the instruction before if_, starting with ifop.
 475           // When if_ and ifop are not in the same block, prev
 476           // becomes null In such (rare) cases it is not
 477           // profitable to perform the optimization.
 478           Value prev = ifop;
 479           while (prev != nullptr && prev->next() != if_) {
 480             prev = prev->next();
 481           }
 482 
 483           if (prev != nullptr) {
 484             Instruction::Condition cond = if_->cond();
 485             BlockBegin* tsux = if_->tsux();
 486             BlockBegin* fsux = if_->fsux();
 487             if (swapped) {
 488               cond = Instruction::mirror(cond);
 489             }
 490 
 491             BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
 492             BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
 493             if (tblock != fblock && !if_->is_safepoint()) {
 494               If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
 495                                  tblock, fblock, if_->state_before(), if_->is_safepoint(), ifop->substitutability_check());
 496               newif->set_state(if_->state()->copy());
 497 
 498               assert(prev->next() == if_, "must be guaranteed by above search");
 499               NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
 500               prev->set_next(newif);
 501               block->set_end(newif);
 502 
 503               _merge_count++;
 504               if (PrintBlockElimination) {
 505                 tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
 506               }
 507 
 508 #ifdef DO_DELAYED_VERIFICATION
 509               _hir->verify_local(blocks_to_verify_later);
 510 #endif // DO_DELAYED_VERIFICATION
 511             }
 512           }
 513         }
 514       }
 515     }

 569   void do_CheckCast      (CheckCast*       x);
 570   void do_InstanceOf     (InstanceOf*      x);
 571   void do_MonitorEnter   (MonitorEnter*    x);
 572   void do_MonitorExit    (MonitorExit*     x);
 573   void do_Intrinsic      (Intrinsic*       x);
 574   void do_BlockBegin     (BlockBegin*      x);
 575   void do_Goto           (Goto*            x);
 576   void do_If             (If*              x);
 577   void do_TableSwitch    (TableSwitch*     x);
 578   void do_LookupSwitch   (LookupSwitch*    x);
 579   void do_Return         (Return*          x);
 580   void do_Throw          (Throw*           x);
 581   void do_Base           (Base*            x);
 582   void do_OsrEntry       (OsrEntry*        x);
 583   void do_ExceptionObject(ExceptionObject* x);
 584   void do_UnsafeGet      (UnsafeGet*       x);
 585   void do_UnsafePut      (UnsafePut*       x);
 586   void do_UnsafeGetAndSet(UnsafeGetAndSet* x);
 587   void do_ProfileCall    (ProfileCall*     x);
 588   void do_ProfileReturnType (ProfileReturnType*  x);
 589   void do_ProfileACmpTypes(ProfileACmpTypes*  x);
 590   void do_ProfileInvoke  (ProfileInvoke*   x);
 591   void do_RuntimeCall    (RuntimeCall*     x);
 592   void do_MemBar         (MemBar*          x);
 593   void do_RangeCheckPredicate(RangeCheckPredicate* x);
 594 #ifdef ASSERT
 595   void do_Assert         (Assert*          x);
 596 #endif
 597 };
 598 
 599 
 600 // Because of a static contained within (for the purpose of iteration
 601 // over instructions), it is only valid to have one of these active at
 602 // a time
 603 class NullCheckEliminator: public ValueVisitor {
 604  private:
 605   Optimizer*        _opt;
 606 
 607   ValueSet*         _visitable_instructions;        // Visit each instruction only once per basic block
 608   BlockList*        _work_list;                   // Basic blocks to visit
 609 

 698   // (separated out from NullCheckVisitor for clarity)
 699 
 700   // The basic contract is that these must leave the instruction in
 701   // the desired state; must not assume anything about the state of
 702   // the instruction. We make multiple passes over some basic blocks
 703   // and the last pass is the only one whose result is valid.
 704   void handle_AccessField     (AccessField* x);
 705   void handle_ArrayLength     (ArrayLength* x);
 706   void handle_LoadIndexed     (LoadIndexed* x);
 707   void handle_StoreIndexed    (StoreIndexed* x);
 708   void handle_NullCheck       (NullCheck* x);
 709   void handle_Invoke          (Invoke* x);
 710   void handle_NewInstance     (NewInstance* x);
 711   void handle_NewArray        (NewArray* x);
 712   void handle_AccessMonitor   (AccessMonitor* x);
 713   void handle_Intrinsic       (Intrinsic* x);
 714   void handle_ExceptionObject (ExceptionObject* x);
 715   void handle_Phi             (Phi* x);
 716   void handle_ProfileCall     (ProfileCall* x);
 717   void handle_ProfileReturnType (ProfileReturnType* x);
 718   void handle_ProfileACmpTypes(ProfileACmpTypes* x);
 719   void handle_Constant        (Constant* x);
 720   void handle_IfOp            (IfOp* x);
 721 };
 722 
 723 
 724 // NEEDS_CLEANUP
 725 // There may be other instructions which need to clear the last
 726 // explicit null check. Anything across which we can not hoist the
 727 // debug information for a NullCheck instruction must clear it. It
 728 // might be safer to pattern match "NullCheck ; {AccessField,
 729 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
 730 // Should test to see performance hit of clearing it for all handlers
 731 // with empty bodies below. If it is negligible then we should leave
 732 // that in for safety, otherwise should think more about it.
 733 void NullCheckVisitor::do_Phi            (Phi*             x) { nce()->handle_Phi(x);      }
 734 void NullCheckVisitor::do_Local          (Local*           x) {}
 735 void NullCheckVisitor::do_Constant       (Constant*        x) { nce()->handle_Constant(x); }
 736 void NullCheckVisitor::do_LoadField      (LoadField*       x) { nce()->handle_AccessField(x); }
 737 void NullCheckVisitor::do_StoreField     (StoreField*      x) { nce()->handle_AccessField(x); }
 738 void NullCheckVisitor::do_ArrayLength    (ArrayLength*     x) { nce()->handle_ArrayLength(x); }

 757 void NullCheckVisitor::do_MonitorEnter   (MonitorEnter*    x) { nce()->handle_AccessMonitor(x); }
 758 void NullCheckVisitor::do_MonitorExit    (MonitorExit*     x) { nce()->handle_AccessMonitor(x); }
 759 void NullCheckVisitor::do_Intrinsic      (Intrinsic*       x) { nce()->handle_Intrinsic(x);     }
 760 void NullCheckVisitor::do_BlockBegin     (BlockBegin*      x) {}
 761 void NullCheckVisitor::do_Goto           (Goto*            x) {}
 762 void NullCheckVisitor::do_If             (If*              x) {}
 763 void NullCheckVisitor::do_TableSwitch    (TableSwitch*     x) {}
 764 void NullCheckVisitor::do_LookupSwitch   (LookupSwitch*    x) {}
 765 void NullCheckVisitor::do_Return         (Return*          x) {}
 766 void NullCheckVisitor::do_Throw          (Throw*           x) { nce()->clear_last_explicit_null_check(); }
 767 void NullCheckVisitor::do_Base           (Base*            x) {}
 768 void NullCheckVisitor::do_OsrEntry       (OsrEntry*        x) {}
 769 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
 770 void NullCheckVisitor::do_UnsafeGet      (UnsafeGet*       x) {}
 771 void NullCheckVisitor::do_UnsafePut      (UnsafePut*       x) {}
 772 void NullCheckVisitor::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {}
 773 void NullCheckVisitor::do_ProfileCall    (ProfileCall*     x) { nce()->clear_last_explicit_null_check();
 774                                                                 nce()->handle_ProfileCall(x); }
 775 void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); }
 776 void NullCheckVisitor::do_ProfileInvoke  (ProfileInvoke*   x) {}
 777 void NullCheckVisitor::do_ProfileACmpTypes(ProfileACmpTypes* x) { nce()->handle_ProfileACmpTypes(x); }
 778 void NullCheckVisitor::do_RuntimeCall    (RuntimeCall*     x) {}
 779 void NullCheckVisitor::do_MemBar         (MemBar*          x) {}
 780 void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
 781 #ifdef ASSERT
 782 void NullCheckVisitor::do_Assert         (Assert*          x) {}
 783 #endif
 784 
 785 void NullCheckEliminator::visit(Value* p) {
 786   assert(*p != nullptr, "should not find null instructions");
 787   if (visitable(*p)) {
 788     mark_visited(*p);
 789     (*p)->visit(&_visitor);
 790   }
 791 }
 792 
 793 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
 794   ValueSet* state = state_for(block);
 795   if (state == nullptr) {
 796     state = incoming_state->copy();
 797     set_state_for(block, state);

1187     // Value is non-null => update Phi
1188     if (PrintNullCheckElimination) {
1189       tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1190     }
1191     x->set_needs_null_check(false);
1192   } else if (set_contains(x)) {
1193     set_remove(x);
1194   }
1195 }
1196 
1197 void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
1198   for (int i = 0; i < x->nb_profiled_args(); i++) {
1199     x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
1200   }
1201 }
1202 
1203 void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) {
1204   x->set_needs_null_check(!set_contains(x->ret()));
1205 }
1206 
1207 void NullCheckEliminator::handle_ProfileACmpTypes(ProfileACmpTypes* x) {
1208   x->set_left_maybe_null(!set_contains(x->left()));
1209   x->set_right_maybe_null(!set_contains(x->right()));
1210 }
1211 
1212 void NullCheckEliminator::handle_Constant(Constant *x) {
1213   ObjectType* ot = x->type()->as_ObjectType();
1214   if (ot != nullptr && ot->is_loaded()) {
1215     ObjectConstant* oc = ot->as_ObjectConstant();
1216     if (oc == nullptr || !oc->value()->is_null_object()) {
1217       set_put(x);
1218       if (PrintNullCheckElimination) {
1219         tty->print_cr("Constant %d is non-null", x->id());
1220       }
1221     }
1222   }
1223 }
1224 
1225 void NullCheckEliminator::handle_IfOp(IfOp *x) {
1226   if (x->type()->is_object() && set_contains(x->tval()) && set_contains(x->fval())) {
1227     set_put(x);
1228     if (PrintNullCheckElimination) {
1229       tty->print_cr("IfOp %d is non-null", x->id());
1230     }
1231   }
< prev index next >