src/share/vm/opto/subnode.cpp

Thu, 12 Mar 2009 18:16:36 -0700

author
trims
date
Thu, 12 Mar 2009 18:16:36 -0700
changeset 1063
7bb995fbd3c0
parent 835
cc80376deb0c
child 1103
90a66aa50514
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 // Portions of code courtesy of Clifford Click
    27 // Optimization - Graph Style
    29 #include "incls/_precompiled.incl"
    30 #include "incls/_subnode.cpp.incl"
    31 #include "math.h"
    33 //=============================================================================
    34 //------------------------------Identity---------------------------------------
    35 // If right input is a constant 0, return the left input.
    36 Node *SubNode::Identity( PhaseTransform *phase ) {
    37   assert(in(1) != this, "Must already have called Value");
    38   assert(in(2) != this, "Must already have called Value");
    40   // Remove double negation
    41   const Type *zero = add_id();
    42   if( phase->type( in(1) )->higher_equal( zero ) &&
    43       in(2)->Opcode() == Opcode() &&
    44       phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
    45     return in(2)->in(2);
    46   }
    48   // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y
    49   if( in(1)->Opcode() == Op_AddI ) {
    50     if( phase->eqv(in(1)->in(2),in(2)) )
    51       return in(1)->in(1);
    52     if (phase->eqv(in(1)->in(1),in(2)))
    53       return in(1)->in(2);
    55     // Also catch: "(X + Opaque2(Y)) - Y".  In this case, 'Y' is a loop-varying
    56     // trip counter and X is likely to be loop-invariant (that's how O2 Nodes
    57     // are originally used, although the optimizer sometimes jiggers things).
    58     // This folding through an O2 removes a loop-exit use of a loop-varying
    59     // value and generally lowers register pressure in and around the loop.
    60     if( in(1)->in(2)->Opcode() == Op_Opaque2 &&
    61         phase->eqv(in(1)->in(2)->in(1),in(2)) )
    62       return in(1)->in(1);
    63   }
    65   return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this;
    66 }
    68 //------------------------------Value------------------------------------------
    69 // A subtract node differences it's two inputs.
    70 const Type *SubNode::Value( PhaseTransform *phase ) const {
    71   const Node* in1 = in(1);
    72   const Node* in2 = in(2);
    73   // Either input is TOP ==> the result is TOP
    74   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
    75   if( t1 == Type::TOP ) return Type::TOP;
    76   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
    77   if( t2 == Type::TOP ) return Type::TOP;
    79   // Not correct for SubFnode and AddFNode (must check for infinity)
    80   // Equal?  Subtract is zero
    81   if (phase->eqv_uncast(in1, in2))  return add_id();
    83   // Either input is BOTTOM ==> the result is the local BOTTOM
    84   if( t1 == Type::BOTTOM || t2 == Type::BOTTOM )
    85     return bottom_type();
    87   return sub(t1,t2);            // Local flavor of type subtraction
    89 }
    91 //=============================================================================
    93 //------------------------------Helper function--------------------------------
    94 static bool ok_to_convert(Node* inc, Node* iv) {
    95     // Do not collapse (x+c0)-y if "+" is a loop increment, because the
    96     // "-" is loop invariant and collapsing extends the live-range of "x"
    97     // to overlap with the "+", forcing another register to be used in
    98     // the loop.
    99     // This test will be clearer with '&&' (apply DeMorgan's rule)
   100     // but I like the early cutouts that happen here.
   101     const PhiNode *phi;
   102     if( ( !inc->in(1)->is_Phi() ||
   103           !(phi=inc->in(1)->as_Phi()) ||
   104           phi->is_copy() ||
   105           !phi->region()->is_CountedLoop() ||
   106           inc != phi->region()->as_CountedLoop()->incr() )
   107        &&
   108         // Do not collapse (x+c0)-iv if "iv" is a loop induction variable,
   109         // because "x" maybe invariant.
   110         ( !iv->is_loop_iv() )
   111       ) {
   112       return true;
   113     } else {
   114       return false;
   115     }
   116 }
   117 //------------------------------Ideal------------------------------------------
   118 Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){
   119   Node *in1 = in(1);
   120   Node *in2 = in(2);
   121   uint op1 = in1->Opcode();
   122   uint op2 = in2->Opcode();
   124 #ifdef ASSERT
   125   // Check for dead loop
   126   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
   127       ( op1 == Op_AddI || op1 == Op_SubI ) &&
   128       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
   129         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1 ) ) )
   130     assert(false, "dead loop in SubINode::Ideal");
   131 #endif
   133   const Type *t2 = phase->type( in2 );
   134   if( t2 == Type::TOP ) return NULL;
   135   // Convert "x-c0" into "x+ -c0".
   136   if( t2->base() == Type::Int ){        // Might be bottom or top...
   137     const TypeInt *i = t2->is_int();
   138     if( i->is_con() )
   139       return new (phase->C, 3) AddINode(in1, phase->intcon(-i->get_con()));
   140   }
   142   // Convert "(x+c0) - y" into (x-y) + c0"
   143   // Do not collapse (x+c0)-y if "+" is a loop increment or
   144   // if "y" is a loop induction variable.
   145   if( op1 == Op_AddI && ok_to_convert(in1, in2) ) {
   146     const Type *tadd = phase->type( in1->in(2) );
   147     if( tadd->singleton() && tadd != Type::TOP ) {
   148       Node *sub2 = phase->transform( new (phase->C, 3) SubINode( in1->in(1), in2 ));
   149       return new (phase->C, 3) AddINode( sub2, in1->in(2) );
   150     }
   151   }
   154   // Convert "x - (y+c0)" into "(x-y) - c0"
   155   // Need the same check as in above optimization but reversed.
   156   if (op2 == Op_AddI && ok_to_convert(in2, in1)) {
   157     Node* in21 = in2->in(1);
   158     Node* in22 = in2->in(2);
   159     const TypeInt* tcon = phase->type(in22)->isa_int();
   160     if (tcon != NULL && tcon->is_con()) {
   161       Node* sub2 = phase->transform( new (phase->C, 3) SubINode(in1, in21) );
   162       Node* neg_c0 = phase->intcon(- tcon->get_con());
   163       return new (phase->C, 3) AddINode(sub2, neg_c0);
   164     }
   165   }
   167   const Type *t1 = phase->type( in1 );
   168   if( t1 == Type::TOP ) return NULL;
   170 #ifdef ASSERT
   171   // Check for dead loop
   172   if( ( op2 == Op_AddI || op2 == Op_SubI ) &&
   173       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
   174         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
   175     assert(false, "dead loop in SubINode::Ideal");
   176 #endif
   178   // Convert "x - (x+y)" into "-y"
   179   if( op2 == Op_AddI &&
   180       phase->eqv( in1, in2->in(1) ) )
   181     return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(2));
   182   // Convert "(x-y) - x" into "-y"
   183   if( op1 == Op_SubI &&
   184       phase->eqv( in1->in(1), in2 ) )
   185     return new (phase->C, 3) SubINode( phase->intcon(0),in1->in(2));
   186   // Convert "x - (y+x)" into "-y"
   187   if( op2 == Op_AddI &&
   188       phase->eqv( in1, in2->in(2) ) )
   189     return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(1));
   191   // Convert "0 - (x-y)" into "y-x"
   192   if( t1 == TypeInt::ZERO && op2 == Op_SubI )
   193     return new (phase->C, 3) SubINode( in2->in(2), in2->in(1) );
   195   // Convert "0 - (x+con)" into "-con-x"
   196   jint con;
   197   if( t1 == TypeInt::ZERO && op2 == Op_AddI &&
   198       (con = in2->in(2)->find_int_con(0)) != 0 )
   199     return new (phase->C, 3) SubINode( phase->intcon(-con), in2->in(1) );
   201   // Convert "(X+A) - (X+B)" into "A - B"
   202   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) )
   203     return new (phase->C, 3) SubINode( in1->in(2), in2->in(2) );
   205   // Convert "(A+X) - (B+X)" into "A - B"
   206   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) )
   207     return new (phase->C, 3) SubINode( in1->in(1), in2->in(1) );
   209   // Convert "(A+X) - (X+B)" into "A - B"
   210   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) )
   211     return new (phase->C, 3) SubINode( in1->in(1), in2->in(2) );
   213   // Convert "(X+A) - (B+X)" into "A - B"
   214   if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) )
   215     return new (phase->C, 3) SubINode( in1->in(2), in2->in(1) );
   217   // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally
   218   // nicer to optimize than subtract.
   219   if( op2 == Op_SubI && in2->outcnt() == 1) {
   220     Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) );
   221     return new (phase->C, 3) SubINode( add1, in2->in(1) );
   222   }
   224   return NULL;
   225 }
   227 //------------------------------sub--------------------------------------------
   228 // A subtract node differences it's two inputs.
   229 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
   230   const TypeInt *r0 = t1->is_int(); // Handy access
   231   const TypeInt *r1 = t2->is_int();
   232   int32 lo = r0->_lo - r1->_hi;
   233   int32 hi = r0->_hi - r1->_lo;
   235   // We next check for 32-bit overflow.
   236   // If that happens, we just assume all integers are possible.
   237   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   238        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   239       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   240        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   241     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   242   else                          // Overflow; assume all integers
   243     return TypeInt::INT;
   244 }
   246 //=============================================================================
   247 //------------------------------Ideal------------------------------------------
   248 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   249   Node *in1 = in(1);
   250   Node *in2 = in(2);
   251   uint op1 = in1->Opcode();
   252   uint op2 = in2->Opcode();
   254 #ifdef ASSERT
   255   // Check for dead loop
   256   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
   257       ( op1 == Op_AddL || op1 == Op_SubL ) &&
   258       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
   259         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
   260     assert(false, "dead loop in SubLNode::Ideal");
   261 #endif
   263   if( phase->type( in2 ) == Type::TOP ) return NULL;
   264   const TypeLong *i = phase->type( in2 )->isa_long();
   265   // Convert "x-c0" into "x+ -c0".
   266   if( i &&                      // Might be bottom or top...
   267       i->is_con() )
   268     return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con()));
   270   // Convert "(x+c0) - y" into (x-y) + c0"
   271   // Do not collapse (x+c0)-y if "+" is a loop increment or
   272   // if "y" is a loop induction variable.
   273   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
   274     Node *in11 = in1->in(1);
   275     const Type *tadd = phase->type( in1->in(2) );
   276     if( tadd->singleton() && tadd != Type::TOP ) {
   277       Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 ));
   278       return new (phase->C, 3) AddLNode( sub2, in1->in(2) );
   279     }
   280   }
   282   // Convert "x - (y+c0)" into "(x-y) - c0"
   283   // Need the same check as in above optimization but reversed.
   284   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
   285     Node* in21 = in2->in(1);
   286     Node* in22 = in2->in(2);
   287     const TypeLong* tcon = phase->type(in22)->isa_long();
   288     if (tcon != NULL && tcon->is_con()) {
   289       Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) );
   290       Node* neg_c0 = phase->longcon(- tcon->get_con());
   291       return new (phase->C, 3) AddLNode(sub2, neg_c0);
   292     }
   293   }
   295   const Type *t1 = phase->type( in1 );
   296   if( t1 == Type::TOP ) return NULL;
   298 #ifdef ASSERT
   299   // Check for dead loop
   300   if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
   301       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
   302         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
   303     assert(false, "dead loop in SubLNode::Ideal");
   304 #endif
   306   // Convert "x - (x+y)" into "-y"
   307   if( op2 == Op_AddL &&
   308       phase->eqv( in1, in2->in(1) ) )
   309     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
   310   // Convert "x - (y+x)" into "-y"
   311   if( op2 == Op_AddL &&
   312       phase->eqv( in1, in2->in(2) ) )
   313     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
   315   // Convert "0 - (x-y)" into "y-x"
   316   if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
   317     return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) );
   319   // Convert "(X+A) - (X+B)" into "A - B"
   320   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
   321     return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) );
   323   // Convert "(A+X) - (B+X)" into "A - B"
   324   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
   325     return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) );
   327   // Convert "A-(B-C)" into (A+C)-B"
   328   if( op2 == Op_SubL && in2->outcnt() == 1) {
   329     Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) );
   330     return new (phase->C, 3) SubLNode( add1, in2->in(1) );
   331   }
   333   return NULL;
   334 }
   336 //------------------------------sub--------------------------------------------
   337 // A subtract node differences it's two inputs.
   338 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
   339   const TypeLong *r0 = t1->is_long(); // Handy access
   340   const TypeLong *r1 = t2->is_long();
   341   jlong lo = r0->_lo - r1->_hi;
   342   jlong hi = r0->_hi - r1->_lo;
   344   // We next check for 32-bit overflow.
   345   // If that happens, we just assume all integers are possible.
   346   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   347        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   348       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   349        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   350     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   351   else                          // Overflow; assume all integers
   352     return TypeLong::LONG;
   353 }
   355 //=============================================================================
   356 //------------------------------Value------------------------------------------
   357 // A subtract node differences its two inputs.
   358 const Type *SubFPNode::Value( PhaseTransform *phase ) const {
   359   const Node* in1 = in(1);
   360   const Node* in2 = in(2);
   361   // Either input is TOP ==> the result is TOP
   362   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   363   if( t1 == Type::TOP ) return Type::TOP;
   364   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   365   if( t2 == Type::TOP ) return Type::TOP;
   367   // if both operands are infinity of same sign, the result is NaN; do
   368   // not replace with zero
   369   if( (t1->is_finite() && t2->is_finite()) ) {
   370     if( phase->eqv(in1, in2) ) return add_id();
   371   }
   373   // Either input is BOTTOM ==> the result is the local BOTTOM
   374   const Type *bot = bottom_type();
   375   if( (t1 == bot) || (t2 == bot) ||
   376       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
   377     return bot;
   379   return sub(t1,t2);            // Local flavor of type subtraction
   380 }
   383 //=============================================================================
   384 //------------------------------Ideal------------------------------------------
   385 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   386   const Type *t2 = phase->type( in(2) );
   387   // Convert "x-c0" into "x+ -c0".
   388   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
   389     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
   390   }
   392   // Not associative because of boundary conditions (infinity)
   393   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   394     // Convert "x - (x+y)" into "-y"
   395     if( in(2)->is_Add() &&
   396         phase->eqv(in(1),in(2)->in(1) ) )
   397       return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
   398   }
   400   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
   401   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
   402   //if( phase->type(in(1)) == TypeF::ZERO )
   403   //return new (phase->C, 2) NegFNode(in(2));
   405   return NULL;
   406 }
   408 //------------------------------sub--------------------------------------------
   409 // A subtract node differences its two inputs.
   410 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
   411   // no folding if one of operands is infinity or NaN, do not do constant folding
   412   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
   413     return TypeF::make( t1->getf() - t2->getf() );
   414   }
   415   else if( g_isnan(t1->getf()) ) {
   416     return t1;
   417   }
   418   else if( g_isnan(t2->getf()) ) {
   419     return t2;
   420   }
   421   else {
   422     return Type::FLOAT;
   423   }
   424 }
   426 //=============================================================================
   427 //------------------------------Ideal------------------------------------------
   428 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   429   const Type *t2 = phase->type( in(2) );
   430   // Convert "x-c0" into "x+ -c0".
   431   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
   432     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
   433   }
   435   // Not associative because of boundary conditions (infinity)
   436   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   437     // Convert "x - (x+y)" into "-y"
   438     if( in(2)->is_Add() &&
   439         phase->eqv(in(1),in(2)->in(1) ) )
   440       return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
   441   }
   443   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
   444   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
   445   //if( phase->type(in(1)) == TypeD::ZERO )
   446   //return new (phase->C, 2) NegDNode(in(2));
   448   return NULL;
   449 }
   451 //------------------------------sub--------------------------------------------
   452 // A subtract node differences its two inputs.
   453 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
   454   // no folding if one of operands is infinity or NaN, do not do constant folding
   455   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
   456     return TypeD::make( t1->getd() - t2->getd() );
   457   }
   458   else if( g_isnan(t1->getd()) ) {
   459     return t1;
   460   }
   461   else if( g_isnan(t2->getd()) ) {
   462     return t2;
   463   }
   464   else {
   465     return Type::DOUBLE;
   466   }
   467 }
   469 //=============================================================================
   470 //------------------------------Idealize---------------------------------------
   471 // Unlike SubNodes, compare must still flatten return value to the
   472 // range -1, 0, 1.
   473 // And optimizations like those for (X + Y) - X fail if overflow happens.
   474 Node *CmpNode::Identity( PhaseTransform *phase ) {
   475   return this;
   476 }
   478 //=============================================================================
   479 //------------------------------cmp--------------------------------------------
   480 // Simplify a CmpI (compare 2 integers) node, based on local information.
   481 // If both inputs are constants, compare them.
   482 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
   483   const TypeInt *r0 = t1->is_int(); // Handy access
   484   const TypeInt *r1 = t2->is_int();
   486   if( r0->_hi < r1->_lo )       // Range is always low?
   487     return TypeInt::CC_LT;
   488   else if( r0->_lo > r1->_hi )  // Range is always high?
   489     return TypeInt::CC_GT;
   491   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   492     assert(r0->get_con() == r1->get_con(), "must be equal");
   493     return TypeInt::CC_EQ;      // Equal results.
   494   } else if( r0->_hi == r1->_lo ) // Range is never high?
   495     return TypeInt::CC_LE;
   496   else if( r0->_lo == r1->_hi ) // Range is never low?
   497     return TypeInt::CC_GE;
   498   return TypeInt::CC;           // else use worst case results
   499 }
   501 // Simplify a CmpU (compare 2 integers) node, based on local information.
   502 // If both inputs are constants, compare them.
   503 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
   504   assert(!t1->isa_ptr(), "obsolete usage of CmpU");
   506   // comparing two unsigned ints
   507   const TypeInt *r0 = t1->is_int();   // Handy access
   508   const TypeInt *r1 = t2->is_int();
   510   // Current installed version
   511   // Compare ranges for non-overlap
   512   juint lo0 = r0->_lo;
   513   juint hi0 = r0->_hi;
   514   juint lo1 = r1->_lo;
   515   juint hi1 = r1->_hi;
   517   // If either one has both negative and positive values,
   518   // it therefore contains both 0 and -1, and since [0..-1] is the
   519   // full unsigned range, the type must act as an unsigned bottom.
   520   bool bot0 = ((jint)(lo0 ^ hi0) < 0);
   521   bool bot1 = ((jint)(lo1 ^ hi1) < 0);
   523   if (bot0 || bot1) {
   524     // All unsigned values are LE -1 and GE 0.
   525     if (lo0 == 0 && hi0 == 0) {
   526       return TypeInt::CC_LE;            //   0 <= bot
   527     } else if (lo1 == 0 && hi1 == 0) {
   528       return TypeInt::CC_GE;            // bot >= 0
   529     }
   530   } else {
   531     // We can use ranges of the form [lo..hi] if signs are the same.
   532     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
   533     // results are reversed, '-' > '+' for unsigned compare
   534     if (hi0 < lo1) {
   535       return TypeInt::CC_LT;            // smaller
   536     } else if (lo0 > hi1) {
   537       return TypeInt::CC_GT;            // greater
   538     } else if (hi0 == lo1 && lo0 == hi1) {
   539       return TypeInt::CC_EQ;            // Equal results
   540     } else if (lo0 >= hi1) {
   541       return TypeInt::CC_GE;
   542     } else if (hi0 <= lo1) {
   543       // Check for special case in Hashtable::get.  (See below.)
   544       if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   545           in(1)->Opcode() == Op_ModI &&
   546           in(1)->in(2) == in(2) )
   547         return TypeInt::CC_LT;
   548       return TypeInt::CC_LE;
   549     }
   550   }
   551   // Check for special case in Hashtable::get - the hash index is
   552   // mod'ed to the table size so the following range check is useless.
   553   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
   554   // to be positive.
   555   // (This is a gross hack, since the sub method never
   556   // looks at the structure of the node in any other case.)
   557   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   558       in(1)->Opcode() == Op_ModI &&
   559       in(1)->in(2)->uncast() == in(2)->uncast())
   560     return TypeInt::CC_LT;
   561   return TypeInt::CC;                   // else use worst case results
   562 }
   564 //------------------------------Idealize---------------------------------------
   565 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   566   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
   567     switch (in(1)->Opcode()) {
   568     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
   569       return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2));
   570     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
   571       return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2));
   572     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
   573       return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2));
   574     //case Op_SubI:
   575       // If (x - y) cannot overflow, then ((x - y) <?> 0)
   576       // can be turned into (x <?> y).
   577       // This is handled (with more general cases) by Ideal_sub_algebra.
   578     }
   579   }
   580   return NULL;                  // No change
   581 }
   584 //=============================================================================
   585 // Simplify a CmpL (compare 2 longs ) node, based on local information.
   586 // If both inputs are constants, compare them.
   587 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
   588   const TypeLong *r0 = t1->is_long(); // Handy access
   589   const TypeLong *r1 = t2->is_long();
   591   if( r0->_hi < r1->_lo )       // Range is always low?
   592     return TypeInt::CC_LT;
   593   else if( r0->_lo > r1->_hi )  // Range is always high?
   594     return TypeInt::CC_GT;
   596   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   597     assert(r0->get_con() == r1->get_con(), "must be equal");
   598     return TypeInt::CC_EQ;      // Equal results.
   599   } else if( r0->_hi == r1->_lo ) // Range is never high?
   600     return TypeInt::CC_LE;
   601   else if( r0->_lo == r1->_hi ) // Range is never low?
   602     return TypeInt::CC_GE;
   603   return TypeInt::CC;           // else use worst case results
   604 }
   606 //=============================================================================
   607 //------------------------------sub--------------------------------------------
   608 // Simplify an CmpP (compare 2 pointers) node, based on local information.
   609 // If both inputs are constants, compare them.
   610 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
   611   const TypePtr *r0 = t1->is_ptr(); // Handy access
   612   const TypePtr *r1 = t2->is_ptr();
   614   // Undefined inputs makes for an undefined result
   615   if( TypePtr::above_centerline(r0->_ptr) ||
   616       TypePtr::above_centerline(r1->_ptr) )
   617     return Type::TOP;
   619   if (r0 == r1 && r0->singleton()) {
   620     // Equal pointer constants (klasses, nulls, etc.)
   621     return TypeInt::CC_EQ;
   622   }
   624   // See if it is 2 unrelated classes.
   625   const TypeOopPtr* p0 = r0->isa_oopptr();
   626   const TypeOopPtr* p1 = r1->isa_oopptr();
   627   if (p0 && p1) {
   628     Node* in1 = in(1)->uncast();
   629     Node* in2 = in(2)->uncast();
   630     AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
   631     AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
   632     if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
   633       return TypeInt::CC_GT;  // different pointers
   634     }
   635     ciKlass* klass0 = p0->klass();
   636     bool    xklass0 = p0->klass_is_exact();
   637     ciKlass* klass1 = p1->klass();
   638     bool    xklass1 = p1->klass_is_exact();
   639     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
   640     if (klass0 && klass1 &&
   641         kps != 1 &&             // both or neither are klass pointers
   642         !klass0->is_interface() && // do not trust interfaces
   643         !klass1->is_interface()) {
   644       bool unrelated_classes = false;
   645       // See if neither subclasses the other, or if the class on top
   646       // is precise.  In either of these cases, the compare is known
   647       // to fail if at least one of the pointers is provably not null.
   648       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
   649           !klass0->is_java_klass() ||   // types not part of Java language?
   650           !klass1->is_java_klass()) {   // types not part of Java language?
   651         // Do nothing; we know nothing for imprecise types
   652       } else if (klass0->is_subtype_of(klass1)) {
   653         // If klass1's type is PRECISE, then classes are unrelated.
   654         unrelated_classes = xklass1;
   655       } else if (klass1->is_subtype_of(klass0)) {
   656         // If klass0's type is PRECISE, then classes are unrelated.
   657         unrelated_classes = xklass0;
   658       } else {                  // Neither subtypes the other
   659         unrelated_classes = true;
   660       }
   661       if (unrelated_classes) {
   662         // The oops classes are known to be unrelated. If the joined PTRs of
   663         // two oops is not Null and not Bottom, then we are sure that one
   664         // of the two oops is non-null, and the comparison will always fail.
   665         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
   666         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
   667           return TypeInt::CC_GT;
   668         }
   669       }
   670     }
   671   }
   673   // Known constants can be compared exactly
   674   // Null can be distinguished from any NotNull pointers
   675   // Unknown inputs makes an unknown result
   676   if( r0->singleton() ) {
   677     intptr_t bits0 = r0->get_con();
   678     if( r1->singleton() )
   679       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
   680     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   681   } else if( r1->singleton() ) {
   682     intptr_t bits1 = r1->get_con();
   683     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   684   } else
   685     return TypeInt::CC;
   686 }
   688 //------------------------------Ideal------------------------------------------
   689 // Check for the case of comparing an unknown klass loaded from the primary
   690 // super-type array vs a known klass with no subtypes.  This amounts to
   691 // checking to see an unknown klass subtypes a known klass with no subtypes;
   692 // this only happens on an exact match.  We can shorten this test by 1 load.
   693 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   694   // Constant pointer on right?
   695   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
   696   if (t2 == NULL || !t2->klass_is_exact())
   697     return NULL;
   698   // Get the constant klass we are comparing to.
   699   ciKlass* superklass = t2->klass();
   701   // Now check for LoadKlass on left.
   702   Node* ldk1 = in(1);
   703   if (ldk1->is_DecodeN()) {
   704     ldk1 = ldk1->in(1);
   705     if (ldk1->Opcode() != Op_LoadNKlass )
   706       return NULL;
   707   } else if (ldk1->Opcode() != Op_LoadKlass )
   708     return NULL;
   709   // Take apart the address of the LoadKlass:
   710   Node* adr1 = ldk1->in(MemNode::Address);
   711   intptr_t con2 = 0;
   712   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
   713   if (ldk2 == NULL)
   714     return NULL;
   715   if (con2 == oopDesc::klass_offset_in_bytes()) {
   716     // We are inspecting an object's concrete class.
   717     // Short-circuit the check if the query is abstract.
   718     if (superklass->is_interface() ||
   719         superklass->is_abstract()) {
   720       // Make it come out always false:
   721       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
   722       return this;
   723     }
   724   }
   726   // Check for a LoadKlass from primary supertype array.
   727   // Any nested loadklass from loadklass+con must be from the p.s. array.
   728   if (ldk2->is_DecodeN()) {
   729     // Keep ldk2 as DecodeN since it could be used in CmpP below.
   730     if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
   731       return NULL;
   732   } else if (ldk2->Opcode() != Op_LoadKlass)
   733     return NULL;
   735   // Verify that we understand the situation
   736   if (con2 != (intptr_t) superklass->super_check_offset())
   737     return NULL;                // Might be element-klass loading from array klass
   739   // If 'superklass' has no subklasses and is not an interface, then we are
   740   // assured that the only input which will pass the type check is
   741   // 'superklass' itself.
   742   //
   743   // We could be more liberal here, and allow the optimization on interfaces
   744   // which have a single implementor.  This would require us to increase the
   745   // expressiveness of the add_dependency() mechanism.
   746   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
   748   // Object arrays must have their base element have no subtypes
   749   while (superklass->is_obj_array_klass()) {
   750     ciType* elem = superklass->as_obj_array_klass()->element_type();
   751     superklass = elem->as_klass();
   752   }
   753   if (superklass->is_instance_klass()) {
   754     ciInstanceKlass* ik = superklass->as_instance_klass();
   755     if (ik->has_subklass() || ik->is_interface())  return NULL;
   756     // Add a dependency if there is a chance that a subclass will be added later.
   757     if (!ik->is_final()) {
   758       phase->C->dependencies()->assert_leaf_type(ik);
   759     }
   760   }
   762   // Bypass the dependent load, and compare directly
   763   this->set_req(1,ldk2);
   765   return this;
   766 }
   768 //=============================================================================
   769 //------------------------------sub--------------------------------------------
   770 // Simplify an CmpN (compare 2 pointers) node, based on local information.
   771 // If both inputs are constants, compare them.
   772 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
   773   const TypePtr *r0 = t1->make_ptr(); // Handy access
   774   const TypePtr *r1 = t2->make_ptr();
   776   // Undefined inputs makes for an undefined result
   777   if( TypePtr::above_centerline(r0->_ptr) ||
   778       TypePtr::above_centerline(r1->_ptr) )
   779     return Type::TOP;
   781   if (r0 == r1 && r0->singleton()) {
   782     // Equal pointer constants (klasses, nulls, etc.)
   783     return TypeInt::CC_EQ;
   784   }
   786   // See if it is 2 unrelated classes.
   787   const TypeOopPtr* p0 = r0->isa_oopptr();
   788   const TypeOopPtr* p1 = r1->isa_oopptr();
   789   if (p0 && p1) {
   790     ciKlass* klass0 = p0->klass();
   791     bool    xklass0 = p0->klass_is_exact();
   792     ciKlass* klass1 = p1->klass();
   793     bool    xklass1 = p1->klass_is_exact();
   794     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
   795     if (klass0 && klass1 &&
   796         kps != 1 &&             // both or neither are klass pointers
   797         !klass0->is_interface() && // do not trust interfaces
   798         !klass1->is_interface()) {
   799       bool unrelated_classes = false;
   800       // See if neither subclasses the other, or if the class on top
   801       // is precise.  In either of these cases, the compare is known
   802       // to fail if at least one of the pointers is provably not null.
   803       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
   804           !klass0->is_java_klass() ||   // types not part of Java language?
   805           !klass1->is_java_klass()) {   // types not part of Java language?
   806         // Do nothing; we know nothing for imprecise types
   807       } else if (klass0->is_subtype_of(klass1)) {
   808         // If klass1's type is PRECISE, then classes are unrelated.
   809         unrelated_classes = xklass1;
   810       } else if (klass1->is_subtype_of(klass0)) {
   811         // If klass0's type is PRECISE, then classes are unrelated.
   812         unrelated_classes = xklass0;
   813       } else {                  // Neither subtypes the other
   814         unrelated_classes = true;
   815       }
   816       if (unrelated_classes) {
   817         // The oops classes are known to be unrelated. If the joined PTRs of
   818         // two oops is not Null and not Bottom, then we are sure that one
   819         // of the two oops is non-null, and the comparison will always fail.
   820         TypePtr::PTR jp = r0->join_ptr(r1->_ptr);
   821         if (jp != TypePtr::Null && jp != TypePtr::BotPTR) {
   822           return TypeInt::CC_GT;
   823         }
   824       }
   825     }
   826   }
   828   // Known constants can be compared exactly
   829   // Null can be distinguished from any NotNull pointers
   830   // Unknown inputs makes an unknown result
   831   if( r0->singleton() ) {
   832     intptr_t bits0 = r0->get_con();
   833     if( r1->singleton() )
   834       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
   835     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   836   } else if( r1->singleton() ) {
   837     intptr_t bits1 = r1->get_con();
   838     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   839   } else
   840     return TypeInt::CC;
   841 }
   843 //------------------------------Ideal------------------------------------------
   844 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   845   return NULL;
   846 }
   848 //=============================================================================
   849 //------------------------------Value------------------------------------------
   850 // Simplify an CmpF (compare 2 floats ) node, based on local information.
   851 // If both inputs are constants, compare them.
   852 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
   853   const Node* in1 = in(1);
   854   const Node* in2 = in(2);
   855   // Either input is TOP ==> the result is TOP
   856   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   857   if( t1 == Type::TOP ) return Type::TOP;
   858   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   859   if( t2 == Type::TOP ) return Type::TOP;
   861   // Not constants?  Don't know squat - even if they are the same
   862   // value!  If they are NaN's they compare to LT instead of EQ.
   863   const TypeF *tf1 = t1->isa_float_constant();
   864   const TypeF *tf2 = t2->isa_float_constant();
   865   if( !tf1 || !tf2 ) return TypeInt::CC;
   867   // This implements the Java bytecode fcmpl, so unordered returns -1.
   868   if( tf1->is_nan() || tf2->is_nan() )
   869     return TypeInt::CC_LT;
   871   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
   872   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
   873   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
   874   return TypeInt::CC_EQ;
   875 }
   878 //=============================================================================
   879 //------------------------------Value------------------------------------------
   880 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
   881 // If both inputs are constants, compare them.
   882 const Type *CmpDNode::Value( PhaseTransform *phase ) const {
   883   const Node* in1 = in(1);
   884   const Node* in2 = in(2);
   885   // Either input is TOP ==> the result is TOP
   886   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   887   if( t1 == Type::TOP ) return Type::TOP;
   888   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   889   if( t2 == Type::TOP ) return Type::TOP;
   891   // Not constants?  Don't know squat - even if they are the same
   892   // value!  If they are NaN's they compare to LT instead of EQ.
   893   const TypeD *td1 = t1->isa_double_constant();
   894   const TypeD *td2 = t2->isa_double_constant();
   895   if( !td1 || !td2 ) return TypeInt::CC;
   897   // This implements the Java bytecode dcmpl, so unordered returns -1.
   898   if( td1->is_nan() || td2->is_nan() )
   899     return TypeInt::CC_LT;
   901   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
   902   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
   903   assert( td1->_d == td2->_d, "do not understand FP behavior" );
   904   return TypeInt::CC_EQ;
   905 }
   907 //------------------------------Ideal------------------------------------------
   908 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   909   // Check if we can change this to a CmpF and remove a ConvD2F operation.
   910   // Change  (CMPD (F2D (float)) (ConD value))
   911   // To      (CMPF      (float)  (ConF value))
   912   // Valid when 'value' does not lose precision as a float.
   913   // Benefits: eliminates conversion, does not require 24-bit mode
   915   // NaNs prevent commuting operands.  This transform works regardless of the
   916   // order of ConD and ConvF2D inputs by preserving the original order.
   917   int idx_f2d = 1;              // ConvF2D on left side?
   918   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
   919     idx_f2d = 2;                // No, swap to check for reversed args
   920   int idx_con = 3-idx_f2d;      // Check for the constant on other input
   922   if( ConvertCmpD2CmpF &&
   923       in(idx_f2d)->Opcode() == Op_ConvF2D &&
   924       in(idx_con)->Opcode() == Op_ConD ) {
   925     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
   926     double t2_value_as_double = t2->_d;
   927     float  t2_value_as_float  = (float)t2_value_as_double;
   928     if( t2_value_as_double == (double)t2_value_as_float ) {
   929       // Test value can be represented as a float
   930       // Eliminate the conversion to double and create new comparison
   931       Node *new_in1 = in(idx_f2d)->in(1);
   932       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
   933       if( idx_f2d != 1 ) {      // Must flip args to match original order
   934         Node *tmp = new_in1;
   935         new_in1 = new_in2;
   936         new_in2 = tmp;
   937       }
   938       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
   939         ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 )
   940         : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ;
   941       return new_cmp;           // Changed to CmpFNode
   942     }
   943     // Testing value required the precision of a double
   944   }
   945   return NULL;                  // No change
   946 }
   949 //=============================================================================
   950 //------------------------------cc2logical-------------------------------------
   951 // Convert a condition code type to a logical type
   952 const Type *BoolTest::cc2logical( const Type *CC ) const {
   953   if( CC == Type::TOP ) return Type::TOP;
   954   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
   955   const TypeInt *ti = CC->is_int();
   956   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
   957     // Match low order 2 bits
   958     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
   959     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
   960     return TypeInt::make(tmp);       // Boolean result
   961   }
   963   if( CC == TypeInt::CC_GE ) {
   964     if( _test == ge ) return TypeInt::ONE;
   965     if( _test == lt ) return TypeInt::ZERO;
   966   }
   967   if( CC == TypeInt::CC_LE ) {
   968     if( _test == le ) return TypeInt::ONE;
   969     if( _test == gt ) return TypeInt::ZERO;
   970   }
   972   return TypeInt::BOOL;
   973 }
   975 //------------------------------dump_spec-------------------------------------
   976 // Print special per-node info
   977 #ifndef PRODUCT
   978 void BoolTest::dump_on(outputStream *st) const {
   979   const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"};
   980   st->print(msg[_test]);
   981 }
   982 #endif
   984 //=============================================================================
   985 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
   986 uint BoolNode::size_of() const { return sizeof(BoolNode); }
   988 //------------------------------operator==-------------------------------------
   989 uint BoolNode::cmp( const Node &n ) const {
   990   const BoolNode *b = (const BoolNode *)&n; // Cast up
   991   return (_test._test == b->_test._test);
   992 }
   994 //------------------------------clone_cmp--------------------------------------
   995 // Clone a compare/bool tree
   996 static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) {
   997   Node *ncmp = cmp->clone();
   998   ncmp->set_req(1,cmp1);
   999   ncmp->set_req(2,cmp2);
  1000   ncmp = gvn->transform( ncmp );
  1001   return new (gvn->C, 2) BoolNode( ncmp, test );
  1004 //-------------------------------make_predicate--------------------------------
  1005 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
  1006   if (test_value->is_Con())   return test_value;
  1007   if (test_value->is_Bool())  return test_value;
  1008   Compile* C = phase->C;
  1009   if (test_value->is_CMove() &&
  1010       test_value->in(CMoveNode::Condition)->is_Bool()) {
  1011     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
  1012     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
  1013     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
  1014     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
  1015       return bol;
  1016     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
  1017       return phase->transform( bol->negate(phase) );
  1019     // Else fall through.  The CMove gets in the way of the test.
  1020     // It should be the case that make_predicate(bol->as_int_value()) == bol.
  1022   Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0));
  1023   cmp = phase->transform(cmp);
  1024   Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne);
  1025   return phase->transform(bol);
  1028 //--------------------------------as_int_value---------------------------------
  1029 Node* BoolNode::as_int_value(PhaseGVN* phase) {
  1030   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
  1031   Node* cmov = CMoveNode::make(phase->C, NULL, this,
  1032                                phase->intcon(0), phase->intcon(1),
  1033                                TypeInt::BOOL);
  1034   return phase->transform(cmov);
  1037 //----------------------------------negate-------------------------------------
  1038 BoolNode* BoolNode::negate(PhaseGVN* phase) {
  1039   Compile* C = phase->C;
  1040   return new (C, 2) BoolNode(in(1), _test.negate());
  1044 //------------------------------Ideal------------------------------------------
  1045 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1046   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
  1047   // This moves the constant to the right.  Helps value-numbering.
  1048   Node *cmp = in(1);
  1049   if( !cmp->is_Sub() ) return NULL;
  1050   int cop = cmp->Opcode();
  1051   if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL;
  1052   Node *cmp1 = cmp->in(1);
  1053   Node *cmp2 = cmp->in(2);
  1054   if( !cmp1 ) return NULL;
  1056   // Constant on left?
  1057   Node *con = cmp1;
  1058   uint op2 = cmp2->Opcode();
  1059   // Move constants to the right of compare's to canonicalize.
  1060   // Do not muck with Opaque1 nodes, as this indicates a loop
  1061   // guard that cannot change shape.
  1062   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
  1063       // Because of NaN's, CmpD and CmpF are not commutative
  1064       cop != Op_CmpD && cop != Op_CmpF &&
  1065       // Protect against swapping inputs to a compare when it is used by a
  1066       // counted loop exit, which requires maintaining the loop-limit as in(2)
  1067       !is_counted_loop_exit_test() ) {
  1068     // Ok, commute the constant to the right of the cmp node.
  1069     // Clone the Node, getting a new Node of the same class
  1070     cmp = cmp->clone();
  1071     // Swap inputs to the clone
  1072     cmp->swap_edges(1, 2);
  1073     cmp = phase->transform( cmp );
  1074     return new (phase->C, 2) BoolNode( cmp, _test.commute() );
  1077   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
  1078   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
  1079   // test instead.
  1080   int cmp1_op = cmp1->Opcode();
  1081   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
  1082   if (cmp2_type == NULL)  return NULL;
  1083   Node* j_xor = cmp1;
  1084   if( cmp2_type == TypeInt::ZERO &&
  1085       cmp1_op == Op_XorI &&
  1086       j_xor->in(1) != j_xor &&          // An xor of itself is dead
  1087       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
  1088       (_test._test == BoolTest::eq ||
  1089        _test._test == BoolTest::ne) ) {
  1090     Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2));
  1091     return new (phase->C, 2) BoolNode( ncmp, _test.negate() );
  1094   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
  1095   // This is a standard idiom for branching on a boolean value.
  1096   Node *c2b = cmp1;
  1097   if( cmp2_type == TypeInt::ZERO &&
  1098       cmp1_op == Op_Conv2B &&
  1099       (_test._test == BoolTest::eq ||
  1100        _test._test == BoolTest::ne) ) {
  1101     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
  1102        ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2)
  1103        : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
  1104     );
  1105     return new (phase->C, 2) BoolNode( ncmp, _test._test );
  1108   // Comparing a SubI against a zero is equal to comparing the SubI
  1109   // arguments directly.  This only works for eq and ne comparisons
  1110   // due to possible integer overflow.
  1111   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
  1112         (cop == Op_CmpI) &&
  1113         (cmp1->Opcode() == Op_SubI) &&
  1114         ( cmp2_type == TypeInt::ZERO ) ) {
  1115     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2)));
  1116     return new (phase->C, 2) BoolNode( ncmp, _test._test );
  1119   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
  1120   // most general case because negating 0x80000000 does nothing.  Needed for
  1121   // the CmpF3/SubI/CmpI idiom.
  1122   if( cop == Op_CmpI &&
  1123       cmp1->Opcode() == Op_SubI &&
  1124       cmp2_type == TypeInt::ZERO &&
  1125       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
  1126       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
  1127     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2));
  1128     return new (phase->C, 2) BoolNode( ncmp, _test.commute() );
  1131   //  The transformation below is not valid for either signed or unsigned
  1132   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
  1133   //  This transformation can be resurrected when we are able to
  1134   //  make inferences about the range of values being subtracted from
  1135   //  (or added to) relative to the wraparound point.
  1136   //
  1137   //    // Remove +/-1's if possible.
  1138   //    // "X <= Y-1" becomes "X <  Y"
  1139   //    // "X+1 <= Y" becomes "X <  Y"
  1140   //    // "X <  Y+1" becomes "X <= Y"
  1141   //    // "X-1 <  Y" becomes "X <= Y"
  1142   //    // Do not this to compares off of the counted-loop-end.  These guys are
  1143   //    // checking the trip counter and they want to use the post-incremented
  1144   //    // counter.  If they use the PRE-incremented counter, then the counter has
  1145   //    // to be incremented in a private block on a loop backedge.
  1146   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
  1147   //      return NULL;
  1148   //  #ifndef PRODUCT
  1149   //    // Do not do this in a wash GVN pass during verification.
  1150   //    // Gets triggered by too many simple optimizations to be bothered with
  1151   //    // re-trying it again and again.
  1152   //    if( !phase->allow_progress() ) return NULL;
  1153   //  #endif
  1154   //    // Not valid for unsigned compare because of corner cases in involving zero.
  1155   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
  1156   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
  1157   //    // "0 <=u Y" is always true).
  1158   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
  1159   //    int cmp2_op = cmp2->Opcode();
  1160   //    if( _test._test == BoolTest::le ) {
  1161   //      if( cmp1_op == Op_AddI &&
  1162   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
  1163   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
  1164   //      else if( cmp2_op == Op_AddI &&
  1165   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
  1166   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
  1167   //    } else if( _test._test == BoolTest::lt ) {
  1168   //      if( cmp1_op == Op_AddI &&
  1169   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
  1170   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
  1171   //      else if( cmp2_op == Op_AddI &&
  1172   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
  1173   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
  1174   //    }
  1176   return NULL;
  1179 //------------------------------Value------------------------------------------
  1180 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
  1181 // based on local information.   If the input is constant, do it.
  1182 const Type *BoolNode::Value( PhaseTransform *phase ) const {
  1183   return _test.cc2logical( phase->type( in(1) ) );
  1186 //------------------------------dump_spec--------------------------------------
  1187 // Dump special per-node info
  1188 #ifndef PRODUCT
  1189 void BoolNode::dump_spec(outputStream *st) const {
  1190   st->print("[");
  1191   _test.dump_on(st);
  1192   st->print("]");
  1194 #endif
  1196 //------------------------------is_counted_loop_exit_test--------------------------------------
  1197 // Returns true if node is used by a counted loop node.
  1198 bool BoolNode::is_counted_loop_exit_test() {
  1199   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
  1200     Node* use = fast_out(i);
  1201     if (use->is_CountedLoopEnd()) {
  1202       return true;
  1205   return false;
  1208 //=============================================================================
  1209 //------------------------------NegNode----------------------------------------
  1210 Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1211   if( in(1)->Opcode() == Op_SubF )
  1212     return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) );
  1213   return NULL;
  1216 Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1217   if( in(1)->Opcode() == Op_SubD )
  1218     return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) );
  1219   return NULL;
  1223 //=============================================================================
  1224 //------------------------------Value------------------------------------------
  1225 // Compute sqrt
  1226 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
  1227   const Type *t1 = phase->type( in(1) );
  1228   if( t1 == Type::TOP ) return Type::TOP;
  1229   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1230   double d = t1->getd();
  1231   if( d < 0.0 ) return Type::DOUBLE;
  1232   return TypeD::make( sqrt( d ) );
  1235 //=============================================================================
  1236 //------------------------------Value------------------------------------------
  1237 // Compute cos
  1238 const Type *CosDNode::Value( PhaseTransform *phase ) const {
  1239   const Type *t1 = phase->type( in(1) );
  1240   if( t1 == Type::TOP ) return Type::TOP;
  1241   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1242   double d = t1->getd();
  1243   if( d < 0.0 ) return Type::DOUBLE;
  1244   return TypeD::make( SharedRuntime::dcos( d ) );
  1247 //=============================================================================
  1248 //------------------------------Value------------------------------------------
  1249 // Compute sin
  1250 const Type *SinDNode::Value( PhaseTransform *phase ) const {
  1251   const Type *t1 = phase->type( in(1) );
  1252   if( t1 == Type::TOP ) return Type::TOP;
  1253   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1254   double d = t1->getd();
  1255   if( d < 0.0 ) return Type::DOUBLE;
  1256   return TypeD::make( SharedRuntime::dsin( d ) );
  1259 //=============================================================================
  1260 //------------------------------Value------------------------------------------
  1261 // Compute tan
  1262 const Type *TanDNode::Value( PhaseTransform *phase ) const {
  1263   const Type *t1 = phase->type( in(1) );
  1264   if( t1 == Type::TOP ) return Type::TOP;
  1265   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1266   double d = t1->getd();
  1267   if( d < 0.0 ) return Type::DOUBLE;
  1268   return TypeD::make( SharedRuntime::dtan( d ) );
  1271 //=============================================================================
  1272 //------------------------------Value------------------------------------------
  1273 // Compute log
  1274 const Type *LogDNode::Value( PhaseTransform *phase ) const {
  1275   const Type *t1 = phase->type( in(1) );
  1276   if( t1 == Type::TOP ) return Type::TOP;
  1277   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1278   double d = t1->getd();
  1279   if( d < 0.0 ) return Type::DOUBLE;
  1280   return TypeD::make( SharedRuntime::dlog( d ) );
  1283 //=============================================================================
  1284 //------------------------------Value------------------------------------------
  1285 // Compute log10
  1286 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
  1287   const Type *t1 = phase->type( in(1) );
  1288   if( t1 == Type::TOP ) return Type::TOP;
  1289   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1290   double d = t1->getd();
  1291   if( d < 0.0 ) return Type::DOUBLE;
  1292   return TypeD::make( SharedRuntime::dlog10( d ) );
  1295 //=============================================================================
  1296 //------------------------------Value------------------------------------------
  1297 // Compute exp
  1298 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
  1299   const Type *t1 = phase->type( in(1) );
  1300   if( t1 == Type::TOP ) return Type::TOP;
  1301   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1302   double d = t1->getd();
  1303   if( d < 0.0 ) return Type::DOUBLE;
  1304   return TypeD::make( SharedRuntime::dexp( d ) );
  1308 //=============================================================================
  1309 //------------------------------Value------------------------------------------
  1310 // Compute pow
  1311 const Type *PowDNode::Value( PhaseTransform *phase ) const {
  1312   const Type *t1 = phase->type( in(1) );
  1313   if( t1 == Type::TOP ) return Type::TOP;
  1314   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1315   const Type *t2 = phase->type( in(2) );
  1316   if( t2 == Type::TOP ) return Type::TOP;
  1317   if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
  1318   double d1 = t1->getd();
  1319   double d2 = t2->getd();
  1320   if( d1 < 0.0 ) return Type::DOUBLE;
  1321   if( d2 < 0.0 ) return Type::DOUBLE;
  1322   return TypeD::make( SharedRuntime::dpow( d1, d2 ) );

mercurial