duke@435: /* trims@1907: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_c1_Canonicalizer.cpp.incl" duke@435: duke@435: iveresov@1939: class PrintValueVisitor: public ValueVisitor { iveresov@1939: void visit(Value* vp) { iveresov@1939: (*vp)->print_line(); iveresov@1939: } iveresov@1939: }; duke@435: duke@435: void Canonicalizer::set_canonical(Value x) { duke@435: assert(x != NULL, "value must exist"); duke@435: // Note: we can not currently substitute root nodes which show up in duke@435: // the instruction stream (because the instruction list is embedded duke@435: // in the instructions). duke@435: if (canonical() != x) { duke@435: if (PrintCanonicalization) { iveresov@1939: PrintValueVisitor do_print_value; iveresov@1939: canonical()->input_values_do(&do_print_value); duke@435: canonical()->print_line(); duke@435: tty->print_cr("canonicalized to:"); iveresov@1939: x->input_values_do(&do_print_value); duke@435: x->print_line(); duke@435: tty->cr(); duke@435: } duke@435: assert(_canonical->type()->tag() == x->type()->tag(), "types must match"); duke@435: _canonical = x; duke@435: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::move_const_to_right(Op2* x) { duke@435: if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands(); duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_Op2(Op2* x) { duke@435: if (x->x() == x->y()) { duke@435: switch (x->op()) { duke@435: case Bytecodes::_isub: set_constant(0); return; duke@435: case Bytecodes::_lsub: set_constant(jlong_cast(0)); return; duke@435: case Bytecodes::_iand: // fall through duke@435: case Bytecodes::_land: // fall through duke@435: case Bytecodes::_ior: // fall through duke@435: case Bytecodes::_lor : set_canonical(x->x()); return; duke@435: case Bytecodes::_ixor: set_constant(0); return; duke@435: case Bytecodes::_lxor: set_constant(jlong_cast(0)); return; duke@435: } duke@435: } duke@435: duke@435: if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { duke@435: // do constant folding for selected operations duke@435: switch (x->type()->tag()) { duke@435: case intTag: duke@435: { jint a = x->x()->type()->as_IntConstant()->value(); duke@435: jint b = x->y()->type()->as_IntConstant()->value(); duke@435: switch (x->op()) { duke@435: case Bytecodes::_iadd: set_constant(a + b); return; duke@435: case Bytecodes::_isub: set_constant(a - b); return; duke@435: case Bytecodes::_imul: set_constant(a * b); return; duke@435: case Bytecodes::_idiv: duke@435: if (b != 0) { duke@435: if (a == min_jint && b == -1) { duke@435: set_constant(min_jint); duke@435: } else { duke@435: set_constant(a / b); duke@435: } duke@435: return; duke@435: } duke@435: break; duke@435: case Bytecodes::_irem: duke@435: if (b != 0) { duke@435: if (a == min_jint && b == -1) { duke@435: set_constant(0); duke@435: } else { duke@435: set_constant(a % b); duke@435: } duke@435: return; duke@435: } duke@435: break; duke@435: case Bytecodes::_iand: set_constant(a & b); return; duke@435: case Bytecodes::_ior : set_constant(a | b); return; duke@435: case Bytecodes::_ixor: set_constant(a ^ b); return; duke@435: } duke@435: } duke@435: break; duke@435: case longTag: duke@435: { jlong a = x->x()->type()->as_LongConstant()->value(); duke@435: jlong b = x->y()->type()->as_LongConstant()->value(); duke@435: switch (x->op()) { duke@435: case Bytecodes::_ladd: set_constant(a + b); return; duke@435: case Bytecodes::_lsub: set_constant(a - b); return; duke@435: case Bytecodes::_lmul: set_constant(a * b); return; duke@435: case Bytecodes::_ldiv: duke@435: if (b != 0) { duke@435: set_constant(SharedRuntime::ldiv(b, a)); duke@435: return; duke@435: } duke@435: break; duke@435: case Bytecodes::_lrem: duke@435: if (b != 0) { duke@435: set_constant(SharedRuntime::lrem(b, a)); duke@435: return; duke@435: } duke@435: break; duke@435: case Bytecodes::_land: set_constant(a & b); return; duke@435: case Bytecodes::_lor : set_constant(a | b); return; duke@435: case Bytecodes::_lxor: set_constant(a ^ b); return; duke@435: } duke@435: } duke@435: break; duke@435: // other cases not implemented (must be extremely careful with floats & doubles!) duke@435: } duke@435: } duke@435: // make sure constant is on the right side, if any duke@435: move_const_to_right(x); duke@435: duke@435: if (x->y()->type()->is_constant()) { duke@435: // do constant folding for selected operations duke@435: switch (x->type()->tag()) { duke@435: case intTag: duke@435: if (x->y()->type()->as_IntConstant()->value() == 0) { duke@435: switch (x->op()) { duke@435: case Bytecodes::_iadd: set_canonical(x->x()); return; duke@435: case Bytecodes::_isub: set_canonical(x->x()); return; duke@435: case Bytecodes::_imul: set_constant(0); return; duke@435: // Note: for div and rem, make sure that C semantics duke@435: // corresponds to Java semantics! duke@435: case Bytecodes::_iand: set_constant(0); return; duke@435: case Bytecodes::_ior : set_canonical(x->x()); return; duke@435: } duke@435: } duke@435: break; duke@435: case longTag: duke@435: if (x->y()->type()->as_LongConstant()->value() == (jlong)0) { duke@435: switch (x->op()) { duke@435: case Bytecodes::_ladd: set_canonical(x->x()); return; duke@435: case Bytecodes::_lsub: set_canonical(x->x()); return; duke@435: case Bytecodes::_lmul: set_constant((jlong)0); return; duke@435: // Note: for div and rem, make sure that C semantics duke@435: // corresponds to Java semantics! duke@435: case Bytecodes::_land: set_constant((jlong)0); return; duke@435: case Bytecodes::_lor : set_canonical(x->x()); return; duke@435: } duke@435: } duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_Phi (Phi* x) {} duke@435: void Canonicalizer::do_Constant (Constant* x) {} duke@435: void Canonicalizer::do_Local (Local* x) {} duke@435: void Canonicalizer::do_LoadField (LoadField* x) {} duke@435: duke@435: // checks if v is in the block that is currently processed by duke@435: // GraphBuilder. This is the only block that has not BlockEnd yet. duke@435: static bool in_current_block(Value v) { duke@435: int max_distance = 4; duke@435: while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) { duke@435: v = v->next(); duke@435: max_distance--; duke@435: } duke@435: return v == NULL; duke@435: } duke@435: duke@435: void Canonicalizer::do_StoreField (StoreField* x) { duke@435: // If a value is going to be stored into a field or array some of duke@435: // the conversions emitted by javac are unneeded because the fields duke@435: // are packed to their natural size. duke@435: Convert* conv = x->value()->as_Convert(); duke@435: if (conv) { duke@435: Value value = NULL; duke@435: BasicType type = x->field()->type()->basic_type(); duke@435: switch (conv->op()) { duke@435: case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; duke@435: case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; duke@435: case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; duke@435: } duke@435: // limit this optimization to current block duke@435: if (value != NULL && in_current_block(conv)) { duke@435: set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(), iveresov@1939: x->lock_stack(), x->state_before(), x->is_loaded(), x->is_initialized())); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: } duke@435: duke@435: void Canonicalizer::do_ArrayLength (ArrayLength* x) { duke@435: NewArray* array = x->array()->as_NewArray(); duke@435: if (array != NULL && array->length() != NULL) { duke@435: Constant* length = array->length()->as_Constant(); duke@435: if (length != NULL) { duke@435: // do not use the Constant itself, but create a new Constant duke@435: // with same value Otherwise a Constant is live over multiple duke@435: // blocks without being registered in a state array. duke@435: assert(length->type()->as_IntConstant() != NULL, "array length must be integer"); duke@435: set_constant(length->type()->as_IntConstant()->value()); duke@435: } duke@435: } else { duke@435: LoadField* lf = x->array()->as_LoadField(); twisti@1730: if (lf != NULL) { twisti@1730: ciField* field = lf->field(); twisti@1730: if (field->is_constant() && field->is_static()) { twisti@1730: // final static field twisti@1730: ciObject* c = field->constant_value().as_object(); twisti@1730: if (c->is_array()) { twisti@1730: ciArray* array = (ciArray*) c; twisti@1730: set_constant(array->length()); twisti@1730: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {} duke@435: void Canonicalizer::do_StoreIndexed (StoreIndexed* x) { duke@435: // If a value is going to be stored into a field or array some of duke@435: // the conversions emitted by javac are unneeded because the fields duke@435: // are packed to their natural size. duke@435: Convert* conv = x->value()->as_Convert(); duke@435: if (conv) { duke@435: Value value = NULL; duke@435: BasicType type = x->elt_type(); duke@435: switch (conv->op()) { duke@435: case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; duke@435: case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; duke@435: case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; duke@435: } duke@435: // limit this optimization to current block duke@435: if (value != NULL && in_current_block(conv)) { duke@435: set_canonical(new StoreIndexed(x->array(), x->index(), x->length(), duke@435: x->elt_type(), value, x->lock_stack())); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_NegateOp(NegateOp* x) { duke@435: ValueType* t = x->x()->type(); duke@435: if (t->is_constant()) { duke@435: switch (t->tag()) { duke@435: case intTag : set_constant(-t->as_IntConstant ()->value()); return; duke@435: case longTag : set_constant(-t->as_LongConstant ()->value()); return; duke@435: case floatTag : set_constant(-t->as_FloatConstant ()->value()); return; duke@435: case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return; duke@435: default : ShouldNotReachHere(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); } duke@435: duke@435: duke@435: void Canonicalizer::do_ShiftOp (ShiftOp* x) { duke@435: ValueType* t = x->x()->type(); duke@435: ValueType* t2 = x->y()->type(); duke@435: if (t->is_constant()) { duke@435: switch (t->tag()) { duke@435: case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break; duke@435: case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break; duke@435: default : ShouldNotReachHere(); duke@435: } duke@435: if (t2->is_constant()) { duke@435: if (t->tag() == intTag) { duke@435: int value = t->as_IntConstant()->value(); duke@435: int shift = t2->as_IntConstant()->value() & 31; duke@435: jint mask = ~(~0 << (32 - shift)); duke@435: if (shift == 0) mask = ~0; duke@435: switch (x->op()) { duke@435: case Bytecodes::_ishl: set_constant(value << shift); return; duke@435: case Bytecodes::_ishr: set_constant(value >> shift); return; duke@435: case Bytecodes::_iushr: set_constant((value >> shift) & mask); return; duke@435: } duke@435: } else if (t->tag() == longTag) { duke@435: jlong value = t->as_LongConstant()->value(); duke@435: int shift = t2->as_IntConstant()->value() & 63; duke@435: jlong mask = ~(~jlong_cast(0) << (64 - shift)); duke@435: if (shift == 0) mask = ~jlong_cast(0); duke@435: switch (x->op()) { duke@435: case Bytecodes::_lshl: set_constant(value << shift); return; duke@435: case Bytecodes::_lshr: set_constant(value >> shift); return; duke@435: case Bytecodes::_lushr: set_constant((value >> shift) & mask); return; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: if (t2->is_constant()) { duke@435: switch (t2->tag()) { duke@435: case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; duke@435: case longTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; duke@435: default : ShouldNotReachHere(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); } duke@435: void Canonicalizer::do_CompareOp (CompareOp* x) { duke@435: if (x->x() == x->y()) { duke@435: switch (x->x()->type()->tag()) { duke@435: case longTag: set_constant(0); break; duke@435: case floatTag: { duke@435: FloatConstant* fc = x->x()->type()->as_FloatConstant(); duke@435: if (fc) { duke@435: if (g_isnan(fc->value())) { duke@435: set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); duke@435: } else { duke@435: set_constant(0); duke@435: } duke@435: } duke@435: break; duke@435: } duke@435: case doubleTag: { duke@435: DoubleConstant* dc = x->x()->type()->as_DoubleConstant(); duke@435: if (dc) { duke@435: if (g_isnan(dc->value())) { duke@435: set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); duke@435: } else { duke@435: set_constant(0); duke@435: } duke@435: } duke@435: break; duke@435: } duke@435: } duke@435: } else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { duke@435: switch (x->x()->type()->tag()) { duke@435: case longTag: { duke@435: jlong vx = x->x()->type()->as_LongConstant()->value(); duke@435: jlong vy = x->y()->type()->as_LongConstant()->value(); duke@435: if (vx == vy) duke@435: set_constant(0); duke@435: else if (vx < vy) duke@435: set_constant(-1); duke@435: else duke@435: set_constant(1); duke@435: break; duke@435: } duke@435: duke@435: case floatTag: { duke@435: float vx = x->x()->type()->as_FloatConstant()->value(); duke@435: float vy = x->y()->type()->as_FloatConstant()->value(); duke@435: if (g_isnan(vx) || g_isnan(vy)) duke@435: set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1); duke@435: else if (vx == vy) duke@435: set_constant(0); duke@435: else if (vx < vy) duke@435: set_constant(-1); duke@435: else duke@435: set_constant(1); duke@435: break; duke@435: } duke@435: duke@435: case doubleTag: { duke@435: double vx = x->x()->type()->as_DoubleConstant()->value(); duke@435: double vy = x->y()->type()->as_DoubleConstant()->value(); duke@435: if (g_isnan(vx) || g_isnan(vy)) duke@435: set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1); duke@435: else if (vx == vy) duke@435: set_constant(0); duke@435: else if (vx < vy) duke@435: set_constant(-1); duke@435: else duke@435: set_constant(1); duke@435: break; duke@435: } duke@435: } duke@435: duke@435: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {} duke@435: duke@435: void Canonicalizer::do_IfOp(IfOp* x) { duke@435: // Caution: do not use do_Op2(x) here for now since duke@435: // we map the condition to the op for now! duke@435: move_const_to_right(x); duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_Intrinsic (Intrinsic* x) { duke@435: switch (x->id()) { duke@435: case vmIntrinsics::_floatToRawIntBits : { duke@435: FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant(); duke@435: if (c != NULL) { duke@435: JavaValue v; duke@435: v.set_jfloat(c->value()); duke@435: set_constant(v.get_jint()); duke@435: } duke@435: break; duke@435: } duke@435: case vmIntrinsics::_intBitsToFloat : { duke@435: IntConstant* c = x->argument_at(0)->type()->as_IntConstant(); duke@435: if (c != NULL) { duke@435: JavaValue v; duke@435: v.set_jint(c->value()); duke@435: set_constant(v.get_jfloat()); duke@435: } duke@435: break; duke@435: } duke@435: case vmIntrinsics::_doubleToRawLongBits : { duke@435: DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant(); duke@435: if (c != NULL) { duke@435: JavaValue v; duke@435: v.set_jdouble(c->value()); duke@435: set_constant(v.get_jlong()); duke@435: } duke@435: break; duke@435: } duke@435: case vmIntrinsics::_longBitsToDouble : { duke@435: LongConstant* c = x->argument_at(0)->type()->as_LongConstant(); duke@435: if (c != NULL) { duke@435: JavaValue v; duke@435: v.set_jlong(c->value()); duke@435: set_constant(v.get_jdouble()); duke@435: } duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: duke@435: void Canonicalizer::do_Convert (Convert* x) { duke@435: if (x->value()->type()->is_constant()) { duke@435: switch (x->op()) { duke@435: case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break; duke@435: case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break; duke@435: case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break; duke@435: case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break; duke@435: case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break; duke@435: case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break; duke@435: case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break; duke@435: case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break; duke@435: case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break; duke@435: case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break; duke@435: case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break; duke@435: case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break; duke@435: case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break; duke@435: case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break; duke@435: case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break; duke@435: default: duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: duke@435: Value value = x->value(); duke@435: BasicType type = T_ILLEGAL; duke@435: LoadField* lf = value->as_LoadField(); duke@435: if (lf) { duke@435: type = lf->field_type(); duke@435: } else { duke@435: LoadIndexed* li = value->as_LoadIndexed(); duke@435: if (li) { duke@435: type = li->elt_type(); duke@435: } else { duke@435: Convert* conv = value->as_Convert(); duke@435: if (conv) { duke@435: switch (conv->op()) { duke@435: case Bytecodes::_i2b: type = T_BYTE; break; duke@435: case Bytecodes::_i2s: type = T_SHORT; break; duke@435: case Bytecodes::_i2c: type = T_CHAR; break; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: if (type != T_ILLEGAL) { duke@435: switch (x->op()) { duke@435: case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break; duke@435: case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break; duke@435: case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break; duke@435: } duke@435: } else { duke@435: Op2* op2 = x->value()->as_Op2(); duke@435: if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) { duke@435: jint safebits = 0; duke@435: jint mask = op2->y()->type()->as_IntConstant()->value(); duke@435: switch (x->op()) { duke@435: case Bytecodes::_i2b: safebits = 0x7f; break; duke@435: case Bytecodes::_i2s: safebits = 0x7fff; break; duke@435: case Bytecodes::_i2c: safebits = 0xffff; break; duke@435: } duke@435: // When casting a masked integer to a smaller signed type, if duke@435: // the mask doesn't include the sign bit the cast isn't needed. duke@435: if (safebits && (mask & ~safebits) == 0) { duke@435: set_canonical(x->value()); duke@435: } duke@435: } duke@435: } duke@435: duke@435: } duke@435: duke@435: void Canonicalizer::do_NullCheck (NullCheck* x) { duke@435: if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) { duke@435: set_canonical(x->obj()); duke@435: } else { duke@435: Constant* con = x->obj()->as_Constant(); duke@435: if (con) { duke@435: ObjectType* c = con->type()->as_ObjectType(); duke@435: if (c && c->is_loaded()) { duke@435: ObjectConstant* oc = c->as_ObjectConstant(); duke@435: if (!oc || !oc->value()->is_null_object()) { duke@435: set_canonical(con); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: void Canonicalizer::do_Invoke (Invoke* x) {} duke@435: void Canonicalizer::do_NewInstance (NewInstance* x) {} duke@435: void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {} duke@435: void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {} duke@435: void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {} duke@435: void Canonicalizer::do_CheckCast (CheckCast* x) { duke@435: if (x->klass()->is_loaded()) { duke@435: Value obj = x->obj(); duke@435: ciType* klass = obj->exact_type(); duke@435: if (klass == NULL) klass = obj->declared_type(); duke@435: if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) { duke@435: set_canonical(obj); duke@435: return; duke@435: } duke@435: // checkcast of null returns null duke@435: if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { duke@435: set_canonical(obj); duke@435: } duke@435: } duke@435: } duke@435: void Canonicalizer::do_InstanceOf (InstanceOf* x) { duke@435: if (x->klass()->is_loaded()) { duke@435: Value obj = x->obj(); duke@435: ciType* exact = obj->exact_type(); duke@435: if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) { duke@435: set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0); duke@435: return; duke@435: } duke@435: // instanceof null returns false duke@435: if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) { duke@435: set_constant(0); duke@435: } duke@435: } duke@435: duke@435: } duke@435: void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {} duke@435: void Canonicalizer::do_MonitorExit (MonitorExit* x) {} duke@435: void Canonicalizer::do_BlockBegin (BlockBegin* x) {} duke@435: void Canonicalizer::do_Goto (Goto* x) {} duke@435: duke@435: duke@435: static bool is_true(jlong x, If::Condition cond, jlong y) { duke@435: switch (cond) { duke@435: case If::eql: return x == y; duke@435: case If::neq: return x != y; duke@435: case If::lss: return x < y; duke@435: case If::leq: return x <= y; duke@435: case If::gtr: return x > y; duke@435: case If::geq: return x >= y; duke@435: } duke@435: ShouldNotReachHere(); duke@435: return false; duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_If(If* x) { duke@435: // move const to right duke@435: if (x->x()->type()->is_constant()) x->swap_operands(); duke@435: // simplify duke@435: const Value l = x->x(); ValueType* lt = l->type(); duke@435: const Value r = x->y(); ValueType* rt = r->type(); duke@435: duke@435: if (l == r && !lt->is_float_kind()) { duke@435: // pattern: If (a cond a) => simplify to Goto duke@435: BlockBegin* sux; duke@435: switch (x->cond()) { duke@435: case If::eql: sux = x->sux_for(true); break; duke@435: case If::neq: sux = x->sux_for(false); break; duke@435: case If::lss: sux = x->sux_for(false); break; duke@435: case If::leq: sux = x->sux_for(true); break; duke@435: case If::gtr: sux = x->sux_for(false); break; duke@435: case If::geq: sux = x->sux_for(true); break; duke@435: } duke@435: // If is a safepoint then the debug information should come from the state_before of the If. duke@435: set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); duke@435: return; duke@435: } duke@435: duke@435: if (lt->is_constant() && rt->is_constant()) { duke@435: if (x->x()->as_Constant() != NULL) { duke@435: // pattern: If (lc cond rc) => simplify to: Goto duke@435: BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(), duke@435: x->sux_for(true), duke@435: x->sux_for(false)); duke@435: if (sux != NULL) { duke@435: // If is a safepoint then the debug information should come from the state_before of the If. duke@435: set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); duke@435: } duke@435: } duke@435: } else if (rt->as_IntConstant() != NULL) { duke@435: // pattern: If (l cond rc) => investigate further duke@435: const jint rc = rt->as_IntConstant()->value(); duke@435: if (l->as_CompareOp() != NULL) { duke@435: // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto duke@435: CompareOp* cmp = l->as_CompareOp(); duke@435: bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl; duke@435: BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b duke@435: BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b duke@435: BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b duke@435: BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered duke@435: // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are duke@435: // equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either duke@435: // lss_sux or gtr_sux. duke@435: if (lss_sux == eql_sux && eql_sux == gtr_sux) { duke@435: // all successors identical => simplify to: Goto duke@435: set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint())); duke@435: } else { duke@435: // two successors differ and two successors are the same => simplify to: If (x cmp y) duke@435: // determine new condition & successors duke@435: If::Condition cond; duke@435: BlockBegin* tsux = NULL; duke@435: BlockBegin* fsux = NULL; duke@435: if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; } duke@435: else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; } duke@435: else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; } duke@435: else { ShouldNotReachHere(); } iveresov@2138: If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint()); duke@435: if (cmp->x() == cmp->y()) { duke@435: do_If(canon); duke@435: } else { iveresov@2138: if (compilation()->profile_branches()) { iveresov@2138: // TODO: If profiling, leave floating point comparisons unoptimized. iveresov@2138: // We currently do not support profiling of the unordered case. iveresov@2138: switch(cmp->op()) { iveresov@2138: case Bytecodes::_fcmpl: case Bytecodes::_fcmpg: iveresov@2138: case Bytecodes::_dcmpl: case Bytecodes::_dcmpg: iveresov@2138: set_canonical(x); iveresov@2138: return; iveresov@2138: } iveresov@2138: } duke@435: set_canonical(canon); duke@435: set_bci(cmp->bci()); duke@435: } duke@435: } duke@435: } else if (l->as_InstanceOf() != NULL) { duke@435: // NOTE: Code permanently disabled for now since it leaves the old InstanceOf duke@435: // instruction in the graph (it is pinned). Need to fix this at some point. iveresov@2146: // It should also be left in the graph when generating a profiled method version or Goto iveresov@2146: // has to know that it was an InstanceOf. duke@435: return; duke@435: // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto duke@435: InstanceOf* inst = l->as_InstanceOf(); duke@435: BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1 duke@435: BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0 duke@435: if (is_inst_sux == no_inst_sux && inst->is_loaded()) { duke@435: // both successors identical and klass is loaded => simplify to: Goto duke@435: set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint())); duke@435: } else { duke@435: // successors differ => simplify to: IfInstanceOf duke@435: set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->bci(), is_inst_sux, no_inst_sux)); duke@435: } duke@435: } duke@435: } else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) { duke@435: if (x->cond() == Instruction::eql) { duke@435: set_canonical(new Goto(x->fsux(), x->state_before(), x->is_safepoint())); duke@435: } else { duke@435: assert(x->cond() == Instruction::neq, "only other valid case"); duke@435: set_canonical(new Goto(x->tsux(), x->state_before(), x->is_safepoint())); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_TableSwitch(TableSwitch* x) { duke@435: if (x->tag()->type()->is_constant()) { duke@435: int v = x->tag()->type()->as_IntConstant()->value(); duke@435: BlockBegin* sux = x->default_sux(); duke@435: if (v >= x->lo_key() && v <= x->hi_key()) { duke@435: sux = x->sux_at(v - x->lo_key()); duke@435: } duke@435: set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); duke@435: } else if (x->number_of_sux() == 1) { duke@435: // NOTE: Code permanently disabled for now since the switch statement's duke@435: // tag expression may produce side-effects in which case it must duke@435: // be executed. duke@435: return; duke@435: // simplify to Goto duke@435: set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); duke@435: } else if (x->number_of_sux() == 2) { duke@435: // NOTE: Code permanently disabled for now since it produces two new nodes duke@435: // (Constant & If) and the Canonicalizer cannot return them correctly duke@435: // yet. For now we copied the corresponding code directly into the duke@435: // GraphBuilder (i.e., we should never reach here). duke@435: return; duke@435: // simplify to If duke@435: assert(x->lo_key() == x->hi_key(), "keys must be the same"); duke@435: Constant* key = new Constant(new IntConstant(x->lo_key())); duke@435: 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: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_LookupSwitch(LookupSwitch* x) { duke@435: if (x->tag()->type()->is_constant()) { duke@435: int v = x->tag()->type()->as_IntConstant()->value(); duke@435: BlockBegin* sux = x->default_sux(); duke@435: for (int i = 0; i < x->length(); i++) { duke@435: if (v == x->key_at(i)) { duke@435: sux = x->sux_at(i); duke@435: } duke@435: } duke@435: set_canonical(new Goto(sux, x->state_before(), x->is_safepoint())); duke@435: } else if (x->number_of_sux() == 1) { duke@435: // NOTE: Code permanently disabled for now since the switch statement's duke@435: // tag expression may produce side-effects in which case it must duke@435: // be executed. duke@435: return; duke@435: // simplify to Goto duke@435: set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint())); duke@435: } else if (x->number_of_sux() == 2) { duke@435: // NOTE: Code permanently disabled for now since it produces two new nodes duke@435: // (Constant & If) and the Canonicalizer cannot return them correctly duke@435: // yet. For now we copied the corresponding code directly into the duke@435: // GraphBuilder (i.e., we should never reach here). duke@435: return; duke@435: // simplify to If duke@435: assert(x->length() == 1, "length must be the same"); duke@435: Constant* key = new Constant(new IntConstant(x->key_at(0))); duke@435: 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: } duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_Return (Return* x) {} duke@435: void Canonicalizer::do_Throw (Throw* x) {} duke@435: void Canonicalizer::do_Base (Base* x) {} duke@435: void Canonicalizer::do_OsrEntry (OsrEntry* x) {} duke@435: void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {} duke@435: duke@435: static bool match_index_and_scale(Instruction* instr, duke@435: Instruction** index, duke@435: int* log2_scale, duke@435: Instruction** instr_to_unpin) { duke@435: *instr_to_unpin = NULL; duke@435: duke@435: // Skip conversion ops duke@435: Convert* convert = instr->as_Convert(); duke@435: if (convert != NULL) { duke@435: instr = convert->value(); duke@435: } duke@435: duke@435: ShiftOp* shift = instr->as_ShiftOp(); duke@435: if (shift != NULL) { duke@435: if (shift->is_pinned()) { duke@435: *instr_to_unpin = shift; duke@435: } duke@435: // Constant shift value? duke@435: Constant* con = shift->y()->as_Constant(); duke@435: if (con == NULL) return false; duke@435: // Well-known type and value? duke@435: IntConstant* val = con->type()->as_IntConstant(); duke@435: if (val == NULL) return false; duke@435: if (shift->x()->type() != intType) return false; duke@435: *index = shift->x(); duke@435: int tmp_scale = val->value(); duke@435: if (tmp_scale >= 0 && tmp_scale < 4) { duke@435: *log2_scale = tmp_scale; duke@435: return true; duke@435: } else { duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: ArithmeticOp* arith = instr->as_ArithmeticOp(); duke@435: if (arith != NULL) { duke@435: if (arith->is_pinned()) { duke@435: *instr_to_unpin = arith; duke@435: } duke@435: // Check for integer multiply duke@435: if (arith->op() == Bytecodes::_imul) { duke@435: // See if either arg is a known constant duke@435: Constant* con = arith->x()->as_Constant(); duke@435: if (con != NULL) { duke@435: *index = arith->y(); duke@435: } else { duke@435: con = arith->y()->as_Constant(); duke@435: if (con == NULL) return false; duke@435: *index = arith->x(); duke@435: } duke@435: if ((*index)->type() != intType) return false; duke@435: // Well-known type and value? duke@435: IntConstant* val = con->type()->as_IntConstant(); duke@435: if (val == NULL) return false; duke@435: switch (val->value()) { duke@435: case 1: *log2_scale = 0; return true; duke@435: case 2: *log2_scale = 1; return true; duke@435: case 4: *log2_scale = 2; return true; duke@435: case 8: *log2_scale = 3; return true; duke@435: default: return false; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Unknown instruction sequence; don't touch it duke@435: return false; duke@435: } duke@435: duke@435: duke@435: static bool match(UnsafeRawOp* x, duke@435: Instruction** base, duke@435: Instruction** index, duke@435: int* log2_scale) { duke@435: Instruction* instr_to_unpin = NULL; duke@435: ArithmeticOp* root = x->base()->as_ArithmeticOp(); duke@435: if (root == NULL) return false; duke@435: // Limit ourselves to addition for now duke@435: if (root->op() != Bytecodes::_ladd) return false; duke@435: // Try to find shift or scale op duke@435: if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) { duke@435: *base = root->x(); duke@435: } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) { duke@435: *base = root->y(); duke@435: } else if (root->y()->as_Convert() != NULL) { duke@435: Convert* convert = root->y()->as_Convert(); duke@435: if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) { duke@435: // pick base and index, setting scale at 1 duke@435: *base = root->x(); duke@435: *index = convert->value(); duke@435: *log2_scale = 0; duke@435: } else { duke@435: return false; duke@435: } duke@435: } else { duke@435: // doesn't match any expected sequences duke@435: return false; duke@435: } duke@435: duke@435: // If the value is pinned then it will be always be computed so duke@435: // there's no profit to reshaping the expression. duke@435: return !root->is_pinned(); duke@435: } duke@435: duke@435: duke@435: void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) { duke@435: Instruction* base = NULL; duke@435: Instruction* index = NULL; duke@435: int log2_scale; duke@435: duke@435: if (match(x, &base, &index, &log2_scale)) { duke@435: x->set_base(base); duke@435: x->set_index(index); duke@435: x->set_log2_scale(log2_scale); duke@435: if (PrintUnsafeOptimization) { duke@435: tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d", duke@435: x->id(), x->base()->id(), x->index()->id(), x->log2_scale()); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void Canonicalizer::do_RoundFP(RoundFP* x) {} duke@435: void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } duke@435: void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); } duke@435: void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {} duke@435: void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {} duke@435: void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {} duke@435: void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {} duke@435: void Canonicalizer::do_ProfileCall(ProfileCall* x) {} iveresov@2138: void Canonicalizer::do_ProfileInvoke(ProfileInvoke* x) {} iveresov@2138: