src/share/vm/c1/c1_Canonicalizer.cpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 1730
3cf667df43ef
child 1939
b812ff5abc73
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

duke@435 1 /*
trims@1907 2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_c1_Canonicalizer.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 static void do_print_value(Value* vp) {
duke@435 30 (*vp)->print_line();
duke@435 31 }
duke@435 32
duke@435 33 void Canonicalizer::set_canonical(Value x) {
duke@435 34 assert(x != NULL, "value must exist");
duke@435 35 // Note: we can not currently substitute root nodes which show up in
duke@435 36 // the instruction stream (because the instruction list is embedded
duke@435 37 // in the instructions).
duke@435 38 if (canonical() != x) {
duke@435 39 if (PrintCanonicalization) {
duke@435 40 canonical()->input_values_do(do_print_value);
duke@435 41 canonical()->print_line();
duke@435 42 tty->print_cr("canonicalized to:");
duke@435 43 x->input_values_do(do_print_value);
duke@435 44 x->print_line();
duke@435 45 tty->cr();
duke@435 46 }
duke@435 47 assert(_canonical->type()->tag() == x->type()->tag(), "types must match");
duke@435 48 _canonical = x;
duke@435 49 }
duke@435 50 }
duke@435 51
duke@435 52
duke@435 53 void Canonicalizer::move_const_to_right(Op2* x) {
duke@435 54 if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands();
duke@435 55 }
duke@435 56
duke@435 57
duke@435 58 void Canonicalizer::do_Op2(Op2* x) {
duke@435 59 if (x->x() == x->y()) {
duke@435 60 switch (x->op()) {
duke@435 61 case Bytecodes::_isub: set_constant(0); return;
duke@435 62 case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;
duke@435 63 case Bytecodes::_iand: // fall through
duke@435 64 case Bytecodes::_land: // fall through
duke@435 65 case Bytecodes::_ior: // fall through
duke@435 66 case Bytecodes::_lor : set_canonical(x->x()); return;
duke@435 67 case Bytecodes::_ixor: set_constant(0); return;
duke@435 68 case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;
duke@435 69 }
duke@435 70 }
duke@435 71
duke@435 72 if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
duke@435 73 // do constant folding for selected operations
duke@435 74 switch (x->type()->tag()) {
duke@435 75 case intTag:
duke@435 76 { jint a = x->x()->type()->as_IntConstant()->value();
duke@435 77 jint b = x->y()->type()->as_IntConstant()->value();
duke@435 78 switch (x->op()) {
duke@435 79 case Bytecodes::_iadd: set_constant(a + b); return;
duke@435 80 case Bytecodes::_isub: set_constant(a - b); return;
duke@435 81 case Bytecodes::_imul: set_constant(a * b); return;
duke@435 82 case Bytecodes::_idiv:
duke@435 83 if (b != 0) {
duke@435 84 if (a == min_jint && b == -1) {
duke@435 85 set_constant(min_jint);
duke@435 86 } else {
duke@435 87 set_constant(a / b);
duke@435 88 }
duke@435 89 return;
duke@435 90 }
duke@435 91 break;
duke@435 92 case Bytecodes::_irem:
duke@435 93 if (b != 0) {
duke@435 94 if (a == min_jint && b == -1) {
duke@435 95 set_constant(0);
duke@435 96 } else {
duke@435 97 set_constant(a % b);
duke@435 98 }
duke@435 99 return;
duke@435 100 }
duke@435 101 break;
duke@435 102 case Bytecodes::_iand: set_constant(a & b); return;
duke@435 103 case Bytecodes::_ior : set_constant(a | b); return;
duke@435 104 case Bytecodes::_ixor: set_constant(a ^ b); return;
duke@435 105 }
duke@435 106 }
duke@435 107 break;
duke@435 108 case longTag:
duke@435 109 { jlong a = x->x()->type()->as_LongConstant()->value();
duke@435 110 jlong b = x->y()->type()->as_LongConstant()->value();
duke@435 111 switch (x->op()) {
duke@435 112 case Bytecodes::_ladd: set_constant(a + b); return;
duke@435 113 case Bytecodes::_lsub: set_constant(a - b); return;
duke@435 114 case Bytecodes::_lmul: set_constant(a * b); return;
duke@435 115 case Bytecodes::_ldiv:
duke@435 116 if (b != 0) {
duke@435 117 set_constant(SharedRuntime::ldiv(b, a));
duke@435 118 return;
duke@435 119 }
duke@435 120 break;
duke@435 121 case Bytecodes::_lrem:
duke@435 122 if (b != 0) {
duke@435 123 set_constant(SharedRuntime::lrem(b, a));
duke@435 124 return;
duke@435 125 }
duke@435 126 break;
duke@435 127 case Bytecodes::_land: set_constant(a & b); return;
duke@435 128 case Bytecodes::_lor : set_constant(a | b); return;
duke@435 129 case Bytecodes::_lxor: set_constant(a ^ b); return;
duke@435 130 }
duke@435 131 }
duke@435 132 break;
duke@435 133 // other cases not implemented (must be extremely careful with floats & doubles!)
duke@435 134 }
duke@435 135 }
duke@435 136 // make sure constant is on the right side, if any
duke@435 137 move_const_to_right(x);
duke@435 138
duke@435 139 if (x->y()->type()->is_constant()) {
duke@435 140 // do constant folding for selected operations
duke@435 141 switch (x->type()->tag()) {
duke@435 142 case intTag:
duke@435 143 if (x->y()->type()->as_IntConstant()->value() == 0) {
duke@435 144 switch (x->op()) {
duke@435 145 case Bytecodes::_iadd: set_canonical(x->x()); return;
duke@435 146 case Bytecodes::_isub: set_canonical(x->x()); return;
duke@435 147 case Bytecodes::_imul: set_constant(0); return;
duke@435 148 // Note: for div and rem, make sure that C semantics
duke@435 149 // corresponds to Java semantics!
duke@435 150 case Bytecodes::_iand: set_constant(0); return;
duke@435 151 case Bytecodes::_ior : set_canonical(x->x()); return;
duke@435 152 }
duke@435 153 }
duke@435 154 break;
duke@435 155 case longTag:
duke@435 156 if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {
duke@435 157 switch (x->op()) {
duke@435 158 case Bytecodes::_ladd: set_canonical(x->x()); return;
duke@435 159 case Bytecodes::_lsub: set_canonical(x->x()); return;
duke@435 160 case Bytecodes::_lmul: set_constant((jlong)0); return;
duke@435 161 // Note: for div and rem, make sure that C semantics
duke@435 162 // corresponds to Java semantics!
duke@435 163 case Bytecodes::_land: set_constant((jlong)0); return;
duke@435 164 case Bytecodes::_lor : set_canonical(x->x()); return;
duke@435 165 }
duke@435 166 }
duke@435 167 break;
duke@435 168 }
duke@435 169 }
duke@435 170 }
duke@435 171
duke@435 172
duke@435 173 void Canonicalizer::do_Phi (Phi* x) {}
duke@435 174 void Canonicalizer::do_Constant (Constant* x) {}
duke@435 175 void Canonicalizer::do_Local (Local* x) {}
duke@435 176 void Canonicalizer::do_LoadField (LoadField* x) {}
duke@435 177
duke@435 178 // checks if v is in the block that is currently processed by
duke@435 179 // GraphBuilder. This is the only block that has not BlockEnd yet.
duke@435 180 static bool in_current_block(Value v) {
duke@435 181 int max_distance = 4;
duke@435 182 while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) {
duke@435 183 v = v->next();
duke@435 184 max_distance--;
duke@435 185 }
duke@435 186 return v == NULL;
duke@435 187 }
duke@435 188
duke@435 189 void Canonicalizer::do_StoreField (StoreField* x) {
duke@435 190 // If a value is going to be stored into a field or array some of
duke@435 191 // the conversions emitted by javac are unneeded because the fields
duke@435 192 // are packed to their natural size.
duke@435 193 Convert* conv = x->value()->as_Convert();
duke@435 194 if (conv) {
duke@435 195 Value value = NULL;
duke@435 196 BasicType type = x->field()->type()->basic_type();
duke@435 197 switch (conv->op()) {
duke@435 198 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;
duke@435 199 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
duke@435 200 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;
duke@435 201 }
duke@435 202 // limit this optimization to current block
duke@435 203 if (value != NULL && in_current_block(conv)) {
duke@435 204 set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),
duke@435 205 x->lock_stack(), x->state_before(), x->is_loaded(), x->is_initialized()));
duke@435 206 return;
duke@435 207 }
duke@435 208 }
duke@435 209
duke@435 210 }
duke@435 211
duke@435 212 void Canonicalizer::do_ArrayLength (ArrayLength* x) {
duke@435 213 NewArray* array = x->array()->as_NewArray();
duke@435 214 if (array != NULL && array->length() != NULL) {
duke@435 215 Constant* length = array->length()->as_Constant();
duke@435 216 if (length != NULL) {
duke@435 217 // do not use the Constant itself, but create a new Constant
duke@435 218 // with same value Otherwise a Constant is live over multiple
duke@435 219 // blocks without being registered in a state array.
duke@435 220 assert(length->type()->as_IntConstant() != NULL, "array length must be integer");
duke@435 221 set_constant(length->type()->as_IntConstant()->value());
duke@435 222 }
duke@435 223 } else {
duke@435 224 LoadField* lf = x->array()->as_LoadField();
twisti@1730 225 if (lf != NULL) {
twisti@1730 226 ciField* field = lf->field();
twisti@1730 227 if (field->is_constant() && field->is_static()) {
twisti@1730 228 // final static field
twisti@1730 229 ciObject* c = field->constant_value().as_object();
twisti@1730 230 if (c->is_array()) {
twisti@1730 231 ciArray* array = (ciArray*) c;
twisti@1730 232 set_constant(array->length());
twisti@1730 233 }
duke@435 234 }
duke@435 235 }
duke@435 236 }
duke@435 237 }
duke@435 238
duke@435 239 void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {}
duke@435 240 void Canonicalizer::do_StoreIndexed (StoreIndexed* x) {
duke@435 241 // If a value is going to be stored into a field or array some of
duke@435 242 // the conversions emitted by javac are unneeded because the fields
duke@435 243 // are packed to their natural size.
duke@435 244 Convert* conv = x->value()->as_Convert();
duke@435 245 if (conv) {
duke@435 246 Value value = NULL;
duke@435 247 BasicType type = x->elt_type();
duke@435 248 switch (conv->op()) {
duke@435 249 case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;
duke@435 250 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;
duke@435 251 case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;
duke@435 252 }
duke@435 253 // limit this optimization to current block
duke@435 254 if (value != NULL && in_current_block(conv)) {
duke@435 255 set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),
duke@435 256 x->elt_type(), value, x->lock_stack()));
duke@435 257 return;
duke@435 258 }
duke@435 259 }
duke@435 260
duke@435 261
duke@435 262 }
duke@435 263
duke@435 264
duke@435 265 void Canonicalizer::do_NegateOp(NegateOp* x) {
duke@435 266 ValueType* t = x->x()->type();
duke@435 267 if (t->is_constant()) {
duke@435 268 switch (t->tag()) {
duke@435 269 case intTag : set_constant(-t->as_IntConstant ()->value()); return;
duke@435 270 case longTag : set_constant(-t->as_LongConstant ()->value()); return;
duke@435 271 case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;
duke@435 272 case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;
duke@435 273 default : ShouldNotReachHere();
duke@435 274 }
duke@435 275 }
duke@435 276 }
duke@435 277
duke@435 278
duke@435 279 void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); }
duke@435 280
duke@435 281
duke@435 282 void Canonicalizer::do_ShiftOp (ShiftOp* x) {
duke@435 283 ValueType* t = x->x()->type();
duke@435 284 ValueType* t2 = x->y()->type();
duke@435 285 if (t->is_constant()) {
duke@435 286 switch (t->tag()) {
duke@435 287 case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break;
duke@435 288 case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;
duke@435 289 default : ShouldNotReachHere();
duke@435 290 }
duke@435 291 if (t2->is_constant()) {
duke@435 292 if (t->tag() == intTag) {
duke@435 293 int value = t->as_IntConstant()->value();
duke@435 294 int shift = t2->as_IntConstant()->value() & 31;
duke@435 295 jint mask = ~(~0 << (32 - shift));
duke@435 296 if (shift == 0) mask = ~0;
duke@435 297 switch (x->op()) {
duke@435 298 case Bytecodes::_ishl: set_constant(value << shift); return;
duke@435 299 case Bytecodes::_ishr: set_constant(value >> shift); return;
duke@435 300 case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
duke@435 301 }
duke@435 302 } else if (t->tag() == longTag) {
duke@435 303 jlong value = t->as_LongConstant()->value();
duke@435 304 int shift = t2->as_IntConstant()->value() & 63;
duke@435 305 jlong mask = ~(~jlong_cast(0) << (64 - shift));
duke@435 306 if (shift == 0) mask = ~jlong_cast(0);
duke@435 307 switch (x->op()) {
duke@435 308 case Bytecodes::_lshl: set_constant(value << shift); return;
duke@435 309 case Bytecodes::_lshr: set_constant(value >> shift); return;
duke@435 310 case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
duke@435 311 }
duke@435 312 }
duke@435 313 }
duke@435 314 }
duke@435 315 if (t2->is_constant()) {
duke@435 316 switch (t2->tag()) {
duke@435 317 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
duke@435 318 case longTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
duke@435 319 default : ShouldNotReachHere();
duke@435 320 }
duke@435 321 }
duke@435 322 }
duke@435 323
duke@435 324
duke@435 325 void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }
duke@435 326 void Canonicalizer::do_CompareOp (CompareOp* x) {
duke@435 327 if (x->x() == x->y()) {
duke@435 328 switch (x->x()->type()->tag()) {
duke@435 329 case longTag: set_constant(0); break;
duke@435 330 case floatTag: {
duke@435 331 FloatConstant* fc = x->x()->type()->as_FloatConstant();
duke@435 332 if (fc) {
duke@435 333 if (g_isnan(fc->value())) {
duke@435 334 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
duke@435 335 } else {
duke@435 336 set_constant(0);
duke@435 337 }
duke@435 338 }
duke@435 339 break;
duke@435 340 }
duke@435 341 case doubleTag: {
duke@435 342 DoubleConstant* dc = x->x()->type()->as_DoubleConstant();
duke@435 343 if (dc) {
duke@435 344 if (g_isnan(dc->value())) {
duke@435 345 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
duke@435 346 } else {
duke@435 347 set_constant(0);
duke@435 348 }
duke@435 349 }
duke@435 350 break;
duke@435 351 }
duke@435 352 }
duke@435 353 } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {
duke@435 354 switch (x->x()->type()->tag()) {
duke@435 355 case longTag: {
duke@435 356 jlong vx = x->x()->type()->as_LongConstant()->value();
duke@435 357 jlong vy = x->y()->type()->as_LongConstant()->value();
duke@435 358 if (vx == vy)
duke@435 359 set_constant(0);
duke@435 360 else if (vx < vy)
duke@435 361 set_constant(-1);
duke@435 362 else
duke@435 363 set_constant(1);
duke@435 364 break;
duke@435 365 }
duke@435 366
duke@435 367 case floatTag: {
duke@435 368 float vx = x->x()->type()->as_FloatConstant()->value();
duke@435 369 float vy = x->y()->type()->as_FloatConstant()->value();
duke@435 370 if (g_isnan(vx) || g_isnan(vy))
duke@435 371 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
duke@435 372 else if (vx == vy)
duke@435 373 set_constant(0);
duke@435 374 else if (vx < vy)
duke@435 375 set_constant(-1);
duke@435 376 else
duke@435 377 set_constant(1);
duke@435 378 break;
duke@435 379 }
duke@435 380
duke@435 381 case doubleTag: {
duke@435 382 double vx = x->x()->type()->as_DoubleConstant()->value();
duke@435 383 double vy = x->y()->type()->as_DoubleConstant()->value();
duke@435 384 if (g_isnan(vx) || g_isnan(vy))
duke@435 385 set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);
duke@435 386 else if (vx == vy)
duke@435 387 set_constant(0);
duke@435 388 else if (vx < vy)
duke@435 389 set_constant(-1);
duke@435 390 else
duke@435 391 set_constant(1);
duke@435 392 break;
duke@435 393 }
duke@435 394 }
duke@435 395
duke@435 396 }
duke@435 397 }
duke@435 398
duke@435 399
duke@435 400 void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {}
duke@435 401
duke@435 402 void Canonicalizer::do_IfOp(IfOp* x) {
duke@435 403 // Caution: do not use do_Op2(x) here for now since
duke@435 404 // we map the condition to the op for now!
duke@435 405 move_const_to_right(x);
duke@435 406 }
duke@435 407
duke@435 408
duke@435 409 void Canonicalizer::do_Intrinsic (Intrinsic* x) {
duke@435 410 switch (x->id()) {
duke@435 411 case vmIntrinsics::_floatToRawIntBits : {
duke@435 412 FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();
duke@435 413 if (c != NULL) {
duke@435 414 JavaValue v;
duke@435 415 v.set_jfloat(c->value());
duke@435 416 set_constant(v.get_jint());
duke@435 417 }
duke@435 418 break;
duke@435 419 }
duke@435 420 case vmIntrinsics::_intBitsToFloat : {
duke@435 421 IntConstant* c = x->argument_at(0)->type()->as_IntConstant();
duke@435 422 if (c != NULL) {
duke@435 423 JavaValue v;
duke@435 424 v.set_jint(c->value());
duke@435 425 set_constant(v.get_jfloat());
duke@435 426 }
duke@435 427 break;
duke@435 428 }
duke@435 429 case vmIntrinsics::_doubleToRawLongBits : {
duke@435 430 DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();
duke@435 431 if (c != NULL) {
duke@435 432 JavaValue v;
duke@435 433 v.set_jdouble(c->value());
duke@435 434 set_constant(v.get_jlong());
duke@435 435 }
duke@435 436 break;
duke@435 437 }
duke@435 438 case vmIntrinsics::_longBitsToDouble : {
duke@435 439 LongConstant* c = x->argument_at(0)->type()->as_LongConstant();
duke@435 440 if (c != NULL) {
duke@435 441 JavaValue v;
duke@435 442 v.set_jlong(c->value());
duke@435 443 set_constant(v.get_jdouble());
duke@435 444 }
duke@435 445 break;
duke@435 446 }
duke@435 447 }
duke@435 448 }
duke@435 449
duke@435 450 void Canonicalizer::do_Convert (Convert* x) {
duke@435 451 if (x->value()->type()->is_constant()) {
duke@435 452 switch (x->op()) {
duke@435 453 case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;
duke@435 454 case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;
duke@435 455 case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;
duke@435 456 case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break;
duke@435 457 case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break;
duke@435 458 case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break;
duke@435 459 case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break;
duke@435 460 case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;
duke@435 461 case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;
duke@435 462 case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break;
duke@435 463 case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;
duke@435 464 case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;
duke@435 465 case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break;
duke@435 466 case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;
duke@435 467 case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;
duke@435 468 default:
duke@435 469 ShouldNotReachHere();
duke@435 470 }
duke@435 471 }
duke@435 472
duke@435 473 Value value = x->value();
duke@435 474 BasicType type = T_ILLEGAL;
duke@435 475 LoadField* lf = value->as_LoadField();
duke@435 476 if (lf) {
duke@435 477 type = lf->field_type();
duke@435 478 } else {
duke@435 479 LoadIndexed* li = value->as_LoadIndexed();
duke@435 480 if (li) {
duke@435 481 type = li->elt_type();
duke@435 482 } else {
duke@435 483 Convert* conv = value->as_Convert();
duke@435 484 if (conv) {
duke@435 485 switch (conv->op()) {
duke@435 486 case Bytecodes::_i2b: type = T_BYTE; break;
duke@435 487 case Bytecodes::_i2s: type = T_SHORT; break;
duke@435 488 case Bytecodes::_i2c: type = T_CHAR; break;
duke@435 489 }
duke@435 490 }
duke@435 491 }
duke@435 492 }
duke@435 493 if (type != T_ILLEGAL) {
duke@435 494 switch (x->op()) {
duke@435 495 case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break;
duke@435 496 case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;
duke@435 497 case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break;
duke@435 498 }
duke@435 499 } else {
duke@435 500 Op2* op2 = x->value()->as_Op2();
duke@435 501 if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {
duke@435 502 jint safebits = 0;
duke@435 503 jint mask = op2->y()->type()->as_IntConstant()->value();
duke@435 504 switch (x->op()) {
duke@435 505 case Bytecodes::_i2b: safebits = 0x7f; break;
duke@435 506 case Bytecodes::_i2s: safebits = 0x7fff; break;
duke@435 507 case Bytecodes::_i2c: safebits = 0xffff; break;
duke@435 508 }
duke@435 509 // When casting a masked integer to a smaller signed type, if
duke@435 510 // the mask doesn't include the sign bit the cast isn't needed.
duke@435 511 if (safebits && (mask & ~safebits) == 0) {
duke@435 512 set_canonical(x->value());
duke@435 513 }
duke@435 514 }
duke@435 515 }
duke@435 516
duke@435 517 }
duke@435 518
duke@435 519 void Canonicalizer::do_NullCheck (NullCheck* x) {
duke@435 520 if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {
duke@435 521 set_canonical(x->obj());
duke@435 522 } else {
duke@435 523 Constant* con = x->obj()->as_Constant();
duke@435 524 if (con) {
duke@435 525 ObjectType* c = con->type()->as_ObjectType();
duke@435 526 if (c && c->is_loaded()) {
duke@435 527 ObjectConstant* oc = c->as_ObjectConstant();
duke@435 528 if (!oc || !oc->value()->is_null_object()) {
duke@435 529 set_canonical(con);
duke@435 530 }
duke@435 531 }
duke@435 532 }
duke@435 533 }
duke@435 534 }
duke@435 535
duke@435 536 void Canonicalizer::do_Invoke (Invoke* x) {}
duke@435 537 void Canonicalizer::do_NewInstance (NewInstance* x) {}
duke@435 538 void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {}
duke@435 539 void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {}
duke@435 540 void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {}
duke@435 541 void Canonicalizer::do_CheckCast (CheckCast* x) {
duke@435 542 if (x->klass()->is_loaded()) {
duke@435 543 Value obj = x->obj();
duke@435 544 ciType* klass = obj->exact_type();
duke@435 545 if (klass == NULL) klass = obj->declared_type();
duke@435 546 if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) {
duke@435 547 set_canonical(obj);
duke@435 548 return;
duke@435 549 }
duke@435 550 // checkcast of null returns null
duke@435 551 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
duke@435 552 set_canonical(obj);
duke@435 553 }
duke@435 554 }
duke@435 555 }
duke@435 556 void Canonicalizer::do_InstanceOf (InstanceOf* x) {
duke@435 557 if (x->klass()->is_loaded()) {
duke@435 558 Value obj = x->obj();
duke@435 559 ciType* exact = obj->exact_type();
duke@435 560 if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {
duke@435 561 set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);
duke@435 562 return;
duke@435 563 }
duke@435 564 // instanceof null returns false
duke@435 565 if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {
duke@435 566 set_constant(0);
duke@435 567 }
duke@435 568 }
duke@435 569
duke@435 570 }
duke@435 571 void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {}
duke@435 572 void Canonicalizer::do_MonitorExit (MonitorExit* x) {}
duke@435 573 void Canonicalizer::do_BlockBegin (BlockBegin* x) {}
duke@435 574 void Canonicalizer::do_Goto (Goto* x) {}
duke@435 575
duke@435 576
duke@435 577 static bool is_true(jlong x, If::Condition cond, jlong y) {
duke@435 578 switch (cond) {
duke@435 579 case If::eql: return x == y;
duke@435 580 case If::neq: return x != y;
duke@435 581 case If::lss: return x < y;
duke@435 582 case If::leq: return x <= y;
duke@435 583 case If::gtr: return x > y;
duke@435 584 case If::geq: return x >= y;
duke@435 585 }
duke@435 586 ShouldNotReachHere();
duke@435 587 return false;
duke@435 588 }
duke@435 589
duke@435 590
duke@435 591 void Canonicalizer::do_If(If* x) {
duke@435 592 // move const to right
duke@435 593 if (x->x()->type()->is_constant()) x->swap_operands();
duke@435 594 // simplify
duke@435 595 const Value l = x->x(); ValueType* lt = l->type();
duke@435 596 const Value r = x->y(); ValueType* rt = r->type();
duke@435 597
duke@435 598 if (l == r && !lt->is_float_kind()) {
duke@435 599 // pattern: If (a cond a) => simplify to Goto
duke@435 600 BlockBegin* sux;
duke@435 601 switch (x->cond()) {
duke@435 602 case If::eql: sux = x->sux_for(true); break;
duke@435 603 case If::neq: sux = x->sux_for(false); break;
duke@435 604 case If::lss: sux = x->sux_for(false); break;
duke@435 605 case If::leq: sux = x->sux_for(true); break;
duke@435 606 case If::gtr: sux = x->sux_for(false); break;
duke@435 607 case If::geq: sux = x->sux_for(true); break;
duke@435 608 }
duke@435 609 // If is a safepoint then the debug information should come from the state_before of the If.
duke@435 610 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
duke@435 611 return;
duke@435 612 }
duke@435 613
duke@435 614 if (lt->is_constant() && rt->is_constant()) {
duke@435 615 if (x->x()->as_Constant() != NULL) {
duke@435 616 // pattern: If (lc cond rc) => simplify to: Goto
duke@435 617 BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),
duke@435 618 x->sux_for(true),
duke@435 619 x->sux_for(false));
duke@435 620 if (sux != NULL) {
duke@435 621 // If is a safepoint then the debug information should come from the state_before of the If.
duke@435 622 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
duke@435 623 }
duke@435 624 }
duke@435 625 } else if (rt->as_IntConstant() != NULL) {
duke@435 626 // pattern: If (l cond rc) => investigate further
duke@435 627 const jint rc = rt->as_IntConstant()->value();
duke@435 628 if (l->as_CompareOp() != NULL) {
duke@435 629 // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto
duke@435 630 CompareOp* cmp = l->as_CompareOp();
duke@435 631 bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;
duke@435 632 BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b
duke@435 633 BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b
duke@435 634 BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b
duke@435 635 BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered
duke@435 636 // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are
duke@435 637 // equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either
duke@435 638 // lss_sux or gtr_sux.
duke@435 639 if (lss_sux == eql_sux && eql_sux == gtr_sux) {
duke@435 640 // all successors identical => simplify to: Goto
duke@435 641 set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));
duke@435 642 } else {
duke@435 643 // two successors differ and two successors are the same => simplify to: If (x cmp y)
duke@435 644 // determine new condition & successors
duke@435 645 If::Condition cond;
duke@435 646 BlockBegin* tsux = NULL;
duke@435 647 BlockBegin* fsux = NULL;
duke@435 648 if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }
duke@435 649 else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }
duke@435 650 else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }
duke@435 651 else { ShouldNotReachHere(); }
duke@435 652 If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());
duke@435 653 if (cmp->x() == cmp->y()) {
duke@435 654 do_If(canon);
duke@435 655 } else {
duke@435 656 set_canonical(canon);
duke@435 657 set_bci(cmp->bci());
duke@435 658 }
duke@435 659 }
duke@435 660 } else if (l->as_InstanceOf() != NULL) {
duke@435 661 // NOTE: Code permanently disabled for now since it leaves the old InstanceOf
duke@435 662 // instruction in the graph (it is pinned). Need to fix this at some point.
duke@435 663 return;
duke@435 664 // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto
duke@435 665 InstanceOf* inst = l->as_InstanceOf();
duke@435 666 BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1
duke@435 667 BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0
duke@435 668 if (is_inst_sux == no_inst_sux && inst->is_loaded()) {
duke@435 669 // both successors identical and klass is loaded => simplify to: Goto
duke@435 670 set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));
duke@435 671 } else {
duke@435 672 // successors differ => simplify to: IfInstanceOf
duke@435 673 set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->bci(), is_inst_sux, no_inst_sux));
duke@435 674 }
duke@435 675 }
duke@435 676 } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) {
duke@435 677 if (x->cond() == Instruction::eql) {
duke@435 678 set_canonical(new Goto(x->fsux(), x->state_before(), x->is_safepoint()));
duke@435 679 } else {
duke@435 680 assert(x->cond() == Instruction::neq, "only other valid case");
duke@435 681 set_canonical(new Goto(x->tsux(), x->state_before(), x->is_safepoint()));
duke@435 682 }
duke@435 683 }
duke@435 684 }
duke@435 685
duke@435 686
duke@435 687 void Canonicalizer::do_TableSwitch(TableSwitch* x) {
duke@435 688 if (x->tag()->type()->is_constant()) {
duke@435 689 int v = x->tag()->type()->as_IntConstant()->value();
duke@435 690 BlockBegin* sux = x->default_sux();
duke@435 691 if (v >= x->lo_key() && v <= x->hi_key()) {
duke@435 692 sux = x->sux_at(v - x->lo_key());
duke@435 693 }
duke@435 694 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
duke@435 695 } else if (x->number_of_sux() == 1) {
duke@435 696 // NOTE: Code permanently disabled for now since the switch statement's
duke@435 697 // tag expression may produce side-effects in which case it must
duke@435 698 // be executed.
duke@435 699 return;
duke@435 700 // simplify to Goto
duke@435 701 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
duke@435 702 } else if (x->number_of_sux() == 2) {
duke@435 703 // NOTE: Code permanently disabled for now since it produces two new nodes
duke@435 704 // (Constant & If) and the Canonicalizer cannot return them correctly
duke@435 705 // yet. For now we copied the corresponding code directly into the
duke@435 706 // GraphBuilder (i.e., we should never reach here).
duke@435 707 return;
duke@435 708 // simplify to If
duke@435 709 assert(x->lo_key() == x->hi_key(), "keys must be the same");
duke@435 710 Constant* key = new Constant(new IntConstant(x->lo_key()));
duke@435 711 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
duke@435 712 }
duke@435 713 }
duke@435 714
duke@435 715
duke@435 716 void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {
duke@435 717 if (x->tag()->type()->is_constant()) {
duke@435 718 int v = x->tag()->type()->as_IntConstant()->value();
duke@435 719 BlockBegin* sux = x->default_sux();
duke@435 720 for (int i = 0; i < x->length(); i++) {
duke@435 721 if (v == x->key_at(i)) {
duke@435 722 sux = x->sux_at(i);
duke@435 723 }
duke@435 724 }
duke@435 725 set_canonical(new Goto(sux, x->state_before(), x->is_safepoint()));
duke@435 726 } else if (x->number_of_sux() == 1) {
duke@435 727 // NOTE: Code permanently disabled for now since the switch statement's
duke@435 728 // tag expression may produce side-effects in which case it must
duke@435 729 // be executed.
duke@435 730 return;
duke@435 731 // simplify to Goto
duke@435 732 set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));
duke@435 733 } else if (x->number_of_sux() == 2) {
duke@435 734 // NOTE: Code permanently disabled for now since it produces two new nodes
duke@435 735 // (Constant & If) and the Canonicalizer cannot return them correctly
duke@435 736 // yet. For now we copied the corresponding code directly into the
duke@435 737 // GraphBuilder (i.e., we should never reach here).
duke@435 738 return;
duke@435 739 // simplify to If
duke@435 740 assert(x->length() == 1, "length must be the same");
duke@435 741 Constant* key = new Constant(new IntConstant(x->key_at(0)));
duke@435 742 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
duke@435 743 }
duke@435 744 }
duke@435 745
duke@435 746
duke@435 747 void Canonicalizer::do_Return (Return* x) {}
duke@435 748 void Canonicalizer::do_Throw (Throw* x) {}
duke@435 749 void Canonicalizer::do_Base (Base* x) {}
duke@435 750 void Canonicalizer::do_OsrEntry (OsrEntry* x) {}
duke@435 751 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
duke@435 752
duke@435 753 static bool match_index_and_scale(Instruction* instr,
duke@435 754 Instruction** index,
duke@435 755 int* log2_scale,
duke@435 756 Instruction** instr_to_unpin) {
duke@435 757 *instr_to_unpin = NULL;
duke@435 758
duke@435 759 // Skip conversion ops
duke@435 760 Convert* convert = instr->as_Convert();
duke@435 761 if (convert != NULL) {
duke@435 762 instr = convert->value();
duke@435 763 }
duke@435 764
duke@435 765 ShiftOp* shift = instr->as_ShiftOp();
duke@435 766 if (shift != NULL) {
duke@435 767 if (shift->is_pinned()) {
duke@435 768 *instr_to_unpin = shift;
duke@435 769 }
duke@435 770 // Constant shift value?
duke@435 771 Constant* con = shift->y()->as_Constant();
duke@435 772 if (con == NULL) return false;
duke@435 773 // Well-known type and value?
duke@435 774 IntConstant* val = con->type()->as_IntConstant();
duke@435 775 if (val == NULL) return false;
duke@435 776 if (shift->x()->type() != intType) return false;
duke@435 777 *index = shift->x();
duke@435 778 int tmp_scale = val->value();
duke@435 779 if (tmp_scale >= 0 && tmp_scale < 4) {
duke@435 780 *log2_scale = tmp_scale;
duke@435 781 return true;
duke@435 782 } else {
duke@435 783 return false;
duke@435 784 }
duke@435 785 }
duke@435 786
duke@435 787 ArithmeticOp* arith = instr->as_ArithmeticOp();
duke@435 788 if (arith != NULL) {
duke@435 789 if (arith->is_pinned()) {
duke@435 790 *instr_to_unpin = arith;
duke@435 791 }
duke@435 792 // Check for integer multiply
duke@435 793 if (arith->op() == Bytecodes::_imul) {
duke@435 794 // See if either arg is a known constant
duke@435 795 Constant* con = arith->x()->as_Constant();
duke@435 796 if (con != NULL) {
duke@435 797 *index = arith->y();
duke@435 798 } else {
duke@435 799 con = arith->y()->as_Constant();
duke@435 800 if (con == NULL) return false;
duke@435 801 *index = arith->x();
duke@435 802 }
duke@435 803 if ((*index)->type() != intType) return false;
duke@435 804 // Well-known type and value?
duke@435 805 IntConstant* val = con->type()->as_IntConstant();
duke@435 806 if (val == NULL) return false;
duke@435 807 switch (val->value()) {
duke@435 808 case 1: *log2_scale = 0; return true;
duke@435 809 case 2: *log2_scale = 1; return true;
duke@435 810 case 4: *log2_scale = 2; return true;
duke@435 811 case 8: *log2_scale = 3; return true;
duke@435 812 default: return false;
duke@435 813 }
duke@435 814 }
duke@435 815 }
duke@435 816
duke@435 817 // Unknown instruction sequence; don't touch it
duke@435 818 return false;
duke@435 819 }
duke@435 820
duke@435 821
duke@435 822 static bool match(UnsafeRawOp* x,
duke@435 823 Instruction** base,
duke@435 824 Instruction** index,
duke@435 825 int* log2_scale) {
duke@435 826 Instruction* instr_to_unpin = NULL;
duke@435 827 ArithmeticOp* root = x->base()->as_ArithmeticOp();
duke@435 828 if (root == NULL) return false;
duke@435 829 // Limit ourselves to addition for now
duke@435 830 if (root->op() != Bytecodes::_ladd) return false;
duke@435 831 // Try to find shift or scale op
duke@435 832 if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) {
duke@435 833 *base = root->x();
duke@435 834 } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) {
duke@435 835 *base = root->y();
duke@435 836 } else if (root->y()->as_Convert() != NULL) {
duke@435 837 Convert* convert = root->y()->as_Convert();
duke@435 838 if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) {
duke@435 839 // pick base and index, setting scale at 1
duke@435 840 *base = root->x();
duke@435 841 *index = convert->value();
duke@435 842 *log2_scale = 0;
duke@435 843 } else {
duke@435 844 return false;
duke@435 845 }
duke@435 846 } else {
duke@435 847 // doesn't match any expected sequences
duke@435 848 return false;
duke@435 849 }
duke@435 850
duke@435 851 // If the value is pinned then it will be always be computed so
duke@435 852 // there's no profit to reshaping the expression.
duke@435 853 return !root->is_pinned();
duke@435 854 }
duke@435 855
duke@435 856
duke@435 857 void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {
duke@435 858 Instruction* base = NULL;
duke@435 859 Instruction* index = NULL;
duke@435 860 int log2_scale;
duke@435 861
duke@435 862 if (match(x, &base, &index, &log2_scale)) {
duke@435 863 x->set_base(base);
duke@435 864 x->set_index(index);
duke@435 865 x->set_log2_scale(log2_scale);
duke@435 866 if (PrintUnsafeOptimization) {
duke@435 867 tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",
duke@435 868 x->id(), x->base()->id(), x->index()->id(), x->log2_scale());
duke@435 869 }
duke@435 870 }
duke@435 871 }
duke@435 872
duke@435 873 void Canonicalizer::do_RoundFP(RoundFP* x) {}
duke@435 874 void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
duke@435 875 void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }
duke@435 876 void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {}
duke@435 877 void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {}
duke@435 878 void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {}
duke@435 879 void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
duke@435 880 void Canonicalizer::do_ProfileCall(ProfileCall* x) {}
duke@435 881 void Canonicalizer::do_ProfileCounter(ProfileCounter* x) {}

mercurial