Path: blob/master/src/hotspot/share/opto/convertnode.cpp
64440 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 this;113}114return NULL;115}116117//------------------------------Identity---------------------------------------118// Int's can be converted to doubles with no loss of bits. Hence119// converting an integer to a double and back to an integer is a NOP.120Node* ConvD2INode::Identity(PhaseGVN* phase) {121return (in(1)->Opcode() == Op_ConvI2D) ? in(1)->in(1) : this;122}123124//=============================================================================125//------------------------------Value------------------------------------------126const Type* ConvD2LNode::Value(PhaseGVN* phase) const {127const Type *t = phase->type( in(1) );128if( t == Type::TOP ) return Type::TOP;129if( t == Type::DOUBLE ) return TypeLong::LONG;130const TypeD *td = t->is_double_constant();131return TypeLong::make( SharedRuntime::d2l( td->getd() ) );132}133134//------------------------------Identity---------------------------------------135Node* ConvD2LNode::Identity(PhaseGVN* phase) {136// Remove ConvD2L->ConvL2D->ConvD2L sequences.137if( in(1) ->Opcode() == Op_ConvL2D &&138in(1)->in(1)->Opcode() == Op_ConvD2L )139return in(1)->in(1);140return this;141}142143//------------------------------Ideal------------------------------------------144// If converting to an int type, skip any rounding nodes145Node *ConvD2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {146if (in(1)->Opcode() == Op_RoundDouble) {147set_req(1, in(1)->in(1));148return this;149}150return NULL;151}152153//=============================================================================154//------------------------------Value------------------------------------------155const Type* ConvF2DNode::Value(PhaseGVN* phase) const {156const Type *t = phase->type( in(1) );157if( t == Type::TOP ) return Type::TOP;158if( t == Type::FLOAT ) return Type::DOUBLE;159const TypeF *tf = t->is_float_constant();160return TypeD::make( (double)tf->getf() );161}162163//=============================================================================164//------------------------------Value------------------------------------------165const Type* ConvF2INode::Value(PhaseGVN* phase) const {166const Type *t = phase->type( in(1) );167if( t == Type::TOP ) return Type::TOP;168if( t == Type::FLOAT ) return TypeInt::INT;169const TypeF *tf = t->is_float_constant();170return TypeInt::make( SharedRuntime::f2i( tf->getf() ) );171}172173//------------------------------Identity---------------------------------------174Node* ConvF2INode::Identity(PhaseGVN* phase) {175// Remove ConvF2I->ConvI2F->ConvF2I sequences.176if( in(1) ->Opcode() == Op_ConvI2F &&177in(1)->in(1)->Opcode() == Op_ConvF2I )178return in(1)->in(1);179return this;180}181182//------------------------------Ideal------------------------------------------183// If converting to an int type, skip any rounding nodes184Node *ConvF2INode::Ideal(PhaseGVN *phase, bool can_reshape) {185if (in(1)->Opcode() == Op_RoundFloat) {186set_req(1, in(1)->in(1));187return this;188}189return NULL;190}191192//=============================================================================193//------------------------------Value------------------------------------------194const Type* ConvF2LNode::Value(PhaseGVN* phase) const {195const Type *t = phase->type( in(1) );196if( t == Type::TOP ) return Type::TOP;197if( t == Type::FLOAT ) return TypeLong::LONG;198const TypeF *tf = t->is_float_constant();199return TypeLong::make( SharedRuntime::f2l( tf->getf() ) );200}201202//------------------------------Identity---------------------------------------203Node* ConvF2LNode::Identity(PhaseGVN* phase) {204// Remove ConvF2L->ConvL2F->ConvF2L sequences.205if( in(1) ->Opcode() == Op_ConvL2F &&206in(1)->in(1)->Opcode() == Op_ConvF2L )207return in(1)->in(1);208return this;209}210211//------------------------------Ideal------------------------------------------212// If converting to an int type, skip any rounding nodes213Node *ConvF2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {214if (in(1)->Opcode() == Op_RoundFloat) {215set_req(1, in(1)->in(1));216return this;217}218return NULL;219}220221//=============================================================================222//------------------------------Value------------------------------------------223const Type* ConvI2DNode::Value(PhaseGVN* phase) const {224const Type *t = phase->type( in(1) );225if( t == Type::TOP ) return Type::TOP;226const TypeInt *ti = t->is_int();227if( ti->is_con() ) return TypeD::make( (double)ti->get_con() );228return bottom_type();229}230231//=============================================================================232//------------------------------Value------------------------------------------233const Type* ConvI2FNode::Value(PhaseGVN* phase) const {234const Type *t = phase->type( in(1) );235if( t == Type::TOP ) return Type::TOP;236const TypeInt *ti = t->is_int();237if( ti->is_con() ) return TypeF::make( (float)ti->get_con() );238return bottom_type();239}240241//------------------------------Identity---------------------------------------242Node* ConvI2FNode::Identity(PhaseGVN* phase) {243// Remove ConvI2F->ConvF2I->ConvI2F sequences.244if( in(1) ->Opcode() == Op_ConvF2I &&245in(1)->in(1)->Opcode() == Op_ConvI2F )246return in(1)->in(1);247return this;248}249250//=============================================================================251//------------------------------Value------------------------------------------252const Type* ConvI2LNode::Value(PhaseGVN* phase) const {253const Type *t = phase->type( in(1) );254if( t == Type::TOP ) return Type::TOP;255const TypeInt *ti = t->is_int();256const Type* tl = TypeLong::make(ti->_lo, ti->_hi, ti->_widen);257// Join my declared type against my incoming type.258tl = tl->filter(_type);259return tl;260}261262static inline bool long_ranges_overlap(jlong lo1, jlong hi1,263jlong lo2, jlong hi2) {264// Two ranges overlap iff one range's low point falls in the other range.265return (lo2 <= lo1 && lo1 <= hi2) || (lo1 <= lo2 && lo2 <= hi1);266}267268#ifdef _LP64269// If there is an existing ConvI2L node with the given parent and type, return270// it. Otherwise, create and return a new one. Both reusing existing ConvI2L271// nodes and postponing the idealization of new ones are needed to avoid an272// explosion of recursive Ideal() calls when compiling long AddI chains.273static Node* find_or_make_convI2L(PhaseIterGVN* igvn, Node* parent,274const TypeLong* type) {275Node* n = new ConvI2LNode(parent, type);276Node* existing = igvn->hash_find_insert(n);277if (existing != NULL) {278n->destruct(igvn);279return existing;280}281return igvn->register_new_node_with_optimizer(n);282}283#endif284285bool Compile::push_thru_add(PhaseGVN* phase, Node* z, const TypeInteger* tz, const TypeInteger*& rx, const TypeInteger*& ry,286BasicType bt) {287int op = z->Opcode();288if (op == Op_AddI || op == Op_SubI) {289Node* x = z->in(1);290Node* y = z->in(2);291assert (x != z && y != z, "dead loop in ConvI2LNode::Ideal");292if (phase->type(x) == Type::TOP) {293return false;294}295if (phase->type(y) == Type::TOP) {296return false;297}298const TypeInt* tx = phase->type(x)->is_int();299const TypeInt* ty = phase->type(y)->is_int();300301jlong xlo = tx->is_int()->_lo;302jlong xhi = tx->is_int()->_hi;303jlong ylo = ty->is_int()->_lo;304jlong yhi = ty->is_int()->_hi;305jlong zlo = tz->lo_as_long();306jlong zhi = tz->hi_as_long();307jlong vbit = CONST64(1) << BitsPerInt;308int widen = MAX2(tx->_widen, ty->_widen);309if (op == Op_SubI) {310jlong ylo0 = ylo;311ylo = -yhi;312yhi = -ylo0;313}314// See if x+y can cause positive overflow into z+2**32315if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo+vbit, zhi+vbit)) {316return false;317}318// See if x+y can cause negative overflow into z-2**32319if (long_ranges_overlap(xlo+ylo, xhi+yhi, zlo-vbit, zhi-vbit)) {320return false;321}322// Now it's always safe to assume x+y does not overflow.323// This is true even if some pairs x,y might cause overflow, as long324// as that overflow value cannot fall into [zlo,zhi].325326// Confident that the arithmetic is "as if infinite precision",327// we can now use z's range to put constraints on those of x and y.328// The "natural" range of x [xlo,xhi] can perhaps be narrowed to a329// more "restricted" range by intersecting [xlo,xhi] with the330// range obtained by subtracting y's range from the asserted range331// of the I2L conversion. Here's the interval arithmetic algebra:332// x == z-y == [zlo,zhi]-[ylo,yhi] == [zlo,zhi]+[-yhi,-ylo]333// => x in [zlo-yhi, zhi-ylo]334// => x in [zlo-yhi, zhi-ylo] INTERSECT [xlo,xhi]335// => x in [xlo MAX zlo-yhi, xhi MIN zhi-ylo]336jlong rxlo = MAX2(xlo, zlo - yhi);337jlong rxhi = MIN2(xhi, zhi - ylo);338// And similarly, x changing place with y:339jlong rylo = MAX2(ylo, zlo - xhi);340jlong ryhi = MIN2(yhi, zhi - xlo);341if (rxlo > rxhi || rylo > ryhi) {342return false; // x or y is dying; don't mess w/ it343}344if (op == Op_SubI) {345jlong rylo0 = rylo;346rylo = -ryhi;347ryhi = -rylo0;348}349assert(rxlo == (int)rxlo && rxhi == (int)rxhi, "x should not overflow");350assert(rylo == (int)rylo && ryhi == (int)ryhi, "y should not overflow");351rx = TypeInteger::make(rxlo, rxhi, widen, bt);352ry = TypeInteger::make(rylo, ryhi, widen, bt);353return true;354}355return false;356}357358359//------------------------------Ideal------------------------------------------360Node *ConvI2LNode::Ideal(PhaseGVN *phase, bool can_reshape) {361PhaseIterGVN *igvn = phase->is_IterGVN();362const TypeLong* this_type = this->type()->is_long();363Node* this_changed = NULL;364365if (igvn != NULL) {366// Do NOT remove this node's type assertion until no more loop ops can happen.367if (phase->C->post_loop_opts_phase()) {368const TypeInt* in_type = phase->type(in(1))->isa_int();369if (in_type != NULL && this_type != NULL &&370(in_type->_lo != this_type->_lo ||371in_type->_hi != this_type->_hi)) {372// Although this WORSENS the type, it increases GVN opportunities,373// because I2L nodes with the same input will common up, regardless374// of slightly differing type assertions. Such slight differences375// arise routinely as a result of loop unrolling, so this is a376// post-unrolling graph cleanup. Choose a type which depends only377// on my input. (Exception: Keep a range assertion of >=0 or <0.)378jlong lo1 = this_type->_lo;379jlong hi1 = this_type->_hi;380int w1 = this_type->_widen;381if (lo1 != (jint)lo1 ||382hi1 != (jint)hi1 ||383lo1 > hi1) {384// Overflow leads to wraparound, wraparound leads to range saturation.385lo1 = min_jint; hi1 = max_jint;386} else if (lo1 >= 0) {387// Keep a range assertion of >=0.388lo1 = 0; hi1 = max_jint;389} else if (hi1 < 0) {390// Keep a range assertion of <0.391lo1 = min_jint; hi1 = -1;392} else {393lo1 = min_jint; hi1 = max_jint;394}395const TypeLong* wtype = TypeLong::make(MAX2((jlong)in_type->_lo, lo1),396MIN2((jlong)in_type->_hi, hi1),397MAX2((int)in_type->_widen, w1));398if (wtype != type()) {399set_type(wtype);400// Note: this_type still has old type value, for the logic below.401this_changed = this;402}403}404} else {405phase->C->record_for_post_loop_opts_igvn(this);406}407}408#ifdef _LP64409// Convert ConvI2L(AddI(x, y)) to AddL(ConvI2L(x), ConvI2L(y))410// but only if x and y have subranges that cannot cause 32-bit overflow,411// under the assumption that x+y is in my own subrange this->type().412413// This assumption is based on a constraint (i.e., type assertion)414// established in Parse::array_addressing or perhaps elsewhere.415// This constraint has been adjoined to the "natural" type of416// the incoming argument in(0). We know (because of runtime417// checks) - that the result value I2L(x+y) is in the joined range.418// Hence we can restrict the incoming terms (x, y) to values such419// that their sum also lands in that range.420421// This optimization is useful only on 64-bit systems, where we hope422// the addition will end up subsumed in an addressing mode.423// It is necessary to do this when optimizing an unrolled array424// copy loop such as x[i++] = y[i++].425426// On 32-bit systems, it's better to perform as much 32-bit math as427// possible before the I2L conversion, because 32-bit math is cheaper.428// There's no common reason to "leak" a constant offset through the I2L.429// Addressing arithmetic will not absorb it as part of a 64-bit AddL.430431Node* z = in(1);432const TypeInteger* rx = NULL;433const TypeInteger* ry = NULL;434if (Compile::push_thru_add(phase, z, this_type, rx, ry, T_LONG)) {435if (igvn == NULL) {436// Postpone this optimization to iterative GVN, where we can handle deep437// AddI chains without an exponential number of recursive Ideal() calls.438phase->record_for_igvn(this);439return this_changed;440}441int op = z->Opcode();442Node* x = z->in(1);443Node* y = z->in(2);444445Node* cx = find_or_make_convI2L(igvn, x, rx->is_long());446Node* cy = find_or_make_convI2L(igvn, y, ry->is_long());447switch (op) {448case Op_AddI: return new AddLNode(cx, cy);449case Op_SubI: return new SubLNode(cx, cy);450default: ShouldNotReachHere();451}452}453#endif //_LP64454455return this_changed;456}457458//=============================================================================459//------------------------------Value------------------------------------------460const Type* ConvL2DNode::Value(PhaseGVN* phase) const {461const Type *t = phase->type( in(1) );462if( t == Type::TOP ) return Type::TOP;463const TypeLong *tl = t->is_long();464if( tl->is_con() ) return TypeD::make( (double)tl->get_con() );465return bottom_type();466}467468//=============================================================================469//------------------------------Value------------------------------------------470const Type* ConvL2FNode::Value(PhaseGVN* phase) const {471const Type *t = phase->type( in(1) );472if( t == Type::TOP ) return Type::TOP;473const TypeLong *tl = t->is_long();474if( tl->is_con() ) return TypeF::make( (float)tl->get_con() );475return bottom_type();476}477478//=============================================================================479//----------------------------Identity-----------------------------------------480Node* ConvL2INode::Identity(PhaseGVN* phase) {481// Convert L2I(I2L(x)) => x482if (in(1)->Opcode() == Op_ConvI2L) return in(1)->in(1);483return this;484}485486//------------------------------Value------------------------------------------487const Type* ConvL2INode::Value(PhaseGVN* phase) const {488const Type *t = phase->type( in(1) );489if( t == Type::TOP ) return Type::TOP;490const TypeLong *tl = t->is_long();491const TypeInt* ti = TypeInt::INT;492if (tl->is_con()) {493// Easy case.494ti = TypeInt::make((jint)tl->get_con());495} else if (tl->_lo >= min_jint && tl->_hi <= max_jint) {496ti = TypeInt::make((jint)tl->_lo, (jint)tl->_hi, tl->_widen);497}498return ti->filter(_type);499}500501//------------------------------Ideal------------------------------------------502// Return a node which is more "ideal" than the current node.503// Blow off prior masking to int504Node *ConvL2INode::Ideal(PhaseGVN *phase, bool can_reshape) {505Node *andl = in(1);506uint andl_op = andl->Opcode();507if( andl_op == Op_AndL ) {508// Blow off prior masking to int509if( phase->type(andl->in(2)) == TypeLong::make( 0xFFFFFFFF ) ) {510set_req_X(1,andl->in(1), phase);511return this;512}513}514515// Swap with a prior add: convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y))516// This replaces an 'AddL' with an 'AddI'.517if( andl_op == Op_AddL ) {518// Don't do this for nodes which have more than one user since519// we'll end up computing the long add anyway.520if (andl->outcnt() > 1) return NULL;521522Node* x = andl->in(1);523Node* y = andl->in(2);524assert( x != andl && y != andl, "dead loop in ConvL2INode::Ideal" );525if (phase->type(x) == Type::TOP) return NULL;526if (phase->type(y) == Type::TOP) return NULL;527Node *add1 = phase->transform(new ConvL2INode(x));528Node *add2 = phase->transform(new ConvL2INode(y));529return new AddINode(add1,add2);530}531532// Disable optimization: LoadL->ConvL2I ==> LoadI.533// It causes problems (sizes of Load and Store nodes do not match)534// in objects initialization code and Escape Analysis.535return NULL;536}537538539540//=============================================================================541//------------------------------Identity---------------------------------------542// Remove redundant roundings543Node* RoundFloatNode::Identity(PhaseGVN* phase) {544assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");545// Do not round constants546if (phase->type(in(1))->base() == Type::FloatCon) return in(1);547int op = in(1)->Opcode();548// Redundant rounding549if( op == Op_RoundFloat ) return in(1);550// Already rounded551if( op == Op_Parm ) return in(1);552if( op == Op_LoadF ) return in(1);553return this;554}555556//------------------------------Value------------------------------------------557const Type* RoundFloatNode::Value(PhaseGVN* phase) const {558return phase->type( in(1) );559}560561//=============================================================================562//------------------------------Identity---------------------------------------563// Remove redundant roundings. Incoming arguments are already rounded.564Node* RoundDoubleNode::Identity(PhaseGVN* phase) {565assert(Matcher::strict_fp_requires_explicit_rounding, "should only generate for Intel");566// Do not round constants567if (phase->type(in(1))->base() == Type::DoubleCon) return in(1);568int op = in(1)->Opcode();569// Redundant rounding570if( op == Op_RoundDouble ) return in(1);571// Already rounded572if( op == Op_Parm ) return in(1);573if( op == Op_LoadD ) return in(1);574if( op == Op_ConvF2D ) return in(1);575if( op == Op_ConvI2D ) return in(1);576return this;577}578579//------------------------------Value------------------------------------------580const Type* RoundDoubleNode::Value(PhaseGVN* phase) const {581return phase->type( in(1) );582}583584//=============================================================================585RoundDoubleModeNode* RoundDoubleModeNode::make(PhaseGVN& gvn, Node* arg, RoundDoubleModeNode::RoundingMode rmode) {586ConINode* rm = gvn.intcon(rmode);587return new RoundDoubleModeNode(arg, (Node *)rm);588}589590//------------------------------Identity---------------------------------------591// Remove redundant roundings.592Node* RoundDoubleModeNode::Identity(PhaseGVN* phase) {593int op = in(1)->Opcode();594// Redundant rounding e.g. floor(ceil(n)) -> ceil(n)595if(op == Op_RoundDoubleMode) return in(1);596return this;597}598const Type* RoundDoubleModeNode::Value(PhaseGVN* phase) const {599return Type::DOUBLE;600}601//=============================================================================602603604