src/share/vm/c1/c1_Canonicalizer.cpp

Tue, 05 Apr 2016 08:55:39 -0700

author
asaha
date
Tue, 05 Apr 2016 08:55:39 -0700
changeset 8415
d109bda16490
parent 8316
626f594dffa6
parent 8368
32b682649973
child 8604
04d83ba48607
permissions
-rw-r--r--

Merge

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

mercurial