< prev index next >

src/hotspot/share/c1/c1_Optimizer.cpp

Print this page


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


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

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


 182 
 183   // 2) substitute conditional expression
 184   //    with an IfOp followed by a Goto
 185   // cut if_ away and get node before
 186   Instruction* cur_end = if_->prev();
 187 
 188   // append constants of true- and false-block if necessary
 189   // clone constants because original block must not be destroyed
 190   assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
 191   if (t_value == t_const) {
 192     t_value = new Constant(t_const->type());
 193     NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
 194     cur_end = cur_end->set_next(t_value);
 195   }
 196   if (f_value == f_const) {
 197     f_value = new Constant(f_const->type());
 198     NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
 199     cur_end = cur_end->set_next(f_value);
 200   }
 201 
 202   Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value);

 203   assert(result != NULL, "make_ifop must return a non-null instruction");
 204   if (!result->is_linked() && result->can_be_linked()) {
 205     NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
 206     cur_end = cur_end->set_next(result);
 207   }
 208 
 209   // append Goto to successor
 210   ValueStack* state_before = if_->state_before();
 211   Goto* goto_ = new Goto(sux, state_before, is_safepoint);
 212 
 213   // prepare state for Goto
 214   ValueStack* goto_state = if_state;
 215   goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
 216   goto_state->push(result->type(), result);
 217   assert(goto_state->is_same(sux_state), "states must match now");
 218   goto_->set_state(goto_state);
 219 
 220   cur_end = cur_end->set_next(goto_, goto_state->bci());
 221 
 222   // Adjust control flow graph


 234   // update block end
 235   block->set_end(goto_);
 236 
 237   // substitute the phi if possible
 238   if (sux_phi->as_Phi()->operand_count() == 1) {
 239     assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
 240     sux_phi->set_subst(result);
 241     _has_substitution = true;
 242   }
 243 
 244   // 3) successfully eliminated a conditional expression
 245   _cee_count++;
 246   if (PrintCEE) {
 247     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());
 248     tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
 249   }
 250 
 251   _hir->verify();
 252 }
 253 
 254 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) {

 255   if (!OptimizeIfOps) {
 256     return new IfOp(x, cond, y, tval, fval);
 257   }
 258 
 259   tval = tval->subst();
 260   fval = fval->subst();
 261   if (tval == fval) {
 262     _ifop_count++;
 263     return tval;
 264   }
 265 
 266   x = x->subst();
 267   y = y->subst();
 268 
 269   Constant* y_const = y->as_Constant();
 270   if (y_const != NULL) {
 271     IfOp* x_ifop = x->as_IfOp();
 272     if (x_ifop != NULL) {                 // x is an ifop, y is a constant
 273       Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
 274       Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
 275 
 276       if (x_tval_const != NULL && x_fval_const != NULL) {
 277         Instruction::Condition x_ifop_cond = x_ifop->cond();
 278 
 279         Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
 280         Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
 281 
 282         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 283         if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
 284           Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
 285           Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
 286 
 287           _ifop_count++;
 288           if (new_tval == new_fval) {
 289             return new_tval;
 290           } else {
 291             return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval);
 292           }
 293         }
 294       }
 295     } else {
 296       Constant* x_const = x->as_Constant();
 297       if (x_const != NULL) {         // x and y are constants
 298         Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
 299         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 300         if (x_compare_res != Constant::not_comparable) {
 301           _ifop_count++;
 302           return x_compare_res == Constant::cond_true ? tval : fval;
 303         }
 304       }
 305     }
 306   }
 307   return new IfOp(x, cond, y, tval, fval);
 308 }
 309 
 310 void Optimizer::eliminate_conditional_expressions() {
 311   // find conditional expressions & replace them with IfOps
 312   CE_Eliminator ce(ir());
 313 }
 314 
 315 class BlockMerger: public BlockClosure {
 316  private:
 317   IR* _hir;
 318   int _merge_count;              // the number of block pairs successfully merged
 319 
 320  public:
 321   BlockMerger(IR* hir)
 322   : _hir(hir)
 323   , _merge_count(0)
 324   {
 325     _hir->iterate_preorder(this);
 326     CompileLog* log = _hir->compilation()->log();
 327     if (log != NULL)


 419               // When if_ and ifop are not in the same block, prev
 420               // becomes NULL In such (rare) cases it is not
 421               // profitable to perform the optimization.
 422               Value prev = ifop;
 423               while (prev != NULL && prev->next() != if_) {
 424                 prev = prev->next();
 425               }
 426 
 427               if (prev != NULL) {
 428                 Instruction::Condition cond = if_->cond();
 429                 BlockBegin* tsux = if_->tsux();
 430                 BlockBegin* fsux = if_->fsux();
 431                 if (swapped) {
 432                   cond = Instruction::mirror(cond);
 433                 }
 434 
 435                 BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
 436                 BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
 437                 if (tblock != fblock && !if_->is_safepoint()) {
 438                   If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
 439                                      tblock, fblock, if_->state_before(), if_->is_safepoint());
 440                   newif->set_state(if_->state()->copy());
 441 
 442                   assert(prev->next() == if_, "must be guaranteed by above search");
 443                   NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
 444                   prev->set_next(newif);
 445                   block->set_end(newif);
 446 
 447                   _merge_count++;
 448                   if (PrintBlockElimination) {
 449                     tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
 450                   }
 451 
 452                   _hir->verify();
 453                 }
 454               }
 455             }
 456           }
 457         }
 458 
 459         return true;


 491 
 492   void do_Phi            (Phi*             x);
 493   void do_Local          (Local*           x);
 494   void do_Constant       (Constant*        x);
 495   void do_LoadField      (LoadField*       x);
 496   void do_StoreField     (StoreField*      x);
 497   void do_ArrayLength    (ArrayLength*     x);
 498   void do_LoadIndexed    (LoadIndexed*     x);
 499   void do_StoreIndexed   (StoreIndexed*    x);
 500   void do_NegateOp       (NegateOp*        x);
 501   void do_ArithmeticOp   (ArithmeticOp*    x);
 502   void do_ShiftOp        (ShiftOp*         x);
 503   void do_LogicOp        (LogicOp*         x);
 504   void do_CompareOp      (CompareOp*       x);
 505   void do_IfOp           (IfOp*            x);
 506   void do_Convert        (Convert*         x);
 507   void do_NullCheck      (NullCheck*       x);
 508   void do_TypeCast       (TypeCast*        x);
 509   void do_Invoke         (Invoke*          x);
 510   void do_NewInstance    (NewInstance*     x);

 511   void do_NewTypeArray   (NewTypeArray*    x);
 512   void do_NewObjectArray (NewObjectArray*  x);
 513   void do_NewMultiArray  (NewMultiArray*   x);


 514   void do_CheckCast      (CheckCast*       x);
 515   void do_InstanceOf     (InstanceOf*      x);
 516   void do_MonitorEnter   (MonitorEnter*    x);
 517   void do_MonitorExit    (MonitorExit*     x);
 518   void do_Intrinsic      (Intrinsic*       x);
 519   void do_BlockBegin     (BlockBegin*      x);
 520   void do_Goto           (Goto*            x);
 521   void do_If             (If*              x);
 522   void do_IfInstanceOf   (IfInstanceOf*    x);
 523   void do_TableSwitch    (TableSwitch*     x);
 524   void do_LookupSwitch   (LookupSwitch*    x);
 525   void do_Return         (Return*          x);
 526   void do_Throw          (Throw*           x);
 527   void do_Base           (Base*            x);
 528   void do_OsrEntry       (OsrEntry*        x);
 529   void do_ExceptionObject(ExceptionObject* x);
 530   void do_RoundFP        (RoundFP*         x);
 531   void do_UnsafeGetRaw   (UnsafeGetRaw*    x);
 532   void do_UnsafePutRaw   (UnsafePutRaw*    x);
 533   void do_UnsafeGetObject(UnsafeGetObject* x);


 639     _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck);
 640     _last_explicit_null_check->set_can_trap(false);
 641     return _last_explicit_null_check;
 642   }
 643   void        clear_last_explicit_null_check()               { _last_explicit_null_check = NULL; }
 644 
 645   // Handlers for relevant instructions
 646   // (separated out from NullCheckVisitor for clarity)
 647 
 648   // The basic contract is that these must leave the instruction in
 649   // the desired state; must not assume anything about the state of
 650   // the instruction. We make multiple passes over some basic blocks
 651   // and the last pass is the only one whose result is valid.
 652   void handle_AccessField     (AccessField* x);
 653   void handle_ArrayLength     (ArrayLength* x);
 654   void handle_LoadIndexed     (LoadIndexed* x);
 655   void handle_StoreIndexed    (StoreIndexed* x);
 656   void handle_NullCheck       (NullCheck* x);
 657   void handle_Invoke          (Invoke* x);
 658   void handle_NewInstance     (NewInstance* x);

 659   void handle_NewArray        (NewArray* x);
 660   void handle_AccessMonitor   (AccessMonitor* x);
 661   void handle_Intrinsic       (Intrinsic* x);
 662   void handle_ExceptionObject (ExceptionObject* x);
 663   void handle_Phi             (Phi* x);
 664   void handle_ProfileCall     (ProfileCall* x);
 665   void handle_ProfileReturnType (ProfileReturnType* x);
 666 };
 667 
 668 
 669 // NEEDS_CLEANUP
 670 // There may be other instructions which need to clear the last
 671 // explicit null check. Anything across which we can not hoist the
 672 // debug information for a NullCheck instruction must clear it. It
 673 // might be safer to pattern match "NullCheck ; {AccessField,
 674 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
 675 // Should test to see performance hit of clearing it for all handlers
 676 // with empty bodies below. If it is negligible then we should leave
 677 // that in for safety, otherwise should think more about it.
 678 void NullCheckVisitor::do_Phi            (Phi*             x) { nce()->handle_Phi(x);      }
 679 void NullCheckVisitor::do_Local          (Local*           x) {}
 680 void NullCheckVisitor::do_Constant       (Constant*        x) { /* FIXME: handle object constants */ }
 681 void NullCheckVisitor::do_LoadField      (LoadField*       x) { nce()->handle_AccessField(x); }
 682 void NullCheckVisitor::do_StoreField     (StoreField*      x) { nce()->handle_AccessField(x); }
 683 void NullCheckVisitor::do_ArrayLength    (ArrayLength*     x) { nce()->handle_ArrayLength(x); }
 684 void NullCheckVisitor::do_LoadIndexed    (LoadIndexed*     x) { nce()->handle_LoadIndexed(x); }
 685 void NullCheckVisitor::do_StoreIndexed   (StoreIndexed*    x) { nce()->handle_StoreIndexed(x); }
 686 void NullCheckVisitor::do_NegateOp       (NegateOp*        x) {}
 687 void NullCheckVisitor::do_ArithmeticOp   (ArithmeticOp*    x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); }
 688 void NullCheckVisitor::do_ShiftOp        (ShiftOp*         x) {}
 689 void NullCheckVisitor::do_LogicOp        (LogicOp*         x) {}
 690 void NullCheckVisitor::do_CompareOp      (CompareOp*       x) {}
 691 void NullCheckVisitor::do_IfOp           (IfOp*            x) {}
 692 void NullCheckVisitor::do_Convert        (Convert*         x) {}
 693 void NullCheckVisitor::do_NullCheck      (NullCheck*       x) { nce()->handle_NullCheck(x); }
 694 void NullCheckVisitor::do_TypeCast       (TypeCast*        x) {}
 695 void NullCheckVisitor::do_Invoke         (Invoke*          x) { nce()->handle_Invoke(x); }
 696 void NullCheckVisitor::do_NewInstance    (NewInstance*     x) { nce()->handle_NewInstance(x); }

 697 void NullCheckVisitor::do_NewTypeArray   (NewTypeArray*    x) { nce()->handle_NewArray(x); }
 698 void NullCheckVisitor::do_NewObjectArray (NewObjectArray*  x) { nce()->handle_NewArray(x); }
 699 void NullCheckVisitor::do_NewMultiArray  (NewMultiArray*   x) { nce()->handle_NewArray(x); }


 700 void NullCheckVisitor::do_CheckCast      (CheckCast*       x) { nce()->clear_last_explicit_null_check(); }
 701 void NullCheckVisitor::do_InstanceOf     (InstanceOf*      x) {}
 702 void NullCheckVisitor::do_MonitorEnter   (MonitorEnter*    x) { nce()->handle_AccessMonitor(x); }
 703 void NullCheckVisitor::do_MonitorExit    (MonitorExit*     x) { nce()->handle_AccessMonitor(x); }
 704 void NullCheckVisitor::do_Intrinsic      (Intrinsic*       x) { nce()->handle_Intrinsic(x);     }
 705 void NullCheckVisitor::do_BlockBegin     (BlockBegin*      x) {}
 706 void NullCheckVisitor::do_Goto           (Goto*            x) {}
 707 void NullCheckVisitor::do_If             (If*              x) {}
 708 void NullCheckVisitor::do_IfInstanceOf   (IfInstanceOf*    x) {}
 709 void NullCheckVisitor::do_TableSwitch    (TableSwitch*     x) {}
 710 void NullCheckVisitor::do_LookupSwitch   (LookupSwitch*    x) {}
 711 void NullCheckVisitor::do_Return         (Return*          x) {}
 712 void NullCheckVisitor::do_Throw          (Throw*           x) { nce()->clear_last_explicit_null_check(); }
 713 void NullCheckVisitor::do_Base           (Base*            x) {}
 714 void NullCheckVisitor::do_OsrEntry       (OsrEntry*        x) {}
 715 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
 716 void NullCheckVisitor::do_RoundFP        (RoundFP*         x) {}
 717 void NullCheckVisitor::do_UnsafeGetRaw   (UnsafeGetRaw*    x) {}
 718 void NullCheckVisitor::do_UnsafePutRaw   (UnsafePutRaw*    x) {}
 719 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}


 851     }
 852   }
 853 }
 854 
 855 
 856 void NullCheckEliminator::iterate(BlockBegin* block) {
 857   work_list()->push(block);
 858   iterate_all();
 859 }
 860 
 861 void NullCheckEliminator::handle_AccessField(AccessField* x) {
 862   if (x->is_static()) {
 863     if (x->as_LoadField() != NULL) {
 864       // If the field is a non-null static final object field (as is
 865       // often the case for sun.misc.Unsafe), put this LoadField into
 866       // the non-null map
 867       ciField* field = x->field();
 868       if (field->is_constant()) {
 869         ciConstant field_val = field->constant_value();
 870         BasicType field_type = field_val.basic_type();
 871         if (field_type == T_OBJECT || field_type == T_ARRAY) {
 872           ciObject* obj_val = field_val.as_object();
 873           if (!obj_val->is_null_object()) {
 874             if (PrintNullCheckElimination) {
 875               tty->print_cr("AccessField %d proven non-null by static final non-null oop check",
 876                             x->id());
 877             }
 878             set_put(x);
 879           }
 880         }
 881       }
 882     }
 883     // Be conservative
 884     clear_last_explicit_null_check();
 885     return;
 886   }
 887 
 888   Value obj = x->obj();
 889   if (set_contains(obj)) {
 890     // Value is non-null => update AccessField
 891     if (last_explicit_null_check_obj() == obj && !x->needs_patching()) {


1026     // Be conservative
1027     clear_last_explicit_null_check();
1028     return;
1029   }
1030 
1031   Value recv = x->receiver();
1032   if (!set_contains(recv)) {
1033     set_put(recv);
1034     if (PrintNullCheckElimination) {
1035       tty->print_cr("Invoke %d of value %d proves value to be non-null", x->id(), recv->id());
1036     }
1037   }
1038   clear_last_explicit_null_check();
1039 }
1040 
1041 
1042 void NullCheckEliminator::handle_NewInstance(NewInstance* x) {
1043   set_put(x);
1044   if (PrintNullCheckElimination) {
1045     tty->print_cr("NewInstance %d is non-null", x->id());







1046   }
1047 }
1048 
1049 
1050 void NullCheckEliminator::handle_NewArray(NewArray* x) {
1051   set_put(x);
1052   if (PrintNullCheckElimination) {
1053     tty->print_cr("NewArray %d is non-null", x->id());
1054   }
1055 }
1056 
1057 
1058 void NullCheckEliminator::handle_ExceptionObject(ExceptionObject* x) {
1059   set_put(x);
1060   if (PrintNullCheckElimination) {
1061     tty->print_cr("ExceptionObject %d is non-null", x->id());
1062   }
1063 }
1064 
1065 


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


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


 183 
 184   // 2) substitute conditional expression
 185   //    with an IfOp followed by a Goto
 186   // cut if_ away and get node before
 187   Instruction* cur_end = if_->prev();
 188 
 189   // append constants of true- and false-block if necessary
 190   // clone constants because original block must not be destroyed
 191   assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
 192   if (t_value == t_const) {
 193     t_value = new Constant(t_const->type());
 194     NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
 195     cur_end = cur_end->set_next(t_value);
 196   }
 197   if (f_value == f_const) {
 198     f_value = new Constant(f_const->type());
 199     NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
 200     cur_end = cur_end->set_next(f_value);
 201   }
 202 
 203   Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value,
 204                            if_->state_before(), if_->substitutability_check());
 205   assert(result != NULL, "make_ifop must return a non-null instruction");
 206   if (!result->is_linked() && result->can_be_linked()) {
 207     NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
 208     cur_end = cur_end->set_next(result);
 209   }
 210 
 211   // append Goto to successor
 212   ValueStack* state_before = if_->state_before();
 213   Goto* goto_ = new Goto(sux, state_before, is_safepoint);
 214 
 215   // prepare state for Goto
 216   ValueStack* goto_state = if_state;
 217   goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
 218   goto_state->push(result->type(), result);
 219   assert(goto_state->is_same(sux_state), "states must match now");
 220   goto_->set_state(goto_state);
 221 
 222   cur_end = cur_end->set_next(goto_, goto_state->bci());
 223 
 224   // Adjust control flow graph


 236   // update block end
 237   block->set_end(goto_);
 238 
 239   // substitute the phi if possible
 240   if (sux_phi->as_Phi()->operand_count() == 1) {
 241     assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
 242     sux_phi->set_subst(result);
 243     _has_substitution = true;
 244   }
 245 
 246   // 3) successfully eliminated a conditional expression
 247   _cee_count++;
 248   if (PrintCEE) {
 249     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());
 250     tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
 251   }
 252 
 253   _hir->verify();
 254 }
 255 
 256 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval,
 257                                ValueStack* state_before, bool substitutability_check) {
 258   if (!OptimizeIfOps) {
 259     return new IfOp(x, cond, y, tval, fval, state_before, substitutability_check);
 260   }
 261 
 262   tval = tval->subst();
 263   fval = fval->subst();
 264   if (tval == fval) {
 265     _ifop_count++;
 266     return tval;
 267   }
 268 
 269   x = x->subst();
 270   y = y->subst();
 271 
 272   Constant* y_const = y->as_Constant();
 273   if (y_const != NULL) {
 274     IfOp* x_ifop = x->as_IfOp();
 275     if (x_ifop != NULL) {                 // x is an ifop, y is a constant
 276       Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
 277       Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
 278 
 279       if (x_tval_const != NULL && x_fval_const != NULL) {
 280         Instruction::Condition x_ifop_cond = x_ifop->cond();
 281 
 282         Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
 283         Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
 284 
 285         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 286         if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
 287           Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
 288           Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
 289 
 290           _ifop_count++;
 291           if (new_tval == new_fval) {
 292             return new_tval;
 293           } else {
 294             return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval, state_before, substitutability_check);
 295           }
 296         }
 297       }
 298     } else {
 299       Constant* x_const = x->as_Constant();
 300       if (x_const != NULL) {         // x and y are constants
 301         Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
 302         // not_comparable here is a valid return in case we're comparing unloaded oop constants
 303         if (x_compare_res != Constant::not_comparable) {
 304           _ifop_count++;
 305           return x_compare_res == Constant::cond_true ? tval : fval;
 306         }
 307       }
 308     }
 309   }
 310   return new IfOp(x, cond, y, tval, fval, state_before, substitutability_check);
 311 }
 312 
 313 void Optimizer::eliminate_conditional_expressions() {
 314   // find conditional expressions & replace them with IfOps
 315   CE_Eliminator ce(ir());
 316 }
 317 
 318 class BlockMerger: public BlockClosure {
 319  private:
 320   IR* _hir;
 321   int _merge_count;              // the number of block pairs successfully merged
 322 
 323  public:
 324   BlockMerger(IR* hir)
 325   : _hir(hir)
 326   , _merge_count(0)
 327   {
 328     _hir->iterate_preorder(this);
 329     CompileLog* log = _hir->compilation()->log();
 330     if (log != NULL)


 422               // When if_ and ifop are not in the same block, prev
 423               // becomes NULL In such (rare) cases it is not
 424               // profitable to perform the optimization.
 425               Value prev = ifop;
 426               while (prev != NULL && prev->next() != if_) {
 427                 prev = prev->next();
 428               }
 429 
 430               if (prev != NULL) {
 431                 Instruction::Condition cond = if_->cond();
 432                 BlockBegin* tsux = if_->tsux();
 433                 BlockBegin* fsux = if_->fsux();
 434                 if (swapped) {
 435                   cond = Instruction::mirror(cond);
 436                 }
 437 
 438                 BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
 439                 BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
 440                 if (tblock != fblock && !if_->is_safepoint()) {
 441                   If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
 442                                      tblock, fblock, if_->state_before(), if_->is_safepoint(), if_->substitutability_check());
 443                   newif->set_state(if_->state()->copy());
 444 
 445                   assert(prev->next() == if_, "must be guaranteed by above search");
 446                   NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
 447                   prev->set_next(newif);
 448                   block->set_end(newif);
 449 
 450                   _merge_count++;
 451                   if (PrintBlockElimination) {
 452                     tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
 453                   }
 454 
 455                   _hir->verify();
 456                 }
 457               }
 458             }
 459           }
 460         }
 461 
 462         return true;


 494 
 495   void do_Phi            (Phi*             x);
 496   void do_Local          (Local*           x);
 497   void do_Constant       (Constant*        x);
 498   void do_LoadField      (LoadField*       x);
 499   void do_StoreField     (StoreField*      x);
 500   void do_ArrayLength    (ArrayLength*     x);
 501   void do_LoadIndexed    (LoadIndexed*     x);
 502   void do_StoreIndexed   (StoreIndexed*    x);
 503   void do_NegateOp       (NegateOp*        x);
 504   void do_ArithmeticOp   (ArithmeticOp*    x);
 505   void do_ShiftOp        (ShiftOp*         x);
 506   void do_LogicOp        (LogicOp*         x);
 507   void do_CompareOp      (CompareOp*       x);
 508   void do_IfOp           (IfOp*            x);
 509   void do_Convert        (Convert*         x);
 510   void do_NullCheck      (NullCheck*       x);
 511   void do_TypeCast       (TypeCast*        x);
 512   void do_Invoke         (Invoke*          x);
 513   void do_NewInstance    (NewInstance*     x);
 514   void do_NewValueTypeInstance(NewValueTypeInstance* x);
 515   void do_NewTypeArray   (NewTypeArray*    x);
 516   void do_NewObjectArray (NewObjectArray*  x);
 517   void do_NewMultiArray  (NewMultiArray*   x);
 518   void do_WithField      (WithField*       x);
 519   void do_DefaultValue   (DefaultValue*    x);
 520   void do_CheckCast      (CheckCast*       x);
 521   void do_InstanceOf     (InstanceOf*      x);
 522   void do_MonitorEnter   (MonitorEnter*    x);
 523   void do_MonitorExit    (MonitorExit*     x);
 524   void do_Intrinsic      (Intrinsic*       x);
 525   void do_BlockBegin     (BlockBegin*      x);
 526   void do_Goto           (Goto*            x);
 527   void do_If             (If*              x);
 528   void do_IfInstanceOf   (IfInstanceOf*    x);
 529   void do_TableSwitch    (TableSwitch*     x);
 530   void do_LookupSwitch   (LookupSwitch*    x);
 531   void do_Return         (Return*          x);
 532   void do_Throw          (Throw*           x);
 533   void do_Base           (Base*            x);
 534   void do_OsrEntry       (OsrEntry*        x);
 535   void do_ExceptionObject(ExceptionObject* x);
 536   void do_RoundFP        (RoundFP*         x);
 537   void do_UnsafeGetRaw   (UnsafeGetRaw*    x);
 538   void do_UnsafePutRaw   (UnsafePutRaw*    x);
 539   void do_UnsafeGetObject(UnsafeGetObject* x);


 645     _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck);
 646     _last_explicit_null_check->set_can_trap(false);
 647     return _last_explicit_null_check;
 648   }
 649   void        clear_last_explicit_null_check()               { _last_explicit_null_check = NULL; }
 650 
 651   // Handlers for relevant instructions
 652   // (separated out from NullCheckVisitor for clarity)
 653 
 654   // The basic contract is that these must leave the instruction in
 655   // the desired state; must not assume anything about the state of
 656   // the instruction. We make multiple passes over some basic blocks
 657   // and the last pass is the only one whose result is valid.
 658   void handle_AccessField     (AccessField* x);
 659   void handle_ArrayLength     (ArrayLength* x);
 660   void handle_LoadIndexed     (LoadIndexed* x);
 661   void handle_StoreIndexed    (StoreIndexed* x);
 662   void handle_NullCheck       (NullCheck* x);
 663   void handle_Invoke          (Invoke* x);
 664   void handle_NewInstance     (NewInstance* x);
 665   void handle_NewValueTypeInstance(NewValueTypeInstance* x);
 666   void handle_NewArray        (NewArray* x);
 667   void handle_AccessMonitor   (AccessMonitor* x);
 668   void handle_Intrinsic       (Intrinsic* x);
 669   void handle_ExceptionObject (ExceptionObject* x);
 670   void handle_Phi             (Phi* x);
 671   void handle_ProfileCall     (ProfileCall* x);
 672   void handle_ProfileReturnType (ProfileReturnType* x);
 673 };
 674 
 675 
 676 // NEEDS_CLEANUP
 677 // There may be other instructions which need to clear the last
 678 // explicit null check. Anything across which we can not hoist the
 679 // debug information for a NullCheck instruction must clear it. It
 680 // might be safer to pattern match "NullCheck ; {AccessField,
 681 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
 682 // Should test to see performance hit of clearing it for all handlers
 683 // with empty bodies below. If it is negligible then we should leave
 684 // that in for safety, otherwise should think more about it.
 685 void NullCheckVisitor::do_Phi            (Phi*             x) { nce()->handle_Phi(x);      }
 686 void NullCheckVisitor::do_Local          (Local*           x) {}
 687 void NullCheckVisitor::do_Constant       (Constant*        x) { /* FIXME: handle object constants */ }
 688 void NullCheckVisitor::do_LoadField      (LoadField*       x) { nce()->handle_AccessField(x); }
 689 void NullCheckVisitor::do_StoreField     (StoreField*      x) { nce()->handle_AccessField(x); }
 690 void NullCheckVisitor::do_ArrayLength    (ArrayLength*     x) { nce()->handle_ArrayLength(x); }
 691 void NullCheckVisitor::do_LoadIndexed    (LoadIndexed*     x) { nce()->handle_LoadIndexed(x); }
 692 void NullCheckVisitor::do_StoreIndexed   (StoreIndexed*    x) { nce()->handle_StoreIndexed(x); }
 693 void NullCheckVisitor::do_NegateOp       (NegateOp*        x) {}
 694 void NullCheckVisitor::do_ArithmeticOp   (ArithmeticOp*    x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); }
 695 void NullCheckVisitor::do_ShiftOp        (ShiftOp*         x) {}
 696 void NullCheckVisitor::do_LogicOp        (LogicOp*         x) {}
 697 void NullCheckVisitor::do_CompareOp      (CompareOp*       x) {}
 698 void NullCheckVisitor::do_IfOp           (IfOp*            x) {}
 699 void NullCheckVisitor::do_Convert        (Convert*         x) {}
 700 void NullCheckVisitor::do_NullCheck      (NullCheck*       x) { nce()->handle_NullCheck(x); }
 701 void NullCheckVisitor::do_TypeCast       (TypeCast*        x) {}
 702 void NullCheckVisitor::do_Invoke         (Invoke*          x) { nce()->handle_Invoke(x); }
 703 void NullCheckVisitor::do_NewInstance    (NewInstance*     x) { nce()->handle_NewInstance(x); }
 704 void NullCheckVisitor::do_NewValueTypeInstance(NewValueTypeInstance*     x) { nce()->handle_NewValueTypeInstance(x); }
 705 void NullCheckVisitor::do_NewTypeArray   (NewTypeArray*    x) { nce()->handle_NewArray(x); }
 706 void NullCheckVisitor::do_NewObjectArray (NewObjectArray*  x) { nce()->handle_NewArray(x); }
 707 void NullCheckVisitor::do_NewMultiArray  (NewMultiArray*   x) { nce()->handle_NewArray(x); }
 708 void NullCheckVisitor::do_WithField      (WithField*       x) {}
 709 void NullCheckVisitor::do_DefaultValue   (DefaultValue*    x) {}
 710 void NullCheckVisitor::do_CheckCast      (CheckCast*       x) { nce()->clear_last_explicit_null_check(); }
 711 void NullCheckVisitor::do_InstanceOf     (InstanceOf*      x) {}
 712 void NullCheckVisitor::do_MonitorEnter   (MonitorEnter*    x) { nce()->handle_AccessMonitor(x); }
 713 void NullCheckVisitor::do_MonitorExit    (MonitorExit*     x) { nce()->handle_AccessMonitor(x); }
 714 void NullCheckVisitor::do_Intrinsic      (Intrinsic*       x) { nce()->handle_Intrinsic(x);     }
 715 void NullCheckVisitor::do_BlockBegin     (BlockBegin*      x) {}
 716 void NullCheckVisitor::do_Goto           (Goto*            x) {}
 717 void NullCheckVisitor::do_If             (If*              x) {}
 718 void NullCheckVisitor::do_IfInstanceOf   (IfInstanceOf*    x) {}
 719 void NullCheckVisitor::do_TableSwitch    (TableSwitch*     x) {}
 720 void NullCheckVisitor::do_LookupSwitch   (LookupSwitch*    x) {}
 721 void NullCheckVisitor::do_Return         (Return*          x) {}
 722 void NullCheckVisitor::do_Throw          (Throw*           x) { nce()->clear_last_explicit_null_check(); }
 723 void NullCheckVisitor::do_Base           (Base*            x) {}
 724 void NullCheckVisitor::do_OsrEntry       (OsrEntry*        x) {}
 725 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
 726 void NullCheckVisitor::do_RoundFP        (RoundFP*         x) {}
 727 void NullCheckVisitor::do_UnsafeGetRaw   (UnsafeGetRaw*    x) {}
 728 void NullCheckVisitor::do_UnsafePutRaw   (UnsafePutRaw*    x) {}
 729 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}


 861     }
 862   }
 863 }
 864 
 865 
 866 void NullCheckEliminator::iterate(BlockBegin* block) {
 867   work_list()->push(block);
 868   iterate_all();
 869 }
 870 
 871 void NullCheckEliminator::handle_AccessField(AccessField* x) {
 872   if (x->is_static()) {
 873     if (x->as_LoadField() != NULL) {
 874       // If the field is a non-null static final object field (as is
 875       // often the case for sun.misc.Unsafe), put this LoadField into
 876       // the non-null map
 877       ciField* field = x->field();
 878       if (field->is_constant()) {
 879         ciConstant field_val = field->constant_value();
 880         BasicType field_type = field_val.basic_type();
 881         if (field_type == T_OBJECT || field_type == T_ARRAY || field_type == T_VALUETYPE) {
 882           ciObject* obj_val = field_val.as_object();
 883           if (!obj_val->is_null_object()) {
 884             if (PrintNullCheckElimination) {
 885               tty->print_cr("AccessField %d proven non-null by static final non-null oop check",
 886                             x->id());
 887             }
 888             set_put(x);
 889           }
 890         }
 891       }
 892     }
 893     // Be conservative
 894     clear_last_explicit_null_check();
 895     return;
 896   }
 897 
 898   Value obj = x->obj();
 899   if (set_contains(obj)) {
 900     // Value is non-null => update AccessField
 901     if (last_explicit_null_check_obj() == obj && !x->needs_patching()) {


1036     // Be conservative
1037     clear_last_explicit_null_check();
1038     return;
1039   }
1040 
1041   Value recv = x->receiver();
1042   if (!set_contains(recv)) {
1043     set_put(recv);
1044     if (PrintNullCheckElimination) {
1045       tty->print_cr("Invoke %d of value %d proves value to be non-null", x->id(), recv->id());
1046     }
1047   }
1048   clear_last_explicit_null_check();
1049 }
1050 
1051 
1052 void NullCheckEliminator::handle_NewInstance(NewInstance* x) {
1053   set_put(x);
1054   if (PrintNullCheckElimination) {
1055     tty->print_cr("NewInstance %d is non-null", x->id());
1056   }
1057 }
1058 
1059 void NullCheckEliminator::handle_NewValueTypeInstance(NewValueTypeInstance* x) {
1060   set_put(x);
1061   if (PrintNullCheckElimination) {
1062     tty->print_cr("NewValueTypeInstance %d is non-null", x->id());
1063   }
1064 }
1065 
1066 
1067 void NullCheckEliminator::handle_NewArray(NewArray* x) {
1068   set_put(x);
1069   if (PrintNullCheckElimination) {
1070     tty->print_cr("NewArray %d is non-null", x->id());
1071   }
1072 }
1073 
1074 
1075 void NullCheckEliminator::handle_ExceptionObject(ExceptionObject* x) {
1076   set_put(x);
1077   if (PrintNullCheckElimination) {
1078     tty->print_cr("ExceptionObject %d is non-null", x->id());
1079   }
1080 }
1081 
1082 


< prev index next >