Path: blob/master/src/hotspot/share/opto/convertnode.cpp
40930 views
/*1* Copyright (c) 2014, 2019, 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 "opto/addnode.hpp"26#include "opto/castnode.hpp"27#include "opto/convertnode.hpp"28#include "opto/matcher.hpp"29#include "opto/phaseX.hpp"30#include "opto/subnode.hpp"31#include "runtime/sharedRuntime.hpp"3233//=============================================================================34//------------------------------Identity---------------------------------------35Node* Conv2BNode::Identity(PhaseGVN* phase) {36const Type *t = phase->type( in(1) );37if( t == Type::TOP ) return in(1);38if( t == TypeInt::ZERO ) return in(1);39if( t == TypeInt::ONE ) return in(1);40if( t == TypeInt::BOOL ) return in(1);41return this;42}4344//------------------------------Value------------------------------------------45const Type* Conv2BNode::Value(PhaseGVN* phase) const {46const Type *t = phase->type( in(1) );47if( t == Type::TOP ) return Type::TOP;48if( t == TypeInt::ZERO ) return TypeInt::ZERO;49if( t == TypePtr::NULL_PTR ) return TypeInt::ZERO;50const TypePtr *tp = t->isa_ptr();51if( tp != NULL ) {52if( tp->ptr() == TypePtr::AnyNull ) return Type::TOP;53if( tp->ptr() == TypePtr::Constant) return TypeInt::ONE;54if (tp->ptr() == TypePtr::NotNull) return TypeInt::ONE;55return TypeInt::BOOL;56}57if (t->base() != Type::Int) return TypeInt::BOOL;58const TypeInt *ti = t->is_int();59if( ti->_hi < 0 || ti->_lo > 0 ) return TypeInt::ONE;60return TypeInt::BOOL;61}626364// The conversions operations are all Alpha sorted. Please keep it that way!65//=============================================================================66//------------------------------Value------------------------------------------67const Type* ConvD2FNode::Value(PhaseGVN* phase) const {68const Type *t = phase->type( in(1) );69if( t == Type::TOP ) return Type::TOP;70if( t == Type::DOUBLE ) return Type::FLOAT;71const TypeD *td = t->is_double_constant();72return TypeF::make( (float)td->getd() );73}7475//------------------------------Ideal------------------------------------------76// If we see pattern ConvF2D SomeDoubleOp ConvD2F, do operation as float.77Node *ConvD2FNode::Ideal(PhaseGVN *phase, bool can_reshape) {78if ( in(1)->Opcode() == Op_SqrtD ) {79Node* sqrtd = in(1);80if ( sqrtd->in(1)->Opcode() == Op_ConvF2D ) {81if ( Matcher::match_rule_supported(Op_SqrtF) ) {82Node* convf2d = sqrtd->in(1);83return new SqrtFNode(phase->C, sqrtd->in(0), convf2d->in(1));84}85}86}87return NULL;88}8990//------------------------------Identity---------------------------------------91// Float's can be converted to doubles with no loss of bits. Hence92// converting a float to a double and back to a float is a NOP.93Node* ConvD2FNode::Identity(PhaseGVN* phase) {94return (in(1)->Opcode() == Op_ConvF2D) ? in(1)->in(1) : this;95}9697//=============================================================================98//------------------------------Value------------------------------------------99const Type* ConvD2INode::Value(PhaseGVN* phase) const {100const Type *t = phase->type( in(1) );101if( t == Type::TOP ) return Type::TOP;102if( t == Type::DOUBLE ) return TypeInt::INT;103const TypeD *td = t->is_double_constant();104return TypeInt::make( SharedRuntime::d2i( td->getd() ) );105}106107//------------------------------Ideal------------------------------------------108// If converting to an int type, skip any rounding nodes109Node *ConvD2INode::Ideal(PhaseGVN *phase, bool can_reshape) {110if( in(1)->Opcode() == Op_RoundDouble )111set_req(1,in(1)->in(1));112return NULL;113}114115//------------------------------Identity---------------------------------------116// Int's can be converted to doubles with no loss of bits. Hence117// converting an integer to a double and back to an integer is a NOP.118Node* ConvD2INode::Identity(PhaseGVN* phase) {119return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this;120}121122//=============================================================================123//------------------------------Value------------------------------------------124const Type* ConvD2LNode::Value(PhaseGVN* phase) const {125const Type *t = phase->type( in(1) );126if( t == Type::TOP ) return Type::TOP;127if( t == Type::DOUBLE ) return TypeLong::LONG;128const TypeD *td = t->is_double_constant();129return TypeLong::make( SharedRuntime::d2l( td->getd() ) );130}131132//------------------------------Identity---------------------------------------133Node* ConvD2LNode::Identity(PhaseGVN* phase) {134// Remove ConvD2L->ConvL2D->ConvD2L sequences.135if( in(1) ->Opcode() == Op_ConvL2D &&136in(1)->in(1)->Opcode() == Op_ConvD2L )137return in(1)->in(1);138return this;139}140141//------------------------------Ideal------------------------------------------142// If converting to an int type, skip any rounding nodes143Node *ConvD2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {144if( in(1)->Opcode() == Op_RoundDouble )145set_req(1,in(1)->in(1));146return NULL;147}148149//=============================================================================150//------------------------------Value------------------------------------------151const Type* ConvF2DNode::Value(PhaseGVN* phase) const {152const Type *t = phase->type( in(1) );153if( t == Type::TOP ) return Type::TOP;154if( t == Type::FLOAT ) return Type::DOUBLE;155const TypeF *tf = t->is_float_constant();156return TypeD::make( (double)tf->getf() );157}158159//=============================================================================160//------------------------------Value------------------------------------------161const Type* ConvF2INode::Value(PhaseGVN* phase) const {162const Type *t = phase->type( in(1) );163if( t == Type::TOP ) return Type::TOP;164if( t == Type::FLOAT ) return TypeInt::INT;165const TypeF *tf = t->is_float_constant();166return TypeInt::make( SharedRuntime::f2i( tf->getf() ) );167}168169//------------------------------Identity---------------------------------------170Node* ConvF2INode::Identity(PhaseGVN* phase) {171// Remove ConvF2I->ConvI2F->ConvF2I sequences.172if( in(1) ->Opcode() == Op_ConvI2F &&173in(1)->in(1)->Opcode() == Op_ConvF2I )174return in(1)->in(1);175return this;176}177178//------------------------------Ideal------------------------------------------179// If converting to an int type, skip any rounding nodes180Node *ConvF2INode::Ideal(PhaseGVN *phase, bool can_reshape) {181if( in(1)->Opcode() == Op_RoundFloat )182set_req(1,in(1)->in(1));183return NULL;184}185186//=============================================================================187//------------------------------Value------------------------------------------188const Type* ConvF2LNode::Value(PhaseGVN* phase) const {189const Type *t = phase->type( in(1) );190if( t == Type::TOP ) return Type::TOP;191if( t == Type::FLOAT ) return TypeLong::LONG;192const TypeF *tf = t->is_float_constant();193return TypeLong::make( SharedRuntime::f2l( tf->getf() ) );194}195196//------------------------------Identity---------------------------------------197Node* ConvF2LNode::Identity(PhaseGVN* phase) {198// Remove ConvF2L->ConvL2F->ConvF2L sequences.199if( in(1) ->Opcode() == Op_ConvL2F &&200in(1)->in(1)->Opcode() == Op_ConvF2L )201return in(1)->in(1);202return this;203}204205//------------------------------Ideal------------------------------------------206// If converting to an int type, skip any rounding nodes207Node *ConvF2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {208if( in(1)->Opcode() == Op_RoundFloat )209set_req(1,in(1)->in(1));210return NULL;211}212213//=============================================================================214//------------------------------Value------------------------------------------215const Type* ConvI2DNode::Value(PhaseGVN* phase) const {216const Type *t = phase->type( in(1) );217if( t == Type::TOP ) return Type::TOP;218const TypeInt *ti = t->is_int();219if( ti->is_con() ) return TypeD::make( (double)ti->get_con() );220return bottom_type();221}222223//=============================================================================224//------------------------------Value------------------------------------------225const Type* ConvI2FNode::Value(PhaseGVN* phase) const {226const Type *t = phase->type( in(1) );227if( t == Type::TOP ) return Type::TOP;228const TypeInt *ti = t->is_int();229if( ti->is_con() ) return TypeF::make( (float)ti->get_con() );230return bottom_type();231}232233//------------------------------Identity---------------------------------------234Node* ConvI2FNode::Identity(PhaseGVN* phase) {235// Remove ConvI2F->ConvF2I->ConvI2F sequences.236if( in(1) ->Opcode() == Op_ConvF2I &&237in(1)->in(1)->Opcode() == Op_ConvI2F )238return in(1)->in(1);239return this;240}241242//=============================================================================243//------------------------------Value------------------------------------------244const Type* ConvI2LNode::Value(PhaseGVN* phase) const {245const Type *t = phase->type( in(1) );246if( t == Type::TOP ) return Type::TOP;247const TypeInt *ti = t->is_int();248const Type* tl = TypeLong::make(ti->_lo, ti->_hi, ti->_widen);249// Join my declared type against my incoming type.250tl = tl->filter(_type);251return tl;252}253254static inline bool long_ranges_overlap(jlong lo1, jlong hi1,255jlong lo2, jlong hi2) {256// Two ranges overlap iff one range's low point falls in the other range.257return (lo2 <= lo1 && lo1 <= hi2) || (lo1 <= lo2 && lo2 <= hi1);258}259260#ifdef _LP64261// If there is an existing ConvI2L node with the given parent and type, return262// it. Otherwise, create and return a new one. Both reusing existing ConvI2L263// nodes and postponing the idealization of new ones are needed to avoid an264// explosion of recursive Ideal() calls when compiling long AddI chains.265static Node* find_or_make_convI2L(PhaseIterGVN* igvn, Node* parent,266const TypeLong* type) {267Node* n = new ConvI2LNode(parent, type);268Node* existing = igvn->hash_find_insert(n);269if (existing != NULL) {270n->destruct(igvn);271return existing;272}273return igvn->register_new_node_with_optimizer(n);274}275#endif276277bool Compile::push_thru_add(PhaseGVN* phase, Node* z, const TypeInteger* tz, const TypeInteger*& rx, const TypeInteger*& ry,278BasicType bt) {279int op = z->Opcode();280if (op == Op_AddI || op == Op_SubI) {281Node* x = z->in(1);282Node* y = z->in(2);283assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal");284if (phase->type(x) == Type::TOP) {285return false;286}287if (phase->type(y) == Type::TOP) {288return false;289}290const TypeInt* tx = phase->type(x)->is_int();291const TypeInt* ty = phase->type(y)->is_int();292293jlong xlo = tx->is_int()->_lo;294jlong xhi = tx->is_int()->_hi;295jlong ylo = ty->is_int()->_lo;296jlong yhi = ty->is_int()->_hi;297jlong zlo = tz->lo_as_long();298jlong zhi = tz->hi_as_long();299jlong vbit = CONST64(1) << BitsPerInt;300int widen = MAX2(tx->_widen, ty->_widen);301if (op == Op_SubI) {302jlong ylo0 = ylo;303ylo = -yhi;304yhi = -ylo0;305}306// See if x+y can cause positive overflow into z+2**32307if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo+vbit, zhi+vbit)) {308return false;309}310// See if x+y can cause negative overflow into z-2**32311if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo-vbit, zhi-vbit)) {312return false;313}314// Now it's always safe to assume x+y does not overflow.315// This is true even if some pairs x,y might cause overflow, as long316// as that overflow value cannot fall into [zlo,zhi].317318// Confident that the arithmetic is "as if infinite precision",319// we can now use z's range to put constraints on those of x and y.320// The "natural" range of x [xlo,xhi] can perhaps be narrowed to a321// more "restricted" range by intersecting [xlo,xhi] with the322// range obtained by subtracting y's range from the asserted range323// of the I2L conversion. Here's the interval arithmetic algebra:324// x == z-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo]325// => x in [zlo-yhi, zhi-ylo]326// => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi]327// => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo]328jlong rxlo = MAX2(xlo, zlo - yhi);329jlong rxhi = MIN2(xhi, zhi - ylo);330// And similarly, x changing place with y:331jlong rylo = MAX2(ylo, zlo - xhi);332jlong ryhi = MIN2(yhi, zhi - xlo);333if (rxlo > rxhi || rylo > ryhi) {334return false; // x or y is dying; don't mess w/ it335}336if (op == Op_SubI) {337jlong rylo0 = rylo;338rylo = -ryhi;339ryhi = -rylo0;340}341assert(rxlo == (int)rxlo && rxhi == (int)rxhi, "x should not overflow");342assert(rylo == (int)rylo && ryhi == (int)ryhi, "y should not overflow");343rx = TypeInteger::make(rxlo, rxhi, widen, bt);344ry = TypeInteger::make(rylo, ryhi, widen, bt);345return true;346}347return false;348}349350351//------------------------------Ideal------------------------------------------352Node *ConvI2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {353PhaseIterGVN *igvn = phase->is_IterGVN();354const TypeLong* this_type = this->type()->is_long();355Node* this_changed = NULL;356357if (igvn != NULL) {358// Do NOT remove this node's type assertion until no more loop ops can happen.359if (phase->C->post_loop_opts_phase()) {360const TypeInt* in_type = phase->type(in(1))->isa_int();361if (in_type != NULL && this_type != NULL &&362(in_type->_lo != this_type->_lo ||363in_type->_hi != this_type->_hi)) {364// Although this WORSENS the type, it increases GVN opportunities,365// because I2L nodes with the same input will common up, regardless366// of slightly differing type assertions. Such slight differences367// arise routinely as a result of loop unrolling, so this is a368// post-unrolling graph cleanup. Choose a type which depends only369// on my input. (Exception: Keep a range assertion of >=0 or <0.)370jlong lo1 = this_type->_lo;371jlong hi1 = this_type->_hi;372int w1 = this_type->_widen;373if (lo1 != (jint)lo1 ||374hi1 != (jint)hi1 ||375lo1 > hi1) {376// Overflow leads to wraparound, wraparound leads to range saturation.377lo1 = min_jint; hi1 = max_jint;378} else if (lo1 >= 0) {379// Keep a range assertion of >=0.380lo1 = 0; hi1 = max_jint;381} else if (hi1 < 0) {382// Keep a range assertion of <0.383lo1 = min_jint; hi1 = -1;384} else {385lo1 = min_jint; hi1 = max_jint;386}387const TypeLong* wtype = TypeLong::make(MAX2((jlong)in_type->_lo, lo1),388MIN2((jlong)in_type->_hi, hi1),389MAX2((int)in_type->_widen, w1));390if (wtype != type()) {391set_type(wtype);392// Note: this_type still has old type value, for the logic below.393this_changed = this;394}395}396} else {397phase->C->record_for_post_loop_opts_igvn(this);398}399}400#ifdef _LP64401// Convert ConvI2L(AddI(x, y)) to AddL(ConvI2L(x), ConvI2L(y))402// but only if x and y have subranges that cannot cause 32-bit overflow,403// under the assumption that x+y is in my own subrange this->type().404405// This assumption is based on a constraint (i.e., type assertion)406// established in Parse::array_addressing or perhaps elsewhere.407// This constraint has been adjoined to the "natural" type of408// the incoming argument in(0). We know (because of runtime409// checks) - that the result value I2L(x+y) is in the joined range.410// Hence we can restrict the incoming terms (x, y) to values such411// that their sum also lands in that range.412413// This optimization is useful only on 64-bit systems, where we hope414// the addition will end up subsumed in an addressing mode.415// It is necessary to do this when optimizing an unrolled array416// copy loop such as x[i++] = y[i++].417418// On 32-bit systems, it's better to perform as much 32-bit math as419// possible before the I2L conversion, because 32-bit math is cheaper.420// There's no common reason to "leak" a constant offset through the I2L.421// Addressing arithmetic will not absorb it as part of a 64-bit AddL.422423Node* z = in(1);424const TypeInteger* rx = NULL;425const TypeInteger* ry = NULL;426if (Compile::push_thru_add(phase, z, this_type, rx, ry, T_LONG)) {427if (igvn == NULL) {428// Postpone this optimization to iterative GVN, where we can handle deep429// AddI chains without an exponential number of recursive Ideal() calls.430phase->record_for_igvn(this);431return this_changed;432}433int op = z->Opcode();434Node* x = z->in(1);435Node* y = z->in(2);436437Node* cx = find_or_make_convI2L(igvn, x, rx->is_long());438Node* cy = find_or_make_convI2L(igvn, y, ry->is_long());439switch (op) {440case Op_AddI: return new AddLNode(cx, cy);441case Op_SubI: return new SubLNode(cx, cy);442default: ShouldNotReachHere();443}444}445#endif //_LP64446447return this_changed;448}449450//=============================================================================451//------------------------------Value------------------------------------------452const Type* ConvL2DNode::Value(PhaseGVN* phase) const {453const Type *t = phase->type( in(1) );454if( t == Type::TOP ) return Type::TOP;455const TypeLong *tl = t->is_long();456if( tl->is_con() ) return TypeD::make( (double)tl->get_con() );457return bottom_type();458}459460//=============================================================================461//------------------------------Value------------------------------------------462const Type* ConvL2FNode::Value(PhaseGVN* phase) const {463const Type *t = phase->type( in(1) );464if( t == Type::TOP ) return Type::TOP;465const TypeLong *tl = t->is_long();466if( tl->is_con() ) return TypeF::make( (float)tl->get_con() );467return bottom_type();468}469470//=============================================================================471//----------------------------Identity-----------------------------------------472Node* ConvL2INode::Identity(PhaseGVN* phase) {473// Convert L2I(I2L(x)) => x474if (in(1)->Opcode() == Op_ConvI2L) return in(1)->in(1);475return this;476}477478//------------------------------Value------------------------------------------479const Type* ConvL2INode::Value(PhaseGVN* phase) const {480const Type *t = phase->type( in(1) );481if( t == Type::TOP ) return Type::TOP;482const TypeLong *tl = t->is_long();483const TypeInt* ti = TypeInt::INT;484if (tl->is_con()) {485// Easy case.486ti = TypeInt::make((jint)tl->get_con());487} else if (tl->_lo >= min_jint && tl->_hi <= max_jint) {488ti = TypeInt::make((jint)tl->_lo, (jint)tl->_hi, tl->_widen);489}490return ti->filter(_type);491}492493//------------------------------Ideal------------------------------------------494// Return a node which is more "ideal" than the current node.495// Blow off prior masking to int496Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) {497Node *andl = in(1);498uint andl_op = andl->Opcode();499if( andl_op == Op_AndL ) {500// Blow off prior masking to int501if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) {502set_req_X(1,andl->in(1), phase);503return this;504}505}506507// Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y))508// This replaces an 'AddL' with an 'AddI'.509if( andl_op == Op_AddL ) {510// Don't do this for nodes which have more than one user since511// we'll end up computing the long add anyway.512if (andl->outcnt() > 1) return NULL;513514Node* x = andl->in(1);515Node* y = andl->in(2);516assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" );517if (phase->type(x) == Type::TOP) return NULL;518if (phase->type(y) == Type::TOP) return NULL;519Node *add1 = phase->transform(new ConvL2INode(x));520Node *add2 = phase->transform(new ConvL2INode(y));521return new AddINode(add1,add2);522}523524// Disable optimization: LoadL->ConvL2I ==> LoadI.525// It causes problems (sizes of Load and Store nodes do not match)526// in objects initialization code and Escape Analysis.527return NULL;528}529530531532//=============================================================================533//------------------------------Identity---------------------------------------534// Remove redundant roundings535Node* RoundFloatNode::Identity(PhaseGVN* phase) {536assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");537// Do not round constants538if (phase->type(in(1))->base() == Type::FloatCon) return in(1);539int op = in(1)->Opcode();540// Redundant rounding541if( op == Op_RoundFloat ) return in(1);542// Already rounded543if( op == Op_Parm ) return in(1);544if( op == Op_LoadF ) return in(1);545return this;546}547548//------------------------------Value------------------------------------------549const Type* RoundFloatNode::Value(PhaseGVN* phase) const {550return phase->type( in(1) );551}552553//=============================================================================554//------------------------------Identity---------------------------------------555// Remove redundant roundings. Incoming arguments are already rounded.556Node* RoundDoubleNode::Identity(PhaseGVN* phase) {557assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");558// Do not round constants559if (phase->type(in(1))->base() == Type::DoubleCon) return in(1);560int op = in(1)->Opcode();561// Redundant rounding562if( op == Op_RoundDouble ) return in(1);563// Already rounded564if( op == Op_Parm ) return in(1);565if( op == Op_LoadD ) return in(1);566if( op == Op_ConvF2D ) return in(1);567if( op == Op_ConvI2D ) return in(1);568return this;569}570571//------------------------------Value------------------------------------------572const Type* RoundDoubleNode::Value(PhaseGVN* phase) const {573return phase->type( in(1) );574}575576//=============================================================================577RoundDoubleModeNode* RoundDoubleModeNode::make(PhaseGVN& gvn, Node* arg, RoundDoubleModeNode::RoundingMode rmode) {578ConINode* rm = gvn.intcon(rmode);579return new RoundDoubleModeNode(arg, (Node *)rm);580}581582//------------------------------Identity---------------------------------------583// Remove redundant roundings.584Node* RoundDoubleModeNode::Identity(PhaseGVN* phase) {585int op = in(1)->Opcode();586// Redundant rounding e.g. floor(ceil(n)) -> ceil(n)587if(op == Op_RoundDoubleMode) return in(1);588return this;589}590const Type* RoundDoubleModeNode::Value(PhaseGVN* phase) const {591return Type::DOUBLE;592}593//=============================================================================594595596