src/share/vm/c1/c1_Instruction.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:


  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "c1/c1_IR.hpp"
  27 #include "c1/c1_Instruction.hpp"
  28 #include "c1/c1_InstructionPrinter.hpp"
  29 #include "c1/c1_ValueStack.hpp"
  30 #include "ci/ciObjArrayKlass.hpp"
  31 #include "ci/ciTypeArrayKlass.hpp"
  32 
  33 
  34 // Implementation of Instruction
  35 
  36 









  37 Instruction::Condition Instruction::mirror(Condition cond) {
  38   switch (cond) {
  39     case eql: return eql;
  40     case neq: return neq;
  41     case lss: return gtr;
  42     case leq: return geq;
  43     case gtr: return lss;
  44     case geq: return leq;


  45   }
  46   ShouldNotReachHere();
  47   return eql;
  48 }
  49 
  50 
  51 Instruction::Condition Instruction::negate(Condition cond) {
  52   switch (cond) {
  53     case eql: return neq;
  54     case neq: return eql;
  55     case lss: return geq;
  56     case leq: return gtr;
  57     case gtr: return leq;
  58     case geq: return lss;


  59   }
  60   ShouldNotReachHere();
  61   return eql;
  62 }
  63 
  64 void Instruction::update_exception_state(ValueStack* state) {
  65   if (state != NULL && (state->kind() == ValueStack::EmptyExceptionState || state->kind() == ValueStack::ExceptionState)) {
  66     assert(state->kind() == ValueStack::EmptyExceptionState || Compilation::current()->env()->jvmti_can_access_local_variables(), "unexpected state kind");
  67     _exception_state = state;
  68   } else {
  69     _exception_state = NULL;
  70   }
  71 }
  72 
  73 
  74 Instruction* Instruction::prev(BlockBegin* block) {
  75   Instruction* p = NULL;
  76   Instruction* q = block;
  77   while (q != this) {
  78     assert(q != NULL, "this is not in the block's instruction list");
  79     p = q; q = q->next();
  80   }
  81   return p;
  82 }
  83 
  84 
  85 void Instruction::state_values_do(ValueVisitor* f) {
  86   if (state_before() != NULL) {
  87     state_before()->values_do(f);
  88   }
  89   if (exception_state() != NULL){
  90     exception_state()->values_do(f);
  91   }
  92 }
  93 
  94 
  95 #ifndef PRODUCT
  96 void Instruction::check_state(ValueStack* state) {


 105   print(ip);
 106 }
 107 
 108 
 109 void Instruction::print_line() {
 110   InstructionPrinter ip;
 111   ip.print_line(this);
 112 }
 113 
 114 
 115 void Instruction::print(InstructionPrinter& ip) {
 116   ip.print_head();
 117   ip.print_line(this);
 118   tty->cr();
 119 }
 120 #endif // PRODUCT
 121 
 122 
 123 // perform constant and interval tests on index value
 124 bool AccessIndexed::compute_needs_range_check() {



 125   Constant* clength = length()->as_Constant();
 126   Constant* cindex = index()->as_Constant();
 127   if (clength && cindex) {
 128     IntConstant* l = clength->type()->as_IntConstant();
 129     IntConstant* i = cindex->type()->as_IntConstant();
 130     if (l && i && i->value() < l->value() && i->value() >= 0) {
 131       return false;
 132     }
 133   }






 134   return true;
 135 }
 136 
 137 
 138 ciType* Local::exact_type() const {
 139   ciType* type = declared_type();
 140 
 141   // for primitive arrays, the declared type is the exact type
 142   if (type->is_type_array_klass()) {
 143     return type;
 144   } else if (type->is_instance_klass()) {
 145     ciInstanceKlass* ik = (ciInstanceKlass*)type;
 146     if (ik->is_loaded() && ik->is_final() && !ik->is_interface()) {
 147       return type;
 148     }
 149   } else if (type->is_obj_array_klass()) {
 150     ciObjArrayKlass* oak = (ciObjArrayKlass*)type;
 151     ciType* base = oak->base_element_type();
 152     if (base->is_instance_klass()) {
 153       ciInstanceKlass* ik = base->as_instance_klass();


 614 void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
 615   // modify predecessors before substituting successors
 616   for (int i = 0; i < number_of_sux(); i++) {
 617     if (sux_at(i) == old_sux) {
 618       // remove old predecessor before adding new predecessor
 619       // otherwise there is a dead predecessor in the list
 620       new_sux->remove_predecessor(old_sux);
 621       new_sux->add_predecessor(this);
 622     }
 623   }
 624   old_sux->remove_predecessor(this);
 625   end()->substitute_sux(old_sux, new_sux);
 626 }
 627 
 628 
 629 
 630 // In general it is not possible to calculate a value for the field "depth_first_number"
 631 // of the inserted block, without recomputing the values of the other blocks
 632 // in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless.
 633 BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) {
 634   BlockBegin* new_sux = new BlockBegin(end()->state()->bci());






 635 
 636   // mark this block (special treatment when block order is computed)
 637   new_sux->set(critical_edge_split_flag);
 638 
 639   // This goto is not a safepoint.
 640   Goto* e = new Goto(sux, false);
 641   new_sux->set_next(e, end()->state()->bci());
 642   new_sux->set_end(e);
 643   // setup states
 644   ValueStack* s = end()->state();
 645   new_sux->set_state(s->copy());
 646   e->set_state(s->copy());
 647   assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!");
 648   assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!");
 649   assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!");
 650 
 651   // link predecessor to new block
 652   end()->substitute_sux(sux, new_sux);
 653 
 654   // The ordering needs to be the same, so remove the link that the
 655   // set_end call above added and substitute the new_sux for this
 656   // block.
 657   sux->remove_predecessor(new_sux);
 658 
 659   // the successor could be the target of a switch so it might have
 660   // multiple copies of this predecessor, so substitute the new_sux
 661   // for the first and delete the rest.
 662   bool assigned = false;
 663   BlockList& list = sux->_predecessors;
 664   for (int i = 0; i < list.length(); i++) {
 665     BlockBegin** b = list.adr_at(i);
 666     if (*b == this) {


 943 void BlockList::print(bool cfg_only, bool live_only) {
 944   InstructionPrinter ip;
 945   for (int i = 0; i < length(); i++) {
 946     BlockBegin* block = at(i);
 947     if (cfg_only) {
 948       ip.print_instr(block); tty->cr();
 949     } else {
 950       block->print_block(ip, live_only);
 951     }
 952   }
 953 }
 954 #endif // PRODUCT
 955 
 956 
 957 // Implementation of BlockEnd
 958 
 959 void BlockEnd::set_begin(BlockBegin* begin) {
 960   BlockList* sux = NULL;
 961   if (begin != NULL) {
 962     sux = begin->successors();
 963   } else if (_begin != NULL) {
 964     // copy our sux list
 965     BlockList* sux = new BlockList(_begin->number_of_sux());
 966     for (int i = 0; i < _begin->number_of_sux(); i++) {
 967       sux->append(_begin->sux_at(i));
 968     }
 969   }
 970   _sux = sux;
 971   _begin = begin;
 972 }
 973 
 974 
 975 void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
 976   substitute(*_sux, old_sux, new_sux);
 977 }
 978 
 979 
 980 // Implementation of Phi
 981 
 982 // Normal phi functions take their operands from the last instruction of the
 983 // predecessor. Special handling is needed for xhanlder entries because there
 984 // the state of arbitrary instructions are needed.
 985 
 986 Value Phi::operand_at(int i) const {
 987   ValueStack* state;
 988   if (_block->is_set(BlockBegin::exception_entry_flag)) {
 989     state = _block->exception_state_at(i);
 990   } else {
 991     state = _block->pred_at(i)->end()->state();
 992   }
 993   assert(state != NULL, "");
 994 
 995   if (is_local()) {
 996     return state->local_at(local_index());
 997   } else {
 998     return state->stack_at(stack_index());
 999   }
1000 }
1001 
1002 
1003 int Phi::operand_count() const {
1004   if (_block->is_set(BlockBegin::exception_entry_flag)) {
1005     return _block->number_of_exception_states();
1006   } else {
1007     return _block->number_of_preds();
1008   }
1009 }
1010 

1011 






























1012 
1013 void ProfileInvoke::state_values_do(ValueVisitor* f) {
1014   if (state() != NULL) state()->values_do(f);
1015 }


  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "c1/c1_IR.hpp"
  27 #include "c1/c1_Instruction.hpp"
  28 #include "c1/c1_InstructionPrinter.hpp"
  29 #include "c1/c1_ValueStack.hpp"
  30 #include "ci/ciObjArrayKlass.hpp"
  31 #include "ci/ciTypeArrayKlass.hpp"
  32 
  33 
  34 // Implementation of Instruction
  35 
  36 
  37 int Instruction::dominator_depth() {
  38   int result = -1;
  39   if (block()) {
  40     result = block()->dominator_depth();
  41   }
  42   assert(result != -1 || this->as_Local(), "Only locals have dominator depth -1");
  43   return result;
  44 }
  45 
  46 Instruction::Condition Instruction::mirror(Condition cond) {
  47   switch (cond) {
  48     case eql: return eql;
  49     case neq: return neq;
  50     case lss: return gtr;
  51     case leq: return geq;
  52     case gtr: return lss;
  53     case geq: return leq;
  54     case aeq: return beq;
  55     case beq: return aeq;
  56   }
  57   ShouldNotReachHere();
  58   return eql;
  59 }
  60 
  61 
  62 Instruction::Condition Instruction::negate(Condition cond) {
  63   switch (cond) {
  64     case eql: return neq;
  65     case neq: return eql;
  66     case lss: return geq;
  67     case leq: return gtr;
  68     case gtr: return leq;
  69     case geq: return lss;
  70     case aeq: assert(false, "Above equal cannot be negated");
  71     case beq: assert(false, "Below equal cannot be negated");
  72   }
  73   ShouldNotReachHere();
  74   return eql;
  75 }
  76 
  77 void Instruction::update_exception_state(ValueStack* state) {
  78   if (state != NULL && (state->kind() == ValueStack::EmptyExceptionState || state->kind() == ValueStack::ExceptionState)) {
  79     assert(state->kind() == ValueStack::EmptyExceptionState || Compilation::current()->env()->jvmti_can_access_local_variables(), "unexpected state kind");
  80     _exception_state = state;
  81   } else {
  82     _exception_state = NULL;
  83   }
  84 }
  85 
  86 // Prev without need to have BlockBegin
  87 Instruction* Instruction::prev() {
  88   Instruction* p = NULL;
  89   Instruction* q = block();
  90   while (q != this) {
  91     assert(q != NULL, "this is not in the block's instruction list");
  92     p = q; q = q->next();
  93   }
  94   return p;
  95 }
  96 
  97 
  98 void Instruction::state_values_do(ValueVisitor* f) {
  99   if (state_before() != NULL) {
 100     state_before()->values_do(f);
 101   }
 102   if (exception_state() != NULL){
 103     exception_state()->values_do(f);
 104   }
 105 }
 106 
 107 
 108 #ifndef PRODUCT
 109 void Instruction::check_state(ValueStack* state) {


 118   print(ip);
 119 }
 120 
 121 
 122 void Instruction::print_line() {
 123   InstructionPrinter ip;
 124   ip.print_line(this);
 125 }
 126 
 127 
 128 void Instruction::print(InstructionPrinter& ip) {
 129   ip.print_head();
 130   ip.print_line(this);
 131   tty->cr();
 132 }
 133 #endif // PRODUCT
 134 
 135 
 136 // perform constant and interval tests on index value
 137 bool AccessIndexed::compute_needs_range_check() {
 138 
 139   if (length()) {
 140 
 141     Constant* clength = length()->as_Constant();
 142     Constant* cindex = index()->as_Constant();
 143     if (clength && cindex) {
 144       IntConstant* l = clength->type()->as_IntConstant();
 145       IntConstant* i = cindex->type()->as_IntConstant();
 146       if (l && i && i->value() < l->value() && i->value() >= 0) {
 147         return false;
 148       }
 149     }
 150   }
 151 
 152   if (!this->check_flag(NeedsRangeCheckFlag)) {
 153     return false;
 154   }
 155 
 156   return true;
 157 }
 158 
 159 
 160 ciType* Local::exact_type() const {
 161   ciType* type = declared_type();
 162 
 163   // for primitive arrays, the declared type is the exact type
 164   if (type->is_type_array_klass()) {
 165     return type;
 166   } else if (type->is_instance_klass()) {
 167     ciInstanceKlass* ik = (ciInstanceKlass*)type;
 168     if (ik->is_loaded() && ik->is_final() && !ik->is_interface()) {
 169       return type;
 170     }
 171   } else if (type->is_obj_array_klass()) {
 172     ciObjArrayKlass* oak = (ciObjArrayKlass*)type;
 173     ciType* base = oak->base_element_type();
 174     if (base->is_instance_klass()) {
 175       ciInstanceKlass* ik = base->as_instance_klass();


 636 void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
 637   // modify predecessors before substituting successors
 638   for (int i = 0; i < number_of_sux(); i++) {
 639     if (sux_at(i) == old_sux) {
 640       // remove old predecessor before adding new predecessor
 641       // otherwise there is a dead predecessor in the list
 642       new_sux->remove_predecessor(old_sux);
 643       new_sux->add_predecessor(this);
 644     }
 645   }
 646   old_sux->remove_predecessor(this);
 647   end()->substitute_sux(old_sux, new_sux);
 648 }
 649 
 650 
 651 
 652 // In general it is not possible to calculate a value for the field "depth_first_number"
 653 // of the inserted block, without recomputing the values of the other blocks
 654 // in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless.
 655 BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) {
 656   int bci = sux->bci();
 657   // critical edge splitting may introduce a goto after a if and array
 658   // bound check elimination may insert a predicate between the if and
 659   // goto. The bci of the goto can't be the one of the if otherwise
 660   // the state and bci are inconsistent and a deoptimization triggered
 661   // by the predicate would lead to incorrect execution/a crash.
 662   BlockBegin* new_sux = new BlockBegin(bci);
 663 
 664   // mark this block (special treatment when block order is computed)
 665   new_sux->set(critical_edge_split_flag);
 666 
 667   // This goto is not a safepoint.
 668   Goto* e = new Goto(sux, false);
 669   new_sux->set_next(e, bci);
 670   new_sux->set_end(e);
 671   // setup states
 672   ValueStack* s = end()->state();
 673   new_sux->set_state(s->copy(s->kind(), bci));
 674   e->set_state(s->copy(s->kind(), bci));
 675   assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!");
 676   assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!");
 677   assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!");
 678 
 679   // link predecessor to new block
 680   end()->substitute_sux(sux, new_sux);
 681 
 682   // The ordering needs to be the same, so remove the link that the
 683   // set_end call above added and substitute the new_sux for this
 684   // block.
 685   sux->remove_predecessor(new_sux);
 686 
 687   // the successor could be the target of a switch so it might have
 688   // multiple copies of this predecessor, so substitute the new_sux
 689   // for the first and delete the rest.
 690   bool assigned = false;
 691   BlockList& list = sux->_predecessors;
 692   for (int i = 0; i < list.length(); i++) {
 693     BlockBegin** b = list.adr_at(i);
 694     if (*b == this) {


 971 void BlockList::print(bool cfg_only, bool live_only) {
 972   InstructionPrinter ip;
 973   for (int i = 0; i < length(); i++) {
 974     BlockBegin* block = at(i);
 975     if (cfg_only) {
 976       ip.print_instr(block); tty->cr();
 977     } else {
 978       block->print_block(ip, live_only);
 979     }
 980   }
 981 }
 982 #endif // PRODUCT
 983 
 984 
 985 // Implementation of BlockEnd
 986 
 987 void BlockEnd::set_begin(BlockBegin* begin) {
 988   BlockList* sux = NULL;
 989   if (begin != NULL) {
 990     sux = begin->successors();
 991   } else if (this->begin() != NULL) {
 992     // copy our sux list
 993     BlockList* sux = new BlockList(this->begin()->number_of_sux());
 994     for (int i = 0; i < this->begin()->number_of_sux(); i++) {
 995       sux->append(this->begin()->sux_at(i));
 996     }
 997   }
 998   _sux = sux;

 999 }
1000 
1001 
1002 void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {
1003   substitute(*_sux, old_sux, new_sux);
1004 }
1005 
1006 
1007 // Implementation of Phi
1008 
1009 // Normal phi functions take their operands from the last instruction of the
1010 // predecessor. Special handling is needed for xhanlder entries because there
1011 // the state of arbitrary instructions are needed.
1012 
1013 Value Phi::operand_at(int i) const {
1014   ValueStack* state;
1015   if (_block->is_set(BlockBegin::exception_entry_flag)) {
1016     state = _block->exception_state_at(i);
1017   } else {
1018     state = _block->pred_at(i)->end()->state();
1019   }
1020   assert(state != NULL, "");
1021 
1022   if (is_local()) {
1023     return state->local_at(local_index());
1024   } else {
1025     return state->stack_at(stack_index());
1026   }
1027 }
1028 
1029 
1030 int Phi::operand_count() const {
1031   if (_block->is_set(BlockBegin::exception_entry_flag)) {
1032     return _block->number_of_exception_states();
1033   } else {
1034     return _block->number_of_preds();
1035   }
1036 }
1037 
1038 #ifndef PRODUCT
1039 
1040 // Constructor of Assert
1041 Assert::Assert(Value x, Condition cond, bool unordered_is_true, Value y) : Instruction(illegalType)
1042   , _x(x)
1043   , _cond(cond)
1044   , _y(y)
1045 {
1046   set_flag(UnorderedIsTrueFlag, unordered_is_true);
1047   assert(x->type()->tag() == y->type()->tag(), "types must match");
1048   pin();
1049 
1050   stringStream strStream;
1051   Compilation::current()->method()->print_name(&strStream);
1052 
1053   stringStream strStream1;
1054   InstructionPrinter ip1(1, &strStream1);
1055   ip1.print_instr(x);
1056 
1057   stringStream strStream2;
1058   InstructionPrinter ip2(1, &strStream2);
1059   ip2.print_instr(y);
1060 
1061   _message = new char[1024];
1062   snprintf(_message, 1024, "Assertion %s %s %s in method %s", strStream1.as_string(), ip2.cond_name(cond), strStream2.as_string(), strStream.as_string());
1063 }
1064 
1065 #endif
1066 
1067 void RangeCheckPredicate::check_state() {
1068   assert(state()->kind() != ValueStack::EmptyExceptionState && state()->kind() != ValueStack::ExceptionState, "will deopt with empty state");
1069 }
1070 
1071 void ProfileInvoke::state_values_do(ValueVisitor* f) {
1072   if (state() != NULL) state()->values_do(f);
1073 }