src/share/vm/opto/subnode.cpp

Fri, 01 Aug 2008 10:06:45 -0700

author
kvn
date
Fri, 01 Aug 2008 10:06:45 -0700
changeset 728
c3e045194476
parent 670
9c2ecc2ffb12
child 731
ce93a51457ae
permissions
-rw-r--r--

6731641: assert(m->adr_type() == mach->adr_type(),"matcher should not change adr type")
Summary: fixed few addP node type and narrow oop type problems.
Reviewed-by: rasbold, never

     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-(B-C)" into (A+C)-B", since add is commutative and generally
   210   // nicer to optimize than subtract.
   211   if( op2 == Op_SubI && in2->outcnt() == 1) {
   212     Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) );
   213     return new (phase->C, 3) SubINode( add1, in2->in(1) );
   214   }
   216   return NULL;
   217 }
   219 //------------------------------sub--------------------------------------------
   220 // A subtract node differences it's two inputs.
   221 const Type *SubINode::sub( const Type *t1, const Type *t2 ) const {
   222   const TypeInt *r0 = t1->is_int(); // Handy access
   223   const TypeInt *r1 = t2->is_int();
   224   int32 lo = r0->_lo - r1->_hi;
   225   int32 hi = r0->_hi - r1->_lo;
   227   // We next check for 32-bit overflow.
   228   // If that happens, we just assume all integers are possible.
   229   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   230        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   231       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   232        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   233     return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   234   else                          // Overflow; assume all integers
   235     return TypeInt::INT;
   236 }
   238 //=============================================================================
   239 //------------------------------Ideal------------------------------------------
   240 Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   241   Node *in1 = in(1);
   242   Node *in2 = in(2);
   243   uint op1 = in1->Opcode();
   244   uint op2 = in2->Opcode();
   246 #ifdef ASSERT
   247   // Check for dead loop
   248   if( phase->eqv( in1, this ) || phase->eqv( in2, this ) ||
   249       ( op1 == Op_AddL || op1 == Op_SubL ) &&
   250       ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) ||
   251         phase->eqv( in1->in(1), in1  ) || phase->eqv( in1->in(2), in1  ) ) )
   252     assert(false, "dead loop in SubLNode::Ideal");
   253 #endif
   255   if( phase->type( in2 ) == Type::TOP ) return NULL;
   256   const TypeLong *i = phase->type( in2 )->isa_long();
   257   // Convert "x-c0" into "x+ -c0".
   258   if( i &&                      // Might be bottom or top...
   259       i->is_con() )
   260     return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con()));
   262   // Convert "(x+c0) - y" into (x-y) + c0"
   263   // Do not collapse (x+c0)-y if "+" is a loop increment or
   264   // if "y" is a loop induction variable.
   265   if( op1 == Op_AddL && ok_to_convert(in1, in2) ) {
   266     Node *in11 = in1->in(1);
   267     const Type *tadd = phase->type( in1->in(2) );
   268     if( tadd->singleton() && tadd != Type::TOP ) {
   269       Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 ));
   270       return new (phase->C, 3) AddLNode( sub2, in1->in(2) );
   271     }
   272   }
   274   // Convert "x - (y+c0)" into "(x-y) - c0"
   275   // Need the same check as in above optimization but reversed.
   276   if (op2 == Op_AddL && ok_to_convert(in2, in1)) {
   277     Node* in21 = in2->in(1);
   278     Node* in22 = in2->in(2);
   279     const TypeLong* tcon = phase->type(in22)->isa_long();
   280     if (tcon != NULL && tcon->is_con()) {
   281       Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) );
   282       Node* neg_c0 = phase->longcon(- tcon->get_con());
   283       return new (phase->C, 3) AddLNode(sub2, neg_c0);
   284     }
   285   }
   287   const Type *t1 = phase->type( in1 );
   288   if( t1 == Type::TOP ) return NULL;
   290 #ifdef ASSERT
   291   // Check for dead loop
   292   if( ( op2 == Op_AddL || op2 == Op_SubL ) &&
   293       ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) ||
   294         phase->eqv( in2->in(1), in2  ) || phase->eqv( in2->in(2), in2  ) ) )
   295     assert(false, "dead loop in SubLNode::Ideal");
   296 #endif
   298   // Convert "x - (x+y)" into "-y"
   299   if( op2 == Op_AddL &&
   300       phase->eqv( in1, in2->in(1) ) )
   301     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2));
   302   // Convert "x - (y+x)" into "-y"
   303   if( op2 == Op_AddL &&
   304       phase->eqv( in1, in2->in(2) ) )
   305     return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1));
   307   // Convert "0 - (x-y)" into "y-x"
   308   if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL )
   309     return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) );
   311   // Convert "(X+A) - (X+B)" into "A - B"
   312   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) )
   313     return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) );
   315   // Convert "(A+X) - (B+X)" into "A - B"
   316   if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) )
   317     return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) );
   319   // Convert "A-(B-C)" into (A+C)-B"
   320   if( op2 == Op_SubL && in2->outcnt() == 1) {
   321     Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) );
   322     return new (phase->C, 3) SubLNode( add1, in2->in(1) );
   323   }
   325   return NULL;
   326 }
   328 //------------------------------sub--------------------------------------------
   329 // A subtract node differences it's two inputs.
   330 const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const {
   331   const TypeLong *r0 = t1->is_long(); // Handy access
   332   const TypeLong *r1 = t2->is_long();
   333   jlong lo = r0->_lo - r1->_hi;
   334   jlong hi = r0->_hi - r1->_lo;
   336   // We next check for 32-bit overflow.
   337   // If that happens, we just assume all integers are possible.
   338   if( (((r0->_lo ^ r1->_hi) >= 0) ||    // lo ends have same signs OR
   339        ((r0->_lo ^      lo) >= 0)) &&   // lo results have same signs AND
   340       (((r0->_hi ^ r1->_lo) >= 0) ||    // hi ends have same signs OR
   341        ((r0->_hi ^      hi) >= 0)) )    // hi results have same signs
   342     return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen));
   343   else                          // Overflow; assume all integers
   344     return TypeLong::LONG;
   345 }
   347 //=============================================================================
   348 //------------------------------Value------------------------------------------
   349 // A subtract node differences its two inputs.
   350 const Type *SubFPNode::Value( PhaseTransform *phase ) const {
   351   const Node* in1 = in(1);
   352   const Node* in2 = in(2);
   353   // Either input is TOP ==> the result is TOP
   354   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   355   if( t1 == Type::TOP ) return Type::TOP;
   356   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   357   if( t2 == Type::TOP ) return Type::TOP;
   359   // if both operands are infinity of same sign, the result is NaN; do
   360   // not replace with zero
   361   if( (t1->is_finite() && t2->is_finite()) ) {
   362     if( phase->eqv(in1, in2) ) return add_id();
   363   }
   365   // Either input is BOTTOM ==> the result is the local BOTTOM
   366   const Type *bot = bottom_type();
   367   if( (t1 == bot) || (t2 == bot) ||
   368       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
   369     return bot;
   371   return sub(t1,t2);            // Local flavor of type subtraction
   372 }
   375 //=============================================================================
   376 //------------------------------Ideal------------------------------------------
   377 Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   378   const Type *t2 = phase->type( in(2) );
   379   // Convert "x-c0" into "x+ -c0".
   380   if( t2->base() == Type::FloatCon ) {  // Might be bottom or top...
   381     // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) );
   382   }
   384   // Not associative because of boundary conditions (infinity)
   385   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   386     // Convert "x - (x+y)" into "-y"
   387     if( in(2)->is_Add() &&
   388         phase->eqv(in(1),in(2)->in(1) ) )
   389       return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2));
   390   }
   392   // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes
   393   // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0.
   394   //if( phase->type(in(1)) == TypeF::ZERO )
   395   //return new (phase->C, 2) NegFNode(in(2));
   397   return NULL;
   398 }
   400 //------------------------------sub--------------------------------------------
   401 // A subtract node differences its two inputs.
   402 const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const {
   403   // no folding if one of operands is infinity or NaN, do not do constant folding
   404   if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) {
   405     return TypeF::make( t1->getf() - t2->getf() );
   406   }
   407   else if( g_isnan(t1->getf()) ) {
   408     return t1;
   409   }
   410   else if( g_isnan(t2->getf()) ) {
   411     return t2;
   412   }
   413   else {
   414     return Type::FLOAT;
   415   }
   416 }
   418 //=============================================================================
   419 //------------------------------Ideal------------------------------------------
   420 Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   421   const Type *t2 = phase->type( in(2) );
   422   // Convert "x-c0" into "x+ -c0".
   423   if( t2->base() == Type::DoubleCon ) { // Might be bottom or top...
   424     // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) );
   425   }
   427   // Not associative because of boundary conditions (infinity)
   428   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
   429     // Convert "x - (x+y)" into "-y"
   430     if( in(2)->is_Add() &&
   431         phase->eqv(in(1),in(2)->in(1) ) )
   432       return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2));
   433   }
   435   // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes
   436   // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0.
   437   //if( phase->type(in(1)) == TypeD::ZERO )
   438   //return new (phase->C, 2) NegDNode(in(2));
   440   return NULL;
   441 }
   443 //------------------------------sub--------------------------------------------
   444 // A subtract node differences its two inputs.
   445 const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const {
   446   // no folding if one of operands is infinity or NaN, do not do constant folding
   447   if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) {
   448     return TypeD::make( t1->getd() - t2->getd() );
   449   }
   450   else if( g_isnan(t1->getd()) ) {
   451     return t1;
   452   }
   453   else if( g_isnan(t2->getd()) ) {
   454     return t2;
   455   }
   456   else {
   457     return Type::DOUBLE;
   458   }
   459 }
   461 //=============================================================================
   462 //------------------------------Idealize---------------------------------------
   463 // Unlike SubNodes, compare must still flatten return value to the
   464 // range -1, 0, 1.
   465 // And optimizations like those for (X + Y) - X fail if overflow happens.
   466 Node *CmpNode::Identity( PhaseTransform *phase ) {
   467   return this;
   468 }
   470 //=============================================================================
   471 //------------------------------cmp--------------------------------------------
   472 // Simplify a CmpI (compare 2 integers) node, based on local information.
   473 // If both inputs are constants, compare them.
   474 const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const {
   475   const TypeInt *r0 = t1->is_int(); // Handy access
   476   const TypeInt *r1 = t2->is_int();
   478   if( r0->_hi < r1->_lo )       // Range is always low?
   479     return TypeInt::CC_LT;
   480   else if( r0->_lo > r1->_hi )  // Range is always high?
   481     return TypeInt::CC_GT;
   483   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   484     assert(r0->get_con() == r1->get_con(), "must be equal");
   485     return TypeInt::CC_EQ;      // Equal results.
   486   } else if( r0->_hi == r1->_lo ) // Range is never high?
   487     return TypeInt::CC_LE;
   488   else if( r0->_lo == r1->_hi ) // Range is never low?
   489     return TypeInt::CC_GE;
   490   return TypeInt::CC;           // else use worst case results
   491 }
   493 // Simplify a CmpU (compare 2 integers) node, based on local information.
   494 // If both inputs are constants, compare them.
   495 const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const {
   496   assert(!t1->isa_ptr(), "obsolete usage of CmpU");
   498   // comparing two unsigned ints
   499   const TypeInt *r0 = t1->is_int();   // Handy access
   500   const TypeInt *r1 = t2->is_int();
   502   // Current installed version
   503   // Compare ranges for non-overlap
   504   juint lo0 = r0->_lo;
   505   juint hi0 = r0->_hi;
   506   juint lo1 = r1->_lo;
   507   juint hi1 = r1->_hi;
   509   // If either one has both negative and positive values,
   510   // it therefore contains both 0 and -1, and since [0..-1] is the
   511   // full unsigned range, the type must act as an unsigned bottom.
   512   bool bot0 = ((jint)(lo0 ^ hi0) < 0);
   513   bool bot1 = ((jint)(lo1 ^ hi1) < 0);
   515   if (bot0 || bot1) {
   516     // All unsigned values are LE -1 and GE 0.
   517     if (lo0 == 0 && hi0 == 0) {
   518       return TypeInt::CC_LE;            //   0 <= bot
   519     } else if (lo1 == 0 && hi1 == 0) {
   520       return TypeInt::CC_GE;            // bot >= 0
   521     }
   522   } else {
   523     // We can use ranges of the form [lo..hi] if signs are the same.
   524     assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid");
   525     // results are reversed, '-' > '+' for unsigned compare
   526     if (hi0 < lo1) {
   527       return TypeInt::CC_LT;            // smaller
   528     } else if (lo0 > hi1) {
   529       return TypeInt::CC_GT;            // greater
   530     } else if (hi0 == lo1 && lo0 == hi1) {
   531       return TypeInt::CC_EQ;            // Equal results
   532     } else if (lo0 >= hi1) {
   533       return TypeInt::CC_GE;
   534     } else if (hi0 <= lo1) {
   535       // Check for special case in Hashtable::get.  (See below.)
   536       if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   537           in(1)->Opcode() == Op_ModI &&
   538           in(1)->in(2) == in(2) )
   539         return TypeInt::CC_LT;
   540       return TypeInt::CC_LE;
   541     }
   542   }
   543   // Check for special case in Hashtable::get - the hash index is
   544   // mod'ed to the table size so the following range check is useless.
   545   // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have
   546   // to be positive.
   547   // (This is a gross hack, since the sub method never
   548   // looks at the structure of the node in any other case.)
   549   if ((jint)lo0 >= 0 && (jint)lo1 >= 0 &&
   550       in(1)->Opcode() == Op_ModI &&
   551       in(1)->in(2)->uncast() == in(2)->uncast())
   552     return TypeInt::CC_LT;
   553   return TypeInt::CC;                   // else use worst case results
   554 }
   556 //------------------------------Idealize---------------------------------------
   557 Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   558   if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) {
   559     switch (in(1)->Opcode()) {
   560     case Op_CmpL3:              // Collapse a CmpL3/CmpI into a CmpL
   561       return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2));
   562     case Op_CmpF3:              // Collapse a CmpF3/CmpI into a CmpF
   563       return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2));
   564     case Op_CmpD3:              // Collapse a CmpD3/CmpI into a CmpD
   565       return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2));
   566     //case Op_SubI:
   567       // If (x - y) cannot overflow, then ((x - y) <?> 0)
   568       // can be turned into (x <?> y).
   569       // This is handled (with more general cases) by Ideal_sub_algebra.
   570     }
   571   }
   572   return NULL;                  // No change
   573 }
   576 //=============================================================================
   577 // Simplify a CmpL (compare 2 longs ) node, based on local information.
   578 // If both inputs are constants, compare them.
   579 const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const {
   580   const TypeLong *r0 = t1->is_long(); // Handy access
   581   const TypeLong *r1 = t2->is_long();
   583   if( r0->_hi < r1->_lo )       // Range is always low?
   584     return TypeInt::CC_LT;
   585   else if( r0->_lo > r1->_hi )  // Range is always high?
   586     return TypeInt::CC_GT;
   588   else if( r0->is_con() && r1->is_con() ) { // comparing constants?
   589     assert(r0->get_con() == r1->get_con(), "must be equal");
   590     return TypeInt::CC_EQ;      // Equal results.
   591   } else if( r0->_hi == r1->_lo ) // Range is never high?
   592     return TypeInt::CC_LE;
   593   else if( r0->_lo == r1->_hi ) // Range is never low?
   594     return TypeInt::CC_GE;
   595   return TypeInt::CC;           // else use worst case results
   596 }
   598 //=============================================================================
   599 //------------------------------sub--------------------------------------------
   600 // Simplify an CmpP (compare 2 pointers) node, based on local information.
   601 // If both inputs are constants, compare them.
   602 const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const {
   603   const TypePtr *r0 = t1->is_ptr(); // Handy access
   604   const TypePtr *r1 = t2->is_ptr();
   606   // Undefined inputs makes for an undefined result
   607   if( TypePtr::above_centerline(r0->_ptr) ||
   608       TypePtr::above_centerline(r1->_ptr) )
   609     return Type::TOP;
   611   if (r0 == r1 && r0->singleton()) {
   612     // Equal pointer constants (klasses, nulls, etc.)
   613     return TypeInt::CC_EQ;
   614   }
   616   // See if it is 2 unrelated classes.
   617   const TypeOopPtr* p0 = r0->isa_oopptr();
   618   const TypeOopPtr* p1 = r1->isa_oopptr();
   619   if (p0 && p1) {
   620     Node* in1 = in(1)->uncast();
   621     Node* in2 = in(2)->uncast();
   622     AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL);
   623     AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL);
   624     if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) {
   625       return TypeInt::CC_GT;  // different pointers
   626     }
   627     ciKlass* klass0 = p0->klass();
   628     bool    xklass0 = p0->klass_is_exact();
   629     ciKlass* klass1 = p1->klass();
   630     bool    xklass1 = p1->klass_is_exact();
   631     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
   632     if (klass0 && klass1 &&
   633         kps != 1 &&             // both or neither are klass pointers
   634         !klass0->is_interface() && // do not trust interfaces
   635         !klass1->is_interface()) {
   636       // See if neither subclasses the other, or if the class on top
   637       // is precise.  In either of these cases, the compare must fail.
   638       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
   639           !klass0->is_java_klass() ||   // types not part of Java language?
   640           !klass1->is_java_klass()) {   // types not part of Java language?
   641         // Do nothing; we know nothing for imprecise types
   642       } else if (klass0->is_subtype_of(klass1)) {
   643         // If klass1's type is PRECISE, then we can fail.
   644         if (xklass1)  return TypeInt::CC_GT;
   645       } else if (klass1->is_subtype_of(klass0)) {
   646         // If klass0's type is PRECISE, then we can fail.
   647         if (xklass0)  return TypeInt::CC_GT;
   648       } else {                  // Neither subtypes the other
   649         return TypeInt::CC_GT;  // ...so always fail
   650       }
   651     }
   652   }
   654   // Known constants can be compared exactly
   655   // Null can be distinguished from any NotNull pointers
   656   // Unknown inputs makes an unknown result
   657   if( r0->singleton() ) {
   658     intptr_t bits0 = r0->get_con();
   659     if( r1->singleton() )
   660       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
   661     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   662   } else if( r1->singleton() ) {
   663     intptr_t bits1 = r1->get_con();
   664     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   665   } else
   666     return TypeInt::CC;
   667 }
   669 //------------------------------Ideal------------------------------------------
   670 // Check for the case of comparing an unknown klass loaded from the primary
   671 // super-type array vs a known klass with no subtypes.  This amounts to
   672 // checking to see an unknown klass subtypes a known klass with no subtypes;
   673 // this only happens on an exact match.  We can shorten this test by 1 load.
   674 Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   675   // Constant pointer on right?
   676   const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr();
   677   if (t2 == NULL || !t2->klass_is_exact())
   678     return NULL;
   679   // Get the constant klass we are comparing to.
   680   ciKlass* superklass = t2->klass();
   682   // Now check for LoadKlass on left.
   683   Node* ldk1 = in(1);
   684   if (ldk1->is_DecodeN()) {
   685     ldk1 = ldk1->in(1);
   686     if (ldk1->Opcode() != Op_LoadNKlass )
   687       return NULL;
   688   } else if (ldk1->Opcode() != Op_LoadKlass )
   689     return NULL;
   690   // Take apart the address of the LoadKlass:
   691   Node* adr1 = ldk1->in(MemNode::Address);
   692   intptr_t con2 = 0;
   693   Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2);
   694   if (ldk2 == NULL)
   695     return NULL;
   696   if (con2 == oopDesc::klass_offset_in_bytes()) {
   697     // We are inspecting an object's concrete class.
   698     // Short-circuit the check if the query is abstract.
   699     if (superklass->is_interface() ||
   700         superklass->is_abstract()) {
   701       // Make it come out always false:
   702       this->set_req(2, phase->makecon(TypePtr::NULL_PTR));
   703       return this;
   704     }
   705   }
   707   // Check for a LoadKlass from primary supertype array.
   708   // Any nested loadklass from loadklass+con must be from the p.s. array.
   709   if (ldk2->is_DecodeN()) {
   710     // Keep ldk2 as DecodeN since it could be used in CmpP below.
   711     if (ldk2->in(1)->Opcode() != Op_LoadNKlass )
   712       return NULL;
   713   } else if (ldk2->Opcode() != Op_LoadKlass)
   714     return NULL;
   716   // Verify that we understand the situation
   717   if (con2 != (intptr_t) superklass->super_check_offset())
   718     return NULL;                // Might be element-klass loading from array klass
   720   // If 'superklass' has no subklasses and is not an interface, then we are
   721   // assured that the only input which will pass the type check is
   722   // 'superklass' itself.
   723   //
   724   // We could be more liberal here, and allow the optimization on interfaces
   725   // which have a single implementor.  This would require us to increase the
   726   // expressiveness of the add_dependency() mechanism.
   727   // %%% Do this after we fix TypeOopPtr:  Deps are expressive enough now.
   729   // Object arrays must have their base element have no subtypes
   730   while (superklass->is_obj_array_klass()) {
   731     ciType* elem = superklass->as_obj_array_klass()->element_type();
   732     superklass = elem->as_klass();
   733   }
   734   if (superklass->is_instance_klass()) {
   735     ciInstanceKlass* ik = superklass->as_instance_klass();
   736     if (ik->has_subklass() || ik->is_interface())  return NULL;
   737     // Add a dependency if there is a chance that a subclass will be added later.
   738     if (!ik->is_final()) {
   739       phase->C->dependencies()->assert_leaf_type(ik);
   740     }
   741   }
   743   // Bypass the dependent load, and compare directly
   744   this->set_req(1,ldk2);
   746   return this;
   747 }
   749 //=============================================================================
   750 //------------------------------sub--------------------------------------------
   751 // Simplify an CmpN (compare 2 pointers) node, based on local information.
   752 // If both inputs are constants, compare them.
   753 const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const {
   754   const TypePtr *r0 = t1->make_ptr(); // Handy access
   755   const TypePtr *r1 = t2->make_ptr();
   757   // Undefined inputs makes for an undefined result
   758   if( TypePtr::above_centerline(r0->_ptr) ||
   759       TypePtr::above_centerline(r1->_ptr) )
   760     return Type::TOP;
   762   if (r0 == r1 && r0->singleton()) {
   763     // Equal pointer constants (klasses, nulls, etc.)
   764     return TypeInt::CC_EQ;
   765   }
   767   // See if it is 2 unrelated classes.
   768   const TypeOopPtr* p0 = r0->isa_oopptr();
   769   const TypeOopPtr* p1 = r1->isa_oopptr();
   770   if (p0 && p1) {
   771     ciKlass* klass0 = p0->klass();
   772     bool    xklass0 = p0->klass_is_exact();
   773     ciKlass* klass1 = p1->klass();
   774     bool    xklass1 = p1->klass_is_exact();
   775     int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0);
   776     if (klass0 && klass1 &&
   777         kps != 1 &&             // both or neither are klass pointers
   778         !klass0->is_interface() && // do not trust interfaces
   779         !klass1->is_interface()) {
   780       // See if neither subclasses the other, or if the class on top
   781       // is precise.  In either of these cases, the compare must fail.
   782       if (klass0->equals(klass1)   ||   // if types are unequal but klasses are
   783           !klass0->is_java_klass() ||   // types not part of Java language?
   784           !klass1->is_java_klass()) {   // types not part of Java language?
   785         // Do nothing; we know nothing for imprecise types
   786       } else if (klass0->is_subtype_of(klass1)) {
   787         // If klass1's type is PRECISE, then we can fail.
   788         if (xklass1)  return TypeInt::CC_GT;
   789       } else if (klass1->is_subtype_of(klass0)) {
   790         // If klass0's type is PRECISE, then we can fail.
   791         if (xklass0)  return TypeInt::CC_GT;
   792       } else {                  // Neither subtypes the other
   793         return TypeInt::CC_GT;  // ...so always fail
   794       }
   795     }
   796   }
   798   // Known constants can be compared exactly
   799   // Null can be distinguished from any NotNull pointers
   800   // Unknown inputs makes an unknown result
   801   if( r0->singleton() ) {
   802     intptr_t bits0 = r0->get_con();
   803     if( r1->singleton() )
   804       return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT;
   805     return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   806   } else if( r1->singleton() ) {
   807     intptr_t bits1 = r1->get_con();
   808     return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC;
   809   } else
   810     return TypeInt::CC;
   811 }
   813 //------------------------------Ideal------------------------------------------
   814 Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) {
   815   return NULL;
   816 }
   818 //=============================================================================
   819 //------------------------------Value------------------------------------------
   820 // Simplify an CmpF (compare 2 floats ) node, based on local information.
   821 // If both inputs are constants, compare them.
   822 const Type *CmpFNode::Value( PhaseTransform *phase ) const {
   823   const Node* in1 = in(1);
   824   const Node* in2 = in(2);
   825   // Either input is TOP ==> the result is TOP
   826   const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1);
   827   if( t1 == Type::TOP ) return Type::TOP;
   828   const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2);
   829   if( t2 == Type::TOP ) return Type::TOP;
   831   // Not constants?  Don't know squat - even if they are the same
   832   // value!  If they are NaN's they compare to LT instead of EQ.
   833   const TypeF *tf1 = t1->isa_float_constant();
   834   const TypeF *tf2 = t2->isa_float_constant();
   835   if( !tf1 || !tf2 ) return TypeInt::CC;
   837   // This implements the Java bytecode fcmpl, so unordered returns -1.
   838   if( tf1->is_nan() || tf2->is_nan() )
   839     return TypeInt::CC_LT;
   841   if( tf1->_f < tf2->_f ) return TypeInt::CC_LT;
   842   if( tf1->_f > tf2->_f ) return TypeInt::CC_GT;
   843   assert( tf1->_f == tf2->_f, "do not understand FP behavior" );
   844   return TypeInt::CC_EQ;
   845 }
   848 //=============================================================================
   849 //------------------------------Value------------------------------------------
   850 // Simplify an CmpD (compare 2 doubles ) node, based on local information.
   851 // If both inputs are constants, compare them.
   852 const Type *CmpDNode::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 TypeD *td1 = t1->isa_double_constant();
   864   const TypeD *td2 = t2->isa_double_constant();
   865   if( !td1 || !td2 ) return TypeInt::CC;
   867   // This implements the Java bytecode dcmpl, so unordered returns -1.
   868   if( td1->is_nan() || td2->is_nan() )
   869     return TypeInt::CC_LT;
   871   if( td1->_d < td2->_d ) return TypeInt::CC_LT;
   872   if( td1->_d > td2->_d ) return TypeInt::CC_GT;
   873   assert( td1->_d == td2->_d, "do not understand FP behavior" );
   874   return TypeInt::CC_EQ;
   875 }
   877 //------------------------------Ideal------------------------------------------
   878 Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){
   879   // Check if we can change this to a CmpF and remove a ConvD2F operation.
   880   // Change  (CMPD (F2D (float)) (ConD value))
   881   // To      (CMPF      (float)  (ConF value))
   882   // Valid when 'value' does not lose precision as a float.
   883   // Benefits: eliminates conversion, does not require 24-bit mode
   885   // NaNs prevent commuting operands.  This transform works regardless of the
   886   // order of ConD and ConvF2D inputs by preserving the original order.
   887   int idx_f2d = 1;              // ConvF2D on left side?
   888   if( in(idx_f2d)->Opcode() != Op_ConvF2D )
   889     idx_f2d = 2;                // No, swap to check for reversed args
   890   int idx_con = 3-idx_f2d;      // Check for the constant on other input
   892   if( ConvertCmpD2CmpF &&
   893       in(idx_f2d)->Opcode() == Op_ConvF2D &&
   894       in(idx_con)->Opcode() == Op_ConD ) {
   895     const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant();
   896     double t2_value_as_double = t2->_d;
   897     float  t2_value_as_float  = (float)t2_value_as_double;
   898     if( t2_value_as_double == (double)t2_value_as_float ) {
   899       // Test value can be represented as a float
   900       // Eliminate the conversion to double and create new comparison
   901       Node *new_in1 = in(idx_f2d)->in(1);
   902       Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) );
   903       if( idx_f2d != 1 ) {      // Must flip args to match original order
   904         Node *tmp = new_in1;
   905         new_in1 = new_in2;
   906         new_in2 = tmp;
   907       }
   908       CmpFNode *new_cmp = (Opcode() == Op_CmpD3)
   909         ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 )
   910         : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ;
   911       return new_cmp;           // Changed to CmpFNode
   912     }
   913     // Testing value required the precision of a double
   914   }
   915   return NULL;                  // No change
   916 }
   919 //=============================================================================
   920 //------------------------------cc2logical-------------------------------------
   921 // Convert a condition code type to a logical type
   922 const Type *BoolTest::cc2logical( const Type *CC ) const {
   923   if( CC == Type::TOP ) return Type::TOP;
   924   if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse
   925   const TypeInt *ti = CC->is_int();
   926   if( ti->is_con() ) {          // Only 1 kind of condition codes set?
   927     // Match low order 2 bits
   928     int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0;
   929     if( _test & 4 ) tmp = 1-tmp;     // Optionally complement result
   930     return TypeInt::make(tmp);       // Boolean result
   931   }
   933   if( CC == TypeInt::CC_GE ) {
   934     if( _test == ge ) return TypeInt::ONE;
   935     if( _test == lt ) return TypeInt::ZERO;
   936   }
   937   if( CC == TypeInt::CC_LE ) {
   938     if( _test == le ) return TypeInt::ONE;
   939     if( _test == gt ) return TypeInt::ZERO;
   940   }
   942   return TypeInt::BOOL;
   943 }
   945 //------------------------------dump_spec-------------------------------------
   946 // Print special per-node info
   947 #ifndef PRODUCT
   948 void BoolTest::dump_on(outputStream *st) const {
   949   const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"};
   950   st->print(msg[_test]);
   951 }
   952 #endif
   954 //=============================================================================
   955 uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); }
   956 uint BoolNode::size_of() const { return sizeof(BoolNode); }
   958 //------------------------------operator==-------------------------------------
   959 uint BoolNode::cmp( const Node &n ) const {
   960   const BoolNode *b = (const BoolNode *)&n; // Cast up
   961   return (_test._test == b->_test._test);
   962 }
   964 //------------------------------clone_cmp--------------------------------------
   965 // Clone a compare/bool tree
   966 static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) {
   967   Node *ncmp = cmp->clone();
   968   ncmp->set_req(1,cmp1);
   969   ncmp->set_req(2,cmp2);
   970   ncmp = gvn->transform( ncmp );
   971   return new (gvn->C, 2) BoolNode( ncmp, test );
   972 }
   974 //-------------------------------make_predicate--------------------------------
   975 Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) {
   976   if (test_value->is_Con())   return test_value;
   977   if (test_value->is_Bool())  return test_value;
   978   Compile* C = phase->C;
   979   if (test_value->is_CMove() &&
   980       test_value->in(CMoveNode::Condition)->is_Bool()) {
   981     BoolNode*   bol   = test_value->in(CMoveNode::Condition)->as_Bool();
   982     const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse));
   983     const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue));
   984     if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) {
   985       return bol;
   986     } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) {
   987       return phase->transform( bol->negate(phase) );
   988     }
   989     // Else fall through.  The CMove gets in the way of the test.
   990     // It should be the case that make_predicate(bol->as_int_value()) == bol.
   991   }
   992   Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0));
   993   cmp = phase->transform(cmp);
   994   Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne);
   995   return phase->transform(bol);
   996 }
   998 //--------------------------------as_int_value---------------------------------
   999 Node* BoolNode::as_int_value(PhaseGVN* phase) {
  1000   // Inverse to make_predicate.  The CMove probably boils down to a Conv2B.
  1001   Node* cmov = CMoveNode::make(phase->C, NULL, this,
  1002                                phase->intcon(0), phase->intcon(1),
  1003                                TypeInt::BOOL);
  1004   return phase->transform(cmov);
  1007 //----------------------------------negate-------------------------------------
  1008 BoolNode* BoolNode::negate(PhaseGVN* phase) {
  1009   Compile* C = phase->C;
  1010   return new (C, 2) BoolNode(in(1), _test.negate());
  1014 //------------------------------Ideal------------------------------------------
  1015 Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1016   // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)".
  1017   // This moves the constant to the right.  Helps value-numbering.
  1018   Node *cmp = in(1);
  1019   if( !cmp->is_Sub() ) return NULL;
  1020   int cop = cmp->Opcode();
  1021   if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL;
  1022   Node *cmp1 = cmp->in(1);
  1023   Node *cmp2 = cmp->in(2);
  1024   if( !cmp1 ) return NULL;
  1026   // Constant on left?
  1027   Node *con = cmp1;
  1028   uint op2 = cmp2->Opcode();
  1029   // Move constants to the right of compare's to canonicalize.
  1030   // Do not muck with Opaque1 nodes, as this indicates a loop
  1031   // guard that cannot change shape.
  1032   if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 &&
  1033       // Because of NaN's, CmpD and CmpF are not commutative
  1034       cop != Op_CmpD && cop != Op_CmpF &&
  1035       // Protect against swapping inputs to a compare when it is used by a
  1036       // counted loop exit, which requires maintaining the loop-limit as in(2)
  1037       !is_counted_loop_exit_test() ) {
  1038     // Ok, commute the constant to the right of the cmp node.
  1039     // Clone the Node, getting a new Node of the same class
  1040     cmp = cmp->clone();
  1041     // Swap inputs to the clone
  1042     cmp->swap_edges(1, 2);
  1043     cmp = phase->transform( cmp );
  1044     return new (phase->C, 2) BoolNode( cmp, _test.commute() );
  1047   // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)".
  1048   // The XOR-1 is an idiom used to flip the sense of a bool.  We flip the
  1049   // test instead.
  1050   int cmp1_op = cmp1->Opcode();
  1051   const TypeInt* cmp2_type = phase->type(cmp2)->isa_int();
  1052   if (cmp2_type == NULL)  return NULL;
  1053   Node* j_xor = cmp1;
  1054   if( cmp2_type == TypeInt::ZERO &&
  1055       cmp1_op == Op_XorI &&
  1056       j_xor->in(1) != j_xor &&          // An xor of itself is dead
  1057       phase->type( j_xor->in(2) ) == TypeInt::ONE &&
  1058       (_test._test == BoolTest::eq ||
  1059        _test._test == BoolTest::ne) ) {
  1060     Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2));
  1061     return new (phase->C, 2) BoolNode( ncmp, _test.negate() );
  1064   // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)".
  1065   // This is a standard idiom for branching on a boolean value.
  1066   Node *c2b = cmp1;
  1067   if( cmp2_type == TypeInt::ZERO &&
  1068       cmp1_op == Op_Conv2B &&
  1069       (_test._test == BoolTest::eq ||
  1070        _test._test == BoolTest::ne) ) {
  1071     Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int()
  1072        ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2)
  1073        : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR))
  1074     );
  1075     return new (phase->C, 2) BoolNode( ncmp, _test._test );
  1078   // Comparing a SubI against a zero is equal to comparing the SubI
  1079   // arguments directly.  This only works for eq and ne comparisons
  1080   // due to possible integer overflow.
  1081   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
  1082         (cop == Op_CmpI) &&
  1083         (cmp1->Opcode() == Op_SubI) &&
  1084         ( cmp2_type == TypeInt::ZERO ) ) {
  1085     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2)));
  1086     return new (phase->C, 2) BoolNode( ncmp, _test._test );
  1089   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
  1090   // most general case because negating 0x80000000 does nothing.  Needed for
  1091   // the CmpF3/SubI/CmpI idiom.
  1092   if( cop == Op_CmpI &&
  1093       cmp1->Opcode() == Op_SubI &&
  1094       cmp2_type == TypeInt::ZERO &&
  1095       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
  1096       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
  1097     Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2));
  1098     return new (phase->C, 2) BoolNode( ncmp, _test.commute() );
  1101   //  The transformation below is not valid for either signed or unsigned
  1102   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
  1103   //  This transformation can be resurrected when we are able to
  1104   //  make inferences about the range of values being subtracted from
  1105   //  (or added to) relative to the wraparound point.
  1106   //
  1107   //    // Remove +/-1's if possible.
  1108   //    // "X <= Y-1" becomes "X <  Y"
  1109   //    // "X+1 <= Y" becomes "X <  Y"
  1110   //    // "X <  Y+1" becomes "X <= Y"
  1111   //    // "X-1 <  Y" becomes "X <= Y"
  1112   //    // Do not this to compares off of the counted-loop-end.  These guys are
  1113   //    // checking the trip counter and they want to use the post-incremented
  1114   //    // counter.  If they use the PRE-incremented counter, then the counter has
  1115   //    // to be incremented in a private block on a loop backedge.
  1116   //    if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd )
  1117   //      return NULL;
  1118   //  #ifndef PRODUCT
  1119   //    // Do not do this in a wash GVN pass during verification.
  1120   //    // Gets triggered by too many simple optimizations to be bothered with
  1121   //    // re-trying it again and again.
  1122   //    if( !phase->allow_progress() ) return NULL;
  1123   //  #endif
  1124   //    // Not valid for unsigned compare because of corner cases in involving zero.
  1125   //    // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an
  1126   //    // exception in case X is 0 (because 0-1 turns into 4billion unsigned but
  1127   //    // "0 <=u Y" is always true).
  1128   //    if( cmp->Opcode() == Op_CmpU ) return NULL;
  1129   //    int cmp2_op = cmp2->Opcode();
  1130   //    if( _test._test == BoolTest::le ) {
  1131   //      if( cmp1_op == Op_AddI &&
  1132   //          phase->type( cmp1->in(2) ) == TypeInt::ONE )
  1133   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt );
  1134   //      else if( cmp2_op == Op_AddI &&
  1135   //         phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 )
  1136   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt );
  1137   //    } else if( _test._test == BoolTest::lt ) {
  1138   //      if( cmp1_op == Op_AddI &&
  1139   //          phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 )
  1140   //        return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le );
  1141   //      else if( cmp2_op == Op_AddI &&
  1142   //         phase->type( cmp2->in(2) ) == TypeInt::ONE )
  1143   //        return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le );
  1144   //    }
  1146   return NULL;
  1149 //------------------------------Value------------------------------------------
  1150 // Simplify a Bool (convert condition codes to boolean (1 or 0)) node,
  1151 // based on local information.   If the input is constant, do it.
  1152 const Type *BoolNode::Value( PhaseTransform *phase ) const {
  1153   return _test.cc2logical( phase->type( in(1) ) );
  1156 //------------------------------dump_spec--------------------------------------
  1157 // Dump special per-node info
  1158 #ifndef PRODUCT
  1159 void BoolNode::dump_spec(outputStream *st) const {
  1160   st->print("[");
  1161   _test.dump_on(st);
  1162   st->print("]");
  1164 #endif
  1166 //------------------------------is_counted_loop_exit_test--------------------------------------
  1167 // Returns true if node is used by a counted loop node.
  1168 bool BoolNode::is_counted_loop_exit_test() {
  1169   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
  1170     Node* use = fast_out(i);
  1171     if (use->is_CountedLoopEnd()) {
  1172       return true;
  1175   return false;
  1178 //=============================================================================
  1179 //------------------------------NegNode----------------------------------------
  1180 Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1181   if( in(1)->Opcode() == Op_SubF )
  1182     return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) );
  1183   return NULL;
  1186 Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
  1187   if( in(1)->Opcode() == Op_SubD )
  1188     return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) );
  1189   return NULL;
  1193 //=============================================================================
  1194 //------------------------------Value------------------------------------------
  1195 // Compute sqrt
  1196 const Type *SqrtDNode::Value( PhaseTransform *phase ) const {
  1197   const Type *t1 = phase->type( in(1) );
  1198   if( t1 == Type::TOP ) return Type::TOP;
  1199   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1200   double d = t1->getd();
  1201   if( d < 0.0 ) return Type::DOUBLE;
  1202   return TypeD::make( sqrt( d ) );
  1205 //=============================================================================
  1206 //------------------------------Value------------------------------------------
  1207 // Compute cos
  1208 const Type *CosDNode::Value( PhaseTransform *phase ) const {
  1209   const Type *t1 = phase->type( in(1) );
  1210   if( t1 == Type::TOP ) return Type::TOP;
  1211   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1212   double d = t1->getd();
  1213   if( d < 0.0 ) return Type::DOUBLE;
  1214   return TypeD::make( SharedRuntime::dcos( d ) );
  1217 //=============================================================================
  1218 //------------------------------Value------------------------------------------
  1219 // Compute sin
  1220 const Type *SinDNode::Value( PhaseTransform *phase ) const {
  1221   const Type *t1 = phase->type( in(1) );
  1222   if( t1 == Type::TOP ) return Type::TOP;
  1223   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1224   double d = t1->getd();
  1225   if( d < 0.0 ) return Type::DOUBLE;
  1226   return TypeD::make( SharedRuntime::dsin( d ) );
  1229 //=============================================================================
  1230 //------------------------------Value------------------------------------------
  1231 // Compute tan
  1232 const Type *TanDNode::Value( PhaseTransform *phase ) const {
  1233   const Type *t1 = phase->type( in(1) );
  1234   if( t1 == Type::TOP ) return Type::TOP;
  1235   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1236   double d = t1->getd();
  1237   if( d < 0.0 ) return Type::DOUBLE;
  1238   return TypeD::make( SharedRuntime::dtan( d ) );
  1241 //=============================================================================
  1242 //------------------------------Value------------------------------------------
  1243 // Compute log
  1244 const Type *LogDNode::Value( PhaseTransform *phase ) const {
  1245   const Type *t1 = phase->type( in(1) );
  1246   if( t1 == Type::TOP ) return Type::TOP;
  1247   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1248   double d = t1->getd();
  1249   if( d < 0.0 ) return Type::DOUBLE;
  1250   return TypeD::make( SharedRuntime::dlog( d ) );
  1253 //=============================================================================
  1254 //------------------------------Value------------------------------------------
  1255 // Compute log10
  1256 const Type *Log10DNode::Value( PhaseTransform *phase ) const {
  1257   const Type *t1 = phase->type( in(1) );
  1258   if( t1 == Type::TOP ) return Type::TOP;
  1259   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1260   double d = t1->getd();
  1261   if( d < 0.0 ) return Type::DOUBLE;
  1262   return TypeD::make( SharedRuntime::dlog10( d ) );
  1265 //=============================================================================
  1266 //------------------------------Value------------------------------------------
  1267 // Compute exp
  1268 const Type *ExpDNode::Value( PhaseTransform *phase ) const {
  1269   const Type *t1 = phase->type( in(1) );
  1270   if( t1 == Type::TOP ) return Type::TOP;
  1271   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1272   double d = t1->getd();
  1273   if( d < 0.0 ) return Type::DOUBLE;
  1274   return TypeD::make( SharedRuntime::dexp( d ) );
  1278 //=============================================================================
  1279 //------------------------------Value------------------------------------------
  1280 // Compute pow
  1281 const Type *PowDNode::Value( PhaseTransform *phase ) const {
  1282   const Type *t1 = phase->type( in(1) );
  1283   if( t1 == Type::TOP ) return Type::TOP;
  1284   if( t1->base() != Type::DoubleCon ) return Type::DOUBLE;
  1285   const Type *t2 = phase->type( in(2) );
  1286   if( t2 == Type::TOP ) return Type::TOP;
  1287   if( t2->base() != Type::DoubleCon ) return Type::DOUBLE;
  1288   double d1 = t1->getd();
  1289   double d2 = t2->getd();
  1290   if( d1 < 0.0 ) return Type::DOUBLE;
  1291   if( d2 < 0.0 ) return Type::DOUBLE;
  1292   return TypeD::make( SharedRuntime::dpow( d1, d2 ) );

mercurial