c1_Canonicalizer.cpp revision 1472:c18cbe5936b8
138032Speter/* 238032Speter * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 338032Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 438032Speter * 538032Speter * This code is free software; you can redistribute it and/or modify it 638032Speter * under the terms of the GNU General Public License version 2 only, as 738032Speter * published by the Free Software Foundation. 838032Speter * 938032Speter * This code is distributed in the hope that it will be useful, but WITHOUT 1038032Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1138032Speter * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1238032Speter * version 2 for more details (a copy is included in the LICENSE file that 1338032Speter * accompanied this code). 1438032Speter * 1538032Speter * You should have received a copy of the GNU General Public License version 1638032Speter * 2 along with this work; if not, write to the Free Software Foundation, 1738032Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1838032Speter * 1938032Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2038032Speter * or visit www.oracle.com if you need additional information or have any 2138032Speter * questions. 2238032Speter * 2338032Speter */ 2438032Speter 2538032Speter#include "incls/_precompiled.incl" 2638032Speter#include "incls/_c1_Canonicalizer.cpp.incl" 2738032Speter 2838032Speter 2938032Speterstatic void do_print_value(Value* vp) { 3038032Speter (*vp)->print_line(); 3138032Speter} 3238032Speter 3338032Spetervoid Canonicalizer::set_canonical(Value x) { 3438032Speter assert(x != NULL, "value must exist"); 3538032Speter // Note: we can not currently substitute root nodes which show up in 3638032Speter // the instruction stream (because the instruction list is embedded 3738032Speter // in the instructions). 3838032Speter if (canonical() != x) { 3938032Speter if (PrintCanonicalization) { 4038032Speter canonical()->input_values_do(do_print_value); 4138032Speter canonical()->print_line(); 4238032Speter tty->print_cr("canonicalized to:"); 4338032Speter x->input_values_do(do_print_value); 4438032Speter x->print_line(); 4538032Speter tty->cr(); 4638032Speter } 4738032Speter assert(_canonical->type()->tag() == x->type()->tag(), "types must match"); 4838032Speter _canonical = x; 4938032Speter } 5038032Speter} 5138032Speter 5238032Speter 5338032Spetervoid Canonicalizer::move_const_to_right(Op2* x) { 5438032Speter if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands(); 5538032Speter} 5638032Speter 5738032Speter 5838032Spetervoid Canonicalizer::do_Op2(Op2* x) { 5938032Speter if (x->x() == x->y()) { 6038032Speter switch (x->op()) { 6138032Speter case Bytecodes::_isub: set_constant(0); return; 6238032Speter case Bytecodes::_lsub: set_constant(jlong_cast(0)); return; 6338032Speter case Bytecodes::_iand: // fall through 6438032Speter case Bytecodes::_land: // fall through 6538032Speter case Bytecodes::_ior: // fall through 6638032Speter case Bytecodes::_lor : set_canonical(x->x()); return; 6738032Speter case Bytecodes::_ixor: set_constant(0); return; 6838032Speter case Bytecodes::_lxor: set_constant(jlong_cast(0)); return; 6938032Speter } 7038032Speter } 7138032Speter 7238032Speter if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { 7338032Speter // do constant folding for selected operations 7438032Speter switch (x->type()->tag()) { 7538032Speter case intTag: 7638032Speter { jint a = x->x()->type()->as_IntConstant()->value(); 7738032Speter jint b = x->y()->type()->as_IntConstant()->value(); 7838032Speter switch (x->op()) { 7938032Speter case Bytecodes::_iadd: set_constant(a + b); return; 8038032Speter case Bytecodes::_isub: set_constant(a - b); return; 8138032Speter case Bytecodes::_imul: set_constant(a * b); return; 8238032Speter case Bytecodes::_idiv: 8338032Speter if (b != 0) { 8438032Speter if (a == min_jint && b == -1) { 8538032Speter set_constant(min_jint); 8638032Speter } else { 8738032Speter set_constant(a / b); 8838032Speter } 8938032Speter return; 9038032Speter } 9138032Speter break; 9238032Speter case Bytecodes::_irem: 9338032Speter if (b != 0) { 9438032Speter if (a == min_jint && b == -1) { 9538032Speter set_constant(0); 9638032Speter } else { 9738032Speter set_constant(a % b); 9838032Speter } 9938032Speter return; 10038032Speter } 10138032Speter break; 10238032Speter case Bytecodes::_iand: set_constant(a & b); return; 10338032Speter case Bytecodes::_ior : set_constant(a | b); return; 10438032Speter case Bytecodes::_ixor: set_constant(a ^ b); return; 10538032Speter } 10638032Speter } 10738032Speter break; 10838032Speter case longTag: 10938032Speter { jlong a = x->x()->type()->as_LongConstant()->value(); 11038032Speter jlong b = x->y()->type()->as_LongConstant()->value(); 11138032Speter switch (x->op()) { 11238032Speter case Bytecodes::_ladd: set_constant(a + b); return; 11338032Speter case Bytecodes::_lsub: set_constant(a - b); return; 11438032Speter case Bytecodes::_lmul: set_constant(a * b); return; 11538032Speter case Bytecodes::_ldiv: 11638032Speter if (b != 0) { 11738032Speter set_constant(SharedRuntime::ldiv(b, a)); 11838032Speter return; 11938032Speter } 12038032Speter break; 12138032Speter case Bytecodes::_lrem: 12238032Speter if (b != 0) { 12338032Speter set_constant(SharedRuntime::lrem(b, a)); 12438032Speter return; 12538032Speter } 12638032Speter break; 12738032Speter case Bytecodes::_land: set_constant(a & b); return; 12838032Speter case Bytecodes::_lor : set_constant(a | b); return; 12938032Speter case Bytecodes::_lxor: set_constant(a ^ b); return; 13038032Speter } 13138032Speter } 13238032Speter break; 13338032Speter // other cases not implemented (must be extremely careful with floats & doubles!) 13438032Speter } 13538032Speter } 13638032Speter // make sure constant is on the right side, if any 13738032Speter move_const_to_right(x); 13838032Speter 13938032Speter if (x->y()->type()->is_constant()) { 14038032Speter // do constant folding for selected operations 14138032Speter switch (x->type()->tag()) { 14238032Speter case intTag: 14338032Speter if (x->y()->type()->as_IntConstant()->value() == 0) { 14438032Speter switch (x->op()) { 14538032Speter case Bytecodes::_iadd: set_canonical(x->x()); return; 14638032Speter case Bytecodes::_isub: set_canonical(x->x()); return; 14738032Speter case Bytecodes::_imul: set_constant(0); return; 14838032Speter // Note: for div and rem, make sure that C semantics 14938032Speter // corresponds to Java semantics! 15038032Speter case Bytecodes::_iand: set_constant(0); return; 15138032Speter case Bytecodes::_ior : set_canonical(x->x()); return; 15238032Speter } 15338032Speter } 15438032Speter break; 15538032Speter case longTag: 15638032Speter if (x->y()->type()->as_LongConstant()->value() == (jlong)0) { 15738032Speter switch (x->op()) { 15838032Speter case Bytecodes::_ladd: set_canonical(x->x()); return; 15938032Speter case Bytecodes::_lsub: set_canonical(x->x()); return; 16038032Speter case Bytecodes::_lmul: set_constant((jlong)0); return; 16138032Speter // Note: for div and rem, make sure that C semantics 16238032Speter // corresponds to Java semantics! 16338032Speter case Bytecodes::_land: set_constant((jlong)0); return; 16438032Speter case Bytecodes::_lor : set_canonical(x->x()); return; 16538032Speter } 16638032Speter } 16738032Speter break; 16838032Speter } 16938032Speter } 17038032Speter} 17138032Speter 17238032Speter 17338032Spetervoid Canonicalizer::do_Phi (Phi* x) {} 17438032Spetervoid Canonicalizer::do_Constant (Constant* x) {} 17538032Spetervoid Canonicalizer::do_Local (Local* x) {} 17638032Spetervoid Canonicalizer::do_LoadField (LoadField* x) {} 17738032Speter 17838032Speter// checks if v is in the block that is currently processed by 17938032Speter// GraphBuilder. This is the only block that has not BlockEnd yet. 18038032Speterstatic bool in_current_block(Value v) { 18138032Speter int max_distance = 4; 18238032Speter while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) { 18338032Speter v = v->next(); 18438032Speter max_distance--; 18538032Speter } 18638032Speter return v == NULL; 18738032Speter} 18838032Speter 18938032Spetervoid Canonicalizer::do_StoreField (StoreField* x) { 19038032Speter // If a value is going to be stored into a field or array some of 19138032Speter // the conversions emitted by javac are unneeded because the fields 19238032Speter // are packed to their natural size. 19338032Speter Convert* conv = x->value()->as_Convert(); 19438032Speter if (conv) { 19538032Speter Value value = NULL; 19638032Speter BasicType type = x->field()->type()->basic_type(); 19738032Speter switch (conv->op()) { 19838032Speter case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; 19938032Speter case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; 20038032Speter case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; 20138032Speter } 20238032Speter // limit this optimization to current block 20338032Speter if (value != NULL && in_current_block(conv)) { 20438032Speter set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(), 20538032Speter x->lock_stack(), x->state_before(), x->is_loaded(), x->is_initialized())); 206 return; 207 } 208 } 209 210} 211 212void Canonicalizer::do_ArrayLength (ArrayLength* x) { 213 NewArray* array = x->array()->as_NewArray(); 214 if (array != NULL && array->length() != NULL) { 215 Constant* length = array->length()->as_Constant(); 216 if (length != NULL) { 217 // do not use the Constant itself, but create a new Constant 218 // with same value Otherwise a Constant is live over multiple 219 // blocks without being registered in a state array. 220 assert(length->type()->as_IntConstant() != NULL, "array length must be integer"); 221 set_constant(length->type()->as_IntConstant()->value()); 222 } 223 } else { 224 LoadField* lf = x->array()->as_LoadField(); 225 if (lf != NULL) { 226 ciField* field = lf->field(); 227 if (field->is_constant() && field->is_static()) { 228 // final static field 229 ciObject* c = field->constant_value().as_object(); 230 if (c->is_array()) { 231 ciArray* array = (ciArray*) c; 232 set_constant(array->length()); 233 } 234 } 235 } 236 } 237} 238 239void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {} 240void Canonicalizer::do_StoreIndexed (StoreIndexed* x) { 241 // If a value is going to be stored into a field or array some of 242 // the conversions emitted by javac are unneeded because the fields 243 // are packed to their natural size. 244 Convert* conv = x->value()->as_Convert(); 245 if (conv) { 246 Value value = NULL; 247 BasicType type = x->elt_type(); 248 switch (conv->op()) { 249 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; 250 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; 251 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; 252 } 253 // limit this optimization to current block 254 if (value != NULL && in_current_block(conv)) { 255 set_canonical(new StoreIndexed(x->array(), x->index(), x->length(), 256 x->elt_type(), value, x->lock_stack())); 257 return; 258 } 259 } 260 261 262} 263 264 265void Canonicalizer::do_NegateOp(NegateOp* x) { 266 ValueType* t = x->x()->type(); 267 if (t->is_constant()) { 268 switch (t->tag()) { 269 case intTag : set_constant(-t->as_IntConstant ()->value()); return; 270 case longTag : set_constant(-t->as_LongConstant ()->value()); return; 271 case floatTag : set_constant(-t->as_FloatConstant ()->value()); return; 272 case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return; 273 default : ShouldNotReachHere(); 274 } 275 } 276} 277 278 279void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); } 280 281 282void Canonicalizer::do_ShiftOp (ShiftOp* x) { 283 ValueType* t = x->x()->type(); 284 ValueType* t2 = x->y()->type(); 285 if (t->is_constant()) { 286 switch (t->tag()) { 287 case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break; 288 case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break; 289 default : ShouldNotReachHere(); 290 } 291 if (t2->is_constant()) { 292 if (t->tag() == intTag) { 293 int value = t->as_IntConstant()->value(); 294 int shift = t2->as_IntConstant()->value() & 31; 295 jint mask = ~(~0 << (32 - shift)); 296 if (shift == 0) mask = ~0; 297 switch (x->op()) { 298 case Bytecodes::_ishl: set_constant(value << shift); return; 299 case Bytecodes::_ishr: set_constant(value >> shift); return; 300 case Bytecodes::_iushr: set_constant((value >> shift) & mask); return; 301 } 302 } else if (t->tag() == longTag) { 303 jlong value = t->as_LongConstant()->value(); 304 int shift = t2->as_IntConstant()->value() & 63; 305 jlong mask = ~(~jlong_cast(0) << (64 - shift)); 306 if (shift == 0) mask = ~jlong_cast(0); 307 switch (x->op()) { 308 case Bytecodes::_lshl: set_constant(value << shift); return; 309 case Bytecodes::_lshr: set_constant(value >> shift); return; 310 case Bytecodes::_lushr: set_constant((value >> shift) & mask); return; 311 } 312 } 313 } 314 } 315 if (t2->is_constant()) { 316 switch (t2->tag()) { 317 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; 318 case longTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; 319 default : ShouldNotReachHere(); 320 } 321 } 322} 323 324 325void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); } 326void Canonicalizer::do_CompareOp (CompareOp* x) { 327 if (x->x() == x->y()) { 328 switch (x->x()->type()->tag()) { 329 case longTag: set_constant(0); break; 330 case floatTag: { 331 FloatConstant* fc = x->x()->type()->as_FloatConstant(); 332 if (fc) { 333 if (g_isnan(fc->value())) { 334 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); 335 } else { 336 set_constant(0); 337 } 338 } 339 break; 340 } 341 case doubleTag: { 342 DoubleConstant* dc = x->x()->type()->as_DoubleConstant(); 343 if (dc) { 344 if (g_isnan(dc->value())) { 345 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); 346 } else { 347 set_constant(0); 348 } 349 } 350 break; 351 } 352 } 353 } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { 354 switch (x->x()->type()->tag()) { 355 case longTag: { 356 jlong vx = x->x()->type()->as_LongConstant()->value(); 357 jlong vy = x->y()->type()->as_LongConstant()->value(); 358 if (vx == vy) 359 set_constant(0); 360 else if (vx < vy) 361 set_constant(-1); 362 else 363 set_constant(1); 364 break; 365 } 366 367 case floatTag: { 368 float vx = x->x()->type()->as_FloatConstant()->value(); 369 float vy = x->y()->type()->as_FloatConstant()->value(); 370 if (g_isnan(vx) || g_isnan(vy)) 371 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); 372 else if (vx == vy) 373 set_constant(0); 374 else if (vx < vy) 375 set_constant(-1); 376 else 377 set_constant(1); 378 break; 379 } 380 381 case doubleTag: { 382 double vx = x->x()->type()->as_DoubleConstant()->value(); 383 double vy = x->y()->type()->as_DoubleConstant()->value(); 384 if (g_isnan(vx) || g_isnan(vy)) 385 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); 386 else if (vx == vy) 387 set_constant(0); 388 else if (vx < vy) 389 set_constant(-1); 390 else 391 set_constant(1); 392 break; 393 } 394 } 395 396 } 397} 398 399 400void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {} 401 402void Canonicalizer::do_IfOp(IfOp* x) { 403 // Caution: do not use do_Op2(x) here for now since 404 // we map the condition to the op for now! 405 move_const_to_right(x); 406} 407 408 409void Canonicalizer::do_Intrinsic (Intrinsic* x) { 410 switch (x->id()) { 411 case vmIntrinsics::_floatToRawIntBits : { 412 FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant(); 413 if (c != NULL) { 414 JavaValue v; 415 v.set_jfloat(c->value()); 416 set_constant(v.get_jint()); 417 } 418 break; 419 } 420 case vmIntrinsics::_intBitsToFloat : { 421 IntConstant* c = x->argument_at(0)->type()->as_IntConstant(); 422 if (c != NULL) { 423 JavaValue v; 424 v.set_jint(c->value()); 425 set_constant(v.get_jfloat()); 426 } 427 break; 428 } 429 case vmIntrinsics::_doubleToRawLongBits : { 430 DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant(); 431 if (c != NULL) { 432 JavaValue v; 433 v.set_jdouble(c->value()); 434 set_constant(v.get_jlong()); 435 } 436 break; 437 } 438 case vmIntrinsics::_longBitsToDouble : { 439 LongConstant* c = x->argument_at(0)->type()->as_LongConstant(); 440 if (c != NULL) { 441 JavaValue v; 442 v.set_jlong(c->value()); 443 set_constant(v.get_jdouble()); 444 } 445 break; 446 } 447 } 448} 449 450void Canonicalizer::do_Convert (Convert* x) { 451 if (x->value()->type()->is_constant()) { 452 switch (x->op()) { 453 case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break; 454 case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break; 455 case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break; 456 case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break; 457 case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break; 458 case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break; 459 case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break; 460 case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break; 461 case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break; 462 case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break; 463 case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break; 464 case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break; 465 case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break; 466 case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break; 467 case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break; 468 default: 469 ShouldNotReachHere(); 470 } 471 } 472 473 Value value = x->value(); 474 BasicType type = T_ILLEGAL; 475 LoadField* lf = value->as_LoadField(); 476 if (lf) { 477 type = lf->field_type(); 478 } else { 479 LoadIndexed* li = value->as_LoadIndexed(); 480 if (li) { 481 type = li->elt_type(); 482 } else { 483 Convert* conv = value->as_Convert(); 484 if (conv) { 485 switch (conv->op()) { 486 case Bytecodes::_i2b: type = T_BYTE; break; 487 case Bytecodes::_i2s: type = T_SHORT; break; 488 case Bytecodes::_i2c: type = T_CHAR; break; 489 } 490 } 491 } 492 } 493 if (type != T_ILLEGAL) { 494 switch (x->op()) { 495 case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break; 496 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break; 497 case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break; 498 } 499 } else { 500 Op2* op2 = x->value()->as_Op2(); 501 if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) { 502 jint safebits = 0; 503 jint mask = op2->y()->type()->as_IntConstant()->value(); 504 switch (x->op()) { 505 case Bytecodes::_i2b: safebits = 0x7f; break; 506 case Bytecodes::_i2s: safebits = 0x7fff; break; 507 case Bytecodes::_i2c: safebits = 0xffff; break; 508 } 509 // When casting a masked integer to a smaller signed type, if 510 // the mask doesn't include the sign bit the cast isn't needed. 511 if (safebits && (mask & ~safebits) == 0) { 512 set_canonical(x->value()); 513 } 514 } 515 } 516 517} 518 519void Canonicalizer::do_NullCheck (NullCheck* x) { 520 if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) { 521 set_canonical(x->obj()); 522 } else { 523 Constant* con = x->obj()->as_Constant(); 524 if (con) { 525 ObjectType* c = con->type()->as_ObjectType(); 526 if (c && c->is_loaded()) { 527 ObjectConstant* oc = c->as_ObjectConstant(); 528 if (!oc || !oc->value()->is_null_object()) { 529 set_canonical(con); 530 } 531 } 532 } 533 } 534} 535 536void Canonicalizer::do_Invoke (Invoke* x) {} 537void Canonicalizer::do_NewInstance (NewInstance* x) {} 538void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {} 539void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {} 540void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {} 541void Canonicalizer::do_CheckCast (CheckCast* x) { 542 if (x->klass()->is_loaded()) { 543 Value obj = x->obj(); 544 ciType* klass = obj->exact_type(); 545 if (klass == NULL) klass = obj->declared_type(); 546 if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) { 547 set_canonical(obj); 548 return; 549 } 550 // checkcast of null returns null 551 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { 552 set_canonical(obj); 553 } 554 } 555} 556void Canonicalizer::do_InstanceOf (InstanceOf* x) { 557 if (x->klass()->is_loaded()) { 558 Value obj = x->obj(); 559 ciType* exact = obj->exact_type(); 560 if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) { 561 set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0); 562 return; 563 } 564 // instanceof null returns false 565 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { 566 set_constant(0); 567 } 568 } 569 570} 571void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {} 572void Canonicalizer::do_MonitorExit (MonitorExit* x) {} 573void Canonicalizer::do_BlockBegin (BlockBegin* x) {} 574void Canonicalizer::do_Goto (Goto* x) {} 575 576 577static bool is_true(jlong x, If::Condition cond, jlong y) { 578 switch (cond) { 579 case If::eql: return x == y; 580 case If::neq: return x != y; 581 case If::lss: return x < y; 582 case If::leq: return x <= y; 583 case If::gtr: return x > y; 584 case If::geq: return x >= y; 585 } 586 ShouldNotReachHere(); 587 return false; 588} 589 590 591void Canonicalizer::do_If(If* x) { 592 // move const to right 593 if (x->x()->type()->is_constant()) x->swap_operands(); 594 // simplify 595 const Value l = x->x(); ValueType* lt = l->type(); 596 const Value r = x->y(); ValueType* rt = r->type(); 597 598 if (l == r && !lt->is_float_kind()) { 599 // pattern: If (a cond a) => simplify to Goto 600 BlockBegin* sux; 601 switch (x->cond()) { 602 case If::eql: sux = x->sux_for(true); break; 603 case If::neq: sux = x->sux_for(false); break; 604 case If::lss: sux = x->sux_for(false); break; 605 case If::leq: sux = x->sux_for(true); break; 606 case If::gtr: sux = x->sux_for(false); break; 607 case If::geq: sux = x->sux_for(true); break; 608 } 609 // If is a safepoint then the debug information should come from the state_before of the If. 610 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); 611 return; 612 } 613 614 if (lt->is_constant() && rt->is_constant()) { 615 if (x->x()->as_Constant() != NULL) { 616 // pattern: If (lc cond rc) => simplify to: Goto 617 BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(), 618 x->sux_for(true), 619 x->sux_for(false)); 620 if (sux != NULL) { 621 // If is a safepoint then the debug information should come from the state_before of the If. 622 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); 623 } 624 } 625 } else if (rt->as_IntConstant() != NULL) { 626 // pattern: If (l cond rc) => investigate further 627 const jint rc = rt->as_IntConstant()->value(); 628 if (l->as_CompareOp() != NULL) { 629 // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto 630 CompareOp* cmp = l->as_CompareOp(); 631 bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl; 632 BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b 633 BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b 634 BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b 635 BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered 636 // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are 637 // equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either 638 // lss_sux or gtr_sux. 639 if (lss_sux == eql_sux && eql_sux == gtr_sux) { 640 // all successors identical => simplify to: Goto 641 set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint())); 642 } else { 643 // two successors differ and two successors are the same => simplify to: If (x cmp y) 644 // determine new condition & successors 645 If::Condition cond; 646 BlockBegin* tsux = NULL; 647 BlockBegin* fsux = NULL; 648 if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; } 649 else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; } 650 else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; } 651 else { ShouldNotReachHere(); } 652 If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint()); 653 if (cmp->x() == cmp->y()) { 654 do_If(canon); 655 } else { 656 set_canonical(canon); 657 set_bci(cmp->bci()); 658 } 659 } 660 } else if (l->as_InstanceOf() != NULL) { 661 // NOTE: Code permanently disabled for now since it leaves the old InstanceOf 662 // instruction in the graph (it is pinned). Need to fix this at some point. 663 return; 664 // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto 665 InstanceOf* inst = l->as_InstanceOf(); 666 BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1 667 BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0 668 if (is_inst_sux == no_inst_sux && inst->is_loaded()) { 669 // both successors identical and klass is loaded => simplify to: Goto 670 set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint())); 671 } else { 672 // successors differ => simplify to: IfInstanceOf 673 set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->bci(), is_inst_sux, no_inst_sux)); 674 } 675 } 676 } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) { 677 if (x->cond() == Instruction::eql) { 678 set_canonical(new Goto(x->fsux(), x->state_before(), x->is_safepoint())); 679 } else { 680 assert(x->cond() == Instruction::neq, "only other valid case"); 681 set_canonical(new Goto(x->tsux(), x->state_before(), x->is_safepoint())); 682 } 683 } 684} 685 686 687void Canonicalizer::do_TableSwitch(TableSwitch* x) { 688 if (x->tag()->type()->is_constant()) { 689 int v = x->tag()->type()->as_IntConstant()->value(); 690 BlockBegin* sux = x->default_sux(); 691 if (v >= x->lo_key() && v <= x->hi_key()) { 692 sux = x->sux_at(v - x->lo_key()); 693 } 694 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); 695 } else if (x->number_of_sux() == 1) { 696 // NOTE: Code permanently disabled for now since the switch statement's 697 // tag expression may produce side-effects in which case it must 698 // be executed. 699 return; 700 // simplify to Goto 701 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); 702 } else if (x->number_of_sux() == 2) { 703 // NOTE: Code permanently disabled for now since it produces two new nodes 704 // (Constant & If) and the Canonicalizer cannot return them correctly 705 // yet. For now we copied the corresponding code directly into the 706 // GraphBuilder (i.e., we should never reach here). 707 return; 708 // simplify to If 709 assert(x->lo_key() == x->hi_key(), "keys must be the same"); 710 Constant* key = new Constant(new IntConstant(x->lo_key())); 711 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint())); 712 } 713} 714 715 716void Canonicalizer::do_LookupSwitch(LookupSwitch* x) { 717 if (x->tag()->type()->is_constant()) { 718 int v = x->tag()->type()->as_IntConstant()->value(); 719 BlockBegin* sux = x->default_sux(); 720 for (int i = 0; i < x->length(); i++) { 721 if (v == x->key_at(i)) { 722 sux = x->sux_at(i); 723 } 724 } 725 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); 726 } else if (x->number_of_sux() == 1) { 727 // NOTE: Code permanently disabled for now since the switch statement's 728 // tag expression may produce side-effects in which case it must 729 // be executed. 730 return; 731 // simplify to Goto 732 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); 733 } else if (x->number_of_sux() == 2) { 734 // NOTE: Code permanently disabled for now since it produces two new nodes 735 // (Constant & If) and the Canonicalizer cannot return them correctly 736 // yet. For now we copied the corresponding code directly into the 737 // GraphBuilder (i.e., we should never reach here). 738 return; 739 // simplify to If 740 assert(x->length() == 1, "length must be the same"); 741 Constant* key = new Constant(new IntConstant(x->key_at(0))); 742 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint())); 743 } 744} 745 746 747void Canonicalizer::do_Return (Return* x) {} 748void Canonicalizer::do_Throw (Throw* x) {} 749void Canonicalizer::do_Base (Base* x) {} 750void Canonicalizer::do_OsrEntry (OsrEntry* x) {} 751void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {} 752 753static bool match_index_and_scale(Instruction* instr, 754 Instruction** index, 755 int* log2_scale, 756 Instruction** instr_to_unpin) { 757 *instr_to_unpin = NULL; 758 759 // Skip conversion ops 760 Convert* convert = instr->as_Convert(); 761 if (convert != NULL) { 762 instr = convert->value(); 763 } 764 765 ShiftOp* shift = instr->as_ShiftOp(); 766 if (shift != NULL) { 767 if (shift->is_pinned()) { 768 *instr_to_unpin = shift; 769 } 770 // Constant shift value? 771 Constant* con = shift->y()->as_Constant(); 772 if (con == NULL) return false; 773 // Well-known type and value? 774 IntConstant* val = con->type()->as_IntConstant(); 775 if (val == NULL) return false; 776 if (shift->x()->type() != intType) return false; 777 *index = shift->x(); 778 int tmp_scale = val->value(); 779 if (tmp_scale >= 0 && tmp_scale < 4) { 780 *log2_scale = tmp_scale; 781 return true; 782 } else { 783 return false; 784 } 785 } 786 787 ArithmeticOp* arith = instr->as_ArithmeticOp(); 788 if (arith != NULL) { 789 if (arith->is_pinned()) { 790 *instr_to_unpin = arith; 791 } 792 // Check for integer multiply 793 if (arith->op() == Bytecodes::_imul) { 794 // See if either arg is a known constant 795 Constant* con = arith->x()->as_Constant(); 796 if (con != NULL) { 797 *index = arith->y(); 798 } else { 799 con = arith->y()->as_Constant(); 800 if (con == NULL) return false; 801 *index = arith->x(); 802 } 803 if ((*index)->type() != intType) return false; 804 // Well-known type and value? 805 IntConstant* val = con->type()->as_IntConstant(); 806 if (val == NULL) return false; 807 switch (val->value()) { 808 case 1: *log2_scale = 0; return true; 809 case 2: *log2_scale = 1; return true; 810 case 4: *log2_scale = 2; return true; 811 case 8: *log2_scale = 3; return true; 812 default: return false; 813 } 814 } 815 } 816 817 // Unknown instruction sequence; don't touch it 818 return false; 819} 820 821 822static bool match(UnsafeRawOp* x, 823 Instruction** base, 824 Instruction** index, 825 int* log2_scale) { 826 Instruction* instr_to_unpin = NULL; 827 ArithmeticOp* root = x->base()->as_ArithmeticOp(); 828 if (root == NULL) return false; 829 // Limit ourselves to addition for now 830 if (root->op() != Bytecodes::_ladd) return false; 831 // Try to find shift or scale op 832 if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) { 833 *base = root->x(); 834 } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) { 835 *base = root->y(); 836 } else if (root->y()->as_Convert() != NULL) { 837 Convert* convert = root->y()->as_Convert(); 838 if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) { 839 // pick base and index, setting scale at 1 840 *base = root->x(); 841 *index = convert->value(); 842 *log2_scale = 0; 843 } else { 844 return false; 845 } 846 } else { 847 // doesn't match any expected sequences 848 return false; 849 } 850 851 // If the value is pinned then it will be always be computed so 852 // there's no profit to reshaping the expression. 853 return !root->is_pinned(); 854} 855 856 857void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) { 858 Instruction* base = NULL; 859 Instruction* index = NULL; 860 int log2_scale; 861 862 if (match(x, &base, &index, &log2_scale)) { 863 x->set_base(base); 864 x->set_index(index); 865 x->set_log2_scale(log2_scale); 866 if (PrintUnsafeOptimization) { 867 tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d", 868 x->id(), x->base()->id(), x->index()->id(), x->log2_scale()); 869 } 870 } 871} 872 873void Canonicalizer::do_RoundFP(RoundFP* x) {} 874void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } 875void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } 876void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {} 877void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {} 878void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {} 879void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {} 880void Canonicalizer::do_ProfileCall(ProfileCall* x) {} 881void Canonicalizer::do_ProfileCounter(ProfileCounter* x) {} 882