Path: blob/master/src/hotspot/share/c1/c1_Canonicalizer.cpp
40931 views
/*1* Copyright (c) 1999, 2021, 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;80default : break;81}82}8384if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {85// do constant folding for selected operations86switch (x->type()->tag()) {87case intTag:88{ jint a = x->x()->type()->as_IntConstant()->value();89jint b = x->y()->type()->as_IntConstant()->value();90switch (x->op()) {91case Bytecodes::_iadd: set_constant(a + b); return;92case Bytecodes::_isub: set_constant(a - b); return;93case Bytecodes::_imul: set_constant(a * b); return;94case Bytecodes::_idiv:95if (b != 0) {96if (a == min_jint && b == -1) {97set_constant(min_jint);98} else {99set_constant(a / b);100}101return;102}103break;104case Bytecodes::_irem:105if (b != 0) {106if (a == min_jint && b == -1) {107set_constant(0);108} else {109set_constant(a % b);110}111return;112}113break;114case Bytecodes::_iand: set_constant(a & b); return;115case Bytecodes::_ior : set_constant(a | b); return;116case Bytecodes::_ixor: set_constant(a ^ b); return;117default : break;118}119}120break;121case longTag:122{ jlong a = x->x()->type()->as_LongConstant()->value();123jlong b = x->y()->type()->as_LongConstant()->value();124switch (x->op()) {125case Bytecodes::_ladd: set_constant(a + b); return;126case Bytecodes::_lsub: set_constant(a - b); return;127case Bytecodes::_lmul: set_constant(a * b); return;128case Bytecodes::_ldiv:129if (b != 0) {130set_constant(SharedRuntime::ldiv(b, a));131return;132}133break;134case Bytecodes::_lrem:135if (b != 0) {136set_constant(SharedRuntime::lrem(b, a));137return;138}139break;140case Bytecodes::_land: set_constant(a & b); return;141case Bytecodes::_lor : set_constant(a | b); return;142case Bytecodes::_lxor: set_constant(a ^ b); return;143default : break;144}145}146break;147default:148// other cases not implemented (must be extremely careful with floats & doubles!)149break;150}151}152// make sure constant is on the right side, if any153move_const_to_right(x);154155if (x->y()->type()->is_constant()) {156// do constant folding for selected operations157switch (x->type()->tag()) {158case intTag:159if (x->y()->type()->as_IntConstant()->value() == 0) {160switch (x->op()) {161case Bytecodes::_iadd: set_canonical(x->x()); return;162case Bytecodes::_isub: set_canonical(x->x()); return;163case Bytecodes::_imul: set_constant(0); return;164// Note: for div and rem, make sure that C semantics165// corresponds to Java semantics!166case Bytecodes::_iand: set_constant(0); return;167case Bytecodes::_ior : set_canonical(x->x()); return;168default : break;169}170}171break;172case longTag:173if (x->y()->type()->as_LongConstant()->value() == (jlong)0) {174switch (x->op()) {175case Bytecodes::_ladd: set_canonical(x->x()); return;176case Bytecodes::_lsub: set_canonical(x->x()); return;177case Bytecodes::_lmul: set_constant((jlong)0); return;178// Note: for div and rem, make sure that C semantics179// corresponds to Java semantics!180case Bytecodes::_land: set_constant((jlong)0); return;181case Bytecodes::_lor : set_canonical(x->x()); return;182default : break;183}184}185break;186default:187break;188}189}190}191192193void Canonicalizer::do_Phi (Phi* x) {}194void Canonicalizer::do_Constant (Constant* x) {}195void Canonicalizer::do_Local (Local* x) {}196void Canonicalizer::do_LoadField (LoadField* x) {}197198// checks if v is in the block that is currently processed by199// GraphBuilder. This is the only block that has not BlockEnd yet.200static bool in_current_block(Value v) {201int max_distance = 4;202while (max_distance > 0 && v != NULL && v->as_BlockEnd() == NULL) {203v = v->next();204max_distance--;205}206return v == NULL;207}208209void Canonicalizer::do_StoreField (StoreField* x) {210// If a value is going to be stored into a field or array some of211// the conversions emitted by javac are unneeded because the fields212// are packed to their natural size.213Convert* conv = x->value()->as_Convert();214if (conv) {215Value value = NULL;216BasicType type = x->field()->type()->basic_type();217switch (conv->op()) {218case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;219case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;220case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;221default : break;222}223// limit this optimization to current block224if (value != NULL && in_current_block(conv)) {225set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(),226x->state_before(), x->needs_patching()));227return;228}229}230231}232233void Canonicalizer::do_ArrayLength (ArrayLength* x) {234NewArray* na;235Constant* ct;236LoadField* lf;237238if ((na = x->array()->as_NewArray()) != NULL) {239// New arrays might have the known length.240// Do not use the Constant itself, but create a new Constant241// with same value Otherwise a Constant is live over multiple242// blocks without being registered in a state array.243Constant* length;244NewMultiArray* nma;245if (na->length() != NULL &&246(length = na->length()->as_Constant()) != NULL) {247assert(length->type()->as_IntConstant() != NULL, "array length must be integer");248set_constant(length->type()->as_IntConstant()->value());249} else if ((nma = x->array()->as_NewMultiArray()) != NULL &&250(length = nma->dims()->at(0)->as_Constant()) != NULL) {251assert(length->type()->as_IntConstant() != NULL, "array length must be integer");252set_constant(length->type()->as_IntConstant()->value());253}254255} else if ((ct = x->array()->as_Constant()) != NULL) {256// Constant arrays have constant lengths.257ArrayConstant* cnst = ct->type()->as_ArrayConstant();258if (cnst != NULL) {259set_constant(cnst->value()->length());260}261262} else if ((lf = x->array()->as_LoadField()) != NULL) {263ciField* field = lf->field();264if (field->is_static_constant()) {265// Constant field loads are usually folded during parsing.266// But it doesn't happen with PatchALot, ScavengeRootsInCode < 2, or when267// holder class is being initialized during parsing (for static fields).268ciObject* c = field->constant_value().as_object();269if (!c->is_null_object()) {270set_constant(c->as_array()->length());271}272}273}274}275276void Canonicalizer::do_LoadIndexed (LoadIndexed* x) {277StableArrayConstant* array = x->array()->type()->as_StableArrayConstant();278IntConstant* index = x->index()->type()->as_IntConstant();279280assert(array == NULL || FoldStableValues, "not enabled");281282// Constant fold loads from stable arrays.283if (!x->mismatched() && array != NULL && index != NULL) {284jint idx = index->value();285if (idx < 0 || idx >= array->value()->length()) {286// Leave the load as is. The range check will handle it.287return;288}289290ciConstant field_val = array->value()->element_value(idx);291if (!field_val.is_null_or_zero()) {292jint dimension = array->dimension();293assert(dimension <= array->value()->array_type()->dimension(), "inconsistent info");294ValueType* value = NULL;295if (dimension > 1) {296// Preserve information about the dimension for the element.297assert(field_val.as_object()->is_array(), "not an array");298value = new StableArrayConstant(field_val.as_object()->as_array(), dimension - 1);299} else {300assert(dimension == 1, "sanity");301value = as_ValueType(field_val);302}303set_canonical(new Constant(value));304}305}306}307308void Canonicalizer::do_StoreIndexed (StoreIndexed* x) {309// If a value is going to be stored into a field or array some of310// the conversions emitted by javac are unneeded because the fields311// are packed to their natural size.312Convert* conv = x->value()->as_Convert();313if (conv) {314Value value = NULL;315BasicType type = x->elt_type();316switch (conv->op()) {317case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break;318case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break;319case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break;320default : break;321}322// limit this optimization to current block323if (value != NULL && in_current_block(conv)) {324set_canonical(new StoreIndexed(x->array(), x->index(), x->length(),325x->elt_type(), value, x->state_before(),326x->check_boolean()));327return;328}329}330}331332333void Canonicalizer::do_NegateOp(NegateOp* x) {334ValueType* t = x->x()->type();335if (t->is_constant()) {336switch (t->tag()) {337case intTag : set_constant(-t->as_IntConstant ()->value()); return;338case longTag : set_constant(-t->as_LongConstant ()->value()); return;339case floatTag : set_constant(-t->as_FloatConstant ()->value()); return;340case doubleTag: set_constant(-t->as_DoubleConstant()->value()); return;341default : ShouldNotReachHere();342}343}344}345346347void Canonicalizer::do_ArithmeticOp (ArithmeticOp* x) { do_Op2(x); }348349350void Canonicalizer::do_ShiftOp (ShiftOp* x) {351ValueType* t = x->x()->type();352ValueType* t2 = x->y()->type();353if (t->is_constant()) {354switch (t->tag()) {355case intTag : if (t->as_IntConstant()->value() == 0) { set_constant(0); return; } break;356case longTag : if (t->as_LongConstant()->value() == (jlong)0) { set_constant(jlong_cast(0)); return; } break;357default : ShouldNotReachHere();358}359if (t2->is_constant()) {360if (t->tag() == intTag) {361jint value = t->as_IntConstant()->value();362jint shift = t2->as_IntConstant()->value();363switch (x->op()) {364case Bytecodes::_ishl: set_constant(java_shift_left(value, shift)); return;365case Bytecodes::_ishr: set_constant(java_shift_right(value, shift)); return;366case Bytecodes::_iushr: set_constant(java_shift_right_unsigned(value, shift)); return;367default: break;368}369} else if (t->tag() == longTag) {370jlong value = t->as_LongConstant()->value();371jint shift = t2->as_IntConstant()->value();372switch (x->op()) {373case Bytecodes::_lshl: set_constant(java_shift_left(value, shift)); return;374case Bytecodes::_lshr: set_constant(java_shift_right(value, shift)); return;375case Bytecodes::_lushr: set_constant(java_shift_right_unsigned(value, shift)); return;376default: break;377}378}379}380}381if (t2->is_constant()) {382switch (t2->tag()) {383case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;384case longTag : if (t2->as_LongConstant()->value() == (jlong)0) set_canonical(x->x()); return;385default : ShouldNotReachHere(); return;386}387}388}389390391void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }392void Canonicalizer::do_CompareOp (CompareOp* x) {393if (x->x() == x->y()) {394switch (x->x()->type()->tag()) {395case longTag: set_constant(0); break;396case floatTag: {397FloatConstant* fc = x->x()->type()->as_FloatConstant();398if (fc) {399if (g_isnan(fc->value())) {400set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);401} else {402set_constant(0);403}404}405break;406}407case doubleTag: {408DoubleConstant* dc = x->x()->type()->as_DoubleConstant();409if (dc) {410if (g_isnan(dc->value())) {411set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);412} else {413set_constant(0);414}415}416break;417}418default:419break;420}421} else if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) {422switch (x->x()->type()->tag()) {423case longTag: {424jlong vx = x->x()->type()->as_LongConstant()->value();425jlong vy = x->y()->type()->as_LongConstant()->value();426if (vx == vy)427set_constant(0);428else if (vx < vy)429set_constant(-1);430else431set_constant(1);432break;433}434435case floatTag: {436float vx = x->x()->type()->as_FloatConstant()->value();437float vy = x->y()->type()->as_FloatConstant()->value();438if (g_isnan(vx) || g_isnan(vy))439set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);440else if (vx == vy)441set_constant(0);442else if (vx < vy)443set_constant(-1);444else445set_constant(1);446break;447}448449case doubleTag: {450double vx = x->x()->type()->as_DoubleConstant()->value();451double vy = x->y()->type()->as_DoubleConstant()->value();452if (g_isnan(vx) || g_isnan(vy))453set_constant(x->op() == Bytecodes::_dcmpl ? -1 : 1);454else if (vx == vy)455set_constant(0);456else if (vx < vy)457set_constant(-1);458else459set_constant(1);460break;461}462463default:464break;465}466}467}468469470void Canonicalizer::do_IfOp(IfOp* x) {471// Caution: do not use do_Op2(x) here for now since472// we map the condition to the op for now!473move_const_to_right(x);474}475476477void Canonicalizer::do_Intrinsic (Intrinsic* x) {478switch (x->id()) {479case vmIntrinsics::_floatToRawIntBits : {480FloatConstant* c = x->argument_at(0)->type()->as_FloatConstant();481if (c != NULL) {482JavaValue v;483v.set_jfloat(c->value());484set_constant(v.get_jint());485}486break;487}488case vmIntrinsics::_intBitsToFloat : {489IntConstant* c = x->argument_at(0)->type()->as_IntConstant();490if (c != NULL) {491JavaValue v;492v.set_jint(c->value());493set_constant(v.get_jfloat());494}495break;496}497case vmIntrinsics::_doubleToRawLongBits : {498DoubleConstant* c = x->argument_at(0)->type()->as_DoubleConstant();499if (c != NULL) {500JavaValue v;501v.set_jdouble(c->value());502set_constant(v.get_jlong());503}504break;505}506case vmIntrinsics::_longBitsToDouble : {507LongConstant* c = x->argument_at(0)->type()->as_LongConstant();508if (c != NULL) {509JavaValue v;510v.set_jlong(c->value());511set_constant(v.get_jdouble());512}513break;514}515case vmIntrinsics::_isInstance : {516assert(x->number_of_arguments() == 2, "wrong type");517518InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();519if (c != NULL && !c->value()->is_null_object()) {520// ciInstance::java_mirror_type() returns non-NULL only for Java mirrors521ciType* t = c->value()->java_mirror_type();522if (t->is_klass()) {523// substitute cls.isInstance(obj) of a constant Class into524// an InstantOf instruction525InstanceOf* i = new InstanceOf(t->as_klass(), x->argument_at(1), x->state_before());526set_canonical(i);527// and try to canonicalize even further528do_InstanceOf(i);529} else {530assert(t->is_primitive_type(), "should be a primitive type");531// cls.isInstance(obj) always returns false for primitive classes532set_constant(0);533}534}535break;536}537case vmIntrinsics::_isPrimitive : {538assert(x->number_of_arguments() == 1, "wrong type");539540// Class.isPrimitive is known on constant classes:541InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();542if (c != NULL && !c->value()->is_null_object()) {543ciType* t = c->value()->java_mirror_type();544set_constant(t->is_primitive_type());545}546break;547}548case vmIntrinsics::_getModifiers: {549assert(x->number_of_arguments() == 1, "wrong type");550551// Optimize for Foo.class.getModifier()552InstanceConstant* c = x->argument_at(0)->type()->as_InstanceConstant();553if (c != NULL && !c->value()->is_null_object()) {554ciType* t = c->value()->java_mirror_type();555if (t->is_klass()) {556set_constant(t->as_klass()->modifier_flags());557} else {558assert(t->is_primitive_type(), "should be a primitive type");559set_constant(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);560}561}562break;563}564default:565break;566}567}568569void Canonicalizer::do_Convert (Convert* x) {570if (x->value()->type()->is_constant()) {571switch (x->op()) {572case Bytecodes::_i2b: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 24) >> 24)); break;573case Bytecodes::_i2s: set_constant((int)((x->value()->type()->as_IntConstant()->value() << 16) >> 16)); break;574case Bytecodes::_i2c: set_constant((int)(x->value()->type()->as_IntConstant()->value() & ((1<<16)-1))); break;575case Bytecodes::_i2l: set_constant((jlong)(x->value()->type()->as_IntConstant()->value())); break;576case Bytecodes::_i2f: set_constant((float)(x->value()->type()->as_IntConstant()->value())); break;577case Bytecodes::_i2d: set_constant((double)(x->value()->type()->as_IntConstant()->value())); break;578case Bytecodes::_l2i: set_constant((int)(x->value()->type()->as_LongConstant()->value())); break;579case Bytecodes::_l2f: set_constant(SharedRuntime::l2f(x->value()->type()->as_LongConstant()->value())); break;580case Bytecodes::_l2d: set_constant(SharedRuntime::l2d(x->value()->type()->as_LongConstant()->value())); break;581case Bytecodes::_f2d: set_constant((double)(x->value()->type()->as_FloatConstant()->value())); break;582case Bytecodes::_f2i: set_constant(SharedRuntime::f2i(x->value()->type()->as_FloatConstant()->value())); break;583case Bytecodes::_f2l: set_constant(SharedRuntime::f2l(x->value()->type()->as_FloatConstant()->value())); break;584case Bytecodes::_d2f: set_constant((float)(x->value()->type()->as_DoubleConstant()->value())); break;585case Bytecodes::_d2i: set_constant(SharedRuntime::d2i(x->value()->type()->as_DoubleConstant()->value())); break;586case Bytecodes::_d2l: set_constant(SharedRuntime::d2l(x->value()->type()->as_DoubleConstant()->value())); break;587default:588ShouldNotReachHere();589}590}591592Value value = x->value();593BasicType type = T_ILLEGAL;594LoadField* lf = value->as_LoadField();595if (lf) {596type = lf->field_type();597} else {598LoadIndexed* li = value->as_LoadIndexed();599if (li) {600type = li->elt_type();601} else {602Convert* conv = value->as_Convert();603if (conv) {604switch (conv->op()) {605case Bytecodes::_i2b: type = T_BYTE; break;606case Bytecodes::_i2s: type = T_SHORT; break;607case Bytecodes::_i2c: type = T_CHAR; break;608default : break;609}610}611}612}613if (type != T_ILLEGAL) {614switch (x->op()) {615case Bytecodes::_i2b: if (type == T_BYTE) set_canonical(x->value()); break;616case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) set_canonical(x->value()); break;617case Bytecodes::_i2c: if (type == T_CHAR) set_canonical(x->value()); break;618default : break;619}620} else {621Op2* op2 = x->value()->as_Op2();622if (op2 && op2->op() == Bytecodes::_iand && op2->y()->type()->is_constant()) {623jint safebits = 0;624jint mask = op2->y()->type()->as_IntConstant()->value();625switch (x->op()) {626case Bytecodes::_i2b: safebits = 0x7f; break;627case Bytecodes::_i2s: safebits = 0x7fff; break;628case Bytecodes::_i2c: safebits = 0xffff; break;629default : break;630}631// When casting a masked integer to a smaller signed type, if632// the mask doesn't include the sign bit the cast isn't needed.633if (safebits && (mask & ~safebits) == 0) {634set_canonical(x->value());635}636}637}638639}640641void Canonicalizer::do_NullCheck (NullCheck* x) {642if (x->obj()->as_NewArray() != NULL || x->obj()->as_NewInstance() != NULL) {643set_canonical(x->obj());644} else {645Constant* con = x->obj()->as_Constant();646if (con) {647ObjectType* c = con->type()->as_ObjectType();648if (c && c->is_loaded()) {649ObjectConstant* oc = c->as_ObjectConstant();650if (!oc || !oc->value()->is_null_object()) {651set_canonical(con);652}653}654}655}656}657658void Canonicalizer::do_TypeCast (TypeCast* x) {}659void Canonicalizer::do_Invoke (Invoke* x) {}660void Canonicalizer::do_NewInstance (NewInstance* x) {}661void Canonicalizer::do_NewTypeArray (NewTypeArray* x) {}662void Canonicalizer::do_NewObjectArray (NewObjectArray* x) {}663void Canonicalizer::do_NewMultiArray (NewMultiArray* x) {}664void Canonicalizer::do_CheckCast (CheckCast* x) {665if (x->klass()->is_loaded()) {666Value obj = x->obj();667ciType* klass = obj->exact_type();668if (klass == NULL) {669klass = obj->declared_type();670}671if (klass != NULL && klass->is_loaded()) {672bool is_interface = klass->is_instance_klass() &&673klass->as_instance_klass()->is_interface();674// Interface casts can't be statically optimized away since verifier doesn't675// enforce interface types in bytecode.676if (!is_interface && klass->is_subtype_of(x->klass())) {677set_canonical(obj);678return;679}680}681// checkcast of null returns null682if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {683set_canonical(obj);684}685}686}687void Canonicalizer::do_InstanceOf (InstanceOf* x) {688if (x->klass()->is_loaded()) {689Value obj = x->obj();690ciType* exact = obj->exact_type();691if (exact != NULL && exact->is_loaded() && (obj->as_NewInstance() || obj->as_NewArray())) {692set_constant(exact->is_subtype_of(x->klass()) ? 1 : 0);693return;694}695// instanceof null returns false696if (obj->as_Constant() && obj->type()->as_ObjectType()->constant_value()->is_null_object()) {697set_constant(0);698}699}700701}702void Canonicalizer::do_MonitorEnter (MonitorEnter* x) {}703void Canonicalizer::do_MonitorExit (MonitorExit* x) {}704void Canonicalizer::do_BlockBegin (BlockBegin* x) {}705void Canonicalizer::do_Goto (Goto* x) {}706707708static bool is_true(jlong x, If::Condition cond, jlong y) {709switch (cond) {710case If::eql: return x == y;711case If::neq: return x != y;712case If::lss: return x < y;713case If::leq: return x <= y;714case If::gtr: return x > y;715case If::geq: return x >= y;716default:717ShouldNotReachHere();718return false;719}720}721722static bool is_safepoint(BlockEnd* x, BlockBegin* sux) {723// An Instruction with multiple successors, x, is replaced by a Goto724// to a single successor, sux. Is a safepoint check needed = was the725// instruction being replaced a safepoint and the single remaining726// successor a back branch?727return x->is_safepoint() && (sux->bci() < x->state_before()->bci());728}729730void Canonicalizer::do_If(If* x) {731// move const to right732if (x->x()->type()->is_constant()) x->swap_operands();733// simplify734const Value l = x->x(); ValueType* lt = l->type();735const Value r = x->y(); ValueType* rt = r->type();736737if (l == r && !lt->is_float_kind()) {738// pattern: If (a cond a) => simplify to Goto739BlockBegin* sux = NULL;740switch (x->cond()) {741case If::eql: sux = x->sux_for(true); break;742case If::neq: sux = x->sux_for(false); break;743case If::lss: sux = x->sux_for(false); break;744case If::leq: sux = x->sux_for(true); break;745case If::gtr: sux = x->sux_for(false); break;746case If::geq: sux = x->sux_for(true); break;747default: ShouldNotReachHere();748}749// If is a safepoint then the debug information should come from the state_before of the If.750set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));751return;752}753754if (lt->is_constant() && rt->is_constant()) {755if (x->x()->as_Constant() != NULL) {756// pattern: If (lc cond rc) => simplify to: Goto757BlockBegin* sux = x->x()->as_Constant()->compare(x->cond(), x->y(),758x->sux_for(true),759x->sux_for(false));760if (sux != NULL) {761// If is a safepoint then the debug information should come from the state_before of the If.762set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));763}764}765} else if (rt->as_IntConstant() != NULL) {766// pattern: If (l cond rc) => investigate further767const jint rc = rt->as_IntConstant()->value();768if (l->as_CompareOp() != NULL) {769// pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto770CompareOp* cmp = l->as_CompareOp();771bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl;772BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b773BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b774BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b775BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered776// Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are777// equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either778// lss_sux or gtr_sux.779if (lss_sux == eql_sux && eql_sux == gtr_sux) {780// all successors identical => simplify to: Goto781set_canonical(new Goto(lss_sux, x->state_before(), x->is_safepoint()));782} else {783// two successors differ and two successors are the same => simplify to: If (x cmp y)784// determine new condition & successors785If::Condition cond = If::eql;786BlockBegin* tsux = NULL;787BlockBegin* fsux = NULL;788if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; }789else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; }790else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; }791else { ShouldNotReachHere(); }792If* canon = new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint());793if (cmp->x() == cmp->y()) {794do_If(canon);795} else {796if (compilation()->profile_branches() || compilation()->count_backedges()) {797// TODO: If profiling, leave floating point comparisons unoptimized.798// We currently do not support profiling of the unordered case.799switch(cmp->op()) {800case Bytecodes::_fcmpl: case Bytecodes::_fcmpg:801case Bytecodes::_dcmpl: case Bytecodes::_dcmpg:802set_canonical(x);803return;804default:805break;806}807}808set_bci(cmp->state_before()->bci());809set_canonical(canon);810}811}812}813} else if (rt == objectNull &&814(l->as_NewInstance() || l->as_NewArray() ||815(l->as_Local() && l->as_Local()->is_receiver()))) {816if (x->cond() == Instruction::eql) {817BlockBegin* sux = x->fsux();818set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));819} else {820assert(x->cond() == Instruction::neq, "only other valid case");821BlockBegin* sux = x->tsux();822set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));823}824}825}826827828void Canonicalizer::do_TableSwitch(TableSwitch* x) {829if (x->tag()->type()->is_constant()) {830int v = x->tag()->type()->as_IntConstant()->value();831BlockBegin* sux = x->default_sux();832if (v >= x->lo_key() && v <= x->hi_key()) {833sux = x->sux_at(v - x->lo_key());834}835set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));836}837}838839840void Canonicalizer::do_LookupSwitch(LookupSwitch* x) {841if (x->tag()->type()->is_constant()) {842int v = x->tag()->type()->as_IntConstant()->value();843BlockBegin* sux = x->default_sux();844for (int i = 0; i < x->length(); i++) {845if (v == x->key_at(i)) {846sux = x->sux_at(i);847}848}849set_canonical(new Goto(sux, x->state_before(), is_safepoint(x, sux)));850}851}852853854void Canonicalizer::do_Return (Return* x) {}855void Canonicalizer::do_Throw (Throw* x) {}856void Canonicalizer::do_Base (Base* x) {}857void Canonicalizer::do_OsrEntry (OsrEntry* x) {}858void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}859860static bool match_index_and_scale(Instruction* instr,861Instruction** index,862int* log2_scale) {863// Skip conversion ops. This works only on 32bit because of the implicit l2i that the864// unsafe performs.865#ifndef _LP64866Convert* convert = instr->as_Convert();867if (convert != NULL && convert->op() == Bytecodes::_i2l) {868assert(convert->value()->type() == intType, "invalid input type");869instr = convert->value();870}871#endif872873ShiftOp* shift = instr->as_ShiftOp();874if (shift != NULL) {875if (shift->op() == Bytecodes::_lshl) {876assert(shift->x()->type() == longType, "invalid input type");877} else {878#ifndef _LP64879if (shift->op() == Bytecodes::_ishl) {880assert(shift->x()->type() == intType, "invalid input type");881} else {882return false;883}884#else885return false;886#endif887}888889890// Constant shift value?891Constant* con = shift->y()->as_Constant();892if (con == NULL) return false;893// Well-known type and value?894IntConstant* val = con->type()->as_IntConstant();895assert(val != NULL, "Should be an int constant");896897*index = shift->x();898int tmp_scale = val->value();899if (tmp_scale >= 0 && tmp_scale < 4) {900*log2_scale = tmp_scale;901return true;902} else {903return false;904}905}906907ArithmeticOp* arith = instr->as_ArithmeticOp();908if (arith != NULL) {909// See if either arg is a known constant910Constant* con = arith->x()->as_Constant();911if (con != NULL) {912*index = arith->y();913} else {914con = arith->y()->as_Constant();915if (con == NULL) return false;916*index = arith->x();917}918long const_value;919// Check for integer multiply920if (arith->op() == Bytecodes::_lmul) {921assert((*index)->type() == longType, "invalid input type");922LongConstant* val = con->type()->as_LongConstant();923assert(val != NULL, "expecting a long constant");924const_value = val->value();925} else {926#ifndef _LP64927if (arith->op() == Bytecodes::_imul) {928assert((*index)->type() == intType, "invalid input type");929IntConstant* val = con->type()->as_IntConstant();930assert(val != NULL, "expecting an int constant");931const_value = val->value();932} else {933return false;934}935#else936return false;937#endif938}939switch (const_value) {940case 1: *log2_scale = 0; return true;941case 2: *log2_scale = 1; return true;942case 4: *log2_scale = 2; return true;943case 8: *log2_scale = 3; return true;944default: return false;945}946}947948// Unknown instruction sequence; don't touch it949return false;950}951952953static bool match(UnsafeRawOp* x,954Instruction** base,955Instruction** index,956int* log2_scale) {957ArithmeticOp* root = x->base()->as_ArithmeticOp();958if (root == NULL) return false;959// Limit ourselves to addition for now960if (root->op() != Bytecodes::_ladd) return false;961962bool match_found = false;963// Try to find shift or scale op964if (match_index_and_scale(root->y(), index, log2_scale)) {965*base = root->x();966match_found = true;967} else if (match_index_and_scale(root->x(), index, log2_scale)) {968*base = root->y();969match_found = true;970} else if (NOT_LP64(root->y()->as_Convert() != NULL) LP64_ONLY(false)) {971// Skipping i2l works only on 32bit because of the implicit l2i that the unsafe performs.972// 64bit needs a real sign-extending conversion.973Convert* convert = root->y()->as_Convert();974if (convert->op() == Bytecodes::_i2l) {975assert(convert->value()->type() == intType, "should be an int");976// pick base and index, setting scale at 1977*base = root->x();978*index = convert->value();979*log2_scale = 0;980match_found = true;981}982}983// The default solution984if (!match_found) {985*base = root->x();986*index = root->y();987*log2_scale = 0;988}989990// If the value is pinned then it will be always be computed so991// there's no profit to reshaping the expression.992return !root->is_pinned();993}994995996void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {997Instruction* base = NULL;998Instruction* index = NULL;999int log2_scale;10001001if (match(x, &base, &index, &log2_scale)) {1002x->set_base(base);1003x->set_index(index);1004x->set_log2_scale(log2_scale);1005if (PrintUnsafeOptimization) {1006tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",1007x->id(), x->base()->id(), x->index()->id(), x->log2_scale());1008}1009}1010}10111012void Canonicalizer::do_RoundFP(RoundFP* x) {}1013void Canonicalizer::do_UnsafeGetRaw(UnsafeGetRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }1014void Canonicalizer::do_UnsafePutRaw(UnsafePutRaw* x) { if (OptimizeUnsafes) do_UnsafeRawOp(x); }1015void Canonicalizer::do_UnsafeGetObject(UnsafeGetObject* x) {}1016void Canonicalizer::do_UnsafePutObject(UnsafePutObject* x) {}1017void Canonicalizer::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {}1018void Canonicalizer::do_ProfileCall(ProfileCall* x) {}1019void Canonicalizer::do_ProfileReturnType(ProfileReturnType* x) {}1020void Canonicalizer::do_ProfileInvoke(ProfileInvoke* x) {}1021void Canonicalizer::do_RuntimeCall(RuntimeCall* x) {}1022void Canonicalizer::do_RangeCheckPredicate(RangeCheckPredicate* x) {}1023#ifdef ASSERT1024void Canonicalizer::do_Assert(Assert* x) {}1025#endif1026void Canonicalizer::do_MemBar(MemBar* x) {}102710281029