1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/c1/c1_Canonicalizer.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,945 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "c1/c1_Canonicalizer.hpp" 1.30 +#include "c1/c1_InstructionPrinter.hpp" 1.31 +#include "c1/c1_ValueStack.hpp" 1.32 +#include "ci/ciArray.hpp" 1.33 +#include "runtime/sharedRuntime.hpp" 1.34 + 1.35 + 1.36 +class PrintValueVisitor: public ValueVisitor { 1.37 + void visit(Value* vp) { 1.38 + (*vp)->print_line(); 1.39 + } 1.40 +}; 1.41 + 1.42 +void Canonicalizer::set_canonical(Value x) { 1.43 + assert(x != NULL, "value must exist"); 1.44 + // Note: we can not currently substitute root nodes which show up in 1.45 + // the instruction stream (because the instruction list is embedded 1.46 + // in the instructions). 1.47 + if (canonical() != x) { 1.48 +#ifndef PRODUCT 1.49 + if (!x->has_printable_bci()) { 1.50 + x->set_printable_bci(bci()); 1.51 + } 1.52 +#endif 1.53 + if (PrintCanonicalization) { 1.54 + PrintValueVisitor do_print_value; 1.55 + canonical()->input_values_do(&do_print_value); 1.56 + canonical()->print_line(); 1.57 + tty->print_cr("canonicalized to:"); 1.58 + x->input_values_do(&do_print_value); 1.59 + x->print_line(); 1.60 + tty->cr(); 1.61 + } 1.62 + assert(_canonical->type()->tag() == x->type()->tag(), "types must match"); 1.63 + _canonical = x; 1.64 + } 1.65 +} 1.66 + 1.67 + 1.68 +void Canonicalizer::move_const_to_right(Op2* x) { 1.69 + if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands(); 1.70 +} 1.71 + 1.72 + 1.73 +void Canonicalizer::do_Op2(Op2* x) { 1.74 + if (x->x() == x->y()) { 1.75 + switch (x->op()) { 1.76 + case Bytecodes::_isub: set_constant(0); return; 1.77 + case Bytecodes::_lsub: set_constant(jlong_cast(0)); return; 1.78 + case Bytecodes::_iand: // fall through 1.79 + case Bytecodes::_land: // fall through 1.80 + case Bytecodes::_ior: // fall through 1.81 + case Bytecodes::_lor : set_canonical(x->x()); return; 1.82 + case Bytecodes::_ixor: set_constant(0); return; 1.83 + case Bytecodes::_lxor: set_constant(jlong_cast(0)); return; 1.84 + } 1.85 + } 1.86 + 1.87 + if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { 1.88 + // do constant folding for selected operations 1.89 + switch (x->type()->tag()) { 1.90 + case intTag: 1.91 + { jint a = x->x()->type()->as_IntConstant()->value(); 1.92 + jint b = x->y()->type()->as_IntConstant()->value(); 1.93 + switch (x->op()) { 1.94 + case Bytecodes::_iadd: set_constant(a + b); return; 1.95 + case Bytecodes::_isub: set_constant(a - b); return; 1.96 + case Bytecodes::_imul: set_constant(a * b); return; 1.97 + case Bytecodes::_idiv: 1.98 + if (b != 0) { 1.99 + if (a == min_jint && b == -1) { 1.100 + set_constant(min_jint); 1.101 + } else { 1.102 + set_constant(a / b); 1.103 + } 1.104 + return; 1.105 + } 1.106 + break; 1.107 + case Bytecodes::_irem: 1.108 + if (b != 0) { 1.109 + if (a == min_jint && b == -1) { 1.110 + set_constant(0); 1.111 + } else { 1.112 + set_constant(a % b); 1.113 + } 1.114 + return; 1.115 + } 1.116 + break; 1.117 + case Bytecodes::_iand: set_constant(a & b); return; 1.118 + case Bytecodes::_ior : set_constant(a | b); return; 1.119 + case Bytecodes::_ixor: set_constant(a ^ b); return; 1.120 + } 1.121 + } 1.122 + break; 1.123 + case longTag: 1.124 + { jlong a = x->x()->type()->as_LongConstant()->value(); 1.125 + jlong b = x->y()->type()->as_LongConstant()->value(); 1.126 + switch (x->op()) { 1.127 + case Bytecodes::_ladd: set_constant(a + b); return; 1.128 + case Bytecodes::_lsub: set_constant(a - b); return; 1.129 + case Bytecodes::_lmul: set_constant(a * b); return; 1.130 + case Bytecodes::_ldiv: 1.131 + if (b != 0) { 1.132 + set_constant(SharedRuntime::ldiv(b, a)); 1.133 + return; 1.134 + } 1.135 + break; 1.136 + case Bytecodes::_lrem: 1.137 + if (b != 0) { 1.138 + set_constant(SharedRuntime::lrem(b, a)); 1.139 + return; 1.140 + } 1.141 + break; 1.142 + case Bytecodes::_land: set_constant(a & b); return; 1.143 + case Bytecodes::_lor : set_constant(a | b); return; 1.144 + case Bytecodes::_lxor: set_constant(a ^ b); return; 1.145 + } 1.146 + } 1.147 + break; 1.148 + // other cases not implemented (must be extremely careful with floats & doubles!) 1.149 + } 1.150 + } 1.151 + // make sure constant is on the right side, if any 1.152 + move_const_to_right(x); 1.153 + 1.154 + if (x->y()->type()->is_constant()) { 1.155 + // do constant folding for selected operations 1.156 + switch (x->type()->tag()) { 1.157 + case intTag: 1.158 + if (x->y()->type()->as_IntConstant()->value() == 0) { 1.159 + switch (x->op()) { 1.160 + case Bytecodes::_iadd: set_canonical(x->x()); return; 1.161 + case Bytecodes::_isub: set_canonical(x->x()); return; 1.162 + case Bytecodes::_imul: set_constant(0); return; 1.163 + // Note: for div and rem, make sure that C semantics 1.164 + // corresponds to Java semantics! 1.165 + case Bytecodes::_iand: set_constant(0); return; 1.166 + case Bytecodes::_ior : set_canonical(x->x()); return; 1.167 + } 1.168 + } 1.169 + break; 1.170 + case longTag: 1.171 + if (x->y()->type()->as_LongConstant()->value() == (jlong)0) { 1.172 + switch (x->op()) { 1.173 + case Bytecodes::_ladd: set_canonical(x->x()); return; 1.174 + case Bytecodes::_lsub: set_canonical(x->x()); return; 1.175 + case Bytecodes::_lmul: set_constant((jlong)0); return; 1.176 + // Note: for div and rem, make sure that C semantics 1.177 + // corresponds to Java semantics! 1.178 + case Bytecodes::_land: set_constant((jlong)0); return; 1.179 + case Bytecodes::_lor : set_canonical(x->x()); return; 1.180 + } 1.181 + } 1.182 + break; 1.183 + } 1.184 + } 1.185 +} 1.186 + 1.187 + 1.188 +void Canonicalizer::do_Phi (Phi* x) {} 1.189 +void Canonicalizer::do_Constant (Constant* x) {} 1.190 +void Canonicalizer::do_Local (Local* x) {} 1.191 +void Canonicalizer::do_LoadField (LoadField* x) {} 1.192 + 1.193 +// checks if v is in the block that is currently processed by 1.194 +// GraphBuilder. This is the only block that has not BlockEnd yet. 1.195 +static bool in_current_block(Value v) { 1.196 + int max_distance = 4; 1.197 + while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) { 1.198 + v = v->next(); 1.199 + max_distance--; 1.200 + } 1.201 + return v == NULL; 1.202 +} 1.203 + 1.204 +void Canonicalizer::do_StoreField (StoreField* x) { 1.205 + // If a value is going to be stored into a field or array some of 1.206 + // the conversions emitted by javac are unneeded because the fields 1.207 + // are packed to their natural size. 1.208 + Convert* conv = x->value()->as_Convert(); 1.209 + if (conv) { 1.210 + Value value = NULL; 1.211 + BasicType type = x->field()->type()->basic_type(); 1.212 + switch (conv->op()) { 1.213 + case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; 1.214 + case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; 1.215 + case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; 1.216 + } 1.217 + // limit this optimization to current block 1.218 + if (value != NULL && in_current_block(conv)) { 1.219 + set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(), 1.220 + x->state_before(), x->needs_patching())); 1.221 + return; 1.222 + } 1.223 + } 1.224 + 1.225 +} 1.226 + 1.227 +void Canonicalizer::do_ArrayLength (ArrayLength* x) { 1.228 + NewArray* array = x->array()->as_NewArray(); 1.229 + if (array != NULL && array->length() != NULL) { 1.230 + Constant* length = array->length()->as_Constant(); 1.231 + if (length != NULL) { 1.232 + // do not use the Constant itself, but create a new Constant 1.233 + // with same value Otherwise a Constant is live over multiple 1.234 + // blocks without being registered in a state array. 1.235 + assert(length->type()->as_IntConstant() != NULL, "array length must be integer"); 1.236 + set_constant(length->type()->as_IntConstant()->value()); 1.237 + } 1.238 + } else { 1.239 + LoadField* lf = x->array()->as_LoadField(); 1.240 + if (lf != NULL) { 1.241 + ciField* field = lf->field(); 1.242 + if (field->is_constant() && field->is_static()) { 1.243 + // final static field 1.244 + ciObject* c = field->constant_value().as_object(); 1.245 + if (c->is_array()) { 1.246 + ciArray* array = (ciArray*) c; 1.247 + set_constant(array->length()); 1.248 + } 1.249 + } 1.250 + } 1.251 + } 1.252 +} 1.253 + 1.254 +void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {} 1.255 +void Canonicalizer::do_StoreIndexed (StoreIndexed* x) { 1.256 + // If a value is going to be stored into a field or array some of 1.257 + // the conversions emitted by javac are unneeded because the fields 1.258 + // are packed to their natural size. 1.259 + Convert* conv = x->value()->as_Convert(); 1.260 + if (conv) { 1.261 + Value value = NULL; 1.262 + BasicType type = x->elt_type(); 1.263 + switch (conv->op()) { 1.264 + case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; 1.265 + case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; 1.266 + case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; 1.267 + } 1.268 + // limit this optimization to current block 1.269 + if (value != NULL && in_current_block(conv)) { 1.270 + set_canonical(new StoreIndexed(x->array(), x->index(), x->length(), 1.271 + x->elt_type(), value, x->state_before())); 1.272 + return; 1.273 + } 1.274 + } 1.275 + 1.276 + 1.277 +} 1.278 + 1.279 + 1.280 +void Canonicalizer::do_NegateOp(NegateOp* x) { 1.281 + ValueType* t = x->x()->type(); 1.282 + if (t->is_constant()) { 1.283 + switch (t->tag()) { 1.284 + case intTag : set_constant(-t->as_IntConstant ()->value()); return; 1.285 + case longTag : set_constant(-t->as_LongConstant ()->value()); return; 1.286 + case floatTag : set_constant(-t->as_FloatConstant ()->value()); return; 1.287 + case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return; 1.288 + default : ShouldNotReachHere(); 1.289 + } 1.290 + } 1.291 +} 1.292 + 1.293 + 1.294 +void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); } 1.295 + 1.296 + 1.297 +void Canonicalizer::do_ShiftOp (ShiftOp* x) { 1.298 + ValueType* t = x->x()->type(); 1.299 + ValueType* t2 = x->y()->type(); 1.300 + if (t->is_constant()) { 1.301 + switch (t->tag()) { 1.302 + case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break; 1.303 + case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break; 1.304 + default : ShouldNotReachHere(); 1.305 + } 1.306 + if (t2->is_constant()) { 1.307 + if (t->tag() == intTag) { 1.308 + int value = t->as_IntConstant()->value(); 1.309 + int shift = t2->as_IntConstant()->value() & 31; 1.310 + jint mask = ~(~0 << (32 - shift)); 1.311 + if (shift == 0) mask = ~0; 1.312 + switch (x->op()) { 1.313 + case Bytecodes::_ishl: set_constant(value << shift); return; 1.314 + case Bytecodes::_ishr: set_constant(value >> shift); return; 1.315 + case Bytecodes::_iushr: set_constant((value >> shift) & mask); return; 1.316 + } 1.317 + } else if (t->tag() == longTag) { 1.318 + jlong value = t->as_LongConstant()->value(); 1.319 + int shift = t2->as_IntConstant()->value() & 63; 1.320 + jlong mask = ~(~jlong_cast(0) << (64 - shift)); 1.321 + if (shift == 0) mask = ~jlong_cast(0); 1.322 + switch (x->op()) { 1.323 + case Bytecodes::_lshl: set_constant(value << shift); return; 1.324 + case Bytecodes::_lshr: set_constant(value >> shift); return; 1.325 + case Bytecodes::_lushr: set_constant((value >> shift) & mask); return; 1.326 + } 1.327 + } 1.328 + } 1.329 + } 1.330 + if (t2->is_constant()) { 1.331 + switch (t2->tag()) { 1.332 + case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; 1.333 + case longTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; 1.334 + default : ShouldNotReachHere(); 1.335 + } 1.336 + } 1.337 +} 1.338 + 1.339 + 1.340 +void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); } 1.341 +void Canonicalizer::do_CompareOp (CompareOp* x) { 1.342 + if (x->x() == x->y()) { 1.343 + switch (x->x()->type()->tag()) { 1.344 + case longTag: set_constant(0); break; 1.345 + case floatTag: { 1.346 + FloatConstant* fc = x->x()->type()->as_FloatConstant(); 1.347 + if (fc) { 1.348 + if (g_isnan(fc->value())) { 1.349 + set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); 1.350 + } else { 1.351 + set_constant(0); 1.352 + } 1.353 + } 1.354 + break; 1.355 + } 1.356 + case doubleTag: { 1.357 + DoubleConstant* dc = x->x()->type()->as_DoubleConstant(); 1.358 + if (dc) { 1.359 + if (g_isnan(dc->value())) { 1.360 + set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); 1.361 + } else { 1.362 + set_constant(0); 1.363 + } 1.364 + } 1.365 + break; 1.366 + } 1.367 + } 1.368 + } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { 1.369 + switch (x->x()->type()->tag()) { 1.370 + case longTag: { 1.371 + jlong vx = x->x()->type()->as_LongConstant()->value(); 1.372 + jlong vy = x->y()->type()->as_LongConstant()->value(); 1.373 + if (vx == vy) 1.374 + set_constant(0); 1.375 + else if (vx < vy) 1.376 + set_constant(-1); 1.377 + else 1.378 + set_constant(1); 1.379 + break; 1.380 + } 1.381 + 1.382 + case floatTag: { 1.383 + float vx = x->x()->type()->as_FloatConstant()->value(); 1.384 + float vy = x->y()->type()->as_FloatConstant()->value(); 1.385 + if (g_isnan(vx) || g_isnan(vy)) 1.386 + set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); 1.387 + else if (vx == vy) 1.388 + set_constant(0); 1.389 + else if (vx < vy) 1.390 + set_constant(-1); 1.391 + else 1.392 + set_constant(1); 1.393 + break; 1.394 + } 1.395 + 1.396 + case doubleTag: { 1.397 + double vx = x->x()->type()->as_DoubleConstant()->value(); 1.398 + double vy = x->y()->type()->as_DoubleConstant()->value(); 1.399 + if (g_isnan(vx) || g_isnan(vy)) 1.400 + set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); 1.401 + else if (vx == vy) 1.402 + set_constant(0); 1.403 + else if (vx < vy) 1.404 + set_constant(-1); 1.405 + else 1.406 + set_constant(1); 1.407 + break; 1.408 + } 1.409 + } 1.410 + 1.411 + } 1.412 +} 1.413 + 1.414 + 1.415 +void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {} 1.416 + 1.417 +void Canonicalizer::do_IfOp(IfOp* x) { 1.418 + // Caution: do not use do_Op2(x) here for now since 1.419 + // we map the condition to the op for now! 1.420 + move_const_to_right(x); 1.421 +} 1.422 + 1.423 + 1.424 +void Canonicalizer::do_Intrinsic (Intrinsic* x) { 1.425 + switch (x->id()) { 1.426 + case vmIntrinsics::_floatToRawIntBits : { 1.427 + FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant(); 1.428 + if (c != NULL) { 1.429 + JavaValue v; 1.430 + v.set_jfloat(c->value()); 1.431 + set_constant(v.get_jint()); 1.432 + } 1.433 + break; 1.434 + } 1.435 + case vmIntrinsics::_intBitsToFloat : { 1.436 + IntConstant* c = x->argument_at(0)->type()->as_IntConstant(); 1.437 + if (c != NULL) { 1.438 + JavaValue v; 1.439 + v.set_jint(c->value()); 1.440 + set_constant(v.get_jfloat()); 1.441 + } 1.442 + break; 1.443 + } 1.444 + case vmIntrinsics::_doubleToRawLongBits : { 1.445 + DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant(); 1.446 + if (c != NULL) { 1.447 + JavaValue v; 1.448 + v.set_jdouble(c->value()); 1.449 + set_constant(v.get_jlong()); 1.450 + } 1.451 + break; 1.452 + } 1.453 + case vmIntrinsics::_longBitsToDouble : { 1.454 + LongConstant* c = x->argument_at(0)->type()->as_LongConstant(); 1.455 + if (c != NULL) { 1.456 + JavaValue v; 1.457 + v.set_jlong(c->value()); 1.458 + set_constant(v.get_jdouble()); 1.459 + } 1.460 + break; 1.461 + } 1.462 + case vmIntrinsics::_isInstance : { 1.463 + assert(x->number_of_arguments() == 2, "wrong type"); 1.464 + 1.465 + InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant(); 1.466 + if (c != NULL && !c->value()->is_null_object()) { 1.467 + // ciInstance::java_mirror_type() returns non-NULL only for Java mirrors 1.468 + ciType* t = c->value()->as_instance()->java_mirror_type(); 1.469 + if (t->is_klass()) { 1.470 + // substitute cls.isInstance(obj) of a constant Class into 1.471 + // an InstantOf instruction 1.472 + InstanceOf* i = new InstanceOf(t->as_klass(), x->argument_at(1), x->state_before()); 1.473 + set_canonical(i); 1.474 + // and try to canonicalize even further 1.475 + do_InstanceOf(i); 1.476 + } else { 1.477 + assert(t->is_primitive_type(), "should be a primitive type"); 1.478 + // cls.isInstance(obj) always returns false for primitive classes 1.479 + set_constant(0); 1.480 + } 1.481 + } 1.482 + break; 1.483 + } 1.484 + } 1.485 +} 1.486 + 1.487 +void Canonicalizer::do_Convert (Convert* x) { 1.488 + if (x->value()->type()->is_constant()) { 1.489 + switch (x->op()) { 1.490 + case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break; 1.491 + case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break; 1.492 + case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break; 1.493 + case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break; 1.494 + case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break; 1.495 + case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break; 1.496 + case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break; 1.497 + case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break; 1.498 + case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break; 1.499 + case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break; 1.500 + case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break; 1.501 + case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break; 1.502 + case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break; 1.503 + case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break; 1.504 + case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break; 1.505 + default: 1.506 + ShouldNotReachHere(); 1.507 + } 1.508 + } 1.509 + 1.510 + Value value = x->value(); 1.511 + BasicType type = T_ILLEGAL; 1.512 + LoadField* lf = value->as_LoadField(); 1.513 + if (lf) { 1.514 + type = lf->field_type(); 1.515 + } else { 1.516 + LoadIndexed* li = value->as_LoadIndexed(); 1.517 + if (li) { 1.518 + type = li->elt_type(); 1.519 + } else { 1.520 + Convert* conv = value->as_Convert(); 1.521 + if (conv) { 1.522 + switch (conv->op()) { 1.523 + case Bytecodes::_i2b: type = T_BYTE; break; 1.524 + case Bytecodes::_i2s: type = T_SHORT; break; 1.525 + case Bytecodes::_i2c: type = T_CHAR; break; 1.526 + } 1.527 + } 1.528 + } 1.529 + } 1.530 + if (type != T_ILLEGAL) { 1.531 + switch (x->op()) { 1.532 + case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break; 1.533 + case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break; 1.534 + case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break; 1.535 + } 1.536 + } else { 1.537 + Op2* op2 = x->value()->as_Op2(); 1.538 + if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) { 1.539 + jint safebits = 0; 1.540 + jint mask = op2->y()->type()->as_IntConstant()->value(); 1.541 + switch (x->op()) { 1.542 + case Bytecodes::_i2b: safebits = 0x7f; break; 1.543 + case Bytecodes::_i2s: safebits = 0x7fff; break; 1.544 + case Bytecodes::_i2c: safebits = 0xffff; break; 1.545 + } 1.546 + // When casting a masked integer to a smaller signed type, if 1.547 + // the mask doesn't include the sign bit the cast isn't needed. 1.548 + if (safebits && (mask & ~safebits) == 0) { 1.549 + set_canonical(x->value()); 1.550 + } 1.551 + } 1.552 + } 1.553 + 1.554 +} 1.555 + 1.556 +void Canonicalizer::do_NullCheck (NullCheck* x) { 1.557 + if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) { 1.558 + set_canonical(x->obj()); 1.559 + } else { 1.560 + Constant* con = x->obj()->as_Constant(); 1.561 + if (con) { 1.562 + ObjectType* c = con->type()->as_ObjectType(); 1.563 + if (c && c->is_loaded()) { 1.564 + ObjectConstant* oc = c->as_ObjectConstant(); 1.565 + if (!oc || !oc->value()->is_null_object()) { 1.566 + set_canonical(con); 1.567 + } 1.568 + } 1.569 + } 1.570 + } 1.571 +} 1.572 + 1.573 +void Canonicalizer::do_TypeCast (TypeCast* x) {} 1.574 +void Canonicalizer::do_Invoke (Invoke* x) {} 1.575 +void Canonicalizer::do_NewInstance (NewInstance* x) {} 1.576 +void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {} 1.577 +void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {} 1.578 +void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {} 1.579 +void Canonicalizer::do_CheckCast (CheckCast* x) { 1.580 + if (x->klass()->is_loaded()) { 1.581 + Value obj = x->obj(); 1.582 + ciType* klass = obj->exact_type(); 1.583 + if (klass == NULL) klass = obj->declared_type(); 1.584 + if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) { 1.585 + set_canonical(obj); 1.586 + return; 1.587 + } 1.588 + // checkcast of null returns null 1.589 + if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { 1.590 + set_canonical(obj); 1.591 + } 1.592 + } 1.593 +} 1.594 +void Canonicalizer::do_InstanceOf (InstanceOf* x) { 1.595 + if (x->klass()->is_loaded()) { 1.596 + Value obj = x->obj(); 1.597 + ciType* exact = obj->exact_type(); 1.598 + if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) { 1.599 + set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0); 1.600 + return; 1.601 + } 1.602 + // instanceof null returns false 1.603 + if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { 1.604 + set_constant(0); 1.605 + } 1.606 + } 1.607 + 1.608 +} 1.609 +void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {} 1.610 +void Canonicalizer::do_MonitorExit (MonitorExit* x) {} 1.611 +void Canonicalizer::do_BlockBegin (BlockBegin* x) {} 1.612 +void Canonicalizer::do_Goto (Goto* x) {} 1.613 + 1.614 + 1.615 +static bool is_true(jlong x, If::Condition cond, jlong y) { 1.616 + switch (cond) { 1.617 + case If::eql: return x == y; 1.618 + case If::neq: return x != y; 1.619 + case If::lss: return x < y; 1.620 + case If::leq: return x <= y; 1.621 + case If::gtr: return x > y; 1.622 + case If::geq: return x >= y; 1.623 + } 1.624 + ShouldNotReachHere(); 1.625 + return false; 1.626 +} 1.627 + 1.628 +static bool is_safepoint(BlockEnd* x, BlockBegin* sux) { 1.629 + // An Instruction with multiple successors, x, is replaced by a Goto 1.630 + // to a single successor, sux. Is a safepoint check needed = was the 1.631 + // instruction being replaced a safepoint and the single remaining 1.632 + // successor a back branch? 1.633 + return x->is_safepoint() && (sux->bci() < x->state_before()->bci()); 1.634 +} 1.635 + 1.636 +void Canonicalizer::do_If(If* x) { 1.637 + // move const to right 1.638 + if (x->x()->type()->is_constant()) x->swap_operands(); 1.639 + // simplify 1.640 + const Value l = x->x(); ValueType* lt = l->type(); 1.641 + const Value r = x->y(); ValueType* rt = r->type(); 1.642 + 1.643 + if (l == r && !lt->is_float_kind()) { 1.644 + // pattern: If (a cond a) => simplify to Goto 1.645 + BlockBegin* sux; 1.646 + switch (x->cond()) { 1.647 + case If::eql: sux = x->sux_for(true); break; 1.648 + case If::neq: sux = x->sux_for(false); break; 1.649 + case If::lss: sux = x->sux_for(false); break; 1.650 + case If::leq: sux = x->sux_for(true); break; 1.651 + case If::gtr: sux = x->sux_for(false); break; 1.652 + case If::geq: sux = x->sux_for(true); break; 1.653 + } 1.654 + // If is a safepoint then the debug information should come from the state_before of the If. 1.655 + set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux))); 1.656 + return; 1.657 + } 1.658 + 1.659 + if (lt->is_constant() && rt->is_constant()) { 1.660 + if (x->x()->as_Constant() != NULL) { 1.661 + // pattern: If (lc cond rc) => simplify to: Goto 1.662 + BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(), 1.663 + x->sux_for(true), 1.664 + x->sux_for(false)); 1.665 + if (sux != NULL) { 1.666 + // If is a safepoint then the debug information should come from the state_before of the If. 1.667 + set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux))); 1.668 + } 1.669 + } 1.670 + } else if (rt->as_IntConstant() != NULL) { 1.671 + // pattern: If (l cond rc) => investigate further 1.672 + const jint rc = rt->as_IntConstant()->value(); 1.673 + if (l->as_CompareOp() != NULL) { 1.674 + // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto 1.675 + CompareOp* cmp = l->as_CompareOp(); 1.676 + bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl; 1.677 + BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b 1.678 + BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b 1.679 + BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b 1.680 + BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered 1.681 + // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are 1.682 + // equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either 1.683 + // lss_sux or gtr_sux. 1.684 + if (lss_sux == eql_sux && eql_sux == gtr_sux) { 1.685 + // all successors identical => simplify to: Goto 1.686 + set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint())); 1.687 + } else { 1.688 + // two successors differ and two successors are the same => simplify to: If (x cmp y) 1.689 + // determine new condition & successors 1.690 + If::Condition cond; 1.691 + BlockBegin* tsux = NULL; 1.692 + BlockBegin* fsux = NULL; 1.693 + if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; } 1.694 + else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; } 1.695 + else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; } 1.696 + else { ShouldNotReachHere(); } 1.697 + If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint()); 1.698 + if (cmp->x() == cmp->y()) { 1.699 + do_If(canon); 1.700 + } else { 1.701 + if (compilation()->profile_branches()) { 1.702 + // TODO: If profiling, leave floating point comparisons unoptimized. 1.703 + // We currently do not support profiling of the unordered case. 1.704 + switch(cmp->op()) { 1.705 + case Bytecodes::_fcmpl: case Bytecodes::_fcmpg: 1.706 + case Bytecodes::_dcmpl: case Bytecodes::_dcmpg: 1.707 + set_canonical(x); 1.708 + return; 1.709 + } 1.710 + } 1.711 + set_bci(cmp->state_before()->bci()); 1.712 + set_canonical(canon); 1.713 + } 1.714 + } 1.715 + } else if (l->as_InstanceOf() != NULL) { 1.716 + // NOTE: Code permanently disabled for now since it leaves the old InstanceOf 1.717 + // instruction in the graph (it is pinned). Need to fix this at some point. 1.718 + // It should also be left in the graph when generating a profiled method version or Goto 1.719 + // has to know that it was an InstanceOf. 1.720 + return; 1.721 + // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto 1.722 + InstanceOf* inst = l->as_InstanceOf(); 1.723 + BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1 1.724 + BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0 1.725 + if (is_inst_sux == no_inst_sux && inst->is_loaded()) { 1.726 + // both successors identical and klass is loaded => simplify to: Goto 1.727 + set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint())); 1.728 + } else { 1.729 + // successors differ => simplify to: IfInstanceOf 1.730 + set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->state_before()->bci(), is_inst_sux, no_inst_sux)); 1.731 + } 1.732 + } 1.733 + } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) { 1.734 + if (x->cond() == Instruction::eql) { 1.735 + BlockBegin* sux = x->fsux(); 1.736 + set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux))); 1.737 + } else { 1.738 + assert(x->cond() == Instruction::neq, "only other valid case"); 1.739 + BlockBegin* sux = x->tsux(); 1.740 + set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux))); 1.741 + } 1.742 + } 1.743 +} 1.744 + 1.745 + 1.746 +void Canonicalizer::do_TableSwitch(TableSwitch* x) { 1.747 + if (x->tag()->type()->is_constant()) { 1.748 + int v = x->tag()->type()->as_IntConstant()->value(); 1.749 + BlockBegin* sux = x->default_sux(); 1.750 + if (v >= x->lo_key() && v <= x->hi_key()) { 1.751 + sux = x->sux_at(v - x->lo_key()); 1.752 + } 1.753 + set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux))); 1.754 + } else if (x->number_of_sux() == 1) { 1.755 + // NOTE: Code permanently disabled for now since the switch statement's 1.756 + // tag expression may produce side-effects in which case it must 1.757 + // be executed. 1.758 + return; 1.759 + // simplify to Goto 1.760 + set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); 1.761 + } else if (x->number_of_sux() == 2) { 1.762 + // NOTE: Code permanently disabled for now since it produces two new nodes 1.763 + // (Constant & If) and the Canonicalizer cannot return them correctly 1.764 + // yet. For now we copied the corresponding code directly into the 1.765 + // GraphBuilder (i.e., we should never reach here). 1.766 + return; 1.767 + // simplify to If 1.768 + assert(x->lo_key() == x->hi_key(), "keys must be the same"); 1.769 + Constant* key = new Constant(new IntConstant(x->lo_key())); 1.770 + set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint())); 1.771 + } 1.772 +} 1.773 + 1.774 + 1.775 +void Canonicalizer::do_LookupSwitch(LookupSwitch* x) { 1.776 + if (x->tag()->type()->is_constant()) { 1.777 + int v = x->tag()->type()->as_IntConstant()->value(); 1.778 + BlockBegin* sux = x->default_sux(); 1.779 + for (int i = 0; i < x->length(); i++) { 1.780 + if (v == x->key_at(i)) { 1.781 + sux = x->sux_at(i); 1.782 + } 1.783 + } 1.784 + set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux))); 1.785 + } else if (x->number_of_sux() == 1) { 1.786 + // NOTE: Code permanently disabled for now since the switch statement's 1.787 + // tag expression may produce side-effects in which case it must 1.788 + // be executed. 1.789 + return; 1.790 + // simplify to Goto 1.791 + set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); 1.792 + } else if (x->number_of_sux() == 2) { 1.793 + // NOTE: Code permanently disabled for now since it produces two new nodes 1.794 + // (Constant & If) and the Canonicalizer cannot return them correctly 1.795 + // yet. For now we copied the corresponding code directly into the 1.796 + // GraphBuilder (i.e., we should never reach here). 1.797 + return; 1.798 + // simplify to If 1.799 + assert(x->length() == 1, "length must be the same"); 1.800 + Constant* key = new Constant(new IntConstant(x->key_at(0))); 1.801 + set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint())); 1.802 + } 1.803 +} 1.804 + 1.805 + 1.806 +void Canonicalizer::do_Return (Return* x) {} 1.807 +void Canonicalizer::do_Throw (Throw* x) {} 1.808 +void Canonicalizer::do_Base (Base* x) {} 1.809 +void Canonicalizer::do_OsrEntry (OsrEntry* x) {} 1.810 +void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {} 1.811 + 1.812 +static bool match_index_and_scale(Instruction* instr, 1.813 + Instruction** index, 1.814 + int* log2_scale, 1.815 + Instruction** instr_to_unpin) { 1.816 + *instr_to_unpin = NULL; 1.817 + 1.818 + // Skip conversion ops 1.819 + Convert* convert = instr->as_Convert(); 1.820 + if (convert != NULL) { 1.821 + instr = convert->value(); 1.822 + } 1.823 + 1.824 + ShiftOp* shift = instr->as_ShiftOp(); 1.825 + if (shift != NULL) { 1.826 + if (shift->is_pinned()) { 1.827 + *instr_to_unpin = shift; 1.828 + } 1.829 + // Constant shift value? 1.830 + Constant* con = shift->y()->as_Constant(); 1.831 + if (con == NULL) return false; 1.832 + // Well-known type and value? 1.833 + IntConstant* val = con->type()->as_IntConstant(); 1.834 + if (val == NULL) return false; 1.835 + if (shift->x()->type() != intType) return false; 1.836 + *index = shift->x(); 1.837 + int tmp_scale = val->value(); 1.838 + if (tmp_scale >= 0 && tmp_scale < 4) { 1.839 + *log2_scale = tmp_scale; 1.840 + return true; 1.841 + } else { 1.842 + return false; 1.843 + } 1.844 + } 1.845 + 1.846 + ArithmeticOp* arith = instr->as_ArithmeticOp(); 1.847 + if (arith != NULL) { 1.848 + if (arith->is_pinned()) { 1.849 + *instr_to_unpin = arith; 1.850 + } 1.851 + // Check for integer multiply 1.852 + if (arith->op() == Bytecodes::_imul) { 1.853 + // See if either arg is a known constant 1.854 + Constant* con = arith->x()->as_Constant(); 1.855 + if (con != NULL) { 1.856 + *index = arith->y(); 1.857 + } else { 1.858 + con = arith->y()->as_Constant(); 1.859 + if (con == NULL) return false; 1.860 + *index = arith->x(); 1.861 + } 1.862 + if ((*index)->type() != intType) return false; 1.863 + // Well-known type and value? 1.864 + IntConstant* val = con->type()->as_IntConstant(); 1.865 + if (val == NULL) return false; 1.866 + switch (val->value()) { 1.867 + case 1: *log2_scale = 0; return true; 1.868 + case 2: *log2_scale = 1; return true; 1.869 + case 4: *log2_scale = 2; return true; 1.870 + case 8: *log2_scale = 3; return true; 1.871 + default: return false; 1.872 + } 1.873 + } 1.874 + } 1.875 + 1.876 + // Unknown instruction sequence; don't touch it 1.877 + return false; 1.878 +} 1.879 + 1.880 + 1.881 +static bool match(UnsafeRawOp* x, 1.882 + Instruction** base, 1.883 + Instruction** index, 1.884 + int* log2_scale) { 1.885 + Instruction* instr_to_unpin = NULL; 1.886 + ArithmeticOp* root = x->base()->as_ArithmeticOp(); 1.887 + if (root == NULL) return false; 1.888 + // Limit ourselves to addition for now 1.889 + if (root->op() != Bytecodes::_ladd) return false; 1.890 + // Try to find shift or scale op 1.891 + if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) { 1.892 + *base = root->x(); 1.893 + } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) { 1.894 + *base = root->y(); 1.895 + } else if (root->y()->as_Convert() != NULL) { 1.896 + Convert* convert = root->y()->as_Convert(); 1.897 + if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) { 1.898 + // pick base and index, setting scale at 1 1.899 + *base = root->x(); 1.900 + *index = convert->value(); 1.901 + *log2_scale = 0; 1.902 + } else { 1.903 + return false; 1.904 + } 1.905 + } else { 1.906 + // doesn't match any expected sequences 1.907 + return false; 1.908 + } 1.909 + 1.910 + // If the value is pinned then it will be always be computed so 1.911 + // there's no profit to reshaping the expression. 1.912 + return !root->is_pinned(); 1.913 +} 1.914 + 1.915 + 1.916 +void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) { 1.917 + Instruction* base = NULL; 1.918 + Instruction* index = NULL; 1.919 + int log2_scale; 1.920 + 1.921 + if (match(x, &base, &index, &log2_scale)) { 1.922 + x->set_base(base); 1.923 + x->set_index(index); 1.924 + x->set_log2_scale(log2_scale); 1.925 + if (PrintUnsafeOptimization) { 1.926 + tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d", 1.927 + x->id(), x->base()->id(), x->index()->id(), x->log2_scale()); 1.928 + } 1.929 + } 1.930 +} 1.931 + 1.932 +void Canonicalizer::do_RoundFP(RoundFP* x) {} 1.933 +void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } 1.934 +void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } 1.935 +void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {} 1.936 +void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {} 1.937 +void Canonicalizer::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {} 1.938 +void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {} 1.939 +void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {} 1.940 +void Canonicalizer::do_ProfileCall(ProfileCall* x) {} 1.941 +void Canonicalizer::do_ProfileReturnType(ProfileReturnType* x) {} 1.942 +void Canonicalizer::do_ProfileInvoke(ProfileInvoke* x) {} 1.943 +void Canonicalizer::do_RuntimeCall(RuntimeCall* x) {} 1.944 +void Canonicalizer::do_RangeCheckPredicate(RangeCheckPredicate* x) {} 1.945 +#ifdef ASSERT 1.946 +void Canonicalizer::do_Assert(Assert* x) {} 1.947 +#endif 1.948 +void Canonicalizer::do_MemBar(MemBar* x) {}