src/share/vm/c1/c1_Canonicalizer.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial