Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/c1/c1_Canonicalizer.cpp
32285 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_Canonicalizer.hpp"26#include "c1/c1_InstructionPrinter.hpp"27#include "c1/c1_ValueStack.hpp"28#include "ci/ciArray.hpp"29#include "runtime/sharedRuntime.hpp"303132class PrintValueVisitor: public ValueVisitor {33void visit(Value* vp) {34(*vp)->print_line();35}36};3738void Canonicalizer::set_canonical(Value x) {39assert(x != NULL, "value must exist");40// Note: we can not currently substitute root nodes which show up in41// the instruction stream (because the instruction list is embedded42// in the instructions).43if (canonical() != x) {44#ifndef PRODUCT45if (!x->has_printable_bci()) {46x->set_printable_bci(bci());47}48#endif49if (PrintCanonicalization) {50PrintValueVisitor do_print_value;51canonical()->input_values_do(&do_print_value);52canonical()->print_line();53tty->print_cr("canonicalized to:");54x->input_values_do(&do_print_value);55x->print_line();56tty->cr();57}58assert(_canonical->type()->tag() == x->type()->tag(), "types must match");59_canonical = x;60}61}626364void Canonicalizer::move_const_to_right(Op2* x) {65if (x->x()->type()->is_constant() && x->is_commutative()) x->swap_operands();66}676869void Canonicalizer::do_Op2(Op2* x) {70if (x->x() == x->y()) {71switch (x->op()) {72case Bytecodes::_isub: set_constant(0); return;73case Bytecodes::_lsub: set_constant(jlong_cast(0)); return;74case Bytecodes::_iand: // fall through75case Bytecodes::_land: // fall through76case Bytecodes::_ior: // fall through77case Bytecodes::_lor : set_canonical(x->x()); return;78case Bytecodes::_ixor: set_constant(0); return;79case Bytecodes::_lxor: set_constant(jlong_cast(0)); return;80}81}8283if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {84// do constant folding for selected operations85switch (x->type()->tag()) {86case intTag:87{ jint a = x->x()->type()->as_IntConstant()->value();88jint b = x->y()->type()->as_IntConstant()->value();89switch (x->op()) {90case Bytecodes::_iadd: set_constant(a + b); return;91case Bytecodes::_isub: set_constant(a - b); return;92case Bytecodes::_imul: set_constant(a * b); return;93case Bytecodes::_idiv:94if (b != 0) {95if (a == min_jint && b == -1) {96set_constant(min_jint);97} else {98set_constant(a / b);99}100return;101}102break;103case Bytecodes::_irem:104if (b != 0) {105if (a == min_jint && b == -1) {106set_constant(0);107} else {108set_constant(a % b);109}110return;111}112break;113case Bytecodes::_iand: set_constant(a & b); return;114case Bytecodes::_ior : set_constant(a | b); return;115case Bytecodes::_ixor: set_constant(a ^ b); return;116}117}118break;119case longTag:120{ jlong a = x->x()->type()->as_LongConstant()->value();121jlong b = x->y()->type()->as_LongConstant()->value();122switch (x->op()) {123case Bytecodes::_ladd: set_constant(a + b); return;124case Bytecodes::_lsub: set_constant(a - b); return;125case Bytecodes::_lmul: set_constant(a * b); return;126case Bytecodes::_ldiv:127if (b != 0) {128set_constant(SharedRuntime::ldiv(b, a));129return;130}131break;132case Bytecodes::_lrem:133if (b != 0) {134set_constant(SharedRuntime::lrem(b, a));135return;136}137break;138case Bytecodes::_land: set_constant(a & b); return;139case Bytecodes::_lor : set_constant(a | b); return;140case Bytecodes::_lxor: set_constant(a ^ b); return;141}142}143break;144// other cases not implemented (must be extremely careful with floats & doubles!)145}146}147// make sure constant is on the right side, if any148move_const_to_right(x);149150if (x->y()->type()->is_constant()) {151// do constant folding for selected operations152switch (x->type()->tag()) {153case intTag:154if (x->y()->type()->as_IntConstant()->value() == 0) {155switch (x->op()) {156case Bytecodes::_iadd: set_canonical(x->x()); return;157case Bytecodes::_isub: set_canonical(x->x()); return;158case Bytecodes::_imul: set_constant(0); return;159// Note: for div and rem, make sure that C semantics160// corresponds to Java semantics!161case Bytecodes::_iand: set_constant(0); return;162case Bytecodes::_ior : set_canonical(x->x()); return;163}164}165break;166case longTag:167if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {168switch (x->op()) {169case Bytecodes::_ladd: set_canonical(x->x()); return;170case Bytecodes::_lsub: set_canonical(x->x()); return;171case Bytecodes::_lmul: set_constant((jlong)0); return;172// Note: for div and rem, make sure that C semantics173// corresponds to Java semantics!174case Bytecodes::_land: set_constant((jlong)0); return;175case Bytecodes::_lor : set_canonical(x->x()); return;176}177}178break;179}180}181}182183184void Canonicalizer::do_Phi (Phi* x) {}185void Canonicalizer::do_Constant (Constant* x) {}186void Canonicalizer::do_Local (Local* x) {}187void Canonicalizer::do_LoadField (LoadField* x) {}188189// checks if v is in the block that is currently processed by190// GraphBuilder. This is the only block that has not BlockEnd yet.191static bool in_current_block(Value v) {192int max_distance = 4;193while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) {194v = v->next();195max_distance--;196}197return v == NULL;198}199200void Canonicalizer::do_StoreField (StoreField* x) {201// If a value is going to be stored into a field or array some of202// the conversions emitted by javac are unneeded because the fields203// are packed to their natural size.204Convert* conv = x->value()->as_Convert();205if (conv) {206Value value = NULL;207BasicType type = x->field()->type()->basic_type();208switch (conv->op()) {209case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;210case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;211case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;212}213// limit this optimization to current block214if (value != NULL && in_current_block(conv)) {215set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),216x->state_before(), x->needs_patching()));217return;218}219}220221}222223void Canonicalizer::do_ArrayLength (ArrayLength* x) {224NewArray* array = x->array()->as_NewArray();225if (array != NULL && array->length() != NULL) {226Constant* length = array->length()->as_Constant();227if (length != NULL) {228// do not use the Constant itself, but create a new Constant229// with same value Otherwise a Constant is live over multiple230// blocks without being registered in a state array.231assert(length->type()->as_IntConstant() != NULL, "array length must be integer");232set_constant(length->type()->as_IntConstant()->value());233}234} else {235LoadField* lf = x->array()->as_LoadField();236if (lf != NULL) {237ciField* field = lf->field();238if (field->is_constant() && field->is_static()) {239// final static field240ciObject* c = field->constant_value().as_object();241if (c->is_array()) {242ciArray* array = (ciArray*) c;243set_constant(array->length());244}245}246}247}248}249250void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {}251void Canonicalizer::do_StoreIndexed (StoreIndexed* x) {252// If a value is going to be stored into a field or array some of253// the conversions emitted by javac are unneeded because the fields254// are packed to their natural size.255Convert* conv = x->value()->as_Convert();256if (conv) {257Value value = NULL;258BasicType type = x->elt_type();259switch (conv->op()) {260case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;261case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;262case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;263}264// limit this optimization to current block265if (value != NULL && in_current_block(conv)) {266set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),267x->elt_type(), value, x->state_before(),268x->check_boolean()));269return;270}271}272273274}275276277void Canonicalizer::do_NegateOp(NegateOp* x) {278ValueType* t = x->x()->type();279if (t->is_constant()) {280switch (t->tag()) {281case intTag : set_constant(-t->as_IntConstant ()->value()); return;282case longTag : set_constant(-t->as_LongConstant ()->value()); return;283case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;284case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;285default : ShouldNotReachHere();286}287}288}289290291void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); }292293294void Canonicalizer::do_ShiftOp (ShiftOp* x) {295ValueType* t = x->x()->type();296ValueType* t2 = x->y()->type();297if (t->is_constant()) {298switch (t->tag()) {299case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break;300case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;301default : ShouldNotReachHere();302}303if (t2->is_constant()) {304if (t->tag() == intTag) {305int value = t->as_IntConstant()->value();306int shift = t2->as_IntConstant()->value() & 31;307jint mask = ~(~0 << (32 - shift));308if (shift == 0) mask = ~0;309switch (x->op()) {310case Bytecodes::_ishl: set_constant(value << shift); return;311case Bytecodes::_ishr: set_constant(value >> shift); return;312case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;313}314} else if (t->tag() == longTag) {315jlong value = t->as_LongConstant()->value();316int shift = t2->as_IntConstant()->value() & 63;317jlong mask = ~(~jlong_cast(0) << (64 - shift));318if (shift == 0) mask = ~jlong_cast(0);319switch (x->op()) {320case Bytecodes::_lshl: set_constant(value << shift); return;321case Bytecodes::_lshr: set_constant(value >> shift); return;322case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;323}324}325}326}327if (t2->is_constant()) {328switch (t2->tag()) {329case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;330case longTag : if (t2->as_LongConstant()->value() == (jlong)0) set_canonical(x->x()); return;331default : ShouldNotReachHere();332}333}334}335336337void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }338void Canonicalizer::do_CompareOp (CompareOp* x) {339if (x->x() == x->y()) {340switch (x->x()->type()->tag()) {341case longTag: set_constant(0); break;342case floatTag: {343FloatConstant* fc = x->x()->type()->as_FloatConstant();344if (fc) {345if (g_isnan(fc->value())) {346set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);347} else {348set_constant(0);349}350}351break;352}353case doubleTag: {354DoubleConstant* dc = x->x()->type()->as_DoubleConstant();355if (dc) {356if (g_isnan(dc->value())) {357set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);358} else {359set_constant(0);360}361}362break;363}364}365} else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {366switch (x->x()->type()->tag()) {367case longTag: {368jlong vx = x->x()->type()->as_LongConstant()->value();369jlong vy = x->y()->type()->as_LongConstant()->value();370if (vx == vy)371set_constant(0);372else if (vx < vy)373set_constant(-1);374else375set_constant(1);376break;377}378379case floatTag: {380float vx = x->x()->type()->as_FloatConstant()->value();381float vy = x->y()->type()->as_FloatConstant()->value();382if (g_isnan(vx) || g_isnan(vy))383set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);384else if (vx == vy)385set_constant(0);386else if (vx < vy)387set_constant(-1);388else389set_constant(1);390break;391}392393case doubleTag: {394double vx = x->x()->type()->as_DoubleConstant()->value();395double vy = x->y()->type()->as_DoubleConstant()->value();396if (g_isnan(vx) || g_isnan(vy))397set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);398else if (vx == vy)399set_constant(0);400else if (vx < vy)401set_constant(-1);402else403set_constant(1);404break;405}406}407408}409}410411412void Canonicalizer::do_IfInstanceOf(IfInstanceOf* x) {}413414void Canonicalizer::do_IfOp(IfOp* x) {415// Caution: do not use do_Op2(x) here for now since416// we map the condition to the op for now!417move_const_to_right(x);418}419420421void Canonicalizer::do_Intrinsic (Intrinsic* x) {422switch (x->id()) {423case vmIntrinsics::_floatToRawIntBits : {424FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();425if (c != NULL) {426JavaValue v;427v.set_jfloat(c->value());428set_constant(v.get_jint());429}430break;431}432case vmIntrinsics::_intBitsToFloat : {433IntConstant* c = x->argument_at(0)->type()->as_IntConstant();434if (c != NULL) {435JavaValue v;436v.set_jint(c->value());437set_constant(v.get_jfloat());438}439break;440}441case vmIntrinsics::_doubleToRawLongBits : {442DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();443if (c != NULL) {444JavaValue v;445v.set_jdouble(c->value());446set_constant(v.get_jlong());447}448break;449}450case vmIntrinsics::_longBitsToDouble : {451LongConstant* c = x->argument_at(0)->type()->as_LongConstant();452if (c != NULL) {453JavaValue v;454v.set_jlong(c->value());455set_constant(v.get_jdouble());456}457break;458}459case vmIntrinsics::_isInstance : {460assert(x->number_of_arguments() == 2, "wrong type");461462InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();463if (c != NULL && !c->value()->is_null_object()) {464// ciInstance::java_mirror_type() returns non-NULL only for Java mirrors465ciType* t = c->value()->as_instance()->java_mirror_type();466if (t->is_klass()) {467// substitute cls.isInstance(obj) of a constant Class into468// an InstantOf instruction469InstanceOf* i = new InstanceOf(t->as_klass(), x->argument_at(1), x->state_before());470set_canonical(i);471// and try to canonicalize even further472do_InstanceOf(i);473} else {474assert(t->is_primitive_type(), "should be a primitive type");475// cls.isInstance(obj) always returns false for primitive classes476set_constant(0);477}478}479break;480}481}482}483484void Canonicalizer::do_Convert (Convert* x) {485if (x->value()->type()->is_constant()) {486switch (x->op()) {487case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;488case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;489case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;490case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break;491case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break;492case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break;493case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break;494case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;495case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;496case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break;497case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;498case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;499case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break;500case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;501case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;502default:503ShouldNotReachHere();504}505}506507Value value = x->value();508BasicType type = T_ILLEGAL;509LoadField* lf = value->as_LoadField();510if (lf) {511type = lf->field_type();512} else {513LoadIndexed* li = value->as_LoadIndexed();514if (li) {515type = li->elt_type();516} else {517Convert* conv = value->as_Convert();518if (conv) {519switch (conv->op()) {520case Bytecodes::_i2b: type = T_BYTE; break;521case Bytecodes::_i2s: type = T_SHORT; break;522case Bytecodes::_i2c: type = T_CHAR; break;523}524}525}526}527if (type != T_ILLEGAL) {528switch (x->op()) {529case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break;530case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;531case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break;532}533} else {534Op2* op2 = x->value()->as_Op2();535if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {536jint safebits = 0;537jint mask = op2->y()->type()->as_IntConstant()->value();538switch (x->op()) {539case Bytecodes::_i2b: safebits = 0x7f; break;540case Bytecodes::_i2s: safebits = 0x7fff; break;541case Bytecodes::_i2c: safebits = 0xffff; break;542}543// When casting a masked integer to a smaller signed type, if544// the mask doesn't include the sign bit the cast isn't needed.545if (safebits && (mask & ~safebits) == 0) {546set_canonical(x->value());547}548}549}550551}552553void Canonicalizer::do_NullCheck (NullCheck* x) {554if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {555set_canonical(x->obj());556} else {557Constant* con = x->obj()->as_Constant();558if (con) {559ObjectType* c = con->type()->as_ObjectType();560if (c && c->is_loaded()) {561ObjectConstant* oc = c->as_ObjectConstant();562if (!oc || !oc->value()->is_null_object()) {563set_canonical(con);564}565}566}567}568}569570void Canonicalizer::do_TypeCast (TypeCast* x) {}571void Canonicalizer::do_Invoke (Invoke* x) {}572void Canonicalizer::do_NewInstance (NewInstance* x) {}573void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {}574void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {}575void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {}576void Canonicalizer::do_CheckCast (CheckCast* x) {577if (x->klass()->is_loaded()) {578Value obj = x->obj();579ciType* klass = obj->exact_type();580if (klass == NULL) klass = obj->declared_type();581if (klass != NULL && klass->is_loaded() && klass->is_subtype_of(x->klass())) {582set_canonical(obj);583return;584}585// checkcast of null returns null586if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {587set_canonical(obj);588}589}590}591void Canonicalizer::do_InstanceOf (InstanceOf* x) {592if (x->klass()->is_loaded()) {593Value obj = x->obj();594ciType* exact = obj->exact_type();595if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {596set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);597return;598}599// instanceof null returns false600if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {601set_constant(0);602}603}604605}606void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {}607void Canonicalizer::do_MonitorExit (MonitorExit* x) {}608void Canonicalizer::do_BlockBegin (BlockBegin* x) {}609void Canonicalizer::do_Goto (Goto* x) {}610611612static bool is_true(jlong x, If::Condition cond, jlong y) {613switch (cond) {614case If::eql: return x == y;615case If::neq: return x != y;616case If::lss: return x < y;617case If::leq: return x <= y;618case If::gtr: return x > y;619case If::geq: return x >= y;620}621ShouldNotReachHere();622return false;623}624625static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {626// An Instruction with multiple successors, x, is replaced by a Goto627// to a single successor, sux. Is a safepoint check needed = was the628// instruction being replaced a safepoint and the single remaining629// successor a back branch?630return x->is_safepoint() && (sux->bci() < x->state_before()->bci());631}632633void Canonicalizer::do_If(If* x) {634// move const to right635if (x->x()->type()->is_constant()) x->swap_operands();636// simplify637const Value l = x->x(); ValueType* lt = l->type();638const Value r = x->y(); ValueType* rt = r->type();639640if (l == r && !lt->is_float_kind()) {641// pattern: If (a cond a) => simplify to Goto642BlockBegin* sux = NULL;643switch (x->cond()) {644case If::eql: sux = x->sux_for(true); break;645case If::neq: sux = x->sux_for(false); break;646case If::lss: sux = x->sux_for(false); break;647case If::leq: sux = x->sux_for(true); break;648case If::gtr: sux = x->sux_for(false); break;649case If::geq: sux = x->sux_for(true); break;650default: ShouldNotReachHere();651}652// If is a safepoint then the debug information should come from the state_before of the If.653set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));654return;655}656657if (lt->is_constant() && rt->is_constant()) {658if (x->x()->as_Constant() != NULL) {659// pattern: If (lc cond rc) => simplify to: Goto660BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),661x->sux_for(true),662x->sux_for(false));663if (sux != NULL) {664// If is a safepoint then the debug information should come from the state_before of the If.665set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));666}667}668} else if (rt->as_IntConstant() != NULL) {669// pattern: If (l cond rc) => investigate further670const jint rc = rt->as_IntConstant()->value();671if (l->as_CompareOp() != NULL) {672// pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto673CompareOp* cmp = l->as_CompareOp();674bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;675BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b676BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b677BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b678BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered679// Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are680// equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either681// lss_sux or gtr_sux.682if (lss_sux == eql_sux && eql_sux == gtr_sux) {683// all successors identical => simplify to: Goto684set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));685} else {686// two successors differ and two successors are the same => simplify to: If (x cmp y)687// determine new condition & successors688If::Condition cond = If::eql;689BlockBegin* tsux = NULL;690BlockBegin* fsux = NULL;691if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }692else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }693else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }694else { ShouldNotReachHere(); }695If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());696if (cmp->x() == cmp->y()) {697do_If(canon);698} else {699if (compilation()->profile_branches()) {700// TODO: If profiling, leave floating point comparisons unoptimized.701// We currently do not support profiling of the unordered case.702switch(cmp->op()) {703case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:704case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:705set_canonical(x);706return;707}708}709set_bci(cmp->state_before()->bci());710set_canonical(canon);711}712}713} else if (l->as_InstanceOf() != NULL) {714// NOTE: Code permanently disabled for now since it leaves the old InstanceOf715// instruction in the graph (it is pinned). Need to fix this at some point.716// It should also be left in the graph when generating a profiled method version or Goto717// has to know that it was an InstanceOf.718return;719// pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto720InstanceOf* inst = l->as_InstanceOf();721BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1722BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0723if (is_inst_sux == no_inst_sux && inst->is_loaded()) {724// both successors identical and klass is loaded => simplify to: Goto725set_canonical(new Goto(is_inst_sux, x->state_before(), x->is_safepoint()));726} else {727// successors differ => simplify to: IfInstanceOf728set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->state_before()->bci(), is_inst_sux, no_inst_sux));729}730}731} else if (rt == objectNull && (l->as_NewInstance() || l->as_NewArray())) {732if (x->cond() == Instruction::eql) {733BlockBegin* sux = x->fsux();734set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));735} else {736assert(x->cond() == Instruction::neq, "only other valid case");737BlockBegin* sux = x->tsux();738set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));739}740}741}742743744void Canonicalizer::do_TableSwitch(TableSwitch* x) {745if (x->tag()->type()->is_constant()) {746int v = x->tag()->type()->as_IntConstant()->value();747BlockBegin* sux = x->default_sux();748if (v >= x->lo_key() && v <= x->hi_key()) {749sux = x->sux_at(v - x->lo_key());750}751set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));752} else if (x->number_of_sux() == 1) {753// NOTE: Code permanently disabled for now since the switch statement's754// tag expression may produce side-effects in which case it must755// be executed.756return;757// simplify to Goto758set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));759} else if (x->number_of_sux() == 2) {760// NOTE: Code permanently disabled for now since it produces two new nodes761// (Constant & If) and the Canonicalizer cannot return them correctly762// yet. For now we copied the corresponding code directly into the763// GraphBuilder (i.e., we should never reach here).764return;765// simplify to If766assert(x->lo_key() == x->hi_key(), "keys must be the same");767Constant* key = new Constant(new IntConstant(x->lo_key()));768set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));769}770}771772773void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {774if (x->tag()->type()->is_constant()) {775int v = x->tag()->type()->as_IntConstant()->value();776BlockBegin* sux = x->default_sux();777for (int i = 0; i < x->length(); i++) {778if (v == x->key_at(i)) {779sux = x->sux_at(i);780}781}782set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));783} else if (x->number_of_sux() == 1) {784// NOTE: Code permanently disabled for now since the switch statement's785// tag expression may produce side-effects in which case it must786// be executed.787return;788// simplify to Goto789set_canonical(new Goto(x->default_sux(), x->state_before(), x->is_safepoint()));790} else if (x->number_of_sux() == 2) {791// NOTE: Code permanently disabled for now since it produces two new nodes792// (Constant & If) and the Canonicalizer cannot return them correctly793// yet. For now we copied the corresponding code directly into the794// GraphBuilder (i.e., we should never reach here).795return;796// simplify to If797assert(x->length() == 1, "length must be the same");798Constant* key = new Constant(new IntConstant(x->key_at(0)));799set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));800}801}802803804void Canonicalizer::do_Return (Return* x) {}805void Canonicalizer::do_Throw (Throw* x) {}806void Canonicalizer::do_Base (Base* x) {}807void Canonicalizer::do_OsrEntry (OsrEntry* x) {}808void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}809810static bool match_index_and_scale(Instruction* instr,811Instruction** index,812int* log2_scale) {813// Skip conversion ops. This works only on 32bit because of the implicit l2i that the814// unsafe performs.815#ifndef _LP64816Convert* convert = instr->as_Convert();817if (convert != NULL && convert->op() == Bytecodes::_i2l) {818assert(convert->value()->type() == intType, "invalid input type");819instr = convert->value();820}821#endif822823ShiftOp* shift = instr->as_ShiftOp();824if (shift != NULL) {825if (shift->op() == Bytecodes::_lshl) {826assert(shift->x()->type() == longType, "invalid input type");827} else {828#ifndef _LP64829if (shift->op() == Bytecodes::_ishl) {830assert(shift->x()->type() == intType, "invalid input type");831} else {832return false;833}834#else835return false;836#endif837}838839840// Constant shift value?841Constant* con = shift->y()->as_Constant();842if (con == NULL) return false;843// Well-known type and value?844IntConstant* val = con->type()->as_IntConstant();845assert(val != NULL, "Should be an int constant");846847*index = shift->x();848int tmp_scale = val->value();849if (tmp_scale >= 0 && tmp_scale < 4) {850*log2_scale = tmp_scale;851return true;852} else {853return false;854}855}856857ArithmeticOp* arith = instr->as_ArithmeticOp();858if (arith != NULL) {859// See if either arg is a known constant860Constant* con = arith->x()->as_Constant();861if (con != NULL) {862*index = arith->y();863} else {864con = arith->y()->as_Constant();865if (con == NULL) return false;866*index = arith->x();867}868long const_value;869// Check for integer multiply870if (arith->op() == Bytecodes::_lmul) {871assert((*index)->type() == longType, "invalid input type");872LongConstant* val = con->type()->as_LongConstant();873assert(val != NULL, "expecting a long constant");874const_value = val->value();875} else {876#ifndef _LP64877if (arith->op() == Bytecodes::_imul) {878assert((*index)->type() == intType, "invalid input type");879IntConstant* val = con->type()->as_IntConstant();880assert(val != NULL, "expecting an int constant");881const_value = val->value();882} else {883return false;884}885#else886return false;887#endif888}889switch (const_value) {890case 1: *log2_scale = 0; return true;891case 2: *log2_scale = 1; return true;892case 4: *log2_scale = 2; return true;893case 8: *log2_scale = 3; return true;894default: return false;895}896}897898// Unknown instruction sequence; don't touch it899return false;900}901902903static bool match(UnsafeRawOp* x,904Instruction** base,905Instruction** index,906int* log2_scale) {907ArithmeticOp* root = x->base()->as_ArithmeticOp();908if (root == NULL) return false;909// Limit ourselves to addition for now910if (root->op() != Bytecodes::_ladd) return false;911912bool match_found = false;913// Try to find shift or scale op914if (match_index_and_scale(root->y(), index, log2_scale)) {915*base = root->x();916match_found = true;917} else if (match_index_and_scale(root->x(), index, log2_scale)) {918*base = root->y();919match_found = true;920} else if (NOT_LP64(root->y()->as_Convert() != NULL) LP64_ONLY(false)) {921// Skipping i2l works only on 32bit because of the implicit l2i that the unsafe performs.922// 64bit needs a real sign-extending conversion.923Convert* convert = root->y()->as_Convert();924if (convert->op() == Bytecodes::_i2l) {925assert(convert->value()->type() == intType, "should be an int");926// pick base and index, setting scale at 1927*base = root->x();928*index = convert->value();929*log2_scale = 0;930match_found = true;931}932}933// The default solution934if (!match_found) {935*base = root->x();936*index = root->y();937*log2_scale = 0;938}939940// AARCH64 cannot handle shifts which are not either 0, or log2 of the type size941#ifdef AARCH64942if (*log2_scale != 0 &&943(1 << *log2_scale) != type2aelembytes(x->basic_type(), true))944return false;945#endif946947// If the value is pinned then it will be always be computed so948// there's no profit to reshaping the expression.949return !root->is_pinned();950}951952953void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {954Instruction* base = NULL;955Instruction* index = NULL;956int log2_scale;957958if (match(x, &base, &index, &log2_scale)) {959x->set_base(base);960x->set_index(index);961x->set_log2_scale(log2_scale);962if (PrintUnsafeOptimization) {963tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",964x->id(), x->base()->id(), x->index()->id(), x->log2_scale());965}966}967}968969void Canonicalizer::do_RoundFP(RoundFP* x) {}970void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }971void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }972void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {}973void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {}974void Canonicalizer::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {}975void Canonicalizer::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {}976void Canonicalizer::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}977void Canonicalizer::do_ProfileCall(ProfileCall* x) {}978void Canonicalizer::do_ProfileReturnType(ProfileReturnType* x) {}979void Canonicalizer::do_ProfileInvoke(ProfileInvoke* x) {}980void Canonicalizer::do_RuntimeCall(RuntimeCall* x) {}981void Canonicalizer::do_RangeCheckPredicate(RangeCheckPredicate* x) {}982#ifdef ASSERT983void Canonicalizer::do_Assert(Assert* x) {}984#endif985void Canonicalizer::do_MemBar(MemBar* x) {}986987988