1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/c1/c1_Instruction.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,1006 @@ 1.4 +/* 1.5 + * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "incls/_precompiled.incl" 1.29 +#include "incls/_c1_Instruction.cpp.incl" 1.30 + 1.31 + 1.32 +// Implementation of Instruction 1.33 + 1.34 + 1.35 +int Instruction::_next_id = 0; 1.36 + 1.37 +#ifdef ASSERT 1.38 +void Instruction::create_hi_word() { 1.39 + assert(type()->is_double_word() && _hi_word == NULL, "only double word has high word"); 1.40 + _hi_word = new HiWord(this); 1.41 +} 1.42 +#endif 1.43 + 1.44 +Instruction::Condition Instruction::mirror(Condition cond) { 1.45 + switch (cond) { 1.46 + case eql: return eql; 1.47 + case neq: return neq; 1.48 + case lss: return gtr; 1.49 + case leq: return geq; 1.50 + case gtr: return lss; 1.51 + case geq: return leq; 1.52 + } 1.53 + ShouldNotReachHere(); 1.54 + return eql; 1.55 +} 1.56 + 1.57 + 1.58 +Instruction::Condition Instruction::negate(Condition cond) { 1.59 + switch (cond) { 1.60 + case eql: return neq; 1.61 + case neq: return eql; 1.62 + case lss: return geq; 1.63 + case leq: return gtr; 1.64 + case gtr: return leq; 1.65 + case geq: return lss; 1.66 + } 1.67 + ShouldNotReachHere(); 1.68 + return eql; 1.69 +} 1.70 + 1.71 + 1.72 +Instruction* Instruction::prev(BlockBegin* block) { 1.73 + Instruction* p = NULL; 1.74 + Instruction* q = block; 1.75 + while (q != this) { 1.76 + assert(q != NULL, "this is not in the block's instruction list"); 1.77 + p = q; q = q->next(); 1.78 + } 1.79 + return p; 1.80 +} 1.81 + 1.82 + 1.83 +#ifndef PRODUCT 1.84 +void Instruction::print() { 1.85 + InstructionPrinter ip; 1.86 + print(ip); 1.87 +} 1.88 + 1.89 + 1.90 +void Instruction::print_line() { 1.91 + InstructionPrinter ip; 1.92 + ip.print_line(this); 1.93 +} 1.94 + 1.95 + 1.96 +void Instruction::print(InstructionPrinter& ip) { 1.97 + ip.print_head(); 1.98 + ip.print_line(this); 1.99 + tty->cr(); 1.100 +} 1.101 +#endif // PRODUCT 1.102 + 1.103 + 1.104 +// perform constant and interval tests on index value 1.105 +bool AccessIndexed::compute_needs_range_check() { 1.106 + Constant* clength = length()->as_Constant(); 1.107 + Constant* cindex = index()->as_Constant(); 1.108 + if (clength && cindex) { 1.109 + IntConstant* l = clength->type()->as_IntConstant(); 1.110 + IntConstant* i = cindex->type()->as_IntConstant(); 1.111 + if (l && i && i->value() < l->value() && i->value() >= 0) { 1.112 + return false; 1.113 + } 1.114 + } 1.115 + return true; 1.116 +} 1.117 + 1.118 + 1.119 +ciType* LoadIndexed::exact_type() const { 1.120 + ciType* array_type = array()->exact_type(); 1.121 + if (array_type == NULL) { 1.122 + return NULL; 1.123 + } 1.124 + assert(array_type->is_array_klass(), "what else?"); 1.125 + ciArrayKlass* ak = (ciArrayKlass*)array_type; 1.126 + 1.127 + if (ak->element_type()->is_instance_klass()) { 1.128 + ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type(); 1.129 + if (ik->is_loaded() && ik->is_final()) { 1.130 + return ik; 1.131 + } 1.132 + } 1.133 + return NULL; 1.134 +} 1.135 + 1.136 + 1.137 +ciType* LoadIndexed::declared_type() const { 1.138 + ciType* array_type = array()->declared_type(); 1.139 + if (array_type == NULL) { 1.140 + return NULL; 1.141 + } 1.142 + assert(array_type->is_array_klass(), "what else?"); 1.143 + ciArrayKlass* ak = (ciArrayKlass*)array_type; 1.144 + return ak->element_type(); 1.145 +} 1.146 + 1.147 + 1.148 +ciType* LoadField::declared_type() const { 1.149 + return field()->type(); 1.150 +} 1.151 + 1.152 + 1.153 +ciType* LoadField::exact_type() const { 1.154 + ciType* type = declared_type(); 1.155 + // for primitive arrays, the declared type is the exact type 1.156 + if (type->is_type_array_klass()) { 1.157 + return type; 1.158 + } 1.159 + if (type->is_instance_klass()) { 1.160 + ciInstanceKlass* ik = (ciInstanceKlass*)type; 1.161 + if (ik->is_loaded() && ik->is_final()) { 1.162 + return type; 1.163 + } 1.164 + } 1.165 + return NULL; 1.166 +} 1.167 + 1.168 + 1.169 +ciType* NewTypeArray::exact_type() const { 1.170 + return ciTypeArrayKlass::make(elt_type()); 1.171 +} 1.172 + 1.173 + 1.174 +ciType* NewObjectArray::exact_type() const { 1.175 + return ciObjArrayKlass::make(klass()); 1.176 +} 1.177 + 1.178 + 1.179 +ciType* NewInstance::exact_type() const { 1.180 + return klass(); 1.181 +} 1.182 + 1.183 + 1.184 +ciType* CheckCast::declared_type() const { 1.185 + return klass(); 1.186 +} 1.187 + 1.188 +ciType* CheckCast::exact_type() const { 1.189 + if (klass()->is_instance_klass()) { 1.190 + ciInstanceKlass* ik = (ciInstanceKlass*)klass(); 1.191 + if (ik->is_loaded() && ik->is_final()) { 1.192 + return ik; 1.193 + } 1.194 + } 1.195 + return NULL; 1.196 +} 1.197 + 1.198 + 1.199 +void ArithmeticOp::other_values_do(void f(Value*)) { 1.200 + if (lock_stack() != NULL) lock_stack()->values_do(f); 1.201 +} 1.202 + 1.203 +void NullCheck::other_values_do(void f(Value*)) { 1.204 + lock_stack()->values_do(f); 1.205 +} 1.206 + 1.207 +void AccessArray::other_values_do(void f(Value*)) { 1.208 + if (lock_stack() != NULL) lock_stack()->values_do(f); 1.209 +} 1.210 + 1.211 + 1.212 +// Implementation of AccessField 1.213 + 1.214 +void AccessField::other_values_do(void f(Value*)) { 1.215 + if (state_before() != NULL) state_before()->values_do(f); 1.216 + if (lock_stack() != NULL) lock_stack()->values_do(f); 1.217 +} 1.218 + 1.219 + 1.220 +// Implementation of StoreIndexed 1.221 + 1.222 +IRScope* StoreIndexed::scope() const { 1.223 + return lock_stack()->scope(); 1.224 +} 1.225 + 1.226 + 1.227 +// Implementation of ArithmeticOp 1.228 + 1.229 +bool ArithmeticOp::is_commutative() const { 1.230 + switch (op()) { 1.231 + case Bytecodes::_iadd: // fall through 1.232 + case Bytecodes::_ladd: // fall through 1.233 + case Bytecodes::_fadd: // fall through 1.234 + case Bytecodes::_dadd: // fall through 1.235 + case Bytecodes::_imul: // fall through 1.236 + case Bytecodes::_lmul: // fall through 1.237 + case Bytecodes::_fmul: // fall through 1.238 + case Bytecodes::_dmul: return true; 1.239 + } 1.240 + return false; 1.241 +} 1.242 + 1.243 + 1.244 +bool ArithmeticOp::can_trap() const { 1.245 + switch (op()) { 1.246 + case Bytecodes::_idiv: // fall through 1.247 + case Bytecodes::_ldiv: // fall through 1.248 + case Bytecodes::_irem: // fall through 1.249 + case Bytecodes::_lrem: return true; 1.250 + } 1.251 + return false; 1.252 +} 1.253 + 1.254 + 1.255 +// Implementation of LogicOp 1.256 + 1.257 +bool LogicOp::is_commutative() const { 1.258 +#ifdef ASSERT 1.259 + switch (op()) { 1.260 + case Bytecodes::_iand: // fall through 1.261 + case Bytecodes::_land: // fall through 1.262 + case Bytecodes::_ior : // fall through 1.263 + case Bytecodes::_lor : // fall through 1.264 + case Bytecodes::_ixor: // fall through 1.265 + case Bytecodes::_lxor: break; 1.266 + default : ShouldNotReachHere(); 1.267 + } 1.268 +#endif 1.269 + // all LogicOps are commutative 1.270 + return true; 1.271 +} 1.272 + 1.273 + 1.274 +// Implementation of CompareOp 1.275 + 1.276 +void CompareOp::other_values_do(void f(Value*)) { 1.277 + if (state_before() != NULL) state_before()->values_do(f); 1.278 +} 1.279 + 1.280 + 1.281 +// Implementation of IfOp 1.282 + 1.283 +bool IfOp::is_commutative() const { 1.284 + return cond() == eql || cond() == neq; 1.285 +} 1.286 + 1.287 + 1.288 +// Implementation of StateSplit 1.289 + 1.290 +void StateSplit::substitute(BlockList& list, BlockBegin* old_block, BlockBegin* new_block) { 1.291 + NOT_PRODUCT(bool assigned = false;) 1.292 + for (int i = 0; i < list.length(); i++) { 1.293 + BlockBegin** b = list.adr_at(i); 1.294 + if (*b == old_block) { 1.295 + *b = new_block; 1.296 + NOT_PRODUCT(assigned = true;) 1.297 + } 1.298 + } 1.299 + assert(assigned == true, "should have assigned at least once"); 1.300 +} 1.301 + 1.302 + 1.303 +IRScope* StateSplit::scope() const { 1.304 + return _state->scope(); 1.305 +} 1.306 + 1.307 + 1.308 +void StateSplit::state_values_do(void f(Value*)) { 1.309 + if (state() != NULL) state()->values_do(f); 1.310 +} 1.311 + 1.312 + 1.313 +void BlockBegin::state_values_do(void f(Value*)) { 1.314 + StateSplit::state_values_do(f); 1.315 + 1.316 + if (is_set(BlockBegin::exception_entry_flag)) { 1.317 + for (int i = 0; i < number_of_exception_states(); i++) { 1.318 + exception_state_at(i)->values_do(f); 1.319 + } 1.320 + } 1.321 +} 1.322 + 1.323 + 1.324 +void MonitorEnter::state_values_do(void f(Value*)) { 1.325 + StateSplit::state_values_do(f); 1.326 + _lock_stack_before->values_do(f); 1.327 +} 1.328 + 1.329 + 1.330 +void Intrinsic::state_values_do(void f(Value*)) { 1.331 + StateSplit::state_values_do(f); 1.332 + if (lock_stack() != NULL) lock_stack()->values_do(f); 1.333 +} 1.334 + 1.335 + 1.336 +// Implementation of Invoke 1.337 + 1.338 + 1.339 +Invoke::Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args, 1.340 + int vtable_index, ciMethod* target) 1.341 + : StateSplit(result_type) 1.342 + , _code(code) 1.343 + , _recv(recv) 1.344 + , _args(args) 1.345 + , _vtable_index(vtable_index) 1.346 + , _target(target) 1.347 +{ 1.348 + set_flag(TargetIsLoadedFlag, target->is_loaded()); 1.349 + set_flag(TargetIsFinalFlag, target_is_loaded() && target->is_final_method()); 1.350 + set_flag(TargetIsStrictfpFlag, target_is_loaded() && target->is_strict()); 1.351 + 1.352 + assert(args != NULL, "args must exist"); 1.353 +#ifdef ASSERT 1.354 + values_do(assert_value); 1.355 +#endif // ASSERT 1.356 + 1.357 + // provide an initial guess of signature size. 1.358 + _signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0)); 1.359 + if (has_receiver()) { 1.360 + _signature->append(as_BasicType(receiver()->type())); 1.361 + } 1.362 + for (int i = 0; i < number_of_arguments(); i++) { 1.363 + ValueType* t = argument_at(i)->type(); 1.364 + BasicType bt = as_BasicType(t); 1.365 + _signature->append(bt); 1.366 + } 1.367 +} 1.368 + 1.369 + 1.370 +// Implementation of Contant 1.371 +intx Constant::hash() const { 1.372 + if (_state == NULL) { 1.373 + switch (type()->tag()) { 1.374 + case intTag: 1.375 + return HASH2(name(), type()->as_IntConstant()->value()); 1.376 + case longTag: 1.377 + { 1.378 + jlong temp = type()->as_LongConstant()->value(); 1.379 + return HASH3(name(), high(temp), low(temp)); 1.380 + } 1.381 + case floatTag: 1.382 + return HASH2(name(), jint_cast(type()->as_FloatConstant()->value())); 1.383 + case doubleTag: 1.384 + { 1.385 + jlong temp = jlong_cast(type()->as_DoubleConstant()->value()); 1.386 + return HASH3(name(), high(temp), low(temp)); 1.387 + } 1.388 + case objectTag: 1.389 + assert(type()->as_ObjectType()->is_loaded(), "can't handle unloaded values"); 1.390 + return HASH2(name(), type()->as_ObjectType()->constant_value()); 1.391 + } 1.392 + } 1.393 + return 0; 1.394 +} 1.395 + 1.396 +bool Constant::is_equal(Value v) const { 1.397 + if (v->as_Constant() == NULL) return false; 1.398 + 1.399 + switch (type()->tag()) { 1.400 + case intTag: 1.401 + { 1.402 + IntConstant* t1 = type()->as_IntConstant(); 1.403 + IntConstant* t2 = v->type()->as_IntConstant(); 1.404 + return (t1 != NULL && t2 != NULL && 1.405 + t1->value() == t2->value()); 1.406 + } 1.407 + case longTag: 1.408 + { 1.409 + LongConstant* t1 = type()->as_LongConstant(); 1.410 + LongConstant* t2 = v->type()->as_LongConstant(); 1.411 + return (t1 != NULL && t2 != NULL && 1.412 + t1->value() == t2->value()); 1.413 + } 1.414 + case floatTag: 1.415 + { 1.416 + FloatConstant* t1 = type()->as_FloatConstant(); 1.417 + FloatConstant* t2 = v->type()->as_FloatConstant(); 1.418 + return (t1 != NULL && t2 != NULL && 1.419 + jint_cast(t1->value()) == jint_cast(t2->value())); 1.420 + } 1.421 + case doubleTag: 1.422 + { 1.423 + DoubleConstant* t1 = type()->as_DoubleConstant(); 1.424 + DoubleConstant* t2 = v->type()->as_DoubleConstant(); 1.425 + return (t1 != NULL && t2 != NULL && 1.426 + jlong_cast(t1->value()) == jlong_cast(t2->value())); 1.427 + } 1.428 + case objectTag: 1.429 + { 1.430 + ObjectType* t1 = type()->as_ObjectType(); 1.431 + ObjectType* t2 = v->type()->as_ObjectType(); 1.432 + return (t1 != NULL && t2 != NULL && 1.433 + t1->is_loaded() && t2->is_loaded() && 1.434 + t1->constant_value() == t2->constant_value()); 1.435 + } 1.436 + } 1.437 + return false; 1.438 +} 1.439 + 1.440 + 1.441 +BlockBegin* Constant::compare(Instruction::Condition cond, Value right, 1.442 + BlockBegin* true_sux, BlockBegin* false_sux) { 1.443 + Constant* rc = right->as_Constant(); 1.444 + // other is not a constant 1.445 + if (rc == NULL) return NULL; 1.446 + 1.447 + ValueType* lt = type(); 1.448 + ValueType* rt = rc->type(); 1.449 + // different types 1.450 + if (lt->base() != rt->base()) return NULL; 1.451 + switch (lt->tag()) { 1.452 + case intTag: { 1.453 + int x = lt->as_IntConstant()->value(); 1.454 + int y = rt->as_IntConstant()->value(); 1.455 + switch (cond) { 1.456 + case If::eql: return x == y ? true_sux : false_sux; 1.457 + case If::neq: return x != y ? true_sux : false_sux; 1.458 + case If::lss: return x < y ? true_sux : false_sux; 1.459 + case If::leq: return x <= y ? true_sux : false_sux; 1.460 + case If::gtr: return x > y ? true_sux : false_sux; 1.461 + case If::geq: return x >= y ? true_sux : false_sux; 1.462 + } 1.463 + break; 1.464 + } 1.465 + case longTag: { 1.466 + jlong x = lt->as_LongConstant()->value(); 1.467 + jlong y = rt->as_LongConstant()->value(); 1.468 + switch (cond) { 1.469 + case If::eql: return x == y ? true_sux : false_sux; 1.470 + case If::neq: return x != y ? true_sux : false_sux; 1.471 + case If::lss: return x < y ? true_sux : false_sux; 1.472 + case If::leq: return x <= y ? true_sux : false_sux; 1.473 + case If::gtr: return x > y ? true_sux : false_sux; 1.474 + case If::geq: return x >= y ? true_sux : false_sux; 1.475 + } 1.476 + break; 1.477 + } 1.478 + case objectTag: { 1.479 + ciObject* xvalue = lt->as_ObjectType()->constant_value(); 1.480 + ciObject* yvalue = rt->as_ObjectType()->constant_value(); 1.481 + assert(xvalue != NULL && yvalue != NULL, "not constants"); 1.482 + if (xvalue->is_loaded() && yvalue->is_loaded()) { 1.483 + switch (cond) { 1.484 + case If::eql: return xvalue == yvalue ? true_sux : false_sux; 1.485 + case If::neq: return xvalue != yvalue ? true_sux : false_sux; 1.486 + } 1.487 + } 1.488 + break; 1.489 + } 1.490 + } 1.491 + return NULL; 1.492 +} 1.493 + 1.494 + 1.495 +void Constant::other_values_do(void f(Value*)) { 1.496 + if (state() != NULL) state()->values_do(f); 1.497 +} 1.498 + 1.499 + 1.500 +// Implementation of NewArray 1.501 + 1.502 +void NewArray::other_values_do(void f(Value*)) { 1.503 + if (state_before() != NULL) state_before()->values_do(f); 1.504 +} 1.505 + 1.506 + 1.507 +// Implementation of TypeCheck 1.508 + 1.509 +void TypeCheck::other_values_do(void f(Value*)) { 1.510 + if (state_before() != NULL) state_before()->values_do(f); 1.511 +} 1.512 + 1.513 + 1.514 +// Implementation of BlockBegin 1.515 + 1.516 +int BlockBegin::_next_block_id = 0; 1.517 + 1.518 + 1.519 +void BlockBegin::set_end(BlockEnd* end) { 1.520 + assert(end != NULL, "should not reset block end to NULL"); 1.521 + BlockEnd* old_end = _end; 1.522 + if (end == old_end) { 1.523 + return; 1.524 + } 1.525 + // Must make the predecessors/successors match up with the 1.526 + // BlockEnd's notion. 1.527 + int i, n; 1.528 + if (old_end != NULL) { 1.529 + // disconnect from the old end 1.530 + old_end->set_begin(NULL); 1.531 + 1.532 + // disconnect this block from it's current successors 1.533 + for (i = 0; i < _successors.length(); i++) { 1.534 + _successors.at(i)->remove_predecessor(this); 1.535 + } 1.536 + } 1.537 + _end = end; 1.538 + 1.539 + _successors.clear(); 1.540 + // Now reset successors list based on BlockEnd 1.541 + n = end->number_of_sux(); 1.542 + for (i = 0; i < n; i++) { 1.543 + BlockBegin* sux = end->sux_at(i); 1.544 + _successors.append(sux); 1.545 + sux->_predecessors.append(this); 1.546 + } 1.547 + _end->set_begin(this); 1.548 +} 1.549 + 1.550 + 1.551 +void BlockBegin::disconnect_edge(BlockBegin* from, BlockBegin* to) { 1.552 + // disconnect any edges between from and to 1.553 +#ifndef PRODUCT 1.554 + if (PrintIR && Verbose) { 1.555 + tty->print_cr("Disconnected edge B%d -> B%d", from->block_id(), to->block_id()); 1.556 + } 1.557 +#endif 1.558 + for (int s = 0; s < from->number_of_sux();) { 1.559 + BlockBegin* sux = from->sux_at(s); 1.560 + if (sux == to) { 1.561 + int index = sux->_predecessors.index_of(from); 1.562 + if (index >= 0) { 1.563 + sux->_predecessors.remove_at(index); 1.564 + } 1.565 + from->_successors.remove_at(s); 1.566 + } else { 1.567 + s++; 1.568 + } 1.569 + } 1.570 +} 1.571 + 1.572 + 1.573 +void BlockBegin::disconnect_from_graph() { 1.574 + // disconnect this block from all other blocks 1.575 + for (int p = 0; p < number_of_preds(); p++) { 1.576 + pred_at(p)->remove_successor(this); 1.577 + } 1.578 + for (int s = 0; s < number_of_sux(); s++) { 1.579 + sux_at(s)->remove_predecessor(this); 1.580 + } 1.581 +} 1.582 + 1.583 +void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) { 1.584 + // modify predecessors before substituting successors 1.585 + for (int i = 0; i < number_of_sux(); i++) { 1.586 + if (sux_at(i) == old_sux) { 1.587 + // remove old predecessor before adding new predecessor 1.588 + // otherwise there is a dead predecessor in the list 1.589 + new_sux->remove_predecessor(old_sux); 1.590 + new_sux->add_predecessor(this); 1.591 + } 1.592 + } 1.593 + old_sux->remove_predecessor(this); 1.594 + end()->substitute_sux(old_sux, new_sux); 1.595 +} 1.596 + 1.597 + 1.598 + 1.599 +// In general it is not possible to calculate a value for the field "depth_first_number" 1.600 +// of the inserted block, without recomputing the values of the other blocks 1.601 +// in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless. 1.602 +BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) { 1.603 + // Try to make the bci close to a block with a single pred or sux, 1.604 + // since this make the block layout algorithm work better. 1.605 + int bci = -1; 1.606 + if (sux->number_of_preds() == 1) { 1.607 + bci = sux->bci(); 1.608 + } else { 1.609 + bci = end()->bci(); 1.610 + } 1.611 + 1.612 + BlockBegin* new_sux = new BlockBegin(bci); 1.613 + 1.614 + // mark this block (special treatment when block order is computed) 1.615 + new_sux->set(critical_edge_split_flag); 1.616 + 1.617 + // This goto is not a safepoint. 1.618 + Goto* e = new Goto(sux, false); 1.619 + new_sux->set_next(e, bci); 1.620 + new_sux->set_end(e); 1.621 + // setup states 1.622 + ValueStack* s = end()->state(); 1.623 + new_sux->set_state(s->copy()); 1.624 + e->set_state(s->copy()); 1.625 + assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!"); 1.626 + assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!"); 1.627 + assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!"); 1.628 + 1.629 + // link predecessor to new block 1.630 + end()->substitute_sux(sux, new_sux); 1.631 + 1.632 + // The ordering needs to be the same, so remove the link that the 1.633 + // set_end call above added and substitute the new_sux for this 1.634 + // block. 1.635 + sux->remove_predecessor(new_sux); 1.636 + 1.637 + // the successor could be the target of a switch so it might have 1.638 + // multiple copies of this predecessor, so substitute the new_sux 1.639 + // for the first and delete the rest. 1.640 + bool assigned = false; 1.641 + BlockList& list = sux->_predecessors; 1.642 + for (int i = 0; i < list.length(); i++) { 1.643 + BlockBegin** b = list.adr_at(i); 1.644 + if (*b == this) { 1.645 + if (assigned) { 1.646 + list.remove_at(i); 1.647 + // reprocess this index 1.648 + i--; 1.649 + } else { 1.650 + assigned = true; 1.651 + *b = new_sux; 1.652 + } 1.653 + // link the new block back to it's predecessors. 1.654 + new_sux->add_predecessor(this); 1.655 + } 1.656 + } 1.657 + assert(assigned == true, "should have assigned at least once"); 1.658 + return new_sux; 1.659 +} 1.660 + 1.661 + 1.662 +void BlockBegin::remove_successor(BlockBegin* pred) { 1.663 + int idx; 1.664 + while ((idx = _successors.index_of(pred)) >= 0) { 1.665 + _successors.remove_at(idx); 1.666 + } 1.667 +} 1.668 + 1.669 + 1.670 +void BlockBegin::add_predecessor(BlockBegin* pred) { 1.671 + _predecessors.append(pred); 1.672 +} 1.673 + 1.674 + 1.675 +void BlockBegin::remove_predecessor(BlockBegin* pred) { 1.676 + int idx; 1.677 + while ((idx = _predecessors.index_of(pred)) >= 0) { 1.678 + _predecessors.remove_at(idx); 1.679 + } 1.680 +} 1.681 + 1.682 + 1.683 +void BlockBegin::add_exception_handler(BlockBegin* b) { 1.684 + assert(b != NULL && (b->is_set(exception_entry_flag)), "exception handler must exist"); 1.685 + // add only if not in the list already 1.686 + if (!_exception_handlers.contains(b)) _exception_handlers.append(b); 1.687 +} 1.688 + 1.689 +int BlockBegin::add_exception_state(ValueStack* state) { 1.690 + assert(is_set(exception_entry_flag), "only for xhandlers"); 1.691 + if (_exception_states == NULL) { 1.692 + _exception_states = new ValueStackStack(4); 1.693 + } 1.694 + _exception_states->append(state); 1.695 + return _exception_states->length() - 1; 1.696 +} 1.697 + 1.698 + 1.699 +void BlockBegin::iterate_preorder(boolArray& mark, BlockClosure* closure) { 1.700 + if (!mark.at(block_id())) { 1.701 + mark.at_put(block_id(), true); 1.702 + closure->block_do(this); 1.703 + BlockEnd* e = end(); // must do this after block_do because block_do may change it! 1.704 + { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_preorder(mark, closure); } 1.705 + { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_preorder(mark, closure); } 1.706 + } 1.707 +} 1.708 + 1.709 + 1.710 +void BlockBegin::iterate_postorder(boolArray& mark, BlockClosure* closure) { 1.711 + if (!mark.at(block_id())) { 1.712 + mark.at_put(block_id(), true); 1.713 + BlockEnd* e = end(); 1.714 + { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_postorder(mark, closure); } 1.715 + { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_postorder(mark, closure); } 1.716 + closure->block_do(this); 1.717 + } 1.718 +} 1.719 + 1.720 + 1.721 +void BlockBegin::iterate_preorder(BlockClosure* closure) { 1.722 + boolArray mark(number_of_blocks(), false); 1.723 + iterate_preorder(mark, closure); 1.724 +} 1.725 + 1.726 + 1.727 +void BlockBegin::iterate_postorder(BlockClosure* closure) { 1.728 + boolArray mark(number_of_blocks(), false); 1.729 + iterate_postorder(mark, closure); 1.730 +} 1.731 + 1.732 + 1.733 +void BlockBegin::block_values_do(void f(Value*)) { 1.734 + for (Instruction* n = this; n != NULL; n = n->next()) n->values_do(f); 1.735 +} 1.736 + 1.737 + 1.738 +#ifndef PRODUCT 1.739 + #define TRACE_PHI(code) if (PrintPhiFunctions) { code; } 1.740 +#else 1.741 + #define TRACE_PHI(coce) 1.742 +#endif 1.743 + 1.744 + 1.745 +bool BlockBegin::try_merge(ValueStack* new_state) { 1.746 + TRACE_PHI(tty->print_cr("********** try_merge for block B%d", block_id())); 1.747 + 1.748 + // local variables used for state iteration 1.749 + int index; 1.750 + Value new_value, existing_value; 1.751 + 1.752 + ValueStack* existing_state = state(); 1.753 + if (existing_state == NULL) { 1.754 + TRACE_PHI(tty->print_cr("first call of try_merge for this block")); 1.755 + 1.756 + if (is_set(BlockBegin::was_visited_flag)) { 1.757 + // this actually happens for complicated jsr/ret structures 1.758 + return false; // BAILOUT in caller 1.759 + } 1.760 + 1.761 + // copy state because it is altered 1.762 + new_state = new_state->copy(); 1.763 + 1.764 + // Use method liveness to invalidate dead locals 1.765 + MethodLivenessResult liveness = new_state->scope()->method()->liveness_at_bci(bci()); 1.766 + if (liveness.is_valid()) { 1.767 + assert((int)liveness.size() == new_state->locals_size(), "error in use of liveness"); 1.768 + 1.769 + for_each_local_value(new_state, index, new_value) { 1.770 + if (!liveness.at(index) || new_value->type()->is_illegal()) { 1.771 + new_state->invalidate_local(index); 1.772 + TRACE_PHI(tty->print_cr("invalidating dead local %d", index)); 1.773 + } 1.774 + } 1.775 + } 1.776 + 1.777 + if (is_set(BlockBegin::parser_loop_header_flag)) { 1.778 + TRACE_PHI(tty->print_cr("loop header block, initializing phi functions")); 1.779 + 1.780 + for_each_stack_value(new_state, index, new_value) { 1.781 + new_state->setup_phi_for_stack(this, index); 1.782 + TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", new_state->stack_at(index)->type()->tchar(), new_state->stack_at(index)->id(), index)); 1.783 + } 1.784 + 1.785 + BitMap requires_phi_function = new_state->scope()->requires_phi_function(); 1.786 + 1.787 + for_each_local_value(new_state, index, new_value) { 1.788 + bool requires_phi = requires_phi_function.at(index) || (new_value->type()->is_double_word() && requires_phi_function.at(index + 1)); 1.789 + if (requires_phi || !SelectivePhiFunctions) { 1.790 + new_state->setup_phi_for_local(this, index); 1.791 + TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", new_state->local_at(index)->type()->tchar(), new_state->local_at(index)->id(), index)); 1.792 + } 1.793 + } 1.794 + } 1.795 + 1.796 + // initialize state of block 1.797 + set_state(new_state); 1.798 + 1.799 + } else if (existing_state->is_same_across_scopes(new_state)) { 1.800 + TRACE_PHI(tty->print_cr("exisiting state found")); 1.801 + 1.802 + // Inlining may cause the local state not to match up, so walk up 1.803 + // the new state until we get to the same scope as the 1.804 + // existing and then start processing from there. 1.805 + while (existing_state->scope() != new_state->scope()) { 1.806 + new_state = new_state->caller_state(); 1.807 + assert(new_state != NULL, "could not match up scopes"); 1.808 + 1.809 + assert(false, "check if this is necessary"); 1.810 + } 1.811 + 1.812 + assert(existing_state->scope() == new_state->scope(), "not matching"); 1.813 + assert(existing_state->locals_size() == new_state->locals_size(), "not matching"); 1.814 + assert(existing_state->stack_size() == new_state->stack_size(), "not matching"); 1.815 + 1.816 + if (is_set(BlockBegin::was_visited_flag)) { 1.817 + TRACE_PHI(tty->print_cr("loop header block, phis must be present")); 1.818 + 1.819 + if (!is_set(BlockBegin::parser_loop_header_flag)) { 1.820 + // this actually happens for complicated jsr/ret structures 1.821 + return false; // BAILOUT in caller 1.822 + } 1.823 + 1.824 + for_each_local_value(existing_state, index, existing_value) { 1.825 + Value new_value = new_state->local_at(index); 1.826 + if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) { 1.827 + // The old code invalidated the phi function here 1.828 + // Because dead locals are replaced with NULL, this is a very rare case now, so simply bail out 1.829 + return false; // BAILOUT in caller 1.830 + } 1.831 + } 1.832 + 1.833 +#ifdef ASSERT 1.834 + // check that all necessary phi functions are present 1.835 + for_each_stack_value(existing_state, index, existing_value) { 1.836 + assert(existing_value->as_Phi() != NULL && existing_value->as_Phi()->block() == this, "phi function required"); 1.837 + } 1.838 + for_each_local_value(existing_state, index, existing_value) { 1.839 + assert(existing_value == new_state->local_at(index) || (existing_value->as_Phi() != NULL && existing_value->as_Phi()->as_Phi()->block() == this), "phi function required"); 1.840 + } 1.841 +#endif 1.842 + 1.843 + } else { 1.844 + TRACE_PHI(tty->print_cr("creating phi functions on demand")); 1.845 + 1.846 + // create necessary phi functions for stack 1.847 + for_each_stack_value(existing_state, index, existing_value) { 1.848 + Value new_value = new_state->stack_at(index); 1.849 + Phi* existing_phi = existing_value->as_Phi(); 1.850 + 1.851 + if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) { 1.852 + existing_state->setup_phi_for_stack(this, index); 1.853 + TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", existing_state->stack_at(index)->type()->tchar(), existing_state->stack_at(index)->id(), index)); 1.854 + } 1.855 + } 1.856 + 1.857 + // create necessary phi functions for locals 1.858 + for_each_local_value(existing_state, index, existing_value) { 1.859 + Value new_value = new_state->local_at(index); 1.860 + Phi* existing_phi = existing_value->as_Phi(); 1.861 + 1.862 + if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) { 1.863 + existing_state->invalidate_local(index); 1.864 + TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index)); 1.865 + } else if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) { 1.866 + existing_state->setup_phi_for_local(this, index); 1.867 + TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", existing_state->local_at(index)->type()->tchar(), existing_state->local_at(index)->id(), index)); 1.868 + } 1.869 + } 1.870 + } 1.871 + 1.872 + assert(existing_state->caller_state() == new_state->caller_state(), "caller states must be equal"); 1.873 + 1.874 + } else { 1.875 + assert(false, "stack or locks not matching (invalid bytecodes)"); 1.876 + return false; 1.877 + } 1.878 + 1.879 + TRACE_PHI(tty->print_cr("********** try_merge for block B%d successful", block_id())); 1.880 + 1.881 + return true; 1.882 +} 1.883 + 1.884 + 1.885 +#ifndef PRODUCT 1.886 +void BlockBegin::print_block() { 1.887 + InstructionPrinter ip; 1.888 + print_block(ip, false); 1.889 +} 1.890 + 1.891 + 1.892 +void BlockBegin::print_block(InstructionPrinter& ip, bool live_only) { 1.893 + ip.print_instr(this); tty->cr(); 1.894 + ip.print_stack(this->state()); tty->cr(); 1.895 + ip.print_inline_level(this); 1.896 + ip.print_head(); 1.897 + for (Instruction* n = next(); n != NULL; n = n->next()) { 1.898 + if (!live_only || n->is_pinned() || n->use_count() > 0) { 1.899 + ip.print_line(n); 1.900 + } 1.901 + } 1.902 + tty->cr(); 1.903 +} 1.904 +#endif // PRODUCT 1.905 + 1.906 + 1.907 +// Implementation of BlockList 1.908 + 1.909 +void BlockList::iterate_forward (BlockClosure* closure) { 1.910 + const int l = length(); 1.911 + for (int i = 0; i < l; i++) closure->block_do(at(i)); 1.912 +} 1.913 + 1.914 + 1.915 +void BlockList::iterate_backward(BlockClosure* closure) { 1.916 + for (int i = length() - 1; i >= 0; i--) closure->block_do(at(i)); 1.917 +} 1.918 + 1.919 + 1.920 +void BlockList::blocks_do(void f(BlockBegin*)) { 1.921 + for (int i = length() - 1; i >= 0; i--) f(at(i)); 1.922 +} 1.923 + 1.924 + 1.925 +void BlockList::values_do(void f(Value*)) { 1.926 + for (int i = length() - 1; i >= 0; i--) at(i)->block_values_do(f); 1.927 +} 1.928 + 1.929 + 1.930 +#ifndef PRODUCT 1.931 +void BlockList::print(bool cfg_only, bool live_only) { 1.932 + InstructionPrinter ip; 1.933 + for (int i = 0; i < length(); i++) { 1.934 + BlockBegin* block = at(i); 1.935 + if (cfg_only) { 1.936 + ip.print_instr(block); tty->cr(); 1.937 + } else { 1.938 + block->print_block(ip, live_only); 1.939 + } 1.940 + } 1.941 +} 1.942 +#endif // PRODUCT 1.943 + 1.944 + 1.945 +// Implementation of BlockEnd 1.946 + 1.947 +void BlockEnd::set_begin(BlockBegin* begin) { 1.948 + BlockList* sux = NULL; 1.949 + if (begin != NULL) { 1.950 + sux = begin->successors(); 1.951 + } else if (_begin != NULL) { 1.952 + // copy our sux list 1.953 + BlockList* sux = new BlockList(_begin->number_of_sux()); 1.954 + for (int i = 0; i < _begin->number_of_sux(); i++) { 1.955 + sux->append(_begin->sux_at(i)); 1.956 + } 1.957 + } 1.958 + _sux = sux; 1.959 + _begin = begin; 1.960 +} 1.961 + 1.962 + 1.963 +void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) { 1.964 + substitute(*_sux, old_sux, new_sux); 1.965 +} 1.966 + 1.967 + 1.968 +void BlockEnd::other_values_do(void f(Value*)) { 1.969 + if (state_before() != NULL) state_before()->values_do(f); 1.970 +} 1.971 + 1.972 + 1.973 +// Implementation of Phi 1.974 + 1.975 +// Normal phi functions take their operands from the last instruction of the 1.976 +// predecessor. Special handling is needed for xhanlder entries because there 1.977 +// the state of arbitrary instructions are needed. 1.978 + 1.979 +Value Phi::operand_at(int i) const { 1.980 + ValueStack* state; 1.981 + if (_block->is_set(BlockBegin::exception_entry_flag)) { 1.982 + state = _block->exception_state_at(i); 1.983 + } else { 1.984 + state = _block->pred_at(i)->end()->state(); 1.985 + } 1.986 + assert(state != NULL, ""); 1.987 + 1.988 + if (is_local()) { 1.989 + return state->local_at(local_index()); 1.990 + } else { 1.991 + return state->stack_at(stack_index()); 1.992 + } 1.993 +} 1.994 + 1.995 + 1.996 +int Phi::operand_count() const { 1.997 + if (_block->is_set(BlockBegin::exception_entry_flag)) { 1.998 + return _block->number_of_exception_states(); 1.999 + } else { 1.1000 + return _block->number_of_preds(); 1.1001 + } 1.1002 +} 1.1003 + 1.1004 + 1.1005 +// Implementation of Throw 1.1006 + 1.1007 +void Throw::state_values_do(void f(Value*)) { 1.1008 + BlockEnd::state_values_do(f); 1.1009 +}