duke@435: /* duke@435: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Portions of code courtesy of Clifford Click duke@435: duke@435: // Optimization - Graph Style duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_subnode.cpp.incl" duke@435: #include "math.h" duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: // If right input is a constant 0, return the left input. duke@435: Node *SubNode::Identity( PhaseTransform *phase ) { duke@435: assert(in(1) != this, "Must already have called Value"); duke@435: assert(in(2) != this, "Must already have called Value"); duke@435: duke@435: // Remove double negation duke@435: const Type *zero = add_id(); duke@435: if( phase->type( in(1) )->higher_equal( zero ) && duke@435: in(2)->Opcode() == Opcode() && duke@435: phase->type( in(2)->in(1) )->higher_equal( zero ) ) { duke@435: return in(2)->in(2); duke@435: } duke@435: duke@435: // Convert "(X+Y) - Y" into X duke@435: if( in(1)->Opcode() == Op_AddI ) { duke@435: if( phase->eqv(in(1)->in(2),in(2)) ) duke@435: return in(1)->in(1); duke@435: // Also catch: "(X + Opaque2(Y)) - Y". In this case, 'Y' is a loop-varying duke@435: // trip counter and X is likely to be loop-invariant (that's how O2 Nodes duke@435: // are originally used, although the optimizer sometimes jiggers things). duke@435: // This folding through an O2 removes a loop-exit use of a loop-varying duke@435: // value and generally lowers register pressure in and around the loop. duke@435: if( in(1)->in(2)->Opcode() == Op_Opaque2 && duke@435: phase->eqv(in(1)->in(2)->in(1),in(2)) ) duke@435: return in(1)->in(1); duke@435: } duke@435: duke@435: return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A subtract node differences it's two inputs. duke@435: const Type *SubNode::Value( PhaseTransform *phase ) const { duke@435: const Node* in1 = in(1); duke@435: const Node* in2 = in(2); duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Not correct for SubFnode and AddFNode (must check for infinity) duke@435: // Equal? Subtract is zero duke@435: if (phase->eqv_uncast(in1, in2)) return add_id(); duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: if( t1 == Type::BOTTOM || t2 == Type::BOTTOM ) duke@435: return bottom_type(); duke@435: duke@435: return sub(t1,t2); // Local flavor of type subtraction duke@435: duke@435: } duke@435: duke@435: //============================================================================= duke@435: duke@435: //------------------------------Helper function-------------------------------- duke@435: static bool ok_to_convert(Node* inc, Node* iv) { duke@435: // Do not collapse (x+c0)-y if "+" is a loop increment, because the duke@435: // "-" is loop invariant and collapsing extends the live-range of "x" duke@435: // to overlap with the "+", forcing another register to be used in duke@435: // the loop. duke@435: // This test will be clearer with '&&' (apply DeMorgan's rule) duke@435: // but I like the early cutouts that happen here. duke@435: const PhiNode *phi; duke@435: if( ( !inc->in(1)->is_Phi() || duke@435: !(phi=inc->in(1)->as_Phi()) || duke@435: phi->is_copy() || duke@435: !phi->region()->is_CountedLoop() || duke@435: inc != phi->region()->as_CountedLoop()->incr() ) duke@435: && duke@435: // Do not collapse (x+c0)-iv if "iv" is a loop induction variable, duke@435: // because "x" maybe invariant. duke@435: ( !iv->is_loop_iv() ) duke@435: ) { duke@435: return true; duke@435: } else { duke@435: return false; duke@435: } duke@435: } duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){ duke@435: Node *in1 = in(1); duke@435: Node *in2 = in(2); duke@435: uint op1 = in1->Opcode(); duke@435: uint op2 = in2->Opcode(); duke@435: duke@435: #ifdef ASSERT duke@435: // Check for dead loop duke@435: if( phase->eqv( in1, this ) || phase->eqv( in2, this ) || duke@435: ( op1 == Op_AddI || op1 == Op_SubI ) && duke@435: ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) || duke@435: phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) ) duke@435: assert(false, "dead loop in SubINode::Ideal"); duke@435: #endif duke@435: duke@435: const Type *t2 = phase->type( in2 ); duke@435: if( t2 == Type::TOP ) return NULL; duke@435: // Convert "x-c0" into "x+ -c0". duke@435: if( t2->base() == Type::Int ){ // Might be bottom or top... duke@435: const TypeInt *i = t2->is_int(); duke@435: if( i->is_con() ) duke@435: return new (phase->C, 3) AddINode(in1, phase->intcon(-i->get_con())); duke@435: } duke@435: duke@435: // Convert "(x+c0) - y" into (x-y) + c0" duke@435: // Do not collapse (x+c0)-y if "+" is a loop increment or duke@435: // if "y" is a loop induction variable. duke@435: if( op1 == Op_AddI && ok_to_convert(in1, in2) ) { duke@435: const Type *tadd = phase->type( in1->in(2) ); duke@435: if( tadd->singleton() && tadd != Type::TOP ) { duke@435: Node *sub2 = phase->transform( new (phase->C, 3) SubINode( in1->in(1), in2 )); duke@435: return new (phase->C, 3) AddINode( sub2, in1->in(2) ); duke@435: } duke@435: } duke@435: duke@435: duke@435: // Convert "x - (y+c0)" into "(x-y) - c0" duke@435: // Need the same check as in above optimization but reversed. duke@435: if (op2 == Op_AddI && ok_to_convert(in2, in1)) { duke@435: Node* in21 = in2->in(1); duke@435: Node* in22 = in2->in(2); duke@435: const TypeInt* tcon = phase->type(in22)->isa_int(); duke@435: if (tcon != NULL && tcon->is_con()) { duke@435: Node* sub2 = phase->transform( new (phase->C, 3) SubINode(in1, in21) ); duke@435: Node* neg_c0 = phase->intcon(- tcon->get_con()); duke@435: return new (phase->C, 3) AddINode(sub2, neg_c0); duke@435: } duke@435: } duke@435: duke@435: const Type *t1 = phase->type( in1 ); duke@435: if( t1 == Type::TOP ) return NULL; duke@435: duke@435: #ifdef ASSERT duke@435: // Check for dead loop duke@435: if( ( op2 == Op_AddI || op2 == Op_SubI ) && duke@435: ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) || duke@435: phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) ) duke@435: assert(false, "dead loop in SubINode::Ideal"); duke@435: #endif duke@435: duke@435: // Convert "x - (x+y)" into "-y" duke@435: if( op2 == Op_AddI && duke@435: phase->eqv( in1, in2->in(1) ) ) duke@435: return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(2)); duke@435: // Convert "(x-y) - x" into "-y" duke@435: if( op1 == Op_SubI && duke@435: phase->eqv( in1->in(1), in2 ) ) duke@435: return new (phase->C, 3) SubINode( phase->intcon(0),in1->in(2)); duke@435: // Convert "x - (y+x)" into "-y" duke@435: if( op2 == Op_AddI && duke@435: phase->eqv( in1, in2->in(2) ) ) duke@435: return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(1)); duke@435: duke@435: // Convert "0 - (x-y)" into "y-x" duke@435: if( t1 == TypeInt::ZERO && op2 == Op_SubI ) duke@435: return new (phase->C, 3) SubINode( in2->in(2), in2->in(1) ); duke@435: duke@435: // Convert "0 - (x+con)" into "-con-x" duke@435: jint con; duke@435: if( t1 == TypeInt::ZERO && op2 == Op_AddI && duke@435: (con = in2->in(2)->find_int_con(0)) != 0 ) duke@435: return new (phase->C, 3) SubINode( phase->intcon(-con), in2->in(1) ); duke@435: duke@435: // Convert "(X+A) - (X+B)" into "A - B" duke@435: if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) ) duke@435: return new (phase->C, 3) SubINode( in1->in(2), in2->in(2) ); duke@435: duke@435: // Convert "(A+X) - (B+X)" into "A - B" duke@435: if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) ) duke@435: return new (phase->C, 3) SubINode( in1->in(1), in2->in(1) ); duke@435: duke@435: // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally duke@435: // nicer to optimize than subtract. duke@435: if( op2 == Op_SubI && in2->outcnt() == 1) { duke@435: Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) ); duke@435: return new (phase->C, 3) SubINode( add1, in2->in(1) ); duke@435: } duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------sub-------------------------------------------- duke@435: // A subtract node differences it's two inputs. duke@435: const Type *SubINode::sub( const Type *t1, const Type *t2 ) const { duke@435: const TypeInt *r0 = t1->is_int(); // Handy access duke@435: const TypeInt *r1 = t2->is_int(); duke@435: int32 lo = r0->_lo - r1->_hi; duke@435: int32 hi = r0->_hi - r1->_lo; duke@435: duke@435: // We next check for 32-bit overflow. duke@435: // If that happens, we just assume all integers are possible. duke@435: if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR duke@435: ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND duke@435: (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR duke@435: ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs duke@435: return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen)); duke@435: else // Overflow; assume all integers duke@435: return TypeInt::INT; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: Node *in1 = in(1); duke@435: Node *in2 = in(2); duke@435: uint op1 = in1->Opcode(); duke@435: uint op2 = in2->Opcode(); duke@435: duke@435: #ifdef ASSERT duke@435: // Check for dead loop duke@435: if( phase->eqv( in1, this ) || phase->eqv( in2, this ) || duke@435: ( op1 == Op_AddL || op1 == Op_SubL ) && duke@435: ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) || duke@435: phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) ) duke@435: assert(false, "dead loop in SubLNode::Ideal"); duke@435: #endif duke@435: duke@435: if( phase->type( in2 ) == Type::TOP ) return NULL; duke@435: const TypeLong *i = phase->type( in2 )->isa_long(); duke@435: // Convert "x-c0" into "x+ -c0". duke@435: if( i && // Might be bottom or top... duke@435: i->is_con() ) duke@435: return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con())); duke@435: duke@435: // Convert "(x+c0) - y" into (x-y) + c0" duke@435: // Do not collapse (x+c0)-y if "+" is a loop increment or duke@435: // if "y" is a loop induction variable. duke@435: if( op1 == Op_AddL && ok_to_convert(in1, in2) ) { duke@435: Node *in11 = in1->in(1); duke@435: const Type *tadd = phase->type( in1->in(2) ); duke@435: if( tadd->singleton() && tadd != Type::TOP ) { duke@435: Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 )); duke@435: return new (phase->C, 3) AddLNode( sub2, in1->in(2) ); duke@435: } duke@435: } duke@435: duke@435: // Convert "x - (y+c0)" into "(x-y) - c0" duke@435: // Need the same check as in above optimization but reversed. duke@435: if (op2 == Op_AddL && ok_to_convert(in2, in1)) { duke@435: Node* in21 = in2->in(1); duke@435: Node* in22 = in2->in(2); duke@435: const TypeLong* tcon = phase->type(in22)->isa_long(); duke@435: if (tcon != NULL && tcon->is_con()) { duke@435: Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) ); duke@435: Node* neg_c0 = phase->longcon(- tcon->get_con()); duke@435: return new (phase->C, 3) AddLNode(sub2, neg_c0); duke@435: } duke@435: } duke@435: duke@435: const Type *t1 = phase->type( in1 ); duke@435: if( t1 == Type::TOP ) return NULL; duke@435: duke@435: #ifdef ASSERT duke@435: // Check for dead loop duke@435: if( ( op2 == Op_AddL || op2 == Op_SubL ) && duke@435: ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) || duke@435: phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) ) duke@435: assert(false, "dead loop in SubLNode::Ideal"); duke@435: #endif duke@435: duke@435: // Convert "x - (x+y)" into "-y" duke@435: if( op2 == Op_AddL && duke@435: phase->eqv( in1, in2->in(1) ) ) duke@435: return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2)); duke@435: // Convert "x - (y+x)" into "-y" duke@435: if( op2 == Op_AddL && duke@435: phase->eqv( in1, in2->in(2) ) ) duke@435: return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1)); duke@435: duke@435: // Convert "0 - (x-y)" into "y-x" duke@435: if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL ) duke@435: return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) ); duke@435: duke@435: // Convert "(X+A) - (X+B)" into "A - B" duke@435: if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) ) duke@435: return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) ); duke@435: duke@435: // Convert "(A+X) - (B+X)" into "A - B" duke@435: if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) ) duke@435: return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) ); duke@435: duke@435: // Convert "A-(B-C)" into (A+C)-B" duke@435: if( op2 == Op_SubL && in2->outcnt() == 1) { duke@435: Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) ); duke@435: return new (phase->C, 3) SubLNode( add1, in2->in(1) ); duke@435: } duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------sub-------------------------------------------- duke@435: // A subtract node differences it's two inputs. duke@435: const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const { duke@435: const TypeLong *r0 = t1->is_long(); // Handy access duke@435: const TypeLong *r1 = t2->is_long(); duke@435: jlong lo = r0->_lo - r1->_hi; duke@435: jlong hi = r0->_hi - r1->_lo; duke@435: duke@435: // We next check for 32-bit overflow. duke@435: // If that happens, we just assume all integers are possible. duke@435: if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR duke@435: ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND duke@435: (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR duke@435: ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs duke@435: return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen)); duke@435: else // Overflow; assume all integers duke@435: return TypeLong::LONG; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // A subtract node differences its two inputs. duke@435: const Type *SubFPNode::Value( PhaseTransform *phase ) const { duke@435: const Node* in1 = in(1); duke@435: const Node* in2 = in(2); duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // if both operands are infinity of same sign, the result is NaN; do duke@435: // not replace with zero duke@435: if( (t1->is_finite() && t2->is_finite()) ) { duke@435: if( phase->eqv(in1, in2) ) return add_id(); duke@435: } duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: return sub(t1,t2); // Local flavor of type subtraction duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Convert "x-c0" into "x+ -c0". duke@435: if( t2->base() == Type::FloatCon ) { // Might be bottom or top... duke@435: // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) ); duke@435: } duke@435: duke@435: // Not associative because of boundary conditions (infinity) duke@435: if( IdealizedNumerics && !phase->C->method()->is_strict() ) { duke@435: // Convert "x - (x+y)" into "-y" duke@435: if( in(2)->is_Add() && duke@435: phase->eqv(in(1),in(2)->in(1) ) ) duke@435: return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2)); duke@435: } duke@435: duke@435: // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes duke@435: // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0. duke@435: //if( phase->type(in(1)) == TypeF::ZERO ) duke@435: //return new (phase->C, 2) NegFNode(in(2)); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------sub-------------------------------------------- duke@435: // A subtract node differences its two inputs. duke@435: const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const { duke@435: // no folding if one of operands is infinity or NaN, do not do constant folding duke@435: if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) { duke@435: return TypeF::make( t1->getf() - t2->getf() ); duke@435: } duke@435: else if( g_isnan(t1->getf()) ) { duke@435: return t1; duke@435: } duke@435: else if( g_isnan(t2->getf()) ) { duke@435: return t2; duke@435: } duke@435: else { duke@435: return Type::FLOAT; duke@435: } duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){ duke@435: const Type *t2 = phase->type( in(2) ); duke@435: // Convert "x-c0" into "x+ -c0". duke@435: if( t2->base() == Type::DoubleCon ) { // Might be bottom or top... duke@435: // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) ); duke@435: } duke@435: duke@435: // Not associative because of boundary conditions (infinity) duke@435: if( IdealizedNumerics && !phase->C->method()->is_strict() ) { duke@435: // Convert "x - (x+y)" into "-y" duke@435: if( in(2)->is_Add() && duke@435: phase->eqv(in(1),in(2)->in(1) ) ) duke@435: return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2)); duke@435: } duke@435: duke@435: // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes duke@435: // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0. duke@435: //if( phase->type(in(1)) == TypeD::ZERO ) duke@435: //return new (phase->C, 2) NegDNode(in(2)); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------sub-------------------------------------------- duke@435: // A subtract node differences its two inputs. duke@435: const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const { duke@435: // no folding if one of operands is infinity or NaN, do not do constant folding duke@435: if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) { duke@435: return TypeD::make( t1->getd() - t2->getd() ); duke@435: } duke@435: else if( g_isnan(t1->getd()) ) { duke@435: return t1; duke@435: } duke@435: else if( g_isnan(t2->getd()) ) { duke@435: return t2; duke@435: } duke@435: else { duke@435: return Type::DOUBLE; duke@435: } duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Idealize--------------------------------------- duke@435: // Unlike SubNodes, compare must still flatten return value to the duke@435: // range -1, 0, 1. duke@435: // And optimizations like those for (X + Y) - X fail if overflow happens. duke@435: Node *CmpNode::Identity( PhaseTransform *phase ) { duke@435: return this; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------cmp-------------------------------------------- duke@435: // Simplify a CmpI (compare 2 integers) node, based on local information. duke@435: // If both inputs are constants, compare them. duke@435: const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const { duke@435: const TypeInt *r0 = t1->is_int(); // Handy access duke@435: const TypeInt *r1 = t2->is_int(); duke@435: duke@435: if( r0->_hi < r1->_lo ) // Range is always low? duke@435: return TypeInt::CC_LT; duke@435: else if( r0->_lo > r1->_hi ) // Range is always high? duke@435: return TypeInt::CC_GT; duke@435: duke@435: else if( r0->is_con() && r1->is_con() ) { // comparing constants? duke@435: assert(r0->get_con() == r1->get_con(), "must be equal"); duke@435: return TypeInt::CC_EQ; // Equal results. duke@435: } else if( r0->_hi == r1->_lo ) // Range is never high? duke@435: return TypeInt::CC_LE; duke@435: else if( r0->_lo == r1->_hi ) // Range is never low? duke@435: return TypeInt::CC_GE; duke@435: return TypeInt::CC; // else use worst case results duke@435: } duke@435: duke@435: // Simplify a CmpU (compare 2 integers) node, based on local information. duke@435: // If both inputs are constants, compare them. duke@435: const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const { duke@435: assert(!t1->isa_ptr(), "obsolete usage of CmpU"); duke@435: duke@435: // comparing two unsigned ints duke@435: const TypeInt *r0 = t1->is_int(); // Handy access duke@435: const TypeInt *r1 = t2->is_int(); duke@435: duke@435: // Current installed version duke@435: // Compare ranges for non-overlap duke@435: juint lo0 = r0->_lo; duke@435: juint hi0 = r0->_hi; duke@435: juint lo1 = r1->_lo; duke@435: juint hi1 = r1->_hi; duke@435: duke@435: // If either one has both negative and positive values, duke@435: // it therefore contains both 0 and -1, and since [0..-1] is the duke@435: // full unsigned range, the type must act as an unsigned bottom. duke@435: bool bot0 = ((jint)(lo0 ^ hi0) < 0); duke@435: bool bot1 = ((jint)(lo1 ^ hi1) < 0); duke@435: duke@435: if (bot0 || bot1) { duke@435: // All unsigned values are LE -1 and GE 0. duke@435: if (lo0 == 0 && hi0 == 0) { duke@435: return TypeInt::CC_LE; // 0 <= bot duke@435: } else if (lo1 == 0 && hi1 == 0) { duke@435: return TypeInt::CC_GE; // bot >= 0 duke@435: } duke@435: } else { duke@435: // We can use ranges of the form [lo..hi] if signs are the same. duke@435: assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid"); duke@435: // results are reversed, '-' > '+' for unsigned compare duke@435: if (hi0 < lo1) { duke@435: return TypeInt::CC_LT; // smaller duke@435: } else if (lo0 > hi1) { duke@435: return TypeInt::CC_GT; // greater duke@435: } else if (hi0 == lo1 && lo0 == hi1) { duke@435: return TypeInt::CC_EQ; // Equal results duke@435: } else if (lo0 >= hi1) { duke@435: return TypeInt::CC_GE; duke@435: } else if (hi0 <= lo1) { duke@435: // Check for special case in Hashtable::get. (See below.) duke@435: if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && duke@435: in(1)->Opcode() == Op_ModI && duke@435: in(1)->in(2) == in(2) ) duke@435: return TypeInt::CC_LT; duke@435: return TypeInt::CC_LE; duke@435: } duke@435: } duke@435: // Check for special case in Hashtable::get - the hash index is duke@435: // mod'ed to the table size so the following range check is useless. duke@435: // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have duke@435: // to be positive. duke@435: // (This is a gross hack, since the sub method never duke@435: // looks at the structure of the node in any other case.) duke@435: if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && duke@435: in(1)->Opcode() == Op_ModI && duke@435: in(1)->in(2)->uncast() == in(2)->uncast()) duke@435: return TypeInt::CC_LT; duke@435: return TypeInt::CC; // else use worst case results duke@435: } duke@435: duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) { duke@435: if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) { duke@435: switch (in(1)->Opcode()) { duke@435: case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL duke@435: return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2)); duke@435: case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF duke@435: return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2)); duke@435: case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD duke@435: return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2)); duke@435: //case Op_SubI: duke@435: // If (x - y) cannot overflow, then ((x - y) 0) duke@435: // can be turned into (x y). duke@435: // This is handled (with more general cases) by Ideal_sub_algebra. duke@435: } duke@435: } duke@435: return NULL; // No change duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: // Simplify a CmpL (compare 2 longs ) node, based on local information. duke@435: // If both inputs are constants, compare them. duke@435: const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const { duke@435: const TypeLong *r0 = t1->is_long(); // Handy access duke@435: const TypeLong *r1 = t2->is_long(); duke@435: duke@435: if( r0->_hi < r1->_lo ) // Range is always low? duke@435: return TypeInt::CC_LT; duke@435: else if( r0->_lo > r1->_hi ) // Range is always high? duke@435: return TypeInt::CC_GT; duke@435: duke@435: else if( r0->is_con() && r1->is_con() ) { // comparing constants? duke@435: assert(r0->get_con() == r1->get_con(), "must be equal"); duke@435: return TypeInt::CC_EQ; // Equal results. duke@435: } else if( r0->_hi == r1->_lo ) // Range is never high? duke@435: return TypeInt::CC_LE; duke@435: else if( r0->_lo == r1->_hi ) // Range is never low? duke@435: return TypeInt::CC_GE; duke@435: return TypeInt::CC; // else use worst case results duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------sub-------------------------------------------- duke@435: // Simplify an CmpP (compare 2 pointers) node, based on local information. duke@435: // If both inputs are constants, compare them. duke@435: const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const { duke@435: const TypePtr *r0 = t1->is_ptr(); // Handy access duke@435: const TypePtr *r1 = t2->is_ptr(); duke@435: duke@435: // Undefined inputs makes for an undefined result duke@435: if( TypePtr::above_centerline(r0->_ptr) || duke@435: TypePtr::above_centerline(r1->_ptr) ) duke@435: return Type::TOP; duke@435: duke@435: if (r0 == r1 && r0->singleton()) { duke@435: // Equal pointer constants (klasses, nulls, etc.) duke@435: return TypeInt::CC_EQ; duke@435: } duke@435: duke@435: // See if it is 2 unrelated classes. duke@435: const TypeOopPtr* p0 = r0->isa_oopptr(); duke@435: const TypeOopPtr* p1 = r1->isa_oopptr(); duke@435: if (p0 && p1) { kvn@468: Node* in1 = in(1)->uncast(); kvn@468: Node* in2 = in(2)->uncast(); kvn@468: AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL); kvn@468: AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL); kvn@468: if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) { kvn@468: return TypeInt::CC_GT; // different pointers kvn@468: } duke@435: ciKlass* klass0 = p0->klass(); duke@435: bool xklass0 = p0->klass_is_exact(); duke@435: ciKlass* klass1 = p1->klass(); duke@435: bool xklass1 = p1->klass_is_exact(); duke@435: int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0); duke@435: if (klass0 && klass1 && duke@435: kps != 1 && // both or neither are klass pointers duke@435: !klass0->is_interface() && // do not trust interfaces duke@435: !klass1->is_interface()) { duke@435: // See if neither subclasses the other, or if the class on top duke@435: // is precise. In either of these cases, the compare must fail. duke@435: if (klass0->equals(klass1) || // if types are unequal but klasses are duke@435: !klass0->is_java_klass() || // types not part of Java language? duke@435: !klass1->is_java_klass()) { // types not part of Java language? duke@435: // Do nothing; we know nothing for imprecise types duke@435: } else if (klass0->is_subtype_of(klass1)) { duke@435: // If klass1's type is PRECISE, then we can fail. duke@435: if (xklass1) return TypeInt::CC_GT; duke@435: } else if (klass1->is_subtype_of(klass0)) { duke@435: // If klass0's type is PRECISE, then we can fail. duke@435: if (xklass0) return TypeInt::CC_GT; duke@435: } else { // Neither subtypes the other duke@435: return TypeInt::CC_GT; // ...so always fail duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Known constants can be compared exactly duke@435: // Null can be distinguished from any NotNull pointers duke@435: // Unknown inputs makes an unknown result duke@435: if( r0->singleton() ) { duke@435: intptr_t bits0 = r0->get_con(); duke@435: if( r1->singleton() ) duke@435: return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT; duke@435: return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC; duke@435: } else if( r1->singleton() ) { duke@435: intptr_t bits1 = r1->get_con(); duke@435: return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC; duke@435: } else duke@435: return TypeInt::CC; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: // Check for the case of comparing an unknown klass loaded from the primary duke@435: // super-type array vs a known klass with no subtypes. This amounts to duke@435: // checking to see an unknown klass subtypes a known klass with no subtypes; duke@435: // this only happens on an exact match. We can shorten this test by 1 load. duke@435: Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) { duke@435: // Constant pointer on right? duke@435: const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr(); duke@435: if (t2 == NULL || !t2->klass_is_exact()) duke@435: return NULL; duke@435: // Get the constant klass we are comparing to. duke@435: ciKlass* superklass = t2->klass(); duke@435: duke@435: // Now check for LoadKlass on left. duke@435: Node* ldk1 = in(1); duke@435: if (ldk1->Opcode() != Op_LoadKlass) duke@435: return NULL; duke@435: // Take apart the address of the LoadKlass: duke@435: Node* adr1 = ldk1->in(MemNode::Address); duke@435: intptr_t con2 = 0; duke@435: Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2); duke@435: if (ldk2 == NULL) duke@435: return NULL; duke@435: if (con2 == oopDesc::klass_offset_in_bytes()) { duke@435: // We are inspecting an object's concrete class. duke@435: // Short-circuit the check if the query is abstract. duke@435: if (superklass->is_interface() || duke@435: superklass->is_abstract()) { duke@435: // Make it come out always false: duke@435: this->set_req(2, phase->makecon(TypePtr::NULL_PTR)); duke@435: return this; duke@435: } duke@435: } duke@435: duke@435: // Check for a LoadKlass from primary supertype array. duke@435: // Any nested loadklass from loadklass+con must be from the p.s. array. duke@435: if (ldk2->Opcode() != Op_LoadKlass) duke@435: return NULL; duke@435: duke@435: // Verify that we understand the situation duke@435: if (con2 != (intptr_t) superklass->super_check_offset()) duke@435: return NULL; // Might be element-klass loading from array klass duke@435: duke@435: // If 'superklass' has no subklasses and is not an interface, then we are duke@435: // assured that the only input which will pass the type check is duke@435: // 'superklass' itself. duke@435: // duke@435: // We could be more liberal here, and allow the optimization on interfaces duke@435: // which have a single implementor. This would require us to increase the duke@435: // expressiveness of the add_dependency() mechanism. duke@435: // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now. duke@435: duke@435: // Object arrays must have their base element have no subtypes duke@435: while (superklass->is_obj_array_klass()) { duke@435: ciType* elem = superklass->as_obj_array_klass()->element_type(); duke@435: superklass = elem->as_klass(); duke@435: } duke@435: if (superklass->is_instance_klass()) { duke@435: ciInstanceKlass* ik = superklass->as_instance_klass(); duke@435: if (ik->has_subklass() || ik->is_interface()) return NULL; duke@435: // Add a dependency if there is a chance that a subclass will be added later. duke@435: if (!ik->is_final()) { duke@435: phase->C->dependencies()->assert_leaf_type(ik); duke@435: } duke@435: } duke@435: duke@435: // Bypass the dependent load, and compare directly duke@435: this->set_req(1,ldk2); duke@435: duke@435: return this; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Simplify an CmpF (compare 2 floats ) node, based on local information. duke@435: // If both inputs are constants, compare them. duke@435: const Type *CmpFNode::Value( PhaseTransform *phase ) const { duke@435: const Node* in1 = in(1); duke@435: const Node* in2 = in(2); duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Not constants? Don't know squat - even if they are the same duke@435: // value! If they are NaN's they compare to LT instead of EQ. duke@435: const TypeF *tf1 = t1->isa_float_constant(); duke@435: const TypeF *tf2 = t2->isa_float_constant(); duke@435: if( !tf1 || !tf2 ) return TypeInt::CC; duke@435: duke@435: // This implements the Java bytecode fcmpl, so unordered returns -1. duke@435: if( tf1->is_nan() || tf2->is_nan() ) duke@435: return TypeInt::CC_LT; duke@435: duke@435: if( tf1->_f < tf2->_f ) return TypeInt::CC_LT; duke@435: if( tf1->_f > tf2->_f ) return TypeInt::CC_GT; duke@435: assert( tf1->_f == tf2->_f, "do not understand FP behavior" ); duke@435: return TypeInt::CC_EQ; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Simplify an CmpD (compare 2 doubles ) node, based on local information. duke@435: // If both inputs are constants, compare them. duke@435: const Type *CmpDNode::Value( PhaseTransform *phase ) const { duke@435: const Node* in1 = in(1); duke@435: const Node* in2 = in(2); duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Not constants? Don't know squat - even if they are the same duke@435: // value! If they are NaN's they compare to LT instead of EQ. duke@435: const TypeD *td1 = t1->isa_double_constant(); duke@435: const TypeD *td2 = t2->isa_double_constant(); duke@435: if( !td1 || !td2 ) return TypeInt::CC; duke@435: duke@435: // This implements the Java bytecode dcmpl, so unordered returns -1. duke@435: if( td1->is_nan() || td2->is_nan() ) duke@435: return TypeInt::CC_LT; duke@435: duke@435: if( td1->_d < td2->_d ) return TypeInt::CC_LT; duke@435: if( td1->_d > td2->_d ) return TypeInt::CC_GT; duke@435: assert( td1->_d == td2->_d, "do not understand FP behavior" ); duke@435: return TypeInt::CC_EQ; duke@435: } duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){ duke@435: // Check if we can change this to a CmpF and remove a ConvD2F operation. duke@435: // Change (CMPD (F2D (float)) (ConD value)) duke@435: // To (CMPF (float) (ConF value)) duke@435: // Valid when 'value' does not lose precision as a float. duke@435: // Benefits: eliminates conversion, does not require 24-bit mode duke@435: duke@435: // NaNs prevent commuting operands. This transform works regardless of the duke@435: // order of ConD and ConvF2D inputs by preserving the original order. duke@435: int idx_f2d = 1; // ConvF2D on left side? duke@435: if( in(idx_f2d)->Opcode() != Op_ConvF2D ) duke@435: idx_f2d = 2; // No, swap to check for reversed args duke@435: int idx_con = 3-idx_f2d; // Check for the constant on other input duke@435: duke@435: if( ConvertCmpD2CmpF && duke@435: in(idx_f2d)->Opcode() == Op_ConvF2D && duke@435: in(idx_con)->Opcode() == Op_ConD ) { duke@435: const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant(); duke@435: double t2_value_as_double = t2->_d; duke@435: float t2_value_as_float = (float)t2_value_as_double; duke@435: if( t2_value_as_double == (double)t2_value_as_float ) { duke@435: // Test value can be represented as a float duke@435: // Eliminate the conversion to double and create new comparison duke@435: Node *new_in1 = in(idx_f2d)->in(1); duke@435: Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) ); duke@435: if( idx_f2d != 1 ) { // Must flip args to match original order duke@435: Node *tmp = new_in1; duke@435: new_in1 = new_in2; duke@435: new_in2 = tmp; duke@435: } duke@435: CmpFNode *new_cmp = (Opcode() == Op_CmpD3) duke@435: ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 ) duke@435: : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ; duke@435: return new_cmp; // Changed to CmpFNode duke@435: } duke@435: // Testing value required the precision of a double duke@435: } duke@435: return NULL; // No change duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------cc2logical------------------------------------- duke@435: // Convert a condition code type to a logical type duke@435: const Type *BoolTest::cc2logical( const Type *CC ) const { duke@435: if( CC == Type::TOP ) return Type::TOP; duke@435: if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse duke@435: const TypeInt *ti = CC->is_int(); duke@435: if( ti->is_con() ) { // Only 1 kind of condition codes set? duke@435: // Match low order 2 bits duke@435: int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0; duke@435: if( _test & 4 ) tmp = 1-tmp; // Optionally complement result duke@435: return TypeInt::make(tmp); // Boolean result duke@435: } duke@435: duke@435: if( CC == TypeInt::CC_GE ) { duke@435: if( _test == ge ) return TypeInt::ONE; duke@435: if( _test == lt ) return TypeInt::ZERO; duke@435: } duke@435: if( CC == TypeInt::CC_LE ) { duke@435: if( _test == le ) return TypeInt::ONE; duke@435: if( _test == gt ) return TypeInt::ZERO; duke@435: } duke@435: duke@435: return TypeInt::BOOL; duke@435: } duke@435: duke@435: //------------------------------dump_spec------------------------------------- duke@435: // Print special per-node info duke@435: #ifndef PRODUCT duke@435: void BoolTest::dump_on(outputStream *st) const { duke@435: const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"}; duke@435: st->print(msg[_test]); duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); } duke@435: uint BoolNode::size_of() const { return sizeof(BoolNode); } duke@435: duke@435: //------------------------------operator==------------------------------------- duke@435: uint BoolNode::cmp( const Node &n ) const { duke@435: const BoolNode *b = (const BoolNode *)&n; // Cast up duke@435: return (_test._test == b->_test._test); duke@435: } duke@435: duke@435: //------------------------------clone_cmp-------------------------------------- duke@435: // Clone a compare/bool tree duke@435: static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) { duke@435: Node *ncmp = cmp->clone(); duke@435: ncmp->set_req(1,cmp1); duke@435: ncmp->set_req(2,cmp2); duke@435: ncmp = gvn->transform( ncmp ); duke@435: return new (gvn->C, 2) BoolNode( ncmp, test ); duke@435: } duke@435: duke@435: //-------------------------------make_predicate-------------------------------- duke@435: Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) { duke@435: if (test_value->is_Con()) return test_value; duke@435: if (test_value->is_Bool()) return test_value; duke@435: Compile* C = phase->C; duke@435: if (test_value->is_CMove() && duke@435: test_value->in(CMoveNode::Condition)->is_Bool()) { duke@435: BoolNode* bol = test_value->in(CMoveNode::Condition)->as_Bool(); duke@435: const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse)); duke@435: const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue)); duke@435: if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) { duke@435: return bol; duke@435: } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) { duke@435: return phase->transform( bol->negate(phase) ); duke@435: } duke@435: // Else fall through. The CMove gets in the way of the test. duke@435: // It should be the case that make_predicate(bol->as_int_value()) == bol. duke@435: } duke@435: Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0)); duke@435: cmp = phase->transform(cmp); duke@435: Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne); duke@435: return phase->transform(bol); duke@435: } duke@435: duke@435: //--------------------------------as_int_value--------------------------------- duke@435: Node* BoolNode::as_int_value(PhaseGVN* phase) { duke@435: // Inverse to make_predicate. The CMove probably boils down to a Conv2B. duke@435: Node* cmov = CMoveNode::make(phase->C, NULL, this, duke@435: phase->intcon(0), phase->intcon(1), duke@435: TypeInt::BOOL); duke@435: return phase->transform(cmov); duke@435: } duke@435: duke@435: //----------------------------------negate------------------------------------- duke@435: BoolNode* BoolNode::negate(PhaseGVN* phase) { duke@435: Compile* C = phase->C; duke@435: return new (C, 2) BoolNode(in(1), _test.negate()); duke@435: } duke@435: duke@435: duke@435: //------------------------------Ideal------------------------------------------ duke@435: Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)". duke@435: // This moves the constant to the right. Helps value-numbering. duke@435: Node *cmp = in(1); duke@435: if( !cmp->is_Sub() ) return NULL; duke@435: int cop = cmp->Opcode(); duke@435: if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL; duke@435: Node *cmp1 = cmp->in(1); duke@435: Node *cmp2 = cmp->in(2); duke@435: if( !cmp1 ) return NULL; duke@435: duke@435: // Constant on left? duke@435: Node *con = cmp1; duke@435: uint op2 = cmp2->Opcode(); duke@435: // Move constants to the right of compare's to canonicalize. duke@435: // Do not muck with Opaque1 nodes, as this indicates a loop duke@435: // guard that cannot change shape. duke@435: if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 && duke@435: // Because of NaN's, CmpD and CmpF are not commutative duke@435: cop != Op_CmpD && cop != Op_CmpF && duke@435: // Protect against swapping inputs to a compare when it is used by a duke@435: // counted loop exit, which requires maintaining the loop-limit as in(2) duke@435: !is_counted_loop_exit_test() ) { duke@435: // Ok, commute the constant to the right of the cmp node. duke@435: // Clone the Node, getting a new Node of the same class duke@435: cmp = cmp->clone(); duke@435: // Swap inputs to the clone duke@435: cmp->swap_edges(1, 2); duke@435: cmp = phase->transform( cmp ); duke@435: return new (phase->C, 2) BoolNode( cmp, _test.commute() ); duke@435: } duke@435: duke@435: // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)". duke@435: // The XOR-1 is an idiom used to flip the sense of a bool. We flip the duke@435: // test instead. duke@435: int cmp1_op = cmp1->Opcode(); duke@435: const TypeInt* cmp2_type = phase->type(cmp2)->isa_int(); duke@435: if (cmp2_type == NULL) return NULL; duke@435: Node* j_xor = cmp1; duke@435: if( cmp2_type == TypeInt::ZERO && duke@435: cmp1_op == Op_XorI && duke@435: j_xor->in(1) != j_xor && // An xor of itself is dead duke@435: phase->type( j_xor->in(2) ) == TypeInt::ONE && duke@435: (_test._test == BoolTest::eq || duke@435: _test._test == BoolTest::ne) ) { duke@435: Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2)); duke@435: return new (phase->C, 2) BoolNode( ncmp, _test.negate() ); duke@435: } duke@435: duke@435: // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)". duke@435: // This is a standard idiom for branching on a boolean value. duke@435: Node *c2b = cmp1; duke@435: if( cmp2_type == TypeInt::ZERO && duke@435: cmp1_op == Op_Conv2B && duke@435: (_test._test == BoolTest::eq || duke@435: _test._test == BoolTest::ne) ) { duke@435: Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int() duke@435: ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2) duke@435: : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR)) duke@435: ); duke@435: return new (phase->C, 2) BoolNode( ncmp, _test._test ); duke@435: } duke@435: duke@435: // Comparing a SubI against a zero is equal to comparing the SubI duke@435: // arguments directly. This only works for eq and ne comparisons duke@435: // due to possible integer overflow. duke@435: if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) && duke@435: (cop == Op_CmpI) && duke@435: (cmp1->Opcode() == Op_SubI) && duke@435: ( cmp2_type == TypeInt::ZERO ) ) { duke@435: Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2))); duke@435: return new (phase->C, 2) BoolNode( ncmp, _test._test ); duke@435: } duke@435: duke@435: // Change (-A vs 0) into (A vs 0) by commuting the test. Disallow in the duke@435: // most general case because negating 0x80000000 does nothing. Needed for duke@435: // the CmpF3/SubI/CmpI idiom. duke@435: if( cop == Op_CmpI && duke@435: cmp1->Opcode() == Op_SubI && duke@435: cmp2_type == TypeInt::ZERO && duke@435: phase->type( cmp1->in(1) ) == TypeInt::ZERO && duke@435: phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) { duke@435: Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2)); duke@435: return new (phase->C, 2) BoolNode( ncmp, _test.commute() ); duke@435: } duke@435: duke@435: // The transformation below is not valid for either signed or unsigned duke@435: // comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE. duke@435: // This transformation can be resurrected when we are able to duke@435: // make inferences about the range of values being subtracted from duke@435: // (or added to) relative to the wraparound point. duke@435: // duke@435: // // Remove +/-1's if possible. duke@435: // // "X <= Y-1" becomes "X < Y" duke@435: // // "X+1 <= Y" becomes "X < Y" duke@435: // // "X < Y+1" becomes "X <= Y" duke@435: // // "X-1 < Y" becomes "X <= Y" duke@435: // // Do not this to compares off of the counted-loop-end. These guys are duke@435: // // checking the trip counter and they want to use the post-incremented duke@435: // // counter. If they use the PRE-incremented counter, then the counter has duke@435: // // to be incremented in a private block on a loop backedge. duke@435: // if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd ) duke@435: // return NULL; duke@435: // #ifndef PRODUCT duke@435: // // Do not do this in a wash GVN pass during verification. duke@435: // // Gets triggered by too many simple optimizations to be bothered with duke@435: // // re-trying it again and again. duke@435: // if( !phase->allow_progress() ) return NULL; duke@435: // #endif duke@435: // // Not valid for unsigned compare because of corner cases in involving zero. duke@435: // // For example, replacing "X-1 Opcode() == Op_CmpU ) return NULL; duke@435: // int cmp2_op = cmp2->Opcode(); duke@435: // if( _test._test == BoolTest::le ) { duke@435: // if( cmp1_op == Op_AddI && duke@435: // phase->type( cmp1->in(2) ) == TypeInt::ONE ) duke@435: // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt ); duke@435: // else if( cmp2_op == Op_AddI && duke@435: // phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 ) duke@435: // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt ); duke@435: // } else if( _test._test == BoolTest::lt ) { duke@435: // if( cmp1_op == Op_AddI && duke@435: // phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 ) duke@435: // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le ); duke@435: // else if( cmp2_op == Op_AddI && duke@435: // phase->type( cmp2->in(2) ) == TypeInt::ONE ) duke@435: // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le ); duke@435: // } duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // Simplify a Bool (convert condition codes to boolean (1 or 0)) node, duke@435: // based on local information. If the input is constant, do it. duke@435: const Type *BoolNode::Value( PhaseTransform *phase ) const { duke@435: return _test.cc2logical( phase->type( in(1) ) ); duke@435: } duke@435: duke@435: //------------------------------dump_spec-------------------------------------- duke@435: // Dump special per-node info duke@435: #ifndef PRODUCT duke@435: void BoolNode::dump_spec(outputStream *st) const { duke@435: st->print("["); duke@435: _test.dump_on(st); duke@435: st->print("]"); duke@435: } duke@435: #endif duke@435: duke@435: //------------------------------is_counted_loop_exit_test-------------------------------------- duke@435: // Returns true if node is used by a counted loop node. duke@435: bool BoolNode::is_counted_loop_exit_test() { duke@435: for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) { duke@435: Node* use = fast_out(i); duke@435: if (use->is_CountedLoopEnd()) { duke@435: return true; duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------NegNode---------------------------------------- duke@435: Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: if( in(1)->Opcode() == Op_SubF ) duke@435: return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) ); duke@435: return NULL; duke@435: } duke@435: duke@435: Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: if( in(1)->Opcode() == Op_SubD ) duke@435: return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) ); duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute sqrt duke@435: const Type *SqrtDNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d = t1->getd(); duke@435: if( d < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( sqrt( d ) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute cos duke@435: const Type *CosDNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d = t1->getd(); duke@435: if( d < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( SharedRuntime::dcos( d ) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute sin duke@435: const Type *SinDNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d = t1->getd(); duke@435: if( d < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( SharedRuntime::dsin( d ) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute tan duke@435: const Type *TanDNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d = t1->getd(); duke@435: if( d < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( SharedRuntime::dtan( d ) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute log duke@435: const Type *LogDNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d = t1->getd(); duke@435: if( d < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( SharedRuntime::dlog( d ) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute log10 duke@435: const Type *Log10DNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d = t1->getd(); duke@435: if( d < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( SharedRuntime::dlog10( d ) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute exp duke@435: const Type *ExpDNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d = t1->getd(); duke@435: if( d < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( SharedRuntime::dexp( d ) ); duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // Compute pow duke@435: const Type *PowDNode::Value( PhaseTransform *phase ) const { duke@435: const Type *t1 = phase->type( in(1) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: if( t2->base() != Type::DoubleCon ) return Type::DOUBLE; duke@435: double d1 = t1->getd(); duke@435: double d2 = t2->getd(); duke@435: if( d1 < 0.0 ) return Type::DOUBLE; duke@435: if( d2 < 0.0 ) return Type::DOUBLE; duke@435: return TypeD::make( SharedRuntime::dpow( d1, d2 ) ); duke@435: }